blob: deb6735d295efd562e4950610e091182bacb9541 [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#include <inttypes.h>
18
19#define LOG_TAG "Camera3StreamSplitter"
20#define ATRACE_TAG ATRACE_TAG_CAMERA
21//#define LOG_NDEBUG 0
22
23#include <gui/BufferItem.h>
24#include <gui/IGraphicBufferConsumer.h>
25#include <gui/IGraphicBufferProducer.h>
26#include <gui/BufferQueue.h>
27#include <gui/Surface.h>
28
29#include <ui/GraphicBuffer.h>
30
31#include <binder/ProcessState.h>
32
33#include <utils/Trace.h>
34
35#include "Camera3StreamSplitter.h"
36
37namespace android {
38
39status_t Camera3StreamSplitter::connect(const std::vector<sp<Surface> >& surfaces,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080040 uint32_t consumerUsage, size_t halMaxBuffers, sp<Surface>* consumer) {
41 ATRACE_CALL();
42 if (consumer == nullptr) {
43 SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070044 return BAD_VALUE;
45 }
46
47 Mutex::Autolock lock(mMutex);
48 status_t res = OK;
49
50 if (mOutputs.size() > 0 || mConsumer != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080051 SP_LOGE("%s: already connected", __FUNCTION__);
52 return BAD_VALUE;
53 }
54 if (mBuffers.size() > 0) {
55 SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size());
Shuzhen Wang0129d522016-10-30 22:43:41 -070056 return BAD_VALUE;
57 }
58
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080059 mMaxHalBuffers = halMaxBuffers;
60 mConsumerName = getUniqueConsumerName();
Shuzhen Wang0129d522016-10-30 22:43:41 -070061 // Add output surfaces. This has to be before creating internal buffer queue
62 // in order to get max consumer side buffers.
63 for (size_t i = 0; i < surfaces.size(); i++) {
Shuzhen Wang758c2152017-01-10 18:26:18 -080064 if (surfaces[i] == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080065 SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
Shuzhen Wang758c2152017-01-10 18:26:18 -080066 return BAD_VALUE;
67 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080068 res = addOutputLocked(surfaces[i]);
Shuzhen Wang758c2152017-01-10 18:26:18 -080069 if (res != OK) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080070 SP_LOGE("%s: Failed to add output surface: %s(%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -080071 __FUNCTION__, strerror(-res), res);
72 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -070073 }
74 }
75
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080076 // Create BufferQueue for input
Shuzhen Wang0129d522016-10-30 22:43:41 -070077 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
78
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080079 // Allocate 1 extra buffer to handle the case where all buffers are detached
80 // from input, and attached to the outputs. In this case, the input queue's
81 // dequeueBuffer can still allocate 1 extra buffer before being blocked by
82 // the output's attachBuffer().
Shuzhen Wang0129d522016-10-30 22:43:41 -070083 mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080084 mMaxConsumerBuffers+1);
Shuzhen Wang0129d522016-10-30 22:43:41 -070085 if (mBufferItemConsumer == nullptr) {
86 return NO_MEMORY;
87 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080088 mConsumer->setConsumerName(mConsumerName);
Shuzhen Wang0129d522016-10-30 22:43:41 -070089
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080090 *consumer = new Surface(mProducer);
91 if (*consumer == nullptr) {
Shuzhen Wang0129d522016-10-30 22:43:41 -070092 return NO_MEMORY;
93 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070094
95 res = mConsumer->consumerConnect(this, /* controlledByApp */ false);
96
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080097 SP_LOGV("%s: connected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070098 return res;
99}
100
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800101status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
102 ATRACE_CALL();
103 return mOnFrameAvailableRes.load();
104}
105
Shuzhen Wang0129d522016-10-30 22:43:41 -0700106void Camera3StreamSplitter::disconnect() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800107 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700108 Mutex::Autolock lock(mMutex);
109
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800110 for (auto& notifier : mNotifiers) {
111 sp<IGraphicBufferProducer> producer = notifier.first;
112 sp<OutputListener> listener = notifier.second;
113 IInterface::asBinder(producer)->unlinkToDeath(listener);
114 }
115 mNotifiers.clear();
116
Shuzhen Wang0129d522016-10-30 22:43:41 -0700117 for (auto& output : mOutputs) {
118 output->disconnect(NATIVE_WINDOW_API_CAMERA);
119 }
120 mOutputs.clear();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800121 mOutputSlots.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700122
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800123 mConsumer->consumerDisconnect();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700124
125 if (mBuffers.size() > 0) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800126 SP_LOGW("%zu buffers still being tracked", mBuffers.size());
127 mBuffers.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800129
130 mMaxHalBuffers = 0;
131 mMaxConsumerBuffers = 0;
132 SP_LOGV("%s: Disconnected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700133}
134
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800135
Shuzhen Wang0129d522016-10-30 22:43:41 -0700136Camera3StreamSplitter::~Camera3StreamSplitter() {
137 disconnect();
138}
139
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800140status_t Camera3StreamSplitter::addOutput(const sp<Surface>& outputQueue) {
141 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700142 Mutex::Autolock lock(mMutex);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800143 status_t res = addOutputLocked(outputQueue);
144
145 if (res != OK) {
146 SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
147 return res;
148 }
149
150 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers+1);
151
152 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700153}
154
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800155status_t Camera3StreamSplitter::addOutputLocked(const sp<Surface>& outputQueue) {
156 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700157 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800158 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700159 return BAD_VALUE;
160 }
161
162 sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
163 // Connect to the buffer producer
Shuzhen Wang0129d522016-10-30 22:43:41 -0700164 sp<OutputListener> listener(new OutputListener(this, gbp));
165 IInterface::asBinder(gbp)->linkToDeath(listener);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800166 status_t res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
167 if (res != NO_ERROR) {
168 SP_LOGE("addOutput: failed to connect (%d)", res);
169 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700170 }
171
172 // Query consumer side buffer count, and update overall buffer count
173 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800174 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700175 outputQueue.get(),
176 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800177 if (res != OK) {
178 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700179 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800180 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700181 }
182
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800183 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
184 maxConsumerBuffers, mMaxHalBuffers);
185 size_t totalBufferCount = maxConsumerBuffers + mMaxHalBuffers;
186 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700187 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800188 if (res != OK) {
189 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700190 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800191 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700192 }
193
194 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
195 // We need skip these cases as timeout will disable the non-blocking (async) mode.
196 int32_t usage = 0;
197 static_cast<ANativeWindow*>(outputQueue.get())->query(
198 outputQueue.get(),
199 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &usage);
200 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
201 outputQueue->setDequeueTimeout(kDequeueBufferTimeout);
202 }
203
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800204 res = gbp->allowAllocation(false);
205 if (res != OK) {
206 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
207 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700208 }
209
210 // Add new entry into mOutputs
211 mOutputs.push_back(gbp);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800212 mNotifiers[gbp] = listener;
213 mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
214
215 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700216 return NO_ERROR;
217}
218
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800219status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
220 const BufferItem& bufferItem) {
221 ATRACE_CALL();
222 status_t res;
223 IGraphicBufferProducer::QueueBufferInput queueInput(
224 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
225 bufferItem.mDataSpace, bufferItem.mCrop,
226 static_cast<int32_t>(bufferItem.mScalingMode),
227 bufferItem.mTransform, bufferItem.mFence);
228
229 IGraphicBufferProducer::QueueBufferOutput queueOutput;
230
231 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
232 const BufferTracker& tracker = *(mBuffers[bufferId]);
233 int slot = getSlotForOutputLocked(output, tracker.getBuffer());
234
235 // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
236 // queueBuffer (which will try to acquire the output lock), the output could be holding its
237 // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into
238 // circular lock situation.
239 mMutex.unlock();
240 res = output->queueBuffer(slot, queueInput, &queueOutput);
241 mMutex.lock();
242
243 SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d",
244 __FUNCTION__, output.get(), slot, res);
245 if (res != OK) {
246 if (res != NO_INIT && res != DEAD_OBJECT) {
247 SP_LOGE("Queuing buffer to output failed (%d)", res);
248 }
249 // If we just discovered that this output has been abandoned, note
250 // that, increment the release count so that we still release this
251 // buffer eventually, and move on to the next output
252 onAbandonedLocked();
253 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), output);
254 return res;
255 }
256
257 // If the queued buffer replaces a pending buffer in the async
258 // queue, no onBufferReleased is called by the buffer queue.
259 // Proactively trigger the callback to avoid buffer loss.
260 if (queueOutput.bufferReplaced) {
261 onBufferReleasedByOutputLocked(output);
262 }
263
264 return res;
265}
266
Shuzhen Wang0129d522016-10-30 22:43:41 -0700267String8 Camera3StreamSplitter::getUniqueConsumerName() {
268 static volatile int32_t counter = 0;
269 return String8::format("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
270}
271
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800272status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700273 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800274 status_t res = OK;
275
Shuzhen Wang0129d522016-10-30 22:43:41 -0700276 Mutex::Autolock lock(mMutex);
277
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800278 uint64_t bufferId = buffer->getId();
279 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
280 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700281
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800282 for (const auto surface : tracker_ptr->requestedSurfaces()) {
283 sp<IGraphicBufferProducer>& gbp = mOutputs[surface];
284 OutputSlots& outputSlots = *(mOutputSlots[gbp]);
285 int slot = getSlotForOutputLocked(gbp, buffer);
286 if (slot != BufferItem::INVALID_BUFFER_SLOT) {
287 gbp->detachBuffer(slot);
288 outputSlots[slot].clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700289 }
290 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800291
292 return res;
293}
294
295status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
296 const std::vector<size_t>& surface_ids) {
297 ATRACE_CALL();
298 status_t res = OK;
299
300 Mutex::Autolock lock(mMutex);
301
302 sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
303 uint64_t bufferId = gb->getId();
304
305 // Initialize buffer tracker for this input buffer
306 auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
307
308 for (auto& surface_id : surface_ids) {
309 sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id];
310 int slot = BufferItem::INVALID_BUFFER_SLOT;
311 //Temporarly Unlock the mutex when trying to attachBuffer to the output
312 //queue, because attachBuffer could block in case of a slow consumer. If
313 //we block while holding the lock, onFrameAvailable and onBufferReleased
314 //will block as well because they need to acquire the same lock.
315 mMutex.unlock();
316 res = gbp->attachBuffer(&slot, gb);
317 mMutex.lock();
318 if (res != OK) {
319 SP_LOGE("%s: Cannot acquireBuffer from GraphicBufferProducer %p: %s (%d)",
320 __FUNCTION__, gbp.get(), strerror(-res), res);
321 return res;
322 }
323 auto& outputSlots = *mOutputSlots[gbp];
324 if (outputSlots[slot] != nullptr) {
325 // If the buffer is attached to a slot which already contains a buffer,
326 // the previous buffer will be removed from the output queue. Decrement
327 // the reference count accordingly.
328 decrementBufRefCountLocked(outputSlots[slot]->getId(), gbp);
329 }
330 SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
331 slot, gbp.get());
332 outputSlots[slot] = gb;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700333 }
334
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800335 mBuffers[bufferId] = std::move(tracker);
336
337 return res;
338}
339
340void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
341 ATRACE_CALL();
342 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700343
344 // Acquire and detach the buffer from the input
345 BufferItem bufferItem;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800346 status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
347 if (res != NO_ERROR) {
348 SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
349 mOnFrameAvailableRes.store(res);
350 return;
351 }
352 if (mBuffers.find(bufferItem.mGraphicBuffer->getId()) == mBuffers.end()) {
353 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
354 __FUNCTION__);
355 mOnFrameAvailableRes.store(INVALID_OPERATION);
356 return;
357 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700358
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800359 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
360 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700361
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800362 res = mConsumer->detachBuffer(bufferItem.mSlot);
363 if (res != NO_ERROR) {
364 SP_LOGE("%s: detaching buffer from input failed (%d)", __FUNCTION__, res);
365 mOnFrameAvailableRes.store(res);
366 return;
367 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700368
369 // Attach and queue the buffer to each of the outputs
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800370 BufferTracker& tracker = *(mBuffers[bufferItem.mGraphicBuffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700371
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800372 SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
373 __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
374 for (const auto id : tracker.requestedSurfaces()) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700375
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800376 LOG_ALWAYS_FATAL_IF(id >= mOutputs.size(),
377 "requested surface id exceeding max registered ids");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700378
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800379 res = outputBufferLocked(mOutputs[id], bufferItem);
380 if (res != OK) {
381 SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
382 mOnFrameAvailableRes.store(res);
383 // If we fail to send buffer to certain output, keep sending to
384 // other outputs.
385 continue;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700386 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800387 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700388
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800389 mOnFrameAvailableRes.store(res);
390}
391
392void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id,
393 const sp<IGraphicBufferProducer>& from) {
394 ATRACE_CALL();
395 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked();
396
397 removeSlotForOutputLocked(from, mBuffers[id]->getBuffer());
398 if (referenceCount > 0) {
399 return;
400 }
401
402 // We no longer need to track the buffer now that it is being returned to the
403 // input. Note that this should happen before we unlock the mutex and call
404 // releaseBuffer, to avoid the case where the same bufferId is acquired in
405 // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
406 // overwrites the current one.
407 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
408 mBuffers.erase(id);
409
410 // Attach and release the buffer back to the input
411 int consumerSlot = BufferItem::INVALID_BUFFER_SLOT;
412 status_t res = mConsumer->attachBuffer(&consumerSlot, tracker_ptr->getBuffer());
413 if (res != NO_ERROR) {
414 SP_LOGE("%s: attaching buffer to input failed (%d)", __FUNCTION__, res);
415 return;
416 }
417
418 // Temporarily unlock mutex to avoid circular lock:
419 // 1. This function holds splitter lock, calls releaseBuffer which triggers
420 // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
421 // OutputStream lock
422 // 2. Camera3SharedOutputStream::getBufferLocked calls
423 // attachBufferToOutputs, which holds the stream lock, and waits for the
424 // splitter lock.
425 sp<IGraphicBufferConsumer> consumer(mConsumer);
426 mMutex.unlock();
427 if (consumer != nullptr) {
428 res = consumer->releaseBuffer(consumerSlot, /* frameNumber */ 0,
429 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence());
430 } else {
431 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
432 }
433 mMutex.lock();
434 // If the producer of this queue is disconnected, -22 error will occur
435 if (res != NO_ERROR) {
436 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700437 }
438}
439
440void Camera3StreamSplitter::onBufferReleasedByOutput(
441 const sp<IGraphicBufferProducer>& from) {
442 ATRACE_CALL();
443 Mutex::Autolock lock(mMutex);
444
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800445 onBufferReleasedByOutputLocked(from);
446}
447
448void Camera3StreamSplitter::onBufferReleasedByOutputLocked(
449 const sp<IGraphicBufferProducer>& from) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800450 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700451 sp<GraphicBuffer> buffer;
452 sp<Fence> fence;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800453 status_t res = from->detachNextBuffer(&buffer, &fence);
454 if (res == NO_INIT) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700455 // If we just discovered that this output has been abandoned, note that,
456 // but we can't do anything else, since buffer is invalid
457 onAbandonedLocked();
458 return;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800459 } else if (res == NO_MEMORY) {
460 SP_LOGV("%s: No free buffers", __FUNCTION__);
461 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700462 } else {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800463 LOG_ALWAYS_FATAL_IF(res != NO_ERROR,
464 "detaching buffer from output failed (%d)", res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700465 }
466
Shuzhen Wang0129d522016-10-30 22:43:41 -0700467 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700468 // Merge the release fence of the incoming buffer so that the fence we send
469 // back to the input includes all of the outputs' fences
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800470 if (fence != nullptr && fence->isValid()) {
471 tracker.mergeFence(fence);
472 }
473 SP_LOGV("detached buffer %" PRId64 " %p from output %p",
474 buffer->getId(), buffer.get(), from.get());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700475
476 // Check to see if this is the last outstanding reference to this buffer
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800477 decrementBufRefCountLocked(buffer->getId(), from);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700478}
479
480void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800481 // If this is called from binderDied callback, it means the app process
482 // holding the binder has died. CameraService will be notified of the binder
483 // death, and camera device will be closed, which in turn calls
484 // disconnect().
485 //
486 // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
487 // consumer being abanoned shouldn't impact the other consumer. So we won't
488 // stop the buffer flow.
489 //
490 // In both cases, we don't need to do anything here.
491 SP_LOGV("One of my outputs has abandoned me");
492}
493
494int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
495 const sp<GraphicBuffer>& gb) {
496 auto& outputSlots = *mOutputSlots[gbp];
497
498 for (size_t i = 0; i < outputSlots.size(); i++) {
499 if (outputSlots[i] == gb) {
500 return (int)i;
501 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700502 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800503
504 SP_LOGE("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
505 gbp.get());
506 return BufferItem::INVALID_BUFFER_SLOT;
507}
508
509status_t Camera3StreamSplitter::removeSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
510 const sp<GraphicBuffer>& gb) {
511 auto& outputSlots = *mOutputSlots[gbp];
512
513 for (size_t i = 0; i < outputSlots.size(); i++) {
514 if (outputSlots[i] == gb) {
515 outputSlots[i].clear();
516 return NO_ERROR;
517 }
518 }
519
520 SP_LOGE("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
521 gbp.get());
522 return BAD_VALUE;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700523}
524
525Camera3StreamSplitter::OutputListener::OutputListener(
526 wp<Camera3StreamSplitter> splitter,
527 wp<IGraphicBufferProducer> output)
528 : mSplitter(splitter), mOutput(output) {}
529
530void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800531 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700532 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
533 sp<IGraphicBufferProducer> output = mOutput.promote();
534 if (splitter != nullptr && output != nullptr) {
535 splitter->onBufferReleasedByOutput(output);
536 }
537}
538
539void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
540 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
541 if (splitter != nullptr) {
542 Mutex::Autolock lock(splitter->mMutex);
543 splitter->onAbandonedLocked();
544 }
545}
546
547Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800548 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
549 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
550 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700551
552void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
553 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
554}
555
556size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked() {
557 if (mReferenceCount > 0)
558 --mReferenceCount;
559 return mReferenceCount;
560}
561
562} // namespace android