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"
|
2013-03-17 11:55:13 +04:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2014-03-05 20:10:34 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-08-23 21:56:20 +04:00
|
|
|
#include "nsIDOMEvent.h"
|
2010-09-09 00:40:39 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#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"
|
2010-09-09 00:40:39 +04:00
|
|
|
#include "nsSVGEffects.h" // for nsSVGRenderingObserver
|
2014-05-08 13:53:00 +04:00
|
|
|
#include "nsWindowMemoryReporter.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"
|
2013-08-30 01:18:25 +04:00
|
|
|
#include "nsIDOMEventListener.h"
|
2013-10-22 15:38:08 +04:00
|
|
|
#include "SurfaceCache.h"
|
2015-03-17 22:25:35 +03:00
|
|
|
#include "nsDocument.h"
|
2010-09-09 00:40:40 +04:00
|
|
|
|
2013-11-19 10:03:36 +04:00
|
|
|
// undef the GetCurrentTime macro defined in WinBase.h from the MS Platform SDK
|
|
|
|
#undef GetCurrentTime
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
namespace mozilla {
|
2011-10-15 11:33:26 +04:00
|
|
|
|
|
|
|
using namespace dom;
|
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
|
2015-03-21 19:28:04 +03:00
|
|
|
class SVGRootRenderingObserver final : public nsSVGRenderingObserver {
|
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)
|
2013-06-22 00:32:34 +04:00
|
|
|
: nsSVGRenderingObserver()
|
|
|
|
, mDocWrapper(aDocWrapper)
|
|
|
|
, mVectorImage(aVectorImage)
|
|
|
|
, mHonoringInvalidations(true)
|
2010-09-09 00:40:40 +04:00
|
|
|
{
|
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");
|
|
|
|
|
2010-09-09 00:40:40 +04:00
|
|
|
StartListening();
|
|
|
|
Element* elem = GetTarget();
|
2013-03-01 10:44:59 +04:00
|
|
|
MOZ_ASSERT(elem, "no root SVG node for us to observe");
|
|
|
|
|
|
|
|
nsSVGEffects::AddRenderingObserver(elem, this);
|
|
|
|
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:
|
2015-07-09 21:32:00 +03:00
|
|
|
virtual ~SVGRootRenderingObserver()
|
|
|
|
{
|
|
|
|
StopListening();
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual Element* GetTarget() override
|
2010-09-09 00:40:40 +04:00
|
|
|
{
|
|
|
|
return mDocWrapper->GetRootSVGElem();
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void DoUpdate() override
|
2010-09-09 00:40:40 +04:00
|
|
|
{
|
|
|
|
Element* elem = GetTarget();
|
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();
|
|
|
|
if (!frame || frame->PresContext()->PresShell()->IsDestroying()) {
|
|
|
|
// 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) {
|
|
|
|
nsSVGEffects::AddRenderingObserver(elem, this);
|
|
|
|
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
|
|
|
|
|
|
|
|
SVGParseCompleteListener(nsIDocument* aDocument,
|
|
|
|
VectorImage* aImage)
|
|
|
|
: 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()
|
2013-03-30 00:14:19 +04:00
|
|
|
{
|
2013-02-14 06:04:08 +04:00
|
|
|
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:
|
2015-03-21 19:28:04 +03:00
|
|
|
void EndLoad(nsIDocument* aDocument) override
|
2013-02-14 06:04:08 +04:00
|
|
|
{
|
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:
|
|
|
|
nsCOMPtr<nsIDocument> 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
|
|
|
|
|
|
|
|
SVGLoadEventListener(nsIDocument* aDocument,
|
|
|
|
VectorImage* aImage)
|
|
|
|
: 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:
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) override
|
2013-02-14 06:04:08 +04:00
|
|
|
{
|
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:
|
|
|
|
nsCOMPtr<nsIDocument> 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,
|
2015-04-21 18:04:57 +03:00
|
|
|
const IntRect& aViewport,
|
2014-08-23 00:12:38 +04:00
|
|
|
const IntSize& aSize,
|
|
|
|
uint32_t aImageFlags)
|
|
|
|
: mSVGDocumentWrapper(aSVGDocumentWrapper)
|
|
|
|
, mViewport(aViewport)
|
|
|
|
, mSize(aSize)
|
|
|
|
, mImageFlags(aImageFlags)
|
2015-04-04 22:58:00 +03:00
|
|
|
{ }
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool operator()(gfxContext* aContext,
|
2014-12-16 06:50:02 +03:00
|
|
|
const gfxRect& aFillRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2014-12-16 06:50:02 +03:00
|
|
|
const gfxMatrix& aTransform);
|
2010-09-09 00:40:39 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SVGDocumentWrapper> mSVGDocumentWrapper;
|
2015-04-21 18:04:57 +03:00
|
|
|
const IntRect mViewport;
|
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()
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-09-09 00:40:39 +04:00
|
|
|
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
|
|
|
|
nsCOMPtr<nsIPresShell> presShell;
|
|
|
|
if (NS_FAILED(mSVGDocumentWrapper->GetPresShell(getter_AddRefs(presShell)))) {
|
|
|
|
NS_WARNING("Unable to draw -- presShell lookup failed");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
2013-03-01 10:45:00 +04:00
|
|
|
MOZ_ASSERT(presShell, "GetPresShell succeeded but returned null");
|
2010-09-09 00:40:39 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2014-09-11 10:57:38 +04:00
|
|
|
aContext->SetMatrix(
|
|
|
|
aContext->CurrentMatrix().PreMultiply(matrix).
|
|
|
|
Scale(double(mSize.width) / mViewport.width,
|
|
|
|
double(mSize.height) / mViewport.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
|
|
|
|
|
|
|
nsRect svgRect(presContext->DevPixelsToAppUnits(mViewport.x),
|
|
|
|
presContext->DevPixelsToAppUnits(mViewport.y),
|
|
|
|
presContext->DevPixelsToAppUnits(mViewport.width),
|
|
|
|
presContext->DevPixelsToAppUnits(mViewport.height));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t renderDocFlags = nsIPresShell::RENDER_IGNORE_VIEWPORT_SCROLLING;
|
2010-09-09 00:40:39 +04:00
|
|
|
if (!(mImageFlags & imgIContainer::FLAG_SYNC_DECODE)) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_ASYNC_DECODE_IMAGES;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2015-04-29 02:32:02 +03:00
|
|
|
VectorImage::VectorImage(ImageURL* aURI /* = nullptr */) :
|
2013-09-28 22:28:44 +04:00
|
|
|
ImageResource(aURI), // invoke superclass's constructor
|
2015-03-28 06:17:23 +03:00
|
|
|
mLockCount(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsInitialized(false),
|
|
|
|
mIsFullyLoaded(false),
|
|
|
|
mIsDrawing(false),
|
2013-09-04 14:39:30 +04:00
|
|
|
mHaveAnimations(false),
|
|
|
|
mHasPendingInvalidation(false)
|
2015-04-29 02:32:02 +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
|
|
|
|
|
|
|
|
nsresult
|
2013-02-13 07:00:03 +04:00
|
|
|
VectorImage::Init(const char* aMimeType,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags)
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-02-20 07:51:48 +04:00
|
|
|
size_t
|
2014-11-27 05:00:15 +03:00
|
|
|
VectorImage::SizeOfSourceWithComputedFallback(MallocSizeOf aMallocSizeOf) const
|
2014-11-26 12:37:57 +03:00
|
|
|
{
|
2016-10-26 19:46:00 +03:00
|
|
|
if (!mSVGDocumentWrapper) {
|
|
|
|
return 0; // No document, so no memory used for the document.
|
|
|
|
}
|
|
|
|
|
2014-11-26 13:56:28 +03:00
|
|
|
nsIDocument* doc = mSVGDocumentWrapper->GetDocument();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
nsWindowSizes windowSizes(aMallocSizeOf);
|
|
|
|
doc->DocAddSizeOfIncludingThis(&windowSizes);
|
|
|
|
|
|
|
|
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
|
2014-11-27 05:00:15 +03:00
|
|
|
{
|
2015-04-28 21:46:17 +03:00
|
|
|
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)
|
2012-12-18 02:05:18 +04:00
|
|
|
{
|
2013-02-14 06:41:10 +04:00
|
|
|
// 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.
|
2013-02-14 06:41:10 +04:00
|
|
|
nsresult finalStatus = OnStopRequest(aRequest, aContext, aStatus);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
return OnDataAvailable(aRequest, aContext, aInStr, aSourceOffset, aCount);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-08-05 17:57:16 +04:00
|
|
|
bool
|
2010-11-17 23:39:23 +03:00
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:15 +04:00
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP_(void)
|
2014-07-10 19:00:31 +04:00
|
|
|
VectorImage::RequestRefresh(const TimeStamp& aTime)
|
2011-11-10 01:39:15 +04:00
|
|
|
{
|
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) {
|
|
|
|
mHasPendingInvalidation = false;
|
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.
|
|
|
|
//
|
|
|
|
// Non-animated images call this method directly from
|
|
|
|
// 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
|
|
|
|
// notifications directly in |InvalidateObservers...|.
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgressTracker) {
|
2014-11-27 00:22:10 +03:00
|
|
|
SurfaceCache::RemoveImage(ImageKey(this));
|
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
|
|
|
{
|
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-09-09 00:40:39 +04:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
VectorImage::GetAnimated(bool* aAnimated)
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
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()
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
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)
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
|
|
|
|
|
|
|
|
if (aSize.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhichFrame > FRAME_MAX_VALUE) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mError || !mIsFullyLoaded) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-10-12 23:00:30 +04:00
|
|
|
// Make our surface the size of what will ultimately be drawn to it.
|
|
|
|
// (either the full image size, or the restricted region)
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> dt = gfxPlatform::GetPlatform()->
|
2015-09-19 23:34:09 +03:00
|
|
|
CreateOffscreenContentDrawTarget(aSize, SurfaceFormat::B8G8R8A8);
|
2016-04-12 22:18:11 +03:00
|
|
|
if (!dt || !dt->IsValid()) {
|
2014-05-31 14:26:04 +04:00
|
|
|
NS_ERROR("Could not create a DrawTarget");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-07 02:39:56 +03:00
|
|
|
RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
|
2016-04-12 22:18:11 +03:00
|
|
|
MOZ_ASSERT(context); // already checked the draw target above
|
2014-03-05 20:10:34 +04:00
|
|
|
|
2015-10-06 03:12:46 +03:00
|
|
|
auto result = Draw(context, aSize, ImageRegion::Create(aSize),
|
2017-01-03 08:53:22 +03:00
|
|
|
aWhichFrame, SamplingFilter::POINT, Nothing(), aFlags,
|
|
|
|
1.0);
|
2010-10-12 23:00:30 +04:00
|
|
|
|
2015-02-05 00:50:56 +03:00
|
|
|
return result == DrawResult::SUCCESS ? dt->Snapshot() : nullptr;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 10:23:44 +03:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
VectorImage::IsImageContainerAvailable(LayerManager* aManager, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
2013-03-06 03:39:48 +04:00
|
|
|
{
|
2015-03-18 05:40:16 +03:00
|
|
|
return nullptr;
|
2013-03-06 03:39:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 15:38:08 +04:00
|
|
|
struct SVGDrawingParameters
|
|
|
|
{
|
|
|
|
SVGDrawingParameters(gfxContext* aContext,
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize& aSize,
|
|
|
|
const ImageRegion& aRegion,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2013-10-22 15:38:08 +04:00
|
|
|
float aAnimationTime,
|
2017-01-03 08:53:22 +03:00
|
|
|
uint32_t aFlags,
|
|
|
|
float aOpacity)
|
2013-10-22 15:38:08 +04:00
|
|
|
: context(aContext)
|
2014-08-23 00:12:38 +04:00
|
|
|
, size(aSize.width, aSize.height)
|
|
|
|
, region(aRegion)
|
2016-05-25 19:01:18 +03:00
|
|
|
, samplingFilter(aSamplingFilter)
|
2013-10-22 15:38:08 +04:00
|
|
|
, svgContext(aSVGContext)
|
2015-04-03 22:48:12 +03:00
|
|
|
, viewportSize(aSize)
|
2014-08-23 00:12:38 +04:00
|
|
|
, animationTime(aAnimationTime)
|
2013-10-22 15:38:08 +04:00
|
|
|
, flags(aFlags)
|
2017-01-03 08:53:22 +03:00
|
|
|
, opacity(aSVGContext ? aSVGContext->GetGlobalOpacity() : aOpacity)
|
2015-04-03 22:48:12 +03:00
|
|
|
{
|
|
|
|
if (aSVGContext) {
|
|
|
|
CSSIntSize sz = aSVGContext->GetViewportSize();
|
|
|
|
viewportSize = nsIntSize(sz.width, sz.height); // XXX losing unit
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 00:12:38 +04:00
|
|
|
|
|
|
|
gfxContext* context;
|
|
|
|
IntSize size;
|
|
|
|
ImageRegion region;
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter samplingFilter;
|
2014-08-23 00:12:38 +04:00
|
|
|
const Maybe<SVGImageContext>& svgContext;
|
|
|
|
nsIntSize viewportSize;
|
|
|
|
float animationTime;
|
|
|
|
uint32_t flags;
|
2014-08-29 18:04:34 +04:00
|
|
|
gfxFloat opacity;
|
2013-10-22 15:38:08 +04:00
|
|
|
};
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//******************************************************************************
|
2015-02-05 00:50:56 +03:00
|
|
|
NS_IMETHODIMP_(DrawResult)
|
2010-09-09 00:40:39 +04:00
|
|
|
VectorImage::Draw(gfxContext* aContext,
|
2014-08-23 00:12:38 +04:00
|
|
|
const nsIntSize& aSize,
|
|
|
|
const ImageRegion& aRegion,
|
2013-03-11 05:43:37 +04:00
|
|
|
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,
|
2017-01-03 08:53:22 +03:00
|
|
|
uint32_t aFlags,
|
|
|
|
float aOpacity)
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
2015-02-05 00:50:56 +03:00
|
|
|
if (aWhichFrame > FRAME_MAX_VALUE) {
|
|
|
|
return DrawResult::BAD_ARGS;
|
|
|
|
}
|
2013-03-11 05:43:37 +04:00
|
|
|
|
2015-02-05 00:50:56 +03:00
|
|
|
if (!aContext) {
|
|
|
|
return DrawResult::BAD_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mError) {
|
|
|
|
return DrawResult::BAD_IMAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mIsFullyLoaded) {
|
|
|
|
return DrawResult::NOT_READY;
|
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2010-11-19 19:56:06 +03:00
|
|
|
if (mIsDrawing) {
|
|
|
|
NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
|
2015-02-05 00:50:56 +03:00
|
|
|
return DrawResult::TEMPORARY_ERROR;
|
2010-11-19 19:56:06 +03:00
|
|
|
}
|
2013-04-29 23:10:16 +04:00
|
|
|
|
2016-11-17 09:25:53 +03:00
|
|
|
if (mAnimationConsumers == 0) {
|
|
|
|
SendOnUnlockedDraw(aFlags);
|
2013-04-29 23:10:16 +04:00
|
|
|
}
|
|
|
|
|
2013-03-01 10:44:57 +04:00
|
|
|
AutoRestore<bool> autoRestoreIsDrawing(mIsDrawing);
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDrawing = true;
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2016-03-08 10:54:13 +03:00
|
|
|
Maybe<SVGImageContext> svgContext;
|
|
|
|
// If FLAG_FORCE_PRESERVEASPECTRATIO_NONE bit is set, that mean we should
|
|
|
|
// overwrite SVG preserveAspectRatio attibute of this image with none, and
|
|
|
|
// always stretch this image to viewport non-uniformly.
|
|
|
|
// And we can do this only if the caller pass in the the SVG viewport, via
|
|
|
|
// aSVGContext.
|
|
|
|
if ((aFlags & FLAG_FORCE_PRESERVEASPECTRATIO_NONE) && aSVGContext.isSome()) {
|
|
|
|
Maybe<SVGPreserveAspectRatio> aspectRatio =
|
|
|
|
Some(SVGPreserveAspectRatio(SVG_PRESERVEASPECTRATIO_NONE,
|
|
|
|
SVG_MEETORSLICE_UNKNOWN));
|
|
|
|
svgContext =
|
|
|
|
Some(SVGImageContext(aSVGContext->GetViewportSize(),
|
|
|
|
aspectRatio));
|
|
|
|
} else {
|
|
|
|
svgContext = aSVGContext;
|
|
|
|
}
|
|
|
|
|
2015-04-04 22:58:00 +03:00
|
|
|
float animTime =
|
|
|
|
(aWhichFrame == FRAME_FIRST) ? 0.0f
|
|
|
|
: mSVGDocumentWrapper->GetCurrentTime();
|
2016-03-08 10:54:13 +03:00
|
|
|
AutoSVGRenderingState autoSVGState(svgContext, animTime,
|
2013-03-01 00:22:43 +04:00
|
|
|
mSVGDocumentWrapper->GetRootSVGElem());
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2016-03-08 10:54:13 +03:00
|
|
|
|
2016-05-25 19:01:18 +03:00
|
|
|
SVGDrawingParameters params(aContext, aSize, aRegion, aSamplingFilter,
|
2017-01-03 08:53:22 +03:00
|
|
|
svgContext, 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.
|
|
|
|
RefPtr<gfxDrawable> svgDrawable = LookupCachedSurface(params);
|
|
|
|
if (svgDrawable) {
|
|
|
|
Show(svgDrawable, params);
|
2015-02-05 00:50:56 +03:00
|
|
|
return DrawResult::SUCCESS;
|
2014-06-26 11:40:12 +04:00
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2016-08-18 07:46:25 +03:00
|
|
|
// We didn't get a hit in the surface cache, so we'll need to rerasterize.
|
2016-09-15 14:53:12 +03:00
|
|
|
CreateSurfaceAndShow(params, aContext->GetDrawTarget()->GetBackendType());
|
2016-08-18 07:46:25 +03:00
|
|
|
return DrawResult::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxDrawable>
|
|
|
|
VectorImage::LookupCachedSurface(const SVGDrawingParameters& aParams)
|
|
|
|
{
|
|
|
|
// If we're not allowed to use a cached surface, don't attempt a lookup.
|
|
|
|
if (aParams.flags & FLAG_BYPASS_SURFACE_CACHE) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-01 04:57:03 +03:00
|
|
|
LookupResult result =
|
2014-08-23 00:12:38 +04:00
|
|
|
SurfaceCache::Lookup(ImageKey(this),
|
2016-08-18 10:06:41 +03:00
|
|
|
VectorSurfaceKey(aParams.size, aParams.svgContext));
|
2016-08-18 07:46:25 +03:00
|
|
|
if (!result) {
|
|
|
|
return nullptr; // No matching surface, or the OS freed the volatile buffer.
|
|
|
|
}
|
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();
|
2016-08-18 07:46:25 +03:00
|
|
|
return nullptr;
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
2010-09-09 00:40:39 +04:00
|
|
|
|
2016-08-18 07:46:25 +03:00
|
|
|
RefPtr<gfxDrawable> svgDrawable =
|
|
|
|
new gfxSurfaceDrawable(sourceSurface, result.Surface()->GetSize());
|
|
|
|
return svgDrawable.forget();
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-15 14:53:12 +03:00
|
|
|
VectorImage::CreateSurfaceAndShow(const SVGDrawingParameters& aParams, BackendType aBackend)
|
2013-10-22 15:38:08 +04:00
|
|
|
{
|
|
|
|
mSVGDocumentWrapper->UpdateViewportBounds(aParams.viewportSize);
|
|
|
|
mSVGDocumentWrapper->FlushImageTransformInvalidation();
|
2013-10-21 22:39:33 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxDrawingCallback> cb =
|
2010-09-09 00:40:39 +04:00
|
|
|
new SVGDrawingCallback(mSVGDocumentWrapper,
|
2015-04-21 18:04:57 +03:00
|
|
|
IntRect(IntPoint(0, 0), aParams.viewportSize),
|
2014-08-23 00:12:38 +04:00
|
|
|
aParams.size,
|
2013-10-22 15:38:08 +04:00
|
|
|
aParams.flags);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxDrawable> svgDrawable =
|
2015-03-29 17:59:15 +03:00
|
|
|
new gfxCallbackDrawable(cb, aParams.size);
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2014-06-26 11:40:12 +04:00
|
|
|
bool bypassCache = bool(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:
|
2014-08-23 00:12:38 +04:00
|
|
|
!SurfaceCache::CanHold(aParams.size);
|
2015-04-04 22:58:00 +03:00
|
|
|
if (bypassCache) {
|
2014-08-07 02:43:25 +04:00
|
|
|
return Show(svgDrawable, aParams);
|
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.
|
2016-07-02 08:33:58 +03:00
|
|
|
SurfaceCache::UnlockEntries(ImageKey(this));
|
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.)
|
2016-07-02 08:20:32 +03:00
|
|
|
NotNull<RefPtr<imgFrame>> frame = WrapNotNull(new imgFrame);
|
2014-09-15 02:22:45 +04:00
|
|
|
nsresult rv =
|
2015-03-29 17:59:15 +03:00
|
|
|
frame->InitWithDrawable(svgDrawable, aParams.size,
|
2014-09-15 02:22:45 +04:00
|
|
|
SurfaceFormat::B8G8R8A8,
|
2016-09-15 14:53:12 +03:00
|
|
|
SamplingFilter::POINT, aParams.flags,
|
|
|
|
aBackend);
|
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)) {
|
2014-08-07 02:43:25 +04:00
|
|
|
return Show(svgDrawable, aParams);
|
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) {
|
2014-09-15 02:22:45 +04:00
|
|
|
return Show(svgDrawable, aParams);
|
2015-04-04 22:58:00 +03:00
|
|
|
}
|
2014-07-29 04:05:40 +04:00
|
|
|
|
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 =
|
2016-08-25 00:50:49 +03:00
|
|
|
WrapNotNull(new SimpleSurfaceProvider(ImageKey(this), surfaceKey, frame));
|
2016-08-20 06:26:59 +03:00
|
|
|
SurfaceCache::Insert(provider);
|
2013-10-22 15:38:08 +04:00
|
|
|
|
2014-09-15 02:22:45 +04:00
|
|
|
// Draw.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxDrawable> drawable =
|
2015-03-29 17:59:15 +03:00
|
|
|
new gfxSurfaceDrawable(surface, aParams.size);
|
2014-08-07 02:43:25 +04:00
|
|
|
Show(drawable, aParams);
|
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.
|
2016-11-19 00:59:04 +03:00
|
|
|
if (!(aParams.flags & FLAG_ASYNC_NOTIFY)) {
|
|
|
|
mProgressTracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE,
|
|
|
|
GetMaxSizedIntRect());
|
|
|
|
} else {
|
|
|
|
NotNull<RefPtr<VectorImage>> image = WrapNotNull(this);
|
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction([=]() -> void {
|
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
|
|
|
if (tracker) {
|
|
|
|
tracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE,
|
|
|
|
GetMaxSizedIntRect());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2013-10-22 15:38:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-08-07 02:43:25 +04:00
|
|
|
VectorImage::Show(gfxDrawable* aDrawable, const SVGDrawingParameters& aParams)
|
2013-10-22 15:38:08 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDrawable, "Should have a gfxDrawable by now");
|
|
|
|
gfxUtils::DrawPixelSnapped(aParams.context, aDrawable,
|
2015-03-29 17:59:15 +03:00
|
|
|
aParams.size,
|
2014-08-23 00:12:38 +04:00
|
|
|
aParams.region,
|
2014-04-20 05:28:38 +04:00
|
|
|
SurfaceFormat::B8G8R8A8,
|
2016-05-25 19:01:18 +03:00
|
|
|
aParams.samplingFilter,
|
|
|
|
aParams.flags, aParams.opacity);
|
2010-09-09 00:40:39 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-28 06:17:23 +03:00
|
|
|
void
|
|
|
|
VectorImage::OnSurfaceDiscarded()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mProgressTracker);
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(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");
|
|
|
|
return aWhichFrame == FRAME_FIRST
|
|
|
|
? 0.0f
|
|
|
|
: mSVGDocumentWrapper->GetCurrentTime();
|
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// nsIRequestObserver methods
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt)
|
|
|
|
{
|
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();
|
|
|
|
nsresult rv = mSVGDocumentWrapper->OnStartRequest(aRequest, aCtxt);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2015-07-07 03:11:14 +03:00
|
|
|
// Block page load until the document's ready. (We unblock it in
|
|
|
|
// OnSVGDocumentLoaded or OnSVGDocumentError.)
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgressTracker) {
|
2015-07-07 03:11:14 +03:00
|
|
|
mProgressTracker->SyncNotifyProgress(FLAG_ONLOAD_BLOCKED);
|
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.
|
|
|
|
nsIDocument* document = mSVGDocumentWrapper->GetDocument();
|
|
|
|
mLoadEventListener = new SVGLoadEventListener(document, this);
|
|
|
|
mParseCompleteListener = new SVGParseCompleteListener(document, this);
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VectorImage::OnStopRequest(nsIRequest* aRequest, nsISupports* aCtxt,
|
|
|
|
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
|
|
|
|
2013-02-14 06:04:08 +04:00
|
|
|
return mSVGDocumentWrapper->OnStopRequest(aRequest, aCtxt, aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 |
|
|
|
|
FLAG_FRAME_COMPLETE |
|
|
|
|
FLAG_DECODE_COMPLETE |
|
|
|
|
FLAG_ONLOAD_UNBLOCKED;
|
|
|
|
|
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.
|
2015-10-23 09:29:38 +03:00
|
|
|
Progress progress = FLAG_ONLOAD_UNBLOCKED | 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
|
|
|
|
VectorImage::OnDataAvailable(nsIRequest* aRequest, nsISupports* aCtxt,
|
2012-09-06 06:41:02 +04:00
|
|
|
nsIInputStream* aInStr, uint64_t aSourceOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2010-09-09 00:40:39 +04:00
|
|
|
{
|
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
|
|
|
|
2010-09-09 00:40:39 +04:00
|
|
|
return mSVGDocumentWrapper->OnDataAvailable(aRequest, aCtxt, aInStr,
|
|
|
|
aSourceOffset, aCount);
|
2010-09-09 00:40:39 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 00:40:40 +04:00
|
|
|
// --------------------------
|
|
|
|
// Invalidation helper method
|
|
|
|
|
|
|
|
void
|
2013-09-04 14:39:30 +04:00
|
|
|
VectorImage::InvalidateObserversOnNextRefreshDriverTick()
|
2010-09-09 00:40:40 +04:00
|
|
|
{
|
2013-11-21 05:21:50 +04:00
|
|
|
if (mHaveAnimations) {
|
|
|
|
mHasPendingInvalidation = true;
|
|
|
|
} else {
|
|
|
|
SendInvalidationNotifications();
|
|
|
|
}
|
2010-09-09 00:40:40 +04:00
|
|
|
}
|
|
|
|
|
2015-06-03 20:42:07 +03:00
|
|
|
void
|
|
|
|
VectorImage::PropagateUseCounters(nsIDocument* aParentDocument)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = mSVGDocumentWrapper->GetDocument();
|
|
|
|
if (doc) {
|
|
|
|
doc->PropagateUseCounters(aParentDocument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:25:35 +03:00
|
|
|
void
|
|
|
|
VectorImage::ReportUseCounters()
|
|
|
|
{
|
|
|
|
nsIDocument* doc = mSVGDocumentWrapper->GetDocument();
|
|
|
|
if (doc) {
|
|
|
|
static_cast<nsDocument*>(doc)->ReportUseCounters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 01:27:39 +04:00
|
|
|
nsIntSize
|
2015-04-04 22:58:00 +03:00
|
|
|
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
|