1 /* Copyright (c) 2012-2013, 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 
32 #include <stdlib.h>
33 #include <utils/Log.h>
34 #include <utils/Errors.h>
35 #include <hardware/camera3.h>
36 
37 #include "QCamera3Factory.h"
38 
39 using namespace android;
40 
41 namespace qcamera {
42 
43 QCamera3Factory gQCamera3Factory;
44 
45 /*===========================================================================
46  * FUNCTION   : QCamera3Factory
47  *
48  * DESCRIPTION: default constructor of QCamera3Factory
49  *
50  * PARAMETERS : none
51  *
52  * RETURN     : None
53  *==========================================================================*/
QCamera3Factory()54 QCamera3Factory::QCamera3Factory()
55 {
56     mNumOfCameras = get_num_of_cameras();
57 }
58 
59 /*===========================================================================
60  * FUNCTION   : ~QCamera3Factory
61  *
62  * DESCRIPTION: deconstructor of QCamera2Factory
63  *
64  * PARAMETERS : none
65  *
66  * RETURN     : None
67  *==========================================================================*/
~QCamera3Factory()68 QCamera3Factory::~QCamera3Factory()
69 {
70 }
71 
72 /*===========================================================================
73  * FUNCTION   : get_number_of_cameras
74  *
75  * DESCRIPTION: static function to query number of cameras detected
76  *
77  * PARAMETERS : none
78  *
79  * RETURN     : number of cameras detected
80  *==========================================================================*/
get_number_of_cameras()81 int QCamera3Factory::get_number_of_cameras()
82 {
83     return gQCamera3Factory.getNumberOfCameras();
84 }
85 
86 /*===========================================================================
87  * FUNCTION   : get_camera_info
88  *
89  * DESCRIPTION: static function to query camera information with its ID
90  *
91  * PARAMETERS :
92  *   @camera_id : camera ID
93  *   @info      : ptr to camera info struct
94  *
95  * RETURN     : int32_t type of status
96  *              NO_ERROR  -- success
97  *              none-zero failure code
98  *==========================================================================*/
get_camera_info(int camera_id,struct camera_info * info)99 int QCamera3Factory::get_camera_info(int camera_id, struct camera_info *info)
100 {
101     return gQCamera3Factory.getCameraInfo(camera_id, info);
102 }
103 
104 /*===========================================================================
105  * FUNCTION   : getNumberOfCameras
106  *
107  * DESCRIPTION: query number of cameras detected
108  *
109  * PARAMETERS : none
110  *
111  * RETURN     : number of cameras detected
112  *==========================================================================*/
getNumberOfCameras()113 int QCamera3Factory::getNumberOfCameras()
114 {
115     return mNumOfCameras;
116 }
117 
118 /*===========================================================================
119  * FUNCTION   : getCameraInfo
120  *
121  * DESCRIPTION: query camera information with its ID
122  *
123  * PARAMETERS :
124  *   @camera_id : camera ID
125  *   @info      : ptr to camera info struct
126  *
127  * RETURN     : int32_t type of status
128  *              NO_ERROR  -- success
129  *              none-zero failure code
130  *==========================================================================*/
getCameraInfo(int camera_id,struct camera_info * info)131 int QCamera3Factory::getCameraInfo(int camera_id, struct camera_info *info)
132 {
133     int rc;
134     ALOGV("%s: E, camera_id = %d", __func__, camera_id);
135 
136     if (!mNumOfCameras || camera_id >= mNumOfCameras || !info ||
137         (camera_id < 0)) {
138         return -ENODEV;
139     }
140 
141     rc = QCamera3HardwareInterface::getCamInfo(camera_id, info);
142     ALOGV("%s: X", __func__);
143     return rc;
144 }
145 
146 /*===========================================================================
147  * FUNCTION   : cameraDeviceOpen
148  *
149  * DESCRIPTION: open a camera device with its ID
150  *
151  * PARAMETERS :
152  *   @camera_id : camera ID
153  *   @hw_device : ptr to struct storing camera hardware device info
154  *
155  * RETURN     : int32_t type of status
156  *              NO_ERROR  -- success
157  *              none-zero failure code
158  *==========================================================================*/
cameraDeviceOpen(int camera_id,struct hw_device_t ** hw_device)159 int QCamera3Factory::cameraDeviceOpen(int camera_id,
160                     struct hw_device_t **hw_device)
161 {
162     int rc = NO_ERROR;
163     if (camera_id < 0 || camera_id >= mNumOfCameras)
164         return -ENODEV;
165 
166     QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(camera_id);
167     if (!hw) {
168         ALOGE("Allocation of hardware interface failed");
169         return NO_MEMORY;
170     }
171     rc = hw->openCamera(hw_device);
172     if (rc != 0) {
173         delete hw;
174     }
175     return rc;
176 }
177 
178 /*===========================================================================
179  * FUNCTION   : camera_device_open
180  *
181  * DESCRIPTION: static function to open a camera device by its ID
182  *
183  * PARAMETERS :
184  *   @camera_id : camera ID
185  *   @hw_device : ptr to struct storing camera hardware device info
186  *
187  * RETURN     : int32_t type of status
188  *              NO_ERROR  -- success
189  *              none-zero failure code
190  *==========================================================================*/
camera_device_open(const struct hw_module_t * module,const char * id,struct hw_device_t ** hw_device)191 int QCamera3Factory::camera_device_open(
192     const struct hw_module_t *module, const char *id,
193     struct hw_device_t **hw_device)
194 {
195     if (module != &HAL_MODULE_INFO_SYM.common) {
196         ALOGE("Invalid module. Trying to open %p, expect %p",
197             module, &HAL_MODULE_INFO_SYM.common);
198         return INVALID_OPERATION;
199     }
200     if (!id) {
201         ALOGE("Invalid camera id");
202         return BAD_VALUE;
203     }
204     return gQCamera3Factory.cameraDeviceOpen(atoi(id), hw_device);
205 }
206 
207 struct hw_module_methods_t QCamera3Factory::mModuleMethods = {
208     open: QCamera3Factory::camera_device_open,
209 };
210 
211 }; // namespace qcamera
212 
213