blob: fd239589e5d0d8bfdf13c1db23227bccdb28f3e8 [file] [log] [blame]
Shuzhen Wang0129d522016-10-30 22:43:41 -07001/*
2 * Copyright 2014,2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <inttypes.h>
18
19#define LOG_TAG "Camera3StreamSplitter"
20#define ATRACE_TAG ATRACE_TAG_CAMERA
21//#define LOG_NDEBUG 0
22
23#include <gui/BufferItem.h>
24#include <gui/IGraphicBufferConsumer.h>
25#include <gui/IGraphicBufferProducer.h>
26#include <gui/BufferQueue.h>
27#include <gui/Surface.h>
28
29#include <ui/GraphicBuffer.h>
30
31#include <binder/ProcessState.h>
32
33#include <utils/Trace.h>
34
Mathias Agopian05d19b02017-02-28 16:28:19 -080035#include <cutils/atomic.h>
36
Emilian Peev2295df72021-11-12 18:14:10 -080037#include "Camera3Stream.h"
38
Shuzhen Wang0129d522016-10-30 22:43:41 -070039#include "Camera3StreamSplitter.h"
40
41namespace android {
42
Emilian Peev40ead602017-09-26 15:46:36 +010043status_t Camera3StreamSplitter::connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
44 uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
Emilian Peev2295df72021-11-12 18:14:10 -080045 uint32_t height, android::PixelFormat format, sp<Surface>* consumer,
Emilian Peevc81a7592022-02-14 17:38:18 -080046 int64_t dynamicRangeProfile) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080047 ATRACE_CALL();
48 if (consumer == nullptr) {
49 SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070050 return BAD_VALUE;
51 }
52
53 Mutex::Autolock lock(mMutex);
54 status_t res = OK;
55
56 if (mOutputs.size() > 0 || mConsumer != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080057 SP_LOGE("%s: already connected", __FUNCTION__);
58 return BAD_VALUE;
59 }
60 if (mBuffers.size() > 0) {
61 SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size());
Shuzhen Wang0129d522016-10-30 22:43:41 -070062 return BAD_VALUE;
63 }
64
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080065 mMaxHalBuffers = halMaxBuffers;
66 mConsumerName = getUniqueConsumerName();
Emilian Peev2295df72021-11-12 18:14:10 -080067 mDynamicRangeProfile = dynamicRangeProfile;
Shuzhen Wang0129d522016-10-30 22:43:41 -070068 // Add output surfaces. This has to be before creating internal buffer queue
69 // in order to get max consumer side buffers.
Emilian Peev40ead602017-09-26 15:46:36 +010070 for (auto &it : surfaces) {
71 if (it.second == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080072 SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
Shuzhen Wang758c2152017-01-10 18:26:18 -080073 return BAD_VALUE;
74 }
Emilian Peev40ead602017-09-26 15:46:36 +010075 res = addOutputLocked(it.first, it.second);
Shuzhen Wang758c2152017-01-10 18:26:18 -080076 if (res != OK) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080077 SP_LOGE("%s: Failed to add output surface: %s(%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -080078 __FUNCTION__, strerror(-res), res);
79 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -070080 }
81 }
82
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080083 // Create BufferQueue for input
Shuzhen Wang0129d522016-10-30 22:43:41 -070084 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
85
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080086 // Allocate 1 extra buffer to handle the case where all buffers are detached
87 // from input, and attached to the outputs. In this case, the input queue's
88 // dequeueBuffer can still allocate 1 extra buffer before being blocked by
89 // the output's attachBuffer().
Emilian Peevf130ad72018-10-11 11:03:07 +010090 mMaxConsumerBuffers++;
91 mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage, mMaxConsumerBuffers);
Shuzhen Wang0129d522016-10-30 22:43:41 -070092 if (mBufferItemConsumer == nullptr) {
93 return NO_MEMORY;
94 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080095 mConsumer->setConsumerName(mConsumerName);
Shuzhen Wang0129d522016-10-30 22:43:41 -070096
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080097 *consumer = new Surface(mProducer);
98 if (*consumer == nullptr) {
Shuzhen Wang0129d522016-10-30 22:43:41 -070099 return NO_MEMORY;
100 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700101
Emilian Peev40ead602017-09-26 15:46:36 +0100102 res = mProducer->setAsyncMode(true);
103 if (res != OK) {
104 SP_LOGE("%s: Failed to enable input queue async mode: %s(%d)", __FUNCTION__,
105 strerror(-res), res);
106 return res;
107 }
108
Shuzhen Wang0129d522016-10-30 22:43:41 -0700109 res = mConsumer->consumerConnect(this, /* controlledByApp */ false);
110
Emilian Peev40ead602017-09-26 15:46:36 +0100111 mWidth = width;
112 mHeight = height;
113 mFormat = format;
114 mProducerUsage = producerUsage;
Emilian Peevf130ad72018-10-11 11:03:07 +0100115 mAcquiredInputBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100116
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800117 SP_LOGV("%s: connected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700118 return res;
119}
120
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800121status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
122 ATRACE_CALL();
123 return mOnFrameAvailableRes.load();
124}
125
Shuzhen Wang0129d522016-10-30 22:43:41 -0700126void Camera3StreamSplitter::disconnect() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800127 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128 Mutex::Autolock lock(mMutex);
129
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800130 for (auto& notifier : mNotifiers) {
131 sp<IGraphicBufferProducer> producer = notifier.first;
132 sp<OutputListener> listener = notifier.second;
133 IInterface::asBinder(producer)->unlinkToDeath(listener);
134 }
135 mNotifiers.clear();
136
Shuzhen Wang0129d522016-10-30 22:43:41 -0700137 for (auto& output : mOutputs) {
Emilian Peev40ead602017-09-26 15:46:36 +0100138 if (output.second != nullptr) {
139 output.second->disconnect(NATIVE_WINDOW_API_CAMERA);
140 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700141 }
142 mOutputs.clear();
Emilian Peev2295df72021-11-12 18:14:10 -0800143 mOutputSurfaces.clear();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800144 mOutputSlots.clear();
Emilian Peev40ead602017-09-26 15:46:36 +0100145 mConsumerBufferCount.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700146
Emilian Peev359adca2019-10-30 10:12:46 -0700147 if (mConsumer.get() != nullptr) {
148 mConsumer->consumerDisconnect();
149 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700150
151 if (mBuffers.size() > 0) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800152 SP_LOGW("%zu buffers still being tracked", mBuffers.size());
153 mBuffers.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700154 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800155
156 mMaxHalBuffers = 0;
157 mMaxConsumerBuffers = 0;
Emilian Peevf130ad72018-10-11 11:03:07 +0100158 mAcquiredInputBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800159 SP_LOGV("%s: Disconnected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700160}
161
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700162Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) :
163 mUseHalBufManager(useHalBufManager) {}
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800164
Shuzhen Wang0129d522016-10-30 22:43:41 -0700165Camera3StreamSplitter::~Camera3StreamSplitter() {
166 disconnect();
167}
168
Emilian Peev40ead602017-09-26 15:46:36 +0100169status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800170 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700171 Mutex::Autolock lock(mMutex);
Emilian Peev40ead602017-09-26 15:46:36 +0100172 status_t res = addOutputLocked(surfaceId, outputQueue);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800173
174 if (res != OK) {
175 SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
176 return res;
177 }
178
Emilian Peevf130ad72018-10-11 11:03:07 +0100179 if (mMaxConsumerBuffers > mAcquiredInputBuffers) {
180 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
181 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800182
183 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700184}
185
Emilian Peev40ead602017-09-26 15:46:36 +0100186status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800187 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700188 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800189 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700190 return BAD_VALUE;
191 }
192
Emilian Peev40ead602017-09-26 15:46:36 +0100193 if (mOutputs[surfaceId] != nullptr) {
194 SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
195 return BAD_VALUE;
196 }
197
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700198 status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
Emilian Peev40ead602017-09-26 15:46:36 +0100199 mWidth, mHeight);
200 if (res != NO_ERROR) {
201 SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
202 return res;
203 }
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700204 res = native_window_set_buffers_format(outputQueue.get(),
205 mFormat);
206 if (res != OK) {
207 ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu",
208 __FUNCTION__, mFormat, surfaceId);
209 return res;
210 }
Emilian Peev40ead602017-09-26 15:46:36 +0100211
Shuzhen Wang0129d522016-10-30 22:43:41 -0700212 sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
213 // Connect to the buffer producer
Shuzhen Wang0129d522016-10-30 22:43:41 -0700214 sp<OutputListener> listener(new OutputListener(this, gbp));
215 IInterface::asBinder(gbp)->linkToDeath(listener);
Emilian Peev40ead602017-09-26 15:46:36 +0100216 res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800217 if (res != NO_ERROR) {
218 SP_LOGE("addOutput: failed to connect (%d)", res);
219 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700220 }
221
222 // Query consumer side buffer count, and update overall buffer count
223 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800224 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700225 outputQueue.get(),
226 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800227 if (res != OK) {
228 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700229 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800230 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700231 }
232
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800233 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
234 maxConsumerBuffers, mMaxHalBuffers);
Emilian Peev18e3f372018-05-22 18:55:01 +0100235 // The output slot count requirement can change depending on the current amount
236 // of outputs and incoming buffer consumption rate. To avoid any issues with
237 // insufficient slots, set their count to the maximum supported. The output
238 // surface buffer allocation is disabled so no real buffers will get allocated.
239 size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800240 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700241 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800242 if (res != OK) {
243 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700244 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800245 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700246 }
247
248 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
249 // We need skip these cases as timeout will disable the non-blocking (async) mode.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100250 uint64_t usage = 0;
251 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700252 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700253 nsecs_t timeout = mUseHalBufManager ?
254 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
255 outputQueue->setDequeueTimeout(timeout);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700256 }
257
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800258 res = gbp->allowAllocation(false);
259 if (res != OK) {
260 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
261 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700262 }
263
264 // Add new entry into mOutputs
Emilian Peev40ead602017-09-26 15:46:36 +0100265 mOutputs[surfaceId] = gbp;
Emilian Peev2295df72021-11-12 18:14:10 -0800266 mOutputSurfaces[surfaceId] = outputQueue;
Emilian Peev40ead602017-09-26 15:46:36 +0100267 mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000268 if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
269 SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
270 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
271 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800272 mNotifiers[gbp] = listener;
273 mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
274
275 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700276 return NO_ERROR;
277}
278
Emilian Peev40ead602017-09-26 15:46:36 +0100279status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) {
280 ATRACE_CALL();
281 Mutex::Autolock lock(mMutex);
282
283 status_t res = removeOutputLocked(surfaceId);
284 if (res != OK) {
285 SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res);
286 return res;
287 }
288
Emilian Peevf130ad72018-10-11 11:03:07 +0100289 if (mAcquiredInputBuffers < mMaxConsumerBuffers) {
290 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
291 if (res != OK) {
292 SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res);
293 return res;
294 }
Emilian Peev40ead602017-09-26 15:46:36 +0100295 }
296
297 return res;
298}
299
300status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) {
301 if (mOutputs[surfaceId] == nullptr) {
302 SP_LOGE("%s: output surface is not present!", __FUNCTION__);
303 return BAD_VALUE;
304 }
305
306 sp<IGraphicBufferProducer> gbp = mOutputs[surfaceId];
307 //Search and decrement the ref. count of any buffers that are
308 //still attached to the removed surface.
309 std::vector<uint64_t> pendingBufferIds;
310 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev6f823722017-12-07 18:05:49 +0000311 for (size_t i = 0; i < outputSlots.size(); i++) {
312 if (outputSlots[i] != nullptr) {
313 pendingBufferIds.push_back(outputSlots[i]->getId());
314 auto rc = gbp->detachBuffer(i);
315 if (rc != NO_ERROR) {
316 //Buffers that fail to detach here will be scheduled for detach in the
317 //input buffer queue and the rest of the registered outputs instead.
318 //This will help ensure that camera stops accessing buffers that still
319 //can get referenced by the disconnected output.
320 mDetachedBuffers.emplace(outputSlots[i]->getId());
321 }
Emilian Peev40ead602017-09-26 15:46:36 +0100322 }
323 }
324 mOutputs[surfaceId] = nullptr;
Emilian Peev2295df72021-11-12 18:14:10 -0800325 mOutputSurfaces[surfaceId] = nullptr;
Emilian Peev40ead602017-09-26 15:46:36 +0100326 mOutputSlots[gbp] = nullptr;
327 for (const auto &id : pendingBufferIds) {
328 decrementBufRefCountLocked(id, surfaceId);
329 }
330
331 auto res = IInterface::asBinder(gbp)->unlinkToDeath(mNotifiers[gbp]);
332 if (res != OK) {
333 SP_LOGE("%s: Failed to unlink producer death listener: %d ", __FUNCTION__, res);
334 return res;
335 }
336
337 res = gbp->disconnect(NATIVE_WINDOW_API_CAMERA);
338 if (res != OK) {
339 SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res);
340 return res;
341 }
342
343 mNotifiers[gbp] = nullptr;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000344 mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100345 mConsumerBufferCount[surfaceId] = 0;
346
347 return res;
348}
349
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800350status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100351 const BufferItem& bufferItem, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800352 ATRACE_CALL();
353 status_t res;
354 IGraphicBufferProducer::QueueBufferInput queueInput(
355 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
356 bufferItem.mDataSpace, bufferItem.mCrop,
357 static_cast<int32_t>(bufferItem.mScalingMode),
358 bufferItem.mTransform, bufferItem.mFence);
359
360 IGraphicBufferProducer::QueueBufferOutput queueOutput;
361
362 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
363 const BufferTracker& tracker = *(mBuffers[bufferId]);
364 int slot = getSlotForOutputLocked(output, tracker.getBuffer());
365
Emilian Peev2295df72021-11-12 18:14:10 -0800366 if (mOutputSurfaces[surfaceId] != nullptr) {
367 sp<ANativeWindow> anw = mOutputSurfaces[surfaceId];
368 camera3::Camera3Stream::queueHDRMetadata(
369 bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile);
370 } else {
371 SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId);
372 }
373
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800374 // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
375 // queueBuffer (which will try to acquire the output lock), the output could be holding its
376 // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into
377 // circular lock situation.
378 mMutex.unlock();
379 res = output->queueBuffer(slot, queueInput, &queueOutput);
380 mMutex.lock();
381
382 SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d",
383 __FUNCTION__, output.get(), slot, res);
Emilian Peev40ead602017-09-26 15:46:36 +0100384 //During buffer queue 'mMutex' is not held which makes the removal of
385 //"output" possible. Check whether this is the case and return.
386 if (mOutputSlots[output] == nullptr) {
387 return res;
388 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800389 if (res != OK) {
390 if (res != NO_INIT && res != DEAD_OBJECT) {
391 SP_LOGE("Queuing buffer to output failed (%d)", res);
392 }
393 // If we just discovered that this output has been abandoned, note
394 // that, increment the release count so that we still release this
395 // buffer eventually, and move on to the next output
396 onAbandonedLocked();
Emilian Peev40ead602017-09-26 15:46:36 +0100397 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800398 return res;
399 }
400
401 // If the queued buffer replaces a pending buffer in the async
402 // queue, no onBufferReleased is called by the buffer queue.
403 // Proactively trigger the callback to avoid buffer loss.
404 if (queueOutput.bufferReplaced) {
Emilian Peev703e4992018-02-27 11:42:09 +0000405 onBufferReplacedLocked(output, surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800406 }
407
408 return res;
409}
410
Shuzhen Wang0129d522016-10-30 22:43:41 -0700411String8 Camera3StreamSplitter::getUniqueConsumerName() {
412 static volatile int32_t counter = 0;
413 return String8::format("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
414}
415
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800416status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700417 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800418
Shuzhen Wang0129d522016-10-30 22:43:41 -0700419 Mutex::Autolock lock(mMutex);
420
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800421 uint64_t bufferId = buffer->getId();
422 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
423 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700424
Emilian Peev40ead602017-09-26 15:46:36 +0100425 return OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800426}
427
428status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
429 const std::vector<size_t>& surface_ids) {
430 ATRACE_CALL();
431 status_t res = OK;
432
433 Mutex::Autolock lock(mMutex);
434
435 sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
436 uint64_t bufferId = gb->getId();
437
438 // Initialize buffer tracker for this input buffer
439 auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
440
441 for (auto& surface_id : surface_ids) {
442 sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id];
Emilian Peev40ead602017-09-26 15:46:36 +0100443 if (gbp.get() == nullptr) {
444 //Output surface got likely removed by client.
445 continue;
446 }
447 int slot = getSlotForOutputLocked(gbp, gb);
448 if (slot != BufferItem::INVALID_BUFFER_SLOT) {
449 //Buffer is already attached to this output surface.
450 continue;
451 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800452 //Temporarly Unlock the mutex when trying to attachBuffer to the output
453 //queue, because attachBuffer could block in case of a slow consumer. If
454 //we block while holding the lock, onFrameAvailable and onBufferReleased
455 //will block as well because they need to acquire the same lock.
456 mMutex.unlock();
457 res = gbp->attachBuffer(&slot, gb);
458 mMutex.lock();
459 if (res != OK) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700460 SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)",
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800461 __FUNCTION__, gbp.get(), strerror(-res), res);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700462 // TODO: might need to detach/cleanup the already attached buffers before return?
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800463 return res;
464 }
Emilian Peev21e79db2018-03-16 18:17:57 +0000465 if ((slot < 0) || (slot > BufferQueue::NUM_BUFFER_SLOTS)) {
466 SP_LOGE("%s: Slot received %d either bigger than expected maximum %d or negative!",
467 __FUNCTION__, slot, BufferQueue::NUM_BUFFER_SLOTS);
468 return BAD_VALUE;
469 }
Emilian Peev40ead602017-09-26 15:46:36 +0100470 //During buffer attach 'mMutex' is not held which makes the removal of
471 //"gbp" possible. Check whether this is the case and continue.
472 if (mOutputSlots[gbp] == nullptr) {
473 continue;
474 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800475 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev21e79db2018-03-16 18:17:57 +0000476 if (static_cast<size_t> (slot + 1) > outputSlots.size()) {
477 outputSlots.resize(slot + 1);
478 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800479 if (outputSlots[slot] != nullptr) {
480 // If the buffer is attached to a slot which already contains a buffer,
481 // the previous buffer will be removed from the output queue. Decrement
482 // the reference count accordingly.
Emilian Peev40ead602017-09-26 15:46:36 +0100483 decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800484 }
485 SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
486 slot, gbp.get());
487 outputSlots[slot] = gb;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700488 }
489
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800490 mBuffers[bufferId] = std::move(tracker);
491
492 return res;
493}
494
495void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
496 ATRACE_CALL();
497 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700498
499 // Acquire and detach the buffer from the input
500 BufferItem bufferItem;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800501 status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
502 if (res != NO_ERROR) {
503 SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
504 mOnFrameAvailableRes.store(res);
505 return;
506 }
Emilian Peev40ead602017-09-26 15:46:36 +0100507
508 uint64_t bufferId;
509 if (bufferItem.mGraphicBuffer != nullptr) {
510 mInputSlots[bufferItem.mSlot] = bufferItem;
511 } else if (bufferItem.mAcquireCalled) {
512 bufferItem.mGraphicBuffer = mInputSlots[bufferItem.mSlot].mGraphicBuffer;
513 mInputSlots[bufferItem.mSlot].mFrameNumber = bufferItem.mFrameNumber;
514 } else {
515 SP_LOGE("%s: Invalid input graphic buffer!", __FUNCTION__);
Yin-Chia Yehda208452019-08-26 15:35:57 -0700516 mOnFrameAvailableRes.store(BAD_VALUE);
Emilian Peev40ead602017-09-26 15:46:36 +0100517 return;
518 }
519 bufferId = bufferItem.mGraphicBuffer->getId();
520
521 if (mBuffers.find(bufferId) == mBuffers.end()) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800522 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
523 __FUNCTION__);
524 mOnFrameAvailableRes.store(INVALID_OPERATION);
525 return;
526 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700527
Emilian Peevf130ad72018-10-11 11:03:07 +0100528 mAcquiredInputBuffers++;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800529 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
530 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700531
Emilian Peev3bdcdff2018-06-25 14:37:29 +0100532 if (bufferItem.mTransformToDisplayInverse) {
533 bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
534 }
535
Shuzhen Wang0129d522016-10-30 22:43:41 -0700536 // Attach and queue the buffer to each of the outputs
Emilian Peev40ead602017-09-26 15:46:36 +0100537 BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700538
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800539 SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
540 __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
541 for (const auto id : tracker.requestedSurfaces()) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700542
Emilian Peev40ead602017-09-26 15:46:36 +0100543 if (mOutputs[id] == nullptr) {
544 //Output surface got likely removed by client.
545 continue;
546 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700547
Emilian Peev40ead602017-09-26 15:46:36 +0100548 res = outputBufferLocked(mOutputs[id], bufferItem, id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800549 if (res != OK) {
550 SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
551 mOnFrameAvailableRes.store(res);
552 // If we fail to send buffer to certain output, keep sending to
553 // other outputs.
554 continue;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700555 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800556 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700557
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800558 mOnFrameAvailableRes.store(res);
559}
560
Yin-Chia Yehda208452019-08-26 15:35:57 -0700561void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
562 ATRACE_CALL();
563 onFrameAvailable(item);
564}
565
Emilian Peev40ead602017-09-26 15:46:36 +0100566void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800567 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800568
Emilian Peev40ead602017-09-26 15:46:36 +0100569 if (mBuffers[id] == nullptr) {
570 return;
571 }
572
573 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800574 if (referenceCount > 0) {
575 return;
576 }
577
578 // We no longer need to track the buffer now that it is being returned to the
579 // input. Note that this should happen before we unlock the mutex and call
580 // releaseBuffer, to avoid the case where the same bufferId is acquired in
581 // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
582 // overwrites the current one.
583 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
584 mBuffers.erase(id);
585
Emilian Peev40ead602017-09-26 15:46:36 +0100586 uint64_t bufferId = tracker_ptr->getBuffer()->getId();
587 int consumerSlot = -1;
588 uint64_t frameNumber;
Emilian Peev6f823722017-12-07 18:05:49 +0000589 auto inputSlot = mInputSlots.begin();
590 for (; inputSlot != mInputSlots.end(); inputSlot++) {
591 if (inputSlot->second.mGraphicBuffer->getId() == bufferId) {
592 consumerSlot = inputSlot->second.mSlot;
593 frameNumber = inputSlot->second.mFrameNumber;
Emilian Peev40ead602017-09-26 15:46:36 +0100594 break;
595 }
596 }
597 if (consumerSlot == -1) {
598 SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800599 return;
600 }
601
Emilian Peev6f823722017-12-07 18:05:49 +0000602 auto detachBuffer = mDetachedBuffers.find(bufferId);
603 bool detach = (detachBuffer != mDetachedBuffers.end());
604 if (detach) {
605 mDetachedBuffers.erase(detachBuffer);
606 mInputSlots.erase(inputSlot);
607 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800608 // Temporarily unlock mutex to avoid circular lock:
609 // 1. This function holds splitter lock, calls releaseBuffer which triggers
610 // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
611 // OutputStream lock
612 // 2. Camera3SharedOutputStream::getBufferLocked calls
613 // attachBufferToOutputs, which holds the stream lock, and waits for the
614 // splitter lock.
615 sp<IGraphicBufferConsumer> consumer(mConsumer);
616 mMutex.unlock();
Emilian Peev40ead602017-09-26 15:46:36 +0100617 int res = NO_ERROR;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800618 if (consumer != nullptr) {
Emilian Peev6f823722017-12-07 18:05:49 +0000619 if (detach) {
620 res = consumer->detachBuffer(consumerSlot);
621 } else {
622 res = consumer->releaseBuffer(consumerSlot, frameNumber,
623 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence());
624 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800625 } else {
626 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
627 }
628 mMutex.lock();
Emilian Peev6f823722017-12-07 18:05:49 +0000629
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800630 if (res != NO_ERROR) {
Emilian Peev6f823722017-12-07 18:05:49 +0000631 if (detach) {
632 SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
633 } else {
634 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
635 }
Emilian Peevf130ad72018-10-11 11:03:07 +0100636 } else {
637 if (mAcquiredInputBuffers == 0) {
638 ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
639 } else {
640 mAcquiredInputBuffers--;
641 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700642 }
643}
644
645void Camera3StreamSplitter::onBufferReleasedByOutput(
646 const sp<IGraphicBufferProducer>& from) {
647 ATRACE_CALL();
Emilian Peev703e4992018-02-27 11:42:09 +0000648 sp<Fence> fence;
649
650 int slot = BufferItem::INVALID_BUFFER_SLOT;
651 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
652 nullptr, nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700653 Mutex::Autolock lock(mMutex);
Emilian Peev703e4992018-02-27 11:42:09 +0000654 handleOutputDequeueStatusLocked(res, slot);
655 if (res != OK) {
656 return;
657 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700658
Emilian Peev40ead602017-09-26 15:46:36 +0100659 size_t surfaceId = 0;
660 bool found = false;
661 for (const auto& it : mOutputs) {
662 if (it.second == from) {
663 found = true;
664 surfaceId = it.first;
665 break;
666 }
667 }
668 if (!found) {
669 SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__);
670 return;
671 }
672
Emilian Peev703e4992018-02-27 11:42:09 +0000673 returnOutputBufferLocked(fence, from, surfaceId, slot);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800674}
675
Emilian Peev703e4992018-02-27 11:42:09 +0000676void Camera3StreamSplitter::onBufferReplacedLocked(
Emilian Peev40ead602017-09-26 15:46:36 +0100677 const sp<IGraphicBufferProducer>& from, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800678 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700679 sp<Fence> fence;
Emilian Peev40ead602017-09-26 15:46:36 +0100680
681 int slot = BufferItem::INVALID_BUFFER_SLOT;
682 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
683 nullptr, nullptr);
Emilian Peev703e4992018-02-27 11:42:09 +0000684 handleOutputDequeueStatusLocked(res, slot);
685 if (res != OK) {
Shuzhen Wangafa8a912017-03-15 10:51:27 -0700686 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700687 }
688
Emilian Peev703e4992018-02-27 11:42:09 +0000689 returnOutputBufferLocked(fence, from, surfaceId, slot);
690}
691
692void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence,
693 const sp<IGraphicBufferProducer>& from, size_t surfaceId, int slot) {
694 sp<GraphicBuffer> buffer;
695
696 if (mOutputSlots[from] == nullptr) {
697 //Output surface got likely removed by client.
698 return;
699 }
700
701 auto outputSlots = *mOutputSlots[from];
702 buffer = outputSlots[slot];
Shuzhen Wang0129d522016-10-30 22:43:41 -0700703 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700704 // Merge the release fence of the incoming buffer so that the fence we send
705 // back to the input includes all of the outputs' fences
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800706 if (fence != nullptr && fence->isValid()) {
707 tracker.mergeFence(fence);
708 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700709
Emilian Peev6f823722017-12-07 18:05:49 +0000710 auto detachBuffer = mDetachedBuffers.find(buffer->getId());
711 bool detach = (detachBuffer != mDetachedBuffers.end());
712 if (detach) {
Emilian Peev703e4992018-02-27 11:42:09 +0000713 auto res = from->detachBuffer(slot);
Emilian Peev6f823722017-12-07 18:05:49 +0000714 if (res == NO_ERROR) {
715 outputSlots[slot] = nullptr;
716 } else {
717 SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res);
718 }
719 }
720
Shuzhen Wang0129d522016-10-30 22:43:41 -0700721 // Check to see if this is the last outstanding reference to this buffer
Emilian Peev40ead602017-09-26 15:46:36 +0100722 decrementBufRefCountLocked(buffer->getId(), surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700723}
724
Emilian Peev703e4992018-02-27 11:42:09 +0000725void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res, int slot) {
726 if (res == NO_INIT) {
727 // If we just discovered that this output has been abandoned, note that,
728 // but we can't do anything else, since buffer is invalid
729 onAbandonedLocked();
730 } else if (res == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
731 SP_LOGE("%s: Producer needs to re-allocate buffer!", __FUNCTION__);
732 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
733 } else if (res == IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
734 SP_LOGE("%s: All slot->buffer mapping should be released!", __FUNCTION__);
735 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
736 } else if (res == NO_MEMORY) {
737 SP_LOGE("%s: No free buffers", __FUNCTION__);
738 } else if (res == WOULD_BLOCK) {
739 SP_LOGE("%s: Dequeue call will block", __FUNCTION__);
740 } else if (res != OK || (slot == BufferItem::INVALID_BUFFER_SLOT)) {
741 SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res);
742 }
743}
744
Shuzhen Wang0129d522016-10-30 22:43:41 -0700745void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800746 // If this is called from binderDied callback, it means the app process
747 // holding the binder has died. CameraService will be notified of the binder
748 // death, and camera device will be closed, which in turn calls
749 // disconnect().
750 //
751 // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
752 // consumer being abanoned shouldn't impact the other consumer. So we won't
753 // stop the buffer flow.
754 //
755 // In both cases, we don't need to do anything here.
756 SP_LOGV("One of my outputs has abandoned me");
757}
758
759int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
760 const sp<GraphicBuffer>& gb) {
761 auto& outputSlots = *mOutputSlots[gbp];
762
763 for (size_t i = 0; i < outputSlots.size(); i++) {
764 if (outputSlots[i] == gb) {
765 return (int)i;
766 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700767 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800768
Emilian Peev40ead602017-09-26 15:46:36 +0100769 SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800770 gbp.get());
771 return BufferItem::INVALID_BUFFER_SLOT;
772}
773
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774Camera3StreamSplitter::OutputListener::OutputListener(
775 wp<Camera3StreamSplitter> splitter,
776 wp<IGraphicBufferProducer> output)
777 : mSplitter(splitter), mOutput(output) {}
778
779void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800780 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700781 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
782 sp<IGraphicBufferProducer> output = mOutput.promote();
783 if (splitter != nullptr && output != nullptr) {
784 splitter->onBufferReleasedByOutput(output);
785 }
786}
787
788void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
789 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
790 if (splitter != nullptr) {
791 Mutex::Autolock lock(splitter->mMutex);
792 splitter->onAbandonedLocked();
793 }
794}
795
796Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800797 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
798 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
799 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700800
801void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
802 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
803}
804
Emilian Peev40ead602017-09-26 15:46:36 +0100805size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) {
806 const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId);
807 if (it == mRequestedSurfaces.end()) {
808 return mReferenceCount;
809 } else {
810 mRequestedSurfaces.erase(it);
811 }
812
Shuzhen Wang0129d522016-10-30 22:43:41 -0700813 if (mReferenceCount > 0)
814 --mReferenceCount;
815 return mReferenceCount;
816}
817
818} // namespace android