blob: ffd38be50910de0ad4cf37529e74be9ef48170bb [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
Chien-Yu Chen88da5262015-02-17 13:56:46 -080035/////////////////////////////////////////////////////////////////////
36// CameraFlashlight implementation begins
37// used by camera service to control flashflight.
38/////////////////////////////////////////////////////////////////////
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080039
40CameraFlashlight::CameraFlashlight(sp<CameraProviderManager> providerManager,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080041 CameraProviderManager::StatusListener* callbacks) :
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080042 mProviderManager(providerManager),
43 mCallbacks(callbacks),
Chien-Yu Chen88da5262015-02-17 13:56:46 -080044 mFlashlightMapInitialized(false) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -080045}
46
47CameraFlashlight::~CameraFlashlight() {
48}
49
Chien-Yu Chen88da5262015-02-17 13:56:46 -080050status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -080051 ALOGV("%s: creating a flash light control for camera %s", __FUNCTION__,
52 cameraId.string());
53 if (mFlashControl != NULL) {
54 return INVALID_OPERATION;
55 }
56
Emilian Peevf53f66e2017-04-11 14:29:43 +010057 if (mProviderManager->supportSetTorchMode(cameraId.string())) {
58 mFlashControl = new ProviderFlashControl(mProviderManager);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080059 } else {
Eino-Ville Talvalaa976df82019-06-13 18:01:58 -070060 ALOGE("Flashlight control not supported by this device!");
61 return NO_INIT;
Chien-Yu Chen3068d732015-02-09 13:29:57 -080062 }
63
64 return OK;
65}
66
Chien-Yu Chen88da5262015-02-17 13:56:46 -080067status_t CameraFlashlight::setTorchMode(const String8& cameraId, bool enabled) {
68 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -070069 ALOGE("%s: findFlashUnits() must be called before this method.",
70 __FUNCTION__);
Chien-Yu Chen3068d732015-02-09 13:29:57 -080071 return NO_INIT;
72 }
73
74 ALOGV("%s: set torch mode of camera %s to %d", __FUNCTION__,
75 cameraId.string(), enabled);
76
77 status_t res = OK;
78 Mutex::Autolock l(mLock);
79
Ruben Brunkcc776712015-02-17 20:18:47 -080080 if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) {
81 // This case is needed to avoid state corruption during the following call sequence:
82 // CameraService::setTorchMode for camera ID 0 begins, does torch status checks
83 // CameraService::connect for camera ID 0 begins, calls prepareDeviceOpen, ends
84 // CameraService::setTorchMode for camera ID 0 continues, calls
85 // CameraFlashlight::setTorchMode
86
87 // TODO: Move torch status checks and state updates behind this CameraFlashlight lock
88 // to avoid other similar race conditions.
89 ALOGE("%s: Camera device %s is in use, cannot set torch mode.",
90 __FUNCTION__, cameraId.string());
91 return -EBUSY;
92 }
93
Chien-Yu Chen3068d732015-02-09 13:29:57 -080094 if (mFlashControl == NULL) {
95 res = createFlashlightControl(cameraId);
96 if (res) {
97 return res;
98 }
99 res = mFlashControl->setTorchMode(cameraId, enabled);
100 return res;
101 }
102
103 // if flash control already exists, turning on torch mode may fail if it's
104 // tied to another camera device for module v2.3 and below.
105 res = mFlashControl->setTorchMode(cameraId, enabled);
106 if (res == BAD_INDEX) {
107 // flash control is tied to another camera device, need to close it and
108 // try again.
109 mFlashControl.clear();
110 res = createFlashlightControl(cameraId);
111 if (res) {
112 return res;
113 }
114 res = mFlashControl->setTorchMode(cameraId, enabled);
115 }
116
117 return res;
118}
119
Rucha Katakwar38284522021-11-10 11:25:21 -0800120status_t CameraFlashlight::turnOnTorchWithStrengthLevel(const String8& cameraId,
121 int32_t torchStrength) {
122 if (!mFlashlightMapInitialized) {
123 ALOGE("%s: findFlashUnits() must be called before this method.",
124 __FUNCTION__);
125 return NO_INIT;
126 }
127
128 ALOGV("%s: set torch strength of camera %s to %d", __FUNCTION__,
129 cameraId.string(), torchStrength);
130 status_t res = OK;
131 Mutex::Autolock l(mLock);
132
133 if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) {
134 ALOGE("%s: Camera device %s is in use, cannot be turned ON.",
135 __FUNCTION__, cameraId.string());
136 return -EBUSY;
137 }
138
139 if (mFlashControl == NULL) {
140 res = createFlashlightControl(cameraId);
141 if (res) {
142 return res;
143 }
144 }
145
146 res = mFlashControl->turnOnTorchWithStrengthLevel(cameraId, torchStrength);
147 return res;
148}
149
150
151status_t CameraFlashlight::getTorchStrengthLevel(const String8& cameraId,
152 int32_t* torchStrength) {
153 status_t res = OK;
154 if (!mFlashlightMapInitialized) {
155 ALOGE("%s: findFlashUnits() must be called before this method.",
156 __FUNCTION__);
157 return false;
158 }
159
160 Mutex::Autolock l(mLock);
161
162 if (mFlashControl == NULL) {
163 res = createFlashlightControl(cameraId);
164 if (res) {
165 return res;
166 }
167 }
168
169 res = mFlashControl->getTorchStrengthLevel(cameraId, torchStrength);
170 return res;
171}
172
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800173status_t CameraFlashlight::findFlashUnits() {
174 Mutex::Autolock l(mLock);
175 status_t res;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176
177 std::vector<String8> cameraIds;
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700178 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800179 int numberOfCameras = static_cast<int>(ids.size());
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700180 cameraIds.resize(numberOfCameras);
Emilian Peevf53f66e2017-04-11 14:29:43 +0100181 // No module, must be provider
Emilian Peevf53f66e2017-04-11 14:29:43 +0100182 for (size_t i = 0; i < cameraIds.size(); i++) {
183 cameraIds[i] = String8(ids[i].c_str());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800184 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800185
Emilian Peevaee727d2017-05-04 16:35:48 +0100186 mFlashControl.clear();
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800187
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800188 for (auto &id : cameraIds) {
Emilian Peevaee727d2017-05-04 16:35:48 +0100189 ssize_t index = mHasFlashlightMap.indexOfKey(id);
190 if (0 <= index) {
191 continue;
192 }
193
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800194 bool hasFlash = false;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800195 res = createFlashlightControl(id);
196 if (res) {
197 ALOGE("%s: failed to create flash control for %s", __FUNCTION__,
198 id.string());
199 } else {
200 res = mFlashControl->hasFlashUnit(id, &hasFlash);
201 if (res == -EUSERS || res == -EBUSY) {
202 ALOGE("%s: failed to check if camera %s has a flash unit. Some "
203 "camera devices may be opened", __FUNCTION__,
204 id.string());
205 return res;
206 } else if (res) {
207 ALOGE("%s: failed to check if camera %s has a flash unit. %s"
208 " (%d)", __FUNCTION__, id.string(), strerror(-res),
209 res);
210 }
211
212 mFlashControl.clear();
213 }
214 mHasFlashlightMap.add(id, hasFlash);
215 }
216
217 mFlashlightMapInitialized = true;
218 return OK;
219}
220
221bool CameraFlashlight::hasFlashUnit(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800222 Mutex::Autolock l(mLock);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800223 return hasFlashUnitLocked(cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800224}
225
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800226bool CameraFlashlight::hasFlashUnitLocked(const String8& cameraId) {
227 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700228 ALOGE("%s: findFlashUnits() must be called before this method.",
229 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800230 return false;
231 }
232
233 ssize_t index = mHasFlashlightMap.indexOfKey(cameraId);
234 if (index == NAME_NOT_FOUND) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800235 // Might be external camera
236 ALOGW("%s: camera %s not present when findFlashUnits() was called",
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800237 __FUNCTION__, cameraId.string());
238 return false;
239 }
240
241 return mHasFlashlightMap.valueAt(index);
242}
243
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700244bool CameraFlashlight::isBackwardCompatibleMode(const String8& cameraId) {
245 bool backwardCompatibleMode = false;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100246 if (mProviderManager != nullptr &&
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700247 !mProviderManager->supportSetTorchMode(cameraId.string())) {
248 backwardCompatibleMode = true;
249 }
250 return backwardCompatibleMode;
251}
252
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800253status_t CameraFlashlight::prepareDeviceOpen(const String8& cameraId) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800254 ALOGV("%s: prepare for device open", __FUNCTION__);
255
256 Mutex::Autolock l(mLock);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800257 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700258 ALOGE("%s: findFlashUnits() must be called before this method.",
259 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800260 return NO_INIT;
261 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800262
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700263 if (isBackwardCompatibleMode(cameraId)) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800264 // framework is going to open a camera device, all flash light control
265 // should be closed for backward compatible support.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800266 mFlashControl.clear();
267
268 if (mOpenedCameraIds.size() == 0) {
269 // notify torch unavailable for all cameras with a flash
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700270 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800271 int numCameras = static_cast<int>(ids.size());
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800272 for (int i = 0; i < numCameras; i++) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800273 String8 id8(ids[i].c_str());
274 if (hasFlashUnitLocked(id8)) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800275 mCallbacks->onTorchStatusChanged(
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800276 id8, TorchModeStatus::NOT_AVAILABLE);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800277 }
278 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800279 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800280
281 // close flash control that may be opened by calling hasFlashUnitLocked.
282 mFlashControl.clear();
283 }
284
285 if (mOpenedCameraIds.indexOf(cameraId) == NAME_NOT_FOUND) {
286 mOpenedCameraIds.add(cameraId);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800287 }
288
289 return OK;
290}
291
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800292status_t CameraFlashlight::deviceClosed(const String8& cameraId) {
293 ALOGV("%s: device %s is closed", __FUNCTION__, cameraId.string());
294
295 Mutex::Autolock l(mLock);
296 if (!mFlashlightMapInitialized) {
Ranjith Kagathi Ananda32ab9fd2015-10-08 16:41:09 -0700297 ALOGE("%s: findFlashUnits() must be called before this method.",
298 __FUNCTION__);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800299 return NO_INIT;
300 }
301
302 ssize_t index = mOpenedCameraIds.indexOf(cameraId);
303 if (index == NAME_NOT_FOUND) {
304 ALOGE("%s: couldn't find camera %s in the opened list", __FUNCTION__,
305 cameraId.string());
306 } else {
307 mOpenedCameraIds.removeAt(index);
308 }
309
310 // Cannot do anything until all cameras are closed.
311 if (mOpenedCameraIds.size() != 0)
312 return OK;
313
Yin-Chia Yehdc3134e2017-03-23 15:26:59 -0700314 if (isBackwardCompatibleMode(cameraId)) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800315 // notify torch available for all cameras with a flash
Yin-Chia Yeh98c72a82018-08-28 11:20:16 -0700316 std::vector<std::string> ids = mProviderManager->getCameraDeviceIds();
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800317 int numCameras = static_cast<int>(ids.size());
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800318 for (int i = 0; i < numCameras; i++) {
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800319 String8 id8(ids[i].c_str());
320 if (hasFlashUnitLocked(id8)) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800321 mCallbacks->onTorchStatusChanged(
Yin-Chia Yeh6b1f6112018-02-28 13:05:59 -0800322 id8, TorchModeStatus::AVAILABLE_OFF);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800323 }
324 }
325 }
326
327 return OK;
328}
329// CameraFlashlight implementation ends
330
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800331
332FlashControlBase::~FlashControlBase() {
333}
334
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800335/////////////////////////////////////////////////////////////////////
336// ModuleFlashControl implementation begins
337// Flash control for camera module v2.4 and above.
338/////////////////////////////////////////////////////////////////////
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800339ProviderFlashControl::ProviderFlashControl(sp<CameraProviderManager> providerManager) :
340 mProviderManager(providerManager) {
341}
342
343ProviderFlashControl::~ProviderFlashControl() {
344}
345
346status_t ProviderFlashControl::hasFlashUnit(const String8& cameraId, bool *hasFlash) {
347 if (!hasFlash) {
348 return BAD_VALUE;
349 }
350 *hasFlash = mProviderManager->hasFlashUnit(cameraId.string());
351 return OK;
352}
353
354status_t ProviderFlashControl::setTorchMode(const String8& cameraId, bool enabled) {
355 ALOGV("%s: set camera %s torch mode to %d", __FUNCTION__,
356 cameraId.string(), enabled);
357
358 return mProviderManager->setTorchMode(cameraId.string(), enabled);
359}
Rucha Katakwar38284522021-11-10 11:25:21 -0800360
361status_t ProviderFlashControl::turnOnTorchWithStrengthLevel(const String8& cameraId,
362 int32_t torchStrength) {
363 ALOGV("%s: change torch strength level of camera %s to %d", __FUNCTION__,
364 cameraId.string(), torchStrength);
365
366 return mProviderManager->turnOnTorchWithStrengthLevel(cameraId.string(), torchStrength);
367}
368
369status_t ProviderFlashControl::getTorchStrengthLevel(const String8& cameraId,
370 int32_t* torchStrength) {
371 ALOGV("%s: get torch strength level of camera %s", __FUNCTION__,
372 cameraId.string());
373
374 return mProviderManager->getTorchStrengthLevel(cameraId.string(), torchStrength);
375}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800376// ProviderFlashControl implementation ends
377
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800378}