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

Commit bac7071a authored by Dennis Kiilerich's avatar Dennis Kiilerich
Browse files

Turn off synthetic VSYNC when adjusting thread scheduling for performance

* This CL explicitly sets synthetic VSYNC state based on the new power state. Before, the synthetic VSYNC state was assumed based on the previous power state and in some cases was not set explicitly.
* The previous behaviour of enabling synthetic VSYNC if the only display is DOZE_SUSPEND is preserved.
* This also fixes a hypothetical gap where it would not optimise for performance if there were a display that needed it, while the primary display was DOZE_SUSPEND.

Bug: 342681202

Bug: 241285876

Flag: android.companion.virtualdevice.flags.correct_virtual_display_power_state

Test: manually tested with flag on/off using Android Auto Projected

Change-Id: I181aefad586ad7b1732c837b67a01d61939d1d3e
parent 913c8f38
Loading
Loading
Loading
Loading
+0 −11
Original line number Original line Diff line number Diff line
@@ -50,17 +50,6 @@ namespace android {


namespace hal = hardware::graphics::composer::hal;
namespace hal = hardware::graphics::composer::hal;


namespace gui {
inline std::string_view to_string(ISurfaceComposer::OptimizationPolicy optimizationPolicy) {
    switch (optimizationPolicy) {
        case ISurfaceComposer::OptimizationPolicy::optimizeForPower:
            return "optimizeForPower";
        case ISurfaceComposer::OptimizationPolicy::optimizeForPerformance:
            return "optimizeForPerformance";
    }
}
} // namespace gui

DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
        const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
        const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
        std::shared_ptr<compositionengine::Display> compositionDisplay)
        std::shared_ptr<compositionengine::Display> compositionDisplay)
+11 −0
Original line number Original line Diff line number Diff line
@@ -67,6 +67,17 @@ namespace display {
class DisplaySnapshot;
class DisplaySnapshot;
} // namespace display
} // namespace display


namespace gui {
inline const char* to_string(ISurfaceComposer::OptimizationPolicy optimizationPolicy) {
    switch (optimizationPolicy) {
        case ISurfaceComposer::OptimizationPolicy::optimizeForPower:
            return "optimizeForPower";
        case ISurfaceComposer::OptimizationPolicy::optimizeForPerformance:
            return "optimizeForPerformance";
    }
}
} // namespace gui

class DisplayDevice : public RefBase {
class DisplayDevice : public RefBase {
public:
public:
    constexpr static float sDefaultMinLumiance = 0.0;
    constexpr static float sDefaultMinLumiance = 0.0;
+54 −45
Original line number Original line Diff line number Diff line
@@ -5719,7 +5719,13 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
        incRefreshableDisplays();
        incRefreshableDisplays();
    }
    }


    if (displayId == mActiveDisplayId &&
        FlagManager::getInstance().correct_virtual_display_power_state()) {
        applyOptimizationPolicy(__func__);
    }

    const auto activeMode = display->refreshRateSelector().getActiveMode().modePtr;
    const auto activeMode = display->refreshRateSelector().getActiveMode().modePtr;
    using OptimizationPolicy = gui::ISurfaceComposer::OptimizationPolicy;
    if (currentMode == hal::PowerMode::OFF) {
    if (currentMode == hal::PowerMode::OFF) {
        // Turn on the display
        // Turn on the display


@@ -5734,12 +5740,10 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
            onActiveDisplayChangedLocked(activeDisplay.get(), *display);
            onActiveDisplayChangedLocked(activeDisplay.get(), *display);
        }
        }


        if (displayId == mActiveDisplayId) {
        if (displayId == mActiveDisplayId &&
            if (FlagManager::getInstance().correct_virtual_display_power_state()) {
            !FlagManager::getInstance().correct_virtual_display_power_state()) {
                applyOptimizationPolicy("setPhysicalDisplayPowerMode(ON)");
            optimizeThreadScheduling("setPhysicalDisplayPowerMode(ON/DOZE)",
            } else {
                                     OptimizationPolicy::optimizeForPerformance);
                disablePowerOptimizations("setPhysicalDisplayPowerMode(ON)");
            }
        }
        }


        getHwComposer().setPowerMode(displayId, mode);
        getHwComposer().setPowerMode(displayId, mode);
@@ -5748,7 +5752,8 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
                    mScheduler->getVsyncSchedule(displayId)->getPendingHardwareVsyncState();
                    mScheduler->getVsyncSchedule(displayId)->getPendingHardwareVsyncState();
            requestHardwareVsync(displayId, enable);
            requestHardwareVsync(displayId, enable);


            if (displayId == mActiveDisplayId) {
            if (displayId == mActiveDisplayId &&
                !FlagManager::getInstance().correct_virtual_display_power_state()) {
                mScheduler->enableSyntheticVsync(false);
                mScheduler->enableSyntheticVsync(false);
            }
            }


@@ -5765,13 +5770,13 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
            if (const auto display = getActivatableDisplay()) {
            if (const auto display = getActivatableDisplay()) {
                onActiveDisplayChangedLocked(activeDisplay.get(), *display);
                onActiveDisplayChangedLocked(activeDisplay.get(), *display);
            } else {
            } else {
                if (FlagManager::getInstance().correct_virtual_display_power_state()) {
                if (!FlagManager::getInstance().correct_virtual_display_power_state()) {
                    applyOptimizationPolicy("setPhysicalDisplayPowerMode(OFF)");
                    optimizeThreadScheduling("setPhysicalDisplayPowerMode(OFF)",
                } else {
                                             OptimizationPolicy::optimizeForPower);
                    enablePowerOptimizations("setPhysicalDisplayPowerMode(OFF)");
                }
                }


                if (currentModeNotDozeSuspend) {
                if (currentModeNotDozeSuspend &&
                    !FlagManager::getInstance().correct_virtual_display_power_state()) {
                    mScheduler->enableSyntheticVsync();
                    mScheduler->enableSyntheticVsync();
                }
                }
            }
            }
@@ -5799,8 +5804,10 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
                ALOGI("Force repainting for DOZE_SUSPEND -> DOZE or ON.");
                ALOGI("Force repainting for DOZE_SUSPEND -> DOZE or ON.");
                mVisibleRegionsDirty = true;
                mVisibleRegionsDirty = true;
                scheduleRepaint();
                scheduleRepaint();
                if (!FlagManager::getInstance().correct_virtual_display_power_state()) {
                    mScheduler->enableSyntheticVsync(false);
                    mScheduler->enableSyntheticVsync(false);
                }
                }
            }
            constexpr bool kAllowToEnable = true;
            constexpr bool kAllowToEnable = true;
            mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get());
            mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, activeMode.get());
        }
        }
@@ -5809,7 +5816,8 @@ void SurfaceFlinger::setPhysicalDisplayPowerMode(const sp<DisplayDevice>& displa
        constexpr bool kDisallow = true;
        constexpr bool kDisallow = true;
        mScheduler->disableHardwareVsync(displayId, kDisallow);
        mScheduler->disableHardwareVsync(displayId, kDisallow);


        if (displayId == mActiveDisplayId) {
        if (displayId == mActiveDisplayId &&
            !FlagManager::getInstance().correct_virtual_display_power_state()) {
            mScheduler->enableSyntheticVsync();
            mScheduler->enableSyntheticVsync();
        }
        }
        getHwComposer().setPowerMode(displayId, mode);
        getHwComposer().setPowerMode(displayId, mode);
@@ -5848,43 +5856,44 @@ void SurfaceFlinger::setVirtualDisplayPowerMode(const sp<DisplayDevice>& display
          to_string(displayId).c_str());
          to_string(displayId).c_str());
}
}


bool SurfaceFlinger::shouldOptimizeForPerformance() {
void SurfaceFlinger::optimizeThreadScheduling(
    for (const auto& [_, display] : mDisplays) {
        const char* whence, gui::ISurfaceComposer::OptimizationPolicy optimizationPolicy) {
        // Displays that are optimized for power are always powered on and should not influence
    ALOGD("%s: Optimizing thread scheduling: %s", whence, to_string(optimizationPolicy));
        // whether there is an active display for the purpose of power optimization, etc. If these
        // displays are being shown somewhere, a different (physical or virtual) display that is
        // optimized for performance will be powered on in addition. Displays optimized for
        // performance will change power mode, so if they are off then they are not active.
        if (display->isPoweredOn() &&
            display->getOptimizationPolicy() ==
                    gui::ISurfaceComposer::OptimizationPolicy::optimizeForPerformance) {
            return true;
        }
    }
    return false;
}

void SurfaceFlinger::enablePowerOptimizations(const char* whence) {
    ALOGD("%s: Enabling power optimizations", whence);

    setSchedAttr(false, whence);
    setSchedFifo(false, whence);
}

void SurfaceFlinger::disablePowerOptimizations(const char* whence) {
    ALOGD("%s: Disabling power optimizations", whence);


    const bool optimizeForPerformance =
            optimizationPolicy == gui::ISurfaceComposer::OptimizationPolicy::optimizeForPerformance;
    // TODO: b/281692563 - Merge the syscalls. For now, keep uclamp in a separate syscall
    // TODO: b/281692563 - Merge the syscalls. For now, keep uclamp in a separate syscall
    // and set it before SCHED_FIFO due to b/190237315.
    // and set it before SCHED_FIFO due to b/190237315.
    setSchedAttr(true, whence);
    setSchedAttr(optimizeForPerformance, whence);
    setSchedFifo(true, whence);
    setSchedFifo(optimizeForPerformance, whence);
}
}


void SurfaceFlinger::applyOptimizationPolicy(const char* whence) {
void SurfaceFlinger::applyOptimizationPolicy(const char* whence) {
    if (shouldOptimizeForPerformance()) {
    using OptimizationPolicy = gui::ISurfaceComposer::OptimizationPolicy;
        disablePowerOptimizations(whence);

    } else {
    const bool optimizeForPerformance =
        enablePowerOptimizations(whence);
            std::any_of(mDisplays.begin(), mDisplays.end(), [](const auto& pair) {
                const auto& display = pair.second;
                return display->isPoweredOn() &&
                        display->getOptimizationPolicy() ==
                        OptimizationPolicy::optimizeForPerformance;
            });

    optimizeThreadScheduling(whence,
                             optimizeForPerformance ? OptimizationPolicy::optimizeForPerformance
                                                    : OptimizationPolicy::optimizeForPower);

    if (mScheduler) {
        const bool disableSyntheticVsync =
                std::any_of(mDisplays.begin(), mDisplays.end(), [](const auto& pair) {
                    const auto& display = pair.second;
                    const hal::PowerMode powerMode = display->getPowerMode();
                    return powerMode != hal::PowerMode::OFF &&
                            powerMode != hal::PowerMode::DOZE_SUSPEND &&
                            display->getOptimizationPolicy() ==
                            OptimizationPolicy::optimizeForPerformance;
                });
        mScheduler->enableSyntheticVsync(!disableSyntheticVsync);
    }
    }
}
}


+5 −10
Original line number Original line Diff line number Diff line
@@ -733,19 +733,14 @@ private:
    void setVirtualDisplayPowerMode(const sp<DisplayDevice>& display, hal::PowerMode mode)
    void setVirtualDisplayPowerMode(const sp<DisplayDevice>& display, hal::PowerMode mode)
            REQUIRES(mStateLock, kMainThreadContext);
            REQUIRES(mStateLock, kMainThreadContext);


    // Returns whether to optimize globally for performance instead of power.
    // Adjusts thread scheduling according to the optimization policy
    bool shouldOptimizeForPerformance() REQUIRES(mStateLock);
    static void optimizeThreadScheduling(

            const char* whence, gui::ISurfaceComposer::OptimizationPolicy optimizationPolicy);
    // Turns on power optimizations, for example when there are no displays to be optimized for
    // performance.
    static void enablePowerOptimizations(const char* whence);

    // Turns off power optimizations.
    static void disablePowerOptimizations(const char* whence);


    // Enables or disables power optimizations depending on whether there are displays that should
    // Enables or disables power optimizations depending on whether there are displays that should
    // be optimized for performance.
    // be optimized for performance.
    void applyOptimizationPolicy(const char* whence) REQUIRES(mStateLock);
    void applyOptimizationPolicy(const char* whence) REQUIRES(kMainThreadContext)
            REQUIRES(mStateLock);


    // Returns the preferred mode for PhysicalDisplayId if the Scheduler has selected one for that
    // Returns the preferred mode for PhysicalDisplayId if the Scheduler has selected one for that
    // display. Falls back to the display's defaultModeId otherwise.
    // display. Falls back to the display's defaultModeId otherwise.
+25 −5
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
#undef LOG_TAG
#undef LOG_TAG
#define LOG_TAG "LibSurfaceFlingerUnittests"
#define LOG_TAG "LibSurfaceFlingerUnittests"


#include <android_companion_virtualdevice_flags.h>
#include <com_android_graphics_surfaceflinger_flags.h>
#include <com_android_graphics_surfaceflinger_flags.h>
#include <common/test/FlagUtils.h>
#include <common/test/FlagUtils.h>
#include "DisplayTransactionTestHelpers.h"
#include "DisplayTransactionTestHelpers.h"
@@ -78,11 +79,19 @@ struct EventThreadBaseSupportedVariant {
struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
struct EventThreadNotSupportedVariant : public EventThreadBaseSupportedVariant {
    static void setupEnableVsyncCallExpectations(DisplayTransactionTest* test) {
    static void setupEnableVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, true)).Times(1);
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, true)).Times(1);
        setupDisableSyntheticVsyncCallExpectations(test);
    }

    static void setupDisableSyntheticVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(_)).Times(0);
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(_)).Times(0);
    }
    }


    static void setupDisableVsyncCallExpectations(DisplayTransactionTest* test) {
    static void setupDisableVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, false)).Times(1);
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, false)).Times(1);
        setupEnableSyntheticVsyncCallExpectations(test);
    }

    static void setupEnableSyntheticVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(_)).Times(0);
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(_)).Times(0);
    }
    }
};
};
@@ -91,12 +100,20 @@ struct EventThreadIsSupportedVariant : public EventThreadBaseSupportedVariant {
    static void setupEnableVsyncCallExpectations(DisplayTransactionTest* test) {
    static void setupEnableVsyncCallExpectations(DisplayTransactionTest* test) {
        // Expect to enable hardware VSYNC and disable synthetic VSYNC.
        // Expect to enable hardware VSYNC and disable synthetic VSYNC.
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, true)).Times(1);
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, true)).Times(1);
        setupDisableSyntheticVsyncCallExpectations(test);
    }

    static void setupDisableSyntheticVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(false)).Times(1);
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(false)).Times(1);
    }
    }


    static void setupDisableVsyncCallExpectations(DisplayTransactionTest* test) {
    static void setupDisableVsyncCallExpectations(DisplayTransactionTest* test) {
        // Expect to disable hardware VSYNC and enable synthetic VSYNC.
        // Expect to disable hardware VSYNC and enable synthetic VSYNC.
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, false)).Times(1);
        EXPECT_CALL(test->mFlinger.scheduler()->mockRequestHardwareVsync, Call(_, false)).Times(1);
        setupEnableSyntheticVsyncCallExpectations(test);
    }

    static void setupEnableSyntheticVsyncCallExpectations(DisplayTransactionTest* test) {
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(true)).Times(1);
        EXPECT_CALL(*test->mEventThread, enableSyntheticVsync(true)).Times(1);
    }
    }
};
};
@@ -151,7 +168,7 @@ struct TransitionOffToDozeSuspendVariant
    template <typename Case>
    template <typename Case>
    static void setupCallExpectations(DisplayTransactionTest* test) {
    static void setupCallExpectations(DisplayTransactionTest* test) {
        Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
        Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE_SUSPEND);
        Case::EventThread::setupVsyncNoCallExpectations(test);
        Case::EventThread::setupEnableSyntheticVsyncCallExpectations(test);
        Case::setupRepaintEverythingCallExpectations(test);
        Case::setupRepaintEverythingCallExpectations(test);
    }
    }


@@ -176,7 +193,7 @@ struct TransitionDozeSuspendToOffVariant
      : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::OFF> {
      : public TransitionVariantCommon<PowerMode::DOZE_SUSPEND, PowerMode::OFF> {
    template <typename Case>
    template <typename Case>
    static void setupCallExpectations(DisplayTransactionTest* test) {
    static void setupCallExpectations(DisplayTransactionTest* test) {
        Case::EventThread::setupVsyncNoCallExpectations(test);
        Case::EventThread::setupEnableSyntheticVsyncCallExpectations(test);
        Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
        Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::OFF);
    }
    }


@@ -188,7 +205,7 @@ struct TransitionDozeSuspendToOffVariant
struct TransitionOnToDozeVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE> {
struct TransitionOnToDozeVariant : public TransitionVariantCommon<PowerMode::ON, PowerMode::DOZE> {
    template <typename Case>
    template <typename Case>
    static void setupCallExpectations(DisplayTransactionTest* test) {
    static void setupCallExpectations(DisplayTransactionTest* test) {
        Case::EventThread::setupVsyncNoCallExpectations(test);
        Case::EventThread::setupDisableSyntheticVsyncCallExpectations(test);
        Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
        Case::setupComposerCallExpectations(test, Case::Doze::ACTUAL_POWER_MODE_FOR_DOZE);
    }
    }
};
};
@@ -206,7 +223,7 @@ struct TransitionDozeSuspendToDozeVariant
struct TransitionDozeToOnVariant : public TransitionVariantCommon<PowerMode::DOZE, PowerMode::ON> {
struct TransitionDozeToOnVariant : public TransitionVariantCommon<PowerMode::DOZE, PowerMode::ON> {
    template <typename Case>
    template <typename Case>
    static void setupCallExpectations(DisplayTransactionTest* test) {
    static void setupCallExpectations(DisplayTransactionTest* test) {
        Case::EventThread::setupVsyncNoCallExpectations(test);
        Case::EventThread::setupDisableSyntheticVsyncCallExpectations(test);
        Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
        Case::setupComposerCallExpectations(test, IComposerClient::PowerMode::ON);
    }
    }
};
};
@@ -234,7 +251,7 @@ struct TransitionOnToUnknownVariant
      : public TransitionVariantCommon<PowerMode::ON, static_cast<PowerMode>(POWER_MODE_LEET)> {
      : public TransitionVariantCommon<PowerMode::ON, static_cast<PowerMode>(POWER_MODE_LEET)> {
    template <typename Case>
    template <typename Case>
    static void setupCallExpectations(DisplayTransactionTest* test) {
    static void setupCallExpectations(DisplayTransactionTest* test) {
        Case::EventThread::setupVsyncNoCallExpectations(test);
        Case::EventThread::setupDisableSyntheticVsyncCallExpectations(test);
        Case::setupNoComposerPowerModeCallExpectations(test);
        Case::setupNoComposerPowerModeCallExpectations(test);
    }
    }
};
};
@@ -335,6 +352,9 @@ void SetPhysicalDisplayPowerModeTest::transitionDisplayCommon() {
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // Preconditions
    // Preconditions


    SET_FLAG_FOR_TEST(android::companion::virtualdevice::flags::correct_virtual_display_power_state,
                      true);

    Case::Doze::setupComposerCallExpectations(this);
    Case::Doze::setupComposerCallExpectations(this);
    auto display =
    auto display =
            Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);
            Case::injectDisplayWithInitialPowerMode(this, Case::Transition::INITIAL_POWER_MODE);