ZslProcessor3.cpp revision cb0652e5a850b2fcd919e977247e87239efaf70e
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 "Camera2-ZslProcessor3" 18#define ATRACE_TAG ATRACE_TAG_CAMERA 19//#define LOG_NDEBUG 0 20//#define LOG_NNDEBUG 0 21 22#ifdef LOG_NNDEBUG 23#define ALOGVV(...) ALOGV(__VA_ARGS__) 24#else 25#define ALOGVV(...) ((void)0) 26#endif 27 28#include <inttypes.h> 29 30#include <utils/Log.h> 31#include <utils/Trace.h> 32#include <gui/Surface.h> 33 34#include "common/CameraDeviceBase.h" 35#include "api1/Camera2Client.h" 36#include "api1/client2/CaptureSequencer.h" 37#include "api1/client2/ZslProcessor3.h" 38#include "device3/Camera3Device.h" 39 40namespace android { 41namespace camera2 { 42 43ZslProcessor3::ZslProcessor3( 44 sp<Camera2Client> client, 45 wp<CaptureSequencer> sequencer): 46 Thread(false), 47 mState(RUNNING), 48 mClient(client), 49 mSequencer(sequencer), 50 mId(client->getCameraId()), 51 mZslStreamId(NO_STREAM), 52 mFrameListHead(0), 53 mZslQueueHead(0), 54 mZslQueueTail(0) { 55 mZslQueue.insertAt(0, kZslBufferDepth); 56 mFrameList.insertAt(0, kFrameListDepth); 57 sp<CaptureSequencer> captureSequencer = mSequencer.promote(); 58 if (captureSequencer != 0) captureSequencer->setZslProcessor(this); 59} 60 61ZslProcessor3::~ZslProcessor3() { 62 ALOGV("%s: Exit", __FUNCTION__); 63 deleteStream(); 64} 65 66void ZslProcessor3::onResultAvailable(const CaptureResult &result) { 67 ATRACE_CALL(); 68 ALOGV("%s:", __FUNCTION__); 69 Mutex::Autolock l(mInputMutex); 70 camera_metadata_ro_entry_t entry; 71 entry = result.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); 72 nsecs_t timestamp = entry.data.i64[0]; 73 (void)timestamp; 74 ALOGVV("Got preview metadata for timestamp %" PRId64, timestamp); 75 76 if (mState != RUNNING) return; 77 78 mFrameList.editItemAt(mFrameListHead) = result.mMetadata; 79 mFrameListHead = (mFrameListHead + 1) % kFrameListDepth; 80} 81 82status_t ZslProcessor3::updateStream(const Parameters ¶ms) { 83 ATRACE_CALL(); 84 ALOGV("%s: Configuring ZSL streams", __FUNCTION__); 85 status_t res; 86 87 Mutex::Autolock l(mInputMutex); 88 89 sp<Camera2Client> client = mClient.promote(); 90 if (client == 0) { 91 ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId); 92 return INVALID_OPERATION; 93 } 94 sp<Camera3Device> device = 95 static_cast<Camera3Device*>(client->getCameraDevice().get()); 96 if (device == 0) { 97 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId); 98 return INVALID_OPERATION; 99 } 100 101 if (mZslStreamId != NO_STREAM) { 102 // Check if stream parameters have to change 103 uint32_t currentWidth, currentHeight; 104 res = device->getStreamInfo(mZslStreamId, 105 ¤tWidth, ¤tHeight, 0); 106 if (res != OK) { 107 ALOGE("%s: Camera %d: Error querying capture output stream info: " 108 "%s (%d)", __FUNCTION__, 109 client->getCameraId(), strerror(-res), res); 110 return res; 111 } 112 if (currentWidth != (uint32_t)params.fastInfo.arrayWidth || 113 currentHeight != (uint32_t)params.fastInfo.arrayHeight) { 114 ALOGV("%s: Camera %d: Deleting stream %d since the buffer " 115 "dimensions changed", 116 __FUNCTION__, client->getCameraId(), mZslStreamId); 117 res = device->deleteStream(mZslStreamId); 118 if (res == -EBUSY) { 119 ALOGV("%s: Camera %d: Device is busy, call updateStream again " 120 " after it becomes idle", __FUNCTION__, mId); 121 return res; 122 } else if(res != OK) { 123 ALOGE("%s: Camera %d: Unable to delete old output stream " 124 "for ZSL: %s (%d)", __FUNCTION__, 125 client->getCameraId(), strerror(-res), res); 126 return res; 127 } 128 mZslStreamId = NO_STREAM; 129 } 130 } 131 132 if (mZslStreamId == NO_STREAM) { 133 // Create stream for HAL production 134 // TODO: Sort out better way to select resolution for ZSL 135 136 // Note that format specified internally in Camera3ZslStream 137 res = device->createZslStream( 138 params.fastInfo.arrayWidth, params.fastInfo.arrayHeight, 139 kZslBufferDepth, 140 &mZslStreamId, 141 &mZslStream); 142 if (res != OK) { 143 ALOGE("%s: Camera %d: Can't create ZSL stream: " 144 "%s (%d)", __FUNCTION__, client->getCameraId(), 145 strerror(-res), res); 146 return res; 147 } 148 } 149 client->registerFrameListener(Camera2Client::kPreviewRequestIdStart, 150 Camera2Client::kPreviewRequestIdEnd, 151 this); 152 153 return OK; 154} 155 156status_t ZslProcessor3::deleteStream() { 157 ATRACE_CALL(); 158 status_t res; 159 160 Mutex::Autolock l(mInputMutex); 161 162 if (mZslStreamId != NO_STREAM) { 163 sp<Camera2Client> client = mClient.promote(); 164 if (client == 0) { 165 ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId); 166 return INVALID_OPERATION; 167 } 168 169 sp<Camera3Device> device = 170 reinterpret_cast<Camera3Device*>(client->getCameraDevice().get()); 171 if (device == 0) { 172 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId); 173 return INVALID_OPERATION; 174 } 175 176 res = device->deleteStream(mZslStreamId); 177 if (res != OK) { 178 ALOGE("%s: Camera %d: Cannot delete ZSL output stream %d: " 179 "%s (%d)", __FUNCTION__, client->getCameraId(), 180 mZslStreamId, strerror(-res), res); 181 return res; 182 } 183 184 mZslStreamId = NO_STREAM; 185 } 186 return OK; 187} 188 189int ZslProcessor3::getStreamId() const { 190 Mutex::Autolock l(mInputMutex); 191 return mZslStreamId; 192} 193 194status_t ZslProcessor3::pushToReprocess(int32_t requestId) { 195 ALOGV("%s: Send in reprocess request with id %d", 196 __FUNCTION__, requestId); 197 Mutex::Autolock l(mInputMutex); 198 status_t res; 199 sp<Camera2Client> client = mClient.promote(); 200 201 if (client == 0) { 202 ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId); 203 return INVALID_OPERATION; 204 } 205 206 IF_ALOGV() { 207 dumpZslQueue(-1); 208 } 209 210 size_t metadataIdx; 211 nsecs_t candidateTimestamp = getCandidateTimestampLocked(&metadataIdx); 212 213 if (candidateTimestamp == -1) { 214 ALOGE("%s: Could not find good candidate for ZSL reprocessing", 215 __FUNCTION__); 216 return NOT_ENOUGH_DATA; 217 } 218 219 res = mZslStream->enqueueInputBufferByTimestamp(candidateTimestamp, 220 /*actualTimestamp*/NULL); 221 222 if (res == mZslStream->NO_BUFFER_AVAILABLE) { 223 ALOGV("%s: No ZSL buffers yet", __FUNCTION__); 224 return NOT_ENOUGH_DATA; 225 } else if (res != OK) { 226 ALOGE("%s: Unable to push buffer for reprocessing: %s (%d)", 227 __FUNCTION__, strerror(-res), res); 228 return res; 229 } 230 231 { 232 CameraMetadata request = mFrameList[metadataIdx]; 233 234 // Verify that the frame is reasonable for reprocessing 235 236 camera_metadata_entry_t entry; 237 entry = request.find(ANDROID_CONTROL_AE_STATE); 238 if (entry.count == 0) { 239 ALOGE("%s: ZSL queue frame has no AE state field!", 240 __FUNCTION__); 241 return BAD_VALUE; 242 } 243 if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED && 244 entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) { 245 ALOGV("%s: ZSL queue frame AE state is %d, need full capture", 246 __FUNCTION__, entry.data.u8[0]); 247 return NOT_ENOUGH_DATA; 248 } 249 250 uint8_t requestType = ANDROID_REQUEST_TYPE_REPROCESS; 251 res = request.update(ANDROID_REQUEST_TYPE, 252 &requestType, 1); 253 int32_t inputStreams[1] = 254 { mZslStreamId }; 255 if (res == OK) request.update(ANDROID_REQUEST_INPUT_STREAMS, 256 inputStreams, 1); 257 // TODO: Shouldn't we also update the latest preview frame? 258 int32_t outputStreams[1] = 259 { client->getCaptureStreamId() }; 260 if (res == OK) request.update(ANDROID_REQUEST_OUTPUT_STREAMS, 261 outputStreams, 1); 262 res = request.update(ANDROID_REQUEST_ID, 263 &requestId, 1); 264 265 if (res != OK ) { 266 ALOGE("%s: Unable to update frame to a reprocess request", 267 __FUNCTION__); 268 return INVALID_OPERATION; 269 } 270 271 res = client->stopStream(); 272 if (res != OK) { 273 ALOGE("%s: Camera %d: Unable to stop preview for ZSL capture: " 274 "%s (%d)", 275 __FUNCTION__, client->getCameraId(), strerror(-res), res); 276 return INVALID_OPERATION; 277 } 278 279 // Flush device to clear out all in-flight requests pending in HAL. 280 res = client->getCameraDevice()->flush(); 281 if (res != OK) { 282 ALOGE("%s: Camera %d: Failed to flush device: " 283 "%s (%d)", 284 __FUNCTION__, client->getCameraId(), strerror(-res), res); 285 return res; 286 } 287 288 // Update JPEG settings 289 { 290 SharedParameters::Lock l(client->getParameters()); 291 res = l.mParameters.updateRequestJpeg(&request); 292 if (res != OK) { 293 ALOGE("%s: Camera %d: Unable to update JPEG entries of ZSL " 294 "capture request: %s (%d)", __FUNCTION__, 295 client->getCameraId(), 296 strerror(-res), res); 297 return res; 298 } 299 } 300 301 mLatestCapturedRequest = request; 302 res = client->getCameraDevice()->capture(request); 303 if (res != OK ) { 304 ALOGE("%s: Unable to send ZSL reprocess request to capture: %s" 305 " (%d)", __FUNCTION__, strerror(-res), res); 306 return res; 307 } 308 309 mState = LOCKED; 310 } 311 312 return OK; 313} 314 315status_t ZslProcessor3::clearZslQueue() { 316 Mutex::Autolock l(mInputMutex); 317 // If in middle of capture, can't clear out queue 318 if (mState == LOCKED) return OK; 319 320 return clearZslQueueLocked(); 321} 322 323status_t ZslProcessor3::clearZslQueueLocked() { 324 if (mZslStream != 0) { 325 return mZslStream->clearInputRingBuffer(); 326 } 327 return OK; 328} 329 330void ZslProcessor3::dump(int fd, const Vector<String16>& /*args*/) const { 331 Mutex::Autolock l(mInputMutex); 332 if (!mLatestCapturedRequest.isEmpty()) { 333 String8 result(" Latest ZSL capture request:\n"); 334 write(fd, result.string(), result.size()); 335 mLatestCapturedRequest.dump(fd, 2, 6); 336 } else { 337 String8 result(" Latest ZSL capture request: none yet\n"); 338 write(fd, result.string(), result.size()); 339 } 340 dumpZslQueue(fd); 341} 342 343bool ZslProcessor3::threadLoop() { 344 // TODO: remove dependency on thread. For now, shut thread down right 345 // away. 346 return false; 347} 348 349void ZslProcessor3::dumpZslQueue(int fd) const { 350 String8 header("ZSL queue contents:"); 351 String8 indent(" "); 352 ALOGV("%s", header.string()); 353 if (fd != -1) { 354 header = indent + header + "\n"; 355 write(fd, header.string(), header.size()); 356 } 357 for (size_t i = 0; i < mZslQueue.size(); i++) { 358 const ZslPair &queueEntry = mZslQueue[i]; 359 nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp; 360 camera_metadata_ro_entry_t entry; 361 nsecs_t frameTimestamp = 0; 362 int frameAeState = -1; 363 if (!queueEntry.frame.isEmpty()) { 364 entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP); 365 if (entry.count > 0) frameTimestamp = entry.data.i64[0]; 366 entry = queueEntry.frame.find(ANDROID_CONTROL_AE_STATE); 367 if (entry.count > 0) frameAeState = entry.data.u8[0]; 368 } 369 String8 result = 370 String8::format(" %zu: b: %" PRId64 "\tf: %" PRId64 ", AE state: %d", i, 371 bufferTimestamp, frameTimestamp, frameAeState); 372 ALOGV("%s", result.string()); 373 if (fd != -1) { 374 result = indent + result + "\n"; 375 write(fd, result.string(), result.size()); 376 } 377 378 } 379} 380 381nsecs_t ZslProcessor3::getCandidateTimestampLocked(size_t* metadataIdx) const { 382 /** 383 * Find the smallest timestamp we know about so far 384 * - ensure that aeState is either converged or locked 385 */ 386 387 size_t idx = 0; 388 nsecs_t minTimestamp = -1; 389 390 size_t emptyCount = mFrameList.size(); 391 392 for (size_t j = 0; j < mFrameList.size(); j++) { 393 const CameraMetadata &frame = mFrameList[j]; 394 if (!frame.isEmpty()) { 395 396 emptyCount--; 397 398 camera_metadata_ro_entry_t entry; 399 entry = frame.find(ANDROID_SENSOR_TIMESTAMP); 400 if (entry.count == 0) { 401 ALOGE("%s: Can't find timestamp in frame!", 402 __FUNCTION__); 403 continue; 404 } 405 nsecs_t frameTimestamp = entry.data.i64[0]; 406 if (minTimestamp > frameTimestamp || minTimestamp == -1) { 407 408 entry = frame.find(ANDROID_CONTROL_AE_STATE); 409 410 if (entry.count == 0) { 411 /** 412 * This is most likely a HAL bug. The aeState field is 413 * mandatory, so it should always be in a metadata packet. 414 */ 415 ALOGW("%s: ZSL queue frame has no AE state field!", 416 __FUNCTION__); 417 continue; 418 } 419 if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED && 420 entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) { 421 ALOGVV("%s: ZSL queue frame AE state is %d, need " 422 "full capture", __FUNCTION__, entry.data.u8[0]); 423 continue; 424 } 425 426 minTimestamp = frameTimestamp; 427 idx = j; 428 } 429 430 ALOGVV("%s: Saw timestamp %" PRId64, __FUNCTION__, frameTimestamp); 431 } 432 } 433 434 if (emptyCount == mFrameList.size()) { 435 /** 436 * This could be mildly bad and means our ZSL was triggered before 437 * there were any frames yet received by the camera framework. 438 * 439 * This is a fairly corner case which can happen under: 440 * + a user presses the shutter button real fast when the camera starts 441 * (startPreview followed immediately by takePicture). 442 * + burst capture case (hitting shutter button as fast possible) 443 * 444 * If this happens in steady case (preview running for a while, call 445 * a single takePicture) then this might be a fwk bug. 446 */ 447 ALOGW("%s: ZSL queue has no metadata frames", __FUNCTION__); 448 } 449 450 ALOGV("%s: Candidate timestamp %" PRId64 " (idx %zu), empty frames: %zu", 451 __FUNCTION__, minTimestamp, idx, emptyCount); 452 453 if (metadataIdx) { 454 *metadataIdx = idx; 455 } 456 457 return minTimestamp; 458} 459 460void ZslProcessor3::onBufferAcquired(const BufferInfo& /*bufferInfo*/) { 461 // Intentionally left empty 462 // Although theoretically we could use this to get better dump info 463} 464 465void ZslProcessor3::onBufferReleased(const BufferInfo& bufferInfo) { 466 Mutex::Autolock l(mInputMutex); 467 468 // ignore output buffers 469 if (bufferInfo.mOutput) { 470 return; 471 } 472 473 // TODO: Verify that the buffer is in our queue by looking at timestamp 474 // theoretically unnecessary unless we change the following assumptions: 475 // -- only 1 buffer reprocessed at a time (which is the case now) 476 477 // Erase entire ZSL queue since we've now completed the capture and preview 478 // is stopped. 479 // 480 // We need to guarantee that if we do two back-to-back captures, 481 // the second won't use a buffer that's older/the same as the first, which 482 // is theoretically possible if we don't clear out the queue and the 483 // selection criteria is something like 'newest'. Clearing out the queue 484 // on a completed capture ensures we'll only use new data. 485 ALOGV("%s: Memory optimization, clearing ZSL queue", 486 __FUNCTION__); 487 clearZslQueueLocked(); 488 489 // Required so we accept more ZSL requests 490 mState = RUNNING; 491} 492 493}; // namespace camera2 494}; // namespace android 495