blob: ae502b90ed004dfaeec3d366efc9325a8de2b3e6 [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
20#include "hardware/camera_common.h"
21#include "utils/KeyedVector.h"
Chien-Yu Chen88da5262015-02-17 13:56:46 -080022#include "utils/SortedVector.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080023#include "gui/GLConsumer.h"
24#include "gui/Surface.h"
25#include "common/CameraDeviceBase.h"
26
27namespace 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 */
33class 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.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080041 virtual status_t hasFlashUnit(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080042 bool *hasFlash) = 0;
43
44 // set the torch mode to on or off.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080045 virtual status_t setTorchMode(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -080046 bool enabled) = 0;
47};
48
49/**
50 * CameraFlashlight can be used by camera service to control flashflight.
51 */
52class CameraFlashlight : public virtual VirtualLightRefBase {
53 public:
54 CameraFlashlight(CameraModule& cameraModule,
55 const camera_module_callbacks_t& callbacks);
56 virtual ~CameraFlashlight();
57
Chien-Yu Chen88da5262015-02-17 13:56:46 -080058 // 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();
Chien-Yu Chen3068d732015-02-09 13:29:57 -080062
Chien-Yu Chen88da5262015-02-17 13:56:46 -080063 // 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);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080069
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.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080074 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);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080080
81 private:
82 // create flashlight control based on camera module API and camera
83 // device API versions.
Chien-Yu Chen88da5262015-02-17 13:56:46 -080084 status_t createFlashlightControl(const String8& cameraId);
85
86 // mLock should be locked.
87 bool hasFlashUnitLocked(const String8& cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080088
89 sp<FlashControlBase> mFlashControl;
90 CameraModule *mCameraModule;
91 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -080092 SortedVector<String8> mOpenedCameraIds;
Chien-Yu Chen3068d732015-02-09 13:29:57 -080093
Chien-Yu Chen88da5262015-02-17 13:56:46 -080094 // camera id -> if it has a flash unit
95 KeyedVector<String8, bool> mHasFlashlightMap;
96 bool mFlashlightMapInitialized;
97
98 Mutex mLock; // protect CameraFlashlight API
Chien-Yu Chen3068d732015-02-09 13:29:57 -080099};
100
101/**
102 * Flash control for camera module v2.4 and above.
103 */
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800104class ModuleFlashControl : public FlashControlBase {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800105 public:
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800106 ModuleFlashControl(CameraModule& cameraModule,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800107 const camera_module_callbacks_t& callbacks);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800108 virtual ~ModuleFlashControl();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800109
110 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800111 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
112 status_t setTorchMode(const String8& cameraId, bool enabled);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800113
114 private:
115 CameraModule *mCameraModule;
116
117 Mutex mLock;
118};
119
120/**
121 * Flash control for camera module <= v2.3 and camera HAL v2-v3
122 */
123class CameraDeviceClientFlashControl : public FlashControlBase {
124 public:
125 CameraDeviceClientFlashControl(CameraModule& cameraModule,
126 const camera_module_callbacks_t& callbacks);
127 virtual ~CameraDeviceClientFlashControl();
128
129 // FlashControlBase
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800130 status_t setTorchMode(const String8& cameraId, bool enabled);
131 status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800132
133 private:
134 // connect to a camera device
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800135 status_t connectCameraDevice(const String8& cameraId);
136 // disconnect and free mDevice
137 status_t disconnectCameraDevice();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800138
139 // initialize a surface
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800140 status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width,
141 int32_t height);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800142
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800143 // submit a request to enable the torch mode
144 status_t submitTorchEnabledRequest();
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800145
146 // get the smallest surface size of IMPLEMENTATION_DEFINED
147 status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width,
148 int32_t *height);
149
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800150 status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800151
152 CameraModule *mCameraModule;
153 const camera_module_callbacks_t *mCallbacks;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800154 String8 mCameraId;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800155 bool mTorchEnabled;
156 CameraMetadata *mMetadata;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800157 // WORKAROUND: will be set to true for HAL v2 devices where
158 // setStreamingRequest() needs to be call for torch mode settings to
159 // take effect.
160 bool mStreaming;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800161
162 sp<CameraDeviceBase> mDevice;
163
164 sp<IGraphicBufferProducer> mProducer;
165 sp<IGraphicBufferConsumer> mConsumer;
166 sp<GLConsumer> mSurfaceTexture;
167 sp<ANativeWindow> mAnw;
168 int32_t mStreamId;
169
170 Mutex mLock;
171};
172
173} // namespace android
174
175#endif