Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52dfaadf authored by Matt Buckley's avatar Matt Buckley
Browse files

Update SF to use createHintSessionWithConfig safely

This patch updates SurfaceFlinger to use the createHintSessionWithConfig
method to create its session, allowing for SF session tagging and
additional metadata to be passed back to SF such as session ID.

This change is gated by the android::os::adpf_use_fmq_channel flag.

Bug: 318517387
Test: atest libpowermanager_test libsurfaceflinger_unittest libcompositionengine_test
Change-Id: Ib22db0c1c51fbcfdb088dbc9b3e501f09cbf1008
parent f5d03c19
Loading
Loading
Loading
Loading
+29 −5
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ namespace impl {
using aidl::android::hardware::power::Boost;
using aidl::android::hardware::power::Mode;
using aidl::android::hardware::power::SessionHint;
using aidl::android::hardware::power::SessionTag;
using aidl::android::hardware::power::WorkDuration;

PowerAdvisor::~PowerAdvisor() = default;
@@ -204,15 +205,38 @@ bool PowerAdvisor::supportsPowerHintSession() {
    return *mSupportsHintSession;
}

bool PowerAdvisor::shouldCreateSessionWithConfig() {
    return mSessionConfigSupported && FlagManager::getInstance().adpf_use_fmq_channel();
}

bool PowerAdvisor::ensurePowerHintSessionRunning() {
    if (mHintSession == nullptr && !mHintSessionThreadIds.empty() && usePowerHintSession()) {
        if (shouldCreateSessionWithConfig()) {
            auto ret = getPowerHal().createHintSessionWithConfig(getpid(),
                                                                 static_cast<int32_t>(getuid()),
                                                                 mHintSessionThreadIds,
                                                                 mTargetDuration.ns(),
                                                                 SessionTag::SURFACEFLINGER,
                                                                 &mSessionConfig);
            if (ret.isOk()) {
                mHintSession = ret.value();
            }
            // If it fails the first time we try, or ever returns unsupported, assume unsupported
            else if (mFirstConfigSupportCheck || ret.isUnsupported()) {
                ALOGI("Hint session with config is unsupported, falling back to a legacy session");
                mSessionConfigSupported = false;
            }
            mFirstConfigSupportCheck = false;
        }
        // Immediately try original method after, in case the first way returned unsupported
        if (mHintSession == nullptr && !shouldCreateSessionWithConfig()) {
            auto ret = getPowerHal().createHintSession(getpid(), static_cast<int32_t>(getuid()),
                                                       mHintSessionThreadIds, mTargetDuration.ns());

            if (ret.isOk()) {
                mHintSession = ret.value();
            }
        }
    }
    return mHintSession != nullptr;
}

+10 −0
Original line number Diff line number Diff line
@@ -230,6 +230,9 @@ private:
    // this normalizes them together and takes the max of the two
    Duration combineTimingEstimates(Duration totalDuration, Duration flingerDuration);

    // Whether to use the new "createHintSessionWithConfig" method
    bool shouldCreateSessionWithConfig() REQUIRES(mHintSessionMutex);

    bool ensurePowerHintSessionRunning() REQUIRES(mHintSessionMutex);
    std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData;

@@ -278,6 +281,13 @@ private:
    std::promise<bool> mDelayReportActualMutexAcquisitonPromise;
    bool mTimingTestingMode = false;

    // Hint session configuration data
    aidl::android::hardware::power::SessionConfig mSessionConfig;

    // Whether createHintSessionWithConfig is supported, assume true until it fails
    bool mSessionConfigSupported = true;
    bool mFirstConfigSupportCheck = true;

    // Whether we should emit ATRACE_INT data for hint sessions
    static const bool sTraceHintSessionData;

+4 −1
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@

#include <common/FlagManager.h>

#define SET_FLAG_FOR_TEST(name, value) TestFlagSetter _testflag_((name), (name), (value))
#define SET_FLAG_FOR_TEST(name, value) \
    TestFlagSetter _testflag_ {        \
        (name), (name), (value)        \
    }

namespace android {
class TestFlagSetter {
+24 −8
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@
#define LOG_TAG "PowerAdvisorTest"

#include <DisplayHardware/PowerAdvisor.h>
#include <android_os.h>
#include <binder/Status.h>
#include <common/test/FlagUtils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <powermanager/PowerHalWrapper.h>
@@ -55,6 +57,7 @@ protected:
    std::unique_ptr<PowerAdvisor> mPowerAdvisor;
    MockPowerHalController* mMockPowerHalController;
    std::shared_ptr<MockPowerHintSessionWrapper> mMockPowerHintSession;
    SET_FLAG_FOR_TEST(android::os::adpf_use_fmq_channel, true);
};

bool PowerAdvisorTest::sessionExists() {
@@ -75,13 +78,14 @@ void PowerAdvisorTest::SetUp() {
void PowerAdvisorTest::startPowerHintSession(bool returnValidSession) {
    mMockPowerHintSession = std::make_shared<NiceMock<MockPowerHintSessionWrapper>>();
    if (returnValidSession) {
        ON_CALL(*mMockPowerHalController, createHintSession)
                .WillByDefault([&](int32_t, int32_t, const std::vector<int32_t>&, int64_t) {
                    return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
                            fromStatus(ndk::ScopedAStatus::ok(), mMockPowerHintSession);
                });
        ON_CALL(*mMockPowerHalController, createHintSessionWithConfig)
                .WillByDefault(DoAll(SetArgPointee<5>(aidl::android::hardware::power::SessionConfig{
                                             .id = 12}),
                                     Return(HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
                                                    fromStatus(binder::Status::ok(),
                                                               mMockPowerHintSession))));
    } else {
        ON_CALL(*mMockPowerHalController, createHintSession).WillByDefault([] {
        ON_CALL(*mMockPowerHalController, createHintSessionWithConfig).WillByDefault([] {
            return HalResult<
                    std::shared_ptr<PowerHintSessionWrapper>>::fromStatus(ndk::ScopedAStatus::ok(),
                                                                          nullptr);
@@ -287,7 +291,7 @@ TEST_F(PowerAdvisorTest, hintSessionValidWhenNullFromPowerHAL) {
}

TEST_F(PowerAdvisorTest, hintSessionOnlyCreatedOnce) {
    EXPECT_CALL(*mMockPowerHalController, createHintSession(_, _, _, _)).Times(1);
    EXPECT_CALL(*mMockPowerHalController, createHintSessionWithConfig(_, _, _, _, _, _)).Times(1);
    mPowerAdvisor->onBootFinished();
    startPowerHintSession();
    mPowerAdvisor->startPowerHintSession({1, 2, 3});
@@ -339,7 +343,7 @@ TEST_F(PowerAdvisorTest, hintSessionTestNotifyReportRace) {
        return HalResult<void>::fromStatus(ndk::ScopedAStatus::fromExceptionCode(-127));
    });

    ON_CALL(*mMockPowerHalController, createHintSession).WillByDefault([] {
    ON_CALL(*mMockPowerHalController, createHintSessionWithConfig).WillByDefault([] {
        return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
                fromStatus(ndk::ScopedAStatus::fromExceptionCode(-127), nullptr);
    });
@@ -374,5 +378,17 @@ TEST_F(PowerAdvisorTest, hintSessionTestNotifyReportRace) {
    EXPECT_EQ(sessionExists(), false);
}

TEST_F(PowerAdvisorTest, legacyHintSessionCreationStillWorks) {
    SET_FLAG_FOR_TEST(android::os::adpf_use_fmq_channel, false);
    mPowerAdvisor->onBootFinished();
    mMockPowerHintSession = std::make_shared<NiceMock<MockPowerHintSessionWrapper>>();
    EXPECT_CALL(*mMockPowerHalController, createHintSession)
            .Times(1)
            .WillOnce(Return(HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
                                     fromStatus(binder::Status::ok(), mMockPowerHintSession)));
    mPowerAdvisor->enablePowerHintSession(true);
    mPowerAdvisor->startPowerHintSession({1, 2, 3});
}

} // namespace
} // namespace android::Hwc2::impl