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" |
| 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. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 41 | virtual status_t hasFlashUnit(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 42 | bool *hasFlash) = 0; |
| 43 | |
| 44 | // set the torch mode to on or off. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 45 | virtual status_t setTorchMode(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 46 | bool enabled) = 0; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * CameraFlashlight can be used by camera service to control flashflight. |
| 51 | */ |
| 52 | class CameraFlashlight : public virtual VirtualLightRefBase { |
| 53 | public: |
| 54 | CameraFlashlight(CameraModule& cameraModule, |
| 55 | const camera_module_callbacks_t& callbacks); |
| 56 | virtual ~CameraFlashlight(); |
| 57 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 58 | // Find all flash units. This must be called before other methods. All |
| 59 | // camera devices must be closed when it's called because HAL v1 devices |
| 60 | // need to be opened to query available flash modes. |
| 61 | status_t findFlashUnits(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 62 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 63 | // Whether a camera device has a flash unit. Before findFlashUnits() is |
| 64 | // called, this function always returns false. |
| 65 | bool hasFlashUnit(const String8& cameraId); |
| 66 | |
| 67 | // set the torch mode to on or off. |
| 68 | status_t setTorchMode(const String8& cameraId, bool enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 69 | |
| 70 | // Notify CameraFlashlight that camera service is going to open a camera |
| 71 | // device. CameraFlashlight will free the resources that may cause the |
| 72 | // camera open to fail. Camera service must call this function before |
| 73 | // opening a camera device. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 74 | status_t prepareDeviceOpen(const String8& cameraId); |
| 75 | |
| 76 | // Notify CameraFlashlight that camera service has closed a camera |
| 77 | // device. CameraFlashlight may invoke callbacks for torch mode |
| 78 | // available depending on the implementation. |
| 79 | status_t deviceClosed(const String8& cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | // create flashlight control based on camera module API and camera |
| 83 | // device API versions. |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 84 | status_t createFlashlightControl(const String8& cameraId); |
| 85 | |
| 86 | // mLock should be locked. |
| 87 | bool hasFlashUnitLocked(const String8& cameraId); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 88 | |
| 89 | sp<FlashControlBase> mFlashControl; |
| 90 | CameraModule *mCameraModule; |
| 91 | const camera_module_callbacks_t *mCallbacks; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 92 | SortedVector<String8> mOpenedCameraIds; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 93 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 94 | // camera id -> if it has a flash unit |
| 95 | KeyedVector<String8, bool> mHasFlashlightMap; |
| 96 | bool mFlashlightMapInitialized; |
| 97 | |
| 98 | Mutex mLock; // protect CameraFlashlight API |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Flash control for camera module v2.4 and above. |
| 103 | */ |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 104 | class ModuleFlashControl : public FlashControlBase { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 105 | public: |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 106 | ModuleFlashControl(CameraModule& cameraModule, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 107 | const camera_module_callbacks_t& callbacks); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 108 | virtual ~ModuleFlashControl(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 109 | |
| 110 | // FlashControlBase |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 111 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
| 112 | status_t setTorchMode(const String8& cameraId, bool enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | CameraModule *mCameraModule; |
| 116 | |
| 117 | Mutex mLock; |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * Flash control for camera module <= v2.3 and camera HAL v2-v3 |
| 122 | */ |
| 123 | class CameraDeviceClientFlashControl : public FlashControlBase { |
| 124 | public: |
| 125 | CameraDeviceClientFlashControl(CameraModule& cameraModule, |
| 126 | const camera_module_callbacks_t& callbacks); |
| 127 | virtual ~CameraDeviceClientFlashControl(); |
| 128 | |
| 129 | // FlashControlBase |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 130 | status_t setTorchMode(const String8& cameraId, bool enabled); |
| 131 | status_t hasFlashUnit(const String8& cameraId, bool *hasFlash); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | // connect to a camera device |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 135 | status_t connectCameraDevice(const String8& cameraId); |
| 136 | // disconnect and free mDevice |
| 137 | status_t disconnectCameraDevice(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 138 | |
| 139 | // initialize a surface |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 140 | status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width, |
| 141 | int32_t height); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 142 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 143 | // submit a request to enable the torch mode |
| 144 | status_t submitTorchEnabledRequest(); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 145 | |
| 146 | // get the smallest surface size of IMPLEMENTATION_DEFINED |
| 147 | status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width, |
| 148 | int32_t *height); |
| 149 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 150 | status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 151 | |
| 152 | CameraModule *mCameraModule; |
| 153 | const camera_module_callbacks_t *mCallbacks; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 154 | String8 mCameraId; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 155 | bool mTorchEnabled; |
| 156 | CameraMetadata *mMetadata; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame^] | 157 | // WORKAROUND: will be set to true for HAL v2 devices where |
| 158 | // setStreamingRequest() needs to be call for torch mode settings to |
| 159 | // take effect. |
| 160 | bool mStreaming; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 161 | |
| 162 | sp<CameraDeviceBase> mDevice; |
| 163 | |
| 164 | sp<IGraphicBufferProducer> mProducer; |
| 165 | sp<IGraphicBufferConsumer> mConsumer; |
| 166 | sp<GLConsumer> mSurfaceTexture; |
| 167 | sp<ANativeWindow> mAnw; |
| 168 | int32_t mStreamId; |
| 169 | |
| 170 | Mutex mLock; |
| 171 | }; |
| 172 | |
| 173 | } // namespace android |
| 174 | |
| 175 | #endif |