2010-08-13 21:09:48 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2010-08-13 21:09:48 -07:00
|
|
|
|
|
|
|
#include "Image.h"
|
2014-05-08 11:37:39 -07:00
|
|
|
#include "nsRefreshDriver.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
2010-08-13 21:09:48 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 08:02:27 -08:00
|
|
|
namespace image {
|
2010-08-13 21:09:48 -07:00
|
|
|
|
2015-04-28 11:46:17 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Memory Reporting
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ImageMemoryCounter::ImageMemoryCounter(Image* aImage,
|
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
bool aIsUsed)
|
|
|
|
: mIsUsed(aIsUsed)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
// Extract metadata about the image.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<ImageURL> imageURL(aImage->GetURI());
|
2015-04-28 11:46:17 -07:00
|
|
|
if (imageURL) {
|
|
|
|
imageURL->GetSpec(mURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t width = 0;
|
|
|
|
int32_t height = 0;
|
|
|
|
aImage->GetWidth(&width);
|
|
|
|
aImage->GetHeight(&height);
|
|
|
|
mIntrinsicSize.SizeTo(width, height);
|
|
|
|
|
|
|
|
mType = aImage->GetType();
|
|
|
|
|
|
|
|
// Populate memory counters for source and decoded data.
|
|
|
|
mValues.SetSource(aImage->SizeOfSourceWithComputedFallback(aMallocSizeOf));
|
|
|
|
aImage->CollectSizeOfSurfaces(mSurfaces, aMallocSizeOf);
|
|
|
|
|
|
|
|
// Compute totals.
|
|
|
|
for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
|
|
|
|
mValues += surfaceCounter.Values();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Image Base Types
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-08-13 21:09:48 -07:00
|
|
|
// Constructor
|
2013-09-28 11:28:44 -07:00
|
|
|
ImageResource::ImageResource(ImageURL* aURI) :
|
2013-09-28 11:28:42 -07:00
|
|
|
mURI(aURI),
|
2012-12-19 14:24:32 -08:00
|
|
|
mInnerWindowId(0),
|
2010-09-07 17:33:02 -07:00
|
|
|
mAnimationConsumers(0),
|
2010-11-17 12:39:23 -08:00
|
|
|
mAnimationMode(kNormalAnimMode),
|
2011-10-17 07:59:28 -07:00
|
|
|
mInitialized(false),
|
|
|
|
mAnimating(false),
|
|
|
|
mError(false)
|
2015-03-25 19:36:00 -07:00
|
|
|
{ }
|
2010-08-13 21:09:48 -07:00
|
|
|
|
2015-04-28 16:32:02 -07:00
|
|
|
ImageResource::~ImageResource()
|
|
|
|
{
|
|
|
|
// Ask our ProgressTracker to drop its weak reference to us.
|
|
|
|
mProgressTracker->ResetImage();
|
|
|
|
}
|
|
|
|
|
2010-09-07 17:33:02 -07:00
|
|
|
void
|
2012-12-19 14:24:32 -08:00
|
|
|
ImageResource::IncrementAnimationConsumers()
|
2010-09-07 17:33:02 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
|
|
|
|
"with DecrementAnimationConsumers");
|
2010-09-07 17:33:02 -07:00
|
|
|
mAnimationConsumers++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-19 14:24:32 -08:00
|
|
|
ImageResource::DecrementAnimationConsumers()
|
2010-09-07 17:33:02 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
|
|
|
|
"with IncrementAnimationConsumers");
|
2015-03-25 19:36:00 -07:00
|
|
|
MOZ_ASSERT(mAnimationConsumers >= 1,
|
|
|
|
"Invalid no. of animation consumers!");
|
2010-09-07 17:33:02 -07:00
|
|
|
mAnimationConsumers--;
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:27:11 -07:00
|
|
|
nsresult
|
2012-12-19 14:24:32 -08:00
|
|
|
ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode)
|
2010-11-17 12:39:23 -08:00
|
|
|
{
|
2015-03-25 19:36:00 -07:00
|
|
|
if (mError) {
|
2010-11-17 12:39:23 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-25 19:36:00 -07:00
|
|
|
}
|
2010-11-17 12:39:23 -08:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aAnimationMode);
|
2012-10-19 13:27:11 -07:00
|
|
|
|
2010-11-17 12:39:23 -08:00
|
|
|
*aAnimationMode = mAnimationMode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:27:11 -07:00
|
|
|
nsresult
|
2012-12-19 14:24:32 -08:00
|
|
|
ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode)
|
2010-11-17 12:39:23 -08:00
|
|
|
{
|
2015-03-25 19:36:00 -07:00
|
|
|
if (mError) {
|
2010-11-17 12:39:23 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-25 19:36:00 -07:00
|
|
|
}
|
2010-11-17 12:39:23 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
|
|
|
|
aAnimationMode == kDontAnimMode ||
|
|
|
|
aAnimationMode == kLoopOnceAnimMode,
|
|
|
|
"Wrong Animation Mode is being set!");
|
2012-10-19 13:27:11 -07:00
|
|
|
|
2010-11-17 12:39:23 -08:00
|
|
|
mAnimationMode = aAnimationMode;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:37:39 -07:00
|
|
|
bool
|
|
|
|
ImageResource::HadRecentRefresh(const TimeStamp& aTime)
|
|
|
|
{
|
|
|
|
// Our threshold for "recent" is 1/2 of the default refresh-driver interval.
|
|
|
|
// This ensures that we allow for frame rates at least as fast as the
|
|
|
|
// refresh driver's default rate.
|
|
|
|
static TimeDuration recentThreshold =
|
|
|
|
TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval() / 2.0);
|
|
|
|
|
|
|
|
if (!mLastRefreshTime.IsNull() &&
|
|
|
|
aTime - mLastRefreshTime < recentThreshold) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else, we can proceed with a refresh.
|
|
|
|
// But first, update our last refresh time:
|
|
|
|
mLastRefreshTime = aTime;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-07 17:33:02 -07:00
|
|
|
void
|
2012-12-19 14:24:32 -08:00
|
|
|
ImageResource::EvaluateAnimation()
|
2010-09-07 17:33:02 -07:00
|
|
|
{
|
|
|
|
if (!mAnimating && ShouldAnimate()) {
|
|
|
|
nsresult rv = StartAnimation();
|
|
|
|
mAnimating = NS_SUCCEEDED(rv);
|
|
|
|
} else if (mAnimating && !ShouldAnimate()) {
|
|
|
|
StopAnimation();
|
|
|
|
}
|
|
|
|
}
|
2010-08-22 19:30:45 -07:00
|
|
|
|
2012-01-06 08:02:27 -08:00
|
|
|
} // namespace image
|
2010-08-13 21:09:48 -07:00
|
|
|
} // namespace mozilla
|