blob: debe637f5b7024c36555b989f09341c004770921 [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stdint.h>
12#include <string.h>
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000013
Mirko Bonadeid9708072019-01-25 20:26:48 +010014#include "api/scoped_refptr.h"
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "api/video/i420_buffer.h"
16#include "api/video/video_frame_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_video/include/i420_buffer_pool.h"
18#include "test/gtest.h"
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000019
20namespace webrtc {
21
22TEST(TestI420BufferPool, SimpleFrameReuse) {
23 I420BufferPool pool;
magjed3f075492017-06-01 10:02:26 -070024 rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000025 EXPECT_EQ(16, buffer->width());
26 EXPECT_EQ(16, buffer->height());
27 // Extract non-refcounted pointers for testing.
nisse06176e42016-04-18 05:34:40 -070028 const uint8_t* y_ptr = buffer->DataY();
29 const uint8_t* u_ptr = buffer->DataU();
30 const uint8_t* v_ptr = buffer->DataV();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000031 // Release buffer so that it is returned to the pool.
32 buffer = nullptr;
33 // Check that the memory is resued.
34 buffer = pool.CreateBuffer(16, 16);
nisse06176e42016-04-18 05:34:40 -070035 EXPECT_EQ(y_ptr, buffer->DataY());
36 EXPECT_EQ(u_ptr, buffer->DataU());
37 EXPECT_EQ(v_ptr, buffer->DataV());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000038 EXPECT_EQ(16, buffer->width());
39 EXPECT_EQ(16, buffer->height());
40}
41
42TEST(TestI420BufferPool, FailToReuse) {
43 I420BufferPool pool;
magjed3f075492017-06-01 10:02:26 -070044 rtc::scoped_refptr<I420BufferInterface> buffer = pool.CreateBuffer(16, 16);
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000045 // Extract non-refcounted pointers for testing.
nisse06176e42016-04-18 05:34:40 -070046 const uint8_t* u_ptr = buffer->DataU();
47 const uint8_t* v_ptr = buffer->DataV();
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000048 // Release buffer so that it is returned to the pool.
49 buffer = nullptr;
50 // Check that the pool doesn't try to reuse buffers of incorrect size.
51 buffer = pool.CreateBuffer(32, 16);
52 EXPECT_EQ(32, buffer->width());
53 EXPECT_EQ(16, buffer->height());
nisse06176e42016-04-18 05:34:40 -070054 EXPECT_NE(u_ptr, buffer->DataU());
55 EXPECT_NE(v_ptr, buffer->DataV());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000056}
57
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000058TEST(TestI420BufferPool, FrameValidAfterPoolDestruction) {
nisse64ec8f82016-09-27 00:17:25 -070059 rtc::scoped_refptr<I420Buffer> buffer;
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000060 {
61 I420BufferPool pool;
62 buffer = pool.CreateBuffer(16, 16);
63 }
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000064 EXPECT_EQ(16, buffer->width());
65 EXPECT_EQ(16, buffer->height());
66 // Try to trigger use-after-free errors by writing to y-plane.
nisse06176e42016-04-18 05:34:40 -070067 memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY());
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000068}
69
Per00983572016-11-04 08:57:26 +010070TEST(TestI420BufferPool, MaxNumberOfBuffers) {
71 I420BufferPool pool(false, 1);
magjed3f075492017-06-01 10:02:26 -070072 rtc::scoped_refptr<I420BufferInterface> buffer1 = pool.CreateBuffer(16, 16);
Per00983572016-11-04 08:57:26 +010073 EXPECT_NE(nullptr, buffer1.get());
74 EXPECT_EQ(nullptr, pool.CreateBuffer(16, 16).get());
75}
76
magjed@webrtc.org73d763e2015-03-17 11:40:45 +000077} // namespace webrtc