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