| 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" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 117 |
| 118 // This is a generic Buffer tracker | 118 // This is a generic Buffer tracker |
| 119 class Buffer { | 119 class Buffer { |
| 120 public: | 120 public: |
| 121 Buffer(const scoped_refptr<VideoCaptureBufferPool> pool, | 121 Buffer(const scoped_refptr<VideoCaptureBufferPool> pool, |
| 122 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle, | 122 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle, |
| 123 int id) | 123 int id) |
| 124 : id_(id), pool_(pool), buffer_handle_(buffer_handle.Pass()) {} | 124 : id_(id), pool_(pool), buffer_handle_(buffer_handle.Pass()) {} |
| 125 ~Buffer() { pool_->RelinquishProducerReservation(id()); } | 125 ~Buffer() { pool_->RelinquishProducerReservation(id()); } |
| 126 int id() const { return id_; } | 126 int id() const { return id_; } |
| 127 size_t size() { return buffer_handle_->size(); } | 127 size_t mapped_size() { return buffer_handle_->mapped_size(); } |
| 128 void* data() { return buffer_handle_->data(0); } | 128 void* data() { return buffer_handle_->data(0); } |
| 129 | 129 |
| 130 private: | 130 private: |
| 131 const int id_; | 131 const int id_; |
| 132 const scoped_refptr<VideoCaptureBufferPool> pool_; | 132 const scoped_refptr<VideoCaptureBufferPool> pool_; |
| 133 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_; | 133 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_; |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 VideoCaptureBufferPoolTest() | 136 VideoCaptureBufferPoolTest() |
| 137 : expected_dropped_id_(0), | 137 : expected_dropped_id_(0), |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 ASSERT_EQ(1.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 210 ASSERT_EQ(1.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 211 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); | 211 scoped_ptr<Buffer> buffer2 = ReserveBuffer(size_lo, GetParam()); |
| 212 ASSERT_NE(nullptr, buffer2.get()); | 212 ASSERT_NE(nullptr, buffer2.get()); |
| 213 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 213 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 214 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); | 214 scoped_ptr<Buffer> buffer3 = ReserveBuffer(size_lo, GetParam()); |
| 215 ASSERT_NE(nullptr, buffer3.get()); | 215 ASSERT_NE(nullptr, buffer3.get()); |
| 216 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 216 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 217 | 217 |
| 218 // GMB backed frames have their platform and format specific allocations. | 218 // GMB backed frames have their platform and format specific allocations. |
| 219 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) { | 219 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) { |
| 220 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->size()); | 220 ASSERT_LE(format_lo.ImageAllocationSize(), buffer1->mapped_size()); |
| 221 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); | 221 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->mapped_size()); |
| 222 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->size()); | 222 ASSERT_LE(format_lo.ImageAllocationSize(), buffer3->mapped_size()); |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Texture backed Frames cannot be manipulated via mapping. | 225 // Texture backed Frames cannot be manipulated via mapping. |
| 226 if (GetParam().pixel_storage != media::PIXEL_STORAGE_TEXTURE) { | 226 if (GetParam().pixel_storage != media::PIXEL_STORAGE_TEXTURE) { |
| 227 ASSERT_NE(nullptr, buffer1->data()); | 227 ASSERT_NE(nullptr, buffer1->data()); |
| 228 ASSERT_NE(nullptr, buffer2->data()); | 228 ASSERT_NE(nullptr, buffer2->data()); |
| 229 ASSERT_NE(nullptr, buffer3->data()); | 229 ASSERT_NE(nullptr, buffer3->data()); |
| 230 } | 230 } |
| 231 // Touch the memory. | 231 // Touch the memory. |
| 232 if (buffer1->data() != nullptr) | 232 if (buffer1->data() != nullptr) |
| 233 memset(buffer1->data(), 0x11, buffer1->size()); | 233 memset(buffer1->data(), 0x11, buffer1->mapped_size()); |
| 234 if (buffer2->data() != nullptr) | 234 if (buffer2->data() != nullptr) |
| 235 memset(buffer2->data(), 0x44, buffer2->size()); | 235 memset(buffer2->data(), 0x44, buffer2->mapped_size()); |
| 236 if (buffer3->data() != nullptr) | 236 if (buffer3->data() != nullptr) |
| 237 memset(buffer3->data(), 0x77, buffer3->size()); | 237 memset(buffer3->data(), 0x77, buffer3->mapped_size()); |
| 238 | 238 |
| 239 // Fourth buffer should fail. Buffer pool utilization should be at 100%. | 239 // Fourth buffer should fail. Buffer pool utilization should be at 100%. |
| 240 ASSERT_FALSE(ReserveBuffer(size_lo, GetParam())) << "Pool should be empty"; | 240 ASSERT_FALSE(ReserveBuffer(size_lo, GetParam())) << "Pool should be empty"; |
| 241 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); | 241 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); |
| 242 | 242 |
| 243 // Release 1st buffer and retry; this should succeed. | 243 // Release 1st buffer and retry; this should succeed. |
| 244 buffer1.reset(); | 244 buffer1.reset(); |
| 245 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 245 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 246 scoped_ptr<Buffer> buffer4 = ReserveBuffer(size_lo, GetParam()); | 246 scoped_ptr<Buffer> buffer4 = ReserveBuffer(size_lo, GetParam()); |
| 247 ASSERT_NE(nullptr, buffer4.get()); | 247 ASSERT_NE(nullptr, buffer4.get()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); | 319 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); |
| 320 | 320 |
| 321 // Now try reallocation with different resolutions. We expect reallocation | 321 // Now try reallocation with different resolutions. We expect reallocation |
| 322 // to occur only when the old buffer is too small. | 322 // to occur only when the old buffer is too small. |
| 323 buffer2.reset(); | 323 buffer2.reset(); |
| 324 ExpectDroppedId(buffer_id2); | 324 ExpectDroppedId(buffer_id2); |
| 325 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 325 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 326 buffer2 = ReserveBuffer(size_hi, GetParam()); | 326 buffer2 = ReserveBuffer(size_hi, GetParam()); |
| 327 ASSERT_NE(nullptr, buffer2.get()); | 327 ASSERT_NE(nullptr, buffer2.get()); |
| 328 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) | 328 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) |
| 329 ASSERT_LE(format_hi.ImageAllocationSize(), buffer2->size()); | 329 ASSERT_LE(format_hi.ImageAllocationSize(), buffer2->mapped_size()); |
| 330 ASSERT_EQ(3, buffer2->id()); | 330 ASSERT_EQ(3, buffer2->id()); |
| 331 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 331 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 332 void* const memory_pointer_hi = buffer2->data(); | 332 void* const memory_pointer_hi = buffer2->data(); |
| 333 buffer2.reset(); // Frees it. | 333 buffer2.reset(); // Frees it. |
| 334 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); | 334 ExpectDroppedId(VideoCaptureBufferPool::kInvalidId); |
| 335 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 335 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 336 buffer2 = ReserveBuffer(size_lo, GetParam()); | 336 buffer2 = ReserveBuffer(size_lo, GetParam()); |
| 337 void* const memory_pointer_lo = buffer2->data(); | 337 void* const memory_pointer_lo = buffer2->data(); |
| 338 ASSERT_EQ(memory_pointer_hi, memory_pointer_lo) | 338 ASSERT_EQ(memory_pointer_hi, memory_pointer_lo) |
| 339 << "Decrease in resolution should not reallocate buffer"; | 339 << "Decrease in resolution should not reallocate buffer"; |
| 340 ASSERT_NE(nullptr, buffer2.get()); | 340 ASSERT_NE(nullptr, buffer2.get()); |
| 341 ASSERT_EQ(3, buffer2->id()); | 341 ASSERT_EQ(3, buffer2->id()); |
| 342 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) | 342 if (GetParam().pixel_storage != media::PIXEL_STORAGE_GPUMEMORYBUFFER) |
| 343 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->size()); | 343 ASSERT_LE(format_lo.ImageAllocationSize(), buffer2->mapped_size()); |
| 344 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 344 ASSERT_EQ(3.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 345 ASSERT_FALSE(ReserveBuffer(size_lo, GetParam())) << "Pool should be empty"; | 345 ASSERT_FALSE(ReserveBuffer(size_lo, GetParam())) << "Pool should be empty"; |
| 346 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); | 346 ASSERT_EQ(1.0, pool_->GetBufferPoolUtilization()); |
| 347 | 347 |
| 348 // Tear down the pool_, writing into the buffers. The buffer should preserve | 348 // Tear down the pool_, writing into the buffers. The buffer should preserve |
| 349 // the lifetime of the underlying memory. | 349 // the lifetime of the underlying memory. |
| 350 buffer3.reset(); | 350 buffer3.reset(); |
| 351 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); | 351 ASSERT_EQ(2.0 / kTestBufferPoolSize, pool_->GetBufferPoolUtilization()); |
| 352 pool_ = NULL; | 352 pool_ = NULL; |
| 353 | 353 |
| 354 // Touch the memory. | 354 // Touch the memory. |
| 355 if (buffer2->data() != nullptr) | 355 if (buffer2->data() != nullptr) |
| 356 memset(buffer2->data(), 0x22, buffer2->size()); | 356 memset(buffer2->data(), 0x22, buffer2->mapped_size()); |
| 357 if (buffer4->data() != nullptr) | 357 if (buffer4->data() != nullptr) |
| 358 memset(buffer4->data(), 0x55, buffer4->size()); | 358 memset(buffer4->data(), 0x55, buffer4->mapped_size()); |
| 359 buffer2.reset(); | 359 buffer2.reset(); |
| 360 | 360 |
| 361 if (buffer4->data() != nullptr) | 361 if (buffer4->data() != nullptr) |
| 362 memset(buffer4->data(), 0x77, buffer4->size()); | 362 memset(buffer4->data(), 0x77, buffer4->mapped_size()); |
| 363 buffer4.reset(); | 363 buffer4.reset(); |
| 364 } | 364 } |
| 365 | 365 |
| 366 INSTANTIATE_TEST_CASE_P(, | 366 INSTANTIATE_TEST_CASE_P(, |
| 367 VideoCaptureBufferPoolTest, | 367 VideoCaptureBufferPoolTest, |
| 368 testing::ValuesIn(kCapturePixelFormatAndStorages)); | 368 testing::ValuesIn(kCapturePixelFormatAndStorages)); |
| 369 | 369 |
| 370 } // namespace content | 370 } // namespace content |
| OLD | NEW |