1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dp_power_manager.h"
17 
18 #include "dp_log.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
23 namespace {
24     const std::string DEFERRED_LOCK_NAME = "DeferredProcessingWakeLock";
25 }
26 
DPSProwerManager()27 DPSProwerManager::DPSProwerManager()
28 {
29     DP_DEBUG_LOG("entered.");
30     Initialize();
31 }
32 
~DPSProwerManager()33 DPSProwerManager::~DPSProwerManager()
34 {
35     DP_DEBUG_LOG("entered.");
36     wakeLock_ = nullptr;
37 }
38 
Initialize()39 void DPSProwerManager::Initialize()
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     DP_DEBUG_LOG("entered.");
43     if (initialized_.load()) {
44         return;
45     }
46 
47 #ifdef CAMERA_USE_POWER
48     auto& power = PowerMgr::PowerMgrClient::GetInstance();
49     wakeLock_ = power.CreateRunningLock(DEFERRED_LOCK_NAME, PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND);
50     if (wakeLock_ == nullptr) {
51         DP_ERR_LOG("DPSProwerManager initialize failed.");
52         return;
53     }
54 #endif
55     initialized_.store(true);
56 }
57 
SetAutoSuspend(bool isAutoSuspend,uint32_t time)58 void DPSProwerManager::SetAutoSuspend(bool isAutoSuspend, uint32_t time)
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     DP_DEBUG_LOG("entered.");
62     if (wakeLock_ == nullptr) {
63         DP_ERR_LOG("WakeLock is null.");
64         return;
65     }
66 
67     if (isAutoSuspend) {
68         if (isSuspend_) {
69             EnableAutoSuspend();
70             isSuspend_ = false;
71         } else {
72             DP_DEBUG_LOG("AutoSuspend already turn on.");
73         }
74     } else {
75         if (!isSuspend_) {
76             DisableAutoSuspend(time);
77             isSuspend_ = true;
78         } else {
79             DP_DEBUG_LOG("AutoSuspend already not turn on yet.");
80         }
81     }
82 }
83 
EnableAutoSuspend()84 void DPSProwerManager::EnableAutoSuspend()
85 {
86     DP_CHECK_EXECUTE(wakeLock_ != nullptr, wakeLock_->UnLock());
87 }
88 
DisableAutoSuspend(uint32_t time)89 void DPSProwerManager::DisableAutoSuspend(uint32_t time)
90 {
91     DP_CHECK_EXECUTE(wakeLock_ != nullptr, wakeLock_->Lock(time));
92 }
93 } // namespace DeferredProcessing
94 } // namespace CameraStandard
95 } // namespace OHOS