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
diff --git a/services/camera/libcameraservice/CameraFlashlight.cpp b/services/camera/libcameraservice/CameraFlashlight.cpp
index 6fda9b2..8613ac6 100644
--- a/services/camera/libcameraservice/CameraFlashlight.cpp
+++ b/services/camera/libcameraservice/CameraFlashlight.cpp
@@ -55,8 +55,7 @@
 
     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,8 +65,8 @@
     } else {
         uint32_t deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
 
-        if (mCameraModule->getRawModule()->module_api_version >=
-                CAMERA_MODULE_API_VERSION_2_0) {
+        if (mCameraModule->getModuleApiVersion() >=
+                    CAMERA_MODULE_API_VERSION_2_0) {
             camera_info info;
             res = mCameraModule->getCameraInfo(
                     atoi(String8(cameraId).string()), &info);
@@ -224,8 +223,7 @@
         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 @@
     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++) {
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 55f7a40..9632ea9 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -150,8 +150,7 @@
     }
     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 @@
             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 @@
             }
         }
 
-        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 @@
 
     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 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 @@
         /*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 @@
             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 @@
                         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;
diff --git a/services/camera/libcameraservice/common/CameraModule.cpp b/services/camera/libcameraservice/common/CameraModule.cpp
index 5f767ad..50cece4 100644
--- a/services/camera/libcameraservice/common/CameraModule.cpp
+++ b/services/camera/libcameraservice/common/CameraModule.cpp
@@ -54,14 +54,12 @@
     }
 
     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 @@
         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 @@
     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 @@
     return mModule->set_torch_mode(camera_id, enable);
 }
 
-
 status_t CameraModule::filterOpenErrorCode(status_t err) {
     switch(err) {
         case NO_ERROR:
@@ -139,6 +138,25 @@
     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
 
diff --git a/services/camera/libcameraservice/common/CameraModule.h b/services/camera/libcameraservice/common/CameraModule.h
index 16207aa..ecc5b2d 100644
--- a/services/camera/libcameraservice/common/CameraModule.h
+++ b/services/camera/libcameraservice/common/CameraModule.h
@@ -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 @@
 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 @@
     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;
 };
 
diff --git a/services/camera/libcameraservice/device1/CameraHardwareInterface.h b/services/camera/libcameraservice/device1/CameraHardwareInterface.h
index f5ebbf8..7f14cd4 100644
--- a/services/camera/libcameraservice/device1/CameraHardwareInterface.h
+++ b/services/camera/libcameraservice/device1/CameraHardwareInterface.h
@@ -97,7 +97,7 @@
         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(),