Summary of underlying data structure and class of camera HAL of Android Camera principle

Posted by jpopuk on Sun, 20 Feb 2022 05:36:02 +0100

There are many data structures in the camera HAL layer. When looking at the code, we often look for a long time to understand these data structures. In order to facilitate everyone's study, we specially summarize some data structures and their locations:

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

Where hw_module_methods_t structure is as follows:

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;

There is a function pointer in this structure. Where does it specify the direction of the function pointer?

In hardware / libhardware / modules / camera / 3_ 0/CameraHAL. Line 167 in CPP specifies the function pointer. Point to open_dev function.

static int open_dev(const hw_module_t* mod, const char* name, hw_device_t** dev)
{
    return gCameraHAL.open(mod, name, dev);
}

static hw_module_methods_t gCameraModuleMethods = {
    .open = open_dev
};

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 mapping function pointer operation: 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},
};

The above function pointer mapping is only a default mapping method provided by the abstraction layer. In fact, the mapping relationship of this pointer function will be copied in the chip.
Taking Qualcomm 660 chip as an example, the function pointer mapping relationship is actually hardware / qcom / camera / qcamera2 / hal3 / qcamera3hwi In CPP, different chips will be in different places, but there will be no big difference. Moreover, these function pointers are the same, which is a general calling method provided by android hal layer.

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},
};

At hardware / qcom / camera / qcamera2 / hal3 / qcamera3hwi This connection has been established in the CPP constructor.

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 is the callback from the camera provider to the camera service, which is directly connected to the icameradevicecallback Corresponding to h, you can directly call back the method IPC in this interface to the camera service process.

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;

Perform the camera preview operation. A single camera request is sent to the HAL device through the processCaptureRequest() function of the camera service layer for image capture or image buffer reprocessing.

The request contains the settings for this capture and the set of output buffers used to write the resulting image data. It can optionally contain an input buffer, in which case the request is used to reprocess the input buffer instead of capturing a new image taken with the camera sensor. Captured by frame_number ID.

In response, the camera HAL device must send camera3_capture_result uses process_capture_result () and the asynchronous structure of the framework call back.

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;

OBJECTIVEC copy full screen

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;

To be continued...

Topics: Java Android data structure