2010-09-09 00:40:39 +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-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
#include "VectorImage.h"
|
2011-10-15 11:33:26 +04:00
|
|
|
|
2014-02-09 12:04:37 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2010-09-09 00:40:39 +04:00
|
|
|
#include "gfxContext.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "gfxDrawable.h"
|
2013-10-22 15:38:08 +04:00
|
|
|
#include "gfxPlatform.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "gfxUtils.h"
|
2014-09-15 02:22:45 +04:00
|
|
|
#include "imgFrame.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2018-04-20 07:49:29 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2018-04-21 19:48:29 +03:00
|
|
|
#include "mozilla/dom/SVGDocument.h"
|
2014-03-05 20:10:34 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2019-04-11 22:30:48 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2018-12-26 05:35:30 +03:00
|
|
|
#include "mozilla/PendingAnimationTracker.h"
|
2019-04-06 09:02:28 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2017-11-17 22:08:52 +03:00
|
|
|
#include "mozilla/Tuple.h"
|
2010-09-09 00:40:39 +04:00
|
|
|
#include "nsIStreamListener.h"
|
2013-01-09 01:40:47 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsRect.h"
|
2014-05-08 13:53:00 +04:00
|
|
|
#include "nsString.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "nsStubDocumentObserver.h"
|
2018-08-09 12:29:01 +03:00
|
|
|
#include "SVGObserverUtils.h" // for SVGRenderingObserver
|
2017-08-10 07:14:09 +03:00
|
|
|
#include "nsWindowSizes.h"
|
2014-08-23 00:12:38 +04:00
|
|
|
#include "ImageRegion.h"
|
2016-07-02 08:20:32 +03:00
|
|
|
#include "ISurfaceProvider.h"
|
2015-07-01 04:57:03 +03:00
|
|
|
#include "LookupResult.h"
|
2013-08-25 11:19:42 +04:00
|
|
|
#include "Orientation.h"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "SVGDocumentWrapper.h"
|
2017-04-18 14:46:19 +03:00
|
|
|
#include "SVGDrawingParameters.h"
|
2013-08-30 01:18:25 +04:00
|
|
|
#include "nsIDOMEventListener.h"
|
2013-10-22 15:38:08 +04:00
|
|
|
#include "SurfaceCache.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2010-09-09 00:40:40 +04:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
namespace mozilla {
|
2011-10-15 11:33:26 +04:00
|
|
|
|
|
|
|
using namespace dom;
|
2018-06-26 00:20:54 +03:00
|
|
|
using namespace dom::SVGPreserveAspectRatio_Binding;
|
2014-02-09 12:04:37 +04:00
|
|
|
using namespace gfx;
|
2012-11-28 06:34:45 +04:00
|
|
|
using namespace layers;
|
2011-10-15 11:33:26 +04:00
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2010-09-09 00:40:40 +04:00
|
|
|
// Helper-class: SVGRootRenderingObserver
|
2018-08-09 12:29:01 +03:00
|
|
|
class SVGRootRenderingObserver final : public SVGRenderingObserver {
|
2010-09-09 00:40:40 +04:00
|
|
|
public:
|
2015-07-09 21:32:00 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2010-09-09 00:40:40 +04:00
|
|
|
SVGRootRenderingObserver(SVGDocumentWrapper* aDocWrapper,
|
|
|
|
VectorImage* aVectorImage)
|
2018-08-09 12:29:01 +03:00
|
|
|
: SVGRenderingObserver(),
|
2013-06-22 00:32:34 +04:00
|
|
|
mDocWrapper(aDocWrapper),
|
|
|
|
mVectorImage(aVectorImage),
|
|
|
|
mHonoringInvalidations(true) {
|
2013-03-01 10:44:59 +04:00
|
|
|
MOZ_ASSERT(mDocWrapper, "Need a non-null SVG document wrapper");
|
|
|
|
MOZ_ASSERT(mVectorImage, "Need a non-null VectorImage");
|
|
|
|
|
2017-09-14 11:47:19 +03:00
|
|
|
StartObserving();
|
2018-09-04 12:04:07 +03:00
|
|
|
Element* elem = GetReferencedElementWithoutObserving();
|
2013-03-01 10:44:59 +04:00
|
|
|
MOZ_ASSERT(elem, "no root SVG node for us to observe");
|
|
|
|
|
2017-08-30 17:58:31 +03:00
|
|
|
SVGObserverUtils::AddRenderingObserver(elem, this);
|
2013-03-01 10:44:59 +04:00
|
|
|
mInObserverList = true;
|
2010-09-09 00:40:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-22 00:32:34 +04:00
|
|
|
void ResumeHonoringInvalidations() { mHonoringInvalidations = true; }
|
2010-09-09 00:40:40 +04:00
|
|
|
|
|
|
|
protected:
|
2018-10-02 02:58:09 +03:00
|
|
|
virtual ~SVGRootRenderingObserver() {
|
2018-09-25 12:40:12 +03:00
|
|
|
// This needs to call our GetReferencedElementWithoutObserving override,
|
|
|
|
// so must be called here rather than in our base class's dtor.
|
2018-10-02 02:58:09 +03:00
|
|
|
StopObserving();
|
|
|
|
}
|
2015-07-09 21:32:00 +03:00
|
|
|
|
2018-09-25 12:40:12 +03:00
|
|
|
Element* GetReferencedElementWithoutObserving() final {
|
2010-09-09 00:40:40 +04:00
|
|
|
return mDocWrapper->GetRootSVGElem();
|
|
|
|
}
|
|
|
|
|
2017-09-15 12:01:34 +03:00
|
|
|
virtual void OnRenderingChange() override {
|
2018-09-04 12:04:07 +03:00
|
|
|
Element* elem = GetReferencedElementWithoutObserving();
|
2013-03-01 10:44:59 +04:00
|
|
|
MOZ_ASSERT(elem, "missing root SVG node");
|
2010-09-09 00:40:40 +04:00
|
|
|
|
2013-06-22 00:32:34 +04:00
|
|
|
if (mHonoringInvalidations && !mDocWrapper->ShouldIgnoreInvalidation()) {
|
2010-09-09 00:40:40 +04:00
|
|
|
nsIFrame* frame = elem->GetPrimaryFrame();
|
2017-11-09 05:00:48 +03:00
|
|
|
if (!frame || frame->PresShell()->IsDestroying()) {
|
2010-09-09 00:40:40 +04:00
|
|
|
// We're being destroyed. Bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-22 00:32:34 +04:00
|
|
|
// Ignore further invalidations until we draw.
|
|
|
|
mHonoringInvalidations = false;
|
|
|
|
|
2013-09-04 14:39:30 +04:00
|
|
|
mVectorImage->InvalidateObserversOnNextRefreshDriverTick();
|
2013-03-21 22:22:42 +04:00
|
|
|
}
|
2013-06-22 00:32:33 +04:00
|
|
|
|
2013-06-22 00:32:34 +04:00
|
|
|
// Our caller might've removed us from rendering-observer list.
|
|
|
|
// Add ourselves back!
|
|
|
|
if (!mInObserverList) {
|
2017-08-30 17:58:31 +03:00
|
|
|
SVGObserverUtils::AddRenderingObserver(elem, this);
|
2013-06-22 00:32:34 +04:00
|
|
|
mInObserverList = true;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private data
|
2015-10-18 08:24:48 +03:00
|
|
|
const RefPtr<SVGDocumentWrapper> mDocWrapper;
|
2013-03-01 10:44:59 +04:00
|
|
|
VectorImage* const mVectorImage; // Raw pointer because it owns me.
|
2013-06-22 00:32:34 +04:00
|
|
|
bool mHonoringInvalidations;
|
2010-09-09 00:40:40 +04:00
|
|
|
};
|
|
|
|
|
2015-07-09 21:32:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(SVGRootRenderingObserver, nsIMutationObserver)
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SVGParseCompleteListener final : public nsStubDocumentObserver {
|
2013-02-14 06:04:08 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2018-04-21 19:48:29 +03:00
|
|
|
SVGParseCompleteListener(SVGDocument* aDocument, VectorImage* aImage)
|
2013-02-14 06:04:08 +04:00
|
|
|
: mDocument(aDocument), mImage(aImage) {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mDocument, "Need an SVG document");
|
|
|
|
MOZ_ASSERT(mImage, "Need an image");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
mDocument->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
private:
|
2013-02-14 06:04:08 +04:00
|
|
|
~SVGParseCompleteListener() {
|
|
|
|
if (mDocument) {
|
|
|
|
// The document must have been destroyed before we got our event.
|
|
|
|
// Otherwise this can't happen, since documents hold strong references to
|
|
|
|
// their observers.
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
public:
|
2019-01-02 16:05:23 +03:00
|
|
|
void EndLoad(Document* aDocument) override {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(aDocument == mDocument, "Got EndLoad for wrong document?");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
// OnSVGDocumentParsed will release our owner's reference to us, so ensure
|
|
|
|
// we stick around long enough to complete our work.
|
2016-10-13 15:35:55 +03:00
|
|
|
RefPtr<SVGParseCompleteListener> kungFuDeathGrip(this);
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
mImage->OnSVGDocumentParsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mDocument, "Duplicate call to Cancel");
|
2013-02-27 01:21:07 +04:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->RemoveObserver(this);
|
|
|
|
mDocument = nullptr;
|
|
|
|
}
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-21 19:48:29 +03:00
|
|
|
RefPtr<SVGDocument> mDocument;
|
2013-03-01 10:44:59 +04:00
|
|
|
VectorImage* const mImage; // Raw pointer to owner.
|
2013-02-14 06:04:08 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SVGParseCompleteListener, nsIDocumentObserver)
|
2013-02-14 06:04:08 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SVGLoadEventListener final : public nsIDOMEventListener {
|
2013-02-14 06:04:08 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
SVGLoadEventListener(Document* aDocument, VectorImage* aImage)
|
2013-02-14 06:04:08 +04:00
|
|
|
: mDocument(aDocument), mImage(aImage) {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mDocument, "Need an SVG document");
|
|
|
|
MOZ_ASSERT(mImage, "Need an image");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
2015-04-04 22:58:00 +03:00
|
|
|
mDocument->AddEventListener(NS_LITERAL_STRING("MozSVGAsImageDocumentLoad"),
|
|
|
|
this, true, false);
|
|
|
|
mDocument->AddEventListener(NS_LITERAL_STRING("SVGAbort"), this, true,
|
|
|
|
false);
|
|
|
|
mDocument->AddEventListener(NS_LITERAL_STRING("SVGError"), this, true,
|
|
|
|
false);
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
private:
|
2013-02-14 06:04:08 +04:00
|
|
|
~SVGLoadEventListener() {
|
|
|
|
if (mDocument) {
|
|
|
|
// The document must have been destroyed before we got our event.
|
|
|
|
// Otherwise this can't happen, since documents hold strong references to
|
|
|
|
// their observers.
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
public:
|
2018-04-20 07:49:29 +03:00
|
|
|
NS_IMETHOD HandleEvent(Event* aEvent) override {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mDocument, "Need an SVG document. Received multiple events?");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
// OnSVGDocumentLoaded/OnSVGDocumentError will release our owner's reference
|
|
|
|
// to us, so ensure we stick around long enough to complete our work.
|
2016-10-13 15:35:55 +03:00
|
|
|
RefPtr<SVGLoadEventListener> kungFuDeathGrip(this);
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(eventType.EqualsLiteral("MozSVGAsImageDocumentLoad") ||
|
|
|
|
eventType.EqualsLiteral("SVGAbort") ||
|
|
|
|
eventType.EqualsLiteral("SVGError"),
|
|
|
|
"Received unexpected event");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
if (eventType.EqualsLiteral("MozSVGAsImageDocumentLoad")) {
|
|
|
|
mImage->OnSVGDocumentLoaded();
|
|
|
|
} else {
|
|
|
|
mImage->OnSVGDocumentError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mDocument, "Duplicate call to Cancel");
|
2013-02-27 01:21:07 +04:00
|
|
|
if (mDocument) {
|
2015-04-04 22:58:00 +03:00
|
|
|
mDocument->RemoveEventListener(
|
|
|
|
NS_LITERAL_STRING("MozSVGAsImageDocumentLoad"), this, true);
|
2013-02-27 01:21:07 +04:00
|
|
|
mDocument->RemoveEventListener(NS_LITERAL_STRING("SVGAbort"), this, true);
|
|
|
|
mDocument->RemoveEventListener(NS_LITERAL_STRING("SVGError"), this, true);
|
|
|
|
mDocument = nullptr;
|
|
|
|
}
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> mDocument;
|
2013-03-01 10:44:59 +04:00
|
|
|
VectorImage* const mImage; // Raw pointer to owner.
|
2013-02-14 06:04:08 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SVGLoadEventListener, nsIDOMEventListener)
|
2013-02-14 06:04:08 +04:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
// Helper-class: SVGDrawingCallback
|
|
|
|
class SVGDrawingCallback : public gfxDrawingCallback {
|
|
|
|
public:
|
|
|
|
SVGDrawingCallback(SVGDocumentWrapper* aSVGDocumentWrapper,
|
2017-01-25 11:18:13 +03:00
|
|
|
const IntSize& aViewportSize, const IntSize& aSize,
|
2014-08-23 00:12:38 +04:00
|
|
|
uint32_t aImageFlags)
|
|
|
|
: mSVGDocumentWrapper(aSVGDocumentWrapper),
|
2017-01-25 11:18:13 +03:00
|
|
|
mViewportSize(aViewportSize),
|
2014-08-23 00:12:38 +04:00
|
|
|
mSize(aSize),
|
|
|
|
mImageFlags(aImageFlags) {}
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2017-11-06 06:37:28 +03:00
|
|
|
const gfxMatrix& aTransform) override;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
|
2017-01-25 11:18:13 +03:00
|
|
|
const IntSize mViewportSize;
|
2014-08-23 00:12:38 +04:00
|
|
|
const IntSize mSize;
|
|
|
|
uint32_t mImageFlags;
|
2010-09-09 00:40:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Based loosely on nsSVGIntegrationUtils' PaintFrameCallback::operator()
|
|
|
|
bool SVGDrawingCallback::operator()(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2010-09-09 00:40:39 +04:00
|
|
|
const gfxMatrix& aTransform) {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mSVGDocumentWrapper, "need an SVGDocumentWrapper");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
// Get (& sanity-check) the helper-doc's presShell
|
2019-04-16 05:47:26 +03:00
|
|
|
RefPtr<PresShell> presShell = mSVGDocumentWrapper->GetPresShell();
|
2018-11-20 04:17:53 +03:00
|
|
|
MOZ_ASSERT(presShell, "GetPresShell returned null for an SVG image?");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2018-11-07 04:47:08 +03:00
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = presShell->GetDocument();
|
2018-11-07 04:47:08 +03:00
|
|
|
nsIURI* uri = doc ? doc->GetDocumentURI() : nullptr;
|
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"SVG Image drawing", GRAPHICS,
|
|
|
|
nsPrintfCString("%dx%d %s", mSize.width, mSize.height,
|
|
|
|
uri ? uri->GetSpecOrDefault().get() : "N/A"));
|
|
|
|
#endif
|
|
|
|
|
2011-01-24 21:32:34 +03:00
|
|
|
gfxContextAutoSaveRestore contextRestorer(aContext);
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
// Clip to aFillRect so that we don't paint outside.
|
|
|
|
aContext->NewPath();
|
|
|
|
aContext->Rectangle(aFillRect);
|
|
|
|
aContext->Clip();
|
|
|
|
|
2014-07-11 11:06:39 +04:00
|
|
|
gfxMatrix matrix = aTransform;
|
|
|
|
if (!matrix.Invert()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-11 05:14:09 +03:00
|
|
|
aContext->SetMatrixDouble(
|
|
|
|
aContext->CurrentMatrixDouble().PreMultiply(matrix).PreScale(
|
|
|
|
double(mSize.width) / mViewportSize.width,
|
|
|
|
double(mSize.height) / mViewportSize.height));
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
nsPresContext* presContext = presShell->GetPresContext();
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(presContext, "pres shell w/out pres context");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2017-01-25 11:18:13 +03:00
|
|
|
nsRect svgRect(0, 0, presContext->DevPixelsToAppUnits(mViewportSize.width),
|
|
|
|
presContext->DevPixelsToAppUnits(mViewportSize.height));
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2019-04-25 08:04:23 +03:00
|
|
|
RenderDocumentFlags renderDocFlags =
|
|
|
|
RenderDocumentFlags::IgnoreViewportScrolling;
|
2010-09-09 00:40:39 +04:00
|
|
|
if (!(mImageFlags & imgIContainer::FLAG_SYNC_DECODE)) {
|
2019-04-25 08:04:23 +03:00
|
|
|
renderDocFlags |= RenderDocumentFlags::AsyncDecodeImages;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
presShell->RenderDocument(svgRect, renderDocFlags,
|
|
|
|
NS_RGBA(0, 0, 0, 0), // transparent
|
|
|
|
aContext);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
class MOZ_STACK_CLASS AutoRestoreSVGState final {
|
|
|
|
public:
|
|
|
|
AutoRestoreSVGState(const SVGDrawingParameters& aParams,
|
|
|
|
SVGDocumentWrapper* aSVGDocumentWrapper, bool& aIsDrawing,
|
|
|
|
bool aContextPaint)
|
|
|
|
: mIsDrawing(aIsDrawing)
|
|
|
|
// Apply any 'preserveAspectRatio' override (if specified) to the root
|
|
|
|
// element:
|
|
|
|
,
|
|
|
|
mPAR(aParams.svgContext, aSVGDocumentWrapper->GetRootSVGElem())
|
|
|
|
// Set the animation time:
|
|
|
|
,
|
|
|
|
mTime(aSVGDocumentWrapper->GetRootSVGElem(), aParams.animationTime) {
|
|
|
|
MOZ_ASSERT(!aIsDrawing);
|
2018-04-21 19:48:29 +03:00
|
|
|
MOZ_ASSERT(aSVGDocumentWrapper->GetDocument());
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
aIsDrawing = true;
|
|
|
|
|
|
|
|
// Set context paint (if specified) on the document:
|
|
|
|
if (aContextPaint) {
|
2018-04-21 19:48:29 +03:00
|
|
|
MOZ_ASSERT(aParams.svgContext->GetContextPaint());
|
|
|
|
mContextPaint.emplace(*aParams.svgContext->GetContextPaint(),
|
|
|
|
*aSVGDocumentWrapper->GetDocument());
|
2017-12-02 02:50:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoRestore<bool> mIsDrawing;
|
|
|
|
AutoPreserveAspectRatioOverride mPAR;
|
|
|
|
AutoSVGTimeSetRestore mTime;
|
|
|
|
Maybe<AutoSetRestoreSVGContextPaint> mContextPaint;
|
|
|
|
};
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
// Implement VectorImage's nsISupports-inherited methods
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(VectorImage, imgIContainer, nsIStreamListener,
|
|
|
|
nsIRequestObserver)
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructor / Destructor
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
VectorImage::VectorImage(nsIURI* aURI /* = nullptr */)
|
2018-04-13 16:01:28 +03:00
|
|
|
: ImageResource(aURI), // invoke superclass's constructor
|
|
|
|
mLockCount(0),
|
|
|
|
mIsInitialized(false),
|
2018-06-14 08:21:37 +03:00
|
|
|
mDiscardable(false),
|
2018-04-13 16:01:28 +03:00
|
|
|
mIsFullyLoaded(false),
|
|
|
|
mIsDrawing(false),
|
|
|
|
mHaveAnimations(false),
|
|
|
|
mHasPendingInvalidation(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
VectorImage::~VectorImage() {
|
2013-02-26 05:40:13 +04:00
|
|
|
CancelAllListeners();
|
2014-11-27 00:22:10 +03:00
|
|
|
SurfaceCache::RemoveImage(ImageKey(this));
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Methods inherited from Image.h
|
|
|
|
|
2013-02-13 07:00:03 +04:00
|
|
|
nsresult VectorImage::Init(const char* aMimeType, uint32_t aFlags) {
|
2010-09-09 00:40:39 +04:00
|
|
|
// We don't support re-initialization
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mIsInitialized) {
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2013-04-06 01:15:05 +04:00
|
|
|
MOZ_ASSERT(!mIsFullyLoaded && !mHaveAnimations && !mError,
|
2013-03-01 10:45:00 +04:00
|
|
|
"Flags unexpectedly set before initialization");
|
|
|
|
MOZ_ASSERT(!strcmp(aMimeType, IMAGE_SVG_XML), "Unexpected mimetype");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2015-03-28 06:17:23 +03:00
|
|
|
mDiscardable = !!(aFlags & INIT_FLAG_DISCARDABLE);
|
|
|
|
|
|
|
|
// Lock this image's surfaces in the SurfaceCache if we're not discardable.
|
|
|
|
if (!mDiscardable) {
|
|
|
|
mLockCount++;
|
|
|
|
SurfaceCache::LockImage(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsInitialized = true;
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:10:04 +03:00
|
|
|
size_t VectorImage::SizeOfSourceWithComputedFallback(
|
|
|
|
SizeOfState& aState) const {
|
2016-10-26 19:46:00 +03:00
|
|
|
if (!mSVGDocumentWrapper) {
|
|
|
|
return 0; // No document, so no memory used for the document.
|
|
|
|
}
|
|
|
|
|
2018-04-21 19:48:29 +03:00
|
|
|
SVGDocument* doc = mSVGDocumentWrapper->GetDocument();
|
2014-11-26 13:56:28 +03:00
|
|
|
if (!doc) {
|
2014-11-27 05:00:15 +03:00
|
|
|
return 0; // No document, so no memory used for the document.
|
2014-11-26 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:10:04 +03:00
|
|
|
nsWindowSizes windowSizes(aState);
|
2017-08-10 07:13:22 +03:00
|
|
|
doc->DocAddSizeOfIncludingThis(windowSizes);
|
2014-11-27 05:00:15 +03:00
|
|
|
|
|
|
|
if (windowSizes.getTotalSize() == 0) {
|
|
|
|
// MallocSizeOf fails on this platform. Because we also use this method for
|
|
|
|
// determining the size of cache entries, we need to return something
|
|
|
|
// reasonable here. Unfortunately, there's no way to estimate the document's
|
|
|
|
// size accurately, so we just use a constant value of 100KB, which will
|
|
|
|
// generally underestimate the true size.
|
|
|
|
return 100 * 1024;
|
2014-11-26 13:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return windowSizes.getTotalSize();
|
2014-11-26 12:37:57 +03:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
void VectorImage::CollectSizeOfSurfaces(
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters, aMallocSizeOf);
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
|
|
|
|
2012-12-18 02:05:18 +04:00
|
|
|
nsresult VectorImage::OnImageDataComplete(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
2013-02-14 06:41:10 +04:00
|
|
|
nsresult aStatus, bool aLastPart) {
|
|
|
|
// Call our internal OnStopRequest method, which only talks to our embedded
|
2014-11-15 07:10:47 +03:00
|
|
|
// SVG document. This won't have any effect on our ProgressTracker.
|
2019-02-28 02:41:31 +03:00
|
|
|
nsresult finalStatus = OnStopRequest(aRequest, aStatus);
|
2013-02-14 06:41:10 +04:00
|
|
|
|
|
|
|
// Give precedence to Necko failure codes.
|
2015-04-04 22:58:00 +03:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2013-02-14 06:41:10 +04:00
|
|
|
finalStatus = aStatus;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2015-06-19 00:48:41 +03:00
|
|
|
Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
|
2013-02-14 06:41:10 +04:00
|
|
|
|
2015-06-19 00:48:41 +03:00
|
|
|
if (mIsFullyLoaded || mError) {
|
|
|
|
// Our document is loaded, so we're ready to notify now.
|
|
|
|
mProgressTracker->SyncNotifyProgress(loadProgress);
|
|
|
|
} else {
|
|
|
|
// Record our progress so far; we'll actually send the notifications in
|
|
|
|
// OnSVGDocumentLoaded or OnSVGDocumentError.
|
|
|
|
mLoadProgress = Some(loadProgress);
|
2013-02-12 04:34:20 +04:00
|
|
|
}
|
2015-06-19 00:48:41 +03:00
|
|
|
|
2013-02-14 06:41:10 +04:00
|
|
|
return finalStatus;
|
2012-12-18 02:05:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult VectorImage::OnImageDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aInStr,
|
|
|
|
uint64_t aSourceOffset,
|
|
|
|
uint32_t aCount) {
|
2019-02-28 02:42:27 +03:00
|
|
|
return OnDataAvailable(aRequest, aInStr, aSourceOffset, aCount);
|
2012-12-18 02:05:18 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
nsresult VectorImage::StartAnimation() {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError) {
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
mSVGDocumentWrapper->StartAnimation();
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult VectorImage::StopAnimation() {
|
2013-09-20 16:12:10 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mError) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mIsFullyLoaded && mHaveAnimations,
|
|
|
|
"Should not have been animating!");
|
|
|
|
|
|
|
|
mSVGDocumentWrapper->StopAnimation();
|
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2013-09-20 16:12:10 +04:00
|
|
|
mAnimating = false;
|
|
|
|
return rv;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2010-11-17 23:39:23 +03:00
|
|
|
bool VectorImage::ShouldAnimate() {
|
2012-12-20 02:24:32 +04:00
|
|
|
return ImageResource::ShouldAnimate() && mIsFullyLoaded && mHaveAnimations;
|
2010-11-17 23:39:23 +03:00
|
|
|
}
|
|
|
|
|
2013-05-18 00:57:20 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-10 19:00:31 +04:00
|
|
|
VectorImage::SetAnimationStartTime(const TimeStamp& aTime) {
|
2013-05-18 00:57:20 +04:00
|
|
|
// We don't care about animation start time.
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// imgIContainer methods
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
VectorImage::GetWidth(int32_t* aWidth) {
|
2010-09-09 00:40:39 +04:00
|
|
|
if (mError || !mIsFullyLoaded) {
|
2015-09-19 01:33:43 +03:00
|
|
|
// XXXdholbert Technically we should leave outparam untouched when we
|
|
|
|
// fail. But since many callers don't check for failure, we set it to 0 on
|
|
|
|
// failure, for sane/predictable results.
|
|
|
|
*aWidth = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SVGSVGElement* rootElem = mSVGDocumentWrapper->GetRootSVGElem();
|
|
|
|
MOZ_ASSERT(rootElem,
|
|
|
|
"Should have a root SVG elem, since we finished "
|
|
|
|
"loading without errors");
|
|
|
|
int32_t rootElemWidth = rootElem->GetIntrinsicWidth();
|
|
|
|
if (rootElemWidth < 0) {
|
|
|
|
*aWidth = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
2015-09-19 01:33:43 +03:00
|
|
|
*aWidth = rootElemWidth;
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2017-03-22 16:05:36 +03:00
|
|
|
//******************************************************************************
|
|
|
|
nsresult VectorImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) const {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:58:44 +03:00
|
|
|
//******************************************************************************
|
|
|
|
size_t VectorImage::GetNativeSizesLength() const { return 0; }
|
|
|
|
|
2011-11-10 01:39:15 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-10 19:00:31 +04:00
|
|
|
VectorImage::RequestRefresh(const TimeStamp& aTime) {
|
2014-05-08 22:37:39 +04:00
|
|
|
if (HadRecentRefresh(aTime)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:58:50 +03:00
|
|
|
PendingAnimationTracker* tracker =
|
|
|
|
mSVGDocumentWrapper->GetDocument()->GetPendingAnimationTracker();
|
|
|
|
if (tracker && ShouldAnimate()) {
|
|
|
|
tracker->TriggerPendingAnimationsOnNextTick(aTime);
|
|
|
|
}
|
|
|
|
|
2013-05-18 00:57:26 +04:00
|
|
|
EvaluateAnimation();
|
2013-09-04 14:39:30 +04:00
|
|
|
|
2014-05-01 20:31:54 +04:00
|
|
|
mSVGDocumentWrapper->TickRefreshDriver();
|
|
|
|
|
2013-11-21 05:21:50 +04:00
|
|
|
if (mHasPendingInvalidation) {
|
2016-10-13 15:35:55 +03:00
|
|
|
SendInvalidationNotifications();
|
2013-11-21 05:21:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VectorImage::SendInvalidationNotifications() {
|
|
|
|
// Animated images don't send out invalidation notifications as soon as
|
|
|
|
// they're generated. Instead, InvalidateObserversOnNextRefreshDriverTick
|
|
|
|
// records that there are pending invalidations and then returns immediately.
|
|
|
|
// The notifications are actually sent from RequestRefresh(). We send these
|
|
|
|
// notifications there to ensure that there is actually a document observing
|
|
|
|
// us. Otherwise, the notifications are just wasted effort.
|
|
|
|
//
|
2019-01-03 20:55:06 +03:00
|
|
|
// Non-animated images post an event to call this method from
|
2013-11-21 05:21:50 +04:00
|
|
|
// InvalidateObserversOnNextRefreshDriverTick, because RequestRefresh is never
|
|
|
|
// called for them. Ordinarily this isn't needed, since we send out
|
|
|
|
// invalidation notifications in OnSVGDocumentLoaded, but in rare cases the
|
|
|
|
// SVG document may not be 100% ready to render at that time. In those cases
|
|
|
|
// we would miss the subsequent invalidations if we didn't send out the
|
2019-01-03 20:55:06 +03:00
|
|
|
// notifications indirectly in |InvalidateObservers...|.
|
2013-11-21 05:21:50 +04:00
|
|
|
|
2019-01-03 20:55:06 +03:00
|
|
|
MOZ_ASSERT(mHasPendingInvalidation);
|
|
|
|
mHasPendingInvalidation = false;
|
2018-12-17 16:31:04 +03:00
|
|
|
SurfaceCache::RemoveImage(ImageKey(this));
|
|
|
|
|
|
|
|
if (UpdateImageContainer(Nothing())) {
|
|
|
|
// If we have image containers, that means we probably won't get a Draw call
|
|
|
|
// from the owner since they are using the container. We must assume all
|
|
|
|
// invalidations need to be handled.
|
|
|
|
MOZ_ASSERT(mRenderingObserver, "Should have a rendering observer by now");
|
|
|
|
mRenderingObserver->ResumeHonoringInvalidations();
|
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgressTracker) {
|
2014-11-18 01:29:56 +03:00
|
|
|
mProgressTracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE,
|
2015-04-21 18:04:57 +03:00
|
|
|
GetMaxSizedIntRect());
|
2013-09-04 14:39:30 +04:00
|
|
|
}
|
2011-11-10 01:39:15 +04:00
|
|
|
}
|
|
|
|
|
2015-04-21 18:04:57 +03:00
|
|
|
NS_IMETHODIMP_(IntRect)
|
|
|
|
VectorImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
|
2014-06-27 03:02:04 +04:00
|
|
|
return aRect;
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
VectorImage::GetHeight(int32_t* aHeight) {
|
2010-09-09 00:40:39 +04:00
|
|
|
if (mError || !mIsFullyLoaded) {
|
2015-09-19 01:33:43 +03:00
|
|
|
// XXXdholbert Technically we should leave outparam untouched when we
|
|
|
|
// fail. But since many callers don't check for failure, we set it to 0 on
|
|
|
|
// failure, for sane/predictable results.
|
|
|
|
*aHeight = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SVGSVGElement* rootElem = mSVGDocumentWrapper->GetRootSVGElem();
|
|
|
|
MOZ_ASSERT(rootElem,
|
|
|
|
"Should have a root SVG elem, since we finished "
|
|
|
|
"loading without errors");
|
|
|
|
int32_t rootElemHeight = rootElem->GetIntrinsicHeight();
|
|
|
|
if (rootElemHeight < 0) {
|
|
|
|
*aHeight = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
2015-09-19 01:33:43 +03:00
|
|
|
*aHeight = rootElemHeight;
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-11 07:38:34 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::GetIntrinsicSize(nsSize* aSize) {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError || !mIsFullyLoaded) {
|
2013-01-11 07:38:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-01-11 07:38:34 +04:00
|
|
|
|
2013-03-01 00:22:46 +04:00
|
|
|
nsIFrame* rootFrame = mSVGDocumentWrapper->GetRootLayoutFrame();
|
2015-04-04 22:58:00 +03:00
|
|
|
if (!rootFrame) {
|
2013-07-23 00:42:05 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-07-23 00:42:05 +04:00
|
|
|
|
2013-01-11 07:38:34 +04:00
|
|
|
*aSize = nsSize(-1, -1);
|
2013-10-01 01:26:04 +04:00
|
|
|
IntrinsicSize rfSize = rootFrame->GetIntrinsicSize();
|
2015-04-04 22:58:00 +03:00
|
|
|
if (rfSize.width.GetUnit() == eStyleUnit_Coord) {
|
2013-01-11 07:38:34 +04:00
|
|
|
aSize->width = rfSize.width.GetCoordValue();
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
|
|
|
if (rfSize.height.GetUnit() == eStyleUnit_Coord) {
|
2013-01-11 07:38:34 +04:00
|
|
|
aSize->height = rfSize.height.GetCoordValue();
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-01-11 07:38:34 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::GetIntrinsicRatio(nsSize* aRatio) {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError || !mIsFullyLoaded) {
|
2013-01-11 07:38:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-01-11 07:38:34 +04:00
|
|
|
|
2013-03-01 00:22:46 +04:00
|
|
|
nsIFrame* rootFrame = mSVGDocumentWrapper->GetRootLayoutFrame();
|
2015-04-04 22:58:00 +03:00
|
|
|
if (!rootFrame) {
|
2013-07-23 00:42:05 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-07-23 00:42:05 +04:00
|
|
|
|
2013-01-11 07:38:34 +04:00
|
|
|
*aRatio = rootFrame->GetIntrinsicRatio();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-25 11:19:42 +04:00
|
|
|
NS_IMETHODIMP_(Orientation)
|
|
|
|
VectorImage::GetOrientation() { return Orientation(); }
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
VectorImage::GetType(uint16_t* aType) {
|
2010-12-07 21:40:28 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
|
|
|
|
2015-03-19 17:12:52 +03:00
|
|
|
*aType = imgIContainer::TYPE_VECTOR;
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:13:55 +03:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::GetProducerId(uint32_t* aId) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aId);
|
|
|
|
|
|
|
|
*aId = ImageResource::GetImageProducerId();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
VectorImage::GetAnimated(bool* aAnimated) {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError || !mIsFullyLoaded) {
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
*aAnimated = mSVGDocumentWrapper->IsAnimated();
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 21:42:20 +04:00
|
|
|
//******************************************************************************
|
|
|
|
int32_t VectorImage::GetFirstFrameDelay() {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError) {
|
2013-05-17 21:42:20 +04:00
|
|
|
return -1;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-05-17 21:42:20 +04:00
|
|
|
|
2015-04-04 22:58:00 +03:00
|
|
|
if (!mSVGDocumentWrapper->IsAnimated()) {
|
2013-05-17 21:42:20 +04:00
|
|
|
return -1;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-05-17 21:42:20 +04:00
|
|
|
|
|
|
|
// We don't really have a frame delay, so just pretend that we constantly
|
|
|
|
// need updates.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-05 02:22:30 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2016-08-23 05:15:38 +03:00
|
|
|
VectorImage::WillDrawOpaqueNow() {
|
2013-02-05 02:22:30 +04:00
|
|
|
return false; // In general, SVG content is not opaque.
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2015-06-17 17:00:52 +03:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
2015-09-19 23:34:09 +03:00
|
|
|
VectorImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
|
2016-05-08 13:17:46 +03:00
|
|
|
if (mError) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-10-12 23:00:30 +04:00
|
|
|
// Look up height & width
|
|
|
|
// ----------------------
|
2015-01-20 22:12:54 +03:00
|
|
|
SVGSVGElement* svgElem = mSVGDocumentWrapper->GetRootSVGElem();
|
|
|
|
MOZ_ASSERT(svgElem,
|
|
|
|
"Should have a root SVG elem, since we finished "
|
|
|
|
"loading without errors");
|
|
|
|
nsIntSize imageIntSize(svgElem->GetIntrinsicWidth(),
|
|
|
|
svgElem->GetIntrinsicHeight());
|
2015-04-04 22:58:00 +03:00
|
|
|
|
2015-01-20 22:12:54 +03:00
|
|
|
if (imageIntSize.IsEmpty()) {
|
|
|
|
// We'll get here if our SVG doc has a percent-valued or negative width or
|
|
|
|
// height.
|
2013-12-13 12:34:24 +04:00
|
|
|
return nullptr;
|
2010-10-12 23:00:30 +04:00
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:09 +03:00
|
|
|
return GetFrameAtSize(imageIntSize, aWhichFrame, aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
|
|
|
VectorImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
|
|
|
|
uint32_t aFlags) {
|
2017-12-02 02:50:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
NotifyDrawingObservers();
|
|
|
|
#endif
|
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
auto result = GetFrameInternal(aSize, Nothing(), aWhichFrame, aFlags);
|
2018-02-12 15:59:58 +03:00
|
|
|
return Get<2>(result).forget();
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 18:37:59 +03:00
|
|
|
Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>>
|
2017-11-17 22:08:52 +03:00
|
|
|
VectorImage::GetFrameInternal(const IntSize& aSize,
|
2017-11-17 22:08:52 +03:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2017-11-17 22:08:52 +03:00
|
|
|
uint32_t aWhichFrame, uint32_t aFlags) {
|
2015-09-19 23:34:09 +03:00
|
|
|
MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
|
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return MakeTuple(ImgDrawResult::BAD_ARGS, aSize, RefPtr<SourceSurface>());
|
2015-09-19 23:34:09 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
if (mError) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return MakeTuple(ImgDrawResult::BAD_IMAGE, aSize, RefPtr<SourceSurface>());
|
2015-09-19 23:34:09 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
if (!mIsFullyLoaded) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return MakeTuple(ImgDrawResult::NOT_READY, aSize, RefPtr<SourceSurface>());
|
2015-09-19 23:34:09 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
RefPtr<SourceSurface> sourceSurface;
|
|
|
|
IntSize decodeSize;
|
|
|
|
Tie(sourceSurface, decodeSize) =
|
2017-11-17 22:08:52 +03:00
|
|
|
LookupCachedSurface(aSize, aSVGContext, aFlags);
|
2017-11-17 22:08:51 +03:00
|
|
|
if (sourceSurface) {
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(ImgDrawResult::SUCCESS, decodeSize,
|
|
|
|
std::move(sourceSurface));
|
2017-11-17 22:08:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsDrawing) {
|
|
|
|
NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(ImgDrawResult::TEMPORARY_ERROR, decodeSize,
|
2017-11-17 22:08:52 +03:00
|
|
|
RefPtr<SourceSurface>());
|
2017-11-17 22:08:51 +03:00
|
|
|
}
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
// By using a null gfxContext, we ensure that we will always attempt to
|
|
|
|
// create a surface, even if we aren't capable of caching it (e.g. due to our
|
|
|
|
// flags, having an animation, etc). Otherwise CreateSurface will assume that
|
|
|
|
// the caller is capable of drawing directly to its own draw target if we
|
|
|
|
// cannot cache.
|
2018-09-21 03:22:06 +03:00
|
|
|
SVGDrawingParameters params(
|
|
|
|
nullptr, decodeSize, aSize, ImageRegion::Create(decodeSize),
|
2018-12-21 08:37:58 +03:00
|
|
|
SamplingFilter::POINT, aSVGContext,
|
|
|
|
mSVGDocumentWrapper->GetCurrentTimeAsFloat(), aFlags, 1.0);
|
2010-10-12 23:00:30 +04:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
bool didCache; // Was the surface put into the cache?
|
2017-11-17 22:08:52 +03:00
|
|
|
bool contextPaint = aSVGContext && aSVGContext->GetContextPaint();
|
2017-12-02 02:50:07 +03:00
|
|
|
|
|
|
|
AutoRestoreSVGState autoRestore(params, mSVGDocumentWrapper, mIsDrawing,
|
|
|
|
contextPaint);
|
|
|
|
|
|
|
|
RefPtr<gfxDrawable> svgDrawable = CreateSVGDrawable(params);
|
|
|
|
RefPtr<SourceSurface> surface = CreateSurface(params, svgDrawable, didCache);
|
2017-11-17 22:08:52 +03:00
|
|
|
if (!surface) {
|
2017-12-02 02:50:07 +03:00
|
|
|
MOZ_ASSERT(!didCache);
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(ImgDrawResult::TEMPORARY_ERROR, decodeSize,
|
2017-12-02 02:50:07 +03:00
|
|
|
RefPtr<SourceSurface>());
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
SendFrameComplete(didCache, params.flags);
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(ImgDrawResult::SUCCESS, decodeSize, std::move(surface));
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:26 +03:00
|
|
|
//******************************************************************************
|
2017-11-17 22:08:52 +03:00
|
|
|
Tuple<ImgDrawResult, IntSize> VectorImage::GetImageContainerSize(
|
|
|
|
LayerManager* aManager, const IntSize& aSize, uint32_t aFlags) {
|
2018-09-14 02:48:21 +03:00
|
|
|
if (mError) {
|
|
|
|
return MakeTuple(ImgDrawResult::BAD_IMAGE, IntSize(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIsFullyLoaded) {
|
|
|
|
return MakeTuple(ImgDrawResult::NOT_READY, IntSize(0, 0));
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:48:21 +03:00
|
|
|
if (mHaveAnimations ||
|
|
|
|
aManager->GetBackendType() != LayersBackend::LAYERS_WR) {
|
|
|
|
return MakeTuple(ImgDrawResult::NOT_SUPPORTED, IntSize(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to check if the size is too big since we only support
|
|
|
|
// WebRender backends.
|
|
|
|
if (aSize.IsEmpty()) {
|
|
|
|
return MakeTuple(ImgDrawResult::BAD_ARGS, IntSize(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeTuple(ImgDrawResult::SUCCESS, aSize);
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
2015-05-13 10:23:44 +03:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
VectorImage::IsImageContainerAvailable(LayerManager* aManager,
|
|
|
|
uint32_t aFlags) {
|
2018-03-14 21:19:13 +03:00
|
|
|
if (mError || !mIsFullyLoaded || mHaveAnimations ||
|
|
|
|
aManager->GetBackendType() != LayersBackend::LAYERS_WR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-05-13 10:23:44 +03:00
|
|
|
}
|
|
|
|
|
2013-03-06 03:39:48 +04:00
|
|
|
//******************************************************************************
|
2015-03-18 05:40:16 +03:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
|
|
|
|
VectorImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
|
2018-03-14 21:19:13 +03:00
|
|
|
MOZ_ASSERT(aManager->GetBackendType() != LayersBackend::LAYERS_WR,
|
|
|
|
"WebRender should always use GetImageContainerAvailableAtSize!");
|
2015-03-18 05:40:16 +03:00
|
|
|
return nullptr;
|
2013-03-06 03:39:48 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:45:26 +03:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
VectorImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
|
|
|
|
const IntSize& aSize,
|
|
|
|
uint32_t aFlags) {
|
2018-03-14 21:19:13 +03:00
|
|
|
// Since we only support image containers with WebRender, and it can handle
|
|
|
|
// textures larger than the hw max texture size, we don't need to check aSize.
|
2019-04-11 22:30:48 +03:00
|
|
|
return !aSize.IsEmpty() && IsImageContainerAvailable(aManager, aFlags);
|
2017-11-17 14:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2018-09-14 02:48:21 +03:00
|
|
|
NS_IMETHODIMP_(ImgDrawResult)
|
|
|
|
VectorImage::GetImageContainerAtSize(layers::LayerManager* aManager,
|
|
|
|
const gfx::IntSize& aSize,
|
2017-11-17 22:08:52 +03:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2018-09-14 02:48:21 +03:00
|
|
|
uint32_t aFlags,
|
|
|
|
layers::ImageContainer** aOutContainer) {
|
2017-11-17 22:08:52 +03:00
|
|
|
Maybe<SVGImageContext> newSVGContext;
|
|
|
|
MaybeRestrictSVGContext(newSVGContext, aSVGContext, aFlags);
|
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
// The aspect ratio flag was already handled as part of the SVG context
|
|
|
|
// restriction above.
|
|
|
|
uint32_t flags = aFlags & ~(FLAG_FORCE_PRESERVEASPECTRATIO_NONE);
|
2017-11-17 22:08:52 +03:00
|
|
|
return GetImageContainerImpl(aManager, aSize,
|
|
|
|
newSVGContext ? newSVGContext : aSVGContext,
|
2018-09-14 02:48:21 +03:00
|
|
|
flags, aOutContainer);
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VectorImage::MaybeRestrictSVGContext(
|
|
|
|
Maybe<SVGImageContext>& aNewSVGContext,
|
|
|
|
const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags) {
|
|
|
|
bool overridePAR =
|
|
|
|
(aFlags & FLAG_FORCE_PRESERVEASPECTRATIO_NONE) && aSVGContext;
|
|
|
|
|
|
|
|
bool haveContextPaint = aSVGContext && aSVGContext->GetContextPaint();
|
|
|
|
bool blockContextPaint = false;
|
|
|
|
if (haveContextPaint) {
|
2018-06-06 03:42:56 +03:00
|
|
|
blockContextPaint = !SVGContextPaint::IsAllowedForImageFromURI(mURI);
|
2017-11-17 22:08:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (overridePAR || blockContextPaint) {
|
|
|
|
// The key that we create for the image surface cache must match the way
|
|
|
|
// that the image will be painted, so we need to initialize a new matching
|
|
|
|
// SVGImageContext here in order to generate the correct key.
|
|
|
|
|
|
|
|
aNewSVGContext = aSVGContext; // copy
|
|
|
|
|
|
|
|
if (overridePAR) {
|
|
|
|
// The SVGImageContext must take account of the preserveAspectRatio
|
|
|
|
// overide:
|
|
|
|
MOZ_ASSERT(!aSVGContext->GetPreserveAspectRatio(),
|
|
|
|
"FLAG_FORCE_PRESERVEASPECTRATIO_NONE is not expected if a "
|
|
|
|
"preserveAspectRatio override is supplied");
|
|
|
|
Maybe<SVGPreserveAspectRatio> aspectRatio = Some(SVGPreserveAspectRatio(
|
|
|
|
SVG_PRESERVEASPECTRATIO_NONE, SVG_MEETORSLICE_UNKNOWN));
|
|
|
|
aNewSVGContext->SetPreserveAspectRatio(aspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockContextPaint) {
|
|
|
|
// The SVGImageContext must not include context paint if the image is
|
|
|
|
// not allowed to use it:
|
|
|
|
aNewSVGContext->ClearContextPaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return haveContextPaint && !blockContextPaint;
|
2017-11-17 14:45:26 +03:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
2017-12-11 18:37:59 +03:00
|
|
|
NS_IMETHODIMP_(ImgDrawResult)
|
2010-09-09 00:40:39 +04:00
|
|
|
VectorImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
|
2014-08-23 00:12:38 +04:00
|
|
|
const ImageRegion& aRegion, uint32_t aWhichFrame,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
|
2017-01-03 08:53:22 +03:00
|
|
|
float aOpacity) {
|
2015-02-05 00:50:56 +03:00
|
|
|
if (aWhichFrame > FRAME_MAX_VALUE) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::BAD_ARGS;
|
2015-02-05 00:50:56 +03:00
|
|
|
}
|
2013-03-11 05:43:37 +04:00
|
|
|
|
2015-02-05 00:50:56 +03:00
|
|
|
if (!aContext) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::BAD_ARGS;
|
2015-02-05 00:50:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mError) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::BAD_IMAGE;
|
2015-02-05 00:50:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIsFullyLoaded) {
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::NOT_READY;
|
2015-02-05 00:50:56 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2016-11-17 09:25:53 +03:00
|
|
|
if (mAnimationConsumers == 0) {
|
|
|
|
SendOnUnlockedDraw(aFlags);
|
2013-04-29 23:10:16 +04:00
|
|
|
}
|
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
// We should bypass the cache when:
|
|
|
|
// - We are using a DrawTargetRecording because we prefer the drawing commands
|
|
|
|
// in general to the rasterized surface. This allows blob images to avoid
|
|
|
|
// rasterized SVGs with WebRender.
|
2019-04-11 22:30:48 +03:00
|
|
|
// - The size exceeds what we are willing to cache as a rasterized surface.
|
|
|
|
// We don't do this for WebRender because the performance of the fallback
|
|
|
|
// path is quite bad and upscaling the SVG from the clamped size is better
|
|
|
|
// than bringing the browser to a crawl.
|
2018-09-21 03:22:06 +03:00
|
|
|
if (aContext->GetDrawTarget()->GetBackendType() == BackendType::RECORDING ||
|
2019-04-11 22:30:48 +03:00
|
|
|
(!gfxVars::UseWebRender() &&
|
|
|
|
aSize != SurfaceCache::ClampVectorSize(aSize))) {
|
2017-11-24 02:14:52 +03:00
|
|
|
aFlags |= FLAG_BYPASS_SURFACE_CACHE;
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:49:44 +03:00
|
|
|
MOZ_ASSERT(!(aFlags & FLAG_FORCE_PRESERVEASPECTRATIO_NONE) ||
|
|
|
|
(aSVGContext && aSVGContext->GetViewportSize()),
|
|
|
|
"Viewport size is required when using "
|
|
|
|
"FLAG_FORCE_PRESERVEASPECTRATIO_NONE");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2017-03-27 14:49:44 +03:00
|
|
|
float animTime = (aWhichFrame == FRAME_FIRST)
|
|
|
|
? 0.0f
|
2018-12-21 08:37:58 +03:00
|
|
|
: mSVGDocumentWrapper->GetCurrentTimeAsFloat();
|
2016-03-08 10:54:13 +03:00
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
Maybe<SVGImageContext> newSVGContext;
|
|
|
|
bool contextPaint =
|
|
|
|
MaybeRestrictSVGContext(newSVGContext, aSVGContext, aFlags);
|
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
SVGDrawingParameters params(aContext, aSize, aSize, aRegion, aSamplingFilter,
|
2017-02-06 14:21:49 +03:00
|
|
|
newSVGContext ? newSVGContext : aSVGContext,
|
|
|
|
animTime, aFlags, aOpacity);
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2016-08-18 07:46:25 +03:00
|
|
|
// If we have an prerasterized version of this image that matches the
|
|
|
|
// drawing parameters, use that.
|
2018-09-21 03:22:06 +03:00
|
|
|
RefPtr<SourceSurface> sourceSurface;
|
|
|
|
Tie(sourceSurface, params.size) =
|
2017-11-17 22:08:51 +03:00
|
|
|
LookupCachedSurface(aSize, params.svgContext, aFlags);
|
|
|
|
if (sourceSurface) {
|
2018-09-21 03:22:06 +03:00
|
|
|
RefPtr<gfxDrawable> drawable =
|
|
|
|
new gfxSurfaceDrawable(sourceSurface, params.size);
|
|
|
|
Show(drawable, params);
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::SUCCESS;
|
2014-06-26 11:40:12 +04:00
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2017-03-27 14:49:44 +03:00
|
|
|
// else, we need to paint the image:
|
|
|
|
|
|
|
|
if (mIsDrawing) {
|
|
|
|
NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::TEMPORARY_ERROR;
|
2017-03-27 14:49:44 +03:00
|
|
|
}
|
2017-11-17 22:08:51 +03:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
AutoRestoreSVGState autoRestore(params, mSVGDocumentWrapper, mIsDrawing,
|
|
|
|
contextPaint);
|
|
|
|
|
|
|
|
bool didCache; // Was the surface put into the cache?
|
|
|
|
RefPtr<gfxDrawable> svgDrawable = CreateSVGDrawable(params);
|
|
|
|
sourceSurface = CreateSurface(params, svgDrawable, didCache);
|
|
|
|
if (!sourceSurface) {
|
|
|
|
MOZ_ASSERT(!didCache);
|
|
|
|
Show(svgDrawable, params);
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::SUCCESS;
|
2017-12-02 02:50:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfxDrawable> drawable =
|
|
|
|
new gfxSurfaceDrawable(sourceSurface, params.size);
|
|
|
|
Show(drawable, params);
|
|
|
|
SendFrameComplete(didCache, params.flags);
|
2017-12-11 18:37:59 +03:00
|
|
|
return ImgDrawResult::SUCCESS;
|
2017-11-17 22:08:51 +03:00
|
|
|
}
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
already_AddRefed<gfxDrawable> VectorImage::CreateSVGDrawable(
|
|
|
|
const SVGDrawingParameters& aParams) {
|
|
|
|
RefPtr<gfxDrawingCallback> cb = new SVGDrawingCallback(
|
|
|
|
mSVGDocumentWrapper, aParams.viewportSize, aParams.size, aParams.flags);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
RefPtr<gfxDrawable> svgDrawable = new gfxCallbackDrawable(cb, aParams.size);
|
|
|
|
return svgDrawable.forget();
|
2016-08-18 07:46:25 +03:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:08:51 +03:00
|
|
|
Tuple<RefPtr<SourceSurface>, IntSize> VectorImage::LookupCachedSurface(
|
|
|
|
const IntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
|
|
|
|
uint32_t aFlags) {
|
2016-08-18 07:46:25 +03:00
|
|
|
// If we're not allowed to use a cached surface, don't attempt a lookup.
|
2017-11-17 22:08:51 +03:00
|
|
|
if (aFlags & FLAG_BYPASS_SURFACE_CACHE) {
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(RefPtr<SourceSurface>(), aSize);
|
2016-08-18 07:46:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 10:06:41 +03:00
|
|
|
// We don't do any caching if we have animation, so don't bother with a lookup
|
|
|
|
// in this case either.
|
|
|
|
if (mHaveAnimations) {
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(RefPtr<SourceSurface>(), aSize);
|
2016-08-18 10:06:41 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
LookupResult result(MatchType::NOT_FOUND);
|
|
|
|
SurfaceKey surfaceKey = VectorSurfaceKey(aSize, aSVGContext);
|
|
|
|
if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
|
2018-10-13 08:31:03 +03:00
|
|
|
result = SurfaceCache::Lookup(ImageKey(this), surfaceKey,
|
|
|
|
/* aMarkUsed = */ true);
|
2018-09-21 03:22:06 +03:00
|
|
|
} else {
|
2018-10-13 08:31:03 +03:00
|
|
|
result = SurfaceCache::LookupBestMatch(ImageKey(this), surfaceKey,
|
|
|
|
/* aMarkUsed = */ true);
|
2018-09-21 03:22:06 +03:00
|
|
|
}
|
2017-11-17 22:08:52 +03:00
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
IntSize rasterSize =
|
|
|
|
result.SuggestedSize().IsEmpty() ? aSize : result.SuggestedSize();
|
|
|
|
MOZ_ASSERT(result.Type() != MatchType::SUBSTITUTE_BECAUSE_PENDING);
|
|
|
|
if (!result || result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND) {
|
|
|
|
// No matching surface, or the OS freed the volatile buffer.
|
|
|
|
return MakeTuple(RefPtr<SourceSurface>(), rasterSize);
|
2016-08-18 07:46:25 +03:00
|
|
|
}
|
2015-02-04 03:38:39 +03:00
|
|
|
|
2016-08-18 07:46:25 +03:00
|
|
|
RefPtr<SourceSurface> sourceSurface = result.Surface()->GetSourceSurface();
|
|
|
|
if (!sourceSurface) {
|
|
|
|
// Something went wrong. (Probably a GPU driver crash or device reset.)
|
|
|
|
// Attempt to recover.
|
2015-02-04 03:38:39 +03:00
|
|
|
RecoverFromLossOfSurfaces();
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(RefPtr<SourceSurface>(), rasterSize);
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2018-09-21 03:22:06 +03:00
|
|
|
return MakeTuple(std::move(sourceSurface), rasterSize);
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
|
|
|
|
2017-11-17 22:08:52 +03:00
|
|
|
already_AddRefed<SourceSurface> VectorImage::CreateSurface(
|
2017-12-02 02:50:07 +03:00
|
|
|
const SVGDrawingParameters& aParams, gfxDrawable* aSVGDrawable,
|
|
|
|
bool& aWillCache) {
|
|
|
|
MOZ_ASSERT(mIsDrawing);
|
|
|
|
|
2013-10-22 15:38:08 +04:00
|
|
|
mSVGDocumentWrapper->UpdateViewportBounds(aParams.viewportSize);
|
|
|
|
mSVGDocumentWrapper->FlushImageTransformInvalidation();
|
2013-10-21 22:39:33 +04:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
// Determine whether or not we should put the surface to be created into
|
|
|
|
// the cache. If we fail, we need to reset this to false to let the caller
|
|
|
|
// know nothing was put in the cache.
|
|
|
|
aWillCache = !(aParams.flags & FLAG_BYPASS_SURFACE_CACHE) &&
|
|
|
|
// Refuse to cache animated images:
|
|
|
|
// XXX(seth): We may remove this restriction in bug 922893.
|
|
|
|
!mHaveAnimations &&
|
|
|
|
// The image is too big to fit in the cache:
|
|
|
|
SurfaceCache::CanHold(aParams.size);
|
|
|
|
|
|
|
|
// If we weren't given a context, then we know we just want the rasterized
|
|
|
|
// surface. We will create the frame below but only insert it into the cache
|
|
|
|
// if we actually need to.
|
|
|
|
if (!aWillCache && aParams.context) {
|
2017-11-17 22:08:52 +03:00
|
|
|
return nullptr;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2015-03-28 06:17:23 +03:00
|
|
|
// We're about to rerasterize, which may mean that some of the previous
|
|
|
|
// surfaces we've rasterized aren't useful anymore. We can allow them to
|
|
|
|
// expire from the cache by unlocking them here, and then sending out an
|
|
|
|
// invalidation. If this image is locked, any surfaces that are still useful
|
|
|
|
// will become locked again when Draw touches them, and the remainder will
|
|
|
|
// eventually expire.
|
2017-12-02 02:50:07 +03:00
|
|
|
if (aWillCache) {
|
|
|
|
SurfaceCache::UnlockEntries(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no context, the default backend is fine.
|
|
|
|
BackendType backend =
|
|
|
|
aParams.context ? aParams.context->GetDrawTarget()->GetBackendType()
|
|
|
|
: gfxPlatform::GetPlatform()->GetDefaultContentBackend();
|
2015-03-28 06:17:23 +03:00
|
|
|
|
2014-09-15 02:22:45 +04:00
|
|
|
// Try to create an imgFrame, initializing the surface it contains by drawing
|
|
|
|
// our gfxDrawable into it. (We use FILTER_NEAREST since we never scale here.)
|
2017-10-20 10:25:33 +03:00
|
|
|
auto frame = MakeNotNull<RefPtr<imgFrame>>();
|
2017-12-02 02:50:07 +03:00
|
|
|
nsresult rv = frame->InitWithDrawable(
|
|
|
|
aSVGDrawable, aParams.size, SurfaceFormat::B8G8R8A8,
|
2019-01-18 09:56:43 +03:00
|
|
|
SamplingFilter::POINT, aParams.flags, backend,
|
|
|
|
aParams.context ? aParams.context->GetDrawTarget() : nullptr);
|
2013-10-21 22:39:33 +04:00
|
|
|
|
2014-09-15 02:22:45 +04:00
|
|
|
// If we couldn't create the frame, it was probably because it would end
|
2013-10-22 15:38:08 +04:00
|
|
|
// up way too big. Generally it also wouldn't fit in the cache, but the prefs
|
2013-11-13 08:32:29 +04:00
|
|
|
// could be set such that the cache isn't the limiting factor.
|
2015-04-04 22:58:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-12-02 02:50:07 +03:00
|
|
|
aWillCache = false;
|
2017-11-17 22:08:52 +03:00
|
|
|
return nullptr;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2014-09-15 02:22:45 +04:00
|
|
|
// Take a strong reference to the frame's surface and make sure it hasn't
|
|
|
|
// already been purged by the operating system.
|
2016-08-18 22:43:16 +03:00
|
|
|
RefPtr<SourceSurface> surface = frame->GetSourceSurface();
|
2015-04-04 22:58:00 +03:00
|
|
|
if (!surface) {
|
2017-12-02 02:50:07 +03:00
|
|
|
aWillCache = false;
|
2017-11-17 22:08:52 +03:00
|
|
|
return nullptr;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2014-07-29 04:05:40 +04:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
// We created the frame, but only because we had no context to draw to
|
|
|
|
// directly. All the caller wants is the surface in this case.
|
|
|
|
if (!aWillCache) {
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-15 02:22:45 +04:00
|
|
|
// Attempt to cache the frame.
|
2016-08-25 00:50:49 +03:00
|
|
|
SurfaceKey surfaceKey = VectorSurfaceKey(aParams.size, aParams.svgContext);
|
2016-07-02 08:20:32 +03:00
|
|
|
NotNull<RefPtr<ISurfaceProvider>> provider =
|
2017-10-20 10:25:33 +03:00
|
|
|
MakeNotNull<SimpleSurfaceProvider*>(ImageKey(this), surfaceKey, frame);
|
2018-09-21 03:22:06 +03:00
|
|
|
|
|
|
|
if (SurfaceCache::Insert(provider) == InsertOutcome::SUCCESS &&
|
|
|
|
aParams.size != aParams.drawSize) {
|
|
|
|
// We created a new surface that wasn't the size we requested, which means
|
|
|
|
// we entered factor-of-2 mode. We should purge any surfaces we no longer
|
|
|
|
// need rather than waiting for the cache to expire them.
|
|
|
|
SurfaceCache::PruneImage(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
return surface.forget();
|
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2017-12-02 02:50:07 +03:00
|
|
|
void VectorImage::SendFrameComplete(bool aDidCache, uint32_t aFlags) {
|
|
|
|
// If the cache was not updated, we have nothing to do.
|
|
|
|
if (!aDidCache) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-28 06:17:23 +03:00
|
|
|
|
|
|
|
// Send out an invalidation so that surfaces that are still in use get
|
|
|
|
// re-locked. See the discussion of the UnlockSurfaces call above.
|
2017-12-02 02:50:07 +03:00
|
|
|
if (!(aFlags & FLAG_ASYNC_NOTIFY)) {
|
2016-11-19 00:59:04 +03:00
|
|
|
mProgressTracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE,
|
|
|
|
GetMaxSizedIntRect());
|
|
|
|
} else {
|
|
|
|
NotNull<RefPtr<VectorImage>> image = WrapNotNull(this);
|
2019-03-01 16:11:56 +03:00
|
|
|
NS_DispatchToMainThread(CreateMediumHighRunnable(NS_NewRunnableFunction(
|
2017-02-11 09:11:48 +03:00
|
|
|
"ProgressTracker::SyncNotifyProgress", [=]() -> void {
|
2016-11-19 00:59:04 +03:00
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
tracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE,
|
|
|
|
GetMaxSizedIntRect());
|
|
|
|
}
|
2019-03-01 16:11:56 +03:00
|
|
|
})));
|
2016-11-19 00:59:04 +03:00
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:43:25 +04:00
|
|
|
void VectorImage::Show(gfxDrawable* aDrawable,
|
|
|
|
const SVGDrawingParameters& aParams) {
|
2018-09-21 03:22:06 +03:00
|
|
|
// The surface size may differ from the size at which we wish to draw. As
|
|
|
|
// such, we may need to adjust the context/region to take this into account.
|
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aParams.context);
|
|
|
|
ImageRegion region(aParams.region);
|
|
|
|
if (aParams.drawSize != aParams.size) {
|
|
|
|
gfx::Size scale(double(aParams.drawSize.width) / aParams.size.width,
|
|
|
|
double(aParams.drawSize.height) / aParams.size.height);
|
|
|
|
aParams.context->Multiply(gfxMatrix::Scaling(scale.width, scale.height));
|
|
|
|
region.Scale(1.0 / scale.width, 1.0 / scale.height);
|
|
|
|
}
|
|
|
|
|
2013-10-22 15:38:08 +04:00
|
|
|
MOZ_ASSERT(aDrawable, "Should have a gfxDrawable by now");
|
|
|
|
gfxUtils::DrawPixelSnapped(aParams.context, aDrawable,
|
2017-06-28 18:41:21 +03:00
|
|
|
SizeDouble(aParams.size), region,
|
2014-04-20 05:28:38 +04:00
|
|
|
SurfaceFormat::B8G8R8A8, aParams.samplingFilter,
|
2018-06-08 19:38:13 +03:00
|
|
|
aParams.flags, aParams.opacity, false);
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2017-06-15 01:11:48 +03:00
|
|
|
#ifdef DEBUG
|
2017-11-17 22:08:52 +03:00
|
|
|
NotifyDrawingObservers();
|
2017-06-15 01:11:48 +03:00
|
|
|
#endif
|
|
|
|
|
2013-06-22 00:32:34 +04:00
|
|
|
MOZ_ASSERT(mRenderingObserver, "Should have a rendering observer by now");
|
|
|
|
mRenderingObserver->ResumeHonoringInvalidations();
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2015-02-04 03:38:39 +03:00
|
|
|
void VectorImage::RecoverFromLossOfSurfaces() {
|
|
|
|
NS_WARNING("An imgFrame became invalid. Attempting to recover...");
|
|
|
|
|
|
|
|
// Discard all existing frames, since they're probably all now invalid.
|
|
|
|
SurfaceCache::RemoveImage(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
2012-10-05 00:02:15 +04:00
|
|
|
NS_IMETHODIMP
|
2016-11-26 10:56:26 +03:00
|
|
|
VectorImage::StartDecoding(uint32_t aFlags) {
|
2012-10-05 00:02:15 +04:00
|
|
|
// Nothing to do for SVG images
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:07:45 +03:00
|
|
|
bool VectorImage::StartDecodingWithResult(uint32_t aFlags) {
|
|
|
|
// SVG images are ready to draw when they are loaded
|
|
|
|
return mIsFullyLoaded;
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
bool VectorImage::RequestDecodeWithResult(uint32_t aFlags) {
|
|
|
|
// SVG images are ready to draw when they are loaded
|
|
|
|
return mIsFullyLoaded;
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:02:13 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::RequestDecodeForSize(const nsIntSize& aSize, uint32_t aFlags) {
|
|
|
|
// Nothing to do for SVG images, though in theory we could rasterize to the
|
|
|
|
// provided size ahead of time if we supported off-main-thread SVG
|
|
|
|
// rasterization...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
2015-08-05 02:17:36 +03:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::LockImage() {
|
2015-03-28 06:17:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mError) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLockCount++;
|
|
|
|
|
|
|
|
if (mLockCount == 1) {
|
|
|
|
// Lock this image's surfaces in the SurfaceCache.
|
|
|
|
SurfaceCache::LockImage(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2015-08-05 02:17:36 +03:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::UnlockImage() {
|
2015-03-28 06:17:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mError) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLockCount == 0) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Calling UnlockImage with a zero lock count");
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLockCount--;
|
|
|
|
|
|
|
|
if (mLockCount == 0) {
|
|
|
|
// Unlock this image's surfaces in the SurfaceCache.
|
|
|
|
SurfaceCache::UnlockImage(ImageKey(this));
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2012-03-10 10:29:28 +04:00
|
|
|
//******************************************************************************
|
2015-08-05 02:17:36 +03:00
|
|
|
|
2012-03-10 10:29:28 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::RequestDiscard() {
|
2015-03-28 06:17:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mDiscardable && mLockCount == 0) {
|
|
|
|
SurfaceCache::RemoveImage(ImageKey(this));
|
|
|
|
mProgressTracker->OnDiscard();
|
|
|
|
}
|
|
|
|
|
2012-03-10 10:29:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:06:06 +03:00
|
|
|
void VectorImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
|
2015-03-28 06:17:23 +03:00
|
|
|
MOZ_ASSERT(mProgressTracker);
|
|
|
|
|
2017-05-10 02:19:37 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod("ProgressTracker::OnDiscard",
|
|
|
|
mProgressTracker,
|
|
|
|
&ProgressTracker::OnDiscard));
|
2015-03-28 06:17:23 +03:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::ResetAnimation() {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError) {
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
if (!mIsFullyLoaded || !mHaveAnimations) {
|
|
|
|
return NS_OK; // There are no animations to be reset.
|
|
|
|
}
|
|
|
|
|
|
|
|
mSVGDocumentWrapper->ResetAnimation();
|
2011-02-03 09:36:09 +03:00
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 02:58:20 +04:00
|
|
|
NS_IMETHODIMP_(float)
|
|
|
|
VectorImage::GetFrameIndex(uint32_t aWhichFrame) {
|
|
|
|
MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
|
2018-12-21 08:37:58 +03:00
|
|
|
return aWhichFrame == FRAME_FIRST
|
|
|
|
? 0.0f
|
|
|
|
: mSVGDocumentWrapper->GetCurrentTimeAsFloat();
|
2013-04-26 02:58:20 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// nsIRequestObserver methods
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
VectorImage::OnStartRequest(nsIRequest* aRequest) {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(!mSVGDocumentWrapper,
|
|
|
|
"Repeated call to OnStartRequest -- can this happen?");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
|
|
|
mSVGDocumentWrapper = new SVGDocumentWrapper();
|
2019-02-28 02:41:04 +03:00
|
|
|
nsresult rv = mSVGDocumentWrapper->OnStartRequest(aRequest);
|
2010-09-09 00:40:39 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mSVGDocumentWrapper = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mError = true;
|
2013-02-14 06:04:08 +04:00
|
|
|
return rv;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 06:04:08 +04:00
|
|
|
// Create a listener to wait until the SVG document is fully loaded, which
|
|
|
|
// will signal that this image is ready to render. Certain error conditions
|
|
|
|
// will prevent us from ever getting this notification, so we also create a
|
|
|
|
// listener that waits for parsing to complete and cancels the
|
|
|
|
// SVGLoadEventListener if needed. The listeners are automatically attached
|
|
|
|
// to the document by their constructors.
|
2018-04-21 19:48:29 +03:00
|
|
|
SVGDocument* document = mSVGDocumentWrapper->GetDocument();
|
2013-02-14 06:04:08 +04:00
|
|
|
mLoadEventListener = new SVGLoadEventListener(document, this);
|
|
|
|
mParseCompleteListener = new SVGParseCompleteListener(document, this);
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2019-03-13 12:19:06 +03:00
|
|
|
VectorImage::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError) {
|
2010-09-09 00:40:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2019-02-28 02:41:31 +03:00
|
|
|
return mSVGDocumentWrapper->OnStopRequest(aRequest, aStatus);
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VectorImage::OnSVGDocumentParsed() {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mParseCompleteListener, "Should have the parse complete listener");
|
|
|
|
MOZ_ASSERT(mLoadEventListener, "Should have the load event listener");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
if (!mSVGDocumentWrapper->GetRootSVGElem()) {
|
|
|
|
// This is an invalid SVG document. It may have failed to parse, or it may
|
|
|
|
// be missing the <svg> root element, or the <svg> root element may not
|
|
|
|
// declare the correct namespace. In any of these cases, we'll never be
|
2015-04-04 22:58:00 +03:00
|
|
|
// notified that the SVG finished loading, so we need to treat this as an
|
|
|
|
// error.
|
2013-02-14 06:04:08 +04:00
|
|
|
OnSVGDocumentError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VectorImage::CancelAllListeners() {
|
|
|
|
if (mParseCompleteListener) {
|
|
|
|
mParseCompleteListener->Cancel();
|
|
|
|
mParseCompleteListener = nullptr;
|
|
|
|
}
|
|
|
|
if (mLoadEventListener) {
|
|
|
|
mLoadEventListener->Cancel();
|
|
|
|
mLoadEventListener = nullptr;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void VectorImage::OnSVGDocumentLoaded() {
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(mSVGDocumentWrapper->GetRootSVGElem(),
|
|
|
|
"Should have parsed successfully");
|
|
|
|
MOZ_ASSERT(!mIsFullyLoaded && !mHaveAnimations,
|
|
|
|
"These flags shouldn't get set until OnSVGDocumentLoaded. "
|
|
|
|
"Duplicate calls to OnSVGDocumentLoaded?");
|
2013-02-14 06:04:08 +04:00
|
|
|
|
|
|
|
CancelAllListeners();
|
|
|
|
|
|
|
|
// XXX Flushing is wasteful if embedding frame hasn't had initial reflow.
|
|
|
|
mSVGDocumentWrapper->FlushLayout();
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsFullyLoaded = true;
|
2010-09-09 00:40:39 +04:00
|
|
|
mHaveAnimations = mSVGDocumentWrapper->IsAnimated();
|
|
|
|
|
2013-02-14 06:04:08 +04:00
|
|
|
// Start listening to our image for rendering updates.
|
2010-09-09 00:40:40 +04:00
|
|
|
mRenderingObserver = new SVGRootRenderingObserver(mSVGDocumentWrapper, this);
|
|
|
|
|
2016-10-13 15:35:55 +03:00
|
|
|
// ProgressTracker::SyncNotifyProgress may release us, so ensure we
|
|
|
|
// stick around long enough to complete our work.
|
|
|
|
RefPtr<VectorImage> kungFuDeathGrip(this);
|
|
|
|
|
2013-02-14 06:04:08 +04:00
|
|
|
// Tell *our* observers that we're done loading.
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgressTracker) {
|
2015-06-19 00:48:41 +03:00
|
|
|
Progress progress = FLAG_SIZE_AVAILABLE | FLAG_HAS_TRANSPARENCY |
|
2017-11-01 13:59:10 +03:00
|
|
|
FLAG_FRAME_COMPLETE | FLAG_DECODE_COMPLETE;
|
2015-06-19 00:48:41 +03:00
|
|
|
|
2016-06-28 10:05:24 +03:00
|
|
|
if (mHaveAnimations) {
|
|
|
|
progress |= FLAG_IS_ANIMATED;
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:48:41 +03:00
|
|
|
// Merge in any saved progress from OnImageDataComplete.
|
|
|
|
if (mLoadProgress) {
|
|
|
|
progress |= *mLoadProgress;
|
|
|
|
mLoadProgress = Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
mProgressTracker->SyncNotifyProgress(progress, GetMaxSizedIntRect());
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
2013-02-14 06:04:08 +04:00
|
|
|
|
2010-12-20 03:45:29 +03:00
|
|
|
EvaluateAnimation();
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2013-02-14 06:04:08 +04:00
|
|
|
void VectorImage::OnSVGDocumentError() {
|
|
|
|
CancelAllListeners();
|
|
|
|
|
|
|
|
mError = true;
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgressTracker) {
|
2015-06-19 00:48:41 +03:00
|
|
|
// Notify observers about the error and unblock page load.
|
2017-11-01 13:59:10 +03:00
|
|
|
Progress progress = FLAG_HAS_ERROR;
|
2015-06-19 00:48:41 +03:00
|
|
|
|
|
|
|
// Merge in any saved progress from OnImageDataComplete.
|
|
|
|
if (mLoadProgress) {
|
|
|
|
progress |= *mLoadProgress;
|
|
|
|
mLoadProgress = Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
mProgressTracker->SyncNotifyProgress(progress);
|
2013-02-14 06:04:08 +04:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// nsIStreamListener method
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2019-03-13 12:19:06 +03:00
|
|
|
VectorImage::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInStr,
|
|
|
|
uint64_t aSourceOffset, uint32_t aCount) {
|
2015-04-04 22:58:00 +03:00
|
|
|
if (mError) {
|
2011-05-23 06:22:13 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2011-05-23 06:22:13 +04:00
|
|
|
|
2019-03-13 12:19:06 +03:00
|
|
|
return mSVGDocumentWrapper->OnDataAvailable(aRequest, aInStr, aSourceOffset,
|
|
|
|
aCount);
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:40 +04:00
|
|
|
// --------------------------
|
|
|
|
// Invalidation helper method
|
|
|
|
|
2013-09-04 14:39:30 +04:00
|
|
|
void VectorImage::InvalidateObserversOnNextRefreshDriverTick() {
|
2019-01-03 20:55:06 +03:00
|
|
|
if (mHasPendingInvalidation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mHasPendingInvalidation = true;
|
|
|
|
|
|
|
|
// Animated images can wait for the refresh tick.
|
2013-11-21 05:21:50 +04:00
|
|
|
if (mHaveAnimations) {
|
2019-01-03 20:55:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-animated images won't get the refresh tick, so we should just send an
|
|
|
|
// invalidation outside the current execution context. We need to defer
|
|
|
|
// because the layout tree is in the middle of invalidation, and the tree
|
|
|
|
// state needs to be consistent. Specifically only some of the frames have
|
|
|
|
// had the NS_FRAME_DESCENDANT_NEEDS_PAINT and/or NS_FRAME_NEEDS_PAINT bits
|
|
|
|
// set by InvalidateFrameInternal in layout/generic/nsFrame.cpp. These bits
|
|
|
|
// get cleared when we repaint the SVG into a surface by
|
|
|
|
// nsIFrame::ClearInvalidationStateBits in nsDisplayList::PaintRoot.
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget;
|
|
|
|
if (mProgressTracker) {
|
|
|
|
eventTarget = mProgressTracker->GetEventTarget();
|
2013-11-21 05:21:50 +04:00
|
|
|
} else {
|
2019-01-03 20:55:06 +03:00
|
|
|
eventTarget = do_GetMainThread();
|
2013-11-21 05:21:50 +04:00
|
|
|
}
|
2019-01-03 20:55:06 +03:00
|
|
|
|
|
|
|
RefPtr<VectorImage> self(this);
|
|
|
|
nsCOMPtr<nsIRunnable> ev(NS_NewRunnableFunction(
|
|
|
|
"VectorImage::SendInvalidationNotifications",
|
|
|
|
[=]() -> void { self->SendInvalidationNotifications(); }));
|
2019-03-01 16:11:56 +03:00
|
|
|
eventTarget->Dispatch(CreateMediumHighRunnable(ev.forget()),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2010-09-09 00:40:40 +04:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void VectorImage::PropagateUseCounters(Document* aParentDocument) {
|
|
|
|
Document* doc = mSVGDocumentWrapper->GetDocument();
|
2015-06-03 20:42:07 +03:00
|
|
|
if (doc) {
|
|
|
|
doc->PropagateUseCounters(aParentDocument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:25:35 +03:00
|
|
|
void VectorImage::ReportUseCounters() {
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = mSVGDocumentWrapper->GetDocument()) {
|
2018-12-26 05:35:30 +03:00
|
|
|
doc->ReportUseCounters();
|
2015-03-17 22:25:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 22:58:00 +03:00
|
|
|
nsIntSize VectorImage::OptimalImageSizeForDest(const gfxSize& aDest,
|
|
|
|
uint32_t aWhichFrame,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2015-04-04 22:58:00 +03:00
|
|
|
uint32_t aFlags) {
|
2014-07-29 01:27:39 +04:00
|
|
|
MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
|
|
|
|
aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
|
|
|
|
"Unexpected destination size");
|
|
|
|
|
|
|
|
// We can rescale SVGs freely, so just return the provided destination size.
|
2016-07-26 17:48:30 +03:00
|
|
|
return nsIntSize::Ceil(aDest.width, aDest.height);
|
2014-07-29 01:27:39 +04:00
|
|
|
}
|
|
|
|
|
2014-06-28 07:50:26 +04:00
|
|
|
already_AddRefed<imgIContainer> VectorImage::Unwrap() {
|
|
|
|
nsCOMPtr<imgIContainer> self(this);
|
|
|
|
return self.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-09-09 00:40:39 +04:00
|
|
|
} // namespace mozilla
|