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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" | 10 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" |
11 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" | 11 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "media/base/video_frame.h" | 13 #include "media/base/video_frame.h" |
14 | 14 |
15 using media::VideoFrame; | 15 using media::VideoFrame; |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 const int VideoCaptureBufferPool::kInvalidId = -1; | 19 const int VideoCaptureBufferPool::kInvalidId = -1; |
20 | 20 |
21 VideoFrame::Format VideoPixelFormatToVideoFrameFormat( | 21 VideoFrame::Format VideoPixelFormatToVideoFrameFormat( |
22 media::VideoPixelFormat pixel_format) { | 22 media::VideoPixelFormat pixel_format) { |
23 static struct { | 23 static struct { |
24 media::VideoPixelFormat pixel_format; | 24 media::VideoPixelFormat pixel_format; |
25 VideoFrame::Format frame_format; | 25 VideoFrame::Format frame_format; |
26 } const kVideoPixelFormatToVideoFrameFormat[] = { | 26 } const kVideoPixelFormatToVideoFrameFormat[] = { |
27 {media::PIXEL_FORMAT_I420, VideoFrame::I420}, | 27 {media::PIXEL_FORMAT_I420, VideoFrame::I420}, |
28 {media::PIXEL_FORMAT_TEXTURE, VideoFrame::ARGB}, | 28 {media::PIXEL_FORMAT_ARGB, VideoFrame::ARGB}, |
29 {media::PIXEL_FORMAT_GPUMEMORYBUFFER, VideoFrame::ARGB}, | |
30 }; | 29 }; |
31 | 30 |
32 for (const auto& format_pair : kVideoPixelFormatToVideoFrameFormat) { | 31 for (const auto& format_pair : kVideoPixelFormatToVideoFrameFormat) { |
33 if (format_pair.pixel_format == pixel_format) | 32 if (format_pair.pixel_format == pixel_format) |
34 return format_pair.frame_format; | 33 return format_pair.frame_format; |
35 } | 34 } |
36 LOG(ERROR) << "Unsupported VideoPixelFormat " | 35 LOG(ERROR) << "Unsupported VideoPixelFormat " |
37 << media::VideoCaptureFormat::PixelFormatToString(pixel_format); | 36 << media::VideoCaptureFormat::PixelFormatToString(pixel_format); |
38 return VideoFrame::UNKNOWN; | 37 return VideoFrame::UNKNOWN; |
39 } | 38 } |
40 | 39 |
41 VideoFrame::StorageType VideoPixelFormatToVideoFrameStorageType( | 40 VideoFrame::StorageType VideoPixelStorageToVideoFrameStorageType( |
42 media::VideoPixelFormat pixel_format) { | 41 media::VideoPixelStorage pixel_storage) { |
43 static struct { | 42 static struct { |
44 media::VideoPixelFormat pixel_format; | 43 media::VideoPixelStorage pixel_storage; |
45 VideoFrame::StorageType storage_type; | 44 VideoFrame::StorageType storage_type; |
46 } const kVideoPixelFormatToVideoFrameStorageType[] = { | 45 } const kVideoPixelStorageToVideoFrameStorageType[] = { |
47 {media::PIXEL_FORMAT_I420, VideoFrame::STORAGE_SHMEM}, | 46 {media::PIXEL_STORAGE_CPU, VideoFrame::STORAGE_SHMEM}, |
48 {media::PIXEL_FORMAT_TEXTURE, VideoFrame::STORAGE_OPAQUE}, | 47 {media::PIXEL_STORAGE_TEXTURE, VideoFrame::STORAGE_OPAQUE}, |
49 {media::PIXEL_FORMAT_GPUMEMORYBUFFER, VideoFrame::STORAGE_OPAQUE}, | 48 {media::PIXEL_STORAGE_GPUMEMORYBUFFER, |
miu
2015/06/13 00:41:37
nit: Up to you, but this feels more readable:
#if
mcasas
2015/06/16 23:14:01
Yes. And this is actually for USE_OZONE build,
not
| |
49 #if defined(OS_LINUX) | |
50 VideoFrame::STORAGE_DMABUFS}, | |
51 #else | |
52 VideoFrame::STORAGE_SHMEM}, | |
53 #endif | |
50 }; | 54 }; |
51 | 55 |
52 for (const auto& format_pair : kVideoPixelFormatToVideoFrameStorageType) { | 56 for (const auto& format_pair : kVideoPixelStorageToVideoFrameStorageType) { |
53 if (format_pair.pixel_format == pixel_format) | 57 if (format_pair.pixel_storage == pixel_storage) |
54 return format_pair.storage_type; | 58 return format_pair.storage_type; |
55 } | 59 } |
56 LOG(ERROR) << "Unsupported VideoPixelFormat " | 60 LOG(ERROR) << "Unsupported VideoPixelStorage " << pixel_storage; |
57 << media::VideoCaptureFormat::PixelFormatToString(pixel_format); | |
58 return VideoFrame::STORAGE_UNKNOWN; | 61 return VideoFrame::STORAGE_UNKNOWN; |
59 } | 62 } |
60 | 63 |
61 // A simple holder of a memory-backed buffer and accesors to it. | 64 // A simple holder of a memory-backed buffer and accesors to it. |
62 class SimpleBufferHandle final : public VideoCaptureBufferPool::BufferHandle { | 65 class SimpleBufferHandle final : public VideoCaptureBufferPool::BufferHandle { |
63 public: | 66 public: |
64 SimpleBufferHandle(void* data, size_t size, base::SharedMemoryHandle handle) | 67 SimpleBufferHandle(void* data, size_t size, base::SharedMemoryHandle handle) |
65 : data_(data), size_(size), handle_(handle) {} | 68 : data_(data), size_(size), handle_(handle) {} |
66 ~SimpleBufferHandle() override {} | 69 ~SimpleBufferHandle() override {} |
67 | 70 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 if (!tracker) { | 279 if (!tracker) { |
277 NOTREACHED() << "Invalid buffer_id."; | 280 NOTREACHED() << "Invalid buffer_id."; |
278 return scoped_ptr<BufferHandle>(); | 281 return scoped_ptr<BufferHandle>(); |
279 } | 282 } |
280 | 283 |
281 DCHECK(tracker->held_by_producer()); | 284 DCHECK(tracker->held_by_producer()); |
282 return tracker->GetBufferHandle(); | 285 return tracker->GetBufferHandle(); |
283 } | 286 } |
284 | 287 |
285 int VideoCaptureBufferPool::ReserveForProducer(media::VideoPixelFormat format, | 288 int VideoCaptureBufferPool::ReserveForProducer(media::VideoPixelFormat format, |
289 media::VideoPixelStorage storage, | |
286 const gfx::Size& dimensions, | 290 const gfx::Size& dimensions, |
287 int* buffer_id_to_drop) { | 291 int* buffer_id_to_drop) { |
288 base::AutoLock lock(lock_); | 292 base::AutoLock lock(lock_); |
289 return ReserveForProducerInternal(format, dimensions, buffer_id_to_drop); | 293 return ReserveForProducerInternal(format, storage, dimensions, |
294 buffer_id_to_drop); | |
290 } | 295 } |
291 | 296 |
292 void VideoCaptureBufferPool::RelinquishProducerReservation(int buffer_id) { | 297 void VideoCaptureBufferPool::RelinquishProducerReservation(int buffer_id) { |
293 base::AutoLock lock(lock_); | 298 base::AutoLock lock(lock_); |
294 Tracker* tracker = GetTracker(buffer_id); | 299 Tracker* tracker = GetTracker(buffer_id); |
295 if (!tracker) { | 300 if (!tracker) { |
296 NOTREACHED() << "Invalid buffer_id."; | 301 NOTREACHED() << "Invalid buffer_id."; |
297 return; | 302 return; |
298 } | 303 } |
299 DCHECK(tracker->held_by_producer()); | 304 DCHECK(tracker->held_by_producer()); |
(...skipping 27 matching lines...) Expand all Loading... | |
327 return; | 332 return; |
328 } | 333 } |
329 DCHECK_GE(tracker->consumer_hold_count(), num_clients); | 334 DCHECK_GE(tracker->consumer_hold_count(), num_clients); |
330 | 335 |
331 tracker->set_consumer_hold_count(tracker->consumer_hold_count() - | 336 tracker->set_consumer_hold_count(tracker->consumer_hold_count() - |
332 num_clients); | 337 num_clients); |
333 } | 338 } |
334 | 339 |
335 int VideoCaptureBufferPool::ReserveForProducerInternal( | 340 int VideoCaptureBufferPool::ReserveForProducerInternal( |
336 media::VideoPixelFormat format, | 341 media::VideoPixelFormat format, |
342 media::VideoPixelStorage storage, | |
337 const gfx::Size& dimensions, | 343 const gfx::Size& dimensions, |
338 int* buffer_id_to_drop) { | 344 int* buffer_id_to_drop) { |
339 DCHECK(format == media::PIXEL_FORMAT_I420 || | |
340 format == media::PIXEL_FORMAT_TEXTURE || | |
341 format == media::PIXEL_FORMAT_GPUMEMORYBUFFER ); | |
342 lock_.AssertAcquired(); | 345 lock_.AssertAcquired(); |
343 *buffer_id_to_drop = kInvalidId; | 346 *buffer_id_to_drop = kInvalidId; |
344 | 347 |
345 const size_t size_in_pixels = dimensions.GetArea(); | 348 const size_t size_in_pixels = dimensions.GetArea(); |
346 const media::VideoFrame::Format pixel_format = | 349 const media::VideoFrame::Format pixel_format = |
347 VideoPixelFormatToVideoFrameFormat(format); | 350 VideoPixelFormatToVideoFrameFormat(format); |
348 const media::VideoFrame::StorageType storage_type = | 351 const media::VideoFrame::StorageType storage_type = |
349 VideoPixelFormatToVideoFrameStorageType(format); | 352 VideoPixelStorageToVideoFrameStorageType(storage); |
350 // Look for a tracker that's allocated, big enough, and not in use. Track the | 353 // Look for a tracker that's allocated, big enough, and not in use. Track the |
351 // largest one that's not big enough, in case we have to reallocate a tracker. | 354 // largest one that's not big enough, in case we have to reallocate a tracker. |
352 *buffer_id_to_drop = kInvalidId; | 355 *buffer_id_to_drop = kInvalidId; |
353 size_t largest_size_in_pixels = 0; | 356 size_t largest_size_in_pixels = 0; |
354 TrackerMap::iterator tracker_to_drop = trackers_.end(); | 357 TrackerMap::iterator tracker_to_drop = trackers_.end(); |
355 for (TrackerMap::iterator it = trackers_.begin(); it != trackers_.end(); | 358 for (TrackerMap::iterator it = trackers_.begin(); it != trackers_.end(); |
356 ++it) { | 359 ++it) { |
357 Tracker* const tracker = it->second; | 360 Tracker* const tracker = it->second; |
358 if (!tracker->consumer_hold_count() && !tracker->held_by_producer()) { | 361 if (!tracker->consumer_hold_count() && !tracker->held_by_producer()) { |
359 if (tracker->pixel_count() >= size_in_pixels && | 362 if (tracker->pixel_count() >= size_in_pixels && |
(...skipping 18 matching lines...) Expand all Loading... | |
378 return kInvalidId; | 381 return kInvalidId; |
379 } | 382 } |
380 *buffer_id_to_drop = tracker_to_drop->first; | 383 *buffer_id_to_drop = tracker_to_drop->first; |
381 delete tracker_to_drop->second; | 384 delete tracker_to_drop->second; |
382 trackers_.erase(tracker_to_drop); | 385 trackers_.erase(tracker_to_drop); |
383 } | 386 } |
384 | 387 |
385 // Create the new tracker. | 388 // Create the new tracker. |
386 const int buffer_id = next_buffer_id_++; | 389 const int buffer_id = next_buffer_id_++; |
387 | 390 |
388 scoped_ptr<Tracker> tracker = | 391 const bool use_simple_tracker = |
389 Tracker::CreateTracker(format == media::PIXEL_FORMAT_GPUMEMORYBUFFER); | 392 storage == media::PIXEL_STORAGE_GPUMEMORYBUFFER; |
393 scoped_ptr<Tracker> tracker = Tracker::CreateTracker(use_simple_tracker); | |
390 if (!tracker->Init(pixel_format, storage_type, dimensions)) { | 394 if (!tracker->Init(pixel_format, storage_type, dimensions)) { |
391 DLOG(ERROR) << "Error initializing Tracker"; | 395 DLOG(ERROR) << "Error initializing Tracker"; |
392 return kInvalidId; | 396 return kInvalidId; |
393 } | 397 } |
394 tracker->set_held_by_producer(true); | 398 tracker->set_held_by_producer(true); |
395 trackers_[buffer_id] = tracker.release(); | 399 trackers_[buffer_id] = tracker.release(); |
396 | 400 |
397 return buffer_id; | 401 return buffer_id; |
398 } | 402 } |
399 | 403 |
400 VideoCaptureBufferPool::Tracker* VideoCaptureBufferPool::GetTracker( | 404 VideoCaptureBufferPool::Tracker* VideoCaptureBufferPool::GetTracker( |
401 int buffer_id) { | 405 int buffer_id) { |
402 TrackerMap::const_iterator it = trackers_.find(buffer_id); | 406 TrackerMap::const_iterator it = trackers_.find(buffer_id); |
403 return (it == trackers_.end()) ? NULL : it->second; | 407 return (it == trackers_.end()) ? NULL : it->second; |
404 } | 408 } |
405 | 409 |
406 } // namespace content | 410 } // namespace content |
OLD | NEW |