| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Unit test for VideoCaptureBufferPool. | 5 // Unit test for VideoCaptureBufferPool. |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 7 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "cc/test/test_context_provider.h" | 13 #include "cc/test/test_context_provider.h" |
| 14 #include "cc/test/test_web_graphics_context_3d.h" | 14 #include "cc/test/test_web_graphics_context_3d.h" |
| 15 #include "content/browser/compositor/buffer_queue.h" | 15 #include "content/browser/compositor/buffer_queue.h" |
| 16 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | 16 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_controller.h" | 17 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 18 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 19 #include "media/base/video_util.h" | 19 #include "media/base/video_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 static const media::VideoPixelFormat kCaptureFormats[] = { | 25 struct PixelFormatAndStorage { |
| 26 media::PIXEL_FORMAT_I420, | 26 media::VideoPixelFormat pixel_format; |
| 27 media::PIXEL_FORMAT_TEXTURE, | 27 media::VideoPixelStorage pixel_storage; |
| 28 }; |
| 29 |
| 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 }, |
| 28 #if !defined(OS_ANDROID) | 34 #if !defined(OS_ANDROID) |
| 29 media::PIXEL_FORMAT_GPUMEMORYBUFFER | 35 { media::PIXEL_FORMAT_I420, media::PIXEL_STORAGE_GPUMEMORYBUFFER }, |
| 36 { media::PIXEL_FORMAT_ARGB, media::PIXEL_STORAGE_GPUMEMORYBUFFER }, |
| 30 #endif | 37 #endif |
| 31 }; | 38 }; |
| 32 | 39 |
| 33 static const int kTestBufferPoolSize = 3; | 40 static const int kTestBufferPoolSize = 3; |
| 34 | 41 |
| 35 class VideoCaptureBufferPoolTest | 42 class VideoCaptureBufferPoolTest |
| 36 : public testing::TestWithParam<media::VideoPixelFormat> { | 43 : public testing::TestWithParam<PixelFormatAndStorage> { |
| 37 protected: | 44 protected: |
| 38 // A GpuMemoryBuffer Mock to provide a trivial RGBA buffer as Map() backing. | 45 // A GpuMemoryBuffer Mock to provide a trivial RGBA buffer as Map() backing. |
| 39 // We need to allocate on ctor and deallocate on dtor so that consecutive | 46 // We need to allocate on ctor and deallocate on dtor so that consecutive |
| 40 // Map()-Unmap() cycles yield the same underlying data pointer. | 47 // Map()-Unmap() cycles yield the same underlying data pointer. |
| 41 class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer { | 48 class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer { |
| 42 public: | 49 public: |
| 43 explicit MockGpuMemoryBuffer(const gfx::Size& size) | 50 explicit MockGpuMemoryBuffer(const gfx::Size& size) |
| 44 : size_(size), data_(new uint8[size_.GetArea() * 4]), mapped_(false) {} | 51 : size_(size), data_(new uint8[size_.GetArea() * 4]), mapped_(false) {} |
| 45 ~MockGpuMemoryBuffer() override { delete[] data_; } | 52 ~MockGpuMemoryBuffer() override { delete[] data_; } |
| 46 | 53 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 output_surface_.reset(new MockBufferQueue( | 139 output_surface_.reset(new MockBufferQueue( |
| 133 context_provider, gpu_memory_buffer_manager_.get(), GL_RGBA)); | 140 context_provider, gpu_memory_buffer_manager_.get(), GL_RGBA)); |
| 134 output_surface_->Initialize(); | 141 output_surface_->Initialize(); |
| 135 } | 142 } |
| 136 #endif | 143 #endif |
| 137 | 144 |
| 138 void ExpectDroppedId(int expected_dropped_id) { | 145 void ExpectDroppedId(int expected_dropped_id) { |
| 139 expected_dropped_id_ = expected_dropped_id; | 146 expected_dropped_id_ = expected_dropped_id; |
| 140 } | 147 } |
| 141 | 148 |
| 142 scoped_ptr<Buffer> ReserveBuffer(const gfx::Size& dimensions, | 149 scoped_ptr<Buffer> ReserveBuffer( |
| 143 media::VideoPixelFormat pixel_format) { | 150 const gfx::Size& dimensions, |
| 151 PixelFormatAndStorage format_and_storage) { |
| 144 // To verify that ReserveBuffer always sets |buffer_id_to_drop|, | 152 // To verify that ReserveBuffer always sets |buffer_id_to_drop|, |
| 145 // initialize it to something different than the expected value. | 153 // initialize it to something different than the expected value. |
| 146 int buffer_id_to_drop = ~expected_dropped_id_; | 154 int buffer_id_to_drop = ~expected_dropped_id_; |
| 147 DVLOG(1) << media::VideoCaptureFormat::PixelFormatToString(pixel_format) | 155 DVLOG(1) << media::VideoCaptureFormat::PixelStorageToString( |
| 156 format_and_storage.pixel_storage) |
| 157 << " " << media::VideoCaptureFormat::PixelFormatToString( |
| 158 format_and_storage.pixel_format) |
| 148 << " " << dimensions.ToString(); | 159 << " " << dimensions.ToString(); |
| 149 int buffer_id = | 160 const int buffer_id = pool_->ReserveForProducer( |
| 150 pool_->ReserveForProducer(pixel_format, dimensions, &buffer_id_to_drop); | 161 format_and_storage.pixel_format, |
| 162 format_and_storage.pixel_storage, |
| 163 dimensions, |
| 164 &buffer_id_to_drop); |
| 151 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 165 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
| 152 return scoped_ptr<Buffer>(); | 166 return scoped_ptr<Buffer>(); |
| 153 EXPECT_EQ(expected_dropped_id_, buffer_id_to_drop); | 167 EXPECT_EQ(expected_dropped_id_, buffer_id_to_drop); |
| 154 | 168 |
| 155 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle = | 169 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle = |
| 156 pool_->GetBufferHandle(buffer_id); | 170 pool_->GetBufferHandle(buffer_id); |
| 157 return scoped_ptr<Buffer>( | 171 return scoped_ptr<Buffer>( |
| 158 new Buffer(pool_, buffer_handle.Pass(), buffer_id)); | 172 new Buffer(pool_, buffer_handle.Pass(), buffer_id)); |
| 159 } | 173 } |
| 160 | 174 |
| 161 base::MessageLoop loop_; | 175 base::MessageLoop loop_; |
| 162 int expected_dropped_id_; | 176 int expected_dropped_id_; |
| 163 scoped_refptr<VideoCaptureBufferPool> pool_; | 177 scoped_refptr<VideoCaptureBufferPool> pool_; |
| 164 | 178 |
| 165 private: | 179 private: |
| 166 #if !defined(OS_ANDROID) | 180 #if !defined(OS_ANDROID) |
| 167 scoped_ptr<StubBrowserGpuMemoryBufferManager> gpu_memory_buffer_manager_; | 181 scoped_ptr<StubBrowserGpuMemoryBufferManager> gpu_memory_buffer_manager_; |
| 168 scoped_ptr<MockBufferQueue> output_surface_; | 182 scoped_ptr<MockBufferQueue> output_surface_; |
| 169 #endif | 183 #endif |
| 170 | 184 |
| 171 DISALLOW_COPY_AND_ASSIGN(VideoCaptureBufferPoolTest); | 185 DISALLOW_COPY_AND_ASSIGN(VideoCaptureBufferPoolTest); |
| 172 }; | 186 }; |
| 173 | 187 |
| 174 TEST_P(VideoCaptureBufferPoolTest, BufferPool) { | 188 TEST_P(VideoCaptureBufferPoolTest, BufferPool) { |
| 175 const gfx::Size size_lo = gfx::Size(10, 10); | 189 const gfx::Size size_lo = gfx::Size(10, 10); |
| 176 const gfx::Size size_hi = gfx::Size(21, 33); | 190 const gfx::Size size_hi = gfx::Size(21, 33); |
| 177 const media::VideoCaptureFormat format_lo(size_lo, 0.0, GetParam()); | 191 const media::VideoCaptureFormat format_lo( |
| 178 const media::VideoCaptureFormat format_hi(size_hi, 0.0, GetParam()); | 192 size_lo, 0.0, GetParam().pixel_format, GetParam().pixel_storage); |
| 193 const media::VideoCaptureFormat format_hi( |
| 194 size_hi, 0.0, GetParam().pixel_format, GetParam().pixel_storage); |
| 179 | 195 |
| 180 // Reallocation won't happen for the first part of the test. | 196 // Reallocation won't happen for the first part of the test. |
| 181 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); | 197 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); |
| 182 | 198 |
| 183 // The buffer pool should have zero utilization before any buffers have been | 199 // The buffer pool should have zero utilization before any buffers have been |
| 184 // reserved. | 200 // reserved. |
| 185 ASSERT_EQ(0.0, pool_->GetBufferPoolUtilization()); | 201 ASSERT_EQ(0.0, pool_->GetBufferPoolUtilization()); |
| 186 | 202 |
| 187 scoped_ptr<Buffer> buffer1 = ReserveBuffer(size_lo, GetParam()); | 203 scoped_ptr<Buffer> buffer1 = ReserveBuffer(size_lo, GetParam()); |
| 188 ASSERT_NE(nullptr, buffer1.get()); | 204 ASSERT_NE(nullptr, buffer1.get()); |
| 189 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->size()); | 205 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->size()); |
| 190 ASSERT_EQ(1.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 206 ASSERT_EQ(1.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 191 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); | 207 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); |
| 192 ASSERT_NE(nullptr, buffer2.get()); | 208 ASSERT_NE(nullptr, buffer2.get()); |
| 193 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); | 209 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); |
| 194 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 210 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 195 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); | 211 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); |
| 196 ASSERT_NE(nullptr, buffer3.get()); | 212 ASSERT_NE(nullptr, buffer3.get()); |
| 197 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->size()); | 213 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->size()); |
| 198 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 214 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 199 | 215 |
| 200 // Texture backed Frames cannot be manipulated via mapping. | 216 // Texture backed Frames cannot be manipulated via mapping. |
| 201 if (GetParam() != media::PIXEL_FORMAT_TEXTURE) { | 217 if (GetParam().pixel_storage != media::PIXEL_STORAGE_TEXTURE) { |
| 202 ASSERT_NE(nullptr, buffer1->data()); | 218 ASSERT_NE(nullptr, buffer1->data()); |
| 203 ASSERT_NE(nullptr, buffer2->data()); | 219 ASSERT_NE(nullptr, buffer2->data()); |
| 204 ASSERT_NE(nullptr, buffer3->data()); | 220 ASSERT_NE(nullptr, buffer3->data()); |
| 205 | 221 |
| 206 } | 222 } |
| 207 // Touch the memory. | 223 // Touch the memory. |
| 208 if (buffer1->data() != nullptr) | 224 if (buffer1->data() != nullptr) |
| 209 memset(buffer1->data(), 0x11, buffer1->size()); | 225 memset(buffer1->data(), 0x11, buffer1->size()); |
| 210 if (buffer2->data() != nullptr) | 226 if (buffer2->data() != nullptr) |
| 211 memset(buffer2->data(), 0x44, buffer2->size()); | 227 memset(buffer2->data(), 0x44, buffer2->size()); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 memset(buffer4->data(), 0x55, buffer4->size()); | 348 memset(buffer4->data(), 0x55, buffer4->size()); |
| 333 buffer2.reset(); | 349 buffer2.reset(); |
| 334 | 350 |
| 335 if (buffer4->data() != nullptr) | 351 if (buffer4->data() != nullptr) |
| 336 memset(buffer4->data(), 0x77, buffer4->size()); | 352 memset(buffer4->data(), 0x77, buffer4->size()); |
| 337 buffer4.reset(); | 353 buffer4.reset(); |
| 338 } | 354 } |
| 339 | 355 |
| 340 INSTANTIATE_TEST_CASE_P(, | 356 INSTANTIATE_TEST_CASE_P(, |
| 341 VideoCaptureBufferPoolTest, | 357 VideoCaptureBufferPoolTest, |
| 342 testing::ValuesIn(kCaptureFormats)); | 358 testing::ValuesIn(kCapturePixelFormatAndStorages)); |
| 343 | 359 |
| 344 } // namespace content | 360 } // namespace content |
| OLD | NEW |