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 <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
14 #include "base/process/process.h" | 14 #include "base/process/process.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
17 #include "media/base/video_capture_types.h" | |
17 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
18 | 19 |
19 namespace media { | |
20 | |
21 class VideoFrame; | |
22 | |
23 } // namespace media | |
24 | |
25 namespace content { | 20 namespace content { |
26 | 21 |
27 // A thread-safe class that does the bookkeeping and lifetime management for a | 22 // A thread-safe class that does the bookkeeping and lifetime management for a |
28 // pool of shared-memory pixel buffers cycled between an in-process producer | 23 // pool of pixel buffers cycled between an in-process producer (e.g. a |
29 // (e.g. a VideoCaptureDevice) and a set of out-of-process consumers. The pool | 24 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is |
30 // is intended to be orchestrated by a VideoCaptureController, but is designed | 25 // intended to be orchestrated by a VideoCaptureController, but is designed |
31 // to outlive the controller if necessary. | 26 // to outlive the controller if necessary. The pixel buffers may be backed by a |
27 // SharedMemory, but this is not compulsory. | |
32 // | 28 // |
33 // Producers get a buffer by calling ReserveForProducer(), and may pass on their | 29 // Producers get a buffer by calling ReserveForProducer(), and may pass on their |
34 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer | 30 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer |
35 // (without further processing) by calling RelinquishProducerReservation(). | 31 // (without further processing) by calling RelinquishProducerReservation(). |
36 // Consumers signal that they are done with the buffer by calling | 32 // Consumers signal that they are done with the buffer by calling |
37 // RelinquishConsumerHold(). | 33 // RelinquishConsumerHold(). |
38 // | 34 // |
39 // Buffers are allocated on demand, but there will never be more than |count| | 35 // Buffers are allocated on demand, but there will never be more than |count| |
40 // buffers in existence at any time. Buffers are identified by an int value | 36 // buffers in existence at any time. Buffers are identified by an int value |
41 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | 37 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by |
42 // some methods to indicate failure. The active set of buffer ids may change | 38 // some methods to indicate failure. The active set of buffer ids may change |
43 // over the lifetime of the buffer pool, as existing buffers are freed and | 39 // over the lifetime of the buffer pool, as existing buffers are freed and |
44 // reallocated at larger size. When reallocation occurs, new buffer IDs will | 40 // reallocated at larger size. When reallocation occurs, new buffer IDs will |
45 // circulate. | 41 // circulate. |
46 class CONTENT_EXPORT VideoCaptureBufferPool | 42 class CONTENT_EXPORT VideoCaptureBufferPool |
47 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | 43 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { |
48 public: | 44 public: |
49 static const int kInvalidId; | 45 static const int kInvalidId; |
50 explicit VideoCaptureBufferPool(int count); | 46 explicit VideoCaptureBufferPool(int count); |
51 | 47 |
52 // One-time (per client/per-buffer) initialization to share a particular | 48 // One-time (per client/per-buffer) initialization to share a particular |
53 // buffer to a process. The size of the allocation is returned as | 49 // buffer to a process. The size of the allocation is returned as |
54 // |memory_size|. | 50 // |memory_size|. |
55 base::SharedMemoryHandle ShareToProcess(int buffer_id, | 51 base::SharedMemoryHandle ShareToProcess(int buffer_id, |
56 base::ProcessHandle process_handle, | 52 base::ProcessHandle process_handle, |
57 size_t* memory_size); | 53 size_t* memory_size); |
58 | 54 |
59 // Query the memory parameters of |buffer_id|. Fills in parameters in the | 55 // Query the memory parameters of |buffer_id|. Fills in parameters in the |
60 // pointer arguments, and returns true iff the buffer exists. | 56 // pointer arguments, and returns true iff the buffer exists. |
61 bool GetBufferInfo(int buffer_id, void** memory, size_t* size); | 57 bool GetBufferInfo(int buffer_id, void** storage, size_t* size); |
62 | 58 |
63 // Reserve or allocate a buffer of at least |size| bytes and return its id. | 59 // Reserve or allocate a buffer of at least |size| bytes and return its id. |
64 // This will fail (returning kInvalidId) if the pool already is at its |count| | 60 // This will fail (returning kInvalidId) if the pool already is at its |count| |
65 // limit of the number of allocations, and all allocated buffers are in use by | 61 // limit of the number of allocations, and all allocated buffers are in use by |
66 // the producer and/or consumers. | 62 // the producer and/or consumers. |
67 // | 63 // |
68 // If successful, the reserved buffer remains reserved (and writable by the | 64 // If successful, the reserved buffer remains reserved (and writable by the |
69 // producer) until ownership is transferred either to the consumer via | 65 // producer) until ownership is transferred either to the consumer via |
70 // HoldForConsumers(), or back to the pool with | 66 // HoldForConsumers(), or back to the pool with |
71 // RelinquishProducerReservation(). | 67 // RelinquishProducerReservation(). |
72 // | 68 // |
73 // On occasion, this call will decide to free an old buffer to make room for a | 69 // On occasion, this call will decide to free an old buffer to make room for a |
74 // new allocation at a larger size. If so, the ID of the destroyed buffer is | 70 // new allocation at a larger size. If so, the ID of the destroyed buffer is |
75 // returned via |buffer_id_to_drop|. | 71 // returned via |buffer_id_to_drop|. |
76 int ReserveForProducer(size_t size, int* buffer_id_to_drop); | 72 int ReserveForProducer(const media::VideoCaptureFormat& format, |
73 int* buffer_id_to_drop); | |
77 | 74 |
78 // Indicate that a buffer held for the producer should be returned back to the | 75 // Indicate that a buffer held for the producer should be returned back to the |
79 // pool without passing on to the consumer. This effectively is the opposite | 76 // pool without passing on to the consumer. This effectively is the opposite |
80 // of ReserveForProducer(). | 77 // of ReserveForProducer(). |
81 void RelinquishProducerReservation(int buffer_id); | 78 void RelinquishProducerReservation(int buffer_id); |
82 | 79 |
83 // Transfer a buffer from producer to consumer ownership. | 80 // Transfer a buffer from producer to consumer ownership. |
84 // |buffer_id| must be a buffer index previously returned by | 81 // |buffer_id| must be a buffer index previously returned by |
85 // ReserveForProducer(), and not already passed to HoldForConsumers(). | 82 // ReserveForProducer(), and not already passed to HoldForConsumers(). |
86 void HoldForConsumers(int buffer_id, int num_clients); | 83 void HoldForConsumers(int buffer_id, int num_clients); |
87 | 84 |
88 // Indicate that one or more consumers are done with a particular buffer. This | 85 // Indicate that one or more consumers are done with a particular buffer. This |
89 // effectively is the opposite of HoldForConsumers(). Once the consumers are | 86 // effectively is the opposite of HoldForConsumers(). Once the consumers are |
90 // done, a buffer is returned to the pool for reuse. | 87 // done, a buffer is returned to the pool for reuse. |
91 void RelinquishConsumerHold(int buffer_id, int num_clients); | 88 void RelinquishConsumerHold(int buffer_id, int num_clients); |
92 | 89 |
93 int count() const { return count_; } | 90 int count() const { return count_; } |
94 | 91 |
95 private: | 92 private: |
96 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | 93 class SharedMemTracker; |
94 // Generic class to keep track of the state of a given mappable resource. | |
95 class Tracker { | |
96 public: | |
97 static scoped_ptr<Tracker> CreateTracker(); | |
97 | 98 |
98 // Per-buffer state. | 99 Tracker() : held_by_producer_(false), consumer_hold_count_(0) {} |
99 struct Buffer { | 100 virtual bool Init(const gfx::Size& dimensions) = 0; |
100 Buffer(); | 101 virtual ~Tracker(); |
101 | 102 |
102 // The memory created to be shared with renderer processes. | 103 bool held_by_producer() const { return held_by_producer_; } |
103 base::SharedMemory shared_memory; | 104 void set_held_by_producer(bool value) { held_by_producer_ = value; } |
105 int consumer_hold_count() const { return consumer_hold_count_; } | |
106 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; } | |
104 | 107 |
105 // Tracks whether this buffer is currently referenced by the producer. | 108 // Returns a void* to the underlying storage, be that a memory block for |
106 bool held_by_producer; | 109 // Shared Memory, or a GpuMemoryBuffer. |
110 virtual void* storage() = 0; | |
111 // Amount of bytes requested when first created. Can be zero if it does not | |
112 // need RAM, e.g. is allocated in GPU memory. | |
113 virtual size_t requested_size() = 0; | |
114 // The actual size of the underlying backing resource. | |
115 virtual size_t mapped_size() = 0; | |
107 | 116 |
108 // Number of consumer processes which hold this shared memory. | 117 virtual bool ShareToProcess(base::ProcessHandle process_handle, |
109 int consumer_hold_count; | 118 base::SharedMemoryHandle* new_handle) = 0; |
119 | |
120 private: | |
121 // Indicates whether this Tracker is currently referenced by the producer. | |
122 bool held_by_producer_; | |
123 // Number of consumer processes which hold this Tracker. | |
124 int consumer_hold_count_; | |
110 }; | 125 }; |
111 | 126 |
112 typedef std::map<int, Buffer*> BufferMap; | 127 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; |
113 | |
114 virtual ~VideoCaptureBufferPool(); | 128 virtual ~VideoCaptureBufferPool(); |
115 | 129 |
116 int ReserveForProducerInternal(size_t size, int* buffer_id_to_drop); | 130 int ReserveForProducerInternal(const media::VideoCaptureFormat& format, |
131 int* buffer_id_to_drop); | |
117 | 132 |
118 Buffer* GetBuffer(int buffer_id); | 133 Tracker* GetBuffer(int buffer_id); |
miu
2015/04/08 01:20:00
naming: Should be GetTracker() now. Also, there s
mcasas
2015/04/08 22:07:05
Done.
| |
119 | 134 |
120 // The max number of buffers that the pool is allowed to have at any moment. | 135 // The max number of buffers that the pool is allowed to have at any moment. |
121 const int count_; | 136 const int count_; |
122 | 137 |
123 // Protects everything below it. | 138 // Protects everything below it. |
124 base::Lock lock_; | 139 base::Lock lock_; |
125 | 140 |
126 // The ID of the next buffer. | 141 // The ID of the next buffer. |
127 int next_buffer_id_; | 142 int next_buffer_id_; |
128 | 143 |
129 // The buffers, indexed by |buffer_id|. | 144 // The buffers, indexed by |buffer_id|. |
145 typedef std::map<int, Tracker*> BufferMap; | |
miu
2015/04/08 01:20:00
Consider the newer, more readable C++11 style:
us
mcasas
2015/04/08 22:07:05
Done.
| |
130 BufferMap buffers_; | 146 BufferMap buffers_; |
131 | 147 |
132 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); | 148 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); |
133 }; | 149 }; |
134 | 150 |
135 } // namespace content | 151 } // namespace content |
136 | 152 |
137 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 153 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ |
OLD | NEW |