Change camera HAL to catch up with changes in emulator.

Emulator now allows fake camera to emulate both, back and front cameras at the same time.
Camera HAL is changed in this CL in order to support this.

Change-Id: I4dcf2d569bb0c6a3800b4a8da5edd4777b5a078a
diff --git a/tools/emulator/system/camera/EmulatedCameraFactory.cpp b/tools/emulator/system/camera/EmulatedCameraFactory.cpp
index a805418..35e4e5c 100755
--- a/tools/emulator/system/camera/EmulatedCameraFactory.cpp
+++ b/tools/emulator/system/camera/EmulatedCameraFactory.cpp
@@ -40,9 +40,8 @@
         : mQemuClient(),
           mEmulatedCameras(NULL),
           mEmulatedCameraNum(0),
-          mFakeCameraID(-1),
+          mFakeCameraNum(0),
           mConstructedOK(false)
-
 {
     /* Connect to the factory service in the emulator, and create Qemu cameras. */
     if (mQemuClient.connectClient(NULL) == NO_ERROR) {
@@ -51,9 +50,43 @@
         createQemuCameras();
     }
 
-    if (isFakeCameraEmulationOn()) {
-        /* ID fake camera with the number of created 'qemud' cameras. */
-        mFakeCameraID = mEmulatedCameraNum;
+    if (isBackFakeCameraEmulationOn()) {
+        /* Camera ID. */
+        const int camera_id = mEmulatedCameraNum;
+        /* Use fake camera to emulate back-facing camera. */
+        mEmulatedCameraNum++;
+
+        /* Make sure that array is allocated (in case there were no 'qemu'
+         * cameras created. Note that we preallocate the array so it may contain
+         * two fake cameras: one facing back, and another facing front. */
+        if (mEmulatedCameras == NULL) {
+            mEmulatedCameras = new EmulatedCamera*[mEmulatedCameraNum + 1];
+            if (mEmulatedCameras == NULL) {
+                ALOGE("%s: Unable to allocate emulated camera array for %d entries",
+                     __FUNCTION__, mEmulatedCameraNum);
+                return;
+            }
+            memset(mEmulatedCameras, 0, (mEmulatedCameraNum + 1) * sizeof(EmulatedCamera*));
+        }
+
+        /* Create, and initialize the fake camera */
+        mEmulatedCameras[camera_id] =
+            new EmulatedFakeCamera(camera_id, true, &HAL_MODULE_INFO_SYM.common);
+        if (mEmulatedCameras[camera_id] != NULL) {
+            if (mEmulatedCameras[camera_id]->Initialize() != NO_ERROR) {
+                delete mEmulatedCameras[camera_id];
+                mEmulatedCameras--;
+            }
+        } else {
+            mEmulatedCameras--;
+            ALOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
+        }
+    }
+
+    if (isFrontFakeCameraEmulationOn()) {
+        /* Camera ID. */
+        const int camera_id = mEmulatedCameraNum;
+        /* Use fake camera to emulate front-facing camera. */
         mEmulatedCameraNum++;
 
         /* Make sure that array is allocated (in case there were no 'qemu'
@@ -69,25 +102,21 @@
         }
 
         /* Create, and initialize the fake camera */
-        mEmulatedCameras[mFakeCameraID] =
-            new EmulatedFakeCamera(mFakeCameraID, &HAL_MODULE_INFO_SYM.common);
-        if (mEmulatedCameras[mFakeCameraID] != NULL) {
-            if (mEmulatedCameras[mFakeCameraID]->Initialize() != NO_ERROR) {
-                delete mEmulatedCameras[mFakeCameraID];
+        mEmulatedCameras[camera_id] =
+            new EmulatedFakeCamera(camera_id, false, &HAL_MODULE_INFO_SYM.common);
+        if (mEmulatedCameras[camera_id] != NULL) {
+            if (mEmulatedCameras[camera_id]->Initialize() != NO_ERROR) {
+                delete mEmulatedCameras[camera_id];
                 mEmulatedCameras--;
-                mFakeCameraID = -1;
             }
         } else {
             mEmulatedCameras--;
-            mFakeCameraID = -1;
             ALOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
         }
-    } else {
-        ALOGD("Fake camera emulation is disabled.");
     }
 
-    ALOGV("%d cameras are being emulated. Fake camera ID is %d",
-         mEmulatedCameraNum, mFakeCameraID);
+    ALOGV("%d cameras are being emulated. %d of them are fake cameras.",
+          mEmulatedCameraNum, mFakeCameraNum);
 
     mConstructedOK = true;
 }
@@ -230,8 +259,8 @@
     }
 
     /* Allocate the array for emulated camera instances. Note that we allocate
-     * one more entry for the fake camera emulation. */
-    mEmulatedCameras = new EmulatedCamera*[num + 1];
+     * two more entries for back and front fake camera emulation. */
+    mEmulatedCameras = new EmulatedCamera*[num + 2];
     if (mEmulatedCameras == NULL) {
         ALOGE("%s: Unable to allocate emulated camera array for %d entries",
              __FUNCTION__, num + 1);
@@ -305,13 +334,28 @@
     mEmulatedCameraNum = index;
 }
 
-bool EmulatedCameraFactory::isFakeCameraEmulationOn()
+bool EmulatedCameraFactory::isBackFakeCameraEmulationOn()
 {
-    /* Defined by 'qemu.sf.fake_camera' boot property: If property is there
-     * and contains 'off', fake camera emulation is disabled. */
+    /* Defined by 'qemu.sf.fake_camera' boot property: if property exist, and
+     * is set to 'both', or 'back', then fake camera is used to emulate back
+     * camera. */
     char prop[PROPERTY_VALUE_MAX];
-    if (property_get("qemu.sf.fake_camera", prop, NULL) <= 0 ||
-        strcmp(prop, "off")) {
+    if ((property_get("qemu.sf.fake_camera", prop, NULL) > 0) &&
+        (!strcmp(prop, "both") || !strcmp(prop, "back"))) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool EmulatedCameraFactory::isFrontFakeCameraEmulationOn()
+{
+    /* Defined by 'qemu.sf.fake_camera' boot property: if property exist, and
+     * is set to 'both', or 'front', then fake camera is used to emulate front
+     * camera. */
+    char prop[PROPERTY_VALUE_MAX];
+    if ((property_get("qemu.sf.fake_camera", prop, NULL) > 0) &&
+        (!strcmp(prop, "both") || !strcmp(prop, "front"))) {
         return true;
     } else {
         return false;
diff --git a/tools/emulator/system/camera/EmulatedCameraFactory.h b/tools/emulator/system/camera/EmulatedCameraFactory.h
index 19745a3..0aa4a32 100755
--- a/tools/emulator/system/camera/EmulatedCameraFactory.h
+++ b/tools/emulator/system/camera/EmulatedCameraFactory.h
@@ -131,8 +131,11 @@
      */
     void createQemuCameras();
 
-    /* Checks if fake camera emulation is on. */
-    bool isFakeCameraEmulationOn();
+    /* Checks if fake camera emulation is on for the camera facing back. */
+    bool isBackFakeCameraEmulationOn();
+
+    /* Checks if fake camera emulation is on for the camera facing front. */
+    bool isFrontFakeCameraEmulationOn();
 
     /****************************************************************************
      * Data members.
@@ -145,11 +148,11 @@
     /* Array of cameras available for the emulation. */
     EmulatedCamera**    mEmulatedCameras;
 
-    /* Number of emulated cameras (including the fake one). */
+    /* Number of emulated cameras (including the fake ones). */
     int                 mEmulatedCameraNum;
 
-    /* Fake camera ID. */
-    int                 mFakeCameraID;
+    /* Number of emulated fake cameras. */
+    int                 mFakeCameraNum;
 
     /* Flags whether or not constructor has succeeded. */
     bool                mConstructedOK;
diff --git a/tools/emulator/system/camera/EmulatedFakeCamera.cpp b/tools/emulator/system/camera/EmulatedFakeCamera.cpp
index 461af28..457850d 100755
--- a/tools/emulator/system/camera/EmulatedFakeCamera.cpp
+++ b/tools/emulator/system/camera/EmulatedFakeCamera.cpp
@@ -28,8 +28,11 @@
 
 namespace android {
 
-EmulatedFakeCamera::EmulatedFakeCamera(int cameraId, struct hw_module_t* module)
+EmulatedFakeCamera::EmulatedFakeCamera(int cameraId,
+                                       bool facingBack,
+                                       struct hw_module_t* module)
         : EmulatedCamera(cameraId, module),
+          mFacingBack(facingBack),
           mFakeCameraDevice(this)
 {
 }
@@ -49,10 +52,8 @@
         return res;
     }
 
-    /* Fake camera facing is defined by the qemu.sf.fake_camera boot property. */
-    char prop[PROPERTY_VALUE_MAX];
-    property_get("qemu.sf.fake_camera", prop, EmulatedCamera::FACING_BACK);
-    const char* facing = prop;
+    const char* facing = mFacingBack ? EmulatedCamera::FACING_BACK :
+                                       EmulatedCamera::FACING_FRONT;
 
     mParameters.set(EmulatedCamera::FACING_KEY, facing);
     ALOGD("%s: Fake camera is facing %s", __FUNCTION__, facing);
diff --git a/tools/emulator/system/camera/EmulatedFakeCamera.h b/tools/emulator/system/camera/EmulatedFakeCamera.h
index 3debe9e..4bfbd70 100755
--- a/tools/emulator/system/camera/EmulatedFakeCamera.h
+++ b/tools/emulator/system/camera/EmulatedFakeCamera.h
@@ -35,7 +35,7 @@
 class EmulatedFakeCamera : public EmulatedCamera {
 public:
     /* Constructs EmulatedFakeCamera instance. */
-    EmulatedFakeCamera(int cameraId, struct hw_module_t* module);
+    EmulatedFakeCamera(int cameraId, bool facingBack, struct hw_module_t* module);
 
     /* Destructs EmulatedFakeCamera instance. */
     ~EmulatedFakeCamera();
@@ -62,6 +62,9 @@
      ***************************************************************************/
 
 protected:
+    /* Facing back (true) or front (false) switch. */
+    bool                        mFacingBack;
+
     /* Contained fake camera device object. */
     EmulatedFakeCameraDevice    mFakeCameraDevice;
 };