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 namespace chre {
25
PowerControlManagerBase()26 PowerControlManagerBase::PowerControlManagerBase() {
27 #ifdef CHRE_THREAD_UTIL_ENABLED
28 sns_client_create_thread_utilization_client(&mThreadUtilClient);
29 #endif // CHRE_THREAD_UTIL_ENABLED
30 }
31
~PowerControlManagerBase()32 PowerControlManagerBase::~PowerControlManagerBase() {
33 #ifdef CHRE_THREAD_UTIL_ENABLED
34 sns_client_remove_thread_utilization_client(mThreadUtilClient);
35 #endif // CHRE_THREAD_UTIL_ENABLED
36 }
37
voteBigImage(bool bigImage)38 bool PowerControlManagerBase::voteBigImage(bool bigImage) {
39 return IslandVoteClientSingleton::get()->voteBigImage(bigImage);
40 }
41
onHostWakeSuspendEvent(bool awake)42 void PowerControlManagerBase::onHostWakeSuspendEvent(bool awake) {
43 if (mHostIsAwake != awake) {
44 mHostIsAwake = awake;
45
46 EventLoopManagerSingleton::get()->getEventLoop().postEvent(
47 mHostIsAwake ? CHRE_EVENT_HOST_AWAKE : CHRE_EVENT_HOST_ASLEEP,
48 nullptr /* eventData */, nullptr /* freeCallback */);
49
50 #ifdef CHRE_AUDIO_SUPPORT_ENABLED
51 if (awake) {
52 auto callback = [](uint16_t /* eventType */, void * /* eventData*/) {
53 EventLoopManagerSingleton::get()->getAudioRequestManager()
54 .getPlatformAudio().onHostAwake();
55 };
56
57 EventLoopManagerSingleton::get()->deferCallback(
58 SystemCallbackType::AudioHandleHostAwake, nullptr, callback);
59 }
60 #endif // CHRE_AUDIO_SUPPORT_ENABLED
61 }
62 }
63
postEventLoopProcess(size_t numPendingEvents)64 void PowerControlManager::postEventLoopProcess(size_t numPendingEvents) {
65 #ifdef CHRE_THREAD_UTIL_ENABLED
66 // Although this execution point does not actually represent the start
67 // of the CHRE thread's activity, we only care about cases where the
68 // CHRE's event queue is highly backlogged for voting higher clock rates.
69 if (mIsThreadIdle && numPendingEvents != 0) {
70 sns_client_thread_utilization_start(mThreadUtilClient);
71 mIsThreadIdle = false;
72 } else if (!mIsThreadIdle) {
73 // Update the time profile as frequently as possible so that clock updates
74 // are not deferred until all events are processed.
75 sns_client_thread_utilization_stop(mThreadUtilClient);
76 if (numPendingEvents != 0) {
77 sns_client_thread_utilization_start(mThreadUtilClient);
78 } else {
79 mIsThreadIdle = true;
80 }
81 }
82 #endif // CHRE_THREAD_UTIL_ENABLED
83
84 if (numPendingEvents == 0 && !slpiInUImage()) {
85 voteBigImage(false /* bigImage */);
86 }
87 }
88
hostIsAwake()89 bool PowerControlManager::hostIsAwake() {
90 return mHostIsAwake;
91 }
92
93 } // namespace chre
94