blob: 77c037a3c558200a9bc1e19298fdd8baf68ce628 [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
Jim Shargo2e0202f2024-07-30 19:54:43 +000023#include <camera/StringUtils.h>
24#include <com_android_graphics_libgui_flags.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070025#include <gui/BufferItem.h>
Jim Shargo2e0202f2024-07-30 19:54:43 +000026#include <gui/BufferQueue.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070027#include <gui/IGraphicBufferConsumer.h>
28#include <gui/IGraphicBufferProducer.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070029#include <gui/Surface.h>
30
31#include <ui/GraphicBuffer.h>
32
33#include <binder/ProcessState.h>
34
35#include <utils/Trace.h>
36
Mathias Agopian05d19b02017-02-28 16:28:19 -080037#include <cutils/atomic.h>
38
Emilian Peev2295df72021-11-12 18:14:10 -080039#include "Camera3Stream.h"
40
Shuzhen Wang0129d522016-10-30 22:43:41 -070041#include "Camera3StreamSplitter.h"
42
43namespace android {
44
Emilian Peev40ead602017-09-26 15:46:36 +010045status_t Camera3StreamSplitter::connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
46 uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
Emilian Peev2295df72021-11-12 18:14:10 -080047 uint32_t height, android::PixelFormat format, sp<Surface>* consumer,
Emilian Peevc81a7592022-02-14 17:38:18 -080048 int64_t dynamicRangeProfile) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080049 ATRACE_CALL();
50 if (consumer == nullptr) {
51 SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070052 return BAD_VALUE;
53 }
54
55 Mutex::Autolock lock(mMutex);
56 status_t res = OK;
57
58 if (mOutputs.size() > 0 || mConsumer != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080059 SP_LOGE("%s: already connected", __FUNCTION__);
60 return BAD_VALUE;
61 }
62 if (mBuffers.size() > 0) {
63 SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size());
Shuzhen Wang0129d522016-10-30 22:43:41 -070064 return BAD_VALUE;
65 }
66
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080067 mMaxHalBuffers = halMaxBuffers;
68 mConsumerName = getUniqueConsumerName();
Emilian Peev2295df72021-11-12 18:14:10 -080069 mDynamicRangeProfile = dynamicRangeProfile;
Shuzhen Wang0129d522016-10-30 22:43:41 -070070 // Add output surfaces. This has to be before creating internal buffer queue
71 // in order to get max consumer side buffers.
Emilian Peev40ead602017-09-26 15:46:36 +010072 for (auto &it : surfaces) {
73 if (it.second == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080074 SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
Shuzhen Wang758c2152017-01-10 18:26:18 -080075 return BAD_VALUE;
76 }
Emilian Peev40ead602017-09-26 15:46:36 +010077 res = addOutputLocked(it.first, it.second);
Shuzhen Wang758c2152017-01-10 18:26:18 -080078 if (res != OK) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080079 SP_LOGE("%s: Failed to add output surface: %s(%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -080080 __FUNCTION__, strerror(-res), res);
81 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -070082 }
83 }
84
Jim Shargo2e0202f2024-07-30 19:54:43 +000085#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080086 // Create BufferQueue for input
Shuzhen Wang0129d522016-10-30 22:43:41 -070087 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Jim Shargo2e0202f2024-07-30 19:54:43 +000088#endif // !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Shuzhen Wang0129d522016-10-30 22:43:41 -070089
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080090 // Allocate 1 extra buffer to handle the case where all buffers are detached
91 // from input, and attached to the outputs. In this case, the input queue's
92 // dequeueBuffer can still allocate 1 extra buffer before being blocked by
93 // the output's attachBuffer().
Emilian Peevf130ad72018-10-11 11:03:07 +010094 mMaxConsumerBuffers++;
Jim Shargo2e0202f2024-07-30 19:54:43 +000095#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
96 mBufferItemConsumer = new BufferItemConsumer(consumerUsage, mMaxConsumerBuffers);
97#else
Emilian Peevf130ad72018-10-11 11:03:07 +010098 mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage, mMaxConsumerBuffers);
Jim Shargo2e0202f2024-07-30 19:54:43 +000099#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Shuzhen Wang0129d522016-10-30 22:43:41 -0700100 if (mBufferItemConsumer == nullptr) {
101 return NO_MEMORY;
102 }
Jim Shargo2e0202f2024-07-30 19:54:43 +0000103#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
104 mProducer = mBufferItemConsumer->getSurface()->getIGraphicBufferProducer();
105 mConsumer = mBufferItemConsumer->getIGraphicBufferConsumer();
106#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Austin Borger1c1bee02023-06-01 16:51:35 -0700107 mConsumer->setConsumerName(toString8(mConsumerName));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700108
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800109 *consumer = new Surface(mProducer);
110 if (*consumer == nullptr) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700111 return NO_MEMORY;
112 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700113
Emilian Peev40ead602017-09-26 15:46:36 +0100114 res = mProducer->setAsyncMode(true);
115 if (res != OK) {
116 SP_LOGE("%s: Failed to enable input queue async mode: %s(%d)", __FUNCTION__,
117 strerror(-res), res);
118 return res;
119 }
120
Shuzhen Wang0129d522016-10-30 22:43:41 -0700121 res = mConsumer->consumerConnect(this, /* controlledByApp */ false);
122
Emilian Peev40ead602017-09-26 15:46:36 +0100123 mWidth = width;
124 mHeight = height;
125 mFormat = format;
126 mProducerUsage = producerUsage;
Emilian Peevf130ad72018-10-11 11:03:07 +0100127 mAcquiredInputBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100128
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800129 SP_LOGV("%s: connected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700130 return res;
131}
132
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800133status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
134 ATRACE_CALL();
135 return mOnFrameAvailableRes.load();
136}
137
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138void Camera3StreamSplitter::disconnect() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800139 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700140 Mutex::Autolock lock(mMutex);
141
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800142 for (auto& notifier : mNotifiers) {
143 sp<IGraphicBufferProducer> producer = notifier.first;
144 sp<OutputListener> listener = notifier.second;
145 IInterface::asBinder(producer)->unlinkToDeath(listener);
146 }
147 mNotifiers.clear();
148
Shuzhen Wang0129d522016-10-30 22:43:41 -0700149 for (auto& output : mOutputs) {
Emilian Peev40ead602017-09-26 15:46:36 +0100150 if (output.second != nullptr) {
151 output.second->disconnect(NATIVE_WINDOW_API_CAMERA);
152 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700153 }
154 mOutputs.clear();
Emilian Peev2295df72021-11-12 18:14:10 -0800155 mOutputSurfaces.clear();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800156 mOutputSlots.clear();
Emilian Peev40ead602017-09-26 15:46:36 +0100157 mConsumerBufferCount.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700158
Emilian Peev359adca2019-10-30 10:12:46 -0700159 if (mConsumer.get() != nullptr) {
160 mConsumer->consumerDisconnect();
161 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700162
163 if (mBuffers.size() > 0) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800164 SP_LOGW("%zu buffers still being tracked", mBuffers.size());
165 mBuffers.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700166 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800167
168 mMaxHalBuffers = 0;
169 mMaxConsumerBuffers = 0;
Emilian Peevf130ad72018-10-11 11:03:07 +0100170 mAcquiredInputBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800171 SP_LOGV("%s: Disconnected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700172}
173
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700174Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) :
175 mUseHalBufManager(useHalBufManager) {}
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800176
Shuzhen Wang0129d522016-10-30 22:43:41 -0700177Camera3StreamSplitter::~Camera3StreamSplitter() {
178 disconnect();
179}
180
Emilian Peev40ead602017-09-26 15:46:36 +0100181status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800182 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700183 Mutex::Autolock lock(mMutex);
Emilian Peev40ead602017-09-26 15:46:36 +0100184 status_t res = addOutputLocked(surfaceId, outputQueue);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800185
186 if (res != OK) {
187 SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
188 return res;
189 }
190
Emilian Peevf130ad72018-10-11 11:03:07 +0100191 if (mMaxConsumerBuffers > mAcquiredInputBuffers) {
192 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
193 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800194
195 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700196}
197
Jayant Chowdhary9d3a6492023-11-22 07:23:59 +0000198void Camera3StreamSplitter::setHalBufferManager(bool enabled) {
199 Mutex::Autolock lock(mMutex);
200 mUseHalBufManager = enabled;
201}
202
Emilian Peev40ead602017-09-26 15:46:36 +0100203status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800204 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700205 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800206 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700207 return BAD_VALUE;
208 }
209
Emilian Peev40ead602017-09-26 15:46:36 +0100210 if (mOutputs[surfaceId] != nullptr) {
211 SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
212 return BAD_VALUE;
213 }
214
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700215 status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
Emilian Peev40ead602017-09-26 15:46:36 +0100216 mWidth, mHeight);
217 if (res != NO_ERROR) {
218 SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
219 return res;
220 }
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700221 res = native_window_set_buffers_format(outputQueue.get(),
222 mFormat);
223 if (res != OK) {
224 ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu",
225 __FUNCTION__, mFormat, surfaceId);
226 return res;
227 }
Emilian Peev40ead602017-09-26 15:46:36 +0100228
Shuzhen Wang0129d522016-10-30 22:43:41 -0700229 sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer();
230 // Connect to the buffer producer
Shuzhen Wang0129d522016-10-30 22:43:41 -0700231 sp<OutputListener> listener(new OutputListener(this, gbp));
232 IInterface::asBinder(gbp)->linkToDeath(listener);
Emilian Peev40ead602017-09-26 15:46:36 +0100233 res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800234 if (res != NO_ERROR) {
235 SP_LOGE("addOutput: failed to connect (%d)", res);
236 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700237 }
238
239 // Query consumer side buffer count, and update overall buffer count
240 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800241 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700242 outputQueue.get(),
243 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800244 if (res != OK) {
245 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700246 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800247 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700248 }
249
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800250 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
251 maxConsumerBuffers, mMaxHalBuffers);
Emilian Peev18e3f372018-05-22 18:55:01 +0100252 // The output slot count requirement can change depending on the current amount
253 // of outputs and incoming buffer consumption rate. To avoid any issues with
254 // insufficient slots, set their count to the maximum supported. The output
255 // surface buffer allocation is disabled so no real buffers will get allocated.
256 size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800257 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700258 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800259 if (res != OK) {
260 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700261 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800262 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700263 }
264
265 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
266 // We need skip these cases as timeout will disable the non-blocking (async) mode.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100267 uint64_t usage = 0;
268 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700269 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700270 nsecs_t timeout = mUseHalBufManager ?
271 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
272 outputQueue->setDequeueTimeout(timeout);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700273 }
274
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800275 res = gbp->allowAllocation(false);
276 if (res != OK) {
277 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
278 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700279 }
280
281 // Add new entry into mOutputs
Emilian Peev40ead602017-09-26 15:46:36 +0100282 mOutputs[surfaceId] = gbp;
Emilian Peev2295df72021-11-12 18:14:10 -0800283 mOutputSurfaces[surfaceId] = outputQueue;
Emilian Peev40ead602017-09-26 15:46:36 +0100284 mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000285 if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
286 SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
287 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
288 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800289 mNotifiers[gbp] = listener;
290 mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount);
291
292 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700293 return NO_ERROR;
294}
295
Emilian Peev40ead602017-09-26 15:46:36 +0100296status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) {
297 ATRACE_CALL();
298 Mutex::Autolock lock(mMutex);
299
300 status_t res = removeOutputLocked(surfaceId);
301 if (res != OK) {
302 SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res);
303 return res;
304 }
305
Emilian Peevf130ad72018-10-11 11:03:07 +0100306 if (mAcquiredInputBuffers < mMaxConsumerBuffers) {
307 res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
308 if (res != OK) {
309 SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res);
310 return res;
311 }
Emilian Peev40ead602017-09-26 15:46:36 +0100312 }
313
314 return res;
315}
316
317status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) {
318 if (mOutputs[surfaceId] == nullptr) {
319 SP_LOGE("%s: output surface is not present!", __FUNCTION__);
320 return BAD_VALUE;
321 }
322
323 sp<IGraphicBufferProducer> gbp = mOutputs[surfaceId];
324 //Search and decrement the ref. count of any buffers that are
325 //still attached to the removed surface.
326 std::vector<uint64_t> pendingBufferIds;
327 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev6f823722017-12-07 18:05:49 +0000328 for (size_t i = 0; i < outputSlots.size(); i++) {
329 if (outputSlots[i] != nullptr) {
330 pendingBufferIds.push_back(outputSlots[i]->getId());
331 auto rc = gbp->detachBuffer(i);
332 if (rc != NO_ERROR) {
333 //Buffers that fail to detach here will be scheduled for detach in the
334 //input buffer queue and the rest of the registered outputs instead.
335 //This will help ensure that camera stops accessing buffers that still
336 //can get referenced by the disconnected output.
337 mDetachedBuffers.emplace(outputSlots[i]->getId());
338 }
Emilian Peev40ead602017-09-26 15:46:36 +0100339 }
340 }
341 mOutputs[surfaceId] = nullptr;
Emilian Peev2295df72021-11-12 18:14:10 -0800342 mOutputSurfaces[surfaceId] = nullptr;
Emilian Peev40ead602017-09-26 15:46:36 +0100343 mOutputSlots[gbp] = nullptr;
344 for (const auto &id : pendingBufferIds) {
345 decrementBufRefCountLocked(id, surfaceId);
346 }
347
348 auto res = IInterface::asBinder(gbp)->unlinkToDeath(mNotifiers[gbp]);
349 if (res != OK) {
350 SP_LOGE("%s: Failed to unlink producer death listener: %d ", __FUNCTION__, res);
351 return res;
352 }
353
354 res = gbp->disconnect(NATIVE_WINDOW_API_CAMERA);
355 if (res != OK) {
356 SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res);
357 return res;
358 }
359
360 mNotifiers[gbp] = nullptr;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000361 mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100362 mConsumerBufferCount[surfaceId] = 0;
363
364 return res;
365}
366
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800367status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100368 const BufferItem& bufferItem, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800369 ATRACE_CALL();
370 status_t res;
371 IGraphicBufferProducer::QueueBufferInput queueInput(
372 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp,
373 bufferItem.mDataSpace, bufferItem.mCrop,
374 static_cast<int32_t>(bufferItem.mScalingMode),
375 bufferItem.mTransform, bufferItem.mFence);
376
377 IGraphicBufferProducer::QueueBufferOutput queueOutput;
378
379 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
380 const BufferTracker& tracker = *(mBuffers[bufferId]);
381 int slot = getSlotForOutputLocked(output, tracker.getBuffer());
382
Emilian Peev2295df72021-11-12 18:14:10 -0800383 if (mOutputSurfaces[surfaceId] != nullptr) {
384 sp<ANativeWindow> anw = mOutputSurfaces[surfaceId];
385 camera3::Camera3Stream::queueHDRMetadata(
386 bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile);
387 } else {
388 SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId);
389 }
390
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800391 // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
392 // queueBuffer (which will try to acquire the output lock), the output could be holding its
393 // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into
394 // circular lock situation.
395 mMutex.unlock();
396 res = output->queueBuffer(slot, queueInput, &queueOutput);
397 mMutex.lock();
398
399 SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d",
400 __FUNCTION__, output.get(), slot, res);
Emilian Peev40ead602017-09-26 15:46:36 +0100401 //During buffer queue 'mMutex' is not held which makes the removal of
402 //"output" possible. Check whether this is the case and return.
403 if (mOutputSlots[output] == nullptr) {
404 return res;
405 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800406 if (res != OK) {
407 if (res != NO_INIT && res != DEAD_OBJECT) {
408 SP_LOGE("Queuing buffer to output failed (%d)", res);
409 }
410 // If we just discovered that this output has been abandoned, note
411 // that, increment the release count so that we still release this
412 // buffer eventually, and move on to the next output
413 onAbandonedLocked();
Emilian Peev40ead602017-09-26 15:46:36 +0100414 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800415 return res;
416 }
417
418 // If the queued buffer replaces a pending buffer in the async
419 // queue, no onBufferReleased is called by the buffer queue.
420 // Proactively trigger the callback to avoid buffer loss.
421 if (queueOutput.bufferReplaced) {
Emilian Peev703e4992018-02-27 11:42:09 +0000422 onBufferReplacedLocked(output, surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800423 }
424
425 return res;
426}
427
Austin Borger1c1bee02023-06-01 16:51:35 -0700428std::string Camera3StreamSplitter::getUniqueConsumerName() {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700429 static volatile int32_t counter = 0;
Austin Borger1c1bee02023-06-01 16:51:35 -0700430 return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700431}
432
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800433status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700434 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800435
Shuzhen Wang0129d522016-10-30 22:43:41 -0700436 Mutex::Autolock lock(mMutex);
437
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800438 uint64_t bufferId = buffer->getId();
439 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
440 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700441
Emilian Peev40ead602017-09-26 15:46:36 +0100442 return OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800443}
444
445status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
446 const std::vector<size_t>& surface_ids) {
447 ATRACE_CALL();
448 status_t res = OK;
449
450 Mutex::Autolock lock(mMutex);
451
452 sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
453 uint64_t bufferId = gb->getId();
454
455 // Initialize buffer tracker for this input buffer
456 auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
457
458 for (auto& surface_id : surface_ids) {
459 sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id];
Emilian Peev40ead602017-09-26 15:46:36 +0100460 if (gbp.get() == nullptr) {
461 //Output surface got likely removed by client.
462 continue;
463 }
464 int slot = getSlotForOutputLocked(gbp, gb);
465 if (slot != BufferItem::INVALID_BUFFER_SLOT) {
466 //Buffer is already attached to this output surface.
467 continue;
468 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800469 //Temporarly Unlock the mutex when trying to attachBuffer to the output
470 //queue, because attachBuffer could block in case of a slow consumer. If
471 //we block while holding the lock, onFrameAvailable and onBufferReleased
472 //will block as well because they need to acquire the same lock.
473 mMutex.unlock();
474 res = gbp->attachBuffer(&slot, gb);
475 mMutex.lock();
476 if (res != OK) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700477 SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)",
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800478 __FUNCTION__, gbp.get(), strerror(-res), res);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700479 // TODO: might need to detach/cleanup the already attached buffers before return?
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800480 return res;
481 }
Emilian Peev21e79db2018-03-16 18:17:57 +0000482 if ((slot < 0) || (slot > BufferQueue::NUM_BUFFER_SLOTS)) {
483 SP_LOGE("%s: Slot received %d either bigger than expected maximum %d or negative!",
484 __FUNCTION__, slot, BufferQueue::NUM_BUFFER_SLOTS);
485 return BAD_VALUE;
486 }
Emilian Peev40ead602017-09-26 15:46:36 +0100487 //During buffer attach 'mMutex' is not held which makes the removal of
488 //"gbp" possible. Check whether this is the case and continue.
489 if (mOutputSlots[gbp] == nullptr) {
490 continue;
491 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800492 auto& outputSlots = *mOutputSlots[gbp];
Emilian Peev21e79db2018-03-16 18:17:57 +0000493 if (static_cast<size_t> (slot + 1) > outputSlots.size()) {
494 outputSlots.resize(slot + 1);
495 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800496 if (outputSlots[slot] != nullptr) {
497 // If the buffer is attached to a slot which already contains a buffer,
498 // the previous buffer will be removed from the output queue. Decrement
499 // the reference count accordingly.
Emilian Peev40ead602017-09-26 15:46:36 +0100500 decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800501 }
502 SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(),
503 slot, gbp.get());
504 outputSlots[slot] = gb;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700505 }
506
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800507 mBuffers[bufferId] = std::move(tracker);
508
509 return res;
510}
511
512void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
513 ATRACE_CALL();
514 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700515
516 // Acquire and detach the buffer from the input
517 BufferItem bufferItem;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800518 status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
519 if (res != NO_ERROR) {
520 SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
521 mOnFrameAvailableRes.store(res);
522 return;
523 }
Emilian Peev40ead602017-09-26 15:46:36 +0100524
525 uint64_t bufferId;
526 if (bufferItem.mGraphicBuffer != nullptr) {
527 mInputSlots[bufferItem.mSlot] = bufferItem;
528 } else if (bufferItem.mAcquireCalled) {
529 bufferItem.mGraphicBuffer = mInputSlots[bufferItem.mSlot].mGraphicBuffer;
530 mInputSlots[bufferItem.mSlot].mFrameNumber = bufferItem.mFrameNumber;
531 } else {
532 SP_LOGE("%s: Invalid input graphic buffer!", __FUNCTION__);
Yin-Chia Yehda208452019-08-26 15:35:57 -0700533 mOnFrameAvailableRes.store(BAD_VALUE);
Emilian Peev40ead602017-09-26 15:46:36 +0100534 return;
535 }
536 bufferId = bufferItem.mGraphicBuffer->getId();
537
538 if (mBuffers.find(bufferId) == mBuffers.end()) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800539 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
540 __FUNCTION__);
541 mOnFrameAvailableRes.store(INVALID_OPERATION);
542 return;
543 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700544
Emilian Peevf130ad72018-10-11 11:03:07 +0100545 mAcquiredInputBuffers++;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800546 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
547 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700548
Emilian Peev3bdcdff2018-06-25 14:37:29 +0100549 if (bufferItem.mTransformToDisplayInverse) {
550 bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
551 }
552
Shuzhen Wang0129d522016-10-30 22:43:41 -0700553 // Attach and queue the buffer to each of the outputs
Emilian Peev40ead602017-09-26 15:46:36 +0100554 BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700555
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800556 SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
557 __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
558 for (const auto id : tracker.requestedSurfaces()) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700559
Emilian Peev40ead602017-09-26 15:46:36 +0100560 if (mOutputs[id] == nullptr) {
561 //Output surface got likely removed by client.
562 continue;
563 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700564
Emilian Peev40ead602017-09-26 15:46:36 +0100565 res = outputBufferLocked(mOutputs[id], bufferItem, id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800566 if (res != OK) {
567 SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
568 mOnFrameAvailableRes.store(res);
569 // If we fail to send buffer to certain output, keep sending to
570 // other outputs.
571 continue;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700572 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800573 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700574
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800575 mOnFrameAvailableRes.store(res);
576}
577
Yin-Chia Yehda208452019-08-26 15:35:57 -0700578void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
579 ATRACE_CALL();
580 onFrameAvailable(item);
581}
582
Emilian Peev40ead602017-09-26 15:46:36 +0100583void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800584 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800585
Emilian Peev40ead602017-09-26 15:46:36 +0100586 if (mBuffers[id] == nullptr) {
587 return;
588 }
589
590 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800591 if (referenceCount > 0) {
592 return;
593 }
594
595 // We no longer need to track the buffer now that it is being returned to the
596 // input. Note that this should happen before we unlock the mutex and call
597 // releaseBuffer, to avoid the case where the same bufferId is acquired in
598 // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
599 // overwrites the current one.
600 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
601 mBuffers.erase(id);
602
Emilian Peev40ead602017-09-26 15:46:36 +0100603 uint64_t bufferId = tracker_ptr->getBuffer()->getId();
604 int consumerSlot = -1;
605 uint64_t frameNumber;
Emilian Peev6f823722017-12-07 18:05:49 +0000606 auto inputSlot = mInputSlots.begin();
607 for (; inputSlot != mInputSlots.end(); inputSlot++) {
608 if (inputSlot->second.mGraphicBuffer->getId() == bufferId) {
609 consumerSlot = inputSlot->second.mSlot;
610 frameNumber = inputSlot->second.mFrameNumber;
Emilian Peev40ead602017-09-26 15:46:36 +0100611 break;
612 }
613 }
614 if (consumerSlot == -1) {
615 SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800616 return;
617 }
618
Emilian Peev6f823722017-12-07 18:05:49 +0000619 auto detachBuffer = mDetachedBuffers.find(bufferId);
620 bool detach = (detachBuffer != mDetachedBuffers.end());
621 if (detach) {
622 mDetachedBuffers.erase(detachBuffer);
623 mInputSlots.erase(inputSlot);
624 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800625 // Temporarily unlock mutex to avoid circular lock:
626 // 1. This function holds splitter lock, calls releaseBuffer which triggers
627 // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
628 // OutputStream lock
629 // 2. Camera3SharedOutputStream::getBufferLocked calls
630 // attachBufferToOutputs, which holds the stream lock, and waits for the
631 // splitter lock.
632 sp<IGraphicBufferConsumer> consumer(mConsumer);
633 mMutex.unlock();
Emilian Peev40ead602017-09-26 15:46:36 +0100634 int res = NO_ERROR;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800635 if (consumer != nullptr) {
Emilian Peev6f823722017-12-07 18:05:49 +0000636 if (detach) {
637 res = consumer->detachBuffer(consumerSlot);
638 } else {
639 res = consumer->releaseBuffer(consumerSlot, frameNumber,
640 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence());
641 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800642 } else {
643 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
644 }
645 mMutex.lock();
Emilian Peev6f823722017-12-07 18:05:49 +0000646
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800647 if (res != NO_ERROR) {
Emilian Peev6f823722017-12-07 18:05:49 +0000648 if (detach) {
649 SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
650 } else {
651 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
652 }
Emilian Peevf130ad72018-10-11 11:03:07 +0100653 } else {
654 if (mAcquiredInputBuffers == 0) {
655 ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
656 } else {
657 mAcquiredInputBuffers--;
658 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700659 }
660}
661
662void Camera3StreamSplitter::onBufferReleasedByOutput(
663 const sp<IGraphicBufferProducer>& from) {
664 ATRACE_CALL();
Emilian Peev703e4992018-02-27 11:42:09 +0000665 sp<Fence> fence;
666
667 int slot = BufferItem::INVALID_BUFFER_SLOT;
668 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
669 nullptr, nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700670 Mutex::Autolock lock(mMutex);
Emilian Peev703e4992018-02-27 11:42:09 +0000671 handleOutputDequeueStatusLocked(res, slot);
672 if (res != OK) {
673 return;
674 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700675
Emilian Peev40ead602017-09-26 15:46:36 +0100676 size_t surfaceId = 0;
677 bool found = false;
678 for (const auto& it : mOutputs) {
679 if (it.second == from) {
680 found = true;
681 surfaceId = it.first;
682 break;
683 }
684 }
685 if (!found) {
686 SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__);
687 return;
688 }
689
Emilian Peev703e4992018-02-27 11:42:09 +0000690 returnOutputBufferLocked(fence, from, surfaceId, slot);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800691}
692
Emilian Peev703e4992018-02-27 11:42:09 +0000693void Camera3StreamSplitter::onBufferReplacedLocked(
Emilian Peev40ead602017-09-26 15:46:36 +0100694 const sp<IGraphicBufferProducer>& from, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800695 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700696 sp<Fence> fence;
Emilian Peev40ead602017-09-26 15:46:36 +0100697
698 int slot = BufferItem::INVALID_BUFFER_SLOT;
699 auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage,
700 nullptr, nullptr);
Emilian Peev703e4992018-02-27 11:42:09 +0000701 handleOutputDequeueStatusLocked(res, slot);
702 if (res != OK) {
Shuzhen Wangafa8a912017-03-15 10:51:27 -0700703 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700704 }
705
Emilian Peev703e4992018-02-27 11:42:09 +0000706 returnOutputBufferLocked(fence, from, surfaceId, slot);
707}
708
709void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence,
710 const sp<IGraphicBufferProducer>& from, size_t surfaceId, int slot) {
711 sp<GraphicBuffer> buffer;
712
713 if (mOutputSlots[from] == nullptr) {
714 //Output surface got likely removed by client.
715 return;
716 }
717
718 auto outputSlots = *mOutputSlots[from];
719 buffer = outputSlots[slot];
Shuzhen Wang0129d522016-10-30 22:43:41 -0700720 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700721 // Merge the release fence of the incoming buffer so that the fence we send
722 // back to the input includes all of the outputs' fences
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800723 if (fence != nullptr && fence->isValid()) {
724 tracker.mergeFence(fence);
725 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700726
Emilian Peev6f823722017-12-07 18:05:49 +0000727 auto detachBuffer = mDetachedBuffers.find(buffer->getId());
728 bool detach = (detachBuffer != mDetachedBuffers.end());
729 if (detach) {
Emilian Peev703e4992018-02-27 11:42:09 +0000730 auto res = from->detachBuffer(slot);
Emilian Peev6f823722017-12-07 18:05:49 +0000731 if (res == NO_ERROR) {
732 outputSlots[slot] = nullptr;
733 } else {
734 SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res);
735 }
736 }
737
Shuzhen Wang0129d522016-10-30 22:43:41 -0700738 // Check to see if this is the last outstanding reference to this buffer
Emilian Peev40ead602017-09-26 15:46:36 +0100739 decrementBufRefCountLocked(buffer->getId(), surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700740}
741
Emilian Peev703e4992018-02-27 11:42:09 +0000742void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res, int slot) {
743 if (res == NO_INIT) {
744 // If we just discovered that this output has been abandoned, note that,
745 // but we can't do anything else, since buffer is invalid
746 onAbandonedLocked();
747 } else if (res == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
748 SP_LOGE("%s: Producer needs to re-allocate buffer!", __FUNCTION__);
749 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
750 } else if (res == IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
751 SP_LOGE("%s: All slot->buffer mapping should be released!", __FUNCTION__);
752 SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__);
753 } else if (res == NO_MEMORY) {
754 SP_LOGE("%s: No free buffers", __FUNCTION__);
755 } else if (res == WOULD_BLOCK) {
756 SP_LOGE("%s: Dequeue call will block", __FUNCTION__);
757 } else if (res != OK || (slot == BufferItem::INVALID_BUFFER_SLOT)) {
758 SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res);
759 }
760}
761
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800763 // If this is called from binderDied callback, it means the app process
764 // holding the binder has died. CameraService will be notified of the binder
765 // death, and camera device will be closed, which in turn calls
766 // disconnect().
767 //
768 // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
769 // consumer being abanoned shouldn't impact the other consumer. So we won't
770 // stop the buffer flow.
771 //
772 // In both cases, we don't need to do anything here.
773 SP_LOGV("One of my outputs has abandoned me");
774}
775
776int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp,
777 const sp<GraphicBuffer>& gb) {
778 auto& outputSlots = *mOutputSlots[gbp];
779
780 for (size_t i = 0; i < outputSlots.size(); i++) {
781 if (outputSlots[i] == gb) {
782 return (int)i;
783 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700784 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800785
Emilian Peev40ead602017-09-26 15:46:36 +0100786 SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800787 gbp.get());
788 return BufferItem::INVALID_BUFFER_SLOT;
789}
790
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791Camera3StreamSplitter::OutputListener::OutputListener(
792 wp<Camera3StreamSplitter> splitter,
793 wp<IGraphicBufferProducer> output)
794 : mSplitter(splitter), mOutput(output) {}
795
796void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800797 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700798 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
799 sp<IGraphicBufferProducer> output = mOutput.promote();
800 if (splitter != nullptr && output != nullptr) {
801 splitter->onBufferReleasedByOutput(output);
802 }
803}
804
805void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) {
806 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
807 if (splitter != nullptr) {
808 Mutex::Autolock lock(splitter->mMutex);
809 splitter->onAbandonedLocked();
810 }
811}
812
813Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800814 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
815 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
816 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817
818void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
819 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
820}
821
Emilian Peev40ead602017-09-26 15:46:36 +0100822size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) {
823 const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId);
824 if (it == mRequestedSurfaces.end()) {
825 return mReferenceCount;
826 } else {
827 mRequestedSurfaces.erase(it);
828 }
829
Shuzhen Wang0129d522016-10-30 22:43:41 -0700830 if (mReferenceCount > 0)
831 --mReferenceCount;
832 return mReferenceCount;
833}
834
835} // namespace android