blob: 30f01f052eebfc7c1b3fd853e538621c30fa7087 [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
20#include "hardware/camera_common.h"
21#include "utils/KeyedVector.h"
Chien-Yu Chen88da5262015-02-17 13:56:46 -080022#include "utils/SortedVector.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080023#include "gui/GLConsumer.h"
24#include "gui/Surface.h"
25#include "common/CameraDeviceBase.h"
Chien-Yu Chend231fd62015-02-25 16:04:22 -080026#include "device1/CameraHardwareInterface.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080027
28namespace 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 */
34class 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.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080042 virtual status_t hasFlashUnit(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080043 bool *hasFlash) = 0;
44
45 // set the torch mode to on or off.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080046 virtual status_t setTorchMode(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080047 bool enabled) = 0;
48};
49
50/**
51 * CameraFlashlight can be used by camera service to control flashflight.
52 */
53class CameraFlashlight : public virtual VirtualLightRefBase {
54 public:
55 CameraFlashlight(CameraModule& cameraModule,
56 const camera_module_callbacks_t& callbacks);
57 virtual ~CameraFlashlight();
58
Chien-Yu Chen88da5262015-02-17 13:56:46 -080059 // Find all flash units. This must be called before other methods. All
60 // camera devices must be closed when it's called because HAL v1 devices
61 // need to be opened to query available flash modes.
62 status_t findFlashUnits();
Chien-Yu Chen3068d732015-02-09 13:29:57 -080063
Chien-Yu Chen88da5262015-02-17 13:56:46 -080064 // Whether a camera device has a flash unit. Before findFlashUnits() is
65 // called, this function always returns false.
66 bool hasFlashUnit(const String8& cameraId);
67
68 // set the torch mode to on or off.
69 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080070
71 // Notify CameraFlashlight that camera service is going to open a camera
72 // device. CameraFlashlight will free the resources that may cause the
73 // camera open to fail. Camera service must call this function before
74 // opening a camera device.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080075 status_t prepareDeviceOpen(const String8& cameraId);
76
77 // Notify CameraFlashlight that camera service has closed a camera
78 // device. CameraFlashlight may invoke callbacks for torch mode
79 // available depending on the implementation.
80 status_t deviceClosed(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080081
82 private:
83 // create flashlight control based on camera module API and camera
84 // device API versions.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080085 status_t createFlashlightControl(const String8& cameraId);
86
87 // mLock should be locked.
88 bool hasFlashUnitLocked(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080089
90 sp<FlashControlBase> mFlashControl;
91 CameraModule *mCameraModule;
92 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -080093 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -080094
Chien-Yu Chen88da5262015-02-17 13:56:46 -080095 // camera id -> if it has a flash unit
96 KeyedVector<String8, bool> mHasFlashlightMap;
97 bool mFlashlightMapInitialized;
98
99 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800100};
101
102/**
103 * Flash control for camera module v2.4 and above.
104 */
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800105class ModuleFlashControl : public FlashControlBase {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800106 public:
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800107 ModuleFlashControl(CameraModule& cameraModule,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108 const camera_module_callbacks_t& callbacks);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800109 virtual ~ModuleFlashControl();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800110
111 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800112 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
113 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800114
115 private:
116 CameraModule *mCameraModule;
117
118 Mutex mLock;
119};
120
121/**
122 * Flash control for camera module <= v2.3 and camera HAL v2-v3
123 */
124class CameraDeviceClientFlashControl : public FlashControlBase {
125 public:
126 CameraDeviceClientFlashControl(CameraModule& cameraModule,
127 const camera_module_callbacks_t& callbacks);
128 virtual ~CameraDeviceClientFlashControl();
129
130 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800131 status_t setTorchMode(const String8& cameraId, bool enabled);
132 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800133
134 private:
135 // connect to a camera device
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800136 status_t connectCameraDevice(const String8& cameraId);
137 // disconnect and free mDevice
138 status_t disconnectCameraDevice();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800139
140 // initialize a surface
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800141 status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width,
142 int32_t height);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800143
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800144 // submit a request to enable the torch mode
145 status_t submitTorchEnabledRequest();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800146
147 // get the smallest surface size of IMPLEMENTATION_DEFINED
148 status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width,
149 int32_t *height);
150
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800151 // protected by mLock
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800152 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800153
154 CameraModule *mCameraModule;
155 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800156 String8 mCameraId;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800157 bool mTorchEnabled;
158 CameraMetadata *mMetadata;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800159 // WORKAROUND: will be set to true for HAL v2 devices where
160 // setStreamingRequest() needs to be call for torch mode settings to
161 // take effect.
162 bool mStreaming;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800163
164 sp<CameraDeviceBase> mDevice;
165
166 sp<IGraphicBufferProducer> mProducer;
167 sp<IGraphicBufferConsumer> mConsumer;
168 sp<GLConsumer> mSurfaceTexture;
169 sp<ANativeWindow> mAnw;
170 int32_t mStreamId;
171
172 Mutex mLock;
173};
174
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800175/**
176 * Flash control for camera module <= v2.3 and camera HAL v1
177 */
178class CameraHardwareInterfaceFlashControl : public FlashControlBase {
179 public:
180 CameraHardwareInterfaceFlashControl(CameraModule& cameraModule,
181 const camera_module_callbacks_t& callbacks);
182 virtual ~CameraHardwareInterfaceFlashControl();
183
184 // FlashControlBase
185 status_t setTorchMode(const String8& cameraId, bool enabled);
186 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
187
188 private:
189 // connect to a camera device
190 status_t connectCameraDevice(const String8& cameraId);
191
192 // disconnect and free mDevice
193 status_t disconnectCameraDevice();
194
195 // initialize the preview window
196 status_t initializePreviewWindow(sp<CameraHardwareInterface> device,
197 int32_t width, int32_t height);
198
199 // start preview and enable torch
200 status_t startPreviewAndTorch();
201
202 // get the smallest surface
203 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height);
204
205 // protected by mLock
206 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);
207
208 CameraModule *mCameraModule;
209 const camera_module_callbacks_t *mCallbacks;
210 sp<CameraHardwareInterface> mDevice;
211 String8 mCameraId;
212 CameraParameters mParameters;
213 bool mTorchEnabled;
214
215 sp<IGraphicBufferProducer> mProducer;
216 sp<IGraphicBufferConsumer> mConsumer;
217 sp<GLConsumer> mSurfaceTexture;
218 sp<ANativeWindow> mAnw;
219
220 Mutex mLock;
221};
222
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800223} // namespace android
224
225#endif