blob: 9b107fc514557d6cfdb6c61c53616e22f6f9c2a3 [file] [log] [blame]
Dan Stoza99b18b42014-03-28 15:34:33 -07001/*
2 * Copyright 2014 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
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070017#include <inttypes.h>
18
Dan Stoza99b18b42014-03-28 15:34:33 -070019#define LOG_TAG "StreamSplitter"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Dan Stozadd264162015-03-12 13:58:47 -070023#include <gui/BufferItem.h>
Dan Stoza99b18b42014-03-28 15:34:33 -070024#include <gui/IGraphicBufferConsumer.h>
25#include <gui/IGraphicBufferProducer.h>
26#include <gui/StreamSplitter.h>
27
28#include <ui/GraphicBuffer.h>
29
30#include <binder/ProcessState.h>
31
32#include <utils/Trace.h>
33
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070034#include <system/window.h>
35
Dan Stoza99b18b42014-03-28 15:34:33 -070036namespace android {
37
38status_t StreamSplitter::createSplitter(
39 const sp<IGraphicBufferConsumer>& inputQueue,
40 sp<StreamSplitter>* outSplitter) {
Yi Kong48a619f2018-06-05 16:34:59 -070041 if (inputQueue == nullptr) {
Dan Stoza99b18b42014-03-28 15:34:33 -070042 ALOGE("createSplitter: inputQueue must not be NULL");
43 return BAD_VALUE;
44 }
Yi Kong48a619f2018-06-05 16:34:59 -070045 if (outSplitter == nullptr) {
Dan Stoza99b18b42014-03-28 15:34:33 -070046 ALOGE("createSplitter: outSplitter must not be NULL");
47 return BAD_VALUE;
48 }
49
Anton Ivanov81793802025-02-13 22:57:24 -080050 sp<StreamSplitter> splitter = sp<StreamSplitter>::make(inputQueue);
Dan Stoza99b18b42014-03-28 15:34:33 -070051 status_t status = splitter->mInput->consumerConnect(splitter, false);
52 if (status == NO_ERROR) {
53 splitter->mInput->setConsumerName(String8("StreamSplitter"));
54 *outSplitter = splitter;
55 }
56 return status;
57}
58
59StreamSplitter::StreamSplitter(const sp<IGraphicBufferConsumer>& inputQueue)
60 : mIsAbandoned(false), mMutex(), mReleaseCondition(),
61 mOutstandingBuffers(0), mInput(inputQueue), mOutputs(), mBuffers() {}
62
63StreamSplitter::~StreamSplitter() {
64 mInput->consumerDisconnect();
65 Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin();
66 for (; output != mOutputs.end(); ++output) {
67 (*output)->disconnect(NATIVE_WINDOW_API_CPU);
68 }
69
70 if (mBuffers.size() > 0) {
Mark Salyzyn8f515ce2014-06-09 14:32:04 -070071 ALOGE("%zu buffers still being tracked", mBuffers.size());
Dan Stoza99b18b42014-03-28 15:34:33 -070072 }
73}
74
75status_t StreamSplitter::addOutput(
76 const sp<IGraphicBufferProducer>& outputQueue) {
Yi Kong48a619f2018-06-05 16:34:59 -070077 if (outputQueue == nullptr) {
Dan Stoza99b18b42014-03-28 15:34:33 -070078 ALOGE("addOutput: outputQueue must not be NULL");
79 return BAD_VALUE;
80 }
81
82 Mutex::Autolock lock(mMutex);
83
84 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
Anton Ivanov81793802025-02-13 22:57:24 -080085 sp<OutputListener> listener =
86 sp<OutputListener>::make(sp<StreamSplitter>::fromExisting(this), outputQueue);
Marco Nelissen2ea926b2014-11-14 08:01:01 -080087 IInterface::asBinder(outputQueue)->linkToDeath(listener);
Dan Stoza99b18b42014-03-28 15:34:33 -070088 status_t status = outputQueue->connect(listener, NATIVE_WINDOW_API_CPU,
89 /* producerControlledByApp */ false, &queueBufferOutput);
90 if (status != NO_ERROR) {
91 ALOGE("addOutput: failed to connect (%d)", status);
92 return status;
93 }
94
95 mOutputs.push_back(outputQueue);
96
97 return NO_ERROR;
98}
99
100void StreamSplitter::setName(const String8 &name) {
101 Mutex::Autolock lock(mMutex);
102 mInput->setConsumerName(name);
103}
104
Dan Stoza8dc55392014-11-04 11:37:46 -0800105void StreamSplitter::onFrameAvailable(const BufferItem& /* item */) {
Dan Stoza99b18b42014-03-28 15:34:33 -0700106 ATRACE_CALL();
107 Mutex::Autolock lock(mMutex);
108
109 // The current policy is that if any one consumer is consuming buffers too
110 // slowly, the splitter will stall the rest of the outputs by not acquiring
111 // any more buffers from the input. This will cause back pressure on the
112 // input queue, slowing down its producer.
113
114 // If there are too many outstanding buffers, we block until a buffer is
115 // released back to the input in onBufferReleased
116 while (mOutstandingBuffers >= MAX_OUTSTANDING_BUFFERS) {
117 mReleaseCondition.wait(mMutex);
118
119 // If the splitter is abandoned while we are waiting, the release
120 // condition variable will be broadcast, and we should just return
121 // without attempting to do anything more (since the input queue will
122 // also be abandoned).
123 if (mIsAbandoned) {
124 return;
125 }
126 }
127 ++mOutstandingBuffers;
128
129 // Acquire and detach the buffer from the input
Dan Stozadd264162015-03-12 13:58:47 -0700130 BufferItem bufferItem;
Dan Stoza99b18b42014-03-28 15:34:33 -0700131 status_t status = mInput->acquireBuffer(&bufferItem, /* presentWhen */ 0);
132 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
133 "acquiring buffer from input failed (%d)", status);
134
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700135 ALOGV("acquired buffer %#" PRIx64 " from input",
Dan Stoza99b18b42014-03-28 15:34:33 -0700136 bufferItem.mGraphicBuffer->getId());
137
Pablo Ceballos47650f42015-08-04 16:38:17 -0700138 status = mInput->detachBuffer(bufferItem.mSlot);
Dan Stoza99b18b42014-03-28 15:34:33 -0700139 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
140 "detaching buffer from input failed (%d)", status);
141
142 // Initialize our reference count for this buffer
143 mBuffers.add(bufferItem.mGraphicBuffer->getId(),
Anton Ivanov81793802025-02-13 22:57:24 -0800144 sp<BufferTracker>::make(bufferItem.mGraphicBuffer));
Dan Stoza99b18b42014-03-28 15:34:33 -0700145
146 IGraphicBufferProducer::QueueBufferInput queueInput(
147 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800148 bufferItem.mDataSpace, bufferItem.mCrop,
149 static_cast<int32_t>(bufferItem.mScalingMode),
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700150 bufferItem.mTransform, bufferItem.mFence);
Dan Stoza99b18b42014-03-28 15:34:33 -0700151
152 // Attach and queue the buffer to each of the outputs
153 Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin();
154 for (; output != mOutputs.end(); ++output) {
155 int slot;
156 status = (*output)->attachBuffer(&slot, bufferItem.mGraphicBuffer);
157 if (status == NO_INIT) {
158 // If we just discovered that this output has been abandoned, note
159 // that, increment the release count so that we still release this
160 // buffer eventually, and move on to the next output
161 onAbandonedLocked();
162 mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())->
163 incrementReleaseCountLocked();
164 continue;
165 } else {
166 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
167 "attaching buffer to output failed (%d)", status);
168 }
169
170 IGraphicBufferProducer::QueueBufferOutput queueOutput;
171 status = (*output)->queueBuffer(slot, queueInput, &queueOutput);
172 if (status == NO_INIT) {
173 // If we just discovered that this output has been abandoned, note
174 // that, increment the release count so that we still release this
175 // buffer eventually, and move on to the next output
176 onAbandonedLocked();
177 mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())->
178 incrementReleaseCountLocked();
179 continue;
180 } else {
181 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
182 "queueing buffer to output failed (%d)", status);
183 }
184
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700185 ALOGV("queued buffer %#" PRIx64 " to output %p",
Dan Stoza99b18b42014-03-28 15:34:33 -0700186 bufferItem.mGraphicBuffer->getId(), output->get());
187 }
188}
189
190void StreamSplitter::onBufferReleasedByOutput(
191 const sp<IGraphicBufferProducer>& from) {
192 ATRACE_CALL();
193 Mutex::Autolock lock(mMutex);
194
195 sp<GraphicBuffer> buffer;
196 sp<Fence> fence;
197 status_t status = from->detachNextBuffer(&buffer, &fence);
198 if (status == NO_INIT) {
199 // If we just discovered that this output has been abandoned, note that,
200 // but we can't do anything else, since buffer is invalid
201 onAbandonedLocked();
202 return;
203 } else {
204 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
205 "detaching buffer from output failed (%d)", status);
206 }
207
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700208 ALOGV("detached buffer %#" PRIx64 " from output %p",
209 buffer->getId(), from.get());
Dan Stoza99b18b42014-03-28 15:34:33 -0700210
211 const sp<BufferTracker>& tracker = mBuffers.editValueFor(buffer->getId());
212
213 // Merge the release fence of the incoming buffer so that the fence we send
214 // back to the input includes all of the outputs' fences
215 tracker->mergeFence(fence);
216
217 // Check to see if this is the last outstanding reference to this buffer
218 size_t releaseCount = tracker->incrementReleaseCountLocked();
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700219 ALOGV("buffer %#" PRIx64 " reference count %zu (of %zu)", buffer->getId(),
Dan Stoza99b18b42014-03-28 15:34:33 -0700220 releaseCount, mOutputs.size());
221 if (releaseCount < mOutputs.size()) {
222 return;
223 }
224
225 // If we've been abandoned, we can't return the buffer to the input, so just
226 // stop tracking it and move on
227 if (mIsAbandoned) {
228 mBuffers.removeItem(buffer->getId());
229 return;
230 }
231
232 // Attach and release the buffer back to the input
233 int consumerSlot;
234 status = mInput->attachBuffer(&consumerSlot, tracker->getBuffer());
235 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
236 "attaching buffer to input failed (%d)", status);
237
Jim Shargo52108082024-11-15 16:53:57 +0000238 status = mInput->releaseBuffer(consumerSlot, /* frameNumber */ 0, tracker->getMergedFence());
Dan Stoza99b18b42014-03-28 15:34:33 -0700239 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
240 "releasing buffer to input failed (%d)", status);
241
Mark Salyzyn8f515ce2014-06-09 14:32:04 -0700242 ALOGV("released buffer %#" PRIx64 " to input", buffer->getId());
Dan Stoza99b18b42014-03-28 15:34:33 -0700243
244 // We no longer need to track the buffer once it has been returned to the
245 // input
246 mBuffers.removeItem(buffer->getId());
247
248 // Notify any waiting onFrameAvailable calls
249 --mOutstandingBuffers;
250 mReleaseCondition.signal();
251}
252
253void StreamSplitter::onAbandonedLocked() {
254 ALOGE("one of my outputs has abandoned me");
255 if (!mIsAbandoned) {
256 mInput->consumerDisconnect();
257 }
258 mIsAbandoned = true;
259 mReleaseCondition.broadcast();
260}
261
262StreamSplitter::OutputListener::OutputListener(
263 const sp<StreamSplitter>& splitter,
264 const sp<IGraphicBufferProducer>& output)
265 : mSplitter(splitter), mOutput(output) {}
266
267StreamSplitter::OutputListener::~OutputListener() {}
268
269void StreamSplitter::OutputListener::onBufferReleased() {
270 mSplitter->onBufferReleasedByOutput(mOutput);
271}
272
273void StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
274 Mutex::Autolock lock(mSplitter->mMutex);
275 mSplitter->onAbandonedLocked();
276}
277
278StreamSplitter::BufferTracker::BufferTracker(const sp<GraphicBuffer>& buffer)
279 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mReleaseCount(0) {}
280
281StreamSplitter::BufferTracker::~BufferTracker() {}
282
283void StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
284 mMergedFence = Fence::merge(String8("StreamSplitter"), mMergedFence, with);
285}
286
287} // namespace android