blob: 98f269a6c4bda11042bbc0ec176353d595f99011 [file] [log] [blame]
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001/*
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 Talvala2f09bac2016-12-13 11:29:54 -080020#include <gui/GLConsumer.h>
21#include <gui/Surface.h>
22#include <hardware/camera_common.h>
23#include <utils/KeyedVector.h>
24#include <utils/SortedVector.h>
25#include "common/CameraProviderManager.h"
26#include "common/CameraModule.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080027#include "common/CameraDeviceBase.h"
Chien-Yu Chend231fd62015-02-25 16:04:22 -080028#include "device1/CameraHardwareInterface.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080029
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080030
Chien-Yu Chen3068d732015-02-09 13:29:57 -080031namespace android {
32
33/**
34 * FlashControlBase is a base class for flash control. It defines the functions
35 * that a flash control for each camera module/device version should implement.
36 */
37class FlashControlBase : public virtual VirtualLightRefBase {
38 public:
39 virtual ~FlashControlBase();
40
41 // Whether a camera device has a flash unit. Calling this function may
42 // cause the torch mode to be turned off in HAL v1 devices. If
43 // previously-on torch mode is turned off,
44 // callbacks.torch_mode_status_change() should be invoked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080045 virtual status_t hasFlashUnit(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080046 bool *hasFlash) = 0;
47
48 // set the torch mode to on or off.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080049 virtual status_t setTorchMode(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080050 bool enabled) = 0;
51};
52
53/**
54 * CameraFlashlight can be used by camera service to control flashflight.
55 */
56class CameraFlashlight : public virtual VirtualLightRefBase {
57 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058 CameraFlashlight(CameraModule* cameraModule,
59 camera_module_callbacks_t* callbacks);
60 CameraFlashlight(sp<CameraProviderManager> providerManager,
61 camera_module_callbacks_t* callbacks);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080062 virtual ~CameraFlashlight();
63
Chien-Yu Chen88da5262015-02-17 13:56:46 -080064 // Find all flash units. This must be called before other methods. All
65 // camera devices must be closed when it's called because HAL v1 devices
66 // need to be opened to query available flash modes.
67 status_t findFlashUnits();
Chien-Yu Chen3068d732015-02-09 13:29:57 -080068
Chien-Yu Chen88da5262015-02-17 13:56:46 -080069 // Whether a camera device has a flash unit. Before findFlashUnits() is
70 // called, this function always returns false.
71 bool hasFlashUnit(const String8& cameraId);
72
73 // set the torch mode to on or off.
74 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080075
76 // Notify CameraFlashlight that camera service is going to open a camera
77 // device. CameraFlashlight will free the resources that may cause the
78 // camera open to fail. Camera service must call this function before
79 // opening a camera device.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080080 status_t prepareDeviceOpen(const String8& cameraId);
81
82 // Notify CameraFlashlight that camera service has closed a camera
83 // device. CameraFlashlight may invoke callbacks for torch mode
84 // available depending on the implementation.
85 status_t deviceClosed(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080086
87 private:
88 // create flashlight control based on camera module API and camera
89 // device API versions.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080090 status_t createFlashlightControl(const String8& cameraId);
91
92 // mLock should be locked.
93 bool hasFlashUnitLocked(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080094
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -070095 // Check if flash control is in backward compatible mode (simulated torch API by
96 // opening cameras)
97 bool isBackwardCompatibleMode(const String8& cameraId);
98
99 int getNumberOfCameras();
100
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800101 sp<FlashControlBase> mFlashControl;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800102
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800103 CameraModule *mCameraModule;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800104 sp<CameraProviderManager> mProviderManager;
105
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800106 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800107 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800109 // camera id -> if it has a flash unit
110 KeyedVector<String8, bool> mHasFlashlightMap;
111 bool mFlashlightMapInitialized;
112
113 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800114};
115
116/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800117 * Flash control for camera provider v2.4 and above.
118 */
119class ProviderFlashControl : public FlashControlBase {
120 public:
121 ProviderFlashControl(sp<CameraProviderManager> providerManager);
122 virtual ~ProviderFlashControl();
123
124 // FlashControlBase
125 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
126 status_t setTorchMode(const String8& cameraId, bool enabled);
127
128 private:
129 sp<CameraProviderManager> mProviderManager;
130
131 Mutex mLock;
132};
133
134/**
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800135 * Flash control for camera module v2.4 and above.
136 */
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800137class ModuleFlashControl : public FlashControlBase {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800138 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800139 ModuleFlashControl(CameraModule& cameraModule);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800140 virtual ~ModuleFlashControl();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800141
142 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800143 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
144 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800145
146 private:
147 CameraModule *mCameraModule;
148
149 Mutex mLock;
150};
151
152/**
153 * Flash control for camera module <= v2.3 and camera HAL v2-v3
154 */
155class CameraDeviceClientFlashControl : public FlashControlBase {
156 public:
157 CameraDeviceClientFlashControl(CameraModule& cameraModule,
158 const camera_module_callbacks_t& callbacks);
159 virtual ~CameraDeviceClientFlashControl();
160
161 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800162 status_t setTorchMode(const String8& cameraId, bool enabled);
163 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800164
165 private:
166 // connect to a camera device
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800167 status_t connectCameraDevice(const String8& cameraId);
168 // disconnect and free mDevice
169 status_t disconnectCameraDevice();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800170
171 // initialize a surface
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800172 status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width,
173 int32_t height);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800174
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800175 // submit a request to enable the torch mode
176 status_t submitTorchEnabledRequest();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800177
178 // get the smallest surface size of IMPLEMENTATION_DEFINED
179 status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width,
180 int32_t *height);
181
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800182 // protected by mLock
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800183 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800184
185 CameraModule *mCameraModule;
186 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800187 String8 mCameraId;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800188 bool mTorchEnabled;
189 CameraMetadata *mMetadata;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800190 // WORKAROUND: will be set to true for HAL v2 devices where
191 // setStreamingRequest() needs to be call for torch mode settings to
192 // take effect.
193 bool mStreaming;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800194
195 sp<CameraDeviceBase> mDevice;
196
197 sp<IGraphicBufferProducer> mProducer;
198 sp<IGraphicBufferConsumer> mConsumer;
199 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700200 sp<Surface> mSurface;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800201 int32_t mStreamId;
202
203 Mutex mLock;
204};
205
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800206/**
207 * Flash control for camera module <= v2.3 and camera HAL v1
208 */
209class CameraHardwareInterfaceFlashControl : public FlashControlBase {
210 public:
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700211 CameraHardwareInterfaceFlashControl(
212 CameraModule* cameraModule,
213 const camera_module_callbacks_t& callbacks);
214 CameraHardwareInterfaceFlashControl(
215 sp<CameraProviderManager> manager,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800216 const camera_module_callbacks_t& callbacks);
217 virtual ~CameraHardwareInterfaceFlashControl();
218
219 // FlashControlBase
220 status_t setTorchMode(const String8& cameraId, bool enabled);
221 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
222
223 private:
224 // connect to a camera device
225 status_t connectCameraDevice(const String8& cameraId);
226
227 // disconnect and free mDevice
228 status_t disconnectCameraDevice();
229
230 // initialize the preview window
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700231 status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800232 int32_t width, int32_t height);
233
234 // start preview and enable torch
235 status_t startPreviewAndTorch();
236
237 // get the smallest surface
238 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height);
239
240 // protected by mLock
Chien-Yu Chen6dcc7062016-04-18 11:14:48 -0700241 // If this function opens camera device in order to check if it has a flash unit, the
242 // camera device will remain open if keepDeviceOpen is true and the camera device will be
243 // closed if keepDeviceOpen is false. If camera device is already open when calling this
244 // function, keepDeviceOpen is ignored.
245 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen);
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800246
247 CameraModule *mCameraModule;
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700248 sp<CameraProviderManager> mProviderManager;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800249 const camera_module_callbacks_t *mCallbacks;
250 sp<CameraHardwareInterface> mDevice;
251 String8 mCameraId;
252 CameraParameters mParameters;
253 bool mTorchEnabled;
254
255 sp<IGraphicBufferProducer> mProducer;
256 sp<IGraphicBufferConsumer> mConsumer;
257 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700258 sp<Surface> mSurface;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800259
260 Mutex mLock;
261};
262
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800263} // namespace android
264
265#endif