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 22 #include <utils/Vector.h> 23 #include "CameraConfiguration.h" 24 #include "EmulatedBaseCamera.h" 25 #include "common/libs/threads/cuttlefish_thread.h" 26 #include "guest/libs/platform_support/api_level_fixes.h" 27 28 namespace android { 29 30 class EmulatedCameraHotplugThread; 31 32 /* 33 * Contains declaration of a class EmulatedCameraFactory that manages cameras 34 * available for the emulation. A global instance of this class is statically 35 * instantiated and initialized when camera emulation HAL is loaded. 36 */ 37 38 /* Class EmulatedCameraFactoryManages cameras available for the emulation. 39 * 40 * When the global static instance of this class is created on the module load, 41 * it enumerates cameras available for the emulation by connecting to the 42 * emulator's 'camera' service. For every camera found out there it creates an 43 * instance of an appropriate class, and stores it an in array of emulated 44 * cameras. In addition to the cameras reported by the emulator, a fake camera 45 * emulator is always created, so there is always at least one camera that is 46 * available. 47 * 48 * Instance of this class is also used as the entry point for the camera HAL 49 * API, including: 50 * - hw_module_methods_t::open entry point 51 * - camera_module_t::get_number_of_cameras entry point 52 * - camera_module_t::get_camera_info entry point 53 * 54 */ 55 class EmulatedCameraFactory { 56 public: 57 /* Constructs EmulatedCameraFactory instance. 58 * In this constructor the factory will create and initialize a list of 59 * emulated cameras. All errors that occur on this constructor are reported 60 * via mConstructedOK data member of this class. 61 */ 62 EmulatedCameraFactory(); 63 64 /* Destructs EmulatedCameraFactory instance. */ 65 ~EmulatedCameraFactory(); 66 67 /**************************************************************************** 68 * Camera HAL API handlers. 69 ***************************************************************************/ 70 71 public: 72 /* Returns a (singleton) instance of the EmulatedCameraFactory. 73 */ 74 static EmulatedCameraFactory& Instance(); 75 76 /* Opens (connects to) a camera device. 77 * This method is called in response to hw_module_methods_t::open callback. 78 */ 79 int cameraDeviceOpen(int camera_id, hw_device_t** device); 80 81 /* Gets emulated camera information. 82 * This method is called in response to camera_module_t::get_camera_info 83 * callback. 84 */ 85 int getCameraInfo(int camera_id, struct camera_info* info); 86 87 #if VSOC_PLATFORM_SDK_AFTER(J_MR2) 88 /* Sets emulated camera callbacks. 89 * This method is called in response to camera_module_t::set_callbacks 90 * callback. 91 */ 92 int setCallbacks(const camera_module_callbacks_t* callbacks); 93 94 /* Fill in vendor tags for the module 95 * This method is called in response to camera_module_t::get_vendor_tag_ops 96 * callback. 97 */ 98 void getVendorTagOps(vendor_tag_ops_t* ops); 99 #endif 100 101 int setTorchMode(const char* camera_id, bool enabled); 102 103 /**************************************************************************** 104 * Camera HAL API callbacks. 105 ***************************************************************************/ 106 107 public: 108 /* camera_module_t::get_number_of_cameras callback entry point. */ 109 static int get_number_of_cameras(void); 110 111 /* camera_module_t::get_camera_info callback entry point. */ 112 static int get_camera_info(int camera_id, struct camera_info* info); 113 114 #if VSOC_PLATFORM_SDK_AFTER(J_MR2) 115 /* camera_module_t::set_callbacks callback entry point. */ 116 static int set_callbacks(const camera_module_callbacks_t* callbacks); 117 118 /* camera_module_t::get_vendor_tag_ops callback entry point */ 119 static void get_vendor_tag_ops(vendor_tag_ops_t* ops); 120 #endif 121 122 /* camera_module_t::open_legacy callback entry point */ 123 static int open_legacy(const struct hw_module_t* module, const char* id, 124 uint32_t halVersion, struct hw_device_t** device); 125 126 static int set_torch_mode(const char* camera_id, bool enabled); 127 128 private: 129 /* hw_module_methods_t::open callback entry point. */ 130 static int device_open(const hw_module_t* module, const char* name, 131 hw_device_t** device); 132 133 /**************************************************************************** 134 * Public API. 135 ***************************************************************************/ 136 137 public: 138 /* Gets fake camera orientation. */ getFakeCameraOrientation()139 int getFakeCameraOrientation() { 140 /* TODO: Have a boot property that controls that. */ 141 return 90; 142 } 143 144 /* Gets number of emulated cameras. 145 */ getEmulatedCameraNum()146 inline size_t getEmulatedCameraNum() const { 147 return mCameraDefinitions.size(); 148 } 149 150 void onStatusChanged(int cameraId, int newStatus); 151 152 void onTorchModeStatusChanged(int cameraId, int newStatus); 153 154 /**************************************************************************** 155 * Private API 156 ***************************************************************************/ 157 158 private: 159 /* Create new or return existing fake camera based on camera definition 160 * found in mCameraDefinitions. 161 * Returns NULL if cameraId is not valid (= not a valid index of 162 * mCameraDefinitions) 163 */ 164 EmulatedBaseCamera* getOrCreateFakeCamera(size_t cameraId); 165 166 /**************************************************************************** 167 * Data members. 168 ***************************************************************************/ 169 170 private: 171 /* Array of cameras available for the emulation. */ 172 Vector<EmulatedBaseCamera*> mEmulatedCameras; 173 174 /* Guards access to mEmulatedCameras. */ 175 cvd::Mutex mEmulatedCamerasMutex; 176 177 #if VSOC_PLATFORM_SDK_AFTER(J_MR2) 178 /* Camera callbacks (for status changing) */ 179 const camera_module_callbacks_t* mCallbacks; 180 181 /* Hotplug thread (to call onStatusChanged) */ 182 sp<EmulatedCameraHotplugThread> mHotplugThread; 183 #endif 184 185 /* Back- and front camera properties accessed from the vsoc device. */ 186 cvd::CameraConfiguration mCameraConfiguration; 187 Vector<cvd::CameraDefinition> mCameraDefinitions; 188 189 public: 190 /* Contains device open entry point, as required by HAL API. */ 191 static struct hw_module_methods_t mCameraModuleMethods; 192 }; 193 194 }; /* namespace android */ 195 196 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H */ 197