2010-08-14 04:09:48 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2010-08-14 04:09:48 +00:00
|
|
|
|
|
|
|
#include "Image.h"
|
2019-05-26 14:31:53 +00:00
|
|
|
|
2020-01-21 14:39:59 +00:00
|
|
|
#include "imgRequest.h"
|
2017-11-17 11:45:24 +00:00
|
|
|
#include "Layers.h" // for LayerManager
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsIObserverService.h"
|
2014-05-08 18:37:39 +00:00
|
|
|
#include "nsRefreshDriver.h"
|
2018-04-03 17:33:18 +00:00
|
|
|
#include "nsContentUtils.h"
|
2021-01-04 14:20:22 +00:00
|
|
|
#include "mozilla/gfx/Point.h"
|
|
|
|
#include "mozilla/gfx/Rect.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "mozilla/Services.h"
|
2018-03-27 23:44:49 +00:00
|
|
|
#include "mozilla/SizeOfState.h"
|
2014-05-08 18:37:39 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2017-11-17 11:45:24 +00:00
|
|
|
#include "mozilla/Tuple.h" // for Tie
|
2018-10-02 17:28:33 +00:00
|
|
|
#include "mozilla/layers/SharedSurfacesChild.h"
|
2010-08-14 04:09:48 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 16:02:27 +00:00
|
|
|
namespace image {
|
2010-08-14 04:09:48 +00:00
|
|
|
|
2015-04-28 18:46:17 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Memory Reporting
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-01-21 14:39:59 +00:00
|
|
|
ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest,
|
|
|
|
SizeOfState& aState, bool aIsUsed)
|
|
|
|
: mProgress(UINT32_MAX),
|
|
|
|
mType(UINT16_MAX),
|
|
|
|
mIsUsed(aIsUsed),
|
2020-01-21 16:32:11 +00:00
|
|
|
mHasError(false),
|
|
|
|
mValidating(false) {
|
2020-01-21 14:39:59 +00:00
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
|
|
|
// We don't have the image object yet, but we can get some information.
|
|
|
|
nsCOMPtr<nsIURI> imageURL;
|
|
|
|
nsresult rv = aRequest->GetURI(getter_AddRefs(imageURL));
|
|
|
|
if (NS_SUCCEEDED(rv) && imageURL) {
|
|
|
|
imageURL->GetSpec(mURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
mType = imgIContainer::TYPE_REQUEST;
|
|
|
|
mHasError = NS_FAILED(aRequest->GetImageErrorCode());
|
2020-01-21 16:32:11 +00:00
|
|
|
mValidating = !!aRequest->GetValidator();
|
2020-01-21 14:39:59 +00:00
|
|
|
|
|
|
|
RefPtr<ProgressTracker> tracker = aRequest->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
mProgress = tracker->GetProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:32:11 +00:00
|
|
|
ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest, Image* aImage,
|
|
|
|
SizeOfState& aState, bool aIsUsed)
|
2020-01-21 14:39:59 +00:00
|
|
|
: mProgress(UINT32_MAX),
|
|
|
|
mType(UINT16_MAX),
|
|
|
|
mIsUsed(aIsUsed),
|
2020-01-21 16:32:11 +00:00
|
|
|
mHasError(false),
|
|
|
|
mValidating(false) {
|
|
|
|
MOZ_ASSERT(aRequest);
|
2015-04-28 18:46:17 +00:00
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
// Extract metadata about the image.
|
2018-06-06 00:42:56 +00:00
|
|
|
nsCOMPtr<nsIURI> imageURL(aImage->GetURI());
|
2015-04-28 18:46:17 +00: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();
|
2020-01-21 14:39:59 +00:00
|
|
|
mHasError = aImage->HasError();
|
2020-01-21 16:32:11 +00:00
|
|
|
mValidating = !!aRequest->GetValidator();
|
2020-01-21 14:39:59 +00:00
|
|
|
|
|
|
|
RefPtr<ProgressTracker> tracker = aImage->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
mProgress = tracker->GetProgress();
|
|
|
|
}
|
2015-04-28 18:46:17 +00:00
|
|
|
|
|
|
|
// Populate memory counters for source and decoded data.
|
2017-07-28 10:10:04 +00:00
|
|
|
mValues.SetSource(aImage->SizeOfSourceWithComputedFallback(aState));
|
|
|
|
aImage->CollectSizeOfSurfaces(mSurfaces, aState.mMallocSizeOf);
|
2015-04-28 18:46:17 +00:00
|
|
|
|
|
|
|
// Compute totals.
|
|
|
|
for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
|
|
|
|
mValues += surfaceCounter.Values();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Image Base Types
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-06 00:42:56 +00:00
|
|
|
bool ImageResource::GetSpecTruncatedTo1k(nsCString& aSpec) const {
|
|
|
|
static const size_t sMaxTruncatedLength = 1024;
|
|
|
|
|
|
|
|
mURI->GetSpec(aSpec);
|
|
|
|
if (sMaxTruncatedLength >= aSpec.Length()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
aSpec.Truncate(sMaxTruncatedLength);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:20:22 +00:00
|
|
|
void ImageResource::SetCurrentImage(layers::ImageContainer* aContainer,
|
|
|
|
gfx::SourceSurface* aSurface,
|
|
|
|
const Maybe<gfx::IntRect>& aDirtyRect) {
|
2017-11-17 11:45:24 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aContainer);
|
|
|
|
|
2017-11-17 11:45:27 +00:00
|
|
|
if (!aSurface) {
|
2017-11-17 11:45:24 +00:00
|
|
|
// The OS threw out some or all of our buffer. We'll need to wait for the
|
|
|
|
// redecode (which was automatically triggered by GetFrame) to complete.
|
2017-11-17 11:45:27 +00:00
|
|
|
return;
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:45:25 +00:00
|
|
|
// |image| holds a reference to a SourceSurface which in turn holds a lock on
|
|
|
|
// the current frame's data buffer, ensuring that it doesn't get freed as
|
|
|
|
// long as the layer system keeps this ImageContainer alive.
|
2017-11-17 11:45:27 +00:00
|
|
|
RefPtr<layers::Image> image = new layers::SourceSurfaceImage(aSurface);
|
2017-11-17 11:45:25 +00:00
|
|
|
|
|
|
|
// We can share the producer ID with other containers because it is only
|
|
|
|
// used internally to validate the frames given to a particular container
|
|
|
|
// so that another object cannot add its own. Similarly the frame ID is
|
|
|
|
// only used internally to ensure it is always increasing, and skipping
|
|
|
|
// IDs from an individual container's perspective is acceptable.
|
2021-01-04 14:20:22 +00:00
|
|
|
AutoTArray<layers::ImageContainer::NonOwningImage, 1> imageList;
|
|
|
|
imageList.AppendElement(layers::ImageContainer::NonOwningImage(
|
2017-11-17 11:45:25 +00:00
|
|
|
image, TimeStamp(), mLastFrameID++, mImageProducerID));
|
|
|
|
|
2018-10-02 17:28:33 +00:00
|
|
|
if (aDirtyRect) {
|
2017-11-17 11:45:25 +00:00
|
|
|
aContainer->SetCurrentImagesInTransaction(imageList);
|
|
|
|
} else {
|
|
|
|
aContainer->SetCurrentImages(imageList);
|
|
|
|
}
|
2018-10-02 17:28:33 +00:00
|
|
|
|
2019-03-11 17:20:49 +00:00
|
|
|
// If we are animated, then we should request that the image container be
|
|
|
|
// treated as such, to avoid display list rebuilding to update frames for
|
|
|
|
// WebRender.
|
|
|
|
if (mProgressTracker->GetProgress() & FLAG_IS_ANIMATED) {
|
2018-10-02 17:28:33 +00:00
|
|
|
if (aDirtyRect) {
|
|
|
|
layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
|
|
|
|
aDirtyRect.ref());
|
|
|
|
} else {
|
2021-01-04 14:20:22 +00:00
|
|
|
gfx::IntRect dirtyRect(gfx::IntPoint(0, 0), aSurface->GetSize());
|
2018-10-02 17:28:33 +00:00
|
|
|
layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
|
|
|
|
dirtyRect);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:45:24 +00:00
|
|
|
ImgDrawResult ImageResource::GetImageContainerImpl(
|
2021-01-04 14:20:22 +00:00
|
|
|
layers::LayerManager* aManager, const gfx::IntSize& aSize,
|
2017-11-17 19:08:52 +00:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
|
2021-01-04 14:20:22 +00:00
|
|
|
layers::ImageContainer** aOutContainer) {
|
2017-11-17 11:45:24 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aManager);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
(aFlags & ~(FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST |
|
2017-11-17 11:45:26 +00:00
|
|
|
FLAG_ASYNC_NOTIFY | FLAG_HIGH_QUALITY_SCALING)) == FLAG_NONE,
|
2017-11-17 11:45:24 +00:00
|
|
|
"Unsupported flag passed to GetImageContainer");
|
|
|
|
|
2018-09-13 23:48:21 +00:00
|
|
|
ImgDrawResult drawResult;
|
2021-01-04 14:20:22 +00:00
|
|
|
gfx::IntSize size;
|
2018-09-13 23:48:21 +00:00
|
|
|
Tie(drawResult, size) = GetImageContainerSize(aManager, aSize, aFlags);
|
|
|
|
if (drawResult != ImgDrawResult::SUCCESS) {
|
|
|
|
return drawResult;
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 23:48:21 +00:00
|
|
|
MOZ_ASSERT(!size.IsEmpty());
|
|
|
|
|
2017-11-17 11:45:24 +00:00
|
|
|
if (mAnimationConsumers == 0) {
|
|
|
|
SendOnUnlockedDraw(aFlags);
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:45:28 +00:00
|
|
|
uint32_t flags = (aFlags & ~(FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST)) |
|
|
|
|
FLAG_ASYNC_NOTIFY;
|
2017-11-17 11:45:25 +00:00
|
|
|
RefPtr<layers::ImageContainer> container;
|
|
|
|
ImageContainerEntry* entry = nullptr;
|
|
|
|
int i = mImageContainers.Length() - 1;
|
|
|
|
for (; i >= 0; --i) {
|
|
|
|
entry = &mImageContainers[i];
|
2017-11-17 19:08:52 +00:00
|
|
|
if (size == entry->mSize && flags == entry->mFlags &&
|
|
|
|
aSVGContext == entry->mSVGContext) {
|
2017-11-17 11:45:25 +00:00
|
|
|
// Lack of a container is handled below.
|
2020-09-21 18:14:47 +00:00
|
|
|
container = RefPtr<layers::ImageContainer>(entry->mContainer);
|
2017-11-17 11:45:25 +00:00
|
|
|
break;
|
2021-02-03 17:00:04 +00:00
|
|
|
} else if (entry->mContainer.IsDead()) {
|
2017-11-17 11:45:25 +00:00
|
|
|
// Stop tracking if our weak pointer to the image container was freed.
|
|
|
|
mImageContainers.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 11:45:24 +00:00
|
|
|
|
2017-11-17 12:54:50 +00:00
|
|
|
if (container) {
|
2017-11-17 11:45:25 +00:00
|
|
|
switch (entry->mLastDrawResult) {
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::SUCCESS:
|
|
|
|
case ImgDrawResult::BAD_IMAGE:
|
|
|
|
case ImgDrawResult::BAD_ARGS:
|
2018-09-21 00:22:06 +00:00
|
|
|
case ImgDrawResult::NOT_SUPPORTED:
|
2018-09-13 23:48:21 +00:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::NOT_READY:
|
|
|
|
case ImgDrawResult::INCOMPLETE:
|
|
|
|
case ImgDrawResult::TEMPORARY_ERROR:
|
2017-11-17 11:45:25 +00:00
|
|
|
// Temporary conditions where we need to rerequest the frame to recover.
|
|
|
|
break;
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::WRONG_SIZE:
|
2017-11-17 11:45:25 +00:00
|
|
|
// Unused by GetFrameInternal
|
|
|
|
default:
|
2017-12-11 15:37:59 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
|
2018-09-13 23:48:21 +00:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-11-17 11:45:25 +00:00
|
|
|
}
|
2017-11-17 11:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NotifyDrawingObservers();
|
|
|
|
#endif
|
|
|
|
|
2021-01-04 14:20:22 +00:00
|
|
|
gfx::IntSize bestSize;
|
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
2017-11-17 11:45:27 +00:00
|
|
|
Tie(drawResult, bestSize, surface) = GetFrameInternal(
|
2017-11-17 19:08:52 +00:00
|
|
|
size, aSVGContext, FRAME_CURRENT, aFlags | FLAG_ASYNC_NOTIFY);
|
2017-11-17 11:45:27 +00:00
|
|
|
|
|
|
|
// The requested size might be refused by the surface cache (i.e. due to
|
|
|
|
// factor-of-2 mode). In that case we don't want to create an entry for this
|
|
|
|
// specific size, but rather re-use the entry for the substituted size.
|
|
|
|
if (bestSize != size) {
|
|
|
|
MOZ_ASSERT(!bestSize.IsEmpty());
|
|
|
|
|
|
|
|
// We can only remove the entry if we no longer have a container, because if
|
|
|
|
// there are strong references to it remaining, we need to still update it
|
|
|
|
// in UpdateImageContainer.
|
|
|
|
if (i >= 0 && !container) {
|
|
|
|
mImageContainers.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forget about the stale container, if any. This lets the entry creation
|
|
|
|
// logic do its job below, if it turns out there is no existing best entry
|
|
|
|
// or the best entry doesn't have a container.
|
|
|
|
container = nullptr;
|
|
|
|
|
|
|
|
// We need to do the entry search again for the new size. We skip pruning
|
|
|
|
// because we did this above once already, but ImageContainer is threadsafe,
|
|
|
|
// so there is a remote possibility it got freed.
|
|
|
|
i = mImageContainers.Length() - 1;
|
|
|
|
for (; i >= 0; --i) {
|
|
|
|
entry = &mImageContainers[i];
|
2017-11-17 19:08:52 +00:00
|
|
|
if (bestSize == entry->mSize && flags == entry->mFlags &&
|
|
|
|
aSVGContext == entry->mSVGContext) {
|
2020-09-21 18:14:47 +00:00
|
|
|
container = RefPtr<layers::ImageContainer>(entry->mContainer);
|
2017-11-17 11:45:27 +00:00
|
|
|
if (container) {
|
|
|
|
switch (entry->mLastDrawResult) {
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::SUCCESS:
|
|
|
|
case ImgDrawResult::BAD_IMAGE:
|
|
|
|
case ImgDrawResult::BAD_ARGS:
|
2018-09-21 00:22:06 +00:00
|
|
|
case ImgDrawResult::NOT_SUPPORTED:
|
2018-09-13 23:48:21 +00:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::NOT_READY:
|
|
|
|
case ImgDrawResult::INCOMPLETE:
|
|
|
|
case ImgDrawResult::TEMPORARY_ERROR:
|
2017-11-17 11:45:27 +00:00
|
|
|
// Temporary conditions where we need to rerequest the frame to
|
|
|
|
// recover. We have already done so!
|
|
|
|
break;
|
2017-12-11 15:37:59 +00:00
|
|
|
case ImgDrawResult::WRONG_SIZE:
|
2017-11-17 11:45:27 +00:00
|
|
|
// Unused by GetFrameInternal
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
|
2018-09-13 23:48:21 +00:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-11-17 11:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container) {
|
2017-11-17 11:45:25 +00:00
|
|
|
// We need a new ImageContainer, so create one.
|
2021-01-04 14:20:22 +00:00
|
|
|
container = layers::LayerManager::CreateImageContainer();
|
2017-11-17 11:45:25 +00:00
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
entry->mContainer = container;
|
|
|
|
} else {
|
|
|
|
entry = mImageContainers.AppendElement(
|
2017-11-17 19:08:52 +00:00
|
|
|
ImageContainerEntry(bestSize, aSVGContext, container.get(), flags));
|
2017-11-17 11:45:25 +00:00
|
|
|
}
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:28:33 +00:00
|
|
|
SetCurrentImage(container, surface, Nothing());
|
2017-11-17 11:45:27 +00:00
|
|
|
entry->mLastDrawResult = drawResult;
|
2018-09-13 23:48:21 +00:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return drawResult;
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:20:22 +00:00
|
|
|
bool ImageResource::UpdateImageContainer(
|
|
|
|
const Maybe<gfx::IntRect>& aDirtyRect) {
|
2017-11-17 11:45:24 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-11-17 11:45:25 +00:00
|
|
|
for (int i = mImageContainers.Length() - 1; i >= 0; --i) {
|
|
|
|
ImageContainerEntry& entry = mImageContainers[i];
|
2020-09-21 18:14:47 +00:00
|
|
|
RefPtr<layers::ImageContainer> container(entry.mContainer);
|
2017-11-17 11:45:25 +00:00
|
|
|
if (container) {
|
2021-01-04 14:20:22 +00:00
|
|
|
gfx::IntSize bestSize;
|
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
2017-11-17 11:45:27 +00:00
|
|
|
Tie(entry.mLastDrawResult, bestSize, surface) = GetFrameInternal(
|
2017-11-17 19:08:52 +00:00
|
|
|
entry.mSize, entry.mSVGContext, FRAME_CURRENT, entry.mFlags);
|
2017-11-17 11:45:27 +00:00
|
|
|
|
|
|
|
// It is possible that this is a factor-of-2 substitution. Since we
|
|
|
|
// managed to convert the weak reference into a strong reference, that
|
|
|
|
// means that an imagelib user still is holding onto the container. thus
|
|
|
|
// we cannot consolidate and must keep updating the duplicate container.
|
2018-10-02 17:28:33 +00:00
|
|
|
if (aDirtyRect) {
|
|
|
|
SetCurrentImage(container, surface, aDirtyRect);
|
|
|
|
} else {
|
2021-01-04 14:20:22 +00:00
|
|
|
gfx::IntRect dirtyRect(gfx::IntPoint(0, 0), bestSize);
|
2018-10-02 17:28:33 +00:00
|
|
|
SetCurrentImage(container, surface, Some(dirtyRect));
|
|
|
|
}
|
2017-11-17 11:45:25 +00:00
|
|
|
} else {
|
|
|
|
// Stop tracking if our weak pointer to the image container was freed.
|
|
|
|
mImageContainers.RemoveElementAt(i);
|
|
|
|
}
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
2018-12-17 13:31:04 +00:00
|
|
|
|
|
|
|
return !mImageContainers.IsEmpty();
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResource::ReleaseImageContainer() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-17 11:45:25 +00:00
|
|
|
mImageContainers.Clear();
|
2017-11-17 11:45:24 +00:00
|
|
|
}
|
|
|
|
|
2010-08-14 04:09:48 +00:00
|
|
|
// Constructor
|
2018-06-06 00:42:56 +00:00
|
|
|
ImageResource::ImageResource(nsIURI* aURI)
|
2013-09-28 18:28:42 +00:00
|
|
|
: mURI(aURI),
|
2012-12-19 22:24:32 +00:00
|
|
|
mInnerWindowId(0),
|
2010-09-08 00:33:02 +00:00
|
|
|
mAnimationConsumers(0),
|
2010-11-17 20:39:23 +00:00
|
|
|
mAnimationMode(kNormalAnimMode),
|
2011-10-17 14:59:28 +00:00
|
|
|
mInitialized(false),
|
|
|
|
mAnimating(false),
|
2017-11-17 11:45:23 +00:00
|
|
|
mError(false),
|
2021-01-04 14:20:22 +00:00
|
|
|
mImageProducerID(layers::ImageContainer::AllocateProducerID()),
|
2017-11-17 11:45:25 +00:00
|
|
|
mLastFrameID(0) {}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2015-04-28 23:32:02 +00:00
|
|
|
ImageResource::~ImageResource() {
|
|
|
|
// Ask our ProgressTracker to drop its weak reference to us.
|
|
|
|
mProgressTracker->ResetImage();
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:24:32 +00:00
|
|
|
void ImageResource::IncrementAnimationConsumers() {
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Main thread only to encourage serialization "
|
|
|
|
"with DecrementAnimationConsumers");
|
2010-09-08 00:33:02 +00:00
|
|
|
mAnimationConsumers++;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:24:32 +00:00
|
|
|
void ImageResource::DecrementAnimationConsumers() {
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Main thread only to encourage serialization "
|
|
|
|
"with IncrementAnimationConsumers");
|
2015-03-26 02:36:00 +00:00
|
|
|
MOZ_ASSERT(mAnimationConsumers >= 1, "Invalid no. of animation consumers!");
|
2010-09-08 00:33:02 +00:00
|
|
|
mAnimationConsumers--;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:24:32 +00:00
|
|
|
nsresult ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode) {
|
2015-03-26 02:36:00 +00:00
|
|
|
if (mError) {
|
2010-11-17 20:39:23 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-26 02:36:00 +00:00
|
|
|
}
|
2010-11-17 20:39:23 +00:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aAnimationMode);
|
2012-10-19 20:27:11 +00:00
|
|
|
|
2010-11-17 20:39:23 +00:00
|
|
|
*aAnimationMode = mAnimationMode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:24:32 +00:00
|
|
|
nsresult ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode) {
|
2015-03-26 02:36:00 +00:00
|
|
|
if (mError) {
|
2010-11-17 20:39:23 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-26 02:36:00 +00:00
|
|
|
}
|
2010-11-17 20:39:23 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
|
|
|
|
aAnimationMode == kDontAnimMode ||
|
|
|
|
aAnimationMode == kLoopOnceAnimMode,
|
|
|
|
"Wrong Animation Mode is being set!");
|
2012-10-19 20:27:11 +00:00
|
|
|
|
2010-11-17 20:39:23 +00:00
|
|
|
mAnimationMode = aAnimationMode;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:37:39 +00: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;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:24:32 +00:00
|
|
|
void ImageResource::EvaluateAnimation() {
|
2010-09-08 00:33:02 +00:00
|
|
|
if (!mAnimating && ShouldAnimate()) {
|
|
|
|
nsresult rv = StartAnimation();
|
|
|
|
mAnimating = NS_SUCCEEDED(rv);
|
|
|
|
} else if (mAnimating && !ShouldAnimate()) {
|
|
|
|
StopAnimation();
|
|
|
|
}
|
|
|
|
}
|
2010-08-23 02:30:45 +00:00
|
|
|
|
2016-11-17 06:25:53 +00:00
|
|
|
void ImageResource::SendOnUnlockedDraw(uint32_t aFlags) {
|
|
|
|
if (!mProgressTracker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(aFlags & FLAG_ASYNC_NOTIFY)) {
|
|
|
|
mProgressTracker->OnUnlockedDraw();
|
|
|
|
} else {
|
|
|
|
NotNull<RefPtr<ImageResource>> image = WrapNotNull(this);
|
2017-08-15 00:24:55 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget = mProgressTracker->GetEventTarget();
|
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
|
2017-06-12 19:34:10 +00:00
|
|
|
"image::ImageResource::SendOnUnlockedDraw", [=]() -> void {
|
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
tracker->OnUnlockedDraw();
|
|
|
|
}
|
2017-08-15 00:24:55 +00:00
|
|
|
});
|
2019-03-01 13:11:56 +00:00
|
|
|
eventTarget->Dispatch(CreateMediumHighRunnable(ev.forget()),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2016-11-17 06:25:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:45:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
void ImageResource::NotifyDrawingObservers() {
|
|
|
|
if (!mURI || !NS_IsMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-03 09:44:12 +00:00
|
|
|
if (!mURI->SchemeIs("resource") && !mURI->SchemeIs("chrome")) {
|
2017-11-17 11:45:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the image drawing for startup performance testing.
|
2018-06-06 00:42:56 +00:00
|
|
|
nsCOMPtr<nsIURI> uri = mURI;
|
2018-04-03 17:33:18 +00:00
|
|
|
nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
|
|
|
|
"image::ImageResource::NotifyDrawingObservers", [uri]() {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
|
|
|
|
if (obs) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
obs->NotifyObservers(nullptr, "image-drawing",
|
|
|
|
NS_ConvertUTF8toUTF16(spec).get());
|
|
|
|
}
|
|
|
|
}));
|
2017-11-17 11:45:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-06 16:02:27 +00:00
|
|
|
} // namespace image
|
2010-08-14 04:09:48 +00:00
|
|
|
} // namespace mozilla
|