Android Camera原理之camera HAL底层数据结构与类总结

《Android Camera架构》
《Android Camera进程间通信类总结》
《Android Camera模块解析之拍照》
《Android Camera模块解析之视频录制》
《Android Camera原理之CameraDeviceCallbacks回调模块》
《Android Camera原理之openCamera模块(一)》
《Android Camera原理之openCamera模块(二)》
《Android Camera原理之createCaptureSession模块》
《Android Camera原理之setRepeatingRequest与capture模块》
《Android Camera原理之编译》
《Android Camera原理之camera provider启动》
《Android Camera原理之cameraserver与cameraprovider是怎样联系的》
《Android Camera原理之camera service与camera provider session会话与capture request轮转》
《Android Camera原理之camera HAL底层数据结构与类总结》
《Android Camera原理之camera service类与接口关系》

camera HAL层数据结构非常多,看代码的时候常常为了了解这些数据结构找半天,为了方便大家学习,特地总结了一些数据结构以及这些数据结构的位置:

1.hardware/libhardware/include/hardware/camera_common.h:

1.1 camera_info_t : camera_info

typedef struct camera_info {
    int facing;
    int orientation;
    uint32_t device_version;
    const camera_metadata_t *static_camera_characteristics;
    int resource_cost;
    char** conflicting_devices;
    size_t conflicting_devices_length;
} camera_info_t;

1.2 camera_device_status_t : camera_device_status

typedef enum camera_device_status {
    CAMERA_DEVICE_STATUS_NOT_PRESENT = 0,
    CAMERA_DEVICE_STATUS_PRESENT = 1,
    CAMERA_DEVICE_STATUS_ENUMERATING = 2,
} camera_device_status_t;

1.3 torch_mode_status_t : torch_mode_status

typedef enum torch_mode_status {
    TORCH_MODE_STATUS_NOT_AVAILABLE = 0,
    TORCH_MODE_STATUS_AVAILABLE_OFF = 1,
    TORCH_MODE_STATUS_AVAILABLE_ON = 2,

} torch_mode_status_t;

1.4 camera_module_callbacks_t : camera_module_callbacks

typedef struct camera_module_callbacks {
    void (*camera_device_status_change)(const struct camera_module_callbacks*,
            int camera_id,
            int new_status);

    void (*torch_mode_status_change)(const struct camera_module_callbacks*,
            const char* camera_id,
            int new_status);
} camera_module_callbacks_t;

1.5 camera_module_t : camera_module

typedef struct camera_module {
    hw_module_t common;
    int (*get_number_of_cameras)(void);
    int (*get_camera_info)(int camera_id, struct camera_info *info);
    int (*set_callbacks)(const camera_module_callbacks_t *callbacks);
    void (*get_vendor_tag_ops)(vendor_tag_ops_t* ops);
    int (*open_legacy)(const struct hw_module_t* module, const char* id,
            uint32_t halVersion, struct hw_device_t** device);
    int (*set_torch_mode)(const char* camera_id, bool enabled);
    int (*init)();
    void* reserved[5];
} camera_module_t;

2.hardware/libhardware/include/hardware/hardware.h:

2.1 hw_module_t : hw_module_t

typedef struct hw_module_t {
    /** tag must be initialized to HARDWARE_MODULE_TAG */
    uint32_t tag;

    uint16_t module_api_version;
#define version_major module_api_version

    uint16_t hal_api_version;
#define version_minor hal_api_version

    /** Identifier of module */
    const char *id;

    /** Name of this module */
    const char *name;

    /** Author/owner/implementor of the module */
    const char *author;

    /** Modules methods */
    struct hw_module_methods_t* methods;

    /** module's dso */
    void* dso;

#ifdef __LP64__
    uint64_t reserved[32-7];
#else
    /** padding to 128 bytes, reserved for future use */
    uint32_t reserved[32-7];
#endif

} hw_module_t;

2.2 hw_module_methods_t : hw_module_methods_t

其中 hw_module_methods_t 结构如下:

typedef struct hw_module_methods_t {
    /** Open a specific device */
    int (*open)(const struct hw_module_t* module, const char* id,
            struct hw_device_t** device);
 
} hw_module_methods_t;

这个结构体里面有一个函数指针,什么地方明确了函数指针的指向了?

在 hardware/libhardware/modules/camera/3_0/CameraHAL.cpp中的167行明确了函数指针指向。指向其中的open_dev函数。

162static int open_dev(const hw_module_t* mod, const char* name, hw_device_t** dev)
163{
164    return gCameraHAL.open(mod, name, dev);
165}
166
167static hw_module_methods_t gCameraModuleMethods = {
168    .open = open_dev
169};

2.3 hw_device_t : hw_device_t

typedef struct hw_device_t {
    /** tag must be initialized to HARDWARE_DEVICE_TAG */
    uint32_t tag;
    uint32_t version;

    /** reference to the module this device belongs to */
    struct hw_module_t* module;

    /** padding reserved for future use */
#ifdef __LP64__
    uint64_t reserved[12];
#else
    uint32_t reserved[12];
#endif

    /** Close this device */
    int (*close)(struct hw_device_t* device);

} hw_device_t;

3.hardware/libhardware/include/hardware/camera3.h

3.1 camera3_device_t : camera3_device

typedef struct camera3_device {
    hw_device_t common;
    camera3_device_ops_t *ops;
    void *priv;
} camera3_device_t;

3.2 camera3_device_ops_t : camera3_device_ops

typedef struct camera3_device_ops {
    int (*initialize)(const struct camera3_device *,
            const camera3_callback_ops_t *callback_ops);
    int (*configure_streams)(const struct camera3_device *,
            camera3_stream_configuration_t *stream_list);
    int (*register_stream_buffers)(const struct camera3_device *,
            const camera3_stream_buffer_set_t *buffer_set);
    const camera_metadata_t* (*construct_default_request_settings)(
            const struct camera3_device *,
            int type);
    int (*process_capture_request)(const struct camera3_device *,
            camera3_capture_request_t *request);
    void (*get_metadata_vendor_tag_ops)(const struct camera3_device*,
            vendor_tag_query_ops_t* ops);
    void (*dump)(const struct camera3_device *, int fd);
    int (*flush)(const struct camera3_device *);

    /* reserved for future use */
    void *reserved[8];
} camera3_device_ops_t;

camera3_device_ops_t 映射函数指针操作: hardware/libhardware/modules/camera/3_0/Camera.cpp

const camera3_device_ops_t Camera::sOps = {
    .initialize = default_camera_hal::initialize,
    .configure_streams = default_camera_hal::configure_streams,
    .register_stream_buffers = default_camera_hal::register_stream_buffers,
    .construct_default_request_settings
        = default_camera_hal::construct_default_request_settings,
    .process_capture_request = default_camera_hal::process_capture_request,
    .get_metadata_vendor_tag_ops = NULL,
    .dump = default_camera_hal::dump,
    .flush = default_camera_hal::flush,
    .reserved = {0},
};

上面的函数指针映射只是抽象层提供一个默认映射方法,实际上芯片中都会复写这个指针函数的映射关系。
以高通660芯片为例,实际上映射的函数指针映射关系是hardware/qcom/camera/QCamera2/HAL3/QCamera3HWI.cpp中,不同的芯片会在不同的地方,但是不会相差太大,况且这些函数指针都是一样的,这是android hal层提供的通用调用方法。

camera3_device_ops_t QCamera3HardwareInterface::mCameraOps = {
    .initialize                         = QCamera3HardwareInterface::initialize,
    .configure_streams                  = QCamera3HardwareInterface::configure_streams,
    .register_stream_buffers            = NULL,
    .construct_default_request_settings = QCamera3HardwareInterface::construct_default_request_settings,
    .process_capture_request            = QCamera3HardwareInterface::process_capture_request,
    .get_metadata_vendor_tag_ops        = NULL,
    .dump                               = QCamera3HardwareInterface::dump,
    .flush                              = QCamera3HardwareInterface::flush,
    .reserved                           = {0},
};

在hardware/qcom/camera/QCamera2/HAL3/QCamera3HWI.cpp构造函数中已经建立了这种联系了。

mCameraDevice.ops = &mCameraOps;

现在camera hal层一般使用camx结构,将hal层的核心代码放在./vendor/qcom/proprietary/camx/目录下,但是换汤不换药,还是关注这个函数指针的映射关系,这时候映射到vendor/qcom/proprietary/camx/src/core/hal/camxhal3.cpp



3.3 camera3_callback_ops_t : camera3_callback_ops

typedef struct camera3_callback_ops {
    void (*process_capture_result)(const struct camera3_callback_ops *,
            const camera3_capture_result_t *result);
    void (*notify)(const struct camera3_callback_ops *,
            const camera3_notify_msg_t *msg);

} camera3_callback_ops_t;

camera3_callback_ops_t 是camera provider 到camera service 之间的回调,直接和 ICameraDeviceCallback.h对应,可以直接回调这个接口中的方法IPC调用到 camera service进程中。

3.4 camera3_capture_result_t : camera3_capture_result

typedef struct camera3_capture_result {
    uint32_t frame_number;
    const camera_metadata_t *result;
    uint32_t num_output_buffers;
     const camera3_stream_buffer_t *output_buffers;
     const camera3_stream_buffer_t *input_buffer;
     uint32_t partial_result;
     uint32_t num_physcam_metadata;
     const char **physcam_ids;
     const camera_metadata_t **physcam_metadata;

} camera3_capture_result_t;

3.5 camera3_capture_request_t : camera3_capture_request

typedef struct camera3_capture_request {
    uint32_t frame_number;
    const camera_metadata_t *settings;
    camera3_stream_buffer_t *input_buffer;
    uint32_t num_output_buffers;
    const camera3_stream_buffer_t *output_buffers;
    uint32_t num_physcam_settings;
    const char **physcam_id;
    const camera_metadata_t **physcam_settings;

} camera3_capture_request_t;

执行相机预览的操作,单个camera request请求通过camera service层的 processCaptureRequest()函数发送到HAL 设备上,来进行图像捕获或者图像缓冲区重新处理。

该请求包含用于此捕获的设置,以及用于将结果图像数据写入的输出缓冲区集。它可以选择性地包含输入缓冲区,在这种情况下,请求用于重新处理该输入缓冲区而不是捕获新的用相机传感器拍摄图像。捕获由frame_number标识。

作为响应,相机HAL设备必须发送camera3_capture_result使用process_capture_result()与框架异步结构打回来。

3.6 camera3_request_template_t : camera3_request_template

typedef enum camera3_request_template {
    /**
     * Standard camera preview operation with 3A on auto.
     */
    CAMERA3_TEMPLATE_PREVIEW = 1,

    /**
     * Standard camera high-quality still capture with 3A and flash on auto.
     */
    CAMERA3_TEMPLATE_STILL_CAPTURE = 2,

    /**
     * Standard video recording plus preview with 3A on auto, torch off.
     */
    CAMERA3_TEMPLATE_VIDEO_RECORD = 3,

    /**
     * High-quality still capture while recording video. Application will
     * include preview, video record, and full-resolution YUV or JPEG streams in
     * request. Must not cause stuttering on video stream. 3A on auto.
     */
    CAMERA3_TEMPLATE_VIDEO_SNAPSHOT = 4,

    /**
     * Zero-shutter-lag mode. Application will request preview and
     * full-resolution data for each frame, and reprocess it to JPEG when a
     * still image is requested by user. Settings should provide highest-quality
     * full-resolution images without compromising preview frame rate. 3A on
     * auto.
     */
    CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG = 5,

    /**
     * A basic template for direct application control of capture
     * parameters. All automatic control is disabled (auto-exposure, auto-white
     * balance, auto-focus), and post-processing parameters are set to preview
     * quality. The manual capture parameters (exposure, sensitivity, etc.)
     * are set to reasonable defaults, but should be overridden by the
     * application depending on the intended use case.
     */
    CAMERA3_TEMPLATE_MANUAL = 6,

    /* Total number of templates */
    CAMERA3_TEMPLATE_COUNT,

    /**
     * First value for vendor-defined request templates
     */
    CAMERA3_VENDOR_TEMPLATE_START = 0x40000000

} camera3_request_template_t;

3.7 camera3_notify_msg_t : camera3_notify_msg

typedef struct camera3_notify_msg {
    int type;

    union {
        camera3_error_msg_t error;
        camera3_shutter_msg_t shutter;
        uint8_t generic[32];
    } message;

} camera3_notify_msg_t;

3.8 camera3_shutter_msg_t : camera3_shutter_msg

typedef struct camera3_shutter_msg {
    uint32_t frame_number;
    uint64_t timestamp;

} camera3_shutter_msg_t;

3.9 camera3_error_msg_t : camera3_error_msg

typedef struct camera3_error_msg {
    uint32_t frame_number;
    camera3_stream_t *error_stream;
    int error_code;

} camera3_error_msg_t;

3.10 camera3_error_msg_code_t : camera3_error_msg_code

typedef enum camera3_error_msg_code {
    CAMERA3_MSG_ERROR_DEVICE = 1,

    CAMERA3_MSG_ERROR_REQUEST = 2,

    CAMERA3_MSG_ERROR_RESULT = 3,

    CAMERA3_MSG_ERROR_BUFFER = 4,

    CAMERA3_MSG_NUM_ERRORS

} camera3_error_msg_code_t;

3.11 camera3_msg_type_t : camera3_msg_type

typedef enum camera3_msg_type {
    CAMERA3_MSG_ERROR = 1,

    CAMERA3_MSG_SHUTTER = 2,

    CAMERA3_NUM_MESSAGES

} camera3_msg_type_t;

3.12 camera3_jpeg_blob_t : camera3_jpeg_blob

typedef struct camera3_jpeg_blob {
    uint16_t jpeg_blob_id;
    uint32_t jpeg_size;
} camera3_jpeg_blob_t;

3.13 camera3_stream_buffer_set_t : camera3_stream_buffer_set

typedef struct camera3_stream_buffer_set {
    camera3_stream_t *stream;

    uint32_t num_buffers;

    buffer_handle_t **buffers;

} camera3_stream_buffer_set_t;

3.14 camera3_stream_buffer_t : camera3_stream_buffer

typedef struct camera3_stream_buffer {
    camera3_stream_t *stream;
    buffer_handle_t *buffer;
    int status;
     int acquire_fence;
    int release_fence;

} camera3_stream_buffer_t;

3.15 camera3_buffer_status_t : camera3_buffer_status

typedef enum camera3_buffer_status {
    CAMERA3_BUFFER_STATUS_OK = 0,

    CAMERA3_BUFFER_STATUS_ERROR = 1

} camera3_buffer_status_t;

3.16 camera3_stream_configuration_t : camera3_stream_configuration

typedef struct camera3_stream_configuration {
    uint32_t num_streams;

    camera3_stream_t **streams;

    uint32_t operation_mode;

    const camera_metadata_t *session_parameters;
} camera3_stream_configuration_t;

3.17 camera3_stream_t : camera3_stream

typedef struct camera3_stream {
    int stream_type;
    uint32_t width;
    uint32_t height;
    int format;
    uint32_t usage;
    uint32_t max_buffers;
    void *priv;
    android_dataspace_t data_space;
    int rotation;
    const char* physical_camera_id;
    void *reserved[6];

} camera3_stream_t;

3.18 camera3_stream_configuration_mode_t : camera3_stream_configuration_mode

typedef enum camera3_stream_configuration_mode {
    CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE = 0,
    CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE = 1,
    CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START = 0x8000
} camera3_stream_configuration_mode_t;

3.19 camera3_stream_rotation_t : camera3_stream_rotation

typedef enum camera3_stream_rotation {
    /* No rotation */
    CAMERA3_STREAM_ROTATION_0 = 0,

    /* Rotate by 90 degree counterclockwise */
    CAMERA3_STREAM_ROTATION_90 = 1,

    /* Rotate by 180 degree counterclockwise */
    CAMERA3_STREAM_ROTATION_180 = 2,

    /* Rotate by 270 degree counterclockwise */
    CAMERA3_STREAM_ROTATION_270 = 3
} camera3_stream_rotation_t;

3.20 camera3_stream_type_t : camera3_stream_type

typedef enum camera3_stream_type {
    CAMERA3_STREAM_OUTPUT = 0,

    CAMERA3_STREAM_INPUT = 1,

    CAMERA3_STREAM_BIDIRECTIONAL = 2,

    CAMERA3_NUM_STREAM_TYPES

} camera3_stream_type_t;

未完待续。。。

推荐阅读更多精彩内容

  • 2017年7月29日日记 恶言恶语 我们一家四口欢乐地吃着饺子。来了一个电话,妈妈接了,是房东的弟妹。 那女人说县...
    逸黎阅读 123评论 2 3
  • 最近丢书行动很火爆,艾玛沃特森,赫敏,黄晓明,徐静蕾……纷纷在地铁上丢下了自己的书,很多网友也都拍手叫好,并且加入...
    南稀NAN阅读 40评论 0 0
  • 我们是什么时候学会甜言蜜语的呢,可能是从翅膀被折断的那刻开始吧。因为拥有翅膀被嫉妒,被排挤,最终被折断了翅膀,于是...
    zz南北阅读 14评论 0 0