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

Commit 79e0f7f4 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "scheduler::123998711"

* changes:
  SF: Adding testable Scheduler and updating tests.
  SF: Migrate to Scheduler and clean up duplicate code.
parents 46c6532f afb45844
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -236,9 +236,7 @@ status_t BufferQueueLayer::updateTexImage(bool& recomputeVisibleRegions, nsecs_t
                    getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode,
                    getTransformToDisplayInverse(), mFreezeGeometryUpdates);

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

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

        Mutex::Autolock lock(mQueueItemLock);
        // Reset the frame number tracker when we receive the first buffer after
+43 −10
Original line number 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) {
    std::lock_guard<std::mutex> lock(mHWVsyncLock);
    mPrimaryDispSync->reset();
@@ -229,16 +272,6 @@ void Scheduler::setIgnorePresentFences(bool 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() {
    return mPrimaryDispSync->expectedPresentTime();
}
+20 −4
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ class EventControlThread;
class Scheduler {
public:
    using ExpiredIdleTimerCallback = std::function<void()>;
    using GetVsyncPeriod = std::function<nsecs_t()>;
    using ResetIdleTimerCallback = std::function<void()>;

    // Enum to indicate whether to start the transaction early, or at vsync time.
@@ -67,6 +68,16 @@ public:
        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);

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

    void enableHardwareVsync();
    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 addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
    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();
    // Adds the present time for given layer to the history of present times.
    void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
@@ -133,6 +144,8 @@ protected:
            impl::EventThread::InterceptVSyncsCallback interceptCallback);

private:
    friend class TestableScheduler;

    // Creates a connection on the given EventThread and forwards the given callbacks.
    sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&);

@@ -150,6 +163,8 @@ private:
    void resetTimerCallback();
    // Function that is called when the timer expires.
    void expiredTimerCallback();
    // Sets vsync period.
    void setVsyncPeriod(const nsecs_t period);

    // If fences from sync Framework are supported.
    const bool mHasSyncFramework;
@@ -167,6 +182,7 @@ private:
    std::mutex mHWVsyncLock;
    bool mPrimaryHWVsyncEnabled 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<EventControlThread> mEventControlThread;
+76 −303

File changed.

Preview size limit exceeded, changes collapsed.

+1 −36
Original line number Diff line number Diff line
@@ -786,37 +786,11 @@ private:
     * VSync
     */
    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
    // the desired refresh rate.
    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
     */
@@ -954,11 +928,7 @@ private:
    // constant members (no synchronization needed for access)
    nsecs_t mBootTime;
    bool mGpuToCpuSupported;
    std::unique_ptr<EventThread> mEventThread;
    std::unique_ptr<EventThread> mSFEventThread;
    std::unique_ptr<EventThread> mInjectorEventThread;
    std::unique_ptr<VSyncSource> mEventThreadSource;
    std::unique_ptr<VSyncSource> mSfEventThreadSource;
    std::unique_ptr<InjectVSyncSource> mVSyncInjector;
    std::unique_ptr<EventControlThread> mEventControlThread;

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

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

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

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