| 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 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 5 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | 13 #include "content/browser/renderer_host/media/video_capture_buffer_handle.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/browser/renderer_host/media/video_capture_buffer_tracker.h" |
| 15 #include "content/browser/renderer_host/media/video_capture_buffer_tracker_facto
ry.h" |
| 15 #include "ui/gfx/buffer_format_util.h" | 16 #include "ui/gfx/buffer_format_util.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 // Tracker specifics for SharedMemory. | |
| 20 class VideoCaptureBufferPoolImpl::SharedMemTracker final : public Tracker { | |
| 21 public: | |
| 22 SharedMemTracker() : Tracker() {} | |
| 23 | |
| 24 bool Init(const gfx::Size& dimensions, | |
| 25 media::VideoPixelFormat format, | |
| 26 media::VideoPixelStorage storage_type, | |
| 27 base::Lock* lock) override { | |
| 28 DVLOG(2) << "allocating ShMem of " << dimensions.ToString(); | |
| 29 set_dimensions(dimensions); | |
| 30 // |dimensions| can be 0x0 for trackers that do not require memory backing. | |
| 31 set_max_pixel_count(dimensions.GetArea()); | |
| 32 set_pixel_format(format); | |
| 33 set_storage_type(storage_type); | |
| 34 mapped_size_ = | |
| 35 media::VideoCaptureFormat(dimensions, 0.0f, format, storage_type) | |
| 36 .ImageAllocationSize(); | |
| 37 if (!mapped_size_) | |
| 38 return true; | |
| 39 return shared_memory_.CreateAndMapAnonymous(mapped_size_); | |
| 40 } | |
| 41 | |
| 42 std::unique_ptr<BufferHandle> GetBufferHandle() override { | |
| 43 return base::MakeUnique<SharedMemBufferHandle>(this); | |
| 44 } | |
| 45 bool ShareToProcess(base::ProcessHandle process_handle, | |
| 46 base::SharedMemoryHandle* new_handle) override { | |
| 47 return shared_memory_.ShareToProcess(process_handle, new_handle); | |
| 48 } | |
| 49 bool ShareToProcess2(int plane, | |
| 50 base::ProcessHandle process_handle, | |
| 51 gfx::GpuMemoryBufferHandle* new_handle) override { | |
| 52 NOTREACHED(); | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 // A simple proxy that implements the BufferHandle interface, providing | |
| 58 // accessors to SharedMemTracker's memory and properties. | |
| 59 class SharedMemBufferHandle | |
| 60 : public VideoCaptureBufferPoolImpl::BufferHandle { | |
| 61 public: | |
| 62 // |tracker| must outlive SimpleBufferHandle. This is ensured since a | |
| 63 // tracker is pinned until ownership of this SimpleBufferHandle is returned | |
| 64 // to VideoCaptureBufferPoolImpl. | |
| 65 explicit SharedMemBufferHandle(SharedMemTracker* tracker) | |
| 66 : tracker_(tracker) {} | |
| 67 ~SharedMemBufferHandle() override {} | |
| 68 | |
| 69 gfx::Size dimensions() const override { return tracker_->dimensions(); } | |
| 70 size_t mapped_size() const override { return tracker_->mapped_size_; } | |
| 71 void* data(int plane) override { | |
| 72 DCHECK_EQ(plane, 0); | |
| 73 return tracker_->shared_memory_.memory(); | |
| 74 } | |
| 75 ClientBuffer AsClientBuffer(int plane) override { | |
| 76 NOTREACHED(); | |
| 77 return nullptr; | |
| 78 } | |
| 79 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 80 base::FileDescriptor AsPlatformFile() override { | |
| 81 return tracker_->shared_memory_.handle(); | |
| 82 } | |
| 83 #endif | |
| 84 | |
| 85 private: | |
| 86 SharedMemTracker* const tracker_; | |
| 87 }; | |
| 88 | |
| 89 // The memory created to be shared with renderer processes. | |
| 90 base::SharedMemory shared_memory_; | |
| 91 size_t mapped_size_; | |
| 92 }; | |
| 93 | |
| 94 // Tracker specifics for GpuMemoryBuffer. Owns GpuMemoryBuffers and its | |
| 95 // associated pixel dimensions. | |
| 96 class VideoCaptureBufferPoolImpl::GpuMemoryBufferTracker final | |
| 97 : public Tracker { | |
| 98 public: | |
| 99 GpuMemoryBufferTracker() : Tracker() {} | |
| 100 | |
| 101 ~GpuMemoryBufferTracker() override { | |
| 102 for (const auto& gmb : gpu_memory_buffers_) | |
| 103 gmb->Unmap(); | |
| 104 } | |
| 105 | |
| 106 bool Init(const gfx::Size& dimensions, | |
| 107 media::VideoPixelFormat format, | |
| 108 media::VideoPixelStorage storage_type, | |
| 109 base::Lock* lock) override { | |
| 110 DVLOG(2) << "allocating GMB for " << dimensions.ToString(); | |
| 111 // BrowserGpuMemoryBufferManager::current() may not be accessed on IO | |
| 112 // Thread. | |
| 113 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 114 DCHECK(BrowserGpuMemoryBufferManager::current()); | |
| 115 // This class is only expected to be called with I420 buffer requests at | |
| 116 // this point. | |
| 117 DCHECK_EQ(format, media::PIXEL_FORMAT_I420); | |
| 118 set_dimensions(dimensions); | |
| 119 set_max_pixel_count(dimensions.GetArea()); | |
| 120 set_pixel_format(format); | |
| 121 set_storage_type(storage_type); | |
| 122 // |dimensions| can be 0x0 for trackers that do not require memory backing. | |
| 123 if (dimensions.GetArea() == 0u) | |
| 124 return true; | |
| 125 | |
| 126 base::AutoUnlock auto_unlock(*lock); | |
| 127 const size_t num_planes = media::VideoFrame::NumPlanes(pixel_format()); | |
| 128 for (size_t i = 0; i < num_planes; ++i) { | |
| 129 const gfx::Size& size = | |
| 130 media::VideoFrame::PlaneSize(pixel_format(), i, dimensions); | |
| 131 gpu_memory_buffers_.push_back( | |
| 132 BrowserGpuMemoryBufferManager::current()->AllocateGpuMemoryBuffer( | |
| 133 size, gfx::BufferFormat::R_8, | |
| 134 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE, | |
| 135 gpu::kNullSurfaceHandle)); | |
| 136 | |
| 137 DLOG_IF(ERROR, !gpu_memory_buffers_[i]) << "Allocating GpuMemoryBuffer"; | |
| 138 if (!gpu_memory_buffers_[i] || !gpu_memory_buffers_[i]->Map()) | |
| 139 return false; | |
| 140 } | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 std::unique_ptr<BufferHandle> GetBufferHandle() override { | |
| 145 DCHECK_EQ(gpu_memory_buffers_.size(), | |
| 146 media::VideoFrame::NumPlanes(pixel_format())); | |
| 147 return base::MakeUnique<GpuMemoryBufferBufferHandle>(this); | |
| 148 } | |
| 149 | |
| 150 bool ShareToProcess(base::ProcessHandle process_handle, | |
| 151 base::SharedMemoryHandle* new_handle) override { | |
| 152 NOTREACHED(); | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 bool ShareToProcess2(int plane, | |
| 157 base::ProcessHandle process_handle, | |
| 158 gfx::GpuMemoryBufferHandle* new_handle) override { | |
| 159 DCHECK_LE(plane, static_cast<int>(gpu_memory_buffers_.size())); | |
| 160 | |
| 161 const auto& current_gmb_handle = gpu_memory_buffers_[plane]->GetHandle(); | |
| 162 switch (current_gmb_handle.type) { | |
| 163 case gfx::EMPTY_BUFFER: | |
| 164 NOTREACHED(); | |
| 165 return false; | |
| 166 case gfx::SHARED_MEMORY_BUFFER: { | |
| 167 DCHECK(base::SharedMemory::IsHandleValid(current_gmb_handle.handle)); | |
| 168 base::SharedMemory shared_memory( | |
| 169 base::SharedMemory::DuplicateHandle(current_gmb_handle.handle), | |
| 170 false); | |
| 171 shared_memory.ShareToProcess(process_handle, &new_handle->handle); | |
| 172 DCHECK(base::SharedMemory::IsHandleValid(new_handle->handle)); | |
| 173 new_handle->type = gfx::SHARED_MEMORY_BUFFER; | |
| 174 return true; | |
| 175 } | |
| 176 case gfx::IO_SURFACE_BUFFER: | |
| 177 case gfx::OZONE_NATIVE_PIXMAP: | |
| 178 *new_handle = current_gmb_handle; | |
| 179 return true; | |
| 180 } | |
| 181 NOTREACHED(); | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 private: | |
| 186 // A simple proxy that implements the BufferHandle interface, providing | |
| 187 // accessors to GpuMemoryBufferTracker's memory and properties. | |
| 188 class GpuMemoryBufferBufferHandle | |
| 189 : public VideoCaptureBufferPoolImpl::BufferHandle { | |
| 190 public: | |
| 191 // |tracker| must outlive GpuMemoryBufferBufferHandle. This is ensured since | |
| 192 // a tracker is pinned until ownership of this GpuMemoryBufferBufferHandle | |
| 193 // is returned to VideoCaptureBufferPoolImpl. | |
| 194 explicit GpuMemoryBufferBufferHandle(GpuMemoryBufferTracker* tracker) | |
| 195 : tracker_(tracker) {} | |
| 196 ~GpuMemoryBufferBufferHandle() override {} | |
| 197 | |
| 198 gfx::Size dimensions() const override { return tracker_->dimensions(); } | |
| 199 size_t mapped_size() const override { | |
| 200 return tracker_->dimensions().GetArea(); | |
| 201 } | |
| 202 void* data(int plane) override { | |
| 203 DCHECK_GE(plane, 0); | |
| 204 DCHECK_LT(plane, static_cast<int>(tracker_->gpu_memory_buffers_.size())); | |
| 205 DCHECK(tracker_->gpu_memory_buffers_[plane]); | |
| 206 return tracker_->gpu_memory_buffers_[plane]->memory(0); | |
| 207 } | |
| 208 ClientBuffer AsClientBuffer(int plane) override { | |
| 209 DCHECK_GE(plane, 0); | |
| 210 DCHECK_LT(plane, static_cast<int>(tracker_->gpu_memory_buffers_.size())); | |
| 211 return tracker_->gpu_memory_buffers_[plane]->AsClientBuffer(); | |
| 212 } | |
| 213 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 214 base::FileDescriptor AsPlatformFile() override { | |
| 215 NOTREACHED(); | |
| 216 return base::FileDescriptor(); | |
| 217 } | |
| 218 #endif | |
| 219 | |
| 220 private: | |
| 221 GpuMemoryBufferTracker* const tracker_; | |
| 222 }; | |
| 223 | |
| 224 // Owned references to GpuMemoryBuffers. | |
| 225 std::vector<std::unique_ptr<gfx::GpuMemoryBuffer>> gpu_memory_buffers_; | |
| 226 }; | |
| 227 | |
| 228 // static | |
| 229 std::unique_ptr<VideoCaptureBufferPoolImpl::Tracker> | |
| 230 VideoCaptureBufferPoolImpl::Tracker::CreateTracker( | |
| 231 media::VideoPixelStorage storage) { | |
| 232 switch (storage) { | |
| 233 case media::PIXEL_STORAGE_GPUMEMORYBUFFER: | |
| 234 return base::MakeUnique<GpuMemoryBufferTracker>(); | |
| 235 case media::PIXEL_STORAGE_CPU: | |
| 236 return base::MakeUnique<SharedMemTracker>(); | |
| 237 } | |
| 238 NOTREACHED(); | |
| 239 return std::unique_ptr<VideoCaptureBufferPoolImpl::Tracker>(); | |
| 240 } | |
| 241 | |
| 242 VideoCaptureBufferPoolImpl::Tracker::~Tracker() {} | |
| 243 | |
| 244 VideoCaptureBufferPoolImpl::VideoCaptureBufferPoolImpl(int count) | 20 VideoCaptureBufferPoolImpl::VideoCaptureBufferPoolImpl(int count) |
| 245 : count_(count), | 21 : count_(count), |
| 246 next_buffer_id_(0), | 22 next_buffer_id_(0), |
| 247 last_relinquished_buffer_id_(kInvalidId) { | 23 last_relinquished_buffer_id_(kInvalidId) { |
| 248 DCHECK_GT(count, 0); | 24 DCHECK_GT(count, 0); |
| 249 } | 25 } |
| 250 | 26 |
| 251 VideoCaptureBufferPoolImpl::~VideoCaptureBufferPoolImpl() { | 27 VideoCaptureBufferPoolImpl::~VideoCaptureBufferPoolImpl() { |
| 252 base::STLDeleteValues(&trackers_); | 28 base::STLDeleteValues(&trackers_); |
| 253 } | 29 } |
| 254 | 30 |
| 255 bool VideoCaptureBufferPoolImpl::ShareToProcess( | 31 bool VideoCaptureBufferPoolImpl::ShareToProcess( |
| 256 int buffer_id, | 32 int buffer_id, |
| 257 base::ProcessHandle process_handle, | 33 base::ProcessHandle process_handle, |
| 258 base::SharedMemoryHandle* new_handle) { | 34 base::SharedMemoryHandle* new_handle) { |
| 259 base::AutoLock lock(lock_); | 35 base::AutoLock lock(lock_); |
| 260 | 36 |
| 261 Tracker* tracker = GetTracker(buffer_id); | 37 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 262 if (!tracker) { | 38 if (!tracker) { |
| 263 NOTREACHED() << "Invalid buffer_id."; | 39 NOTREACHED() << "Invalid buffer_id."; |
| 264 return false; | 40 return false; |
| 265 } | 41 } |
| 266 if (tracker->ShareToProcess(process_handle, new_handle)) | 42 if (tracker->ShareToProcess(process_handle, new_handle)) |
| 267 return true; | 43 return true; |
| 268 DPLOG(ERROR) << "Error mapping memory"; | 44 DPLOG(ERROR) << "Error mapping memory"; |
| 269 return false; | 45 return false; |
| 270 } | 46 } |
| 271 | 47 |
| 272 bool VideoCaptureBufferPoolImpl::ShareToProcess2( | 48 bool VideoCaptureBufferPoolImpl::ShareToProcess2( |
| 273 int buffer_id, | 49 int buffer_id, |
| 274 int plane, | 50 int plane, |
| 275 base::ProcessHandle process_handle, | 51 base::ProcessHandle process_handle, |
| 276 gfx::GpuMemoryBufferHandle* new_handle) { | 52 gfx::GpuMemoryBufferHandle* new_handle) { |
| 277 base::AutoLock lock(lock_); | 53 base::AutoLock lock(lock_); |
| 278 | 54 |
| 279 Tracker* tracker = GetTracker(buffer_id); | 55 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 280 if (!tracker) { | 56 if (!tracker) { |
| 281 NOTREACHED() << "Invalid buffer_id."; | 57 NOTREACHED() << "Invalid buffer_id."; |
| 282 return false; | 58 return false; |
| 283 } | 59 } |
| 284 if (tracker->ShareToProcess2(plane, process_handle, new_handle)) | 60 if (tracker->ShareToProcess2(plane, process_handle, new_handle)) |
| 285 return true; | 61 return true; |
| 286 DPLOG(ERROR) << "Error mapping memory"; | 62 DPLOG(ERROR) << "Error mapping memory"; |
| 287 return false; | 63 return false; |
| 288 } | 64 } |
| 289 | 65 |
| 290 std::unique_ptr<VideoCaptureBufferPoolImpl::BufferHandle> | 66 std::unique_ptr<VideoCaptureBufferHandle> |
| 291 VideoCaptureBufferPoolImpl::GetBufferHandle(int buffer_id) { | 67 VideoCaptureBufferPoolImpl::GetBufferHandle(int buffer_id) { |
| 292 base::AutoLock lock(lock_); | 68 base::AutoLock lock(lock_); |
| 293 | 69 |
| 294 Tracker* tracker = GetTracker(buffer_id); | 70 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 295 if (!tracker) { | 71 if (!tracker) { |
| 296 NOTREACHED() << "Invalid buffer_id."; | 72 NOTREACHED() << "Invalid buffer_id."; |
| 297 return std::unique_ptr<BufferHandle>(); | 73 return std::unique_ptr<VideoCaptureBufferHandle>(); |
| 298 } | 74 } |
| 299 | 75 |
| 300 DCHECK(tracker->held_by_producer()); | 76 DCHECK(tracker->held_by_producer()); |
| 301 return tracker->GetBufferHandle(); | 77 return tracker->GetBufferHandle(); |
| 302 } | 78 } |
| 303 | 79 |
| 304 int VideoCaptureBufferPoolImpl::ReserveForProducer( | 80 int VideoCaptureBufferPoolImpl::ReserveForProducer( |
| 305 const gfx::Size& dimensions, | 81 const gfx::Size& dimensions, |
| 306 media::VideoPixelFormat format, | 82 media::VideoPixelFormat format, |
| 307 media::VideoPixelStorage storage, | 83 media::VideoPixelStorage storage, |
| 308 int* buffer_id_to_drop) { | 84 int* buffer_id_to_drop) { |
| 309 base::AutoLock lock(lock_); | 85 base::AutoLock lock(lock_); |
| 310 return ReserveForProducerInternal(dimensions, format, storage, | 86 return ReserveForProducerInternal(dimensions, format, storage, |
| 311 buffer_id_to_drop); | 87 buffer_id_to_drop); |
| 312 } | 88 } |
| 313 | 89 |
| 314 void VideoCaptureBufferPoolImpl::RelinquishProducerReservation(int buffer_id) { | 90 void VideoCaptureBufferPoolImpl::RelinquishProducerReservation(int buffer_id) { |
| 315 base::AutoLock lock(lock_); | 91 base::AutoLock lock(lock_); |
| 316 Tracker* tracker = GetTracker(buffer_id); | 92 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 317 if (!tracker) { | 93 if (!tracker) { |
| 318 NOTREACHED() << "Invalid buffer_id."; | 94 NOTREACHED() << "Invalid buffer_id."; |
| 319 return; | 95 return; |
| 320 } | 96 } |
| 321 DCHECK(tracker->held_by_producer()); | 97 DCHECK(tracker->held_by_producer()); |
| 322 tracker->set_held_by_producer(false); | 98 tracker->set_held_by_producer(false); |
| 323 last_relinquished_buffer_id_ = buffer_id; | 99 last_relinquished_buffer_id_ = buffer_id; |
| 324 } | 100 } |
| 325 | 101 |
| 326 void VideoCaptureBufferPoolImpl::HoldForConsumers(int buffer_id, | 102 void VideoCaptureBufferPoolImpl::HoldForConsumers(int buffer_id, |
| 327 int num_clients) { | 103 int num_clients) { |
| 328 base::AutoLock lock(lock_); | 104 base::AutoLock lock(lock_); |
| 329 Tracker* tracker = GetTracker(buffer_id); | 105 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 330 if (!tracker) { | 106 if (!tracker) { |
| 331 NOTREACHED() << "Invalid buffer_id."; | 107 NOTREACHED() << "Invalid buffer_id."; |
| 332 return; | 108 return; |
| 333 } | 109 } |
| 334 DCHECK(tracker->held_by_producer()); | 110 DCHECK(tracker->held_by_producer()); |
| 335 DCHECK(!tracker->consumer_hold_count()); | 111 DCHECK(!tracker->consumer_hold_count()); |
| 336 | 112 |
| 337 tracker->set_consumer_hold_count(num_clients); | 113 tracker->set_consumer_hold_count(num_clients); |
| 338 // Note: |held_by_producer()| will stay true until | 114 // Note: |held_by_producer()| will stay true until |
| 339 // RelinquishProducerReservation() (usually called by destructor of the object | 115 // RelinquishProducerReservation() (usually called by destructor of the object |
| 340 // wrapping this tracker, e.g. a media::VideoFrame). | 116 // wrapping this tracker, e.g. a media::VideoFrame). |
| 341 } | 117 } |
| 342 | 118 |
| 343 void VideoCaptureBufferPoolImpl::RelinquishConsumerHold(int buffer_id, | 119 void VideoCaptureBufferPoolImpl::RelinquishConsumerHold(int buffer_id, |
| 344 int num_clients) { | 120 int num_clients) { |
| 345 base::AutoLock lock(lock_); | 121 base::AutoLock lock(lock_); |
| 346 Tracker* tracker = GetTracker(buffer_id); | 122 VideoCaptureBufferTracker* tracker = GetTracker(buffer_id); |
| 347 if (!tracker) { | 123 if (!tracker) { |
| 348 NOTREACHED() << "Invalid buffer_id."; | 124 NOTREACHED() << "Invalid buffer_id."; |
| 349 return; | 125 return; |
| 350 } | 126 } |
| 351 DCHECK_GE(tracker->consumer_hold_count(), num_clients); | 127 DCHECK_GE(tracker->consumer_hold_count(), num_clients); |
| 352 | 128 |
| 353 tracker->set_consumer_hold_count(tracker->consumer_hold_count() - | 129 tracker->set_consumer_hold_count(tracker->consumer_hold_count() - |
| 354 num_clients); | 130 num_clients); |
| 355 } | 131 } |
| 356 | 132 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 381 return resurrected_buffer_id; | 157 return resurrected_buffer_id; |
| 382 } | 158 } |
| 383 | 159 |
| 384 return kInvalidId; | 160 return kInvalidId; |
| 385 } | 161 } |
| 386 | 162 |
| 387 double VideoCaptureBufferPoolImpl::GetBufferPoolUtilization() const { | 163 double VideoCaptureBufferPoolImpl::GetBufferPoolUtilization() const { |
| 388 base::AutoLock lock(lock_); | 164 base::AutoLock lock(lock_); |
| 389 int num_buffers_held = 0; | 165 int num_buffers_held = 0; |
| 390 for (const auto& entry : trackers_) { | 166 for (const auto& entry : trackers_) { |
| 391 Tracker* const tracker = entry.second; | 167 VideoCaptureBufferTracker* const tracker = entry.second; |
| 392 if (tracker->held_by_producer() || tracker->consumer_hold_count() > 0) | 168 if (tracker->held_by_producer() || tracker->consumer_hold_count() > 0) |
| 393 ++num_buffers_held; | 169 ++num_buffers_held; |
| 394 } | 170 } |
| 395 return static_cast<double>(num_buffers_held) / count_; | 171 return static_cast<double>(num_buffers_held) / count_; |
| 396 } | 172 } |
| 397 | 173 |
| 398 int VideoCaptureBufferPoolImpl::ReserveForProducerInternal( | 174 int VideoCaptureBufferPoolImpl::ReserveForProducerInternal( |
| 399 const gfx::Size& dimensions, | 175 const gfx::Size& dimensions, |
| 400 media::VideoPixelFormat pixel_format, | 176 media::VideoPixelFormat pixel_format, |
| 401 media::VideoPixelStorage storage_type, | 177 media::VideoPixelStorage storage_type, |
| 402 int* buffer_id_to_drop) { | 178 int* buffer_id_to_drop) { |
| 403 lock_.AssertAcquired(); | 179 lock_.AssertAcquired(); |
| 404 | 180 |
| 405 const size_t size_in_pixels = dimensions.GetArea(); | 181 const size_t size_in_pixels = dimensions.GetArea(); |
| 406 // Look for a tracker that's allocated, big enough, and not in use. Track the | 182 // Look for a tracker that's allocated, big enough, and not in use. Track the |
| 407 // largest one that's not big enough, in case we have to reallocate a tracker. | 183 // largest one that's not big enough, in case we have to reallocate a tracker. |
| 408 *buffer_id_to_drop = kInvalidId; | 184 *buffer_id_to_drop = kInvalidId; |
| 409 size_t largest_size_in_pixels = 0; | 185 size_t largest_size_in_pixels = 0; |
| 410 TrackerMap::iterator tracker_of_last_resort = trackers_.end(); | 186 TrackerMap::iterator tracker_of_last_resort = trackers_.end(); |
| 411 TrackerMap::iterator tracker_to_drop = trackers_.end(); | 187 TrackerMap::iterator tracker_to_drop = trackers_.end(); |
| 412 for (TrackerMap::iterator it = trackers_.begin(); it != trackers_.end(); | 188 for (TrackerMap::iterator it = trackers_.begin(); it != trackers_.end(); |
| 413 ++it) { | 189 ++it) { |
| 414 Tracker* const tracker = it->second; | 190 VideoCaptureBufferTracker* const tracker = it->second; |
| 415 if (!tracker->consumer_hold_count() && !tracker->held_by_producer()) { | 191 if (!tracker->consumer_hold_count() && !tracker->held_by_producer()) { |
| 416 if (tracker->max_pixel_count() >= size_in_pixels && | 192 if (tracker->max_pixel_count() >= size_in_pixels && |
| 417 (tracker->pixel_format() == pixel_format) && | 193 (tracker->pixel_format() == pixel_format) && |
| 418 (tracker->storage_type() == storage_type)) { | 194 (tracker->storage_type() == storage_type)) { |
| 419 if (it->first == last_relinquished_buffer_id_) { | 195 if (it->first == last_relinquished_buffer_id_) { |
| 420 // This buffer would do just fine, but avoid returning it because the | 196 // This buffer would do just fine, but avoid returning it because the |
| 421 // client may want to resurrect it. It will be returned perforce if | 197 // client may want to resurrect it. It will be returned perforce if |
| 422 // the pool has reached it's maximum limit (see code below). | 198 // the pool has reached it's maximum limit (see code below). |
| 423 tracker_of_last_resort = it; | 199 tracker_of_last_resort = it; |
| 424 continue; | 200 continue; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 452 if (tracker_to_drop->first == last_relinquished_buffer_id_) | 228 if (tracker_to_drop->first == last_relinquished_buffer_id_) |
| 453 last_relinquished_buffer_id_ = kInvalidId; | 229 last_relinquished_buffer_id_ = kInvalidId; |
| 454 *buffer_id_to_drop = tracker_to_drop->first; | 230 *buffer_id_to_drop = tracker_to_drop->first; |
| 455 delete tracker_to_drop->second; | 231 delete tracker_to_drop->second; |
| 456 trackers_.erase(tracker_to_drop); | 232 trackers_.erase(tracker_to_drop); |
| 457 } | 233 } |
| 458 | 234 |
| 459 // Create the new tracker. | 235 // Create the new tracker. |
| 460 const int buffer_id = next_buffer_id_++; | 236 const int buffer_id = next_buffer_id_++; |
| 461 | 237 |
| 462 std::unique_ptr<Tracker> tracker = Tracker::CreateTracker(storage_type); | 238 std::unique_ptr<VideoCaptureBufferTracker> tracker = |
| 239 VideoCaptureBufferTrackerFactory::CreateTracker(storage_type); |
| 463 // TODO(emircan): We pass the lock here to solve GMB allocation issue, see | 240 // TODO(emircan): We pass the lock here to solve GMB allocation issue, see |
| 464 // crbug.com/545238. | 241 // crbug.com/545238. |
| 465 if (!tracker->Init(dimensions, pixel_format, storage_type, &lock_)) { | 242 if (!tracker->Init(dimensions, pixel_format, storage_type, &lock_)) { |
| 466 DLOG(ERROR) << "Error initializing Tracker"; | 243 DLOG(ERROR) << "Error initializing VideoCaptureBufferTracker"; |
| 467 return kInvalidId; | 244 return kInvalidId; |
| 468 } | 245 } |
| 469 | 246 |
| 470 tracker->set_held_by_producer(true); | 247 tracker->set_held_by_producer(true); |
| 471 trackers_[buffer_id] = tracker.release(); | 248 trackers_[buffer_id] = tracker.release(); |
| 472 | 249 |
| 473 return buffer_id; | 250 return buffer_id; |
| 474 } | 251 } |
| 475 | 252 |
| 476 VideoCaptureBufferPoolImpl::Tracker* VideoCaptureBufferPoolImpl::GetTracker( | 253 VideoCaptureBufferTracker* VideoCaptureBufferPoolImpl::GetTracker( |
| 477 int buffer_id) { | 254 int buffer_id) { |
| 478 TrackerMap::const_iterator it = trackers_.find(buffer_id); | 255 TrackerMap::const_iterator it = trackers_.find(buffer_id); |
| 479 return (it == trackers_.end()) ? NULL : it->second; | 256 return (it == trackers_.end()) ? NULL : it->second; |
| 480 } | 257 } |
| 481 | 258 |
| 482 } // namespace content | 259 } // namespace content |
| OLD | NEW |