Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_buffer_pool.h
blobb76fbe7feb4bb81424662c1770eab25fd16b35c5
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_
8 #include <map>
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"
22 namespace content {
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
43 // circulate.
44 class CONTENT_EXPORT VideoCaptureBufferPool
45 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> {
46 public:
47 static const int kInvalidId;
49 // Abstraction of a pool's buffer data buffer and size for clients.
50 class BufferHandle {
51 public:
52 virtual ~BufferHandle() {}
53 virtual size_t size() const = 0;
54 virtual void* data() = 0;
55 virtual ClientBuffer AsClientBuffer() = 0;
56 #if defined(OS_POSIX)
57 virtual base::FileDescriptor AsPlatformFile() = 0;
58 #endif
61 explicit VideoCaptureBufferPool(int count);
63 // One-time (per client/per-buffer) initialization to share a particular
64 // buffer to a process. The size of the allocation is returned as
65 // |memory_size|.
66 base::SharedMemoryHandle ShareToProcess(int buffer_id,
67 base::ProcessHandle process_handle,
68 size_t* memory_size);
70 // Try and obtain a BufferHandle for |buffer_id|.
71 scoped_ptr<BufferHandle> GetBufferHandle(int buffer_id);
73 // Reserve or allocate a buffer to support a packed frame of |dimensions| of
74 // pixel |format| and return its id. This will fail (returning kInvalidId) if
75 // the pool already is at its |count| limit of the number of allocations, and
76 // all allocated buffers are in use by the producer and/or consumers.
78 // If successful, the reserved buffer remains reserved (and writable by the
79 // producer) until ownership is transferred either to the consumer via
80 // HoldForConsumers(), or back to the pool with
81 // RelinquishProducerReservation().
83 // On occasion, this call will decide to free an old buffer to make room for a
84 // new allocation at a larger size. If so, the ID of the destroyed buffer is
85 // returned via |buffer_id_to_drop|.
86 int ReserveForProducer(media::VideoCapturePixelFormat format,
87 media::VideoPixelStorage storage,
88 const gfx::Size& dimensions,
89 int* buffer_id_to_drop);
91 // Indicate that a buffer held for the producer should be returned back to the
92 // pool without passing on to the consumer. This effectively is the opposite
93 // of ReserveForProducer().
94 void RelinquishProducerReservation(int buffer_id);
96 // Transfer a buffer from producer to consumer ownership.
97 // |buffer_id| must be a buffer index previously returned by
98 // ReserveForProducer(), and not already passed to HoldForConsumers().
99 void HoldForConsumers(int buffer_id, int num_clients);
101 // Indicate that one or more consumers are done with a particular buffer. This
102 // effectively is the opposite of HoldForConsumers(). Once the consumers are
103 // done, a buffer is returned to the pool for reuse.
104 void RelinquishConsumerHold(int buffer_id, int num_clients);
106 // Returns a snapshot of the current number of buffers in-use divided by the
107 // maximum |count_|.
108 double GetBufferPoolUtilization() const;
110 private:
111 class GpuMemoryBufferTracker;
112 class SharedMemTracker;
113 // Generic class to keep track of the state of a given mappable resource. Each
114 // Tracker carries indication of pixel format and storage type.
115 class Tracker {
116 public:
117 static scoped_ptr<Tracker> CreateTracker(bool use_gmb);
119 Tracker()
120 : pixel_count_(0), held_by_producer_(false), consumer_hold_count_(0) {}
121 virtual bool Init(media::VideoCapturePixelFormat format,
122 media::VideoPixelStorage storage_type,
123 const gfx::Size& dimensions) = 0;
124 virtual ~Tracker();
126 size_t pixel_count() const { return pixel_count_; }
127 void set_pixel_count(size_t count) { pixel_count_ = count; }
128 media::VideoCapturePixelFormat pixel_format() const {
129 return pixel_format_;
131 void set_pixel_format(media::VideoCapturePixelFormat format) {
132 pixel_format_ = format;
134 media::VideoPixelStorage storage_type() const { return storage_type_; }
135 void set_storage_type(media::VideoPixelStorage storage_type) {
136 storage_type_ = storage_type;
138 bool held_by_producer() const { return held_by_producer_; }
139 void set_held_by_producer(bool value) { held_by_producer_ = value; }
140 int consumer_hold_count() const { return consumer_hold_count_; }
141 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; }
143 // Returns a handle to the underlying storage, be that a block of Shared
144 // Memory, or a GpuMemoryBuffer.
145 virtual scoped_ptr<BufferHandle> GetBufferHandle() = 0;
146 // The actual size of the underlying backing resource.
147 virtual size_t mapped_size() const = 0;
149 virtual bool ShareToProcess(base::ProcessHandle process_handle,
150 base::SharedMemoryHandle* new_handle) = 0;
152 private:
153 size_t pixel_count_;
154 media::VideoCapturePixelFormat pixel_format_;
155 media::VideoPixelStorage storage_type_;
156 // Indicates whether this Tracker is currently referenced by the producer.
157 bool held_by_producer_;
158 // Number of consumer processes which hold this Tracker.
159 int consumer_hold_count_;
162 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>;
163 virtual ~VideoCaptureBufferPool();
165 int ReserveForProducerInternal(media::VideoCapturePixelFormat format,
166 media::VideoPixelStorage storage,
167 const gfx::Size& dimensions,
168 int* tracker_id_to_drop);
170 Tracker* GetTracker(int buffer_id);
172 // The max number of buffers that the pool is allowed to have at any moment.
173 const int count_;
175 // Protects everything below it.
176 mutable base::Lock lock_;
178 // The ID of the next buffer.
179 int next_buffer_id_;
181 // The buffers, indexed by the first parameter, a buffer id.
182 using TrackerMap = std::map<int, Tracker*>;
183 TrackerMap trackers_;
185 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool);
188 } // namespace content
190 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_