blob: b7c76907deda5a6ecff520065c1e3c1388050118 [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
95 sp<FlashControlBase> mFlashControl;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080096
Chien-Yu Chen3068d732015-02-09 13:29:57 -080097 CameraModule *mCameraModule;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080098 sp<CameraProviderManager> mProviderManager;
99
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800100 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800101 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800102
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800103 // camera id -> if it has a flash unit
104 KeyedVector<String8, bool> mHasFlashlightMap;
105 bool mFlashlightMapInitialized;
106
107 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108};
109
110/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800111 * Flash control for camera provider v2.4 and above.
112 */
113class ProviderFlashControl : public FlashControlBase {
114 public:
115 ProviderFlashControl(sp<CameraProviderManager> providerManager);
116 virtual ~ProviderFlashControl();
117
118 // FlashControlBase
119 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
120 status_t setTorchMode(const String8& cameraId, bool enabled);
121
122 private:
123 sp<CameraProviderManager> mProviderManager;
124
125 Mutex mLock;
126};
127
128/**
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800129 * Flash control for camera module v2.4 and above.
130 */
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800131class ModuleFlashControl : public FlashControlBase {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800132 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 ModuleFlashControl(CameraModule& cameraModule);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800134 virtual ~ModuleFlashControl();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800135
136 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800137 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
138 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800139
140 private:
141 CameraModule *mCameraModule;
142
143 Mutex mLock;
144};
145
146/**
147 * Flash control for camera module <= v2.3 and camera HAL v2-v3
148 */
149class CameraDeviceClientFlashControl : public FlashControlBase {
150 public:
151 CameraDeviceClientFlashControl(CameraModule& cameraModule,
152 const camera_module_callbacks_t& callbacks);
153 virtual ~CameraDeviceClientFlashControl();
154
155 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800156 status_t setTorchMode(const String8& cameraId, bool enabled);
157 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800158
159 private:
160 // connect to a camera device
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800161 status_t connectCameraDevice(const String8& cameraId);
162 // disconnect and free mDevice
163 status_t disconnectCameraDevice();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800164
165 // initialize a surface
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800166 status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width,
167 int32_t height);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800168
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800169 // submit a request to enable the torch mode
170 status_t submitTorchEnabledRequest();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800171
172 // get the smallest surface size of IMPLEMENTATION_DEFINED
173 status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width,
174 int32_t *height);
175
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800176 // protected by mLock
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800177 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800178
179 CameraModule *mCameraModule;
180 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800181 String8 mCameraId;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800182 bool mTorchEnabled;
183 CameraMetadata *mMetadata;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800184 // WORKAROUND: will be set to true for HAL v2 devices where
185 // setStreamingRequest() needs to be call for torch mode settings to
186 // take effect.
187 bool mStreaming;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800188
189 sp<CameraDeviceBase> mDevice;
190
191 sp<IGraphicBufferProducer> mProducer;
192 sp<IGraphicBufferConsumer> mConsumer;
193 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700194 sp<Surface> mSurface;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800195 int32_t mStreamId;
196
197 Mutex mLock;
198};
199
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800200/**
201 * Flash control for camera module <= v2.3 and camera HAL v1
202 */
203class CameraHardwareInterfaceFlashControl : public FlashControlBase {
204 public:
205 CameraHardwareInterfaceFlashControl(CameraModule& cameraModule,
206 const camera_module_callbacks_t& callbacks);
207 virtual ~CameraHardwareInterfaceFlashControl();
208
209 // FlashControlBase
210 status_t setTorchMode(const String8& cameraId, bool enabled);
211 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
212
213 private:
214 // connect to a camera device
215 status_t connectCameraDevice(const String8& cameraId);
216
217 // disconnect and free mDevice
218 status_t disconnectCameraDevice();
219
220 // initialize the preview window
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700221 status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800222 int32_t width, int32_t height);
223
224 // start preview and enable torch
225 status_t startPreviewAndTorch();
226
227 // get the smallest surface
228 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height);
229
230 // protected by mLock
Chien-Yu Chen6dcc7062016-04-18 11:14:48 -0700231 // If this function opens camera device in order to check if it has a flash unit, the
232 // camera device will remain open if keepDeviceOpen is true and the camera device will be
233 // closed if keepDeviceOpen is false. If camera device is already open when calling this
234 // function, keepDeviceOpen is ignored.
235 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen);
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800236
237 CameraModule *mCameraModule;
238 const camera_module_callbacks_t *mCallbacks;
239 sp<CameraHardwareInterface> mDevice;
240 String8 mCameraId;
241 CameraParameters mParameters;
242 bool mTorchEnabled;
243
244 sp<IGraphicBufferProducer> mProducer;
245 sp<IGraphicBufferConsumer> mConsumer;
246 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700247 sp<Surface> mSurface;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800248
249 Mutex mLock;
250};
251
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800252} // namespace android
253
254#endif