1 /*
2  * Copyright 2018 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 #pragma once
18 
19 #include <atomic>
20 #include <unordered_set>
21 
22 #include <utils/Mutex.h>
23 
24 #include "../Scheduler/OneShotTimer.h"
25 #include "DisplayIdentification.h"
26 
27 namespace android {
28 namespace Hwc2 {
29 
30 class PowerAdvisor {
31 public:
32     virtual ~PowerAdvisor();
33 
34     virtual void onBootFinished() = 0;
35     virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
36     virtual void notifyDisplayUpdateImminent() = 0;
37 };
38 
39 namespace impl {
40 
41 // PowerAdvisor is a wrapper around IPower HAL which takes into account the
42 // full state of the system when sending out power hints to things like the GPU.
43 class PowerAdvisor final : public Hwc2::PowerAdvisor {
44 public:
45     class HalWrapper {
46     public:
47         virtual ~HalWrapper() = default;
48 
49         virtual bool setExpensiveRendering(bool enabled) = 0;
50         virtual bool notifyDisplayUpdateImminent() = 0;
51     };
52 
53     PowerAdvisor();
54     ~PowerAdvisor() override;
55 
56     void onBootFinished() override;
57     void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
58     void notifyDisplayUpdateImminent() override;
59 
60 private:
61     HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex);
62     bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false;
63     std::mutex mPowerHalMutex;
64 
65     std::atomic_bool mBootFinished = false;
66 
67     std::unordered_set<DisplayId> mExpensiveDisplays;
68     bool mNotifiedExpensiveRendering = false;
69 
70     const bool mUseUpdateImminentTimer;
71     std::atomic_bool mSendUpdateImminent = true;
72     scheduler::OneShotTimer mUpdateImminentTimer;
73 };
74 
75 } // namespace impl
76 } // namespace Hwc2
77 } // namespace android
78