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> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 22 | #include <utils/KeyedVector.h> |
| 23 | #include <utils/SortedVector.h> |
| 24 | #include "common/CameraProviderManager.h" |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 25 | #include "common/CameraDeviceBase.h" |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 26 | #include "device1/CameraHardwareInterface.h" |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 27 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 28 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | /** |
| 32 | * FlashControlBase is a base class for flash control. It defines the functions |
| 33 | * that a flash control for each camera module/device version should implement. |
| 34 | */ |
| 35 | class FlashControlBase : public virtual VirtualLightRefBase { |
| 36 | public: |
| 37 | virtual ~FlashControlBase(); |
| 38 | |
| 39 | // Whether a camera device has a flash unit. Calling this function may |
| 40 | // cause the torch mode to be turned off in HAL v1 devices. If |
| 41 | // previously-on torch mode is turned off, |
| 42 | // callbacks.torch_mode_status_change() should be invoked. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 43 | virtual status_t hasFlashUnit(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 44 | bool *hasFlash) = 0; |
| 45 | |
| 46 | // set the torch mode to on or off. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 47 | virtual status_t setTorchMode(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 48 | bool enabled) = 0; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * CameraFlashlight can be used by camera service to control flashflight. |
| 53 | */ |
| 54 | class CameraFlashlight : public virtual VirtualLightRefBase { |
| 55 | public: |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 56 | CameraFlashlight(sp<CameraProviderManager> providerManager, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame^] | 57 | CameraProviderManager::StatusListener* callbacks); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 58 | virtual ~CameraFlashlight(); |
| 59 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 60 | // Find all flash units. This must be called before other methods. All |
| 61 | // camera devices must be closed when it's called because HAL v1 devices |
| 62 | // need to be opened to query available flash modes. |
| 63 | status_t findFlashUnits(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 64 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 65 | // Whether a camera device has a flash unit. Before findFlashUnits() is |
| 66 | // called, this function always returns false. |
| 67 | bool hasFlashUnit(const String8& cameraId); |
| 68 | |
| 69 | // set the torch mode to on or off. |
| 70 | status_t setTorchMode(const String8& cameraId, bool enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 71 | |
| 72 | // Notify CameraFlashlight that camera service is going to open a camera |
| 73 | // device. CameraFlashlight will free the resources that may cause the |
| 74 | // camera open to fail. Camera service must call this function before |
| 75 | // opening a camera device. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 76 | status_t prepareDeviceOpen(const String8& cameraId); |
| 77 | |
| 78 | // Notify CameraFlashlight that camera service has closed a camera |
| 79 | // device. CameraFlashlight may invoke callbacks for torch mode |
| 80 | // available depending on the implementation. |
| 81 | status_t deviceClosed(const String8& cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | // create flashlight control based on camera module API and camera |
| 85 | // device API versions. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 86 | status_t createFlashlightControl(const String8& cameraId); |
| 87 | |
| 88 | // mLock should be locked. |
| 89 | bool hasFlashUnitLocked(const String8& cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 90 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 91 | // Check if flash control is in backward compatible mode (simulated torch API by |
| 92 | // opening cameras) |
| 93 | bool isBackwardCompatibleMode(const String8& cameraId); |
| 94 | |
| 95 | int getNumberOfCameras(); |
| 96 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 97 | sp<FlashControlBase> mFlashControl; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 98 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 99 | sp<CameraProviderManager> mProviderManager; |
| 100 | |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame^] | 101 | CameraProviderManager::StatusListener* mCallbacks; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 102 | SortedVector<String8> mOpenedCameraIds; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 103 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 104 | // camera id -> if it has a flash unit |
| 105 | KeyedVector<String8, bool> mHasFlashlightMap; |
| 106 | bool mFlashlightMapInitialized; |
| 107 | |
| 108 | Mutex mLock; // protect CameraFlashlight API |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 112 | * Flash control for camera provider v2.4 and above. |
| 113 | */ |
| 114 | class ProviderFlashControl : public FlashControlBase { |
| 115 | public: |
| 116 | ProviderFlashControl(sp<CameraProviderManager> providerManager); |
| 117 | virtual ~ProviderFlashControl(); |
| 118 | |
| 119 | // FlashControlBase |
| 120 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
| 121 | status_t setTorchMode(const String8& cameraId, bool enabled); |
| 122 | |
| 123 | private: |
| 124 | sp<CameraProviderManager> mProviderManager; |
| 125 | |
| 126 | Mutex mLock; |
| 127 | }; |
| 128 | |
| 129 | /** |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 130 | * Flash control for camera module <= v2.3 and camera HAL v1 |
| 131 | */ |
| 132 | class CameraHardwareInterfaceFlashControl : public FlashControlBase { |
| 133 | public: |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 134 | CameraHardwareInterfaceFlashControl( |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 135 | sp<CameraProviderManager> manager, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame^] | 136 | CameraProviderManager::StatusListener* callbacks); |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 137 | virtual ~CameraHardwareInterfaceFlashControl(); |
| 138 | |
| 139 | // FlashControlBase |
| 140 | status_t setTorchMode(const String8& cameraId, bool enabled); |
| 141 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
| 142 | |
| 143 | private: |
| 144 | // connect to a camera device |
| 145 | status_t connectCameraDevice(const String8& cameraId); |
| 146 | |
| 147 | // disconnect and free mDevice |
| 148 | status_t disconnectCameraDevice(); |
| 149 | |
| 150 | // initialize the preview window |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 151 | status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device, |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 152 | int32_t width, int32_t height); |
| 153 | |
| 154 | // start preview and enable torch |
| 155 | status_t startPreviewAndTorch(); |
| 156 | |
| 157 | // get the smallest surface |
| 158 | status_t getSmallestSurfaceSize(int32_t *width, int32_t *height); |
| 159 | |
| 160 | // protected by mLock |
Chien-Yu Chen | 6dcc706 | 2016-04-18 11:14:48 -0700 | [diff] [blame] | 161 | // If this function opens camera device in order to check if it has a flash unit, the |
| 162 | // camera device will remain open if keepDeviceOpen is true and the camera device will be |
| 163 | // closed if keepDeviceOpen is false. If camera device is already open when calling this |
| 164 | // function, keepDeviceOpen is ignored. |
| 165 | status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen); |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 166 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 167 | sp<CameraProviderManager> mProviderManager; |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame^] | 168 | CameraProviderManager::StatusListener* mCallbacks; |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 169 | sp<CameraHardwareInterface> mDevice; |
| 170 | String8 mCameraId; |
| 171 | CameraParameters mParameters; |
| 172 | bool mTorchEnabled; |
| 173 | |
| 174 | sp<IGraphicBufferProducer> mProducer; |
| 175 | sp<IGraphicBufferConsumer> mConsumer; |
| 176 | sp<GLConsumer> mSurfaceTexture; |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 177 | sp<Surface> mSurface; |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 178 | |
| 179 | Mutex mLock; |
| 180 | }; |
| 181 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 182 | } // namespace android |
| 183 | |
| 184 | #endif |