| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // all allocated buffers are in use by the producer and/or consumers. | 84 // all allocated buffers are in use by the producer and/or consumers. |
| 85 // | 85 // |
| 86 // If successful, the reserved buffer remains reserved (and writable by the | 86 // If successful, the reserved buffer remains reserved (and writable by the |
| 87 // producer) until ownership is transferred either to the consumer via | 87 // producer) until ownership is transferred either to the consumer via |
| 88 // HoldForConsumers(), or back to the pool with | 88 // HoldForConsumers(), or back to the pool with |
| 89 // RelinquishProducerReservation(). | 89 // RelinquishProducerReservation(). |
| 90 // | 90 // |
| 91 // On occasion, this call will decide to free an old buffer to make room for a | 91 // On occasion, this call will decide to free an old buffer to make room for a |
| 92 // new allocation at a larger size. If so, the ID of the destroyed buffer is | 92 // new allocation at a larger size. If so, the ID of the destroyed buffer is |
| 93 // returned via |buffer_id_to_drop|. | 93 // returned via |buffer_id_to_drop|. |
| 94 int ReserveForProducer(media::VideoPixelFormat format, | 94 int ReserveForProducer(const gfx::Size& dimensions, |
| 95 media::VideoPixelFormat format, |
| 95 media::VideoPixelStorage storage, | 96 media::VideoPixelStorage storage, |
| 96 const gfx::Size& dimensions, | |
| 97 int* buffer_id_to_drop); | 97 int* buffer_id_to_drop); |
| 98 | 98 |
| 99 // Indicate that a buffer held for the producer should be returned back to the | 99 // Indicate that a buffer held for the producer should be returned back to the |
| 100 // pool without passing on to the consumer. This effectively is the opposite | 100 // pool without passing on to the consumer. This effectively is the opposite |
| 101 // of ReserveForProducer(). | 101 // of ReserveForProducer(). |
| 102 void RelinquishProducerReservation(int buffer_id); | 102 void RelinquishProducerReservation(int buffer_id); |
| 103 | 103 |
| 104 // Transfer a buffer from producer to consumer ownership. | 104 // Transfer a buffer from producer to consumer ownership. |
| 105 // |buffer_id| must be a buffer index previously returned by | 105 // |buffer_id| must be a buffer index previously returned by |
| 106 // ReserveForProducer(), and not already passed to HoldForConsumers(). | 106 // ReserveForProducer(), and not already passed to HoldForConsumers(). |
| 107 void HoldForConsumers(int buffer_id, int num_clients); | 107 void HoldForConsumers(int buffer_id, int num_clients); |
| 108 | 108 |
| 109 // Indicate that one or more consumers are done with a particular buffer. This | 109 // Indicate that one or more consumers are done with a particular buffer. This |
| 110 // effectively is the opposite of HoldForConsumers(). Once the consumers are | 110 // effectively is the opposite of HoldForConsumers(). Once the consumers are |
| 111 // done, a buffer is returned to the pool for reuse. | 111 // done, a buffer is returned to the pool for reuse. |
| 112 void RelinquishConsumerHold(int buffer_id, int num_clients); | 112 void RelinquishConsumerHold(int buffer_id, int num_clients); |
| 113 | 113 |
| 114 // Attempt to reserve the same buffer that was relinquished in the last call |
| 115 // to RelinquishProducerReservation(). If the buffer is not still being |
| 116 // consumed, and has not yet been re-used since being consumed, and the |
| 117 // specified |dimensions|, |format|, and |storage| agree with its last |
| 118 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned. |
| 119 // |
| 120 // A producer may assume the content of the buffer has been preserved and may |
| 121 // also make modifications. |
| 122 int ResurrectLastForProducer(const gfx::Size& dimensions, |
| 123 media::VideoPixelFormat format, |
| 124 media::VideoPixelStorage storage); |
| 125 |
| 114 // Returns a snapshot of the current number of buffers in-use divided by the | 126 // Returns a snapshot of the current number of buffers in-use divided by the |
| 115 // maximum |count_|. | 127 // maximum |count_|. |
| 116 double GetBufferPoolUtilization() const; | 128 double GetBufferPoolUtilization() const; |
| 117 | 129 |
| 118 private: | 130 private: |
| 119 class GpuMemoryBufferTracker; | 131 class GpuMemoryBufferTracker; |
| 120 class SharedMemTracker; | 132 class SharedMemTracker; |
| 121 // Generic class to keep track of the state of a given mappable resource. Each | 133 // Generic class to keep track of the state of a given mappable resource. Each |
| 122 // Tracker carries indication of pixel format and storage type. | 134 // Tracker carries indication of pixel format and storage type. |
| 123 class Tracker { | 135 class Tracker { |
| 124 public: | 136 public: |
| 125 static scoped_ptr<Tracker> CreateTracker(media::VideoPixelStorage storage); | 137 static scoped_ptr<Tracker> CreateTracker(media::VideoPixelStorage storage); |
| 126 | 138 |
| 127 Tracker() | 139 Tracker() |
| 128 : pixel_count_(0), held_by_producer_(false), consumer_hold_count_(0) {} | 140 : max_pixel_count_(0), |
| 129 virtual bool Init(media::VideoPixelFormat format, | 141 held_by_producer_(false), |
| 142 consumer_hold_count_(0) {} |
| 143 virtual bool Init(const gfx::Size& dimensions, |
| 144 media::VideoPixelFormat format, |
| 130 media::VideoPixelStorage storage_type, | 145 media::VideoPixelStorage storage_type, |
| 131 const gfx::Size& dimensions, | |
| 132 base::Lock* lock) = 0; | 146 base::Lock* lock) = 0; |
| 133 virtual ~Tracker(); | 147 virtual ~Tracker(); |
| 134 | 148 |
| 135 size_t pixel_count() const { return pixel_count_; } | 149 const gfx::Size& dimensions() const { return dimensions_; } |
| 136 void set_pixel_count(size_t count) { pixel_count_ = count; } | 150 void set_dimensions(const gfx::Size& dim) { dimensions_ = dim; } |
| 151 size_t max_pixel_count() const { return max_pixel_count_; } |
| 152 void set_max_pixel_count(size_t count) { max_pixel_count_ = count; } |
| 137 media::VideoPixelFormat pixel_format() const { | 153 media::VideoPixelFormat pixel_format() const { |
| 138 return pixel_format_; | 154 return pixel_format_; |
| 139 } | 155 } |
| 140 void set_pixel_format(media::VideoPixelFormat format) { | 156 void set_pixel_format(media::VideoPixelFormat format) { |
| 141 pixel_format_ = format; | 157 pixel_format_ = format; |
| 142 } | 158 } |
| 143 media::VideoPixelStorage storage_type() const { return storage_type_; } | 159 media::VideoPixelStorage storage_type() const { return storage_type_; } |
| 144 void set_storage_type(media::VideoPixelStorage storage_type) { | 160 void set_storage_type(media::VideoPixelStorage storage_type) { |
| 145 storage_type_ = storage_type; | 161 storage_type_ = storage_type; |
| 146 } | 162 } |
| 147 bool held_by_producer() const { return held_by_producer_; } | 163 bool held_by_producer() const { return held_by_producer_; } |
| 148 void set_held_by_producer(bool value) { held_by_producer_ = value; } | 164 void set_held_by_producer(bool value) { held_by_producer_ = value; } |
| 149 int consumer_hold_count() const { return consumer_hold_count_; } | 165 int consumer_hold_count() const { return consumer_hold_count_; } |
| 150 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } | 166 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } |
| 151 | 167 |
| 152 // Returns a handle to the underlying storage, be that a block of Shared | 168 // Returns a handle to the underlying storage, be that a block of Shared |
| 153 // Memory, or a GpuMemoryBuffer. | 169 // Memory, or a GpuMemoryBuffer. |
| 154 virtual scoped_ptr<BufferHandle> GetBufferHandle() = 0; | 170 virtual scoped_ptr<BufferHandle> GetBufferHandle() = 0; |
| 155 | 171 |
| 156 virtual bool ShareToProcess(base::ProcessHandle process_handle, | 172 virtual bool ShareToProcess(base::ProcessHandle process_handle, |
| 157 base::SharedMemoryHandle* new_handle) = 0; | 173 base::SharedMemoryHandle* new_handle) = 0; |
| 158 virtual bool ShareToProcess2(int plane, | 174 virtual bool ShareToProcess2(int plane, |
| 159 base::ProcessHandle process_handle, | 175 base::ProcessHandle process_handle, |
| 160 gfx::GpuMemoryBufferHandle* new_handle) = 0; | 176 gfx::GpuMemoryBufferHandle* new_handle) = 0; |
| 161 | 177 |
| 162 private: | 178 private: |
| 163 size_t pixel_count_; | 179 // |dimensions_| may change as a Tracker is re-used, but |max_pixel_count_|, |
| 180 // |pixel_format_|, and |storage_type_| are set once for the lifetime of a |
| 181 // Tracker. |
| 182 gfx::Size dimensions_; |
| 183 size_t max_pixel_count_; |
| 164 media::VideoPixelFormat pixel_format_; | 184 media::VideoPixelFormat pixel_format_; |
| 165 media::VideoPixelStorage storage_type_; | 185 media::VideoPixelStorage storage_type_; |
| 186 |
| 166 // Indicates whether this Tracker is currently referenced by the producer. | 187 // Indicates whether this Tracker is currently referenced by the producer. |
| 167 bool held_by_producer_; | 188 bool held_by_producer_; |
| 189 |
| 168 // Number of consumer processes which hold this Tracker. | 190 // Number of consumer processes which hold this Tracker. |
| 169 int consumer_hold_count_; | 191 int consumer_hold_count_; |
| 170 }; | 192 }; |
| 171 | 193 |
| 172 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | 194 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; |
| 173 virtual ~VideoCaptureBufferPool(); | 195 virtual ~VideoCaptureBufferPool(); |
| 174 | 196 |
| 175 int ReserveForProducerInternal(media::VideoPixelFormat format, | 197 int ReserveForProducerInternal(const gfx::Size& dimensions, |
| 198 media::VideoPixelFormat format, |
| 176 media::VideoPixelStorage storage, | 199 media::VideoPixelStorage storage, |
| 177 const gfx::Size& dimensions, | |
| 178 int* tracker_id_to_drop); | 200 int* tracker_id_to_drop); |
| 179 | 201 |
| 180 Tracker* GetTracker(int buffer_id); | 202 Tracker* GetTracker(int buffer_id); |
| 181 | 203 |
| 182 // The max number of buffers that the pool is allowed to have at any moment. | 204 // The max number of buffers that the pool is allowed to have at any moment. |
| 183 const int count_; | 205 const int count_; |
| 184 | 206 |
| 185 // Protects everything below it. | 207 // Protects everything below it. |
| 186 mutable base::Lock lock_; | 208 mutable base::Lock lock_; |
| 187 | 209 |
| 188 // The ID of the next buffer. | 210 // The ID of the next buffer. |
| 189 int next_buffer_id_; | 211 int next_buffer_id_; |
| 190 | 212 |
| 213 // The ID of the buffer last relinquished by the producer (a candidate for |
| 214 // resurrection). |
| 215 int last_relinquished_buffer_id_; |
| 216 |
| 191 // The buffers, indexed by the first parameter, a buffer id. | 217 // The buffers, indexed by the first parameter, a buffer id. |
| 192 using TrackerMap = std::map<int, Tracker*>; | 218 using TrackerMap = std::map<int, Tracker*>; |
| 193 TrackerMap trackers_; | 219 TrackerMap trackers_; |
| 194 | 220 |
| 195 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); | 221 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); |
| 196 }; | 222 }; |
| 197 | 223 |
| 198 } // namespace content | 224 } // namespace content |
| 199 | 225 |
| 200 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 226 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| OLD | NEW |