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 | |
| 20 | #include "hardware/camera_common.h" |
| 21 | #include "utils/KeyedVector.h" |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 22 | #include "utils/SortedVector.h" |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 23 | #include "gui/GLConsumer.h" |
| 24 | #include "gui/Surface.h" |
| 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 | |
| 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. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 42 | virtual status_t hasFlashUnit(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 43 | bool *hasFlash) = 0; |
| 44 | |
| 45 | // set the torch mode to on or off. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 46 | virtual status_t setTorchMode(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 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 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 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(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 63 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 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); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 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. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 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); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | // create flashlight control based on camera module API and camera |
| 84 | // device API versions. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 85 | status_t createFlashlightControl(const String8& cameraId); |
| 86 | |
| 87 | // mLock should be locked. |
| 88 | bool hasFlashUnitLocked(const String8& cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 89 | |
| 90 | sp<FlashControlBase> mFlashControl; |
| 91 | CameraModule *mCameraModule; |
| 92 | const camera_module_callbacks_t *mCallbacks; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 93 | SortedVector<String8> mOpenedCameraIds; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 94 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 95 | // camera id -> if it has a flash unit |
| 96 | KeyedVector<String8, bool> mHasFlashlightMap; |
| 97 | bool mFlashlightMapInitialized; |
| 98 | |
| 99 | Mutex mLock; // protect CameraFlashlight API |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Flash control for camera module v2.4 and above. |
| 104 | */ |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 105 | class ModuleFlashControl : public FlashControlBase { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 106 | public: |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 107 | ModuleFlashControl(CameraModule& cameraModule, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 108 | const camera_module_callbacks_t& callbacks); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 109 | virtual ~ModuleFlashControl(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 110 | |
| 111 | // FlashControlBase |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 112 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
| 113 | status_t setTorchMode(const String8& cameraId, bool enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 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 |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 131 | status_t setTorchMode(const String8& cameraId, bool enabled); |
| 132 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | // connect to a camera device |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 136 | status_t connectCameraDevice(const String8& cameraId); |
| 137 | // disconnect and free mDevice |
| 138 | status_t disconnectCameraDevice(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 139 | |
| 140 | // initialize a surface |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 141 | status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width, |
| 142 | int32_t height); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 143 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 144 | // submit a request to enable the torch mode |
| 145 | status_t submitTorchEnabledRequest(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 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 | |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame^] | 151 | // protected by mLock |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 152 | status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 153 | |
| 154 | CameraModule *mCameraModule; |
| 155 | const camera_module_callbacks_t *mCallbacks; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 156 | String8 mCameraId; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 157 | bool mTorchEnabled; |
| 158 | CameraMetadata *mMetadata; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 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; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 163 | |
| 164 | sp<CameraDeviceBase> mDevice; |
| 165 | |
| 166 | sp<IGraphicBufferProducer> mProducer; |
| 167 | sp<IGraphicBufferConsumer> mConsumer; |
| 168 | sp<GLConsumer> mSurfaceTexture; |
| 169 | sp<ANativeWindow> mAnw; |
| 170 | int32_t mStreamId; |
| 171 | |
| 172 | Mutex mLock; |
| 173 | }; |
| 174 | |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame^] | 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 | status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash); |
| 207 | |
| 208 | CameraModule *mCameraModule; |
| 209 | const camera_module_callbacks_t *mCallbacks; |
| 210 | sp<CameraHardwareInterface> mDevice; |
| 211 | String8 mCameraId; |
| 212 | CameraParameters mParameters; |
| 213 | bool mTorchEnabled; |
| 214 | |
| 215 | sp<IGraphicBufferProducer> mProducer; |
| 216 | sp<IGraphicBufferConsumer> mConsumer; |
| 217 | sp<GLConsumer> mSurfaceTexture; |
| 218 | sp<ANativeWindow> mAnw; |
| 219 | |
| 220 | Mutex mLock; |
| 221 | }; |
| 222 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 223 | } // namespace android |
| 224 | |
| 225 | #endif |