1 /*
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 #include <camera/StringUtils.h>
29 
30 #include <ui/GraphicBuffer.h>
31 
32 #include <binder/ProcessState.h>
33 
34 #include <utils/Trace.h>
35 
36 #include <cutils/atomic.h>
37 
38 #include "Camera3Stream.h"
39 
40 #include "Camera3StreamSplitter.h"
41 
42 namespace android {
43 
connect(const std::unordered_map<size_t,sp<Surface>> & surfaces,uint64_t consumerUsage,uint64_t producerUsage,size_t halMaxBuffers,uint32_t width,uint32_t height,android::PixelFormat format,sp<Surface> * consumer,int64_t dynamicRangeProfile)44 status_t Camera3StreamSplitter::connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
45         uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
46         uint32_t height, android::PixelFormat format, sp<Surface>* consumer,
47         int64_t dynamicRangeProfile) {
48     ATRACE_CALL();
49     if (consumer == nullptr) {
50         SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
51         return BAD_VALUE;
52     }
53 
54     Mutex::Autolock lock(mMutex);
55     status_t res = OK;
56 
57     if (mOutputs.size() > 0 || mConsumer != nullptr) {
58         SP_LOGE("%s: already connected", __FUNCTION__);
59         return BAD_VALUE;
60     }
61     if (mBuffers.size() > 0) {
62         SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size());
63         return BAD_VALUE;
64     }
65 
66     mMaxHalBuffers = halMaxBuffers;
67     mConsumerName = getUniqueConsumerName();
68     mDynamicRangeProfile = dynamicRangeProfile;
69     // Add output surfaces. This has to be before creating internal buffer queue
70     // in order to get max consumer side buffers.
71     for (auto &it : surfaces) {
72         if (it.second == nullptr) {
73             SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
74             return BAD_VALUE;
75         }
76         res = addOutputLocked(it.first, it.second);
77         if (res != OK) {
78             SP_LOGE("%s: Failed to add output surface: %s(%d)",
79                     __FUNCTION__, strerror(-res), res);
80             return res;
81         }
82     }
83 
84     // Create BufferQueue for input
85     BufferQueue::createBufferQueue(&mProducer, &mConsumer);
86 
87     // Allocate 1 extra buffer to handle the case where all buffers are detached
88     // from input, and attached to the outputs. In this case, the input queue's
89     // dequeueBuffer can still allocate 1 extra buffer before being blocked by
90     // the output's attachBuffer().
91     mMaxConsumerBuffers++;
92     mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage, mMaxConsumerBuffers);
93     if (mBufferItemConsumer == nullptr) {
94         return NO_MEMORY;
95     }
96     mConsumer->setConsumerName(toString8(mConsumerName));
97 
98     *consumer = new Surface(mProducer);
99     if (*consumer == nullptr) {
100         return NO_MEMORY;
101     }
102 
103     res = mProducer->setAsyncMode(true);
104     if (res != OK) {
105         SP_LOGE("%s: Failed to enable input queue async mode: %s(%d)", __FUNCTION__,
106                 strerror(-res), res);
107         return res;
108     }
109 
110     res = mConsumer->consumerConnect(this, /* controlledByApp */ false);
111 
112     mWidth = width;
113     mHeight = height;
114     mFormat = format;
115     mProducerUsage = producerUsage;
116     mAcquiredInputBuffers = 0;
117 
118     SP_LOGV("%s: connected", __FUNCTION__);
119     return res;
120 }
121 
getOnFrameAvailableResult()122 status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
123     ATRACE_CALL();
124     return mOnFrameAvailableRes.load();
125 }
126 
disconnect()127 void Camera3StreamSplitter::disconnect() {
128     ATRACE_CALL();
129     Mutex::Autolock lock(mMutex);
130 
131     for (auto& notifier : mNotifiers) {
132         sp<IGraphicBufferProducer> producer = notifier.first;
133         sp<OutputListener> listener = notifier.second;
134         IInterface::asBinder(producer)->unlinkToDeath(listener);
135     }
136     mNotifiers.clear();
137 
138     for (auto& output : mOutputs) {
139         if (output.second != nullptr) {
140             output.second->disconnect(NATIVE_WINDOW_API_CAMERA);
141         }
142     }
143     mOutputs.clear();
144     mOutputSurfaces.clear();
145     mOutputSlots.clear();
146     mConsumerBufferCount.clear();
147 
148     if (mConsumer.get() != nullptr) {
149         mConsumer->consumerDisconnect();
150     }
151 
152     if (mBuffers.size() > 0) {
153         SP_LOGW("%zu buffers still being tracked", mBuffers.size());
154         mBuffers.clear();
155     }
156 
157     mMaxHalBuffers = 0;
158     mMaxConsumerBuffers = 0;
159     mAcquiredInputBuffers = 0;
160     SP_LOGV("%s: Disconnected", __FUNCTION__);
161 }
162 
Camera3StreamSplitter(bool useHalBufManager)163 Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) :
164         mUseHalBufManager(useHalBufManager) {}
165 
~Camera3StreamSplitter()166 Camera3StreamSplitter::~Camera3StreamSplitter() {
167     disconnect();
168 }
169 
addOutput(size_t surfaceId,const sp<Surface> & outputQueue)170 status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) {
171     ATRACE_CALL();
172     Mutex::Autolock lock(mMutex);
173     status_t res = addOutputLocked(surfaceId, outputQueue);
174 
175     if (res != OK) {
176         SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
177         return res;
178     }
179 
180     if (mMaxConsumerBuffers > mAcquiredInputBuffers) {
181         res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
182     }
183 
184     return res;
185 }
186 
setHalBufferManager(bool enabled)187 void Camera3StreamSplitter::setHalBufferManager(bool enabled) {
188     Mutex::Autolock lock(mMutex);
189     mUseHalBufManager = enabled;
190 }
191 
addOutputLocked(size_t surfaceId,const sp<Surface> & outputQueue)192 status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
193     ATRACE_CALL();
194     if (outputQueue == nullptr) {
195         SP_LOGE("addOutput: outputQueue must not be NULL");
196         return BAD_VALUE;
197     }
198 
199     if (mOutputs[surfaceId] != nullptr) {
200         SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
201         return BAD_VALUE;
202     }
203 
204     status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
205             mWidth, mHeight);
206     if (res != NO_ERROR) {
207         SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
208         return res;
209     }
210     res = native_window_set_buffers_format(outputQueue.get(),
211             mFormat);
212     if (res != OK) {
213         ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu",
214                 __FUNCTION__, mFormat, surfaceId);
215         return res;
216     }
217 
218     sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
219     // Connect to the buffer producer
220     sp<OutputListener> listener(new OutputListener(this, gbp));
221     IInterface::asBinder(gbp)->linkToDeath(listener);
222     res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
223     if (res != NO_ERROR) {
224         SP_LOGE("addOutput: failed to connect (%d)", res);
225         return res;
226     }
227 
228     // Query consumer side buffer count, and update overall buffer count
229     int maxConsumerBuffers = 0;
230     res = static_cast<ANativeWindow*>(outputQueue.get())->query(
231             outputQueue.get(),
232             NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
233     if (res != OK) {
234         SP_LOGE("%s: Unable to query consumer undequeued buffer count"
235               " for surface", __FUNCTION__);
236         return res;
237     }
238 
239     SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
240             maxConsumerBuffers, mMaxHalBuffers);
241     // The output slot count requirement can change depending on the current amount
242     // of outputs and incoming buffer consumption rate. To avoid any issues with
243     // insufficient slots, set their count to the maximum supported. The output
244     // surface buffer allocation is disabled so no real buffers will get allocated.
245     size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS;
246     res = native_window_set_buffer_count(outputQueue.get(),
247             totalBufferCount);
248     if (res != OK) {
249         SP_LOGE("%s: Unable to set buffer count for surface %p",
250                 __FUNCTION__, outputQueue.get());
251         return res;
252     }
253 
254     // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
255     // We need skip these cases as timeout will disable the non-blocking (async) mode.
256     uint64_t usage = 0;
257     res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
258     if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
259         nsecs_t timeout = mUseHalBufManager ?
260                 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
261         outputQueue->setDequeueTimeout(timeout);
262     }
263 
264     res = gbp->allowAllocation(false);
265     if (res != OK) {
266         SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
267         return res;
268     }
269 
270     // Add new entry into mOutputs
271     mOutputs[surfaceId] = gbp;
272     mOutputSurfaces[surfaceId] = outputQueue;
273     mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
274     if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
275         SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
276                 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
277     }
278     mNotifiers[gbp] = listener;
279     mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
280 
281     mMaxConsumerBuffers += maxConsumerBuffers;
282     return NO_ERROR;
283 }
284 
removeOutput(size_t surfaceId)285 status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) {
286     ATRACE_CALL();
287     Mutex::Autolock lock(mMutex);
288 
289     status_t res = removeOutputLocked(surfaceId);
290     if (res != OK) {
291         SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res);
292         return res;
293     }
294 
295     if (mAcquiredInputBuffers < mMaxConsumerBuffers) {
296         res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
297         if (res != OK) {
298             SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res);
299             return res;
300         }
301     }
302 
303     return res;
304 }
305 
removeOutputLocked(size_t surfaceId)306 status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) {
307     if (mOutputs[surfaceId] == nullptr) {
308         SP_LOGE("%s: output surface is not present!", __FUNCTION__);
309         return BAD_VALUE;
310     }
311 
312     sp<IGraphicBufferProducer> gbp = mOutputs[surfaceId];
313     //Search and decrement the ref. count of any buffers that are
314     //still attached to the removed surface.
315     std::vector<uint64_t> pendingBufferIds;
316     auto& outputSlots = *mOutputSlots[gbp];
317     for (size_t i = 0; i < outputSlots.size(); i++) {
318         if (outputSlots[i] != nullptr) {
319             pendingBufferIds.push_back(outputSlots[i]->getId());
320             auto rc = gbp->detachBuffer(i);
321             if (rc != NO_ERROR) {
322                 //Buffers that fail to detach here will be scheduled for detach in the
323                 //input buffer queue and the rest of the registered outputs instead.
324                 //This will help ensure that camera stops accessing buffers that still
325                 //can get referenced by the disconnected output.
326                 mDetachedBuffers.emplace(outputSlots[i]->getId());
327             }
328         }
329     }
330     mOutputs[surfaceId] = nullptr;
331     mOutputSurfaces[surfaceId] = nullptr;
332     mOutputSlots[gbp] = nullptr;
333     for (const auto &id : pendingBufferIds) {
334         decrementBufRefCountLocked(id, surfaceId);
335     }
336 
337     auto res = IInterface::asBinder(gbp)->unlinkToDeath(mNotifiers[gbp]);
338     if (res != OK) {
339         SP_LOGE("%s: Failed to unlink producer death listener: %d ", __FUNCTION__, res);
340         return res;
341     }
342 
343     res = gbp->disconnect(NATIVE_WINDOW_API_CAMERA);
344     if (res != OK) {
345         SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res);
346         return res;
347     }
348 
349     mNotifiers[gbp] = nullptr;
350     mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
351     mConsumerBufferCount[surfaceId] = 0;
352 
353     return res;
354 }
355 
outputBufferLocked(const sp<IGraphicBufferProducer> & output,const BufferItem & bufferItem,size_t surfaceId)356 status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
357         const BufferItem& bufferItem, size_t surfaceId) {
358     ATRACE_CALL();
359     status_t res;
360     IGraphicBufferProducer::QueueBufferInput queueInput(
361             bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
362             bufferItem.mDataSpace, bufferItem.mCrop,
363             static_cast<int32_t>(bufferItem.mScalingMode),
364             bufferItem.mTransform, bufferItem.mFence);
365 
366     IGraphicBufferProducer::QueueBufferOutput queueOutput;
367 
368     uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
369     const BufferTracker& tracker = *(mBuffers[bufferId]);
370     int slot = getSlotForOutputLocked(output, tracker.getBuffer());
371 
372     if (mOutputSurfaces[surfaceId] != nullptr) {
373         sp<ANativeWindow> anw = mOutputSurfaces[surfaceId];
374         camera3::Camera3Stream::queueHDRMetadata(
375                 bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile);
376     } else {
377         SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId);
378     }
379 
380     // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
381     // queueBuffer (which will try to acquire the output lock), the output could be holding its
382     // own lock calling releaseBuffer (which  will try to acquire the splitter lock), running into
383     // circular lock situation.
384     mMutex.unlock();
385     res = output->queueBuffer(slot, queueInput, &queueOutput);
386     mMutex.lock();
387 
388     SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d",
389             __FUNCTION__, output.get(), slot, res);
390     //During buffer queue 'mMutex' is not held which makes the removal of
391     //"output" possible. Check whether this is the case and return.
392     if (mOutputSlots[output] == nullptr) {
393         return res;
394     }
395     if (res != OK) {
396         if (res != NO_INIT && res != DEAD_OBJECT) {
397             SP_LOGE("Queuing buffer to output failed (%d)", res);
398         }
399         // If we just discovered that this output has been abandoned, note
400         // that, increment the release count so that we still release this
401         // buffer eventually, and move on to the next output
402         onAbandonedLocked();
403         decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
404         return res;
405     }
406 
407     // If the queued buffer replaces a pending buffer in the async
408     // queue, no onBufferReleased is called by the buffer queue.
409     // Proactively trigger the callback to avoid buffer loss.
410     if (queueOutput.bufferReplaced) {
411         onBufferReplacedLocked(output, surfaceId);
412     }
413 
414     return res;
415 }
416 
getUniqueConsumerName()417 std::string Camera3StreamSplitter::getUniqueConsumerName() {
418     static volatile int32_t counter = 0;
419     return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
420 }
421 
notifyBufferReleased(const sp<GraphicBuffer> & buffer)422 status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
423     ATRACE_CALL();
424 
425     Mutex::Autolock lock(mMutex);
426 
427     uint64_t bufferId = buffer->getId();
428     std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
429     mBuffers.erase(bufferId);
430 
431     return OK;
432 }
433 
attachBufferToOutputs(ANativeWindowBuffer * anb,const std::vector<size_t> & surface_ids)434 status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
435         const std::vector<size_t>& surface_ids) {
436     ATRACE_CALL();
437     status_t res = OK;
438 
439     Mutex::Autolock lock(mMutex);
440 
441     sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
442     uint64_t bufferId = gb->getId();
443 
444     // Initialize buffer tracker for this input buffer
445     auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
446 
447     for (auto& surface_id : surface_ids) {
448         sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id];
449         if (gbp.get() == nullptr) {
450             //Output surface got likely removed by client.
451             continue;
452         }
453         int slot = getSlotForOutputLocked(gbp, gb);
454         if (slot != BufferItem::INVALID_BUFFER_SLOT) {
455             //Buffer is already attached to this output surface.
456             continue;
457         }
458         //Temporarly Unlock the mutex when trying to attachBuffer to the output
459         //queue, because attachBuffer could block in case of a slow consumer. If
460         //we block while holding the lock, onFrameAvailable and onBufferReleased
461         //will block as well because they need to acquire the same lock.
462         mMutex.unlock();
463         res = gbp->attachBuffer(&slot, gb);
464         mMutex.lock();
465         if (res != OK) {
466             SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)",
467                     __FUNCTION__, gbp.get(), strerror(-res), res);
468             // TODO: might need to detach/cleanup the already attached buffers before return?
469             return res;
470         }
471         if ((slot < 0) || (slot > BufferQueue::NUM_BUFFER_SLOTS)) {
472             SP_LOGE("%s: Slot received %d either bigger than expected maximum %d or negative!",
473                     __FUNCTION__, slot, BufferQueue::NUM_BUFFER_SLOTS);
474             return BAD_VALUE;
475         }
476         //During buffer attach 'mMutex' is not held which makes the removal of
477         //"gbp" possible. Check whether this is the case and continue.
478         if (mOutputSlots[gbp] == nullptr) {
479             continue;
480         }
481         auto& outputSlots = *mOutputSlots[gbp];
482         if (static_cast<size_t> (slot + 1) > outputSlots.size()) {
483             outputSlots.resize(slot + 1);
484         }
485         if (outputSlots[slot] != nullptr) {
486             // If the buffer is attached to a slot which already contains a buffer,
487             // the previous buffer will be removed from the output queue. Decrement
488             // the reference count accordingly.
489             decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id);
490         }
491         SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
492                 slot, gbp.get());
493         outputSlots[slot] = gb;
494     }
495 
496     mBuffers[bufferId] = std::move(tracker);
497 
498     return res;
499 }
500 
onFrameAvailable(const BufferItem &)501 void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
502     ATRACE_CALL();
503     Mutex::Autolock lock(mMutex);
504 
505     // Acquire and detach the buffer from the input
506     BufferItem bufferItem;
507     status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
508     if (res != NO_ERROR) {
509         SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
510         mOnFrameAvailableRes.store(res);
511         return;
512     }
513 
514     uint64_t bufferId;
515     if (bufferItem.mGraphicBuffer != nullptr) {
516         mInputSlots[bufferItem.mSlot] = bufferItem;
517     } else if (bufferItem.mAcquireCalled) {
518         bufferItem.mGraphicBuffer = mInputSlots[bufferItem.mSlot].mGraphicBuffer;
519         mInputSlots[bufferItem.mSlot].mFrameNumber = bufferItem.mFrameNumber;
520     } else {
521         SP_LOGE("%s: Invalid input graphic buffer!", __FUNCTION__);
522         mOnFrameAvailableRes.store(BAD_VALUE);
523         return;
524     }
525     bufferId = bufferItem.mGraphicBuffer->getId();
526 
527     if (mBuffers.find(bufferId) == mBuffers.end()) {
528         SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
529                 __FUNCTION__);
530         mOnFrameAvailableRes.store(INVALID_OPERATION);
531         return;
532     }
533 
534     mAcquiredInputBuffers++;
535     SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
536             bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
537 
538     if (bufferItem.mTransformToDisplayInverse) {
539         bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
540     }
541 
542     // Attach and queue the buffer to each of the outputs
543     BufferTracker& tracker = *(mBuffers[bufferId]);
544 
545     SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
546            __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
547     for (const auto id : tracker.requestedSurfaces()) {
548 
549         if (mOutputs[id] == nullptr) {
550             //Output surface got likely removed by client.
551             continue;
552         }
553 
554         res = outputBufferLocked(mOutputs[id], bufferItem, id);
555         if (res != OK) {
556             SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
557             mOnFrameAvailableRes.store(res);
558             // If we fail to send buffer to certain output, keep sending to
559             // other outputs.
560             continue;
561         }
562     }
563 
564     mOnFrameAvailableRes.store(res);
565 }
566 
onFrameReplaced(const BufferItem & item)567 void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
568     ATRACE_CALL();
569     onFrameAvailable(item);
570 }
571 
decrementBufRefCountLocked(uint64_t id,size_t surfaceId)572 void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
573     ATRACE_CALL();
574 
575     if (mBuffers[id] == nullptr) {
576         return;
577     }
578 
579     size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
580     if (referenceCount > 0) {
581         return;
582     }
583 
584     // We no longer need to track the buffer now that it is being returned to the
585     // input. Note that this should happen before we unlock the mutex and call
586     // releaseBuffer, to avoid the case where the same bufferId is acquired in
587     // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
588     // overwrites the current one.
589     std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
590     mBuffers.erase(id);
591 
592     uint64_t bufferId = tracker_ptr->getBuffer()->getId();
593     int consumerSlot = -1;
594     uint64_t frameNumber;
595     auto inputSlot = mInputSlots.begin();
596     for (; inputSlot != mInputSlots.end(); inputSlot++) {
597         if (inputSlot->second.mGraphicBuffer->getId() == bufferId) {
598             consumerSlot = inputSlot->second.mSlot;
599             frameNumber = inputSlot->second.mFrameNumber;
600             break;
601         }
602     }
603     if (consumerSlot == -1) {
604         SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__);
605         return;
606     }
607 
608     auto detachBuffer = mDetachedBuffers.find(bufferId);
609     bool detach = (detachBuffer != mDetachedBuffers.end());
610     if (detach) {
611         mDetachedBuffers.erase(detachBuffer);
612         mInputSlots.erase(inputSlot);
613     }
614     // Temporarily unlock mutex to avoid circular lock:
615     // 1. This function holds splitter lock, calls releaseBuffer which triggers
616     // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
617     // OutputStream lock
618     // 2. Camera3SharedOutputStream::getBufferLocked calls
619     // attachBufferToOutputs, which holds the stream lock, and waits for the
620     // splitter lock.
621     sp<IGraphicBufferConsumer> consumer(mConsumer);
622     mMutex.unlock();
623     int res = NO_ERROR;
624     if (consumer != nullptr) {
625         if (detach) {
626             res = consumer->detachBuffer(consumerSlot);
627         } else {
628             res = consumer->releaseBuffer(consumerSlot, frameNumber,
629                     EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence());
630         }
631     } else {
632         SP_LOGE("%s: consumer has become null!", __FUNCTION__);
633     }
634     mMutex.lock();
635 
636     if (res != NO_ERROR) {
637         if (detach) {
638             SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
639         } else {
640             SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
641         }
642     } else {
643         if (mAcquiredInputBuffers == 0) {
644             ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
645         } else {
646             mAcquiredInputBuffers--;
647         }
648     }
649 }
650 
onBufferReleasedByOutput(const sp<IGraphicBufferProducer> & from)651 void Camera3StreamSplitter::onBufferReleasedByOutput(
652         const sp<IGraphicBufferProducer>& from) {
653     ATRACE_CALL();
654     sp<Fence> fence;
655 
656     int slot = BufferItem::INVALID_BUFFER_SLOT;
657     auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
658             nullptr, nullptr);
659     Mutex::Autolock lock(mMutex);
660     handleOutputDequeueStatusLocked(res, slot);
661     if (res != OK) {
662         return;
663     }
664 
665     size_t surfaceId = 0;
666     bool found = false;
667     for (const auto& it : mOutputs) {
668         if (it.second == from) {
669             found = true;
670             surfaceId = it.first;
671             break;
672         }
673     }
674     if (!found) {
675         SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__);
676         return;
677     }
678 
679     returnOutputBufferLocked(fence, from, surfaceId, slot);
680 }
681 
onBufferReplacedLocked(const sp<IGraphicBufferProducer> & from,size_t surfaceId)682 void Camera3StreamSplitter::onBufferReplacedLocked(
683         const sp<IGraphicBufferProducer>& from, size_t surfaceId) {
684     ATRACE_CALL();
685     sp<Fence> fence;
686 
687     int slot = BufferItem::INVALID_BUFFER_SLOT;
688     auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
689             nullptr, nullptr);
690     handleOutputDequeueStatusLocked(res, slot);
691     if (res != OK) {
692         return;
693     }
694 
695     returnOutputBufferLocked(fence, from, surfaceId, slot);
696 }
697 
returnOutputBufferLocked(const sp<Fence> & fence,const sp<IGraphicBufferProducer> & from,size_t surfaceId,int slot)698 void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence,
699         const sp<IGraphicBufferProducer>& from, size_t surfaceId, int slot) {
700     sp<GraphicBuffer> buffer;
701 
702     if (mOutputSlots[from] == nullptr) {
703         //Output surface got likely removed by client.
704         return;
705     }
706 
707     auto outputSlots = *mOutputSlots[from];
708     buffer = outputSlots[slot];
709     BufferTracker& tracker = *(mBuffers[buffer->getId()]);
710     // Merge the release fence of the incoming buffer so that the fence we send
711     // back to the input includes all of the outputs' fences
712     if (fence != nullptr && fence->isValid()) {
713         tracker.mergeFence(fence);
714     }
715 
716     auto detachBuffer = mDetachedBuffers.find(buffer->getId());
717     bool detach = (detachBuffer != mDetachedBuffers.end());
718     if (detach) {
719         auto res = from->detachBuffer(slot);
720         if (res == NO_ERROR) {
721             outputSlots[slot] = nullptr;
722         } else {
723             SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res);
724         }
725     }
726 
727     // Check to see if this is the last outstanding reference to this buffer
728     decrementBufRefCountLocked(buffer->getId(), surfaceId);
729 }
730 
handleOutputDequeueStatusLocked(status_t res,int slot)731 void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res, int slot) {
732     if (res == NO_INIT) {
733         // If we just discovered that this output has been abandoned, note that,
734         // but we can't do anything else, since buffer is invalid
735         onAbandonedLocked();
736     } else if (res == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
737         SP_LOGE("%s: Producer needs to re-allocate buffer!", __FUNCTION__);
738         SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
739     } else if (res == IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
740         SP_LOGE("%s: All slot->buffer mapping should be released!", __FUNCTION__);
741         SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
742     } else if (res == NO_MEMORY) {
743         SP_LOGE("%s: No free buffers", __FUNCTION__);
744     } else if (res == WOULD_BLOCK) {
745         SP_LOGE("%s: Dequeue call will block", __FUNCTION__);
746     } else if (res != OK || (slot == BufferItem::INVALID_BUFFER_SLOT)) {
747         SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res);
748     }
749 }
750 
onAbandonedLocked()751 void Camera3StreamSplitter::onAbandonedLocked() {
752     // If this is called from binderDied callback, it means the app process
753     // holding the binder has died. CameraService will be notified of the binder
754     // death, and camera device will be closed, which in turn calls
755     // disconnect().
756     //
757     // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
758     // consumer being abanoned shouldn't impact the other consumer. So we won't
759     // stop the buffer flow.
760     //
761     // In both cases, we don't need to do anything here.
762     SP_LOGV("One of my outputs has abandoned me");
763 }
764 
getSlotForOutputLocked(const sp<IGraphicBufferProducer> & gbp,const sp<GraphicBuffer> & gb)765 int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
766         const sp<GraphicBuffer>& gb) {
767     auto& outputSlots = *mOutputSlots[gbp];
768 
769     for (size_t i = 0; i < outputSlots.size(); i++) {
770         if (outputSlots[i] == gb) {
771             return (int)i;
772         }
773     }
774 
775     SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
776             gbp.get());
777     return BufferItem::INVALID_BUFFER_SLOT;
778 }
779 
OutputListener(wp<Camera3StreamSplitter> splitter,wp<IGraphicBufferProducer> output)780 Camera3StreamSplitter::OutputListener::OutputListener(
781         wp<Camera3StreamSplitter> splitter,
782         wp<IGraphicBufferProducer> output)
783       : mSplitter(splitter), mOutput(output) {}
784 
onBufferReleased()785 void Camera3StreamSplitter::OutputListener::onBufferReleased() {
786     ATRACE_CALL();
787     sp<Camera3StreamSplitter> splitter = mSplitter.promote();
788     sp<IGraphicBufferProducer> output = mOutput.promote();
789     if (splitter != nullptr && output != nullptr) {
790         splitter->onBufferReleasedByOutput(output);
791     }
792 }
793 
binderDied(const wp<IBinder> &)794 void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
795     sp<Camera3StreamSplitter> splitter = mSplitter.promote();
796     if (splitter != nullptr) {
797         Mutex::Autolock lock(splitter->mMutex);
798         splitter->onAbandonedLocked();
799     }
800 }
801 
BufferTracker(const sp<GraphicBuffer> & buffer,const std::vector<size_t> & requestedSurfaces)802 Camera3StreamSplitter::BufferTracker::BufferTracker(
803         const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
804       : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
805         mReferenceCount(requestedSurfaces.size()) {}
806 
mergeFence(const sp<Fence> & with)807 void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
808     mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
809 }
810 
decrementReferenceCountLocked(size_t surfaceId)811 size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) {
812     const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId);
813     if (it == mRequestedSurfaces.end()) {
814         return mReferenceCount;
815     } else {
816         mRequestedSurfaces.erase(it);
817     }
818 
819     if (mReferenceCount > 0)
820         --mReferenceCount;
821     return mReferenceCount;
822 }
823 
824 } // namespace android
825