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