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/common/content_export.h" | 19 #include "content/common/content_export.h" |
20 #include "media/base/video_capture_types.h" | 20 #include "media/base/video_capture_types.h" |
21 #include "media/base/video_frame.h" | 21 #include "media/base/video_frame.h" |
22 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
23 #include "ui/gfx/gpu_memory_buffer.h" | 23 #include "ui/gfx/gpu_memory_buffer.h" |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
27 // A thread-safe class that does the bookkeeping and lifetime management for a | 27 class VideoCaptureBufferPoolBufferHandle { |
28 // pool of pixel buffers cycled between an in-process producer (e.g. a | 28 public: |
29 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is | 29 virtual ~VideoCaptureBufferPoolBufferHandle() {} |
30 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed | 30 virtual gfx::Size dimensions() const = 0; |
31 // to outlive the controller if necessary. The pixel buffers may be backed by a | 31 virtual size_t mapped_size() const = 0; |
32 // SharedMemory, but this is not compulsory. | 32 virtual void* data(int plane) = 0; |
33 // | 33 virtual ClientBuffer AsClientBuffer(int plane) = 0; |
34 // Producers get a buffer by calling ReserveForProducer(), and may pass on their | 34 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
35 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer | 35 virtual base::FileDescriptor AsPlatformFile() = 0; |
36 // (without further processing) by calling RelinquishProducerReservation(). | 36 #endif |
37 // Consumers signal that they are done with the buffer by calling | 37 }; |
38 // RelinquishConsumerHold(). | 38 |
39 // | |
40 // Buffers are allocated on demand, but there will never be more than |count| | |
41 // buffers in existence at any time. Buffers are identified by an int value | |
42 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | |
43 // some methods to indicate failure. The active set of buffer ids may change | |
44 // over the lifetime of the buffer pool, as existing buffers are freed and | |
45 // reallocated at larger size. When reallocation occurs, new buffer IDs will | |
46 // circulate. | |
47 class CONTENT_EXPORT VideoCaptureBufferPool | 39 class CONTENT_EXPORT VideoCaptureBufferPool |
48 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | 40 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { |
49 public: | 41 public: |
50 static const int kInvalidId; | 42 static constexpr int kInvalidId = -1; |
51 | |
52 // Abstraction of a pool's buffer data buffer and size for clients. | |
53 // TODO(emircan): See https://crbug.com/521059, refactor this class. | |
54 class BufferHandle { | |
55 public: | |
56 virtual ~BufferHandle() {} | |
57 virtual gfx::Size dimensions() const = 0; | |
58 virtual size_t mapped_size() const = 0; | |
59 virtual void* data(int plane) = 0; | |
60 virtual ClientBuffer AsClientBuffer(int plane) = 0; | |
61 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
62 virtual base::FileDescriptor AsPlatformFile() = 0; | |
63 #endif | |
64 }; | |
65 | |
66 explicit VideoCaptureBufferPool(int count); | |
67 | 43 |
68 // One-time (per client/per-buffer) initialization to share a particular | 44 // One-time (per client/per-buffer) initialization to share a particular |
69 // buffer to a process. The shared handle is returned as |new_handle|. | 45 // buffer to a process. The shared handle is returned as |new_handle|. |
70 bool ShareToProcess(int buffer_id, | 46 virtual bool ShareToProcess(int buffer_id, |
71 base::ProcessHandle process_handle, | 47 base::ProcessHandle process_handle, |
72 base::SharedMemoryHandle* new_handle); | 48 base::SharedMemoryHandle* new_handle) = 0; |
73 bool ShareToProcess2(int buffer_id, | 49 virtual bool ShareToProcess2(int buffer_id, |
74 int plane, | 50 int plane, |
75 base::ProcessHandle process_handle, | 51 base::ProcessHandle process_handle, |
76 gfx::GpuMemoryBufferHandle* new_handle); | 52 gfx::GpuMemoryBufferHandle* new_handle) = 0; |
77 | 53 |
78 // Try and obtain a BufferHandle for |buffer_id|. | 54 // Try and obtain a BufferHandle for |buffer_id|. |
79 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id); | 55 virtual std::unique_ptr<VideoCaptureBufferPoolBufferHandle> GetBufferHandle( |
| 56 int buffer_id) = 0; |
80 | 57 |
81 // Reserve or allocate a buffer to support a packed frame of |dimensions| of | 58 // Reserve or allocate a buffer to support a packed frame of |dimensions| of |
82 // pixel |format| and return its id. This will fail (returning kInvalidId) if | 59 // pixel |format| and return its id. This will fail (returning kInvalidId) if |
83 // the pool already is at its |count| limit of the number of allocations, and | 60 // the pool already is at its |count| limit of the number of allocations, and |
84 // all allocated buffers are in use by the producer and/or consumers. | 61 // all allocated buffers are in use by the producer and/or consumers. |
85 // | 62 // |
86 // If successful, the reserved buffer remains reserved (and writable by the | 63 // If successful, the reserved buffer remains reserved (and writable by the |
87 // producer) until ownership is transferred either to the consumer via | 64 // producer) until ownership is transferred either to the consumer via |
88 // HoldForConsumers(), or back to the pool with | 65 // HoldForConsumers(), or back to the pool with |
89 // RelinquishProducerReservation(). | 66 // RelinquishProducerReservation(). |
90 // | 67 // |
91 // On occasion, this call will decide to free an old buffer to make room for a | 68 // 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 | 69 // new allocation at a larger size. If so, the ID of the destroyed buffer is |
93 // returned via |buffer_id_to_drop|. | 70 // returned via |buffer_id_to_drop|. |
94 int ReserveForProducer(const gfx::Size& dimensions, | 71 virtual int ReserveForProducer(const gfx::Size& dimensions, |
95 media::VideoPixelFormat format, | 72 media::VideoPixelFormat format, |
96 media::VideoPixelStorage storage, | 73 media::VideoPixelStorage storage, |
97 int* buffer_id_to_drop); | 74 int* buffer_id_to_drop) = 0; |
98 | 75 |
99 // Indicate that a buffer held for the producer should be returned back to the | 76 // 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 | 77 // pool without passing on to the consumer. This effectively is the opposite |
101 // of ReserveForProducer(). | 78 // of ReserveForProducer(). |
102 void RelinquishProducerReservation(int buffer_id); | 79 virtual void RelinquishProducerReservation(int buffer_id) = 0; |
103 | |
104 // Transfer a buffer from producer to consumer ownership. | |
105 // |buffer_id| must be a buffer index previously returned by | |
106 // ReserveForProducer(), and not already passed to HoldForConsumers(). | |
107 void HoldForConsumers(int buffer_id, int num_clients); | |
108 | |
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 | |
111 // done, a buffer is returned to the pool for reuse. | |
112 void RelinquishConsumerHold(int buffer_id, int num_clients); | |
113 | 80 |
114 // Attempt to reserve the same buffer that was relinquished in the last call | 81 // Attempt to reserve the same buffer that was relinquished in the last call |
115 // to RelinquishProducerReservation(). If the buffer is not still being | 82 // to RelinquishProducerReservation(). If the buffer is not still being |
116 // consumed, and has not yet been re-used since being consumed, and the | 83 // consumed, and has not yet been re-used since being consumed, and the |
117 // specified |dimensions|, |format|, and |storage| agree with its last | 84 // specified |dimensions|, |format|, and |storage| agree with its last |
118 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned. | 85 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned. |
119 // | 86 // |
120 // A producer may assume the content of the buffer has been preserved and may | 87 // A producer may assume the content of the buffer has been preserved and may |
121 // also make modifications. | 88 // also make modifications. |
122 int ResurrectLastForProducer(const gfx::Size& dimensions, | 89 virtual int ResurrectLastForProducer(const gfx::Size& dimensions, |
123 media::VideoPixelFormat format, | 90 media::VideoPixelFormat format, |
124 media::VideoPixelStorage storage); | 91 media::VideoPixelStorage storage) = 0; |
125 | 92 |
126 // Returns a snapshot of the current number of buffers in-use divided by the | 93 // Returns a snapshot of the current number of buffers in-use divided by the |
127 // maximum |count_|. | 94 // maximum |count_|. |
128 double GetBufferPoolUtilization() const; | 95 virtual double GetBufferPoolUtilization() const = 0; |
| 96 |
| 97 // Transfer a buffer from producer to consumer ownership. |
| 98 // |buffer_id| must be a buffer index previously returned by |
| 99 // ReserveForProducer(), and not already passed to HoldForConsumers(). |
| 100 virtual void HoldForConsumers(int buffer_id, int num_clients) = 0; |
| 101 |
| 102 // Indicate that one or more consumers are done with a particular buffer. This |
| 103 // effectively is the opposite of HoldForConsumers(). Once the consumers are |
| 104 // done, a buffer is returned to the pool for reuse. |
| 105 virtual void RelinquishConsumerHold(int buffer_id, int num_clients) = 0; |
| 106 |
| 107 protected: |
| 108 virtual ~VideoCaptureBufferPool() {} |
| 109 |
| 110 private: |
| 111 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; |
| 112 }; |
| 113 |
| 114 // A thread-safe class that does the bookkeeping and lifetime management for a |
| 115 // pool of pixel buffers cycled between an in-process producer (e.g. a |
| 116 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is |
| 117 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed |
| 118 // to outlive the controller if necessary. The pixel buffers may be backed by a |
| 119 // SharedMemory, but this is not compulsory. |
| 120 // |
| 121 // Producers get a buffer by calling ReserveForProducer(), and may pass on their |
| 122 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer |
| 123 // (without further processing) by calling RelinquishProducerReservation(). |
| 124 // Consumers signal that they are done with the buffer by calling |
| 125 // RelinquishConsumerHold(). |
| 126 // |
| 127 // 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 |
| 129 // 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 |
| 131 // 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 |
| 133 // circulate. |
| 134 class CONTENT_EXPORT VideoCaptureBufferPoolImpl |
| 135 : public VideoCaptureBufferPool { |
| 136 public: |
| 137 using BufferHandle = VideoCaptureBufferPoolBufferHandle; |
| 138 |
| 139 explicit VideoCaptureBufferPoolImpl(int count); |
| 140 |
| 141 // Implementation of VideoCaptureBufferPool interface: |
| 142 bool ShareToProcess(int buffer_id, |
| 143 base::ProcessHandle process_handle, |
| 144 base::SharedMemoryHandle* new_handle) override; |
| 145 bool ShareToProcess2(int buffer_id, |
| 146 int plane, |
| 147 base::ProcessHandle process_handle, |
| 148 gfx::GpuMemoryBufferHandle* new_handle) override; |
| 149 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id) override; |
| 150 int ReserveForProducer(const gfx::Size& dimensions, |
| 151 media::VideoPixelFormat format, |
| 152 media::VideoPixelStorage storage, |
| 153 int* buffer_id_to_drop) override; |
| 154 void RelinquishProducerReservation(int buffer_id) override; |
| 155 int ResurrectLastForProducer(const gfx::Size& dimensions, |
| 156 media::VideoPixelFormat format, |
| 157 media::VideoPixelStorage storage) override; |
| 158 double GetBufferPoolUtilization() const override; |
| 159 void HoldForConsumers(int buffer_id, int num_clients) override; |
| 160 void RelinquishConsumerHold(int buffer_id, int num_clients) override; |
129 | 161 |
130 private: | 162 private: |
131 class GpuMemoryBufferTracker; | 163 class GpuMemoryBufferTracker; |
132 class SharedMemTracker; | 164 class SharedMemTracker; |
133 // Generic class to keep track of the state of a given mappable resource. Each | 165 // Generic class to keep track of the state of a given mappable resource. Each |
134 // Tracker carries indication of pixel format and storage type. | 166 // Tracker carries indication of pixel format and storage type. |
135 class Tracker { | 167 class Tracker { |
136 public: | 168 public: |
137 static std::unique_ptr<Tracker> CreateTracker( | 169 static std::unique_ptr<Tracker> CreateTracker( |
138 media::VideoPixelStorage storage); | 170 media::VideoPixelStorage storage); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 media::VideoPixelFormat pixel_format_; | 217 media::VideoPixelFormat pixel_format_; |
186 media::VideoPixelStorage storage_type_; | 218 media::VideoPixelStorage storage_type_; |
187 | 219 |
188 // Indicates whether this Tracker is currently referenced by the producer. | 220 // Indicates whether this Tracker is currently referenced by the producer. |
189 bool held_by_producer_; | 221 bool held_by_producer_; |
190 | 222 |
191 // Number of consumer processes which hold this Tracker. | 223 // Number of consumer processes which hold this Tracker. |
192 int consumer_hold_count_; | 224 int consumer_hold_count_; |
193 }; | 225 }; |
194 | 226 |
195 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | 227 friend class base::RefCountedThreadSafe<VideoCaptureBufferPoolImpl>; |
196 virtual ~VideoCaptureBufferPool(); | 228 ~VideoCaptureBufferPoolImpl() override; |
197 | 229 |
198 int ReserveForProducerInternal(const gfx::Size& dimensions, | 230 int ReserveForProducerInternal(const gfx::Size& dimensions, |
199 media::VideoPixelFormat format, | 231 media::VideoPixelFormat format, |
200 media::VideoPixelStorage storage, | 232 media::VideoPixelStorage storage, |
201 int* tracker_id_to_drop); | 233 int* tracker_id_to_drop); |
202 | 234 |
203 Tracker* GetTracker(int buffer_id); | 235 Tracker* GetTracker(int buffer_id); |
204 | 236 |
205 // The max number of buffers that the pool is allowed to have at any moment. | 237 // The max number of buffers that the pool is allowed to have at any moment. |
206 const int count_; | 238 const int count_; |
207 | 239 |
208 // Protects everything below it. | 240 // Protects everything below it. |
209 mutable base::Lock lock_; | 241 mutable base::Lock lock_; |
210 | 242 |
211 // The ID of the next buffer. | 243 // The ID of the next buffer. |
212 int next_buffer_id_; | 244 int next_buffer_id_; |
213 | 245 |
214 // The ID of the buffer last relinquished by the producer (a candidate for | 246 // The ID of the buffer last relinquished by the producer (a candidate for |
215 // resurrection). | 247 // resurrection). |
216 int last_relinquished_buffer_id_; | 248 int last_relinquished_buffer_id_; |
217 | 249 |
218 // The buffers, indexed by the first parameter, a buffer id. | 250 // The buffers, indexed by the first parameter, a buffer id. |
219 using TrackerMap = std::map<int, Tracker*>; | 251 using TrackerMap = std::map<int, Tracker*>; |
220 TrackerMap trackers_; | 252 TrackerMap trackers_; |
221 | 253 |
222 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); | 254 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPoolImpl); |
223 }; | 255 }; |
224 | 256 |
225 } // namespace content | 257 } // namespace content |
226 | 258 |
227 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 259 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
OLD | NEW |