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 <string> 21 #include <gui/GLConsumer.h> 22 #include <gui/Surface.h> 23 #include <utils/KeyedVector.h> 24 #include <utils/SortedVector.h> 25 #include "common/CameraProviderManager.h" 26 #include "common/CameraDeviceBase.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 std::string& cameraId, 43 bool *hasFlash) = 0; 44 45 // set the torch mode to on or off. 46 virtual status_t setTorchMode(const std::string& cameraId, 47 bool enabled) = 0; 48 49 // Change the brightness level of the torch. If the torch is OFF and 50 // torchStrength >= 1, then the torch will also be turned ON. 51 virtual status_t turnOnTorchWithStrengthLevel(const std::string& cameraId, 52 int32_t torchStrength) = 0; 53 54 // Returns the torch strength level. 55 virtual status_t getTorchStrengthLevel(const std::string& cameraId, 56 int32_t* torchStrength) = 0; 57 }; 58 59 /** 60 * CameraFlashlight can be used by camera service to control flashflight. 61 */ 62 class CameraFlashlight : public virtual VirtualLightRefBase { 63 public: 64 CameraFlashlight(sp<CameraProviderManager> providerManager, 65 CameraProviderManager::StatusListener* callbacks); 66 virtual ~CameraFlashlight(); 67 68 // Find all flash units. This must be called before other methods. All 69 // camera devices must be closed when it's called because HAL v1 devices 70 // need to be opened to query available flash modes. 71 status_t findFlashUnits(); 72 73 // Whether a camera device has a flash unit. Before findFlashUnits() is 74 // called, this function always returns false. 75 bool hasFlashUnit(const std::string& cameraId); 76 77 // set the torch mode to on or off. 78 status_t setTorchMode(const std::string& cameraId, bool enabled); 79 80 // Change the torch strength level of the flash unit in torch mode. 81 status_t turnOnTorchWithStrengthLevel(const std::string& cameraId, int32_t torchStrength); 82 83 // Get the torch strength level 84 status_t getTorchStrengthLevel(const std::string& cameraId, int32_t* torchStrength); 85 86 // Notify CameraFlashlight that camera service is going to open a camera 87 // device. CameraFlashlight will free the resources that may cause the 88 // camera open to fail. Camera service must call this function before 89 // opening a camera device. 90 status_t prepareDeviceOpen(const std::string& cameraId); 91 92 // Notify CameraFlashlight that camera service has closed a camera 93 // device. CameraFlashlight may invoke callbacks for torch mode 94 // available depending on the implementation. 95 status_t deviceClosed(const std::string& cameraId); 96 97 private: 98 // create flashlight control based on camera module API and camera 99 // device API versions. 100 status_t createFlashlightControl(const std::string& cameraId); 101 102 // mLock should be locked. 103 bool hasFlashUnitLocked(const std::string& cameraId); 104 105 // Check if flash control is in backward compatible mode (simulated torch API by 106 // opening cameras) 107 bool isBackwardCompatibleMode(const std::string& cameraId); 108 109 sp<FlashControlBase> mFlashControl; 110 111 sp<CameraProviderManager> mProviderManager; 112 113 CameraProviderManager::StatusListener* mCallbacks; 114 SortedVector<std::string> mOpenedCameraIds; 115 116 // camera id -> if it has a flash unit 117 KeyedVector<std::string, bool> mHasFlashlightMap; 118 bool mFlashlightMapInitialized; 119 120 Mutex mLock; // protect CameraFlashlight API 121 }; 122 123 /** 124 * Flash control for camera provider v2.4 and above. 125 */ 126 class ProviderFlashControl : public FlashControlBase { 127 public: 128 ProviderFlashControl(sp<CameraProviderManager> providerManager); 129 virtual ~ProviderFlashControl(); 130 131 // FlashControlBase 132 status_t hasFlashUnit(const std::string& cameraId, bool *hasFlash); 133 status_t setTorchMode(const std::string& cameraId, bool enabled); 134 status_t turnOnTorchWithStrengthLevel(const std::string& cameraId, int32_t torchStrength); 135 status_t getTorchStrengthLevel(const std::string& cameraId, int32_t* torchStrength); 136 137 private: 138 sp<CameraProviderManager> mProviderManager; 139 140 Mutex mLock; 141 }; 142 143 } // namespace android 144 145 #endif 146