Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014,2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
| 18 | |
| 19 | #define LOG_TAG "Camera3StreamSplitter" |
| 20 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
| 23 | #include <gui/BufferItem.h> |
| 24 | #include <gui/IGraphicBufferConsumer.h> |
| 25 | #include <gui/IGraphicBufferProducer.h> |
| 26 | #include <gui/BufferQueue.h> |
| 27 | #include <gui/Surface.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 28 | #include <camera/StringUtils.h> |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 29 | |
| 30 | #include <ui/GraphicBuffer.h> |
| 31 | |
| 32 | #include <binder/ProcessState.h> |
| 33 | |
| 34 | #include <utils/Trace.h> |
| 35 | |
Mathias Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 36 | #include <cutils/atomic.h> |
| 37 | |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 38 | #include "Camera3Stream.h" |
| 39 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 40 | #include "Camera3StreamSplitter.h" |
| 41 | |
| 42 | namespace android { |
| 43 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 44 | status_t Camera3StreamSplitter::connect(const std::unordered_map<size_t, sp<Surface>> &surfaces, |
| 45 | uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 46 | uint32_t height, android::PixelFormat format, sp<Surface>* consumer, |
Emilian Peev | c81a759 | 2022-02-14 17:38:18 -0800 | [diff] [blame] | 47 | int64_t dynamicRangeProfile) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 48 | ATRACE_CALL(); |
| 49 | if (consumer == nullptr) { |
| 50 | SP_LOGE("%s: consumer pointer is NULL", __FUNCTION__); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 51 | return BAD_VALUE; |
| 52 | } |
| 53 | |
| 54 | Mutex::Autolock lock(mMutex); |
| 55 | status_t res = OK; |
| 56 | |
| 57 | if (mOutputs.size() > 0 || mConsumer != nullptr) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 58 | SP_LOGE("%s: already connected", __FUNCTION__); |
| 59 | return BAD_VALUE; |
| 60 | } |
| 61 | if (mBuffers.size() > 0) { |
| 62 | SP_LOGE("%s: still has %zu pending buffers", __FUNCTION__, mBuffers.size()); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 63 | return BAD_VALUE; |
| 64 | } |
| 65 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 66 | mMaxHalBuffers = halMaxBuffers; |
| 67 | mConsumerName = getUniqueConsumerName(); |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 68 | mDynamicRangeProfile = dynamicRangeProfile; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 69 | // Add output surfaces. This has to be before creating internal buffer queue |
| 70 | // in order to get max consumer side buffers. |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 71 | for (auto &it : surfaces) { |
| 72 | if (it.second == nullptr) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 73 | SP_LOGE("%s: Fatal: surface is NULL", __FUNCTION__); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 74 | return BAD_VALUE; |
| 75 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 76 | res = addOutputLocked(it.first, it.second); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 77 | if (res != OK) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 78 | SP_LOGE("%s: Failed to add output surface: %s(%d)", |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 79 | __FUNCTION__, strerror(-res), res); |
| 80 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 84 | // Create BufferQueue for input |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 85 | BufferQueue::createBufferQueue(&mProducer, &mConsumer); |
| 86 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 87 | // Allocate 1 extra buffer to handle the case where all buffers are detached |
| 88 | // from input, and attached to the outputs. In this case, the input queue's |
| 89 | // dequeueBuffer can still allocate 1 extra buffer before being blocked by |
| 90 | // the output's attachBuffer(). |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 91 | mMaxConsumerBuffers++; |
| 92 | mBufferItemConsumer = new BufferItemConsumer(mConsumer, consumerUsage, mMaxConsumerBuffers); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 93 | if (mBufferItemConsumer == nullptr) { |
| 94 | return NO_MEMORY; |
| 95 | } |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 96 | mConsumer->setConsumerName(toString8(mConsumerName)); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 97 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 98 | *consumer = new Surface(mProducer); |
| 99 | if (*consumer == nullptr) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 100 | return NO_MEMORY; |
| 101 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 102 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 103 | res = mProducer->setAsyncMode(true); |
| 104 | if (res != OK) { |
| 105 | SP_LOGE("%s: Failed to enable input queue async mode: %s(%d)", __FUNCTION__, |
| 106 | strerror(-res), res); |
| 107 | return res; |
| 108 | } |
| 109 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 110 | res = mConsumer->consumerConnect(this, /* controlledByApp */ false); |
| 111 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 112 | mWidth = width; |
| 113 | mHeight = height; |
| 114 | mFormat = format; |
| 115 | mProducerUsage = producerUsage; |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 116 | mAcquiredInputBuffers = 0; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 117 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 118 | SP_LOGV("%s: connected", __FUNCTION__); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 119 | return res; |
| 120 | } |
| 121 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 122 | status_t Camera3StreamSplitter::getOnFrameAvailableResult() { |
| 123 | ATRACE_CALL(); |
| 124 | return mOnFrameAvailableRes.load(); |
| 125 | } |
| 126 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 127 | void Camera3StreamSplitter::disconnect() { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 128 | ATRACE_CALL(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 129 | Mutex::Autolock lock(mMutex); |
| 130 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 131 | for (auto& notifier : mNotifiers) { |
| 132 | sp<IGraphicBufferProducer> producer = notifier.first; |
| 133 | sp<OutputListener> listener = notifier.second; |
| 134 | IInterface::asBinder(producer)->unlinkToDeath(listener); |
| 135 | } |
| 136 | mNotifiers.clear(); |
| 137 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 138 | for (auto& output : mOutputs) { |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 139 | if (output.second != nullptr) { |
| 140 | output.second->disconnect(NATIVE_WINDOW_API_CAMERA); |
| 141 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 142 | } |
| 143 | mOutputs.clear(); |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 144 | mOutputSurfaces.clear(); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 145 | mOutputSlots.clear(); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 146 | mConsumerBufferCount.clear(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 147 | |
Emilian Peev | 359adca | 2019-10-30 10:12:46 -0700 | [diff] [blame] | 148 | if (mConsumer.get() != nullptr) { |
| 149 | mConsumer->consumerDisconnect(); |
| 150 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 151 | |
| 152 | if (mBuffers.size() > 0) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 153 | SP_LOGW("%zu buffers still being tracked", mBuffers.size()); |
| 154 | mBuffers.clear(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 155 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 156 | |
| 157 | mMaxHalBuffers = 0; |
| 158 | mMaxConsumerBuffers = 0; |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 159 | mAcquiredInputBuffers = 0; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 160 | SP_LOGV("%s: Disconnected", __FUNCTION__); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 163 | Camera3StreamSplitter::Camera3StreamSplitter(bool useHalBufManager) : |
| 164 | mUseHalBufManager(useHalBufManager) {} |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 165 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 166 | Camera3StreamSplitter::~Camera3StreamSplitter() { |
| 167 | disconnect(); |
| 168 | } |
| 169 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 170 | status_t Camera3StreamSplitter::addOutput(size_t surfaceId, const sp<Surface>& outputQueue) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 171 | ATRACE_CALL(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 172 | Mutex::Autolock lock(mMutex); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 173 | status_t res = addOutputLocked(surfaceId, outputQueue); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 174 | |
| 175 | if (res != OK) { |
| 176 | SP_LOGE("%s: addOutputLocked failed %d", __FUNCTION__, res); |
| 177 | return res; |
| 178 | } |
| 179 | |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 180 | if (mMaxConsumerBuffers > mAcquiredInputBuffers) { |
| 181 | res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers); |
| 182 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 183 | |
| 184 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jayant Chowdhary | 9d3a649 | 2023-11-22 07:23:59 +0000 | [diff] [blame] | 187 | void Camera3StreamSplitter::setHalBufferManager(bool enabled) { |
| 188 | Mutex::Autolock lock(mMutex); |
| 189 | mUseHalBufManager = enabled; |
| 190 | } |
| 191 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 192 | status_t Camera3StreamSplitter::addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 193 | ATRACE_CALL(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 194 | if (outputQueue == nullptr) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 195 | SP_LOGE("addOutput: outputQueue must not be NULL"); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 196 | return BAD_VALUE; |
| 197 | } |
| 198 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 199 | if (mOutputs[surfaceId] != nullptr) { |
| 200 | SP_LOGE("%s: surfaceId: %u already taken!", __FUNCTION__, (unsigned) surfaceId); |
| 201 | return BAD_VALUE; |
| 202 | } |
| 203 | |
Shuzhen Wang | 9d5c936 | 2018-08-27 11:39:53 -0700 | [diff] [blame] | 204 | status_t res = native_window_set_buffers_dimensions(outputQueue.get(), |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 205 | mWidth, mHeight); |
| 206 | if (res != NO_ERROR) { |
| 207 | SP_LOGE("addOutput: failed to set buffer dimensions (%d)", res); |
| 208 | return res; |
| 209 | } |
Shuzhen Wang | 9d5c936 | 2018-08-27 11:39:53 -0700 | [diff] [blame] | 210 | res = native_window_set_buffers_format(outputQueue.get(), |
| 211 | mFormat); |
| 212 | if (res != OK) { |
| 213 | ALOGE("%s: Unable to configure stream buffer format %#x for surfaceId %zu", |
| 214 | __FUNCTION__, mFormat, surfaceId); |
| 215 | return res; |
| 216 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 217 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 218 | sp<IGraphicBufferProducer> gbp = outputQueue->getIGraphicBufferProducer(); |
| 219 | // Connect to the buffer producer |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 220 | sp<OutputListener> listener(new OutputListener(this, gbp)); |
| 221 | IInterface::asBinder(gbp)->linkToDeath(listener); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 222 | res = outputQueue->connect(NATIVE_WINDOW_API_CAMERA, listener); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 223 | if (res != NO_ERROR) { |
| 224 | SP_LOGE("addOutput: failed to connect (%d)", res); |
| 225 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Query consumer side buffer count, and update overall buffer count |
| 229 | int maxConsumerBuffers = 0; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 230 | res = static_cast<ANativeWindow*>(outputQueue.get())->query( |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 231 | outputQueue.get(), |
| 232 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 233 | if (res != OK) { |
| 234 | SP_LOGE("%s: Unable to query consumer undequeued buffer count" |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 235 | " for surface", __FUNCTION__); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 236 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 239 | SP_LOGV("%s: Consumer wants %d buffers, Producer wants %zu", __FUNCTION__, |
| 240 | maxConsumerBuffers, mMaxHalBuffers); |
Emilian Peev | 18e3f37 | 2018-05-22 18:55:01 +0100 | [diff] [blame] | 241 | // The output slot count requirement can change depending on the current amount |
| 242 | // of outputs and incoming buffer consumption rate. To avoid any issues with |
| 243 | // insufficient slots, set their count to the maximum supported. The output |
| 244 | // surface buffer allocation is disabled so no real buffers will get allocated. |
| 245 | size_t totalBufferCount = BufferQueue::NUM_BUFFER_SLOTS; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 246 | res = native_window_set_buffer_count(outputQueue.get(), |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 247 | totalBufferCount); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 248 | if (res != OK) { |
| 249 | SP_LOGE("%s: Unable to set buffer count for surface %p", |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 250 | __FUNCTION__, outputQueue.get()); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 251 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture. |
| 255 | // We need skip these cases as timeout will disable the non-blocking (async) mode. |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 256 | uint64_t usage = 0; |
| 257 | res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(outputQueue.get()), &usage); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 258 | if (!(usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE))) { |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 259 | nsecs_t timeout = mUseHalBufManager ? |
| 260 | kHalBufMgrDequeueBufferTimeout : kNormalDequeueBufferTimeout; |
| 261 | outputQueue->setDequeueTimeout(timeout); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 264 | res = gbp->allowAllocation(false); |
| 265 | if (res != OK) { |
| 266 | SP_LOGE("%s: Failed to turn off allocation for outputQueue", __FUNCTION__); |
| 267 | return res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // Add new entry into mOutputs |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 271 | mOutputs[surfaceId] = gbp; |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 272 | mOutputSurfaces[surfaceId] = outputQueue; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 273 | mConsumerBufferCount[surfaceId] = maxConsumerBuffers; |
Emilian Peev | f63f1d9 | 2018-11-06 16:33:29 +0000 | [diff] [blame] | 274 | if (mConsumerBufferCount[surfaceId] > mMaxHalBuffers) { |
| 275 | SP_LOGW("%s: Consumer buffer count %zu larger than max. Hal buffers: %zu", __FUNCTION__, |
| 276 | mConsumerBufferCount[surfaceId], mMaxHalBuffers); |
| 277 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 278 | mNotifiers[gbp] = listener; |
| 279 | mOutputSlots[gbp] = std::make_unique<OutputSlots>(totalBufferCount); |
| 280 | |
| 281 | mMaxConsumerBuffers += maxConsumerBuffers; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 282 | return NO_ERROR; |
| 283 | } |
| 284 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 285 | status_t Camera3StreamSplitter::removeOutput(size_t surfaceId) { |
| 286 | ATRACE_CALL(); |
| 287 | Mutex::Autolock lock(mMutex); |
| 288 | |
| 289 | status_t res = removeOutputLocked(surfaceId); |
| 290 | if (res != OK) { |
| 291 | SP_LOGE("%s: removeOutputLocked failed %d", __FUNCTION__, res); |
| 292 | return res; |
| 293 | } |
| 294 | |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 295 | if (mAcquiredInputBuffers < mMaxConsumerBuffers) { |
| 296 | res = mConsumer->setMaxAcquiredBufferCount(mMaxConsumerBuffers); |
| 297 | if (res != OK) { |
| 298 | SP_LOGE("%s: setMaxAcquiredBufferCount failed %d", __FUNCTION__, res); |
| 299 | return res; |
| 300 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return res; |
| 304 | } |
| 305 | |
| 306 | status_t Camera3StreamSplitter::removeOutputLocked(size_t surfaceId) { |
| 307 | if (mOutputs[surfaceId] == nullptr) { |
| 308 | SP_LOGE("%s: output surface is not present!", __FUNCTION__); |
| 309 | return BAD_VALUE; |
| 310 | } |
| 311 | |
| 312 | sp<IGraphicBufferProducer> gbp = mOutputs[surfaceId]; |
| 313 | //Search and decrement the ref. count of any buffers that are |
| 314 | //still attached to the removed surface. |
| 315 | std::vector<uint64_t> pendingBufferIds; |
| 316 | auto& outputSlots = *mOutputSlots[gbp]; |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 317 | for (size_t i = 0; i < outputSlots.size(); i++) { |
| 318 | if (outputSlots[i] != nullptr) { |
| 319 | pendingBufferIds.push_back(outputSlots[i]->getId()); |
| 320 | auto rc = gbp->detachBuffer(i); |
| 321 | if (rc != NO_ERROR) { |
| 322 | //Buffers that fail to detach here will be scheduled for detach in the |
| 323 | //input buffer queue and the rest of the registered outputs instead. |
| 324 | //This will help ensure that camera stops accessing buffers that still |
| 325 | //can get referenced by the disconnected output. |
| 326 | mDetachedBuffers.emplace(outputSlots[i]->getId()); |
| 327 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | mOutputs[surfaceId] = nullptr; |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 331 | mOutputSurfaces[surfaceId] = nullptr; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 332 | mOutputSlots[gbp] = nullptr; |
| 333 | for (const auto &id : pendingBufferIds) { |
| 334 | decrementBufRefCountLocked(id, surfaceId); |
| 335 | } |
| 336 | |
| 337 | auto res = IInterface::asBinder(gbp)->unlinkToDeath(mNotifiers[gbp]); |
| 338 | if (res != OK) { |
| 339 | SP_LOGE("%s: Failed to unlink producer death listener: %d ", __FUNCTION__, res); |
| 340 | return res; |
| 341 | } |
| 342 | |
| 343 | res = gbp->disconnect(NATIVE_WINDOW_API_CAMERA); |
| 344 | if (res != OK) { |
| 345 | SP_LOGE("%s: Unable disconnect from producer interface: %d ", __FUNCTION__, res); |
| 346 | return res; |
| 347 | } |
| 348 | |
| 349 | mNotifiers[gbp] = nullptr; |
Emilian Peev | f63f1d9 | 2018-11-06 16:33:29 +0000 | [diff] [blame] | 350 | mMaxConsumerBuffers -= mConsumerBufferCount[surfaceId]; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 351 | mConsumerBufferCount[surfaceId] = 0; |
| 352 | |
| 353 | return res; |
| 354 | } |
| 355 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 356 | status_t Camera3StreamSplitter::outputBufferLocked(const sp<IGraphicBufferProducer>& output, |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 357 | const BufferItem& bufferItem, size_t surfaceId) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 358 | ATRACE_CALL(); |
| 359 | status_t res; |
| 360 | IGraphicBufferProducer::QueueBufferInput queueInput( |
| 361 | bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp, |
| 362 | bufferItem.mDataSpace, bufferItem.mCrop, |
| 363 | static_cast<int32_t>(bufferItem.mScalingMode), |
| 364 | bufferItem.mTransform, bufferItem.mFence); |
| 365 | |
| 366 | IGraphicBufferProducer::QueueBufferOutput queueOutput; |
| 367 | |
| 368 | uint64_t bufferId = bufferItem.mGraphicBuffer->getId(); |
| 369 | const BufferTracker& tracker = *(mBuffers[bufferId]); |
| 370 | int slot = getSlotForOutputLocked(output, tracker.getBuffer()); |
| 371 | |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 372 | if (mOutputSurfaces[surfaceId] != nullptr) { |
| 373 | sp<ANativeWindow> anw = mOutputSurfaces[surfaceId]; |
| 374 | camera3::Camera3Stream::queueHDRMetadata( |
| 375 | bufferItem.mGraphicBuffer->getNativeBuffer()->handle, anw, mDynamicRangeProfile); |
| 376 | } else { |
| 377 | SP_LOGE("%s: Invalid surface id: %zu!", __FUNCTION__, surfaceId); |
| 378 | } |
| 379 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 380 | // In case the output BufferQueue has its own lock, if we hold splitter lock while calling |
| 381 | // queueBuffer (which will try to acquire the output lock), the output could be holding its |
| 382 | // own lock calling releaseBuffer (which will try to acquire the splitter lock), running into |
| 383 | // circular lock situation. |
| 384 | mMutex.unlock(); |
| 385 | res = output->queueBuffer(slot, queueInput, &queueOutput); |
| 386 | mMutex.lock(); |
| 387 | |
| 388 | SP_LOGV("%s: Queuing buffer to buffer queue %p slot %d returns %d", |
| 389 | __FUNCTION__, output.get(), slot, res); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 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 (mOutputSlots[output] == nullptr) { |
| 393 | return res; |
| 394 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 395 | 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 Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 403 | decrementBufRefCountLocked(bufferItem.mGraphicBuffer->getId(), surfaceId); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 404 | return res; |
| 405 | } |
| 406 | |
| 407 | // If the queued buffer replaces a pending buffer in the async |
| 408 | // queue, no onBufferReleased is called by the buffer queue. |
| 409 | // Proactively trigger the callback to avoid buffer loss. |
| 410 | if (queueOutput.bufferReplaced) { |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 411 | onBufferReplacedLocked(output, surfaceId); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | return res; |
| 415 | } |
| 416 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 417 | std::string Camera3StreamSplitter::getUniqueConsumerName() { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 418 | static volatile int32_t counter = 0; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 419 | return fmt::sprintf("Camera3StreamSplitter-%d", android_atomic_inc(&counter)); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 422 | status_t Camera3StreamSplitter::notifyBufferReleased(const sp<GraphicBuffer>& buffer) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 423 | ATRACE_CALL(); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 424 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 425 | Mutex::Autolock lock(mMutex); |
| 426 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 427 | uint64_t bufferId = buffer->getId(); |
| 428 | std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[bufferId]); |
| 429 | mBuffers.erase(bufferId); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 430 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 431 | return OK; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | status_t Camera3StreamSplitter::attachBufferToOutputs(ANativeWindowBuffer* anb, |
| 435 | const std::vector<size_t>& surface_ids) { |
| 436 | ATRACE_CALL(); |
| 437 | status_t res = OK; |
| 438 | |
| 439 | Mutex::Autolock lock(mMutex); |
| 440 | |
| 441 | sp<GraphicBuffer> gb(static_cast<GraphicBuffer*>(anb)); |
| 442 | uint64_t bufferId = gb->getId(); |
| 443 | |
| 444 | // Initialize buffer tracker for this input buffer |
| 445 | auto tracker = std::make_unique<BufferTracker>(gb, surface_ids); |
| 446 | |
| 447 | for (auto& surface_id : surface_ids) { |
| 448 | sp<IGraphicBufferProducer>& gbp = mOutputs[surface_id]; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 449 | if (gbp.get() == nullptr) { |
| 450 | //Output surface got likely removed by client. |
| 451 | continue; |
| 452 | } |
| 453 | int slot = getSlotForOutputLocked(gbp, gb); |
| 454 | if (slot != BufferItem::INVALID_BUFFER_SLOT) { |
| 455 | //Buffer is already attached to this output surface. |
| 456 | continue; |
| 457 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 458 | //Temporarly Unlock the mutex when trying to attachBuffer to the output |
| 459 | //queue, because attachBuffer could block in case of a slow consumer. If |
| 460 | //we block while holding the lock, onFrameAvailable and onBufferReleased |
| 461 | //will block as well because they need to acquire the same lock. |
| 462 | mMutex.unlock(); |
| 463 | res = gbp->attachBuffer(&slot, gb); |
| 464 | mMutex.lock(); |
| 465 | if (res != OK) { |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 466 | SP_LOGE("%s: Cannot attachBuffer from GraphicBufferProducer %p: %s (%d)", |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 467 | __FUNCTION__, gbp.get(), strerror(-res), res); |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 468 | // TODO: might need to detach/cleanup the already attached buffers before return? |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 469 | return res; |
| 470 | } |
Emilian Peev | 21e79db | 2018-03-16 18:17:57 +0000 | [diff] [blame] | 471 | if ((slot < 0) || (slot > BufferQueue::NUM_BUFFER_SLOTS)) { |
| 472 | SP_LOGE("%s: Slot received %d either bigger than expected maximum %d or negative!", |
| 473 | __FUNCTION__, slot, BufferQueue::NUM_BUFFER_SLOTS); |
| 474 | return BAD_VALUE; |
| 475 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 476 | //During buffer attach 'mMutex' is not held which makes the removal of |
| 477 | //"gbp" possible. Check whether this is the case and continue. |
| 478 | if (mOutputSlots[gbp] == nullptr) { |
| 479 | continue; |
| 480 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 481 | auto& outputSlots = *mOutputSlots[gbp]; |
Emilian Peev | 21e79db | 2018-03-16 18:17:57 +0000 | [diff] [blame] | 482 | if (static_cast<size_t> (slot + 1) > outputSlots.size()) { |
| 483 | outputSlots.resize(slot + 1); |
| 484 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 485 | if (outputSlots[slot] != nullptr) { |
| 486 | // If the buffer is attached to a slot which already contains a buffer, |
| 487 | // the previous buffer will be removed from the output queue. Decrement |
| 488 | // the reference count accordingly. |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 489 | decrementBufRefCountLocked(outputSlots[slot]->getId(), surface_id); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 490 | } |
| 491 | SP_LOGV("%s: Attached buffer %p to slot %d on output %p.",__FUNCTION__, gb.get(), |
| 492 | slot, gbp.get()); |
| 493 | outputSlots[slot] = gb; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 496 | mBuffers[bufferId] = std::move(tracker); |
| 497 | |
| 498 | return res; |
| 499 | } |
| 500 | |
| 501 | void Camera3StreamSplitter::onFrameAvailable(const BufferItem& /*item*/) { |
| 502 | ATRACE_CALL(); |
| 503 | Mutex::Autolock lock(mMutex); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 504 | |
| 505 | // Acquire and detach the buffer from the input |
| 506 | BufferItem bufferItem; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 507 | status_t res = mConsumer->acquireBuffer(&bufferItem, /* presentWhen */ 0); |
| 508 | if (res != NO_ERROR) { |
| 509 | SP_LOGE("%s: Acquiring buffer from input failed (%d)", __FUNCTION__, res); |
| 510 | mOnFrameAvailableRes.store(res); |
| 511 | return; |
| 512 | } |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 513 | |
| 514 | uint64_t bufferId; |
| 515 | if (bufferItem.mGraphicBuffer != nullptr) { |
| 516 | mInputSlots[bufferItem.mSlot] = bufferItem; |
| 517 | } else if (bufferItem.mAcquireCalled) { |
| 518 | bufferItem.mGraphicBuffer = mInputSlots[bufferItem.mSlot].mGraphicBuffer; |
| 519 | mInputSlots[bufferItem.mSlot].mFrameNumber = bufferItem.mFrameNumber; |
| 520 | } else { |
| 521 | SP_LOGE("%s: Invalid input graphic buffer!", __FUNCTION__); |
Yin-Chia Yeh | da20845 | 2019-08-26 15:35:57 -0700 | [diff] [blame] | 522 | mOnFrameAvailableRes.store(BAD_VALUE); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | bufferId = bufferItem.mGraphicBuffer->getId(); |
| 526 | |
| 527 | if (mBuffers.find(bufferId) == mBuffers.end()) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 528 | SP_LOGE("%s: Acquired buffer doesn't exist in attached buffer map", |
| 529 | __FUNCTION__); |
| 530 | mOnFrameAvailableRes.store(INVALID_OPERATION); |
| 531 | return; |
| 532 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 533 | |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 534 | mAcquiredInputBuffers++; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 535 | SP_LOGV("acquired buffer %" PRId64 " from input at slot %d", |
| 536 | bufferItem.mGraphicBuffer->getId(), bufferItem.mSlot); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 537 | |
Emilian Peev | 3bdcdff | 2018-06-25 14:37:29 +0100 | [diff] [blame] | 538 | if (bufferItem.mTransformToDisplayInverse) { |
| 539 | bufferItem.mTransform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 540 | } |
| 541 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 542 | // Attach and queue the buffer to each of the outputs |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 543 | BufferTracker& tracker = *(mBuffers[bufferId]); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 544 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 545 | SP_LOGV("%s: BufferTracker for buffer %" PRId64 ", number of requests %zu", |
| 546 | __FUNCTION__, bufferItem.mGraphicBuffer->getId(), tracker.requestedSurfaces().size()); |
| 547 | for (const auto id : tracker.requestedSurfaces()) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 548 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 549 | if (mOutputs[id] == nullptr) { |
| 550 | //Output surface got likely removed by client. |
| 551 | continue; |
| 552 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 553 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 554 | res = outputBufferLocked(mOutputs[id], bufferItem, id); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 555 | if (res != OK) { |
| 556 | SP_LOGE("%s: outputBufferLocked failed %d", __FUNCTION__, res); |
| 557 | mOnFrameAvailableRes.store(res); |
| 558 | // If we fail to send buffer to certain output, keep sending to |
| 559 | // other outputs. |
| 560 | continue; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 561 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 562 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 563 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 564 | mOnFrameAvailableRes.store(res); |
| 565 | } |
| 566 | |
Yin-Chia Yeh | da20845 | 2019-08-26 15:35:57 -0700 | [diff] [blame] | 567 | void Camera3StreamSplitter::onFrameReplaced(const BufferItem& item) { |
| 568 | ATRACE_CALL(); |
| 569 | onFrameAvailable(item); |
| 570 | } |
| 571 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 572 | void Camera3StreamSplitter::decrementBufRefCountLocked(uint64_t id, size_t surfaceId) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 573 | ATRACE_CALL(); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 574 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 575 | if (mBuffers[id] == nullptr) { |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | size_t referenceCount = mBuffers[id]->decrementReferenceCountLocked(surfaceId); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 580 | if (referenceCount > 0) { |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | // We no longer need to track the buffer now that it is being returned to the |
| 585 | // input. Note that this should happen before we unlock the mutex and call |
| 586 | // releaseBuffer, to avoid the case where the same bufferId is acquired in |
| 587 | // attachBufferToOutputs resulting in a new BufferTracker with same bufferId |
| 588 | // overwrites the current one. |
| 589 | std::unique_ptr<BufferTracker> tracker_ptr = std::move(mBuffers[id]); |
| 590 | mBuffers.erase(id); |
| 591 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 592 | uint64_t bufferId = tracker_ptr->getBuffer()->getId(); |
| 593 | int consumerSlot = -1; |
| 594 | uint64_t frameNumber; |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 595 | auto inputSlot = mInputSlots.begin(); |
| 596 | for (; inputSlot != mInputSlots.end(); inputSlot++) { |
| 597 | if (inputSlot->second.mGraphicBuffer->getId() == bufferId) { |
| 598 | consumerSlot = inputSlot->second.mSlot; |
| 599 | frameNumber = inputSlot->second.mFrameNumber; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
| 603 | if (consumerSlot == -1) { |
| 604 | SP_LOGE("%s: Buffer missing inside input slots!", __FUNCTION__); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 608 | auto detachBuffer = mDetachedBuffers.find(bufferId); |
| 609 | bool detach = (detachBuffer != mDetachedBuffers.end()); |
| 610 | if (detach) { |
| 611 | mDetachedBuffers.erase(detachBuffer); |
| 612 | mInputSlots.erase(inputSlot); |
| 613 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 614 | // Temporarily unlock mutex to avoid circular lock: |
| 615 | // 1. This function holds splitter lock, calls releaseBuffer which triggers |
| 616 | // onBufferReleased in Camera3OutputStream. onBufferReleased waits on the |
| 617 | // OutputStream lock |
| 618 | // 2. Camera3SharedOutputStream::getBufferLocked calls |
| 619 | // attachBufferToOutputs, which holds the stream lock, and waits for the |
| 620 | // splitter lock. |
| 621 | sp<IGraphicBufferConsumer> consumer(mConsumer); |
| 622 | mMutex.unlock(); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 623 | int res = NO_ERROR; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 624 | if (consumer != nullptr) { |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 625 | if (detach) { |
| 626 | res = consumer->detachBuffer(consumerSlot); |
| 627 | } else { |
| 628 | res = consumer->releaseBuffer(consumerSlot, frameNumber, |
| 629 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker_ptr->getMergedFence()); |
| 630 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 631 | } else { |
| 632 | SP_LOGE("%s: consumer has become null!", __FUNCTION__); |
| 633 | } |
| 634 | mMutex.lock(); |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 635 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 636 | if (res != NO_ERROR) { |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 637 | if (detach) { |
| 638 | SP_LOGE("%s: detachBuffer returns %d", __FUNCTION__, res); |
| 639 | } else { |
| 640 | SP_LOGE("%s: releaseBuffer returns %d", __FUNCTION__, res); |
| 641 | } |
Emilian Peev | f130ad7 | 2018-10-11 11:03:07 +0100 | [diff] [blame] | 642 | } else { |
| 643 | if (mAcquiredInputBuffers == 0) { |
| 644 | ALOGW("%s: Acquired input buffer count already at zero!", __FUNCTION__); |
| 645 | } else { |
| 646 | mAcquiredInputBuffers--; |
| 647 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | void Camera3StreamSplitter::onBufferReleasedByOutput( |
| 652 | const sp<IGraphicBufferProducer>& from) { |
| 653 | ATRACE_CALL(); |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 654 | sp<Fence> fence; |
| 655 | |
| 656 | int slot = BufferItem::INVALID_BUFFER_SLOT; |
| 657 | auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage, |
| 658 | nullptr, nullptr); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 659 | Mutex::Autolock lock(mMutex); |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 660 | handleOutputDequeueStatusLocked(res, slot); |
| 661 | if (res != OK) { |
| 662 | return; |
| 663 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 664 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 665 | size_t surfaceId = 0; |
| 666 | bool found = false; |
| 667 | for (const auto& it : mOutputs) { |
| 668 | if (it.second == from) { |
| 669 | found = true; |
| 670 | surfaceId = it.first; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | if (!found) { |
| 675 | SP_LOGV("%s: output surface not registered anymore!", __FUNCTION__); |
| 676 | return; |
| 677 | } |
| 678 | |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 679 | returnOutputBufferLocked(fence, from, surfaceId, slot); |
Shuzhen Wang | a141c5f | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 682 | void Camera3StreamSplitter::onBufferReplacedLocked( |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 683 | const sp<IGraphicBufferProducer>& from, size_t surfaceId) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 684 | ATRACE_CALL(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 685 | sp<Fence> fence; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 686 | |
| 687 | int slot = BufferItem::INVALID_BUFFER_SLOT; |
| 688 | auto res = from->dequeueBuffer(&slot, &fence, mWidth, mHeight, mFormat, mProducerUsage, |
| 689 | nullptr, nullptr); |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 690 | handleOutputDequeueStatusLocked(res, slot); |
| 691 | if (res != OK) { |
Shuzhen Wang | afa8a91 | 2017-03-15 10:51:27 -0700 | [diff] [blame] | 692 | return; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 695 | returnOutputBufferLocked(fence, from, surfaceId, slot); |
| 696 | } |
| 697 | |
| 698 | void Camera3StreamSplitter::returnOutputBufferLocked(const sp<Fence>& fence, |
| 699 | const sp<IGraphicBufferProducer>& from, size_t surfaceId, int slot) { |
| 700 | sp<GraphicBuffer> buffer; |
| 701 | |
| 702 | if (mOutputSlots[from] == nullptr) { |
| 703 | //Output surface got likely removed by client. |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | auto outputSlots = *mOutputSlots[from]; |
| 708 | buffer = outputSlots[slot]; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 709 | BufferTracker& tracker = *(mBuffers[buffer->getId()]); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 710 | // Merge the release fence of the incoming buffer so that the fence we send |
| 711 | // back to the input includes all of the outputs' fences |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 712 | if (fence != nullptr && fence->isValid()) { |
| 713 | tracker.mergeFence(fence); |
| 714 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 715 | |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 716 | auto detachBuffer = mDetachedBuffers.find(buffer->getId()); |
| 717 | bool detach = (detachBuffer != mDetachedBuffers.end()); |
| 718 | if (detach) { |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 719 | auto res = from->detachBuffer(slot); |
Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame] | 720 | if (res == NO_ERROR) { |
| 721 | outputSlots[slot] = nullptr; |
| 722 | } else { |
| 723 | SP_LOGE("%s: detach buffer from output failed (%d)", __FUNCTION__, res); |
| 724 | } |
| 725 | } |
| 726 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 727 | // Check to see if this is the last outstanding reference to this buffer |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 728 | decrementBufRefCountLocked(buffer->getId(), surfaceId); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Emilian Peev | 703e499 | 2018-02-27 11:42:09 +0000 | [diff] [blame] | 731 | void Camera3StreamSplitter::handleOutputDequeueStatusLocked(status_t res, int slot) { |
| 732 | if (res == NO_INIT) { |
| 733 | // If we just discovered that this output has been abandoned, note that, |
| 734 | // but we can't do anything else, since buffer is invalid |
| 735 | onAbandonedLocked(); |
| 736 | } else if (res == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { |
| 737 | SP_LOGE("%s: Producer needs to re-allocate buffer!", __FUNCTION__); |
| 738 | SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__); |
| 739 | } else if (res == IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 740 | SP_LOGE("%s: All slot->buffer mapping should be released!", __FUNCTION__); |
| 741 | SP_LOGE("%s: This should not happen with buffer allocation disabled!", __FUNCTION__); |
| 742 | } else if (res == NO_MEMORY) { |
| 743 | SP_LOGE("%s: No free buffers", __FUNCTION__); |
| 744 | } else if (res == WOULD_BLOCK) { |
| 745 | SP_LOGE("%s: Dequeue call will block", __FUNCTION__); |
| 746 | } else if (res != OK || (slot == BufferItem::INVALID_BUFFER_SLOT)) { |
| 747 | SP_LOGE("%s: dequeue buffer from output failed (%d)", __FUNCTION__, res); |
| 748 | } |
| 749 | } |
| 750 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 751 | void Camera3StreamSplitter::onAbandonedLocked() { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 752 | // If this is called from binderDied callback, it means the app process |
| 753 | // holding the binder has died. CameraService will be notified of the binder |
| 754 | // death, and camera device will be closed, which in turn calls |
| 755 | // disconnect(). |
| 756 | // |
| 757 | // If this is called from onBufferReleasedByOutput or onFrameAvailable, one |
| 758 | // consumer being abanoned shouldn't impact the other consumer. So we won't |
| 759 | // stop the buffer flow. |
| 760 | // |
| 761 | // In both cases, we don't need to do anything here. |
| 762 | SP_LOGV("One of my outputs has abandoned me"); |
| 763 | } |
| 764 | |
| 765 | int Camera3StreamSplitter::getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp, |
| 766 | const sp<GraphicBuffer>& gb) { |
| 767 | auto& outputSlots = *mOutputSlots[gbp]; |
| 768 | |
| 769 | for (size_t i = 0; i < outputSlots.size(); i++) { |
| 770 | if (outputSlots[i] == gb) { |
| 771 | return (int)i; |
| 772 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 773 | } |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 774 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 775 | SP_LOGV("%s: Cannot find slot for gb %p on output %p", __FUNCTION__, gb.get(), |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 776 | gbp.get()); |
| 777 | return BufferItem::INVALID_BUFFER_SLOT; |
| 778 | } |
| 779 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 780 | Camera3StreamSplitter::OutputListener::OutputListener( |
| 781 | wp<Camera3StreamSplitter> splitter, |
| 782 | wp<IGraphicBufferProducer> output) |
| 783 | : mSplitter(splitter), mOutput(output) {} |
| 784 | |
| 785 | void Camera3StreamSplitter::OutputListener::onBufferReleased() { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 786 | ATRACE_CALL(); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 787 | sp<Camera3StreamSplitter> splitter = mSplitter.promote(); |
| 788 | sp<IGraphicBufferProducer> output = mOutput.promote(); |
| 789 | if (splitter != nullptr && output != nullptr) { |
| 790 | splitter->onBufferReleasedByOutput(output); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | void Camera3StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) { |
| 795 | sp<Camera3StreamSplitter> splitter = mSplitter.promote(); |
| 796 | if (splitter != nullptr) { |
| 797 | Mutex::Autolock lock(splitter->mMutex); |
| 798 | splitter->onAbandonedLocked(); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | Camera3StreamSplitter::BufferTracker::BufferTracker( |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 803 | const sp<GraphicBuffer>& buffer, const std::vector<size_t>& requestedSurfaces) |
| 804 | : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mRequestedSurfaces(requestedSurfaces), |
| 805 | mReferenceCount(requestedSurfaces.size()) {} |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 806 | |
| 807 | void Camera3StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) { |
| 808 | mMergedFence = Fence::merge(String8("Camera3StreamSplitter"), mMergedFence, with); |
| 809 | } |
| 810 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 811 | size_t Camera3StreamSplitter::BufferTracker::decrementReferenceCountLocked(size_t surfaceId) { |
| 812 | const auto& it = std::find(mRequestedSurfaces.begin(), mRequestedSurfaces.end(), surfaceId); |
| 813 | if (it == mRequestedSurfaces.end()) { |
| 814 | return mReferenceCount; |
| 815 | } else { |
| 816 | mRequestedSurfaces.erase(it); |
| 817 | } |
| 818 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 819 | if (mReferenceCount > 0) |
| 820 | --mReferenceCount; |
| 821 | return mReferenceCount; |
| 822 | } |
| 823 | |
| 824 | } // namespace android |