| 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 class VideoCaptureBufferPoolTest | 40 class VideoCaptureBufferPoolTest |
| 34 : public testing::TestWithParam<media::VideoPixelFormat> { | 41 : public testing::TestWithParam<PixelFormatAndStorage> { |
| 35 protected: | 42 protected: |
| 36 // A GpuMemoryBuffer Mock to provide a trivial RGBA buffer as Map() backing. | 43 // A GpuMemoryBuffer Mock to provide a trivial RGBA buffer as Map() backing. |
| 37 // We need to allocate on ctor and deallocate on dtor so that consecutive | 44 // We need to allocate on ctor and deallocate on dtor so that consecutive |
| 38 // Map()-Unmap() cycles yield the same underlying data pointer. | 45 // Map()-Unmap() cycles yield the same underlying data pointer. |
| 39 class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer { | 46 class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer { |
| 40 public: | 47 public: |
| 41 explicit MockGpuMemoryBuffer(const gfx::Size& size) | 48 explicit MockGpuMemoryBuffer(const gfx::Size& size) |
| 42 : size_(size), data_(new uint8[size_.GetArea() * 4]), mapped_(false) {} | 49 : size_(size), data_(new uint8[size_.GetArea() * 4]), mapped_(false) {} |
| 43 ~MockGpuMemoryBuffer() override { delete[] data_; } | 50 ~MockGpuMemoryBuffer() override { delete[] data_; } |
| 44 | 51 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 output_surface_.reset(new MockBufferQueue( | 138 output_surface_.reset(new MockBufferQueue( |
| 132 context_provider, gpu_memory_buffer_manager_.get(), GL_RGBA)); | 139 context_provider, gpu_memory_buffer_manager_.get(), GL_RGBA)); |
| 133 output_surface_->Initialize(); | 140 output_surface_->Initialize(); |
| 134 } | 141 } |
| 135 #endif | 142 #endif |
| 136 | 143 |
| 137 void ExpectDroppedId(int expected_dropped_id) { | 144 void ExpectDroppedId(int expected_dropped_id) { |
| 138 expected_dropped_id_ = expected_dropped_id; | 145 expected_dropped_id_ = expected_dropped_id; |
| 139 } | 146 } |
| 140 | 147 |
| 141 scoped_ptr<Buffer> ReserveBuffer(const gfx::Size& dimensions, | 148 scoped_ptr<Buffer> ReserveBuffer( |
| 142 media::VideoPixelFormat pixel_format) { | 149 const gfx::Size& dimensions, |
| 150 PixelFormatAndStorage format_and_storage) { |
| 143 // To verify that ReserveBuffer always sets |buffer_id_to_drop|, | 151 // To verify that ReserveBuffer always sets |buffer_id_to_drop|, |
| 144 // initialize it to something different than the expected value. | 152 // initialize it to something different than the expected value. |
| 145 int buffer_id_to_drop = ~expected_dropped_id_; | 153 int buffer_id_to_drop = ~expected_dropped_id_; |
| 146 DVLOG(1) << media::VideoCaptureFormat::PixelFormatToString(pixel_format) | 154 DVLOG(1) << media::VideoCaptureFormat::PixelStorageToString( |
| 155 format_and_storage.pixel_storage) |
| 156 << " " << media::VideoCaptureFormat::PixelFormatToString( |
| 157 format_and_storage.pixel_format) |
| 147 << " " << dimensions.ToString(); | 158 << " " << dimensions.ToString(); |
| 148 int buffer_id = | 159 const int buffer_id = pool_->ReserveForProducer( |
| 149 pool_->ReserveForProducer(pixel_format, dimensions, &buffer_id_to_drop); | 160 format_and_storage.pixel_format, |
| 161 format_and_storage.pixel_storage, |
| 162 dimensions, |
| 163 &buffer_id_to_drop); |
| 150 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 164 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
| 151 return scoped_ptr<Buffer>(); | 165 return scoped_ptr<Buffer>(); |
| 152 EXPECT_EQ(expected_dropped_id_, buffer_id_to_drop); | 166 EXPECT_EQ(expected_dropped_id_, buffer_id_to_drop); |
| 153 | 167 |
| 154 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle = | 168 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle = |
| 155 pool_->GetBufferHandle(buffer_id); | 169 pool_->GetBufferHandle(buffer_id); |
| 156 return scoped_ptr<Buffer>( | 170 return scoped_ptr<Buffer>( |
| 157 new Buffer(pool_, buffer_handle.Pass(), buffer_id)); | 171 new Buffer(pool_, buffer_handle.Pass(), buffer_id)); |
| 158 } | 172 } |
| 159 | 173 |
| 160 base::MessageLoop loop_; | 174 base::MessageLoop loop_; |
| 161 int expected_dropped_id_; | 175 int expected_dropped_id_; |
| 162 scoped_refptr<VideoCaptureBufferPool> pool_; | 176 scoped_refptr<VideoCaptureBufferPool> pool_; |
| 163 | 177 |
| 164 private: | 178 private: |
| 165 #if !defined(OS_ANDROID) | 179 #if !defined(OS_ANDROID) |
| 166 scoped_ptr<StubBrowserGpuMemoryBufferManager> gpu_memory_buffer_manager_; | 180 scoped_ptr<StubBrowserGpuMemoryBufferManager> gpu_memory_buffer_manager_; |
| 167 scoped_ptr<MockBufferQueue> output_surface_; | 181 scoped_ptr<MockBufferQueue> output_surface_; |
| 168 #endif | 182 #endif |
| 169 | 183 |
| 170 DISALLOW_COPY_AND_ASSIGN(VideoCaptureBufferPoolTest); | 184 DISALLOW_COPY_AND_ASSIGN(VideoCaptureBufferPoolTest); |
| 171 }; | 185 }; |
| 172 | 186 |
| 173 TEST_P(VideoCaptureBufferPoolTest, BufferPool) { | 187 TEST_P(VideoCaptureBufferPoolTest, BufferPool) { |
| 174 const gfx::Size size_lo = gfx::Size(10, 10); | 188 const gfx::Size size_lo = gfx::Size(10, 10); |
| 175 const gfx::Size size_hi = gfx::Size(21, 33); | 189 const gfx::Size size_hi = gfx::Size(21, 33); |
| 176 const media::VideoCaptureFormat format_lo(size_lo, 0.0, GetParam()); | 190 const media::VideoCaptureFormat format_lo( |
| 177 const media::VideoCaptureFormat format_hi(size_hi, 0.0, GetParam()); | 191 size_lo, 0.0, GetParam().pixel_format, GetParam().pixel_storage); |
| 192 const media::VideoCaptureFormat format_hi( |
| 193 size_hi, 0.0, GetParam().pixel_format, GetParam().pixel_storage); |
| 178 | 194 |
| 179 // Reallocation won't happen for the first part of the test. | 195 // Reallocation won't happen for the first part of the test. |
| 180 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); | 196 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); |
| 181 | 197 |
| 182 scoped_ptr<Buffer> buffer1 = ReserveBuffer(size_lo, GetParam()); | 198 scoped_ptr<Buffer> buffer1 = ReserveBuffer(size_lo, GetParam()); |
| 183 ASSERT_NE(nullptr, buffer1.get()); | 199 ASSERT_NE(nullptr, buffer1.get()); |
| 184 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->size()); | 200 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->size()); |
| 185 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); | 201 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); |
| 186 ASSERT_NE(nullptr, buffer2.get()); | 202 ASSERT_NE(nullptr, buffer2.get()); |
| 187 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); | 203 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); |
| 188 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); | 204 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); |
| 189 ASSERT_NE(nullptr, buffer3.get()); | 205 ASSERT_NE(nullptr, buffer3.get()); |
| 190 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->size()); | 206 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->size()); |
| 191 | 207 |
| 192 // Texture backed Frames cannot be manipulated via mapping. | 208 // Texture backed Frames cannot be manipulated via mapping. |
| 193 if (GetParam() != media::PIXEL_FORMAT_TEXTURE) { | 209 if (GetParam().pixel_storage != media::PIXEL_STORAGE_TEXTURE) { |
| 194 ASSERT_NE(nullptr, buffer1->data()); | 210 ASSERT_NE(nullptr, buffer1->data()); |
| 195 ASSERT_NE(nullptr, buffer2->data()); | 211 ASSERT_NE(nullptr, buffer2->data()); |
| 196 ASSERT_NE(nullptr, buffer3->data()); | 212 ASSERT_NE(nullptr, buffer3->data()); |
| 197 | 213 |
| 198 } | 214 } |
| 199 // Touch the memory. | 215 // Touch the memory. |
| 200 if (buffer1->data() != nullptr) | 216 if (buffer1->data() != nullptr) |
| 201 memset(buffer1->data(), 0x11, buffer1->size()); | 217 memset(buffer1->data(), 0x11, buffer1->size()); |
| 202 if (buffer2->data() != nullptr) | 218 if (buffer2->data() != nullptr) |
| 203 memset(buffer2->data(), 0x44, buffer2->size()); | 219 memset(buffer2->data(), 0x44, buffer2->size()); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 memset(buffer4->data(), 0x55, buffer4->size()); | 317 memset(buffer4->data(), 0x55, buffer4->size()); |
| 302 buffer2.reset(); | 318 buffer2.reset(); |
| 303 | 319 |
| 304 if (buffer4->data() != nullptr) | 320 if (buffer4->data() != nullptr) |
| 305 memset(buffer4->data(), 0x77, buffer4->size()); | 321 memset(buffer4->data(), 0x77, buffer4->size()); |
| 306 buffer4.reset(); | 322 buffer4.reset(); |
| 307 } | 323 } |
| 308 | 324 |
| 309 INSTANTIATE_TEST_CASE_P(, | 325 INSTANTIATE_TEST_CASE_P(, |
| 310 VideoCaptureBufferPoolTest, | 326 VideoCaptureBufferPoolTest, |
| 311 testing::ValuesIn(kCaptureFormats)); | 327 testing::ValuesIn(kCapturePixelFormatAndStorages)); |
| 312 | 328 |
| 313 } // namespace content | 329 } // namespace content |
| OLD | NEW |