blob: b8fd17d654a1496599e79d6a3988b15b0a079267 [file] [log] [blame]
Michael Wright5d22d4f2018-06-21 02:50:34 +01001/*
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
Dan Stoza030fbc12020-02-19 15:32:01 -080019#include <atomic>
Peiyong Lin74ca2f42019-01-14 19:36:57 -080020#include <unordered_set>
21
Dan Stoza20950002020-06-18 14:56:58 -070022#include <utils/Mutex.h>
23
Dan Stoza030fbc12020-02-19 15:32:01 -080024#include "../Scheduler/OneShotTimer.h"
Peiyong Lin74ca2f42019-01-14 19:36:57 -080025#include "DisplayIdentification.h"
Michael Wright5d22d4f2018-06-21 02:50:34 +010026
27namespace android {
Alec Mouridea1ac52021-06-23 18:12:18 -070028
29class SurfaceFlinger;
30
Michael Wright5d22d4f2018-06-21 02:50:34 +010031namespace Hwc2 {
32
33class PowerAdvisor {
34public:
35 virtual ~PowerAdvisor();
36
Alec Mouridea1ac52021-06-23 18:12:18 -070037 // Initializes resources that cannot be initialized on construction
38 virtual void init() = 0;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070039 virtual void onBootFinished() = 0;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080040 virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
Alec Mouridea1ac52021-06-23 18:12:18 -070041 virtual bool isUsingExpensiveRendering() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -080042 virtual void notifyDisplayUpdateImminent() = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +000043 virtual bool usePowerHintSession() = 0;
44 virtual bool supportsPowerHintSession() = 0;
45 virtual bool isPowerHintSessionRunning() = 0;
46 virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
47 virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0;
48 virtual void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) = 0;
49 virtual void enablePowerHint(bool enabled) = 0;
Michael Wright5d22d4f2018-06-21 02:50:34 +010050};
51
52namespace impl {
53
Michael Wright5d22d4f2018-06-21 02:50:34 +010054// PowerAdvisor is a wrapper around IPower HAL which takes into account the
55// full state of the system when sending out power hints to things like the GPU.
56class PowerAdvisor final : public Hwc2::PowerAdvisor {
57public:
Dan Stoza030fbc12020-02-19 15:32:01 -080058 class HalWrapper {
59 public:
60 virtual ~HalWrapper() = default;
61
62 virtual bool setExpensiveRendering(bool enabled) = 0;
63 virtual bool notifyDisplayUpdateImminent() = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +000064 virtual bool supportsPowerHintSession() = 0;
65 virtual bool isPowerHintSessionRunning() = 0;
66 virtual void restartPowerHintSession() = 0;
67 virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0;
68 virtual bool startPowerHintSession() = 0;
69 virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
70 virtual void sendActualWorkDuration(int64_t actualDurationNanos,
71 nsecs_t timeStampNanos) = 0;
72 virtual bool shouldReconnectHAL() = 0;
73 virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0;
74 virtual std::optional<int64_t> getTargetWorkDuration() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -080075 };
76
Alec Mouridea1ac52021-06-23 18:12:18 -070077 PowerAdvisor(SurfaceFlinger& flinger);
Michael Wright5d22d4f2018-06-21 02:50:34 +010078 ~PowerAdvisor() override;
79
Alec Mouridea1ac52021-06-23 18:12:18 -070080 void init() override;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070081 void onBootFinished() override;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080082 void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
Matt Buckley06f299a2021-09-24 19:43:51 +000083 bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; };
Dan Stoza030fbc12020-02-19 15:32:01 -080084 void notifyDisplayUpdateImminent() override;
Matt Buckley06f299a2021-09-24 19:43:51 +000085 bool usePowerHintSession() override;
86 bool supportsPowerHintSession() override;
87 bool isPowerHintSessionRunning() override;
88 void setTargetWorkDuration(int64_t targetDurationNanos) override;
89 void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override;
90 void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) override;
91 void enablePowerHint(bool enabled) override;
Michael Wright5d22d4f2018-06-21 02:50:34 +010092
93private:
Dan Stoza20950002020-06-18 14:56:58 -070094 HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex);
95 bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false;
96 std::mutex mPowerHalMutex;
Dan Stoza030fbc12020-02-19 15:32:01 -080097
Dan Stoza29e7bdf2020-03-23 14:43:09 -070098 std::atomic_bool mBootFinished = false;
Matt Buckley06f299a2021-09-24 19:43:51 +000099 std::optional<bool> mPowerHintEnabled;
100 std::optional<bool> mSupportsPowerHint;
101 bool mPowerHintSessionRunning = false;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100102
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800103 std::unordered_set<DisplayId> mExpensiveDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100104 bool mNotifiedExpensiveRendering = false;
Dan Stoza030fbc12020-02-19 15:32:01 -0800105
Alec Mouridea1ac52021-06-23 18:12:18 -0700106 SurfaceFlinger& mFlinger;
107 const bool mUseScreenUpdateTimer;
Dan Stoza030fbc12020-02-19 15:32:01 -0800108 std::atomic_bool mSendUpdateImminent = true;
Alec Mouridea1ac52021-06-23 18:12:18 -0700109 scheduler::OneShotTimer mScreenUpdateTimer;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100110};
111
112} // namespace impl
113} // namespace Hwc2
114} // namespace android