Android Camera原理之camera HAL底層數據結構與類總結

camera HAL層數據結構很是多,看代碼的時候經常爲了瞭解這些數據結構找半天,爲了方便你們學習,特意總結了一些數據結構以及這些數據結構的位置:android

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 結構以下:api

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;

這個結構體裏面有一個函數指針,什麼地方明確了函數指針的指向了?session

在 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.cppapp

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;

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進程中。ide

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標識。post

做爲響應,相機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;

未完待續。。。

相關文章
相關標籤/搜索