• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <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/CameraDeviceBase.h"
27 #include "device1/CameraHardwareInterface.h"
28 
29 
30 namespace 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  */
36 class 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.
44         virtual status_t hasFlashUnit(const String8& cameraId,
45                     bool *hasFlash) = 0;
46 
47         // set the torch mode to on or off.
48         virtual status_t setTorchMode(const String8& cameraId,
49                     bool enabled) = 0;
50 };
51 
52 /**
53  * CameraFlashlight can be used by camera service to control flashflight.
54  */
55 class CameraFlashlight : public virtual VirtualLightRefBase {
56     public:
57         CameraFlashlight(sp<CameraProviderManager> providerManager,
58                 camera_module_callbacks_t* callbacks);
59         virtual ~CameraFlashlight();
60 
61         // 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();
65 
66         // 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);
72 
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.
77         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);
83 
84     private:
85         // create flashlight control based on camera module API and camera
86         // device API versions.
87         status_t createFlashlightControl(const String8& cameraId);
88 
89         // mLock should be locked.
90         bool hasFlashUnitLocked(const String8& cameraId);
91 
92         // 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 
98         sp<FlashControlBase> mFlashControl;
99 
100         sp<CameraProviderManager> mProviderManager;
101 
102         const camera_module_callbacks_t *mCallbacks;
103         SortedVector<String8> mOpenedCameraIds;
104 
105         // camera id -> if it has a flash unit
106         KeyedVector<String8, bool> mHasFlashlightMap;
107         bool mFlashlightMapInitialized;
108 
109         Mutex mLock; // protect CameraFlashlight API
110 };
111 
112 /**
113  * Flash control for camera provider v2.4 and above.
114  */
115 class 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 /**
131  * Flash control for camera module <= v2.3 and camera HAL v1
132  */
133 class CameraHardwareInterfaceFlashControl : public FlashControlBase {
134     public:
135         CameraHardwareInterfaceFlashControl(
136                 sp<CameraProviderManager> manager,
137                 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
152         status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
153                 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
162         // 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);
167 
168         sp<CameraProviderManager> mProviderManager;
169         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;
178         sp<Surface> mSurface;
179 
180         Mutex mLock;
181 };
182 
183 } // namespace android
184 
185 #endif
186