| 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_vector.h" |
| 12 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 13 #include "base/process/process.h" | 14 #include "base/process/process.h" |
| 14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 16 #include "media/base/video_capture_types.h" | 17 #include "media/base/video_capture_types.h" |
| 17 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 18 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 19 #include "ui/gfx/gpu_memory_buffer.h" | |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 // A thread-safe class that does the bookkeeping and lifetime management for a | 23 // A thread-safe class that does the bookkeeping and lifetime management for a |
| 24 // pool of pixel buffers cycled between an in-process producer (e.g. a | 24 // pool of pixel buffers cycled between an in-process producer (e.g. a |
| 25 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is | 25 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is |
| 26 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed | 26 // intended to be orchestrated by a VideoCaptureController, but is designed |
| 27 // to outlive the controller if necessary. The pixel buffers may be backed by a | 27 // to outlive the controller if necessary. The pixel buffers may be backed by a |
| 28 // SharedMemory, but this is not compulsory. | 28 // SharedMemory, but this is not compulsory. |
| 29 // | 29 // |
| 30 // Producers get a buffer by calling ReserveForProducer(), and may pass on their | 30 // Producers get a buffer by calling ReserveForProducer(), and may pass on their |
| 31 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer | 31 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer |
| 32 // (without further processing) by calling RelinquishProducerReservation(). | 32 // (without further processing) by calling RelinquishProducerReservation(). |
| 33 // Consumers signal that they are done with the buffer by calling | 33 // Consumers signal that they are done with the buffer by calling |
| 34 // RelinquishConsumerHold(). | 34 // RelinquishConsumerHold(). |
| 35 // | 35 // |
| 36 // Buffers are allocated on demand, but there will never be more than |count| | 36 // Buffers are allocated on demand, but there will never be more than |count| |
| 37 // buffers in existence at any time. Buffers are identified by an int value | 37 // buffers in existence at any time. Buffers are identified by an int value |
| 38 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | 38 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by |
| 39 // some methods to indicate failure. The active set of buffer ids may change | 39 // some methods to indicate failure. The active set of buffer ids may change |
| 40 // over the lifetime of the buffer pool, as existing buffers are freed and | 40 // over the lifetime of the buffer pool, as existing buffers are freed and |
| 41 // reallocated at larger size. When reallocation occurs, new buffer IDs will | 41 // reallocated at larger size. When reallocation occurs, new buffer IDs will |
| 42 // circulate. | 42 // circulate. |
| 43 class CONTENT_EXPORT VideoCaptureBufferPool | 43 class CONTENT_EXPORT VideoCaptureBufferPool |
| 44 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | 44 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { |
| 45 public: | 45 public: |
| 46 static const int kInvalidId; | 46 static const int kInvalidId; |
| 47 | |
| 48 // Abstraction of a pool's buffer data buffer and size for clients. | |
| 49 class BufferHandle { | |
| 50 public: | |
| 51 virtual ~BufferHandle() {} | |
| 52 virtual size_t size() const = 0; | |
| 53 virtual void* data() = 0; | |
| 54 virtual ClientBuffer AsClientBuffer() = 0; | |
| 55 }; | |
| 56 | |
| 57 explicit VideoCaptureBufferPool(int count); | 47 explicit VideoCaptureBufferPool(int count); |
| 58 | 48 |
| 59 // One-time (per client/per-buffer) initialization to share a particular | 49 // One-time (per client/per-buffer) initialization to share a particular |
| 60 // buffer to a process. The size of the allocation is returned as | 50 // buffer to a process. The size of the allocation is returned as |
| 61 // |memory_size|. | 51 // |memory_size|. |
| 62 base::SharedMemoryHandle ShareToProcess(int buffer_id, | 52 base::SharedMemoryHandle ShareToProcess(int buffer_id, |
| 63 base::ProcessHandle process_handle, | 53 base::ProcessHandle process_handle, |
| 64 size_t* memory_size); | 54 size_t* memory_size); |
| 65 | 55 |
| 66 // Try and obtain a BufferHandle for |buffer_id|. | 56 // Query the memory parameters of |buffer_id|. Fills in parameters in the |
| 67 scoped_ptr<BufferHandle> GetBufferHandle(int buffer_id); | 57 // pointer arguments, and returns true iff the buffer exists. |
| 58 bool GetBufferInfo(int buffer_id, void** storage, size_t* size); |
| 68 | 59 |
| 69 // Reserve or allocate a buffer to support a packed frame of |dimensions| of | 60 // Reserve or allocate a buffer to support a packed frame of |dimensions| of |
| 70 // pixel |format| and return its id. This will fail (returning kInvalidId) if | 61 // pixel |format| and return its id. This will fail (returning kInvalidId) if |
| 71 // the pool already is at its |count| limit of the number of allocations, and | 62 // the pool already is at its |count| limit of the number of allocations, and |
| 72 // all allocated buffers are in use by the producer and/or consumers. | 63 // all allocated buffers are in use by the producer and/or consumers. |
| 73 // | 64 // |
| 74 // If successful, the reserved buffer remains reserved (and writable by the | 65 // If successful, the reserved buffer remains reserved (and writable by the |
| 75 // producer) until ownership is transferred either to the consumer via | 66 // producer) until ownership is transferred either to the consumer via |
| 76 // HoldForConsumers(), or back to the pool with | 67 // HoldForConsumers(), or back to the pool with |
| 77 // RelinquishProducerReservation(). | 68 // RelinquishProducerReservation(). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 92 // |buffer_id| must be a buffer index previously returned by | 83 // |buffer_id| must be a buffer index previously returned by |
| 93 // ReserveForProducer(), and not already passed to HoldForConsumers(). | 84 // ReserveForProducer(), and not already passed to HoldForConsumers(). |
| 94 void HoldForConsumers(int buffer_id, int num_clients); | 85 void HoldForConsumers(int buffer_id, int num_clients); |
| 95 | 86 |
| 96 // Indicate that one or more consumers are done with a particular buffer. This | 87 // Indicate that one or more consumers are done with a particular buffer. This |
| 97 // effectively is the opposite of HoldForConsumers(). Once the consumers are | 88 // effectively is the opposite of HoldForConsumers(). Once the consumers are |
| 98 // done, a buffer is returned to the pool for reuse. | 89 // done, a buffer is returned to the pool for reuse. |
| 99 void RelinquishConsumerHold(int buffer_id, int num_clients); | 90 void RelinquishConsumerHold(int buffer_id, int num_clients); |
| 100 | 91 |
| 101 private: | 92 private: |
| 102 class GpuMemoryBufferTracker; | |
| 103 class SharedMemTracker; | 93 class SharedMemTracker; |
| 104 // Generic class to keep track of the state of a given mappable resource. | 94 // Generic class to keep track of the state of a given mappable resource. |
| 105 class Tracker { | 95 class Tracker { |
| 106 public: | 96 public: |
| 107 static scoped_ptr<Tracker> CreateTracker(bool use_gmb); | 97 static scoped_ptr<Tracker> CreateTracker(); |
| 108 | 98 |
| 109 Tracker() | 99 Tracker() : held_by_producer_(false), consumer_hold_count_(0) {} |
| 110 : pixel_count_(0), held_by_producer_(false), consumer_hold_count_(0) {} | |
| 111 virtual bool Init(media::VideoFrame::Format format, | 100 virtual bool Init(media::VideoFrame::Format format, |
| 112 const gfx::Size& dimensions) = 0; | 101 const gfx::Size& dimensions) = 0; |
| 113 virtual ~Tracker(); | 102 virtual ~Tracker(); |
| 114 | 103 |
| 115 size_t pixel_count() const { return pixel_count_; } | |
| 116 void set_pixel_count(size_t count) { pixel_count_ = count; } | |
| 117 bool held_by_producer() const { return held_by_producer_; } | 104 bool held_by_producer() const { return held_by_producer_; } |
| 118 void set_held_by_producer(bool value) { held_by_producer_ = value; } | 105 void set_held_by_producer(bool value) { held_by_producer_ = value; } |
| 119 int consumer_hold_count() const { return consumer_hold_count_; } | 106 int consumer_hold_count() const { return consumer_hold_count_; } |
| 120 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } | 107 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } |
| 121 | 108 |
| 122 // Returns a handle to the underlying storage, be that a block of Shared | 109 // Returns a void* to the underlying storage, be that a memory block for |
| 123 // Memory, or a GpuMemoryBuffer. | 110 // Shared Memory, or a GpuMemoryBuffer. |
| 124 virtual scoped_ptr<BufferHandle> GetBufferHandle() = 0; | 111 virtual void* storage() = 0; |
| 112 // Amount of bytes requested when first created. Can be zero if it does not |
| 113 // need RAM, e.g. is allocated in GPU memory. |
| 114 virtual size_t requested_size() = 0; |
| 125 // The actual size of the underlying backing resource. | 115 // The actual size of the underlying backing resource. |
| 126 virtual size_t mapped_size() const = 0; | 116 virtual size_t mapped_size() = 0; |
| 127 | 117 |
| 128 virtual bool ShareToProcess(base::ProcessHandle process_handle, | 118 virtual bool ShareToProcess(base::ProcessHandle process_handle, |
| 129 base::SharedMemoryHandle* new_handle) = 0; | 119 base::SharedMemoryHandle* new_handle) = 0; |
| 130 | 120 |
| 131 private: | 121 private: |
| 132 size_t pixel_count_; | |
| 133 // Indicates whether this Tracker is currently referenced by the producer. | 122 // Indicates whether this Tracker is currently referenced by the producer. |
| 134 bool held_by_producer_; | 123 bool held_by_producer_; |
| 135 // Number of consumer processes which hold this Tracker. | 124 // Number of consumer processes which hold this Tracker. |
| 136 int consumer_hold_count_; | 125 int consumer_hold_count_; |
| 137 }; | 126 }; |
| 138 | 127 |
| 139 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | 128 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; |
| 140 virtual ~VideoCaptureBufferPool(); | 129 virtual ~VideoCaptureBufferPool(); |
| 141 | 130 |
| 142 int ReserveForProducerInternal(media::VideoPixelFormat format, | 131 int ReserveForProducerInternal(media::VideoPixelFormat format, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 // The buffers, indexed by the first parameter, a buffer id. | 146 // The buffers, indexed by the first parameter, a buffer id. |
| 158 using TrackerMap = std::map<int, Tracker*>; | 147 using TrackerMap = std::map<int, Tracker*>; |
| 159 TrackerMap trackers_; | 148 TrackerMap trackers_; |
| 160 | 149 |
| 161 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); | 150 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); |
| 162 }; | 151 }; |
| 163 | 152 |
| 164 } // namespace content | 153 } // namespace content |
| 165 | 154 |
| 166 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 155 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| OLD | NEW |