blob: 76a5b7d00044572a991d3d3712186aed8e62c4ed [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
Emilian Peev6f823722017-12-07 18:05:49 +000020#include <unordered_set>
21
Shuzhen Wang0129d522016-10-30 22:43:41 -070022#include <gui/IConsumerListener.h>
23#include <gui/IProducerListener.h>
24#include <gui/BufferItemConsumer.h>
25
26#include <utils/Condition.h>
27#include <utils/Mutex.h>
28#include <utils/StrongPointer.h>
29#include <utils/Timers.h>
30
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080031#define SP_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
32#define SP_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
33#define SP_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
34#define SP_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
35
Shuzhen Wang0129d522016-10-30 22:43:41 -070036namespace android {
37
38class GraphicBuffer;
39class IGraphicBufferConsumer;
40class IGraphicBufferProducer;
41
42// Camera3StreamSplitter is an autonomous class that manages one input BufferQueue
43// and multiple output BufferQueues. By using the buffer attach and detach logic
44// in BufferQueue, it is able to present the illusion of a single split
45// BufferQueue, where each buffer queued to the input is available to be
46// acquired by each of the outputs, and is able to be dequeued by the input
47// again only once all of the outputs have released it.
48class Camera3StreamSplitter : public BnConsumerListener {
49public:
50
51 // Constructor
52 Camera3StreamSplitter() = default;
53
54 // Connect to the stream splitter by creating buffer queue and connecting it
55 // with output surfaces.
Emilian Peev40ead602017-09-26 15:46:36 +010056 status_t connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
57 uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
58 uint32_t height, android::PixelFormat format, sp<Surface>* consumer);
Shuzhen Wang0129d522016-10-30 22:43:41 -070059
60 // addOutput adds an output BufferQueue to the splitter. The splitter
61 // connects to outputQueue as a CPU producer, and any buffers queued
Emilian Peev40ead602017-09-26 15:46:36 +010062 // to the input will be queued to each output. If any output is abandoned
63 // by its consumer, the splitter will abandon its input queue (see onAbandoned).
Shuzhen Wang0129d522016-10-30 22:43:41 -070064 //
65 // A return value other than NO_ERROR means that an error has occurred and
66 // outputQueue has not been added to the splitter. BAD_VALUE is returned if
67 // outputQueue is NULL. See IGraphicBufferProducer::connect for explanations
68 // of other error codes.
Emilian Peev40ead602017-09-26 15:46:36 +010069 status_t addOutput(size_t surfaceId, const sp<Surface>& outputQueue);
70
71 //removeOutput will remove a BufferQueue that was previously added to
72 //the splitter outputs. Any pending buffers in the BufferQueue will get
73 //reclaimed.
74 status_t removeOutput(size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -070075
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080076 // Notification that the graphic buffer has been released to the input
77 // BufferQueue. The buffer should be reused by the camera device instead of
78 // queuing to the outputs.
79 status_t notifyBufferReleased(const sp<GraphicBuffer>& buffer);
80
81 // Attach a buffer to the specified outputs. This call reserves a buffer
82 // slot in the output queue.
83 status_t attachBufferToOutputs(ANativeWindowBuffer* anb,
84 const std::vector<size_t>& surface_ids);
85
86 // Get return value of onFrameAvailable to work around problem that
87 // onFrameAvailable is void. This function should be called by the producer
88 // right after calling queueBuffer().
89 status_t getOnFrameAvailableResult();
Shuzhen Wang0129d522016-10-30 22:43:41 -070090
91 // Disconnect the buffer queue from output surfaces.
92 void disconnect();
93
94private:
95 // From IConsumerListener
96 //
97 // During this callback, we store some tracking information, detach the
98 // buffer from the input, and attach it to each of the outputs. This call
99 // can block if there are too many outstanding buffers. If it blocks, it
100 // will resume when onBufferReleasedByOutput releases a buffer back to the
101 // input.
102 void onFrameAvailable(const BufferItem& item) override;
103
104 // From IConsumerListener
105 // We don't care about released buffers because we detach each buffer as
106 // soon as we acquire it. See the comment for onBufferReleased below for
107 // some clarifying notes about the name.
108 void onBuffersReleased() override {}
109
110 // From IConsumerListener
111 // We don't care about sideband streams, since we won't be splitting them
112 void onSidebandStreamChanged() override {}
113
114 // This is the implementation of the onBufferReleased callback from
115 // IProducerListener. It gets called from an OutputListener (see below), and
116 // 'from' is which producer interface from which the callback was received.
117 //
118 // During this callback, we detach the buffer from the output queue that
119 // generated the callback, update our state tracking to see if this is the
120 // last output releasing the buffer, and if so, release it to the input.
121 // If we release the buffer to the input, we allow a blocked
122 // onFrameAvailable call to proceed.
123 void onBufferReleasedByOutput(const sp<IGraphicBufferProducer>& from);
124
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800125 // This is the implementation of onBufferReleasedByOutput without the mutex locked.
126 // It could either be called from onBufferReleasedByOutput or from
127 // onFrameAvailable when a buffer in the async buffer queue is overwritten.
Emilian Peev40ead602017-09-26 15:46:36 +0100128 void onBufferReleasedByOutputLocked(const sp<IGraphicBufferProducer>& from, size_t surfaceId);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800129
Shuzhen Wang0129d522016-10-30 22:43:41 -0700130 // When this is called, the splitter disconnects from (i.e., abandons) its
131 // input queue and signals any waiting onFrameAvailable calls to wake up.
132 // It still processes callbacks from other outputs, but only detaches their
133 // buffers so they can continue operating until they run out of buffers to
134 // acquire. This must be called with mMutex locked.
135 void onAbandonedLocked();
136
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800137 // Decrement the buffer's reference count. Once the reference count becomes
138 // 0, return the buffer back to the input BufferQueue.
Emilian Peev40ead602017-09-26 15:46:36 +0100139 void decrementBufRefCountLocked(uint64_t id, size_t surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800140
Shuzhen Wang0129d522016-10-30 22:43:41 -0700141 // This is a thin wrapper class that lets us determine which BufferQueue
142 // the IProducerListener::onBufferReleased callback is associated with. We
143 // create one of these per output BufferQueue, and then pass the producer
144 // into onBufferReleasedByOutput above.
145 class OutputListener : public BnProducerListener,
146 public IBinder::DeathRecipient {
147 public:
148 OutputListener(wp<Camera3StreamSplitter> splitter,
149 wp<IGraphicBufferProducer> output);
150 virtual ~OutputListener() = default;
151
152 // From IProducerListener
153 void onBufferReleased() override;
154
155 // From IBinder::DeathRecipient
156 void binderDied(const wp<IBinder>& who) override;
157
158 private:
159 wp<Camera3StreamSplitter> mSplitter;
160 wp<IGraphicBufferProducer> mOutput;
161 };
162
163 class BufferTracker {
164 public:
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800165 BufferTracker(const sp<GraphicBuffer>& buffer,
166 const std::vector<size_t>& requestedSurfaces);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700167 ~BufferTracker() = default;
168
169 const sp<GraphicBuffer>& getBuffer() const { return mBuffer; }
170 const sp<Fence>& getMergedFence() const { return mMergedFence; }
171
172 void mergeFence(const sp<Fence>& with);
173
174 // Returns the new value
175 // Only called while mMutex is held
Emilian Peev40ead602017-09-26 15:46:36 +0100176 size_t decrementReferenceCountLocked(size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700177
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800178 const std::vector<size_t> requestedSurfaces() const { return mRequestedSurfaces; }
179
Shuzhen Wang0129d522016-10-30 22:43:41 -0700180 private:
181
182 // Disallow copying
183 BufferTracker(const BufferTracker& other);
184 BufferTracker& operator=(const BufferTracker& other);
185
186 sp<GraphicBuffer> mBuffer; // One instance that holds this native handle
187 sp<Fence> mMergedFence;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800188
189 // Request surfaces for a particular buffer. And when the buffer becomes
190 // available from the input queue, the registered surfaces are used to decide
191 // which output is the buffer sent to.
192 std::vector<size_t> mRequestedSurfaces;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700193 size_t mReferenceCount;
194 };
195
Shuzhen Wang0129d522016-10-30 22:43:41 -0700196 // Must be accessed through RefBase
197 virtual ~Camera3StreamSplitter();
198
Emilian Peev40ead602017-09-26 15:46:36 +0100199 status_t addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue);
200
201 status_t removeOutputLocked(size_t surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800202
203 // Send a buffer to particular output, and increment the reference count
204 // of the buffer. If this output is abandoned, the buffer's reference count
205 // won't be incremented.
206 status_t outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100207 const BufferItem& bufferItem, size_t surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700208
209 // Get unique name for the buffer queue consumer
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800210 String8 getUniqueConsumerName();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700211
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800212 // Helper function to get the BufferQueue slot where a particular buffer is attached to.
213 int getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
214 const sp<GraphicBuffer>& gb);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800215
216 // Sum of max consumer buffers for all outputs
217 size_t mMaxConsumerBuffers = 0;
218 size_t mMaxHalBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100219 uint32_t mWidth = 0;
220 uint32_t mHeight = 0;
221 android::PixelFormat mFormat = android::PIXEL_FORMAT_NONE;
222 uint64_t mProducerUsage = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700223
224 static const nsecs_t kDequeueBufferTimeout = s2ns(1); // 1 sec
225
Shuzhen Wang0129d522016-10-30 22:43:41 -0700226 Mutex mMutex;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700227
228 sp<IGraphicBufferProducer> mProducer;
229 sp<IGraphicBufferConsumer> mConsumer;
230 sp<BufferItemConsumer> mBufferItemConsumer;
231 sp<Surface> mSurface;
232
Emilian Peev40ead602017-09-26 15:46:36 +0100233 //Map graphic buffer ids -> buffer items
234 std::unordered_map<uint64_t, BufferItem> mInputSlots;
235
236 //Map surface ids -> gbp outputs
237 std::unordered_map<int, sp<IGraphicBufferProducer> > mOutputs;
238
239 //Map surface ids -> consumer buffer count
240 std::unordered_map<int, size_t > mConsumerBufferCount;
241
Shuzhen Wang0129d522016-10-30 22:43:41 -0700242 // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking
243 // objects (which are mostly for counting how many outputs have released the
244 // buffer, but also contain merged release fences).
245 std::unordered_map<uint64_t, std::unique_ptr<BufferTracker> > mBuffers;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800246
247 struct GBPHash {
248 std::size_t operator()(const sp<IGraphicBufferProducer>& producer) const {
249 return std::hash<IGraphicBufferProducer *>{}(producer.get());
250 }
251 };
252
253 std::unordered_map<sp<IGraphicBufferProducer>, sp<OutputListener>,
254 GBPHash> mNotifiers;
255
256 typedef std::vector<sp<GraphicBuffer>> OutputSlots;
257 std::unordered_map<sp<IGraphicBufferProducer>, std::unique_ptr<OutputSlots>,
258 GBPHash> mOutputSlots;
259
Emilian Peev6f823722017-12-07 18:05:49 +0000260 //A set of buffers that could potentially stay in some of the outputs after removal
261 //and therefore should be detached from the input queue.
262 std::unordered_set<uint64_t> mDetachedBuffers;
263
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800264 // Latest onFrameAvailable return value
265 std::atomic<status_t> mOnFrameAvailableRes{0};
266
267 String8 mConsumerName;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700268};
269
270} // namespace android
271
272#endif