blob: 8175eb5f2ad030bccb5141ef3cc69eb0c03edd9e [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
Emilian Peev40ead602017-09-26 15:46:36 +0100187status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800188 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700189 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800190 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700191 return BAD_VALUE;
192 }
193
Emilian Peev40ead602017-09-26 15:46:36 +0100194 if (mOutputs[surfaceId] != nullptr) {
195 SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
196 return BAD_VALUE;
197 }
198
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700199 status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
Emilian Peev40ead602017-09-26 15:46:36 +0100200 mWidth, mHeight);
201 if (res != NO_ERROR) {
202 SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
203 return res;
204 }
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700205 res = native_window_set_buffers_format(outputQueue.get(),
206 mFormat);
207 if (res != OK) {
208 ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu",
209 __FUNCTION__, mFormat, surfaceId);
210 return res;
211 }
Emilian Peev40ead602017-09-26 15:46:36 +0100212
Shuzhen Wang0129d522016-10-30 22:43:41 -0700213 sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
214 // Connect to the buffer producer
Shuzhen Wang0129d522016-10-30 22:43:41 -0700215 sp<OutputListener> listener(new OutputListener(this, gbp));
216 IInterface::asBinder(gbp)->linkToDeath(listener);
Emilian Peev40ead602017-09-26 15:46:36 +0100217 res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800218 if (res != NO_ERROR) {
219 SP_LOGE("addOutput: failed to connect (%d)", res);
220 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700221 }
222
223 // Query consumer side buffer count, and update overall buffer count
224 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800225 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700226 outputQueue.get(),
227 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800228 if (res != OK) {
229 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700230 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800231 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700232 }
233
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800234 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
235 maxConsumerBuffers, mMaxHalBuffers);
Emilian Peev18e3f372018-05-22 18:55:01 +0100236 // The output slot count requirement can change depending on the current amount
237 // of outputs and incoming buffer consumption rate. To avoid any issues with
238 // insufficient slots, set their count to the maximum supported. The output
239 // surface buffer allocation is disabled so no real buffers will get allocated.
240 size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800241 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700242 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800243 if (res != OK) {
244 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700245 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800246 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700247 }
248
249 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
250 // We need skip these cases as timeout will disable the non-blocking (async) mode.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100251 uint64_t usage = 0;
252 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700253 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700254 nsecs_t timeout = mUseHalBufManager ?
255 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
256 outputQueue->setDequeueTimeout(timeout);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700257 }
258
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800259 res = gbp->allowAllocation(false);
260 if (res != OK) {
261 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
262 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700263 }
264
265 // Add new entry into mOutputs
Emilian Peev40ead602017-09-26 15:46:36 +0100266 mOutputs[surfaceId] = gbp;
Emilian Peev2295df72021-11-12 18:14:10 -0800267 mOutputSurfaces[surfaceId] = outputQueue;
Emilian Peev40ead602017-09-26 15:46:36 +0100268 mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000269 if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
270 SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
271 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
272 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800273 mNotifiers[gbp] = listener;
274 mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
275
276 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700277 return NO_ERROR;
278}
279
Emilian Peev40ead602017-09-26 15:46:36 +0100280status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) {
281 ATRACE_CALL();
282 Mutex::Autolock lock(mMutex);
283
284 status_t res = removeOutputLocked(surfaceId);
285 if (res != OK) {
286 SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res);
287 return res;
288 }
289
Emilian Peevf130ad72018-10-11 11:03:07 +0100290 if (mAcquiredInputBuffers < mMaxConsumerBuffers) {
291 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
292 if (res != OK) {
293 SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res);
294 return res;
295 }
Emilian Peev40ead602017-09-26 15:46:36 +0100296 }
297
298 return res;
299}
300
301status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) {
302 if (mOutputs[surfaceId] == nullptr) {
303 SP_LOGE("%s: output surface is not present!", __FUNCTION__);
304 return BAD_VALUE;
305 }
306
307 sp<IGraphicBufferProducer> gbp = mOutputs[surfaceId];
308 //Search and decrement the ref. count of any buffers that are
309 //still attached to the removed surface.
310 std::vector<uint64_t> pendingBufferIds;
311 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev6f823722017-12-07 18:05:49 +0000312 for (size_t i = 0; i < outputSlots.size(); i++) {
313 if (outputSlots[i] != nullptr) {
314 pendingBufferIds.push_back(outputSlots[i]->getId());
315 auto rc = gbp->detachBuffer(i);
316 if (rc != NO_ERROR) {
317 //Buffers that fail to detach here will be scheduled for detach in the
318 //input buffer queue and the rest of the registered outputs instead.
319 //This will help ensure that camera stops accessing buffers that still
320 //can get referenced by the disconnected output.
321 mDetachedBuffers.emplace(outputSlots[i]->getId());
322 }
Emilian Peev40ead602017-09-26 15:46:36 +0100323 }
324 }
325 mOutputs[surfaceId] = nullptr;
Emilian Peev2295df72021-11-12 18:14:10 -0800326 mOutputSurfaces[surfaceId] = nullptr;
Emilian Peev40ead602017-09-26 15:46:36 +0100327 mOutputSlots[gbp] = nullptr;
328 for (const auto &id : pendingBufferIds) {
329 decrementBufRefCountLocked(id, surfaceId);
330 }
331
332 auto res = IInterface::asBinder(gbp)->unlinkToDeath(mNotifiers[gbp]);
333 if (res != OK) {
334 SP_LOGE("%s: Failed to unlink producer death listener: %d ", __FUNCTION__, res);
335 return res;
336 }
337
338 res = gbp->disconnect(NATIVE_WINDOW_API_CAMERA);
339 if (res != OK) {
340 SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res);
341 return res;
342 }
343
344 mNotifiers[gbp] = nullptr;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000345 mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100346 mConsumerBufferCount[surfaceId] = 0;
347
348 return res;
349}
350
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800351status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100352 const BufferItem& bufferItem, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800353 ATRACE_CALL();
354 status_t res;
355 IGraphicBufferProducer::QueueBufferInput queueInput(
356 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
357 bufferItem.mDataSpace, bufferItem.mCrop,
358 static_cast<int32_t>(bufferItem.mScalingMode),
359 bufferItem.mTransform, bufferItem.mFence);
360
361 IGraphicBufferProducer::QueueBufferOutput queueOutput;
362
363 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
364 const BufferTracker& tracker = *(mBuffers[bufferId]);
365 int slot = getSlotForOutputLocked(output, tracker.getBuffer());
366
Emilian Peev2295df72021-11-12 18:14:10 -0800367 if (mOutputSurfaces[surfaceId] != nullptr) {
368 sp<ANativeWindow> anw = mOutputSurfaces[surfaceId];
369 camera3::Camera3Stream::queueHDRMetadata(
370 bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile);
371 } else {
372 SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId);
373 }
374
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800375 // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
376 // queueBuffer (which will try to acquire the output lock), the output could be holding its
377 // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into
378 // circular lock situation.
379 mMutex.unlock();
380 res = output->queueBuffer(slot, queueInput, &queueOutput);
381 mMutex.lock();
382
383 SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d",
384 __FUNCTION__, output.get(), slot, res);
Emilian Peev40ead602017-09-26 15:46:36 +0100385 //During buffer queue 'mMutex' is not held which makes the removal of
386 //"output" possible. Check whether this is the case and return.
387 if (mOutputSlots[output] == nullptr) {
388 return res;
389 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800390 if (res != OK) {
391 if (res != NO_INIT && res != DEAD_OBJECT) {
392 SP_LOGE("Queuing buffer to output failed (%d)", res);
393 }
394 // If we just discovered that this output has been abandoned, note
395 // that, increment the release count so that we still release this
396 // buffer eventually, and move on to the next output
397 onAbandonedLocked();
Emilian Peev40ead602017-09-26 15:46:36 +0100398 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800399 return res;
400 }
401
402 // If the queued buffer replaces a pending buffer in the async
403 // queue, no onBufferReleased is called by the buffer queue.
404 // Proactively trigger the callback to avoid buffer loss.
405 if (queueOutput.bufferReplaced) {
Emilian Peev703e4992018-02-27 11:42:09 +0000406 onBufferReplacedLocked(output, surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800407 }
408
409 return res;
410}
411
Austin Borger1c1bee02023-06-01 16:51:35 -0700412std::string Camera3StreamSplitter::getUniqueConsumerName() {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700413 static volatile int32_t counter = 0;
Austin Borger1c1bee02023-06-01 16:51:35 -0700414 return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700415}
416
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800417status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700418 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800419
Shuzhen Wang0129d522016-10-30 22:43:41 -0700420 Mutex::Autolock lock(mMutex);
421
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800422 uint64_t bufferId = buffer->getId();
423 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
424 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700425
Emilian Peev40ead602017-09-26 15:46:36 +0100426 return OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800427}
428
429status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
430 const std::vector<size_t>& surface_ids) {
431 ATRACE_CALL();
432 status_t res = OK;
433
434 Mutex::Autolock lock(mMutex);
435
436 sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
437 uint64_t bufferId = gb->getId();
438
439 // Initialize buffer tracker for this input buffer
440 auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
441
442 for (auto& surface_id : surface_ids) {
443 sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id];
Emilian Peev40ead602017-09-26 15:46:36 +0100444 if (gbp.get() == nullptr) {
445 //Output surface got likely removed by client.
446 continue;
447 }
448 int slot = getSlotForOutputLocked(gbp, gb);
449 if (slot != BufferItem::INVALID_BUFFER_SLOT) {
450 //Buffer is already attached to this output surface.
451 continue;
452 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800453 //Temporarly Unlock the mutex when trying to attachBuffer to the output
454 //queue, because attachBuffer could block in case of a slow consumer. If
455 //we block while holding the lock, onFrameAvailable and onBufferReleased
456 //will block as well because they need to acquire the same lock.
457 mMutex.unlock();
458 res = gbp->attachBuffer(&slot, gb);
459 mMutex.lock();
460 if (res != OK) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700461 SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)",
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800462 __FUNCTION__, gbp.get(), strerror(-res), res);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700463 // TODO: might need to detach/cleanup the already attached buffers before return?
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800464 return res;
465 }
Emilian Peev21e79db2018-03-16 18:17:57 +0000466 if ((slot < 0) || (slot > BufferQueue::NUM_BUFFER_SLOTS)) {
467 SP_LOGE("%s: Slot received %d either bigger than expected maximum %d or negative!",
468 __FUNCTION__, slot, BufferQueue::NUM_BUFFER_SLOTS);
469 return BAD_VALUE;
470 }
Emilian Peev40ead602017-09-26 15:46:36 +0100471 //During buffer attach 'mMutex' is not held which makes the removal of
472 //"gbp" possible. Check whether this is the case and continue.
473 if (mOutputSlots[gbp] == nullptr) {
474 continue;
475 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800476 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev21e79db2018-03-16 18:17:57 +0000477 if (static_cast<size_t> (slot + 1) > outputSlots.size()) {
478 outputSlots.resize(slot + 1);
479 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800480 if (outputSlots[slot] != nullptr) {
481 // If the buffer is attached to a slot which already contains a buffer,
482 // the previous buffer will be removed from the output queue. Decrement
483 // the reference count accordingly.
Emilian Peev40ead602017-09-26 15:46:36 +0100484 decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800485 }
486 SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
487 slot, gbp.get());
488 outputSlots[slot] = gb;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700489 }
490
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800491 mBuffers[bufferId] = std::move(tracker);
492
493 return res;
494}
495
496void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
497 ATRACE_CALL();
498 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700499
500 // Acquire and detach the buffer from the input
501 BufferItem bufferItem;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800502 status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
503 if (res != NO_ERROR) {
504 SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
505 mOnFrameAvailableRes.store(res);
506 return;
507 }
Emilian Peev40ead602017-09-26 15:46:36 +0100508
509 uint64_t bufferId;
510 if (bufferItem.mGraphicBuffer != nullptr) {
511 mInputSlots[bufferItem.mSlot] = bufferItem;
512 } else if (bufferItem.mAcquireCalled) {
513 bufferItem.mGraphicBuffer = mInputSlots[bufferItem.mSlot].mGraphicBuffer;
514 mInputSlots[bufferItem.mSlot].mFrameNumber = bufferItem.mFrameNumber;
515 } else {
516 SP_LOGE("%s: Invalid input graphic buffer!", __FUNCTION__);
Yin-Chia Yehda208452019-08-26 15:35:57 -0700517 mOnFrameAvailableRes.store(BAD_VALUE);
Emilian Peev40ead602017-09-26 15:46:36 +0100518 return;
519 }
520 bufferId = bufferItem.mGraphicBuffer->getId();
521
522 if (mBuffers.find(bufferId) == mBuffers.end()) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800523 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
524 __FUNCTION__);
525 mOnFrameAvailableRes.store(INVALID_OPERATION);
526 return;
527 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700528
Emilian Peevf130ad72018-10-11 11:03:07 +0100529 mAcquiredInputBuffers++;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800530 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
531 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700532
Emilian Peev3bdcdff2018-06-25 14:37:29 +0100533 if (bufferItem.mTransformToDisplayInverse) {
534 bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
535 }
536
Shuzhen Wang0129d522016-10-30 22:43:41 -0700537 // Attach and queue the buffer to each of the outputs
Emilian Peev40ead602017-09-26 15:46:36 +0100538 BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700539
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800540 SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
541 __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
542 for (const auto id : tracker.requestedSurfaces()) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543
Emilian Peev40ead602017-09-26 15:46:36 +0100544 if (mOutputs[id] == nullptr) {
545 //Output surface got likely removed by client.
546 continue;
547 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700548
Emilian Peev40ead602017-09-26 15:46:36 +0100549 res = outputBufferLocked(mOutputs[id], bufferItem, id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800550 if (res != OK) {
551 SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
552 mOnFrameAvailableRes.store(res);
553 // If we fail to send buffer to certain output, keep sending to
554 // other outputs.
555 continue;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700556 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800557 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700558
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800559 mOnFrameAvailableRes.store(res);
560}
561
Yin-Chia Yehda208452019-08-26 15:35:57 -0700562void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
563 ATRACE_CALL();
564 onFrameAvailable(item);
565}
566
Emilian Peev40ead602017-09-26 15:46:36 +0100567void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800568 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800569
Emilian Peev40ead602017-09-26 15:46:36 +0100570 if (mBuffers[id] == nullptr) {
571 return;
572 }
573
574 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800575 if (referenceCount > 0) {
576 return;
577 }
578
579 // We no longer need to track the buffer now that it is being returned to the
580 // input. Note that this should happen before we unlock the mutex and call
581 // releaseBuffer, to avoid the case where the same bufferId is acquired in
582 // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
583 // overwrites the current one.
584 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
585 mBuffers.erase(id);
586
Emilian Peev40ead602017-09-26 15:46:36 +0100587 uint64_t bufferId = tracker_ptr->getBuffer()->getId();
588 int consumerSlot = -1;
589 uint64_t frameNumber;
Emilian Peev6f823722017-12-07 18:05:49 +0000590 auto inputSlot = mInputSlots.begin();
591 for (; inputSlot != mInputSlots.end(); inputSlot++) {
592 if (inputSlot->second.mGraphicBuffer->getId() == bufferId) {
593 consumerSlot = inputSlot->second.mSlot;
594 frameNumber = inputSlot->second.mFrameNumber;
Emilian Peev40ead602017-09-26 15:46:36 +0100595 break;
596 }
597 }
598 if (consumerSlot == -1) {
599 SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800600 return;
601 }
602
Emilian Peev6f823722017-12-07 18:05:49 +0000603 auto detachBuffer = mDetachedBuffers.find(bufferId);
604 bool detach = (detachBuffer != mDetachedBuffers.end());
605 if (detach) {
606 mDetachedBuffers.erase(detachBuffer);
607 mInputSlots.erase(inputSlot);
608 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800609 // Temporarily unlock mutex to avoid circular lock:
610 // 1. This function holds splitter lock, calls releaseBuffer which triggers
611 // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
612 // OutputStream lock
613 // 2. Camera3SharedOutputStream::getBufferLocked calls
614 // attachBufferToOutputs, which holds the stream lock, and waits for the
615 // splitter lock.
616 sp<IGraphicBufferConsumer> consumer(mConsumer);
617 mMutex.unlock();
Emilian Peev40ead602017-09-26 15:46:36 +0100618 int res = NO_ERROR;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800619 if (consumer != nullptr) {
Emilian Peev6f823722017-12-07 18:05:49 +0000620 if (detach) {
621 res = consumer->detachBuffer(consumerSlot);
622 } else {
623 res = consumer->releaseBuffer(consumerSlot, frameNumber,
624 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence());
625 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800626 } else {
627 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
628 }
629 mMutex.lock();
Emilian Peev6f823722017-12-07 18:05:49 +0000630
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800631 if (res != NO_ERROR) {
Emilian Peev6f823722017-12-07 18:05:49 +0000632 if (detach) {
633 SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
634 } else {
635 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
636 }
Emilian Peevf130ad72018-10-11 11:03:07 +0100637 } else {
638 if (mAcquiredInputBuffers == 0) {
639 ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
640 } else {
641 mAcquiredInputBuffers--;
642 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700643 }
644}
645
646void Camera3StreamSplitter::onBufferReleasedByOutput(
647 const sp<IGraphicBufferProducer>& from) {
648 ATRACE_CALL();
Emilian Peev703e4992018-02-27 11:42:09 +0000649 sp<Fence> fence;
650
651 int slot = BufferItem::INVALID_BUFFER_SLOT;
652 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
653 nullptr, nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700654 Mutex::Autolock lock(mMutex);
Emilian Peev703e4992018-02-27 11:42:09 +0000655 handleOutputDequeueStatusLocked(res, slot);
656 if (res != OK) {
657 return;
658 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700659
Emilian Peev40ead602017-09-26 15:46:36 +0100660 size_t surfaceId = 0;
661 bool found = false;
662 for (const auto& it : mOutputs) {
663 if (it.second == from) {
664 found = true;
665 surfaceId = it.first;
666 break;
667 }
668 }
669 if (!found) {
670 SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__);
671 return;
672 }
673
Emilian Peev703e4992018-02-27 11:42:09 +0000674 returnOutputBufferLocked(fence, from, surfaceId, slot);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800675}
676
Emilian Peev703e4992018-02-27 11:42:09 +0000677void Camera3StreamSplitter::onBufferReplacedLocked(
Emilian Peev40ead602017-09-26 15:46:36 +0100678 const sp<IGraphicBufferProducer>& from, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800679 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700680 sp<Fence> fence;
Emilian Peev40ead602017-09-26 15:46:36 +0100681
682 int slot = BufferItem::INVALID_BUFFER_SLOT;
683 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
684 nullptr, nullptr);
Emilian Peev703e4992018-02-27 11:42:09 +0000685 handleOutputDequeueStatusLocked(res, slot);
686 if (res != OK) {
Shuzhen Wangafa8a912017-03-15 10:51:27 -0700687 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700688 }
689
Emilian Peev703e4992018-02-27 11:42:09 +0000690 returnOutputBufferLocked(fence, from, surfaceId, slot);
691}
692
693void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence,
694 const sp<IGraphicBufferProducer>& from, size_t surfaceId, int slot) {
695 sp<GraphicBuffer> buffer;
696
697 if (mOutputSlots[from] == nullptr) {
698 //Output surface got likely removed by client.
699 return;
700 }
701
702 auto outputSlots = *mOutputSlots[from];
703 buffer = outputSlots[slot];
Shuzhen Wang0129d522016-10-30 22:43:41 -0700704 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700705 // Merge the release fence of the incoming buffer so that the fence we send
706 // back to the input includes all of the outputs' fences
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800707 if (fence != nullptr && fence->isValid()) {
708 tracker.mergeFence(fence);
709 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700710
Emilian Peev6f823722017-12-07 18:05:49 +0000711 auto detachBuffer = mDetachedBuffers.find(buffer->getId());
712 bool detach = (detachBuffer != mDetachedBuffers.end());
713 if (detach) {
Emilian Peev703e4992018-02-27 11:42:09 +0000714 auto res = from->detachBuffer(slot);
Emilian Peev6f823722017-12-07 18:05:49 +0000715 if (res == NO_ERROR) {
716 outputSlots[slot] = nullptr;
717 } else {
718 SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res);
719 }
720 }
721
Shuzhen Wang0129d522016-10-30 22:43:41 -0700722 // Check to see if this is the last outstanding reference to this buffer
Emilian Peev40ead602017-09-26 15:46:36 +0100723 decrementBufRefCountLocked(buffer->getId(), surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700724}
725
Emilian Peev703e4992018-02-27 11:42:09 +0000726void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res, int slot) {
727 if (res == NO_INIT) {
728 // If we just discovered that this output has been abandoned, note that,
729 // but we can't do anything else, since buffer is invalid
730 onAbandonedLocked();
731 } else if (res == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
732 SP_LOGE("%s: Producer needs to re-allocate buffer!", __FUNCTION__);
733 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
734 } else if (res == IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
735 SP_LOGE("%s: All slot->buffer mapping should be released!", __FUNCTION__);
736 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
737 } else if (res == NO_MEMORY) {
738 SP_LOGE("%s: No free buffers", __FUNCTION__);
739 } else if (res == WOULD_BLOCK) {
740 SP_LOGE("%s: Dequeue call will block", __FUNCTION__);
741 } else if (res != OK || (slot == BufferItem::INVALID_BUFFER_SLOT)) {
742 SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res);
743 }
744}
745
Shuzhen Wang0129d522016-10-30 22:43:41 -0700746void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800747 // If this is called from binderDied callback, it means the app process
748 // holding the binder has died. CameraService will be notified of the binder
749 // death, and camera device will be closed, which in turn calls
750 // disconnect().
751 //
752 // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
753 // consumer being abanoned shouldn't impact the other consumer. So we won't
754 // stop the buffer flow.
755 //
756 // In both cases, we don't need to do anything here.
757 SP_LOGV("One of my outputs has abandoned me");
758}
759
760int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
761 const sp<GraphicBuffer>& gb) {
762 auto& outputSlots = *mOutputSlots[gbp];
763
764 for (size_t i = 0; i < outputSlots.size(); i++) {
765 if (outputSlots[i] == gb) {
766 return (int)i;
767 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700768 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800769
Emilian Peev40ead602017-09-26 15:46:36 +0100770 SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800771 gbp.get());
772 return BufferItem::INVALID_BUFFER_SLOT;
773}
774
Shuzhen Wang0129d522016-10-30 22:43:41 -0700775Camera3StreamSplitter::OutputListener::OutputListener(
776 wp<Camera3StreamSplitter> splitter,
777 wp<IGraphicBufferProducer> output)
778 : mSplitter(splitter), mOutput(output) {}
779
780void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800781 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700782 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
783 sp<IGraphicBufferProducer> output = mOutput.promote();
784 if (splitter != nullptr && output != nullptr) {
785 splitter->onBufferReleasedByOutput(output);
786 }
787}
788
789void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
790 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
791 if (splitter != nullptr) {
792 Mutex::Autolock lock(splitter->mMutex);
793 splitter->onAbandonedLocked();
794 }
795}
796
797Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800798 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
799 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
800 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700801
802void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
803 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
804}
805
Emilian Peev40ead602017-09-26 15:46:36 +0100806size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) {
807 const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId);
808 if (it == mRequestedSurfaces.end()) {
809 return mReferenceCount;
810 } else {
811 mRequestedSurfaces.erase(it);
812 }
813
Shuzhen Wang0129d522016-10-30 22:43:41 -0700814 if (mReferenceCount > 0)
815 --mReferenceCount;
816 return mReferenceCount;
817}
818
819} // namespace android