Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_buffer_pool.h
blobf3c22f74835b1822ffac3da6a2bd8275181c0d60
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/memory/ref_counted.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/process/process.h"
14 #include "base/synchronization/lock.h"
15 #include "content/common/content_export.h"
16 #include "media/base/video_capture_types.h"
17 #include "media/base/video_frame.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "ui/gfx/gpu_memory_buffer.h"
21 namespace content {
23 // A thread-safe class that does the bookkeeping and lifetime management for a
24 // pool of pixel buffers cycled between an in-process producer (e.g. a
25 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is
26 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed
27 // to outlive the controller if necessary. The pixel buffers may be backed by a
28 // SharedMemory, but this is not compulsory.
30 // Producers get a buffer by calling ReserveForProducer(), and may pass on their
31 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer
32 // (without further processing) by calling RelinquishProducerReservation().
33 // Consumers signal that they are done with the buffer by calling
34 // RelinquishConsumerHold().
36 // Buffers are allocated on demand, but there will never be more than |count|
37 // buffers in existence at any time. Buffers are identified by an int value
38 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by
39 // some methods to indicate failure. The active set of buffer ids may change
40 // over the lifetime of the buffer pool, as existing buffers are freed and
41 // reallocated at larger size. When reallocation occurs, new buffer IDs will
42 // circulate.
43 class CONTENT_EXPORT VideoCaptureBufferPool
44 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> {
45 public:
46 static const int kInvalidId;
48 // Abstraction of a pool's buffer data buffer and size for clients.
49 class BufferHandle {
50 public:
51 virtual ~BufferHandle() {}
52 virtual size_t size() const = 0;
53 virtual void* data() = 0;
54 virtual ClientBuffer AsClientBuffer() = 0;
57 explicit VideoCaptureBufferPool(int count);
59 // One-time (per client/per-buffer) initialization to share a particular
60 // buffer to a process. The size of the allocation is returned as
61 // |memory_size|.
62 base::SharedMemoryHandle ShareToProcess(int buffer_id,
63 base::ProcessHandle process_handle,
64 size_t* memory_size);
66 // Try and obtain a BufferHandle for |buffer_id|.
67 scoped_ptr<BufferHandle> GetBufferHandle(int buffer_id);
69 // Reserve or allocate a buffer to support a packed frame of |dimensions| of
70 // pixel |format| and return its id. This will fail (returning kInvalidId) if
71 // the pool already is at its |count| limit of the number of allocations, and
72 // all allocated buffers are in use by the producer and/or consumers.
74 // If successful, the reserved buffer remains reserved (and writable by the
75 // producer) until ownership is transferred either to the consumer via
76 // HoldForConsumers(), or back to the pool with
77 // RelinquishProducerReservation().
79 // On occasion, this call will decide to free an old buffer to make room for a
80 // new allocation at a larger size. If so, the ID of the destroyed buffer is
81 // returned via |buffer_id_to_drop|.
82 int ReserveForProducer(media::VideoPixelFormat format,
83 const gfx::Size& dimensions,
84 int* buffer_id_to_drop);
86 // Indicate that a buffer held for the producer should be returned back to the
87 // pool without passing on to the consumer. This effectively is the opposite
88 // of ReserveForProducer().
89 void RelinquishProducerReservation(int buffer_id);
91 // Transfer a buffer from producer to consumer ownership.
92 // |buffer_id| must be a buffer index previously returned by
93 // ReserveForProducer(), and not already passed to HoldForConsumers().
94 void HoldForConsumers(int buffer_id, int num_clients);
96 // Indicate that one or more consumers are done with a particular buffer. This
97 // effectively is the opposite of HoldForConsumers(). Once the consumers are
98 // done, a buffer is returned to the pool for reuse.
99 void RelinquishConsumerHold(int buffer_id, int num_clients);
101 private:
102 class GpuMemoryBufferTracker;
103 class SharedMemTracker;
104 // Generic class to keep track of the state of a given mappable resource.
105 class Tracker {
106 public:
107 static scoped_ptr<Tracker> CreateTracker(bool use_gmb);
109 Tracker()
110 : pixel_count_(0), held_by_producer_(false), consumer_hold_count_(0) {}
111 virtual bool Init(media::VideoFrame::Format format,
112 const gfx::Size& dimensions) = 0;
113 virtual ~Tracker();
115 size_t pixel_count() const { return pixel_count_; }
116 void set_pixel_count(size_t count) { pixel_count_ = count; }
117 bool held_by_producer() const { return held_by_producer_; }
118 void set_held_by_producer(bool value) { held_by_producer_ = value; }
119 int consumer_hold_count() const { return consumer_hold_count_; }
120 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; }
122 // Returns a handle to the underlying storage, be that a block of Shared
123 // Memory, or a GpuMemoryBuffer.
124 virtual scoped_ptr<BufferHandle> GetBufferHandle() = 0;
125 // The actual size of the underlying backing resource.
126 virtual size_t mapped_size() const = 0;
128 virtual bool ShareToProcess(base::ProcessHandle process_handle,
129 base::SharedMemoryHandle* new_handle) = 0;
131 private:
132 size_t pixel_count_;
133 // Indicates whether this Tracker is currently referenced by the producer.
134 bool held_by_producer_;
135 // Number of consumer processes which hold this Tracker.
136 int consumer_hold_count_;
139 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>;
140 virtual ~VideoCaptureBufferPool();
142 int ReserveForProducerInternal(media::VideoPixelFormat format,
143 const gfx::Size& dimensions,
144 int* tracker_id_to_drop);
146 Tracker* GetTracker(int buffer_id);
148 // The max number of buffers that the pool is allowed to have at any moment.
149 const int count_;
151 // Protects everything below it.
152 base::Lock lock_;
154 // The ID of the next buffer.
155 int next_buffer_id_;
157 // The buffers, indexed by the first parameter, a buffer id.
158 using TrackerMap = std::map<int, Tracker*>;
159 TrackerMap trackers_;
161 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool);
164 } // namespace content
166 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_