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 #include <cstdlib> 18 #include <stdio.h> 19 #include <hardware/camera3.h> 20 #include <sync/sync.h> 21 #include <system/camera_metadata.h> 22 #include <system/graphics.h> 23 #include <utils/Mutex.h> 24 #include "CameraHAL.h" 25 #include "Metadata.h" 26 #include "Stream.h" 27 28 //#define LOG_NDEBUG 0 29 #define LOG_TAG "Camera" 30 #include <cutils/log.h> 31 32 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) 33 #include <utils/Trace.h> 34 35 #include "Camera.h" 36 37 #define CAMERA_SYNC_TIMEOUT 5000 // in msecs 38 39 namespace default_camera_hal { 40 41 extern "C" { 42 // Shim passed to the framework to close an opened device. 43 static int close_device(hw_device_t* dev) 44 { 45 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev); 46 Camera* cam = static_cast<Camera*>(cam_dev->priv); 47 return cam->close(); 48 } 49 } // extern "C" 50 51 Camera::Camera(int id) 52 : mId(id), 53 mStaticInfo(NULL), 54 mBusy(false), 55 mCallbackOps(NULL), 56 mStreams(NULL), 57 mNumStreams(0), 58 mSettings(NULL) 59 { 60 memset(&mTemplates, 0, sizeof(mTemplates)); 61 memset(&mDevice, 0, sizeof(mDevice)); 62 mDevice.common.tag = HARDWARE_DEVICE_TAG; 63 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0; 64 mDevice.common.close = close_device; 65 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); 66 mDevice.priv = this; 67 } 68 69 Camera::~Camera() 70 { 71 if (mStaticInfo != NULL) { 72 free_camera_metadata(mStaticInfo); 73 } 74 } 75 76 int Camera::open(const hw_module_t *module, hw_device_t **device) 77 { 78 ALOGI("%s:%d: Opening camera device", __func__, mId); 79 ATRACE_CALL(); 80 android::Mutex::Autolock al(mDeviceLock); 81 82 if (mBusy) { 83 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); 84 return -EBUSY; 85 } 86 87 // TODO: open camera dev nodes, etc 88 mBusy = true; 89 mDevice.common.module = const_cast<hw_module_t*>(module); 90 *device = &mDevice.common; 91 return 0; 92 } 93 94 int Camera::getInfo(struct camera_info *info) 95 { 96 android::Mutex::Autolock al(mStaticInfoLock); 97 98 info->facing = CAMERA_FACING_FRONT; 99 info->orientation = 0; 100 info->device_version = mDevice.common.version; 101 if (mStaticInfo == NULL) { 102 mStaticInfo = initStaticInfo(); 103 } 104 info->static_camera_characteristics = mStaticInfo; 105 return 0; 106 } 107 108 int Camera::close() 109 { 110 ALOGI("%s:%d: Closing camera device", __func__, mId); 111 ATRACE_CALL(); 112 android::Mutex::Autolock al(mDeviceLock); 113 114 if (!mBusy) { 115 ALOGE("%s:%d: Error! Camera device not open", __func__, mId); 116 return -EINVAL; 117 } 118 119 // TODO: close camera dev nodes, etc 120 mBusy = false; 121 return 0; 122 } 123 124 int Camera::initialize(const camera3_callback_ops_t *callback_ops) 125 { 126 int res; 127 128 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); 129 mCallbackOps = callback_ops; 130 // per-device specific initialization 131 res = initDevice(); 132 if (res != 0) { 133 ALOGE("%s:%d: Failed to initialize device!", __func__, mId); 134 return res; 135 } 136 return 0; 137 } 138 139 int Camera::configureStreams(camera3_stream_configuration_t *stream_config) 140 { 141 camera3_stream_t *astream; 142 Stream **newStreams = NULL; 143 144 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config); 145 ATRACE_CALL(); 146 android::Mutex::Autolock al(mDeviceLock); 147 148 if (stream_config == NULL) { 149 ALOGE("%s:%d: NULL stream configuration array", __func__, mId); 150 return -EINVAL; 151 } 152 if (stream_config->num_streams == 0) { 153 ALOGE("%s:%d: Empty stream configuration array", __func__, mId); 154 return -EINVAL; 155 } 156 157 // Create new stream array 158 newStreams = new Stream*[stream_config->num_streams]; 159 ALOGV("%s:%d: Number of Streams: %d", __func__, mId, 160 stream_config->num_streams); 161 162 // Mark all current streams unused for now 163 for (int i = 0; i < mNumStreams; i++) 164 mStreams[i]->mReuse = false; 165 // Fill new stream array with reused streams and new streams 166 for (unsigned int i = 0; i < stream_config->num_streams; i++) { 167 astream = stream_config->streams[i]; 168 if (astream->max_buffers > 0) { 169 ALOGV("%s:%d: Reusing stream %d", __func__, mId, i); 170 newStreams[i] = reuseStream(astream); 171 } else { 172 ALOGV("%s:%d: Creating new stream %d", __func__, mId, i); 173 newStreams[i] = new Stream(mId, astream); 174 } 175 176 if (newStreams[i] == NULL) { 177 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i); 178 goto err_out; 179 } 180 astream->priv = newStreams[i]; 181 } 182 183 // Verify the set of streams in aggregate 184 if (!isValidStreamSet(newStreams, stream_config->num_streams)) { 185 ALOGE("%s:%d: Invalid stream set", __func__, mId); 186 goto err_out; 187 } 188 189 // Set up all streams (calculate usage/max_buffers for each) 190 setupStreams(newStreams, stream_config->num_streams); 191 192 // Destroy all old streams and replace stream array with new one 193 destroyStreams(mStreams, mNumStreams); 194 mStreams = newStreams; 195 mNumStreams = stream_config->num_streams; 196 197 // Clear out last seen settings metadata 198 setSettings(NULL); 199 return 0; 200 201 err_out: 202 // Clean up temporary streams, preserve existing mStreams/mNumStreams 203 destroyStreams(newStreams, stream_config->num_streams); 204 return -EINVAL; 205 } 206 207 void Camera::destroyStreams(Stream **streams, int count) 208 { 209 if (streams == NULL) 210 return; 211 for (int i = 0; i < count; i++) { 212 // Only destroy streams that weren't reused 213 if (streams[i] != NULL && !streams[i]->mReuse) 214 delete streams[i]; 215 } 216 delete [] streams; 217 } 218 219 Stream *Camera::reuseStream(camera3_stream_t *astream) 220 { 221 Stream *priv = reinterpret_cast<Stream*>(astream->priv); 222 // Verify the re-used stream's parameters match 223 if (!priv->isValidReuseStream(mId, astream)) { 224 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId); 225 return NULL; 226 } 227 // Mark stream to be reused 228 priv->mReuse = true; 229 return priv; 230 } 231 232 bool Camera::isValidStreamSet(Stream **streams, int count) 233 { 234 int inputs = 0; 235 int outputs = 0; 236 237 if (streams == NULL) { 238 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId); 239 return false; 240 } 241 if (count == 0) { 242 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId); 243 return false; 244 } 245 // Validate there is at most one input stream and at least one output stream 246 for (int i = 0; i < count; i++) { 247 // A stream may be both input and output (bidirectional) 248 if (streams[i]->isInputType()) 249 inputs++; 250 if (streams[i]->isOutputType()) 251 outputs++; 252 } 253 ALOGV("%s:%d: Configuring %d output streams and %d input streams", 254 __func__, mId, outputs, inputs); 255 if (outputs < 1) { 256 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId); 257 return false; 258 } 259 if (inputs > 1) { 260 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId); 261 return false; 262 } 263 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams 264 return true; 265 } 266 267 void Camera::setupStreams(Stream **streams, int count) 268 { 269 /* 270 * This is where the HAL has to decide internally how to handle all of the 271 * streams, and then produce usage and max_buffer values for each stream. 272 * Note, the stream array has been checked before this point for ALL invalid 273 * conditions, so it must find a successful configuration for this stream 274 * array. The HAL may not return an error from this point. 275 * 276 * In this demo HAL, we just set all streams to be the same dummy values; 277 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN. 278 */ 279 for (int i = 0; i < count; i++) { 280 uint32_t usage = 0; 281 282 if (streams[i]->isOutputType()) 283 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN | 284 GRALLOC_USAGE_HW_CAMERA_WRITE; 285 if (streams[i]->isInputType()) 286 usage |= GRALLOC_USAGE_SW_READ_OFTEN | 287 GRALLOC_USAGE_HW_CAMERA_READ; 288 289 streams[i]->setUsage(usage); 290 streams[i]->setMaxBuffers(1); 291 } 292 } 293 294 int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set) 295 { 296 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set); 297 if (buf_set == NULL) { 298 ALOGE("%s:%d: NULL buffer set", __func__, mId); 299 return -EINVAL; 300 } 301 if (buf_set->stream == NULL) { 302 ALOGE("%s:%d: NULL stream handle", __func__, mId); 303 return -EINVAL; 304 } 305 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv); 306 return stream->registerBuffers(buf_set); 307 } 308 309 bool Camera::isValidTemplateType(int type) 310 { 311 return type < 1 || type >= CAMERA3_TEMPLATE_COUNT; 312 } 313 314 const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) 315 { 316 ALOGV("%s:%d: type=%d", __func__, mId, type); 317 318 if (!isValidTemplateType(type)) { 319 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); 320 return NULL; 321 } 322 return mTemplates[type]; 323 } 324 325 int Camera::processCaptureRequest(camera3_capture_request_t *request) 326 { 327 camera3_capture_result result; 328 329 ALOGV("%s:%d: request=%p", __func__, mId, request); 330 ATRACE_CALL(); 331 332 if (request == NULL) { 333 ALOGE("%s:%d: NULL request recieved", __func__, mId); 334 return -EINVAL; 335 } 336 337 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId, 338 request->frame_number, request->settings); 339 340 // NULL indicates use last settings 341 if (request->settings == NULL) { 342 if (mSettings == NULL) { 343 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p", 344 __func__, mId, request->frame_number, request); 345 return -EINVAL; 346 } 347 } else { 348 setSettings(request->settings); 349 } 350 351 if (request->input_buffer != NULL) { 352 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId, 353 request->input_buffer); 354 355 if (!isValidReprocessSettings(request->settings)) { 356 ALOGE("%s:%d: Invalid settings for reprocess request: %p", 357 __func__, mId, request->settings); 358 return -EINVAL; 359 } 360 } else { 361 ALOGV("%s:%d: Capturing new frame.", __func__, mId); 362 363 if (!isValidCaptureSettings(request->settings)) { 364 ALOGE("%s:%d: Invalid settings for capture request: %p", 365 __func__, mId, request->settings); 366 return -EINVAL; 367 } 368 } 369 370 if (request->num_output_buffers <= 0) { 371 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId, 372 request->num_output_buffers); 373 return -EINVAL; 374 } 375 result.num_output_buffers = request->num_output_buffers; 376 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers]; 377 for (unsigned int i = 0; i < request->num_output_buffers; i++) { 378 int res = processCaptureBuffer(&request->output_buffers[i], 379 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i])); 380 if (res) 381 goto err_out; 382 } 383 384 result.frame_number = request->frame_number; 385 // TODO: return actual captured/reprocessed settings 386 result.result = request->settings; 387 // TODO: asynchronously return results 388 notifyShutter(request->frame_number, 0); 389 mCallbackOps->process_capture_result(mCallbackOps, &result); 390 391 return 0; 392 393 err_out: 394 delete [] result.output_buffers; 395 // TODO: this should probably be a total device failure; transient for now 396 return -EINVAL; 397 } 398 399 void Camera::setSettings(const camera_metadata_t *new_settings) 400 { 401 if (mSettings != NULL) { 402 free_camera_metadata(mSettings); 403 mSettings = NULL; 404 } 405 406 if (new_settings != NULL) 407 mSettings = clone_camera_metadata(new_settings); 408 } 409 410 bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/) 411 { 412 // TODO: reject settings that cannot be reprocessed 413 // input buffers unimplemented, use this to reject reprocessing requests 414 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId); 415 return false; 416 } 417 418 int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in, 419 camera3_stream_buffer_t *out) 420 { 421 if (in->acquire_fence != -1) { 422 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT); 423 if (res == -ETIME) { 424 ALOGE("%s:%d: Timeout waiting on buffer acquire fence", 425 __func__, mId); 426 return res; 427 } else if (res) { 428 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)", 429 __func__, mId, strerror(-res), res); 430 return res; 431 } 432 } 433 434 out->stream = in->stream; 435 out->buffer = in->buffer; 436 out->status = CAMERA3_BUFFER_STATUS_OK; 437 // TODO: use driver-backed release fences 438 out->acquire_fence = -1; 439 out->release_fence = -1; 440 441 // TODO: lock and software-paint buffer 442 return 0; 443 } 444 445 void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp) 446 { 447 int res; 448 struct timespec ts; 449 450 // If timestamp is 0, get timestamp from right now instead 451 if (timestamp == 0) { 452 ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME", 453 __func__, mId); 454 res = clock_gettime(CLOCK_BOOTTIME, &ts); 455 if (res == 0) { 456 timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec; 457 } else { 458 ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)", 459 __func__, mId, strerror(errno), errno); 460 } 461 } 462 camera3_notify_msg_t m; 463 memset(&m, 0, sizeof(m)); 464 m.type = CAMERA3_MSG_SHUTTER; 465 m.message.shutter.frame_number = frame_number; 466 m.message.shutter.timestamp = timestamp; 467 mCallbackOps->notify(mCallbackOps, &m); 468 } 469 470 void Camera::dump(int fd) 471 { 472 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); 473 ATRACE_CALL(); 474 android::Mutex::Autolock al(mDeviceLock); 475 476 dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy); 477 478 // TODO: dump all settings 479 dprintf(fd, "Most Recent Settings: (%p)\n", mSettings); 480 481 dprintf(fd, "Number of streams: %d\n", mNumStreams); 482 for (int i = 0; i < mNumStreams; i++) { 483 dprintf(fd, "Stream %d/%d:\n", i, mNumStreams); 484 mStreams[i]->dump(fd); 485 } 486 } 487 488 const char* Camera::templateToString(int type) 489 { 490 switch (type) { 491 case CAMERA3_TEMPLATE_PREVIEW: 492 return "CAMERA3_TEMPLATE_PREVIEW"; 493 case CAMERA3_TEMPLATE_STILL_CAPTURE: 494 return "CAMERA3_TEMPLATE_STILL_CAPTURE"; 495 case CAMERA3_TEMPLATE_VIDEO_RECORD: 496 return "CAMERA3_TEMPLATE_VIDEO_RECORD"; 497 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: 498 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT"; 499 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: 500 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG"; 501 } 502 // TODO: support vendor templates 503 return "Invalid template type!"; 504 } 505 506 int Camera::setTemplate(int type, camera_metadata_t *settings) 507 { 508 android::Mutex::Autolock al(mDeviceLock); 509 510 if (!isValidTemplateType(type)) { 511 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); 512 return -EINVAL; 513 } 514 515 if (mTemplates[type] != NULL) { 516 ALOGE("%s:%d: Setting already constructed template type %s(%d)", 517 __func__, mId, templateToString(type), type); 518 return -EINVAL; 519 } 520 521 // Make a durable copy of the underlying metadata 522 mTemplates[type] = clone_camera_metadata(settings); 523 if (mTemplates[type] == NULL) { 524 ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)", 525 __func__, mId, settings, templateToString(type), type); 526 return -EINVAL; 527 } 528 return 0; 529 } 530 531 extern "C" { 532 // Get handle to camera from device priv data 533 static Camera *camdev_to_camera(const camera3_device_t *dev) 534 { 535 return reinterpret_cast<Camera*>(dev->priv); 536 } 537 538 static int initialize(const camera3_device_t *dev, 539 const camera3_callback_ops_t *callback_ops) 540 { 541 return camdev_to_camera(dev)->initialize(callback_ops); 542 } 543 544 static int configure_streams(const camera3_device_t *dev, 545 camera3_stream_configuration_t *stream_list) 546 { 547 return camdev_to_camera(dev)->configureStreams(stream_list); 548 } 549 550 static int register_stream_buffers(const camera3_device_t *dev, 551 const camera3_stream_buffer_set_t *buffer_set) 552 { 553 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set); 554 } 555 556 static const camera_metadata_t *construct_default_request_settings( 557 const camera3_device_t *dev, int type) 558 { 559 return camdev_to_camera(dev)->constructDefaultRequestSettings(type); 560 } 561 562 static int process_capture_request(const camera3_device_t *dev, 563 camera3_capture_request_t *request) 564 { 565 return camdev_to_camera(dev)->processCaptureRequest(request); 566 } 567 568 static void dump(const camera3_device_t *dev, int fd) 569 { 570 camdev_to_camera(dev)->dump(fd); 571 } 572 573 static int flush(const camera3_device_t*) 574 { 575 ALOGE("%s: unimplemented.", __func__); 576 return -1; 577 } 578 579 } // extern "C" 580 581 const camera3_device_ops_t Camera::sOps = { 582 .initialize = default_camera_hal::initialize, 583 .configure_streams = default_camera_hal::configure_streams, 584 .register_stream_buffers = default_camera_hal::register_stream_buffers, 585 .construct_default_request_settings 586 = default_camera_hal::construct_default_request_settings, 587 .process_capture_request = default_camera_hal::process_capture_request, 588 .get_metadata_vendor_tag_ops = NULL, 589 .dump = default_camera_hal::dump, 590 .flush = default_camera_hal::flush, 591 .reserved = {0}, 592 }; 593 594 } // namespace default_camera_hal 595