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