• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H
19 
20 #include "EmulatedBaseCamera.h"
21 #include "QemuClient.h"
22 
23 #include <cutils/properties.h>
24 
25 #include <ui/GraphicBufferMapper.h>
26 #include <utils/RefBase.h>
27 #include <vector>
28 
29 namespace android {
30 
31 class EmulatedCameraHotplugThread;
32 
33 /*
34  * Contains declaration of a class EmulatedCameraFactory that manages cameras
35  * available for the emulation. A global instance of this class is statically
36  * instantiated and initialized when camera emulation HAL is loaded.
37  */
38 
39 /*
40  * Class EmulatedCameraFactoryManages cameras available for the emulation.
41  *
42  * When the global static instance of this class is created on the module load,
43  * it enumerates cameras available for the emulation by connecting to the
44  * emulator's 'camera' service. For every camera found out there it creates an
45  * instance of an appropriate class, and stores it an in array of emulated
46  * cameras. In addition to the cameras reported by the emulator, a fake camera
47  * emulator is always created, so there is always at least one camera that is
48  * available.
49  *
50  * Instance of this class is also used as the entry point for the camera HAL API,
51  * including:
52  *  - hw_module_methods_t::open entry point
53  *  - camera_module_t::get_number_of_cameras entry point
54  *  - camera_module_t::get_camera_info entry point
55  *
56  */
57 class EmulatedCameraFactory {
58 public:
59     /*
60      * Constructs EmulatedCameraFactory instance.
61      * In this constructor the factory will create and initialize a list of
62      * emulated cameras. All errors that occur on this constructor are reported
63      * via mConstructedOK data member of this class.
64      */
65     EmulatedCameraFactory();
66 
67     /*
68      * Destructs EmulatedCameraFactory instance.
69      */
70     ~EmulatedCameraFactory();
71 
72 public:
73     /****************************************************************************
74      * Camera HAL API handlers.
75      ***************************************************************************/
76 
77     /*
78      * Opens (connects to) a camera device.
79      *
80      * This method is called in response to hw_module_methods_t::open callback.
81      */
82     int cameraDeviceOpen(int camera_id, hw_device_t **device);
83 
84     /*
85      * Gets emulated camera information.
86      *
87      * This method is called in response to camera_module_t::get_camera_info
88      * callback.
89      */
90     int getCameraInfo(int camera_id, struct camera_info *info);
91 
92     /*
93      * Sets emulated camera callbacks.
94      *
95      * This method is called in response to camera_module_t::set_callbacks
96      * callback.
97      */
98     int setCallbacks(const camera_module_callbacks_t *callbacks);
99 
100     /*
101      * Fill in vendor tags for the module.
102      *
103      * This method is called in response to camera_module_t::get_vendor_tag_ops
104      * callback.
105      */
106     void getVendorTagOps(vendor_tag_ops_t *ops);
107 
108 public:
109     /****************************************************************************
110      * Camera HAL API callbacks.
111      ***************************************************************************/
112 
113     /*
114      * camera_module_t::get_number_of_cameras callback entry point.
115      */
116     static int get_number_of_cameras(void);
117 
118     /*
119      * camera_module_t::get_camera_info callback entry point.
120      */
121     static int get_camera_info(int camera_id, struct camera_info *info);
122 
123     /*
124      * camera_module_t::set_callbacks callback entry point.
125      */
126     static int set_callbacks(const camera_module_callbacks_t *callbacks);
127 
128     /*
129      * camera_module_t::get_vendor_tag_ops callback entry point.
130      */
131     static void get_vendor_tag_ops(vendor_tag_ops_t *ops);
132 
133     /*
134      * camera_module_t::open_legacy callback entry point.
135      */
136     static int open_legacy(const struct hw_module_t *module, const char *id,
137             uint32_t halVersion, struct hw_device_t **device);
138 
139 private:
140     /*
141      * hw_module_methods_t::open callback entry point.
142      */
143     static int device_open(const hw_module_t *module, const char *name,
144             hw_device_t **device);
145 
146 public:
147     /****************************************************************************
148      * Public API.
149      ***************************************************************************/
150 
151     /*
152      * Gets fake camera orientation.
153      */
getFakeCameraOrientation()154     int getFakeCameraOrientation() {
155         const char *key = "qemu.camera.fake.orientation";
156         int degree = property_get_int32(key, 90);
157         return degree;
158     }
159 
160     /*
161      * Gets qemu camera orientation.
162      */
getQemuCameraOrientation()163     int getQemuCameraOrientation() {
164         const char *key = "qemu.camera.webcam.orientation";
165         int degree = property_get_int32(key, 90);
166         return degree;
167     }
168 
169     /*
170      * Gets number of emulated cameras.
171      */
getEmulatedCameraNum()172     int getEmulatedCameraNum() const {
173         return mEmulatedCameras.size();
174     }
175 
176     /*
177      * Checks whether or not the constructor has succeeded.
178      */
isConstructedOK()179     bool isConstructedOK() const {
180         return mConstructedOK;
181     }
182 
183     void onStatusChanged(int cameraId, int newStatus);
184 
185 private:
186     /****************************************************************************
187      * Private API
188      ***************************************************************************/
189 
190     // For carrying QEMU camera information between methods.
191     struct QemuCameraInfo {
192         char *name;
193         char *frameDims;
194         char *dir;
195     };
196 
197     /*
198      * Args:
199      *     token: token whose value is being searched for.
200      *     s: string containing one or more tokens in the format
201      *        "token_name=token_value".
202      *     value: Output parameter for the value of the token.
203      *
204      * Returns:
205      *     true if the token was successfully parsed.
206      */
207     bool getTokenValue(const char *token, const std::string &s, char **value);
208 
209     /*
210      * Args:
211      *     qemuCameras: Output parameter for the list of detected camera
212      *                  strings. Each camera is represented by a string of three
213      *                  attributes "name=... framedims=... dir=...", not
214      *                  necessarily in that order.
215      */
216     void findQemuCameras(std::vector<QemuCameraInfo> *qemuCameras);
217 
218     /*
219      * Populates emulated cameras array with cameras that are available via
220      * 'camera' service in the emulator. For each such camera, one of the
221      * EmulatedQemuCamera* classes will be created and added to
222      * mEmulatedCameras (based on the HAL version specified in system
223      * properties).
224      */
225     void createQemuCameras(const std::vector<QemuCameraInfo> &qemuCameras);
226 
227     std::unique_ptr<EmulatedBaseCamera> createQemuCameraImpl(
228         int halVersion,
229         const QemuCameraInfo& cameraInfo,
230         int cameraId,
231         struct hw_module_t* module);
232 
233     /*
234      * Creates a fake camera and adds it to mEmulatedCameras. If backCamera is
235      * true, it will be created as if it were a camera on the back of the phone.
236      * Otherwise, it will be front-facing.
237      */
238     void createFakeCamera(bool backCamera);
239 
240     std::unique_ptr<EmulatedBaseCamera> createFakeCameraImpl(
241         bool backCamera,
242         int halVersion,
243         int cameraId,
244         struct hw_module_t* module);
245 
246     /*
247      * Waits till qemu-props has done setup, timeout after 500ms.
248      */
249     void waitForQemuSfFakeCameraPropertyAvailable();
250 
251     /*
252      * Checks if fake camera emulation is on for the camera facing back.
253      */
254     bool isFakeCameraEmulationOn(bool backCamera);
255 
256     /*
257      * Gets camera device version number to use for back camera emulation.
258      */
259     int getCameraHalVersion(bool backCamera);
260 
261 
262 private:
263     /****************************************************************************
264      * Data members.
265      ***************************************************************************/
266 
267     // Connection to the camera service in the emulator.
268     FactoryQemuClient mQemuClient;
269 
270     // Array of cameras available for the emulation.
271     std::vector<std::unique_ptr<EmulatedBaseCamera>> mEmulatedCameras;
272 
273     // Flags whether or not constructor has succeeded.
274     bool mConstructedOK;
275 
276     GraphicBufferMapper *mGBM;
277 
278     // Camera callbacks (for status changing).
279     const camera_module_callbacks_t *mCallbacks;
280 
281     // Hotplug thread (to call onStatusChanged).
282     sp<EmulatedCameraHotplugThread> mHotplugThread;
283 
284 public:
285     // Contains device open entry point, as required by HAL API.
286     static struct hw_module_methods_t mCameraModuleMethods;
287 };
288 
289 }; // end of namespace android
290 
291 // References the global EmulatedCameraFactory instance.
292 extern android::EmulatedCameraFactory gEmulatedCameraFactory;
293 
294 #endif  // HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H
295