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.)
|
|
|
|
*/
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
#include "nsRefreshDriver.h"
|
|
|
|
#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:27 +00:00
|
|
|
#include "nsCSSFrameConstructor.h"
|
2010-08-11 21:05:28 +00:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsEventDispatcher.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"
|
2011-12-24 03:52:22 +00:00
|
|
|
#include "nsIViewManager.h"
|
2012-08-04 03:23:24 +00:00
|
|
|
#include "sampler.h"
|
2010-08-23 09:30:08 +00:00
|
|
|
|
|
|
|
using mozilla::TimeStamp;
|
2011-04-12 06:18:43 +00:00
|
|
|
using mozilla::TimeDuration;
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2011-05-25 06:32:00 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
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
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool sPrecisePref;
|
2011-03-07 16:58:48 +00:00
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsRefreshDriver::InitializeStatics()
|
|
|
|
{
|
2011-05-28 23:42:57 +00:00
|
|
|
Preferences::AddBoolVarCache(&sPrecisePref,
|
|
|
|
"layout.frame_rate.precise",
|
2011-10-17 14:59:28 +00:00
|
|
|
false);
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-23 09:30:08 +00:00
|
|
|
// Compute the interval to use for the refresh driver timer, in
|
|
|
|
// milliseconds
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2011-03-04 15:27:02 +00:00
|
|
|
nsRefreshDriver::GetRefreshTimerInterval() const
|
2010-08-23 09:30:08 +00:00
|
|
|
{
|
2010-08-27 04:26:23 +00:00
|
|
|
const char* prefName =
|
2011-03-04 15:27:02 +00:00
|
|
|
mThrottled ? "layout.throttled_frame_rate" : "layout.frame_rate";
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t rate = Preferences::GetInt(prefName, -1);
|
2010-08-23 09:30:08 +00:00
|
|
|
if (rate <= 0) {
|
|
|
|
// TODO: get the rate from the platform
|
2011-03-04 15:27:02 +00:00
|
|
|
rate = mThrottled ? DEFAULT_THROTTLED_FRAME_RATE : DEFAULT_FRAME_RATE;
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
|
|
|
NS_ASSERTION(rate > 0, "Must have positive rate here");
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t interval = NSToIntRound(1000.0/rate);
|
2011-03-04 15:27:02 +00:00
|
|
|
if (mThrottled) {
|
|
|
|
interval = NS_MAX(interval, mLastTimerInterval * 2);
|
|
|
|
}
|
|
|
|
mLastTimerInterval = interval;
|
|
|
|
return interval;
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2011-03-04 15:27:02 +00:00
|
|
|
nsRefreshDriver::GetRefreshTimerType() const
|
2010-08-23 09:30:08 +00:00
|
|
|
{
|
2011-03-04 15:27:02 +00:00
|
|
|
if (mThrottled) {
|
|
|
|
return nsITimer::TYPE_ONE_SHOT;
|
|
|
|
}
|
2011-11-28 12:48:30 +00:00
|
|
|
if (HaveFrameRequestCallbacks() || sPrecisePref) {
|
2011-04-28 23:33:52 +00:00
|
|
|
return nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP;
|
2011-03-07 16:58:48 +00:00
|
|
|
}
|
|
|
|
return nsITimer::TYPE_REPEATING_SLACK;
|
2010-08-23 09:30:08 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
2009-12-21 21:46:24 +00:00
|
|
|
nsRefreshDriver::nsRefreshDriver(nsPresContext *aPresContext)
|
2010-02-03 21:17:55 +00:00
|
|
|
: mPresContext(aPresContext),
|
2010-08-27 04:26:23 +00:00
|
|
|
mFrozen(false),
|
2011-03-07 16:58:48 +00:00
|
|
|
mThrottled(false),
|
2011-04-12 06:18:43 +00:00
|
|
|
mTestControllingRefreshes(false),
|
2011-03-29 04:41:01 +00:00
|
|
|
mTimerIsPrecise(false),
|
2011-12-24 03:52:22 +00:00
|
|
|
mViewManagerFlushIsPending(false),
|
2011-03-29 04:41:01 +00:00
|
|
|
mLastTimerInterval(0)
|
2009-10-08 03:22:42 +00:00
|
|
|
{
|
2011-11-09 21:39:16 +00:00
|
|
|
mRequests.Init();
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver::~nsRefreshDriver()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(ObserverCount() == 0,
|
|
|
|
"observers should have unregistered");
|
|
|
|
NS_ABORT_IF_FALSE(!mTimer, "timer should be gone");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
mTestControllingRefreshes = true;
|
|
|
|
mMostRecentRefreshEpochTime += aMilliseconds * 1000;
|
|
|
|
mMostRecentRefresh += TimeDuration::FromMilliseconds(aMilliseconds);
|
2011-05-05 16:26:32 +00:00
|
|
|
nsCxPusher pusher;
|
|
|
|
if (pusher.PushNull()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
Notify(nullptr);
|
2011-05-05 16:26:32 +00:00
|
|
|
pusher.Pop();
|
|
|
|
}
|
2011-04-12 06:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RestoreNormalRefresh()
|
|
|
|
{
|
|
|
|
mTestControllingRefreshes = false;
|
2011-05-05 16:26:32 +00:00
|
|
|
nsCxPusher pusher;
|
|
|
|
if (pusher.PushNull()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
Notify(nullptr); // will call UpdateMostRecentRefresh()
|
2011-05-05 16:26:32 +00:00
|
|
|
pusher.Pop();
|
|
|
|
}
|
2011-04-12 06:18:43 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
TimeStamp
|
|
|
|
nsRefreshDriver::MostRecentRefresh() const
|
|
|
|
{
|
2011-04-22 03:17:32 +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
|
|
|
|
{
|
2011-04-22 03:17:32 +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
|
2009-10-08 03:22:42 +00:00
|
|
|
nsRefreshDriver::AddRefreshObserver(nsARefreshObserver *aObserver,
|
|
|
|
mozFlushType aFlushType)
|
|
|
|
{
|
|
|
|
ObserverArray& array = ArrayFor(aFlushType);
|
2012-07-30 14:20:58 +00:00
|
|
|
bool success = array.AppendElement(aObserver) != nullptr;
|
2009-10-08 03:22:42 +00:00
|
|
|
|
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
|
2009-10-08 03:22:42 +00:00
|
|
|
nsRefreshDriver::RemoveRefreshObserver(nsARefreshObserver *aObserver,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-11-09 21:39:16 +00:00
|
|
|
bool
|
|
|
|
nsRefreshDriver::AddImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
|
|
|
if (!mRequests.PutEntry(aRequest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTimerStarted(false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::RemoveImageRequest(imgIRequest* aRequest)
|
|
|
|
{
|
|
|
|
mRequests.RemoveEntry(aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsRefreshDriver::ClearAllImageRequests()
|
|
|
|
{
|
|
|
|
mRequests.Clear();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-02-03 21:17:55 +00:00
|
|
|
if (mTimer || mFrozen || !mPresContext) {
|
|
|
|
// It's already been started, or we don't want to start it now or
|
|
|
|
// we've been disconnected.
|
2009-10-08 03:22:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-22 03:17:32 +00:00
|
|
|
if (!aAdjustingTimer) {
|
|
|
|
// If we didn't already have a timer and aAdjustingTimer is false,
|
|
|
|
// then we just got our first observer (or an explicit call to
|
|
|
|
// MostRecentRefresh by a caller who's likely to add an observer
|
|
|
|
// shortly). This means we should fake a most-recent-refresh time
|
|
|
|
// of now so that said observer gets a reasonable refresh time, so
|
|
|
|
// things behave as though the timer had always been running.
|
|
|
|
UpdateMostRecentRefresh();
|
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!mTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t timerType = GetRefreshTimerType();
|
2011-04-28 23:33:52 +00:00
|
|
|
mTimerIsPrecise = (timerType == nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP);
|
2011-03-07 16:58:48 +00:00
|
|
|
|
2010-08-27 04:26:23 +00:00
|
|
|
nsresult rv = mTimer->InitWithCallback(this,
|
2011-03-04 15:27:02 +00:00
|
|
|
GetRefreshTimerInterval(),
|
2011-03-07 16:58:48 +00:00
|
|
|
timerType);
|
2009-10-08 03:22:42 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mTimer = nullptr;
|
2009-10-08 03:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::StopTimer()
|
|
|
|
{
|
|
|
|
if (!mTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer->Cancel();
|
2012-07-30 14:20:58 +00:00
|
|
|
mTimer = nullptr;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2011-11-09 21:39:16 +00:00
|
|
|
nsRefreshDriver::ImageRequestCount() const
|
|
|
|
{
|
|
|
|
return mRequests.Count();
|
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::UpdateMostRecentRefresh()
|
|
|
|
{
|
2011-04-12 06:18:43 +00:00
|
|
|
if (mTestControllingRefreshes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-11 21:05:28 +00:00
|
|
|
// Call JS_Now first, since that can have nonzero latency in some rare cases.
|
|
|
|
mMostRecentRefreshEpochTime = JS_Now();
|
2009-10-08 03:22:42 +00:00
|
|
|
mMostRecentRefresh = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2009-12-21 21:46:24 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsRefreshDriver, nsITimerCallback)
|
2009-10-08 03:22:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsITimerCallback implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-04-12 06:18:43 +00:00
|
|
|
nsRefreshDriver::Notify(nsITimer *aTimer)
|
2009-10-08 03:22:42 +00:00
|
|
|
{
|
2012-08-04 03:23:24 +00:00
|
|
|
SAMPLE_LABEL("nsRefreshDriver", "Notify");
|
|
|
|
|
2010-02-03 21:17:55 +00:00
|
|
|
NS_PRECONDITION(!mFrozen, "Why are we notified while frozen?");
|
|
|
|
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");
|
2010-02-03 21:17:55 +00:00
|
|
|
|
2011-04-12 06:18:43 +00:00
|
|
|
if (mTestControllingRefreshes && aTimer) {
|
|
|
|
// Ignore real refreshes from our timer (but honor the others).
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
UpdateMostRecentRefresh();
|
|
|
|
|
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();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
obs->WillRefresh(mMostRecentRefresh);
|
|
|
|
|
|
|
|
if (!mPresContext || !mPresContext->GetPresShell()) {
|
|
|
|
StopTimer();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
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.
|
2011-11-28 12:48:30 +00:00
|
|
|
nsIDocument::FrameRequestCallbackList frameRequestCallbacks;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mFrameRequestCallbackDocs.Length(); ++i) {
|
2011-11-28 12:48:30 +00:00
|
|
|
mFrameRequestCallbackDocs[i]->
|
|
|
|
TakeFrameRequestCallbacks(frameRequestCallbacks);
|
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
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t eventTime = mMostRecentRefreshEpochTime / PR_USEC_PER_MSEC;
|
|
|
|
for (uint32_t i = 0; i < frameRequestCallbacks.Length(); ++i) {
|
2012-03-31 16:30:13 +00:00
|
|
|
nsAutoMicroTask mt;
|
2011-11-28 12:48:30 +00:00
|
|
|
frameRequestCallbacks[i]->Sample(eventTime);
|
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);
|
2011-10-17 14:59:28 +00:00
|
|
|
shell->FrameConstructor()->mObservingRefreshDriver = false;
|
2011-01-14 20:03:06 +00:00
|
|
|
shell->FlushPendingNotifications(Flush_Style);
|
|
|
|
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;
|
2011-01-14 20:03:06 +00:00
|
|
|
shell->FlushPendingNotifications(Flush_InterruptibleLayout);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ImageRequestParameters parms = {mMostRecentRefresh};
|
|
|
|
if (mRequests.Count()) {
|
|
|
|
mRequests.EnumerateEntries(nsRefreshDriver::ImageRequestEnumerator, &parms);
|
|
|
|
EnsureTimerStarted(false);
|
|
|
|
}
|
|
|
|
|
2011-12-24 03:52:22 +00:00
|
|
|
if (mViewManagerFlushIsPending) {
|
2012-08-13 10:10:10 +00:00
|
|
|
#ifdef DEBUG_INVALIDATIONS
|
|
|
|
printf("Starting ProcessPendingUpdates\n");
|
|
|
|
#endif
|
2011-12-24 03:52:22 +00:00
|
|
|
mViewManagerFlushIsPending = false;
|
2012-07-04 00:14:47 +00:00
|
|
|
mPresContext->GetPresShell()->GetViewManager()->ProcessPendingUpdates();
|
2012-08-13 10:10:10 +00:00
|
|
|
#ifdef DEBUG_INVALIDATIONS
|
|
|
|
printf("Ending ProcessPendingUpdates\n");
|
|
|
|
#endif
|
2011-12-24 03:52:22 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 16:58:48 +00:00
|
|
|
if (mThrottled ||
|
|
|
|
(mTimerIsPrecise !=
|
2011-04-28 23:33:52 +00:00
|
|
|
(GetRefreshTimerType() == nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP))) {
|
2011-03-07 16:58:48 +00:00
|
|
|
// Stop the timer now and restart it here. Stopping is in the mThrottled
|
|
|
|
// case ok because either it's already one-shot, and it just fired, and all
|
|
|
|
// we need to do is null it out, or it's repeating and we need to reset it
|
|
|
|
// to be one-shot. Stopping and restarting in the case when we need to
|
|
|
|
// switch from precise to slack timers or vice versa is unfortunately
|
|
|
|
// required.
|
|
|
|
|
|
|
|
// Note that the EnsureTimerStarted() call here is ok because
|
|
|
|
// EnsureTimerStarted makes sure to not start the timer if it shouldn't be
|
|
|
|
// started.
|
2011-03-04 15:27:02 +00:00
|
|
|
StopTimer();
|
2011-04-22 03:17:32 +00:00
|
|
|
EnsureTimerStarted(true);
|
2011-03-04 15:27:02 +00:00
|
|
|
}
|
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-03 21:17:55 +00:00
|
|
|
|
2011-11-09 21:39:16 +00:00
|
|
|
PLDHashOperator
|
|
|
|
nsRefreshDriver::ImageRequestEnumerator(nsISupportsHashKey* aEntry,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
ImageRequestParameters* parms =
|
|
|
|
static_cast<ImageRequestParameters*> (aUserArg);
|
|
|
|
mozilla::TimeStamp mostRecentRefresh = parms->ts;
|
|
|
|
imgIRequest* req = static_cast<imgIRequest*>(aEntry->GetKey());
|
|
|
|
NS_ABORT_IF_FALSE(req, "Unable to retrieve the image request");
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
req->GetImage(getter_AddRefs(image));
|
|
|
|
if (image) {
|
|
|
|
image->RequestRefresh(mostRecentRefresh);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2010-02-03 21:17:55 +00:00
|
|
|
void
|
|
|
|
nsRefreshDriver::Freeze()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mFrozen, "Freeze called on already-frozen refresh driver");
|
|
|
|
StopTimer();
|
2010-08-27 04:26:23 +00:00
|
|
|
mFrozen = true;
|
2010-02-03 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRefreshDriver::Thaw()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mFrozen, "Thaw called on an unfrozen refresh driver");
|
2010-08-27 04:26:23 +00:00
|
|
|
mFrozen = false;
|
2011-11-09 21:39:16 +00:00
|
|
|
if (ObserverCount() || ImageRequestCount()) {
|
2011-04-22 03:17:32 +00:00
|
|
|
// 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.
|
2010-04-20 23:21:35 +00:00
|
|
|
NS_DispatchToCurrentThread(NS_NewRunnableMethod(this, &nsRefreshDriver::DoRefresh));
|
2011-04-22 03:17:32 +00:00
|
|
|
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;
|
|
|
|
if (mTimer) {
|
2011-03-04 15:27:02 +00:00
|
|
|
// We want to switch our timer type here, so just stop and
|
|
|
|
// restart the timer.
|
|
|
|
StopTimer();
|
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.
|
|
|
|
if (!mFrozen && mPresContext && mTimer) {
|
2012-07-30 14:20:58 +00:00
|
|
|
Notify(nullptr);
|
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
|
2010-02-19 17:11:40 +00:00
|
|
|
nsRefreshDriver::IsRefreshObserver(nsARefreshObserver *aObserver,
|
|
|
|
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);
|
2011-03-07 16:58:48 +00:00
|
|
|
// No need to worry about restarting our timer in precise mode if it's
|
|
|
|
// already running; that will happen automatically when it fires.
|
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);
|
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
|
|
|
}
|