blob: 1baaba27fd57bab027b749fbda8b0d60e9674585 [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>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080022#include <utils/KeyedVector.h>
23#include <utils/SortedVector.h>
24#include "common/CameraProviderManager.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080025#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
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080028
Chien-Yu Chen3068d732015-02-09 13:29:57 -080029namespace android {
30
31/**
32 * FlashControlBase is a base class for flash control. It defines the functions
33 * that a flash control for each camera module/device version should implement.
34 */
35class FlashControlBase : public virtual VirtualLightRefBase {
36 public:
37 virtual ~FlashControlBase();
38
39 // Whether a camera device has a flash unit. Calling this function may
40 // cause the torch mode to be turned off in HAL v1 devices. If
41 // previously-on torch mode is turned off,
42 // callbacks.torch_mode_status_change() should be invoked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080043 virtual status_t hasFlashUnit(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080044 bool *hasFlash) = 0;
45
46 // set the torch mode to on or off.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080047 virtual status_t setTorchMode(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080048 bool enabled) = 0;
49};
50
51/**
52 * CameraFlashlight can be used by camera service to control flashflight.
53 */
54class CameraFlashlight : public virtual VirtualLightRefBase {
55 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080056 CameraFlashlight(sp<CameraProviderManager> providerManager,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080057 CameraProviderManager::StatusListener* callbacks);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080058 virtual ~CameraFlashlight();
59
Chien-Yu Chen88da5262015-02-17 13:56:46 -080060 // Find all flash units. This must be called before other methods. All
61 // camera devices must be closed when it's called because HAL v1 devices
62 // need to be opened to query available flash modes.
63 status_t findFlashUnits();
Chien-Yu Chen3068d732015-02-09 13:29:57 -080064
Chien-Yu Chen88da5262015-02-17 13:56:46 -080065 // Whether a camera device has a flash unit. Before findFlashUnits() is
66 // called, this function always returns false.
67 bool hasFlashUnit(const String8& cameraId);
68
69 // set the torch mode to on or off.
70 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080071
72 // Notify CameraFlashlight that camera service is going to open a camera
73 // device. CameraFlashlight will free the resources that may cause the
74 // camera open to fail. Camera service must call this function before
75 // opening a camera device.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080076 status_t prepareDeviceOpen(const String8& cameraId);
77
78 // Notify CameraFlashlight that camera service has closed a camera
79 // device. CameraFlashlight may invoke callbacks for torch mode
80 // available depending on the implementation.
81 status_t deviceClosed(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080082
83 private:
84 // create flashlight control based on camera module API and camera
85 // device API versions.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080086 status_t createFlashlightControl(const String8& cameraId);
87
88 // mLock should be locked.
89 bool hasFlashUnitLocked(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080090
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -070091 // Check if flash control is in backward compatible mode (simulated torch API by
92 // opening cameras)
93 bool isBackwardCompatibleMode(const String8& cameraId);
94
Chien-Yu Chen3068d732015-02-09 13:29:57 -080095 sp<FlashControlBase> mFlashControl;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080096
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080097 sp<CameraProviderManager> mProviderManager;
98
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080099 CameraProviderManager::StatusListener* mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800100 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800101
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800102 // camera id -> if it has a flash unit
103 KeyedVector<String8, bool> mHasFlashlightMap;
104 bool mFlashlightMapInitialized;
105
106 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800107};
108
109/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800110 * Flash control for camera provider v2.4 and above.
111 */
112class ProviderFlashControl : public FlashControlBase {
113 public:
114 ProviderFlashControl(sp<CameraProviderManager> providerManager);
115 virtual ~ProviderFlashControl();
116
117 // FlashControlBase
118 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
119 status_t setTorchMode(const String8& cameraId, bool enabled);
120
121 private:
122 sp<CameraProviderManager> mProviderManager;
123
124 Mutex mLock;
125};
126
127/**
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800128 * Flash control for camera module <= v2.3 and camera HAL v1
129 */
130class CameraHardwareInterfaceFlashControl : public FlashControlBase {
131 public:
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700132 CameraHardwareInterfaceFlashControl(
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700133 sp<CameraProviderManager> manager,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800134 CameraProviderManager::StatusListener* callbacks);
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800135 virtual ~CameraHardwareInterfaceFlashControl();
136
137 // FlashControlBase
138 status_t setTorchMode(const String8& cameraId, bool enabled);
139 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
140
141 private:
142 // connect to a camera device
143 status_t connectCameraDevice(const String8& cameraId);
144
145 // disconnect and free mDevice
146 status_t disconnectCameraDevice();
147
148 // initialize the preview window
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700149 status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800150 int32_t width, int32_t height);
151
152 // start preview and enable torch
153 status_t startPreviewAndTorch();
154
155 // get the smallest surface
156 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height);
157
158 // protected by mLock
Chien-Yu Chen6dcc7062016-04-18 11:14:48 -0700159 // If this function opens camera device in order to check if it has a flash unit, the
160 // camera device will remain open if keepDeviceOpen is true and the camera device will be
161 // closed if keepDeviceOpen is false. If camera device is already open when calling this
162 // function, keepDeviceOpen is ignored.
163 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen);
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800164
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700165 sp<CameraProviderManager> mProviderManager;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800166 CameraProviderManager::StatusListener* mCallbacks;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800167 sp<CameraHardwareInterface> mDevice;
168 String8 mCameraId;
169 CameraParameters mParameters;
170 bool mTorchEnabled;
171
172 sp<IGraphicBufferProducer> mProducer;
173 sp<IGraphicBufferConsumer> mConsumer;
174 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700175 sp<Surface> mSurface;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800176
177 Mutex mLock;
178};
179
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800180} // namespace android
181
182#endif