1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Unit test for VideoCaptureBufferPool.
7 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "cc/test/test_context_provider.h"
14 #include "cc/test/test_web_graphics_context_3d.h"
15 #include "content/browser/compositor/buffer_queue.h"
16 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
17 #include "content/browser/renderer_host/media/video_capture_controller.h"
18 #include "media/base/video_frame.h"
19 #include "media/base/video_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 struct PixelFormatAndStorage
{
26 media::VideoPixelFormat pixel_format
;
27 media::VideoPixelStorage pixel_storage
;
30 static const PixelFormatAndStorage kCapturePixelFormatAndStorages
[] = {
31 {media::PIXEL_FORMAT_I420
, media::PIXEL_STORAGE_CPU
},
32 {media::PIXEL_FORMAT_ARGB
, media::PIXEL_STORAGE_CPU
},
33 {media::PIXEL_FORMAT_ARGB
, media::PIXEL_STORAGE_TEXTURE
},
34 #if !defined(OS_ANDROID)
35 {media::PIXEL_FORMAT_I420
,
36 media::PIXEL_STORAGE_GPUMEMORYBUFFER
},
40 static const int kTestBufferPoolSize
= 3;
42 class VideoCaptureBufferPoolTest
43 : public testing::TestWithParam
<PixelFormatAndStorage
> {
45 // A GpuMemoryBuffer Mock to provide a trivial RGBA buffer as Map() backing.
46 // We need to allocate on ctor and deallocate on dtor so that consecutive
47 // Map()-Unmap() cycles yield the same underlying data pointer.
48 class MockGpuMemoryBuffer
: public gfx::GpuMemoryBuffer
{
50 explicit MockGpuMemoryBuffer(const gfx::Size
& size
)
51 : size_(size
), data_(new uint8
[size_
.GetArea() * 4]), mapped_(false) {}
52 ~MockGpuMemoryBuffer() override
{ delete[] data_
; }
54 bool Map(void** data
) override
{
55 EXPECT_EQ(mapped_
, false);
57 data
[0] = static_cast<void*>(data_
);
60 void Unmap() override
{
61 EXPECT_EQ(mapped_
, true);
64 bool IsMapped() const override
{ return mapped_
; }
65 gfx::BufferFormat
GetFormat() const override
{
66 return gfx::BufferFormat::BGRA_8888
;
68 void GetStride(int* stride
) const override
{
69 *stride
= size_
.width() * 4;
72 gfx::GpuMemoryBufferId
GetId() const override
{
73 return gfx::GpuMemoryBufferId(0);
75 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
76 return gfx::GpuMemoryBufferHandle();
78 ClientBuffer
AsClientBuffer() override
{ return nullptr; }
81 const gfx::Size size_
;
86 #if !defined(OS_ANDROID)
87 // The next two classes are needed to replicate the GpuMemoryBuffer allocation
89 class StubBrowserGpuMemoryBufferManager
90 : public BrowserGpuMemoryBufferManager
{
92 StubBrowserGpuMemoryBufferManager() : BrowserGpuMemoryBufferManager(1, 1) {}
94 scoped_ptr
<gfx::GpuMemoryBuffer
> AllocateGpuMemoryBuffer(
95 const gfx::Size
& size
,
96 gfx::BufferFormat format
,
97 gfx::BufferUsage usage
) override
{
98 return make_scoped_ptr(new MockGpuMemoryBuffer(size
));
101 class MockBufferQueue
: public BufferQueue
{
103 MockBufferQueue(scoped_refptr
<cc::ContextProvider
> context_provider
,
104 BrowserGpuMemoryBufferManager
* gpu_memory_buffer_manager
,
106 unsigned int internalformat
)
107 : BufferQueue(context_provider
,
111 gpu_memory_buffer_manager
,
113 MOCK_METHOD4(CopyBufferDamage
,
114 void(int, int, const gfx::Rect
&, const gfx::Rect
&));
118 // This is a generic Buffer tracker
121 Buffer(const scoped_refptr
<VideoCaptureBufferPool
> pool
,
122 scoped_ptr
<VideoCaptureBufferPool::BufferHandle
> buffer_handle
,
124 : id_(id
), pool_(pool
), buffer_handle_(buffer_handle
.Pass()) {}
125 ~Buffer() { pool_
->RelinquishProducerReservation(id()); }
126 int id() const { return id_
; }
127 size_t mapped_size() { return buffer_handle_
->mapped_size(); }
128 void* data() { return buffer_handle_
->data(0); }
132 const scoped_refptr
<VideoCaptureBufferPool
> pool_
;
133 const scoped_ptr
<VideoCaptureBufferPool::BufferHandle
> buffer_handle_
;
136 VideoCaptureBufferPoolTest()
137 : expected_dropped_id_(0),
138 pool_(new VideoCaptureBufferPool(kTestBufferPoolSize
)) {}
140 #if !defined(OS_ANDROID)
141 void SetUp() override
{
142 scoped_refptr
<cc::TestContextProvider
> context_provider
=
143 cc::TestContextProvider::Create(cc::TestWebGraphicsContext3D::Create());
144 context_provider
->BindToCurrentThread();
145 gpu_memory_buffer_manager_
.reset(new StubBrowserGpuMemoryBufferManager
);
146 output_surface_
.reset(new MockBufferQueue(context_provider
,
147 gpu_memory_buffer_manager_
.get(),
148 GL_TEXTURE_2D
, GL_RGBA
));
149 output_surface_
->Initialize();
153 void ExpectDroppedId(int expected_dropped_id
) {
154 expected_dropped_id_
= expected_dropped_id
;
157 scoped_ptr
<Buffer
> ReserveBuffer(const gfx::Size
& dimensions
,
158 PixelFormatAndStorage format_and_storage
) {
159 // To verify that ReserveBuffer always sets |buffer_id_to_drop|,
160 // initialize it to something different than the expected value.
161 int buffer_id_to_drop
= ~expected_dropped_id_
;
162 DVLOG(1) << media::VideoCaptureFormat::PixelStorageToString(
163 format_and_storage
.pixel_storage
) << " "
164 << media::VideoPixelFormatToString(format_and_storage
.pixel_format
)
165 << " " << dimensions
.ToString();
166 const int buffer_id
= pool_
->ReserveForProducer(
167 format_and_storage
.pixel_format
, format_and_storage
.pixel_storage
,
168 dimensions
, &buffer_id_to_drop
);
169 if (buffer_id
== VideoCaptureBufferPool::kInvalidId
)
170 return scoped_ptr
<Buffer
>();
171 EXPECT_EQ(expected_dropped_id_
, buffer_id_to_drop
);
173 scoped_ptr
<VideoCaptureBufferPool::BufferHandle
> buffer_handle
=
174 pool_
->GetBufferHandle(buffer_id
);
175 return scoped_ptr
<Buffer
>(
176 new Buffer(pool_
, buffer_handle
.Pass(), buffer_id
));
179 base::MessageLoop loop_
;
180 int expected_dropped_id_
;
181 scoped_refptr
<VideoCaptureBufferPool
> pool_
;
184 #if !defined(OS_ANDROID)
185 scoped_ptr
<StubBrowserGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
186 scoped_ptr
<MockBufferQueue
> output_surface_
;
189 DISALLOW_COPY_AND_ASSIGN(VideoCaptureBufferPoolTest
);
192 TEST_P(VideoCaptureBufferPoolTest
, BufferPool
) {
193 const gfx::Size size_lo
= gfx::Size(10, 10);
194 const gfx::Size size_hi
= gfx::Size(21, 33);
195 const media::VideoCaptureFormat
format_lo(
196 size_lo
, 0.0, GetParam().pixel_format
, GetParam().pixel_storage
);
197 const media::VideoCaptureFormat
format_hi(
198 size_hi
, 0.0, GetParam().pixel_format
, GetParam().pixel_storage
);
200 // Reallocation won't happen for the first part of the test.
201 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId
);
203 // The buffer pool should have zero utilization before any buffers have been
205 ASSERT_EQ(0.0, pool_
->GetBufferPoolUtilization());
207 scoped_ptr
<Buffer
> buffer1
= ReserveBuffer(size_lo
, GetParam());
208 ASSERT_NE(nullptr, buffer1
.get());
209 ASSERT_EQ(1.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
210 scoped_ptr
<Buffer
> buffer2
= ReserveBuffer(size_lo
, GetParam());
211 ASSERT_NE(nullptr, buffer2
.get());
212 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
213 scoped_ptr
<Buffer
> buffer3
= ReserveBuffer(size_lo
, GetParam());
214 ASSERT_NE(nullptr, buffer3
.get());
215 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
217 // GMB backed frames have their platform and format specific allocations.
218 if (GetParam().pixel_storage
!= media::PIXEL_STORAGE_GPUMEMORYBUFFER
) {
219 ASSERT_LE(format_lo
.ImageAllocationSize(), buffer1
->mapped_size());
220 ASSERT_LE(format_lo
.ImageAllocationSize(), buffer2
->mapped_size());
221 ASSERT_LE(format_lo
.ImageAllocationSize(), buffer3
->mapped_size());
224 // Texture backed Frames cannot be manipulated via mapping.
225 if (GetParam().pixel_storage
!= media::PIXEL_STORAGE_TEXTURE
) {
226 ASSERT_NE(nullptr, buffer1
->data());
227 ASSERT_NE(nullptr, buffer2
->data());
228 ASSERT_NE(nullptr, buffer3
->data());
231 if (buffer1
->data() != nullptr)
232 memset(buffer1
->data(), 0x11, buffer1
->mapped_size());
233 if (buffer2
->data() != nullptr)
234 memset(buffer2
->data(), 0x44, buffer2
->mapped_size());
235 if (buffer3
->data() != nullptr)
236 memset(buffer3
->data(), 0x77, buffer3
->mapped_size());
238 // Fourth buffer should fail. Buffer pool utilization should be at 100%.
239 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
240 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
242 // Release 1st buffer and retry; this should succeed.
244 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
245 scoped_ptr
<Buffer
> buffer4
= ReserveBuffer(size_lo
, GetParam());
246 ASSERT_NE(nullptr, buffer4
.get());
247 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
249 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
250 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
251 ASSERT_FALSE(ReserveBuffer(size_hi
, GetParam())) << "Pool should be empty";
252 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
255 int buffer_id2
= buffer2
->id();
256 ASSERT_EQ(1, buffer_id2
);
257 const int buffer_id3
= buffer3
->id();
258 ASSERT_EQ(2, buffer_id3
);
259 const int buffer_id4
= buffer4
->id();
260 ASSERT_EQ(0, buffer_id4
);
261 void* const memory_pointer3
= buffer3
->data();
264 pool_
->HoldForConsumers(buffer_id3
, 2);
266 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
267 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
269 buffer3
.reset(); // Old producer releases buffer. Should be a noop.
270 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
271 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
272 ASSERT_FALSE(ReserveBuffer(size_hi
, GetParam())) << "Pool should be empty";
273 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
275 buffer2
.reset(); // Active producer releases buffer. Should free a buffer.
277 buffer1
= ReserveBuffer(size_lo
, GetParam());
278 ASSERT_NE(nullptr, buffer1
.get());
279 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
280 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
281 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
283 // First consumer finishes.
284 pool_
->RelinquishConsumerHold(buffer_id3
, 1);
285 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
286 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
288 // Second consumer finishes. This should free that buffer.
289 pool_
->RelinquishConsumerHold(buffer_id3
, 1);
290 buffer3
= ReserveBuffer(size_lo
, GetParam());
291 ASSERT_NE(nullptr, buffer3
.get());
292 ASSERT_EQ(buffer_id3
, buffer3
->id()) << "Buffer ID should be reused.";
293 ASSERT_EQ(memory_pointer3
, buffer3
->data());
294 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
295 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
296 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
298 // Now deliver & consume buffer1, but don't release the buffer.
299 int buffer_id1
= buffer1
->id();
300 ASSERT_EQ(1, buffer_id1
);
301 pool_
->HoldForConsumers(buffer_id1
, 5);
302 pool_
->RelinquishConsumerHold(buffer_id1
, 5);
304 // Even though the consumer is done with the buffer at |buffer_id1|, it cannot
305 // be re-allocated to the producer, because |buffer1| still references it. But
306 // when |buffer1| goes away, we should be able to re-reserve the buffer (and
307 // the ID ought to be the same).
308 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
309 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
310 buffer1
.reset(); // Should free the buffer.
311 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
312 buffer2
= ReserveBuffer(size_lo
, GetParam());
313 ASSERT_NE(nullptr, buffer2
.get());
314 ASSERT_EQ(buffer_id1
, buffer2
->id());
315 buffer_id2
= buffer_id1
;
316 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
317 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
318 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
320 // Now try reallocation with different resolutions. We expect reallocation
321 // to occur only when the old buffer is too small.
323 ExpectDroppedId(buffer_id2
);
324 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
325 buffer2
= ReserveBuffer(size_hi
, GetParam());
326 ASSERT_NE(nullptr, buffer2
.get());
327 if (GetParam().pixel_storage
!= media::PIXEL_STORAGE_GPUMEMORYBUFFER
)
328 ASSERT_LE(format_hi
.ImageAllocationSize(), buffer2
->mapped_size());
329 ASSERT_EQ(3, buffer2
->id());
330 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
331 void* const memory_pointer_hi
= buffer2
->data();
332 buffer2
.reset(); // Frees it.
333 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId
);
334 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
335 buffer2
= ReserveBuffer(size_lo
, GetParam());
336 void* const memory_pointer_lo
= buffer2
->data();
337 ASSERT_EQ(memory_pointer_hi
, memory_pointer_lo
)
338 << "Decrease in resolution should not reallocate buffer";
339 ASSERT_NE(nullptr, buffer2
.get());
340 ASSERT_EQ(3, buffer2
->id());
341 if (GetParam().pixel_storage
!= media::PIXEL_STORAGE_GPUMEMORYBUFFER
)
342 ASSERT_LE(format_lo
.ImageAllocationSize(), buffer2
->mapped_size());
343 ASSERT_EQ(3.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
344 ASSERT_FALSE(ReserveBuffer(size_lo
, GetParam())) << "Pool should be empty";
345 ASSERT_EQ(1.0, pool_
->GetBufferPoolUtilization());
347 // Tear down the pool_, writing into the buffers. The buffer should preserve
348 // the lifetime of the underlying memory.
350 ASSERT_EQ(2.0 / kTestBufferPoolSize
, pool_
->GetBufferPoolUtilization());
354 if (buffer2
->data() != nullptr)
355 memset(buffer2
->data(), 0x22, buffer2
->mapped_size());
356 if (buffer4
->data() != nullptr)
357 memset(buffer4
->data(), 0x55, buffer4
->mapped_size());
360 if (buffer4
->data() != nullptr)
361 memset(buffer4
->data(), 0x77, buffer4
->mapped_size());
365 INSTANTIATE_TEST_CASE_P(,
366 VideoCaptureBufferPoolTest
,
367 testing::ValuesIn(kCapturePixelFormatAndStorages
));
369 } // namespace content