blob: ccdd9e5c62d028f9f7d7b598070dc5051ae87f51 [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#define LOG_TAG "CameraFlashlight"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Chien-Yu Chen88da5262015-02-17 13:56:46 -080019// #define LOG_NDEBUG 0
Chien-Yu Chen3068d732015-02-09 13:29:57 -080020
21#include <utils/Log.h>
22#include <utils/Trace.h>
23#include <cutils/properties.h>
24
25#include "camera/CameraMetadata.h"
26#include "CameraFlashlight.h"
27#include "gui/IGraphicBufferConsumer.h"
28#include "gui/BufferQueue.h"
29#include "camera/camera2/CaptureRequest.h"
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -080030#include "device3/Camera3Device.h"
Chien-Yu Chen3068d732015-02-09 13:29:57 -080031
32
33namespace android {
34
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080035using hardware::camera::common::V1_0::TorchModeStatus;
36
Chien-Yu Chen88da5262015-02-17 13:56:46 -080037/////////////////////////////////////////////////////////////////////
38// CameraFlashlight implementation begins
39// used by camera service to control flashflight.
40/////////////////////////////////////////////////////////////////////
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080041
42CameraFlashlight::CameraFlashlight(sp<CameraProviderManager> providerManager,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080043 CameraProviderManager::StatusListener* callbacks) :
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080044 mProviderManager(providerManager),
45 mCallbacks(callbacks),
Chien-Yu Chen88da5262015-02-17 13:56:46 -080046 mFlashlightMapInitialized(false) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -080047}
48
49CameraFlashlight::~CameraFlashlight() {
50}
51
Chien-Yu Chen88da5262015-02-17 13:56:46 -080052status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -080053 ALOGV("%s: creating a flash light control for camera %s", __FUNCTION__,
54 cameraId.string());
55 if (mFlashControl != NULL) {
56 return INVALID_OPERATION;
57 }
58
Emilian Peevf53f66e2017-04-11 14:29:43 +010059 if (mProviderManager->supportSetTorchMode(cameraId.string())) {
60 mFlashControl = new ProviderFlashControl(mProviderManager);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080061 } else {
Eino-Ville Talvalaa976df82019-06-13 18:01:58 -070062 ALOGE("Flashlight control not supported by this device!");
63 return NO_INIT;
Chien-Yu Chen3068d732015-02-09 13:29:57 -080064 }
65
66 return OK;
67}
68
Chien-Yu Chen88da5262015-02-17 13:56:46 -080069status_t CameraFlashlight::setTorchMode(const String8& cameraId, bool enabled) {
70 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -070071 ALOGE("%s: findFlashUnits() must be called before this method.",
72 __FUNCTION__);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080073 return NO_INIT;
74 }
75
76 ALOGV("%s: set torch mode of camera %s to %d", __FUNCTION__,
77 cameraId.string(), enabled);
78
79 status_t res = OK;
80 Mutex::Autolock l(mLock);
81
Ruben Brunkcc776712015-02-17 20:18:47 -080082 if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) {
83 // This case is needed to avoid state corruption during the following call sequence:
84 // CameraService::setTorchMode for camera ID 0 begins, does torch status checks
85 // CameraService::connect for camera ID 0 begins, calls prepareDeviceOpen, ends
86 // CameraService::setTorchMode for camera ID 0 continues, calls
87 // CameraFlashlight::setTorchMode
88
89 // TODO: Move torch status checks and state updates behind this CameraFlashlight lock
90 // to avoid other similar race conditions.
91 ALOGE("%s: Camera device %s is in use, cannot set torch mode.",
92 __FUNCTION__, cameraId.string());
93 return -EBUSY;
94 }
95
Chien-Yu Chen3068d732015-02-09 13:29:57 -080096 if (mFlashControl == NULL) {
97 res = createFlashlightControl(cameraId);
98 if (res) {
99 return res;
100 }
101 res = mFlashControl->setTorchMode(cameraId, enabled);
102 return res;
103 }
104
105 // if flash control already exists, turning on torch mode may fail if it's
106 // tied to another camera device for module v2.3 and below.
107 res = mFlashControl->setTorchMode(cameraId, enabled);
108 if (res == BAD_INDEX) {
109 // flash control is tied to another camera device, need to close it and
110 // try again.
111 mFlashControl.clear();
112 res = createFlashlightControl(cameraId);
113 if (res) {
114 return res;
115 }
116 res = mFlashControl->setTorchMode(cameraId, enabled);
117 }
118
119 return res;
120}
121
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800122status_t CameraFlashlight::findFlashUnits() {
123 Mutex::Autolock l(mLock);
124 status_t res;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800125
126 std::vector<String8> cameraIds;
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700127 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800128 int numberOfCameras = static_cast<int>(ids.size());
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700129 cameraIds.resize(numberOfCameras);
Emilian Peevf53f66e2017-04-11 14:29:43 +0100130 // No module, must be provider
Emilian Peevf53f66e2017-04-11 14:29:43 +0100131 for (size_t i = 0; i < cameraIds.size(); i++) {
132 cameraIds[i] = String8(ids[i].c_str());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800134
Emilian Peevaee727d2017-05-04 16:35:48 +0100135 mFlashControl.clear();
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800136
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800137 for (auto &id : cameraIds) {
Emilian Peevaee727d2017-05-04 16:35:48 +0100138 ssize_t index = mHasFlashlightMap.indexOfKey(id);
139 if (0 <= index) {
140 continue;
141 }
142
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800143 bool hasFlash = false;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800144 res = createFlashlightControl(id);
145 if (res) {
146 ALOGE("%s: failed to create flash control for %s", __FUNCTION__,
147 id.string());
148 } else {
149 res = mFlashControl->hasFlashUnit(id, &hasFlash);
150 if (res == -EUSERS || res == -EBUSY) {
151 ALOGE("%s: failed to check if camera %s has a flash unit. Some "
152 "camera devices may be opened", __FUNCTION__,
153 id.string());
154 return res;
155 } else if (res) {
156 ALOGE("%s: failed to check if camera %s has a flash unit. %s"
157 " (%d)", __FUNCTION__, id.string(), strerror(-res),
158 res);
159 }
160
161 mFlashControl.clear();
162 }
163 mHasFlashlightMap.add(id, hasFlash);
164 }
165
166 mFlashlightMapInitialized = true;
167 return OK;
168}
169
170bool CameraFlashlight::hasFlashUnit(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800171 Mutex::Autolock l(mLock);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800172 return hasFlashUnitLocked(cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800173}
174
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800175bool CameraFlashlight::hasFlashUnitLocked(const String8& cameraId) {
176 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700177 ALOGE("%s: findFlashUnits() must be called before this method.",
178 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800179 return false;
180 }
181
182 ssize_t index = mHasFlashlightMap.indexOfKey(cameraId);
183 if (index == NAME_NOT_FOUND) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800184 // Might be external camera
185 ALOGW("%s: camera %s not present when findFlashUnits() was called",
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800186 __FUNCTION__, cameraId.string());
187 return false;
188 }
189
190 return mHasFlashlightMap.valueAt(index);
191}
192
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700193bool CameraFlashlight::isBackwardCompatibleMode(const String8& cameraId) {
194 bool backwardCompatibleMode = false;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100195 if (mProviderManager != nullptr &&
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700196 !mProviderManager->supportSetTorchMode(cameraId.string())) {
197 backwardCompatibleMode = true;
198 }
199 return backwardCompatibleMode;
200}
201
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800202status_t CameraFlashlight::prepareDeviceOpen(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800203 ALOGV("%s: prepare for device open", __FUNCTION__);
204
205 Mutex::Autolock l(mLock);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800206 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700207 ALOGE("%s: findFlashUnits() must be called before this method.",
208 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800209 return NO_INIT;
210 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800211
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700212 if (isBackwardCompatibleMode(cameraId)) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800213 // framework is going to open a camera device, all flash light control
214 // should be closed for backward compatible support.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800215 mFlashControl.clear();
216
217 if (mOpenedCameraIds.size() == 0) {
218 // notify torch unavailable for all cameras with a flash
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700219 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800220 int numCameras = static_cast<int>(ids.size());
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800221 for (int i = 0; i < numCameras; i++) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800222 String8 id8(ids[i].c_str());
223 if (hasFlashUnitLocked(id8)) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800224 mCallbacks->onTorchStatusChanged(
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800225 id8, TorchModeStatus::NOT_AVAILABLE);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800226 }
227 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800228 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800229
230 // close flash control that may be opened by calling hasFlashUnitLocked.
231 mFlashControl.clear();
232 }
233
234 if (mOpenedCameraIds.indexOf(cameraId) == NAME_NOT_FOUND) {
235 mOpenedCameraIds.add(cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800236 }
237
238 return OK;
239}
240
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800241status_t CameraFlashlight::deviceClosed(const String8& cameraId) {
242 ALOGV("%s: device %s is closed", __FUNCTION__, cameraId.string());
243
244 Mutex::Autolock l(mLock);
245 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700246 ALOGE("%s: findFlashUnits() must be called before this method.",
247 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800248 return NO_INIT;
249 }
250
251 ssize_t index = mOpenedCameraIds.indexOf(cameraId);
252 if (index == NAME_NOT_FOUND) {
253 ALOGE("%s: couldn't find camera %s in the opened list", __FUNCTION__,
254 cameraId.string());
255 } else {
256 mOpenedCameraIds.removeAt(index);
257 }
258
259 // Cannot do anything until all cameras are closed.
260 if (mOpenedCameraIds.size() != 0)
261 return OK;
262
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700263 if (isBackwardCompatibleMode(cameraId)) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800264 // notify torch available for all cameras with a flash
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700265 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800266 int numCameras = static_cast<int>(ids.size());
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800267 for (int i = 0; i < numCameras; i++) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800268 String8 id8(ids[i].c_str());
269 if (hasFlashUnitLocked(id8)) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800270 mCallbacks->onTorchStatusChanged(
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800271 id8, TorchModeStatus::AVAILABLE_OFF);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800272 }
273 }
274 }
275
276 return OK;
277}
278// CameraFlashlight implementation ends
279
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800280
281FlashControlBase::~FlashControlBase() {
282}
283
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800284/////////////////////////////////////////////////////////////////////
285// ModuleFlashControl implementation begins
286// Flash control for camera module v2.4 and above.
287/////////////////////////////////////////////////////////////////////
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800288ProviderFlashControl::ProviderFlashControl(sp<CameraProviderManager> providerManager) :
289 mProviderManager(providerManager) {
290}
291
292ProviderFlashControl::~ProviderFlashControl() {
293}
294
295status_t ProviderFlashControl::hasFlashUnit(const String8& cameraId, bool *hasFlash) {
296 if (!hasFlash) {
297 return BAD_VALUE;
298 }
299 *hasFlash = mProviderManager->hasFlashUnit(cameraId.string());
300 return OK;
301}
302
303status_t ProviderFlashControl::setTorchMode(const String8& cameraId, bool enabled) {
304 ALOGV("%s: set camera %s torch mode to %d", __FUNCTION__,
305 cameraId.string(), enabled);
306
307 return mProviderManager->setTorchMode(cameraId.string(), enabled);
308}
309// ProviderFlashControl implementation ends
310
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800311}