Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "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, |
| 30 | nsecs_t timestampOffset, int setId) : |
| 31 | Camera3OutputStream(id, CAMERA3_STREAM_OUTPUT, width, height, |
| 32 | format, dataSpace, rotation, consumerUsage, |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame^] | 33 | timestampOffset, setId) { |
| 34 | size_t consumerCount = std::min(surfaces.size(), kMaxOutputs); |
| 35 | if (surfaces.size() > consumerCount) { |
| 36 | ALOGE("%s: Trying to add more consumers than the maximum ", __func__); |
| 37 | } |
| 38 | for (size_t i = 0; i < consumerCount; i++) { |
| 39 | mSurfaces[i] = surfaces[i]; |
| 40 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Camera3SharedOutputStream::~Camera3SharedOutputStream() { |
| 44 | disconnectLocked(); |
| 45 | } |
| 46 | |
| 47 | status_t Camera3SharedOutputStream::connectStreamSplitterLocked() { |
| 48 | status_t res = OK; |
| 49 | |
| 50 | mStreamSplitter = new Camera3StreamSplitter(); |
| 51 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 52 | uint64_t usage; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 53 | getEndpointUsage(&usage); |
| 54 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame^] | 55 | std::unordered_map<size_t, sp<Surface>> initialSurfaces; |
| 56 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 57 | if (mSurfaces[i] != nullptr) { |
| 58 | initialSurfaces.emplace(i, mSurfaces[i]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | android::PixelFormat format = isFormatOverridden() ? getOriginalFormat() : getFormat(); |
| 63 | res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera3_stream::max_buffers, |
| 64 | getWidth(), getHeight(), format, &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 | |
| 141 | // Attach the buffer to the splitter output queues. This could block if |
| 142 | // the output queue doesn't have any empty slot. So unlock during the course |
| 143 | // of attachBufferToOutputs. |
| 144 | sp<Camera3StreamSplitter> splitter = mStreamSplitter; |
| 145 | mLock.unlock(); |
| 146 | res = splitter->attachBufferToOutputs(anb, surface_ids); |
| 147 | mLock.lock(); |
| 148 | if (res != OK) { |
| 149 | ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)", |
| 150 | __FUNCTION__, mId, strerror(-res), res); |
| 151 | // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING, |
| 152 | // let prepareNextBuffer handle the error.) |
| 153 | if (res == NO_INIT && mState == STATE_CONFIGURED) { |
| 154 | mState = STATE_ABANDONED; |
| 155 | } |
| 156 | |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * FenceFD now owned by HAL except in case of error, |
| 162 | * in which case we reassign it to acquire_fence |
| 163 | */ |
| 164 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
| 165 | /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true); |
| 166 | |
| 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer, |
| 171 | ANativeWindowBuffer* buffer, int anwReleaseFence) { |
| 172 | status_t res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence); |
| 173 | |
| 174 | // After queuing buffer to the internal consumer queue, check whether the buffer is |
| 175 | // successfully queued to the output queues. |
| 176 | if (res == OK) { |
| 177 | res = mStreamSplitter->getOnFrameAvailableResult(); |
| 178 | if (res != OK) { |
| 179 | ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res); |
| 180 | } |
| 181 | } else { |
| 182 | ALOGE("%s: queueBufer failed %d", __FUNCTION__, res); |
| 183 | } |
| 184 | |
| 185 | return res; |
| 186 | } |
| 187 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 188 | status_t Camera3SharedOutputStream::configureQueueLocked() { |
| 189 | status_t res; |
| 190 | |
| 191 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 192 | return res; |
| 193 | } |
| 194 | |
| 195 | res = connectStreamSplitterLocked(); |
| 196 | if (res != OK) { |
| 197 | ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res); |
| 198 | return res; |
| 199 | } |
| 200 | |
| 201 | res = configureConsumerQueueLocked(); |
| 202 | if (res != OK) { |
| 203 | ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res); |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | return OK; |
| 208 | } |
| 209 | |
| 210 | status_t Camera3SharedOutputStream::disconnectLocked() { |
| 211 | status_t res; |
| 212 | res = Camera3OutputStream::disconnectLocked(); |
| 213 | |
| 214 | if (mStreamSplitter != nullptr) { |
| 215 | mStreamSplitter->disconnect(); |
| 216 | } |
| 217 | |
| 218 | return res; |
| 219 | } |
| 220 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 221 | status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 222 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 223 | status_t res = OK; |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 224 | uint64_t u = 0; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 225 | |
| 226 | if (mConsumer == nullptr) { |
| 227 | // Called before shared buffer queue is constructed. |
| 228 | *usage = getPresetConsumerUsage(); |
| 229 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame^] | 230 | for (size_t id = 0; id < kMaxOutputs; id++) { |
| 231 | if (mSurfaces[id] != nullptr) { |
| 232 | res = getEndpointUsageForSurface(&u, mSurfaces[id]); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 233 | *usage |= u; |
| 234 | } |
| 235 | } |
| 236 | } else { |
| 237 | // Called after shared buffer queue is constructed. |
| 238 | res = getEndpointUsageForSurface(&u, mConsumer); |
| 239 | *usage |= u; |
| 240 | } |
| 241 | |
| 242 | return res; |
| 243 | } |
| 244 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame^] | 245 | ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() { |
| 246 | ssize_t id = -1; |
| 247 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 248 | if (mSurfaces[i] == nullptr) { |
| 249 | id = i; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return id; |
| 255 | } |
| 256 | |
| 257 | ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) { |
| 258 | Mutex::Autolock l(mLock); |
| 259 | ssize_t id = -1; |
| 260 | for (size_t i = 0; i < kMaxOutputs; i++) { |
| 261 | if (mSurfaces[i] == surface) { |
| 262 | id = i; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return id; |
| 268 | } |
| 269 | |
| 270 | status_t Camera3SharedOutputStream::revertPartialUpdateLocked( |
| 271 | const KeyedVector<sp<Surface>, size_t> &removedSurfaces, |
| 272 | const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) { |
| 273 | status_t ret = OK; |
| 274 | |
| 275 | for (size_t i = 0; i < attachedSurfaces.size(); i++) { |
| 276 | size_t index = attachedSurfaces.valueAt(i); |
| 277 | if (mStreamSplitter != nullptr) { |
| 278 | ret = mStreamSplitter->removeOutput(index); |
| 279 | if (ret != OK) { |
| 280 | return UNKNOWN_ERROR; |
| 281 | } |
| 282 | } |
| 283 | mSurfaces[index] = nullptr; |
| 284 | } |
| 285 | |
| 286 | for (size_t i = 0; i < removedSurfaces.size(); i++) { |
| 287 | size_t index = removedSurfaces.valueAt(i); |
| 288 | if (mStreamSplitter != nullptr) { |
| 289 | ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i)); |
| 290 | if (ret != OK) { |
| 291 | return UNKNOWN_ERROR; |
| 292 | } |
| 293 | } |
| 294 | mSurfaces[index] = removedSurfaces.keyAt(i); |
| 295 | } |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces, |
| 301 | const std::vector<OutputStreamInfo> &outputInfo, |
| 302 | const std::vector<size_t> &removedSurfaceIds, |
| 303 | KeyedVector<sp<Surface>, size_t> *outputMap) { |
| 304 | status_t ret = OK; |
| 305 | Mutex::Autolock l(mLock); |
| 306 | |
| 307 | if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) || |
| 308 | (outputSurfaces.size() > kMaxOutputs)) { |
| 309 | return BAD_VALUE; |
| 310 | } |
| 311 | |
| 312 | uint64_t usage; |
| 313 | getEndpointUsage(&usage); |
| 314 | KeyedVector<sp<Surface>, size_t> removedSurfaces; |
| 315 | //Check whether the new surfaces are compatible. |
| 316 | for (const auto &infoIt : outputInfo) { |
| 317 | bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false; |
| 318 | bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) || |
| 319 | (static_cast<uint32_t> (infoIt.height) != getHeight())) ? |
| 320 | true : false; |
| 321 | if ((imgReaderUsage && sizeMismatch) || |
| 322 | (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) || |
| 323 | (infoIt.dataSpace != getDataSpace() && |
| 324 | infoIt.dataSpace != getOriginalDataSpace())) { |
| 325 | ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x " |
| 326 | " don't match source stream format: 0x%x dataSpace: 0x%x", __FUNCTION__, |
| 327 | infoIt.format, infoIt.dataSpace, getFormat(), getDataSpace()); |
| 328 | return BAD_VALUE; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | //First remove all absent outputs |
| 333 | for (const auto &it : removedSurfaceIds) { |
| 334 | if (mStreamSplitter != nullptr) { |
| 335 | ret = mStreamSplitter->removeOutput(it); |
| 336 | if (ret != OK) { |
| 337 | ALOGE("%s: failed with error code %d", __FUNCTION__, ret); |
| 338 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 339 | if (res != OK) { |
| 340 | return res; |
| 341 | } |
| 342 | return ret; |
| 343 | |
| 344 | } |
| 345 | } |
| 346 | mSurfaces[it] = nullptr; |
| 347 | removedSurfaces.add(mSurfaces[it], it); |
| 348 | } |
| 349 | |
| 350 | //Next add the new outputs |
| 351 | for (const auto &it : outputSurfaces) { |
| 352 | ssize_t surfaceId = getNextSurfaceIdLocked(); |
| 353 | if (surfaceId < 0) { |
| 354 | ALOGE("%s: No more available output slots!", __FUNCTION__); |
| 355 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 356 | if (res != OK) { |
| 357 | return res; |
| 358 | } |
| 359 | return NO_MEMORY; |
| 360 | } |
| 361 | if (mStreamSplitter != nullptr) { |
| 362 | ret = mStreamSplitter->addOutput(surfaceId, it); |
| 363 | if (ret != OK) { |
| 364 | ALOGE("%s: failed with error code %d", __FUNCTION__, ret); |
| 365 | status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap); |
| 366 | if (res != OK) { |
| 367 | return res; |
| 368 | } |
| 369 | return ret; |
| 370 | } |
| 371 | } |
| 372 | mSurfaces[surfaceId] = it; |
| 373 | outputMap->add(it, surfaceId); |
| 374 | } |
| 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 379 | } // namespace camera3 |
| 380 | |
| 381 | } // namespace android |