1/* 2 * Copyright (C) 2013 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#define LOG_TAG "Camera3-OutputStream" 18#define ATRACE_TAG ATRACE_TAG_CAMERA 19//#define LOG_NDEBUG 0 20 21#include <utils/Log.h> 22#include <utils/Trace.h> 23#include "Camera3OutputStream.h" 24 25#ifndef container_of 26#define container_of(ptr, type, member) \ 27 (type *)((char*)(ptr) - offsetof(type, member)) 28#endif 29 30namespace android { 31 32namespace camera3 { 33 34Camera3OutputStream::Camera3OutputStream(int id, 35 sp<Surface> consumer, 36 uint32_t width, uint32_t height, int format, 37 android_dataspace dataSpace, camera3_stream_rotation_t rotation, 38 nsecs_t timestampOffset, int setId) : 39 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, 40 /*maxSize*/0, format, dataSpace, rotation, setId), 41 mConsumer(consumer), 42 mTransform(0), 43 mTraceFirstBuffer(true), 44 mUseBufferManager(false), 45 mTimestampOffset(timestampOffset), 46 mConsumerUsage(0), 47 mDequeueBufferLatency(kDequeueLatencyBinSize) { 48 49 if (mConsumer == NULL) { 50 ALOGE("%s: Consumer is NULL!", __FUNCTION__); 51 mState = STATE_ERROR; 52 } 53 54 if (setId > CAMERA3_STREAM_SET_ID_INVALID) { 55 mBufferReleasedListener = new BufferReleasedListener(this); 56 } 57} 58 59Camera3OutputStream::Camera3OutputStream(int id, 60 sp<Surface> consumer, 61 uint32_t width, uint32_t height, size_t maxSize, int format, 62 android_dataspace dataSpace, camera3_stream_rotation_t rotation, 63 nsecs_t timestampOffset, int setId) : 64 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize, 65 format, dataSpace, rotation, setId), 66 mConsumer(consumer), 67 mTransform(0), 68 mTraceFirstBuffer(true), 69 mUseMonoTimestamp(false), 70 mUseBufferManager(false), 71 mTimestampOffset(timestampOffset), 72 mConsumerUsage(0), 73 mDequeueBufferLatency(kDequeueLatencyBinSize) { 74 75 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) { 76 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, 77 format); 78 mState = STATE_ERROR; 79 } 80 81 if (mConsumer == NULL) { 82 ALOGE("%s: Consumer is NULL!", __FUNCTION__); 83 mState = STATE_ERROR; 84 } 85 86 if (setId > CAMERA3_STREAM_SET_ID_INVALID) { 87 mBufferReleasedListener = new BufferReleasedListener(this); 88 } 89} 90 91Camera3OutputStream::Camera3OutputStream(int id, 92 uint32_t width, uint32_t height, int format, 93 uint32_t consumerUsage, android_dataspace dataSpace, 94 camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) : 95 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, 96 /*maxSize*/0, format, dataSpace, rotation, setId), 97 mConsumer(nullptr), 98 mTransform(0), 99 mTraceFirstBuffer(true), 100 mUseBufferManager(false), 101 mTimestampOffset(timestampOffset), 102 mConsumerUsage(consumerUsage), 103 mDequeueBufferLatency(kDequeueLatencyBinSize) { 104 // Deferred consumer only support preview surface format now. 105 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 106 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!", 107 __FUNCTION__); 108 mState = STATE_ERROR; 109 } 110 111 // Sanity check for the consumer usage flag. 112 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 && 113 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) { 114 ALOGE("%s: Deferred consumer usage flag is illegal (0x%x)!", __FUNCTION__, consumerUsage); 115 mState = STATE_ERROR; 116 } 117 118 mConsumerName = String8("Deferred"); 119 if (setId > CAMERA3_STREAM_SET_ID_INVALID) { 120 mBufferReleasedListener = new BufferReleasedListener(this); 121 } 122 123} 124 125Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type, 126 uint32_t width, uint32_t height, 127 int format, 128 android_dataspace dataSpace, 129 camera3_stream_rotation_t rotation, 130 uint32_t consumerUsage, nsecs_t timestampOffset, 131 int setId) : 132 Camera3IOStreamBase(id, type, width, height, 133 /*maxSize*/0, 134 format, dataSpace, rotation, setId), 135 mTransform(0), 136 mTraceFirstBuffer(true), 137 mUseMonoTimestamp(false), 138 mUseBufferManager(false), 139 mTimestampOffset(timestampOffset), 140 mConsumerUsage(consumerUsage), 141 mDequeueBufferLatency(kDequeueLatencyBinSize) { 142 143 if (setId > CAMERA3_STREAM_SET_ID_INVALID) { 144 mBufferReleasedListener = new BufferReleasedListener(this); 145 } 146 147 // Subclasses expected to initialize mConsumer themselves 148} 149 150 151Camera3OutputStream::~Camera3OutputStream() { 152 disconnectLocked(); 153} 154 155status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer, 156 const std::vector<size_t>&) { 157 ATRACE_CALL(); 158 159 ANativeWindowBuffer* anb; 160 int fenceFd = -1; 161 162 status_t res; 163 res = getBufferLockedCommon(&anb, &fenceFd); 164 if (res != OK) { 165 return res; 166 } 167 168 /** 169 * FenceFD now owned by HAL except in case of error, 170 * in which case we reassign it to acquire_fence 171 */ 172 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, 173 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true); 174 175 return OK; 176} 177 178status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer, 179 ANativeWindowBuffer* buffer, int anwReleaseFence) { 180 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence); 181} 182 183status_t Camera3OutputStream::returnBufferLocked( 184 const camera3_stream_buffer &buffer, 185 nsecs_t timestamp) { 186 ATRACE_CALL(); 187 188 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true); 189 190 if (res != OK) { 191 return res; 192 } 193 194 mLastTimestamp = timestamp; 195 mFrameCount++; 196 197 return OK; 198} 199 200status_t Camera3OutputStream::returnBufferCheckedLocked( 201 const camera3_stream_buffer &buffer, 202 nsecs_t timestamp, 203 bool output, 204 /*out*/ 205 sp<Fence> *releaseFenceOut) { 206 207 (void)output; 208 ALOG_ASSERT(output, "Expected output to be true"); 209 210 status_t res; 211 212 // Fence management - always honor release fence from HAL 213 sp<Fence> releaseFence = new Fence(buffer.release_fence); 214 int anwReleaseFence = releaseFence->dup(); 215 216 /** 217 * Release the lock briefly to avoid deadlock with 218 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this 219 * thread will go into StreamingProcessor::onFrameAvailable) during 220 * queueBuffer 221 */ 222 sp<ANativeWindow> currentConsumer = mConsumer; 223 mLock.unlock(); 224 225 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle); 226 /** 227 * Return buffer back to ANativeWindow 228 */ 229 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) { 230 // Cancel buffer 231 ALOGW("A frame is dropped for stream %d", mId); 232 res = currentConsumer->cancelBuffer(currentConsumer.get(), 233 anwBuffer, 234 anwReleaseFence); 235 if (res != OK) { 236 ALOGE("%s: Stream %d: Error cancelling buffer to native window:" 237 " %s (%d)", __FUNCTION__, mId, strerror(-res), res); 238 } 239 240 notifyBufferReleased(anwBuffer); 241 if (mUseBufferManager) { 242 // Return this buffer back to buffer manager. 243 mBufferReleasedListener->onBufferReleased(); 244 } 245 } else { 246 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) { 247 { 248 char traceLog[48]; 249 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId); 250 ATRACE_NAME(traceLog); 251 } 252 mTraceFirstBuffer = false; 253 } 254 255 /* Certain consumers (such as AudioSource or HardwareComposer) use 256 * MONOTONIC time, causing time misalignment if camera timestamp is 257 * in BOOTTIME. Do the conversion if necessary. */ 258 res = native_window_set_buffers_timestamp(mConsumer.get(), 259 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp); 260 if (res != OK) { 261 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", 262 __FUNCTION__, mId, strerror(-res), res); 263 return res; 264 } 265 266 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence); 267 if (res != OK) { 268 ALOGE("%s: Stream %d: Error queueing buffer to native window: " 269 "%s (%d)", __FUNCTION__, mId, strerror(-res), res); 270 } 271 } 272 mLock.lock(); 273 274 // Once a valid buffer has been returned to the queue, can no longer 275 // dequeue all buffers for preallocation. 276 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) { 277 mStreamUnpreparable = true; 278 } 279 280 if (res != OK) { 281 close(anwReleaseFence); 282 } 283 284 *releaseFenceOut = releaseFence; 285 286 return res; 287} 288 289void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const { 290 (void) args; 291 String8 lines; 292 lines.appendFormat(" Stream[%d]: Output\n", mId); 293 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string()); 294 write(fd, lines.string(), lines.size()); 295 296 Camera3IOStreamBase::dump(fd, args); 297 298 mDequeueBufferLatency.dump(fd, 299 " DequeueBuffer latency histogram:"); 300} 301 302status_t Camera3OutputStream::setTransform(int transform) { 303 ATRACE_CALL(); 304 Mutex::Autolock l(mLock); 305 return setTransformLocked(transform); 306} 307 308status_t Camera3OutputStream::setTransformLocked(int transform) { 309 status_t res = OK; 310 if (mState == STATE_ERROR) { 311 ALOGE("%s: Stream in error state", __FUNCTION__); 312 return INVALID_OPERATION; 313 } 314 315 mTransform = transform; 316 if (mState == STATE_CONFIGURED) { 317 res = native_window_set_buffers_transform(mConsumer.get(), 318 transform); 319 if (res != OK) { 320 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", 321 __FUNCTION__, transform, strerror(-res), res); 322 } 323 } 324 return res; 325} 326 327status_t Camera3OutputStream::configureQueueLocked() { 328 status_t res; 329 330 mTraceFirstBuffer = true; 331 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { 332 return res; 333 } 334 335 if ((res = configureConsumerQueueLocked()) != OK) { 336 return res; 337 } 338 339 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture. 340 // We need skip these cases as timeout will disable the non-blocking (async) mode. 341 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) { 342 mConsumer->setDequeueTimeout(kDequeueBufferTimeout); 343 } 344 345 return OK; 346} 347 348status_t Camera3OutputStream::configureConsumerQueueLocked() { 349 status_t res; 350 351 mTraceFirstBuffer = true; 352 353 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL"); 354 355 // Configure consumer-side ANativeWindow interface. The listener may be used 356 // to notify buffer manager (if it is used) of the returned buffers. 357 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, 358 /*listener*/mBufferReleasedListener, 359 /*reportBufferRemoval*/true); 360 if (res != OK) { 361 ALOGE("%s: Unable to connect to native window for stream %d", 362 __FUNCTION__, mId); 363 return res; 364 } 365 366 mConsumerName = mConsumer->getConsumerName(); 367 368 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage); 369 if (res != OK) { 370 ALOGE("%s: Unable to configure usage %08x for stream %d", 371 __FUNCTION__, camera3_stream::usage, mId); 372 return res; 373 } 374 375 res = native_window_set_scaling_mode(mConsumer.get(), 376 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); 377 if (res != OK) { 378 ALOGE("%s: Unable to configure stream scaling: %s (%d)", 379 __FUNCTION__, strerror(-res), res); 380 return res; 381 } 382 383 if (mMaxSize == 0) { 384 // For buffers of known size 385 res = native_window_set_buffers_dimensions(mConsumer.get(), 386 camera3_stream::width, camera3_stream::height); 387 } else { 388 // For buffers with bounded size 389 res = native_window_set_buffers_dimensions(mConsumer.get(), 390 mMaxSize, 1); 391 } 392 if (res != OK) { 393 ALOGE("%s: Unable to configure stream buffer dimensions" 394 " %d x %d (maxSize %zu) for stream %d", 395 __FUNCTION__, camera3_stream::width, camera3_stream::height, 396 mMaxSize, mId); 397 return res; 398 } 399 res = native_window_set_buffers_format(mConsumer.get(), 400 camera3_stream::format); 401 if (res != OK) { 402 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d", 403 __FUNCTION__, camera3_stream::format, mId); 404 return res; 405 } 406 407 res = native_window_set_buffers_data_space(mConsumer.get(), 408 camera3_stream::data_space); 409 if (res != OK) { 410 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d", 411 __FUNCTION__, camera3_stream::data_space, mId); 412 return res; 413 } 414 415 int maxConsumerBuffers; 416 res = static_cast<ANativeWindow*>(mConsumer.get())->query( 417 mConsumer.get(), 418 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); 419 if (res != OK) { 420 ALOGE("%s: Unable to query consumer undequeued" 421 " buffer count for stream %d", __FUNCTION__, mId); 422 return res; 423 } 424 425 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__, 426 maxConsumerBuffers, camera3_stream::max_buffers); 427 if (camera3_stream::max_buffers == 0) { 428 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1", 429 __FUNCTION__, camera3_stream::max_buffers); 430 return INVALID_OPERATION; 431 } 432 433 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers; 434 mHandoutTotalBufferCount = 0; 435 mFrameCount = 0; 436 mLastTimestamp = 0; 437 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream()); 438 439 res = native_window_set_buffer_count(mConsumer.get(), 440 mTotalBufferCount); 441 if (res != OK) { 442 ALOGE("%s: Unable to set buffer count for stream %d", 443 __FUNCTION__, mId); 444 return res; 445 } 446 447 res = native_window_set_buffers_transform(mConsumer.get(), 448 mTransform); 449 if (res != OK) { 450 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", 451 __FUNCTION__, mTransform, strerror(-res), res); 452 return res; 453 } 454 455 /** 456 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires 457 * buffers to be statically allocated for internal static buffer registration, while the 458 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only 459 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer 460 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some 461 * HAL3.2 devices may not support the dynamic buffer registeration. 462 */ 463 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) { 464 uint32_t consumerUsage = 0; 465 getEndpointUsage(&consumerUsage); 466 StreamInfo streamInfo( 467 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(), 468 camera3_stream::usage | consumerUsage, mTotalBufferCount, 469 /*isConfigured*/true); 470 wp<Camera3OutputStream> weakThis(this); 471 res = mBufferManager->registerStream(weakThis, 472 streamInfo); 473 if (res == OK) { 474 // Disable buffer allocation for this BufferQueue, buffer manager will take over 475 // the buffer allocation responsibility. 476 mConsumer->getIGraphicBufferProducer()->allowAllocation(false); 477 mUseBufferManager = true; 478 } else { 479 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, " 480 "(error %d %s), fall back to BufferQueue for buffer management!", 481 __FUNCTION__, mId, res, strerror(-res)); 482 } 483 } 484 485 return OK; 486} 487 488status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) { 489 ATRACE_CALL(); 490 status_t res; 491 492 if ((res = getBufferPreconditionCheckLocked()) != OK) { 493 return res; 494 } 495 496 bool gotBufferFromManager = false; 497 498 if (mUseBufferManager) { 499 sp<GraphicBuffer> gb; 500 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd); 501 if (res == OK) { 502 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a 503 // successful return. 504 *anb = gb.get(); 505 res = mConsumer->attachBuffer(*anb); 506 if (res != OK) { 507 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)", 508 __FUNCTION__, mId, strerror(-res), res); 509 return res; 510 } 511 gotBufferFromManager = true; 512 ALOGV("Stream %d: Attached new buffer", getId()); 513 } else if (res == ALREADY_EXISTS) { 514 // Have sufficient free buffers already attached, can just 515 // dequeue from buffer queue 516 ALOGV("Stream %d: Reusing attached buffer", getId()); 517 gotBufferFromManager = false; 518 } else if (res != OK) { 519 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)", 520 __FUNCTION__, mId, strerror(-res), res); 521 return res; 522 } 523 } 524 if (!gotBufferFromManager) { 525 /** 526 * Release the lock briefly to avoid deadlock for below scenario: 527 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring(). 528 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock. 529 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable(). 530 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock 531 * StreamingProcessor lock. 532 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock 533 * and try to lock bufferQueue lock. 534 * Then there is circular locking dependency. 535 */ 536 sp<ANativeWindow> currentConsumer = mConsumer; 537 mLock.unlock(); 538 539 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC); 540 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd); 541 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC); 542 mDequeueBufferLatency.add(dequeueStart, dequeueEnd); 543 544 mLock.lock(); 545 if (res != OK) { 546 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)", 547 __FUNCTION__, mId, strerror(-res), res); 548 549 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING, 550 // let prepareNextBuffer handle the error.) 551 if (res == NO_INIT && mState == STATE_CONFIGURED) { 552 mState = STATE_ABANDONED; 553 } 554 555 return res; 556 } 557 } 558 559 if (res == OK) { 560 std::vector<sp<GraphicBuffer>> removedBuffers; 561 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers); 562 if (res == OK) { 563 onBuffersRemovedLocked(removedBuffers); 564 565 if (mUseBufferManager && removedBuffers.size() > 0) { 566 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size()); 567 } 568 } 569 } 570 571 return res; 572} 573 574status_t Camera3OutputStream::disconnectLocked() { 575 status_t res; 576 577 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { 578 return res; 579 } 580 581 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED 582 // state), don't need change the stream state, return OK. 583 if (mConsumer == nullptr) { 584 return OK; 585 } 586 587 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId()); 588 589 res = native_window_api_disconnect(mConsumer.get(), 590 NATIVE_WINDOW_API_CAMERA); 591 /** 592 * This is not an error. if client calling process dies, the window will 593 * also die and all calls to it will return DEAD_OBJECT, thus it's already 594 * "disconnected" 595 */ 596 if (res == DEAD_OBJECT) { 597 ALOGW("%s: While disconnecting stream %d from native window, the" 598 " native window died from under us", __FUNCTION__, mId); 599 } 600 else if (res != OK) { 601 ALOGE("%s: Unable to disconnect stream %d from native window " 602 "(error %d %s)", 603 __FUNCTION__, mId, res, strerror(-res)); 604 mState = STATE_ERROR; 605 return res; 606 } 607 608 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the 609 // stream at this point should be safe. 610 if (mUseBufferManager) { 611 res = mBufferManager->unregisterStream(getId(), getStreamSetId()); 612 if (res != OK) { 613 ALOGE("%s: Unable to unregister stream %d from buffer manager " 614 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res)); 615 mState = STATE_ERROR; 616 return res; 617 } 618 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as 619 // the stream is still in usable state after this call. 620 mUseBufferManager = false; 621 } 622 623 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG 624 : STATE_CONSTRUCTED; 625 626 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId); 627 mDequeueBufferLatency.reset(); 628 return OK; 629} 630 631status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const { 632 633 status_t res; 634 635 if (mConsumer == nullptr) { 636 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed. 637 *usage = mConsumerUsage; 638 return OK; 639 } 640 641 res = getEndpointUsageForSurface(usage, mConsumer); 642 643 return res; 644} 645 646status_t Camera3OutputStream::getEndpointUsageForSurface(uint32_t *usage, 647 const sp<Surface>& surface) const { 648 status_t res; 649 int32_t u = 0; 650 651 res = static_cast<ANativeWindow*>(surface.get())->query(surface.get(), 652 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u); 653 654 // If an opaque output stream's endpoint is ImageReader, add 655 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used 656 // for the ZSL use case. 657 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set: 658 // 1. GRALLOC_USAGE_HW_TEXTURE 659 // 2. GRALLOC_USAGE_HW_RENDER 660 // 3. GRALLOC_USAGE_HW_COMPOSER 661 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER 662 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && 663 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | 664 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) { 665 u |= GRALLOC_USAGE_HW_CAMERA_ZSL; 666 } 667 668 *usage = u; 669 return res; 670} 671 672bool Camera3OutputStream::isVideoStream() const { 673 uint32_t usage = 0; 674 status_t res = getEndpointUsage(&usage); 675 if (res != OK) { 676 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); 677 return false; 678 } 679 680 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0; 681} 682 683status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) { 684 Mutex::Autolock l(mLock); 685 if (mState != STATE_CONSTRUCTED) { 686 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.", 687 __FUNCTION__); 688 return INVALID_OPERATION; 689 } 690 mBufferManager = bufferManager; 691 692 return OK; 693} 694 695void Camera3OutputStream::BufferReleasedListener::onBufferReleased() { 696 sp<Camera3OutputStream> stream = mParent.promote(); 697 if (stream == nullptr) { 698 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); 699 return; 700 } 701 702 Mutex::Autolock l(stream->mLock); 703 if (!(stream->mUseBufferManager)) { 704 return; 705 } 706 707 ALOGV("Stream %d: Buffer released", stream->getId()); 708 bool shouldFreeBuffer = false; 709 status_t res = stream->mBufferManager->onBufferReleased( 710 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer); 711 if (res != OK) { 712 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__, 713 strerror(-res), res); 714 stream->mState = STATE_ERROR; 715 } 716 717 if (shouldFreeBuffer) { 718 sp<GraphicBuffer> buffer; 719 // Detach and free a buffer (when buffer goes out of scope) 720 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr); 721 if (buffer.get() != nullptr) { 722 stream->mBufferManager->notifyBufferRemoved( 723 stream->getId(), stream->getStreamSetId()); 724 } 725 } 726} 727 728void Camera3OutputStream::onBuffersRemovedLocked( 729 const std::vector<sp<GraphicBuffer>>& removedBuffers) { 730 Camera3StreamBufferFreedListener* callback = mBufferFreedListener; 731 if (callback != nullptr) { 732 for (auto gb : removedBuffers) { 733 callback->onBufferFreed(mId, gb->handle); 734 } 735 } 736} 737 738status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) { 739 Mutex::Autolock l(mLock); 740 return detachBufferLocked(buffer, fenceFd); 741} 742 743status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) { 744 ALOGV("Stream %d: detachBuffer", getId()); 745 if (buffer == nullptr) { 746 return BAD_VALUE; 747 } 748 749 sp<Fence> fence; 750 status_t res = mConsumer->detachNextBuffer(buffer, &fence); 751 if (res == NO_MEMORY) { 752 // This may rarely happen, which indicates that the released buffer was freed by other 753 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the 754 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided, 755 // therefore log a warning. 756 *buffer = 0; 757 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__); 758 } else if (res != OK) { 759 // Treat other errors as abandonment 760 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res); 761 mState = STATE_ABANDONED; 762 return res; 763 } 764 765 if (fenceFd != nullptr) { 766 if (fence!= 0 && fence->isValid()) { 767 *fenceFd = fence->dup(); 768 } else { 769 *fenceFd = -1; 770 } 771 } 772 773 std::vector<sp<GraphicBuffer>> removedBuffers; 774 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers); 775 if (res == OK) { 776 onBuffersRemovedLocked(removedBuffers); 777 } 778 return res; 779} 780 781status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) { 782 return OK; 783} 784 785bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const { 786 Mutex::Autolock l(mLock); 787 788 if (surface_id != 0) { 789 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id); 790 } 791 return mConsumer == nullptr; 792} 793 794status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) { 795 Mutex::Autolock l(mLock); 796 if (consumers.size() != 1) { 797 ALOGE("%s: it's illegal to set %zu consumer surfaces!", 798 __FUNCTION__, consumers.size()); 799 return INVALID_OPERATION; 800 } 801 if (consumers[0] == nullptr) { 802 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__); 803 return INVALID_OPERATION; 804 } 805 806 if (mConsumer != nullptr) { 807 ALOGE("%s: consumer surface was already set!", __FUNCTION__); 808 return INVALID_OPERATION; 809 } 810 811 mConsumer = consumers[0]; 812 return OK; 813} 814 815bool Camera3OutputStream::isConsumedByHWComposer() const { 816 uint32_t usage = 0; 817 status_t res = getEndpointUsage(&usage); 818 if (res != OK) { 819 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); 820 return false; 821 } 822 823 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0; 824} 825 826bool Camera3OutputStream::isConsumedByHWTexture() const { 827 uint32_t usage = 0; 828 status_t res = getEndpointUsage(&usage); 829 if (res != OK) { 830 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); 831 return false; 832 } 833 834 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0; 835} 836 837}; // namespace camera3 838 839}; // namespace android 840