blob: 28d28f41ebfa18454288bcc90eab38b0bb9aae72 [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>
Matt Buckleyef51fba2021-10-12 19:30:12 +000020#include <chrono>
Peiyong Lin74ca2f42019-01-14 19:36:57 -080021#include <unordered_set>
22
Dan Stoza20950002020-06-18 14:56:58 -070023#include <utils/Mutex.h>
24
Dan Stoza030fbc12020-02-19 15:32:01 -080025#include "../Scheduler/OneShotTimer.h"
Peiyong Lin74ca2f42019-01-14 19:36:57 -080026#include "DisplayIdentification.h"
Michael Wright5d22d4f2018-06-21 02:50:34 +010027
Matt Buckleyef51fba2021-10-12 19:30:12 +000028using namespace std::chrono_literals;
29
Michael Wright5d22d4f2018-06-21 02:50:34 +010030namespace android {
Alec Mouridea1ac52021-06-23 18:12:18 -070031
32class SurfaceFlinger;
33
Michael Wright5d22d4f2018-06-21 02:50:34 +010034namespace Hwc2 {
35
36class PowerAdvisor {
37public:
38 virtual ~PowerAdvisor();
39
Alec Mouridea1ac52021-06-23 18:12:18 -070040 // Initializes resources that cannot be initialized on construction
41 virtual void init() = 0;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070042 virtual void onBootFinished() = 0;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080043 virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
Alec Mouridea1ac52021-06-23 18:12:18 -070044 virtual bool isUsingExpensiveRendering() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -080045 virtual void notifyDisplayUpdateImminent() = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +000046 virtual bool usePowerHintSession() = 0;
47 virtual bool supportsPowerHintSession() = 0;
48 virtual bool isPowerHintSessionRunning() = 0;
49 virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +000050 virtual void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) = 0;
51 virtual void enablePowerHint(bool enabled) = 0;
Matt Buckleyef51fba2021-10-12 19:30:12 +000052 virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0;
Michael Wright5d22d4f2018-06-21 02:50:34 +010053};
54
55namespace impl {
56
Michael Wright5d22d4f2018-06-21 02:50:34 +010057// PowerAdvisor is a wrapper around IPower HAL which takes into account the
58// full state of the system when sending out power hints to things like the GPU.
59class PowerAdvisor final : public Hwc2::PowerAdvisor {
60public:
Dan Stoza030fbc12020-02-19 15:32:01 -080061 class HalWrapper {
62 public:
63 virtual ~HalWrapper() = default;
64
65 virtual bool setExpensiveRendering(bool enabled) = 0;
66 virtual bool notifyDisplayUpdateImminent() = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +000067 virtual bool supportsPowerHintSession() = 0;
68 virtual bool isPowerHintSessionRunning() = 0;
69 virtual void restartPowerHintSession() = 0;
70 virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0;
71 virtual bool startPowerHintSession() = 0;
72 virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0;
73 virtual void sendActualWorkDuration(int64_t actualDurationNanos,
74 nsecs_t timeStampNanos) = 0;
75 virtual bool shouldReconnectHAL() = 0;
76 virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0;
77 virtual std::optional<int64_t> getTargetWorkDuration() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -080078 };
79
Alec Mouridea1ac52021-06-23 18:12:18 -070080 PowerAdvisor(SurfaceFlinger& flinger);
Michael Wright5d22d4f2018-06-21 02:50:34 +010081 ~PowerAdvisor() override;
82
Alec Mouridea1ac52021-06-23 18:12:18 -070083 void init() override;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070084 void onBootFinished() override;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080085 void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
Matt Buckley06f299a2021-09-24 19:43:51 +000086 bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; };
Dan Stoza030fbc12020-02-19 15:32:01 -080087 void notifyDisplayUpdateImminent() override;
Matt Buckley06f299a2021-09-24 19:43:51 +000088 bool usePowerHintSession() override;
89 bool supportsPowerHintSession() override;
90 bool isPowerHintSessionRunning() override;
91 void setTargetWorkDuration(int64_t targetDurationNanos) override;
Matt Buckley06f299a2021-09-24 19:43:51 +000092 void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) override;
93 void enablePowerHint(bool enabled) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +000094 bool startPowerHintSession(const std::vector<int32_t>& threadIds) override;
Michael Wright5d22d4f2018-06-21 02:50:34 +010095
96private:
Dan Stoza20950002020-06-18 14:56:58 -070097 HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex);
98 bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false;
99 std::mutex mPowerHalMutex;
Dan Stoza030fbc12020-02-19 15:32:01 -0800100
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700101 std::atomic_bool mBootFinished = false;
Matt Buckley06f299a2021-09-24 19:43:51 +0000102 std::optional<bool> mPowerHintEnabled;
103 std::optional<bool> mSupportsPowerHint;
104 bool mPowerHintSessionRunning = false;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100105
Matt Buckleyef51fba2021-10-12 19:30:12 +0000106 // An adjustable safety margin which moves the "target" earlier to allow flinger to
107 // go a bit over without dropping a frame, especially since we can't measure
108 // the exact time HWC finishes composition so "actual" durations are measured
109 // from the end of present() instead, which is a bit later.
110 static constexpr const std::chrono::nanoseconds kTargetSafetyMargin = 2ms;
111
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800112 std::unordered_set<DisplayId> mExpensiveDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100113 bool mNotifiedExpensiveRendering = false;
Dan Stoza030fbc12020-02-19 15:32:01 -0800114
Alec Mouridea1ac52021-06-23 18:12:18 -0700115 SurfaceFlinger& mFlinger;
116 const bool mUseScreenUpdateTimer;
Dan Stoza030fbc12020-02-19 15:32:01 -0800117 std::atomic_bool mSendUpdateImminent = true;
Alec Mouridea1ac52021-06-23 18:12:18 -0700118 scheduler::OneShotTimer mScreenUpdateTimer;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100119};
120
121} // namespace impl
122} // namespace Hwc2
123} // namespace android