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