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     /****************************************************************************
84      * Camera HAL API callbacks.
85      ***************************************************************************/
86 
87 public:
88     /* camera_module_t::get_number_of_cameras callback entry point. */
89     static int get_number_of_cameras(void);
90 
91     /* camera_module_t::get_camera_info callback entry point. */
92     static int get_camera_info(int camera_id, struct camera_info *info);
93 
94     /* camera_module_t::set_callbacks callback entry point. */
95     static int set_callbacks(const camera_module_callbacks_t *callbacks);
96 
97 private:
98     /* hw_module_methods_t::open callback entry point. */
99     static int device_open(const hw_module_t* module,
100                            const char* name,
101                            hw_device_t** device);
102 
103     /****************************************************************************
104      * Public API.
105      ***************************************************************************/
106 
107 public:
108 
109     /* Gets fake camera orientation. */
getFakeCameraOrientation()110     int getFakeCameraOrientation() {
111         /* TODO: Have a boot property that controls that. */
112         return 90;
113     }
114 
115     /* Gets qemu camera orientation. */
getQemuCameraOrientation()116     int getQemuCameraOrientation() {
117         /* TODO: Have a boot property that controls that. */
118         return 270;
119     }
120 
121     /* Gets number of emulated cameras.
122      */
getEmulatedCameraNum()123     int getEmulatedCameraNum() const {
124         return mEmulatedCameraNum;
125     }
126 
127     /* Checks whether or not the constructor has succeeded.
128      */
isConstructedOK()129     bool isConstructedOK() const {
130         return mConstructedOK;
131     }
132 
133     void onStatusChanged(int cameraId, int newStatus);
134 
135     /****************************************************************************
136      * Private API
137      ***************************************************************************/
138 
139 private:
140     /* Populates emulated cameras array with cameras that are available via
141      * 'camera' service in the emulator. For each such camera and instance of
142      * the EmulatedCameraQemud will be created and added to the mEmulatedCameras
143      * array.
144      */
145     void createQemuCameras();
146 
147     /* Checks if fake camera emulation is on for the camera facing back. */
148     bool isBackFakeCameraEmulationOn();
149 
150     /* Gets camera device version number to use for back camera emulation */
151     int getBackCameraHalVersion();
152 
153     /* Checks if fake camera emulation is on for the camera facing front. */
154     bool isFrontFakeCameraEmulationOn();
155 
156     /* Gets camera device version number to use for front camera emulation */
157     int getFrontCameraHalVersion();
158 
159     /****************************************************************************
160      * Data members.
161      ***************************************************************************/
162 
163 private:
164     /* Connection to the camera service in the emulator. */
165     FactoryQemuClient   mQemuClient;
166 
167     /* Array of cameras available for the emulation. */
168     EmulatedBaseCamera**    mEmulatedCameras;
169 
170     /* Number of emulated cameras (including the fake ones). */
171     int                 mEmulatedCameraNum;
172 
173     /* Number of emulated fake cameras. */
174     int                 mFakeCameraNum;
175 
176     /* Flags whether or not constructor has succeeded. */
177     bool                mConstructedOK;
178 
179     /* Camera callbacks (for status changing) */
180     const camera_module_callbacks_t* mCallbacks;
181 
182     /* Hotplug thread (to call onStatusChanged) */
183     sp<EmulatedCameraHotplugThread> mHotplugThread;
184 
185 public:
186     /* Contains device open entry point, as required by HAL API. */
187     static struct hw_module_methods_t   mCameraModuleMethods;
188 };
189 
190 }; /* namespace android */
191 
192 /* References the global EmulatedCameraFactory instance. */
193 extern android::EmulatedCameraFactory   gEmulatedCameraFactory;
194 
195 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H */
196