blob: 92371ffcd56e4597a6da70694a8082321ec08558 [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
29namespace android {
30
31class GraphicBuffer;
32class IGraphicBufferConsumer;
33class IGraphicBufferProducer;
34
35// Camera3StreamSplitter is an autonomous class that manages one input BufferQueue
36// and multiple output BufferQueues. By using the buffer attach and detach logic
37// in BufferQueue, it is able to present the illusion of a single split
38// BufferQueue, where each buffer queued to the input is available to be
39// acquired by each of the outputs, and is able to be dequeued by the input
40// again only once all of the outputs have released it.
41class Camera3StreamSplitter : public BnConsumerListener {
42public:
43
44 // Constructor
45 Camera3StreamSplitter() = default;
46
47 // Connect to the stream splitter by creating buffer queue and connecting it
48 // with output surfaces.
49 status_t connect(const std::vector<sp<Surface> >& surfaces,
50 uint32_t consumerUsage, size_t hal_max_buffers,
51 sp<Surface>& consumer);
52
53 // addOutput adds an output BufferQueue to the splitter. The splitter
54 // connects to outputQueue as a CPU producer, and any buffers queued
55 // to the input will be queued to each output. It is assumed that all of the
56 // outputs are added before any buffers are queued on the input. If any
57 // output is abandoned by its consumer, the splitter will abandon its input
58 // queue (see onAbandoned).
59 //
60 // A return value other than NO_ERROR means that an error has occurred and
61 // outputQueue has not been added to the splitter. BAD_VALUE is returned if
62 // outputQueue is NULL. See IGraphicBufferProducer::connect for explanations
63 // of other error codes.
Shuzhen Wang758c2152017-01-10 18:26:18 -080064 status_t addOutput(const sp<Surface>& outputQueue, size_t hal_max_buffers);
Shuzhen Wang0129d522016-10-30 22:43:41 -070065
66 // Request surfaces for a particular frame number. The requested surfaces
67 // are stored in a FIFO queue. And when the buffer becomes available from the
68 // input queue, the registered surfaces are used to decide which output is
69 // the buffer sent to.
70 status_t notifyRequestedSurfaces(const std::vector<size_t>& surfaces);
71
72 // Disconnect the buffer queue from output surfaces.
73 void disconnect();
74
75private:
76 // From IConsumerListener
77 //
78 // During this callback, we store some tracking information, detach the
79 // buffer from the input, and attach it to each of the outputs. This call
80 // can block if there are too many outstanding buffers. If it blocks, it
81 // will resume when onBufferReleasedByOutput releases a buffer back to the
82 // input.
83 void onFrameAvailable(const BufferItem& item) override;
84
85 // From IConsumerListener
86 // We don't care about released buffers because we detach each buffer as
87 // soon as we acquire it. See the comment for onBufferReleased below for
88 // some clarifying notes about the name.
89 void onBuffersReleased() override {}
90
91 // From IConsumerListener
92 // We don't care about sideband streams, since we won't be splitting them
93 void onSidebandStreamChanged() override {}
94
95 // This is the implementation of the onBufferReleased callback from
96 // IProducerListener. It gets called from an OutputListener (see below), and
97 // 'from' is which producer interface from which the callback was received.
98 //
99 // During this callback, we detach the buffer from the output queue that
100 // generated the callback, update our state tracking to see if this is the
101 // last output releasing the buffer, and if so, release it to the input.
102 // If we release the buffer to the input, we allow a blocked
103 // onFrameAvailable call to proceed.
104 void onBufferReleasedByOutput(const sp<IGraphicBufferProducer>& from);
105
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800106 // This is the implementation of onBufferReleasedByOutput without the mutex locked.
107 // It could either be called from onBufferReleasedByOutput or from
108 // onFrameAvailable when a buffer in the async buffer queue is overwritten.
109 void onBufferReleasedByOutputLocked(const sp<IGraphicBufferProducer>& from);
110
Shuzhen Wang0129d522016-10-30 22:43:41 -0700111 // When this is called, the splitter disconnects from (i.e., abandons) its
112 // input queue and signals any waiting onFrameAvailable calls to wake up.
113 // It still processes callbacks from other outputs, but only detaches their
114 // buffers so they can continue operating until they run out of buffers to
115 // acquire. This must be called with mMutex locked.
116 void onAbandonedLocked();
117
118 // This is a thin wrapper class that lets us determine which BufferQueue
119 // the IProducerListener::onBufferReleased callback is associated with. We
120 // create one of these per output BufferQueue, and then pass the producer
121 // into onBufferReleasedByOutput above.
122 class OutputListener : public BnProducerListener,
123 public IBinder::DeathRecipient {
124 public:
125 OutputListener(wp<Camera3StreamSplitter> splitter,
126 wp<IGraphicBufferProducer> output);
127 virtual ~OutputListener() = default;
128
129 // From IProducerListener
130 void onBufferReleased() override;
131
132 // From IBinder::DeathRecipient
133 void binderDied(const wp<IBinder>& who) override;
134
135 private:
136 wp<Camera3StreamSplitter> mSplitter;
137 wp<IGraphicBufferProducer> mOutput;
138 };
139
140 class BufferTracker {
141 public:
142 BufferTracker(const sp<GraphicBuffer>& buffer, size_t referenceCount);
143 ~BufferTracker() = default;
144
145 const sp<GraphicBuffer>& getBuffer() const { return mBuffer; }
146 const sp<Fence>& getMergedFence() const { return mMergedFence; }
147
148 void mergeFence(const sp<Fence>& with);
149
150 // Returns the new value
151 // Only called while mMutex is held
152 size_t decrementReferenceCountLocked();
153
154 private:
155
156 // Disallow copying
157 BufferTracker(const BufferTracker& other);
158 BufferTracker& operator=(const BufferTracker& other);
159
160 sp<GraphicBuffer> mBuffer; // One instance that holds this native handle
161 sp<Fence> mMergedFence;
162 size_t mReferenceCount;
163 };
164
165 // A deferred output is an output being added to the splitter after
166 // connect() call, whereas a non deferred output is added within connect()
167 // call.
168 enum class OutputType { NonDeferred, Deferred };
169
170 // Must be accessed through RefBase
171 virtual ~Camera3StreamSplitter();
172
173 status_t addOutputLocked(const sp<Surface>& outputQueue,
174 size_t hal_max_buffers, OutputType outputType);
175
176 // Get unique name for the buffer queue consumer
177 static String8 getUniqueConsumerName();
178
179 // Max consumer side buffers for deferred surface. This will be used as a
180 // lower bound for overall consumer side max buffers.
181 static const int MAX_BUFFERS_DEFERRED_OUTPUT = 2;
182 int mMaxConsumerBuffers = MAX_BUFFERS_DEFERRED_OUTPUT;
183
184 static const nsecs_t kDequeueBufferTimeout = s2ns(1); // 1 sec
185
186 // mIsAbandoned is set to true when an output dies. Once the Camera3StreamSplitter
187 // has been abandoned, it will continue to detach buffers from other
188 // outputs, but it will disconnect from the input and not attempt to
189 // communicate with it further.
190 bool mIsAbandoned = false;
191
192 Mutex mMutex;
193 Condition mReleaseCondition;
194 int mOutstandingBuffers = 0;
195
196 sp<IGraphicBufferProducer> mProducer;
197 sp<IGraphicBufferConsumer> mConsumer;
198 sp<BufferItemConsumer> mBufferItemConsumer;
199 sp<Surface> mSurface;
200
201 std::vector<sp<IGraphicBufferProducer> > mOutputs;
202 // Tracking which outputs should the buffer be attached and queued
203 // to for each input buffer.
204 std::vector<std::vector<size_t> > mRequestedSurfaces;
205
206 // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking
207 // objects (which are mostly for counting how many outputs have released the
208 // buffer, but also contain merged release fences).
209 std::unordered_map<uint64_t, std::unique_ptr<BufferTracker> > mBuffers;
210};
211
212} // namespace android
213
214#endif