blob: c86ee851af9237ed52d13e6bdf985963fca268bc [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"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080026#include "common/CameraDeviceBase.h"
Chien-Yu Chend231fd62015-02-25 16:04:22 -080027#include "device1/CameraHardwareInterface.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080028
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080029
Chien-Yu Chen3068d732015-02-09 13:29:57 -080030namespace android {
31
32/**
33 * FlashControlBase is a base class for flash control. It defines the functions
34 * that a flash control for each camera module/device version should implement.
35 */
36class FlashControlBase : public virtual VirtualLightRefBase {
37 public:
38 virtual ~FlashControlBase();
39
40 // Whether a camera device has a flash unit. Calling this function may
41 // cause the torch mode to be turned off in HAL v1 devices. If
42 // previously-on torch mode is turned off,
43 // callbacks.torch_mode_status_change() should be invoked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080044 virtual status_t hasFlashUnit(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080045 bool *hasFlash) = 0;
46
47 // set the torch mode to on or off.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080048 virtual status_t setTorchMode(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080049 bool enabled) = 0;
50};
51
52/**
53 * CameraFlashlight can be used by camera service to control flashflight.
54 */
55class CameraFlashlight : public virtual VirtualLightRefBase {
56 public:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080057 CameraFlashlight(sp<CameraProviderManager> providerManager,
58 camera_module_callbacks_t* callbacks);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080059 virtual ~CameraFlashlight();
60
Chien-Yu Chen88da5262015-02-17 13:56:46 -080061 // Find all flash units. This must be called before other methods. All
62 // camera devices must be closed when it's called because HAL v1 devices
63 // need to be opened to query available flash modes.
64 status_t findFlashUnits();
Chien-Yu Chen3068d732015-02-09 13:29:57 -080065
Chien-Yu Chen88da5262015-02-17 13:56:46 -080066 // Whether a camera device has a flash unit. Before findFlashUnits() is
67 // called, this function always returns false.
68 bool hasFlashUnit(const String8& cameraId);
69
70 // set the torch mode to on or off.
71 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080072
73 // Notify CameraFlashlight that camera service is going to open a camera
74 // device. CameraFlashlight will free the resources that may cause the
75 // camera open to fail. Camera service must call this function before
76 // opening a camera device.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080077 status_t prepareDeviceOpen(const String8& cameraId);
78
79 // Notify CameraFlashlight that camera service has closed a camera
80 // device. CameraFlashlight may invoke callbacks for torch mode
81 // available depending on the implementation.
82 status_t deviceClosed(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080083
84 private:
85 // create flashlight control based on camera module API and camera
86 // device API versions.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080087 status_t createFlashlightControl(const String8& cameraId);
88
89 // mLock should be locked.
90 bool hasFlashUnitLocked(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080091
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -070092 // Check if flash control is in backward compatible mode (simulated torch API by
93 // opening cameras)
94 bool isBackwardCompatibleMode(const String8& cameraId);
95
96 int getNumberOfCameras();
97
Chien-Yu Chen3068d732015-02-09 13:29:57 -080098 sp<FlashControlBase> mFlashControl;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080099
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800100 sp<CameraProviderManager> mProviderManager;
101
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800102 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800103 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800104
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800105 // camera id -> if it has a flash unit
106 KeyedVector<String8, bool> mHasFlashlightMap;
107 bool mFlashlightMapInitialized;
108
109 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800110};
111
112/**
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800113 * Flash control for camera provider v2.4 and above.
114 */
115class ProviderFlashControl : public FlashControlBase {
116 public:
117 ProviderFlashControl(sp<CameraProviderManager> providerManager);
118 virtual ~ProviderFlashControl();
119
120 // FlashControlBase
121 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
122 status_t setTorchMode(const String8& cameraId, bool enabled);
123
124 private:
125 sp<CameraProviderManager> mProviderManager;
126
127 Mutex mLock;
128};
129
130/**
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800131 * Flash control for camera module <= v2.3 and camera HAL v1
132 */
133class CameraHardwareInterfaceFlashControl : public FlashControlBase {
134 public:
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700135 CameraHardwareInterfaceFlashControl(
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700136 sp<CameraProviderManager> manager,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800137 const camera_module_callbacks_t& callbacks);
138 virtual ~CameraHardwareInterfaceFlashControl();
139
140 // FlashControlBase
141 status_t setTorchMode(const String8& cameraId, bool enabled);
142 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
143
144 private:
145 // connect to a camera device
146 status_t connectCameraDevice(const String8& cameraId);
147
148 // disconnect and free mDevice
149 status_t disconnectCameraDevice();
150
151 // initialize the preview window
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700152 status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800153 int32_t width, int32_t height);
154
155 // start preview and enable torch
156 status_t startPreviewAndTorch();
157
158 // get the smallest surface
159 status_t getSmallestSurfaceSize(int32_t *width, int32_t *height);
160
161 // protected by mLock
Chien-Yu Chen6dcc7062016-04-18 11:14:48 -0700162 // If this function opens camera device in order to check if it has a flash unit, the
163 // camera device will remain open if keepDeviceOpen is true and the camera device will be
164 // closed if keepDeviceOpen is false. If camera device is already open when calling this
165 // function, keepDeviceOpen is ignored.
166 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen);
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800167
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700168 sp<CameraProviderManager> mProviderManager;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800169 const camera_module_callbacks_t *mCallbacks;
170 sp<CameraHardwareInterface> mDevice;
171 String8 mCameraId;
172 CameraParameters mParameters;
173 bool mTorchEnabled;
174
175 sp<IGraphicBufferProducer> mProducer;
176 sp<IGraphicBufferConsumer> mConsumer;
177 sp<GLConsumer> mSurfaceTexture;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700178 sp<Surface> mSurface;
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800179
180 Mutex mLock;
181};
182
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800183} // namespace android
184
185#endif