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