框架层调用
camera_module_t->common.open(),之后HAL返回一个hardware_device_t结构。Framework calls camera_module_t->common.open(), which returns a hardware_device_t structure.
框架层检查
hardware_device_t->version,并根据相机硬件设备的版本创建相应的处理程序。如果版本为CAMERA_DEVICE_API_VERSION_3_0,设备实例将会被转化为camera3_device_t。Framework inspects the hardware_device_t->version field, and instantiates the appropriate handler for that version of the camera hardware device. In case the version is CAMERA_DEVICE_API_VERSION_3_0, the device is cast to a camera3_device_t.
框架层调用
camera3_device_t->ops->initialize()并将框架层回调函数指针传入。该函数只会在open()函数被调用后和在ops结构的任何其他函数被调用前的时间间隔中被调用一次。Framework calls camera3_device_t->ops->initialize() with the framework callback function pointers. This will only be called this one time after open(), before any other functions in the ops structure are called.
框架层调用
camera3_device_t->ops->configure_streams(),将一个输入/输出流列表传递到HAL设备中。The framework calls camera3_device_t->ops->configure_streams() with a list of input/output streams to the HAL device.
根据设备版本进行不同的处理
版本低于或等于
CAMERA_DEVICE_API_VERSION_3_1时,框架层分配图形缓冲区并为configure_streams中的至少一个输出流调用camera3_device_t->ops->register_stream_buffers()。相同的流只会被注册一次。<= CAMERA_DEVICE_API_VERSION_3_1: The framework allocates gralloc buffers and calls camera3_device_t->ops->register_stream_buffers() for at least one of the output streams listed in configure_streams. The same stream is registered only once.
版本低于或等于
CAMERA_DEVICE_API_VERSION_3_2时,camera3_device_t->ops->register_stream_buffers()不会被调用,其必须为NULL。CAMERA_DEVICE_API_VERSION_3_2:camera3_device_t->ops->register_stream_buffers() is not called and must be NULL.
框架层调用
camera3_device_t->ops->construct_default_request_settings()请求一些使用用例的默认设置。这可能发生在步骤3之后的任何时间。The framework requests default settings for some number of use cases with calls to camera3_device_t->ops->construct_default_request_settings(). This may occur any time after step 3.
框架层构造并向HAL发送第一个捕获请求。该请求携带着基于默认设置集合中的某一个默认设置的设置项,并且关联着不少于一个之前框架层完成注册的输出流。请求被
camera3_device_t->ops->process_capture_request()方法传送到HAL。HAL将会阻塞该方法的返回,直到下一个请求将被发送。The framework constructs and sends the first capture request to the HAL, with settings based on one of the sets of default settings, and with at least one output stream, which has been registered earlier by the framework. This is sent to the HAL with camera3_device_t->ops->process_capture_request(). The HAL must block the return of this call until it is ready for the next request to be sent.
版本为
CAMERA_DEVICE_API_VERSION_3_2或更高时,在camera3_capture_request_t中的camera3_stream_buffer_t数组中的buffer_handle_t可能是新的,HAL从未在任何新的请求中见过。>= CAMERA_DEVICE_API_VERSION_3_2: The buffer_handle_t provided in the camera3_stream_buffer_t array in the camera3_capture_request_t may be new and never-before-seen by the HAL on any given new request.
框架层不断地提交请求,并调用
construct_default_request_settings以获得其他使用用例的默认设置缓冲区。The framework continues to submit requests, and call construct_default_request_settings to get default settings buffers for other use cases.
当版本为
CAMERA_DEVICE_API_VERSION_3_1或更低时,框架层可能会为那些还未注册的流调用register_stream_buffers()方法。<= CAMERA_DEVICE_API_VERSION_3_1: The framework may call register_stream_buffers() at this time for not-yet-registered streams.
当捕获请求开始处理(传感器开始为捕获图像进行曝光)或当再处理请求开始被处理时,HAL将调用
camera3_callback_ops_t->notify(),该次调用将以SHUTTER事件作为参数,这包括帧序号和曝光开始时间戳。对于再处理请求,时间戳必须是输入图像的曝光开始时间,在process_capture_request()调用时,可以从camera3_capture_request_t.settings的android.sensor.timestamp获得该时间。When the capture of a request begins (sensor starts exposing for thecapture) or processing a reprocess request begins, the HAL calls camera3_callback_ops_t->notify() with the SHUTTER event, including the frame number and the timestamp for start of exposure. For a reprocess request, the timestamp must be the start of exposure of the input image which can be looked up with android.sensor.timestamp from camera3_capture_request_t.settings when process_capture_request() is called.
当版本低于或等于
CAMERA_DEVICE_API_VERSION_3_1时,这个通知必须在process_capture_result()第一次为处理该帧的调用前完成。<= CAMERA_DEVICE_API_VERSION_3_1: This notify call must be made before the first call to process_capture_result() for that frame number.
当版本高于或等于
CAMERA_DEVICE_API_VERSION_3_2时,携带SHUTTER事件的camera3_callback_ops_t->notify()应该被尽可能早的完成,因为在接收到一个有效的曝光开始时间戳(对于再处理请求则是输入图像的开始曝光时间戳)前框架层不能将(该帧的)图形缓冲区传递到应用层。>= CAMERA_DEVICE_API_VERSION_3_2: The camera3_callback_ops_t->notify() call with the SHUTTER event should be made as early as possible since the framework will be unable to deliver gralloc buffers to the application layer (for that frame) until it has a valid timestamp for the start of exposure (or the input image’s start of exposure for a reprocess request).
部分元数据和图形缓冲区可能在
SHUTTER时间发生前或后的任何时间返回。Both partial metadata results and the gralloc buffers may be sent to the framework at any time before or after the SHUTTER event.
部分流水线阻塞等待一段时间后,HAL将调用
camera3_callback_ops_t->process_capture_result()将完成的捕获结果返回框架层。结果的返回顺序将和请求提交的顺序一致。多个请求可以被同时处理,这取决于相机HAL设备的流水线深度。After some pipeline delay, the HAL begins to return completed captures to the framework with camera3_callback_ops_t->process_capture_result(). These are returned in the same order as the requests were submitted. Multiple requests can be in flight at once, depending on the pipeline depth of the camera HAL device.
当版本高于或等于
CAMERA_DEVICE_API_VERSION_3_2时,一旦process_capture_result将缓冲区作为camera3_stream_buffer_t数组的一部分返回且release_fence指定的栅栏发出信号(对于-1栅栏而言是无操作),该缓冲区的所有权将被视为转移给框架层。之后,HAL将不会保留该缓冲区,框架层能够立即清理其占用的内存。>= CAMERA_DEVICE_API_VERSION_3_2: Once a buffer is returned by process_capture_result as part of the camera3_stream_buffer_t array, and the fence specified by release_fence has been signaled (this is a no-op for -1 fences), the ownership of that buffer is considered to be transferred back to the framework. After that, the HAL must no longer retain that particular buffer, and the framework may clean up the memory for it immediately.
对于同一帧,
process_capture_result可能会被多次调用,每一次都携带一个新的不相交的元数据碎片和/或设置图形缓冲区。框架层将合并这些分块的元数据到一个结构中。process_capture_result may be called multiple times for a single frame, each time with a new disjoint piece of metadata and/or set of gralloc buffers. The framework will accumulate these partial metadata results into one result.
特别的,只要上述规则适用于图形缓冲区(不论输入或输出),同时为第N帧和第N+1帧调用
process_capture_result是合法的。In particular, it is legal for a process_capture_result to be called simultaneously for both a frame N and a frame N+1 as long as the above rule holds for gralloc buffers (both input and output).
一段时间后,框架层可能停止提交新的请求,并等待现有的捕获完成(所有的缓冲区完成填充且所有的请求返回),然后再次调用
configure_streams()。这将为一组新的输入/输出流重置相机硬件和流水线。某些之前的流可以被复用,如果这些流的缓冲区已经被注册到HAL中了则不需要再次注册。如果至少有一个完成注册的输出流被保留,则框架层从步骤7开始(否则从步骤5开始)。After some time, the framework may stop submitting new requests, wait for the existing captures to complete (all buffers filled, all results returned), and then call configure_streams() again. This resets the camera hardware and pipeline for a new set of input/output streams. Some streams may be reused from the previous configuration; if these streams’ buffers had already been registered with the HAL, they will not be registered again. The framework then continues from step 7, if at least one registered output stream remains (otherwise, step 5 is required first).
同时,框架层可能调用
camera3_device_t->common->close()终止相机会话。这可能在没有其他任何来自框架层的请求活动的任何时候被调用,但该调用将会被阻塞直到所有正在处理的捕获完成(所有结果返回且所有缓冲区被填充)。在close调用返回后,不允许从HAL调用camera3_callback_ops_t函数。一旦close()的调用开始运行,框架层不能调用任何其他HAL设备函数。Alternatively, the framework may call camera3_device_t->common->close() to end the camera session. This may be called at any time when no other calls from the framework are active, although the call may block until all in-flight captures have completed (all results returned, all buffers filled). After the close call returns, no more calls to the camera3_callback_ops_t functions are allowed from the HAL. Once the close() call is underway, the framework may not call any other HAL device functions.
当有错误或其他异步事件发生时,HAL必须调用
camera3_callback_ops_t->notify()传递恰当的错误/事件信息。在从一个设备范围的致命的错误通知返回后,HAL应该表现的像对其完成close()调用一样。同时,在调用notify()前HAL必须放弃或完成所有未解决的捕获,因此一旦调用notify()发送致命错误信息,框架层将不会从设备中接收到进一步的回调。此外,在发送错误信息的notify()方法返回后,close()应该返回-ENODEV或NULL。In case of an error or other asynchronous event, the HAL must call camera3_callback_ops_t->notify() with the appropriate error/event message. After returning from a fatal device-wide error notification, the HAL should act as if close() had been called on it. However, the HAL must either cancel or complete all outstanding captures before calling notify(), so that once notify() is called with a fatal error, the framework will not receive further callbacks from the device. Methods besides close() should return -ENODEV or NULL after the notify() method returns from a fatal error message.