/* * Copyright 2018 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. */ #undef LOG_TAG #define LOG_TAG "Scheduler" #define ATRACE_TAG ATRACE_TAG_GRAPHICS #include "Scheduler.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../Layer.h" #include "Display/DisplayMap.h" #include "EventThread.h" #include "FrameRateOverrideMappings.h" #include "FrontEnd/LayerHandle.h" #include "OneShotTimer.h" #include "SurfaceFlingerProperties.h" #include "VSyncTracker.h" #include "VsyncController.h" #include "VsyncSchedule.h" #ifdef MTK_SF_DEBUG_SUPPORT #include "mediatek/MtkDebugAPI.h" #endif #ifdef MTK_SF_SCHEDULE_DELAY #include #endif #define RETURN_IF_INVALID_HANDLE(handle, ...) \ do { \ if (mConnections.count(handle) == 0) { \ ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \ return __VA_ARGS__; \ } \ } while (false) namespace android::scheduler { Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features, sp modulatorPtr) : impl::MessageQueue(compositor), mFeatures(features), mVsyncModulator(std::move(modulatorPtr)), mSchedulerCallback(callback) { #ifdef MTK_AOSP_DISPLAY_BUGFIX char value[PROPERTY_VALUE_MAX]; property_get("debug.sf.set_binder_thread_rt", value, "0"); mSetInheritRT = atoi(value); ALOGD("setInheritRT %d",mSetInheritRT); #endif } Scheduler::~Scheduler() { // MessageQueue depends on VsyncSchedule, so first destroy it. // Otherwise, MessageQueue will get destroyed after Scheduler's dtor, // which will cause a use-after-free issue. Impl::destroyVsync(); // Stop timers and wait for their threads to exit. mDisplayPowerTimer.reset(); mTouchTimer.reset(); // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler. demotePacesetterDisplay(); } void Scheduler::startTimers() { using namespace sysprop; using namespace std::string_literals; #ifdef MTK_SF_MSYNC_3 const int32_t touchTimerMs = base::GetIntProperty("debug.sf.set_touch_timer_ms", 0); if (const int64_t millis = touchTimerMs ? touchTimerMs : set_touch_timer_ms(0); millis > 0) { #else if (const int64_t millis = set_touch_timer_ms(0); millis > 0) { #endif // Touch events are coming to SF every 100ms, so the timer needs to be higher than that mTouchTimer.emplace( "TouchTimer", std::chrono::milliseconds(millis), [this] { touchTimerCallback(TimerState::Reset); }, [this] { touchTimerCallback(TimerState::Expired); }); mTouchTimer->start(); } if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) { mDisplayPowerTimer.emplace( "DisplayPowerTimer", std::chrono::milliseconds(millis), [this] { displayPowerTimerCallback(TimerState::Reset); }, [this] { displayPowerTimerCallback(TimerState::Expired); }); mDisplayPowerTimer->start(); } } void Scheduler::setPacesetterDisplay(std::optional pacesetterIdOpt) { demotePacesetterDisplay(); promotePacesetterDisplay(pacesetterIdOpt); } void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr) { registerDisplayInternal(displayId, std::move(selectorPtr), std::make_shared(displayId, mFeatures)); } void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr, VsyncSchedulePtr schedulePtr) { demotePacesetterDisplay(); std::shared_ptr pacesetterVsyncSchedule; { std::scoped_lock lock(mDisplayLock); mDisplays.emplace_or_replace(displayId, std::move(selectorPtr), std::move(schedulePtr)); pacesetterVsyncSchedule = promotePacesetterDisplayLocked(); } applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); } void Scheduler::unregisterDisplay(PhysicalDisplayId displayId) { demotePacesetterDisplay(); std::shared_ptr pacesetterVsyncSchedule; { std::scoped_lock lock(mDisplayLock); mDisplays.erase(displayId); // Do not allow removing the final display. Code in the scheduler expects // there to be at least one display. (This may be relaxed in the future with // headless virtual display.) LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!"); pacesetterVsyncSchedule = promotePacesetterDisplayLocked(); } applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); } void Scheduler::run() { while (true) { waitMessage(); } } void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId, TimePoint expectedVsyncTime) { const TimePoint frameTime = SchedulerClock::now(); if (!compositor.commit(frameTime, vsyncId, expectedVsyncTime)) { return; } compositor.composite(frameTime, vsyncId); compositor.sample(); #ifdef MTK_SF_DEBUG_SUPPORT slowMotion(); #endif } std::optional Scheduler::getFrameRateOverride(uid_t uid) const { const bool supportsFrameRateOverrideByContent = pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent(); return mFrameRateOverrideMappings .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent); } bool Scheduler::isVsyncValid(TimePoint expectedVsyncTimestamp, uid_t uid) const { const auto frameRate = getFrameRateOverride(uid); if (!frameRate.has_value()) { return true; } ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str()); return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTimestamp.ns(), *frameRate); } bool Scheduler::isVsyncInPhase(TimePoint timePoint, const Fps frameRate) const { return getVsyncSchedule()->getTracker().isVSyncInPhase(timePoint.ns(), frameRate); } impl::EventThread::ThrottleVsyncCallback Scheduler::makeThrottleVsyncCallback() const { return [this](nsecs_t expectedVsyncTimestamp, uid_t uid) { return !isVsyncValid(TimePoint::fromNs(expectedVsyncTimestamp), uid); }; } impl::EventThread::GetVsyncPeriodFunction Scheduler::makeGetVsyncPeriodFunction() const { return [this](uid_t uid) { const auto [refreshRate, period] = [this] { std::scoped_lock lock(mDisplayLock); const auto pacesetterOpt = pacesetterDisplayLocked(); LOG_ALWAYS_FATAL_IF(!pacesetterOpt); const Display& pacesetter = *pacesetterOpt; return std::make_pair(pacesetter.selectorPtr->getActiveMode().fps, pacesetter.schedulePtr->period()); }(); const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod(); const auto frameRate = getFrameRateOverride(uid); if (!frameRate.has_value()) { return currentPeriod.ns(); } const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate); if (divisor <= 1) { return currentPeriod.ns(); } return currentPeriod.ns() * divisor; }; } ConnectionHandle Scheduler::createEventThread(Cycle cycle, frametimeline::TokenManager* tokenManager, std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration) { auto eventThread = std::make_unique(cycle == Cycle::Render ? "app" : "appSf", getVsyncSchedule(), tokenManager, makeThrottleVsyncCallback(), makeGetVsyncPeriodFunction(), workDuration, readyDuration); auto& handle = cycle == Cycle::Render ? mAppConnectionHandle : mSfConnectionHandle; handle = createConnection(std::move(eventThread)); return handle; } ConnectionHandle Scheduler::createConnection(std::unique_ptr eventThread) { const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++}; ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id); auto connection = createConnectionInternal(eventThread.get()); std::lock_guard lock(mConnectionsLock); mConnections.emplace(handle, Connection{connection, std::move(eventThread)}); return handle; } sp Scheduler::createConnectionInternal( EventThread* eventThread, EventRegistrationFlags eventRegistration, const sp& layerHandle) { int32_t layerId = static_cast(LayerHandle::getLayerId(layerHandle)); auto connection = eventThread->createEventConnection([&] { resync(); }, eventRegistration); #ifdef MTK_AOSP_DISPLAY_BUGFIX if (mSetInheritRT == 1) { connection->setInheritRt(true); } #endif mLayerHistory.attachChoreographer(layerId, connection); return connection; } sp Scheduler::createDisplayEventConnection( ConnectionHandle handle, EventRegistrationFlags eventRegistration, const sp& layerHandle) { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle, nullptr); return createConnectionInternal(mConnections[handle].thread.get(), eventRegistration, layerHandle); } sp Scheduler::getEventConnection(ConnectionHandle handle) { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle, nullptr); return mConnections[handle].connection; } void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId, bool connected) { android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->onHotplugReceived(displayId, connected); } void Scheduler::enableSyntheticVsync(bool enable) { // TODO(b/241285945): Remove connection handles. const ConnectionHandle handle = mAppConnectionHandle; android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->enableSyntheticVsync(enable); } void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId) { const bool supportsFrameRateOverrideByContent = pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent(); std::vector overrides = mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent); android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->onFrameRateOverridesChanged(displayId, std::move(overrides)); } void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { { std::lock_guard lock(mPolicyLock); // Cache the last reported modes for primary display. mPolicy.cachedModeChangedParams = {handle, mode}; // Invalidate content based refresh rate selection so it could be calculated // again for the new refresh rate. mPolicy.contentRequirements.clear(); } onNonPrimaryDisplayModeChanged(handle, mode); } void Scheduler::dispatchCachedReportedMode() { // Check optional fields first. if (!mPolicy.modeOpt) { ALOGW("No mode ID found, not dispatching cached mode."); return; } if (!mPolicy.cachedModeChangedParams) { ALOGW("No mode changed params found, not dispatching cached mode."); return; } // If the mode is not the current mode, this means that a // 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 (pacesetterSelectorPtr()->getActiveMode() != mPolicy.modeOpt) { return; } // If there is no change from cached mode, there is no need to dispatch an event if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) { return; } mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt; onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->handle, mPolicy.cachedModeChangedParams->mode); } void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->onModeChanged(mode); } size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle, 0); return mConnections[handle].thread->getEventThreadConnectionCount(); } void Scheduler::dump(ConnectionHandle handle, std::string& result) const { android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections.at(handle).thread.get(); } thread->dump(result); } void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration) { android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->setDuration(workDuration, readyDuration); } void Scheduler::setVsyncConfigSet(const VsyncConfigSet& configs, Period vsyncPeriod) { setVsyncConfig(mVsyncModulator->setVsyncConfigSet(configs), vsyncPeriod); } void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) { setDuration(mAppConnectionHandle, /* workDuration */ config.appWorkDuration, /* readyDuration */ config.sfWorkDuration); setDuration(mSfConnectionHandle, /* workDuration */ vsyncPeriod, /* readyDuration */ config.sfWorkDuration); setDuration(config.sfWorkDuration); #ifdef MTK_SF_SCHEDULE_DELAY ScheduleHelper::getInstance().updateDurationVsync(config.sfWorkDuration.count(), vsyncPeriod.ns()); #endif } void Scheduler::enableHardwareVsync(PhysicalDisplayId id) { auto schedule = getVsyncSchedule(id); LOG_ALWAYS_FATAL_IF(!schedule); schedule->enableHardwareVsync(mSchedulerCallback); } void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) { auto schedule = getVsyncSchedule(id); LOG_ALWAYS_FATAL_IF(!schedule); schedule->disableHardwareVsync(mSchedulerCallback, disallow); } void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) { ATRACE_CALL(); std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); for (const auto& [id, _] : mDisplays) { resyncToHardwareVsyncLocked(id, allowToEnable); } } void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable, std::optional refreshRate) { const auto displayOpt = mDisplays.get(id); if (!displayOpt) { ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); return; } const Display& display = *displayOpt; #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { std::string result = base::StringPrintf("%s: refreshRate=%d", __func__, refreshRate ? refreshRate->getIntValue() : 0); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } #endif if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) { if (!refreshRate) { refreshRate = display.selectorPtr->getActiveMode().modePtr->getFps(); } #ifdef MTK_SF_MSYNC_3 // If resync happens right after refresh rate changes, // getActiveMode would still be old config, it makes // hw vsync change to the old one. To prevent that, // we always use msync3 period to resync. nsecs_t msync3Period = 0; if (mMsync3_period.contains(id)) { msync3Period = *mMsync3_period.get(id); } //nsecs_t msync3Period = msync3PeriodOpt ? *msync3PeriodOpt : static_cast(0); if (msync3Period > 0) { std::string _trace = ""; base::StringAppendF(&_trace, "%s: resync overrided=%" PRId64, __func__, msync3Period); ATRACE_NAME(_trace.c_str()); Fps overrideFps = Fps::fromPeriodNsecs(msync3Period); display.schedulePtr->startPeriodTransition(mSchedulerCallback, overrideFps.getPeriod(), false); } else { if (refreshRate->isValid()) { display.schedulePtr->startPeriodTransition(mSchedulerCallback, refreshRate->getPeriod(), false /* force */); } } #else if (refreshRate->isValid()) { display.schedulePtr->startPeriodTransition(mSchedulerCallback, refreshRate->getPeriod(), false /* force */); } #endif } } void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate) { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); const auto displayOpt = mDisplays.get(id); if (!displayOpt) { ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); return; } const Display& display = *displayOpt; const auto mode = display.selectorPtr->getActiveMode(); using fps_approx_ops::operator!=; LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps, "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: " "%" PRIu64, to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value); ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(), to_string(mode.modePtr->getFps()).c_str()); display.schedulePtr->getTracker().setRenderRate(renderFrameRate); } void Scheduler::resync() { static constexpr nsecs_t kIgnoreDelay = ms2ns(750); const nsecs_t now = systemTime(); const nsecs_t last = mLastResyncTime.exchange(now); if (now - last > kIgnoreDelay) { resyncAllToHardwareVsync(false /* allowToEnable */); } } bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp, std::optional hwcVsyncPeriodIn) { const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) { return Period::fromNs(nanos); }); auto schedule = getVsyncSchedule(id); if (!schedule) { ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); return false; } return schedule->addResyncSample(mSchedulerCallback, TimePoint::fromNs(timestamp), hwcVsyncPeriod); } void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr fence) { auto schedule = getVsyncSchedule(id); LOG_ALWAYS_FATAL_IF(!schedule); const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence)); if (needMoreSignals) { schedule->enableHardwareVsync(mSchedulerCallback); } else { schedule->disableHardwareVsync(mSchedulerCallback, false /* disallow */); } } void Scheduler::registerLayer(Layer* layer) { // If the content detection feature is off, we still keep the layer history, // since we use it for other features (like Frame Rate API), so layers // still need to be registered. mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection)); } void Scheduler::deregisterLayer(Layer* layer) { mLayerHistory.deregisterLayer(layer); } void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime, LayerHistory::LayerUpdateType updateType) { if (pacesetterSelectorPtr()->canSwitch()) { mLayerHistory.record(id, layerProps, presentTime, systemTime(), updateType); } } void Scheduler::setModeChangePending(bool pending) { #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { std::string result = base::StringPrintf("%s: pending=%d", __func__, pending); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } #endif mLayerHistory.setModeChangePending(pending); } void Scheduler::setDefaultFrameRateCompatibility(Layer* layer) { mLayerHistory.setDefaultFrameRateCompatibility(layer, mFeatures.test(Feature::kContentDetection)); } void Scheduler::chooseRefreshRateForContent() { const auto selectorPtr = pacesetterSelectorPtr(); if (!selectorPtr->canSwitch()) return; ATRACE_CALL(); LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime()); #ifdef MTK_SF_HINT_LOW_POWER if (!applyLowPower(&LowPower::contentRequirements, summary)) { applyPolicy(&Policy::contentRequirements, std::move(summary)); } #else applyPolicy(&Policy::contentRequirements, std::move(summary)); #endif } void Scheduler::resetIdleTimer() { pacesetterSelectorPtr()->resetIdleTimer(); } void Scheduler::onTouchHint() { if (mTouchTimer) { mTouchTimer->reset(); pacesetterSelectorPtr()->resetKernelIdleTimer(); } } void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) { const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) { ftl::FakeGuard guard(mDisplayLock); return id == mPacesetterDisplayId; }(); if (isPacesetter) { // TODO (b/255657128): This needs to be handled per display. std::lock_guard lock(mPolicyLock); mPolicy.displayPowerMode = powerMode; } { std::scoped_lock lock(mDisplayLock); auto vsyncSchedule = getVsyncScheduleLocked(id); LOG_ALWAYS_FATAL_IF(!vsyncSchedule); vsyncSchedule->getController().setDisplayPowerMode(powerMode); } if (!isPacesetter) return; if (mDisplayPowerTimer) { mDisplayPowerTimer->reset(); } // Display Power event will boost the refresh rate to performance. // Clear Layer History to get fresh FPS detection mLayerHistory.clear(); } auto Scheduler::getVsyncSchedule(std::optional idOpt) const -> ConstVsyncSchedulePtr { std::scoped_lock lock(mDisplayLock); return getVsyncScheduleLocked(idOpt); } auto Scheduler::getVsyncScheduleLocked(std::optional idOpt) const -> ConstVsyncSchedulePtr { ftl::FakeGuard guard(kMainThreadContext); if (!idOpt) { LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!"); idOpt = mPacesetterDisplayId; } const auto displayOpt = mDisplays.get(*idOpt); if (!displayOpt) { return nullptr; } return displayOpt->get().schedulePtr; } void Scheduler::kernelIdleTimerCallback(TimerState state) { ATRACE_INT("ExpiredKernelIdleTimer", static_cast(state)); // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate // magic number const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getFps(); constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz; using namespace fps_approx_ops; if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) { // If we're not in performance mode then the kernel timer shouldn't do // anything, as the refresh rate during DPU power collapse will be the // same. resyncAllToHardwareVsync(true /* allowToEnable */); } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) { // Disable HW VSYNC if the timer expired, as we don't need it enabled if // we're not pushing frames, and if we're in PERFORMANCE mode then we'll // need to update the VsyncController model anyway. std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); for (const auto& [_, display] : mDisplays) { constexpr bool kDisallow = false; display.schedulePtr->disableHardwareVsync(mSchedulerCallback, kDisallow); } } mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired); } void Scheduler::idleTimerCallback(TimerState state) { #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { ALOGI("%s: idleState=%s", __func__, state == TimerState::Reset ? "Reset" : "Expired"); } #endif #ifdef MTK_SF_HINT_LOW_POWER if (!applyLowPower(&LowPower::idleTimer, state)) { applyPolicy(&Policy::idleTimer, state); } #else applyPolicy(&Policy::idleTimer, state); #endif ATRACE_INT("ExpiredIdleTimer", static_cast(state)); } void Scheduler::touchTimerCallback(TimerState state) { const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive; // Touch event will boost the refresh rate to performance. // 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. #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { std::string result = base::StringPrintf("%s: touchState=%s", __func__, touch == TouchState::Active ? "Active" : "Inactive"); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } #endif #ifdef MTK_SF_HINT_LOW_POWER if (!applyLowPower(&LowPower::touch, touch)) { if (applyPolicy(&Policy::touch, touch).touch) { mLayerHistory.clear(); } } else if (touch == TouchState::Active) { mLayerHistory.clear(); } #else if (applyPolicy(&Policy::touch, touch).touch) { mLayerHistory.clear(); } #endif ATRACE_INT("TouchState", static_cast(touch)); } void Scheduler::displayPowerTimerCallback(TimerState state) { applyPolicy(&Policy::displayPowerTimer, state); ATRACE_INT("ExpiredDisplayPowerTimer", static_cast(state)); } void Scheduler::dump(utils::Dumper& dumper) const { using namespace std::string_view_literals; { utils::Dumper::Section section(dumper, "Features"sv); for (Feature feature : ftl::enum_range()) { if (const auto flagOpt = ftl::flag_name(feature)) { dumper.dump(flagOpt->substr(1), mFeatures.test(feature)); } } } { utils::Dumper::Section section(dumper, "Policy"sv); { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId); } dumper.dump("layerHistory"sv, mLayerHistory.dump()); dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval)); dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval)); } mFrameRateOverrideMappings.dump(dumper); dumper.eol(); } void Scheduler::dumpVsync(std::string& out) const { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); if (mPacesetterDisplayId) { base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n", to_string(*mPacesetterDisplayId).c_str()); getVsyncScheduleLocked()->dump(out); } for (auto& [id, display] : mDisplays) { if (id == mPacesetterDisplayId) { continue; } base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str()); display.schedulePtr->dump(out); } } bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) { if (consideredSignals.idle) return false; const auto frameRateOverrides = pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements, displayRefreshRate, consideredSignals); #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { using base::StringAppendF; std::string result; StringAppendF(&result, "new frameRateOverrides (setFrameRate): {"); for (const auto& [uid, frameRate] : frameRateOverrides) { StringAppendF(&result, "[uid: %d frameRate: %s], ", uid, to_string(frameRate).c_str()); } StringAppendF(&result, "}\n"); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } #endif // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying // the FrameRateOverrideMappings rather than here. return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides); } void Scheduler::promotePacesetterDisplay(std::optional pacesetterIdOpt) { std::shared_ptr pacesetterVsyncSchedule; { std::scoped_lock lock(mDisplayLock); pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterIdOpt); } applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); } std::shared_ptr Scheduler::promotePacesetterDisplayLocked( std::optional pacesetterIdOpt) { // TODO(b/241286431): Choose the pacesetter display. mPacesetterDisplayId = pacesetterIdOpt.value_or(mDisplays.begin()->first); ALOGI("Display %s is the pacesetter", to_string(*mPacesetterDisplayId).c_str()); std::shared_ptr newVsyncSchedulePtr; if (const auto pacesetterOpt = pacesetterDisplayLocked()) { const Display& pacesetter = *pacesetterOpt; pacesetter.selectorPtr->setIdleTimerCallbacks( {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); }, .onExpired = [this] { idleTimerCallback(TimerState::Expired); }}, .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); }, .onExpired = [this] { kernelIdleTimerCallback(TimerState::Expired); }}}); pacesetter.selectorPtr->startIdleTimer(); newVsyncSchedulePtr = pacesetter.schedulePtr; const Fps refreshRate = pacesetter.selectorPtr->getActiveMode().modePtr->getFps(); newVsyncSchedulePtr->startPeriodTransition(mSchedulerCallback, refreshRate.getPeriod(), true /* force */); } return newVsyncSchedulePtr; } void Scheduler::applyNewVsyncSchedule(std::shared_ptr vsyncSchedule) { onNewVsyncSchedule(vsyncSchedule->getDispatch()); std::vector threads; { std::lock_guard lock(mConnectionsLock); threads.reserve(mConnections.size()); for (auto& [_, connection] : mConnections) { threads.push_back(connection.thread.get()); } } for (auto* thread : threads) { thread->onNewVsyncSchedule(vsyncSchedule); } } void Scheduler::demotePacesetterDisplay() { // No need to lock for reads on kMainThreadContext. if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) { pacesetterPtr->stopIdleTimer(); pacesetterPtr->clearIdleTimerCallbacks(); } // Clear state that depends on the pacesetter's RefreshRateSelector. std::scoped_lock lock(mPolicyLock); mPolicy = {}; } template auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals { ATRACE_CALL(); std::vector modeRequests; GlobalSignals consideredSignals; bool refreshRateChanged = false; bool frameRateOverridesChanged; { std::scoped_lock lock(mPolicyLock); auto& currentState = mPolicy.*statePtr; if (currentState == newState) return {}; currentState = std::forward(newState); DisplayModeChoiceMap modeChoices; ftl::Optional modeOpt; { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); modeChoices = chooseDisplayModes(); // TODO(b/240743786): The pacesetter display's mode must change for any // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy // and timers. std::tie(modeOpt, consideredSignals) = modeChoices.get(*mPacesetterDisplayId) .transform([](const DisplayModeChoice& choice) { return std::make_pair(choice.mode, choice.consideredSignals); }) .value(); } modeRequests.reserve(modeChoices.size()); for (auto& [id, choice] : modeChoices) { modeRequests.emplace_back( display::DisplayModeRequest{.mode = std::move(choice.mode), .emitEvent = !choice.consideredSignals.idle}); } frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, modeOpt->fps); #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { std::string result = base::StringPrintf("%s: frameRateOverridesChanged=%d", __func__, frameRateOverridesChanged); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } #endif if (mPolicy.modeOpt != modeOpt) { #ifdef MTK_SF_MSYNC_3 if (mPolicy.modeOpt && modeOpt) { if (isShowCDDetailLog()) { std::string result = base::StringPrintf("%s: refreshRateChanged, oldMode=%d(%s(%s)), newMode=%d(%s(%s))", __func__, mPolicy.modeOpt->modePtr->getId().value(), to_string(mPolicy.modeOpt->fps).c_str(), to_string(mPolicy.modeOpt->modePtr->getFps()).c_str(), modeOpt->modePtr->getId().value(), to_string(modeOpt->fps).c_str(), to_string(modeOpt->modePtr->getFps()).c_str()); ALOGI("%s", result.c_str()); ATRACE_NAME(result.c_str()); } /*if (mPolicy.modeOpt->modePtr->getFps().getIntValue() > modeOpt->modePtr->getFps().getIntValue()) { mSchedulerCallback.changeToForeground(); }*/ } #endif mPolicy.modeOpt = modeOpt; refreshRateChanged = true; } else { // 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 if previously considered idle. if (!consideredSignals.idle) { dispatchCachedReportedMode(); } } } if (refreshRateChanged) { mSchedulerCallback.requestDisplayModes(std::move(modeRequests)); } if (frameRateOverridesChanged) { mSchedulerCallback.triggerOnFrameRateOverridesChanged(); } return consideredSignals; } auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap { ATRACE_CALL(); using RankedRefreshRates = RefreshRateSelector::RankedFrameRates; display::PhysicalDisplayVector perDisplayRanking; const auto globalSignals = makeGlobalSignals(); Fps pacesetterFps; for (const auto& [id, display] : mDisplays) { auto rankedFrameRates = display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals); if (id == *mPacesetterDisplayId) { pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps; } perDisplayRanking.push_back(std::move(rankedFrameRates)); } DisplayModeChoiceMap modeChoices; using fps_approx_ops::operator==; for (auto& [rankings, signals] : perDisplayRanking) { const auto chosenFrameRateMode = ftl::find_if(rankings, [&](const auto& ranking) { return ranking.frameRateMode.fps == pacesetterFps; }) .transform([](const auto& scoredFrameRate) { return scoredFrameRate.get().frameRateMode; }) .value_or(rankings.front().frameRateMode); modeChoices.try_emplace(chosenFrameRateMode.modePtr->getPhysicalDisplayId(), DisplayModeChoice{chosenFrameRateMode, signals}); } return modeChoices; } GlobalSignals Scheduler::makeGlobalSignals() const { const bool powerOnImminent = mDisplayPowerTimer && (mPolicy.displayPowerMode != hal::PowerMode::ON || mPolicy.displayPowerTimer == TimerState::Reset); return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active, .idle = mPolicy.idleTimer == TimerState::Expired, .powerOnImminent = powerOnImminent}; } FrameRateMode Scheduler::getPreferredDisplayMode() { std::lock_guard lock(mPolicyLock); const auto frameRateMode = pacesetterSelectorPtr() ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals()) .ranking.front() .frameRateMode; #ifdef MTK_SF_MSYNC_3 if (isShowCDDetailLog()) { ALOGI("%s: %s", __func__, to_string(frameRateMode).c_str()); } #endif // Make sure the stored mode is up to date. mPolicy.modeOpt = frameRateMode; return frameRateMode; } void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) { std::lock_guard lock(mVsyncTimelineLock); mLastVsyncPeriodChangeTimeline = std::make_optional(timeline); const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count(); if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) { mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime; } } bool Scheduler::onPostComposition(nsecs_t presentTime) { std::lock_guard lock(mVsyncTimelineLock); if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) { if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) { // We need to composite again as refreshTimeNanos is still in the future. return true; } mLastVsyncPeriodChangeTimeline->refreshRequired = false; } return false; } void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) { mLayerHistory.setDisplayArea(displayArea); } void Scheduler::setGameModeRefreshRateForUid(FrameRateOverride frameRateOverride) { if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { return; } mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride); } void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) { if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { return; } mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride); } #ifdef MTK_SF_MSYNC_3 void Scheduler::setMsync3Period(PhysicalDisplayId id, const nsecs_t period) { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); const auto displayOpt = mDisplays.get(id); LOG_ALWAYS_FATAL_IF(!displayOpt); const Display& display = *displayOpt; display.schedulePtr->getTracker().setPeriod(period); if (isShowCDDetailLog()) { ATRACE_NAME(android::base::StringPrintf("%s: period=%" PRId64, __func__, period).c_str()); } mMsync3_period.emplace_or_replace(id, period); } void Scheduler::resetMsync3Period(PhysicalDisplayId id) { if (isShowCDDetailLog()) { ATRACE_NAME(android::base::StringPrintf("%s" PRId64, __func__).c_str()); } mMsync3_period.emplace_or_replace(id, static_cast(0)); } bool Scheduler::isModeChangePending(PhysicalDisplayId id) { if (mMsync3_period.contains(id)) { const nsecs_t msync3Period = *mMsync3_period.get(id); return msync3Period > 0; } return false; } void Scheduler::setNextPredictedTargetVsync(PhysicalDisplayId id, const nsecs_t time) { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); const auto displayOpt = mDisplays.get(id); LOG_ALWAYS_FATAL_IF(!displayOpt); const Display& display = *displayOpt; if (isShowCDDetailLog()) { ATRACE_NAME(android::base::StringPrintf("%s: vsyncTime=%" PRId64, __func__, time).c_str()); } display.schedulePtr->getTracker().setNextPredictedTargetVsync(time); } void Scheduler::setTargetVsyncVector(std::vector>& vecTargetVsync) { ALOGI("%s: vecTargetVsync.size()=%zu", __func__, vecTargetVsync.size()); mVecTargetVsync = vecTargetVsync; for (size_t i = 0; i < mVecTargetVsync.size(); i++) { for (size_t j = 0; j < mVecTargetVsync[i].size(); j++) { ALOGI("%s: (%zu,%zu)=%u", __func__, i, j, mVecTargetVsync[i][j]); } } } bool Scheduler::isTargetNext2Vsync(size_t high, size_t low) { if (high < mVecTargetVsync.size() && low < mVecTargetVsync[high].size()) { return mVecTargetVsync[high][low] >= 2 ? true : false; } return false; } bool Scheduler::isShowCDDetailLog() { static bool enable = false; static bool read = false; if (!read) { enable = android::base::GetBoolProperty("debug.sf.show_content_detection_detail_log", false); read = true; } return enable; } void Scheduler::cancelAppVsync() { const ConnectionHandle handle = mAppConnectionHandle; android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->cancelVsync(); } void Scheduler::scheduleAppVsync() { const ConnectionHandle handle = mAppConnectionHandle; android::EventThread* thread; { std::lock_guard lock(mConnectionsLock); RETURN_IF_INVALID_HANDLE(handle); thread = mConnections[handle].thread.get(); } thread->scheduleVsync(); } #endif #ifdef MTK_SF_MSYNC void Scheduler::setMSyncOn(PhysicalDisplayId id, bool on) { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); const auto displayOpt = mDisplays.get(id); LOG_ALWAYS_FATAL_IF(!displayOpt); const Display& display = *displayOpt; display.schedulePtr->getTracker().setMSyncOn(on); } void Scheduler::setQ2QFull(PhysicalDisplayId id, bool bQ2QFull) { std::scoped_lock lock(mDisplayLock); ftl::FakeGuard guard(kMainThreadContext); const auto displayOpt = mDisplays.get(id); LOG_ALWAYS_FATAL_IF(!displayOpt); const Display& display = *displayOpt; display.schedulePtr->getTracker().setQ2QFull(bQ2QFull); } #endif #ifdef MTK_SF_HINT_LOW_POWER template bool Scheduler::applyLowPower(S LowPower::*statePtr, T&& newState) { std::lock_guard lock(mLowPowerLock); if (!mFeatures.test(Feature::kSFHintLowPower) && !mIsPELT32Enabled) { return false; } auto& currentState = mLowPower.*statePtr; if (currentState == newState) return true; currentState = std::forward(newState); bool bHintLowPower = false; int nLowPowerCount = 0; if (mLowPower.touch == TouchState::Active) { ALOGI("%s: touch active", __func__); bHintLowPower = false; } else if (mLowPower.idleTimer == TimerState::Expired) { ALOGI("%s: idle", __func__); bHintLowPower = true; } else if (!mLowPower.contentRequirements.empty()) { ALOGI("%s: %zu layer(s)", __func__, mLowPower.contentRequirements.size()); for (const auto& layer : mLowPower.contentRequirements) { ALOGI("%s: vote=%s, fps=%.2f, weight=%.2f, focused=%d, name=%s", __func__, ftl::enum_string(layer.vote).c_str(), layer.desiredRefreshRate.getValue(), layer.weight, layer.focused, layer.name.c_str()); if (layer.vote == scheduler::LayerHistory::LayerVoteType::Max) { ALOGI("%s: Layer with Max vote type, not hint low power", __func__); bHintLowPower = false; break; } else if (layer.vote == scheduler::LayerHistory::LayerVoteType::Min) { ALOGI("%s: Layer with Min vote type, hint low power", __func__); nLowPowerCount++; } else { if (layer.desiredRefreshRate.getIntValue() <= 30) { ALOGI("%s: Layer's fps is less than or equal to 30, hint low power", __func__); nLowPowerCount++; } else { ALOGI("%s: Layer's fps is greater than 30, not hint low power", __func__); bHintLowPower = false; break; } } } if (nLowPowerCount == 1) { bHintLowPower = true; } } else { ALOGI("%s: no layers update, skip hint low power", __func__); return true; } if (mLowPower.bHintLowPower != bHintLowPower) { ALOGI("%s: hint low power as %d", __func__, bHintLowPower); mSchedulerCallback.hintLowPower(bHintLowPower); mLowPower.bHintLowPower = bHintLowPower; } return true; } bool Scheduler::setPELT32(bool enabled) { std::lock_guard lock(mLowPowerLock); if (mIsPELT32Enabled == enabled) { return true; } mIsPELT32Enabled = enabled; ALOGI("%s: mIsPELT32Enabled=%d", __func__, mIsPELT32Enabled); if (mIsPELT32Enabled) { // restart idle and touch timer if no content detection if (!mFeatures.test(Feature::kContentDetection)) { mLayerHistory.setPELT32(true); if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) { pacesetterPtr->stopIdleTimer(); pacesetterPtr->clearIdleTimerCallbacks(); pacesetterPtr->replaceIdleTimer(1000); pacesetterPtr->setIdleTimerCallbacks( {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); }, .onExpired = [this] { idleTimerCallback(TimerState::Expired); }}, .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); }, .onExpired = [this] { kernelIdleTimerCallback(TimerState::Expired); }}}); pacesetterPtr->startIdleTimer(); } if (mTouchTimer) { //mTouchTimer->stop(); mTouchTimer.reset(); } mTouchTimer.emplace( "TouchTimer", std::chrono::milliseconds(1000), [this] { touchTimerCallback(TimerState::Reset); }, [this] { touchTimerCallback(TimerState::Expired); }); mTouchTimer->start(); } } else { mLowPower.bHintLowPower = false; // stop idle and touch timer if no content detection if (!mFeatures.test(Feature::kContentDetection)) { mLayerHistory.setPELT32(false); mLayerHistory.clear(); if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) { pacesetterPtr->stopIdleTimer(); pacesetterPtr->clearIdleTimerCallbacks(); } if (mTouchTimer) { //mTouchTimer->stop(); mTouchTimer.reset(); } } } return true; } #endif } // namespace android::scheduler