magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <string> |
| 12 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 13 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 14 | #include "webrtc/test/gtest.h" |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | TEST(TestI420BufferPool, SimpleFrameReuse) { |
| 19 | I420BufferPool pool; |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 20 | rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 21 | EXPECT_EQ(16, buffer->width()); |
| 22 | EXPECT_EQ(16, buffer->height()); |
| 23 | // Extract non-refcounted pointers for testing. |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 24 | const uint8_t* y_ptr = buffer->DataY(); |
| 25 | const uint8_t* u_ptr = buffer->DataU(); |
| 26 | const uint8_t* v_ptr = buffer->DataV(); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 27 | // Release buffer so that it is returned to the pool. |
| 28 | buffer = nullptr; |
| 29 | // Check that the memory is resued. |
| 30 | buffer = pool.CreateBuffer(16, 16); |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 31 | EXPECT_EQ(y_ptr, buffer->DataY()); |
| 32 | EXPECT_EQ(u_ptr, buffer->DataU()); |
| 33 | EXPECT_EQ(v_ptr, buffer->DataV()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 34 | EXPECT_EQ(16, buffer->width()); |
| 35 | EXPECT_EQ(16, buffer->height()); |
| 36 | } |
| 37 | |
| 38 | TEST(TestI420BufferPool, FailToReuse) { |
| 39 | I420BufferPool pool; |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 40 | rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 41 | // Extract non-refcounted pointers for testing. |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 42 | const uint8_t* u_ptr = buffer->DataU(); |
| 43 | const uint8_t* v_ptr = buffer->DataV(); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 44 | // Release buffer so that it is returned to the pool. |
| 45 | buffer = nullptr; |
| 46 | // Check that the pool doesn't try to reuse buffers of incorrect size. |
| 47 | buffer = pool.CreateBuffer(32, 16); |
| 48 | EXPECT_EQ(32, buffer->width()); |
| 49 | EXPECT_EQ(16, buffer->height()); |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 50 | EXPECT_NE(u_ptr, buffer->DataU()); |
| 51 | EXPECT_NE(v_ptr, buffer->DataV()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 54 | TEST(TestI420BufferPool, FrameValidAfterPoolDestruction) { |
nisse | 64ec8f8 | 2016-09-27 00:17:25 -0700 | [diff] [blame] | 55 | rtc::scoped_refptr<I420Buffer> buffer; |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 56 | { |
| 57 | I420BufferPool pool; |
| 58 | buffer = pool.CreateBuffer(16, 16); |
| 59 | } |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 60 | EXPECT_EQ(16, buffer->width()); |
| 61 | EXPECT_EQ(16, buffer->height()); |
| 62 | // Try to trigger use-after-free errors by writing to y-plane. |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 63 | memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY()); |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 66 | TEST(TestI420BufferPool, MaxNumberOfBuffers) { |
| 67 | I420BufferPool pool(false, 1); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 68 | rtc::scoped_refptr<I420BufferInterface> buffer1 = pool.CreateBuffer(16, 16); |
Per | 0098357 | 2016-11-04 08:57:26 +0100 | [diff] [blame] | 69 | EXPECT_NE(nullptr, buffer1.get()); |
| 70 | EXPECT_EQ(nullptr, pool.CreateBuffer(16, 16).get()); |
| 71 | } |
| 72 | |
magjed@webrtc.org | 73d763e | 2015-03-17 11:40:45 +0000 | [diff] [blame] | 73 | } // namespace webrtc |