Camera3Stream.cpp revision 4d44cad22ea925a651463f2d51d6586c14d4b787
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-Stream" 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 "device3/Camera3Stream.h" 24#include "device3/StatusTracker.h" 25 26#include <cutils/properties.h> 27 28namespace android { 29 30namespace camera3 { 31 32Camera3Stream::~Camera3Stream() { 33 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 34 if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { 35 statusTracker->removeComponent(mStatusId); 36 } 37} 38 39Camera3Stream* Camera3Stream::cast(camera3_stream *stream) { 40 return static_cast<Camera3Stream*>(stream); 41} 42 43const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) { 44 return static_cast<const Camera3Stream*>(stream); 45} 46 47Camera3Stream::Camera3Stream(int id, 48 camera3_stream_type type, 49 uint32_t width, uint32_t height, size_t maxSize, int format, 50 android_dataspace dataSpace, camera3_stream_rotation_t rotation) : 51 camera3_stream(), 52 mId(id), 53 mName(String8::format("Camera3Stream[%d]", id)), 54 mMaxSize(maxSize), 55 mState(STATE_CONSTRUCTED), 56 mStatusId(StatusTracker::NO_STATUS_ID) { 57 58 camera3_stream::stream_type = type; 59 camera3_stream::width = width; 60 camera3_stream::height = height; 61 camera3_stream::format = format; 62 camera3_stream::data_space = dataSpace; 63 camera3_stream::rotation = rotation; 64 camera3_stream::usage = 0; 65 camera3_stream::max_buffers = 0; 66 camera3_stream::priv = NULL; 67 68 if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) { 69 ALOGE("%s: BLOB format with size == 0", __FUNCTION__); 70 mState = STATE_ERROR; 71 } 72} 73 74int Camera3Stream::getId() const { 75 return mId; 76} 77 78uint32_t Camera3Stream::getWidth() const { 79 return camera3_stream::width; 80} 81 82uint32_t Camera3Stream::getHeight() const { 83 return camera3_stream::height; 84} 85 86int Camera3Stream::getFormat() const { 87 return camera3_stream::format; 88} 89 90android_dataspace Camera3Stream::getDataSpace() const { 91 return camera3_stream::data_space; 92} 93 94camera3_stream* Camera3Stream::startConfiguration() { 95 ATRACE_CALL(); 96 Mutex::Autolock l(mLock); 97 status_t res; 98 99 switch (mState) { 100 case STATE_ERROR: 101 ALOGE("%s: In error state", __FUNCTION__); 102 return NULL; 103 case STATE_CONSTRUCTED: 104 // OK 105 break; 106 case STATE_IN_CONFIG: 107 case STATE_IN_RECONFIG: 108 // Can start config again with no trouble; but don't redo 109 // oldUsage/oldMaxBuffers 110 return this; 111 case STATE_CONFIGURED: 112 if (stream_type == CAMERA3_STREAM_INPUT) { 113 ALOGE("%s: Cannot configure an input stream twice", 114 __FUNCTION__); 115 return NULL; 116 } else if (hasOutstandingBuffersLocked()) { 117 ALOGE("%s: Cannot configure stream; has outstanding buffers", 118 __FUNCTION__); 119 return NULL; 120 } 121 break; 122 default: 123 ALOGE("%s: Unknown state %d", __FUNCTION__, mState); 124 return NULL; 125 } 126 127 oldUsage = camera3_stream::usage; 128 oldMaxBuffers = camera3_stream::max_buffers; 129 130 res = getEndpointUsage(&(camera3_stream::usage)); 131 if (res != OK) { 132 ALOGE("%s: Cannot query consumer endpoint usage!", 133 __FUNCTION__); 134 return NULL; 135 } 136 137 // Stop tracking if currently doing so 138 if (mStatusId != StatusTracker::NO_STATUS_ID) { 139 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 140 if (statusTracker != 0) { 141 statusTracker->removeComponent(mStatusId); 142 } 143 mStatusId = StatusTracker::NO_STATUS_ID; 144 } 145 146 if (mState == STATE_CONSTRUCTED) { 147 mState = STATE_IN_CONFIG; 148 } else { // mState == STATE_CONFIGURED 149 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState); 150 mState = STATE_IN_RECONFIG; 151 } 152 153 return this; 154} 155 156bool Camera3Stream::isConfiguring() const { 157 Mutex::Autolock l(mLock); 158 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG); 159} 160 161status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) { 162 ATRACE_CALL(); 163 Mutex::Autolock l(mLock); 164 switch (mState) { 165 case STATE_ERROR: 166 ALOGE("%s: In error state", __FUNCTION__); 167 return INVALID_OPERATION; 168 case STATE_IN_CONFIG: 169 case STATE_IN_RECONFIG: 170 // OK 171 break; 172 case STATE_CONSTRUCTED: 173 case STATE_CONFIGURED: 174 ALOGE("%s: Cannot finish configuration that hasn't been started", 175 __FUNCTION__); 176 return INVALID_OPERATION; 177 default: 178 ALOGE("%s: Unknown state", __FUNCTION__); 179 return INVALID_OPERATION; 180 } 181 182 // Register for idle tracking 183 sp<StatusTracker> statusTracker = mStatusTracker.promote(); 184 if (statusTracker != 0) { 185 mStatusId = statusTracker->addComponent(); 186 } 187 188 // Check if the stream configuration is unchanged, and skip reallocation if 189 // so. As documented in hardware/camera3.h:configure_streams(). 190 if (mState == STATE_IN_RECONFIG && 191 oldUsage == camera3_stream::usage && 192 oldMaxBuffers == camera3_stream::max_buffers) { 193 mState = STATE_CONFIGURED; 194 return OK; 195 } 196 197 // Reset prepared state, since buffer config has changed, and existing 198 // allocations are no longer valid 199 mPrepared = false; 200 mStreamUnpreparable = false; 201 202 status_t res; 203 res = configureQueueLocked(); 204 if (res != OK) { 205 ALOGE("%s: Unable to configure stream %d queue: %s (%d)", 206 __FUNCTION__, mId, strerror(-res), res); 207 mState = STATE_ERROR; 208 return res; 209 } 210 211 res = registerBuffersLocked(hal3Device); 212 if (res != OK) { 213 ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)", 214 __FUNCTION__, strerror(-res), res); 215 mState = STATE_ERROR; 216 return res; 217 } 218 219 mState = STATE_CONFIGURED; 220 221 return res; 222} 223 224status_t Camera3Stream::cancelConfiguration() { 225 ATRACE_CALL(); 226 Mutex::Autolock l(mLock); 227 switch (mState) { 228 case STATE_ERROR: 229 ALOGE("%s: In error state", __FUNCTION__); 230 return INVALID_OPERATION; 231 case STATE_IN_CONFIG: 232 case STATE_IN_RECONFIG: 233 // OK 234 break; 235 case STATE_CONSTRUCTED: 236 case STATE_CONFIGURED: 237 ALOGE("%s: Cannot cancel configuration that hasn't been started", 238 __FUNCTION__); 239 return INVALID_OPERATION; 240 default: 241 ALOGE("%s: Unknown state", __FUNCTION__); 242 return INVALID_OPERATION; 243 } 244 245 camera3_stream::usage = oldUsage; 246 camera3_stream::max_buffers = oldMaxBuffers; 247 248 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED; 249 return OK; 250} 251 252bool Camera3Stream::isUnpreparable() { 253 ATRACE_CALL(); 254 255 Mutex::Autolock l(mLock); 256 return mStreamUnpreparable; 257} 258 259status_t Camera3Stream::startPrepare() { 260 ATRACE_CALL(); 261 262 Mutex::Autolock l(mLock); 263 status_t res = OK; 264 265 // This function should be only called when the stream is configured already. 266 if (mState != STATE_CONFIGURED) { 267 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED " 268 "state %d", __FUNCTION__, mId, mState); 269 return INVALID_OPERATION; 270 } 271 272 // This function can't be called if the stream has already received filled 273 // buffers 274 if (mStreamUnpreparable) { 275 ALOGE("%s: Stream %d: Can't prepare stream that's already in use", 276 __FUNCTION__, mId); 277 return INVALID_OPERATION; 278 } 279 280 if (getHandoutOutputBufferCountLocked() > 0) { 281 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers", 282 __FUNCTION__, mId); 283 return INVALID_OPERATION; 284 } 285 286 if (mPrepared) return OK; 287 288 size_t bufferCount = getBufferCountLocked(); 289 290 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); 291 mPreparedBufferIdx = 0; 292 293 mState = STATE_PREPARING; 294 295 return NOT_ENOUGH_DATA; 296} 297 298bool Camera3Stream::isPreparing() const { 299 Mutex::Autolock l(mLock); 300 return mState == STATE_PREPARING; 301} 302 303status_t Camera3Stream::prepareNextBuffer() { 304 ATRACE_CALL(); 305 306 Mutex::Autolock l(mLock); 307 status_t res = OK; 308 309 // This function should be only called when the stream is preparing 310 if (mState != STATE_PREPARING) { 311 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING " 312 "state %d", __FUNCTION__, mId, mState); 313 return INVALID_OPERATION; 314 } 315 316 // Get next buffer - this may allocate, and take a while for large buffers 317 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) ); 318 if (res != OK) { 319 ALOGE("%s: Stream %d: Unable to allocate buffer %d during preparation", 320 __FUNCTION__, mId, mPreparedBufferIdx); 321 return NO_INIT; 322 } 323 324 mPreparedBufferIdx++; 325 326 // Check if we still have buffers left to allocate 327 if (mPreparedBufferIdx < mPreparedBuffers.size()) { 328 return NOT_ENOUGH_DATA; 329 } 330 331 // Done with prepare - mark stream as such, and return all buffers 332 // via cancelPrepare 333 mPrepared = true; 334 335 return cancelPrepareLocked(); 336} 337 338status_t Camera3Stream::cancelPrepare() { 339 ATRACE_CALL(); 340 341 Mutex::Autolock l(mLock); 342 343 return cancelPrepareLocked(); 344} 345 346status_t Camera3Stream::cancelPrepareLocked() { 347 status_t res = OK; 348 349 // This function should be only called when the stream is mid-preparing. 350 if (mState != STATE_PREPARING) { 351 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in " 352 "PREPARING state %d", __FUNCTION__, mId, mState); 353 return INVALID_OPERATION; 354 } 355 356 // Return all valid buffers to stream, in ERROR state to indicate 357 // they weren't filled. 358 for (size_t i = 0; i < mPreparedBufferIdx; i++) { 359 mPreparedBuffers.editItemAt(i).release_fence = -1; 360 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 361 returnBufferLocked(mPreparedBuffers[i], 0); 362 } 363 mPreparedBuffers.clear(); 364 mPreparedBufferIdx = 0; 365 366 mState = STATE_CONFIGURED; 367 368 return res; 369} 370 371status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) { 372 ATRACE_CALL(); 373 Mutex::Autolock l(mLock); 374 status_t res = OK; 375 376 // This function should be only called when the stream is configured already. 377 if (mState != STATE_CONFIGURED) { 378 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d", 379 __FUNCTION__, mId, mState); 380 return INVALID_OPERATION; 381 } 382 383 // Wait for new buffer returned back if we are running into the limit. 384 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) { 385 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.", 386 __FUNCTION__, camera3_stream::max_buffers); 387 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); 388 if (res != OK) { 389 if (res == TIMED_OUT) { 390 ALOGE("%s: wait for output buffer return timed out after %lldms", __FUNCTION__, 391 kWaitForBufferDuration / 1000000LL); 392 } 393 return res; 394 } 395 } 396 397 res = getBufferLocked(buffer); 398 if (res == OK) { 399 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true); 400 } 401 402 return res; 403} 404 405status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer, 406 nsecs_t timestamp) { 407 ATRACE_CALL(); 408 Mutex::Autolock l(mLock); 409 410 /** 411 * TODO: Check that the state is valid first. 412 * 413 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED. 414 * >= HAL3.2 CONFIGURED only 415 * 416 * Do this for getBuffer as well. 417 */ 418 status_t res = returnBufferLocked(buffer, timestamp); 419 if (res == OK) { 420 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true); 421 mOutputBufferReturnedSignal.signal(); 422 } 423 424 return res; 425} 426 427status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) { 428 ATRACE_CALL(); 429 Mutex::Autolock l(mLock); 430 status_t res = OK; 431 432 // This function should be only called when the stream is configured already. 433 if (mState != STATE_CONFIGURED) { 434 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d", 435 __FUNCTION__, mId, mState); 436 return INVALID_OPERATION; 437 } 438 439 // Wait for new buffer returned back if we are running into the limit. 440 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) { 441 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.", 442 __FUNCTION__, camera3_stream::max_buffers); 443 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); 444 if (res != OK) { 445 if (res == TIMED_OUT) { 446 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__, 447 kWaitForBufferDuration / 1000000LL); 448 } 449 return res; 450 } 451 } 452 453 res = getInputBufferLocked(buffer); 454 if (res == OK) { 455 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false); 456 } 457 458 return res; 459} 460 461status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) { 462 ATRACE_CALL(); 463 Mutex::Autolock l(mLock); 464 465 status_t res = returnInputBufferLocked(buffer); 466 if (res == OK) { 467 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false); 468 mInputBufferReturnedSignal.signal(); 469 } 470 return res; 471} 472 473status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) { 474 ATRACE_CALL(); 475 Mutex::Autolock l(mLock); 476 477 return getInputBufferProducerLocked(producer); 478} 479 480void Camera3Stream::fireBufferListenersLocked( 481 const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) { 482 List<wp<Camera3StreamBufferListener> >::iterator it, end; 483 484 // TODO: finish implementing 485 486 Camera3StreamBufferListener::BufferInfo info = 487 Camera3StreamBufferListener::BufferInfo(); 488 info.mOutput = output; 489 // TODO: rest of fields 490 491 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); 492 it != end; 493 ++it) { 494 495 sp<Camera3StreamBufferListener> listener = it->promote(); 496 if (listener != 0) { 497 if (acquired) { 498 listener->onBufferAcquired(info); 499 } else { 500 listener->onBufferReleased(info); 501 } 502 } 503 } 504} 505 506bool Camera3Stream::hasOutstandingBuffers() const { 507 ATRACE_CALL(); 508 Mutex::Autolock l(mLock); 509 return hasOutstandingBuffersLocked(); 510} 511 512status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) { 513 Mutex::Autolock l(mLock); 514 sp<StatusTracker> oldTracker = mStatusTracker.promote(); 515 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { 516 oldTracker->removeComponent(mStatusId); 517 } 518 mStatusId = StatusTracker::NO_STATUS_ID; 519 mStatusTracker = statusTracker; 520 521 return OK; 522} 523 524status_t Camera3Stream::disconnect() { 525 ATRACE_CALL(); 526 Mutex::Autolock l(mLock); 527 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId); 528 status_t res = disconnectLocked(); 529 530 if (res == -ENOTCONN) { 531 // "Already disconnected" -- not an error 532 return OK; 533 } else { 534 return res; 535 } 536} 537 538status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) { 539 ATRACE_CALL(); 540 541 /** 542 * >= CAMERA_DEVICE_API_VERSION_3_2: 543 * 544 * camera3_device_t->ops->register_stream_buffers() is not called and must 545 * be NULL. 546 */ 547 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) { 548 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__); 549 550 if (hal3Device->ops->register_stream_buffers != NULL) { 551 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; " 552 "must be set to NULL in camera3_device::ops", __FUNCTION__); 553 return INVALID_OPERATION; 554 } 555 556 return OK; 557 } 558 559 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__); 560 561 status_t res; 562 563 size_t bufferCount = getBufferCountLocked(); 564 565 Vector<buffer_handle_t*> buffers; 566 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount); 567 568 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set(); 569 bufferSet.stream = this; 570 bufferSet.num_buffers = bufferCount; 571 bufferSet.buffers = buffers.editArray(); 572 573 Vector<camera3_stream_buffer_t> streamBuffers; 574 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); 575 576 // Register all buffers with the HAL. This means getting all the buffers 577 // from the stream, providing them to the HAL with the 578 // register_stream_buffers() method, and then returning them back to the 579 // stream in the error state, since they won't have valid data. 580 // 581 // Only registered buffers can be sent to the HAL. 582 583 uint32_t bufferIdx = 0; 584 for (; bufferIdx < bufferCount; bufferIdx++) { 585 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) ); 586 if (res != OK) { 587 ALOGE("%s: Unable to get buffer %d for registration with HAL", 588 __FUNCTION__, bufferIdx); 589 // Skip registering, go straight to cleanup 590 break; 591 } 592 593 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence); 594 fence->waitForever("Camera3Stream::registerBuffers"); 595 596 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer; 597 } 598 if (bufferIdx == bufferCount) { 599 // Got all buffers, register with HAL 600 ALOGV("%s: Registering %zu buffers with camera HAL", 601 __FUNCTION__, bufferCount); 602 ATRACE_BEGIN("camera3->register_stream_buffers"); 603 res = hal3Device->ops->register_stream_buffers(hal3Device, 604 &bufferSet); 605 ATRACE_END(); 606 } 607 608 // Return all valid buffers to stream, in ERROR state to indicate 609 // they weren't filled. 610 for (size_t i = 0; i < bufferIdx; i++) { 611 streamBuffers.editItemAt(i).release_fence = -1; 612 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; 613 returnBufferLocked(streamBuffers[i], 0); 614 } 615 616 mPrepared = true; 617 618 return res; 619} 620 621status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) { 622 ALOGE("%s: This type of stream does not support output", __FUNCTION__); 623 return INVALID_OPERATION; 624} 625status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &, 626 nsecs_t) { 627 ALOGE("%s: This type of stream does not support output", __FUNCTION__); 628 return INVALID_OPERATION; 629} 630status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) { 631 ALOGE("%s: This type of stream does not support input", __FUNCTION__); 632 return INVALID_OPERATION; 633} 634status_t Camera3Stream::returnInputBufferLocked( 635 const camera3_stream_buffer &) { 636 ALOGE("%s: This type of stream does not support input", __FUNCTION__); 637 return INVALID_OPERATION; 638} 639status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) { 640 ALOGE("%s: This type of stream does not support input", __FUNCTION__); 641 return INVALID_OPERATION; 642} 643 644void Camera3Stream::addBufferListener( 645 wp<Camera3StreamBufferListener> listener) { 646 Mutex::Autolock l(mLock); 647 648 List<wp<Camera3StreamBufferListener> >::iterator it, end; 649 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); 650 it != end; 651 ) { 652 if (*it == listener) { 653 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__); 654 return; 655 } 656 it++; 657 } 658 659 mBufferListenerList.push_back(listener); 660} 661 662void Camera3Stream::removeBufferListener( 663 const sp<Camera3StreamBufferListener>& listener) { 664 Mutex::Autolock l(mLock); 665 666 bool erased = true; 667 List<wp<Camera3StreamBufferListener> >::iterator it, end; 668 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); 669 it != end; 670 ) { 671 672 if (*it == listener) { 673 it = mBufferListenerList.erase(it); 674 erased = true; 675 } else { 676 ++it; 677 } 678 } 679 680 if (!erased) { 681 ALOGW("%s: Could not find listener to remove, already removed", 682 __FUNCTION__); 683 } 684} 685 686}; // namespace camera3 687 688}; // namespace android 689