• 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 <utils/KeyedVector.h>
23 #include <utils/SortedVector.h>
24 #include "common/CameraProviderManager.h"
25 #include "common/CameraDeviceBase.h"
26 #include "device1/CameraHardwareInterface.h"
27 
28 
29 namespace 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  */
35 class 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.
43         virtual status_t hasFlashUnit(const String8& cameraId,
44                     bool *hasFlash) = 0;
45 
46         // set the torch mode to on or off.
47         virtual status_t setTorchMode(const String8& cameraId,
48                     bool enabled) = 0;
49 };
50 
51 /**
52  * CameraFlashlight can be used by camera service to control flashflight.
53  */
54 class CameraFlashlight : public virtual VirtualLightRefBase {
55     public:
56         CameraFlashlight(sp<CameraProviderManager> providerManager,
57                 CameraProviderManager::StatusListener* callbacks);
58         virtual ~CameraFlashlight();
59 
60         // 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();
64 
65         // 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);
71 
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.
76         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);
82 
83     private:
84         // create flashlight control based on camera module API and camera
85         // device API versions.
86         status_t createFlashlightControl(const String8& cameraId);
87 
88         // mLock should be locked.
89         bool hasFlashUnitLocked(const String8& cameraId);
90 
91         // Check if flash control is in backward compatible mode (simulated torch API by
92         // opening cameras)
93         bool isBackwardCompatibleMode(const String8& cameraId);
94 
95         sp<FlashControlBase> mFlashControl;
96 
97         sp<CameraProviderManager> mProviderManager;
98 
99         CameraProviderManager::StatusListener* mCallbacks;
100         SortedVector<String8> mOpenedCameraIds;
101 
102         // camera id -> if it has a flash unit
103         KeyedVector<String8, bool> mHasFlashlightMap;
104         bool mFlashlightMapInitialized;
105 
106         Mutex mLock; // protect CameraFlashlight API
107 };
108 
109 /**
110  * Flash control for camera provider v2.4 and above.
111  */
112 class 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 /**
128  * Flash control for camera module <= v2.3 and camera HAL v1
129  */
130 class CameraHardwareInterfaceFlashControl : public FlashControlBase {
131     public:
132         CameraHardwareInterfaceFlashControl(
133                 sp<CameraProviderManager> manager,
134                 CameraProviderManager::StatusListener* callbacks);
135         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
149         status_t initializePreviewWindow(const sp<CameraHardwareInterface>& device,
150                 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
159         // 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);
164 
165         sp<CameraProviderManager> mProviderManager;
166         CameraProviderManager::StatusListener* mCallbacks;
167         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;
175         sp<Surface> mSurface;
176 
177         Mutex mLock;
178 };
179 
180 } // namespace android
181 
182 #endif
183