blob: 255b4f2ac95afa73914f7b9403c7b8b15450b2c9 [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>
Austin Borger1c1bee02023-06-01 16:51:35 -070028#include <camera/StringUtils.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070029
30#include <ui/GraphicBuffer.h>
31
32#include <binder/ProcessState.h>
33
34#include <utils/Trace.h>
35
Mathias Agopian05d19b02017-02-28 16:28:19 -080036#include <cutils/atomic.h>
37
Emilian Peev2295df72021-11-12 18:14:10 -080038#include "Camera3Stream.h"
39
Shuzhen Wang0129d522016-10-30 22:43:41 -070040#include "Camera3StreamSplitter.h"
41
42namespace android {
43
Emilian Peev40ead602017-09-26 15:46:36 +010044status_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,
Emilian Peev2295df72021-11-12 18:14:10 -080046 uint32_t height, android::PixelFormat format, sp<Surface>* consumer,
Emilian Peevc81a7592022-02-14 17:38:18 -080047 int64_t dynamicRangeProfile) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080048 ATRACE_CALL();
49 if (consumer == nullptr) {
50 SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070051 return BAD_VALUE;
52 }
53
54 Mutex::Autolock lock(mMutex);
55 status_t res = OK;
56
57 if (mOutputs.size() > 0 || mConsumer != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080058 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());
Shuzhen Wang0129d522016-10-30 22:43:41 -070063 return BAD_VALUE;
64 }
65
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080066 mMaxHalBuffers = halMaxBuffers;
67 mConsumerName = getUniqueConsumerName();
Emilian Peev2295df72021-11-12 18:14:10 -080068 mDynamicRangeProfile = dynamicRangeProfile;
Shuzhen Wang0129d522016-10-30 22:43:41 -070069 // Add output surfaces. This has to be before creating internal buffer queue
70 // in order to get max consumer side buffers.
Emilian Peev40ead602017-09-26 15:46:36 +010071 for (auto &it : surfaces) {
72 if (it.second == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080073 SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
Shuzhen Wang758c2152017-01-10 18:26:18 -080074 return BAD_VALUE;
75 }
Emilian Peev40ead602017-09-26 15:46:36 +010076 res = addOutputLocked(it.first, it.second);
Shuzhen Wang758c2152017-01-10 18:26:18 -080077 if (res != OK) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080078 SP_LOGE("%s: Failed to add output surface: %s(%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -080079 __FUNCTION__, strerror(-res), res);
80 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -070081 }
82 }
83
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080084 // Create BufferQueue for input
Shuzhen Wang0129d522016-10-30 22:43:41 -070085 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
86
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080087 // 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().
Emilian Peevf130ad72018-10-11 11:03:07 +010091 mMaxConsumerBuffers++;
92 mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage, mMaxConsumerBuffers);
Shuzhen Wang0129d522016-10-30 22:43:41 -070093 if (mBufferItemConsumer == nullptr) {
94 return NO_MEMORY;
95 }
Austin Borger1c1bee02023-06-01 16:51:35 -070096 mConsumer->setConsumerName(toString8(mConsumerName));
Shuzhen Wang0129d522016-10-30 22:43:41 -070097
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080098 *consumer = new Surface(mProducer);
99 if (*consumer == nullptr) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700100 return NO_MEMORY;
101 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700102
Emilian Peev40ead602017-09-26 15:46:36 +0100103 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
Shuzhen Wang0129d522016-10-30 22:43:41 -0700110 res = mConsumer->consumerConnect(this, /* controlledByApp */ false);
111
Emilian Peev40ead602017-09-26 15:46:36 +0100112 mWidth = width;
113 mHeight = height;
114 mFormat = format;
115 mProducerUsage = producerUsage;
Emilian Peevf130ad72018-10-11 11:03:07 +0100116 mAcquiredInputBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100117
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800118 SP_LOGV("%s: connected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700119 return res;
120}
121
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800122status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
123 ATRACE_CALL();
124 return mOnFrameAvailableRes.load();
125}
126
Shuzhen Wang0129d522016-10-30 22:43:41 -0700127void Camera3StreamSplitter::disconnect() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800128 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700129 Mutex::Autolock lock(mMutex);
130
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800131 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
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138 for (auto& output : mOutputs) {
Emilian Peev40ead602017-09-26 15:46:36 +0100139 if (output.second != nullptr) {
140 output.second->disconnect(NATIVE_WINDOW_API_CAMERA);
141 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700142 }
143 mOutputs.clear();
Emilian Peev2295df72021-11-12 18:14:10 -0800144 mOutputSurfaces.clear();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800145 mOutputSlots.clear();
Emilian Peev40ead602017-09-26 15:46:36 +0100146 mConsumerBufferCount.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700147
Emilian Peev359adca2019-10-30 10:12:46 -0700148 if (mConsumer.get() != nullptr) {
149 mConsumer->consumerDisconnect();
150 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700151
152 if (mBuffers.size() > 0) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800153 SP_LOGW("%zu buffers still being tracked", mBuffers.size());
154 mBuffers.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700155 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800156
157 mMaxHalBuffers = 0;
158 mMaxConsumerBuffers = 0;
Emilian Peevf130ad72018-10-11 11:03:07 +0100159 mAcquiredInputBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800160 SP_LOGV("%s: Disconnected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700161}
162
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700163Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) :
164 mUseHalBufManager(useHalBufManager) {}
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800165
Shuzhen Wang0129d522016-10-30 22:43:41 -0700166Camera3StreamSplitter::~Camera3StreamSplitter() {
167 disconnect();
168}
169
Emilian Peev40ead602017-09-26 15:46:36 +0100170status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800171 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700172 Mutex::Autolock lock(mMutex);
Emilian Peev40ead602017-09-26 15:46:36 +0100173 status_t res = addOutputLocked(surfaceId, outputQueue);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800174
175 if (res != OK) {
176 SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
177 return res;
178 }
179
Emilian Peevf130ad72018-10-11 11:03:07 +0100180 if (mMaxConsumerBuffers > mAcquiredInputBuffers) {
181 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
182 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800183
184 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700185}
186
Jayant Chowdhary9d3a6492023-11-22 07:23:59 +0000187void Camera3StreamSplitter::setHalBufferManager(bool enabled) {
188 Mutex::Autolock lock(mMutex);
189 mUseHalBufManager = enabled;
190}
191
Emilian Peev40ead602017-09-26 15:46:36 +0100192status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800193 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700194 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800195 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700196 return BAD_VALUE;
197 }
198
Emilian Peev40ead602017-09-26 15:46:36 +0100199 if (mOutputs[surfaceId] != nullptr) {
200 SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
201 return BAD_VALUE;
202 }
203
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700204 status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
Emilian Peev40ead602017-09-26 15:46:36 +0100205 mWidth, mHeight);
206 if (res != NO_ERROR) {
207 SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
208 return res;
209 }
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700210 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 }
Emilian Peev40ead602017-09-26 15:46:36 +0100217
Shuzhen Wang0129d522016-10-30 22:43:41 -0700218 sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
219 // Connect to the buffer producer
Shuzhen Wang0129d522016-10-30 22:43:41 -0700220 sp<OutputListener> listener(new OutputListener(this, gbp));
221 IInterface::asBinder(gbp)->linkToDeath(listener);
Emilian Peev40ead602017-09-26 15:46:36 +0100222 res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800223 if (res != NO_ERROR) {
224 SP_LOGE("addOutput: failed to connect (%d)", res);
225 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700226 }
227
228 // Query consumer side buffer count, and update overall buffer count
229 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800230 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700231 outputQueue.get(),
232 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800233 if (res != OK) {
234 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700235 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800236 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700237 }
238
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800239 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
240 maxConsumerBuffers, mMaxHalBuffers);
Emilian Peev18e3f372018-05-22 18:55:01 +0100241 // 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;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800246 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700247 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800248 if (res != OK) {
249 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700250 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800251 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700252 }
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.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100256 uint64_t usage = 0;
257 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700258 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700259 nsecs_t timeout = mUseHalBufManager ?
260 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
261 outputQueue->setDequeueTimeout(timeout);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700262 }
263
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800264 res = gbp->allowAllocation(false);
265 if (res != OK) {
266 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
267 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700268 }
269
270 // Add new entry into mOutputs
Emilian Peev40ead602017-09-26 15:46:36 +0100271 mOutputs[surfaceId] = gbp;
Emilian Peev2295df72021-11-12 18:14:10 -0800272 mOutputSurfaces[surfaceId] = outputQueue;
Emilian Peev40ead602017-09-26 15:46:36 +0100273 mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000274 if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
275 SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
276 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
277 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800278 mNotifiers[gbp] = listener;
279 mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
280
281 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700282 return NO_ERROR;
283}
284
Emilian Peev40ead602017-09-26 15:46:36 +0100285status_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
Emilian Peevf130ad72018-10-11 11:03:07 +0100295 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 }
Emilian Peev40ead602017-09-26 15:46:36 +0100301 }
302
303 return res;
304}
305
306status_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];
Emilian Peev6f823722017-12-07 18:05:49 +0000317 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 }
Emilian Peev40ead602017-09-26 15:46:36 +0100328 }
329 }
330 mOutputs[surfaceId] = nullptr;
Emilian Peev2295df72021-11-12 18:14:10 -0800331 mOutputSurfaces[surfaceId] = nullptr;
Emilian Peev40ead602017-09-26 15:46:36 +0100332 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;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000350 mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100351 mConsumerBufferCount[surfaceId] = 0;
352
353 return res;
354}
355
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800356status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100357 const BufferItem& bufferItem, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800358 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
Emilian Peev2295df72021-11-12 18:14:10 -0800372 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
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800380 // 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);
Emilian Peev40ead602017-09-26 15:46:36 +0100390 //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 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800395 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();
Emilian Peev40ead602017-09-26 15:46:36 +0100403 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800404 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) {
Emilian Peev703e4992018-02-27 11:42:09 +0000411 onBufferReplacedLocked(output, surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800412 }
413
414 return res;
415}
416
Austin Borger1c1bee02023-06-01 16:51:35 -0700417std::string Camera3StreamSplitter::getUniqueConsumerName() {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700418 static volatile int32_t counter = 0;
Austin Borger1c1bee02023-06-01 16:51:35 -0700419 return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700420}
421
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800422status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700423 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800424
Shuzhen Wang0129d522016-10-30 22:43:41 -0700425 Mutex::Autolock lock(mMutex);
426
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800427 uint64_t bufferId = buffer->getId();
428 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
429 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700430
Emilian Peev40ead602017-09-26 15:46:36 +0100431 return OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800432}
433
434status_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];
Emilian Peev40ead602017-09-26 15:46:36 +0100449 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 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800458 //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) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700466 SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)",
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800467 __FUNCTION__, gbp.get(), strerror(-res), res);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700468 // TODO: might need to detach/cleanup the already attached buffers before return?
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800469 return res;
470 }
Emilian Peev21e79db2018-03-16 18:17:57 +0000471 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 }
Emilian Peev40ead602017-09-26 15:46:36 +0100476 //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 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800481 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev21e79db2018-03-16 18:17:57 +0000482 if (static_cast<size_t> (slot + 1) > outputSlots.size()) {
483 outputSlots.resize(slot + 1);
484 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800485 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.
Emilian Peev40ead602017-09-26 15:46:36 +0100489 decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800490 }
491 SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
492 slot, gbp.get());
493 outputSlots[slot] = gb;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700494 }
495
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800496 mBuffers[bufferId] = std::move(tracker);
497
498 return res;
499}
500
501void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
502 ATRACE_CALL();
503 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700504
505 // Acquire and detach the buffer from the input
506 BufferItem bufferItem;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800507 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 }
Emilian Peev40ead602017-09-26 15:46:36 +0100513
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__);
Yin-Chia Yehda208452019-08-26 15:35:57 -0700522 mOnFrameAvailableRes.store(BAD_VALUE);
Emilian Peev40ead602017-09-26 15:46:36 +0100523 return;
524 }
525 bufferId = bufferItem.mGraphicBuffer->getId();
526
527 if (mBuffers.find(bufferId) == mBuffers.end()) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800528 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
529 __FUNCTION__);
530 mOnFrameAvailableRes.store(INVALID_OPERATION);
531 return;
532 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700533
Emilian Peevf130ad72018-10-11 11:03:07 +0100534 mAcquiredInputBuffers++;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800535 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
536 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700537
Emilian Peev3bdcdff2018-06-25 14:37:29 +0100538 if (bufferItem.mTransformToDisplayInverse) {
539 bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
540 }
541
Shuzhen Wang0129d522016-10-30 22:43:41 -0700542 // Attach and queue the buffer to each of the outputs
Emilian Peev40ead602017-09-26 15:46:36 +0100543 BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700544
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800545 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()) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700548
Emilian Peev40ead602017-09-26 15:46:36 +0100549 if (mOutputs[id] == nullptr) {
550 //Output surface got likely removed by client.
551 continue;
552 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700553
Emilian Peev40ead602017-09-26 15:46:36 +0100554 res = outputBufferLocked(mOutputs[id], bufferItem, id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800555 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;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700561 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800562 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700563
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800564 mOnFrameAvailableRes.store(res);
565}
566
Yin-Chia Yehda208452019-08-26 15:35:57 -0700567void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
568 ATRACE_CALL();
569 onFrameAvailable(item);
570}
571
Emilian Peev40ead602017-09-26 15:46:36 +0100572void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800573 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800574
Emilian Peev40ead602017-09-26 15:46:36 +0100575 if (mBuffers[id] == nullptr) {
576 return;
577 }
578
579 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800580 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
Emilian Peev40ead602017-09-26 15:46:36 +0100592 uint64_t bufferId = tracker_ptr->getBuffer()->getId();
593 int consumerSlot = -1;
594 uint64_t frameNumber;
Emilian Peev6f823722017-12-07 18:05:49 +0000595 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;
Emilian Peev40ead602017-09-26 15:46:36 +0100600 break;
601 }
602 }
603 if (consumerSlot == -1) {
604 SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800605 return;
606 }
607
Emilian Peev6f823722017-12-07 18:05:49 +0000608 auto detachBuffer = mDetachedBuffers.find(bufferId);
609 bool detach = (detachBuffer != mDetachedBuffers.end());
610 if (detach) {
611 mDetachedBuffers.erase(detachBuffer);
612 mInputSlots.erase(inputSlot);
613 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800614 // 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();
Emilian Peev40ead602017-09-26 15:46:36 +0100623 int res = NO_ERROR;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800624 if (consumer != nullptr) {
Emilian Peev6f823722017-12-07 18:05:49 +0000625 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 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800631 } else {
632 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
633 }
634 mMutex.lock();
Emilian Peev6f823722017-12-07 18:05:49 +0000635
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800636 if (res != NO_ERROR) {
Emilian Peev6f823722017-12-07 18:05:49 +0000637 if (detach) {
638 SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
639 } else {
640 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
641 }
Emilian Peevf130ad72018-10-11 11:03:07 +0100642 } else {
643 if (mAcquiredInputBuffers == 0) {
644 ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
645 } else {
646 mAcquiredInputBuffers--;
647 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700648 }
649}
650
651void Camera3StreamSplitter::onBufferReleasedByOutput(
652 const sp<IGraphicBufferProducer>& from) {
653 ATRACE_CALL();
Emilian Peev703e4992018-02-27 11:42:09 +0000654 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);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700659 Mutex::Autolock lock(mMutex);
Emilian Peev703e4992018-02-27 11:42:09 +0000660 handleOutputDequeueStatusLocked(res, slot);
661 if (res != OK) {
662 return;
663 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700664
Emilian Peev40ead602017-09-26 15:46:36 +0100665 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
Emilian Peev703e4992018-02-27 11:42:09 +0000679 returnOutputBufferLocked(fence, from, surfaceId, slot);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800680}
681
Emilian Peev703e4992018-02-27 11:42:09 +0000682void Camera3StreamSplitter::onBufferReplacedLocked(
Emilian Peev40ead602017-09-26 15:46:36 +0100683 const sp<IGraphicBufferProducer>& from, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800684 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700685 sp<Fence> fence;
Emilian Peev40ead602017-09-26 15:46:36 +0100686
687 int slot = BufferItem::INVALID_BUFFER_SLOT;
688 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
689 nullptr, nullptr);
Emilian Peev703e4992018-02-27 11:42:09 +0000690 handleOutputDequeueStatusLocked(res, slot);
691 if (res != OK) {
Shuzhen Wangafa8a912017-03-15 10:51:27 -0700692 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700693 }
694
Emilian Peev703e4992018-02-27 11:42:09 +0000695 returnOutputBufferLocked(fence, from, surfaceId, slot);
696}
697
698void 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];
Shuzhen Wang0129d522016-10-30 22:43:41 -0700709 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700710 // 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
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800712 if (fence != nullptr && fence->isValid()) {
713 tracker.mergeFence(fence);
714 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700715
Emilian Peev6f823722017-12-07 18:05:49 +0000716 auto detachBuffer = mDetachedBuffers.find(buffer->getId());
717 bool detach = (detachBuffer != mDetachedBuffers.end());
718 if (detach) {
Emilian Peev703e4992018-02-27 11:42:09 +0000719 auto res = from->detachBuffer(slot);
Emilian Peev6f823722017-12-07 18:05:49 +0000720 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
Shuzhen Wang0129d522016-10-30 22:43:41 -0700727 // Check to see if this is the last outstanding reference to this buffer
Emilian Peev40ead602017-09-26 15:46:36 +0100728 decrementBufRefCountLocked(buffer->getId(), surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700729}
730
Emilian Peev703e4992018-02-27 11:42:09 +0000731void 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
Shuzhen Wang0129d522016-10-30 22:43:41 -0700751void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800752 // 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
765int 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 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800774
Emilian Peev40ead602017-09-26 15:46:36 +0100775 SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800776 gbp.get());
777 return BufferItem::INVALID_BUFFER_SLOT;
778}
779
Shuzhen Wang0129d522016-10-30 22:43:41 -0700780Camera3StreamSplitter::OutputListener::OutputListener(
781 wp<Camera3StreamSplitter> splitter,
782 wp<IGraphicBufferProducer> output)
783 : mSplitter(splitter), mOutput(output) {}
784
785void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800786 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
788 sp<IGraphicBufferProducer> output = mOutput.promote();
789 if (splitter != nullptr && output != nullptr) {
790 splitter->onBufferReleasedByOutput(output);
791 }
792}
793
794void 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
802Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800803 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
804 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
805 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700806
807void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
808 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
809}
810
Emilian Peev40ead602017-09-26 15:46:36 +0100811size_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
Shuzhen Wang0129d522016-10-30 22:43:41 -0700819 if (mReferenceCount > 0)
820 --mReferenceCount;
821 return mReferenceCount;
822}
823
824} // namespace android