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> |
11 | 11 |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "content/browser/renderer_host/media/video_capture_buffer_handle.h" | |
19 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
20 #include "media/base/video_capture_types.h" | 21 #include "media/base/video_capture_types.h" |
21 #include "media/base/video_frame.h" | 22 #include "media/base/video_frame.h" |
22 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
23 #include "ui/gfx/gpu_memory_buffer.h" | 24 #include "ui/gfx/gpu_memory_buffer.h" |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 | 27 |
27 class VideoCaptureBufferPoolBufferHandle { | 28 class VideoCaptureBufferTracker; |
mcasas
2016/09/20 00:51:08
Maybe also forward declare VideoCaptureBufferHandl
chfremer
2016/09/20 18:43:10
Done.
| |
28 public: | |
29 virtual ~VideoCaptureBufferPoolBufferHandle() {} | |
30 virtual gfx::Size dimensions() const = 0; | |
31 virtual size_t mapped_size() const = 0; | |
32 virtual void* data(int plane) = 0; | |
33 virtual ClientBuffer AsClientBuffer(int plane) = 0; | |
34 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
35 virtual base::FileDescriptor AsPlatformFile() = 0; | |
36 #endif | |
37 }; | |
38 | 29 |
39 class CONTENT_EXPORT VideoCaptureBufferPool | 30 class CONTENT_EXPORT VideoCaptureBufferPool |
40 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | 31 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { |
41 public: | 32 public: |
42 static constexpr int kInvalidId = -1; | 33 static constexpr int kInvalidId = -1; |
43 | 34 |
44 // One-time (per client/per-buffer) initialization to share a particular | 35 // One-time (per client/per-buffer) initialization to share a particular |
45 // buffer to a process. The shared handle is returned as |new_handle|. | 36 // buffer to a process. The shared handle is returned as |new_handle|. |
46 virtual bool ShareToProcess(int buffer_id, | 37 virtual bool ShareToProcess(int buffer_id, |
47 base::ProcessHandle process_handle, | 38 base::ProcessHandle process_handle, |
48 base::SharedMemoryHandle* new_handle) = 0; | 39 base::SharedMemoryHandle* new_handle) = 0; |
49 virtual bool ShareToProcess2(int buffer_id, | 40 virtual bool ShareToProcess2(int buffer_id, |
50 int plane, | 41 int plane, |
51 base::ProcessHandle process_handle, | 42 base::ProcessHandle process_handle, |
52 gfx::GpuMemoryBufferHandle* new_handle) = 0; | 43 gfx::GpuMemoryBufferHandle* new_handle) = 0; |
53 | 44 |
54 // Try and obtain a BufferHandle for |buffer_id|. | 45 // Try and obtain a BufferHandle for |buffer_id|. |
55 virtual std::unique_ptr<VideoCaptureBufferPoolBufferHandle> GetBufferHandle( | 46 virtual std::unique_ptr<VideoCaptureBufferHandle> GetBufferHandle( |
56 int buffer_id) = 0; | 47 int buffer_id) = 0; |
57 | 48 |
58 // Reserve or allocate a buffer to support a packed frame of |dimensions| of | 49 // Reserve or allocate a buffer to support a packed frame of |dimensions| of |
59 // pixel |format| and return its id. This will fail (returning kInvalidId) if | 50 // pixel |format| and return its id. This will fail (returning kInvalidId) if |
60 // the pool already is at its |count| limit of the number of allocations, and | 51 // the pool already is at its |count| limit of the number of allocations, and |
61 // all allocated buffers are in use by the producer and/or consumers. | 52 // all allocated buffers are in use by the producer and/or consumers. |
62 // | 53 // |
63 // If successful, the reserved buffer remains reserved (and writable by the | 54 // If successful, the reserved buffer remains reserved (and writable by the |
64 // producer) until ownership is transferred either to the consumer via | 55 // producer) until ownership is transferred either to the consumer via |
65 // HoldForConsumers(), or back to the pool with | 56 // HoldForConsumers(), or back to the pool with |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 // Buffers are allocated on demand, but there will never be more than |count| | 118 // Buffers are allocated on demand, but there will never be more than |count| |
128 // buffers in existence at any time. Buffers are identified by an int value | 119 // buffers in existence at any time. Buffers are identified by an int value |
129 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | 120 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by |
130 // some methods to indicate failure. The active set of buffer ids may change | 121 // some methods to indicate failure. The active set of buffer ids may change |
131 // over the lifetime of the buffer pool, as existing buffers are freed and | 122 // over the lifetime of the buffer pool, as existing buffers are freed and |
132 // reallocated at larger size. When reallocation occurs, new buffer IDs will | 123 // reallocated at larger size. When reallocation occurs, new buffer IDs will |
133 // circulate. | 124 // circulate. |
134 class CONTENT_EXPORT VideoCaptureBufferPoolImpl | 125 class CONTENT_EXPORT VideoCaptureBufferPoolImpl |
135 : public VideoCaptureBufferPool { | 126 : public VideoCaptureBufferPool { |
136 public: | 127 public: |
137 using BufferHandle = VideoCaptureBufferPoolBufferHandle; | |
138 | |
139 explicit VideoCaptureBufferPoolImpl(int count); | 128 explicit VideoCaptureBufferPoolImpl(int count); |
140 | 129 |
141 // Implementation of VideoCaptureBufferPool interface: | 130 // Implementation of VideoCaptureBufferPool interface: |
142 bool ShareToProcess(int buffer_id, | 131 bool ShareToProcess(int buffer_id, |
143 base::ProcessHandle process_handle, | 132 base::ProcessHandle process_handle, |
144 base::SharedMemoryHandle* new_handle) override; | 133 base::SharedMemoryHandle* new_handle) override; |
145 bool ShareToProcess2(int buffer_id, | 134 bool ShareToProcess2(int buffer_id, |
146 int plane, | 135 int plane, |
147 base::ProcessHandle process_handle, | 136 base::ProcessHandle process_handle, |
148 gfx::GpuMemoryBufferHandle* new_handle) override; | 137 gfx::GpuMemoryBufferHandle* new_handle) override; |
149 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id) override; | 138 std::unique_ptr<VideoCaptureBufferHandle> GetBufferHandle( |
139 int buffer_id) override; | |
150 int ReserveForProducer(const gfx::Size& dimensions, | 140 int ReserveForProducer(const gfx::Size& dimensions, |
151 media::VideoPixelFormat format, | 141 media::VideoPixelFormat format, |
152 media::VideoPixelStorage storage, | 142 media::VideoPixelStorage storage, |
153 int* buffer_id_to_drop) override; | 143 int* buffer_id_to_drop) override; |
154 void RelinquishProducerReservation(int buffer_id) override; | 144 void RelinquishProducerReservation(int buffer_id) override; |
155 int ResurrectLastForProducer(const gfx::Size& dimensions, | 145 int ResurrectLastForProducer(const gfx::Size& dimensions, |
156 media::VideoPixelFormat format, | 146 media::VideoPixelFormat format, |
157 media::VideoPixelStorage storage) override; | 147 media::VideoPixelStorage storage) override; |
158 double GetBufferPoolUtilization() const override; | 148 double GetBufferPoolUtilization() const override; |
159 void HoldForConsumers(int buffer_id, int num_clients) override; | 149 void HoldForConsumers(int buffer_id, int num_clients) override; |
160 void RelinquishConsumerHold(int buffer_id, int num_clients) override; | 150 void RelinquishConsumerHold(int buffer_id, int num_clients) override; |
161 | 151 |
162 private: | 152 private: |
163 class GpuMemoryBufferTracker; | |
164 class SharedMemTracker; | |
165 // Generic class to keep track of the state of a given mappable resource. Each | |
166 // Tracker carries indication of pixel format and storage type. | |
167 class Tracker { | |
168 public: | |
169 static std::unique_ptr<Tracker> CreateTracker( | |
170 media::VideoPixelStorage storage); | |
171 | |
172 Tracker() | |
173 : max_pixel_count_(0), | |
174 held_by_producer_(false), | |
175 consumer_hold_count_(0) {} | |
176 virtual bool Init(const gfx::Size& dimensions, | |
177 media::VideoPixelFormat format, | |
178 media::VideoPixelStorage storage_type, | |
179 base::Lock* lock) = 0; | |
180 virtual ~Tracker(); | |
181 | |
182 const gfx::Size& dimensions() const { return dimensions_; } | |
183 void set_dimensions(const gfx::Size& dim) { dimensions_ = dim; } | |
184 size_t max_pixel_count() const { return max_pixel_count_; } | |
185 void set_max_pixel_count(size_t count) { max_pixel_count_ = count; } | |
186 media::VideoPixelFormat pixel_format() const { | |
187 return pixel_format_; | |
188 } | |
189 void set_pixel_format(media::VideoPixelFormat format) { | |
190 pixel_format_ = format; | |
191 } | |
192 media::VideoPixelStorage storage_type() const { return storage_type_; } | |
193 void set_storage_type(media::VideoPixelStorage storage_type) { | |
194 storage_type_ = storage_type; | |
195 } | |
196 bool held_by_producer() const { return held_by_producer_; } | |
197 void set_held_by_producer(bool value) { held_by_producer_ = value; } | |
198 int consumer_hold_count() const { return consumer_hold_count_; } | |
199 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } | |
200 | |
201 // Returns a handle to the underlying storage, be that a block of Shared | |
202 // Memory, or a GpuMemoryBuffer. | |
203 virtual std::unique_ptr<BufferHandle> GetBufferHandle() = 0; | |
204 | |
205 virtual bool ShareToProcess(base::ProcessHandle process_handle, | |
206 base::SharedMemoryHandle* new_handle) = 0; | |
207 virtual bool ShareToProcess2(int plane, | |
208 base::ProcessHandle process_handle, | |
209 gfx::GpuMemoryBufferHandle* new_handle) = 0; | |
210 | |
211 private: | |
212 // |dimensions_| may change as a Tracker is re-used, but |max_pixel_count_|, | |
213 // |pixel_format_|, and |storage_type_| are set once for the lifetime of a | |
214 // Tracker. | |
215 gfx::Size dimensions_; | |
216 size_t max_pixel_count_; | |
217 media::VideoPixelFormat pixel_format_; | |
218 media::VideoPixelStorage storage_type_; | |
219 | |
220 // Indicates whether this Tracker is currently referenced by the producer. | |
221 bool held_by_producer_; | |
222 | |
223 // Number of consumer processes which hold this Tracker. | |
224 int consumer_hold_count_; | |
225 }; | |
226 | |
227 friend class base::RefCountedThreadSafe<VideoCaptureBufferPoolImpl>; | 153 friend class base::RefCountedThreadSafe<VideoCaptureBufferPoolImpl>; |
228 ~VideoCaptureBufferPoolImpl() override; | 154 ~VideoCaptureBufferPoolImpl() override; |
229 | 155 |
230 int ReserveForProducerInternal(const gfx::Size& dimensions, | 156 int ReserveForProducerInternal(const gfx::Size& dimensions, |
231 media::VideoPixelFormat format, | 157 media::VideoPixelFormat format, |
232 media::VideoPixelStorage storage, | 158 media::VideoPixelStorage storage, |
233 int* tracker_id_to_drop); | 159 int* tracker_id_to_drop); |
234 | 160 |
235 Tracker* GetTracker(int buffer_id); | 161 VideoCaptureBufferTracker* GetTracker(int buffer_id); |
236 | 162 |
237 // The max number of buffers that the pool is allowed to have at any moment. | 163 // The max number of buffers that the pool is allowed to have at any moment. |
238 const int count_; | 164 const int count_; |
239 | 165 |
240 // Protects everything below it. | 166 // Protects everything below it. |
241 mutable base::Lock lock_; | 167 mutable base::Lock lock_; |
242 | 168 |
243 // The ID of the next buffer. | 169 // The ID of the next buffer. |
244 int next_buffer_id_; | 170 int next_buffer_id_; |
245 | 171 |
246 // The ID of the buffer last relinquished by the producer (a candidate for | 172 // The ID of the buffer last relinquished by the producer (a candidate for |
247 // resurrection). | 173 // resurrection). |
248 int last_relinquished_buffer_id_; | 174 int last_relinquished_buffer_id_; |
249 | 175 |
250 // The buffers, indexed by the first parameter, a buffer id. | 176 // The buffers, indexed by the first parameter, a buffer id. |
251 using TrackerMap = std::map<int, Tracker*>; | 177 using TrackerMap = std::map<int, VideoCaptureBufferTracker*>; |
252 TrackerMap trackers_; | 178 TrackerMap trackers_; |
253 | 179 |
254 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPoolImpl); | 180 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPoolImpl); |
255 }; | 181 }; |
256 | 182 |
257 } // namespace content | 183 } // namespace content |
258 | 184 |
259 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 185 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
OLD | NEW |