Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 676b21b3 authored by Chien-Yu Chen's avatar Chien-Yu Chen Committed by Yin-Chia Yeh
Browse files

camera: update CameraModule

1. Add more accessor methods to CameraModule to prevent exposing
raw module pointer
2. Use KeyedVector to replace array

Bug: 19897963

Change-Id: I111cc093f09f5fb3c4b13693d5d0687e1f441058
parent 0e442128
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -55,8 +55,7 @@ status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {

    status_t res = OK;

    if (mCameraModule->getRawModule()->module_api_version >=
            CAMERA_MODULE_API_VERSION_2_4) {
    if (mCameraModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
        mFlashControl = new ModuleFlashControl(*mCameraModule, *mCallbacks);
        if (mFlashControl == NULL) {
            ALOGV("%s: cannot create flash control for module api v2.4+",
@@ -66,7 +65,7 @@ status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
    } else {
        uint32_t deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;

        if (mCameraModule->getRawModule()->module_api_version >=
        if (mCameraModule->getModuleApiVersion() >=
                    CAMERA_MODULE_API_VERSION_2_0) {
            camera_info info;
            res = mCameraModule->getCameraInfo(
@@ -224,8 +223,7 @@ status_t CameraFlashlight::prepareDeviceOpen(const String8& cameraId) {
        return NO_INIT;
    }

    if (mCameraModule->getRawModule()->module_api_version <
            CAMERA_MODULE_API_VERSION_2_4) {
    if (mCameraModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_4) {
        // framework is going to open a camera device, all flash light control
        // should be closed for backward compatible support.
        mFlashControl.clear();
@@ -274,8 +272,7 @@ status_t CameraFlashlight::deviceClosed(const String8& cameraId) {
    if (mOpenedCameraIds.size() != 0)
        return OK;

    if (mCameraModule->getRawModule()->module_api_version <
            CAMERA_MODULE_API_VERSION_2_4) {
    if (mCameraModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_4) {
        // notify torch available for all cameras with a flash
        int numCameras = mCameraModule->getNumberOfCameras();
        for (int i = 0; i < numCameras; i++) {
+12 −15
Original line number Diff line number Diff line
@@ -150,8 +150,7 @@ void CameraService::onFirstRef()
    }
    else {
        mModule = new CameraModule(rawModule);
        const hw_module_t *common = mModule->getRawModule();
        ALOGI("Loaded \"%s\" camera module", common->name);
        ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
        mNumberOfCameras = mModule->getNumberOfCameras();

        mFlashlight = new CameraFlashlight(*mModule, *this);
@@ -170,7 +169,7 @@ void CameraService::onFirstRef()
            size_t conflicting_devices_length = 0;

            // If using post-2.4 module version, query the cost + conflicting devices from the HAL
            if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_4) {
            if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
                struct camera_info info;
                status_t rc = mModule->getCameraInfo(i, &info);
                if (rc == NO_ERROR) {
@@ -202,13 +201,13 @@ void CameraService::onFirstRef()
            }
        }

        if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_1) {
        if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
            mModule->setCallbacks(this);
        }

        VendorTagDescriptor::clearGlobalVendorTagDescriptor();

        if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_2) {
        if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
            setUpVendorTags();
        }

@@ -458,7 +457,7 @@ status_t CameraService::getCameraCharacteristics(int cameraId,

    int facing;
    status_t ret = OK;
    if (mModule->getRawModule()->module_api_version < CAMERA_MODULE_API_VERSION_2_0 ||
    if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
            getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) {
        /**
         * Backwards compatibility mode for old HALs:
@@ -551,7 +550,7 @@ int CameraService::getDeviceVersion(int cameraId, int* facing) {
    }

    int deviceVersion;
    if (mModule->getRawModule()->module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
    if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
        deviceVersion = info.device_version;
    } else {
        deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
@@ -1038,7 +1037,7 @@ status_t CameraService::connectLegacy(
        /*out*/
        sp<ICamera>& device) {

    int apiVersion = mModule->getRawModule()->module_api_version;
    int apiVersion = mModule->getModuleApiVersion();
    if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
            apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
        /*
@@ -1876,16 +1875,14 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
            return NO_ERROR;
        }

        const hw_module_t* common = mModule->getRawModule();
        result = String8::format("Camera module HAL API version: %#x\n", common->hal_api_version);
        result.appendFormat("Camera module API version: %#x\n", common->module_api_version);
        result.appendFormat("Camera module name: %s\n", common->name);
        result.appendFormat("Camera module author: %s\n", common->author);
        result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
        result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
        result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
        result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
        result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
        String8 activeClientString = mActiveClientManager.toString();
        result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());


        sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
        if (desc == NULL) {
            result.appendFormat("Vendor tags left unimplemented.\n");
@@ -1932,7 +1929,7 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
                        info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
                result.appendFormat("  Orientation: %d\n", info.orientation);
                int deviceVersion;
                if (common->module_api_version < CAMERA_MODULE_API_VERSION_2_0) {
                if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
                    deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
                } else {
                    deviceVersion = info.device_version;
+34 −16
Original line number Diff line number Diff line
@@ -54,14 +54,12 @@ CameraModule::CameraModule(camera_module_t *module) {
    }

    mModule = module;
    for (int i = 0; i < MAX_CAMERAS_PER_MODULE; i++) {
        mCameraInfoCached[i] = false;
    }
    mCameraInfoMap.setCapacity(getNumberOfCameras());
}

int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
    Mutex::Autolock lock(mCameraInfoLock);
    if (cameraId < 0 || cameraId >= MAX_CAMERAS_PER_MODULE) {
    if (cameraId < 0) {
        ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
        return -EINVAL;
    }
@@ -72,21 +70,27 @@ int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
        return mModule->get_camera_info(cameraId, info);
    }

    camera_info &wrappedInfo = mCameraInfo[cameraId];
    if (!mCameraInfoCached[cameraId]) {
    ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
    if (index == NAME_NOT_FOUND) {
        // Get camera info from raw module and cache it
        CameraInfo cameraInfo;
        camera_info rawInfo;
        int ret = mModule->get_camera_info(cameraId, &rawInfo);
        if (ret != 0) {
            return ret;
        }
        CameraMetadata &m = mCameraCharacteristics[cameraId];
        CameraMetadata &m = cameraInfo.cameraCharacteristics;
        m = rawInfo.static_camera_characteristics;
        deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
        wrappedInfo = rawInfo;
        wrappedInfo.static_camera_characteristics = m.getAndLock();
        mCameraInfoCached[cameraId] = true;
        cameraInfo.cameraInfo = rawInfo;
        cameraInfo.cameraInfo.static_camera_characteristics = m.getAndLock();
        mCameraInfoMap.add(cameraId, cameraInfo);
        index = mCameraInfoMap.indexOfKey(cameraId);
    }
    *info = wrappedInfo;

    assert(index != NAME_NOT_FOUND);
    // return the cached camera info
    *info = mCameraInfoMap[index].cameraInfo;
    return 0;
}

@@ -99,10 +103,6 @@ int CameraModule::openLegacy(
    return mModule->open_legacy(&mModule->common, id, halVersion, device);
}

const hw_module_t* CameraModule::getRawModule() {
    return &mModule->common;
}

int CameraModule::getNumberOfCameras() {
    return mModule->get_number_of_cameras();
}
@@ -125,7 +125,6 @@ int CameraModule::setTorchMode(const char* camera_id, bool enable) {
    return mModule->set_torch_mode(camera_id, enable);
}


status_t CameraModule::filterOpenErrorCode(status_t err) {
    switch(err) {
        case NO_ERROR:
@@ -139,6 +138,25 @@ status_t CameraModule::filterOpenErrorCode(status_t err) {
    return -ENODEV;
}

uint16_t CameraModule::getModuleApiVersion() {
    return mModule->common.module_api_version;
}

const char* CameraModule::getModuleName() {
    return mModule->common.name;
}

uint16_t CameraModule::getHalApiVersion() {
    return mModule->common.hal_api_version;
}

const char* CameraModule::getModuleAuthor() {
    return mModule->common.author;
}

void* CameraModule::getDso() {
    return mModule->common.dso;
}

}; // namespace android
+13 −8
Original line number Diff line number Diff line
@@ -20,10 +20,7 @@
#include <hardware/camera.h>
#include <camera/CameraMetadata.h>
#include <utils/Mutex.h>

/* This needs to be increased if we can have more cameras */
#define MAX_CAMERAS_PER_MODULE 2

#include <utils/KeyedVector.h>

namespace android {
/**
@@ -37,7 +34,6 @@ class CameraModule {
public:
    CameraModule(camera_module_t *module);

    const hw_module_t* getRawModule();
    int getCameraInfo(int cameraId, struct camera_info *info);
    int getNumberOfCameras(void);
    int open(const char* id, struct hw_device_t** device);
@@ -46,16 +42,25 @@ public:
    bool isVendorTagDefined();
    void getVendorTagOps(vendor_tag_ops_t* ops);
    int setTorchMode(const char* camera_id, bool enable);
    uint16_t getModuleApiVersion();
    const char* getModuleName();
    uint16_t getHalApiVersion();
    const char* getModuleAuthor();
    // Only used by CameraModuleFixture native test. Do NOT use elsewhere.
    void *getDso();

private:
    // Derive camera characteristics keys defined after HAL device version
    static void deriveCameraCharacteristicsKeys(uint32_t deviceVersion, CameraMetadata &chars);
    status_t filterOpenErrorCode(status_t err);

    struct CameraInfo {
        CameraMetadata cameraCharacteristics;
        camera_info    cameraInfo;
    };

    camera_module_t *mModule;
    CameraMetadata mCameraCharacteristics[MAX_CAMERAS_PER_MODULE];
    camera_info mCameraInfo[MAX_CAMERAS_PER_MODULE];
    bool mCameraInfoCached[MAX_CAMERAS_PER_MODULE];
    KeyedVector<int, CameraInfo> mCameraInfoMap;
    Mutex mCameraInfoLock;
};

+1 −1
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ public:
        if (res != OK) return res;

        int rc = OK;
        if (module->getRawModule()->module_api_version >= CAMERA_MODULE_API_VERSION_2_3 &&
        if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
            info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
            // Open higher version camera device as HAL1.0 device.
            rc = module->openLegacy(mName.string(),