简介

本文针对以下几个问题进行总结

  • Android系统架构,HAL
  • Android系统中Camera2的框架是什么样的?宏观上理解Android Camera

往期文章

Android 系统架构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j9kb9LSQ-1627209883385)(../../img/android_fwk_all.png)]

如上图所示,Android架构分为应用层Binder IPCSystem ServiceHALLinux Kernel5层

以Camera为视角,Camera类别的App通过IPC调用安卓系统的Camera Service。而Camera Service通过调用HAL提供的接口,最终链接Linux的Camera Driver。

  1. Binder 进程间通信 (IPC) 机制允许应用框架跨越进程边界并调用 Android 系统服务代码,这使得高级框架 API 能与 Android 系统服务进行交互。
  2. HAL 可定义一个标准接口以供硬件供应商实现,这可让 Android 忽略较低级别的驱动程序实现。

HAL

HAL 可定义一个标准接口以供硬件供应商实现,可让Android忽略较低级别的驱动程序实现。HAL实现通常会内置在共享库模块(.so)中。

每个硬件专用 HAL 接口都要具有在 hardware/libhardware/include/hardware/hardware.h中定义的属性。Android根据其中定义的元数据来正确加载HAL模块。

/*
 * The current HAL API version.
 */
#define HARDWARE_HAL_API_VERSION HARDWARE_MAKE_API_VERSION(1, 0)

struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;

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

    uint16_t 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;
		//...
} hw_module_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;

/**
 * Every device data structure must begin with hw_device_t
 * followed by module specific public methods and attributes.
 */
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;
    /** Close this device */
    int (*close)(struct hw_device_t* device);

} hw_device_t;

int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module);

hardware.h定义了一个结构体hw_module_t;其包含了指向hw_module_methods_t的指针,hw_module_methods_t定义了一个打开相应模块的open函数的指针。

HAL类型

HAL有2种类型,以Android8.0为界限,在Android8.0及更高版本中,较低级别的层已重新编写以采用更加模块化的新架构。

  • 绑定式HAL:以 HAL 接口定义语言 (HIDL) 或 Android 接口定义语言 (AIDL) 表示的 HAL。这些 HAL 取代了早期 Android 版本中使用的传统 HAL 和旧版 HAL。在绑定式 HAL 中,Android 框架和 HAL 之间通过 Binder 进程间通信 (IPC) 调用进行通信。
  • 直通式HAL:以 HIDL 封装的传统 HAL 或旧版 HAL。这些 HAL 封装了现有的 HAL。

这也区分了Camera和Camera2的区别。Camera2使用了新的HAL架构,Camera使用了旧的HAL架构。

CameraHAL.h

下面列举了CameraHAL.h相关的代码:

#ifndef CAMERA_HAL_H_
#define CAMERA_HAL_H_

#include <cutils/bitops.h>
#include <hardware/hardware.h>
#include <hardware/camera_common.h>
#include <system/camera_vendor_tags.h>
#include "Camera.h"
#include "VendorTags.h"

namespace default_camera_hal {
// CameraHAL contains all module state that isn't specific to an individual
// camera device.
class CameraHAL {
    public:
        explicit CameraHAL(int num_cameras);
        ~CameraHAL();

        // Camera Module Interface (see <hardware/camera_common.h>)
        int getNumberOfCameras();
        int getCameraInfo(int camera_id, struct camera_info *info);
        int setCallbacks(const camera_module_callbacks_t *callbacks);
        void getVendorTagOps(vendor_tag_ops_t* ops);

        // Hardware Module Interface (see <hardware/hardware.h>)
        int open(const hw_module_t* mod, const char* name, hw_device_t** dev);

    private:
        // Number of cameras
        const int mNumberOfCameras;
        // Callback handle
        const camera_module_callbacks_t *mCallbacks;
        // Array of camera devices, contains mNumberOfCameras device pointers
        Camera **mCameras;
};
} // namespace default_camera_hal
  • CameraHAL包含了getNumberOfCameras(), getCameraInfo(),setCallbacks()等常用方法
  • CameraHAL.h里还包含了一些其他的头文件如<hardware/hardware.h>,<hardware/camera_common.h>, <system/camera_vendor_tags.h>,Camera.h 感兴趣的可以自行查看相关源码,也可关注后续相关文章。

Camera2 相机架构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xucohipd-1627209883389)(/Users/sunquan/mine/myInfo/MkDocs/myInfo/docs/img/ape_fwk_camera2.png)]

整个Camera2的相机架构图如上,分为4层,后续文章也将围绕这4层进行相关源码分析。

应用层

​ 使用camera2提供的API与相机硬件进行互动,内部会调用相应的Binder接口。

CameraDevice

CameraDevice is a representation of a single camera connected to an Android device, allowing for fine-grain control of image capture and post-processing at high frame rates.

CameraManager

A system service manager for detecting, characterizing, and connecting to CameraDevice

AIDL

  • ICameraDeviceUser.aidl 是已打开的特定相机设备的接口如disconnect()cancelRequestflush()等。

  • ICameraService是相机服务的接口如:getNumberOfCameras(int type)connect()addListener()getCameraCharacteristics(String cameraId)

  • ICameraServiceListenerICameraDeviceCallbacks分别是对应用框架的 CameraServiceCameraDevice 回调。

Native

具体与HAL进行互动的实际代码

HIDL Interface

HAL 位于相机驱动程序和更高级别的 Android 框架之间,它定义您必须实现的接口,以便应用可以正确地操作相机硬件。

  • ICameraProvider用于枚举单个设备并管理其状态。提供API如:setCallbackgetVendorTags()getCameraIdList()等。
  • ICameraDevice相机设备接口。提供API如:getCameraCharacteristics()setTorchMode()open()
  • ICameraDeviceSession活跃的相机设备会话接口。提供API如:constructDefaultRequestSettings()configureStreamsprocessCaptureRequestflush()等。

个人简介

工作:无线开发专家 Alibaba
email:sunquan9301@163.com
WX:sunquan97
HomePage:qsun97.com

Logo

开源、云原生的融合云平台

更多推荐