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

Commit c2870424 authored by Ana Krulec's avatar Ana Krulec
Browse files

SF: Migrate to Scheduler and clean up duplicate code.

Test: manual. Updating unittests.
Bug: 123998711
Change-Id: I02a3807e21a186864a2eb485905cadc15983f049
parent b2069eba
Loading
Loading
Loading
Loading
+3 −7
Original line number Original line Diff line number Diff line
@@ -237,9 +237,7 @@ status_t BufferQueueLayer::updateTexImage(bool& recomputeVisibleRegions, nsecs_t
                    getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode,
                    getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode,
                    getTransformToDisplayInverse(), mFreezeGeometryUpdates);
                    getTransformToDisplayInverse(), mFreezeGeometryUpdates);


    nsecs_t expectedPresentTime = mFlinger->mUseScheduler
    nsecs_t expectedPresentTime = mFlinger->mScheduler->expectedPresentTime();
            ? mFlinger->mScheduler->expectedPresentTime()
            : mFlinger->mPrimaryDispSync->expectedPresentTime();


    if (isRemovedFromCurrentState()) {
    if (isRemovedFromCurrentState()) {
        expectedPresentTime = 0;
        expectedPresentTime = 0;
@@ -402,10 +400,8 @@ void BufferQueueLayer::onFrameAvailable(const BufferItem& item) {
    // Add this buffer from our internal queue tracker
    // Add this buffer from our internal queue tracker
    { // Autolock scope
    { // Autolock scope
        // Report the requested present time to the Scheduler.
        // Report the requested present time to the Scheduler.
        if (mFlinger->mUseScheduler) {
        mFlinger->mScheduler->addFramePresentTimeForLayer(item.mTimestamp, item.mIsAutoTimestamp,
            mFlinger->mScheduler->addFramePresentTimeForLayer(item.mTimestamp,
                                                          mName.c_str());
                                                              item.mIsAutoTimestamp, mName.c_str());
        }


        Mutex::Autolock lock(mQueueItemLock);
        Mutex::Autolock lock(mQueueItemLock);
        // Reset the frame number tracker when we receive the first buffer after
        // Reset the frame number tracker when we receive the first buffer after
+43 −10
Original line number Original line Diff line number Diff line
@@ -189,6 +189,49 @@ void Scheduler::disableHardwareVsync(bool makeUnavailable) {
    }
    }
}
}


void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
    {
        std::lock_guard<std::mutex> lock(mHWVsyncLock);
        if (makeAvailable) {
            mHWVsyncAvailable = makeAvailable;
        } else if (!mHWVsyncAvailable) {
            // Hardware vsync is not currently available, so abort the resync
            // attempt for now
            return;
        }
    }

    if (period <= 0) {
        return;
    }

    setVsyncPeriod(period);
}

ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
    std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
    return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
        if (const auto vsync = ptr.lock()) {
            vsync->resync(getVsyncPeriod);
        }
    };
}

void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
    static constexpr nsecs_t kIgnoreDelay = ms2ns(500);

    const nsecs_t now = systemTime();
    const nsecs_t last = lastResyncTime.exchange(now);

    if (now - last > kIgnoreDelay) {
        scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
    }
}

void Scheduler::setRefreshSkipCount(int count) {
    mPrimaryDispSync->setRefreshSkipCount(count);
}

void Scheduler::setVsyncPeriod(const nsecs_t period) {
void Scheduler::setVsyncPeriod(const nsecs_t period) {
    std::lock_guard<std::mutex> lock(mHWVsyncLock);
    std::lock_guard<std::mutex> lock(mHWVsyncLock);
    mPrimaryDispSync->reset();
    mPrimaryDispSync->reset();
@@ -229,16 +272,6 @@ void Scheduler::setIgnorePresentFences(bool ignore) {
    mPrimaryDispSync->setIgnorePresentFences(ignore);
    mPrimaryDispSync->setIgnorePresentFences(ignore);
}
}


void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
    std::lock_guard<std::mutex> lock(mHWVsyncLock);
    mHWVsyncAvailable = makeAvailable;
}

bool Scheduler::getHWSyncAvailable() {
    std::lock_guard<std::mutex> lock(mHWVsyncLock);
    return mHWVsyncAvailable;
}

nsecs_t Scheduler::expectedPresentTime() {
nsecs_t Scheduler::expectedPresentTime() {
    return mPrimaryDispSync->expectedPresentTime();
    return mPrimaryDispSync->expectedPresentTime();
}
}
+18 −4
Original line number Original line Diff line number Diff line
@@ -37,6 +37,7 @@ class EventControlThread;
class Scheduler {
class Scheduler {
public:
public:
    using ExpiredIdleTimerCallback = std::function<void()>;
    using ExpiredIdleTimerCallback = std::function<void()>;
    using GetVsyncPeriod = std::function<nsecs_t()>;
    using ResetIdleTimerCallback = std::function<void()>;
    using ResetIdleTimerCallback = std::function<void()>;


    // Enum to indicate whether to start the transaction early, or at vsync time.
    // Enum to indicate whether to start the transaction early, or at vsync time.
@@ -67,6 +68,16 @@ public:
        const std::unique_ptr<EventThread> thread;
        const std::unique_ptr<EventThread> thread;
    };
    };


    // Stores per-display state about VSYNC.
    struct VsyncState {
        explicit VsyncState(Scheduler& scheduler) : scheduler(scheduler) {}

        void resync(const GetVsyncPeriod&);

        Scheduler& scheduler;
        std::atomic<nsecs_t> lastResyncTime = 0;
    };

    explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
    explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);


    virtual ~Scheduler();
    virtual ~Scheduler();
@@ -104,13 +115,13 @@ public:


    void enableHardwareVsync();
    void enableHardwareVsync();
    void disableHardwareVsync(bool makeUnavailable);
    void disableHardwareVsync(bool makeUnavailable);
    void setVsyncPeriod(const nsecs_t period);
    void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
    // Creates a callback for resyncing.
    ResyncCallback makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod);
    void setRefreshSkipCount(int count);
    void addResyncSample(const nsecs_t timestamp);
    void addResyncSample(const nsecs_t timestamp);
    void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
    void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
    void setIgnorePresentFences(bool ignore);
    void setIgnorePresentFences(bool ignore);
    void makeHWSyncAvailable(bool makeAvailable);
    // returns HWSyncAvailable flag to SF would enable HW vsync based on this
    bool getHWSyncAvailable();
    nsecs_t expectedPresentTime();
    nsecs_t expectedPresentTime();
    // Adds the present time for given layer to the history of present times.
    // Adds the present time for given layer to the history of present times.
    void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
    void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
@@ -150,6 +161,8 @@ private:
    void resetTimerCallback();
    void resetTimerCallback();
    // Function that is called when the timer expires.
    // Function that is called when the timer expires.
    void expiredTimerCallback();
    void expiredTimerCallback();
    // Sets vsync period.
    void setVsyncPeriod(const nsecs_t period);


    // If fences from sync Framework are supported.
    // If fences from sync Framework are supported.
    const bool mHasSyncFramework;
    const bool mHasSyncFramework;
@@ -167,6 +180,7 @@ private:
    std::mutex mHWVsyncLock;
    std::mutex mHWVsyncLock;
    bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
    bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
    bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
    bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
    const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)};


    std::unique_ptr<DispSync> mPrimaryDispSync;
    std::unique_ptr<DispSync> mPrimaryDispSync;
    std::unique_ptr<EventControlThread> mEventControlThread;
    std::unique_ptr<EventControlThread> mEventControlThread;
+76 −303

File changed.

Preview size limit exceeded, changes collapsed.

+1 −36
Original line number Original line Diff line number Diff line
@@ -789,37 +789,11 @@ private:
     * VSync
     * VSync
     */
     */
    nsecs_t getVsyncPeriod() const REQUIRES(mStateLock);
    nsecs_t getVsyncPeriod() const REQUIRES(mStateLock);
    void enableHardwareVsync();
    void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
    void disableHardwareVsync(bool makeUnavailable);


    // Sets the refresh rate by switching active configs, if they are available for
    // Sets the refresh rate by switching active configs, if they are available for
    // the desired refresh rate.
    // the desired refresh rate.
    void setRefreshRateTo(scheduler::RefreshRateConfigs::RefreshRateType) REQUIRES(mStateLock);
    void setRefreshRateTo(scheduler::RefreshRateConfigs::RefreshRateType) REQUIRES(mStateLock);


    using GetVsyncPeriod = std::function<nsecs_t()>;

    // Stores per-display state about VSYNC.
    struct VsyncState {
        explicit VsyncState(SurfaceFlinger& flinger) : flinger(flinger) {}

        void resync(const GetVsyncPeriod&);

        SurfaceFlinger& flinger;
        std::atomic<nsecs_t> lastResyncTime = 0;
    };

    const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)};

    auto makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
        std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
        return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
            if (const auto vsync = ptr.lock()) {
                vsync->resync(getVsyncPeriod);
            }
        };
    }

    /*
    /*
     * Display identification
     * Display identification
     */
     */
@@ -957,11 +931,7 @@ private:
    // constant members (no synchronization needed for access)
    // constant members (no synchronization needed for access)
    nsecs_t mBootTime;
    nsecs_t mBootTime;
    bool mGpuToCpuSupported;
    bool mGpuToCpuSupported;
    std::unique_ptr<EventThread> mEventThread;
    std::unique_ptr<EventThread> mSFEventThread;
    std::unique_ptr<EventThread> mInjectorEventThread;
    std::unique_ptr<EventThread> mInjectorEventThread;
    std::unique_ptr<VSyncSource> mEventThreadSource;
    std::unique_ptr<VSyncSource> mSfEventThreadSource;
    std::unique_ptr<InjectVSyncSource> mVSyncInjector;
    std::unique_ptr<InjectVSyncSource> mVSyncInjector;
    std::unique_ptr<EventControlThread> mEventControlThread;
    std::unique_ptr<EventControlThread> mEventControlThread;


@@ -1028,16 +998,11 @@ private:
    // these are thread safe
    // these are thread safe
    mutable std::unique_ptr<MessageQueue> mEventQueue{mFactory.createMessageQueue()};
    mutable std::unique_ptr<MessageQueue> mEventQueue{mFactory.createMessageQueue()};
    FrameTracker mAnimFrameTracker;
    FrameTracker mAnimFrameTracker;
    std::unique_ptr<DispSync> mPrimaryDispSync;


    // protected by mDestroyedLayerLock;
    // protected by mDestroyedLayerLock;
    mutable Mutex mDestroyedLayerLock;
    mutable Mutex mDestroyedLayerLock;
    Vector<Layer const *> mDestroyedLayers;
    Vector<Layer const *> mDestroyedLayers;


    // protected by mHWVsyncLock
    Mutex mHWVsyncLock;
    bool mPrimaryHWVsyncEnabled;
    bool mHWVsyncAvailable;
    nsecs_t mRefreshStartTime;
    nsecs_t mRefreshStartTime;


    std::atomic<bool> mRefreshPending{false};
    std::atomic<bool> mRefreshPending{false};
@@ -1113,7 +1078,7 @@ private:
    /* ------------------------------------------------------------------------
    /* ------------------------------------------------------------------------
     * Scheduler
     * Scheduler
     */
     */
    bool mUseScheduler = false;
    bool mUse90Hz = false;
    std::unique_ptr<Scheduler> mScheduler;
    std::unique_ptr<Scheduler> mScheduler;
    sp<Scheduler::ConnectionHandle> mAppConnectionHandle;
    sp<Scheduler::ConnectionHandle> mAppConnectionHandle;
    sp<Scheduler::ConnectionHandle> mSfConnectionHandle;
    sp<Scheduler::ConnectionHandle> mSfConnectionHandle;