SF: Add VsyncSchedule skeleton
Pull the Scheduler::VsyncSchedule struct and related code into its own
file, as it will be extended with more per-display state, e.g. reactor
registrations, resync state machine, etc.
Add <scheduler/Features.h> for feature flags. Move Scheduler into its
namespace.
Bug: 185535769
Test: libsurfaceflinger_unittest
Change-Id: I6e10893632c5abf40380df924791d1fcc27c3cc2
diff --git a/services/surfaceflinger/Scheduler/LayerHistory.h b/services/surfaceflinger/Scheduler/LayerHistory.h
index 92236f5..8d56951 100644
--- a/services/surfaceflinger/Scheduler/LayerHistory.h
+++ b/services/surfaceflinger/Scheduler/LayerHistory.h
@@ -31,11 +31,9 @@
namespace android {
class Layer;
-class TestableScheduler;
namespace scheduler {
-class LayerHistoryTest;
class LayerInfo;
class LayerHistory {
@@ -75,8 +73,8 @@
std::string dump() const;
private:
- friend LayerHistoryTest;
- friend TestableScheduler;
+ friend class LayerHistoryTest;
+ friend class TestableScheduler;
using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
using LayerInfos = std::vector<LayerPair>;
diff --git a/services/surfaceflinger/Scheduler/RefreshRateConfigs.h b/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
index 8a1c206..492feb1 100644
--- a/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
+++ b/services/surfaceflinger/Scheduler/RefreshRateConfigs.h
@@ -37,11 +37,11 @@
using namespace std::chrono_literals;
-enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
+enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
-inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
- using T = std::underlying_type_t<RefreshRateConfigEvent>;
- return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
+inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
+ using T = std::underlying_type_t<DisplayModeEvent>;
+ return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
}
using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
diff --git a/services/surfaceflinger/Scheduler/Scheduler.cpp b/services/surfaceflinger/Scheduler/Scheduler.cpp
index 4d72798..cbe4552 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.cpp
+++ b/services/surfaceflinger/Scheduler/Scheduler.cpp
@@ -46,11 +46,8 @@
#include "OneShotTimer.h"
#include "SchedulerUtils.h"
#include "SurfaceFlingerProperties.h"
-#include "Timer.h"
-#include "VSyncDispatchTimerQueue.h"
#include "VSyncPredictor.h"
#include "VSyncReactor.h"
-#include "VsyncController.h"
#define RETURN_IF_INVALID_HANDLE(handle, ...) \
do { \
@@ -60,68 +57,14 @@
} \
} while (false)
-using namespace std::string_literals;
+namespace android::scheduler {
-namespace android {
-
-using gui::WindowInfo;
-
-namespace {
-
-std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() {
- // TODO(b/144707443): Tune constants.
- constexpr int kDefaultRate = 60;
- constexpr auto initialPeriod = std::chrono::duration<nsecs_t, std::ratio<1, kDefaultRate>>(1);
- constexpr nsecs_t idealPeriod =
- std::chrono::duration_cast<std::chrono::nanoseconds>(initialPeriod).count();
- constexpr size_t vsyncTimestampHistorySize = 20;
- constexpr size_t minimumSamplesForPrediction = 6;
- constexpr uint32_t discardOutlierPercent = 20;
- return std::make_unique<scheduler::VSyncPredictor>(idealPeriod, vsyncTimestampHistorySize,
- minimumSamplesForPrediction,
- discardOutlierPercent);
-}
-
-std::unique_ptr<scheduler::VSyncDispatch> createVSyncDispatch(scheduler::VSyncTracker& tracker) {
- // TODO(b/144707443): Tune constants.
- constexpr std::chrono::nanoseconds vsyncMoveThreshold = 3ms;
- constexpr std::chrono::nanoseconds timerSlack = 500us;
- return std::make_unique<
- scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), tracker,
- timerSlack.count(), vsyncMoveThreshold.count());
-}
-
-const char* toContentDetectionString(bool useContentDetection) {
- return useContentDetection ? "on" : "off";
-}
-
-} // namespace
-
-class PredictedVsyncTracer {
-public:
- PredictedVsyncTracer(scheduler::VSyncDispatch& dispatch)
- : mRegistration(dispatch, std::bind(&PredictedVsyncTracer::callback, this),
- "PredictedVsyncTracer") {
- scheduleRegistration();
- }
-
-private:
- TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
- scheduler::VSyncCallbackRegistration mRegistration;
-
- void scheduleRegistration() { mRegistration.schedule({0, 0, 0}); }
-
- void callback() {
- mParity = !mParity;
- scheduleRegistration();
- }
-};
-
-Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, Options options)
- : impl::MessageQueue(compositor), mOptions(options), mSchedulerCallback(callback) {}
+Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features)
+ : impl::MessageQueue(compositor), mFeatures(features), mSchedulerCallback(callback) {}
void Scheduler::startTimers() {
using namespace sysprop;
+ using namespace std::string_literals;
if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
// Touch events are coming to SF every 100ms, so the timer needs to be higher than that
@@ -154,27 +97,14 @@
}
}
-void Scheduler::createVsyncSchedule(bool supportKernelTimer) {
- auto clock = std::make_unique<scheduler::SystemClock>();
- auto tracker = createVSyncTracker();
- auto dispatch = createVSyncDispatch(*tracker);
-
- // TODO(b/144707443): Tune constants.
- constexpr size_t pendingFenceLimit = 20;
- auto controller =
- std::make_unique<scheduler::VSyncReactor>(std::move(clock), *tracker, pendingFenceLimit,
- supportKernelTimer);
- mVsyncSchedule = {std::move(controller), std::move(tracker), std::move(dispatch)};
-
- if (base::GetBoolProperty("debug.sf.show_predicted_vsync", false)) {
- mPredictedVsyncTracer = std::make_unique<PredictedVsyncTracer>(*mVsyncSchedule.dispatch);
- }
+void Scheduler::createVsyncSchedule(FeatureFlags features) {
+ mVsyncSchedule.emplace(features);
}
std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
const char* name, std::chrono::nanoseconds workDuration,
std::chrono::nanoseconds readyDuration, bool traceVsync) {
- return std::make_unique<scheduler::DispSyncSource>(*mVsyncSchedule.dispatch, workDuration,
+ return std::make_unique<scheduler::DispSyncSource>(getVsyncDispatch(), workDuration,
readyDuration, traceVsync, name);
}
@@ -210,7 +140,7 @@
return true;
}
- return mVsyncSchedule.tracker->isVSyncInPhase(expectedVsyncTimestamp, *frameRate);
+ return mVsyncSchedule->getTracker().isVSyncInPhase(expectedVsyncTimestamp, *frameRate);
}
impl::EventThread::ThrottleVsyncCallback Scheduler::makeThrottleVsyncCallback() const {
@@ -245,7 +175,7 @@
};
}
-Scheduler::ConnectionHandle Scheduler::createConnection(
+ConnectionHandle Scheduler::createConnection(
const char* connectionName, frametimeline::TokenManager* tokenManager,
std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration,
impl::EventThread::InterceptVSyncsCallback interceptCallback) {
@@ -259,7 +189,7 @@
return createConnection(std::move(eventThread));
}
-Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
+ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
@@ -346,24 +276,24 @@
void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, DisplayModePtr mode) {
{
- std::lock_guard<std::mutex> lock(mFeatureStateLock);
+ std::lock_guard<std::mutex> lock(mPolicyLock);
// Cache the last reported modes for primary display.
- mFeatures.cachedModeChangedParams = {handle, mode};
+ mPolicy.cachedModeChangedParams = {handle, mode};
// Invalidate content based refresh rate selection so it could be calculated
// again for the new refresh rate.
- mFeatures.contentRequirements.clear();
+ mPolicy.contentRequirements.clear();
}
onNonPrimaryDisplayModeChanged(handle, mode);
}
void Scheduler::dispatchCachedReportedMode() {
// Check optional fields first.
- if (!mFeatures.mode) {
+ if (!mPolicy.mode) {
ALOGW("No mode ID found, not dispatching cached mode.");
return;
}
- if (!mFeatures.cachedModeChangedParams.has_value()) {
+ if (!mPolicy.cachedModeChangedParams) {
ALOGW("No mode changed params found, not dispatching cached mode.");
return;
}
@@ -372,18 +302,18 @@
// mode change is in progress. In that case we shouldn't dispatch an event
// as it will be dispatched when the current mode changes.
if (std::scoped_lock lock(mRefreshRateConfigsLock);
- mRefreshRateConfigs->getCurrentRefreshRate().getMode() != mFeatures.mode) {
+ mRefreshRateConfigs->getCurrentRefreshRate().getMode() != mPolicy.mode) {
return;
}
// If there is no change from cached mode, there is no need to dispatch an event
- if (mFeatures.mode == mFeatures.cachedModeChangedParams->mode) {
+ if (mPolicy.mode == mPolicy.cachedModeChangedParams->mode) {
return;
}
- mFeatures.cachedModeChangedParams->mode = mFeatures.mode;
- onNonPrimaryDisplayModeChanged(mFeatures.cachedModeChangedParams->handle,
- mFeatures.cachedModeChangedParams->mode);
+ mPolicy.cachedModeChangedParams->mode = mPolicy.mode;
+ onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->handle,
+ mPolicy.cachedModeChangedParams->mode);
}
void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, DisplayModePtr mode) {
@@ -424,12 +354,12 @@
}
DisplayStatInfo Scheduler::getDisplayStatInfo(nsecs_t now) {
- const auto vsyncTime = mVsyncSchedule.tracker->nextAnticipatedVSyncTimeFrom(now);
- const auto vsyncPeriod = mVsyncSchedule.tracker->currentPeriod();
+ const auto vsyncTime = mVsyncSchedule->getTracker().nextAnticipatedVSyncTimeFrom(now);
+ const auto vsyncPeriod = mVsyncSchedule->getTracker().currentPeriod();
return DisplayStatInfo{.vsyncTime = vsyncTime, .vsyncPeriod = vsyncPeriod};
}
-Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
+ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
if (mInjectVSyncs == enable) {
return {};
}
@@ -470,7 +400,7 @@
void Scheduler::enableHardwareVsync() {
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
- mVsyncSchedule.tracker->resetModel();
+ mVsyncSchedule->getTracker().resetModel();
mSchedulerCallback.setVsyncEnabled(true);
mPrimaryHWVsyncEnabled = true;
}
@@ -523,10 +453,10 @@
void Scheduler::setVsyncPeriod(nsecs_t period) {
std::lock_guard<std::mutex> lock(mHWVsyncLock);
- mVsyncSchedule.controller->startPeriodTransition(period);
+ mVsyncSchedule->getController().startPeriodTransition(period);
if (!mPrimaryHWVsyncEnabled) {
- mVsyncSchedule.tracker->resetModel();
+ mVsyncSchedule->getTracker().resetModel();
mSchedulerCallback.setVsyncEnabled(true);
mPrimaryHWVsyncEnabled = true;
}
@@ -539,8 +469,9 @@
{ // Scope for the lock
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (mPrimaryHWVsyncEnabled) {
- needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
- periodFlushed);
+ needsHwVsync =
+ mVsyncSchedule->getController().addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
+ periodFlushed);
}
}
@@ -551,24 +482,23 @@
}
}
-void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
- if (mVsyncSchedule.controller->addPresentFence(fenceTime)) {
+void Scheduler::addPresentFence(std::shared_ptr<FenceTime> fence) {
+ if (mVsyncSchedule->getController().addPresentFence(std::move(fence))) {
enableHardwareVsync();
} else {
disableHardwareVsync(false);
}
}
-void Scheduler::setIgnorePresentFences(bool ignore) {
- mVsyncSchedule.controller->setIgnorePresentFences(ignore);
-}
-
void Scheduler::registerLayer(Layer* layer) {
+ using WindowType = gui::WindowInfo::Type;
+
scheduler::LayerHistory::LayerVoteType voteType;
- if (!mOptions.useContentDetection || layer->getWindowType() == WindowInfo::Type::STATUS_BAR) {
+ if (!mFeatures.test(Feature::kContentDetection) ||
+ layer->getWindowType() == WindowType::STATUS_BAR) {
voteType = scheduler::LayerHistory::LayerVoteType::NoVote;
- } else if (layer->getWindowType() == WindowInfo::Type::WALLPAPER) {
+ } else if (layer->getWindowType() == WindowType::WALLPAPER) {
// Running Wallpaper at Min is considered as part of content detection.
voteType = scheduler::LayerHistory::LayerVoteType::Min;
} else {
@@ -615,13 +545,13 @@
bool frameRateChanged;
bool frameRateOverridesChanged;
{
- std::lock_guard<std::mutex> lock(mFeatureStateLock);
- mFeatures.contentRequirements = summary;
+ std::lock_guard<std::mutex> lock(mPolicyLock);
+ mPolicy.contentRequirements = summary;
newMode = calculateRefreshRateModeId(&consideredSignals);
frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, newMode->getFps());
- if (mFeatures.mode == newMode) {
+ if (mPolicy.mode == newMode) {
// We don't need to change the display mode, but we might need to send an event
// about a mode change, since it was suppressed due to a previous idleConsidered
if (!consideredSignals.idle) {
@@ -629,15 +559,16 @@
}
frameRateChanged = false;
} else {
- mFeatures.mode = newMode;
+ mPolicy.mode = newMode;
frameRateChanged = true;
}
}
if (frameRateChanged) {
- auto newRefreshRate = refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
+ const auto newRefreshRate = refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
+
mSchedulerCallback.changeRefreshRate(newRefreshRate,
- consideredSignals.idle ? ModeEvent::None
- : ModeEvent::Changed);
+ consideredSignals.idle ? DisplayModeEvent::None
+ : DisplayModeEvent::Changed);
}
if (frameRateOverridesChanged) {
mSchedulerCallback.triggerOnFrameRateOverridesChanged();
@@ -660,8 +591,8 @@
void Scheduler::setDisplayPowerState(bool normal) {
{
- std::lock_guard<std::mutex> lock(mFeatureStateLock);
- mFeatures.isDisplayPowerStateNormal = normal;
+ std::lock_guard<std::mutex> lock(mPolicyLock);
+ mPolicy.isDisplayPowerStateNormal = normal;
}
if (mDisplayPowerTimer) {
@@ -703,7 +634,7 @@
}
void Scheduler::idleTimerCallback(TimerState state) {
- handleTimerStateChanged(&mFeatures.idleTimer, state);
+ handleTimerStateChanged(&mPolicy.idleTimer, state);
ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
}
@@ -713,14 +644,14 @@
// Clear layer history to get fresh FPS detection.
// NOTE: Instead of checking all the layers, we should be checking the layer
// that is currently on top. b/142507166 will give us this capability.
- if (handleTimerStateChanged(&mFeatures.touch, touch)) {
+ if (handleTimerStateChanged(&mPolicy.touch, touch)) {
mLayerHistory.clear();
}
ATRACE_INT("TouchState", static_cast<int>(touch));
}
void Scheduler::displayPowerTimerCallback(TimerState state) {
- handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
+ handleTimerStateChanged(&mPolicy.displayPowerTimer, state);
ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
}
@@ -730,7 +661,7 @@
StringAppendF(&result, "+ Touch timer: %s\n",
mTouchTimer ? mTouchTimer->dump().c_str() : "off");
StringAppendF(&result, "+ Content detection: %s %s\n\n",
- toContentDetectionString(mOptions.useContentDetection),
+ mFeatures.test(Feature::kContentDetection) ? "on" : "off",
mLayerHistory.dump().c_str());
{
@@ -756,13 +687,8 @@
}
}
-void Scheduler::dumpVsync(std::string& s) const {
- using base::StringAppendF;
-
- StringAppendF(&s, "VSyncReactor:\n");
- mVsyncSchedule.controller->dump(s);
- StringAppendF(&s, "VSyncDispatch:\n");
- mVsyncSchedule.dispatch->dump(s);
+void Scheduler::dumpVsync(std::string& out) const {
+ mVsyncSchedule->dump(out);
}
bool Scheduler::updateFrameRateOverrides(
@@ -774,7 +700,7 @@
if (!consideredSignals.idle) {
const auto frameRateOverrides =
- refreshRateConfigs->getFrameRateOverrides(mFeatures.contentRequirements,
+ refreshRateConfigs->getFrameRateOverrides(mPolicy.contentRequirements,
displayRefreshRate, consideredSignals);
std::lock_guard lock(mFrameRateOverridesLock);
if (!std::equal(mFrameRateOverridesByContent.begin(), mFrameRateOverridesByContent.end(),
@@ -797,31 +723,30 @@
scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
const auto refreshRateConfigs = holdRefreshRateConfigs();
{
- std::lock_guard<std::mutex> lock(mFeatureStateLock);
+ std::lock_guard<std::mutex> lock(mPolicyLock);
if (*currentState == newState) {
return false;
}
*currentState = newState;
newMode = calculateRefreshRateModeId(&consideredSignals);
frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, newMode->getFps());
- if (mFeatures.mode == newMode) {
+ if (mPolicy.mode == newMode) {
// We don't need to change the display mode, but we might need to send an event
// about a mode change, since it was suppressed due to a previous idleConsidered
if (!consideredSignals.idle) {
dispatchCachedReportedMode();
}
} else {
- mFeatures.mode = newMode;
+ mPolicy.mode = newMode;
refreshRateChanged = true;
}
}
if (refreshRateChanged) {
- const RefreshRate& newRefreshRate =
- refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
+ const auto newRefreshRate = refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
mSchedulerCallback.changeRefreshRate(newRefreshRate,
- consideredSignals.idle ? ModeEvent::None
- : ModeEvent::Changed);
+ consideredSignals.idle ? DisplayModeEvent::None
+ : DisplayModeEvent::Changed);
}
if (frameRateOverridesChanged) {
mSchedulerCallback.triggerOnFrameRateOverridesChanged();
@@ -838,27 +763,26 @@
// If Display Power is not in normal operation we want to be in performance mode. When coming
// back to normal mode, a grace period is given with DisplayPowerTimer.
if (mDisplayPowerTimer &&
- (!mFeatures.isDisplayPowerStateNormal ||
- mFeatures.displayPowerTimer == TimerState::Reset)) {
+ (!mPolicy.isDisplayPowerStateNormal || mPolicy.displayPowerTimer == TimerState::Reset)) {
return refreshRateConfigs->getMaxRefreshRateByPolicy().getMode();
}
- const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
- const bool idle = mFeatures.idleTimer == TimerState::Expired;
+ const bool touchActive = mTouchTimer && mPolicy.touch == TouchState::Active;
+ const bool idle = mPolicy.idleTimer == TimerState::Expired;
return refreshRateConfigs
- ->getBestRefreshRate(mFeatures.contentRequirements,
- {.touch = touchActive, .idle = idle}, consideredSignals)
+ ->getBestRefreshRate(mPolicy.contentRequirements, {.touch = touchActive, .idle = idle},
+ consideredSignals)
.getMode();
}
DisplayModePtr Scheduler::getPreferredDisplayMode() {
- std::lock_guard<std::mutex> lock(mFeatureStateLock);
+ std::lock_guard<std::mutex> lock(mPolicyLock);
// Make sure that the default mode ID is first updated, before returned.
- if (mFeatures.mode) {
- mFeatures.mode = calculateRefreshRateModeId();
+ if (mPolicy.mode) {
+ mPolicy.mode = calculateRefreshRateModeId();
}
- return mFeatures.mode;
+ return mPolicy.mode;
}
void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
@@ -915,8 +839,8 @@
std::chrono::steady_clock::time_point Scheduler::getPreviousVsyncFrom(
nsecs_t expectedPresentTime) const {
const auto presentTime = std::chrono::nanoseconds(expectedPresentTime);
- const auto vsyncPeriod = std::chrono::nanoseconds(mVsyncSchedule.tracker->currentPeriod());
+ const auto vsyncPeriod = std::chrono::nanoseconds(mVsyncSchedule->getTracker().currentPeriod());
return std::chrono::steady_clock::time_point(presentTime - vsyncPeriod);
}
-} // namespace android
+} // namespace android::scheduler
diff --git a/services/surfaceflinger/Scheduler/Scheduler.h b/services/surfaceflinger/Scheduler/Scheduler.h
index 2a6de54..e127ff7 100644
--- a/services/surfaceflinger/Scheduler/Scheduler.h
+++ b/services/surfaceflinger/Scheduler/Scheduler.h
@@ -31,40 +31,37 @@
#include <ui/GraphicTypes.h>
#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
+#include <scheduler/Features.h>
+
#include "EventThread.h"
#include "LayerHistory.h"
#include "MessageQueue.h"
#include "OneShotTimer.h"
#include "RefreshRateConfigs.h"
#include "SchedulerUtils.h"
+#include "VsyncSchedule.h"
namespace android {
-using namespace std::chrono_literals;
-using scheduler::LayerHistory;
-
class FenceTime;
class InjectVSyncSource;
-class PredictedVsyncTracer;
-
-namespace scheduler {
-class VsyncController;
-class VSyncDispatch;
-class VSyncTracker;
-} // namespace scheduler
namespace frametimeline {
class TokenManager;
} // namespace frametimeline
+namespace scheduler {
+
struct ISchedulerCallback {
// Indicates frame activity, i.e. whether commit and/or composite is taking place.
enum class FrameHint { kNone, kActive };
+ using RefreshRate = RefreshRateConfigs::RefreshRate;
+ using DisplayModeEvent = scheduler::DisplayModeEvent;
+
virtual void scheduleComposite(FrameHint) = 0;
virtual void setVsyncEnabled(bool) = 0;
- virtual void changeRefreshRate(const scheduler::RefreshRateConfigs::RefreshRate&,
- scheduler::RefreshRateConfigEvent) = 0;
+ virtual void changeRefreshRate(const RefreshRate&, DisplayModeEvent) = 0;
virtual void kernelTimerChanged(bool expired) = 0;
virtual void triggerOnFrameRateOverridesChanged() = 0;
@@ -76,18 +73,10 @@
using Impl = impl::MessageQueue;
public:
- using RefreshRate = scheduler::RefreshRateConfigs::RefreshRate;
- using ModeEvent = scheduler::RefreshRateConfigEvent;
-
- struct Options {
- // Whether to use content detection at all.
- bool useContentDetection;
- };
-
- Scheduler(ICompositor&, ISchedulerCallback&, Options);
+ Scheduler(ICompositor&, ISchedulerCallback&, FeatureFlags);
~Scheduler();
- void createVsyncSchedule(bool supportKernelIdleTimer);
+ void createVsyncSchedule(FeatureFlags);
void startTimers();
void run();
@@ -107,7 +96,6 @@
return std::move(future);
}
- using ConnectionHandle = scheduler::ConnectionHandle;
ConnectionHandle createConnection(const char* connectionName, frametimeline::TokenManager*,
std::chrono::nanoseconds workDuration,
std::chrono::nanoseconds readyDuration,
@@ -119,7 +107,7 @@
sp<EventThreadConnection> getEventConnection(ConnectionHandle);
void onHotplugReceived(ConnectionHandle, PhysicalDisplayId, bool connected);
- void onPrimaryDisplayModeChanged(ConnectionHandle, DisplayModePtr) EXCLUDES(mFeatureStateLock);
+ void onPrimaryDisplayModeChanged(ConnectionHandle, DisplayModePtr) EXCLUDES(mPolicyLock);
void onNonPrimaryDisplayModeChanged(ConnectionHandle, DisplayModePtr);
void onScreenAcquired(ConnectionHandle);
void onScreenReleased(ConnectionHandle);
@@ -152,8 +140,7 @@
// VsyncController detected that the vsync period changed, and false otherwise.
void addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
bool* periodFlushed);
- void addPresentFence(const std::shared_ptr<FenceTime>&);
- void setIgnorePresentFences(bool ignore);
+ void addPresentFence(std::shared_ptr<FenceTime>);
// Layers are registered on creation, and unregistered when the weak reference expires.
void registerLayer(Layer*);
@@ -172,7 +159,7 @@
void setDisplayPowerState(bool normal);
- scheduler::VSyncDispatch& getVsyncDispatch() { return *mVsyncSchedule.dispatch; }
+ VSyncDispatch& getVsyncDispatch() { return mVsyncSchedule->getDispatch(); }
// Returns true if a given vsync timestamp is considered valid vsync
// for a given uid
@@ -211,7 +198,7 @@
std::optional<Fps> getFrameRateOverride(uid_t uid) const
EXCLUDES(mRefreshRateConfigsLock, mFrameRateOverridesLock);
- void setRefreshRateConfigs(std::shared_ptr<scheduler::RefreshRateConfigs> refreshRateConfigs)
+ void setRefreshRateConfigs(std::shared_ptr<RefreshRateConfigs> refreshRateConfigs)
EXCLUDES(mRefreshRateConfigsLock) {
// We need to stop the idle timer on the previous RefreshRateConfigs instance
// and cleanup the scheduler's state before we switch to the other RefreshRateConfigs.
@@ -220,8 +207,8 @@
if (mRefreshRateConfigs) mRefreshRateConfigs->stopIdleTimer();
}
{
- std::scoped_lock lock(mFeatureStateLock);
- mFeatures = {};
+ std::scoped_lock lock(mPolicyLock);
+ mPolicy = {};
}
{
std::scoped_lock lock(mRefreshRateConfigsLock);
@@ -251,18 +238,10 @@
using FrameHint = ISchedulerCallback::FrameHint;
- // In order to make sure that the features don't override themselves, we need a state machine
- // to keep track which feature requested the config change.
enum class ContentDetectionState { Off, On };
enum class TimerState { Reset, Expired };
enum class TouchState { Inactive, Active };
- struct VsyncSchedule {
- std::unique_ptr<scheduler::VsyncController> controller;
- std::unique_ptr<scheduler::VSyncTracker> tracker;
- std::unique_ptr<scheduler::VSyncDispatch> dispatch;
- };
-
// Create a connection on the given EventThread.
ConnectionHandle createConnection(std::unique_ptr<EventThread>);
sp<EventThreadConnection> createConnectionInternal(
@@ -284,19 +263,17 @@
// selection were initialized, prioritizes them, and calculates the DisplayModeId
// for the suggested refresh rate.
DisplayModePtr calculateRefreshRateModeId(
- scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals = nullptr)
- REQUIRES(mFeatureStateLock);
+ RefreshRateConfigs::GlobalSignals* consideredSignals = nullptr) REQUIRES(mPolicyLock);
- void dispatchCachedReportedMode() REQUIRES(mFeatureStateLock) EXCLUDES(mRefreshRateConfigsLock);
- bool updateFrameRateOverrides(scheduler::RefreshRateConfigs::GlobalSignals consideredSignals,
- Fps displayRefreshRate) REQUIRES(mFeatureStateLock)
- EXCLUDES(mFrameRateOverridesLock);
+ void dispatchCachedReportedMode() REQUIRES(mPolicyLock) EXCLUDES(mRefreshRateConfigsLock);
+ bool updateFrameRateOverrides(RefreshRateConfigs::GlobalSignals, Fps displayRefreshRate)
+ REQUIRES(mPolicyLock) EXCLUDES(mFrameRateOverridesLock);
impl::EventThread::ThrottleVsyncCallback makeThrottleVsyncCallback() const
EXCLUDES(mRefreshRateConfigsLock);
impl::EventThread::GetVsyncPeriodFunction makeGetVsyncPeriodFunction() const;
- std::shared_ptr<scheduler::RefreshRateConfigs> holdRefreshRateConfigs() const
+ std::shared_ptr<RefreshRateConfigs> holdRefreshRateConfigs() const
EXCLUDES(mRefreshRateConfigsLock) {
std::scoped_lock lock(mRefreshRateConfigsLock);
return mRefreshRateConfigs;
@@ -322,66 +299,63 @@
std::atomic<nsecs_t> mLastResyncTime = 0;
- const Options mOptions;
- VsyncSchedule mVsyncSchedule;
+ const FeatureFlags mFeatures;
+ std::optional<VsyncSchedule> mVsyncSchedule;
// Used to choose refresh rate if content detection is enabled.
LayerHistory mLayerHistory;
// Timer used to monitor touch events.
- std::optional<scheduler::OneShotTimer> mTouchTimer;
+ std::optional<OneShotTimer> mTouchTimer;
// Timer used to monitor display power mode.
- std::optional<scheduler::OneShotTimer> mDisplayPowerTimer;
+ std::optional<OneShotTimer> mDisplayPowerTimer;
ISchedulerCallback& mSchedulerCallback;
- // In order to make sure that the features don't override themselves, we need a state machine
- // to keep track which feature requested the config change.
- mutable std::mutex mFeatureStateLock;
+ mutable std::mutex mPolicyLock;
struct {
+ // Policy for choosing the display mode.
+ LayerHistory::Summary contentRequirements;
TimerState idleTimer = TimerState::Reset;
TouchState touch = TouchState::Inactive;
TimerState displayPowerTimer = TimerState::Expired;
-
- DisplayModePtr mode;
- LayerHistory::Summary contentRequirements;
-
bool isDisplayPowerStateNormal = true;
- // Used to cache the last parameters of onPrimaryDisplayModeChanged
+ // Chosen display mode.
+ DisplayModePtr mode;
+
struct ModeChangedParams {
ConnectionHandle handle;
DisplayModePtr mode;
};
+ // Parameters for latest dispatch of mode change event.
std::optional<ModeChangedParams> cachedModeChangedParams;
- } mFeatures GUARDED_BY(mFeatureStateLock);
+ } mPolicy GUARDED_BY(mPolicyLock);
mutable std::mutex mRefreshRateConfigsLock;
- std::shared_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs
- GUARDED_BY(mRefreshRateConfigsLock);
+ std::shared_ptr<RefreshRateConfigs> mRefreshRateConfigs GUARDED_BY(mRefreshRateConfigsLock);
std::mutex mVsyncTimelineLock;
std::optional<hal::VsyncPeriodChangeTimeline> mLastVsyncPeriodChangeTimeline
GUARDED_BY(mVsyncTimelineLock);
static constexpr std::chrono::nanoseconds MAX_VSYNC_APPLIED_TIME = 200ms;
- std::unique_ptr<PredictedVsyncTracer> mPredictedVsyncTracer;
-
// The frame rate override lists need their own mutex as they are being read
// by SurfaceFlinger, Scheduler and EventThread (as a callback) to prevent deadlocks
mutable std::mutex mFrameRateOverridesLock;
// mappings between a UID and a preferred refresh rate that this app would
// run at.
- scheduler::RefreshRateConfigs::UidToFrameRateOverride mFrameRateOverridesByContent
+ RefreshRateConfigs::UidToFrameRateOverride mFrameRateOverridesByContent
GUARDED_BY(mFrameRateOverridesLock);
- scheduler::RefreshRateConfigs::UidToFrameRateOverride mFrameRateOverridesFromBackdoor
+ RefreshRateConfigs::UidToFrameRateOverride mFrameRateOverridesFromBackdoor
GUARDED_BY(mFrameRateOverridesLock);
// Keeps track of whether the screen is acquired for debug
std::atomic<bool> mScreenAcquired = false;
};
+} // namespace scheduler
} // namespace android
diff --git a/services/surfaceflinger/Scheduler/VSyncReactor.cpp b/services/surfaceflinger/Scheduler/VSyncReactor.cpp
index ee973f7..1c9de1c 100644
--- a/services/surfaceflinger/Scheduler/VSyncReactor.cpp
+++ b/services/surfaceflinger/Scheduler/VSyncReactor.cpp
@@ -47,7 +47,7 @@
VSyncReactor::~VSyncReactor() = default;
-bool VSyncReactor::addPresentFence(const std::shared_ptr<android::FenceTime>& fence) {
+bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
if (!fence) {
return false;
}
@@ -80,7 +80,7 @@
if (mPendingLimit == mUnfiredFences.size()) {
mUnfiredFences.erase(mUnfiredFences.begin());
}
- mUnfiredFences.push_back(fence);
+ mUnfiredFences.push_back(std::move(fence));
} else {
timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
}
diff --git a/services/surfaceflinger/Scheduler/VSyncReactor.h b/services/surfaceflinger/Scheduler/VSyncReactor.h
index 449d4c3..a9d536b 100644
--- a/services/surfaceflinger/Scheduler/VSyncReactor.h
+++ b/services/surfaceflinger/Scheduler/VSyncReactor.h
@@ -37,7 +37,7 @@
bool supportKernelIdleTimer);
~VSyncReactor();
- bool addPresentFence(const std::shared_ptr<android::FenceTime>& fence) final;
+ bool addPresentFence(std::shared_ptr<FenceTime>) final;
void setIgnorePresentFences(bool ignore) final;
void startPeriodTransition(nsecs_t period) final;
diff --git a/services/surfaceflinger/Scheduler/VsyncController.h b/services/surfaceflinger/Scheduler/VsyncController.h
index 0f0df22..59f6537 100644
--- a/services/surfaceflinger/Scheduler/VsyncController.h
+++ b/services/surfaceflinger/Scheduler/VsyncController.h
@@ -17,19 +17,15 @@
#pragma once
#include <cstddef>
+#include <memory>
+#include <ui/FenceTime.h>
#include <utils/Mutex.h>
#include <utils/RefBase.h>
#include <utils/Timers.h>
-#include <ui/FenceTime.h>
-
-#include <memory>
-
namespace android::scheduler {
-class FenceTime;
-
class VsyncController {
public:
virtual ~VsyncController();
@@ -43,7 +39,7 @@
* an accurate prediction,
* False otherwise
*/
- virtual bool addPresentFence(const std::shared_ptr<android::FenceTime>&) = 0;
+ virtual bool addPresentFence(std::shared_ptr<FenceTime>) = 0;
/*
* Adds a hw sync timestamp to the model. The controller will use the timestamp
diff --git a/services/surfaceflinger/Scheduler/VsyncSchedule.cpp b/services/surfaceflinger/Scheduler/VsyncSchedule.cpp
new file mode 100644
index 0000000..77d1223
--- /dev/null
+++ b/services/surfaceflinger/Scheduler/VsyncSchedule.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <scheduler/Fps.h>
+
+#include "VsyncSchedule.h"
+
+#include "Timer.h"
+#include "VSyncDispatchTimerQueue.h"
+#include "VSyncPredictor.h"
+#include "VSyncReactor.h"
+
+#include "../TracedOrdinal.h"
+
+namespace android::scheduler {
+
+class VsyncSchedule::PredictedVsyncTracer {
+ // Invoked from the thread of the VsyncDispatch owned by this VsyncSchedule.
+ constexpr auto makeVsyncCallback() {
+ return [this](nsecs_t, nsecs_t, nsecs_t) {
+ mParity = !mParity;
+ schedule();
+ };
+ }
+
+public:
+ explicit PredictedVsyncTracer(VsyncDispatch& dispatch)
+ : mRegistration(dispatch, makeVsyncCallback(), __func__) {
+ schedule();
+ }
+
+private:
+ void schedule() { mRegistration.schedule({0, 0, 0}); }
+
+ TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
+ VSyncCallbackRegistration mRegistration;
+};
+
+VsyncSchedule::VsyncSchedule(FeatureFlags features)
+ : mTracker(createTracker()),
+ mDispatch(createDispatch(*mTracker)),
+ mController(createController(*mTracker, features)) {
+ if (features.test(Feature::kTracePredictedVsync)) {
+ mTracer = std::make_unique<PredictedVsyncTracer>(*mDispatch);
+ }
+}
+
+VsyncSchedule::VsyncSchedule(TrackerPtr tracker, DispatchPtr dispatch, ControllerPtr controller)
+ : mTracker(std::move(tracker)),
+ mDispatch(std::move(dispatch)),
+ mController(std::move(controller)) {}
+
+VsyncSchedule::VsyncSchedule(VsyncSchedule&&) = default;
+VsyncSchedule::~VsyncSchedule() = default;
+
+void VsyncSchedule::dump(std::string& out) const {
+ out.append("VsyncController:\n");
+ mController->dump(out);
+
+ out.append("VsyncDispatch:\n");
+ mDispatch->dump(out);
+}
+
+VsyncSchedule::TrackerPtr VsyncSchedule::createTracker() {
+ // TODO(b/144707443): Tune constants.
+ constexpr nsecs_t kInitialPeriod = (60_Hz).getPeriodNsecs();
+ constexpr size_t kHistorySize = 20;
+ constexpr size_t kMinSamplesForPrediction = 6;
+ constexpr uint32_t kDiscardOutlierPercent = 20;
+
+ return std::make_unique<VSyncPredictor>(kInitialPeriod, kHistorySize, kMinSamplesForPrediction,
+ kDiscardOutlierPercent);
+}
+
+VsyncSchedule::DispatchPtr VsyncSchedule::createDispatch(VsyncTracker& tracker) {
+ using namespace std::chrono_literals;
+
+ // TODO(b/144707443): Tune constants.
+ constexpr std::chrono::nanoseconds kGroupDispatchWithin = 500us;
+ constexpr std::chrono::nanoseconds kSnapToSameVsyncWithin = 3ms;
+
+ return std::make_unique<VSyncDispatchTimerQueue>(std::make_unique<Timer>(), tracker,
+ kGroupDispatchWithin.count(),
+ kSnapToSameVsyncWithin.count());
+}
+
+VsyncSchedule::ControllerPtr VsyncSchedule::createController(VsyncTracker& tracker,
+ FeatureFlags features) {
+ // TODO(b/144707443): Tune constants.
+ constexpr size_t kMaxPendingFences = 20;
+ const bool hasKernelIdleTimer = features.test(Feature::kKernelIdleTimer);
+
+ auto reactor = std::make_unique<VSyncReactor>(std::make_unique<SystemClock>(), tracker,
+ kMaxPendingFences, hasKernelIdleTimer);
+
+ reactor->setIgnorePresentFences(!features.test(Feature::kPresentFences));
+ return reactor;
+}
+
+} // namespace android::scheduler
diff --git a/services/surfaceflinger/Scheduler/VsyncSchedule.h b/services/surfaceflinger/Scheduler/VsyncSchedule.h
new file mode 100644
index 0000000..0d9b114
--- /dev/null
+++ b/services/surfaceflinger/Scheduler/VsyncSchedule.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <scheduler/Features.h>
+
+namespace android::scheduler {
+
+// TODO(b/185535769): Rename classes, and remove aliases.
+class VSyncDispatch;
+class VSyncTracker;
+
+class VsyncController;
+using VsyncDispatch = VSyncDispatch;
+using VsyncTracker = VSyncTracker;
+
+// Schedule that synchronizes to hardware VSYNC of a physical display.
+class VsyncSchedule {
+public:
+ explicit VsyncSchedule(FeatureFlags);
+ VsyncSchedule(VsyncSchedule&&);
+ ~VsyncSchedule();
+
+ // TODO(b/185535769): Hide behind API.
+ const VsyncTracker& getTracker() const { return *mTracker; }
+ VsyncTracker& getTracker() { return *mTracker; }
+ VsyncController& getController() { return *mController; }
+
+ // TODO(b/185535769): Remove once VsyncSchedule owns all registrations.
+ VsyncDispatch& getDispatch() { return *mDispatch; }
+
+ void dump(std::string&) const;
+
+private:
+ friend class TestableScheduler;
+
+ using TrackerPtr = std::unique_ptr<VsyncTracker>;
+ using DispatchPtr = std::unique_ptr<VsyncDispatch>;
+ using ControllerPtr = std::unique_ptr<VsyncController>;
+
+ // For tests.
+ VsyncSchedule(TrackerPtr, DispatchPtr, ControllerPtr);
+
+ static TrackerPtr createTracker();
+ static DispatchPtr createDispatch(VsyncTracker&);
+ static ControllerPtr createController(VsyncTracker&, FeatureFlags);
+
+ class PredictedVsyncTracer;
+ using TracerPtr = std::unique_ptr<PredictedVsyncTracer>;
+
+ // Effectively const except in move constructor.
+ TrackerPtr mTracker;
+ DispatchPtr mDispatch;
+ ControllerPtr mController;
+ TracerPtr mTracer;
+};
+
+} // namespace android::scheduler
diff --git a/services/surfaceflinger/Scheduler/include/scheduler/Features.h b/services/surfaceflinger/Scheduler/include/scheduler/Features.h
new file mode 100644
index 0000000..0e96678
--- /dev/null
+++ b/services/surfaceflinger/Scheduler/include/scheduler/Features.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <ftl/Flags.h>
+
+#include <cstdint>
+
+namespace android::scheduler {
+
+enum class Feature : std::uint8_t {
+ kPresentFences = 0b1,
+ kKernelIdleTimer = 0b10,
+ kContentDetection = 0b100,
+ kTracePredictedVsync = 0b1000,
+};
+
+using FeatureFlags = Flags<Feature>;
+
+} // namespace android::scheduler