欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Camera 五 Camera HAL v1

程序员文章站 2022-05-08 22:38:14
...

Android Camera 一 源码路径

Android Camera 二 JNI JAVA和C/CPP图像数据传输流程分析

Android Camera 三 CameraService 和 Client 链接到 HAL

Android Camera 四 Camera HAL 分析

Android Camera 五 Camera HAL v1

Linux v4l2 一 应用层

Linux v4l2 二 驱动和 usb 摄像头

 

Android硬件抽象层(Hardware Abstraction Layer,HAL) 承上启下;

  • 承上:连接camera framework 层APIs 。
  • 启下:连接底层 Camera 驱动和硬件 。

hardware/libhardware/include/hardware/camera.h

  •  HAL API 接口声明

hardware/camera/capability_manager/CapabilityManager.cpp

  • camera 能力集管理,如 曝光 白平衡 聚焦 相机支持的图像格式等

hardware/camera/camera_hal/CameraHalImpl.cpp

  • camera hal 接口实现

hardware/camera/camera_hal/CameraHal.cpp    

  • 启下 链接底层 camera 驱动和硬件

hardware/camera/camera_hal/CameraModule.cpp  

  •  承上  HAL module  注册

 

camera.h 定义了 HAL 的标准接口API , HAL 层中代码实现并注册 camera.h 中声明的函数指针。

#ifndef ANDROID_INCLUDE_CAMERA_H
#define ANDROID_INCLUDE_CAMERA_H

#include "camera_common.h"

/**
 * Camera device HAL, initial version [ CAMERA_DEVICE_API_VERSION_1_0 ]
 *
 * DEPRECATED. New devices should use Camera HAL v3.2 or newer.
 *
 * Supports the android.hardware.Camera API, and the android.hardware.camera2
 * API in legacy mode only.
 *
 * Camera devices that support this version of the HAL must return a value in
 * the range HARDWARE_DEVICE_API_VERSION(0,0)-(1,FF) in
 * camera_device_t.common.version. CAMERA_DEVICE_API_VERSION_1_0 is the
 * recommended value.
 *
 * Camera modules that implement version 2.0 or higher of camera_module_t must
 * also return the value of camera_device_t.common.version in
 * camera_info_t.device_version.
 *
 * See camera_common.h for more details.
 */

__BEGIN_DECLS

struct camera_memory;
typedef void (*camera_release_memory)(struct camera_memory *mem);

typedef struct camera_memory
{
    void *data;
    size_t size;
    void *handle;
    camera_release_memory release;
} camera_memory_t;

typedef camera_memory_t* (*camera_request_memory)(int fd, size_t buf_size, unsigned int num_bufs,
        void *user);

typedef void (*camera_notify_callback)(int32_t msg_type,
                                       int32_t ext1,
                                       int32_t ext2,
                                       void *user);

typedef void (*camera_data_callback)(int32_t msg_type,
                                     const camera_memory_t *data, unsigned int index,
                                     camera_frame_metadata_t *metadata, void *user);

typedef void (*camera_data_timestamp_callback)(int64_t timestamp,
        int32_t msg_type,
        const camera_memory_t *data, unsigned int index,
        void *user);

#define HAL_CAMERA_PREVIEW_WINDOW_TAG 0xcafed00d

typedef struct preview_stream_ops
{
    int (*dequeue_buffer)(struct preview_stream_ops* w,
                          buffer_handle_t** buffer, int *stride);
    int (*enqueue_buffer)(struct preview_stream_ops* w,
                          buffer_handle_t* buffer);
    int (*cancel_buffer)(struct preview_stream_ops* w,
                         buffer_handle_t* buffer);
    int (*set_buffer_count)(struct preview_stream_ops* w, int count);
    int (*set_buffers_geometry)(struct preview_stream_ops* pw,
                                int w, int h, int format);
    int (*set_crop)(struct preview_stream_ops *w,
                    int left, int top, int right, int bottom);
    int (*set_usage)(struct preview_stream_ops* w, int usage);
    int (*set_swap_interval)(struct preview_stream_ops *w, int interval);
    int (*get_min_undequeued_buffer_count)(const struct preview_stream_ops *w,
                                           int *count);
    int (*lock_buffer)(struct preview_stream_ops* w,
                       buffer_handle_t* buffer);

    int (*set_timestamp)(struct preview_stream_ops *w, int64_t timestamp);
} preview_stream_ops_t;

struct camera_device;
typedef struct camera_device_ops
{
    /* 设置将预览帧发送到的活动窗口 */
    int (*set_preview_window)(struct camera_device *,
                              struct preview_stream_ops *window);

    /* 设置通知和数据回调 */
    void (*set_callbacks)(struct camera_device *,
                          camera_notify_callback notify_cb,
                          camera_data_callback data_cb,
                          camera_data_timestamp_callback data_cb_timestamp,
                          camera_request_memory get_memory,
                          void *user);

    void (*enable_msg_type)(struct camera_device *, int32_t msg_type);

    void (*disable_msg_type)(struct camera_device *, int32_t msg_type);

    int (*msg_type_enabled)(struct camera_device *, int32_t msg_type);

    int (*start_preview)(struct camera_device *);

    void (*stop_preview)(struct camera_device *);

    /** 正在预览返回 true */
    int (*preview_enabled)(struct camera_device *);

    int (*store_meta_data_in_buffers)(struct camera_device *, int enable);

    int (*start_recording)(struct camera_device *);

    void (*stop_recording)(struct camera_device *);

    int (*recording_enabled)(struct camera_device *);

    void (*release_recording_frame)(struct camera_device *,
                                    const void *opaque);

    int (*auto_focus)(struct camera_device *);

    int (*cancel_auto_focus)(struct camera_device *);

    int (*take_picture)(struct camera_device *);

    int (*cancel_picture)(struct camera_device *);

    int (*set_parameters)(struct camera_device *, const char *parms);

    char *(*get_parameters)(struct camera_device *);

    void (*put_parameters)(struct camera_device *, char *);

    int (*send_command)(struct camera_device *,
                        int32_t cmd, int32_t arg1, int32_t arg2);

    void (*release)(struct camera_device *);

    int (*dump)(struct camera_device *, int fd);
} camera_device_ops_t;

typedef struct camera_device
{
    /**
     * camera_device.common.version must be in the range
     * HARDWARE_DEVICE_API_VERSION(0,0)-(1,FF). CAMERA_DEVICE_API_VERSION_1_0 is
     * recommended.
     */
    hw_device_t common;
    camera_device_ops_t *ops;
    void *priv;
} camera_device_t;

__END_DECLS

#endif /* #ifdef ANDROID_INCLUDE_CAMERA_H */

 

来看看 camera HAL 的注册:

把实现的回调函数注册到 camera.h 中声明的 struct camera_device_t  

在 framework 中 CameraDevice.cpp 匹配 camera HAL  来操作相机硬件设备。

CameraDevice::CameraDevice(
    sp<CameraModule> module, const std::string& cameraId,
    const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames) :
        mModule(module),
        mCameraId(cameraId),
        mDisconnected(false),
        mCameraDeviceNames(cameraDeviceNames);

 

// hardware/camera/camera_hal/CameraModule.cpp

/* 注册 camera 设备操作类方法 */
static int camera_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
{
    CAMERA_HAL_LOGV("enter %s()", __FUNCTION__);

    int ret         = 0;
    int camera_id   = 0;

    camera_device_t* camera_device              = NULL;
    camera_device_ops_t* camera_ops             = NULL;
    android::CameraHal* camera_hal   = NULL;

    if(!name || !device)
    {
        ALOGE("invalid parameter[name=%p, device=%p]", name, device);
        return -EINVAL;
    }

    camera_id  = atoi(name);
    camera_hal = new android::CameraHal(camera_id);
    if(!camera_hal || !camera_hal->mInitOK)
    {
        ALOGE("fail to allocate memory for CameraHal or fail to init CameraHal");
        ret = -ENOMEM;
        goto EXIT;
        //return -EINVAL;
    }

    camera_device   = new camera_device_t;
    camera_ops      = new camera_device_ops_t;
    if(!camera_device || !camera_ops)
    {
        ALOGE("fail to allocate memory for camera_device_t or camera_device_ops_t");
        ret = -ENOMEM;
        goto EXIT;
    }

    memset(camera_device, 0x00, sizeof(*camera_device));
    memset(camera_ops, 0x00, sizeof(*camera_ops));
	/* 注册回调函数 ,操作 camera */
    camera_device->common.tag                 = HARDWARE_DEVICE_TAG;
    camera_device->common.version             = 0;
    camera_device->common.module              = const_cast<hw_module_t*>(module);
    camera_device->common.close               = camera_device_close;
    camera_device->ops                        = camera_ops;
    camera_device->priv                       = camera_hal;

    camera_ops->set_preview_window            = set_preview_window;
    camera_ops->set_callbacks                 = set_callbacks;
    camera_ops->auto_focus                    = auto_focus;
    camera_ops->enable_msg_type               = enable_msg_type;
    camera_ops->disable_msg_type              = disable_msg_type;
    camera_ops->msg_type_enabled              = msg_type_enabled;
    camera_ops->start_preview                 = start_preview;
    camera_ops->stop_preview                  = stop_preview;
    camera_ops->preview_enabled               = preview_enabled;
    camera_ops->store_meta_data_in_buffers    = store_meta_data_in_buffers;
    camera_ops->start_recording               = start_recording;
    camera_ops->stop_recording                = stop_recording;
    camera_ops->recording_enabled             = recording_enabled;
    camera_ops->release_recording_frame       = release_recording_frame;
    camera_ops->cancel_auto_focus             = cancel_auto_focus;
    camera_ops->take_picture                  = take_picture;
    camera_ops->cancel_picture                = cancel_picture;
    camera_ops->set_parameters                = set_parameters;
    camera_ops->get_parameters                = get_parameters;
    camera_ops->put_parameters                = put_parameters;
    camera_ops->send_command                  = send_command;
    camera_ops->release                       = release;
    camera_ops->dump                          = dump;

    *device                                   = &camera_device->common;

    return 0;

EXIT:
    if(camera_hal)
    {
        delete camera_hal;
        camera_hal = NULL;
    }

    if(camera_device)
    {
        delete camera_device;
        camera_device = NULL;
    }

    if(camera_ops)
    {
        delete camera_ops;
        camera_ops = NULL;
    }

    return -1;
}

/*  camera hardware module methods */
static struct hw_module_methods_t camera_module_methods =
{
open : camera_device_open
};

/*  externel interface for camera service */
/* 注册 HAL 必须实现的字段 */
struct camera_module HAL_MODULE_INFO_SYM =
{
common:
    {
tag                 : HARDWARE_MODULE_TAG,
        version_major       : 1,
        version_minor       : 0,
id                  : CAMERA_HARDWARE_MODULE_ID,
name                : "Camera module",
author              : "xxxxxx",
methods             : &camera_module_methods,
dso                 : NULL,
reserved            : {0},
    },

get_number_of_cameras   :camera_get_number_of_cameras,
get_camera_info         :camera_get_camera_info,
};

 

 

 

 

相关标签: camera android