blob: 704f88fbcbc003c424c19da33889268746bb1bc4 [file] [log] [blame]
Shuzhen Wang0129d522016-10-30 22:43:41 -07001/*
2 * Copyright 2014,2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SERVERS_STREAMSPLITTER_H
18#define ANDROID_SERVERS_STREAMSPLITTER_H
19
20#include <gui/IConsumerListener.h>
21#include <gui/IProducerListener.h>
22#include <gui/BufferItemConsumer.h>
23
24#include <utils/Condition.h>
25#include <utils/Mutex.h>
26#include <utils/StrongPointer.h>
27#include <utils/Timers.h>
28
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080029#define SP_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
30#define SP_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
31#define SP_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
32#define SP_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
33
Shuzhen Wang0129d522016-10-30 22:43:41 -070034namespace android {
35
36class GraphicBuffer;
37class IGraphicBufferConsumer;
38class IGraphicBufferProducer;
39
40// Camera3StreamSplitter is an autonomous class that manages one input BufferQueue
41// and multiple output BufferQueues. By using the buffer attach and detach logic
42// in BufferQueue, it is able to present the illusion of a single split
43// BufferQueue, where each buffer queued to the input is available to be
44// acquired by each of the outputs, and is able to be dequeued by the input
45// again only once all of the outputs have released it.
46class Camera3StreamSplitter : public BnConsumerListener {
47public:
48
49 // Constructor
50 Camera3StreamSplitter() = default;
51
52 // Connect to the stream splitter by creating buffer queue and connecting it
53 // with output surfaces.
Emilian Peev40ead602017-09-26 15:46:36 +010054 status_t connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
55 uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
56 uint32_t height, android::PixelFormat format, sp<Surface>* consumer);
Shuzhen Wang0129d522016-10-30 22:43:41 -070057
58 // addOutput adds an output BufferQueue to the splitter. The splitter
59 // connects to outputQueue as a CPU producer, and any buffers queued
Emilian Peev40ead602017-09-26 15:46:36 +010060 // to the input will be queued to each output. If any output is abandoned
61 // by its consumer, the splitter will abandon its input queue (see onAbandoned).
Shuzhen Wang0129d522016-10-30 22:43:41 -070062 //
63 // A return value other than NO_ERROR means that an error has occurred and
64 // outputQueue has not been added to the splitter. BAD_VALUE is returned if
65 // outputQueue is NULL. See IGraphicBufferProducer::connect for explanations
66 // of other error codes.
Emilian Peev40ead602017-09-26 15:46:36 +010067 status_t addOutput(size_t surfaceId, const sp<Surface>& outputQueue);
68
69 //removeOutput will remove a BufferQueue that was previously added to
70 //the splitter outputs. Any pending buffers in the BufferQueue will get
71 //reclaimed.
72 status_t removeOutput(size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -070073
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080074 // Notification that the graphic buffer has been released to the input
75 // BufferQueue. The buffer should be reused by the camera device instead of
76 // queuing to the outputs.
77 status_t notifyBufferReleased(const sp<GraphicBuffer>& buffer);
78
79 // Attach a buffer to the specified outputs. This call reserves a buffer
80 // slot in the output queue.
81 status_t attachBufferToOutputs(ANativeWindowBuffer* anb,
82 const std::vector<size_t>& surface_ids);
83
84 // Get return value of onFrameAvailable to work around problem that
85 // onFrameAvailable is void. This function should be called by the producer
86 // right after calling queueBuffer().
87 status_t getOnFrameAvailableResult();
Shuzhen Wang0129d522016-10-30 22:43:41 -070088
89 // Disconnect the buffer queue from output surfaces.
90 void disconnect();
91
92private:
93 // From IConsumerListener
94 //
95 // During this callback, we store some tracking information, detach the
96 // buffer from the input, and attach it to each of the outputs. This call
97 // can block if there are too many outstanding buffers. If it blocks, it
98 // will resume when onBufferReleasedByOutput releases a buffer back to the
99 // input.
100 void onFrameAvailable(const BufferItem& item) override;
101
102 // From IConsumerListener
103 // We don't care about released buffers because we detach each buffer as
104 // soon as we acquire it. See the comment for onBufferReleased below for
105 // some clarifying notes about the name.
106 void onBuffersReleased() override {}
107
108 // From IConsumerListener
109 // We don't care about sideband streams, since we won't be splitting them
110 void onSidebandStreamChanged() override {}
111
112 // This is the implementation of the onBufferReleased callback from
113 // IProducerListener. It gets called from an OutputListener (see below), and
114 // 'from' is which producer interface from which the callback was received.
115 //
116 // During this callback, we detach the buffer from the output queue that
117 // generated the callback, update our state tracking to see if this is the
118 // last output releasing the buffer, and if so, release it to the input.
119 // If we release the buffer to the input, we allow a blocked
120 // onFrameAvailable call to proceed.
121 void onBufferReleasedByOutput(const sp<IGraphicBufferProducer>& from);
122
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800123 // This is the implementation of onBufferReleasedByOutput without the mutex locked.
124 // It could either be called from onBufferReleasedByOutput or from
125 // onFrameAvailable when a buffer in the async buffer queue is overwritten.
Emilian Peev40ead602017-09-26 15:46:36 +0100126 void onBufferReleasedByOutputLocked(const sp<IGraphicBufferProducer>& from, size_t surfaceId);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800127
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128 // When this is called, the splitter disconnects from (i.e., abandons) its
129 // input queue and signals any waiting onFrameAvailable calls to wake up.
130 // It still processes callbacks from other outputs, but only detaches their
131 // buffers so they can continue operating until they run out of buffers to
132 // acquire. This must be called with mMutex locked.
133 void onAbandonedLocked();
134
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800135 // Decrement the buffer's reference count. Once the reference count becomes
136 // 0, return the buffer back to the input BufferQueue.
Emilian Peev40ead602017-09-26 15:46:36 +0100137 void decrementBufRefCountLocked(uint64_t id, size_t surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800138
Shuzhen Wang0129d522016-10-30 22:43:41 -0700139 // This is a thin wrapper class that lets us determine which BufferQueue
140 // the IProducerListener::onBufferReleased callback is associated with. We
141 // create one of these per output BufferQueue, and then pass the producer
142 // into onBufferReleasedByOutput above.
143 class OutputListener : public BnProducerListener,
144 public IBinder::DeathRecipient {
145 public:
146 OutputListener(wp<Camera3StreamSplitter> splitter,
147 wp<IGraphicBufferProducer> output);
148 virtual ~OutputListener() = default;
149
150 // From IProducerListener
151 void onBufferReleased() override;
152
153 // From IBinder::DeathRecipient
154 void binderDied(const wp<IBinder>& who) override;
155
156 private:
157 wp<Camera3StreamSplitter> mSplitter;
158 wp<IGraphicBufferProducer> mOutput;
159 };
160
161 class BufferTracker {
162 public:
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800163 BufferTracker(const sp<GraphicBuffer>& buffer,
164 const std::vector<size_t>& requestedSurfaces);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700165 ~BufferTracker() = default;
166
167 const sp<GraphicBuffer>& getBuffer() const { return mBuffer; }
168 const sp<Fence>& getMergedFence() const { return mMergedFence; }
169
170 void mergeFence(const sp<Fence>& with);
171
172 // Returns the new value
173 // Only called while mMutex is held
Emilian Peev40ead602017-09-26 15:46:36 +0100174 size_t decrementReferenceCountLocked(size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700175
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800176 const std::vector<size_t> requestedSurfaces() const { return mRequestedSurfaces; }
177
Shuzhen Wang0129d522016-10-30 22:43:41 -0700178 private:
179
180 // Disallow copying
181 BufferTracker(const BufferTracker& other);
182 BufferTracker& operator=(const BufferTracker& other);
183
184 sp<GraphicBuffer> mBuffer; // One instance that holds this native handle
185 sp<Fence> mMergedFence;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800186
187 // Request surfaces for a particular buffer. And when the buffer becomes
188 // available from the input queue, the registered surfaces are used to decide
189 // which output is the buffer sent to.
190 std::vector<size_t> mRequestedSurfaces;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700191 size_t mReferenceCount;
192 };
193
Shuzhen Wang0129d522016-10-30 22:43:41 -0700194 // Must be accessed through RefBase
195 virtual ~Camera3StreamSplitter();
196
Emilian Peev40ead602017-09-26 15:46:36 +0100197 status_t addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue);
198
199 status_t removeOutputLocked(size_t surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800200
201 // Send a buffer to particular output, and increment the reference count
202 // of the buffer. If this output is abandoned, the buffer's reference count
203 // won't be incremented.
204 status_t outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100205 const BufferItem& bufferItem, size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700206
207 // Get unique name for the buffer queue consumer
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800208 String8 getUniqueConsumerName();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700209
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800210 // Helper function to get the BufferQueue slot where a particular buffer is attached to.
211 int getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
212 const sp<GraphicBuffer>& gb);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800213
214 // Sum of max consumer buffers for all outputs
215 size_t mMaxConsumerBuffers = 0;
216 size_t mMaxHalBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100217 uint32_t mWidth = 0;
218 uint32_t mHeight = 0;
219 android::PixelFormat mFormat = android::PIXEL_FORMAT_NONE;
220 uint64_t mProducerUsage = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700221
222 static const nsecs_t kDequeueBufferTimeout = s2ns(1); // 1 sec
223
Shuzhen Wang0129d522016-10-30 22:43:41 -0700224 Mutex mMutex;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700225
226 sp<IGraphicBufferProducer> mProducer;
227 sp<IGraphicBufferConsumer> mConsumer;
228 sp<BufferItemConsumer> mBufferItemConsumer;
229 sp<Surface> mSurface;
230
Emilian Peev40ead602017-09-26 15:46:36 +0100231 //Map graphic buffer ids -> buffer items
232 std::unordered_map<uint64_t, BufferItem> mInputSlots;
233
234 //Map surface ids -> gbp outputs
235 std::unordered_map<int, sp<IGraphicBufferProducer> > mOutputs;
236
237 //Map surface ids -> consumer buffer count
238 std::unordered_map<int, size_t > mConsumerBufferCount;
239
Shuzhen Wang0129d522016-10-30 22:43:41 -0700240 // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking
241 // objects (which are mostly for counting how many outputs have released the
242 // buffer, but also contain merged release fences).
243 std::unordered_map<uint64_t, std::unique_ptr<BufferTracker> > mBuffers;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800244
245 struct GBPHash {
246 std::size_t operator()(const sp<IGraphicBufferProducer>& producer) const {
247 return std::hash<IGraphicBufferProducer *>{}(producer.get());
248 }
249 };
250
251 std::unordered_map<sp<IGraphicBufferProducer>, sp<OutputListener>,
252 GBPHash> mNotifiers;
253
254 typedef std::vector<sp<GraphicBuffer>> OutputSlots;
255 std::unordered_map<sp<IGraphicBufferProducer>, std::unique_ptr<OutputSlots>,
256 GBPHash> mOutputSlots;
257
258 // Latest onFrameAvailable return value
259 std::atomic<status_t> mOnFrameAvailableRes{0};
260
261 String8 mConsumerName;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700262};
263
264} // namespace android
265
266#endif