1 /*
2  * Copyright (C) 2017 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 #include "chre/platform/power_control_manager.h"
18 
19 #include "chre/platform/slpi/power_control_util.h"
20 #include "chre/platform/slpi/see/island_vote_client.h"
21 #include "chre/platform/system_time.h"
22 #include "chre/util/lock_guard.h"
23 
24 #ifdef CHRE_USE_BUFFERED_LOGGING
25 #include "chre/platform/shared/log_buffer_manager.h"
26 #endif
27 
28 namespace chre {
29 
PowerControlManagerBase()30 PowerControlManagerBase::PowerControlManagerBase() : mHostIsAwake(true) {
31 #ifdef CHRE_THREAD_UTIL_ENABLED
32   sns_client_create_thread_utilization_client(&mThreadUtilClient);
33 #endif  // CHRE_THREAD_UTIL_ENABLED
34 }
35 
~PowerControlManagerBase()36 PowerControlManagerBase::~PowerControlManagerBase() {
37 #ifdef CHRE_THREAD_UTIL_ENABLED
38   sns_client_remove_thread_utilization_client(mThreadUtilClient);
39 #endif  // CHRE_THREAD_UTIL_ENABLED
40 }
41 
voteBigImage(bool bigImage)42 bool PowerControlManagerBase::voteBigImage(bool bigImage) {
43   return IslandVoteClientSingleton::get()->voteBigImage(bigImage);
44 }
45 
onHostWakeSuspendEvent(bool awake)46 void PowerControlManagerBase::onHostWakeSuspendEvent(bool awake) {
47   if (mHostIsAwake != awake) {
48     mHostIsAwake = awake;
49 
50     if (!awake) {
51       EventLoopManagerSingleton::get()
52           ->getHostCommsManager()
53           .resetBlameForNanoappHostWakeup();
54     }
55 
56     EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
57         mHostIsAwake ? CHRE_EVENT_HOST_AWAKE : CHRE_EVENT_HOST_ASLEEP,
58         nullptr /* eventData */, nullptr /* freeCallback */);
59 
60 #ifdef CHRE_AUDIO_SUPPORT_ENABLED
61     if (awake) {
62       auto callback = [](uint16_t /*type*/, void * /*data*/,
63                          void * /*extraData*/) {
64         EventLoopManagerSingleton::get()
65             ->getAudioRequestManager()
66             .getPlatformAudio()
67             .onHostAwake();
68       };
69 
70       EventLoopManagerSingleton::get()->deferCallback(
71           SystemCallbackType::AudioHandleHostAwake, nullptr, callback);
72     }
73 #endif  // CHRE_AUDIO_SUPPORT_ENABLED
74 
75 #ifdef CHRE_USE_BUFFERED_LOGGING
76     if (awake) {
77       LogBufferManagerSingleton::get()->flushLogs();
78     }
79 #endif
80   }
81 }
82 
preEventLoopProcess(size_t)83 void PowerControlManager::preEventLoopProcess(size_t /* numPendingEvents */) {}
84 
postEventLoopProcess(size_t numPendingEvents)85 void PowerControlManager::postEventLoopProcess(size_t numPendingEvents) {
86 #ifdef CHRE_THREAD_UTIL_ENABLED
87   // Although this execution point does not actually represent the start
88   // of the CHRE thread's activity, we only care about cases where the
89   // CHRE's event queue is highly backlogged for voting higher clock rates.
90   if (mIsThreadIdle && numPendingEvents != 0) {
91     sns_client_thread_utilization_start(mThreadUtilClient);
92     mIsThreadIdle = false;
93   } else if (!mIsThreadIdle) {
94     // Update the time profile as frequently as possible so that clock updates
95     // are not deferred until all events are processed.
96     sns_client_thread_utilization_stop(mThreadUtilClient);
97     if (numPendingEvents != 0) {
98       sns_client_thread_utilization_start(mThreadUtilClient);
99     } else {
100       mIsThreadIdle = true;
101     }
102   }
103 #endif  // CHRE_THREAD_UTIL_ENABLED
104 
105   if (numPendingEvents == 0 && !slpiInUImage()) {
106     voteBigImage(false /* bigImage */);
107   }
108 }
109 
hostIsAwake()110 bool PowerControlManager::hostIsAwake() {
111   return mHostIsAwake;
112 }
113 
114 }  // namespace chre
115