Camera3: Only use Plain Old Data for global variable

When a shared library is loaded, the order in which its global
variables is not defined. For example, for case like below:

A a;
int *ptr = NULL;

Class A {
A() {
  if (ptr == NULL) {
      //do this;
  } else {
      // do that;
  }
}
};

ptr could be initialized before or after A, which causes the
behavior of A's constructor to be different.

The fix is to use Plain Old Data (integer, float, char, or pointer)
as global variable, and defer instantiation of class instance to
runtime.

Bug: 11822202
Change-Id: I806c527f06e19fc44e880d9a536ee7a060b4fa4f
diff --git a/QCamera2/HAL3/QCamera3Factory.cpp b/QCamera2/HAL3/QCamera3Factory.cpp
index 34264b0..65fc27b 100644
--- a/QCamera2/HAL3/QCamera3Factory.cpp
+++ b/QCamera2/HAL3/QCamera3Factory.cpp
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
+/* Copyright (c) 2012-2014, The Linux Foundataion. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -41,7 +41,7 @@
 
 namespace qcamera {
 
-QCamera3Factory gQCamera3Factory;
+QCamera3Factory *gQCamera3Factory = NULL;
 
 /*===========================================================================
  * FUNCTION   : QCamera3Factory
@@ -92,7 +92,14 @@
  *==========================================================================*/
 int QCamera3Factory::get_number_of_cameras()
 {
-    return gQCamera3Factory.getNumberOfCameras();
+    if (!gQCamera3Factory) {
+        gQCamera3Factory = new QCamera3Factory();
+        if (!gQCamera3Factory) {
+            ALOGE("%s: Failed to allocate Camera3Factory object", __func__);
+            return 0;
+        }
+    }
+    return gQCamera3Factory->getNumberOfCameras();
 }
 
 /*===========================================================================
@@ -110,7 +117,7 @@
  *==========================================================================*/
 int QCamera3Factory::get_camera_info(int camera_id, struct camera_info *info)
 {
-    return gQCamera3Factory.getCameraInfo(camera_id, info);
+    return gQCamera3Factory->getCameraInfo(camera_id, info);
 }
 
 /*===========================================================================
@@ -213,7 +220,7 @@
         ALOGE("Invalid camera id");
         return BAD_VALUE;
     }
-    return gQCamera3Factory.cameraDeviceOpen(atoi(id), hw_device);
+    return gQCamera3Factory->cameraDeviceOpen(atoi(id), hw_device);
 }
 
 struct hw_module_methods_t QCamera3Factory::mModuleMethods = {