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