• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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_NDEBUG 0
18 #define LOG_TAG "EmulatedTorchState"
19 #include "EmulatedTorchState.h"
20 
21 #include <log/log.h>
22 
23 namespace android {
24 
25 using android::google_camera_hal::TorchModeStatus;
26 
SetTorchMode(TorchMode mode)27 status_t EmulatedTorchState::SetTorchMode(TorchMode mode) {
28   std::lock_guard<std::mutex> lock(mutex_);
29   if (camera_open_) {
30     ALOGE("%s: Camera device open, torch cannot be controlled using this API!",
31           __FUNCTION__);
32     return UNKNOWN_ERROR;
33   }
34 
35   torch_status_ = (mode == TorchMode::kOn) ? TorchModeStatus::kAvailableOn : TorchModeStatus::kAvailableOff;
36   if (mode == TorchMode::kOff && support_torch_strength_control_) {
37     new_torch_strength_level_ = default_level_;
38     ALOGV("%s: Turning torch OFF so reset the torch strength to default level:%d",
39           __FUNCTION__, default_level_);
40   }
41 
42   torch_cb_(camera_id_, (mode == TorchMode::kOn)
43                             ? TorchModeStatus::kAvailableOn
44                             : TorchModeStatus::kAvailableOff);
45   return OK;
46 }
47 
TurnOnTorchWithStrengthLevel(int32_t torch_strength)48 status_t EmulatedTorchState::TurnOnTorchWithStrengthLevel(int32_t torch_strength) {
49   std::lock_guard<std::mutex> lock(mutex_);
50   if (camera_open_) {
51     ALOGE("%s: Camera device open, torch cannot be controlled using this API!",
52           __FUNCTION__);
53     return UNKNOWN_ERROR;
54   }
55   new_torch_strength_level_ = torch_strength;
56   // If the torch mode is OFF and device is available, torch will be turned ON.
57   // torch_strength value should be greater than 1.
58   if (torch_status_ != TorchModeStatus::kAvailableOn && torch_strength > 1) {
59     torch_status_ = TorchModeStatus::kAvailableOn;
60     ALOGV("Changed the torch status to: %d", torch_status_);
61     torch_cb_(camera_id_, TorchModeStatus::kAvailableOn);
62   }
63 
64   ALOGV("%s: Torch strength level successfully set to %d", __FUNCTION__, torch_strength);
65 
66   return OK;
67 }
68 
AcquireFlashHw()69 void EmulatedTorchState::AcquireFlashHw() {
70   std::lock_guard<std::mutex> lock(mutex_);
71   camera_open_ = true;
72   torch_cb_(camera_id_, TorchModeStatus::kNotAvailable);
73 }
74 
ReleaseFlashHw()75 void EmulatedTorchState::ReleaseFlashHw() {
76   std::lock_guard<std::mutex> lock(mutex_);
77   camera_open_ = false;
78   torch_cb_(camera_id_, TorchModeStatus::kAvailableOff);
79 }
80 
GetTorchStrengthLevel()81 int32_t EmulatedTorchState::GetTorchStrengthLevel() {
82   std::lock_guard<std::mutex> lock(mutex_);
83   return new_torch_strength_level_;
84 }
85 
InitializeTorchDefaultLevel(int32_t default_level)86 void EmulatedTorchState::InitializeTorchDefaultLevel(int32_t default_level) {
87   std::lock_guard<std::mutex> lock(mutex_);
88   default_level_ = default_level;
89 }
90 
InitializeSupportTorchStrengthLevel(bool is_torch_strength_control_supported)91 void EmulatedTorchState::InitializeSupportTorchStrengthLevel(bool is_torch_strength_control_supported) {
92   std::lock_guard<std::mutex> lock(mutex_);
93   support_torch_strength_control_ = is_torch_strength_control_supported;
94 }
95 
96 
97 }  // namespace android
98