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

Commit 76b2fc03 authored by Su Hong Koo's avatar Su Hong Koo
Browse files

SF: Use pacesetter instead of active display as the "default" display

Active display as the default display does not extend very well to the
connected display world, where the active display(s) may be off while
the external display(s) are powered-on and actually active.

This CL replaces the usage of mActiveDisplayId and
getDefaultDisplayDevice* where it makes sense. It adds a new getter
method for pacesetter to the Scheduler and SurfaceFlinger. The default
display getters are renamed as getActiveDisplayDevice* to better
reflect on their source and to discourage its use as the "default".

Flag: com.android.graphics.surfaceflinger.flags.pacesetter_selection
Bug: 255635821
Test: manual testing on clamshell and foldable w/ ext display
Change-Id: I87aeb3551cf893f8161534cdd5568bb90e4f47cf
parent b70835d1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ void RegionSamplingThread::captureSample() {

    {
        // TODO(b/159112860): Don't keep sp<DisplayDevice> outside of SF main thread
        const sp<const DisplayDevice> display = mFlinger.getDefaultDisplayDevice();
        const sp<const DisplayDevice> display = mFlinger.getActiveDisplay();
        displayWeak = display;
        layerStack = display->getLayerStack();
        orientation = ui::Transform::toRotationFlags(display->getOrientation());
+6 −0
Original line number Diff line number Diff line
@@ -131,6 +131,12 @@ void Scheduler::setPacesetterDisplay(PhysicalDisplayId pacesetterId) {
    updatePhaseConfiguration(pacesetterId, pacesetterSelectorPtr()->getActiveMode().fps);
}

PhysicalDisplayId Scheduler::getPacesetterDisplayId() const {
    std::scoped_lock lock(mDisplayLock);
    LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
    return *mPacesetterDisplayId;
}

void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr,
                                PhysicalDisplayId activeDisplayId) {
    auto schedulePtr =
+4 −2
Original line number Diff line number Diff line
@@ -98,6 +98,8 @@ public:
    void setPacesetterDisplay(PhysicalDisplayId) REQUIRES(kMainThreadContext)
            EXCLUDES(mDisplayLock, mVsyncConfigLock);

    PhysicalDisplayId getPacesetterDisplayId() const EXCLUDES(mDisplayLock);

    using RefreshRateSelectorPtr = std::shared_ptr<RefreshRateSelector>;

    using ConstVsyncSchedulePtr = std::shared_ptr<const VsyncSchedule>;
@@ -549,8 +551,8 @@ private:
    ui::PhysicalDisplayMap<PhysicalDisplayId, Display> mDisplays GUARDED_BY(mDisplayLock)
            GUARDED_BY(kMainThreadContext);

    ftl::Optional<PhysicalDisplayId> mPacesetterDisplayId GUARDED_BY(mDisplayLock)
            GUARDED_BY(kMainThreadContext);
    // May be read from any thread, but must only be written from the main thread.
    ftl::Optional<PhysicalDisplayId> mPacesetterDisplayId GUARDED_BY(mDisplayLock);

    ftl::Optional<DisplayRef> pacesetterDisplayLocked() REQUIRES(mDisplayLock) {
        return static_cast<const Scheduler*>(this)->pacesetterDisplayLocked().transform(
+18 −15
Original line number Diff line number Diff line
@@ -704,11 +704,11 @@ std::vector<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdsLocked() con
    std::vector<PhysicalDisplayId> displayIds;
    displayIds.reserve(mPhysicalDisplays.size());

    const auto defaultDisplayId = getDefaultDisplayDeviceLocked()->getPhysicalId();
    displayIds.push_back(defaultDisplayId);
    const auto activeDisplayId = getActiveDisplayLocked()->getPhysicalId();
    displayIds.push_back(activeDisplayId);

    for (const auto& [id, display] : mPhysicalDisplays) {
        if (id != defaultDisplayId) {
        if (id != activeDisplayId) {
            displayIds.push_back(id);
        }
    }
@@ -960,7 +960,7 @@ void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
        processDisplayAdded(token, state);
        mDrawingState.displays.add(token, state);

        display = getDefaultDisplayDeviceLocked();
        display = getActiveDisplayLocked();
    }

    LOG_ALWAYS_FATAL_IF(!display, "Failed to configure the primary display");
@@ -6460,7 +6460,7 @@ void SurfaceFlinger::dumpAll(const DumpArgs& args, const std::string& compositio
    ClientCache::getInstance().dump(result);
    DebugEGLImageTracker::getInstance()->dump(result);

    if (const auto display = getDefaultDisplayDeviceLocked()) {
    if (const auto display = getActiveDisplayLocked()) {
        display->getCompositionDisplay()->getState().undefinedRegion.dump(result,
                                                                          "undefinedRegion");
        StringAppendF(&result, "  orientation=%s, isPoweredOn=%d\n",
@@ -6468,7 +6468,7 @@ void SurfaceFlinger::dumpAll(const DumpArgs& args, const std::string& compositio
    }
    StringAppendF(&result, "  transaction-flags         : %08x\n", mTransactionFlags.load());

    if (const auto display = getDefaultDisplayDeviceLocked()) {
    if (const auto display = getActiveDisplayLocked()) {
        std::string peakFps, xDpi, yDpi;
        const auto activeMode = display->refreshRateSelector().getActiveMode();
        if (const auto activeModePtr = activeMode.modePtr.get()) {
@@ -6841,7 +6841,7 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
            }
            // Is a DisplayColorSetting supported?
            case 1027: {
                const auto display = getDefaultDisplayDevice();
                const auto display = getActiveDisplay();
                if (!display) {
                    return NAME_NOT_FOUND;
                }
@@ -6924,7 +6924,7 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
            case 1035: {
                // Parameters:
                // - (required) i32 mode id.
                // - (optional) i64 display id. Using default display if not provided.
                // - (optional) i64 display id. Using pacesetter display if not provided.
                // - (optional) f min render rate. Using mode's fps is not provided.
                // - (optional) f max render rate. Using mode's fps is not provided.

@@ -6933,7 +6933,7 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
                const auto display = [&]() -> sp<IBinder> {
                    uint64_t value;
                    if (data.readUint64(&value) != NO_ERROR) {
                        return getDefaultDisplayDevice()->getDisplayToken().promote();
                        return getPacesetterDisplay()->getDisplayToken().promote();
                    }

                    if (const auto token =
@@ -6971,7 +6971,7 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
                    return mScheduler
                            ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) {
                                const auto display =
                                        FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked());
                                        FTL_FAKE_GUARD(mStateLock, getPacesetterDisplayLocked());

                                // This is a little racy, but not in a way that hurts anything. As
                                // we grab the defaultMode from the display manager policy, we could
@@ -6991,7 +6991,7 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
                    return mScheduler
                            ->schedule([this]() FTL_FAKE_GUARD(kMainThreadContext) {
                                const auto display =
                                        FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked());
                                        FTL_FAKE_GUARD(mStateLock, getPacesetterDisplayLocked());
                                return setDesiredDisplayModeSpecsInternal(
                                        display,
                                        scheduler::RefreshRateSelector::NoOverridePolicy{});
@@ -7251,7 +7251,7 @@ void SurfaceFlinger::kernelTimerChanged(bool expired) {
    // Update the overlay on the main thread to avoid race conditions with
    // RefreshRateSelector::getActiveMode
    static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(kMainThreadContext) {
        const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked());
        const auto display = FTL_FAKE_GUARD(mStateLock, getPacesetterDisplayLocked());
        if (!display) {
            ALOGW("%s: default display is null", __func__);
            return;
@@ -7866,7 +7866,7 @@ bool SurfaceFlinger::getDisplayStateOnMainThread(ScreenshotArgs& args) {
        }

        if (display == nullptr) {
            display = getDefaultDisplayDeviceLocked();
            display = getPacesetterDisplayLocked();
        }

        if (display != nullptr) {
@@ -8401,7 +8401,8 @@ status_t SurfaceFlinger::getMaxAcquiredBufferCount(int* buffers) const {
    Fps maxRefreshRate = 60_Hz;

    if (!getHwComposer().isHeadless()) {
        if (const auto display = getDefaultDisplayDevice()) {
        const sp<const DisplayDevice> display = getPacesetterDisplay();
        if (display) {
            maxRefreshRate = display->refreshRateSelector().getSupportedRefreshRateRange().max;
        }
    }
@@ -8416,7 +8417,9 @@ uint32_t SurfaceFlinger::getMaxAcquiredBufferCountForCurrentRefreshRate(uid_t ui
    if (const auto frameRateOverride = mScheduler->getFrameRateOverride(uid)) {
        refreshRate = *frameRateOverride;
    } else if (!getHwComposer().isHeadless()) {
        if (const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked())) {
        const sp<const DisplayDevice> display =
                FTL_FAKE_GUARD(mStateLock, getPacesetterDisplayLocked());
        if (display) {
            refreshRate = display->refreshRateSelector().getActiveMode().fps;
        }
    }
+21 −6
Original line number Diff line number Diff line
@@ -999,18 +999,33 @@ private:
        return nullptr;
    }

    // Returns the primary display or (for foldables) the active display.
    sp<const DisplayDevice> getDefaultDisplayDeviceLocked() const REQUIRES(mStateLock) {
        return const_cast<SurfaceFlinger*>(this)->getDefaultDisplayDeviceLocked();
    sp<const DisplayDevice> getPacesetterDisplayLocked() const REQUIRES(mStateLock) {
        return const_cast<SurfaceFlinger*>(this)->getPacesetterDisplayLocked();
    }

    sp<DisplayDevice> getDefaultDisplayDeviceLocked() REQUIRES(mStateLock) {
    sp<DisplayDevice> getPacesetterDisplayLocked() REQUIRES(mStateLock) {
        if (!FlagManager::getInstance().pacesetter_selection()) {
            return getActiveDisplayLocked();
        }
        return getDisplayDeviceLocked(mScheduler->getPacesetterDisplayId());
    }

    sp<const DisplayDevice> getPacesetterDisplay() const EXCLUDES(mStateLock) {
        Mutex::Autolock lock(mStateLock);
        return getPacesetterDisplayLocked();
    }

    sp<const DisplayDevice> getActiveDisplayLocked() const REQUIRES(mStateLock) {
        return const_cast<SurfaceFlinger*>(this)->getActiveDisplayLocked();
    }

    sp<DisplayDevice> getActiveDisplayLocked() REQUIRES(mStateLock) {
        return getDisplayDeviceLocked(mActiveDisplayId);
    }

    sp<const DisplayDevice> getDefaultDisplayDevice() const EXCLUDES(mStateLock) {
    sp<const DisplayDevice> getActiveDisplay() const EXCLUDES(mStateLock) {
        Mutex::Autolock lock(mStateLock);
        return getDefaultDisplayDeviceLocked();
        return getActiveDisplayLocked();
    }

    using DisplayDeviceAndSnapshot = std::pair<sp<DisplayDevice>, display::DisplaySnapshotRef>;
Loading