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

Commit 0f0ddc10 authored by Daniel Solomon's avatar Daniel Solomon
Browse files

SurfaceFlinger: Query Scheduler when updating allowed display configs

Currently two entities in SurfaceFlinger can set a new display refresh
rate: (1) SurfaceFlinger core, and (2) Scheduler. It's possible for
these two entities to get out of sync in the following way:
1) Scheduler updates the refresh rate to some rate
2) Upper layers call into SurfaceFlinger to update allowed display
configs
3) SurfaceFlinger always sets display rate to max

If the refresh rate from #1 and #3 don't match, it can leave the system
in an inconsistent state, potentially causing visual and power issues.

This change fixes this problem by changing step #3: Instead of always
choosing the max refresh rate, SurfaceFlinger queries the optimal
refresh rate from Scheduler. If that rate isn't available, only then
does SurfaceFlinger default to the maximum rate.

Bug: 139557239
Test: atest libsurfaceflinger_unittest
Test: Manual:
    1) Start with SurfaceFlinger idling (Scheduler selected
    RefreshRateType::DEFAULT)
    2) Trigger a change in allowed display configs from
    DisplayModeDirector
    3) Make sure the RefreshRateType SurfaceFlinger sets is DEFAULT
    instead of PERFORMANCE
Change-Id: Ia85a60fde55afaed5106462942e0bb77652ec737
parent 3967b1fe
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -512,6 +512,7 @@ Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
    }

    // Content detection is on, find the appropriate refresh rate with minimal error
    // TODO(b/139751853): Scan allowed refresh rates only (SurfaceFlinger::mAllowedDisplayConfigs)
    const float rate = static_cast<float>(mFeatures.contentRefreshRate);
    auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(),
                            mRefreshRateConfigs.getRefreshRates().cend(),
@@ -541,6 +542,11 @@ Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
    return currRefreshRateType;
}

Scheduler::RefreshRateType Scheduler::getPreferredRefreshRateType() {
    std::lock_guard<std::mutex> lock(mFeatureStateLock);
    return mFeatures.refreshRateType;
}

void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
    std::lock_guard<std::mutex> lock(mCallbackLock);
    if (mChangeRefreshRateCallback) {
+3 −0
Original line number Diff line number Diff line
@@ -127,6 +127,9 @@ public:
    void dump(std::string&) const;
    void dump(ConnectionHandle, std::string&) const;

    // Get the appropriate refresh type for current conditions.
    RefreshRateType getPreferredRefreshRateType();

private:
    friend class TestableScheduler;

+20 −10
Original line number Diff line number Diff line
@@ -5634,6 +5634,25 @@ void SurfaceFlinger::traverseLayersInDisplay(const sp<const DisplayDevice>& disp
    }
}

void SurfaceFlinger::setPreferredDisplayConfig() {
    const auto& type = mScheduler->getPreferredRefreshRateType();
    const auto& config = mRefreshRateConfigs.getRefreshRate(type);
    if (config && isDisplayConfigAllowed(config->configId)) {
        ALOGV("switching to Scheduler preferred config %d", config->configId);
        setDesiredActiveConfig({type, config->configId, Scheduler::ConfigEvent::Changed});
    } else {
        // Set the highest allowed config by iterating backwards on available refresh rates
        const auto& refreshRates = mRefreshRateConfigs.getRefreshRates();
        for (auto iter = refreshRates.crbegin(); iter != refreshRates.crend(); ++iter) {
            if (iter->second && isDisplayConfigAllowed(iter->second->configId)) {
                ALOGV("switching to allowed config %d", iter->second->configId);
                setDesiredActiveConfig({iter->first, iter->second->configId,
                        Scheduler::ConfigEvent::Changed});
            }
        }
    }
}

void SurfaceFlinger::setAllowedDisplayConfigsInternal(const sp<DisplayDevice>& display,
                                                      const std::vector<int32_t>& allowedConfigs) {
    if (!display->isPrimary()) {
@@ -5655,16 +5674,7 @@ void SurfaceFlinger::setAllowedDisplayConfigsInternal(const sp<DisplayDevice>& d
    mScheduler->onConfigChanged(mAppConnectionHandle, display->getId()->value,
                                display->getActiveConfig());

    // Set the highest allowed config by iterating backwards on available refresh rates
    const auto& refreshRates = mRefreshRateConfigs.getRefreshRates();
    for (auto iter = refreshRates.crbegin(); iter != refreshRates.crend(); ++iter) {
        if (iter->second && isDisplayConfigAllowed(iter->second->configId)) {
            ALOGV("switching to config %d", iter->second->configId);
            setDesiredActiveConfig(
                    {iter->first, iter->second->configId, Scheduler::ConfigEvent::Changed});
            break;
        }
    }
    setPreferredDisplayConfig();
}

status_t SurfaceFlinger::setAllowedDisplayConfigs(const sp<IBinder>& displayToken,
+3 −0
Original line number Diff line number Diff line
@@ -528,6 +528,9 @@ private:
    // called on the main thread in response to setPowerMode()
    void setPowerModeInternal(const sp<DisplayDevice>& display, int mode) REQUIRES(mStateLock);

    // Query the Scheduler or allowed display configs list for a matching config, and set it
    void setPreferredDisplayConfig() REQUIRES(mStateLock);

    // called on the main thread in response to setAllowedDisplayConfigs()
    void setAllowedDisplayConfigsInternal(const sp<DisplayDevice>& display,
                                          const std::vector<int32_t>& allowedConfigs)