最近调查一个Camera的问题,发现Camera的流程越来越庞大,所以把调用关系和流程整理到流程图中。方便以后查阅,整理的时间比较短有很多细节被忽略,主要看大的流程。
一图在手,回家不愁,一张图搞定!
1.CameraManagerGlobal中connectCameraServiceLocked通过binder连接到CameraService
/**
* Connect to the camera service if it's available, and set up listeners.
* If the service is already connected, do nothing.
*
* <p>Sets mCameraService to a valid pointer or null if the connection does not succeed.</p>
*/
private void connectCameraServiceLocked() {
// Only reconnect if necessary
if (mCameraService != null || sCameraServiceDisabled) return;
Log.i(TAG, "Connecting to camera service");
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
if (cameraServiceBinder == null) {
// Camera service is now down, leave mCameraService as null
return;
}
try {
cameraServiceBinder.linkToDeath(this, /*flags*/ 0);
} catch (RemoteException e) {
// Camera service is now down, leave mCameraService as null
return;
}
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
try {
CameraMetadataNative.setupGlobalVendorTagDescriptor();
} catch (ServiceSpecificException e) {
handleRecoverableSetupErrors(e);
}
try {
CameraStatus[] cameraStatuses = cameraService.addListener(this);
for (CameraStatus c : cameraStatuses) {
onStatusChangedLocked(c.status, c.cameraId);
}
mCameraService = cameraService;
} catch(ServiceSpecificException e) {
// Unexpected failure
throw new IllegalStateException("Failed to register a camera service listener", e);
} catch (RemoteException e) {
// Camera service is now down, leave mCameraService as null
}
}
2.CameraDevice3构造函数中的如下代码:
camera3_callback_ops::notify = &sNotify;
camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
注册回调到process_capture_result函数中,在整个Camera框架中CameraDevice3起到了承上启下的作用,也是整体的核心部分。
process_capture_result的定义在/hardware/libhardware/include/hardware/camera3.h文件中:
1 typedef struct camera3_callback_ops {
2542
2543 /**
2544 * process_capture_result:
2545 *
2546 * Send results from a completed capture to the framework.
2547 * process_capture_result() may be invoked multiple times by the HAL in
2548 * response to a single capture request. This allows, for example, the
2549 * metadata and low-resolution buffers to be returned in one call, and
2550 * post-processed JPEG buffers in a later call, once it is available. Each
2551 * call must include the frame number of the request it is returning
2552 * metadata or buffers for.
2553 *
2554 * A component (buffer or metadata) of the complete result may only be
2555 * included in one process_capture_result call. A buffer for each stream,
2556 * and the result metadata, must be returned by the HAL for each request in
2557 * one of the process_capture_result calls, even in case of errors producing
2558 * some of the output. A call to process_capture_result() with neither
2559 * output buffers or result metadata is not allowed.
2560 *
2561 * The order of returning metadata and buffers for a single result does not
2562 * matter, but buffers for a given stream must be returned in FIFO order. So
2563 * the buffer for request 5 for stream A must always be returned before the
2564 * buffer for request 6 for stream A. This also applies to the result
2565 * metadata; the metadata for request 5 must be returned before the metadata
2566 * for request 6.
2567 *
2568 * However, different streams are independent of each other, so it is
2569 * acceptable and expected that the buffer for request 5 for stream A may be
2570 * returned after the buffer for request 6 for stream B is. And it is
2571 * acceptable that the result metadata for request 6 for stream B is
2572 * returned before the buffer for request 5 for stream A is.
2573 *
2574 * The HAL retains ownership of result structure, which only needs to be
2575 * valid to access during this call. The framework will copy whatever it
2576 * needs before this call returns.
2577 *
2578 * The output buffers do not need to be filled yet; the framework will wait
2579 * on the stream buffer release sync fence before reading the buffer
2580 * data. Therefore, this method should be called by the HAL as soon as
2581 * possible, even if some or all of the output buffers are still in
2582 * being filled. The HAL must include valid release sync fences into each
2583 * output_buffers stream buffer entry, or -1 if that stream buffer is
2584 * already filled.
2585 *
2586 * If the result buffer cannot be constructed for a request, the HAL should
2587 * return an empty metadata buffer, but still provide the output buffers and
2588 * their sync fences. In addition, notify() must be called with an
2589 * ERROR_RESULT message.
2590 *
2591 * If an output buffer cannot be filled, its status field must be set to
2592 * STATUS_ERROR. In addition, notify() must be called with a ERROR_BUFFER
2593 * message.
2594 *
2595 * If the entire capture has failed, then this method still needs to be
2596 * called to return the output buffers to the framework. All the buffer
2597 * statuses should be STATUS_ERROR, and the result metadata should be an
2598 * empty buffer. In addition, notify() must be called with a ERROR_REQUEST
2599 * message. In this case, individual ERROR_RESULT/ERROR_BUFFER messages
2600 * should not be sent.
2601 *
2602 * Performance requirements:
2603 *
2604 * This is a non-blocking call. The framework will return this call in 5ms.
2605 *
2606 * The pipeline latency (see S7 for definition) should be less than or equal to
2607 * 4 frame intervals, and must be less than or equal to 8 frame intervals.
2608 *
2609 */
2610 void (*process_capture_result)(const struct camera3_callback_ops *,
2611 const camera3_capture_result_t *result);
2612
2613 /**
2614 * notify:
2615 *
2616 * Asynchronous notification callback from the HAL, fired for various
2617 * reasons. Only for information independent of frame capture, or that
2618 * require specific timing. The ownership of the message structure remains
2619 * with the HAL, and the msg only needs to be valid for the duration of this
2620 * call.
2621 *
2622 * Multiple threads may call notify() simultaneously.
2623 *
2624 * <= CAMERA_DEVICE_API_VERSION_3_1:
2625 *
2626 * The notification for the start of exposure for a given request must be
2627 * sent by the HAL before the first call to process_capture_result() for
2628 * that request is made.
2629 *
2630 * >= CAMERA_DEVICE_API_VERSION_3_2:
2631 *
2632 * Buffers delivered to the framework will not be dispatched to the
2633 * application layer until a start of exposure timestamp (or input image's
2634 * start of exposure timestamp for a reprocess request) has been received
2635 * via a SHUTTER notify() call. It is highly recommended to dispatch this
2636 * call as early as possible.
2637 *
2638 * ------------------------------------------------------------------------
2639 * Performance requirements:
2640 *
2641 * This is a non-blocking call. The framework will return this call in 5ms.
2642 */
2643 void (*notify)(const struct camera3_callback_ops *,
2644 const camera3_notify_msg_t *msg);
2645
2646 } camera3_callback_ops_t;