Move HwVsyncState into VsyncSchedule
In preparation for having a separate schedule per display, pull state
which is logically for a single display into VsyncSchedule. This should
be a pure refactor.
Consolidate mPrimaryHWVsyncEnabled and mHWVsyncAvailable (now in
VsyncSchedule) into a single enum which better captures the possible
states. Move SF's record of the pending HWC VSYNC state into the same
place, so it sits with other related logic. Remove the last HWC
VSYNC state, which did not affect behavior.
Move ISchedulerCallback into its own file, so that VsyncSchedule.cpp
doesn't need to depend on Scheduler.h.
Rename variables `makeUnavailable` and `makeAvailable` for clarity.
Make addResyncSample return a boolean, instead of void with an out
parameter.
Remove VsyncSchedule's move constructor. Now that it has a mutex, it is
no longer thread-safe. We don't actually need to move it anyway. Replace
std::optional with std::unique_ptr for owning a VsyncSchedule.
Factored out of If60218e85292c786b9fa70ecb33ee374d3a385e0. Relevant
differences from that patch:
- There is still only one VsyncSchedule.
- When SF turns off a display, we still disable vsync on that display.
In If60218e85292c786b9fa70ecb33ee374d3a385e0, this was redundant with
the call to disableHardwareVsync shortly before it, but right now we
only call disableHardwareVsync for the active display.
- Hold the VSYNC lock for more time in resyncToHaredwareVsync. This
better matches the original behavior.
- Remove mLastHwVsyncState.
- Recreate the code for storing and setting the pending VSYNC state.
- Inline setVsyncPeriod at its only call-site.
Bug: 255601557
Bug: 256196556
Bug: 241286146
Fixes: 266712910
Test: libsurfaceflinger_unittest
Change-Id: I54a1304a3428968134cc707b24d5b325927c31df
diff --git a/services/surfaceflinger/Scheduler/VsyncSchedule.h b/services/surfaceflinger/Scheduler/VsyncSchedule.h
index 173b1d0..d88f1d1 100644
--- a/services/surfaceflinger/Scheduler/VsyncSchedule.h
+++ b/services/surfaceflinger/Scheduler/VsyncSchedule.h
@@ -19,6 +19,9 @@
#include <memory>
#include <string>
+#include <ThreadContext.h>
+#include <ftl/enum.h>
+#include <ftl/optional.h>
#include <scheduler/Features.h>
#include <scheduler/Time.h>
@@ -32,6 +35,8 @@
namespace android::scheduler {
+struct ISchedulerCallback;
+
// TODO(b/185535769): Rename classes, and remove aliases.
class VSyncDispatch;
class VSyncTracker;
@@ -44,12 +49,24 @@
class VsyncSchedule {
public:
explicit VsyncSchedule(FeatureFlags);
- VsyncSchedule(VsyncSchedule&&);
~VsyncSchedule();
Period period() const;
TimePoint vsyncDeadlineAfter(TimePoint) const;
+ // Inform the schedule that the period is changing and the schedule needs to recalibrate
+ // itself. The schedule will end the period transition internally. This will
+ // enable hardware VSYNCs in order to calibrate.
+ //
+ // \param [in] period The period that the system is changing into.
+ void startPeriodTransition(ISchedulerCallback&, Period period);
+
+ // Pass a VSYNC sample to VsyncController. Return true if
+ // VsyncController detected that the VSYNC period changed. Enable or disable
+ // hardware VSYNCs depending on whether more samples are needed.
+ bool addResyncSample(ISchedulerCallback&, TimePoint timestamp,
+ ftl::Optional<Period> hwcVsyncPeriod);
+
// TODO(b/185535769): Hide behind API.
const VsyncTracker& getTracker() const { return *mTracker; }
VsyncTracker& getTracker() { return *mTracker; }
@@ -60,6 +77,22 @@
void dump(std::string&) const;
+ // Turn on hardware VSYNCs, unless mHwVsyncState is Disallowed, in which
+ // case this call is ignored.
+ void enableHardwareVsync(ISchedulerCallback&) EXCLUDES(mHwVsyncLock);
+
+ // Disable hardware VSYNCs. If `disallow` is true, future calls to
+ // enableHardwareVsync are ineffective until allowHardwareVsync is called.
+ void disableHardwareVsync(ISchedulerCallback&, bool disallow) EXCLUDES(mHwVsyncLock);
+
+ // If true, enableHardwareVsync can enable hardware VSYNC (if not already
+ // enabled). If false, enableHardwareVsync does nothing.
+ bool isHardwareVsyncAllowed(bool makeAllowed) EXCLUDES(mHwVsyncLock);
+
+ void setPendingHardwareVsyncState(bool enabled) REQUIRES(kMainThreadContext);
+
+ bool getPendingHardwareVsyncState() const REQUIRES(kMainThreadContext);
+
private:
friend class TestableScheduler;
friend class android::EventThreadTest;
@@ -76,14 +109,36 @@
static DispatchPtr createDispatch(VsyncTracker&);
static ControllerPtr createController(VsyncTracker&, FeatureFlags);
+ void enableHardwareVsyncLocked(ISchedulerCallback&) REQUIRES(mHwVsyncLock);
+
+ mutable std::mutex mHwVsyncLock;
+ enum class HwVsyncState {
+ // Hardware VSYNCs are currently enabled.
+ Enabled,
+
+ // Hardware VSYNCs are currently disabled. They can be enabled by a call
+ // to `enableHardwareVsync`.
+ Disabled,
+
+ // Hardware VSYNCs are not currently allowed (e.g. because the display
+ // is off).
+ Disallowed,
+
+ ftl_last = Disallowed,
+ };
+ HwVsyncState mHwVsyncState GUARDED_BY(mHwVsyncLock) = HwVsyncState::Disallowed;
+
+ // Pending state, in case an attempt is made to set the state while the
+ // device is off.
+ HwVsyncState mPendingHwVsyncState GUARDED_BY(kMainThreadContext) = HwVsyncState::Disabled;
+
class PredictedVsyncTracer;
using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
- // Effectively const except in move constructor.
- TrackerPtr mTracker;
- DispatchPtr mDispatch;
- ControllerPtr mController;
- TracerPtr mTracer;
+ const TrackerPtr mTracker;
+ const DispatchPtr mDispatch;
+ const ControllerPtr mController;
+ const TracerPtr mTracer;
};
} // namespace android::scheduler