Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1 | /* |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016-2018 The Android Open Source Project |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 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 "Camera3SharedOutputStream.h" |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | namespace camera3 { |
| 22 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 23 | const size_t Camera3SharedOutputStream::kMaxOutputs; |
| 24 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 25 | Camera3SharedOutputStream::Camera3SharedOutputStream(int id, |
| 26 | const std::vector<sp<Surface>>& surfaces, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 27 | uint32_t width, uint32_t height, int format, |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 28 | uint64_t consumerUsage, android_dataspace dataSpace, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 29 | camera3_stream_rotation_t rotation, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 30 | nsecs_t timestampOffset, const String8& physicalCameraId, |
| 31 | int setId) : |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 32 | Camera3OutputStream(id, CAMERA3_STREAM_OUTPUT, width, height, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 33 | format, dataSpace, rotation, physicalCameraId, |
| 34 | consumerUsage, timestampOffset, setId) { |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 35 | size_t consumerCount = std::min(surfaces.size(), kMaxOutputs); |
| 36 | if (surfaces.size() > consumerCount) { |
| 37 | ALOGE("%s: Trying to add more consumers than the maximum ", __func__); |
| 38 | } |
| 39 | for (size_t i = 0; i < consumerCount; i++) { |
| 40 | mSurfaces[i] = surfaces[i]; |
| 41 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Camera3SharedOutputStream::~Camera3SharedOutputStream() { |
| 45 | disconnectLocked(); |
| 46 | } |
| 47 | |
| 48 | status_t Camera3SharedOutputStream::connectStreamSplitterLocked() { |
| 49 | status_t res = OK; |
| 50 | |
| 51 | mStreamSplitter = new Camera3StreamSplitter(); |
| 52 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 53 | uint64_t usage; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 54 | getEndpointUsage(&usage); |
| 55 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 56 | std::unordered_map<size_t, sp<Surface>> initialSurfaces; |
| 57 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 58 | if (mSurfaces[i] != nullptr) { |
| 59 | initialSurfaces.emplace(i, mSurfaces[i]); |
| 60 | } |
| 61 | } |
| 62 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 63 | res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera3_stream::max_buffers, |
Shuzhen Wang | 2f074ce | 2018-08-27 11:39:53 -0700 | [diff] [blame] | 64 | getWidth(), getHeight(), getFormat(), &mConsumer); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 65 | if (res != OK) { |
| 66 | ALOGE("%s: Failed to connect to stream splitter: %s(%d)", |
| 67 | __FUNCTION__, strerror(-res), res); |
| 68 | return res; |
| 69 | } |
| 70 | |
| 71 | return res; |
| 72 | } |
| 73 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 74 | status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 75 | Mutex::Autolock l(mLock); |
| 76 | status_t res = OK; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 77 | const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer)); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 78 | |
| 79 | if (mStreamSplitter != nullptr) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 80 | res = mStreamSplitter->notifyBufferReleased(buffer); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const { |
| 87 | Mutex::Autolock l(mLock); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 88 | if (surface_id >= kMaxOutputs) { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | return (mSurfaces[surface_id] == nullptr); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 95 | status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 96 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 97 | if (surfaces.size() == 0) { |
| 98 | ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 99 | return INVALID_OPERATION; |
| 100 | } |
| 101 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 102 | status_t ret = OK; |
| 103 | for (auto& surface : surfaces) { |
| 104 | if (surface == nullptr) { |
| 105 | ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__); |
| 106 | return INVALID_OPERATION; |
| 107 | } |
| 108 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 109 | ssize_t id = getNextSurfaceIdLocked(); |
| 110 | if (id < 0) { |
| 111 | ALOGE("%s: No surface ids available!", __func__); |
| 112 | return NO_MEMORY; |
| 113 | } |
| 114 | |
| 115 | mSurfaces[id] = surface; |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 116 | |
| 117 | // Only call addOutput if the splitter has been connected. |
| 118 | if (mStreamSplitter != nullptr) { |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 119 | ret = mStreamSplitter->addOutput(id, surface); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 120 | if (ret != OK) { |
| 121 | ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret); |
| 122 | return ret; |
| 123 | |
| 124 | } |
| 125 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 126 | } |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 127 | return ret; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 130 | status_t Camera3SharedOutputStream::getBufferLocked(camera3_stream_buffer *buffer, |
| 131 | const std::vector<size_t>& surface_ids) { |
| 132 | ANativeWindowBuffer* anb; |
| 133 | int fenceFd = -1; |
| 134 | |
| 135 | status_t res; |
| 136 | res = getBufferLockedCommon(&anb, &fenceFd); |
| 137 | if (res != OK) { |
| 138 | return res; |
| 139 | } |
| 140 | |
Yin-Chia Yeh | d5cd5ff | 2018-10-01 14:43:04 -0700 | [diff] [blame^] | 141 | // TODO: need to refactor this to support requestStreamBuffers API |
| 142 | // Need to wait until processCaptureResult to decide the source buffer |
| 143 | // to attach to output... |
| 144 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 145 | // Attach the buffer to the splitter output queues. This could block if |
| 146 | // the output queue doesn't have any empty slot. So unlock during the course |
| 147 | // of attachBufferToOutputs. |
| 148 | sp<Camera3StreamSplitter> splitter = mStreamSplitter; |
| 149 | mLock.unlock(); |
| 150 | res = splitter->attachBufferToOutputs(anb, surface_ids); |
| 151 | mLock.lock(); |
| 152 | if (res != OK) { |
| 153 | ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)", |
| 154 | __FUNCTION__, mId, strerror(-res), res); |
| 155 | // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING, |
| 156 | // let prepareNextBuffer handle the error.) |
| 157 | if (res == NO_INIT && mState == STATE_CONFIGURED) { |
| 158 | mState = STATE_ABANDONED; |
| 159 | } |
| 160 | |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * FenceFD now owned by HAL except in case of error, |
| 166 | * in which case we reassign it to acquire_fence |
| 167 | */ |
| 168 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
| 169 | /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true); |
| 170 | |
| 171 | return OK; |
| 172 | } |
| 173 | |
| 174 | status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer, |
| 175 | ANativeWindowBuffer* buffer, int anwReleaseFence) { |
| 176 | status_t res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence); |
| 177 | |
| 178 | // After queuing buffer to the internal consumer queue, check whether the buffer is |
| 179 | // successfully queued to the output queues. |
| 180 | if (res == OK) { |
| 181 | res = mStreamSplitter->getOnFrameAvailableResult(); |
| 182 | if (res != OK) { |
| 183 | ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res); |
| 184 | } |
| 185 | } else { |
| 186 | ALOGE("%s: queueBufer failed %d", __FUNCTION__, res); |
| 187 | } |
| 188 | |
| 189 | return res; |
| 190 | } |
| 191 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 192 | status_t Camera3SharedOutputStream::configureQueueLocked() { |
| 193 | status_t res; |
| 194 | |
| 195 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | res = connectStreamSplitterLocked(); |
| 200 | if (res != OK) { |
| 201 | ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res); |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | res = configureConsumerQueueLocked(); |
| 206 | if (res != OK) { |
| 207 | ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res); |
| 208 | return res; |
| 209 | } |
| 210 | |
| 211 | return OK; |
| 212 | } |
| 213 | |
| 214 | status_t Camera3SharedOutputStream::disconnectLocked() { |
| 215 | status_t res; |
| 216 | res = Camera3OutputStream::disconnectLocked(); |
| 217 | |
| 218 | if (mStreamSplitter != nullptr) { |
| 219 | mStreamSplitter->disconnect(); |
| 220 | } |
| 221 | |
| 222 | return res; |
| 223 | } |
| 224 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 225 | status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 226 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 227 | status_t res = OK; |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 228 | uint64_t u = 0; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 229 | |
| 230 | if (mConsumer == nullptr) { |
| 231 | // Called before shared buffer queue is constructed. |
| 232 | *usage = getPresetConsumerUsage(); |
| 233 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 234 | for (size_t id = 0; id < kMaxOutputs; id++) { |
| 235 | if (mSurfaces[id] != nullptr) { |
| 236 | res = getEndpointUsageForSurface(&u, mSurfaces[id]); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 237 | *usage |= u; |
| 238 | } |
| 239 | } |
| 240 | } else { |
| 241 | // Called after shared buffer queue is constructed. |
| 242 | res = getEndpointUsageForSurface(&u, mConsumer); |
| 243 | *usage |= u; |
| 244 | } |
| 245 | |
| 246 | return res; |
| 247 | } |
| 248 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 249 | ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() { |
| 250 | ssize_t id = -1; |
| 251 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 252 | if (mSurfaces[i] == nullptr) { |
| 253 | id = i; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return id; |
| 259 | } |
| 260 | |
| 261 | ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) { |
| 262 | Mutex::Autolock l(mLock); |
| 263 | ssize_t id = -1; |
| 264 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 265 | if (mSurfaces[i] == surface) { |
| 266 | id = i; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return id; |
| 272 | } |
| 273 | |
| 274 | status_t Camera3SharedOutputStream::revertPartialUpdateLocked( |
| 275 | const KeyedVector<sp<Surface>, size_t> &removedSurfaces, |
| 276 | const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) { |
| 277 | status_t ret = OK; |
| 278 | |
| 279 | for (size_t i = 0; i < attachedSurfaces.size(); i++) { |
| 280 | size_t index = attachedSurfaces.valueAt(i); |
| 281 | if (mStreamSplitter != nullptr) { |
| 282 | ret = mStreamSplitter->removeOutput(index); |
| 283 | if (ret != OK) { |
| 284 | return UNKNOWN_ERROR; |
| 285 | } |
| 286 | } |
| 287 | mSurfaces[index] = nullptr; |
| 288 | } |
| 289 | |
| 290 | for (size_t i = 0; i < removedSurfaces.size(); i++) { |
| 291 | size_t index = removedSurfaces.valueAt(i); |
| 292 | if (mStreamSplitter != nullptr) { |
| 293 | ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i)); |
| 294 | if (ret != OK) { |
| 295 | return UNKNOWN_ERROR; |
| 296 | } |
| 297 | } |
| 298 | mSurfaces[index] = removedSurfaces.keyAt(i); |
| 299 | } |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces, |
| 305 | const std::vector<OutputStreamInfo> &outputInfo, |
| 306 | const std::vector<size_t> &removedSurfaceIds, |
| 307 | KeyedVector<sp<Surface>, size_t> *outputMap) { |
| 308 | status_t ret = OK; |
| 309 | Mutex::Autolock l(mLock); |
| 310 | |
| 311 | if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) || |
| 312 | (outputSurfaces.size() > kMaxOutputs)) { |
| 313 | return BAD_VALUE; |
| 314 | } |
| 315 | |
| 316 | uint64_t usage; |
| 317 | getEndpointUsage(&usage); |
| 318 | KeyedVector<sp<Surface>, size_t> removedSurfaces; |
| 319 | //Check whether the new surfaces are compatible. |
| 320 | for (const auto &infoIt : outputInfo) { |
| 321 | bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false; |
| 322 | bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) || |
| 323 | (static_cast<uint32_t> (infoIt.height) != getHeight())) ? |
| 324 | true : false; |
| 325 | if ((imgReaderUsage && sizeMismatch) || |
| 326 | (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) || |
| 327 | (infoIt.dataSpace != getDataSpace() && |
| 328 | infoIt.dataSpace != getOriginalDataSpace())) { |
| 329 | ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x " |
| 330 | " don't match source stream format: 0x%x dataSpace: 0x%x", __FUNCTION__, |
| 331 | infoIt.format, infoIt.dataSpace, getFormat(), getDataSpace()); |
| 332 | return BAD_VALUE; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | //First remove all absent outputs |
| 337 | for (const auto &it : removedSurfaceIds) { |
| 338 | if (mStreamSplitter != nullptr) { |
| 339 | ret = mStreamSplitter->removeOutput(it); |
| 340 | if (ret != OK) { |
| 341 | ALOGE("%s: failed with error code %d", __FUNCTION__, ret); |
| 342 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 343 | if (res != OK) { |
| 344 | return res; |
| 345 | } |
| 346 | return ret; |
| 347 | |
| 348 | } |
| 349 | } |
| 350 | mSurfaces[it] = nullptr; |
| 351 | removedSurfaces.add(mSurfaces[it], it); |
| 352 | } |
| 353 | |
| 354 | //Next add the new outputs |
| 355 | for (const auto &it : outputSurfaces) { |
| 356 | ssize_t surfaceId = getNextSurfaceIdLocked(); |
| 357 | if (surfaceId < 0) { |
| 358 | ALOGE("%s: No more available output slots!", __FUNCTION__); |
| 359 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 360 | if (res != OK) { |
| 361 | return res; |
| 362 | } |
| 363 | return NO_MEMORY; |
| 364 | } |
| 365 | if (mStreamSplitter != nullptr) { |
| 366 | ret = mStreamSplitter->addOutput(surfaceId, it); |
| 367 | if (ret != OK) { |
| 368 | ALOGE("%s: failed with error code %d", __FUNCTION__, ret); |
| 369 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 370 | if (res != OK) { |
| 371 | return res; |
| 372 | } |
| 373 | return ret; |
| 374 | } |
| 375 | } |
| 376 | mSurfaces[surfaceId] = it; |
| 377 | outputMap->add(it, surfaceId); |
| 378 | } |
| 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 383 | } // namespace camera3 |
| 384 | |
| 385 | } // namespace android |