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