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 "QCamera3Factory"
31 //#define LOG_NDEBUG 0
32 
33 #include <stdlib.h>
34 #include <utils/Log.h>
35 #include <utils/Errors.h>
36 #include <hardware/camera3.h>
37 
38 #include "QCamera3Factory.h"
39 
40 using namespace android;
41 
42 namespace qcamera {
43 
44 QCamera3Factory *gQCamera3Factory = NULL;
45 
46 /*===========================================================================
47  * FUNCTION   : QCamera3Factory
48  *
49  * DESCRIPTION: default constructor of QCamera3Factory
50  *
51  * PARAMETERS : none
52  *
53  * RETURN     : None
54  *==========================================================================*/
QCamera3Factory()55 QCamera3Factory::QCamera3Factory()
56 {
57     camera_info info;
58 
59     mCallbacks = NULL;
60     mNumOfCameras = get_num_of_cameras();
61 
62     //Query camera at this point in order
63     //to avoid any delays during subsequent
64     //calls to 'getCameraInfo()'
65     for (int i = 0 ; i < mNumOfCameras ; i++) {
66         getCameraInfo(i, &info);
67     }
68     //
69 
70 }
71 
72 /*===========================================================================
73  * FUNCTION   : ~QCamera3Factory
74  *
75  * DESCRIPTION: deconstructor of QCamera2Factory
76  *
77  * PARAMETERS : none
78  *
79  * RETURN     : None
80  *==========================================================================*/
~QCamera3Factory()81 QCamera3Factory::~QCamera3Factory()
82 {
83 }
84 
85 /*===========================================================================
86  * FUNCTION   : get_number_of_cameras
87  *
88  * DESCRIPTION: static function to query number of cameras detected
89  *
90  * PARAMETERS : none
91  *
92  * RETURN     : number of cameras detected
93  *==========================================================================*/
get_number_of_cameras()94 int QCamera3Factory::get_number_of_cameras()
95 {
96     if (!gQCamera3Factory) {
97         gQCamera3Factory = new QCamera3Factory();
98         if (!gQCamera3Factory) {
99             ALOGE("%s: Failed to allocate Camera3Factory object", __func__);
100             return 0;
101         }
102     }
103     return gQCamera3Factory->getNumberOfCameras();
104 }
105 
106 /*===========================================================================
107  * FUNCTION   : get_camera_info
108  *
109  * DESCRIPTION: static function to query camera information with its ID
110  *
111  * PARAMETERS :
112  *   @camera_id : camera ID
113  *   @info      : ptr to camera info struct
114  *
115  * RETURN     : int32_t type of status
116  *              NO_ERROR  -- success
117  *              none-zero failure code
118  *==========================================================================*/
get_camera_info(int camera_id,struct camera_info * info)119 int QCamera3Factory::get_camera_info(int camera_id, struct camera_info *info)
120 {
121     return gQCamera3Factory->getCameraInfo(camera_id, info);
122 }
123 
124 /*===========================================================================
125  * FUNCTION   : set_callbacks
126  *
127  * DESCRIPTION: static function to set callbacks function to camera module
128  *
129  * PARAMETERS :
130  *   @callbacks : ptr to callback functions
131  *
132  * RETURN     : NO_ERROR  -- success
133  *              none-zero failure code
134  *==========================================================================*/
set_callbacks(const camera_module_callbacks_t * callbacks)135 int QCamera3Factory::set_callbacks(const camera_module_callbacks_t *callbacks)
136 {
137     return gQCamera3Factory->setCallbacks(callbacks);
138 }
139 
140 /*===========================================================================
141  * FUNCTION   : open_legacy
142  *
143  * DESCRIPTION: Function to open older hal version implementation
144  *
145  * PARAMETERS :
146  *   @hw_device : ptr to struct storing camera hardware device info
147  *   @camera_id : camera ID
148  *   @halVersion: Based on camera_module_t.common.module_api_version
149  *
150  * RETURN     : 0  -- success
151  *              none-zero failure code
152  *==========================================================================*/
open_legacy(const struct hw_module_t * module,const char * id,uint32_t halVersion,struct hw_device_t ** device)153 int QCamera3Factory::open_legacy(const struct hw_module_t* module,
154             const char* id, uint32_t halVersion, struct hw_device_t** device)
155 {
156     return -ENOSYS;
157 }
158 
159 /*===========================================================================
160  * FUNCTION   : getNumberOfCameras
161  *
162  * DESCRIPTION: query number of cameras detected
163  *
164  * PARAMETERS : none
165  *
166  * RETURN     : number of cameras detected
167  *==========================================================================*/
getNumberOfCameras()168 int QCamera3Factory::getNumberOfCameras()
169 {
170     return mNumOfCameras;
171 }
172 
173 /*===========================================================================
174  * FUNCTION   : getCameraInfo
175  *
176  * DESCRIPTION: query camera information with its ID
177  *
178  * PARAMETERS :
179  *   @camera_id : camera ID
180  *   @info      : ptr to camera info struct
181  *
182  * RETURN     : int32_t type of status
183  *              NO_ERROR  -- success
184  *              none-zero failure code
185  *==========================================================================*/
getCameraInfo(int camera_id,struct camera_info * info)186 int QCamera3Factory::getCameraInfo(int camera_id, struct camera_info *info)
187 {
188     int rc;
189     ALOGV("%s: E, camera_id = %d", __func__, camera_id);
190 
191     if (!mNumOfCameras || camera_id >= mNumOfCameras || !info ||
192         (camera_id < 0)) {
193         return -ENODEV;
194     }
195 
196     rc = QCamera3HardwareInterface::getCamInfo(camera_id, info);
197     ALOGV("%s: X", __func__);
198     return rc;
199 }
200 
201 /*===========================================================================
202  * FUNCTION   : setCallbacks
203  *
204  * DESCRIPTION: set callback functions to send asynchronous notifications to
205  *              frameworks.
206  *
207  * PARAMETERS :
208  *   @callbacks : callback function pointer
209  *
210  * RETURN     :
211  *              NO_ERROR  -- success
212  *              none-zero failure code
213  *==========================================================================*/
setCallbacks(const camera_module_callbacks_t * callbacks)214 int QCamera3Factory::setCallbacks(const camera_module_callbacks_t *callbacks)
215 {
216     int rc = NO_ERROR;
217     mCallbacks = callbacks;
218     return rc;
219 }
220 
221 /*===========================================================================
222  * FUNCTION   : cameraDeviceOpen
223  *
224  * DESCRIPTION: open a camera device with its ID
225  *
226  * PARAMETERS :
227  *   @camera_id : camera ID
228  *   @hw_device : ptr to struct storing camera hardware device info
229  *
230  * RETURN     : int32_t type of status
231  *              NO_ERROR  -- success
232  *              none-zero failure code
233  *==========================================================================*/
cameraDeviceOpen(int camera_id,struct hw_device_t ** hw_device)234 int QCamera3Factory::cameraDeviceOpen(int camera_id,
235                     struct hw_device_t **hw_device)
236 {
237     int rc = NO_ERROR;
238     if (camera_id < 0 || camera_id >= mNumOfCameras)
239         return -ENODEV;
240 
241     QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(
242             camera_id, mCallbacks);
243     if (!hw) {
244         ALOGE("Allocation of hardware interface failed");
245         return NO_MEMORY;
246     }
247     rc = hw->openCamera(hw_device);
248     if (rc != 0) {
249         delete hw;
250     }
251     return rc;
252 }
253 
254 /*===========================================================================
255  * FUNCTION   : camera_device_open
256  *
257  * DESCRIPTION: static function to open a camera device by its ID
258  *
259  * PARAMETERS :
260  *   @camera_id : camera ID
261  *   @hw_device : ptr to struct storing camera hardware device info
262  *
263  * RETURN     : int32_t type of status
264  *              NO_ERROR  -- success
265  *              none-zero failure code
266  *==========================================================================*/
camera_device_open(const struct hw_module_t * module,const char * id,struct hw_device_t ** hw_device)267 int QCamera3Factory::camera_device_open(
268     const struct hw_module_t *module, const char *id,
269     struct hw_device_t **hw_device)
270 {
271     if (module != &HAL_MODULE_INFO_SYM.common) {
272         ALOGE("Invalid module. Trying to open %p, expect %p",
273             module, &HAL_MODULE_INFO_SYM.common);
274         return INVALID_OPERATION;
275     }
276     if (!id) {
277         ALOGE("Invalid camera id");
278         return BAD_VALUE;
279     }
280     return gQCamera3Factory->cameraDeviceOpen(atoi(id), hw_device);
281 }
282 
283 struct hw_module_methods_t QCamera3Factory::mModuleMethods = {
284     open: QCamera3Factory::camera_device_open,
285 };
286 
287 }; // namespace qcamera
288 
289