blob: 77aa0861d3a876b7b512204ca5e05a5ee2e80a96 [file] [log] [blame]
magjed@webrtc.org73d763e2015-03-17 11:40:45 +00001/*
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
kjellander6f8ce062015-11-16 13:52:24 -080013#include "webrtc/common_video/include/i420_buffer_pool.h"
kwibergac9f8762016-09-30 22:29:43 -070014#include "webrtc/test/gtest.h"
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000015
16namespace webrtc {
17
18TEST(TestI420BufferPool, SimpleFrameReuse) {
19 I420BufferPool pool;
magjed3f075492017-06-01 10:02:26 -070020 rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000021 EXPECT_EQ(16, buffer->width());
22 EXPECT_EQ(16, buffer->height());
23 // Extract non-refcounted pointers for testing.
nisse06176e42016-04-18 05:34:40 -070024 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.org73d763e2015-03-17 11:40:45 +000027 // 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);
nisse06176e42016-04-18 05:34:40 -070031 EXPECT_EQ(y_ptr, buffer->DataY());
32 EXPECT_EQ(u_ptr, buffer->DataU());
33 EXPECT_EQ(v_ptr, buffer->DataV());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000034 EXPECT_EQ(16, buffer->width());
35 EXPECT_EQ(16, buffer->height());
36}
37
38TEST(TestI420BufferPool, FailToReuse) {
39 I420BufferPool pool;
magjed3f075492017-06-01 10:02:26 -070040 rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000041 // Extract non-refcounted pointers for testing.
nisse06176e42016-04-18 05:34:40 -070042 const uint8_t* u_ptr = buffer->DataU();
43 const uint8_t* v_ptr = buffer->DataV();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000044 // 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());
nisse06176e42016-04-18 05:34:40 -070050 EXPECT_NE(u_ptr, buffer->DataU());
51 EXPECT_NE(v_ptr, buffer->DataV());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000052}
53
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000054TEST(TestI420BufferPool, FrameValidAfterPoolDestruction) {
nisse64ec8f82016-09-27 00:17:25 -070055 rtc::scoped_refptr<I420Buffer> buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000056 {
57 I420BufferPool pool;
58 buffer = pool.CreateBuffer(16, 16);
59 }
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000060 EXPECT_EQ(16, buffer->width());
61 EXPECT_EQ(16, buffer->height());
62 // Try to trigger use-after-free errors by writing to y-plane.
nisse06176e42016-04-18 05:34:40 -070063 memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000064}
65
Per00983572016-11-04 08:57:26 +010066TEST(TestI420BufferPool, MaxNumberOfBuffers) {
67 I420BufferPool pool(false, 1);
magjed3f075492017-06-01 10:02:26 -070068 rtc::scoped_refptr<I420BufferInterface> buffer1 = pool.CreateBuffer(16, 16);
Per00983572016-11-04 08:57:26 +010069 EXPECT_NE(nullptr, buffer1.get());
70 EXPECT_EQ(nullptr, pool.CreateBuffer(16, 16).get());
71}
72
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000073} // namespace webrtc