blob: 5a25712cb1f10ed354165aba942b9641cae3f94e [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.
64 status_t addOutput(sp<Surface>& outputQueue, size_t hal_max_buffers);
65
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
106 // When this is called, the splitter disconnects from (i.e., abandons) its
107 // input queue and signals any waiting onFrameAvailable calls to wake up.
108 // It still processes callbacks from other outputs, but only detaches their
109 // buffers so they can continue operating until they run out of buffers to
110 // acquire. This must be called with mMutex locked.
111 void onAbandonedLocked();
112
113 // This is a thin wrapper class that lets us determine which BufferQueue
114 // the IProducerListener::onBufferReleased callback is associated with. We
115 // create one of these per output BufferQueue, and then pass the producer
116 // into onBufferReleasedByOutput above.
117 class OutputListener : public BnProducerListener,
118 public IBinder::DeathRecipient {
119 public:
120 OutputListener(wp<Camera3StreamSplitter> splitter,
121 wp<IGraphicBufferProducer> output);
122 virtual ~OutputListener() = default;
123
124 // From IProducerListener
125 void onBufferReleased() override;
126
127 // From IBinder::DeathRecipient
128 void binderDied(const wp<IBinder>& who) override;
129
130 private:
131 wp<Camera3StreamSplitter> mSplitter;
132 wp<IGraphicBufferProducer> mOutput;
133 };
134
135 class BufferTracker {
136 public:
137 BufferTracker(const sp<GraphicBuffer>& buffer, size_t referenceCount);
138 ~BufferTracker() = default;
139
140 const sp<GraphicBuffer>& getBuffer() const { return mBuffer; }
141 const sp<Fence>& getMergedFence() const { return mMergedFence; }
142
143 void mergeFence(const sp<Fence>& with);
144
145 // Returns the new value
146 // Only called while mMutex is held
147 size_t decrementReferenceCountLocked();
148
149 private:
150
151 // Disallow copying
152 BufferTracker(const BufferTracker& other);
153 BufferTracker& operator=(const BufferTracker& other);
154
155 sp<GraphicBuffer> mBuffer; // One instance that holds this native handle
156 sp<Fence> mMergedFence;
157 size_t mReferenceCount;
158 };
159
160 // A deferred output is an output being added to the splitter after
161 // connect() call, whereas a non deferred output is added within connect()
162 // call.
163 enum class OutputType { NonDeferred, Deferred };
164
165 // Must be accessed through RefBase
166 virtual ~Camera3StreamSplitter();
167
168 status_t addOutputLocked(const sp<Surface>& outputQueue,
169 size_t hal_max_buffers, OutputType outputType);
170
171 // Get unique name for the buffer queue consumer
172 static String8 getUniqueConsumerName();
173
174 // Max consumer side buffers for deferred surface. This will be used as a
175 // lower bound for overall consumer side max buffers.
176 static const int MAX_BUFFERS_DEFERRED_OUTPUT = 2;
177 int mMaxConsumerBuffers = MAX_BUFFERS_DEFERRED_OUTPUT;
178
179 static const nsecs_t kDequeueBufferTimeout = s2ns(1); // 1 sec
180
181 // mIsAbandoned is set to true when an output dies. Once the Camera3StreamSplitter
182 // has been abandoned, it will continue to detach buffers from other
183 // outputs, but it will disconnect from the input and not attempt to
184 // communicate with it further.
185 bool mIsAbandoned = false;
186
187 Mutex mMutex;
188 Condition mReleaseCondition;
189 int mOutstandingBuffers = 0;
190
191 sp<IGraphicBufferProducer> mProducer;
192 sp<IGraphicBufferConsumer> mConsumer;
193 sp<BufferItemConsumer> mBufferItemConsumer;
194 sp<Surface> mSurface;
195
196 std::vector<sp<IGraphicBufferProducer> > mOutputs;
197 // Tracking which outputs should the buffer be attached and queued
198 // to for each input buffer.
199 std::vector<std::vector<size_t> > mRequestedSurfaces;
200
201 // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking
202 // objects (which are mostly for counting how many outputs have released the
203 // buffer, but also contain merged release fences).
204 std::unordered_map<uint64_t, std::unique_ptr<BufferTracker> > mBuffers;
205};
206
207} // namespace android
208
209#endif