1 /* Copyright (c) 2012-2014, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #define LOG_TAG "QCamera2Factory"
31 //#define LOG_NDEBUG 0
32 
33 #include <stdlib.h>
34 #include <utils/Log.h>
35 #include <utils/Errors.h>
36 #include <hardware/camera.h>
37 #include <hardware/camera3.h>
38 
39 #include "HAL/QCamera2HWI.h"
40 #include "HAL3/QCamera3HWI.h"
41 #include "QCamera2Factory.h"
42 
43 using namespace android;
44 
45 namespace qcamera {
46 
47 QCamera2Factory *gQCamera2Factory = NULL;
48 
49 /*===========================================================================
50  * FUNCTION   : QCamera2Factory
51  *
52  * DESCRIPTION: default constructor of QCamera2Factory
53  *
54  * PARAMETERS : none
55  *
56  * RETURN     : None
57  *==========================================================================*/
QCamera2Factory()58 QCamera2Factory::QCamera2Factory()
59 {
60     camera_info info;
61     int i = 0;
62     mHalDescriptors = NULL;
63     mCallbacks = NULL;
64     mNumOfCameras = get_num_of_cameras();
65     char prop[PROPERTY_VALUE_MAX];
66     property_get("persist.camera.HAL3.enabled", prop, "0");
67     int isHAL3Enabled = atoi(prop);
68     //Query camera at this point in order
69     //to avoid any delays during subsequent
70     //calls to 'getCameraInfo()'
71     for (int i = 0 ; i < mNumOfCameras ; i++) {
72         getCameraInfo(i, &info);
73     }
74     //
75 
76     if ((mNumOfCameras > 0) && (mNumOfCameras <= MM_CAMERA_MAX_NUM_SENSORS)) {
77         mHalDescriptors = new hal_desc[mNumOfCameras];
78         if ( NULL != mHalDescriptors) {
79             uint32_t cameraId = 0;
80 
81             for (; i < mNumOfCameras ; i++, cameraId++) {
82                 mHalDescriptors[i].cameraId = cameraId;
83                 if (isHAL3Enabled) {
84                     mHalDescriptors[i].device_version = CAMERA_DEVICE_API_VERSION_3_0;
85                 } else {
86                     mHalDescriptors[i].device_version = CAMERA_DEVICE_API_VERSION_1_0;
87                 }
88             }
89         } else {
90             ALOGE("%s: Not enough resources to allocate HAL descriptor table!",
91                   __func__);
92         }
93     } else {
94         ALOGE("%s: %d camera devices detected!", __func__, mNumOfCameras);
95     }
96 }
97 
98 /*===========================================================================
99  * FUNCTION   : ~QCamera2Factory
100  *
101  * DESCRIPTION: deconstructor of QCamera2Factory
102  *
103  * PARAMETERS : none
104  *
105  * RETURN     : None
106  *==========================================================================*/
~QCamera2Factory()107 QCamera2Factory::~QCamera2Factory()
108 {
109     if ( NULL != mHalDescriptors ) {
110         delete [] mHalDescriptors;
111     }
112 }
113 
114 /*===========================================================================
115  * FUNCTION   : get_number_of_cameras
116  *
117  * DESCRIPTION: static function to query number of cameras detected
118  *
119  * PARAMETERS : none
120  *
121  * RETURN     : number of cameras detected
122  *==========================================================================*/
get_number_of_cameras()123 int QCamera2Factory::get_number_of_cameras()
124 {
125     if (!gQCamera2Factory) {
126         gQCamera2Factory = new QCamera2Factory();
127         if (!gQCamera2Factory) {
128             ALOGE("%s: Failed to allocate Camera2Factory object", __func__);
129             return 0;
130         }
131     }
132     return gQCamera2Factory->getNumberOfCameras();
133 }
134 
135 /*===========================================================================
136  * FUNCTION   : get_camera_info
137  *
138  * DESCRIPTION: static function to query camera information with its ID
139  *
140  * PARAMETERS :
141  *   @camera_id : camera ID
142  *   @info      : ptr to camera info struct
143  *
144  * RETURN     : int32_t type of status
145  *              NO_ERROR  -- success
146  *              none-zero failure code
147  *==========================================================================*/
get_camera_info(int camera_id,struct camera_info * info)148 int QCamera2Factory::get_camera_info(int camera_id, struct camera_info *info)
149 {
150     return gQCamera2Factory->getCameraInfo(camera_id, info);
151 }
152 
153 /*===========================================================================
154  * FUNCTION   : set_callbacks
155  *
156  * DESCRIPTION: static function to set callbacks function to camera module
157  *
158  * PARAMETERS :
159  *   @callbacks : ptr to callback functions
160  *
161  * RETURN     : NO_ERROR  -- success
162  *              none-zero failure code
163  *==========================================================================*/
set_callbacks(const camera_module_callbacks_t * callbacks)164 int QCamera2Factory::set_callbacks(const camera_module_callbacks_t *callbacks)
165 {
166     return gQCamera2Factory->setCallbacks(callbacks);
167 }
168 
169 /*===========================================================================
170  * FUNCTION   : open_legacy
171  *
172  * DESCRIPTION: Function to open older hal version implementation
173  *
174  * PARAMETERS :
175  *   @hw_device : ptr to struct storing camera hardware device info
176  *   @camera_id : camera ID
177  *   @halVersion: Based on camera_module_t.common.module_api_version
178  *
179  * RETURN     : 0  -- success
180  *              none-zero failure code
181  *==========================================================================*/
open_legacy(const struct hw_module_t * module,const char * id,uint32_t halVersion,struct hw_device_t ** device)182 int QCamera2Factory::open_legacy(const struct hw_module_t* module,
183             const char* id, uint32_t halVersion, struct hw_device_t** device)
184 {
185     return -ENOSYS;
186 }
187 
188 /*===========================================================================
189  * FUNCTION   : getNumberOfCameras
190  *
191  * DESCRIPTION: query number of cameras detected
192  *
193  * PARAMETERS : none
194  *
195  * RETURN     : number of cameras detected
196  *==========================================================================*/
getNumberOfCameras()197 int QCamera2Factory::getNumberOfCameras()
198 {
199     return mNumOfCameras;
200 }
201 
202 /*===========================================================================
203  * FUNCTION   : getCameraInfo
204  *
205  * DESCRIPTION: query camera information with its ID
206  *
207  * PARAMETERS :
208  *   @camera_id : camera ID
209  *   @info      : ptr to camera info struct
210  *
211  * RETURN     : int32_t type of status
212  *              NO_ERROR  -- success
213  *              none-zero failure code
214  *==========================================================================*/
getCameraInfo(int camera_id,struct camera_info * info)215 int QCamera2Factory::getCameraInfo(int camera_id, struct camera_info *info)
216 {
217     int rc;
218     ALOGV("%s: E, camera_id = %d", __func__, camera_id);
219 
220     if (!mNumOfCameras || camera_id >= mNumOfCameras || !info ||
221         (camera_id < 0)) {
222         return -ENODEV;
223     }
224 
225     if ( NULL == mHalDescriptors ) {
226         ALOGE("%s : Hal descriptor table is not initialized!", __func__);
227         return NO_INIT;
228     }
229 
230     if ( mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_3_0 ) {
231         rc = QCamera3HardwareInterface::getCamInfo(mHalDescriptors[camera_id].cameraId, info);
232     } else if (mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_1_0) {
233         rc = QCamera2HardwareInterface::getCapabilities(mHalDescriptors[camera_id].cameraId, info);
234     } else {
235         ALOGE("%s: Device version for camera id %d invalid %d",
236               __func__,
237               camera_id,
238               mHalDescriptors[camera_id].device_version);
239         return BAD_VALUE;
240     }
241 
242     ALOGV("%s: X", __func__);
243     return rc;
244 }
245 
246 /*===========================================================================
247  * FUNCTION   : setCallbacks
248  *
249  * DESCRIPTION: set callback functions to send asynchronous notifications to
250  *              frameworks.
251  *
252  * PARAMETERS :
253  *   @callbacks : callback function pointer
254  *
255  * RETURN     :
256  *              NO_ERROR  -- success
257  *              none-zero failure code
258  *==========================================================================*/
setCallbacks(const camera_module_callbacks_t * callbacks)259 int QCamera2Factory::setCallbacks(const camera_module_callbacks_t *callbacks)
260 {
261     int rc = NO_ERROR;
262     mCallbacks = callbacks;
263     return rc;
264 }
265 
266 /*===========================================================================
267  * FUNCTION   : cameraDeviceOpen
268  *
269  * DESCRIPTION: open a camera device with its ID
270  *
271  * PARAMETERS :
272  *   @camera_id : camera ID
273  *   @hw_device : ptr to struct storing camera hardware device info
274  *
275  * RETURN     : int32_t type of status
276  *              NO_ERROR  -- success
277  *              none-zero failure code
278  *==========================================================================*/
cameraDeviceOpen(int camera_id,struct hw_device_t ** hw_device)279 int QCamera2Factory::cameraDeviceOpen(int camera_id,
280                     struct hw_device_t **hw_device)
281 {
282     int rc = NO_ERROR;
283     if (camera_id < 0 || camera_id >= mNumOfCameras)
284         return -ENODEV;
285 
286     if ( NULL == mHalDescriptors ) {
287         ALOGE("%s : Hal descriptor table is not initialized!", __func__);
288         return NO_INIT;
289     }
290 
291     if ( mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_3_0 ) {
292         QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(mHalDescriptors[camera_id].cameraId,
293                 mCallbacks);
294         if (!hw) {
295             ALOGE("Allocation of hardware interface failed");
296             return NO_MEMORY;
297         }
298         rc = hw->openCamera(hw_device);
299         if (rc != 0) {
300             delete hw;
301         }
302     } else if (mHalDescriptors[camera_id].device_version == CAMERA_DEVICE_API_VERSION_1_0) {
303         QCamera2HardwareInterface *hw = new QCamera2HardwareInterface(camera_id);
304         if (!hw) {
305             ALOGE("Allocation of hardware interface failed");
306             return NO_MEMORY;
307         }
308         rc = hw->openCamera(hw_device);
309         if (rc != NO_ERROR) {
310             delete hw;
311         }
312     } else {
313         ALOGE("%s: Device version for camera id %d invalid %d",
314               __func__,
315               camera_id,
316               mHalDescriptors[camera_id].device_version);
317         return BAD_VALUE;
318     }
319 
320     return rc;
321 }
322 
323 /*===========================================================================
324  * FUNCTION   : camera_device_open
325  *
326  * DESCRIPTION: static function to open a camera device by its ID
327  *
328  * PARAMETERS :
329  *   @camera_id : camera ID
330  *   @hw_device : ptr to struct storing camera hardware device info
331  *
332  * RETURN     : int32_t type of status
333  *              NO_ERROR  -- success
334  *              none-zero failure code
335  *==========================================================================*/
camera_device_open(const struct hw_module_t * module,const char * id,struct hw_device_t ** hw_device)336 int QCamera2Factory::camera_device_open(
337     const struct hw_module_t *module, const char *id,
338     struct hw_device_t **hw_device)
339 {
340     if (module != &HAL_MODULE_INFO_SYM.common) {
341         ALOGE("Invalid module. Trying to open %p, expect %p",
342             module, &HAL_MODULE_INFO_SYM.common);
343         return INVALID_OPERATION;
344     }
345     if (!id) {
346         ALOGE("Invalid camera id");
347         return BAD_VALUE;
348     }
349     return gQCamera2Factory->cameraDeviceOpen(atoi(id), hw_device);
350 }
351 
352 struct hw_module_methods_t QCamera2Factory::mModuleMethods = {
353     open: QCamera2Factory::camera_device_open,
354 };
355 
356 }; // namespace qcamera
357 
358