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