1 /* 2 * Copyright (C) 2015 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 ANDROID_SERVERS_CAMERA_CAMERAFLASHLIGHT_H 18 #define ANDROID_SERVERS_CAMERA_CAMERAFLASHLIGHT_H 19 20 #include "hardware/camera_common.h" 21 #include "utils/KeyedVector.h" 22 #include "utils/SortedVector.h" 23 #include "gui/GLConsumer.h" 24 #include "gui/Surface.h" 25 #include "common/CameraDeviceBase.h" 26 #include "device1/CameraHardwareInterface.h" 27 28 namespace android { 29 30 /** 31 * FlashControlBase is a base class for flash control. It defines the functions 32 * that a flash control for each camera module/device version should implement. 33 */ 34 class FlashControlBase : public virtual VirtualLightRefBase { 35 public: 36 virtual ~FlashControlBase(); 37 38 // Whether a camera device has a flash unit. Calling this function may 39 // cause the torch mode to be turned off in HAL v1 devices. If 40 // previously-on torch mode is turned off, 41 // callbacks.torch_mode_status_change() should be invoked. 42 virtual status_t hasFlashUnit(const String8& cameraId, 43 bool *hasFlash) = 0; 44 45 // set the torch mode to on or off. 46 virtual status_t setTorchMode(const String8& cameraId, 47 bool enabled) = 0; 48 }; 49 50 /** 51 * CameraFlashlight can be used by camera service to control flashflight. 52 */ 53 class CameraFlashlight : public virtual VirtualLightRefBase { 54 public: 55 CameraFlashlight(CameraModule& cameraModule, 56 const camera_module_callbacks_t& callbacks); 57 virtual ~CameraFlashlight(); 58 59 // Find all flash units. This must be called before other methods. All 60 // camera devices must be closed when it's called because HAL v1 devices 61 // need to be opened to query available flash modes. 62 status_t findFlashUnits(); 63 64 // Whether a camera device has a flash unit. Before findFlashUnits() is 65 // called, this function always returns false. 66 bool hasFlashUnit(const String8& cameraId); 67 68 // set the torch mode to on or off. 69 status_t setTorchMode(const String8& cameraId, bool enabled); 70 71 // Notify CameraFlashlight that camera service is going to open a camera 72 // device. CameraFlashlight will free the resources that may cause the 73 // camera open to fail. Camera service must call this function before 74 // opening a camera device. 75 status_t prepareDeviceOpen(const String8& cameraId); 76 77 // Notify CameraFlashlight that camera service has closed a camera 78 // device. CameraFlashlight may invoke callbacks for torch mode 79 // available depending on the implementation. 80 status_t deviceClosed(const String8& cameraId); 81 82 private: 83 // create flashlight control based on camera module API and camera 84 // device API versions. 85 status_t createFlashlightControl(const String8& cameraId); 86 87 // mLock should be locked. 88 bool hasFlashUnitLocked(const String8& cameraId); 89 90 sp<FlashControlBase> mFlashControl; 91 CameraModule *mCameraModule; 92 const camera_module_callbacks_t *mCallbacks; 93 SortedVector<String8> mOpenedCameraIds; 94 95 // camera id -> if it has a flash unit 96 KeyedVector<String8, bool> mHasFlashlightMap; 97 bool mFlashlightMapInitialized; 98 99 Mutex mLock; // protect CameraFlashlight API 100 }; 101 102 /** 103 * Flash control for camera module v2.4 and above. 104 */ 105 class ModuleFlashControl : public FlashControlBase { 106 public: 107 ModuleFlashControl(CameraModule& cameraModule, 108 const camera_module_callbacks_t& callbacks); 109 virtual ~ModuleFlashControl(); 110 111 // FlashControlBase 112 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); 113 status_t setTorchMode(const String8& cameraId, bool enabled); 114 115 private: 116 CameraModule *mCameraModule; 117 118 Mutex mLock; 119 }; 120 121 /** 122 * Flash control for camera module <= v2.3 and camera HAL v2-v3 123 */ 124 class CameraDeviceClientFlashControl : public FlashControlBase { 125 public: 126 CameraDeviceClientFlashControl(CameraModule& cameraModule, 127 const camera_module_callbacks_t& callbacks); 128 virtual ~CameraDeviceClientFlashControl(); 129 130 // FlashControlBase 131 status_t setTorchMode(const String8& cameraId, bool enabled); 132 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); 133 134 private: 135 // connect to a camera device 136 status_t connectCameraDevice(const String8& cameraId); 137 // disconnect and free mDevice 138 status_t disconnectCameraDevice(); 139 140 // initialize a surface 141 status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width, 142 int32_t height); 143 144 // submit a request to enable the torch mode 145 status_t submitTorchEnabledRequest(); 146 147 // get the smallest surface size of IMPLEMENTATION_DEFINED 148 status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width, 149 int32_t *height); 150 151 // protected by mLock 152 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash); 153 154 CameraModule *mCameraModule; 155 const camera_module_callbacks_t *mCallbacks; 156 String8 mCameraId; 157 bool mTorchEnabled; 158 CameraMetadata *mMetadata; 159 // WORKAROUND: will be set to true for HAL v2 devices where 160 // setStreamingRequest() needs to be call for torch mode settings to 161 // take effect. 162 bool mStreaming; 163 164 sp<CameraDeviceBase> mDevice; 165 166 sp<IGraphicBufferProducer> mProducer; 167 sp<IGraphicBufferConsumer> mConsumer; 168 sp<GLConsumer> mSurfaceTexture; 169 sp<Surface> mSurface; 170 int32_t mStreamId; 171 172 Mutex mLock; 173 }; 174 175 /** 176 * Flash control for camera module <= v2.3 and camera HAL v1 177 */ 178 class CameraHardwareInterfaceFlashControl : public FlashControlBase { 179 public: 180 CameraHardwareInterfaceFlashControl(CameraModule& cameraModule, 181 const camera_module_callbacks_t& callbacks); 182 virtual ~CameraHardwareInterfaceFlashControl(); 183 184 // FlashControlBase 185 status_t setTorchMode(const String8& cameraId, bool enabled); 186 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); 187 188 private: 189 // connect to a camera device 190 status_t connectCameraDevice(const String8& cameraId); 191 192 // disconnect and free mDevice 193 status_t disconnectCameraDevice(); 194 195 // initialize the preview window 196 status_t initializePreviewWindow(sp<CameraHardwareInterface> device, 197 int32_t width, int32_t height); 198 199 // start preview and enable torch 200 status_t startPreviewAndTorch(); 201 202 // get the smallest surface 203 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height); 204 205 // protected by mLock 206 // If this function opens camera device in order to check if it has a flash unit, the 207 // camera device will remain open if keepDeviceOpen is true and the camera device will be 208 // closed if keepDeviceOpen is false. If camera device is already open when calling this 209 // function, keepDeviceOpen is ignored. 210 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen); 211 212 CameraModule *mCameraModule; 213 const camera_module_callbacks_t *mCallbacks; 214 sp<CameraHardwareInterface> mDevice; 215 String8 mCameraId; 216 CameraParameters mParameters; 217 bool mTorchEnabled; 218 219 sp<IGraphicBufferProducer> mProducer; 220 sp<IGraphicBufferConsumer> mConsumer; 221 sp<GLConsumer> mSurfaceTexture; 222 sp<Surface> mSurface; 223 224 Mutex mLock; 225 }; 226 227 } // namespace android 228 229 #endif 230