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
40using namespace android;
41
42namespace qcamera {
43
44QCamera3Factory *gQCamera3Factory = NULL;
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    if (!gQCamera3Factory) {
96        gQCamera3Factory = new QCamera3Factory();
97        if (!gQCamera3Factory) {
98            ALOGE("%s: Failed to allocate Camera3Factory object", __func__);
99            return 0;
100        }
101    }
102    return gQCamera3Factory->getNumberOfCameras();
103}
104
105/*===========================================================================
106 * FUNCTION   : get_camera_info
107 *
108 * DESCRIPTION: static function to query camera information with its ID
109 *
110 * PARAMETERS :
111 *   @camera_id : camera ID
112 *   @info      : ptr to camera info struct
113 *
114 * RETURN     : int32_t type of status
115 *              NO_ERROR  -- success
116 *              none-zero failure code
117 *==========================================================================*/
118int QCamera3Factory::get_camera_info(int camera_id, struct camera_info *info)
119{
120    return gQCamera3Factory->getCameraInfo(camera_id, info);
121}
122
123/*===========================================================================
124 * FUNCTION   : getNumberOfCameras
125 *
126 * DESCRIPTION: query number of cameras detected
127 *
128 * PARAMETERS : none
129 *
130 * RETURN     : number of cameras detected
131 *==========================================================================*/
132int QCamera3Factory::getNumberOfCameras()
133{
134    return mNumOfCameras;
135}
136
137/*===========================================================================
138 * FUNCTION   : getCameraInfo
139 *
140 * DESCRIPTION: query camera information with its ID
141 *
142 * PARAMETERS :
143 *   @camera_id : camera ID
144 *   @info      : ptr to camera info struct
145 *
146 * RETURN     : int32_t type of status
147 *              NO_ERROR  -- success
148 *              none-zero failure code
149 *==========================================================================*/
150int QCamera3Factory::getCameraInfo(int camera_id, struct camera_info *info)
151{
152    int rc;
153    ALOGV("%s: E, camera_id = %d", __func__, camera_id);
154
155    if (!mNumOfCameras || camera_id >= mNumOfCameras || !info ||
156        (camera_id < 0)) {
157        return -ENODEV;
158    }
159
160    rc = QCamera3HardwareInterface::getCamInfo(camera_id, info);
161    ALOGV("%s: X", __func__);
162    return rc;
163}
164
165/*===========================================================================
166 * FUNCTION   : cameraDeviceOpen
167 *
168 * DESCRIPTION: open a camera device with its ID
169 *
170 * PARAMETERS :
171 *   @camera_id : camera ID
172 *   @hw_device : ptr to struct storing camera hardware device info
173 *
174 * RETURN     : int32_t type of status
175 *              NO_ERROR  -- success
176 *              none-zero failure code
177 *==========================================================================*/
178int QCamera3Factory::cameraDeviceOpen(int camera_id,
179                    struct hw_device_t **hw_device)
180{
181    int rc = NO_ERROR;
182    if (camera_id < 0 || camera_id >= mNumOfCameras)
183        return -ENODEV;
184
185    QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(camera_id);
186    if (!hw) {
187        ALOGE("Allocation of hardware interface failed");
188        return NO_MEMORY;
189    }
190    rc = hw->openCamera(hw_device);
191    if (rc != 0) {
192        delete hw;
193    }
194    return rc;
195}
196
197/*===========================================================================
198 * FUNCTION   : camera_device_open
199 *
200 * DESCRIPTION: static function to open a camera device by its ID
201 *
202 * PARAMETERS :
203 *   @camera_id : camera ID
204 *   @hw_device : ptr to struct storing camera hardware device info
205 *
206 * RETURN     : int32_t type of status
207 *              NO_ERROR  -- success
208 *              none-zero failure code
209 *==========================================================================*/
210int QCamera3Factory::camera_device_open(
211    const struct hw_module_t *module, const char *id,
212    struct hw_device_t **hw_device)
213{
214    if (module != &HAL_MODULE_INFO_SYM.common) {
215        ALOGE("Invalid module. Trying to open %p, expect %p",
216            module, &HAL_MODULE_INFO_SYM.common);
217        return INVALID_OPERATION;
218    }
219    if (!id) {
220        ALOGE("Invalid camera id");
221        return BAD_VALUE;
222    }
223    return gQCamera3Factory->cameraDeviceOpen(atoi(id), hw_device);
224}
225
226struct hw_module_methods_t QCamera3Factory::mModuleMethods = {
227    open: QCamera3Factory::camera_device_open,
228};
229
230}; // namespace qcamera
231
232