blob: 7090545a83cac5faaa1234ed42496a2f6eb71739 [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
Shuzhen Wang0129d522016-10-30 22:43:41 -070017#define LOG_TAG "Camera3StreamSplitter"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Jim Shargobdc46fa2024-07-01 16:46:49 +000021#include <binder/ProcessState.h>
Jim Shargo2e0202f2024-07-30 19:54:43 +000022#include <camera/StringUtils.h>
23#include <com_android_graphics_libgui_flags.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070024#include <gui/BufferItem.h>
Jim Shargobdc46fa2024-07-01 16:46:49 +000025#include <gui/BufferItemConsumer.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>
Jim Shargobdc46fa2024-07-01 16:46:49 +000030#include <system/window.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070031#include <ui/GraphicBuffer.h>
Shuzhen Wang0129d522016-10-30 22:43:41 -070032#include <utils/Trace.h>
33
Mathias Agopian05d19b02017-02-28 16:28:19 -080034#include <cutils/atomic.h>
Jim Shargobdc46fa2024-07-01 16:46:49 +000035#include <inttypes.h>
36#include <algorithm>
37#include <cstdint>
38#include <memory>
Mathias Agopian05d19b02017-02-28 16:28:19 -080039
Emilian Peev2295df72021-11-12 18:14:10 -080040#include "Camera3Stream.h"
Jim Shargobdc46fa2024-07-01 16:46:49 +000041#include "Flags.h"
Emilian Peev2295df72021-11-12 18:14:10 -080042
Shuzhen Wang0129d522016-10-30 22:43:41 -070043#include "Camera3StreamSplitter.h"
44
Jim Shargobdc46fa2024-07-01 16:46:49 +000045// We're relying on a large number of yet-to-be-fully-launched flag dependencies
46// here. So instead of flagging each one, we flag the entire implementation to
47// improve legibility.
48#if USE_NEW_STREAM_SPLITTER
49
Shuzhen Wang0129d522016-10-30 22:43:41 -070050namespace android {
51
Emilian Peev40ead602017-09-26 15:46:36 +010052status_t Camera3StreamSplitter::connect(const std::unordered_map<size_t, sp<Surface>> &surfaces,
53 uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width,
Emilian Peev2295df72021-11-12 18:14:10 -080054 uint32_t height, android::PixelFormat format, sp<Surface>* consumer,
Emilian Peevc81a7592022-02-14 17:38:18 -080055 int64_t dynamicRangeProfile) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080056 ATRACE_CALL();
57 if (consumer == nullptr) {
58 SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070059 return BAD_VALUE;
60 }
61
62 Mutex::Autolock lock(mMutex);
63 status_t res = OK;
64
Jim Shargobdc46fa2024-07-01 16:46:49 +000065 if (mOutputSurfaces.size() > 0 || mBufferItemConsumer != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080066 SP_LOGE("%s: already connected", __FUNCTION__);
67 return BAD_VALUE;
68 }
69 if (mBuffers.size() > 0) {
70 SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size());
Shuzhen Wang0129d522016-10-30 22:43:41 -070071 return BAD_VALUE;
72 }
73
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080074 mMaxHalBuffers = halMaxBuffers;
75 mConsumerName = getUniqueConsumerName();
Emilian Peev2295df72021-11-12 18:14:10 -080076 mDynamicRangeProfile = dynamicRangeProfile;
Shuzhen Wang0129d522016-10-30 22:43:41 -070077 // Add output surfaces. This has to be before creating internal buffer queue
78 // in order to get max consumer side buffers.
Emilian Peev40ead602017-09-26 15:46:36 +010079 for (auto &it : surfaces) {
80 if (it.second == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080081 SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__);
Shuzhen Wang758c2152017-01-10 18:26:18 -080082 return BAD_VALUE;
83 }
Emilian Peev40ead602017-09-26 15:46:36 +010084 res = addOutputLocked(it.first, it.second);
Shuzhen Wang758c2152017-01-10 18:26:18 -080085 if (res != OK) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080086 SP_LOGE("%s: Failed to add output surface: %s(%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -080087 __FUNCTION__, strerror(-res), res);
88 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -070089 }
90 }
91
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080092 // Allocate 1 extra buffer to handle the case where all buffers are detached
93 // from input, and attached to the outputs. In this case, the input queue's
94 // dequeueBuffer can still allocate 1 extra buffer before being blocked by
95 // the output's attachBuffer().
Emilian Peevf130ad72018-10-11 11:03:07 +010096 mMaxConsumerBuffers++;
Jim Shargobdc46fa2024-07-01 16:46:49 +000097
Jim Shargo2e0202f2024-07-30 19:54:43 +000098#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Jim Shargobdc46fa2024-07-01 16:46:49 +000099 mBufferItemConsumer = sp<BufferItemConsumer>::make(consumerUsage, mMaxConsumerBuffers);
100 mSurface = mBufferItemConsumer->getSurface();
Jim Shargo2e0202f2024-07-30 19:54:43 +0000101#else
Jim Shargobdc46fa2024-07-01 16:46:49 +0000102 // Create BufferQueue for input
103 sp<IGraphicBufferProducer> bqProducer;
104 sp<IGraphicBufferConsumer> bqConsumer;
105 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
106
107 mBufferItemConsumer = new BufferItemConsumer(bqConsumer, consumerUsage, mMaxConsumerBuffers);
108 mSurface = new Surface(bqProducer);
Jim Shargo2e0202f2024-07-30 19:54:43 +0000109#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Jim Shargobdc46fa2024-07-01 16:46:49 +0000110
Shuzhen Wang0129d522016-10-30 22:43:41 -0700111 if (mBufferItemConsumer == nullptr) {
112 return NO_MEMORY;
113 }
Jim Shargobdc46fa2024-07-01 16:46:49 +0000114 mBufferItemConsumer->setName(toString8(mConsumerName));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700115
Jim Shargobdc46fa2024-07-01 16:46:49 +0000116 *consumer = mSurface;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800117 if (*consumer == nullptr) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700118 return NO_MEMORY;
119 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700120
Jim Shargobdc46fa2024-07-01 16:46:49 +0000121 res = mSurface->setAsyncMode(true);
Emilian Peev40ead602017-09-26 15:46:36 +0100122 if (res != OK) {
123 SP_LOGE("%s: Failed to enable input queue async mode: %s(%d)", __FUNCTION__,
124 strerror(-res), res);
125 return res;
126 }
127
Jim Shargobdc46fa2024-07-01 16:46:49 +0000128 mBufferItemConsumer->setFrameAvailableListener(this);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700129
Emilian Peev40ead602017-09-26 15:46:36 +0100130 mWidth = width;
131 mHeight = height;
132 mFormat = format;
133 mProducerUsage = producerUsage;
Emilian Peevf130ad72018-10-11 11:03:07 +0100134 mAcquiredInputBuffers = 0;
Emilian Peev40ead602017-09-26 15:46:36 +0100135
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800136 SP_LOGV("%s: connected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700137 return res;
138}
139
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800140status_t Camera3StreamSplitter::getOnFrameAvailableResult() {
141 ATRACE_CALL();
142 return mOnFrameAvailableRes.load();
143}
144
Shuzhen Wang0129d522016-10-30 22:43:41 -0700145void Camera3StreamSplitter::disconnect() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800146 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700147 Mutex::Autolock lock(mMutex);
148
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800149 mNotifiers.clear();
150
Jim Shargobdc46fa2024-07-01 16:46:49 +0000151 for (auto& output : mOutputSurfaces) {
Emilian Peev40ead602017-09-26 15:46:36 +0100152 if (output.second != nullptr) {
153 output.second->disconnect(NATIVE_WINDOW_API_CAMERA);
154 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700155 }
Emilian Peev2295df72021-11-12 18:14:10 -0800156 mOutputSurfaces.clear();
Jim Shargobdc46fa2024-07-01 16:46:49 +0000157 mHeldBuffers.clear();
Emilian Peev40ead602017-09-26 15:46:36 +0100158 mConsumerBufferCount.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700159
Jim Shargobdc46fa2024-07-01 16:46:49 +0000160 if (mBufferItemConsumer != nullptr) {
161 mBufferItemConsumer->abandon();
Emilian Peev359adca2019-10-30 10:12:46 -0700162 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700163
164 if (mBuffers.size() > 0) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800165 SP_LOGW("%zu buffers still being tracked", mBuffers.size());
166 mBuffers.clear();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700167 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800168
169 mMaxHalBuffers = 0;
170 mMaxConsumerBuffers = 0;
Emilian Peevf130ad72018-10-11 11:03:07 +0100171 mAcquiredInputBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800172 SP_LOGV("%s: Disconnected", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700173}
174
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700175Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) :
176 mUseHalBufManager(useHalBufManager) {}
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800177
Shuzhen Wang0129d522016-10-30 22:43:41 -0700178Camera3StreamSplitter::~Camera3StreamSplitter() {
179 disconnect();
180}
181
Emilian Peev40ead602017-09-26 15:46:36 +0100182status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800183 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700184 Mutex::Autolock lock(mMutex);
Emilian Peev40ead602017-09-26 15:46:36 +0100185 status_t res = addOutputLocked(surfaceId, outputQueue);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800186
187 if (res != OK) {
188 SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res);
189 return res;
190 }
191
Emilian Peevf130ad72018-10-11 11:03:07 +0100192 if (mMaxConsumerBuffers > mAcquiredInputBuffers) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000193 res = mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
Emilian Peevf130ad72018-10-11 11:03:07 +0100194 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800195
196 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700197}
198
Jayant Chowdhary9d3a6492023-11-22 07:23:59 +0000199void Camera3StreamSplitter::setHalBufferManager(bool enabled) {
200 Mutex::Autolock lock(mMutex);
201 mUseHalBufManager = enabled;
202}
203
Emilian Peev40ead602017-09-26 15:46:36 +0100204status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800205 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700206 if (outputQueue == nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800207 SP_LOGE("addOutput: outputQueue must not be NULL");
Shuzhen Wang0129d522016-10-30 22:43:41 -0700208 return BAD_VALUE;
209 }
210
Jim Shargobdc46fa2024-07-01 16:46:49 +0000211 if (mOutputSurfaces[surfaceId] != nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100212 SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId);
213 return BAD_VALUE;
214 }
215
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700216 status_t res = native_window_set_buffers_dimensions(outputQueue.get(),
Emilian Peev40ead602017-09-26 15:46:36 +0100217 mWidth, mHeight);
218 if (res != NO_ERROR) {
219 SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res);
220 return res;
221 }
Shuzhen Wang9d5c9362018-08-27 11:39:53 -0700222 res = native_window_set_buffers_format(outputQueue.get(),
223 mFormat);
224 if (res != OK) {
225 ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu",
226 __FUNCTION__, mFormat, surfaceId);
227 return res;
228 }
Emilian Peev40ead602017-09-26 15:46:36 +0100229
Shuzhen Wang0129d522016-10-30 22:43:41 -0700230 // Connect to the buffer producer
Jim Shargobdc46fa2024-07-01 16:46:49 +0000231 sp<OutputListener> listener = sp<OutputListener>::make(this, outputQueue);
232 res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener, /* reportBufferRemoval */ false);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800233 if (res != NO_ERROR) {
234 SP_LOGE("addOutput: failed to connect (%d)", res);
235 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700236 }
237
238 // Query consumer side buffer count, and update overall buffer count
239 int maxConsumerBuffers = 0;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800240 res = static_cast<ANativeWindow*>(outputQueue.get())->query(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700241 outputQueue.get(),
242 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800243 if (res != OK) {
244 SP_LOGE("%s: Unable to query consumer undequeued buffer count"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700245 " for surface", __FUNCTION__);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800246 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700247 }
248
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800249 SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__,
250 maxConsumerBuffers, mMaxHalBuffers);
Emilian Peev18e3f372018-05-22 18:55:01 +0100251 // The output slot count requirement can change depending on the current amount
252 // of outputs and incoming buffer consumption rate. To avoid any issues with
253 // insufficient slots, set their count to the maximum supported. The output
254 // surface buffer allocation is disabled so no real buffers will get allocated.
255 size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800256 res = native_window_set_buffer_count(outputQueue.get(),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700257 totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800258 if (res != OK) {
259 SP_LOGE("%s: Unable to set buffer count for surface %p",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700260 __FUNCTION__, outputQueue.get());
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800261 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700262 }
263
264 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
265 // We need skip these cases as timeout will disable the non-blocking (async) mode.
Emilian Peev050f5dc2017-05-18 14:43:56 +0100266 uint64_t usage = 0;
267 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700268 if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700269 nsecs_t timeout = mUseHalBufManager ?
270 kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout;
271 outputQueue->setDequeueTimeout(timeout);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700272 }
273
Jim Shargobdc46fa2024-07-01 16:46:49 +0000274 res = outputQueue->allowAllocation(false);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800275 if (res != OK) {
276 SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__);
277 return res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700278 }
279
280 // Add new entry into mOutputs
Emilian Peev2295df72021-11-12 18:14:10 -0800281 mOutputSurfaces[surfaceId] = outputQueue;
Emilian Peev40ead602017-09-26 15:46:36 +0100282 mConsumerBufferCount[surfaceId] = maxConsumerBuffers;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000283 if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) {
284 SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__,
285 mConsumerBufferCount[surfaceId], mMaxHalBuffers);
286 }
Jim Shargobdc46fa2024-07-01 16:46:49 +0000287 mNotifiers[outputQueue] = listener;
288 mHeldBuffers[outputQueue] = std::make_unique<HeldBuffers>(totalBufferCount);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800289
290 mMaxConsumerBuffers += maxConsumerBuffers;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700291 return NO_ERROR;
292}
293
Emilian Peev40ead602017-09-26 15:46:36 +0100294status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) {
295 ATRACE_CALL();
296 Mutex::Autolock lock(mMutex);
297
298 status_t res = removeOutputLocked(surfaceId);
299 if (res != OK) {
300 SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res);
301 return res;
302 }
303
Emilian Peevf130ad72018-10-11 11:03:07 +0100304 if (mAcquiredInputBuffers < mMaxConsumerBuffers) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000305 res = mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers);
Emilian Peevf130ad72018-10-11 11:03:07 +0100306 if (res != OK) {
307 SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res);
308 return res;
309 }
Emilian Peev40ead602017-09-26 15:46:36 +0100310 }
311
312 return res;
313}
314
315status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000316 if (mOutputSurfaces[surfaceId] == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100317 SP_LOGE("%s: output surface is not present!", __FUNCTION__);
318 return BAD_VALUE;
319 }
320
Jim Shargobdc46fa2024-07-01 16:46:49 +0000321 sp<Surface> surface = mOutputSurfaces[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100322 //Search and decrement the ref. count of any buffers that are
323 //still attached to the removed surface.
324 std::vector<uint64_t> pendingBufferIds;
Jim Shargobdc46fa2024-07-01 16:46:49 +0000325
326 // TODO: can we simplify this to just use the tracker?
327 for (const auto& buffer : (*mHeldBuffers[surface])) {
328 pendingBufferIds.push_back(buffer->getId());
329 auto rc = surface->detachBuffer(buffer);
330 if (rc != NO_ERROR) {
331 // Buffers that fail to detach here will be scheduled for detach in the
332 // input buffer queue and the rest of the registered outputs instead.
333 // This will help ensure that camera stops accessing buffers that still
334 // can get referenced by the disconnected output.
335 mDetachedBuffers.emplace(buffer->getId());
Emilian Peev40ead602017-09-26 15:46:36 +0100336 }
337 }
Emilian Peev2295df72021-11-12 18:14:10 -0800338 mOutputSurfaces[surfaceId] = nullptr;
Jim Shargobdc46fa2024-07-01 16:46:49 +0000339 mHeldBuffers[surface] = nullptr;
Emilian Peev40ead602017-09-26 15:46:36 +0100340 for (const auto &id : pendingBufferIds) {
341 decrementBufRefCountLocked(id, surfaceId);
342 }
343
Jim Shargobdc46fa2024-07-01 16:46:49 +0000344 status_t res = surface->disconnect(NATIVE_WINDOW_API_CAMERA);
Emilian Peev40ead602017-09-26 15:46:36 +0100345 if (res != OK) {
346 SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res);
347 return res;
348 }
349
Jim Shargobdc46fa2024-07-01 16:46:49 +0000350 mNotifiers[surface] = nullptr;
Emilian Peevf63f1d92018-11-06 16:33:29 +0000351 mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId];
Emilian Peev40ead602017-09-26 15:46:36 +0100352 mConsumerBufferCount[surfaceId] = 0;
353
354 return res;
355}
356
Jim Shargobdc46fa2024-07-01 16:46:49 +0000357status_t Camera3StreamSplitter::outputBufferLocked(const sp<Surface>& output,
Emilian Peev40ead602017-09-26 15:46:36 +0100358 const BufferItem& bufferItem, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800359 ATRACE_CALL();
360 status_t res;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800361
362 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
363 const BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800364
Emilian Peev2295df72021-11-12 18:14:10 -0800365 if (mOutputSurfaces[surfaceId] != nullptr) {
366 sp<ANativeWindow> anw = mOutputSurfaces[surfaceId];
367 camera3::Camera3Stream::queueHDRMetadata(
368 bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile);
369 } else {
370 SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId);
371 }
372
Jim Shargobdc46fa2024-07-01 16:46:49 +0000373 output->setBuffersTimestamp(bufferItem.mTimestamp);
374 output->setBuffersDataSpace(static_cast<ui::Dataspace>(bufferItem.mDataSpace));
375 output->setCrop(&bufferItem.mCrop);
376 output->setScalingMode(bufferItem.mScalingMode);
377 output->setBuffersTransform(bufferItem.mTransform);
378
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800379 // In case the output BufferQueue has its own lock, if we hold splitter lock while calling
380 // queueBuffer (which will try to acquire the output lock), the output could be holding its
381 // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into
382 // circular lock situation.
383 mMutex.unlock();
Jim Shargobdc46fa2024-07-01 16:46:49 +0000384 SurfaceQueueBufferOutput queueBufferOutput;
385 res = output->queueBuffer(bufferItem.mGraphicBuffer, bufferItem.mFence, &queueBufferOutput);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800386 mMutex.lock();
387
Jim Shargobdc46fa2024-07-01 16:46:49 +0000388 SP_LOGV("%s: Queuing buffer to buffer queue %p bufferId %" PRIu64 " returns %d", __FUNCTION__,
389 output.get(), bufferId, res);
390 // During buffer queue 'mMutex' is not held which makes the removal of
391 // "output" possible. Check whether this is the case and return.
392 if (mOutputSurfaces[surfaceId] == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100393 return res;
394 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800395 if (res != OK) {
396 if (res != NO_INIT && res != DEAD_OBJECT) {
397 SP_LOGE("Queuing buffer to output failed (%d)", res);
398 }
399 // If we just discovered that this output has been abandoned, note
400 // that, increment the release count so that we still release this
401 // buffer eventually, and move on to the next output
402 onAbandonedLocked();
Emilian Peev40ead602017-09-26 15:46:36 +0100403 decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800404 return res;
405 }
406
407 // If the queued buffer replaces a pending buffer in the async
408 // queue, no onBufferReleased is called by the buffer queue.
409 // Proactively trigger the callback to avoid buffer loss.
Jim Shargobdc46fa2024-07-01 16:46:49 +0000410 if (queueBufferOutput.bufferReplaced) {
Emilian Peev703e4992018-02-27 11:42:09 +0000411 onBufferReplacedLocked(output, surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800412 }
413
414 return res;
415}
416
Austin Borger1c1bee02023-06-01 16:51:35 -0700417std::string Camera3StreamSplitter::getUniqueConsumerName() {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700418 static volatile int32_t counter = 0;
Austin Borger1c1bee02023-06-01 16:51:35 -0700419 return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700420}
421
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800422status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700423 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800424
Shuzhen Wang0129d522016-10-30 22:43:41 -0700425 Mutex::Autolock lock(mMutex);
426
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800427 uint64_t bufferId = buffer->getId();
428 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]);
429 mBuffers.erase(bufferId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700430
Emilian Peev40ead602017-09-26 15:46:36 +0100431 return OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800432}
433
434status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb,
435 const std::vector<size_t>& surface_ids) {
436 ATRACE_CALL();
437 status_t res = OK;
438
439 Mutex::Autolock lock(mMutex);
440
441 sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb));
442 uint64_t bufferId = gb->getId();
443
444 // Initialize buffer tracker for this input buffer
445 auto tracker = std::make_unique<BufferTracker>(gb, surface_ids);
446
447 for (auto& surface_id : surface_ids) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000448 sp<Surface>& surface = mOutputSurfaces[surface_id];
449 if (surface.get() == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100450 //Output surface got likely removed by client.
451 continue;
452 }
Jim Shargobdc46fa2024-07-01 16:46:49 +0000453
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800454 //Temporarly Unlock the mutex when trying to attachBuffer to the output
455 //queue, because attachBuffer could block in case of a slow consumer. If
456 //we block while holding the lock, onFrameAvailable and onBufferReleased
457 //will block as well because they need to acquire the same lock.
458 mMutex.unlock();
Jim Shargobdc46fa2024-07-01 16:46:49 +0000459 res = surface->attachBuffer(anb);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800460 mMutex.lock();
461 if (res != OK) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000462 SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)", __FUNCTION__,
463 surface.get(), strerror(-res), res);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700464 // TODO: might need to detach/cleanup the already attached buffers before return?
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800465 return res;
466 }
Emilian Peev40ead602017-09-26 15:46:36 +0100467 //During buffer attach 'mMutex' is not held which makes the removal of
468 //"gbp" possible. Check whether this is the case and continue.
Jim Shargobdc46fa2024-07-01 16:46:49 +0000469 if (mHeldBuffers[surface] == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100470 continue;
471 }
Jim Shargobdc46fa2024-07-01 16:46:49 +0000472 mHeldBuffers[surface]->insert(gb);
473 SP_LOGV("%s: Attached buffer %p on output %p.", __FUNCTION__, gb.get(), surface.get());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700474 }
475
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800476 mBuffers[bufferId] = std::move(tracker);
477
478 return res;
479}
480
481void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) {
482 ATRACE_CALL();
483 Mutex::Autolock lock(mMutex);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700484
485 // Acquire and detach the buffer from the input
486 BufferItem bufferItem;
Jim Shargobdc46fa2024-07-01 16:46:49 +0000487 status_t res = mBufferItemConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800488 if (res != NO_ERROR) {
489 SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res);
490 mOnFrameAvailableRes.store(res);
491 return;
492 }
Emilian Peev40ead602017-09-26 15:46:36 +0100493
Jim Shargobdc46fa2024-07-01 16:46:49 +0000494 uint64_t bufferId = bufferItem.mGraphicBuffer->getId();
Emilian Peev40ead602017-09-26 15:46:36 +0100495
496 if (mBuffers.find(bufferId) == mBuffers.end()) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800497 SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map",
498 __FUNCTION__);
499 mOnFrameAvailableRes.store(INVALID_OPERATION);
500 return;
501 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700502
Emilian Peevf130ad72018-10-11 11:03:07 +0100503 mAcquiredInputBuffers++;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800504 SP_LOGV("acquired buffer %" PRId64 " from input at slot %d",
505 bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700506
Emilian Peev3bdcdff2018-06-25 14:37:29 +0100507 if (bufferItem.mTransformToDisplayInverse) {
508 bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
509 }
510
Shuzhen Wang0129d522016-10-30 22:43:41 -0700511 // Attach and queue the buffer to each of the outputs
Emilian Peev40ead602017-09-26 15:46:36 +0100512 BufferTracker& tracker = *(mBuffers[bufferId]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700513
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800514 SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu",
515 __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size());
516 for (const auto id : tracker.requestedSurfaces()) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000517 if (mOutputSurfaces[id] == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100518 //Output surface got likely removed by client.
519 continue;
520 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700521
Jim Shargobdc46fa2024-07-01 16:46:49 +0000522 res = outputBufferLocked(mOutputSurfaces[id], bufferItem, id);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800523 if (res != OK) {
524 SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res);
525 mOnFrameAvailableRes.store(res);
526 // If we fail to send buffer to certain output, keep sending to
527 // other outputs.
528 continue;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700529 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800530 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700531
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800532 mOnFrameAvailableRes.store(res);
533}
534
Yin-Chia Yehda208452019-08-26 15:35:57 -0700535void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) {
536 ATRACE_CALL();
537 onFrameAvailable(item);
538}
539
Emilian Peev40ead602017-09-26 15:46:36 +0100540void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800541 ATRACE_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800542
Emilian Peev40ead602017-09-26 15:46:36 +0100543 if (mBuffers[id] == nullptr) {
544 return;
545 }
546
547 size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800548 if (referenceCount > 0) {
549 return;
550 }
551
552 // We no longer need to track the buffer now that it is being returned to the
553 // input. Note that this should happen before we unlock the mutex and call
554 // releaseBuffer, to avoid the case where the same bufferId is acquired in
555 // attachBufferToOutputs resulting in a new BufferTracker with same bufferId
556 // overwrites the current one.
557 std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]);
558 mBuffers.erase(id);
559
Emilian Peev40ead602017-09-26 15:46:36 +0100560 uint64_t bufferId = tracker_ptr->getBuffer()->getId();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800561
Emilian Peev6f823722017-12-07 18:05:49 +0000562 auto detachBuffer = mDetachedBuffers.find(bufferId);
563 bool detach = (detachBuffer != mDetachedBuffers.end());
564 if (detach) {
565 mDetachedBuffers.erase(detachBuffer);
Emilian Peev6f823722017-12-07 18:05:49 +0000566 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800567 // Temporarily unlock mutex to avoid circular lock:
568 // 1. This function holds splitter lock, calls releaseBuffer which triggers
569 // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the
570 // OutputStream lock
571 // 2. Camera3SharedOutputStream::getBufferLocked calls
572 // attachBufferToOutputs, which holds the stream lock, and waits for the
573 // splitter lock.
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800574 mMutex.unlock();
Emilian Peev40ead602017-09-26 15:46:36 +0100575 int res = NO_ERROR;
Jim Shargobdc46fa2024-07-01 16:46:49 +0000576 if (mBufferItemConsumer != nullptr) {
Emilian Peev6f823722017-12-07 18:05:49 +0000577 if (detach) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000578 res = mBufferItemConsumer->detachBuffer(tracker_ptr->getBuffer());
Emilian Peev6f823722017-12-07 18:05:49 +0000579 } else {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000580 res = mBufferItemConsumer->releaseBuffer(tracker_ptr->getBuffer(),
581 tracker_ptr->getMergedFence());
Emilian Peev6f823722017-12-07 18:05:49 +0000582 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800583 } else {
584 SP_LOGE("%s: consumer has become null!", __FUNCTION__);
585 }
586 mMutex.lock();
Emilian Peev6f823722017-12-07 18:05:49 +0000587
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800588 if (res != NO_ERROR) {
Emilian Peev6f823722017-12-07 18:05:49 +0000589 if (detach) {
590 SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res);
591 } else {
592 SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res);
593 }
Emilian Peevf130ad72018-10-11 11:03:07 +0100594 } else {
595 if (mAcquiredInputBuffers == 0) {
596 ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__);
597 } else {
598 mAcquiredInputBuffers--;
599 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700600 }
601}
602
Jim Shargobdc46fa2024-07-01 16:46:49 +0000603void Camera3StreamSplitter::onBufferReleasedByOutput(const sp<Surface>& from) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700604 ATRACE_CALL();
Emilian Peev703e4992018-02-27 11:42:09 +0000605
Jim Shargobdc46fa2024-07-01 16:46:49 +0000606 from->setBuffersDimensions(mWidth, mHeight);
607 from->setBuffersFormat(mFormat);
608 from->setUsage(mProducerUsage);
609
610 sp<GraphicBuffer> buffer;
611 sp<Fence> fence;
612 auto res = from->dequeueBuffer(&buffer, &fence);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700613 Mutex::Autolock lock(mMutex);
Jim Shargobdc46fa2024-07-01 16:46:49 +0000614 handleOutputDequeueStatusLocked(res, buffer);
Emilian Peev703e4992018-02-27 11:42:09 +0000615 if (res != OK) {
616 return;
617 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700618
Emilian Peev40ead602017-09-26 15:46:36 +0100619 size_t surfaceId = 0;
620 bool found = false;
Jim Shargobdc46fa2024-07-01 16:46:49 +0000621 for (const auto& it : mOutputSurfaces) {
Emilian Peev40ead602017-09-26 15:46:36 +0100622 if (it.second == from) {
623 found = true;
624 surfaceId = it.first;
625 break;
626 }
627 }
628 if (!found) {
629 SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__);
630 return;
631 }
632
Jim Shargobdc46fa2024-07-01 16:46:49 +0000633 returnOutputBufferLocked(fence, from, surfaceId, buffer);
Shuzhen Wanga141c5f2017-01-24 14:51:37 -0800634}
635
Jim Shargobdc46fa2024-07-01 16:46:49 +0000636void Camera3StreamSplitter::onBufferReplacedLocked(const sp<Surface>& from, size_t surfaceId) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800637 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +0100638
Jim Shargobdc46fa2024-07-01 16:46:49 +0000639 from->setBuffersDimensions(mWidth, mHeight);
640 from->setBuffersFormat(mFormat);
641 from->setUsage(mProducerUsage);
642
643 sp<GraphicBuffer> buffer;
644 sp<Fence> fence;
645 auto res = from->dequeueBuffer(&buffer, &fence);
646 handleOutputDequeueStatusLocked(res, buffer);
Emilian Peev703e4992018-02-27 11:42:09 +0000647 if (res != OK) {
Shuzhen Wangafa8a912017-03-15 10:51:27 -0700648 return;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700649 }
650
Jim Shargobdc46fa2024-07-01 16:46:49 +0000651 returnOutputBufferLocked(fence, from, surfaceId, buffer);
Emilian Peev703e4992018-02-27 11:42:09 +0000652}
653
654void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence,
Jim Shargobdc46fa2024-07-01 16:46:49 +0000655 const sp<Surface>& from, size_t surfaceId, const sp<GraphicBuffer>& buffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700656 BufferTracker& tracker = *(mBuffers[buffer->getId()]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700657 // Merge the release fence of the incoming buffer so that the fence we send
658 // back to the input includes all of the outputs' fences
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800659 if (fence != nullptr && fence->isValid()) {
660 tracker.mergeFence(fence);
661 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700662
Emilian Peev6f823722017-12-07 18:05:49 +0000663 auto detachBuffer = mDetachedBuffers.find(buffer->getId());
664 bool detach = (detachBuffer != mDetachedBuffers.end());
665 if (detach) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000666 auto res = from->detachBuffer(buffer);
Emilian Peev6f823722017-12-07 18:05:49 +0000667 if (res == NO_ERROR) {
Jim Shargobdc46fa2024-07-01 16:46:49 +0000668 if (mHeldBuffers.contains(from)) {
669 mHeldBuffers[from]->erase(buffer);
670 } else {
671 uint64_t surfaceId = 0;
672 from->getUniqueId(&surfaceId);
673 SP_LOGW("%s: buffer %" PRIu64 " not found in held buffers of surface %" PRIu64,
674 __FUNCTION__, buffer->getId(), surfaceId);
675 }
Emilian Peev6f823722017-12-07 18:05:49 +0000676 } else {
677 SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res);
678 }
679 }
680
Shuzhen Wang0129d522016-10-30 22:43:41 -0700681 // Check to see if this is the last outstanding reference to this buffer
Emilian Peev40ead602017-09-26 15:46:36 +0100682 decrementBufRefCountLocked(buffer->getId(), surfaceId);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700683}
684
Jim Shargobdc46fa2024-07-01 16:46:49 +0000685void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res,
686 const sp<GraphicBuffer>& buffer) {
Emilian Peev703e4992018-02-27 11:42:09 +0000687 if (res == NO_INIT) {
688 // If we just discovered that this output has been abandoned, note that,
689 // but we can't do anything else, since buffer is invalid
690 onAbandonedLocked();
Emilian Peev703e4992018-02-27 11:42:09 +0000691 } else if (res == NO_MEMORY) {
692 SP_LOGE("%s: No free buffers", __FUNCTION__);
693 } else if (res == WOULD_BLOCK) {
694 SP_LOGE("%s: Dequeue call will block", __FUNCTION__);
Jim Shargobdc46fa2024-07-01 16:46:49 +0000695 } else if (res != OK || buffer == nullptr) {
Emilian Peev703e4992018-02-27 11:42:09 +0000696 SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res);
697 }
698}
699
Shuzhen Wang0129d522016-10-30 22:43:41 -0700700void Camera3StreamSplitter::onAbandonedLocked() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800701 // If this is called from binderDied callback, it means the app process
702 // holding the binder has died. CameraService will be notified of the binder
703 // death, and camera device will be closed, which in turn calls
704 // disconnect().
705 //
706 // If this is called from onBufferReleasedByOutput or onFrameAvailable, one
707 // consumer being abanoned shouldn't impact the other consumer. So we won't
708 // stop the buffer flow.
709 //
710 // In both cases, we don't need to do anything here.
711 SP_LOGV("One of my outputs has abandoned me");
712}
713
Jim Shargobdc46fa2024-07-01 16:46:49 +0000714Camera3StreamSplitter::OutputListener::OutputListener(wp<Camera3StreamSplitter> splitter,
715 wp<Surface> output)
716 : mSplitter(splitter), mOutput(output) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700717
718void Camera3StreamSplitter::OutputListener::onBufferReleased() {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800719 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700720 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
Jim Shargobdc46fa2024-07-01 16:46:49 +0000721 sp<Surface> output = mOutput.promote();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700722 if (splitter != nullptr && output != nullptr) {
723 splitter->onBufferReleasedByOutput(output);
724 }
725}
726
Jim Shargobdc46fa2024-07-01 16:46:49 +0000727void Camera3StreamSplitter::OutputListener::onRemoteDied() {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700728 sp<Camera3StreamSplitter> splitter = mSplitter.promote();
729 if (splitter != nullptr) {
730 Mutex::Autolock lock(splitter->mMutex);
731 splitter->onAbandonedLocked();
732 }
733}
734
735Camera3StreamSplitter::BufferTracker::BufferTracker(
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800736 const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces)
737 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces),
738 mReferenceCount(requestedSurfaces.size()) {}
Shuzhen Wang0129d522016-10-30 22:43:41 -0700739
740void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) {
741 mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with);
742}
743
Emilian Peev40ead602017-09-26 15:46:36 +0100744size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) {
745 const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId);
746 if (it == mRequestedSurfaces.end()) {
747 return mReferenceCount;
748 } else {
749 mRequestedSurfaces.erase(it);
750 }
751
Shuzhen Wang0129d522016-10-30 22:43:41 -0700752 if (mReferenceCount > 0)
753 --mReferenceCount;
754 return mReferenceCount;
755}
756
757} // namespace android
Jim Shargobdc46fa2024-07-01 16:46:49 +0000758
759#endif // USE_NEW_STREAM_SPLITTER