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