Skip to content

Commit 18b9352

Browse files
committed
Move ADPF to standalone lib
Refactor the PowerAdvisor into its own independent library to better reflect ownership and team structure, as well as making it easier to expand and test, as the previous placement under "DisplayHardware" was confusing. Test: libpoweradvisor_test Test: libsurfaceflinger_unittest Bug: 369692284 Flag: EXEMPT refactor Change-Id: I51388d0143b7e78413995d9c3a9bde69ae43eff2
1 parent 7d24e7e commit 18b9352

25 files changed

Lines changed: 119 additions & 302 deletions

services/surfaceflinger/Android.bp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ cc_library_headers {
177177
filegroup {
178178
name: "libsurfaceflinger_backend_sources",
179179
srcs: [
180+
"PowerAdvisor/*.cpp",
180181
"DisplayHardware/AidlComposerHal.cpp",
181182
"DisplayHardware/ComposerHal.cpp",
182183
"DisplayHardware/FramebufferSurface.cpp",
183184
"DisplayHardware/HWC2.cpp",
184185
"DisplayHardware/HWComposer.cpp",
185186
"DisplayHardware/HidlComposerHal.cpp",
186-
"DisplayHardware/PowerAdvisor.cpp",
187187
"DisplayHardware/VirtualDisplaySurface.cpp",
188188
],
189189
}

services/surfaceflinger/CompositionEngine/include/compositionengine/DisplayCreationArgs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <ui/Size.h>
2525
#include <ui/StaticDisplayInfo.h>
2626

27-
#include "DisplayHardware/PowerAdvisor.h"
27+
#include "PowerAdvisor/PowerAdvisor.h"
2828

2929
namespace android::compositionengine {
3030

@@ -48,7 +48,7 @@ struct DisplayCreationArgs {
4848

4949
// Optional pointer to the power advisor interface, if one is needed for
5050
// this display.
51-
Hwc2::PowerAdvisor* powerAdvisor = nullptr;
51+
adpf::PowerAdvisor* powerAdvisor = nullptr;
5252

5353
// Debugging. Human readable name for the display.
5454
std::string name;
@@ -82,7 +82,7 @@ class DisplayCreationArgsBuilder {
8282
return *this;
8383
}
8484

85-
DisplayCreationArgsBuilder& setPowerAdvisor(Hwc2::PowerAdvisor* powerAdvisor) {
85+
DisplayCreationArgsBuilder& setPowerAdvisor(adpf::PowerAdvisor* powerAdvisor) {
8686
mArgs.powerAdvisor = powerAdvisor;
8787
return *this;
8888
}

services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Display.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <ui/DisplayIdentification.h>
3131

3232
#include "DisplayHardware/HWComposer.h"
33-
#include "DisplayHardware/PowerAdvisor.h"
33+
#include "PowerAdvisor/PowerAdvisor.h"
3434

3535
namespace android::compositionengine {
3636

@@ -102,7 +102,7 @@ class Display : public compositionengine::impl::Output, public virtual compositi
102102
void setHintSessionRequiresRenderEngine(bool requiresRenderEngine) override;
103103
DisplayId mId;
104104
bool mIsDisconnected = false;
105-
Hwc2::PowerAdvisor* mPowerAdvisor = nullptr;
105+
adpf::PowerAdvisor* mPowerAdvisor = nullptr;
106106
const aidl::android::hardware::graphics::composer3::OverlayProperties* getOverlaySupport()
107107
override;
108108
};

services/surfaceflinger/CompositionEngine/src/Display.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// TODO(b/129481165): remove the #pragma below and fix conversion issues
3737
#pragma clang diagnostic pop // ignored "-Wconversion"
3838

39-
#include "DisplayHardware/PowerAdvisor.h"
39+
#include "PowerAdvisor/PowerAdvisor.h"
4040

4141
using aidl::android::hardware::graphics::composer3::Capability;
4242
using aidl::android::hardware::graphics::composer3::DisplayCapability;

services/surfaceflinger/CompositionEngine/tests/DisplayTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "ftl/future.h"
4040
#include "mock/DisplayHardware/MockHWC2.h"
4141
#include "mock/DisplayHardware/MockHWComposer.h"
42-
#include "mock/DisplayHardware/MockPowerAdvisor.h"
42+
#include "mock/PowerAdvisor/MockPowerAdvisor.h"
4343

4444
#include <aidl/android/hardware/graphics/composer3/Composition.h>
4545

@@ -192,7 +192,7 @@ struct DisplayTestCommon : public testing::Test {
192192
}
193193

194194
StrictMock<android::mock::HWComposer> mHwComposer;
195-
StrictMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
195+
StrictMock<adpf::mock::PowerAdvisor> mPowerAdvisor;
196196
StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
197197
StrictMock<mock::CompositionEngine> mCompositionEngine;
198198
sp<mock::NativeWindow> mNativeWindow = sp<StrictMock<mock::NativeWindow>>::make();
@@ -1035,7 +1035,7 @@ struct DisplayFunctionalTest : public testing::Test {
10351035
}
10361036

10371037
NiceMock<android::mock::HWComposer> mHwComposer;
1038-
NiceMock<Hwc2::mock::PowerAdvisor> mPowerAdvisor;
1038+
NiceMock<adpf::mock::PowerAdvisor> mPowerAdvisor;
10391039
NiceMock<mock::CompositionEngine> mCompositionEngine;
10401040
sp<mock::NativeWindow> mNativeWindow = sp<NiceMock<mock::NativeWindow>>::make();
10411041
sp<mock::DisplaySurface> mDisplaySurface = sp<NiceMock<mock::DisplaySurface>>::make();

services/surfaceflinger/DisplayDevice.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
#include "DisplayHardware/DisplayMode.h"
4444
#include "DisplayHardware/Hal.h"
45-
#include "DisplayHardware/PowerAdvisor.h"
4645
#include "FrontEnd/DisplayInfo.h"
4746
#include "Scheduler/RefreshRateSelector.h"
4847
#include "ThreadContext.h"

services/surfaceflinger/DisplayHardware/PowerAdvisor.cpp renamed to services/surfaceflinger/PowerAdvisor/PowerAdvisor.cpp

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
//#define LOG_NDEBUG 0
17+
// #define LOG_NDEBUG 0
1818

1919
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
2020

@@ -24,6 +24,7 @@
2424
#include <unistd.h>
2525
#include <cinttypes>
2626
#include <cstdint>
27+
#include <functional>
2728
#include <optional>
2829

2930
#include <android-base/properties.h>
@@ -33,45 +34,29 @@
3334

3435
#include <binder/IServiceManager.h>
3536

36-
#include "../SurfaceFlingerProperties.h"
37+
#pragma clang diagnostic push
38+
#pragma clang diagnostic ignored "-Wconversion"
39+
#include <powermanager/PowerHalController.h>
40+
#include <powermanager/PowerHintSessionWrapper.h>
41+
#pragma clang diagnostic pop
3742

43+
#include <common/FlagManager.h>
3844
#include "PowerAdvisor.h"
39-
#include "SurfaceFlinger.h"
4045

41-
namespace android {
42-
namespace Hwc2 {
46+
namespace hal = aidl::android::hardware::power;
4347

44-
PowerAdvisor::~PowerAdvisor() = default;
45-
46-
namespace impl {
47-
48-
using aidl::android::hardware::power::Boost;
49-
using aidl::android::hardware::power::ChannelConfig;
50-
using aidl::android::hardware::power::Mode;
51-
using aidl::android::hardware::power::SessionHint;
52-
using aidl::android::hardware::power::SessionTag;
53-
using aidl::android::hardware::power::WorkDuration;
54-
using aidl::android::hardware::power::WorkDurationFixedV1;
48+
namespace android::adpf::impl {
5549

56-
using aidl::android::hardware::common::fmq::MQDescriptor;
5750
using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
58-
using aidl::android::hardware::power::ChannelMessage;
5951
using android::hardware::EventFlag;
6052

61-
using ChannelMessageContents = ChannelMessage::ChannelMessageContents;
62-
using MsgQueue = android::AidlMessageQueue<ChannelMessage, SynchronizedReadWrite>;
53+
using ChannelMessageContents = hal::ChannelMessage::ChannelMessageContents;
54+
using MsgQueue = android::AidlMessageQueue<hal::ChannelMessage, SynchronizedReadWrite>;
6355
using FlagQueue = android::AidlMessageQueue<int8_t, SynchronizedReadWrite>;
6456

6557
PowerAdvisor::~PowerAdvisor() = default;
6658

6759
namespace {
68-
std::chrono::milliseconds getUpdateTimeout() {
69-
// Default to a timeout of 80ms if nothing else is specified
70-
static std::chrono::milliseconds timeout =
71-
std::chrono::milliseconds(sysprop::display_update_imminent_timeout_ms(80));
72-
return timeout;
73-
}
74-
7560
void traceExpensiveRendering(bool enabled) {
7661
if (enabled) {
7762
SFTRACE_ASYNC_BEGIN("ExpensiveRendering", 0);
@@ -82,28 +67,30 @@ void traceExpensiveRendering(bool enabled) {
8267

8368
} // namespace
8469

85-
PowerAdvisor::PowerAdvisor(SurfaceFlinger& flinger)
86-
: mPowerHal(std::make_unique<power::PowerHalController>()), mFlinger(flinger) {
87-
if (getUpdateTimeout() > 0ms) {
88-
mScreenUpdateTimer.emplace("UpdateImminentTimer", getUpdateTimeout(),
70+
PowerAdvisor::PowerAdvisor(std::function<void()>&& sfDisableExpensiveFn,
71+
std::chrono::milliseconds timeout)
72+
: mPowerHal(std::make_unique<power::PowerHalController>()) {
73+
if (timeout > 0ms) {
74+
mScreenUpdateTimer.emplace("UpdateImminentTimer", timeout,
8975
/* resetCallback */ nullptr,
9076
/* timeoutCallback */
91-
[this] {
77+
[this, disableExpensiveFn = std::move(sfDisableExpensiveFn),
78+
timeout] {
9279
while (true) {
9380
auto timeSinceLastUpdate = std::chrono::nanoseconds(
9481
systemTime() - mLastScreenUpdatedTime.load());
95-
if (timeSinceLastUpdate >= getUpdateTimeout()) {
82+
if (timeSinceLastUpdate >= timeout) {
9683
break;
9784
}
9885
// We may try to disable expensive rendering and allow
9986
// for sending DISPLAY_UPDATE_IMMINENT hints too early if
10087
// we idled very shortly after updating the screen, so
10188
// make sure we wait enough time.
102-
std::this_thread::sleep_for(getUpdateTimeout() -
89+
std::this_thread::sleep_for(timeout -
10390
timeSinceLastUpdate);
10491
}
10592
mSendUpdateImminent.store(true);
106-
mFlinger.disableExpensiveRendering();
93+
disableExpensiveFn();
10794
});
10895
}
10996
}
@@ -132,7 +119,7 @@ void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expec
132119

133120
const bool expectsExpensiveRendering = !mExpensiveDisplays.empty();
134121
if (mNotifiedExpensiveRendering != expectsExpensiveRendering) {
135-
auto ret = getPowerHal().setMode(Mode::EXPENSIVE_RENDERING, expectsExpensiveRendering);
122+
auto ret = getPowerHal().setMode(hal::Mode::EXPENSIVE_RENDERING, expectsExpensiveRendering);
136123
if (!ret.isOk()) {
137124
if (ret.isUnsupported()) {
138125
mHasExpensiveRendering = false;
@@ -151,7 +138,7 @@ void PowerAdvisor::notifyCpuLoadUp() {
151138
if (!mBootFinished.load()) {
152139
return;
153140
}
154-
sendHintSessionHint(SessionHint::CPU_LOAD_UP);
141+
sendHintSessionHint(hal::SessionHint::CPU_LOAD_UP);
155142
}
156143

157144
void PowerAdvisor::notifyDisplayUpdateImminentAndCpuReset() {
@@ -163,12 +150,12 @@ void PowerAdvisor::notifyDisplayUpdateImminentAndCpuReset() {
163150

164151
if (mSendUpdateImminent.exchange(false)) {
165152
ALOGV("AIDL notifyDisplayUpdateImminentAndCpuReset");
166-
sendHintSessionHint(SessionHint::CPU_LOAD_RESET);
153+
sendHintSessionHint(hal::SessionHint::CPU_LOAD_RESET);
167154

168155
if (!mHasDisplayUpdateImminent) {
169156
ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it");
170157
} else {
171-
auto ret = getPowerHal().setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0);
158+
auto ret = getPowerHal().setBoost(hal::Boost::DISPLAY_UPDATE_IMMINENT, 0);
172159
if (ret.isUnsupported()) {
173160
mHasDisplayUpdateImminent = false;
174161
}
@@ -205,7 +192,7 @@ bool PowerAdvisor::shouldCreateSessionWithConfig() {
205192
FlagManager::getInstance().adpf_use_fmq_channel();
206193
}
207194

208-
void PowerAdvisor::sendHintSessionHint(SessionHint hint) {
195+
void PowerAdvisor::sendHintSessionHint(hal::SessionHint hint) {
209196
if (!mBootFinished || !usePowerHintSession()) {
210197
ALOGV("Power hint session is not enabled, skip sending session hint");
211198
return;
@@ -236,7 +223,7 @@ bool PowerAdvisor::ensurePowerHintSessionRunning() {
236223
static_cast<int32_t>(getuid()),
237224
mHintSessionThreadIds,
238225
mTargetDuration.ns(),
239-
SessionTag::SURFACEFLINGER,
226+
hal::SessionTag::SURFACEFLINGER,
240227
&mSessionConfig);
241228
if (ret.isOk()) {
242229
mHintSession = ret.value();
@@ -326,7 +313,7 @@ void PowerAdvisor::reportActualWorkDuration() {
326313
return;
327314
}
328315
SFTRACE_CALL();
329-
std::optional<WorkDuration> actualDuration = estimateWorkDuration();
316+
std::optional<hal::WorkDuration> actualDuration = estimateWorkDuration();
330317
if (!actualDuration.has_value() || actualDuration->durationNanos < 0) {
331318
ALOGV("Failed to send actual work duration, skipping");
332319
return;
@@ -377,7 +364,7 @@ void PowerAdvisor::reportActualWorkDuration() {
377364
mHintSessionQueue.clear();
378365
}
379366

380-
template <ChannelMessage::ChannelMessageContents::Tag T, class In>
367+
template <hal::ChannelMessage::ChannelMessageContents::Tag T, class In>
381368
bool PowerAdvisor::writeHintSessionMessage(In* contents, size_t count) {
382369
if (!mMsgQueue) {
383370
ALOGV("Skip using FMQ with message tag %hhd as it's not supported", T);
@@ -395,21 +382,21 @@ bool PowerAdvisor::writeHintSessionMessage(In* contents, size_t count) {
395382
}
396383
for (size_t i = 0; i < count; ++i) {
397384
if constexpr (T == ChannelMessageContents::Tag::workDuration) {
398-
const WorkDuration& duration = contents[i];
399-
new (tx.getSlot(i)) ChannelMessage{
385+
const hal::WorkDuration& duration = contents[i];
386+
new (tx.getSlot(i)) hal::ChannelMessage{
400387
.sessionID = static_cast<int32_t>(mSessionConfig.id),
401388
.timeStampNanos =
402389
(i == count - 1) ? ::android::uptimeNanos() : duration.timeStampNanos,
403390
.data = ChannelMessageContents::make<ChannelMessageContents::Tag::workDuration,
404-
WorkDurationFixedV1>({
391+
hal::WorkDurationFixedV1>({
405392
.durationNanos = duration.durationNanos,
406393
.workPeriodStartTimestampNanos = duration.workPeriodStartTimestampNanos,
407394
.cpuDurationNanos = duration.cpuDurationNanos,
408395
.gpuDurationNanos = duration.gpuDurationNanos,
409396
}),
410397
};
411398
} else {
412-
new (tx.getSlot(i)) ChannelMessage{
399+
new (tx.getSlot(i)) hal::ChannelMessage{
413400
.sessionID = static_cast<int32_t>(mSessionConfig.id),
414401
.timeStampNanos = ::android::uptimeNanos(),
415402
.data = ChannelMessageContents::make<T, In>(std::move(contents[i])),
@@ -572,7 +559,7 @@ std::vector<DisplayId> PowerAdvisor::getOrderedDisplayIds(
572559
return sortedDisplays;
573560
}
574561

575-
std::optional<WorkDuration> PowerAdvisor::estimateWorkDuration() {
562+
std::optional<hal::WorkDuration> PowerAdvisor::estimateWorkDuration() {
576563
if (!mExpectedPresentTimes.isFull() || !mCommitStartTimes.isFull()) {
577564
return std::nullopt;
578565
}
@@ -657,7 +644,7 @@ std::optional<WorkDuration> PowerAdvisor::estimateWorkDuration() {
657644
Duration combinedDuration = combineTimingEstimates(totalDuration, flingerDuration);
658645
Duration cpuDuration = combineTimingEstimates(totalDurationWithoutGpu, flingerDuration);
659646

660-
WorkDuration duration{
647+
hal::WorkDuration duration{
661648
.timeStampNanos = TimePoint::now().ns(),
662649
.durationNanos = combinedDuration.ns(),
663650
.workPeriodStartTimestampNanos = mCommitStartTimes[0].ns(),
@@ -760,6 +747,4 @@ power::PowerHalController& PowerAdvisor::getPowerHal() {
760747
return *mPowerHal;
761748
}
762749

763-
} // namespace impl
764-
} // namespace Hwc2
765-
} // namespace android
750+
} // namespace android::adpf::impl

0 commit comments

Comments
 (0)