2010-02-04 00:17:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2009-10-08 07:22:42 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2009-10-08 07:22:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Code to notify things that animate before a refresh, at an appropriate
|
|
|
|
* refresh rate. (Perhaps temporary, until replaced by compositor.)
|
2012-12-12 02:15:32 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2009-10-08 07:22:42 +04:00
|
|
|
*/
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
#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>
|
2013-08-15 08:25:58 +04:00
|
|
|
#include "WinUtils.h"
|
2012-12-12 02:15:32 +04:00
|
|
|
#endif
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-09-04 15:47:21 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2015-07-02 12:12:31 +03:00
|
|
|
#include "mozilla/IntegerRange.h"
|
2014-12-04 23:13:37 +03:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2009-10-08 07:22:42 +04:00
|
|
|
#include "nsRefreshDriver.h"
|
2012-12-12 02:15:32 +04:00
|
|
|
#include "nsITimer.h"
|
2013-05-14 03:47:02 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2009-10-08 07:22:42 +04:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2009-12-22 00:46:25 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2010-08-12 01:05:28 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "jsapi.h"
|
2010-08-23 13:30:08 +04:00
|
|
|
#include "nsContentUtils.h"
|
2015-04-21 04:22:09 +03:00
|
|
|
#include "mozilla/PendingAnimationTracker.h"
|
2011-05-25 10:32:00 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-01-05 07:12:24 +04:00
|
|
|
#include "nsViewManager.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2012-12-07 03:58:14 +04:00
|
|
|
#include "nsNPAPIPluginInstance.h"
|
2016-06-09 20:04:42 +03:00
|
|
|
#include "mozilla/dom/Performance.h"
|
2013-04-25 19:13:21 +04:00
|
|
|
#include "mozilla/dom/WindowBinding.h"
|
2017-02-13 06:21:30 +03:00
|
|
|
#include "mozilla/GeckoRestyleManager.h"
|
2017-02-13 06:21:33 +03:00
|
|
|
#include "mozilla/RestyleManager.h"
|
|
|
|
#include "mozilla/RestyleManagerInlines.h"
|
2013-08-14 15:33:03 +04:00
|
|
|
#include "Layers.h"
|
2013-10-02 01:00:38 +04:00
|
|
|
#include "imgIContainer.h"
|
2013-12-12 05:51:58 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-09-09 22:54:08 +04:00
|
|
|
#include "nsDocShell.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2014-11-27 14:47:51 +03:00
|
|
|
#include "nsJSEnvironment.h"
|
2014-11-18 15:47:32 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2015-01-14 02:38:00 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "nsIIPCBackgroundChildCreateCallback.h"
|
|
|
|
#include "mozilla/layout/VsyncChild.h"
|
|
|
|
#include "VsyncSource.h"
|
|
|
|
#include "mozilla/VsyncDispatcher.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-18 16:35:59 +03:00
|
|
|
#include "mozilla/TimelineConsumers.h"
|
2015-07-29 04:57:39 +03:00
|
|
|
#include "nsAnimationManager.h"
|
2016-01-12 07:19:08 +03:00
|
|
|
#include "nsIDOMEvent.h"
|
2016-10-12 10:58:04 +03:00
|
|
|
#include "nsDisplayList.h"
|
2014-11-18 15:47:32 +03:00
|
|
|
|
2017-02-16 16:53:59 +03:00
|
|
|
#ifdef MOZ_XUL
|
|
|
|
#include "nsXULPopupManager.h"
|
|
|
|
#endif
|
|
|
|
|
2011-05-25 10:32:00 +04:00
|
|
|
using namespace mozilla;
|
2013-08-15 08:25:58 +04:00
|
|
|
using namespace mozilla::widget;
|
2015-01-14 02:38:00 +03:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
using namespace mozilla::layout;
|
2011-05-25 10:32:00 +04:00
|
|
|
|
2015-11-06 01:35:03 +03:00
|
|
|
static mozilla::LazyLogModule sRefreshDriverLog("nsRefreshDriver");
|
|
|
|
#define LOG(...) MOZ_LOG(sRefreshDriverLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2010-08-27 08:26:23 +04:00
|
|
|
#define DEFAULT_THROTTLED_FRAME_RATE 1
|
2015-05-30 09:50:51 +03:00
|
|
|
#define DEFAULT_RECOMPUTE_VISIBILITY_INTERVAL_MS 1000
|
2016-10-28 17:24:00 +03:00
|
|
|
#define DEFAULT_NOTIFY_INTERSECTION_OBSERVERS_INTERVAL_MS 100
|
2012-12-12 02:15:32 +04:00
|
|
|
// after 10 minutes, stop firing off inactive timers
|
|
|
|
#define DEFAULT_INACTIVE_TIMER_DISABLE_SECONDS 600
|
|
|
|
|
2016-09-15 23:48:48 +03:00
|
|
|
// The number of seconds spent skipping frames because we are waiting for the compositor
|
|
|
|
// before logging.
|
2016-10-06 14:27:33 +03:00
|
|
|
#if defined(MOZ_ASAN)
|
|
|
|
# define REFRESH_WAIT_WARNING 5
|
|
|
|
#elif defined(DEBUG) && !defined(MOZ_VALGRIND)
|
|
|
|
# define REFRESH_WAIT_WARNING 5
|
|
|
|
#elif defined(DEBUG) && defined(MOZ_VALGRIND)
|
|
|
|
# define REFRESH_WAIT_WARNING (RUNNING_ON_VALGRIND ? 20 : 5)
|
|
|
|
#elif defined(MOZ_VALGRIND)
|
|
|
|
# define REFRESH_WAIT_WARNING (RUNNING_ON_VALGRIND ? 10 : 1)
|
2016-09-15 23:48:48 +03:00
|
|
|
#else
|
2016-10-06 14:27:33 +03:00
|
|
|
# define REFRESH_WAIT_WARNING 1
|
2016-09-15 23:48:48 +03:00
|
|
|
#endif
|
|
|
|
|
2015-10-30 16:35:46 +03:00
|
|
|
namespace {
|
|
|
|
// `true` if we are currently in jank-critical mode.
|
|
|
|
//
|
|
|
|
// In jank-critical mode, any iteration of the event loop that takes
|
|
|
|
// more than 16ms to compute will cause an ongoing animation to miss
|
|
|
|
// frames.
|
|
|
|
//
|
|
|
|
// For simplicity, the current implementation assumes that we are in
|
|
|
|
// jank-critical mode if and only if at least one vsync driver has
|
|
|
|
// at least one observer.
|
|
|
|
static uint64_t sActiveVsyncTimers = 0;
|
2016-01-14 17:07:18 +03:00
|
|
|
|
|
|
|
// The latest value of process-wide jank levels.
|
|
|
|
//
|
|
|
|
// For each i, sJankLevels[i] counts the number of times delivery of
|
|
|
|
// vsync to the main thread has been delayed by at least 2^i ms. Use
|
|
|
|
// GetJankLevels to grab a copy of this array.
|
|
|
|
uint64_t sJankLevels[12];
|
2016-06-06 16:51:15 +03:00
|
|
|
|
|
|
|
// 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;
|
2015-10-30 16:35:46 +03:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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:
|
2015-01-06 20:28:18 +03:00
|
|
|
RefreshDriverTimer()
|
|
|
|
: mLastFireEpoch(0)
|
2016-08-22 15:52:45 +03:00
|
|
|
, mLastFireSkipped(false)
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RefreshDriverTimer()
|
|
|
|
{
|
2015-11-16 19:12:38 +03:00
|
|
|
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!");
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void AddRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
|
|
|
LOG("[%p] AddRefreshDriver %p", this, aDriver);
|
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
bool startTimer = mContentRefreshDrivers.IsEmpty() && mRootRefreshDrivers.IsEmpty();
|
|
|
|
if (IsRootRefreshDriver(aDriver)) {
|
|
|
|
NS_ASSERTION(!mRootRefreshDrivers.Contains(aDriver), "Adding a duplicate root refresh driver!");
|
|
|
|
mRootRefreshDrivers.AppendElement(aDriver);
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(!mContentRefreshDrivers.Contains(aDriver), "Adding a duplicate content refresh driver!");
|
|
|
|
mContentRefreshDrivers.AppendElement(aDriver);
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
if (startTimer) {
|
2012-12-12 02:15:32 +04:00
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void RemoveRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
|
|
|
LOG("[%p] RemoveRefreshDriver %p", this, aDriver);
|
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
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 {
|
2016-10-12 14:27:38 +03:00
|
|
|
nsPresContext* pc = aDriver->GetPresContext();
|
|
|
|
nsPresContext* rootContext = pc ? pc->GetRootPresContext() : nullptr;
|
2015-11-16 19:12:38 +03:00
|
|
|
// 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.
|
2016-01-22 04:09:04 +03:00
|
|
|
if (!rootContext) {
|
2015-11-16 19:12:38 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
bool stopTimer = mContentRefreshDrivers.IsEmpty() && mRootRefreshDrivers.IsEmpty();
|
|
|
|
if (stopTimer) {
|
2012-12-12 02:15:32 +04:00
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp MostRecentRefresh() const { return mLastFireTime; }
|
|
|
|
int64_t MostRecentRefreshEpochTime() const { return mLastFireEpoch; }
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
void SwapRefreshDrivers(RefreshDriverTimer* aNewTimer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
for (nsRefreshDriver* driver : mContentRefreshDrivers) {
|
|
|
|
aNewTimer->AddRefreshDriver(driver);
|
|
|
|
driver->mActiveTimer = aNewTimer;
|
|
|
|
}
|
|
|
|
mContentRefreshDrivers.Clear();
|
|
|
|
|
|
|
|
for (nsRefreshDriver* driver : mRootRefreshDrivers) {
|
2015-06-18 00:36:40 +03:00
|
|
|
aNewTimer->AddRefreshDriver(driver);
|
|
|
|
driver->mActiveTimer = aNewTimer;
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
2015-11-16 19:12:38 +03:00
|
|
|
mRootRefreshDrivers.Clear();
|
2015-01-14 02:38:00 +03:00
|
|
|
|
|
|
|
aNewTimer->mLastFireEpoch = mLastFireEpoch;
|
|
|
|
aNewTimer->mLastFireTime = mLastFireTime;
|
|
|
|
}
|
|
|
|
|
2016-08-22 15:52:45 +03:00
|
|
|
virtual TimeDuration GetTimerRate() = 0;
|
|
|
|
|
|
|
|
bool LastTickSkippedAnyPaints() const
|
|
|
|
{
|
|
|
|
return mLastFireSkipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<TimeStamp> GetIdleDeadlineHint()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (LastTickSkippedAnyPaints()) {
|
|
|
|
return Some(TimeStamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp mostRecentRefresh = MostRecentRefresh();
|
|
|
|
TimeDuration refreshRate = GetTimerRate();
|
|
|
|
TimeStamp idleEnd = mostRecentRefresh + refreshRate;
|
|
|
|
|
|
|
|
if (idleEnd +
|
|
|
|
refreshRate * nsLayoutUtils::QuiescentFramesBeforeIdlePeriod() <
|
|
|
|
TimeStamp::Now()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(idleEnd - TimeDuration::FromMilliseconds(
|
|
|
|
nsLayoutUtils::IdlePeriodDeadlineLimit()));
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
protected:
|
|
|
|
virtual void StartTimer() = 0;
|
|
|
|
virtual void StopTimer() = 0;
|
|
|
|
virtual void ScheduleNextTick(TimeStamp aNowTime) = 0;
|
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
bool IsRootRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
2016-10-12 14:27:38 +03:00
|
|
|
nsPresContext* pc = aDriver->GetPresContext();
|
|
|
|
nsPresContext* rootContext = pc ? pc->GetRootPresContext() : nullptr;
|
2016-01-22 04:09:04 +03:00
|
|
|
if (!rootContext) {
|
2015-11-16 19:12:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-22 04:09:04 +03:00
|
|
|
return aDriver == rootContext->RefreshDriver();
|
2015-11-16 19:12:38 +03:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
/*
|
|
|
|
* Actually runs a tick, poking all the attached RefreshDrivers.
|
|
|
|
* Grabs the "now" time via JS_Now and TimeStamp::Now().
|
|
|
|
*/
|
|
|
|
void Tick()
|
|
|
|
{
|
|
|
|
int64_t jsnow = JS_Now();
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2015-01-14 02:38:00 +03:00
|
|
|
Tick(jsnow, now);
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
void TickRefreshDrivers(int64_t aJsNow, TimeStamp aNow, nsTArray<RefPtr<nsRefreshDriver>>& aDrivers)
|
|
|
|
{
|
|
|
|
if (aDrivers.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<RefPtr<nsRefreshDriver> > drivers(aDrivers);
|
|
|
|
for (nsRefreshDriver* driver : drivers) {
|
|
|
|
// don't poke this driver if it's in test mode
|
|
|
|
if (driver->IsTestControllingRefreshesEnabled()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TickDriver(driver, aJsNow, aNow);
|
2016-08-22 15:52:45 +03:00
|
|
|
|
|
|
|
mLastFireSkipped = mLastFireSkipped || driver->mSkippedPaints;
|
2015-11-16 19:12:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
/*
|
|
|
|
* Tick the refresh drivers based on the given timestamp.
|
|
|
|
*/
|
|
|
|
void Tick(int64_t jsnow, TimeStamp now)
|
|
|
|
{
|
2012-12-12 02:15:32 +04:00
|
|
|
ScheduleNextTick(now);
|
|
|
|
|
|
|
|
mLastFireEpoch = jsnow;
|
|
|
|
mLastFireTime = now;
|
2016-08-22 15:52:45 +03:00
|
|
|
mLastFireSkipped = false;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
LOG("[%p] ticking drivers...", this);
|
2014-04-22 22:13:00 +04:00
|
|
|
// RD is short for RefreshDriver
|
2017-01-07 23:56:49 +03:00
|
|
|
GeckoProfilerTracingRAII tracer("Paint", "RD");
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
TickRefreshDrivers(jsnow, now, mContentRefreshDrivers);
|
|
|
|
TickRefreshDrivers(jsnow, now, mRootRefreshDrivers);
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
LOG("[%p] done.", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TickDriver(nsRefreshDriver* driver, int64_t jsnow, TimeStamp now)
|
|
|
|
{
|
|
|
|
LOG(">> TickDriver: %p (jsnow: %lld)", driver, jsnow);
|
|
|
|
driver->Tick(jsnow, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t mLastFireEpoch;
|
2016-08-22 15:52:45 +03:00
|
|
|
bool mLastFireSkipped;
|
2012-12-12 02:15:32 +04:00
|
|
|
TimeStamp mLastFireTime;
|
|
|
|
TimeStamp mTargetTime;
|
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
nsTArray<RefPtr<nsRefreshDriver> > mContentRefreshDrivers;
|
|
|
|
nsTArray<RefPtr<nsRefreshDriver> > mRootRefreshDrivers;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
// useful callback for nsITimer-based derived classes, here
|
|
|
|
// bacause of c++ protected shenanigans
|
|
|
|
static void TimerTick(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
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:
|
2015-01-06 20:28:18 +03:00
|
|
|
/*
|
|
|
|
* aRate -- the delay, in milliseconds, requested between timer firings
|
|
|
|
*/
|
2014-09-01 07:36:37 +04:00
|
|
|
explicit SimpleTimerBasedRefreshDriverTimer(double aRate)
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
2015-01-06 20:28:18 +03:00
|
|
|
SetRate(aRate);
|
2012-12-12 02:15:32 +04:00
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
~SimpleTimerBasedRefreshDriverTimer() override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:28:18 +03:00
|
|
|
// will take effect at next timer tick
|
|
|
|
virtual void SetRate(double aNewRate)
|
|
|
|
{
|
|
|
|
mRateMilliseconds = aNewRate;
|
|
|
|
mRateDuration = TimeDuration::FromMilliseconds(mRateMilliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetRate() const
|
|
|
|
{
|
|
|
|
return mRateMilliseconds;
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
TimeDuration GetTimerRate() override
|
2016-08-22 15:52:45 +03:00
|
|
|
{
|
|
|
|
return mRateDuration;
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
protected:
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StartTimer() override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
2012-12-24 04:29:23 +04:00
|
|
|
// pretend we just fired, and we schedule the next tick normally
|
|
|
|
mLastFireEpoch = JS_Now();
|
2012-12-12 02:15:32 +04:00
|
|
|
mLastFireTime = TimeStamp::Now();
|
|
|
|
|
2012-12-24 04:29:23 +04:00
|
|
|
mTargetTime = mLastFireTime + mRateDuration;
|
|
|
|
|
|
|
|
uint32_t delay = static_cast<uint32_t>(mRateMilliseconds);
|
|
|
|
mTimer->InitWithFuncCallback(TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT);
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StopTimer() override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:28:18 +03:00
|
|
|
double mRateMilliseconds;
|
|
|
|
TimeDuration mRateDuration;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsITimer> mTimer;
|
2012-12-12 02:15:32 +04:00
|
|
|
};
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
VsyncRefreshDriverTimer()
|
|
|
|
: mVsyncChild(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mVsyncObserver = new RefreshDriverVsyncObserver(this);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::gfx::VsyncSource> vsyncSource = gfxPlatform::GetPlatform()->GetHardwareVsync();
|
2015-01-14 02:38:00 +03:00
|
|
|
MOZ_ALWAYS_TRUE(mVsyncDispatcher = vsyncSource->GetRefreshTimerVsyncDispatcher());
|
|
|
|
mVsyncDispatcher->SetParentRefreshTimer(mVsyncObserver);
|
2016-08-22 15:52:45 +03:00
|
|
|
mVsyncRate = vsyncSource->GetGlobalDisplay().GetVsyncRate();
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit VsyncRefreshDriverTimer(VsyncChild* aVsyncChild)
|
|
|
|
: mVsyncChild(aVsyncChild)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mVsyncChild);
|
|
|
|
mVsyncObserver = new RefreshDriverVsyncObserver(this);
|
|
|
|
mVsyncChild->SetVsyncObserver(mVsyncObserver);
|
2016-08-22 15:52:45 +03:00
|
|
|
mVsyncRate = mVsyncChild->GetVsyncRate();
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
TimeDuration GetTimerRate() override
|
2016-08-22 15:52:45 +03:00
|
|
|
{
|
|
|
|
if (mVsyncRate != TimeDuration::Forever()) {
|
|
|
|
return mVsyncRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVsyncChild) {
|
2016-11-10 12:01:51 +03:00
|
|
|
// VsyncChild::VsyncRate() is a simple getter for the cached
|
|
|
|
// hardware vsync rate. We depend on that
|
|
|
|
// VsyncChild::GetVsyncRate() being called in the constructor
|
|
|
|
// will result in a response with the actual vsync rate sooner
|
|
|
|
// or later. Until that happens VsyncChild::VsyncRate() returns
|
|
|
|
// TimeDuration::Forever() and we have to guess below.
|
|
|
|
mVsyncRate = mVsyncChild->VsyncRate();
|
2016-08-22 15:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If hardware queries fail / are unsupported, we have to just guess.
|
|
|
|
return mVsyncRate != TimeDuration::Forever()
|
|
|
|
? mVsyncRate
|
|
|
|
: TimeDuration::FromMilliseconds(1000.0 / 60.0);
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Since VsyncObservers are refCounted, but the RefreshDriverTimer are
|
|
|
|
// explicitly shutdown. We create an inner class that has the VsyncObserver
|
|
|
|
// and is shutdown when the RefreshDriverTimer is deleted. The alternative is
|
|
|
|
// to (a) make all RefreshDriverTimer RefCounted or (b) use different
|
|
|
|
// VsyncObserver types.
|
2015-03-21 19:28:04 +03:00
|
|
|
class RefreshDriverVsyncObserver final : public VsyncObserver
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit RefreshDriverVsyncObserver(VsyncRefreshDriverTimer* aVsyncRefreshDriverTimer)
|
|
|
|
: mVsyncRefreshDriverTimer(aVsyncRefreshDriverTimer)
|
2015-01-30 04:41:03 +03:00
|
|
|
, mRefreshTickLock("RefreshTickLock")
|
2015-11-06 19:20:58 +03:00
|
|
|
, mRecentVsync(TimeStamp::Now())
|
|
|
|
, mLastChildTick(TimeStamp::Now())
|
|
|
|
, mVsyncRate(TimeDuration::Forever())
|
2015-01-30 04:41:03 +03:00
|
|
|
, mProcessedVsync(true)
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:59:36 +03:00
|
|
|
class ParentProcessVsyncNotifier final: public Runnable,
|
|
|
|
public nsIRunnablePriority
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ParentProcessVsyncNotifier(RefreshDriverVsyncObserver* aObserver,
|
|
|
|
TimeStamp aVsyncTimestamp)
|
|
|
|
: mObserver(aObserver), mVsyncTimestamp(aVsyncTimestamp) {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
mObserver->TickRefreshDriver(mVsyncTimestamp);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetPriority(uint32_t* aPriority) override
|
|
|
|
{
|
|
|
|
*aPriority = nsIRunnablePriority::PRIORITY_HIGH;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ParentProcessVsyncNotifier() {}
|
|
|
|
RefPtr<RefreshDriverVsyncObserver> mObserver;
|
|
|
|
TimeStamp mVsyncTimestamp;
|
|
|
|
};
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
bool NotifyVsync(TimeStamp aVsyncTimestamp) override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
2015-01-30 04:41:03 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
// 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
|
|
|
|
MonitorAutoLock lock(mRefreshTickLock);
|
|
|
|
mRecentVsync = aVsyncTimestamp;
|
|
|
|
if (!mProcessedVsync) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
mProcessedVsync = false;
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> vsyncEvent =
|
2017-02-11 17:59:36 +03:00
|
|
|
new ParentProcessVsyncNotifier(this, aVsyncTimestamp);
|
2015-01-14 02:38:00 +03:00
|
|
|
NS_DispatchToMainThread(vsyncEvent);
|
|
|
|
} else {
|
2017-02-16 19:50:44 +03:00
|
|
|
mRecentVsync = aVsyncTimestamp;
|
|
|
|
if (!mBlockUntil.IsNull() && mBlockUntil > aVsyncTimestamp) {
|
|
|
|
if (mProcessedVsync) {
|
|
|
|
// Re-post vsync update as a normal priority runnable. This way
|
|
|
|
// runnables already in normal priority queue get processed.
|
|
|
|
mProcessedVsync = false;
|
|
|
|
nsCOMPtr<nsIRunnable> vsyncEvent =
|
|
|
|
NewRunnableMethod<>(
|
|
|
|
this, &RefreshDriverVsyncObserver::NormalPriorityNotify);
|
|
|
|
NS_DispatchToMainThread(vsyncEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
TickRefreshDriver(aVsyncTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mVsyncRefreshDriverTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
void OnTimerStart()
|
|
|
|
{
|
|
|
|
if (!XRE_IsParentProcess()) {
|
|
|
|
mLastChildTick = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 19:50:44 +03:00
|
|
|
|
|
|
|
void NormalPriorityNotify()
|
|
|
|
{
|
|
|
|
if (mLastProcessedTickInChildProcess.IsNull() ||
|
|
|
|
mRecentVsync > mLastProcessedTickInChildProcess) {
|
|
|
|
// mBlockUntil is for high priority vsync notifications only.
|
|
|
|
mBlockUntil = TimeStamp();
|
|
|
|
TickRefreshDriver(mRecentVsync);
|
|
|
|
}
|
|
|
|
|
|
|
|
mProcessedVsync = true;
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
private:
|
2016-11-09 16:35:40 +03:00
|
|
|
~RefreshDriverVsyncObserver() = default;
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
void RecordTelemetryProbes(TimeStamp aVsyncTimestamp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
#ifndef ANDROID /* bug 1142079 */
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
TimeDuration vsyncLatency = TimeStamp::Now() - aVsyncTimestamp;
|
2015-12-16 21:06:40 +03:00
|
|
|
uint32_t sample = (uint32_t)vsyncLatency.ToMilliseconds();
|
2015-11-06 19:20:58 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_CHROME_FRAME_DELAY_MS,
|
2015-12-16 21:06:40 +03:00
|
|
|
sample);
|
|
|
|
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_SYNC_SCROLL_FRAME_DELAY_MS,
|
|
|
|
sample);
|
2016-01-14 17:07:18 +03:00
|
|
|
RecordJank(sample);
|
2015-11-06 19:20:58 +03:00
|
|
|
} else if (mVsyncRate != TimeDuration::Forever()) {
|
|
|
|
TimeDuration contentDelay = (TimeStamp::Now() - mLastChildTick) - mVsyncRate;
|
2015-11-17 03:21:04 +03:00
|
|
|
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);
|
|
|
|
}
|
2015-12-16 21:06:40 +03:00
|
|
|
uint32_t sample = (uint32_t)contentDelay.ToMilliseconds();
|
2015-11-06 19:20:58 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_CONTENT_FRAME_DELAY_MS,
|
2015-12-16 21:06:40 +03:00
|
|
|
sample);
|
|
|
|
Telemetry::Accumulate(Telemetry::FX_REFRESH_DRIVER_SYNC_SCROLL_FRAME_DELAY_MS,
|
|
|
|
sample);
|
2016-01-14 17:07:18 +03:00
|
|
|
RecordJank(sample);
|
2015-11-06 19:20:58 +03:00
|
|
|
} else {
|
|
|
|
// Request the vsync rate from the parent process. Might be a few vsyncs
|
|
|
|
// until the parent responds.
|
|
|
|
mVsyncRate = mVsyncRefreshDriverTimer->mVsyncChild->GetVsyncRate();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:07:18 +03:00
|
|
|
void RecordJank(uint32_t aJankMS)
|
|
|
|
{
|
|
|
|
uint32_t duration = 1 /* ms */;
|
|
|
|
for (size_t i = 0;
|
|
|
|
i < mozilla::ArrayLength(sJankLevels) && duration < aJankMS;
|
|
|
|
++i, duration *= 2) {
|
|
|
|
sJankLevels[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
void TickRefreshDriver(TimeStamp aVsyncTimestamp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
RecordTelemetryProbes(aVsyncTimestamp);
|
2015-01-30 04:41:03 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
MonitorAutoLock lock(mRefreshTickLock);
|
|
|
|
aVsyncTimestamp = mRecentVsync;
|
2015-11-14 00:47:34 +03:00
|
|
|
mProcessedVsync = true;
|
2015-11-06 19:20:58 +03:00
|
|
|
} else {
|
2017-02-16 19:50:44 +03:00
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
mLastChildTick = TimeStamp::Now();
|
2017-02-16 19:50:44 +03:00
|
|
|
mLastProcessedTickInChildProcess = aVsyncTimestamp;
|
2015-01-30 04:41:03 +03:00
|
|
|
}
|
2015-08-18 19:11:12 +03:00
|
|
|
MOZ_ASSERT(aVsyncTimestamp <= TimeStamp::Now());
|
2015-01-30 04:41:03 +03:00
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
// We might have a problem that we call ~VsyncRefreshDriverTimer() before
|
|
|
|
// the scheduled TickRefreshDriver() runs. Check mVsyncRefreshDriverTimer
|
|
|
|
// before use.
|
|
|
|
if (mVsyncRefreshDriverTimer) {
|
|
|
|
mVsyncRefreshDriverTimer->RunRefreshDrivers(aVsyncTimestamp);
|
|
|
|
}
|
2017-02-11 17:42:20 +03:00
|
|
|
|
|
|
|
if (!XRE_IsParentProcess()) {
|
|
|
|
TimeDuration tickDuration = TimeStamp::Now() - mLastChildTick;
|
|
|
|
mBlockUntil = aVsyncTimestamp + tickDuration;
|
|
|
|
}
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// VsyncRefreshDriverTimer holds this RefreshDriverVsyncObserver and it will
|
|
|
|
// be always available before Shutdown(). We can just use the raw pointer
|
|
|
|
// here.
|
|
|
|
VsyncRefreshDriverTimer* mVsyncRefreshDriverTimer;
|
2015-01-30 04:41:03 +03:00
|
|
|
Monitor mRefreshTickLock;
|
|
|
|
TimeStamp mRecentVsync;
|
2015-11-06 19:20:58 +03:00
|
|
|
TimeStamp mLastChildTick;
|
2017-02-16 19:50:44 +03:00
|
|
|
TimeStamp mLastProcessedTickInChildProcess;
|
2017-02-11 17:42:20 +03:00
|
|
|
TimeStamp mBlockUntil;
|
2015-11-06 19:20:58 +03:00
|
|
|
TimeDuration mVsyncRate;
|
2015-01-30 04:41:03 +03:00
|
|
|
bool mProcessedVsync;
|
2015-01-14 02:38:00 +03:00
|
|
|
}; // RefreshDriverVsyncObserver
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
~VsyncRefreshDriverTimer() override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
mVsyncDispatcher->SetParentRefreshTimer(nullptr);
|
|
|
|
mVsyncDispatcher = nullptr;
|
|
|
|
} else {
|
|
|
|
// Since the PVsyncChild actors live through the life of the process, just
|
|
|
|
// send the unobserveVsync message to disable vsync event. We don't need
|
|
|
|
// to handle the cleanup stuff of this actor. PVsyncChild::ActorDestroy()
|
|
|
|
// will be called and clean up this actor.
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mVsyncChild->SendUnobserve();
|
2015-01-14 02:38:00 +03:00
|
|
|
mVsyncChild->SetVsyncObserver(nullptr);
|
|
|
|
mVsyncChild = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detach current vsync timer from this VsyncObserver. The observer will no
|
|
|
|
// longer tick this timer.
|
|
|
|
mVsyncObserver->Shutdown();
|
|
|
|
mVsyncObserver = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StartTimer() override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
2015-10-30 16:35:46 +03:00
|
|
|
// Protect updates to `sActiveVsyncTimers`.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
mLastFireEpoch = JS_Now();
|
|
|
|
mLastFireTime = TimeStamp::Now();
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
mVsyncDispatcher->SetParentRefreshTimer(mVsyncObserver);
|
|
|
|
} else {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mVsyncChild->SendObserve();
|
2015-11-06 19:20:58 +03:00
|
|
|
mVsyncObserver->OnTimerStart();
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
2015-10-30 16:35:46 +03:00
|
|
|
|
|
|
|
++sActiveVsyncTimers;
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StopTimer() override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
2015-10-30 16:35:46 +03:00
|
|
|
// Protect updates to `sActiveVsyncTimers`.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
mVsyncDispatcher->SetParentRefreshTimer(nullptr);
|
|
|
|
} else {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mVsyncChild->SendUnobserve();
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
2015-10-30 16:35:46 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(sActiveVsyncTimers > 0);
|
|
|
|
--sActiveVsyncTimers;
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void ScheduleNextTick(TimeStamp aNowTime) override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
// Do nothing since we just wait for the next vsync from
|
|
|
|
// RefreshDriverVsyncObserver.
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunRefreshDrivers(TimeStamp aTimeStamp)
|
|
|
|
{
|
|
|
|
int64_t jsnow = JS_Now();
|
|
|
|
TimeDuration diff = TimeStamp::Now() - aTimeStamp;
|
|
|
|
int64_t vsyncJsNow = jsnow - diff.ToMicroseconds();
|
|
|
|
Tick(vsyncJsNow, aTimeStamp);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefreshDriverVsyncObserver> mVsyncObserver;
|
2015-01-30 00:19:00 +03:00
|
|
|
// Used for parent process.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefreshTimerVsyncDispatcher> mVsyncDispatcher;
|
2015-01-30 00:19:00 +03:00
|
|
|
// Used for child process.
|
|
|
|
// The mVsyncChild will be always available before VsncChild::ActorDestroy().
|
|
|
|
// After ActorDestroy(), StartTimer() and StopTimer() calls will be non-op.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VsyncChild> mVsyncChild;
|
2016-08-22 15:52:45 +03:00
|
|
|
TimeDuration mVsyncRate;
|
2015-01-14 02:38:00 +03:00
|
|
|
}; // VsyncRefreshDriverTimer
|
|
|
|
|
2017-02-11 17:59:36 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(VsyncRefreshDriverTimer::
|
|
|
|
RefreshDriverVsyncObserver::
|
|
|
|
ParentProcessVsyncNotifier,
|
|
|
|
Runnable, nsIRunnablePriority)
|
|
|
|
|
2015-08-26 16:45:54 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
2012-12-12 02:15:32 +04:00
|
|
|
*/
|
2015-08-26 16:45:54 +03:00
|
|
|
class StartupRefreshDriverTimer :
|
2012-12-12 02:15:32 +04:00
|
|
|
public SimpleTimerBasedRefreshDriverTimer
|
|
|
|
{
|
|
|
|
public:
|
2015-08-26 16:45:54 +03:00
|
|
|
explicit StartupRefreshDriverTimer(double aRate)
|
2012-12-12 02:15:32 +04:00
|
|
|
: SimpleTimerBasedRefreshDriverTimer(aRate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-11-09 16:37:59 +03:00
|
|
|
void ScheduleNextTick(TimeStamp aNowTime) override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
2015-08-26 16:45:54 +03:00
|
|
|
// Since this is only used for startup, it isn't super critical
|
|
|
|
// that we tick at consistent intervals.
|
|
|
|
TimeStamp newTarget = aNowTime + mRateDuration;
|
2012-12-12 02:15:32 +04:00
|
|
|
uint32_t delay = static_cast<uint32_t>((newTarget - aNowTime).ToMilliseconds());
|
|
|
|
mTimer->InitWithFuncCallback(TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
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.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class InactiveRefreshDriverTimer final :
|
2015-01-06 20:28:18 +03:00
|
|
|
public SimpleTimerBasedRefreshDriverTimer
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-01 07:36:37 +04:00
|
|
|
explicit InactiveRefreshDriverTimer(double aRate)
|
2015-01-06 20:28:18 +03:00
|
|
|
: SimpleTimerBasedRefreshDriverTimer(aRate),
|
2012-12-12 02:15:32 +04:00
|
|
|
mNextTickDuration(aRate),
|
|
|
|
mDisableAfterMilliseconds(-1.0),
|
|
|
|
mNextDriverIndex(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InactiveRefreshDriverTimer(double aRate, double aDisableAfterMilliseconds)
|
2015-01-06 20:28:18 +03:00
|
|
|
: SimpleTimerBasedRefreshDriverTimer(aRate),
|
2012-12-12 02:15:32 +04:00
|
|
|
mNextTickDuration(aRate),
|
|
|
|
mDisableAfterMilliseconds(aDisableAfterMilliseconds),
|
|
|
|
mNextDriverIndex(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void AddRefreshDriver(nsRefreshDriver* aDriver) override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
RefreshDriverTimer::AddRefreshDriver(aDriver);
|
|
|
|
|
|
|
|
LOG("[%p] inactive timer got new refresh driver %p, resetting rate",
|
|
|
|
this, aDriver);
|
|
|
|
|
|
|
|
// 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.
|
2015-11-16 19:12:38 +03:00
|
|
|
mNextDriverIndex = GetRefreshDriverCount() - 1;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
StopTimer();
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
TimeDuration GetTimerRate() override
|
2016-08-22 15:52:45 +03:00
|
|
|
{
|
|
|
|
return TimeDuration::FromMilliseconds(mNextTickDuration);
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
protected:
|
2015-11-16 19:12:38 +03:00
|
|
|
uint32_t GetRefreshDriverCount()
|
|
|
|
{
|
|
|
|
return mContentRefreshDrivers.Length() + mRootRefreshDrivers.Length();
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StartTimer() override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
2012-12-24 04:29:23 +04:00
|
|
|
mLastFireEpoch = JS_Now();
|
2012-12-12 02:15:32 +04:00
|
|
|
mLastFireTime = TimeStamp::Now();
|
|
|
|
|
2012-12-24 04:29:23 +04:00
|
|
|
mTargetTime = mLastFireTime + mRateDuration;
|
|
|
|
|
|
|
|
uint32_t delay = static_cast<uint32_t>(mRateMilliseconds);
|
|
|
|
mTimer->InitWithFuncCallback(TimerTickOne, this, delay, nsITimer::TYPE_ONE_SHOT);
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void StopTimer() override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void ScheduleNextTick(TimeStamp aNowTime) override
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
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
|
2015-11-16 19:12:38 +03:00
|
|
|
if (mNextDriverIndex >= GetRefreshDriverCount()) {
|
2012-12-12 02:15:32 +04:00
|
|
|
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->InitWithFuncCallback(TimerTickOne, this, delay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
LOG("[%p] inactive timer next tick in %f ms [index %d/%d]", this, mNextTickDuration,
|
2015-11-16 19:12:38 +03:00
|
|
|
mNextDriverIndex, GetRefreshDriverCount());
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Runs just one driver's tick. */
|
|
|
|
void TickOne()
|
|
|
|
{
|
|
|
|
int64_t jsnow = JS_Now();
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
|
|
|
|
ScheduleNextTick(now);
|
|
|
|
|
|
|
|
mLastFireEpoch = jsnow;
|
|
|
|
mLastFireTime = now;
|
2016-08-22 15:52:45 +03:00
|
|
|
mLastFireSkipped = false;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2015-11-16 19:12:38 +03:00
|
|
|
nsTArray<RefPtr<nsRefreshDriver> > drivers(mContentRefreshDrivers);
|
|
|
|
drivers.AppendElements(mRootRefreshDrivers);
|
2016-08-22 15:52:45 +03:00
|
|
|
size_t index = mNextDriverIndex;
|
2015-11-16 19:12:38 +03:00
|
|
|
|
2016-08-22 15:52:45 +03:00
|
|
|
if (index < drivers.Length() &&
|
|
|
|
!drivers[index]->IsTestControllingRefreshesEnabled())
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
2016-08-22 15:52:45 +03:00
|
|
|
TickDriver(drivers[index], jsnow, now);
|
|
|
|
mLastFireSkipped = mLastFireSkipped || drivers[index]->SkippedPaints();
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
mNextDriverIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TimerTickOne(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
InactiveRefreshDriverTimer *timer = static_cast<InactiveRefreshDriverTimer*>(aClosure);
|
|
|
|
timer->TickOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
double mNextTickDuration;
|
|
|
|
double mDisableAfterMilliseconds;
|
|
|
|
uint32_t mNextDriverIndex;
|
|
|
|
};
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
// The PBackground protocol connection callback. It will be called when
|
|
|
|
// PBackground is ready. Then we create the PVsync sub-protocol for our
|
|
|
|
// vsync-base RefreshTimer.
|
2015-03-21 19:28:04 +03:00
|
|
|
class VsyncChildCreateCallback final : public nsIIPCBackgroundChildCreateCallback
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
public:
|
|
|
|
VsyncChildCreateCallback()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateVsyncActor(PBackgroundChild* aPBackgroundChild)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aPBackgroundChild);
|
|
|
|
|
|
|
|
layout::PVsyncChild* actor = aPBackgroundChild->SendPVsyncConstructor();
|
|
|
|
layout::VsyncChild* child = static_cast<layout::VsyncChild*>(actor);
|
|
|
|
nsRefreshDriver::PVsyncActorCreated(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-09 16:35:40 +03:00
|
|
|
virtual ~VsyncChildCreateCallback() = default;
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void ActorCreated(PBackgroundChild* aPBackgroundChild) override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aPBackgroundChild);
|
|
|
|
CreateVsyncActor(aPBackgroundChild);
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:37:59 +03:00
|
|
|
void ActorFailed() override
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_CRASH("Failed To Create VsyncChild Actor");
|
|
|
|
}
|
|
|
|
}; // VsyncChildCreateCallback
|
|
|
|
NS_IMPL_ISUPPORTS(VsyncChildCreateCallback, nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
static RefreshDriverTimer* sRegularRateTimer;
|
|
|
|
static InactiveRefreshDriverTimer* sThrottledRateTimer;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
static int32_t sHighPrecisionTimerRequests = 0;
|
|
|
|
// a bare pointer to avoid introducing a static constructor
|
|
|
|
static nsITimer *sDisableHighPrecisionTimersTimer = nullptr;
|
|
|
|
#endif
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2015-01-19 04:28:00 +03:00
|
|
|
static void
|
|
|
|
CreateContentVsyncRefreshTimer(void*)
|
2015-01-14 02:38:00 +03:00
|
|
|
{
|
2015-01-19 04:28:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!XRE_IsParentProcess());
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2015-01-19 04:28:00 +03:00
|
|
|
// Create the PVsync actor child for vsync-base refresh timer.
|
2015-01-14 02:38:00 +03:00
|
|
|
// PBackgroundChild is created asynchronously. If PBackgroundChild is still
|
|
|
|
// unavailable, setup VsyncChildCreateCallback callback to handle the async
|
|
|
|
// connect. We will still use software timer before PVsync ready, and change
|
|
|
|
// to use hw timer when the connection is done. Please check
|
|
|
|
// VsyncChildCreateCallback::CreateVsyncActor() and
|
|
|
|
// nsRefreshDriver::PVsyncActorCreated().
|
|
|
|
PBackgroundChild* backgroundChild = BackgroundChild::GetForCurrentThread();
|
|
|
|
if (backgroundChild) {
|
|
|
|
// If we already have PBackgroundChild, create the
|
|
|
|
// child VsyncRefreshDriverTimer here.
|
|
|
|
VsyncChildCreateCallback::CreateVsyncActor(backgroundChild);
|
2015-01-19 04:28:00 +03:00
|
|
|
return;
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
// Setup VsyncChildCreateCallback callback
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIIPCBackgroundChildCreateCallback> callback = new VsyncChildCreateCallback();
|
2015-01-14 02:38:00 +03:00
|
|
|
if (NS_WARN_IF(!BackgroundChild::GetOrCreateForCurrentThread(callback))) {
|
|
|
|
MOZ_CRASH("PVsync actor create failed!");
|
|
|
|
}
|
2015-01-19 04:28:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CreateVsyncRefreshTimer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-14 17:07:18 +03:00
|
|
|
PodArrayZero(sJankLevels);
|
2015-01-19 04:28:00 +03:00
|
|
|
// Sometimes, gfxPrefs is not initialized here. Make sure the gfxPrefs is
|
|
|
|
// ready.
|
|
|
|
gfxPrefs::GetSingleton();
|
|
|
|
|
2015-08-25 16:01:40 +03:00
|
|
|
if (gfxPlatform::IsInLayoutAsapMode()) {
|
2015-01-19 04:28:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
// Make sure all vsync systems are ready.
|
|
|
|
gfxPlatform::GetPlatform();
|
|
|
|
// In parent process, we don't need to use ipc. We can create the
|
|
|
|
// VsyncRefreshDriverTimer directly.
|
|
|
|
sRegularRateTimer = new VsyncRefreshDriverTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this process is not created by NUWA, just create the vsync timer here.
|
|
|
|
CreateContentVsyncRefreshTimer(nullptr);
|
2015-01-14 02:38:00 +03:00
|
|
|
}
|
|
|
|
|
2013-05-18 00:57:26 +04:00
|
|
|
static uint32_t
|
|
|
|
GetFirstFrameDelay(imgIRequest* req)
|
|
|
|
{
|
|
|
|
nsCOMPtr<imgIContainer> container;
|
|
|
|
if (NS_FAILED(req->GetImage(getter_AddRefs(container))) || !container) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this image isn't animated, there isn't a first frame delay.
|
|
|
|
int32_t delay = container->GetFirstFrameDelay();
|
|
|
|
if (delay < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return static_cast<uint32_t>(delay);
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
/* static */ void
|
|
|
|
nsRefreshDriver::Shutdown()
|
|
|
|
{
|
|
|
|
// clean up our timers
|
|
|
|
delete sRegularRateTimer;
|
|
|
|
delete sThrottledRateTimer;
|
|
|
|
|
|
|
|
sRegularRateTimer = nullptr;
|
|
|
|
sThrottledRateTimer = nullptr;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (sDisableHighPrecisionTimersTimer) {
|
|
|
|
sDisableHighPrecisionTimersTimer->Cancel();
|
|
|
|
NS_RELEASE(sDisableHighPrecisionTimersTimer);
|
|
|
|
timeEndPeriod(1);
|
|
|
|
} else if (sHighPrecisionTimerRequests) {
|
|
|
|
timeEndPeriod(1);
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-07 19:58:48 +03:00
|
|
|
}
|
2011-12-24 07:52:26 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* static */ int32_t
|
2011-12-24 07:52:26 +04:00
|
|
|
nsRefreshDriver::DefaultInterval()
|
|
|
|
{
|
2015-09-15 18:13:57 +03:00
|
|
|
return NSToIntRound(1000.0 / gfxPlatform::GetDefaultFrameRate());
|
2011-12-24 07:52:26 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 14:31:16 +04:00
|
|
|
// 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)
|
2013-08-03 00:29:57 +04:00
|
|
|
// 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.
|
2012-12-12 02:15:32 +04:00
|
|
|
double
|
2013-05-29 14:31:16 +04:00
|
|
|
nsRefreshDriver::GetRegularTimerInterval(bool *outIsDefault) const
|
2010-08-23 13:30:08 +04:00
|
|
|
{
|
2012-12-12 02:15:32 +04:00
|
|
|
int32_t rate = Preferences::GetInt("layout.frame_rate", -1);
|
2013-08-03 00:29:57 +04:00
|
|
|
if (rate < 0) {
|
2015-09-15 18:13:57 +03:00
|
|
|
rate = gfxPlatform::GetDefaultFrameRate();
|
2013-05-29 14:31:16 +04:00
|
|
|
if (outIsDefault) {
|
|
|
|
*outIsDefault = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (outIsDefault) {
|
|
|
|
*outIsDefault = false;
|
|
|
|
}
|
2010-08-23 13:30:08 +04:00
|
|
|
}
|
2013-08-03 00:29:57 +04:00
|
|
|
|
|
|
|
if (rate == 0) {
|
|
|
|
rate = 10000;
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
return 1000.0 / rate;
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:44:40 +03:00
|
|
|
/* static */ double
|
|
|
|
nsRefreshDriver::GetThrottledTimerInterval()
|
2012-12-12 02:15:32 +04:00
|
|
|
{
|
|
|
|
int32_t rate = Preferences::GetInt("layout.throttled_frame_rate", -1);
|
|
|
|
if (rate <= 0) {
|
|
|
|
rate = DEFAULT_THROTTLED_FRAME_RATE;
|
2011-03-04 18:27:02 +03:00
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
return 1000.0 / rate;
|
|
|
|
}
|
|
|
|
|
2015-05-30 09:50:51 +03:00
|
|
|
/* static */ mozilla::TimeDuration
|
|
|
|
nsRefreshDriver::GetMinRecomputeVisibilityInterval()
|
|
|
|
{
|
|
|
|
int32_t interval =
|
|
|
|
Preferences::GetInt("layout.visibility.min-recompute-interval-ms", -1);
|
|
|
|
if (interval <= 0) {
|
|
|
|
interval = DEFAULT_RECOMPUTE_VISIBILITY_INTERVAL_MS;
|
|
|
|
}
|
|
|
|
return TimeDuration::FromMilliseconds(interval);
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
double
|
|
|
|
nsRefreshDriver::GetRefreshTimerInterval() const
|
|
|
|
{
|
|
|
|
return mThrottled ? GetThrottledTimerInterval() : GetRegularTimerInterval();
|
2010-08-23 13:30:08 +04:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
RefreshDriverTimer*
|
|
|
|
nsRefreshDriver::ChooseTimer() const
|
2010-08-23 13:30:08 +04:00
|
|
|
{
|
2011-03-04 18:27:02 +03:00
|
|
|
if (mThrottled) {
|
2015-07-18 16:35:59 +03:00
|
|
|
if (!sThrottledRateTimer)
|
2012-12-12 02:15:32 +04:00
|
|
|
sThrottledRateTimer = new InactiveRefreshDriverTimer(GetThrottledTimerInterval(),
|
|
|
|
DEFAULT_INACTIVE_TIMER_DISABLE_SECONDS * 1000.0);
|
|
|
|
return sThrottledRateTimer;
|
2012-10-02 20:38:49 +04:00
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
|
2013-05-29 14:31:16 +04:00
|
|
|
if (!sRegularRateTimer) {
|
|
|
|
bool isDefault = true;
|
|
|
|
double rate = GetRegularTimerInterval(&isDefault);
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2015-01-19 04:28:00 +03:00
|
|
|
// Try to use vsync-base refresh timer first for sRegularRateTimer.
|
|
|
|
CreateVsyncRefreshTimer();
|
2015-01-14 02:38:00 +03:00
|
|
|
|
2013-05-29 14:31:16 +04:00
|
|
|
if (!sRegularRateTimer) {
|
2015-08-26 16:45:54 +03:00
|
|
|
sRegularRateTimer = new StartupRefreshDriverTimer(rate);
|
2013-05-29 14:31:16 +04:00
|
|
|
}
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
return sRegularRateTimer;
|
2010-08-23 13:30:08 +04:00
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
nsRefreshDriver::nsRefreshDriver(nsPresContext* aPresContext)
|
|
|
|
: mActiveTimer(nullptr),
|
|
|
|
mPresContext(aPresContext),
|
2014-05-29 01:43:41 +04:00
|
|
|
mRootRefresh(nullptr),
|
2014-05-29 01:42:17 +04:00
|
|
|
mPendingTransaction(0),
|
|
|
|
mCompletedTransaction(0),
|
2013-12-20 01:38:35 +04:00
|
|
|
mFreezeCount(0),
|
2015-04-21 19:44:40 +03:00
|
|
|
mThrottledFrameRequestInterval(TimeDuration::FromMilliseconds(
|
|
|
|
GetThrottledTimerInterval())),
|
2015-05-30 09:50:51 +03:00
|
|
|
mMinRecomputeVisibilityInterval(GetMinRecomputeVisibilityInterval()),
|
2011-03-07 19:58:48 +03:00
|
|
|
mThrottled(false),
|
2015-05-30 09:50:51 +03:00
|
|
|
mNeedToRecomputeVisibility(false),
|
2011-04-12 10:18:43 +04:00
|
|
|
mTestControllingRefreshes(false),
|
2011-12-24 07:52:22 +04:00
|
|
|
mViewManagerFlushIsPending(false),
|
2013-09-04 15:47:21 +04:00
|
|
|
mRequestedHighPrecision(false),
|
2014-05-29 01:42:17 +04:00
|
|
|
mInRefresh(false),
|
|
|
|
mWaitingForTransaction(false),
|
2016-05-03 10:58:57 +03:00
|
|
|
mSkippedPaints(false),
|
2016-09-09 13:06:29 +03:00
|
|
|
mResizeSuppressed(false),
|
2016-09-15 23:48:48 +03:00
|
|
|
mWarningThreshold(REFRESH_WAIT_WARNING)
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
2016-06-06 16:51:15 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPresContext,
|
|
|
|
"Need a pres context to tell us to call Disconnect() later "
|
|
|
|
"and decrement sRefreshDriverCount.");
|
2012-12-12 02:15:32 +04:00
|
|
|
mMostRecentRefreshEpochTime = JS_Now();
|
|
|
|
mMostRecentRefresh = TimeStamp::Now();
|
2014-08-15 06:24:50 +04:00
|
|
|
mMostRecentTick = mMostRecentRefresh;
|
2015-04-21 19:44:40 +03:00
|
|
|
mNextThrottledFrameRequestTick = mMostRecentTick;
|
2015-05-30 09:50:51 +03:00
|
|
|
mNextRecomputeVisibilityTick = mMostRecentTick;
|
2016-06-06 16:51:15 +03:00
|
|
|
|
2016-06-06 16:53:41 +03:00
|
|
|
++sRefreshDriverCount;
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver::~nsRefreshDriver()
|
|
|
|
{
|
2016-06-06 16:51:15 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(ObserverCount() == 0,
|
|
|
|
"observers should have unregistered");
|
|
|
|
MOZ_ASSERT(!mActiveTimer, "timer should be gone");
|
2016-06-06 16:51:15 +03:00
|
|
|
MOZ_ASSERT(!mPresContext,
|
|
|
|
"Should have called Disconnect() and decremented "
|
|
|
|
"sRefreshDriverCount!");
|
2015-07-18 16:35:59 +03:00
|
|
|
|
2014-05-29 01:43:41 +04:00
|
|
|
if (mRootRefresh) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mRootRefresh->RemoveRefreshObserver(this, FlushType::Style);
|
2014-05-29 01:43:41 +04:00
|
|
|
mRootRefresh = nullptr;
|
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
// Method for testing. See nsIDOMWindowUtils.advanceTimeAndRefresh
|
|
|
|
// for description.
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsRefreshDriver::AdvanceTimeAndRefresh(int64_t aMilliseconds)
|
2011-04-12 10:18:43 +04:00
|
|
|
{
|
2012-12-12 02:15:32 +04:00
|
|
|
// ensure that we're removed from our driver
|
|
|
|
StopTimer();
|
|
|
|
|
|
|
|
if (!mTestControllingRefreshes) {
|
|
|
|
mMostRecentRefreshEpochTime = JS_Now();
|
|
|
|
mMostRecentRefresh = TimeStamp::Now();
|
|
|
|
|
|
|
|
mTestControllingRefreshes = true;
|
2014-05-29 01:42:17 +04:00
|
|
|
if (mWaitingForTransaction) {
|
|
|
|
// Disable any refresh driver throttling when entering test mode
|
|
|
|
mWaitingForTransaction = false;
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = false;
|
2016-09-15 23:48:48 +03:00
|
|
|
mWarningThreshold = REFRESH_WAIT_WARNING;
|
2014-05-29 01:42:17 +04:00
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
mMostRecentRefreshEpochTime += aMilliseconds * 1000;
|
2012-12-12 02:15:32 +04:00
|
|
|
mMostRecentRefresh += TimeDuration::FromMilliseconds((double) aMilliseconds);
|
|
|
|
|
2014-04-15 07:27:00 +04:00
|
|
|
mozilla::dom::AutoNoJSAPI nojsapi;
|
2013-02-26 23:04:11 +04:00
|
|
|
DoTick();
|
2011-04-12 10:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RestoreNormalRefresh()
|
|
|
|
{
|
|
|
|
mTestControllingRefreshes = false;
|
2015-01-06 04:56:02 +03:00
|
|
|
EnsureTimerStarted(eAllowTimeToGoBackwards);
|
2014-05-29 01:42:17 +04:00
|
|
|
mCompletedTransaction = mPendingTransaction;
|
2011-04-12 10:18:43 +04:00
|
|
|
}
|
|
|
|
|
2009-10-08 07:22:42 +04:00
|
|
|
TimeStamp
|
|
|
|
nsRefreshDriver::MostRecentRefresh() const
|
|
|
|
{
|
2015-01-06 04:56:02 +03:00
|
|
|
const_cast<nsRefreshDriver*>(this)->EnsureTimerStarted();
|
2012-12-24 04:29:23 +04:00
|
|
|
|
2009-10-08 07:22:42 +04:00
|
|
|
return mMostRecentRefresh;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t
|
2010-08-12 01:05:28 +04:00
|
|
|
nsRefreshDriver::MostRecentRefreshEpochTime() const
|
|
|
|
{
|
2015-01-06 04:56:02 +03:00
|
|
|
const_cast<nsRefreshDriver*>(this)->EnsureTimerStarted();
|
2012-12-24 04:29:23 +04:00
|
|
|
|
2010-08-12 01:05:28 +04:00
|
|
|
return mMostRecentRefreshEpochTime;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-12 02:15:32 +04:00
|
|
|
nsRefreshDriver::AddRefreshObserver(nsARefreshObserver* aObserver,
|
2017-01-05 10:31:56 +03:00
|
|
|
FlushType aFlushType)
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
2012-07-30 18:20:58 +04:00
|
|
|
bool success = array.AppendElement(aObserver) != nullptr;
|
2015-01-06 04:56:02 +03:00
|
|
|
EnsureTimerStarted();
|
2009-10-08 07:22:42 +04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-12 02:15:32 +04:00
|
|
|
nsRefreshDriver::RemoveRefreshObserver(nsARefreshObserver* aObserver,
|
2017-01-05 10:31:56 +03:00
|
|
|
FlushType aFlushType)
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
2009-12-31 22:07:57 +03:00
|
|
|
return array.RemoveElement(aObserver);
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
2013-08-19 17:08:44 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::AddPostRefreshObserver(nsAPostRefreshObserver* aObserver)
|
|
|
|
{
|
|
|
|
mPostRefreshObservers.AppendElement(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RemovePostRefreshObserver(nsAPostRefreshObserver* aObserver)
|
|
|
|
{
|
|
|
|
mPostRefreshObservers.RemoveElement(aObserver);
|
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
bool
|
|
|
|
nsRefreshDriver::AddImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
2013-05-18 00:57:26 +04:00
|
|
|
uint32_t delay = GetFirstFrameDelay(aRequest);
|
|
|
|
if (delay == 0) {
|
|
|
|
if (!mRequests.PutEntry(aRequest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ImageStartData* start = mStartTable.Get(delay);
|
|
|
|
if (!start) {
|
|
|
|
start = new ImageStartData();
|
|
|
|
mStartTable.Put(delay, start);
|
|
|
|
}
|
|
|
|
start->mEntries.PutEntry(aRequest);
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
2015-01-06 04:56:02 +03:00
|
|
|
EnsureTimerStarted();
|
2011-11-10 01:39:16 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RemoveImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
2013-05-18 00:57:26 +04:00
|
|
|
// Try to remove from both places, just in case, because we can't tell
|
|
|
|
// whether RemoveEntry() succeeds.
|
2011-11-10 01:39:16 +04:00
|
|
|
mRequests.RemoveEntry(aRequest);
|
2013-05-18 00:57:26 +04:00
|
|
|
uint32_t delay = GetFirstFrameDelay(aRequest);
|
|
|
|
if (delay != 0) {
|
|
|
|
ImageStartData* start = mStartTable.Get(delay);
|
|
|
|
if (start) {
|
|
|
|
start->mEntries.RemoveEntry(aRequest);
|
|
|
|
}
|
|
|
|
}
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
2009-10-08 07:22:42 +04:00
|
|
|
void
|
2015-01-06 04:56:02 +03:00
|
|
|
nsRefreshDriver::EnsureTimerStarted(EnsureTimerStartedFlags aFlags)
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
2012-12-12 02:15:32 +04:00
|
|
|
if (mTestControllingRefreshes)
|
2009-10-08 07:22:42 +04:00
|
|
|
return;
|
2012-10-02 18:54:46 +04:00
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
// will it already fire, and no other changes needed?
|
2016-01-05 06:50:59 +03:00
|
|
|
if (mActiveTimer && !(aFlags & eForceAdjustTimer))
|
2012-12-12 02:15:32 +04:00
|
|
|
return;
|
2012-10-02 18:54:46 +04:00
|
|
|
|
2013-12-20 01:38:35 +04:00
|
|
|
if (IsFrozen() || !mPresContext) {
|
2012-12-12 02:15:32 +04:00
|
|
|
// If we don't want to start it now, or we've been disconnected.
|
|
|
|
StopTimer();
|
2009-10-08 07:22:42 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-01 20:31:54 +04:00
|
|
|
if (mPresContext->Document()->IsBeingUsedAsImage()) {
|
|
|
|
// Image documents receive ticks from clients' refresh drivers.
|
2014-12-04 23:13:37 +03:00
|
|
|
// XXXdholbert Exclude SVG-in-opentype fonts from this optimization, until
|
|
|
|
// they receive refresh-driver ticks from their client docs (bug 1107252).
|
|
|
|
nsIURI* uri = mPresContext->Document()->GetDocumentURI();
|
|
|
|
if (!uri || !IsFontTableURI(uri)) {
|
|
|
|
MOZ_ASSERT(!mActiveTimer,
|
|
|
|
"image doc refresh driver should never have its own timer");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-01 20:31:54 +04:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
// 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);
|
2012-10-08 13:57:12 +04:00
|
|
|
}
|
2012-12-24 04:29:23 +04:00
|
|
|
|
2016-01-05 06:50:59 +03:00
|
|
|
// When switching from an inactive timer to an active timer, the root
|
|
|
|
// refresh driver is skipped due to being set to the content refresh
|
|
|
|
// driver's timestamp. In case of EnsureTimerStarted is called from
|
|
|
|
// ScheduleViewManagerFlush, we should avoid this behavior to flush
|
|
|
|
// a paint in the same tick on the root refresh driver.
|
|
|
|
if (aFlags & eNeverAdjustTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-26 04:17:50 +04:00
|
|
|
// Since the different timers are sampled at different rates, when switching
|
|
|
|
// timers, the most recent refresh of the new timer may be *before* the
|
|
|
|
// most recent refresh of the old timer. However, the refresh driver time
|
|
|
|
// should not go backwards so we clamp the most recent refresh time.
|
2015-01-06 04:56:02 +03:00
|
|
|
//
|
|
|
|
// The one exception to this is when we are restoring the refresh driver
|
|
|
|
// from test control in which case the time is expected to go backwards
|
|
|
|
// (see bug 1043078).
|
2014-06-26 04:17:50 +04:00
|
|
|
mMostRecentRefresh =
|
2015-01-06 04:56:02 +03:00
|
|
|
aFlags & eAllowTimeToGoBackwards
|
|
|
|
? mActiveTimer->MostRecentRefresh()
|
|
|
|
: std::max(mActiveTimer->MostRecentRefresh(), mMostRecentRefresh);
|
2014-06-26 04:17:50 +04:00
|
|
|
mMostRecentRefreshEpochTime =
|
2015-01-06 04:56:02 +03:00
|
|
|
aFlags & eAllowTimeToGoBackwards
|
|
|
|
? mActiveTimer->MostRecentRefreshEpochTime()
|
|
|
|
: std::max(mActiveTimer->MostRecentRefreshEpochTime(),
|
|
|
|
mMostRecentRefreshEpochTime);
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::StopTimer()
|
|
|
|
{
|
2012-12-12 02:15:32 +04:00
|
|
|
if (!mActiveTimer)
|
2009-10-08 07:22:42 +04:00
|
|
|
return;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
mActiveTimer->RemoveRefreshDriver(this);
|
|
|
|
mActiveTimer = nullptr;
|
|
|
|
|
|
|
|
if (mRequestedHighPrecision) {
|
|
|
|
SetHighPrecisionTimersEnabled(false);
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
static void
|
|
|
|
DisableHighPrecisionTimersCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
|
|
|
timeEndPeriod(1);
|
|
|
|
NS_RELEASE(sDisableHighPrecisionTimersTimer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::ConfigureHighPrecision()
|
|
|
|
{
|
2015-04-21 19:44:40 +03:00
|
|
|
bool haveUnthrottledFrameRequestCallbacks =
|
|
|
|
mFrameRequestCallbackDocs.Length() > 0;
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
// if the only change that's needed is that we need high precision,
|
|
|
|
// then just set that
|
2015-04-21 19:44:40 +03:00
|
|
|
if (!mThrottled && !mRequestedHighPrecision &&
|
|
|
|
haveUnthrottledFrameRequestCallbacks) {
|
2012-12-12 02:15:32 +04:00
|
|
|
SetHighPrecisionTimersEnabled(true);
|
2015-04-21 19:44:40 +03:00
|
|
|
} else if (mRequestedHighPrecision && !haveUnthrottledFrameRequestCallbacks) {
|
2012-12-12 02:15:32 +04:00
|
|
|
SetHighPrecisionTimersEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::SetHighPrecisionTimersEnabled(bool aEnable)
|
|
|
|
{
|
|
|
|
LOG("[%p] SetHighPrecisionTimersEnabled (%s)", this, aEnable ? "true" : "false");
|
|
|
|
|
|
|
|
if (aEnable) {
|
|
|
|
NS_ASSERTION(!mRequestedHighPrecision, "SetHighPrecisionTimersEnabled(true) called when already requested!");
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (++sHighPrecisionTimerRequests == 1) {
|
|
|
|
// If we had a timer scheduled to disable it, that means that it's already
|
|
|
|
// enabled; just cancel the timer. Otherwise, really enable it.
|
|
|
|
if (sDisableHighPrecisionTimersTimer) {
|
|
|
|
sDisableHighPrecisionTimersTimer->Cancel();
|
|
|
|
NS_RELEASE(sDisableHighPrecisionTimersTimer);
|
|
|
|
} else {
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mRequestedHighPrecision = true;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mRequestedHighPrecision, "SetHighPrecisionTimersEnabled(false) called when not requested!");
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (--sHighPrecisionTimerRequests == 0) {
|
|
|
|
// Don't jerk us around between high precision and low precision
|
|
|
|
// timers; instead, only allow leaving high precision timers
|
|
|
|
// after 90 seconds. This is arbitrary, but hopefully good
|
|
|
|
// enough.
|
|
|
|
NS_ASSERTION(!sDisableHighPrecisionTimersTimer, "We shouldn't have an outstanding disable-high-precision timer !");
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
2012-12-14 21:47:57 +04:00
|
|
|
if (timer) {
|
|
|
|
timer.forget(&sDisableHighPrecisionTimersTimer);
|
|
|
|
sDisableHighPrecisionTimersTimer->InitWithFuncCallback(DisableHighPrecisionTimersCallback,
|
|
|
|
nullptr,
|
|
|
|
90 * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
} else {
|
|
|
|
// might happen if we're shutting down XPCOM; just drop the time period down
|
|
|
|
// immediately
|
|
|
|
timeEndPeriod(1);
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mRequestedHighPrecision = false;
|
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2009-10-08 07:22:42 +04:00
|
|
|
nsRefreshDriver::ObserverCount() const
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t sum = 0;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(mObservers); ++i) {
|
2009-10-08 07:22:42 +04:00
|
|
|
sum += mObservers[i].Length();
|
|
|
|
}
|
2011-11-10 01:39:16 +04:00
|
|
|
|
2010-08-27 08:26:23 +04:00
|
|
|
// Even while throttled, we need to process layout and style changes. Style
|
|
|
|
// changes can trigger transitions which fire events when they complete, and
|
|
|
|
// layout changes can affect media queries on child documents, triggering
|
|
|
|
// style changes, etc.
|
2010-08-12 01:05:27 +04:00
|
|
|
sum += mStyleFlushObservers.Length();
|
|
|
|
sum += mLayoutFlushObservers.Length();
|
2015-07-02 12:12:31 +03:00
|
|
|
sum += mPendingEvents.Length();
|
2011-11-28 16:48:30 +04:00
|
|
|
sum += mFrameRequestCallbackDocs.Length();
|
2015-04-21 19:44:40 +03:00
|
|
|
sum += mThrottledFrameRequestCallbackDocs.Length();
|
2011-12-24 07:52:22 +04:00
|
|
|
sum += mViewManagerFlushIsPending;
|
2009-10-08 07:22:42 +04:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2011-11-10 01:39:16 +04:00
|
|
|
nsRefreshDriver::ImageRequestCount() const
|
|
|
|
{
|
2013-05-18 00:57:26 +04:00
|
|
|
uint32_t count = 0;
|
2015-10-23 07:16:45 +03:00
|
|
|
for (auto iter = mStartTable.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
count += iter.UserData()->mEntries.Count();
|
|
|
|
}
|
2013-05-18 00:57:26 +04:00
|
|
|
return count + mRequests.Count();
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
2009-10-08 07:22:42 +04:00
|
|
|
nsRefreshDriver::ObserverArray&
|
2017-01-05 10:31:56 +03:00
|
|
|
nsRefreshDriver::ArrayFor(FlushType aFlushType)
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
|
|
|
switch (aFlushType) {
|
2017-01-05 10:31:56 +03:00
|
|
|
case FlushType::Style:
|
2009-10-08 07:22:42 +04:00
|
|
|
return mObservers[0];
|
2017-01-05 10:31:56 +03:00
|
|
|
case FlushType::Layout:
|
2009-10-08 07:22:42 +04:00
|
|
|
return mObservers[1];
|
2017-01-05 10:31:56 +03:00
|
|
|
case FlushType::Display:
|
2009-10-08 07:22:42 +04:00
|
|
|
return mObservers[2];
|
|
|
|
default:
|
2016-12-18 01:25:37 +03:00
|
|
|
MOZ_CRASH("We don't track refresh observers for this flush type");
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsITimerCallback implementation
|
|
|
|
*/
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::DoTick()
|
2009-10-08 07:22:42 +04:00
|
|
|
{
|
2013-12-20 01:38:35 +04:00
|
|
|
NS_PRECONDITION(!IsFrozen(), "Why are we notified while frozen?");
|
2010-02-04 00:17:55 +03:00
|
|
|
NS_PRECONDITION(mPresContext, "Why are we notified after disconnection?");
|
2011-05-05 20:26:32 +04:00
|
|
|
NS_PRECONDITION(!nsContentUtils::GetCurrentJSContext(),
|
|
|
|
"Shouldn't have a JSContext on the stack");
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
if (mTestControllingRefreshes) {
|
|
|
|
Tick(mMostRecentRefreshEpochTime, mMostRecentRefresh);
|
|
|
|
} else {
|
|
|
|
Tick(JS_Now(), TimeStamp::Now());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 19:13:21 +04:00
|
|
|
struct DocumentFrameCallbacks {
|
2014-09-01 07:36:37 +04:00
|
|
|
explicit DocumentFrameCallbacks(nsIDocument* aDocument) :
|
2013-04-25 19:13:21 +04:00
|
|
|
mDocument(aDocument)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
nsIDocument::FrameRequestCallbackList mCallbacks;
|
|
|
|
};
|
|
|
|
|
2014-09-09 22:54:08 +04:00
|
|
|
static nsDocShell* GetDocShell(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
return static_cast<nsDocShell*>(aPresContext->GetDocShell());
|
|
|
|
}
|
|
|
|
|
2014-12-22 03:35:41 +03:00
|
|
|
static bool
|
|
|
|
HasPendingAnimations(nsIPresShell* aShell)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = aShell->GetDocument();
|
|
|
|
if (!doc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
PendingAnimationTracker* tracker = doc->GetPendingAnimationTracker();
|
|
|
|
return tracker && tracker->HasPendingAnimations();
|
2014-12-22 03:35:41 +03:00
|
|
|
}
|
|
|
|
|
2014-09-09 22:54:08 +04:00
|
|
|
/**
|
|
|
|
* Return a list of all the child docShells in a given root docShell that are
|
|
|
|
* visible and are recording markers for the profilingTimeline
|
|
|
|
*/
|
|
|
|
static void GetProfileTimelineSubDocShells(nsDocShell* aRootDocShell,
|
|
|
|
nsTArray<nsDocShell*>& aShells)
|
|
|
|
{
|
2015-10-22 00:10:05 +03:00
|
|
|
if (!aRootDocShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
|
|
|
|
if (!timelines || timelines->IsEmpty()) {
|
2014-09-09 22:54:08 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
2015-10-22 00:10:05 +03:00
|
|
|
nsresult rv = aRootDocShell->GetDocShellEnumerator(
|
|
|
|
nsIDocShellTreeItem::typeAll,
|
|
|
|
nsIDocShell::ENUMERATE_BACKWARDS,
|
|
|
|
getter_AddRefs(enumerator));
|
2014-09-09 22:54:08 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> curItem;
|
|
|
|
bool hasMore = false;
|
|
|
|
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> curSupports;
|
|
|
|
enumerator->GetNext(getter_AddRefs(curSupports));
|
|
|
|
curItem = do_QueryInterface(curSupports);
|
|
|
|
|
|
|
|
if (!curItem || !curItem->GetRecordProfileTimelineMarkers()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDocShell* shell = static_cast<nsDocShell*>(curItem.get());
|
|
|
|
bool isVisible = false;
|
|
|
|
shell->GetVisibility(&isVisible);
|
|
|
|
if (!isVisible) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aShells.AppendElement(shell);
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2014-09-09 22:54:08 +04:00
|
|
|
}
|
|
|
|
|
2015-04-21 19:44:40 +03:00
|
|
|
static void
|
|
|
|
TakeFrameRequestCallbacksFrom(nsIDocument* aDocument,
|
|
|
|
nsTArray<DocumentFrameCallbacks>& aTarget)
|
|
|
|
{
|
|
|
|
aTarget.AppendElement(aDocument);
|
|
|
|
aDocument->TakeFrameRequestCallbacks(aTarget.LastElement().mCallbacks);
|
|
|
|
}
|
|
|
|
|
2015-07-02 12:12:31 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::DispatchPendingEvents()
|
|
|
|
{
|
|
|
|
// Swap out the current pending events
|
|
|
|
nsTArray<PendingEvent> pendingEvents(Move(mPendingEvents));
|
|
|
|
for (PendingEvent& event : pendingEvents) {
|
|
|
|
bool dummy;
|
|
|
|
event.mTarget->DispatchEvent(event.mEvent, &dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
static bool
|
2016-02-10 16:21:26 +03:00
|
|
|
CollectDocuments(nsIDocument* aDocument, void* aDocArray)
|
2015-07-29 04:57:39 +03:00
|
|
|
{
|
2017-02-13 13:22:21 +03:00
|
|
|
static_cast<AutoTArray<nsCOMPtr<nsIDocument>, 32>*>(aDocArray)->
|
|
|
|
AppendElement(aDocument);
|
2016-02-10 16:21:26 +03:00
|
|
|
aDocument->EnumerateSubDocuments(CollectDocuments, aDocArray);
|
2015-07-29 04:57:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::DispatchAnimationEvents()
|
|
|
|
{
|
|
|
|
if (!mPresContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-13 13:22:21 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIDocument>, 32> documents;
|
2016-02-10 16:21:26 +03:00
|
|
|
CollectDocuments(mPresContext->Document(), &documents);
|
|
|
|
|
2017-02-13 13:22:21 +03:00
|
|
|
for (uint32_t i = 0; i < documents.Length(); ++i) {
|
2016-02-10 16:21:26 +03:00
|
|
|
nsIDocument* doc = documents[i];
|
|
|
|
nsIPresShell* shell = doc->GetShell();
|
|
|
|
if (!shell) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsPresContext> context = shell->GetPresContext();
|
|
|
|
if (!context || context->RefreshDriver() != this) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->TransitionManager()->SortEvents();
|
|
|
|
context->AnimationManager()->SortEvents();
|
|
|
|
|
|
|
|
// Dispatch transition events first since transitions conceptually sit
|
|
|
|
// below animations in terms of compositing order.
|
|
|
|
context->TransitionManager()->DispatchEvents();
|
|
|
|
// Check that the presshell has not been destroyed
|
|
|
|
if (context->GetPresShell()) {
|
|
|
|
context->AnimationManager()->DispatchEvents();
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 19:44:40 +03:00
|
|
|
void
|
2015-07-27 02:06:00 +03:00
|
|
|
nsRefreshDriver::RunFrameRequestCallbacks(TimeStamp aNowTime)
|
2015-04-21 19:44:40 +03:00
|
|
|
{
|
|
|
|
// Grab all of our frame request callbacks up front.
|
|
|
|
nsTArray<DocumentFrameCallbacks>
|
|
|
|
frameRequestCallbacks(mFrameRequestCallbackDocs.Length() +
|
|
|
|
mThrottledFrameRequestCallbackDocs.Length());
|
|
|
|
|
|
|
|
// First, grab throttled frame request callbacks.
|
|
|
|
{
|
|
|
|
nsTArray<nsIDocument*> docsToRemove;
|
|
|
|
|
|
|
|
// We always tick throttled frame requests if the entire refresh driver is
|
|
|
|
// throttled, because in that situation throttled frame requests tick at the
|
|
|
|
// same frequency as non-throttled frame requests.
|
|
|
|
bool tickThrottledFrameRequests = mThrottled;
|
|
|
|
|
|
|
|
if (!tickThrottledFrameRequests &&
|
|
|
|
aNowTime >= mNextThrottledFrameRequestTick) {
|
|
|
|
mNextThrottledFrameRequestTick = aNowTime + mThrottledFrameRequestInterval;
|
|
|
|
tickThrottledFrameRequests = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsIDocument* doc : mThrottledFrameRequestCallbackDocs) {
|
|
|
|
if (tickThrottledFrameRequests) {
|
|
|
|
// We're ticking throttled documents, so grab this document's requests.
|
|
|
|
// We don't bother appending to docsToRemove because we're going to
|
|
|
|
// clear mThrottledFrameRequestCallbackDocs anyway.
|
|
|
|
TakeFrameRequestCallbacksFrom(doc, frameRequestCallbacks);
|
|
|
|
} else if (!doc->ShouldThrottleFrameRequests()) {
|
|
|
|
// This document is no longer throttled, so grab its requests even
|
|
|
|
// though we're not ticking throttled frame requests right now. If
|
|
|
|
// this is the first unthrottled document with frame requests, we'll
|
|
|
|
// enter high precision mode the next time the callback is scheduled.
|
|
|
|
TakeFrameRequestCallbacksFrom(doc, frameRequestCallbacks);
|
|
|
|
docsToRemove.AppendElement(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all the documents we're ticking from
|
|
|
|
// mThrottledFrameRequestCallbackDocs so they can be readded as needed.
|
|
|
|
if (tickThrottledFrameRequests) {
|
|
|
|
mThrottledFrameRequestCallbackDocs.Clear();
|
|
|
|
} else {
|
|
|
|
// XXX(seth): We're using this approach to avoid concurrent modification
|
|
|
|
// of mThrottledFrameRequestCallbackDocs. docsToRemove usually has either
|
|
|
|
// zero elements or a very small number, so this should be OK in practice.
|
|
|
|
for (nsIDocument* doc : docsToRemove) {
|
|
|
|
mThrottledFrameRequestCallbackDocs.RemoveElement(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now grab unthrottled frame request callbacks.
|
|
|
|
for (nsIDocument* doc : mFrameRequestCallbackDocs) {
|
|
|
|
TakeFrameRequestCallbacksFrom(doc, frameRequestCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset mFrameRequestCallbackDocs so they can be readded as needed.
|
|
|
|
mFrameRequestCallbackDocs.Clear();
|
|
|
|
|
2015-06-16 22:56:10 +03:00
|
|
|
if (!frameRequestCallbacks.IsEmpty()) {
|
2017-01-07 23:56:49 +03:00
|
|
|
GeckoProfilerTracingRAII tracer("Paint", "Scripts");
|
2015-06-18 00:36:40 +03:00
|
|
|
for (const DocumentFrameCallbacks& docCallbacks : frameRequestCallbacks) {
|
2015-06-16 22:56:10 +03:00
|
|
|
// XXXbz Bug 863140: GetInnerWindow can return the outer
|
|
|
|
// window in some cases.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* innerWindow =
|
|
|
|
docCallbacks.mDocument->GetInnerWindow();
|
2015-06-16 22:56:10 +03:00
|
|
|
DOMHighResTimeStamp timeStamp = 0;
|
|
|
|
if (innerWindow && innerWindow->IsInnerWindow()) {
|
2016-06-09 20:04:42 +03:00
|
|
|
mozilla::dom::Performance* perf = innerWindow->GetPerformance();
|
2015-06-16 22:56:10 +03:00
|
|
|
if (perf) {
|
|
|
|
timeStamp = perf->GetDOMTiming()->TimeStampToDOMHighRes(aNowTime);
|
|
|
|
}
|
|
|
|
// else window is partially torn down already
|
2015-04-21 19:44:40 +03:00
|
|
|
}
|
2015-07-20 17:14:24 +03:00
|
|
|
for (auto& callback : docCallbacks.mCallbacks) {
|
2015-12-01 06:04:09 +03:00
|
|
|
callback->Call(timeStamp);
|
2015-04-21 19:44:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Tick(int64_t aNowEpoch, TimeStamp aNowTime)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!nsContentUtils::GetCurrentJSContext(),
|
|
|
|
"Shouldn't have a JSContext on the stack");
|
|
|
|
|
2013-02-27 15:50:27 +04:00
|
|
|
if (nsNPAPIPluginInstance::InPluginCallUnsafeForReentry()) {
|
2012-12-07 03:58:14 +04:00
|
|
|
NS_ERROR("Refresh driver should not run during plugin call!");
|
|
|
|
// Try to survive this by just ignoring the refresh tick.
|
2012-12-12 02:15:32 +04:00
|
|
|
return;
|
2012-12-07 03:58:14 +04:00
|
|
|
}
|
2010-02-04 00:17:55 +03:00
|
|
|
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsRefreshDriver", "Tick",
|
|
|
|
js::ProfileEntry::Category::GRAPHICS);
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
// We're either frozen or we were disconnected (likely in the middle
|
|
|
|
// of a tick iteration). Just do nothing here, since our
|
|
|
|
// prescontext went away.
|
2013-12-20 01:38:35 +04:00
|
|
|
if (IsFrozen() || !mPresContext) {
|
2012-12-12 02:15:32 +04:00
|
|
|
return;
|
2011-04-12 10:18:43 +04:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:28:34 +03:00
|
|
|
// We can have a race condition where the vsync timestamp
|
|
|
|
// is before the most recent refresh due to a forced refresh.
|
|
|
|
// The underlying assumption is that the refresh driver tick can only
|
|
|
|
// go forward in time, not backwards. To prevent the refresh
|
|
|
|
// driver from going back in time, just skip this tick and
|
|
|
|
// wait until the next tick.
|
|
|
|
if ((aNowTime <= mMostRecentRefresh) && !mTestControllingRefreshes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-18 00:57:26 +04:00
|
|
|
TimeStamp previousRefresh = mMostRecentRefresh;
|
|
|
|
|
2012-12-12 02:15:32 +04:00
|
|
|
mMostRecentRefresh = aNowTime;
|
|
|
|
mMostRecentRefreshEpochTime = aNowEpoch;
|
2009-10-08 07:22:42 +04:00
|
|
|
|
2014-08-15 06:24:50 +04:00
|
|
|
if (IsWaitingForPaint(aNowTime)) {
|
2014-05-29 01:42:17 +04:00
|
|
|
// We're currently suspended waiting for earlier Tick's to
|
|
|
|
// be completed (on the Compositor). Mark that we missed the paint
|
|
|
|
// and keep waiting.
|
|
|
|
return;
|
|
|
|
}
|
2014-08-15 06:24:50 +04:00
|
|
|
mMostRecentTick = aNowTime;
|
2014-05-29 01:43:41 +04:00
|
|
|
if (mRootRefresh) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mRootRefresh->RemoveRefreshObserver(this, FlushType::Style);
|
2014-05-29 01:43:41 +04:00
|
|
|
mRootRefresh = nullptr;
|
|
|
|
}
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = false;
|
2016-09-09 13:06:29 +03:00
|
|
|
mWarningThreshold = 1;
|
2014-05-29 01:42:17 +04:00
|
|
|
|
2009-12-22 00:46:24 +03:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = mPresContext->GetPresShell();
|
2011-11-10 01:39:16 +04:00
|
|
|
if (!presShell || (ObserverCount() == 0 && ImageRequestCount() == 0)) {
|
2009-12-31 22:07:57 +03:00
|
|
|
// Things are being destroyed, or we no longer have any observers.
|
|
|
|
// We don't want to stop the timer when observers are initially
|
|
|
|
// removed, because sometimes observers can be added and removed
|
|
|
|
// often depending on what other things are going on and in that
|
|
|
|
// situation we don't want to thrash our timer. So instead we
|
|
|
|
// wait until we get a Notify() call when we have no observers
|
|
|
|
// before stopping the timer.
|
2009-10-08 07:22:42 +04:00
|
|
|
StopTimer();
|
2012-12-12 02:15:32 +04:00
|
|
|
return;
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
2016-05-03 10:58:57 +03:00
|
|
|
mResizeSuppressed = false;
|
|
|
|
|
2013-09-04 15:47:21 +04:00
|
|
|
AutoRestore<bool> restoreInRefresh(mInRefresh);
|
|
|
|
mInRefresh = true;
|
|
|
|
|
2014-08-28 23:24:26 +04:00
|
|
|
AutoRestore<TimeStamp> restoreTickStart(mTickStart);
|
|
|
|
mTickStart = TimeStamp::Now();
|
|
|
|
|
2016-02-29 09:53:15 +03:00
|
|
|
gfxPlatform::GetPlatform()->SchedulePaintIfDeviceReset();
|
2015-12-17 22:20:08 +03:00
|
|
|
|
2016-03-14 18:50:55 +03:00
|
|
|
// We want to process any pending APZ metrics ahead of their positions
|
|
|
|
// in the queue. This will prevent us from spending precious time
|
|
|
|
// painting a stale displayport.
|
|
|
|
if (gfxPrefs::APZPeekMessages()) {
|
|
|
|
nsLayoutUtils::UpdateDisplayPortMarginsFromPendingMessages();
|
|
|
|
}
|
|
|
|
|
2009-12-22 00:46:25 +03:00
|
|
|
/*
|
|
|
|
* The timer holds a reference to |this| while calling |Notify|.
|
|
|
|
* However, implementations of |WillRefresh| are permitted to destroy
|
|
|
|
* the pres context, which will cause our |mPresContext| to become
|
|
|
|
* null. If this happens, we must stop notifying observers.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(mObservers); ++i) {
|
2009-10-08 07:22:42 +04:00
|
|
|
ObserverArray::EndLimitedIterator etor(mObservers[i]);
|
|
|
|
while (etor.HasMore()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsARefreshObserver> obs = etor.GetNext();
|
2012-12-12 02:15:32 +04:00
|
|
|
obs->WillRefresh(aNowTime);
|
2014-12-08 23:43:16 +03:00
|
|
|
|
2009-12-22 00:46:25 +03:00
|
|
|
if (!mPresContext || !mPresContext->GetPresShell()) {
|
|
|
|
StopTimer();
|
2012-12-12 02:15:32 +04:00
|
|
|
return;
|
2009-12-22 00:46:25 +03:00
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
2011-11-10 01:39:16 +04:00
|
|
|
|
2009-10-08 07:22:42 +04:00
|
|
|
if (i == 0) {
|
2017-01-05 10:31:56 +03:00
|
|
|
// This is the FlushType::Style case.
|
2014-05-29 01:42:16 +04:00
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
DispatchAnimationEvents();
|
2015-07-02 12:12:31 +03:00
|
|
|
DispatchPendingEvents();
|
2015-07-27 02:06:00 +03:00
|
|
|
RunFrameRequestCallbacks(aNowTime);
|
2010-09-09 09:38:04 +04:00
|
|
|
|
2011-01-14 23:03:06 +03:00
|
|
|
if (mPresContext && mPresContext->GetPresShell()) {
|
2017-01-07 23:56:49 +03:00
|
|
|
Maybe<GeckoProfilerTracingRAII> tracingStyleFlush;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsIPresShell*, 16> observers;
|
2011-01-14 23:03:06 +03:00
|
|
|
observers.AppendElements(mStyleFlushObservers);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t j = observers.Length();
|
2011-01-14 23:03:06 +03:00
|
|
|
j && mPresContext && mPresContext->GetPresShell(); --j) {
|
|
|
|
// Make sure to not process observers which might have been removed
|
|
|
|
// during previous iterations.
|
|
|
|
nsIPresShell* shell = observers[j - 1];
|
|
|
|
if (!mStyleFlushObservers.Contains(shell))
|
|
|
|
continue;
|
2014-04-22 22:13:00 +04:00
|
|
|
|
|
|
|
if (!tracingStyleFlush) {
|
2017-01-07 23:56:49 +03:00
|
|
|
tracingStyleFlush.emplace("Paint", "Styles", Move(mStyleCause));
|
2014-04-22 22:13:00 +04:00
|
|
|
mStyleCause = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-22 01:41:26 +03:00
|
|
|
nsCOMPtr<nsIPresShell> shellKungFuDeathGrip(shell);
|
2011-01-14 23:03:06 +03:00
|
|
|
mStyleFlushObservers.RemoveElement(shell);
|
2017-02-13 06:21:33 +03:00
|
|
|
RestyleManager* restyleManager =
|
2016-02-24 10:01:12 +03:00
|
|
|
shell->GetPresContext()->RestyleManager();
|
2016-07-08 10:08:46 +03:00
|
|
|
restyleManager->SetObservingRefreshDriver(false);
|
2017-01-05 10:31:56 +03:00
|
|
|
shell->FlushPendingNotifications(ChangesToFlush(FlushType::Style, false));
|
2014-10-02 06:32:09 +04:00
|
|
|
// Inform the FontFaceSet that we ticked, so that it can resolve its
|
|
|
|
// ready promise if it needs to (though it might still be waiting on
|
|
|
|
// a layout flush).
|
|
|
|
nsPresContext* presContext = shell->GetPresContext();
|
|
|
|
if (presContext) {
|
|
|
|
presContext->NotifyFontFaceSetOnRefresh();
|
|
|
|
}
|
2015-10-21 17:12:00 +03:00
|
|
|
mNeedToRecomputeVisibility = true;
|
2011-01-14 23:03:06 +03:00
|
|
|
}
|
2014-05-29 01:42:16 +04:00
|
|
|
}
|
2010-02-19 20:11:40 +03:00
|
|
|
} else if (i == 1) {
|
2017-01-05 10:31:56 +03:00
|
|
|
// This is the FlushType::Layout case.
|
2017-01-07 23:56:49 +03:00
|
|
|
Maybe<GeckoProfilerTracingRAII> tracingLayoutFlush;
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsIPresShell*, 16> observers;
|
2015-07-13 02:32:04 +03:00
|
|
|
observers.AppendElements(mLayoutFlushObservers);
|
|
|
|
for (uint32_t j = observers.Length();
|
|
|
|
j && mPresContext && mPresContext->GetPresShell(); --j) {
|
|
|
|
// Make sure to not process observers which might have been removed
|
|
|
|
// during previous iterations.
|
|
|
|
nsIPresShell* shell = observers[j - 1];
|
|
|
|
if (!mLayoutFlushObservers.Contains(shell))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!tracingLayoutFlush) {
|
2017-01-07 23:56:49 +03:00
|
|
|
tracingLayoutFlush.emplace("Paint", "Reflow");
|
2015-07-13 02:32:04 +03:00
|
|
|
mReflowCause = nullptr;
|
|
|
|
}
|
2014-04-22 22:13:00 +04:00
|
|
|
|
2016-09-22 01:41:26 +03:00
|
|
|
nsCOMPtr<nsIPresShell> shellKungFuDeathGrip(shell);
|
2015-07-13 02:32:04 +03:00
|
|
|
mLayoutFlushObservers.RemoveElement(shell);
|
|
|
|
shell->mReflowScheduled = false;
|
|
|
|
shell->mSuppressInterruptibleReflows = false;
|
2017-01-05 10:31:56 +03:00
|
|
|
FlushType flushType = HasPendingAnimations(shell)
|
|
|
|
? FlushType::Layout
|
|
|
|
: FlushType::InterruptibleLayout;
|
2015-07-13 02:32:04 +03:00
|
|
|
shell->FlushPendingNotifications(ChangesToFlush(flushType, false));
|
|
|
|
// Inform the FontFaceSet that we ticked, so that it can resolve its
|
|
|
|
// ready promise if it needs to.
|
|
|
|
nsPresContext* presContext = shell->GetPresContext();
|
|
|
|
if (presContext) {
|
|
|
|
presContext->NotifyFontFaceSetOnRefresh();
|
2011-01-14 23:03:06 +03:00
|
|
|
}
|
2015-10-21 17:12:00 +03:00
|
|
|
mNeedToRecomputeVisibility = true;
|
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
2015-07-13 02:32:04 +03:00
|
|
|
|
|
|
|
// The pres context may be destroyed during we do the flushing.
|
|
|
|
if (!mPresContext || !mPresContext->GetPresShell()) {
|
|
|
|
StopTimer();
|
|
|
|
return;
|
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
|
|
|
|
2016-03-26 00:49:43 +03:00
|
|
|
// Recompute approximate frame visibility if it's necessary and enough time
|
|
|
|
// has passed since the last time we did it.
|
2015-05-30 09:50:51 +03:00
|
|
|
if (mNeedToRecomputeVisibility && !mThrottled &&
|
|
|
|
aNowTime >= mNextRecomputeVisibilityTick &&
|
|
|
|
!presShell->IsPaintingSuppressed()) {
|
|
|
|
mNextRecomputeVisibilityTick = aNowTime + mMinRecomputeVisibilityInterval;
|
|
|
|
mNeedToRecomputeVisibility = false;
|
|
|
|
|
2016-03-26 00:49:43 +03:00
|
|
|
presShell->ScheduleApproximateFrameVisibilityUpdateNow();
|
2015-05-30 09:50:51 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 16:53:59 +03:00
|
|
|
#ifdef MOZ_XUL
|
|
|
|
// Update any popups that may need to be moved or hidden due to their
|
|
|
|
// anchor changing.
|
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
|
|
|
if (pm) {
|
|
|
|
pm->UpdatePopupPositions(this);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-28 17:24:00 +03:00
|
|
|
nsCOMArray<nsIDocument> documents;
|
|
|
|
CollectDocuments(mPresContext->Document(), &documents);
|
|
|
|
for (int32_t i = 0; i < documents.Count(); ++i) {
|
|
|
|
nsIDocument* doc = documents[i];
|
|
|
|
doc->UpdateIntersectionObservations();
|
2017-02-08 21:34:02 +03:00
|
|
|
doc->ScheduleIntersectionObserverNotification();
|
2016-10-28 17:24:00 +03:00
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
/*
|
|
|
|
* Perform notification to imgIRequests subscribed to listen
|
|
|
|
* for refresh events.
|
|
|
|
*/
|
|
|
|
|
2015-10-23 07:16:45 +03:00
|
|
|
for (auto iter = mStartTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
const uint32_t& delay = iter.Key();
|
|
|
|
ImageStartData* data = iter.UserData();
|
|
|
|
|
|
|
|
if (data->mStartTime) {
|
|
|
|
TimeStamp& start = *data->mStartTime;
|
|
|
|
TimeDuration prev = previousRefresh - start;
|
|
|
|
TimeDuration curr = aNowTime - start;
|
|
|
|
uint32_t prevMultiple = uint32_t(prev.ToMilliseconds()) / delay;
|
|
|
|
|
|
|
|
// We want to trigger images' refresh if we've just crossed over a
|
|
|
|
// multiple of the first image's start time. If so, set the animation
|
|
|
|
// start time to the nearest multiple of the delay and move all the
|
|
|
|
// images in this table to the main requests table.
|
|
|
|
if (prevMultiple != uint32_t(curr.ToMilliseconds()) / delay) {
|
|
|
|
mozilla::TimeStamp desired =
|
|
|
|
start + TimeDuration::FromMilliseconds(prevMultiple * delay);
|
|
|
|
BeginRefreshingImages(data->mEntries, desired);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is the very first time we've drawn images with this time delay.
|
|
|
|
// Set the animation start time to "now" and move all the images in this
|
|
|
|
// table to the main requests table.
|
|
|
|
mozilla::TimeStamp desired = aNowTime;
|
|
|
|
BeginRefreshingImages(data->mEntries, desired);
|
|
|
|
data->mStartTime.emplace(aNowTime);
|
|
|
|
}
|
|
|
|
}
|
2013-05-18 00:57:26 +04:00
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
if (mRequests.Count()) {
|
2013-09-06 12:48:26 +04:00
|
|
|
// RequestRefresh may run scripts, so it's not safe to directly call it
|
|
|
|
// while using a hashtable enumerator to enumerate mRequests in case
|
|
|
|
// script modifies the hashtable. Instead, we build a (local) array of
|
|
|
|
// images to refresh, and then we refresh each image in that array.
|
|
|
|
nsCOMArray<imgIContainer> imagesToRefresh(mRequests.Count());
|
2015-07-15 12:05:06 +03:00
|
|
|
|
|
|
|
for (auto iter = mRequests.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsISupportsHashKey* entry = iter.Get();
|
|
|
|
auto req = static_cast<imgIRequest*>(entry->GetKey());
|
|
|
|
MOZ_ASSERT(req, "Unable to retrieve the image request");
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (NS_SUCCEEDED(req->GetImage(getter_AddRefs(image)))) {
|
2016-06-21 01:38:10 +03:00
|
|
|
imagesToRefresh.AppendElement(image.forget());
|
2015-07-15 12:05:06 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-06 12:48:26 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < imagesToRefresh.Length(); i++) {
|
|
|
|
imagesToRefresh[i]->RequestRefresh(aNowTime);
|
|
|
|
}
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
2013-05-18 00:57:26 +04:00
|
|
|
|
2016-09-13 00:00:15 +03:00
|
|
|
bool notifyGC = false;
|
2011-12-24 07:52:22 +04:00
|
|
|
if (mViewManagerFlushIsPending) {
|
2015-10-22 00:10:05 +03:00
|
|
|
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
|
|
|
|
|
2014-09-09 22:54:08 +04:00
|
|
|
nsTArray<nsDocShell*> profilingDocShells;
|
|
|
|
GetProfileTimelineSubDocShells(GetDocShell(mPresContext), profilingDocShells);
|
2015-06-18 00:36:40 +03:00
|
|
|
for (nsDocShell* docShell : profilingDocShells) {
|
2014-09-09 22:54:08 +04:00
|
|
|
// For the sake of the profile timeline's simplicity, this is flagged as
|
|
|
|
// paint even if it includes creating display lists
|
2015-10-22 00:10:05 +03:00
|
|
|
MOZ_ASSERT(timelines);
|
|
|
|
MOZ_ASSERT(timelines->HasConsumer(docShell));
|
|
|
|
timelines->AddMarkerForDocShell(docShell, "Paint", MarkerTracingType::START);
|
2014-09-09 22:54:08 +04:00
|
|
|
}
|
2015-10-22 00:10:05 +03:00
|
|
|
|
2013-05-14 03:47:02 +04:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
|
|
|
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
|
2013-11-24 02:44:18 +04:00
|
|
|
printf_stderr("Starting ProcessPendingUpdates\n");
|
2013-05-14 03:47:02 +04:00
|
|
|
}
|
2012-08-13 14:10:10 +04:00
|
|
|
#endif
|
2013-01-04 06:53:15 +04:00
|
|
|
|
2011-12-24 07:52:22 +04:00
|
|
|
mViewManagerFlushIsPending = false;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsViewManager> vm = mPresContext->GetPresShell()->GetViewManager();
|
2016-10-12 10:58:04 +03:00
|
|
|
{
|
|
|
|
PaintTelemetry::AutoRecordPaint record;
|
|
|
|
vm->ProcessPendingUpdates();
|
|
|
|
}
|
2015-10-22 00:10:05 +03:00
|
|
|
|
2013-05-14 03:47:02 +04:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
|
|
|
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
|
2013-11-24 02:44:18 +04:00
|
|
|
printf_stderr("Ending ProcessPendingUpdates\n");
|
2013-05-14 03:47:02 +04:00
|
|
|
}
|
2012-08-13 14:10:10 +04:00
|
|
|
#endif
|
2015-10-22 00:10:05 +03:00
|
|
|
|
2015-06-18 00:36:40 +03:00
|
|
|
for (nsDocShell* docShell : profilingDocShells) {
|
2015-10-22 00:10:05 +03:00
|
|
|
MOZ_ASSERT(timelines);
|
|
|
|
MOZ_ASSERT(timelines->HasConsumer(docShell));
|
|
|
|
timelines->AddMarkerForDocShell(docShell, "Paint", MarkerTracingType::END);
|
2014-09-09 22:54:08 +04:00
|
|
|
}
|
2014-10-22 05:26:17 +04:00
|
|
|
|
2016-09-13 00:00:15 +03:00
|
|
|
notifyGC = true;
|
2011-12-24 07:52:22 +04:00
|
|
|
}
|
2013-08-19 17:08:44 +04:00
|
|
|
|
2015-03-18 09:59:01 +03:00
|
|
|
#ifndef ANDROID /* bug 1142079 */
|
2014-12-08 23:43:16 +03:00
|
|
|
mozilla::Telemetry::AccumulateTimeDelta(mozilla::Telemetry::REFRESH_DRIVER_TICK, mTickStart);
|
2015-03-18 09:59:01 +03:00
|
|
|
#endif
|
2014-12-08 23:43:16 +03:00
|
|
|
|
2015-06-26 23:20:10 +03:00
|
|
|
nsTObserverArray<nsAPostRefreshObserver*>::ForwardIterator iter(mPostRefreshObservers);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
nsAPostRefreshObserver* observer = iter.GetNext();
|
|
|
|
observer->DidRefresh();
|
2013-08-19 17:08:44 +04:00
|
|
|
}
|
2013-09-04 15:47:21 +04:00
|
|
|
|
2015-07-30 22:32:19 +03:00
|
|
|
ConfigureHighPrecision();
|
|
|
|
|
2013-09-04 15:47:21 +04:00
|
|
|
NS_ASSERTION(mInRefresh, "Still in refresh");
|
2016-08-16 18:46:13 +03:00
|
|
|
|
|
|
|
if (mPresContext->IsRoot() && XRE_IsContentProcess() && gfxPrefs::AlwaysPaint()) {
|
|
|
|
ScheduleViewManagerFlush();
|
|
|
|
}
|
2016-09-13 00:00:15 +03:00
|
|
|
|
|
|
|
if (notifyGC && nsContentUtils::XPConnect()) {
|
|
|
|
nsContentUtils::XPConnect()->NotifyDidPaint();
|
|
|
|
nsJSContext::NotifyDidPaint();
|
|
|
|
}
|
2009-10-08 07:22:42 +04:00
|
|
|
}
|
2010-02-04 00:17:55 +03:00
|
|
|
|
2015-07-15 12:05:06 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::BeginRefreshingImages(RequestTable& aEntries,
|
2015-10-23 07:16:45 +03:00
|
|
|
mozilla::TimeStamp aDesired)
|
2013-05-18 00:57:26 +04:00
|
|
|
{
|
2015-07-15 12:05:06 +03:00
|
|
|
for (auto iter = aEntries.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto req = static_cast<imgIRequest*>(iter.Get()->GetKey());
|
|
|
|
MOZ_ASSERT(req, "Unable to retrieve the image request");
|
2013-05-18 00:57:26 +04:00
|
|
|
|
2015-10-23 07:16:45 +03:00
|
|
|
mRequests.PutEntry(req);
|
2013-05-18 00:57:26 +04:00
|
|
|
|
2015-07-15 12:05:06 +03:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (NS_SUCCEEDED(req->GetImage(getter_AddRefs(image)))) {
|
2015-10-23 07:16:45 +03:00
|
|
|
image->SetAnimationStartTime(aDesired);
|
2015-07-15 12:05:06 +03:00
|
|
|
}
|
2013-05-18 00:57:26 +04:00
|
|
|
}
|
2015-07-15 12:05:06 +03:00
|
|
|
aEntries.Clear();
|
2013-05-18 00:57:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-04 00:17:55 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Freeze()
|
|
|
|
{
|
|
|
|
StopTimer();
|
2013-12-20 01:38:35 +04:00
|
|
|
mFreezeCount++;
|
2010-02-04 00:17:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::Thaw()
|
|
|
|
{
|
2013-12-20 01:38:35 +04:00
|
|
|
NS_ASSERTION(mFreezeCount > 0, "Thaw() called on an unfrozen refresh driver");
|
|
|
|
|
|
|
|
if (mFreezeCount > 0) {
|
|
|
|
mFreezeCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFreezeCount == 0) {
|
|
|
|
if (ObserverCount() || ImageRequestCount()) {
|
|
|
|
// FIXME: This isn't quite right, since our EnsureTimerStarted call
|
|
|
|
// updates our mMostRecentRefresh, but the DoRefresh call won't run
|
|
|
|
// and notify our observers until we get back to the event loop.
|
|
|
|
// Thus MostRecentRefresh() will lie between now and the DoRefresh.
|
2016-05-05 11:45:00 +03:00
|
|
|
NS_DispatchToCurrentThread(NewRunnableMethod(this, &nsRefreshDriver::DoRefresh));
|
2015-01-06 04:56:02 +03:00
|
|
|
EnsureTimerStarted();
|
2013-12-20 01:38:35 +04:00
|
|
|
}
|
2010-02-04 00:17:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 01:42:17 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::FinishedWaitingForTransaction()
|
|
|
|
{
|
|
|
|
mWaitingForTransaction = false;
|
2015-11-14 00:47:34 +03:00
|
|
|
if (mSkippedPaints &&
|
|
|
|
!IsInRefresh() &&
|
|
|
|
(ObserverCount() || ImageRequestCount())) {
|
2017-01-07 23:56:49 +03:00
|
|
|
GeckoProfilerTracingRAII tracer("Paint", "RD");
|
2015-11-14 00:47:34 +03:00
|
|
|
DoRefresh();
|
|
|
|
}
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = false;
|
2016-09-09 13:06:29 +03:00
|
|
|
mWarningThreshold = 1;
|
2014-05-29 01:42:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
nsRefreshDriver::GetTransactionId()
|
|
|
|
{
|
|
|
|
++mPendingTransaction;
|
|
|
|
|
2014-08-15 06:24:50 +04:00
|
|
|
if (mPendingTransaction >= mCompletedTransaction + 2 &&
|
2014-05-29 01:42:17 +04:00
|
|
|
!mWaitingForTransaction &&
|
|
|
|
!mTestControllingRefreshes) {
|
|
|
|
mWaitingForTransaction = true;
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = false;
|
2016-09-09 13:06:29 +03:00
|
|
|
mWarningThreshold = 1;
|
2014-05-29 01:42:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mPendingTransaction;
|
|
|
|
}
|
|
|
|
|
2016-04-13 23:59:15 +03:00
|
|
|
uint64_t
|
|
|
|
nsRefreshDriver::LastTransactionId() const
|
|
|
|
{
|
|
|
|
return mPendingTransaction;
|
|
|
|
}
|
|
|
|
|
2014-05-29 01:42:17 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::RevokeTransactionId(uint64_t aTransactionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTransactionId == mPendingTransaction);
|
|
|
|
if (mPendingTransaction == mCompletedTransaction + 2 &&
|
|
|
|
mWaitingForTransaction) {
|
2014-05-29 01:45:06 +04:00
|
|
|
MOZ_ASSERT(!mSkippedPaints, "How did we skip a paint when we're in the middle of one?");
|
2014-05-29 01:42:17 +04:00
|
|
|
FinishedWaitingForTransaction();
|
|
|
|
}
|
|
|
|
mPendingTransaction--;
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:24:26 +04:00
|
|
|
mozilla::TimeStamp
|
|
|
|
nsRefreshDriver::GetTransactionStart()
|
|
|
|
{
|
|
|
|
return mTickStart;
|
|
|
|
}
|
|
|
|
|
2014-05-29 01:42:17 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::NotifyTransactionCompleted(uint64_t aTransactionId)
|
|
|
|
{
|
|
|
|
if (aTransactionId > mCompletedTransaction) {
|
|
|
|
if (mPendingTransaction > mCompletedTransaction + 1 &&
|
|
|
|
mWaitingForTransaction) {
|
|
|
|
mCompletedTransaction = aTransactionId;
|
|
|
|
FinishedWaitingForTransaction();
|
|
|
|
} else {
|
|
|
|
mCompletedTransaction = aTransactionId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 01:43:41 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::WillRefresh(mozilla::TimeStamp aTime)
|
|
|
|
{
|
2017-01-05 10:31:56 +03:00
|
|
|
mRootRefresh->RemoveRefreshObserver(this, FlushType::Style);
|
2014-05-29 01:43:41 +04:00
|
|
|
mRootRefresh = nullptr;
|
2014-05-29 01:45:06 +04:00
|
|
|
if (mSkippedPaints) {
|
2014-05-29 01:43:41 +04:00
|
|
|
DoRefresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-15 06:24:50 +04:00
|
|
|
nsRefreshDriver::IsWaitingForPaint(mozilla::TimeStamp aTime)
|
2014-05-29 01:43:41 +04:00
|
|
|
{
|
|
|
|
if (mTestControllingRefreshes) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 13:06:29 +03:00
|
|
|
|
2014-05-29 01:43:41 +04:00
|
|
|
if (mWaitingForTransaction) {
|
2016-09-15 23:48:39 +03:00
|
|
|
if (mSkippedPaints && aTime > (mMostRecentTick + TimeDuration::FromMilliseconds(mWarningThreshold * 1000))) {
|
2016-09-20 17:25:52 +03:00
|
|
|
// XXX - Bug 1303369 - too many false positives.
|
|
|
|
//gfxCriticalNote << "Refresh driver waiting for the compositor for "
|
|
|
|
// << (aTime - mMostRecentTick).ToSeconds()
|
|
|
|
// << " seconds.";
|
2016-09-15 23:48:39 +03:00
|
|
|
mWarningThreshold *= 2;
|
|
|
|
}
|
|
|
|
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = true;
|
2014-05-29 01:43:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try find the 'root' refresh driver for the current window and check
|
|
|
|
// if that is waiting for a paint.
|
2016-10-12 14:27:38 +03:00
|
|
|
nsPresContext* pc = GetPresContext();
|
|
|
|
nsPresContext* rootContext = pc ? pc->GetRootPresContext() : nullptr;
|
2016-01-22 04:09:04 +03:00
|
|
|
if (rootContext) {
|
|
|
|
nsRefreshDriver *rootRefresh = rootContext->RefreshDriver();
|
2014-05-29 01:43:41 +04:00
|
|
|
if (rootRefresh && rootRefresh != this) {
|
2014-08-15 06:24:50 +04:00
|
|
|
if (rootRefresh->IsWaitingForPaint(aTime)) {
|
2014-05-29 01:43:41 +04:00
|
|
|
if (mRootRefresh != rootRefresh) {
|
|
|
|
if (mRootRefresh) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mRootRefresh->RemoveRefreshObserver(this, FlushType::Style);
|
2014-05-29 01:43:41 +04:00
|
|
|
}
|
2017-01-05 10:31:56 +03:00
|
|
|
rootRefresh->AddRefreshObserver(this, FlushType::Style);
|
2014-05-29 01:43:41 +04:00
|
|
|
mRootRefresh = rootRefresh;
|
|
|
|
}
|
2014-08-15 06:24:50 +04:00
|
|
|
mSkippedPaints = true;
|
2014-05-29 01:43:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-27 08:26:23 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::SetThrottled(bool aThrottled)
|
|
|
|
{
|
|
|
|
if (aThrottled != mThrottled) {
|
|
|
|
mThrottled = aThrottled;
|
2012-12-12 02:15:32 +04:00
|
|
|
if (mActiveTimer) {
|
2011-03-04 18:27:02 +03:00
|
|
|
// We want to switch our timer type here, so just stop and
|
|
|
|
// restart the timer.
|
2016-01-05 06:50:59 +03:00
|
|
|
EnsureTimerStarted(eForceAdjustTimer);
|
2010-08-27 08:26:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:38:00 +03:00
|
|
|
/*static*/ void
|
|
|
|
nsRefreshDriver::PVsyncActorCreated(VsyncChild* aVsyncChild)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!XRE_IsParentProcess());
|
2016-11-09 16:34:49 +03:00
|
|
|
auto* vsyncRefreshDriverTimer =
|
|
|
|
new VsyncRefreshDriverTimer(aVsyncChild);
|
2015-01-14 02:38:00 +03:00
|
|
|
|
|
|
|
// If we are using software timer, swap current timer to
|
|
|
|
// VsyncRefreshDriverTimer.
|
|
|
|
if (sRegularRateTimer) {
|
|
|
|
sRegularRateTimer->SwapRefreshDrivers(vsyncRefreshDriverTimer);
|
|
|
|
delete sRegularRateTimer;
|
|
|
|
}
|
|
|
|
sRegularRateTimer = vsyncRefreshDriverTimer;
|
|
|
|
}
|
|
|
|
|
2010-02-04 00:17:55 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::DoRefresh()
|
|
|
|
{
|
|
|
|
// Don't do a refresh unless we're in a state where we should be refreshing.
|
2013-12-20 01:38:35 +04:00
|
|
|
if (!IsFrozen() && mPresContext && mActiveTimer) {
|
2012-12-12 02:15:32 +04:00
|
|
|
DoTick();
|
2010-02-04 00:17:55 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-19 20:11:40 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-12 02:15:32 +04:00
|
|
|
nsRefreshDriver::IsRefreshObserver(nsARefreshObserver* aObserver,
|
2017-01-05 10:31:56 +03:00
|
|
|
FlushType aFlushType)
|
2010-02-19 20:11:40 +03:00
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
|
|
|
return array.Contains(aObserver);
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-12 01:05:28 +04:00
|
|
|
|
2012-09-17 09:06:12 +04:00
|
|
|
void
|
|
|
|
nsRefreshDriver::ScheduleViewManagerFlush()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPresContext->IsRoot(),
|
|
|
|
"Should only schedule view manager flush on root prescontexts");
|
|
|
|
mViewManagerFlushIsPending = true;
|
2016-01-05 06:50:59 +03:00
|
|
|
EnsureTimerStarted(eNeverAdjustTimer);
|
2012-09-17 09:06:12 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 09:38:04 +04:00
|
|
|
void
|
2011-11-28 16:48:30 +04:00
|
|
|
nsRefreshDriver::ScheduleFrameRequestCallbacks(nsIDocument* aDocument)
|
2010-09-09 09:38:04 +04:00
|
|
|
{
|
2011-11-28 16:48:30 +04:00
|
|
|
NS_ASSERTION(mFrameRequestCallbackDocs.IndexOf(aDocument) ==
|
2015-04-21 19:44:40 +03:00
|
|
|
mFrameRequestCallbackDocs.NoIndex &&
|
|
|
|
mThrottledFrameRequestCallbackDocs.IndexOf(aDocument) ==
|
|
|
|
mThrottledFrameRequestCallbackDocs.NoIndex,
|
2010-09-09 09:38:04 +04:00
|
|
|
"Don't schedule the same document multiple times");
|
2015-04-21 19:44:40 +03:00
|
|
|
if (aDocument->ShouldThrottleFrameRequests()) {
|
|
|
|
mThrottledFrameRequestCallbackDocs.AppendElement(aDocument);
|
|
|
|
} else {
|
|
|
|
mFrameRequestCallbackDocs.AppendElement(aDocument);
|
|
|
|
}
|
2012-12-12 02:15:32 +04:00
|
|
|
|
|
|
|
// make sure that the timer is running
|
|
|
|
ConfigureHighPrecision();
|
2015-01-06 04:56:02 +03:00
|
|
|
EnsureTimerStarted();
|
2010-09-09 09:38:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-28 16:48:30 +04:00
|
|
|
nsRefreshDriver::RevokeFrameRequestCallbacks(nsIDocument* aDocument)
|
2010-09-09 09:38:04 +04:00
|
|
|
{
|
2011-11-28 16:48:30 +04:00
|
|
|
mFrameRequestCallbackDocs.RemoveElement(aDocument);
|
2015-04-21 19:44:40 +03:00
|
|
|
mThrottledFrameRequestCallbackDocs.RemoveElement(aDocument);
|
2012-12-12 02:15:32 +04:00
|
|
|
ConfigureHighPrecision();
|
2011-03-07 19:58:48 +03:00
|
|
|
// No need to worry about restarting our timer in slack mode if it's already
|
|
|
|
// running; that will happen automatically when it fires.
|
2010-09-09 09:38:04 +04:00
|
|
|
}
|
2013-11-15 06:43:56 +04:00
|
|
|
|
2015-07-02 12:12:31 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::ScheduleEventDispatch(nsINode* aTarget, nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
mPendingEvents.AppendElement(PendingEvent{aTarget, aEvent});
|
|
|
|
// make sure that the timer is running
|
|
|
|
EnsureTimerStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::CancelPendingEvents(nsIDocument* aDocument)
|
|
|
|
{
|
2017-01-06 17:22:53 +03:00
|
|
|
for (auto i : Reversed(IntegerRange(mPendingEvents.Length()))) {
|
2015-07-02 12:12:31 +03:00
|
|
|
if (mPendingEvents[i].mTarget->OwnerDoc() == aDocument) {
|
|
|
|
mPendingEvents.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 15:52:45 +03:00
|
|
|
/* static */ Maybe<TimeStamp>
|
|
|
|
nsRefreshDriver::GetIdleDeadlineHint()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sRegularRateTimer) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For computing idleness of refresh drivers we only care about
|
|
|
|
// sRegularRateTimer, since we consider refresh drivers attached to
|
|
|
|
// sThrottledRateTimer to be inactive. This implies that tasks
|
|
|
|
// resulting from a tick on the sRegularRateTimer counts as being
|
|
|
|
// busy but tasks resulting from a tick on sThrottledRateTimer
|
|
|
|
// counts as being idle.
|
|
|
|
return sRegularRateTimer->GetIdleDeadlineHint();
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:51:15 +03:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Disconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
StopTimer();
|
|
|
|
|
|
|
|
if (mPresContext) {
|
|
|
|
mPresContext = nullptr;
|
|
|
|
if (--sRefreshDriverCount == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:35:46 +03:00
|
|
|
/* static */ bool
|
|
|
|
nsRefreshDriver::IsJankCritical()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return sActiveVsyncTimers > 0;
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:07:18 +03:00
|
|
|
/* static */ bool
|
|
|
|
nsRefreshDriver::GetJankLevels(Vector<uint64_t>& aJank) {
|
|
|
|
aJank.clear();
|
|
|
|
return aJank.append(sJankLevels, ArrayLength(sJankLevels));
|
|
|
|
}
|
|
|
|
|
2013-11-15 06:43:56 +04:00
|
|
|
#undef LOG
|