• 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 
27 namespace android {
28 
29 /**
30  * FlashControlBase is a base class for flash control. It defines the functions
31  * that a flash control for each camera module/device version should implement.
32  */
33 class FlashControlBase : public virtual VirtualLightRefBase {
34     public:
35         virtual ~FlashControlBase();
36 
37         // Whether a camera device has a flash unit. Calling this function may
38         // cause the torch mode to be turned off in HAL v1 devices. If
39         // previously-on torch mode is turned off,
40         // callbacks.torch_mode_status_change() should be invoked.
41         virtual status_t hasFlashUnit(const String8& cameraId,
42                     bool *hasFlash) = 0;
43 
44         // set the torch mode to on or off.
45         virtual status_t setTorchMode(const String8& cameraId,
46                     bool enabled) = 0;
47 };
48 
49 /**
50  * CameraFlashlight can be used by camera service to control flashflight.
51  */
52 class CameraFlashlight : public virtual VirtualLightRefBase {
53     public:
54         CameraFlashlight(sp<CameraProviderManager> providerManager,
55                 CameraProviderManager::StatusListener* callbacks);
56         virtual ~CameraFlashlight();
57 
58         // Find all flash units. This must be called before other methods. All
59         // camera devices must be closed when it's called because HAL v1 devices
60         // need to be opened to query available flash modes.
61         status_t findFlashUnits();
62 
63         // Whether a camera device has a flash unit. Before findFlashUnits() is
64         // called, this function always returns false.
65         bool hasFlashUnit(const String8& cameraId);
66 
67         // set the torch mode to on or off.
68         status_t setTorchMode(const String8& cameraId, bool enabled);
69 
70         // Notify CameraFlashlight that camera service is going to open a camera
71         // device. CameraFlashlight will free the resources that may cause the
72         // camera open to fail. Camera service must call this function before
73         // opening a camera device.
74         status_t prepareDeviceOpen(const String8& cameraId);
75 
76         // Notify CameraFlashlight that camera service has closed a camera
77         // device. CameraFlashlight may invoke callbacks for torch mode
78         // available depending on the implementation.
79         status_t deviceClosed(const String8& cameraId);
80 
81     private:
82         // create flashlight control based on camera module API and camera
83         // device API versions.
84         status_t createFlashlightControl(const String8& cameraId);
85 
86         // mLock should be locked.
87         bool hasFlashUnitLocked(const String8& cameraId);
88 
89         // Check if flash control is in backward compatible mode (simulated torch API by
90         // opening cameras)
91         bool isBackwardCompatibleMode(const String8& cameraId);
92 
93         sp<FlashControlBase> mFlashControl;
94 
95         sp<CameraProviderManager> mProviderManager;
96 
97         CameraProviderManager::StatusListener* mCallbacks;
98         SortedVector<String8> mOpenedCameraIds;
99 
100         // camera id -> if it has a flash unit
101         KeyedVector<String8, bool> mHasFlashlightMap;
102         bool mFlashlightMapInitialized;
103 
104         Mutex mLock; // protect CameraFlashlight API
105 };
106 
107 /**
108  * Flash control for camera provider v2.4 and above.
109  */
110 class ProviderFlashControl : public FlashControlBase {
111     public:
112         ProviderFlashControl(sp<CameraProviderManager> providerManager);
113         virtual ~ProviderFlashControl();
114 
115         // FlashControlBase
116         status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
117         status_t setTorchMode(const String8& cameraId, bool enabled);
118 
119     private:
120         sp<CameraProviderManager> mProviderManager;
121 
122         Mutex mLock;
123 };
124 
125 } // namespace android
126 
127 #endif
128