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 <utils/RefBase.h>
21 #include "EmulatedBaseCamera.h"
22 #include "QemuClient.h"
23 
24 namespace android {
25 
26 struct EmulatedCameraHotplugThread;
27 
28 /*
29  * Contains declaration of a class EmulatedCameraFactory that manages cameras
30  * available for the emulation. A global instance of this class is statically
31  * instantiated and initialized when camera emulation HAL is loaded.
32  */
33 
34 /* Class EmulatedCameraFactoryManages cameras available for the emulation.
35  *
36  * When the global static instance of this class is created on the module load,
37  * it enumerates cameras available for the emulation by connecting to the
38  * emulator's 'camera' service. For every camera found out there it creates an
39  * instance of an appropriate class, and stores it an in array of emulated
40  * cameras. In addition to the cameras reported by the emulator, a fake camera
41  * emulator is always created, so there is always at least one camera that is
42  * available.
43  *
44  * Instance of this class is also used as the entry point for the camera HAL API,
45  * including:
46  *  - hw_module_methods_t::open entry point
47  *  - camera_module_t::get_number_of_cameras entry point
48  *  - camera_module_t::get_camera_info entry point
49  *
50  */
51 class EmulatedCameraFactory {
52 public:
53     /* Constructs EmulatedCameraFactory instance.
54      * In this constructor the factory will create and initialize a list of
55      * emulated cameras. All errors that occur on this constructor are reported
56      * via mConstructedOK data member of this class.
57      */
58     EmulatedCameraFactory();
59 
60     /* Destructs EmulatedCameraFactory instance. */
61     ~EmulatedCameraFactory();
62 
63     /****************************************************************************
64      * Camera HAL API handlers.
65      ***************************************************************************/
66 
67 public:
68     /* Opens (connects to) a camera device.
69      * This method is called in response to hw_module_methods_t::open callback.
70      */
71     int cameraDeviceOpen(int camera_id, hw_device_t** device);
72 
73     /* Gets emulated camera information.
74      * This method is called in response to camera_module_t::get_camera_info callback.
75      */
76     int getCameraInfo(int camera_id, struct camera_info *info);
77 
78     /* Sets emulated camera callbacks.
79      * This method is called in response to camera_module_t::set_callbacks callback.
80      */
81     int setCallbacks(const camera_module_callbacks_t *callbacks);
82 
83     /* Fill in vendor tags for the module
84      * This method is called in response to camera_module_t::get_vendor_tag_ops callback.
85      */
86     void getVendorTagOps(vendor_tag_ops_t* ops);
87 
88     /****************************************************************************
89      * Camera HAL API callbacks.
90      ***************************************************************************/
91 
92 public:
93     /* camera_module_t::get_number_of_cameras callback entry point. */
94     static int get_number_of_cameras(void);
95 
96     /* camera_module_t::get_camera_info callback entry point. */
97     static int get_camera_info(int camera_id, struct camera_info *info);
98 
99     /* camera_module_t::set_callbacks callback entry point. */
100     static int set_callbacks(const camera_module_callbacks_t *callbacks);
101 
102     /* camera_module_t::get_vendor_tag_ops callback entry point */
103     static void get_vendor_tag_ops(vendor_tag_ops_t* ops);
104 
105     /* camera_module_t::open_legacy callback entry point */
106     static int open_legacy(const struct hw_module_t* module, const char* id,
107             uint32_t halVersion, struct hw_device_t** device);
108 
109 private:
110     /* hw_module_methods_t::open callback entry point. */
111     static int device_open(const hw_module_t* module,
112                            const char* name,
113                            hw_device_t** device);
114 
115     /****************************************************************************
116      * Public API.
117      ***************************************************************************/
118 
119 public:
120 
121     /* Gets fake camera orientation. */
getFakeCameraOrientation()122     int getFakeCameraOrientation() {
123         /* TODO: Have a boot property that controls that. */
124         return 90;
125     }
126 
127     /* Gets qemu camera orientation. */
getQemuCameraOrientation()128     int getQemuCameraOrientation() {
129         /* TODO: Have a boot property that controls that. */
130         return 90;
131     }
132 
133     /* Gets number of emulated cameras.
134      */
getEmulatedCameraNum()135     int getEmulatedCameraNum() const {
136         return mEmulatedCameraNum;
137     }
138 
139     /* Checks whether or not the constructor has succeeded.
140      */
isConstructedOK()141     bool isConstructedOK() const {
142         return mConstructedOK;
143     }
144 
145     void onStatusChanged(int cameraId, int newStatus);
146 
147     /****************************************************************************
148      * Private API
149      ***************************************************************************/
150 
151 private:
152     /* Populates emulated cameras array with cameras that are available via
153      * 'camera' service in the emulator. For each such camera and instance of
154      * the EmulatedCameraQemud will be created and added to the mEmulatedCameras
155      * array.
156      */
157     void createQemuCameras();
158 
159     /* Waits till qemu-props has done setup, timeout after 500ms */
160     void waitForQemuSfFakeCameraPropertyAvailable();
161 
162     /* Checks if fake camera emulation is on for the camera facing back. */
163     bool isBackFakeCameraEmulationOn();
164 
165     /* Gets camera device version number to use for back camera emulation */
166     int getBackCameraHalVersion();
167 
168     /* Checks if fake camera emulation is on for the camera facing front. */
169     bool isFrontFakeCameraEmulationOn();
170 
171     /* Gets camera device version number to use for front camera emulation */
172     int getFrontCameraHalVersion();
173 
174     /****************************************************************************
175      * Data members.
176      ***************************************************************************/
177 
178 private:
179     /* Connection to the camera service in the emulator. */
180     FactoryQemuClient   mQemuClient;
181 
182     /* Array of cameras available for the emulation. */
183     EmulatedBaseCamera**    mEmulatedCameras;
184 
185     /* Number of emulated cameras (including the fake ones). */
186     int                 mEmulatedCameraNum;
187 
188     /* Number of emulated fake cameras. */
189     int                 mFakeCameraNum;
190 
191     /* Flags whether or not constructor has succeeded. */
192     bool                mConstructedOK;
193 
194     /* Camera callbacks (for status changing) */
195     const camera_module_callbacks_t* mCallbacks;
196 
197     /* Hotplug thread (to call onStatusChanged) */
198     sp<EmulatedCameraHotplugThread> mHotplugThread;
199 
200 public:
201     /* Contains device open entry point, as required by HAL API. */
202     static struct hw_module_methods_t   mCameraModuleMethods;
203 };
204 
205 }; /* namespace android */
206 
207 /* References the global EmulatedCameraFactory instance. */
208 extern android::EmulatedCameraFactory   gEmulatedCameraFactory;
209 
210 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H */
211