/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * Code to notify things that animate before a refresh, at an appropriate * refresh rate. (Perhaps temporary, until replaced by compositor.) * * Chrome and each tab have their own RefreshDriver, which in turn * hooks into one of a few global timer based on RefreshDriverTimer, * defined below. There are two main global timers -- one for active * animations, and one for inactive ones. These are implemented as * subclasses of RefreshDriverTimer; see below for a description of * their implementations. In the future, additional timer types may * implement things like blocking on vsync.
*/
#ifdef XP_WIN # include <windows.h> // mmsystem isn't part of WIN32_LEAN_AND_MEAN, so we have // to manually include it # include <mmsystem.h> # include "WinUtils.h" #endif
namespace { // The number outstanding nsRefreshDrivers (that have been created but not // disconnected). When this reaches zero we will call // nsRefreshDriver::Shutdown. static uint32_t sRefreshDriverCount = 0;
} // namespace
namespace mozilla {
static TimeStamp sMostRecentHighRateVsync;
static TimeDuration sMostRecentHighRate;
/* * The base class for all global refresh driver timers. It takes care * of managing the list of refresh drivers attached to them and * provides interfaces for querying/setting the rate and actually * running a timer 'Tick'. Subclasses must implement StartTimer(), * StopTimer(), and ScheduleNextTick() -- the first two just * start/stop whatever timer mechanism is in use, and ScheduleNextTick * is called at the start of the Tick() implementation to set a time * for the next tick.
*/ class RefreshDriverTimer { public:
RefreshDriverTimer() = default;
if (IsRootRefreshDriver(aDriver)) {
NS_ASSERTION(mRootRefreshDrivers.Contains(aDriver), "RemoveRefreshDriver for a refresh driver that's not in the " "root refresh list!");
mRootRefreshDrivers.RemoveElement(aDriver);
} else {
nsPresContext* pc = aDriver->GetPresContext();
nsPresContext* rootContext = pc ? pc->GetRootPresContext() : nullptr; // During PresContext shutdown, we can't accurately detect // if a root refresh driver exists or not. Therefore, we have to // search and find out which list this driver exists in. if (!rootContext) { if (mRootRefreshDrivers.Contains(aDriver)) {
mRootRefreshDrivers.RemoveElement(aDriver);
} else {
NS_ASSERTION(mContentRefreshDrivers.Contains(aDriver), "RemoveRefreshDriver without a display root for a " "driver that is not in the content refresh list");
mContentRefreshDrivers.RemoveElement(aDriver);
}
} else {
NS_ASSERTION(mContentRefreshDrivers.Contains(aDriver), "RemoveRefreshDriver for a driver that is not in the " "content refresh list");
mContentRefreshDrivers.RemoveElement(aDriver);
}
}
// If we haven't painted for some time, then guess that we won't paint // again for a while, so the refresh driver is not a good way to predict // idle time. if (highRateMultiplier == 1.0 &&
(idleEnd +
refreshPeriod *
StaticPrefs::layout_idle_period_required_quiescent_frames() <
TimeStamp::Now())) { return aDefault;
}
// End the predicted idle time a little early, the amount controlled by a // pref, to prevent overrunning the idle time and delaying a frame. // But do that only if we aren't in high rate mode.
idleEnd = idleEnd - TimeDuration::FromMilliseconds(
highRateMultiplier *
StaticPrefs::layout_idle_period_time_limit()); return idleEnd < aDefault ? idleEnd : aDefault;
}
// Returns null if the RefreshDriverTimer is attached to several // RefreshDrivers. That may happen for example when there are // several windows open.
nsPresContext* GetPresContextForOnlyRefreshDriver() { if (mRootRefreshDrivers.Length() == 1 && mContentRefreshDrivers.IsEmpty()) { return mRootRefreshDrivers[0]->GetPresContext();
} if (mContentRefreshDrivers.Length() == 1 && mRootRefreshDrivers.IsEmpty()) { return mContentRefreshDrivers[0]->GetPresContext();
} return nullptr;
}
bool IsAnyToplevelContentPageLoading() { for (nsTArray<RefPtr<nsRefreshDriver>>* drivers :
{&mRootRefreshDrivers, &mContentRefreshDrivers}) { for (RefPtr<nsRefreshDriver>& driver : *drivers) { if (nsPresContext* pc = driver->GetPresContext()) { if (pc->Document()->IsTopLevelContentDocument() &&
pc->Document()->GetReadyStateEnum() <
Document::READYSTATE_COMPLETE) { returntrue;
}
}
}
}
returnfalse;
}
protected: virtual ~RefreshDriverTimer() {
MOZ_ASSERT(
mContentRefreshDrivers.Length() == 0, "Should have removed all content refresh drivers from here by now!");
MOZ_ASSERT(
mRootRefreshDrivers.Length() == 0, "Should have removed all root refresh drivers from here by now!");
}
protected: bool IsRootRefreshDriver(nsRefreshDriver* aDriver) {
nsPresContext* pc = aDriver->GetPresContext();
nsPresContext* rootContext = pc ? pc->GetRootPresContext() : nullptr; if (!rootContext) { returnfalse;
}
return aDriver == rootContext->RefreshDriver();
}
/* * Actually runs a tick, poking all the attached RefreshDrivers. * Grabs the "now" time via TimeStamp::Now().
*/ void Tick() {
TimeStamp now = TimeStamp::Now();
Tick(VsyncId(), now);
}
for (nsRefreshDriver* driver : aDrivers.Clone()) { // don't poke this driver if it's in test mode if (driver->IsTestControllingRefreshesEnabled()) { continue;
}
TickDriver(driver, aId, aNow);
}
}
/* * Tick the refresh drivers based on the given timestamp.
*/ void Tick(VsyncId aId, TimeStamp now) {
ScheduleNextTick(now);
// useful callback for nsITimer-based derived classes, here // because of c++ protected shenanigans staticvoid TimerTick(nsITimer* aTimer, void* aClosure) {
RefPtr<RefreshDriverTimer> timer = static_cast<RefreshDriverTimer*>(aClosure);
timer->Tick();
}
};
/* * A RefreshDriverTimer that uses a nsITimer as the underlying timer. Note that * this is a ONE_SHOT timer, not a repeating one! Subclasses are expected to * implement ScheduleNextTick and intelligently calculate the next time to tick, * and to reset mTimer. Using a repeating nsITimer gets us into a lot of pain * with its attempt at intelligent slack removal and such, so we don't do it.
*/ class SimpleTimerBasedRefreshDriverTimer : public RefreshDriverTimer { public: /* * aRate -- the delay, in milliseconds, requested between timer firings
*/ explicit SimpleTimerBasedRefreshDriverTimer(double aRate) {
SetRate(aRate);
mTimer = NS_NewTimer();
}
// will take effect at next timer tick virtualvoid SetRate(double aNewRate) {
mRateMilliseconds = aNewRate;
mRateDuration = TimeDuration::FromMilliseconds(mRateMilliseconds);
}
protected: void StartTimer() override { // pretend we just fired, and we schedule the next tick normally
mLastFireTime = TimeStamp::Now();
mLastFireId = VsyncId();
/* * A refresh driver that listens to vsync events and ticks the refresh driver * on vsync intervals. We throttle the refresh driver if we get too many * vsync events and wait to catch up again.
*/ class VsyncRefreshDriverTimer : public RefreshDriverTimer { public: // This is used in the parent process for all platforms except Linux Wayland. static RefPtr<VsyncRefreshDriverTimer>
CreateForParentProcessWithGlobalVsync() {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<VsyncDispatcher> vsyncDispatcher =
gfxPlatform::GetPlatform()->GetGlobalVsyncDispatcher();
RefPtr<VsyncRefreshDriverTimer> timer = new VsyncRefreshDriverTimer(std::move(vsyncDispatcher), nullptr); return timer.forget();
}
// This is used in the parent process for Linux Wayland only, where we have a // per-widget VsyncSource which is independent from the gfxPlatform's global // VsyncSource. static RefPtr<VsyncRefreshDriverTimer>
CreateForParentProcessWithLocalVsyncDispatcher(
RefPtr<VsyncDispatcher>&& aVsyncDispatcher) {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<VsyncRefreshDriverTimer> timer = new VsyncRefreshDriverTimer(std::move(aVsyncDispatcher), nullptr); return timer.forget();
}
// This is used in the content process. static RefPtr<VsyncRefreshDriverTimer> CreateForContentProcess(
RefPtr<VsyncMainChild>&& aVsyncChild) {
MOZ_RELEASE_ASSERT(XRE_IsContentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<VsyncRefreshDriverTimer> timer = new VsyncRefreshDriverTimer(nullptr, std::move(aVsyncChild)); return timer.forget();
}
// If hardware queries fail / are unsupported, we have to just guess. return mVsyncRate != TimeDuration::Forever()
? mVsyncRate
: TimeDuration::FromMilliseconds(1000.0 / 60.0);
}
private: // RefreshDriverVsyncObserver redirects vsync notifications to the main thread // and calls VsyncRefreshDriverTimer::NotifyVsyncOnMainThread on it. It also // acts as a weak reference to the refresh driver timer, dropping its // reference when RefreshDriverVsyncObserver::Shutdown is called from the // timer's destructor. // // RefreshDriverVsyncObserver::NotifyVsync is called from different places // depending on the process type. // // Parent process: // NotifyVsync is called by RefreshDriverVsyncDispatcher, on a background // thread. RefreshDriverVsyncDispatcher keeps strong references to its // VsyncObservers, both in its array of observers and while calling // NotifyVsync. So it might drop its last reference to the observer on a // background thread. This means that the VsyncRefreshDriverTimer itself can't // be the observer (because its destructor would potentially be run on a // background thread), and it's why we use this separate class. // // Child process: // NotifyVsync is called by VsyncMainChild, on the main thread. // VsyncMainChild keeps raw pointers to its observers. class RefreshDriverVsyncObserver final : public VsyncObserver {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(
VsyncRefreshDriverTimer::RefreshDriverVsyncObserver, override)
void NotifyVsync(const VsyncEvent& aVsync) override { // Compress vsync notifications such that only 1 may run at a time // This is so that we don't flood the refresh driver with vsync messages // if the main thread is blocked for long periods of time
{ // scope lock auto pendingVsync = mLastPendingVsyncNotification.Lock(); bool hadPendingVsync = pendingVsync->isSome();
*pendingVsync = Some(aVsync); if (hadPendingVsync) { return;
}
}
if (XRE_IsContentProcess()) { // In the content process, NotifyVsync is called by VsyncMainChild on // the main thread. No need to use a runnable, just call // NotifyVsyncTimerOnMainThread() directly.
NotifyVsyncTimerOnMainThread(); return;
}
// In the parent process, NotifyVsync is called on the vsync thread, which // on most platforms is different from the main thread, so we need to // dispatch a runnable for running NotifyVsyncTimerOnMainThread on the // main thread. // TODO: On Linux Wayland, the vsync thread is currently the main thread, // and yet we still dispatch the runnable. Do we need to? bool useVsyncPriority = mozilla::BrowserTabsRemoteAutostart();
nsCOMPtr<nsIRunnable> vsyncEvent = new PrioritizableRunnable(
NS_NewRunnableFunction( "RefreshDriverVsyncObserver::NotifyVsyncTimerOnMainThread",
[self = RefPtr{this}]() {
self->NotifyVsyncTimerOnMainThread();
}),
useVsyncPriority ? nsIRunnablePriority::PRIORITY_VSYNC
: nsIRunnablePriority::PRIORITY_NORMAL);
NS_DispatchToMainThread(vsyncEvent);
}
if (!mVsyncRefreshDriverTimer) { // Ignore calls after Shutdown. return;
}
VsyncEvent vsyncEvent;
{ // Get the last of the queued-up vsync notifications. auto pendingVsync = mLastPendingVsyncNotification.Lock();
MOZ_RELEASE_ASSERT(
pendingVsync->isSome(), "We should always have a pending vsync notification here.");
vsyncEvent = pendingVsync->extract();
}
// Call VsyncRefreshDriverTimer::NotifyVsyncOnMainThread, and keep a // strong reference to it while calling the method.
RefPtr<VsyncRefreshDriverTimer> timer = mVsyncRefreshDriverTimer;
timer->NotifyVsyncOnMainThread(vsyncEvent);
}
// VsyncRefreshDriverTimer holds this RefreshDriverVsyncObserver and it will // be always available before Shutdown(). We can just use the raw pointer // here. // Only accessed on the main thread.
VsyncRefreshDriverTimer* mVsyncRefreshDriverTimer;
// Non-empty between a call to NotifyVsync and a call to // NotifyVsyncOnMainThread. When multiple vsync notifications have been // received between those two calls, this contains the last of the pending // notifications. This is used both in the parent process and in the child // process, but it only does something useful in the parent process. In the // child process, both calls happen on the main thread right after one // another, so there's only one notification to keep track of; vsync // notification coalescing for child processes happens at the IPC level // instead.
DataMutex<Maybe<VsyncEvent>> mLastPendingVsyncNotification;
// Detach current vsync timer from this VsyncObserver. The observer will no // longer tick this timer.
mVsyncObserver->Shutdown();
mVsyncObserver = nullptr;
}
// If we haven't processed new idle tasks and we have pending // non-idle tasks, give those non-idle tasks more time, // but only if the main thread wasn't totally empty at some point. // In the parent process RunOutOfMTTasksCount() is less meaningful // because some of the tasks run through AppShell. return mLastIdleTaskCount == idleTaskCount &&
(taskController->RunOutOfMTTasksCount() ==
mLastRunOutOfMTTasksCount ||
XRE_IsParentProcess());
}
mRecentVsync = aVsyncEvent.mTime;
mRecentVsyncId = aVsyncEvent.mId; if (!mSuspendVsyncPriorityTicksUntil.IsNull() &&
mSuspendVsyncPriorityTicksUntil > TimeStamp::Now()) { if (ShouldGiveNonVsyncTasksMoreTime()) { if (!IsAnyToplevelContentPageLoading()) { // If pages aren't loading and there aren't other tasks to run, // trigger the pending vsync notification.
mPendingVsync = mRecentVsync;
mPendingVsyncId = mRecentVsyncId; if (!mHasPendingLowPrioTask) {
mHasPendingLowPrioTask = true;
NS_DispatchToMainThreadQueue(
NS_NewRunnableFunction( "NotifyVsyncOnMainThread[low priority]",
[self = RefPtr{this}]() {
self->mHasPendingLowPrioTask = false; if (self->mRecentVsync == self->mPendingVsync &&
self->mRecentVsyncId == self->mPendingVsyncId &&
!self->ShouldGiveNonVsyncTasksMoreTime()) {
self->mSuspendVsyncPriorityTicksUntil = TimeStamp();
self->NotifyVsyncOnMainThread({self->mPendingVsyncId,
self->mPendingVsync, /* unused */
TimeStamp()});
}
}),
EventQueuePriority::Low);
}
} return;
}
// Clear the value since we aren't blocking anymore because there aren't // any non-idle tasks to process.
mSuspendVsyncPriorityTicksUntil = TimeStamp();
}
if (StaticPrefs::layout_lower_priority_refresh_driver_during_load() &&
ShouldGiveNonVsyncTasksMoreTime()) {
nsPresContext* pctx = GetPresContextForOnlyRefreshDriver(); if (pctx && pctx->HadFirstContentfulPaint() && pctx->Document() &&
pctx->Document()->GetReadyStateEnum() <
Document::READYSTATE_COMPLETE) {
nsPIDOMWindowInner* win = pctx->Document()->GetInnerWindow();
uint32_t frameRateMultiplier = pctx->GetNextFrameRateMultiplier(); if (!frameRateMultiplier) {
pctx->DidUseFrameRateMultiplier();
} if (win && frameRateMultiplier) {
dom::Performance* perf = win->GetPerformance(); // Limit slower refresh rate to 5 seconds between the // first contentful paint and page load. if (perf &&
perf->Now() < StaticPrefs::page_load_deprioritization_period()) { if (mProcessedVsync) {
mProcessedVsync = false;
TimeDuration rate = GetTimerRate();
uint32_t slowRate = static_cast<uint32_t>(rate.ToMilliseconds() *
frameRateMultiplier);
pctx->DidUseFrameRateMultiplier();
nsCOMPtr<nsIRunnable> vsyncEvent = NewRunnableMethod<>( "VsyncRefreshDriverTimer::IdlePriorityNotify", this,
&VsyncRefreshDriverTimer::IdlePriorityNotify);
NS_DispatchToCurrentThreadQueue(vsyncEvent.forget(), slowRate,
EventQueuePriority::Idle);
} return;
}
}
}
}
void RecordTelemetryProbes(TimeStamp aVsyncTimestamp) {
MOZ_ASSERT(NS_IsMainThread()); #ifndef ANDROID /* bug 1142079 */ if (XRE_IsParentProcess()) {
TimeDuration vsyncLatency = TimeStamp::Now() - aVsyncTimestamp;
uint32_t sample = (uint32_t)vsyncLatency.ToMilliseconds();
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_CHROME_FRAME_DELAY_MS,
sample);
} elseif (mVsyncRate != TimeDuration::Forever()) {
TimeDuration contentDelay =
(TimeStamp::Now() - mLastTickStart) - mVsyncRate; if (contentDelay.ToMilliseconds() < 0) { // Vsyncs are noisy and some can come at a rate quicker than // the reported hardware rate. In those cases, consider that we have 0 // delay.
contentDelay = TimeDuration::FromMilliseconds(0);
}
uint32_t sample = (uint32_t)contentDelay.ToMilliseconds();
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_CONTENT_FRAME_DELAY_MS,
sample);
} else { // Request the vsync rate which VsyncChild stored the last time it got a // vsync notification.
mVsyncRate = mVsyncChild->GetVsyncRate();
} #endif
}
void IdlePriorityNotify() { if (mLastProcessedTick.IsNull() || mRecentVsync > mLastProcessedTick) { // mSuspendVsyncPriorityTicksUntil is for high priority vsync // notifications only.
mSuspendVsyncPriorityTicksUntil = TimeStamp();
TickRefreshDriver(mRecentVsyncId, mRecentVsync);
}
mProcessedVsync = true;
}
hal::PerformanceHintSession* GetPerformanceHintSession() { // The ContentChild creates/destroys the PerformanceHintSession in response // to the process' priority being foregrounded/backgrounded. We can only use // this session when using a single vsync source for the process, otherwise // these threads may be performing work for multiple // VsyncRefreshDriverTimers and we will misreport the work duration. const ContentChild* contentChild = ContentChild::GetSingleton(); if (contentChild && mVsyncChild) { return contentChild->PerformanceHintSession();
}
// On 32-bit Windows we sometimes get times where TimeStamp::Now() is not // monotonic because the underlying system apis produce non-monontonic // results. (bug 1306896) #if !defined(_WIN32)
MOZ_ASSERT(aVsyncTimestamp <= tickStart); #endif
// Set these variables before calling RunRefreshDrivers so that they are // visible to any nested ticks.
mLastTickStart = tickStart;
mLastProcessedTick = aVsyncTimestamp;
// Re-read mLastTickStart in case there was a nested tick inside this // tick.
TimeStamp mostRecentTickStart = mLastTickStart;
// Let also non-RefreshDriver code to run at least for awhile if we have // a mVsyncRefreshDriverTimer. // Always give a tiny bit, 5% of the vsync interval, time outside the // tick // In case there are both normal tasks and RefreshDrivers are doing // work, mSuspendVsyncPriorityTicksUntil will be set to a timestamp in the // future where the period between the previous tick start // (mostRecentTickStart) and the next tick needs to be at least the amount // of work normal tasks and RefreshDrivers did together (minus short grace // period).
TimeDuration gracePeriod = rate / int64_t(20);
if (shouldGiveNonVSyncTasksMoreTime && !mLastTickEnd.IsNull() &&
XRE_IsContentProcess() && // For RefreshDriver scheduling during page load there is currently // idle priority based setup. // XXX Consider to remove the page load specific code paths.
!IsAnyToplevelContentPageLoading()) { // In case normal tasks are doing lots of work, we still want to paint // every now and then, so only at maximum 4 * rate of work is counted // here. // If we're giving extra time for tasks outside a tick, try to // ensure the next vsync after that period is handled, so subtract // a grace period.
TimeDuration timeForOutsideTick = std::clamp(
tickStart - mLastTickEnd - gracePeriod, gracePeriod, rate * 4);
mSuspendVsyncPriorityTicksUntil = tickEnd + timeForOutsideTick;
} elseif (ShouldGiveNonVsyncTasksMoreTime(true)) { // We've got some new tasks, give them some extra time. // This handles also the case when mLastTickEnd.IsNull() above and we // should give some more time for non-vsync tasks.
mSuspendVsyncPriorityTicksUntil = tickEnd + gracePeriod;
} else {
mSuspendVsyncPriorityTicksUntil = mostRecentTickStart + gracePeriod;
}
protected: void ScheduleNextTick(TimeStamp aNowTime) override { // Do nothing since we just wait for the next vsync from // RefreshDriverVsyncObserver.
}
// Always non-null. Has a weak pointer to us and notifies us of vsync.
RefPtr<RefreshDriverVsyncObserver> mVsyncObserver;
// Used in the parent process. We register mVsyncObserver with it for the // duration during which we want to receive vsync notifications. We also // use it to query the current vsync rate.
RefPtr<VsyncDispatcher> mVsyncDispatcher; // Used it the content process. We register mVsyncObserver with it for the // duration during which we want to receive vsync notifications. The // mVsyncChild will be always available before VsyncChild::ActorDestroy(). // After ActorDestroy(), StartTimer() and StopTimer() calls will be non-op.
RefPtr<VsyncMainChild> mVsyncChild;
TimeDuration mVsyncRate; bool mIsTicking = false;
TimeStamp mRecentVsync;
VsyncId mRecentVsyncId; // The local start time when RefreshDrivers' Tick was called last time.
TimeStamp mLastTickStart; // The local end time of the last RefreshDrivers' tick.
TimeStamp mLastTickEnd; // The number of idle tasks the main thread has processed. It is updated // right after RefreshDrivers' tick.
uint64_t mLastIdleTaskCount; // If there were no idle tasks, we need to check if the main event queue // was totally empty at times.
uint64_t mLastRunOutOfMTTasksCount; // Note, mLastProcessedTick stores the vsync timestamp, which may be coming // from a different process.
TimeStamp mLastProcessedTick; // mSuspendVsyncPriorityTicksUntil is used to block too high refresh rate in // case the main thread has also other non-idle tasks to process. // The timestamp is effectively mLastTickEnd + some duration.
TimeStamp mSuspendVsyncPriorityTicksUntil; bool mProcessedVsync;
/** * Since the content process takes some time to setup * the vsync IPC connection, this timer is used * during the intial startup process. * During initial startup, the refresh drivers * are ticked off this timer, and are swapped out once content * vsync IPC connection is established.
*/ class StartupRefreshDriverTimer : public SimpleTimerBasedRefreshDriverTimer { public: explicit StartupRefreshDriverTimer(double aRate)
: SimpleTimerBasedRefreshDriverTimer(aRate) {}
protected: void ScheduleNextTick(TimeStamp aNowTime) override { // Since this is only used for startup, it isn't super critical // that we tick at consistent intervals.
TimeStamp newTarget = aNowTime + mRateDuration;
uint32_t delay = static_cast<uint32_t>((newTarget - aNowTime).ToMilliseconds());
mTimer->InitWithNamedFuncCallback(
TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT, "StartupRefreshDriverTimer::ScheduleNextTick");
mTargetTime = newTarget;
}
/* * A RefreshDriverTimer for inactive documents. When a new refresh driver is * added, the rate is reset to the base (normally 1s/1fps). Every time * it ticks, a single refresh driver is poked. Once they have all been poked, * the duration between ticks doubles, up to mDisableAfterMilliseconds. At that * point, the timer is quiet and doesn't tick (until something is added to it * again). * * When a timer is removed, there is a possibility of another timer * being skipped for one cycle. We could avoid this by adjusting * mNextDriverIndex in RemoveRefreshDriver, but there's little need to * add that complexity. All we want is for inactive drivers to tick * at some point, but we don't care too much about how often.
*/ class InactiveRefreshDriverTimer final
: public SimpleTimerBasedRefreshDriverTimer { public: explicit InactiveRefreshDriverTimer(double aRate)
: SimpleTimerBasedRefreshDriverTimer(aRate),
mNextTickDuration(aRate),
mDisableAfterMilliseconds(-1.0),
mNextDriverIndex(0) {}
// reset the timer, and start with the newly added one next time.
mNextTickDuration = mRateMilliseconds;
// we don't really have to start with the newly added one, but we may as // well not tick the old ones at the fastest rate any more than we need to.
mNextDriverIndex = GetRefreshDriverCount() - 1;
void ScheduleNextTick(TimeStamp aNowTime) override { if (mDisableAfterMilliseconds > 0.0 &&
mNextTickDuration > mDisableAfterMilliseconds) { // We hit the time after which we should disable // inactive window refreshes; don't schedule anything // until we get kicked by an AddRefreshDriver call. return;
}
// double the next tick time if we've already gone through all of them once if (mNextDriverIndex >= GetRefreshDriverCount()) {
mNextTickDuration *= 2.0;
mNextDriverIndex = 0;
}
// this doesn't need to be precise; do a simple schedule
uint32_t delay = static_cast<uint32_t>(mNextTickDuration);
mTimer->InitWithNamedFuncCallback(
TimerTickOne, this, delay, nsITimer::TYPE_ONE_SHOT, "InactiveRefreshDriverTimer::ScheduleNextTick");
LOG("[%p] inactive timer next tick in %f ms [index %d/%d]", this,
mNextTickDuration, mNextDriverIndex, GetRefreshDriverCount());
}
if (gfxPlatform::IsInLayoutAsapMode()) { return;
}
if (!mOwnTimer) { // If available, we fetch the widget-specific vsync source.
nsPresContext* pc = GetPresContext();
nsCOMPtr<nsIWidget> widget = pc->GetRootWidget(); if (widget) { if (RefPtr<VsyncDispatcher> vsyncDispatcher =
widget->GetVsyncDispatcher()) {
mOwnTimer = VsyncRefreshDriverTimer::
CreateForParentProcessWithLocalVsyncDispatcher(
std::move(vsyncDispatcher));
sRegularRateTimerList->AppendElement(mOwnTimer.get()); return;
} if (BrowserChild* browserChild = widget->GetOwningBrowserChild()) { if (RefPtr<VsyncMainChild> vsyncChildViaPBrowser =
browserChild->GetVsyncChild()) {
mOwnTimer = VsyncRefreshDriverTimer::CreateForContentProcess(
std::move(vsyncChildViaPBrowser));
sRegularRateTimerList->AppendElement(mOwnTimer.get()); return;
}
}
}
} if (!sRegularRateTimer) { if (XRE_IsParentProcess()) { // Make sure all vsync systems are ready.
gfxPlatform::GetPlatform(); // In parent process, we can create the VsyncRefreshDriverTimer directly.
sRegularRateTimer =
VsyncRefreshDriverTimer::CreateForParentProcessWithGlobalVsync();
} else {
PBackgroundChild* actorChild =
BackgroundChild::GetOrCreateForCurrentThread(); if (NS_WARN_IF(!actorChild)) { return;
}
auto vsyncChildViaPBackground = MakeRefPtr<dom::VsyncMainChild>();
dom::PVsyncChild* actor =
actorChild->SendPVsyncConstructor(vsyncChildViaPBackground); if (NS_WARN_IF(!actor)) { return;
}
/* static */ double nsRefreshDriver::HighRateMultiplier() { // We're in high rate mode if we've gotten a fast rate during the last // DefaultInterval(). bool inHighRateMode =
!gfxPlatform::IsInLayoutAsapMode() &&
StaticPrefs::layout_expose_high_rate_mode_from_refreshdriver() &&
!sMostRecentHighRateVsync.IsNull() &&
(sMostRecentHighRateVsync +
TimeDuration::FromMilliseconds(DefaultInterval())) > TimeStamp::Now(); if (!inHighRateMode) { // Clear the timestamp so that the next call is faster.
sMostRecentHighRateVsync = TimeStamp();
sMostRecentHighRate = TimeDuration(); return 1.0;
}
// Compute the interval to use for the refresh driver timer, in milliseconds. // outIsDefault indicates that rate was not explicitly set by the user // so we might choose other, more appropriate rates (e.g. vsync, etc) // layout.frame_rate=0 indicates "ASAP mode". // In ASAP mode rendering is iterated as fast as possible (typically for stress // testing). A target rate of 10k is used internally instead of special-handling // 0. Backends which block on swap/present/etc should try to not block when // layout.frame_rate=0 - to comply with "ASAP" as much as possible. double nsRefreshDriver::GetRegularTimerInterval() const {
int32_t rate = Preferences::GetInt("layout.frame_rate", -1); if (rate < 0) {
rate = gfxPlatform::GetDefaultFrameRate();
} elseif (rate == 0) {
rate = 10000;
}
if (!sRegularRateTimerList) {
sRegularRateTimerList = new nsTArray<RefreshDriverTimer*>();
}
++sRefreshDriverCount;
}
nsRefreshDriver::~nsRefreshDriver() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(ObserverCount() == mEarlyRunners.Length(), "observers, except pending selection scrolls, " "should have been unregistered");
MOZ_ASSERT(!mActiveTimer, "timer should be gone");
MOZ_ASSERT(!mPresContext, "Should have called Disconnect() and decremented " "sRefreshDriverCount!");
if (mRootRefresh) {
mRootRefresh->RemoveRefreshObserver(this, FlushType::Style);
mRootRefresh = nullptr;
} if (mOwnTimer && sRegularRateTimerList) {
sRegularRateTimerList->RemoveElement(mOwnTimer.get());
}
}
// Method for testing. See nsIDOMWindowUtils.advanceTimeAndRefresh // for description. void nsRefreshDriver::AdvanceTimeAndRefresh(int64_t aMilliseconds) { // ensure that we're removed from our driver
StopTimer();
if (!mTestControllingRefreshes) {
mMostRecentRefresh = TimeStamp::Now();
mTestControllingRefreshes = true; if (mWaitingForTransaction) { // Disable any refresh driver throttling when entering test mode
mWaitingForTransaction = false;
mSkippedPaints = false;
}
}
TimeStamp nsRefreshDriver::MostRecentRefresh(bool aEnsureTimerStarted) const { // In case of stylo traversal, we have already activated the refresh driver in // RestyleManager::ProcessPendingRestyles(). if (aEnsureTimerStarted && !ServoStyleSet::IsInServoTraversal()) { const_cast<nsRefreshDriver*>(this)->EnsureTimerStarted();
}
return mMostRecentRefresh;
}
void nsRefreshDriver::AddRefreshObserver(nsARefreshObserver* aObserver,
FlushType aFlushType, constchar* aObserverDescription) {
ObserverArray& array = ArrayFor(aFlushType);
MOZ_ASSERT(!array.Contains(aObserver), "We don't want to redundantly register the same observer");
array.AppendElement(
ObserverData{aObserver, aObserverDescription, TimeStamp::Now(),
MarkerInnerWindowIdFromDocShell(GetDocShell(mPresContext)),
profiler_capture_backtrace(), aFlushType}); #ifdef DEBUG
MOZ_ASSERT(aObserver->mRegistrationCount >= 0, "Registration count shouldn't be able to go negative");
aObserver->mRegistrationCount++; #endif
EnsureTimerStarted();
}
bool nsRefreshDriver::RemoveRefreshObserver(nsARefreshObserver* aObserver,
FlushType aFlushType) {
ObserverArray& array = ArrayFor(aFlushType); auto index = array.IndexOf(aObserver); if (index == ObserverArray::array_type::NoIndex) { returnfalse;
}
if (profiler_thread_is_being_profiled_for_markers()) { auto& data = array.ElementAt(index);
nsPrintfCString str("%s [%s]", data.mDescription,
kFlushTypeNames[aFlushType]);
PROFILER_MARKER_TEXT( "RefreshObserver", GRAPHICS,
MarkerOptions(MarkerStack::TakeBacktrace(std::move(data.mCause)),
MarkerTiming::IntervalUntilNowFrom(data.mRegisterTime),
std::move(data.mInnerWindowId)),
str);
}
array.RemoveElementAt(index); #ifdef DEBUG
aObserver->mRegistrationCount--;
MOZ_ASSERT(aObserver->mRegistrationCount >= 0, "Registration count shouldn't be able to go negative"); #endif returntrue;
}
void nsRefreshDriver::DispatchVisualViewportResizeEvents() { // We're taking a hint from scroll events and only dispatch the current set // of queued resize events. If additional events are posted in response to // the current events being dispatched, we'll dispatch them on the next tick.
VisualViewportResizeEventArray events =
std::move(mVisualViewportResizeEvents); for (auto& event : events) {
event->Run();
}
}
void nsRefreshDriver::DispatchScrollEvents() { // Scroll events are one-shot, so after running them we can drop them. // However, dispatching a scroll event can potentially cause more scroll // events to be posted, so we move the initial set into a temporary array // first. (Newly posted scroll events will be dispatched on the next tick.)
ScrollEventArray events = std::move(mScrollEvents); for (auto& event : events) {
event->Run();
}
}
void nsRefreshDriver::DispatchVisualViewportScrollEvents() { // Scroll events are one-shot, so after running them we can drop them. // However, dispatching a scroll event can potentially cause more scroll // events to be posted, so we move the initial set into a temporary array // first. (Newly posted scroll events will be dispatched on the next tick.)
VisualViewportScrollEventArray events =
std::move(mVisualViewportScrollEvents); for (auto& event : events) {
event->Run();
}
}
// https://drafts.csswg.org/cssom-view/#evaluate-media-queries-and-report-changes void nsRefreshDriver::EvaluateMediaQueriesAndReportChanges() { if (!mMightNeedMediaQueryListenerUpdate) { return;
}
mMightNeedMediaQueryListenerUpdate = false; if (!mPresContext) { return;
}
AUTO_PROFILER_LABEL_RELEVANT_FOR_JS( "Evaluate media queries and report changes", LAYOUT);
RefPtr<Document> doc = mPresContext->Document();
doc->EvaluateMediaQueriesAndReportChanges(/* aRecurse = */ true);
}
void nsRefreshDriver::FlushForceNotifyContentfulPaintPresContext() { while (!mForceNotifyContentfulPaintPresContexts.IsEmpty()) {
WeakPtr<nsPresContext> presContext =
mForceNotifyContentfulPaintPresContexts.PopLastElement(); if (presContext) {
presContext->NotifyContentfulPaint();
}
}
}
void nsRefreshDriver::RunDelayedEventsSoon() { // Place entries for delayed events into their corresponding normal list, // and schedule a refresh. When these delayed events run, if their document // still has events suppressed then they will be readded to the delayed // events list.
bool nsRefreshDriver::CanDoCatchUpTick() { if (mTestControllingRefreshes || !mActiveTimer) { returnfalse;
}
// If we've already ticked for the current timer refresh (or more recently // than that), then we don't need to do any catching up. if (mMostRecentRefresh >= mActiveTimer->MostRecentRefresh()) { returnfalse;
}
if (mActiveTimer->IsBlocked()) { returnfalse;
}
if (mTickVsyncTime.IsNull()) { // Don't try to run a catch-up tick before there has been at least one // normal tick. The catch-up tick could negatively affect page load // performance. returnfalse;
}
if (mPresContext && mPresContext->Document()->GetReadyStateEnum() <
Document::READYSTATE_COMPLETE) { // Don't try to run a catch-up tick before the page has finished loading. // The catch-up tick could negatively affect page load performance. returnfalse;
}
returntrue;
}
bool nsRefreshDriver::CanDoExtraTick() { // Only allow one extra tick per normal vsync tick. if (mAttemptedExtraTickSinceLastVsync) { returnfalse;
}
// If we don't have a timer, or we didn't tick on the timer's // refresh then we can't do an 'extra' tick (but we may still // do a catch up tick). if (!mActiveTimer ||
mActiveTimer->MostRecentRefresh() != mMostRecentRefresh) { returnfalse;
}
// Grab the current timestamp before checking the tick hint to be sure // sure that it's equal or smaller than the value used within checking // the tick hint.
TimeStamp now = TimeStamp::Now();
Maybe<TimeStamp> nextTick = mActiveTimer->GetNextTickHint();
int32_t minimumRequiredTime = StaticPrefs::layout_extra_tick_minimum_ms(); // If there's less than 4 milliseconds until the next tick, it's probably // not worth trying to catch up. if (minimumRequiredTime < 0 || !nextTick ||
(*nextTick - now) < TimeDuration::FromMilliseconds(minimumRequiredTime)) { returnfalse;
}
returntrue;
}
void nsRefreshDriver::EnsureTimerStarted(EnsureTimerStartedFlags aFlags) { // FIXME: Bug 1346065: We should also assert the case where we have no // stylo-threads.
MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal() || NS_IsMainThread(), "EnsureTimerStarted should be called only when we are not " "in servo traversal or on the main-thread");
if (mTestControllingRefreshes) { return;
}
if (!mRefreshTimerStartedCause) {
mRefreshTimerStartedCause = profiler_capture_backtrace();
}
// will it already fire, and no other changes needed? if (mActiveTimer && !(aFlags & eForceAdjustTimer)) { // If we're being called from within a user input handler, and we think // there's time to rush an extra tick immediately, then schedule a runnable // to run the extra tick. if (mUserInputProcessingCount && CanDoExtraTick()) {
RefPtr<nsRefreshDriver> self = this;
NS_DispatchToCurrentThreadQueue(
NS_NewRunnableFunction( "RefreshDriver::EnsureTimerStarted::extra",
[self]() -> void { // Re-check if we can still do an extra tick, in case anything // changed while the runnable was pending. if (self->CanDoExtraTick()) {
PROFILER_MARKER_UNTYPED("ExtraRefreshDriverTick", GRAPHICS);
LOG("[%p] Doing extra tick for user input", self.get());
self->mAttemptedExtraTickSinceLastVsync = true;
self->Tick(self->mActiveTimer->MostRecentRefreshVsyncId(),
self->mActiveTimer->MostRecentRefresh(),
IsExtraTick::Yes);
}
}),
EventQueuePriority::Vsync);
} return;
}
if (IsFrozen() || !mPresContext) { // If we don't want to start it now, or we've been disconnected.
StopTimer(); return;
}
if (mPresContext->Document()->IsBeingUsedAsImage()) { // Image documents receive ticks from clients' refresh drivers. // XXXdholbert Exclude SVG-in-opentype fonts from this optimization, until // they receive refresh-driver ticks from their client docs (bug 1107252). if (!mPresContext->Document()->IsSVGGlyphsDocument()) {
MOZ_ASSERT(!mActiveTimer, "image doc refresh driver should never have its own timer"); return;
}
}
// We got here because we're either adjusting the time *or* we're // starting it for the first time. Add to the right timer, // prehaps removing it from a previously-set one.
RefreshDriverTimer* newTimer = ChooseTimer(); if (newTimer != mActiveTimer) { if (mActiveTimer) {
mActiveTimer->RemoveRefreshDriver(this);
}
mActiveTimer = newTimer;
mActiveTimer->AddRefreshDriver(this);
if (!mHasStartedTimerAtLeastOnce) {
mHasStartedTimerAtLeastOnce = true; if (profiler_thread_is_being_profiled_for_markers()) {
nsCString text = "initial timer start "_ns; if (mPresContext->Document()->GetDocumentURI()) {
text.Append(nsContentUtils::TruncatedURLForDisplay(
mPresContext->Document()->GetDocumentURI()));
}
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.39 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.