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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
10 #include "base/basictypes.h"
11 #include "base/files/file.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/process/process.h"
15 #include "base/synchronization/lock.h"
16 #include "content/common/content_export.h"
17 #include "media/base/video_capture_types.h"
18 #include "media/base/video_frame.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/gpu_memory_buffer.h"
24 // A thread-safe class that does the bookkeeping and lifetime management for a
25 // pool of pixel buffers cycled between an in-process producer (e.g. a
26 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is
27 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed
28 // to outlive the controller if necessary. The pixel buffers may be backed by a
29 // SharedMemory, but this is not compulsory.
31 // Producers get a buffer by calling ReserveForProducer(), and may pass on their
32 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer
33 // (without further processing) by calling RelinquishProducerReservation().
34 // Consumers signal that they are done with the buffer by calling
35 // RelinquishConsumerHold().
37 // Buffers are allocated on demand, but there will never be more than |count|
38 // buffers in existence at any time. Buffers are identified by an int value
39 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by
40 // some methods to indicate failure. The active set of buffer ids may change
41 // over the lifetime of the buffer pool, as existing buffers are freed and
42 // reallocated at larger size. When reallocation occurs, new buffer IDs will
44 class CONTENT_EXPORT VideoCaptureBufferPool
45 : public base::RefCountedThreadSafe
<VideoCaptureBufferPool
> {
47 static const int kInvalidId
;
49 // Abstraction of a pool's buffer data buffer and size for clients.
50 // TODO(emircan): See https://crbug.com/521059, refactor this class.
53 virtual ~BufferHandle() {}
54 virtual gfx::Size
dimensions() const = 0;
55 virtual size_t mapped_size() const = 0;
56 virtual void* data(int plane
) = 0;
57 virtual ClientBuffer
AsClientBuffer(int plane
) = 0;
59 virtual base::FileDescriptor
AsPlatformFile() = 0;
63 explicit VideoCaptureBufferPool(int count
);
65 // One-time (per client/per-buffer) initialization to share a particular
66 // buffer to a process. The shared handle is returned as |new_handle|.
67 bool ShareToProcess(int buffer_id
,
68 base::ProcessHandle process_handle
,
69 base::SharedMemoryHandle
* new_handle
);
70 bool ShareToProcess2(int buffer_id
,
72 base::ProcessHandle process_handle
,
73 gfx::GpuMemoryBufferHandle
* new_handle
);
75 // Try and obtain a BufferHandle for |buffer_id|.
76 scoped_ptr
<BufferHandle
> GetBufferHandle(int buffer_id
);
78 // Reserve or allocate a buffer to support a packed frame of |dimensions| of
79 // pixel |format| and return its id. This will fail (returning kInvalidId) if
80 // the pool already is at its |count| limit of the number of allocations, and
81 // all allocated buffers are in use by the producer and/or consumers.
83 // If successful, the reserved buffer remains reserved (and writable by the
84 // producer) until ownership is transferred either to the consumer via
85 // HoldForConsumers(), or back to the pool with
86 // RelinquishProducerReservation().
88 // On occasion, this call will decide to free an old buffer to make room for a
89 // new allocation at a larger size. If so, the ID of the destroyed buffer is
90 // returned via |buffer_id_to_drop|.
91 int ReserveForProducer(media::VideoPixelFormat format
,
92 media::VideoPixelStorage storage
,
93 const gfx::Size
& dimensions
,
94 int* buffer_id_to_drop
);
96 // Indicate that a buffer held for the producer should be returned back to the
97 // pool without passing on to the consumer. This effectively is the opposite
98 // of ReserveForProducer().
99 void RelinquishProducerReservation(int buffer_id
);
101 // Transfer a buffer from producer to consumer ownership.
102 // |buffer_id| must be a buffer index previously returned by
103 // ReserveForProducer(), and not already passed to HoldForConsumers().
104 void HoldForConsumers(int buffer_id
, int num_clients
);
106 // Indicate that one or more consumers are done with a particular buffer. This
107 // effectively is the opposite of HoldForConsumers(). Once the consumers are
108 // done, a buffer is returned to the pool for reuse.
109 void RelinquishConsumerHold(int buffer_id
, int num_clients
);
111 // Returns a snapshot of the current number of buffers in-use divided by the
113 double GetBufferPoolUtilization() const;
116 class GpuMemoryBufferTracker
;
117 class SharedMemTracker
;
118 // Generic class to keep track of the state of a given mappable resource. Each
119 // Tracker carries indication of pixel format and storage type.
122 static scoped_ptr
<Tracker
> CreateTracker(bool use_gmb
);
125 : pixel_count_(0), held_by_producer_(false), consumer_hold_count_(0) {}
126 virtual bool Init(media::VideoPixelFormat format
,
127 media::VideoPixelStorage storage_type
,
128 const gfx::Size
& dimensions
) = 0;
131 size_t pixel_count() const { return pixel_count_
; }
132 void set_pixel_count(size_t count
) { pixel_count_
= count
; }
133 media::VideoPixelFormat
pixel_format() const {
134 return pixel_format_
;
136 void set_pixel_format(media::VideoPixelFormat format
) {
137 pixel_format_
= format
;
139 media::VideoPixelStorage
storage_type() const { return storage_type_
; }
140 void set_storage_type(media::VideoPixelStorage storage_type
) {
141 storage_type_
= storage_type
;
143 bool held_by_producer() const { return held_by_producer_
; }
144 void set_held_by_producer(bool value
) { held_by_producer_
= value
; }
145 int consumer_hold_count() const { return consumer_hold_count_
; }
146 void set_consumer_hold_count(int value
) { consumer_hold_count_
= value
; }
148 // Returns a handle to the underlying storage, be that a block of Shared
149 // Memory, or a GpuMemoryBuffer.
150 virtual scoped_ptr
<BufferHandle
> GetBufferHandle() = 0;
152 virtual bool ShareToProcess(base::ProcessHandle process_handle
,
153 base::SharedMemoryHandle
* new_handle
) = 0;
154 virtual bool ShareToProcess2(int plane
,
155 base::ProcessHandle process_handle
,
156 gfx::GpuMemoryBufferHandle
* new_handle
) = 0;
160 media::VideoPixelFormat pixel_format_
;
161 media::VideoPixelStorage storage_type_
;
162 // Indicates whether this Tracker is currently referenced by the producer.
163 bool held_by_producer_
;
164 // Number of consumer processes which hold this Tracker.
165 int consumer_hold_count_
;
168 friend class base::RefCountedThreadSafe
<VideoCaptureBufferPool
>;
169 virtual ~VideoCaptureBufferPool();
171 int ReserveForProducerInternal(media::VideoPixelFormat format
,
172 media::VideoPixelStorage storage
,
173 const gfx::Size
& dimensions
,
174 int* tracker_id_to_drop
);
176 Tracker
* GetTracker(int buffer_id
);
178 // The max number of buffers that the pool is allowed to have at any moment.
181 // Protects everything below it.
182 mutable base::Lock lock_
;
184 // The ID of the next buffer.
187 // The buffers, indexed by the first parameter, a buffer id.
188 using TrackerMap
= std::map
<int, Tracker
*>;
189 TrackerMap trackers_
;
191 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool
);
194 } // namespace content
196 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_