xref: /aosp_15_r20/external/webrtc/common_video/video_frame_buffer_pool_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "common_video/include/video_frame_buffer_pool.h"
12 
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include "api/scoped_refptr.h"
17 #include "api/video/i420_buffer.h"
18 #include "api/video/video_frame_buffer.h"
19 #include "test/gtest.h"
20 
21 namespace webrtc {
22 
TEST(TestVideoFrameBufferPool,SimpleFrameReuse)23 TEST(TestVideoFrameBufferPool, SimpleFrameReuse) {
24   VideoFrameBufferPool pool;
25   auto buffer = pool.CreateI420Buffer(16, 16);
26   EXPECT_EQ(16, buffer->width());
27   EXPECT_EQ(16, buffer->height());
28   // Extract non-refcounted pointers for testing.
29   const uint8_t* y_ptr = buffer->DataY();
30   const uint8_t* u_ptr = buffer->DataU();
31   const uint8_t* v_ptr = buffer->DataV();
32   // Release buffer so that it is returned to the pool.
33   buffer = nullptr;
34   // Check that the memory is resued.
35   buffer = pool.CreateI420Buffer(16, 16);
36   EXPECT_EQ(y_ptr, buffer->DataY());
37   EXPECT_EQ(u_ptr, buffer->DataU());
38   EXPECT_EQ(v_ptr, buffer->DataV());
39 }
40 
TEST(TestVideoFrameBufferPool,FailToReuseWrongSize)41 TEST(TestVideoFrameBufferPool, FailToReuseWrongSize) {
42   // Set max frames to 1, just to make sure the first buffer is being released.
43   VideoFrameBufferPool pool(/*zero_initialize=*/false, 1);
44   auto buffer = pool.CreateI420Buffer(16, 16);
45   EXPECT_EQ(16, buffer->width());
46   EXPECT_EQ(16, buffer->height());
47   // Release buffer so that it is returned to the pool.
48   buffer = nullptr;
49   // Check that the pool doesn't try to reuse buffers of incorrect size.
50   buffer = pool.CreateI420Buffer(32, 16);
51   ASSERT_TRUE(buffer);
52   EXPECT_EQ(32, buffer->width());
53   EXPECT_EQ(16, buffer->height());
54 }
55 
TEST(TestVideoFrameBufferPool,FrameValidAfterPoolDestruction)56 TEST(TestVideoFrameBufferPool, FrameValidAfterPoolDestruction) {
57   rtc::scoped_refptr<I420Buffer> buffer;
58   {
59     VideoFrameBufferPool pool;
60     buffer = pool.CreateI420Buffer(16, 16);
61   }
62   EXPECT_EQ(16, buffer->width());
63   EXPECT_EQ(16, buffer->height());
64   // Access buffer, so that ASAN could find any issues if buffer
65   // doesn't outlive the buffer pool.
66   memset(buffer->MutableDataY(), 0xA5, 16 * buffer->StrideY());
67 }
68 
TEST(TestVideoFrameBufferPool,MaxNumberOfBuffers)69 TEST(TestVideoFrameBufferPool, MaxNumberOfBuffers) {
70   VideoFrameBufferPool pool(false, 1);
71   auto buffer = pool.CreateI420Buffer(16, 16);
72   EXPECT_NE(nullptr, buffer.get());
73   EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get());
74 }
75 
TEST(TestVideoFrameBufferPool,ProducesNv12)76 TEST(TestVideoFrameBufferPool, ProducesNv12) {
77   VideoFrameBufferPool pool(false, 1);
78   auto buffer = pool.CreateNV12Buffer(16, 16);
79   EXPECT_NE(nullptr, buffer.get());
80 }
81 
TEST(TestVideoFrameBufferPool,ProducesI422)82 TEST(TestVideoFrameBufferPool, ProducesI422) {
83   VideoFrameBufferPool pool(false, 1);
84   auto buffer = pool.CreateI422Buffer(16, 16);
85   EXPECT_NE(nullptr, buffer.get());
86 }
87 
TEST(TestVideoFrameBufferPool,ProducesI444)88 TEST(TestVideoFrameBufferPool, ProducesI444) {
89   VideoFrameBufferPool pool(false, 1);
90   auto buffer = pool.CreateI444Buffer(16, 16);
91   EXPECT_NE(nullptr, buffer.get());
92 }
93 
TEST(TestVideoFrameBufferPool,ProducesI010)94 TEST(TestVideoFrameBufferPool, ProducesI010) {
95   VideoFrameBufferPool pool(false, 1);
96   auto buffer = pool.CreateI010Buffer(16, 16);
97   EXPECT_NE(nullptr, buffer.get());
98 }
99 
TEST(TestVideoFrameBufferPool,ProducesI210)100 TEST(TestVideoFrameBufferPool, ProducesI210) {
101   VideoFrameBufferPool pool(false, 1);
102   auto buffer = pool.CreateI210Buffer(16, 16);
103   EXPECT_NE(nullptr, buffer.get());
104 }
105 
TEST(TestVideoFrameBufferPool,SwitchingPixelFormat)106 TEST(TestVideoFrameBufferPool, SwitchingPixelFormat) {
107   VideoFrameBufferPool pool(false, 1);
108   auto buffeNV12 = pool.CreateNV12Buffer(16, 16);
109   EXPECT_EQ(nullptr, pool.CreateNV12Buffer(16, 16).get());
110 
111   auto bufferI420 = pool.CreateI420Buffer(16, 16);
112   EXPECT_NE(nullptr, bufferI420.get());
113   EXPECT_EQ(nullptr, pool.CreateI420Buffer(16, 16).get());
114 
115   auto bufferI444 = pool.CreateI444Buffer(16, 16);
116   EXPECT_NE(nullptr, bufferI444.get());
117   EXPECT_EQ(nullptr, pool.CreateI444Buffer(16, 16).get());
118 
119   auto bufferI422 = pool.CreateI422Buffer(16, 16);
120   EXPECT_NE(nullptr, bufferI422.get());
121   EXPECT_EQ(nullptr, pool.CreateI422Buffer(16, 16).get());
122 
123   auto bufferI010 = pool.CreateI010Buffer(16, 16);
124   EXPECT_NE(nullptr, bufferI010.get());
125   EXPECT_EQ(nullptr, pool.CreateI010Buffer(16, 16).get());
126 
127   auto bufferI210 = pool.CreateI210Buffer(16, 16);
128   EXPECT_NE(nullptr, bufferI210.get());
129   EXPECT_EQ(nullptr, pool.CreateI210Buffer(16, 16).get());
130 }
131 
132 }  // namespace webrtc
133