2010-02-03 21:17:55 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2009-10-08 03:22:42 +00:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 11:12:37 +00: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 03:22:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Code to notify things that animate before a refresh, at an appropriate
|
|
|
|
* refresh rate. (Perhaps temporary, until replaced by compositor.)
|
2012-12-11 22:15:32 +00: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 03:22:42 +00:00
|
|
|
*/
|
|
|
|
|
2012-12-11 22:15:32 +00: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 04:25:58 +00:00
|
|
|
#include "WinUtils.h"
|
2012-12-11 22:15:32 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-09-04 11:47:21 +00:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2009-10-08 03:22:42 +00:00
|
|
|
#include "nsRefreshDriver.h"
|
2012-12-11 22:15:32 +00:00
|
|
|
#include "nsITimer.h"
|
2013-05-13 23:47:02 +00:00
|
|
|
#include "nsLayoutUtils.h"
|
2009-10-08 03:22:42 +00:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "prlog.h"
|
2009-12-21 21:46:25 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2010-08-11 21:05:28 +00:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "jsapi.h"
|
2010-08-23 09:30:08 +00:00
|
|
|
#include "nsContentUtils.h"
|
2011-05-25 06:32:00 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-01-05 03:12:24 +00:00
|
|
|
#include "nsViewManager.h"
|
2013-03-18 14:25:50 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2012-12-06 23:58:14 +00:00
|
|
|
#include "nsNPAPIPluginInstance.h"
|
2013-04-25 15:13:21 +00:00
|
|
|
#include "nsPerformance.h"
|
|
|
|
#include "mozilla/dom/WindowBinding.h"
|
2013-07-20 19:14:25 +00:00
|
|
|
#include "RestyleManager.h"
|
2013-08-14 11:33:03 +00:00
|
|
|
#include "Layers.h"
|
2013-10-01 21:00:38 +00:00
|
|
|
#include "imgIContainer.h"
|
2013-10-08 02:21:07 +00:00
|
|
|
#include "nsIFrameRequestCallback.h"
|
2013-12-12 01:51:58 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2011-05-25 06:32:00 +00:00
|
|
|
using namespace mozilla;
|
2013-08-15 04:25:58 +00:00
|
|
|
using namespace mozilla::widget;
|
2011-05-25 06:32:00 +00:00
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo *gLog = nullptr;
|
|
|
|
#define LOG(...) PR_LOG(gLog, PR_LOG_NOTICE, (__VA_ARGS__))
|
|
|
|
#else
|
|
|
|
#define LOG(...) do { } while(0)
|
|
|
|
#endif
|
|
|
|
|
2010-08-23 09:30:08 +00:00
|
|
|
#define DEFAULT_FRAME_RATE 60
|
2010-08-27 04:26:23 +00:00
|
|
|
#define DEFAULT_THROTTLED_FRAME_RATE 1
|
2012-12-11 22:15:32 +00:00
|
|
|
// after 10 minutes, stop firing off inactive timers
|
|
|
|
#define DEFAULT_INACTIVE_TIMER_DISABLE_SECONDS 600
|
|
|
|
|
|
|
|
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:
|
|
|
|
/*
|
|
|
|
* aRate -- the delay, in milliseconds, requested between timer firings
|
|
|
|
*/
|
|
|
|
RefreshDriverTimer(double aRate)
|
|
|
|
{
|
|
|
|
SetRate(aRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~RefreshDriverTimer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRefreshDrivers.Length() == 0, "Should have removed all refresh drivers from here by now!");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void AddRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
|
|
|
LOG("[%p] AddRefreshDriver %p", this, aDriver);
|
|
|
|
|
|
|
|
NS_ASSERTION(!mRefreshDrivers.Contains(aDriver), "AddRefreshDriver for a refresh driver that's already in the list!");
|
|
|
|
mRefreshDrivers.AppendElement(aDriver);
|
|
|
|
|
|
|
|
if (mRefreshDrivers.Length() == 1) {
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void RemoveRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
|
|
|
LOG("[%p] RemoveRefreshDriver %p", this, aDriver);
|
|
|
|
|
|
|
|
NS_ASSERTION(mRefreshDrivers.Contains(aDriver), "RemoveRefreshDriver for a refresh driver that's not in the list!");
|
|
|
|
mRefreshDrivers.RemoveElement(aDriver);
|
|
|
|
|
|
|
|
if (mRefreshDrivers.Length() == 0) {
|
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetRate() const
|
|
|
|
{
|
|
|
|
return mRateMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
// will take effect at next timer tick
|
|
|
|
virtual void SetRate(double aNewRate)
|
|
|
|
{
|
|
|
|
mRateMilliseconds = aNewRate;
|
|
|
|
mRateDuration = TimeDuration::FromMilliseconds(mRateMilliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp MostRecentRefresh() const { return mLastFireTime; }
|
|
|
|
int64_t MostRecentRefreshEpochTime() const { return mLastFireEpoch; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void StartTimer() = 0;
|
|
|
|
virtual void StopTimer() = 0;
|
|
|
|
virtual void ScheduleNextTick(TimeStamp aNowTime) = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
ScheduleNextTick(now);
|
|
|
|
|
|
|
|
mLastFireEpoch = jsnow;
|
|
|
|
mLastFireTime = now;
|
|
|
|
|
|
|
|
LOG("[%p] ticking drivers...", this);
|
|
|
|
nsTArray<nsRefPtr<nsRefreshDriver> > drivers(mRefreshDrivers);
|
|
|
|
for (size_t i = 0; i < drivers.Length(); ++i) {
|
|
|
|
// don't poke this driver if it's in test mode
|
|
|
|
if (drivers[i]->IsTestControllingRefreshesEnabled()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TickDriver(drivers[i], jsnow, now);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
double mRateMilliseconds;
|
|
|
|
TimeDuration mRateDuration;
|
|
|
|
|
|
|
|
int64_t mLastFireEpoch;
|
|
|
|
TimeStamp mLastFireTime;
|
|
|
|
TimeStamp mTargetTime;
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<nsRefreshDriver> > mRefreshDrivers;
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
SimpleTimerBasedRefreshDriverTimer(double aRate)
|
|
|
|
: RefreshDriverTimer(aRate)
|
|
|
|
{
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SimpleTimerBasedRefreshDriverTimer()
|
|
|
|
{
|
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void StartTimer()
|
|
|
|
{
|
2012-12-24 00:29:23 +00:00
|
|
|
// pretend we just fired, and we schedule the next tick normally
|
|
|
|
mLastFireEpoch = JS_Now();
|
2012-12-11 22:15:32 +00:00
|
|
|
mLastFireTime = TimeStamp::Now();
|
|
|
|
|
2012-12-24 00:29:23 +00:00
|
|
|
mTargetTime = mLastFireTime + mRateDuration;
|
|
|
|
|
|
|
|
uint32_t delay = static_cast<uint32_t>(mRateMilliseconds);
|
|
|
|
mTimer->InitWithFuncCallback(TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT);
|
2012-12-11 22:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StopTimer()
|
|
|
|
{
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsITimer> mTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PreciseRefreshDriverTimer schedules ticks based on the current time
|
|
|
|
* and when the next tick -should- be sent if we were hitting our
|
|
|
|
* rate. It always schedules ticks on multiples of aRate -- meaning that
|
|
|
|
* if some execution takes longer than an alloted slot, the next tick
|
|
|
|
* will be delayed instead of triggering instantly. This might not be
|
|
|
|
* desired -- there's an #if 0'd block below that we could put behind
|
|
|
|
* a pref to control this behaviour.
|
|
|
|
*/
|
|
|
|
class PreciseRefreshDriverTimer :
|
|
|
|
public SimpleTimerBasedRefreshDriverTimer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PreciseRefreshDriverTimer(double aRate)
|
|
|
|
: SimpleTimerBasedRefreshDriverTimer(aRate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void ScheduleNextTick(TimeStamp aNowTime)
|
|
|
|
{
|
|
|
|
// The number of (whole) elapsed intervals between the last target
|
|
|
|
// time and the actual time. We want to truncate the double down
|
|
|
|
// to an int number of intervals.
|
|
|
|
int numElapsedIntervals = static_cast<int>((aNowTime - mTargetTime) / mRateDuration);
|
|
|
|
|
2013-02-20 19:07:51 +00:00
|
|
|
if (numElapsedIntervals < 0) {
|
|
|
|
// It's possible that numElapsedIntervals is negative (e.g. timer compensation
|
|
|
|
// may result in (aNowTime - mTargetTime) < -1.0/mRateDuration, which will result in
|
|
|
|
// negative numElapsedIntervals), so make sure we don't target the same timestamp.
|
|
|
|
numElapsedIntervals = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
// the last "tick" that may or may not have been actually sent was
|
|
|
|
// at this time. For example, if the rate is 15ms, the target
|
|
|
|
// time is 200ms, and it's now 225ms, the last effective tick
|
|
|
|
// would have been at 215ms. The next one should then be
|
|
|
|
// scheduled for 5 ms from now.
|
|
|
|
//
|
|
|
|
// We then add another mRateDuration to find the next tick target.
|
|
|
|
TimeStamp newTarget = mTargetTime + mRateDuration * (numElapsedIntervals + 1);
|
|
|
|
|
|
|
|
// the amount of (integer) ms until the next time we should tick
|
|
|
|
uint32_t delay = static_cast<uint32_t>((newTarget - aNowTime).ToMilliseconds());
|
|
|
|
|
|
|
|
// Without this block, we'll always schedule on interval ticks;
|
|
|
|
// with it, we'll schedule immediately if we missed our tick target
|
|
|
|
// last time.
|
|
|
|
#if 0
|
|
|
|
if (numElapsedIntervals > 0) {
|
|
|
|
// we're late, so reset
|
|
|
|
newTarget = aNowTime;
|
|
|
|
delay = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// log info & lateness
|
|
|
|
LOG("[%p] precise timer last tick late by %f ms, next tick in %d ms",
|
|
|
|
this,
|
|
|
|
(aNowTime - mTargetTime).ToMilliseconds(),
|
|
|
|
delay);
|
|
|
|
|
|
|
|
// then schedule the timer
|
2012-12-24 00:29:23 +00:00
|
|
|
LOG("[%p] scheduling callback for %d ms (2)", this, delay);
|
2012-12-11 22:15:32 +00:00
|
|
|
mTimer->InitWithFuncCallback(TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
mTargetTime = newTarget;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-29 10:31:16 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
/*
|
|
|
|
* Uses vsync timing on windows with DWM. Falls back dynamically to fixed rate if required.
|
|
|
|
*/
|
|
|
|
class PreciseRefreshDriverTimerWindowsDwmVsync :
|
|
|
|
public PreciseRefreshDriverTimer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Checks if the vsync API is accessible.
|
|
|
|
static bool IsSupported()
|
|
|
|
{
|
2013-08-15 04:25:58 +00:00
|
|
|
return WinUtils::dwmGetCompositionTimingInfoPtr != nullptr;
|
2013-05-29 10:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PreciseRefreshDriverTimerWindowsDwmVsync(double aRate, bool aPreferHwTiming = false)
|
|
|
|
: PreciseRefreshDriverTimer(aRate)
|
|
|
|
, mPreferHwTiming(aPreferHwTiming)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Indicates we should try to adjust to the HW's timing (get rate from the OS or use vsync)
|
|
|
|
// This is typically true if the default refresh-rate value was not modified by the user.
|
|
|
|
bool mPreferHwTiming;
|
|
|
|
|
|
|
|
nsresult GetVBlankInfo(mozilla::TimeStamp &aLastVBlank, mozilla::TimeDuration &aInterval)
|
|
|
|
{
|
2013-08-15 04:25:58 +00:00
|
|
|
MOZ_ASSERT(WinUtils::dwmGetCompositionTimingInfoPtr,
|
|
|
|
"DwmGetCompositionTimingInfoPtr is unavailable (windows vsync)");
|
2013-05-29 10:31:16 +00:00
|
|
|
|
|
|
|
DWM_TIMING_INFO timingInfo;
|
|
|
|
timingInfo.cbSize = sizeof(DWM_TIMING_INFO);
|
2013-08-15 04:25:58 +00:00
|
|
|
HRESULT hr = WinUtils::dwmGetCompositionTimingInfoPtr(0, &timingInfo); // For the desktop window instead of a specific one.
|
2013-05-29 10:31:16 +00:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
// This happens first time this is called.
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER time, freq;
|
|
|
|
::QueryPerformanceCounter(&time);
|
|
|
|
::QueryPerformanceFrequency(&freq);
|
|
|
|
aLastVBlank = TimeStamp::Now();
|
|
|
|
double secondsPassed = double(time.QuadPart - timingInfo.qpcVBlank) / double(freq.QuadPart);
|
|
|
|
|
|
|
|
aLastVBlank -= TimeDuration::FromSeconds(secondsPassed);
|
|
|
|
aInterval = TimeDuration::FromSeconds(double(timingInfo.qpcRefreshPeriod) / double(freq.QuadPart));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ScheduleNextTick(TimeStamp aNowTime)
|
|
|
|
{
|
|
|
|
static const TimeDuration kMinSaneInterval = TimeDuration::FromMilliseconds(3); // 330Hz
|
|
|
|
static const TimeDuration kMaxSaneInterval = TimeDuration::FromMilliseconds(44); // 23Hz
|
|
|
|
static const TimeDuration kNegativeMaxSaneInterval = TimeDuration::FromMilliseconds(-44); // Saves conversions for abs interval
|
|
|
|
TimeStamp lastVblank;
|
|
|
|
TimeDuration vblankInterval;
|
|
|
|
|
|
|
|
if (!mPreferHwTiming ||
|
|
|
|
NS_OK != GetVBlankInfo(lastVblank, vblankInterval) ||
|
|
|
|
vblankInterval > kMaxSaneInterval ||
|
|
|
|
vblankInterval < kMinSaneInterval ||
|
|
|
|
(aNowTime - lastVblank) > kMaxSaneInterval ||
|
|
|
|
(aNowTime - lastVblank) < kNegativeMaxSaneInterval) {
|
|
|
|
// Use the default timing without vsync
|
|
|
|
PreciseRefreshDriverTimer::ScheduleNextTick(aNowTime);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp newTarget = lastVblank + vblankInterval; // Base target
|
|
|
|
|
|
|
|
// However, timer callback might return early (or late, but that wouldn't bother us), and vblankInterval
|
|
|
|
// appears to be slightly (~1%) different on each call (probably the OS measuring recent actual interval[s])
|
|
|
|
// and since we don't want to re-target the same vsync, we keep advancing in vblank intervals until we find the
|
|
|
|
// next safe target (next vsync, but not within 10% interval of previous target).
|
|
|
|
// This is typically 0 or 1 iteration:
|
|
|
|
// If we're too early, next vsync would be the one we've already targeted (1 iteration).
|
|
|
|
// If the timer returned late, no iteration will be required.
|
|
|
|
|
|
|
|
const double kSameVsyncThreshold = 0.1;
|
|
|
|
while (newTarget <= mTargetTime + vblankInterval.MultDouble(kSameVsyncThreshold)) {
|
|
|
|
newTarget += vblankInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To make sure we always hit the same "side" of the signal:
|
|
|
|
// round the delay up (by adding 1, since we later floor) and add a little (10% by default).
|
|
|
|
// Note that newTarget doesn't change (and is the next vblank) as a reference when we're back.
|
|
|
|
static const double kDefaultPhaseShiftPercent = 10;
|
|
|
|
static const double phaseShiftFactor = 0.01 *
|
|
|
|
(Preferences::GetInt("layout.frame_rate.vsync.phasePercentage", kDefaultPhaseShiftPercent) % 100);
|
|
|
|
|
|
|
|
double phaseDelay = 1.0 + vblankInterval.ToMilliseconds() * phaseShiftFactor;
|
|
|
|
|
|
|
|
// ms until the next time we should tick
|
|
|
|
double delayMs = (newTarget - aNowTime).ToMilliseconds() + phaseDelay;
|
|
|
|
|
|
|
|
// Make sure the delay is never negative.
|
|
|
|
uint32_t delay = static_cast<uint32_t>(delayMs < 0 ? 0 : delayMs);
|
|
|
|
|
|
|
|
// log info & lateness
|
|
|
|
LOG("[%p] precise dwm-vsync timer last tick late by %f ms, next tick in %d ms",
|
|
|
|
this,
|
|
|
|
(aNowTime - mTargetTime).ToMilliseconds(),
|
|
|
|
delay);
|
|
|
|
|
|
|
|
// then schedule the timer
|
|
|
|
LOG("[%p] scheduling callback for %d ms (2)", this, delay);
|
|
|
|
mTimer->InitWithFuncCallback(TimerTick, this, delay, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
mTargetTime = newTarget;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
/*
|
|
|
|
* A RefreshDriverTimer for inactive documents. When a new refresh driver is
|
|
|
|
* added, the rate is reset to the base (normally 1s/1fps). Every time
|
|
|
|
* it ticks, a single refresh driver is poked. Once they have all been poked,
|
|
|
|
* the duration between ticks doubles, up to mDisableAfterMilliseconds. At that point,
|
|
|
|
* the timer is quiet and doesn't tick (until something is added to it again).
|
|
|
|
*
|
|
|
|
* When a timer is removed, there is a possibility of another timer
|
|
|
|
* being skipped for one cycle. We could avoid this by adjusting
|
|
|
|
* mNextDriverIndex in RemoveRefreshDriver, but there's little need to
|
|
|
|
* add that complexity. All we want is for inactive drivers to tick
|
|
|
|
* at some point, but we don't care too much about how often.
|
|
|
|
*/
|
|
|
|
class InactiveRefreshDriverTimer :
|
|
|
|
public RefreshDriverTimer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InactiveRefreshDriverTimer(double aRate)
|
|
|
|
: RefreshDriverTimer(aRate),
|
|
|
|
mNextTickDuration(aRate),
|
|
|
|
mDisableAfterMilliseconds(-1.0),
|
|
|
|
mNextDriverIndex(0)
|
|
|
|
{
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
InactiveRefreshDriverTimer(double aRate, double aDisableAfterMilliseconds)
|
|
|
|
: RefreshDriverTimer(aRate),
|
|
|
|
mNextTickDuration(aRate),
|
|
|
|
mDisableAfterMilliseconds(aDisableAfterMilliseconds),
|
|
|
|
mNextDriverIndex(0)
|
|
|
|
{
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void AddRefreshDriver(nsRefreshDriver* aDriver)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
mNextDriverIndex = mRefreshDrivers.Length() - 1;
|
|
|
|
|
|
|
|
StopTimer();
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void StartTimer()
|
|
|
|
{
|
2012-12-24 00:29:23 +00:00
|
|
|
mLastFireEpoch = JS_Now();
|
2012-12-11 22:15:32 +00:00
|
|
|
mLastFireTime = TimeStamp::Now();
|
|
|
|
|
2012-12-24 00:29:23 +00:00
|
|
|
mTargetTime = mLastFireTime + mRateDuration;
|
|
|
|
|
|
|
|
uint32_t delay = static_cast<uint32_t>(mRateMilliseconds);
|
|
|
|
mTimer->InitWithFuncCallback(TimerTickOne, this, delay, nsITimer::TYPE_ONE_SHOT);
|
2012-12-11 22:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StopTimer()
|
|
|
|
{
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ScheduleNextTick(TimeStamp aNowTime)
|
|
|
|
{
|
|
|
|
if (mDisableAfterMilliseconds > 0.0 &&
|
|
|
|
mNextTickDuration > mDisableAfterMilliseconds)
|
|
|
|
{
|
|
|
|
// We hit the time after which we should disable
|
|
|
|
// inactive window refreshes; don't schedule anything
|
|
|
|
// until we get kicked by an AddRefreshDriver call.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// double the next tick time if we've already gone through all of them once
|
|
|
|
if (mNextDriverIndex >= mRefreshDrivers.Length()) {
|
|
|
|
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,
|
|
|
|
mNextDriverIndex, mRefreshDrivers.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Runs just one driver's tick. */
|
|
|
|
void TickOne()
|
|
|
|
{
|
|
|
|
int64_t jsnow = JS_Now();
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
|
|
|
|
ScheduleNextTick(now);
|
|
|
|
|
|
|
|
mLastFireEpoch = jsnow;
|
|
|
|
mLastFireTime = now;
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<nsRefreshDriver> > drivers(mRefreshDrivers);
|
|
|
|
if (mNextDriverIndex < drivers.Length() &&
|
|
|
|
!drivers[mNextDriverIndex]->IsTestControllingRefreshesEnabled())
|
|
|
|
{
|
|
|
|
TickDriver(drivers[mNextDriverIndex], jsnow, now);
|
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
mNextDriverIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TimerTickOne(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
InactiveRefreshDriverTimer *timer = static_cast<InactiveRefreshDriverTimer*>(aClosure);
|
|
|
|
timer->TickOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsITimer> mTimer;
|
|
|
|
double mNextTickDuration;
|
|
|
|
double mDisableAfterMilliseconds;
|
|
|
|
uint32_t mNextDriverIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-05-17 20:57:26 +00: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-11 22:15:32 +00:00
|
|
|
static PreciseRefreshDriverTimer *sRegularRateTimer = nullptr;
|
|
|
|
static InactiveRefreshDriverTimer *sThrottledRateTimer = nullptr;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
static int32_t sHighPrecisionTimerRequests = 0;
|
|
|
|
// a bare pointer to avoid introducing a static constructor
|
|
|
|
static nsITimer *sDisableHighPrecisionTimersTimer = nullptr;
|
|
|
|
#endif
|
2011-03-07 16:58:48 +00:00
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsRefreshDriver::InitializeStatics()
|
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gLog) {
|
|
|
|
gLog = PR_NewLogModule("nsRefreshDriver");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 16:58:48 +00:00
|
|
|
}
|
2011-12-24 03:52:26 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
/* static */ int32_t
|
2011-12-24 03:52:26 +00:00
|
|
|
nsRefreshDriver::DefaultInterval()
|
|
|
|
{
|
|
|
|
return NSToIntRound(1000.0 / DEFAULT_FRAME_RATE);
|
|
|
|
}
|
|
|
|
|
2013-05-29 10:31:16 +00: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-02 20:29:57 +00: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-11 22:15:32 +00:00
|
|
|
double
|
2013-05-29 10:31:16 +00:00
|
|
|
nsRefreshDriver::GetRegularTimerInterval(bool *outIsDefault) const
|
2010-08-23 09:30:08 +00:00
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
int32_t rate = Preferences::GetInt("layout.frame_rate", -1);
|
2013-08-02 20:29:57 +00:00
|
|
|
if (rate < 0) {
|
2012-12-11 22:15:32 +00:00
|
|
|
rate = DEFAULT_FRAME_RATE;
|
2013-05-29 10:31:16 +00:00
|
|
|
if (outIsDefault) {
|
|
|
|
*outIsDefault = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (outIsDefault) {
|
|
|
|
*outIsDefault = false;
|
|
|
|
}
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
2013-08-02 20:29:57 +00:00
|
|
|
|
|
|
|
if (rate == 0) {
|
|
|
|
rate = 10000;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
return 1000.0 / rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
nsRefreshDriver::GetThrottledTimerInterval() const
|
|
|
|
{
|
|
|
|
int32_t rate = Preferences::GetInt("layout.throttled_frame_rate", -1);
|
|
|
|
if (rate <= 0) {
|
|
|
|
rate = DEFAULT_THROTTLED_FRAME_RATE;
|
2011-03-04 15:27:02 +00:00
|
|
|
}
|
2012-12-11 22:15:32 +00:00
|
|
|
return 1000.0 / rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
nsRefreshDriver::GetRefreshTimerInterval() const
|
|
|
|
{
|
|
|
|
return mThrottled ? GetThrottledTimerInterval() : GetRegularTimerInterval();
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
RefreshDriverTimer*
|
|
|
|
nsRefreshDriver::ChooseTimer() const
|
2010-08-23 09:30:08 +00:00
|
|
|
{
|
2011-03-04 15:27:02 +00:00
|
|
|
if (mThrottled) {
|
2012-12-11 22:15:32 +00:00
|
|
|
if (!sThrottledRateTimer)
|
|
|
|
sThrottledRateTimer = new InactiveRefreshDriverTimer(GetThrottledTimerInterval(),
|
|
|
|
DEFAULT_INACTIVE_TIMER_DISABLE_SECONDS * 1000.0);
|
|
|
|
return sThrottledRateTimer;
|
2012-10-02 16:38:49 +00:00
|
|
|
}
|
2012-12-11 22:15:32 +00:00
|
|
|
|
2013-05-29 10:31:16 +00:00
|
|
|
if (!sRegularRateTimer) {
|
|
|
|
bool isDefault = true;
|
|
|
|
double rate = GetRegularTimerInterval(&isDefault);
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (PreciseRefreshDriverTimerWindowsDwmVsync::IsSupported()) {
|
|
|
|
sRegularRateTimer = new PreciseRefreshDriverTimerWindowsDwmVsync(rate, isDefault);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!sRegularRateTimer) {
|
|
|
|
sRegularRateTimer = new PreciseRefreshDriverTimer(rate);
|
|
|
|
}
|
|
|
|
}
|
2012-12-11 22:15:32 +00:00
|
|
|
return sRegularRateTimer;
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
nsRefreshDriver::nsRefreshDriver(nsPresContext* aPresContext)
|
|
|
|
: mActiveTimer(nullptr),
|
|
|
|
mPresContext(aPresContext),
|
2013-12-19 21:38:35 +00:00
|
|
|
mFreezeCount(0),
|
2011-03-07 16:58:48 +00:00
|
|
|
mThrottled(false),
|
2011-04-12 06:18:43 +00:00
|
|
|
mTestControllingRefreshes(false),
|
2011-12-24 03:52:22 +00:00
|
|
|
mViewManagerFlushIsPending(false),
|
2013-09-04 11:47:21 +00:00
|
|
|
mRequestedHighPrecision(false),
|
|
|
|
mInRefresh(false)
|
2009-10-08 03:22:42 +00:00
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
mMostRecentRefreshEpochTime = JS_Now();
|
|
|
|
mMostRecentRefresh = TimeStamp::Now();
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver::~nsRefreshDriver()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(ObserverCount() == 0,
|
|
|
|
"observers should have unregistered");
|
2012-12-11 22:15:32 +00:00
|
|
|
NS_ABORT_IF_FALSE(!mActiveTimer, "timer should be gone");
|
2012-08-29 05:48:44 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPresShellsToInvalidateIfHidden.Length(); i++) {
|
|
|
|
mPresShellsToInvalidateIfHidden[i]->InvalidatePresShellIfHidden();
|
|
|
|
}
|
|
|
|
mPresShellsToInvalidateIfHidden.Clear();
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 06:18:43 +00:00
|
|
|
// Method for testing. See nsIDOMWindowUtils.advanceTimeAndRefresh
|
|
|
|
// for description.
|
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsRefreshDriver::AdvanceTimeAndRefresh(int64_t aMilliseconds)
|
2011-04-12 06:18:43 +00:00
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
// ensure that we're removed from our driver
|
|
|
|
StopTimer();
|
|
|
|
|
|
|
|
if (!mTestControllingRefreshes) {
|
|
|
|
mMostRecentRefreshEpochTime = JS_Now();
|
|
|
|
mMostRecentRefresh = TimeStamp::Now();
|
|
|
|
|
|
|
|
mTestControllingRefreshes = true;
|
|
|
|
}
|
|
|
|
|
2011-04-12 06:18:43 +00:00
|
|
|
mMostRecentRefreshEpochTime += aMilliseconds * 1000;
|
2012-12-11 22:15:32 +00:00
|
|
|
mMostRecentRefresh += TimeDuration::FromMilliseconds((double) aMilliseconds);
|
|
|
|
|
2013-12-12 01:51:58 +00:00
|
|
|
mozilla::dom::AutoSystemCaller asc;
|
2013-02-26 19:04:11 +00:00
|
|
|
DoTick();
|
2011-04-12 06:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RestoreNormalRefresh()
|
|
|
|
{
|
|
|
|
mTestControllingRefreshes = false;
|
2012-12-11 22:15:32 +00:00
|
|
|
EnsureTimerStarted(false);
|
2011-04-12 06:18:43 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
TimeStamp
|
|
|
|
nsRefreshDriver::MostRecentRefresh() const
|
|
|
|
{
|
2012-12-24 00:29:23 +00:00
|
|
|
const_cast<nsRefreshDriver*>(this)->EnsureTimerStarted(false);
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
return mMostRecentRefresh;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2010-08-11 21:05:28 +00:00
|
|
|
nsRefreshDriver::MostRecentRefreshEpochTime() const
|
|
|
|
{
|
2012-12-24 00:29:23 +00:00
|
|
|
const_cast<nsRefreshDriver*>(this)->EnsureTimerStarted(false);
|
|
|
|
|
2010-08-11 21:05:28 +00:00
|
|
|
return mMostRecentRefreshEpochTime;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-12-11 22:15:32 +00:00
|
|
|
nsRefreshDriver::AddRefreshObserver(nsARefreshObserver* aObserver,
|
2009-10-08 03:22:42 +00:00
|
|
|
mozFlushType aFlushType)
|
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
2012-07-30 14:20:58 +00:00
|
|
|
bool success = array.AppendElement(aObserver) != nullptr;
|
2011-04-22 03:17:32 +00:00
|
|
|
EnsureTimerStarted(false);
|
2009-10-08 03:22:42 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-12-11 22:15:32 +00:00
|
|
|
nsRefreshDriver::RemoveRefreshObserver(nsARefreshObserver* aObserver,
|
2009-10-08 03:22:42 +00:00
|
|
|
mozFlushType aFlushType)
|
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
2009-12-31 19:07:57 +00:00
|
|
|
return array.RemoveElement(aObserver);
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 13:08:44 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::AddPostRefreshObserver(nsAPostRefreshObserver* aObserver)
|
|
|
|
{
|
|
|
|
mPostRefreshObservers.AppendElement(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RemovePostRefreshObserver(nsAPostRefreshObserver* aObserver)
|
|
|
|
{
|
|
|
|
mPostRefreshObservers.RemoveElement(aObserver);
|
|
|
|
}
|
|
|
|
|
2011-11-09 21:39:16 +00:00
|
|
|
bool
|
|
|
|
nsRefreshDriver::AddImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
2013-05-17 20:57:26 +00: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-09 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTimerStarted(false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RemoveImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
2013-05-17 20:57:26 +00:00
|
|
|
// Try to remove from both places, just in case, because we can't tell
|
|
|
|
// whether RemoveEntry() succeeds.
|
2011-11-09 21:39:16 +00:00
|
|
|
mRequests.RemoveEntry(aRequest);
|
2013-05-17 20:57:26 +00:00
|
|
|
uint32_t delay = GetFirstFrameDelay(aRequest);
|
|
|
|
if (delay != 0) {
|
|
|
|
ImageStartData* start = mStartTable.Get(delay);
|
|
|
|
if (start) {
|
|
|
|
start->mEntries.RemoveEntry(aRequest);
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
void
|
2011-04-22 03:17:32 +00:00
|
|
|
nsRefreshDriver::EnsureTimerStarted(bool aAdjustingTimer)
|
2009-10-08 03:22:42 +00:00
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
if (mTestControllingRefreshes)
|
2009-10-08 03:22:42 +00:00
|
|
|
return;
|
2012-10-02 14:54:46 +00:00
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
// will it already fire, and no other changes needed?
|
|
|
|
if (mActiveTimer && !aAdjustingTimer)
|
|
|
|
return;
|
2012-10-02 14:54:46 +00:00
|
|
|
|
2013-12-19 21:38:35 +00:00
|
|
|
if (IsFrozen() || !mPresContext) {
|
2012-12-11 22:15:32 +00:00
|
|
|
// If we don't want to start it now, or we've been disconnected.
|
|
|
|
StopTimer();
|
2009-10-08 03:22:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:15:32 +00: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 09:57:12 +00:00
|
|
|
}
|
2012-12-24 00:29:23 +00:00
|
|
|
|
|
|
|
mMostRecentRefresh = mActiveTimer->MostRecentRefresh();
|
|
|
|
mMostRecentRefreshEpochTime = mActiveTimer->MostRecentRefreshEpochTime();
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::StopTimer()
|
|
|
|
{
|
2012-12-11 22:15:32 +00:00
|
|
|
if (!mActiveTimer)
|
2009-10-08 03:22:42 +00:00
|
|
|
return;
|
2012-12-11 22:15:32 +00:00
|
|
|
|
|
|
|
mActiveTimer->RemoveRefreshDriver(this);
|
|
|
|
mActiveTimer = nullptr;
|
|
|
|
|
|
|
|
if (mRequestedHighPrecision) {
|
|
|
|
SetHighPrecisionTimersEnabled(false);
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
2012-12-11 22:15:32 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
static void
|
|
|
|
DisableHighPrecisionTimersCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
|
|
|
timeEndPeriod(1);
|
|
|
|
NS_RELEASE(sDisableHighPrecisionTimersTimer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::ConfigureHighPrecision()
|
|
|
|
{
|
|
|
|
bool haveFrameRequestCallbacks = mFrameRequestCallbackDocs.Length() > 0;
|
|
|
|
|
|
|
|
// if the only change that's needed is that we need high precision,
|
|
|
|
// then just set that
|
|
|
|
if (!mThrottled && !mRequestedHighPrecision && haveFrameRequestCallbacks) {
|
|
|
|
SetHighPrecisionTimersEnabled(true);
|
|
|
|
} else if (mRequestedHighPrecision && !haveFrameRequestCallbacks) {
|
|
|
|
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 17:47:57 +00: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-11 22:15:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mRequestedHighPrecision = false;
|
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2009-10-08 03:22:42 +00:00
|
|
|
nsRefreshDriver::ObserverCount() const
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t sum = 0;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(mObservers); ++i) {
|
2009-10-08 03:22:42 +00:00
|
|
|
sum += mObservers[i].Length();
|
|
|
|
}
|
2011-11-09 21:39:16 +00:00
|
|
|
|
2010-08-27 04:26:23 +00: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-11 21:05:27 +00:00
|
|
|
sum += mStyleFlushObservers.Length();
|
|
|
|
sum += mLayoutFlushObservers.Length();
|
2011-11-28 12:48:30 +00:00
|
|
|
sum += mFrameRequestCallbackDocs.Length();
|
2011-12-24 03:52:22 +00:00
|
|
|
sum += mViewManagerFlushIsPending;
|
2009-10-08 03:22:42 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2013-05-17 20:57:26 +00:00
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsRefreshDriver::StartTableRequestCounter(const uint32_t& aKey,
|
|
|
|
ImageStartData* aEntry,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
uint32_t *count = static_cast<uint32_t*>(aUserArg);
|
|
|
|
*count += aEntry->mEntries.Count();
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2011-11-09 21:39:16 +00:00
|
|
|
nsRefreshDriver::ImageRequestCount() const
|
|
|
|
{
|
2013-05-17 20:57:26 +00:00
|
|
|
uint32_t count = 0;
|
|
|
|
mStartTable.EnumerateRead(nsRefreshDriver::StartTableRequestCounter, &count);
|
|
|
|
return count + mRequests.Count();
|
2011-11-09 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
nsRefreshDriver::ObserverArray&
|
|
|
|
nsRefreshDriver::ArrayFor(mozFlushType aFlushType)
|
|
|
|
{
|
|
|
|
switch (aFlushType) {
|
|
|
|
case Flush_Style:
|
|
|
|
return mObservers[0];
|
|
|
|
case Flush_Layout:
|
|
|
|
return mObservers[1];
|
|
|
|
case Flush_Display:
|
|
|
|
return mObservers[2];
|
|
|
|
default:
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ABORT_IF_FALSE(false, "bad flush type");
|
2012-07-30 14:20:58 +00:00
|
|
|
return *static_cast<ObserverArray*>(nullptr);
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsISupports implementation
|
|
|
|
*/
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsRefreshDriver, nsISupports)
|
2009-10-08 03:22:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsITimerCallback implementation
|
|
|
|
*/
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::DoTick()
|
2009-10-08 03:22:42 +00:00
|
|
|
{
|
2013-12-19 21:38:35 +00:00
|
|
|
NS_PRECONDITION(!IsFrozen(), "Why are we notified while frozen?");
|
2010-02-03 21:17:55 +00:00
|
|
|
NS_PRECONDITION(mPresContext, "Why are we notified after disconnection?");
|
2011-05-05 16:26:32 +00:00
|
|
|
NS_PRECONDITION(!nsContentUtils::GetCurrentJSContext(),
|
|
|
|
"Shouldn't have a JSContext on the stack");
|
2012-12-11 22:15:32 +00:00
|
|
|
|
|
|
|
if (mTestControllingRefreshes) {
|
|
|
|
Tick(mMostRecentRefreshEpochTime, mMostRecentRefresh);
|
|
|
|
} else {
|
|
|
|
Tick(JS_Now(), TimeStamp::Now());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 15:13:21 +00:00
|
|
|
struct DocumentFrameCallbacks {
|
|
|
|
DocumentFrameCallbacks(nsIDocument* aDocument) :
|
|
|
|
mDocument(aDocument)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
nsIDocument::FrameRequestCallbackList mCallbacks;
|
|
|
|
};
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Tick(int64_t aNowEpoch, TimeStamp aNowTime)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!nsContentUtils::GetCurrentJSContext(),
|
|
|
|
"Shouldn't have a JSContext on the stack");
|
|
|
|
|
2013-02-27 11:50:27 +00:00
|
|
|
if (nsNPAPIPluginInstance::InPluginCallUnsafeForReentry()) {
|
2012-12-06 23:58:14 +00:00
|
|
|
NS_ERROR("Refresh driver should not run during plugin call!");
|
|
|
|
// Try to survive this by just ignoring the refresh tick.
|
2012-12-11 22:15:32 +00:00
|
|
|
return;
|
2012-12-06 23:58:14 +00:00
|
|
|
}
|
2010-02-03 21:17:55 +00:00
|
|
|
|
2013-03-16 04:47:02 +00:00
|
|
|
PROFILER_LABEL("nsRefreshDriver", "Tick");
|
2012-12-11 22:15:32 +00: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-19 21:38:35 +00:00
|
|
|
if (IsFrozen() || !mPresContext) {
|
2012-12-11 22:15:32 +00:00
|
|
|
return;
|
2011-04-12 06:18:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 20:57:26 +00:00
|
|
|
TimeStamp previousRefresh = mMostRecentRefresh;
|
|
|
|
|
2012-12-11 22:15:32 +00:00
|
|
|
mMostRecentRefresh = aNowTime;
|
|
|
|
mMostRecentRefreshEpochTime = aNowEpoch;
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2009-12-21 21:46:24 +00:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = mPresContext->GetPresShell();
|
2011-11-09 21:39:16 +00:00
|
|
|
if (!presShell || (ObserverCount() == 0 && ImageRequestCount() == 0)) {
|
2009-12-31 19:07:57 +00: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 03:22:42 +00:00
|
|
|
StopTimer();
|
2012-12-11 22:15:32 +00:00
|
|
|
return;
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:08:45 +00:00
|
|
|
profiler_tracing("Paint", "RD", TRACING_INTERVAL_START);
|
|
|
|
|
2013-09-04 11:47:21 +00:00
|
|
|
AutoRestore<bool> restoreInRefresh(mInRefresh);
|
|
|
|
mInRefresh = true;
|
|
|
|
|
2009-12-21 21:46:25 +00: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 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(mObservers); ++i) {
|
2009-10-08 03:22:42 +00:00
|
|
|
ObserverArray::EndLimitedIterator etor(mObservers[i]);
|
|
|
|
while (etor.HasMore()) {
|
2009-12-21 21:46:25 +00:00
|
|
|
nsRefPtr<nsARefreshObserver> obs = etor.GetNext();
|
2012-12-11 22:15:32 +00:00
|
|
|
obs->WillRefresh(aNowTime);
|
2009-12-21 21:46:25 +00:00
|
|
|
|
|
|
|
if (!mPresContext || !mPresContext->GetPresShell()) {
|
|
|
|
StopTimer();
|
2013-09-27 16:08:45 +00:00
|
|
|
profiler_tracing("Paint", "RD", TRACING_INTERVAL_END);
|
2012-12-11 22:15:32 +00:00
|
|
|
return;
|
2009-12-21 21:46:25 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
2011-11-09 21:39:16 +00:00
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
if (i == 0) {
|
2011-11-28 12:49:53 +00:00
|
|
|
// Grab all of our frame request callbacks up front.
|
2013-04-25 15:13:21 +00:00
|
|
|
nsTArray<DocumentFrameCallbacks>
|
|
|
|
frameRequestCallbacks(mFrameRequestCallbackDocs.Length());
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mFrameRequestCallbackDocs.Length(); ++i) {
|
2013-04-25 15:13:21 +00:00
|
|
|
frameRequestCallbacks.AppendElement(mFrameRequestCallbackDocs[i]);
|
2011-11-28 12:48:30 +00:00
|
|
|
mFrameRequestCallbackDocs[i]->
|
2013-04-25 15:13:21 +00:00
|
|
|
TakeFrameRequestCallbacks(frameRequestCallbacks.LastElement().mCallbacks);
|
2010-09-09 05:38:04 +00:00
|
|
|
}
|
2011-11-28 12:48:30 +00:00
|
|
|
// OK, now reset mFrameRequestCallbackDocs so they can be
|
2010-09-09 05:38:04 +00:00
|
|
|
// readded as needed.
|
2011-11-28 12:48:30 +00:00
|
|
|
mFrameRequestCallbackDocs.Clear();
|
2010-09-09 05:38:04 +00:00
|
|
|
|
2013-09-27 16:08:45 +00:00
|
|
|
profiler_tracing("Paint", "Scripts", TRACING_INTERVAL_START);
|
2012-12-11 22:15:32 +00:00
|
|
|
int64_t eventTime = aNowEpoch / PR_USEC_PER_MSEC;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < frameRequestCallbacks.Length(); ++i) {
|
2013-04-25 15:13:21 +00:00
|
|
|
const DocumentFrameCallbacks& docCallbacks = frameRequestCallbacks[i];
|
|
|
|
// XXXbz Bug 863140: GetInnerWindow can return the outer
|
|
|
|
// window in some cases.
|
|
|
|
nsPIDOMWindow* innerWindow = docCallbacks.mDocument->GetInnerWindow();
|
2013-05-02 03:44:11 +00:00
|
|
|
DOMHighResTimeStamp timeStamp = 0;
|
2013-04-25 15:13:21 +00:00
|
|
|
if (innerWindow && innerWindow->IsInnerWindow()) {
|
2013-05-02 03:44:11 +00:00
|
|
|
nsPerformance* perf = innerWindow->GetPerformance();
|
|
|
|
if (perf) {
|
|
|
|
timeStamp = perf->GetDOMTiming()->TimeStampToDOMHighRes(aNowTime);
|
|
|
|
}
|
|
|
|
// else window is partially torn down already
|
2013-04-25 15:13:21 +00:00
|
|
|
}
|
|
|
|
for (uint32_t j = 0; j < docCallbacks.mCallbacks.Length(); ++j) {
|
|
|
|
const nsIDocument::FrameRequestCallbackHolder& holder =
|
|
|
|
docCallbacks.mCallbacks[j];
|
|
|
|
nsAutoMicroTask mt;
|
|
|
|
if (holder.HasWebIDLCallback()) {
|
|
|
|
ErrorResult ignored;
|
|
|
|
holder.GetWebIDLCallback()->Call(timeStamp, ignored);
|
|
|
|
} else {
|
|
|
|
holder.GetXPCOMCallback()->Sample(eventTime);
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 05:38:04 +00:00
|
|
|
}
|
2013-09-27 16:08:45 +00:00
|
|
|
profiler_tracing("Paint", "Scripts", TRACING_INTERVAL_END);
|
2010-09-09 05:38:04 +00:00
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
// This is the Flush_Style case.
|
2011-01-14 20:03:06 +00:00
|
|
|
if (mPresContext && mPresContext->GetPresShell()) {
|
|
|
|
nsAutoTArray<nsIPresShell*, 16> observers;
|
|
|
|
observers.AppendElements(mStyleFlushObservers);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t j = observers.Length();
|
2011-01-14 20:03:06 +00: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;
|
|
|
|
NS_ADDREF(shell);
|
|
|
|
mStyleFlushObservers.RemoveElement(shell);
|
2013-07-20 19:14:25 +00:00
|
|
|
shell->GetPresContext()->RestyleManager()->mObservingRefreshDriver = false;
|
2012-12-11 21:12:43 +00:00
|
|
|
shell->FlushPendingNotifications(ChangesToFlush(Flush_Style, false));
|
2011-01-14 20:03:06 +00:00
|
|
|
NS_RELEASE(shell);
|
|
|
|
}
|
2010-08-11 21:05:27 +00:00
|
|
|
}
|
2010-02-19 17:11:40 +00:00
|
|
|
} else if (i == 1) {
|
|
|
|
// This is the Flush_Layout case.
|
2011-01-14 20:03:06 +00:00
|
|
|
if (mPresContext && mPresContext->GetPresShell()) {
|
|
|
|
nsAutoTArray<nsIPresShell*, 16> observers;
|
|
|
|
observers.AppendElements(mLayoutFlushObservers);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t j = observers.Length();
|
2011-01-14 20:03:06 +00: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 (!mLayoutFlushObservers.Contains(shell))
|
|
|
|
continue;
|
|
|
|
NS_ADDREF(shell);
|
|
|
|
mLayoutFlushObservers.RemoveElement(shell);
|
2011-10-17 14:59:28 +00:00
|
|
|
shell->mReflowScheduled = false;
|
|
|
|
shell->mSuppressInterruptibleReflows = false;
|
2012-12-11 21:12:43 +00:00
|
|
|
shell->FlushPendingNotifications(ChangesToFlush(Flush_InterruptibleLayout,
|
|
|
|
false));
|
2011-01-14 20:03:06 +00:00
|
|
|
NS_RELEASE(shell);
|
|
|
|
}
|
2010-08-11 21:05:27 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 21:39:16 +00:00
|
|
|
/*
|
|
|
|
* Perform notification to imgIRequests subscribed to listen
|
|
|
|
* for refresh events.
|
|
|
|
*/
|
|
|
|
|
2013-05-17 20:57:26 +00:00
|
|
|
ImageRequestParameters parms = {aNowTime, previousRefresh, &mRequests};
|
|
|
|
|
|
|
|
mStartTable.EnumerateRead(nsRefreshDriver::StartTableRefresh, &parms);
|
|
|
|
|
2011-11-09 21:39:16 +00:00
|
|
|
if (mRequests.Count()) {
|
2013-09-06 08:48:26 +00: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());
|
|
|
|
mRequests.EnumerateEntries(nsRefreshDriver::ImageRequestEnumerator,
|
|
|
|
&imagesToRefresh);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < imagesToRefresh.Length(); i++) {
|
|
|
|
imagesToRefresh[i]->RequestRefresh(aNowTime);
|
|
|
|
}
|
2011-11-09 21:39:16 +00:00
|
|
|
}
|
2013-05-17 20:57:26 +00:00
|
|
|
|
2012-08-29 05:48:44 +00:00
|
|
|
for (uint32_t i = 0; i < mPresShellsToInvalidateIfHidden.Length(); i++) {
|
|
|
|
mPresShellsToInvalidateIfHidden[i]->InvalidatePresShellIfHidden();
|
|
|
|
}
|
|
|
|
mPresShellsToInvalidateIfHidden.Clear();
|
2011-11-09 21:39:16 +00:00
|
|
|
|
2011-12-24 03:52:22 +00:00
|
|
|
if (mViewManagerFlushIsPending) {
|
2013-05-13 23:47:02 +00:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
|
|
|
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
|
2013-11-23 22:44:18 +00:00
|
|
|
printf_stderr("Starting ProcessPendingUpdates\n");
|
2013-05-13 23:47:02 +00:00
|
|
|
}
|
2012-08-13 10:10:10 +00:00
|
|
|
#endif
|
2013-01-04 02:53:15 +00:00
|
|
|
|
2011-12-24 03:52:22 +00:00
|
|
|
mViewManagerFlushIsPending = false;
|
2013-01-05 03:12:42 +00:00
|
|
|
nsRefPtr<nsViewManager> vm = mPresContext->GetPresShell()->GetViewManager();
|
2014-01-16 14:09:08 +00:00
|
|
|
vm->ProcessPendingUpdates(nsViewManager::eTrySyncUpdate);
|
2013-05-13 23:47:02 +00:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
|
|
|
if (nsLayoutUtils::InvalidationDebuggingIsEnabled()) {
|
2013-11-23 22:44:18 +00:00
|
|
|
printf_stderr("Ending ProcessPendingUpdates\n");
|
2013-05-13 23:47:02 +00:00
|
|
|
}
|
2012-08-13 10:10:10 +00:00
|
|
|
#endif
|
2011-12-24 03:52:22 +00:00
|
|
|
}
|
2013-08-19 13:08:44 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mPostRefreshObservers.Length(); ++i) {
|
|
|
|
mPostRefreshObservers[i]->DidRefresh();
|
|
|
|
}
|
2013-09-27 16:08:45 +00:00
|
|
|
profiler_tracing("Paint", "RD", TRACING_INTERVAL_END);
|
2013-09-04 11:47:21 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(mInRefresh, "Still in refresh");
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
2010-02-03 21:17:55 +00:00
|
|
|
|
2013-05-17 20:57:26 +00:00
|
|
|
/* static */ PLDHashOperator
|
2011-11-09 21:39:16 +00:00
|
|
|
nsRefreshDriver::ImageRequestEnumerator(nsISupportsHashKey* aEntry,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
2013-09-06 08:48:26 +00:00
|
|
|
nsCOMArray<imgIContainer>* imagesToRefresh =
|
|
|
|
static_cast<nsCOMArray<imgIContainer>*> (aUserArg);
|
2011-11-09 21:39:16 +00:00
|
|
|
imgIRequest* req = static_cast<imgIRequest*>(aEntry->GetKey());
|
|
|
|
NS_ABORT_IF_FALSE(req, "Unable to retrieve the image request");
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
2012-10-16 23:05:54 +00:00
|
|
|
if (NS_SUCCEEDED(req->GetImage(getter_AddRefs(image)))) {
|
2013-09-06 08:48:26 +00:00
|
|
|
imagesToRefresh->AppendElement(image);
|
2011-11-09 21:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-05-17 20:57:26 +00:00
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsRefreshDriver::BeginRefreshingImages(nsISupportsHashKey* aEntry,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ImageRequestParameters* parms =
|
|
|
|
static_cast<ImageRequestParameters*> (aUserArg);
|
|
|
|
|
|
|
|
imgIRequest* req = static_cast<imgIRequest*>(aEntry->GetKey());
|
|
|
|
NS_ABORT_IF_FALSE(req, "Unable to retrieve the image request");
|
|
|
|
|
|
|
|
parms->mRequests->PutEntry(req);
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (NS_SUCCEEDED(req->GetImage(getter_AddRefs(image)))) {
|
|
|
|
image->SetAnimationStartTime(parms->mDesired);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsRefreshDriver::StartTableRefresh(const uint32_t& aDelay,
|
|
|
|
ImageStartData* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ImageRequestParameters* parms =
|
|
|
|
static_cast<ImageRequestParameters*> (aUserArg);
|
|
|
|
|
|
|
|
if (!aData->mStartTime.empty()) {
|
|
|
|
TimeStamp& start = aData->mStartTime.ref();
|
|
|
|
TimeDuration prev = parms->mPrevious - start;
|
|
|
|
TimeDuration curr = parms->mCurrent - start;
|
|
|
|
uint32_t prevMultiple = static_cast<uint32_t>(prev.ToMilliseconds()) / aDelay;
|
|
|
|
|
|
|
|
// 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 != static_cast<uint32_t>(curr.ToMilliseconds()) / aDelay) {
|
|
|
|
parms->mDesired = start + TimeDuration::FromMilliseconds(prevMultiple * aDelay);
|
|
|
|
aData->mEntries.EnumerateEntries(nsRefreshDriver::BeginRefreshingImages, parms);
|
|
|
|
}
|
|
|
|
} 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.
|
|
|
|
parms->mDesired = parms->mCurrent;
|
|
|
|
aData->mEntries.EnumerateEntries(nsRefreshDriver::BeginRefreshingImages, parms);
|
|
|
|
aData->mStartTime.construct(parms->mCurrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-02-03 21:17:55 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Freeze()
|
|
|
|
{
|
|
|
|
StopTimer();
|
2013-12-19 21:38:35 +00:00
|
|
|
mFreezeCount++;
|
2010-02-03 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::Thaw()
|
|
|
|
{
|
2013-12-19 21:38:35 +00: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.
|
|
|
|
NS_DispatchToCurrentThread(NS_NewRunnableMethod(this, &nsRefreshDriver::DoRefresh));
|
|
|
|
EnsureTimerStarted(false);
|
|
|
|
}
|
2010-02-03 21:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 04:26:23 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::SetThrottled(bool aThrottled)
|
|
|
|
{
|
|
|
|
if (aThrottled != mThrottled) {
|
|
|
|
mThrottled = aThrottled;
|
2012-12-11 22:15:32 +00:00
|
|
|
if (mActiveTimer) {
|
2011-03-04 15:27:02 +00:00
|
|
|
// We want to switch our timer type here, so just stop and
|
|
|
|
// restart the timer.
|
2011-04-22 03:17:32 +00:00
|
|
|
EnsureTimerStarted(true);
|
2010-08-27 04:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-03 21:17:55 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::DoRefresh()
|
|
|
|
{
|
|
|
|
// Don't do a refresh unless we're in a state where we should be refreshing.
|
2013-12-19 21:38:35 +00:00
|
|
|
if (!IsFrozen() && mPresContext && mActiveTimer) {
|
2012-12-11 22:15:32 +00:00
|
|
|
DoTick();
|
2010-02-03 21:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-19 17:11:40 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-12-11 22:15:32 +00:00
|
|
|
nsRefreshDriver::IsRefreshObserver(nsARefreshObserver* aObserver,
|
2010-02-19 17:11:40 +00:00
|
|
|
mozFlushType aFlushType)
|
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
|
|
|
return array.Contains(aObserver);
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-11 21:05:28 +00:00
|
|
|
|
2012-09-17 05:06:12 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::ScheduleViewManagerFlush()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPresContext->IsRoot(),
|
|
|
|
"Should only schedule view manager flush on root prescontexts");
|
|
|
|
mViewManagerFlushIsPending = true;
|
|
|
|
EnsureTimerStarted(false);
|
|
|
|
}
|
|
|
|
|
2010-09-09 05:38:04 +00:00
|
|
|
void
|
2011-11-28 12:48:30 +00:00
|
|
|
nsRefreshDriver::ScheduleFrameRequestCallbacks(nsIDocument* aDocument)
|
2010-09-09 05:38:04 +00:00
|
|
|
{
|
2011-11-28 12:48:30 +00:00
|
|
|
NS_ASSERTION(mFrameRequestCallbackDocs.IndexOf(aDocument) ==
|
|
|
|
mFrameRequestCallbackDocs.NoIndex,
|
2010-09-09 05:38:04 +00:00
|
|
|
"Don't schedule the same document multiple times");
|
2011-11-28 12:48:30 +00:00
|
|
|
mFrameRequestCallbackDocs.AppendElement(aDocument);
|
2012-12-11 22:15:32 +00:00
|
|
|
|
|
|
|
// make sure that the timer is running
|
|
|
|
ConfigureHighPrecision();
|
2011-04-22 03:17:32 +00:00
|
|
|
EnsureTimerStarted(false);
|
2010-09-09 05:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-28 12:48:30 +00:00
|
|
|
nsRefreshDriver::RevokeFrameRequestCallbacks(nsIDocument* aDocument)
|
2010-09-09 05:38:04 +00:00
|
|
|
{
|
2011-11-28 12:48:30 +00:00
|
|
|
mFrameRequestCallbackDocs.RemoveElement(aDocument);
|
2012-12-11 22:15:32 +00:00
|
|
|
ConfigureHighPrecision();
|
2011-03-07 16:58:48 +00: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 05:38:04 +00:00
|
|
|
}
|
2013-11-15 02:43:56 +00:00
|
|
|
|
|
|
|
#undef LOG
|