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