ZslProcessor.cpp revision e382ee28709b83264a46b09e8f766c5ef42efa35
1/* 2 * Copyright (C) 2012 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-ZslProcessor" 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 <utils/Log.h> 29#include <utils/Trace.h> 30 31#include "ZslProcessor.h" 32#include <gui/SurfaceTextureClient.h> 33#include "../Camera2Device.h" 34#include "../Camera2Client.h" 35 36 37namespace android { 38namespace camera2 { 39 40ZslProcessor::ZslProcessor( 41 wp<Camera2Client> client, 42 wp<CaptureSequencer> sequencer): 43 Thread(false), 44 mState(RUNNING), 45 mClient(client), 46 mSequencer(sequencer), 47 mZslBufferAvailable(false), 48 mZslStreamId(NO_STREAM), 49 mZslReprocessStreamId(NO_STREAM), 50 mFrameListHead(0), 51 mZslQueueHead(0), 52 mZslQueueTail(0) { 53 mZslQueue.insertAt(0, kZslBufferDepth); 54 mFrameList.insertAt(0, kFrameListDepth); 55 sp<CaptureSequencer> captureSequencer = mSequencer.promote(); 56 if (captureSequencer != 0) captureSequencer->setZslProcessor(this); 57} 58 59ZslProcessor::~ZslProcessor() { 60 ALOGV("%s: Exit", __FUNCTION__); 61 deleteStream(); 62} 63 64void ZslProcessor::onFrameAvailable() { 65 Mutex::Autolock l(mInputMutex); 66 if (!mZslBufferAvailable) { 67 mZslBufferAvailable = true; 68 mZslBufferAvailableSignal.signal(); 69 } 70} 71 72void ZslProcessor::onFrameAvailable(int32_t frameId, CameraMetadata &frame) { 73 Mutex::Autolock l(mInputMutex); 74 camera_metadata_entry_t entry; 75 entry = frame.find(ANDROID_SENSOR_TIMESTAMP); 76 nsecs_t timestamp = entry.data.i64[0]; 77 ALOGVV("Got preview frame for timestamp %lld", timestamp); 78 79 if (mState != RUNNING) return; 80 81 mFrameList.editItemAt(mFrameListHead).acquire(frame); 82 mFrameListHead = (mFrameListHead + 1) % kFrameListDepth; 83 84 findMatchesLocked(); 85} 86 87void ZslProcessor::onBufferReleased(buffer_handle_t *handle) { 88 Mutex::Autolock l(mInputMutex); 89 90 // Verify that the buffer is in our queue 91 size_t i = 0; 92 for (; i < mZslQueue.size(); i++) { 93 if (&(mZslQueue[i].buffer.mGraphicBuffer->handle) == handle) break; 94 } 95 if (i == mZslQueue.size()) { 96 ALOGW("%s: Released buffer %p not found in queue", 97 __FUNCTION__, handle); 98 } 99 100 // Erase entire ZSL queue since we've now completed the capture and preview 101 // is stopped. 102 clearZslQueueLocked(); 103 104 mState = RUNNING; 105} 106 107status_t ZslProcessor::updateStream(const Parameters ¶ms) { 108 ATRACE_CALL(); 109 ALOGV("%s: Configuring ZSL streams", __FUNCTION__); 110 status_t res; 111 112 Mutex::Autolock l(mInputMutex); 113 114 sp<Camera2Client> client = mClient.promote(); 115 if (client == 0) return OK; 116 sp<Camera2Device> device = client->getCameraDevice(); 117 118 if (mZslConsumer == 0) { 119 // Create CPU buffer queue endpoint 120 mZslConsumer = new BufferItemConsumer( 121 GRALLOC_USAGE_HW_CAMERA_ZSL, 122 kZslBufferDepth, 123 true); 124 mZslConsumer->setFrameAvailableListener(this); 125 mZslConsumer->setName(String8("Camera2Client::ZslConsumer")); 126 mZslWindow = new SurfaceTextureClient( 127 mZslConsumer->getProducerInterface()); 128 } 129 130 if (mZslStreamId != NO_STREAM) { 131 // Check if stream parameters have to change 132 uint32_t currentWidth, currentHeight; 133 res = device->getStreamInfo(mZslStreamId, 134 ¤tWidth, ¤tHeight, 0); 135 if (res != OK) { 136 ALOGE("%s: Camera %d: Error querying capture output stream info: " 137 "%s (%d)", __FUNCTION__, 138 client->getCameraId(), strerror(-res), res); 139 return res; 140 } 141 if (currentWidth != (uint32_t)params.fastInfo.arrayWidth || 142 currentHeight != (uint32_t)params.fastInfo.arrayHeight) { 143 res = device->deleteReprocessStream(mZslReprocessStreamId); 144 if (res != OK) { 145 ALOGE("%s: Camera %d: Unable to delete old reprocess stream " 146 "for ZSL: %s (%d)", __FUNCTION__, 147 client->getCameraId(), strerror(-res), res); 148 return res; 149 } 150 res = device->deleteStream(mZslStreamId); 151 if (res != OK) { 152 ALOGE("%s: Camera %d: Unable to delete old output stream " 153 "for ZSL: %s (%d)", __FUNCTION__, 154 client->getCameraId(), strerror(-res), res); 155 return res; 156 } 157 mZslStreamId = NO_STREAM; 158 } 159 } 160 161 if (mZslStreamId == NO_STREAM) { 162 // Create stream for HAL production 163 // TODO: Sort out better way to select resolution for ZSL 164 int streamType = params.quirks.useZslFormat ? 165 (int)CAMERA2_HAL_PIXEL_FORMAT_ZSL : 166 (int)HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 167 res = device->createStream(mZslWindow, 168 params.fastInfo.arrayWidth, params.fastInfo.arrayHeight, 169 streamType, 0, 170 &mZslStreamId); 171 if (res != OK) { 172 ALOGE("%s: Camera %d: Can't create output stream for ZSL: " 173 "%s (%d)", __FUNCTION__, client->getCameraId(), 174 strerror(-res), res); 175 return res; 176 } 177 res = device->createReprocessStreamFromStream(mZslStreamId, 178 &mZslReprocessStreamId); 179 if (res != OK) { 180 ALOGE("%s: Camera %d: Can't create reprocess stream for ZSL: " 181 "%s (%d)", __FUNCTION__, client->getCameraId(), 182 strerror(-res), res); 183 return res; 184 } 185 } 186 client->registerFrameListener(Camera2Client::kPreviewRequestId, this); 187 188 return OK; 189} 190 191status_t ZslProcessor::deleteStream() { 192 ATRACE_CALL(); 193 status_t res; 194 195 Mutex::Autolock l(mInputMutex); 196 197 if (mZslStreamId != NO_STREAM) { 198 sp<Camera2Client> client = mClient.promote(); 199 if (client == 0) return OK; 200 sp<Camera2Device> device = client->getCameraDevice(); 201 202 res = device->deleteReprocessStream(mZslReprocessStreamId); 203 if (res != OK) { 204 ALOGE("%s: Camera %d: Cannot delete ZSL reprocessing stream %d: " 205 "%s (%d)", __FUNCTION__, client->getCameraId(), 206 mZslReprocessStreamId, strerror(-res), res); 207 return res; 208 } 209 210 mZslReprocessStreamId = NO_STREAM; 211 res = device->deleteStream(mZslStreamId); 212 if (res != OK) { 213 ALOGE("%s: Camera %d: Cannot delete ZSL output stream %d: " 214 "%s (%d)", __FUNCTION__, client->getCameraId(), 215 mZslStreamId, strerror(-res), res); 216 return res; 217 } 218 219 mZslWindow.clear(); 220 mZslConsumer.clear(); 221 222 mZslStreamId = NO_STREAM; 223 } 224 return OK; 225} 226 227int ZslProcessor::getStreamId() const { 228 Mutex::Autolock l(mInputMutex); 229 return mZslStreamId; 230} 231 232int ZslProcessor::getReprocessStreamId() const { 233 Mutex::Autolock l(mInputMutex); 234 return mZslReprocessStreamId; 235} 236 237status_t ZslProcessor::pushToReprocess(int32_t requestId) { 238 ALOGV("%s: Send in reprocess request with id %d", 239 __FUNCTION__, requestId); 240 Mutex::Autolock l(mInputMutex); 241 status_t res; 242 sp<Camera2Client> client = mClient.promote(); 243 244 if (client == 0) return INVALID_OPERATION; 245 246 IF_ALOGV() { 247 dumpZslQueue(-1); 248 } 249 250 if (mZslQueueTail != mZslQueueHead) { 251 CameraMetadata request; 252 size_t index = mZslQueueTail; 253 while (request.isEmpty() && index != mZslQueueHead) { 254 request = mZslQueue[index].frame; 255 index = (index + 1) % kZslBufferDepth; 256 } 257 if (request.isEmpty()) { 258 ALOGV("%s: ZSL queue has no valid frames to send yet.", 259 __FUNCTION__); 260 return NOT_ENOUGH_DATA; 261 } 262 // Verify that the frame is reasonable for reprocessing 263 264 camera_metadata_entry_t entry; 265 entry = request.find(ANDROID_CONTROL_AE_STATE); 266 if (entry.count == 0) { 267 ALOGE("%s: ZSL queue frame has no AE state field!", 268 __FUNCTION__); 269 return BAD_VALUE; 270 } 271 if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED && 272 entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) { 273 ALOGV("%s: ZSL queue frame AE state is %d, need full capture", 274 __FUNCTION__, entry.data.u8[0]); 275 return NOT_ENOUGH_DATA; 276 } 277 278 buffer_handle_t *handle = 279 &(mZslQueue[index].buffer.mGraphicBuffer->handle); 280 281 uint8_t requestType = ANDROID_REQUEST_TYPE_REPROCESS; 282 res = request.update(ANDROID_REQUEST_TYPE, 283 &requestType, 1); 284 uint8_t inputStreams[1] = { mZslReprocessStreamId }; 285 if (res == OK) request.update(ANDROID_REQUEST_INPUT_STREAMS, 286 inputStreams, 1); 287 uint8_t outputStreams[1] = { client->getCaptureStreamId() }; 288 if (res == OK) request.update(ANDROID_REQUEST_OUTPUT_STREAMS, 289 outputStreams, 1); 290 res = request.update(ANDROID_REQUEST_ID, 291 &requestId, 1); 292 293 if (res != OK ) { 294 ALOGE("%s: Unable to update frame to a reprocess request", __FUNCTION__); 295 return INVALID_OPERATION; 296 } 297 298 res = client->getCameraDevice()->pushReprocessBuffer(mZslReprocessStreamId, 299 handle, this); 300 if (res != OK) { 301 ALOGE("%s: Unable to push buffer for reprocessing: %s (%d)", 302 __FUNCTION__, strerror(-res), res); 303 return res; 304 } 305 306 res = client->getCameraDevice()->capture(request); 307 if (res != OK ) { 308 ALOGE("%s: Unable to send ZSL reprocess request to capture: %s (%d)", 309 __FUNCTION__, strerror(-res), res); 310 return res; 311 } 312 313 mState = LOCKED; 314 } else { 315 ALOGV("%s: No ZSL buffers yet", __FUNCTION__); 316 return NOT_ENOUGH_DATA; 317 } 318 return OK; 319} 320 321status_t ZslProcessor::clearZslQueue() { 322 Mutex::Autolock l(mInputMutex); 323 // If in middle of capture, can't clear out queue 324 if (mState == LOCKED) return OK; 325 326 return clearZslQueueLocked(); 327} 328 329status_t ZslProcessor::clearZslQueueLocked() { 330 for (size_t i = 0; i < mZslQueue.size(); i++) { 331 if (mZslQueue[i].buffer.mTimestamp != 0) { 332 mZslConsumer->releaseBuffer(mZslQueue[i].buffer); 333 } 334 mZslQueue.replaceAt(i); 335 } 336 mZslQueueHead = 0; 337 mZslQueueTail = 0; 338 return OK; 339} 340 341void ZslProcessor::dump(int fd, const Vector<String16>& args) const { 342 Mutex::Autolock l(mInputMutex); 343 dumpZslQueue(fd); 344} 345 346bool ZslProcessor::threadLoop() { 347 status_t res; 348 349 { 350 Mutex::Autolock l(mInputMutex); 351 while (!mZslBufferAvailable) { 352 res = mZslBufferAvailableSignal.waitRelative(mInputMutex, 353 kWaitDuration); 354 if (res == TIMED_OUT) return true; 355 } 356 mZslBufferAvailable = false; 357 } 358 359 do { 360 sp<Camera2Client> client = mClient.promote(); 361 if (client == 0) return false; 362 res = processNewZslBuffer(client); 363 } while (res == OK); 364 365 return true; 366} 367 368status_t ZslProcessor::processNewZslBuffer(sp<Camera2Client> &client) { 369 ATRACE_CALL(); 370 status_t res; 371 372 ALOGVV("Trying to get next buffer"); 373 BufferItemConsumer::BufferItem item; 374 res = mZslConsumer->acquireBuffer(&item); 375 if (res != OK) { 376 if (res != BufferItemConsumer::NO_BUFFER_AVAILABLE) { 377 ALOGE("%s: Camera %d: Error receiving ZSL image buffer: " 378 "%s (%d)", __FUNCTION__, 379 client->getCameraId(), strerror(-res), res); 380 } else { 381 ALOGVV(" No buffer"); 382 } 383 return res; 384 } 385 386 Mutex::Autolock l(mInputMutex); 387 388 if (mState == LOCKED) { 389 ALOGVV("In capture, discarding new ZSL buffers"); 390 mZslConsumer->releaseBuffer(item); 391 return OK; 392 } 393 394 ALOGVV("Got ZSL buffer: head: %d, tail: %d", mZslQueueHead, mZslQueueTail); 395 396 if ( (mZslQueueHead + 1) % kZslBufferDepth == mZslQueueTail) { 397 ALOGVV("Releasing oldest buffer"); 398 mZslConsumer->releaseBuffer(mZslQueue[mZslQueueTail].buffer); 399 mZslQueue.replaceAt(mZslQueueTail); 400 mZslQueueTail = (mZslQueueTail + 1) % kZslBufferDepth; 401 } 402 403 ZslPair &queueHead = mZslQueue.editItemAt(mZslQueueHead); 404 405 queueHead.buffer = item; 406 queueHead.frame.release(); 407 408 mZslQueueHead = (mZslQueueHead + 1) % kZslBufferDepth; 409 410 ALOGVV(" Acquired buffer, timestamp %lld", queueHead.buffer.mTimestamp); 411 412 findMatchesLocked(); 413 414 return OK; 415} 416 417void ZslProcessor::findMatchesLocked() { 418 ALOGVV("Scanning"); 419 for (size_t i = 0; i < mZslQueue.size(); i++) { 420 ZslPair &queueEntry = mZslQueue.editItemAt(i); 421 nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp; 422 IF_ALOGV() { 423 camera_metadata_entry_t entry; 424 nsecs_t frameTimestamp = 0; 425 if (!queueEntry.frame.isEmpty()) { 426 entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP); 427 frameTimestamp = entry.data.i64[0]; 428 } 429 ALOGVV(" %d: b: %lld\tf: %lld", i, 430 bufferTimestamp, frameTimestamp ); 431 } 432 if (queueEntry.frame.isEmpty() && bufferTimestamp != 0) { 433 // Have buffer, no matching frame. Look for one 434 for (size_t j = 0; j < mFrameList.size(); j++) { 435 bool match = false; 436 CameraMetadata &frame = mFrameList.editItemAt(j); 437 if (!frame.isEmpty()) { 438 camera_metadata_entry_t entry; 439 entry = frame.find(ANDROID_SENSOR_TIMESTAMP); 440 if (entry.count == 0) { 441 ALOGE("%s: Can't find timestamp in frame!", 442 __FUNCTION__); 443 continue; 444 } 445 nsecs_t frameTimestamp = entry.data.i64[0]; 446 if (bufferTimestamp == frameTimestamp) { 447 ALOGVV("%s: Found match %lld", __FUNCTION__, 448 frameTimestamp); 449 match = true; 450 } else { 451 int64_t delta = abs(bufferTimestamp - frameTimestamp); 452 if ( delta < 1000000) { 453 ALOGVV("%s: Found close match %lld (delta %lld)", 454 __FUNCTION__, bufferTimestamp, delta); 455 match = true; 456 } 457 } 458 } 459 if (match) { 460 queueEntry.frame.acquire(frame); 461 break; 462 } 463 } 464 } 465 } 466} 467 468void ZslProcessor::dumpZslQueue(int fd) const { 469 String8 header("ZSL queue contents:"); 470 String8 indent(" "); 471 ALOGV("%s", header.string()); 472 if (fd != -1) { 473 header = indent + header + "\n"; 474 write(fd, header.string(), header.size()); 475 } 476 for (size_t i = 0; i < mZslQueue.size(); i++) { 477 const ZslPair &queueEntry = mZslQueue[i]; 478 nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp; 479 camera_metadata_ro_entry_t entry; 480 nsecs_t frameTimestamp = 0; 481 int frameAeState = -1; 482 if (!queueEntry.frame.isEmpty()) { 483 entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP); 484 if (entry.count > 0) frameTimestamp = entry.data.i64[0]; 485 entry = queueEntry.frame.find(ANDROID_CONTROL_AE_STATE); 486 if (entry.count > 0) frameAeState = entry.data.u8[0]; 487 } 488 String8 result = 489 String8::format(" %d: b: %lld\tf: %lld, AE state: %d", i, 490 bufferTimestamp, frameTimestamp, frameAeState); 491 ALOGV("%s", result.string()); 492 if (fd != -1) { 493 result = indent + result + "\n"; 494 write(fd, result.string(), result.size()); 495 } 496 497 } 498} 499 500}; // namespace camera2 501}; // namespace android 502