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