QCamera3Factory.cpp revision a7661be16c776d6695b0488479cbd5de1a6a5d8b
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
39using namespace android;
40
41namespace qcamera {
42
43QCamera3Factory gQCamera3Factory;
44
45/*===========================================================================
46 * FUNCTION   : QCamera3Factory
47 *
48 * DESCRIPTION: default constructor of QCamera3Factory
49 *
50 * PARAMETERS : none
51 *
52 * RETURN     : None
53 *==========================================================================*/
54QCamera3Factory::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 *==========================================================================*/
68QCamera3Factory::~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 *==========================================================================*/
81int 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 *==========================================================================*/
99int 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 *==========================================================================*/
113int 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 *==========================================================================*/
131int 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        return INVALID_OPERATION;
138    }
139
140    rc = QCamera3HardwareInterface::getCamInfo(camera_id, info);
141    ALOGV("%s: X", __func__);
142    return rc;
143}
144
145/*===========================================================================
146 * FUNCTION   : cameraDeviceOpen
147 *
148 * DESCRIPTION: open a camera device with its ID
149 *
150 * PARAMETERS :
151 *   @camera_id : camera ID
152 *   @hw_device : ptr to struct storing camera hardware device info
153 *
154 * RETURN     : int32_t type of status
155 *              NO_ERROR  -- success
156 *              none-zero failure code
157 *==========================================================================*/
158int QCamera3Factory::cameraDeviceOpen(int camera_id,
159                    struct hw_device_t **hw_device)
160{
161    int rc = NO_ERROR;
162    if (camera_id < 0 || camera_id >= mNumOfCameras)
163        return BAD_VALUE;
164
165    QCamera3HardwareInterface *hw = new QCamera3HardwareInterface(camera_id);
166    if (!hw) {
167        ALOGE("Allocation of hardware interface failed");
168        return NO_MEMORY;
169    }
170    rc = hw->openCamera(hw_device);
171    if (rc != 0) {
172        delete hw;
173    }
174    return rc;
175}
176
177/*===========================================================================
178 * FUNCTION   : camera_device_open
179 *
180 * DESCRIPTION: static function to open a camera device by its ID
181 *
182 * PARAMETERS :
183 *   @camera_id : camera ID
184 *   @hw_device : ptr to struct storing camera hardware device info
185 *
186 * RETURN     : int32_t type of status
187 *              NO_ERROR  -- success
188 *              none-zero failure code
189 *==========================================================================*/
190int QCamera3Factory::camera_device_open(
191    const struct hw_module_t *module, const char *id,
192    struct hw_device_t **hw_device)
193{
194    if (module != &HAL_MODULE_INFO_SYM.common) {
195        ALOGE("Invalid module. Trying to open %p, expect %p",
196            module, &HAL_MODULE_INFO_SYM.common);
197        return INVALID_OPERATION;
198    }
199    if (!id) {
200        ALOGE("Invalid camera id");
201        return BAD_VALUE;
202    }
203    return gQCamera3Factory.cameraDeviceOpen(atoi(id), hw_device);
204}
205
206struct hw_module_methods_t QCamera3Factory::mModuleMethods = {
207    open: QCamera3Factory::camera_device_open,
208};
209
210}; // namespace qcamera
211
212