2010-08-14 08:09:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-14 08:09:48 +04:00
|
|
|
|
|
|
|
#include "Image.h"
|
2019-05-26 17:31:53 +03:00
|
|
|
|
2020-01-21 17:39:59 +03:00
|
|
|
#include "imgRequest.h"
|
2017-11-17 14:45:24 +03:00
|
|
|
#include "Layers.h" // for LayerManager
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsIObserverService.h"
|
2014-05-08 22:37:39 +04:00
|
|
|
#include "nsRefreshDriver.h"
|
2018-04-03 20:33:18 +03:00
|
|
|
#include "nsContentUtils.h"
|
2021-01-04 17:20:22 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
|
|
|
#include "mozilla/gfx/Rect.h"
|
2021-05-20 15:31:27 +03:00
|
|
|
#include "mozilla/gfx/SourceSurfaceRawData.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/Services.h"
|
2018-03-28 02:44:49 +03:00
|
|
|
#include "mozilla/SizeOfState.h"
|
2014-05-08 22:37:39 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2017-11-17 14:45:24 +03:00
|
|
|
#include "mozilla/Tuple.h" // for Tie
|
2018-10-02 20:28:33 +03:00
|
|
|
#include "mozilla/layers/SharedSurfacesChild.h"
|
2021-05-13 19:24:06 +03:00
|
|
|
#include "SourceSurfaceBlobImage.h"
|
2010-08-14 08:09:48 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Memory Reporting
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-01-21 17:39:59 +03:00
|
|
|
ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest,
|
|
|
|
SizeOfState& aState, bool aIsUsed)
|
|
|
|
: mProgress(UINT32_MAX),
|
|
|
|
mType(UINT16_MAX),
|
|
|
|
mIsUsed(aIsUsed),
|
2020-01-21 19:32:11 +03:00
|
|
|
mHasError(false),
|
|
|
|
mValidating(false) {
|
2020-01-21 17:39:59 +03: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 19:32:11 +03:00
|
|
|
mValidating = !!aRequest->GetValidator();
|
2020-01-21 17:39:59 +03:00
|
|
|
|
|
|
|
RefPtr<ProgressTracker> tracker = aRequest->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
mProgress = tracker->GetProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:32:11 +03:00
|
|
|
ImageMemoryCounter::ImageMemoryCounter(imgRequest* aRequest, Image* aImage,
|
|
|
|
SizeOfState& aState, bool aIsUsed)
|
2020-01-21 17:39:59 +03:00
|
|
|
: mProgress(UINT32_MAX),
|
|
|
|
mType(UINT16_MAX),
|
|
|
|
mIsUsed(aIsUsed),
|
2020-01-21 19:32:11 +03:00
|
|
|
mHasError(false),
|
|
|
|
mValidating(false) {
|
|
|
|
MOZ_ASSERT(aRequest);
|
2015-04-28 21:46:17 +03:00
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
// Extract metadata about the image.
|
2018-06-06 03:42:56 +03:00
|
|
|
nsCOMPtr<nsIURI> imageURL(aImage->GetURI());
|
2015-04-28 21:46:17 +03: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 17:39:59 +03:00
|
|
|
mHasError = aImage->HasError();
|
2020-01-21 19:32:11 +03:00
|
|
|
mValidating = !!aRequest->GetValidator();
|
2020-01-21 17:39:59 +03:00
|
|
|
|
|
|
|
RefPtr<ProgressTracker> tracker = aImage->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
mProgress = tracker->GetProgress();
|
|
|
|
}
|
2015-04-28 21:46:17 +03:00
|
|
|
|
|
|
|
// Populate memory counters for source and decoded data.
|
2017-07-28 13:10:04 +03:00
|
|
|
mValues.SetSource(aImage->SizeOfSourceWithComputedFallback(aState));
|
|
|
|
aImage->CollectSizeOfSurfaces(mSurfaces, aState.mMallocSizeOf);
|
2015-04-28 21:46:17 +03:00
|
|
|
|
|
|
|
// Compute totals.
|
|
|
|
for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
|
|
|
|
mValues += surfaceCounter.Values();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Image Base Types
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-06 03:42:56 +03: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 17:20:22 +03:00
|
|
|
void ImageResource::SetCurrentImage(layers::ImageContainer* aContainer,
|
|
|
|
gfx::SourceSurface* aSurface,
|
|
|
|
const Maybe<gfx::IntRect>& aDirtyRect) {
|
2017-11-17 14:45:24 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aContainer);
|
|
|
|
|
2017-11-17 14:45:27 +03:00
|
|
|
if (!aSurface) {
|
2017-11-17 14:45:24 +03: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 14:45:27 +03:00
|
|
|
return;
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:25 +03: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 14:45:27 +03:00
|
|
|
RefPtr<layers::Image> image = new layers::SourceSurfaceImage(aSurface);
|
2017-11-17 14:45:25 +03: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 17:20:22 +03:00
|
|
|
AutoTArray<layers::ImageContainer::NonOwningImage, 1> imageList;
|
|
|
|
imageList.AppendElement(layers::ImageContainer::NonOwningImage(
|
2017-11-17 14:45:25 +03:00
|
|
|
image, TimeStamp(), mLastFrameID++, mImageProducerID));
|
|
|
|
|
2018-10-02 20:28:33 +03:00
|
|
|
if (aDirtyRect) {
|
2017-11-17 14:45:25 +03:00
|
|
|
aContainer->SetCurrentImagesInTransaction(imageList);
|
|
|
|
} else {
|
|
|
|
aContainer->SetCurrentImages(imageList);
|
|
|
|
}
|
2018-10-02 20:28:33 +03:00
|
|
|
|
2019-03-11 20:20:49 +03: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 20:28:33 +03:00
|
|
|
if (aDirtyRect) {
|
|
|
|
layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
|
|
|
|
aDirtyRect.ref());
|
|
|
|
} else {
|
2021-01-04 17:20:22 +03:00
|
|
|
gfx::IntRect dirtyRect(gfx::IntPoint(0, 0), aSurface->GetSize());
|
2018-10-02 20:28:33 +03:00
|
|
|
layers::SharedSurfacesChild::UpdateAnimation(aContainer, aSurface,
|
|
|
|
dirtyRect);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:24 +03:00
|
|
|
ImgDrawResult ImageResource::GetImageContainerImpl(
|
2021-01-04 17:20:22 +03:00
|
|
|
layers::LayerManager* aManager, const gfx::IntSize& aSize,
|
2021-05-13 19:24:07 +03:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
|
|
|
const Maybe<ImageIntRegion>& aRegion, uint32_t aFlags,
|
2021-01-04 17:20:22 +03:00
|
|
|
layers::ImageContainer** aOutContainer) {
|
2017-11-17 14:45:24 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aManager);
|
2021-05-13 19:24:06 +03:00
|
|
|
MOZ_ASSERT((aFlags &
|
|
|
|
~(FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST | FLAG_RECORD_BLOB |
|
|
|
|
FLAG_ASYNC_NOTIFY | FLAG_HIGH_QUALITY_SCALING)) == FLAG_NONE,
|
|
|
|
"Unsupported flag passed to GetImageContainer");
|
2017-11-17 14:45:24 +03:00
|
|
|
|
2018-09-14 02:48:21 +03:00
|
|
|
ImgDrawResult drawResult;
|
2021-01-04 17:20:22 +03:00
|
|
|
gfx::IntSize size;
|
2018-09-14 02:48:21 +03:00
|
|
|
Tie(drawResult, size) = GetImageContainerSize(aManager, aSize, aFlags);
|
|
|
|
if (drawResult != ImgDrawResult::SUCCESS) {
|
|
|
|
return drawResult;
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:48:21 +03:00
|
|
|
MOZ_ASSERT(!size.IsEmpty());
|
|
|
|
|
2017-11-17 14:45:24 +03:00
|
|
|
if (mAnimationConsumers == 0) {
|
|
|
|
SendOnUnlockedDraw(aFlags);
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:28 +03:00
|
|
|
uint32_t flags = (aFlags & ~(FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST)) |
|
|
|
|
FLAG_ASYNC_NOTIFY;
|
2017-11-17 14:45:25 +03:00
|
|
|
RefPtr<layers::ImageContainer> container;
|
|
|
|
ImageContainerEntry* entry = nullptr;
|
|
|
|
int i = mImageContainers.Length() - 1;
|
|
|
|
for (; i >= 0; --i) {
|
|
|
|
entry = &mImageContainers[i];
|
2017-11-17 22:08:52 +03:00
|
|
|
if (size == entry->mSize && flags == entry->mFlags &&
|
2021-05-13 19:24:07 +03:00
|
|
|
aSVGContext == entry->mSVGContext && aRegion == entry->mRegion) {
|
2017-11-17 14:45:25 +03:00
|
|
|
// Lack of a container is handled below.
|
2020-09-21 21:14:47 +03:00
|
|
|
container = RefPtr<layers::ImageContainer>(entry->mContainer);
|
2017-11-17 14:45:25 +03:00
|
|
|
break;
|
2021-02-03 20:00:04 +03:00
|
|
|
} else if (entry->mContainer.IsDead()) {
|
2017-11-17 14:45:25 +03:00
|
|
|
// Stop tracking if our weak pointer to the image container was freed.
|
|
|
|
mImageContainers.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 14:45:24 +03:00
|
|
|
|
2017-11-17 15:54:50 +03:00
|
|
|
if (container) {
|
2017-11-17 14:45:25 +03:00
|
|
|
switch (entry->mLastDrawResult) {
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::SUCCESS:
|
|
|
|
case ImgDrawResult::BAD_IMAGE:
|
|
|
|
case ImgDrawResult::BAD_ARGS:
|
2018-09-21 03:22:06 +03:00
|
|
|
case ImgDrawResult::NOT_SUPPORTED:
|
2018-09-14 02:48:21 +03:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::NOT_READY:
|
|
|
|
case ImgDrawResult::INCOMPLETE:
|
|
|
|
case ImgDrawResult::TEMPORARY_ERROR:
|
2017-11-17 14:45:25 +03:00
|
|
|
// Temporary conditions where we need to rerequest the frame to recover.
|
|
|
|
break;
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::WRONG_SIZE:
|
2017-11-17 14:45:25 +03:00
|
|
|
// Unused by GetFrameInternal
|
|
|
|
default:
|
2017-12-11 18:37:59 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled ImgDrawResult type!");
|
2018-09-14 02:48:21 +03:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-11-17 14:45:25 +03:00
|
|
|
}
|
2017-11-17 14:45:27 +03:00
|
|
|
}
|
|
|
|
|
2021-04-14 12:00:13 +03:00
|
|
|
AutoProfilerImagePaintMarker PROFILER_RAII(this);
|
2017-11-17 14:45:27 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
NotifyDrawingObservers();
|
|
|
|
#endif
|
|
|
|
|
2021-01-04 17:20:22 +03:00
|
|
|
gfx::IntSize bestSize;
|
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
2017-11-17 14:45:27 +03:00
|
|
|
Tie(drawResult, bestSize, surface) = GetFrameInternal(
|
2021-05-13 19:24:07 +03:00
|
|
|
size, aSVGContext, aRegion, FRAME_CURRENT, aFlags | FLAG_ASYNC_NOTIFY);
|
2017-11-17 14:45:27 +03: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 22:08:52 +03:00
|
|
|
if (bestSize == entry->mSize && flags == entry->mFlags &&
|
2021-05-13 19:24:07 +03:00
|
|
|
aSVGContext == entry->mSVGContext && aRegion == entry->mRegion) {
|
2020-09-21 21:14:47 +03:00
|
|
|
container = RefPtr<layers::ImageContainer>(entry->mContainer);
|
2017-11-17 14:45:27 +03:00
|
|
|
if (container) {
|
|
|
|
switch (entry->mLastDrawResult) {
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::SUCCESS:
|
|
|
|
case ImgDrawResult::BAD_IMAGE:
|
|
|
|
case ImgDrawResult::BAD_ARGS:
|
2018-09-21 03:22:06 +03:00
|
|
|
case ImgDrawResult::NOT_SUPPORTED:
|
2018-09-14 02:48:21 +03:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::NOT_READY:
|
|
|
|
case ImgDrawResult::INCOMPLETE:
|
|
|
|
case ImgDrawResult::TEMPORARY_ERROR:
|
2017-11-17 14:45:27 +03:00
|
|
|
// Temporary conditions where we need to rerequest the frame to
|
|
|
|
// recover. We have already done so!
|
|
|
|
break;
|
2017-12-11 18:37:59 +03:00
|
|
|
case ImgDrawResult::WRONG_SIZE:
|
2017-11-17 14:45:27 +03:00
|
|
|
// Unused by GetFrameInternal
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled DrawResult type!");
|
2018-09-14 02:48:21 +03:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return entry->mLastDrawResult;
|
2017-11-17 14:45:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container) {
|
2017-11-17 14:45:25 +03:00
|
|
|
// We need a new ImageContainer, so create one.
|
2021-01-04 17:20:22 +03:00
|
|
|
container = layers::LayerManager::CreateImageContainer();
|
2017-11-17 14:45:25 +03:00
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
entry->mContainer = container;
|
|
|
|
} else {
|
2021-05-13 19:24:07 +03:00
|
|
|
entry = mImageContainers.AppendElement(ImageContainerEntry(
|
|
|
|
bestSize, aSVGContext, aRegion, container.get(), flags));
|
2017-11-17 14:45:25 +03:00
|
|
|
}
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 20:28:33 +03:00
|
|
|
SetCurrentImage(container, surface, Nothing());
|
2017-11-17 14:45:27 +03:00
|
|
|
entry->mLastDrawResult = drawResult;
|
2018-09-14 02:48:21 +03:00
|
|
|
container.forget(aOutContainer);
|
|
|
|
return drawResult;
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:20:22 +03:00
|
|
|
bool ImageResource::UpdateImageContainer(
|
|
|
|
const Maybe<gfx::IntRect>& aDirtyRect) {
|
2017-11-17 14:45:24 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-11-17 14:45:25 +03:00
|
|
|
for (int i = mImageContainers.Length() - 1; i >= 0; --i) {
|
|
|
|
ImageContainerEntry& entry = mImageContainers[i];
|
2020-09-21 21:14:47 +03:00
|
|
|
RefPtr<layers::ImageContainer> container(entry.mContainer);
|
2017-11-17 14:45:25 +03:00
|
|
|
if (container) {
|
2021-05-13 19:24:06 +03:00
|
|
|
// Blob recordings should just be marked as dirty. We will regenerate the
|
|
|
|
// recording when the display list update comes around.
|
|
|
|
if (entry.mFlags & FLAG_RECORD_BLOB) {
|
|
|
|
AutoTArray<layers::ImageContainer::OwningImage, 1> images;
|
|
|
|
container->GetCurrentImages(&images);
|
|
|
|
if (images.IsEmpty()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Empty container!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfx::SourceSurface> surface =
|
|
|
|
images[0].mImage->GetAsSourceSurface();
|
|
|
|
if (!surface || surface->GetType() != gfx::SurfaceType::BLOB_IMAGE) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("No/wrong surface in container!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<SourceSurfaceBlobImage*>(surface.get())->MarkDirty();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:20:22 +03:00
|
|
|
gfx::IntSize bestSize;
|
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
2021-05-13 19:24:07 +03:00
|
|
|
Tie(entry.mLastDrawResult, bestSize, surface) =
|
|
|
|
GetFrameInternal(entry.mSize, entry.mSVGContext, entry.mRegion,
|
|
|
|
FRAME_CURRENT, entry.mFlags);
|
2017-11-17 14:45:27 +03: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 20:28:33 +03:00
|
|
|
if (aDirtyRect) {
|
|
|
|
SetCurrentImage(container, surface, aDirtyRect);
|
|
|
|
} else {
|
2021-01-04 17:20:22 +03:00
|
|
|
gfx::IntRect dirtyRect(gfx::IntPoint(0, 0), bestSize);
|
2018-10-02 20:28:33 +03:00
|
|
|
SetCurrentImage(container, surface, Some(dirtyRect));
|
|
|
|
}
|
2017-11-17 14:45:25 +03:00
|
|
|
} else {
|
|
|
|
// Stop tracking if our weak pointer to the image container was freed.
|
|
|
|
mImageContainers.RemoveElementAt(i);
|
|
|
|
}
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
2018-12-17 16:31:04 +03:00
|
|
|
|
|
|
|
return !mImageContainers.IsEmpty();
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-05-20 15:31:27 +03:00
|
|
|
void ImageResource::CollectSizeOfSurfaces(
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
for (const auto& entry : mImageContainers) {
|
|
|
|
RefPtr<layers::ImageContainer> container(entry.mContainer);
|
|
|
|
if (!container) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoTArray<layers::ImageContainer::OwningImage, 1> images;
|
|
|
|
container->GetCurrentImages(&images);
|
|
|
|
if (images.IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfx::SourceSurface> surface = images[0].mImage->GetAsSourceSurface();
|
|
|
|
if (!surface) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("No surface in container!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The surface might be wrapping another.
|
|
|
|
bool isMappedSurface = surface->GetType() == gfx::SurfaceType::DATA_MAPPED;
|
|
|
|
const gfx::SourceSurface* actualSurface =
|
|
|
|
isMappedSurface
|
|
|
|
? static_cast<gfx::SourceSurfaceMappedData*>(surface.get())
|
|
|
|
->GetScopedSurface()
|
|
|
|
: surface.get();
|
|
|
|
|
|
|
|
// Check if the surface is already in the report. Ignore if so.
|
|
|
|
bool found = false;
|
|
|
|
for (const auto& counter : aCounters) {
|
|
|
|
if (counter.Surface() == actualSurface) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The surface isn't in the report, so it isn't stored in SurfaceCache. We
|
|
|
|
// need to add our own entry here so that it will be included in the memory
|
|
|
|
// report.
|
|
|
|
gfx::SourceSurface::SizeOfInfo info;
|
|
|
|
surface->SizeOfExcludingThis(aMallocSizeOf, info);
|
|
|
|
|
|
|
|
uint32_t heapBytes = aMallocSizeOf(actualSurface);
|
|
|
|
if (isMappedSurface) {
|
|
|
|
heapBytes += aMallocSizeOf(surface.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceKey key = ContainerSurfaceKey(surface->GetSize(), entry.mSVGContext,
|
|
|
|
ToSurfaceFlags(entry.mFlags));
|
|
|
|
SurfaceMemoryCounter counter(key, actualSurface, /* aIsLocked */ false,
|
|
|
|
/* aCannotSubstitute */ false,
|
|
|
|
/* aIsFactor2 */ false, /* aFinished */ true,
|
|
|
|
SurfaceMemoryCounterType::CONTAINER);
|
|
|
|
|
|
|
|
counter.Values().SetDecodedHeap(info.mHeapBytes + heapBytes);
|
|
|
|
counter.Values().SetDecodedNonHeap(info.mNonHeapBytes);
|
|
|
|
counter.Values().SetDecodedUnknown(info.mUnknownBytes);
|
|
|
|
counter.Values().SetExternalHandles(info.mExternalHandles);
|
|
|
|
counter.Values().SetExternalId(info.mExternalId);
|
|
|
|
counter.Values().SetSurfaceTypes(info.mTypes);
|
|
|
|
|
|
|
|
aCounters.AppendElement(counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:24 +03:00
|
|
|
void ImageResource::ReleaseImageContainer() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-17 14:45:25 +03:00
|
|
|
mImageContainers.Clear();
|
2017-11-17 14:45:24 +03:00
|
|
|
}
|
|
|
|
|
2010-08-14 08:09:48 +04:00
|
|
|
// Constructor
|
2018-06-06 03:42:56 +03:00
|
|
|
ImageResource::ImageResource(nsIURI* aURI)
|
2013-09-28 22:28:42 +04:00
|
|
|
: mURI(aURI),
|
2012-12-20 02:24:32 +04:00
|
|
|
mInnerWindowId(0),
|
2010-09-08 04:33:02 +04:00
|
|
|
mAnimationConsumers(0),
|
2010-11-17 23:39:23 +03:00
|
|
|
mAnimationMode(kNormalAnimMode),
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized(false),
|
|
|
|
mAnimating(false),
|
2017-11-17 14:45:23 +03:00
|
|
|
mError(false),
|
2021-01-04 17:20:22 +03:00
|
|
|
mImageProducerID(layers::ImageContainer::AllocateProducerID()),
|
2017-11-17 14:45:25 +03:00
|
|
|
mLastFrameID(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-04-29 02:32:02 +03:00
|
|
|
ImageResource::~ImageResource() {
|
|
|
|
// Ask our ProgressTracker to drop its weak reference to us.
|
|
|
|
mProgressTracker->ResetImage();
|
|
|
|
}
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
void ImageResource::IncrementAnimationConsumers() {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Main thread only to encourage serialization "
|
|
|
|
"with DecrementAnimationConsumers");
|
2010-09-08 04:33:02 +04:00
|
|
|
mAnimationConsumers++;
|
|
|
|
}
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
void ImageResource::DecrementAnimationConsumers() {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Main thread only to encourage serialization "
|
|
|
|
"with IncrementAnimationConsumers");
|
2015-03-26 05:36:00 +03:00
|
|
|
MOZ_ASSERT(mAnimationConsumers >= 1, "Invalid no. of animation consumers!");
|
2010-09-08 04:33:02 +04:00
|
|
|
mAnimationConsumers--;
|
|
|
|
}
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
nsresult ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode) {
|
2015-03-26 05:36:00 +03:00
|
|
|
if (mError) {
|
2010-11-17 23:39:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-26 05:36:00 +03:00
|
|
|
}
|
2010-11-17 23:39:23 +03:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aAnimationMode);
|
2012-10-20 00:27:11 +04:00
|
|
|
|
2010-11-17 23:39:23 +03:00
|
|
|
*aAnimationMode = mAnimationMode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-20 02:24:32 +04:00
|
|
|
nsresult ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode) {
|
2015-03-26 05:36:00 +03:00
|
|
|
if (mError) {
|
2010-11-17 23:39:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-26 05:36:00 +03:00
|
|
|
}
|
2010-11-17 23:39:23 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
|
|
|
|
aAnimationMode == kDontAnimMode ||
|
|
|
|
aAnimationMode == kLoopOnceAnimMode,
|
|
|
|
"Wrong Animation Mode is being set!");
|
2012-10-20 00:27:11 +04:00
|
|
|
|
2010-11-17 23:39:23 +03:00
|
|
|
mAnimationMode = aAnimationMode;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:37:39 +04: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-20 02:24:32 +04:00
|
|
|
void ImageResource::EvaluateAnimation() {
|
2010-09-08 04:33:02 +04:00
|
|
|
if (!mAnimating && ShouldAnimate()) {
|
|
|
|
nsresult rv = StartAnimation();
|
|
|
|
mAnimating = NS_SUCCEEDED(rv);
|
|
|
|
} else if (mAnimating && !ShouldAnimate()) {
|
|
|
|
StopAnimation();
|
|
|
|
}
|
|
|
|
}
|
2010-08-23 06:30:45 +04:00
|
|
|
|
2016-11-17 09:25:53 +03: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 03:24:55 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget = mProgressTracker->GetEventTarget();
|
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
|
2017-06-12 22:34:10 +03:00
|
|
|
"image::ImageResource::SendOnUnlockedDraw", [=]() -> void {
|
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
tracker->OnUnlockedDraw();
|
|
|
|
}
|
2017-08-15 03:24:55 +03:00
|
|
|
});
|
2019-03-01 16:11:56 +03:00
|
|
|
eventTarget->Dispatch(CreateMediumHighRunnable(ev.forget()),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2016-11-17 09:25:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:23 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void ImageResource::NotifyDrawingObservers() {
|
|
|
|
if (!mURI || !NS_IsMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:44:12 +03:00
|
|
|
if (!mURI->SchemeIs("resource") && !mURI->SchemeIs("chrome")) {
|
2017-11-17 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the image drawing for startup performance testing.
|
2018-06-06 03:42:56 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = mURI;
|
2018-04-03 20:33:18 +03: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 14:45:23 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-14 08:09:48 +04:00
|
|
|
} // namespace mozilla
|