1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 // Keep in (case-insensitive) order:
7 #include "gfxContext.h"
8 #include "gfxPlatform.h"
9 #include "mozilla/gfx/2D.h"
10 #include "imgIContainer.h"
11 #include "nsContainerFrame.h"
12 #include "nsIImageLoadingContent.h"
13 #include "nsLayoutUtils.h"
14 #include "nsRenderingContext.h"
15 #include "imgINotificationObserver.h"
16 #include "nsSVGEffects.h"
17 #include "nsSVGPathGeometryFrame.h"
18 #include "mozilla/dom/SVGSVGElement.h"
19 #include "nsSVGUtils.h"
20 #include "SVGContentUtils.h"
21 #include "SVGImageContext.h"
22 #include "mozilla/dom/SVGImageElement.h"
23 #include "nsContentUtils.h"
24 #include "nsIReflowCallback.h"
26 using namespace mozilla
;
27 using namespace mozilla::dom
;
28 using namespace mozilla::gfx
;
30 class nsSVGImageFrame
;
32 class nsSVGImageListener MOZ_FINAL
: public imgINotificationObserver
35 explicit nsSVGImageListener(nsSVGImageFrame
*aFrame
);
38 NS_DECL_IMGINOTIFICATIONOBSERVER
40 void SetFrame(nsSVGImageFrame
*frame
) { mFrame
= frame
; }
43 ~nsSVGImageListener() {}
45 nsSVGImageFrame
*mFrame
;
48 typedef nsSVGPathGeometryFrame nsSVGImageFrameBase
;
50 class nsSVGImageFrame
: public nsSVGImageFrameBase
,
51 public nsIReflowCallback
54 NS_NewSVGImageFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
);
57 explicit nsSVGImageFrame(nsStyleContext
* aContext
) : nsSVGImageFrameBase(aContext
),
58 mReflowCallbackPosted(false) {}
59 virtual ~nsSVGImageFrame();
62 NS_DECL_FRAMEARENA_HELPERS
64 // nsISVGChildFrame interface:
65 virtual nsresult
PaintSVG(nsRenderingContext
*aContext
,
66 const nsIntRect
*aDirtyRect
,
67 nsIFrame
* aTransformRoot
) MOZ_OVERRIDE
;
68 virtual nsIFrame
* GetFrameForPoint(const gfxPoint
& aPoint
) MOZ_OVERRIDE
;
69 virtual void ReflowSVG() MOZ_OVERRIDE
;
71 // nsSVGPathGeometryFrame methods:
72 virtual uint16_t GetHitTestFlags() MOZ_OVERRIDE
;
74 // nsIFrame interface:
75 virtual nsresult
AttributeChanged(int32_t aNameSpaceID
,
77 int32_t aModType
) MOZ_OVERRIDE
;
78 virtual void Init(nsIContent
* aContent
,
79 nsContainerFrame
* aParent
,
80 nsIFrame
* aPrevInFlow
) MOZ_OVERRIDE
;
81 virtual void DestroyFrom(nsIFrame
* aDestructRoot
) MOZ_OVERRIDE
;
84 * Get the "type" of the frame
86 * @see nsGkAtoms::svgImageFrame
88 virtual nsIAtom
* GetType() const MOZ_OVERRIDE
;
90 #ifdef DEBUG_FRAME_DUMP
91 virtual nsresult
GetFrameName(nsAString
& aResult
) const MOZ_OVERRIDE
93 return MakeFrameName(NS_LITERAL_STRING("SVGImage"), aResult
);
98 virtual bool ReflowFinished() MOZ_OVERRIDE
;
99 virtual void ReflowCallbackCanceled() MOZ_OVERRIDE
;
102 gfx::Matrix
GetRasterImageTransform(int32_t aNativeWidth
,
103 int32_t aNativeHeight
,
105 nsIFrame
* aTransformRoot
= nullptr);
106 gfx::Matrix
GetVectorImageTransform(uint32_t aFor
,
107 nsIFrame
* aTransformRoot
= nullptr);
108 bool TransformContextForPainting(gfxContext
* aGfxContext
,
109 nsIFrame
* aTransformRoot
);
111 nsCOMPtr
<imgINotificationObserver
> mListener
;
113 nsCOMPtr
<imgIContainer
> mImageContainer
;
115 bool mReflowCallbackPosted
;
117 friend class nsSVGImageListener
;
120 //----------------------------------------------------------------------
124 NS_NewSVGImageFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
126 return new (aPresShell
) nsSVGImageFrame(aContext
);
129 NS_IMPL_FRAMEARENA_HELPERS(nsSVGImageFrame
)
131 nsSVGImageFrame::~nsSVGImageFrame()
133 // set the frame to null so we don't send messages to a dead object.
135 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
= do_QueryInterface(mContent
);
137 imageLoader
->RemoveObserver(mListener
);
139 reinterpret_cast<nsSVGImageListener
*>(mListener
.get())->SetFrame(nullptr);
145 nsSVGImageFrame::Init(nsIContent
* aContent
,
146 nsContainerFrame
* aParent
,
147 nsIFrame
* aPrevInFlow
)
149 NS_ASSERTION(aContent
->IsSVG(nsGkAtoms::image
),
150 "Content is not an SVG image!");
152 nsSVGImageFrameBase::Init(aContent
, aParent
, aPrevInFlow
);
154 mListener
= new nsSVGImageListener(this);
155 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
= do_QueryInterface(mContent
);
157 NS_RUNTIMEABORT("Why is this not an image loading content?");
160 // We should have a PresContext now, so let's notify our image loader that
161 // we need to register any image animations with the refresh driver.
162 imageLoader
->FrameCreated(this);
164 imageLoader
->AddObserver(mListener
);
168 nsSVGImageFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
170 if (mReflowCallbackPosted
) {
171 PresContext()->PresShell()->CancelReflowCallback(this);
172 mReflowCallbackPosted
= false;
175 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
=
176 do_QueryInterface(nsFrame::mContent
);
179 imageLoader
->FrameDestroyed(this);
182 nsFrame::DestroyFrom(aDestructRoot
);
185 //----------------------------------------------------------------------
189 nsSVGImageFrame::AttributeChanged(int32_t aNameSpaceID
,
193 if (aNameSpaceID
== kNameSpaceID_None
) {
194 if (aAttribute
== nsGkAtoms::x
||
195 aAttribute
== nsGkAtoms::y
||
196 aAttribute
== nsGkAtoms::width
||
197 aAttribute
== nsGkAtoms::height
) {
198 nsSVGEffects::InvalidateRenderingObservers(this);
199 nsSVGUtils::ScheduleReflowSVG(this);
202 else if (aAttribute
== nsGkAtoms::preserveAspectRatio
) {
203 // We don't paint the content of the image using display lists, therefore
204 // we have to invalidate for this children-only transform changes since
205 // there is no layer tree to notice that the transform changed and
211 if (aNameSpaceID
== kNameSpaceID_XLink
&&
212 aAttribute
== nsGkAtoms::href
) {
214 // Prevent setting image.src by exiting early
215 if (nsContentUtils::IsImageSrcSetDisabled()) {
218 SVGImageElement
*element
= static_cast<SVGImageElement
*>(mContent
);
220 if (element
->mStringAttributes
[SVGImageElement::HREF
].IsExplicitlySet()) {
221 element
->LoadSVGImage(true, true);
223 element
->CancelImageRequests(true);
227 return nsSVGImageFrameBase::AttributeChanged(aNameSpaceID
,
228 aAttribute
, aModType
);
232 nsSVGImageFrame::GetRasterImageTransform(int32_t aNativeWidth
,
233 int32_t aNativeHeight
,
235 nsIFrame
* aTransformRoot
)
237 float x
, y
, width
, height
;
238 SVGImageElement
*element
= static_cast<SVGImageElement
*>(mContent
);
239 element
->GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
242 SVGContentUtils::GetViewBoxTransform(width
, height
,
243 0, 0, aNativeWidth
, aNativeHeight
,
244 element
->mPreserveAspectRatio
);
247 gfx::Matrix::Translation(x
, y
) *
248 gfx::ToMatrix(GetCanvasTM(aFor
, aTransformRoot
));
252 nsSVGImageFrame::GetVectorImageTransform(uint32_t aFor
,
253 nsIFrame
* aTransformRoot
)
255 float x
, y
, width
, height
;
256 SVGImageElement
*element
= static_cast<SVGImageElement
*>(mContent
);
257 element
->GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
259 // No viewBoxTM needed here -- our height/width overrides any concept of
260 // "native size" that the SVG image has, and it will handle viewBox and
261 // preserveAspectRatio on its own once we give it a region to draw into.
263 return gfx::Matrix::Translation(x
, y
) *
264 gfx::ToMatrix(GetCanvasTM(aFor
, aTransformRoot
));
268 nsSVGImageFrame::TransformContextForPainting(gfxContext
* aGfxContext
,
269 nsIFrame
* aTransformRoot
)
271 gfx::Matrix imageTransform
;
272 if (mImageContainer
->GetType() == imgIContainer::TYPE_VECTOR
) {
273 imageTransform
= GetVectorImageTransform(FOR_PAINTING
, aTransformRoot
);
275 int32_t nativeWidth
, nativeHeight
;
276 if (NS_FAILED(mImageContainer
->GetWidth(&nativeWidth
)) ||
277 NS_FAILED(mImageContainer
->GetHeight(&nativeHeight
)) ||
278 nativeWidth
== 0 || nativeHeight
== 0) {
282 GetRasterImageTransform(nativeWidth
, nativeHeight
, FOR_PAINTING
,
285 // NOTE: We need to cancel out the effects of Full-Page-Zoom, or else
286 // it'll get applied an extra time by DrawSingleUnscaledImage.
287 nscoord appUnitsPerDevPx
= PresContext()->AppUnitsPerDevPixel();
288 gfxFloat pageZoomFactor
=
289 nsPresContext::AppUnitsToFloatCSSPixels(appUnitsPerDevPx
);
290 imageTransform
.Scale(pageZoomFactor
, pageZoomFactor
);
293 if (imageTransform
.IsSingular()) {
297 aGfxContext
->Multiply(ThebesMatrix(imageTransform
));
301 //----------------------------------------------------------------------
302 // nsISVGChildFrame methods:
304 nsSVGImageFrame::PaintSVG(nsRenderingContext
*aContext
,
305 const nsIntRect
*aDirtyRect
,
306 nsIFrame
* aTransformRoot
)
310 if (!StyleVisibility()->IsVisible())
313 float x
, y
, width
, height
;
314 SVGImageElement
*imgElem
= static_cast<SVGImageElement
*>(mContent
);
315 imgElem
->GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
316 NS_ASSERTION(width
> 0 && height
> 0,
317 "Should only be painting things with valid width/height");
319 if (!mImageContainer
) {
320 nsCOMPtr
<imgIRequest
> currentRequest
;
321 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
= do_QueryInterface(mContent
);
323 imageLoader
->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST
,
324 getter_AddRefs(currentRequest
));
327 currentRequest
->GetImage(getter_AddRefs(mImageContainer
));
330 if (mImageContainer
) {
331 gfxContext
* ctx
= aContext
->ThebesContext();
332 gfxContextAutoSaveRestore
autoRestorer(ctx
);
334 if (StyleDisplay()->IsScrollableOverflow()) {
335 gfxRect clipRect
= nsSVGUtils::GetClipRectForFrame(this, x
, y
,
337 nsSVGUtils::SetClipRect(ctx
, GetCanvasTM(FOR_PAINTING
, aTransformRoot
),
341 if (!TransformContextForPainting(ctx
, aTransformRoot
)) {
342 return NS_ERROR_FAILURE
;
345 // fill-opacity doesn't affect <image>, so if we're allowed to
346 // optimize group opacity, the opacity used for compositing the
347 // image into the current canvas is just the group opacity.
348 float opacity
= 1.0f
;
349 if (nsSVGUtils::CanOptimizeOpacity(this)) {
350 opacity
= StyleDisplay()->mOpacity
;
353 if (opacity
!= 1.0f
|| StyleDisplay()->mMixBlendMode
!= NS_STYLE_BLEND_NORMAL
) {
354 ctx
->PushGroup(gfxContentType::COLOR_ALPHA
);
357 nscoord appUnitsPerDevPx
= PresContext()->AppUnitsPerDevPixel();
358 nsRect dirtyRect
; // only used if aDirtyRect is non-null
360 NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
361 (mState
& NS_FRAME_IS_NONDISPLAY
),
362 "Display lists handle dirty rect intersection test");
363 dirtyRect
= aDirtyRect
->ToAppUnits(appUnitsPerDevPx
);
364 // Adjust dirtyRect to match our local coordinate system.
366 nsSVGUtils::TransformFrameRectToOuterSVG(mRect
,
367 GetCanvasTM(FOR_PAINTING
), PresContext());
368 dirtyRect
.MoveBy(-rootRect
.TopLeft());
371 // XXXbholley - I don't think huge images in SVGs are common enough to
372 // warrant worrying about the responsiveness impact of doing synchronous
373 // decodes. The extra code complexity of determinining when we want to
374 // force sync probably just isn't worth it, so always pass FLAG_SYNC_DECODE
375 uint32_t drawFlags
= imgIContainer::FLAG_SYNC_DECODE
;
377 if (mImageContainer
->GetType() == imgIContainer::TYPE_VECTOR
) {
378 // Package up the attributes of this image element which can override the
379 // attributes of mImageContainer's internal SVG document.
380 SVGImageContext
context(nsIntSize(width
, height
),
381 Some(imgElem
->mPreserveAspectRatio
.GetAnimValue()));
383 nsRect
destRect(0, 0,
384 appUnitsPerDevPx
* width
,
385 appUnitsPerDevPx
* height
);
387 // Note: Can't use DrawSingleUnscaledImage for the TYPE_VECTOR case.
388 // That method needs our image to have a fixed native width & height,
389 // and that's not always true for TYPE_VECTOR images.
390 nsLayoutUtils::DrawSingleImage(
394 nsLayoutUtils::GetGraphicsFilterForFrame(this),
396 aDirtyRect
? dirtyRect
: destRect
,
399 } else { // mImageContainer->GetType() == TYPE_RASTER
400 nsLayoutUtils::DrawSingleUnscaledImage(
404 nsLayoutUtils::GetGraphicsFilterForFrame(this),
406 aDirtyRect
? &dirtyRect
: nullptr,
410 if (opacity
!= 1.0f
|| StyleDisplay()->mMixBlendMode
!= NS_STYLE_BLEND_NORMAL
) {
411 ctx
->PopGroupToSource();
412 ctx
->SetOperator(gfxContext::OPERATOR_OVER
);
415 // gfxContextAutoSaveRestore goes out of scope & cleans up our gfxContext
422 nsSVGImageFrame::GetFrameForPoint(const gfxPoint
& aPoint
)
425 SVGImageElement
*element
= static_cast<SVGImageElement
*>(mContent
);
426 element
->GetAnimatedLengthValues(&rect
.x
, &rect
.y
,
427 &rect
.width
, &rect
.height
, nullptr);
429 if (!rect
.Contains(ToPoint(aPoint
))) {
433 // Special case for raster images -- we only want to accept points that fall
434 // in the underlying image's (scaled to fit) native bounds. That region
435 // doesn't necessarily map to our <image> element's [x,y,width,height] if the
436 // raster image's aspect ratio is being preserved. We have to look up the
437 // native image size & our viewBox transform in order to filter out points
438 // that fall outside that area. (This special case doesn't apply to vector
439 // images because they don't limit their drawing to explicit "native
440 // bounds" -- they have an infinite canvas on which to place content.)
441 if (StyleDisplay()->IsScrollableOverflow() && mImageContainer
) {
442 if (mImageContainer
->GetType() == imgIContainer::TYPE_RASTER
) {
443 int32_t nativeWidth
, nativeHeight
;
444 if (NS_FAILED(mImageContainer
->GetWidth(&nativeWidth
)) ||
445 NS_FAILED(mImageContainer
->GetHeight(&nativeHeight
)) ||
446 nativeWidth
== 0 || nativeHeight
== 0) {
450 SVGContentUtils::GetViewBoxTransform(rect
.width
, rect
.height
,
451 0, 0, nativeWidth
, nativeHeight
,
452 element
->mPreserveAspectRatio
);
453 if (!nsSVGUtils::HitTestRect(viewBoxTM
,
454 0, 0, nativeWidth
, nativeHeight
,
455 aPoint
.x
- rect
.x
, aPoint
.y
- rect
.y
)) {
465 nsSVGImageFrame::GetType() const
467 return nsGkAtoms::svgImageFrame
;
470 //----------------------------------------------------------------------
471 // nsSVGPathGeometryFrame methods:
473 // Lie about our fill/stroke so that covered region and hit detection work properly
476 nsSVGImageFrame::ReflowSVG()
478 NS_ASSERTION(nsSVGUtils::OuterSVGIsCallingReflowSVG(this),
479 "This call is probably a wasteful mistake");
481 NS_ABORT_IF_FALSE(!(GetStateBits() & NS_FRAME_IS_NONDISPLAY
),
482 "ReflowSVG mechanism not designed for this");
484 if (!nsSVGUtils::NeedsReflowSVG(this)) {
488 float x
, y
, width
, height
;
489 SVGImageElement
*element
= static_cast<SVGImageElement
*>(mContent
);
490 element
->GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
492 Rect
extent(x
, y
, width
, height
);
494 if (!extent
.IsEmpty()) {
495 mRect
= nsLayoutUtils::RoundGfxRectToAppRect(extent
,
496 PresContext()->AppUnitsPerCSSPixel());
501 if (mState
& NS_FRAME_FIRST_REFLOW
) {
502 // Make sure we have our filter property (if any) before calling
503 // FinishAndStoreOverflow (subsequent filter changes are handled off
504 // nsChangeHint_UpdateEffects):
505 nsSVGEffects::UpdateEffects(this);
507 if (!mReflowCallbackPosted
) {
508 nsIPresShell
* shell
= PresContext()->PresShell();
509 mReflowCallbackPosted
= true;
510 shell
->PostReflowCallback(this);
514 nsRect overflow
= nsRect(nsPoint(0,0), mRect
.Size());
515 nsOverflowAreas
overflowAreas(overflow
, overflow
);
516 FinishAndStoreOverflow(overflowAreas
, mRect
.Size());
518 mState
&= ~(NS_FRAME_FIRST_REFLOW
| NS_FRAME_IS_DIRTY
|
519 NS_FRAME_HAS_DIRTY_CHILDREN
);
521 // Invalidate, but only if this is not our first reflow (since if it is our
522 // first reflow then we haven't had our first paint yet).
523 if (!(GetParent()->GetStateBits() & NS_FRAME_FIRST_REFLOW
)) {
529 nsSVGImageFrame::ReflowFinished()
531 mReflowCallbackPosted
= false;
533 nsLayoutUtils::UpdateImageVisibilityForFrame(this);
539 nsSVGImageFrame::ReflowCallbackCanceled()
541 mReflowCallbackPosted
= false;
545 nsSVGImageFrame::GetHitTestFlags()
549 switch(StyleVisibility()->mPointerEvents
) {
550 case NS_STYLE_POINTER_EVENTS_NONE
:
552 case NS_STYLE_POINTER_EVENTS_VISIBLEPAINTED
:
553 case NS_STYLE_POINTER_EVENTS_AUTO
:
554 if (StyleVisibility()->IsVisible()) {
555 /* XXX: should check pixel transparency */
556 flags
|= SVG_HIT_TEST_FILL
;
559 case NS_STYLE_POINTER_EVENTS_VISIBLEFILL
:
560 case NS_STYLE_POINTER_EVENTS_VISIBLESTROKE
:
561 case NS_STYLE_POINTER_EVENTS_VISIBLE
:
562 if (StyleVisibility()->IsVisible()) {
563 flags
|= SVG_HIT_TEST_FILL
;
566 case NS_STYLE_POINTER_EVENTS_PAINTED
:
567 /* XXX: should check pixel transparency */
568 flags
|= SVG_HIT_TEST_FILL
;
570 case NS_STYLE_POINTER_EVENTS_FILL
:
571 case NS_STYLE_POINTER_EVENTS_STROKE
:
572 case NS_STYLE_POINTER_EVENTS_ALL
:
573 flags
|= SVG_HIT_TEST_FILL
;
576 NS_ERROR("not reached");
583 //----------------------------------------------------------------------
584 // nsSVGImageListener implementation
586 NS_IMPL_ISUPPORTS(nsSVGImageListener
, imgINotificationObserver
)
588 nsSVGImageListener::nsSVGImageListener(nsSVGImageFrame
*aFrame
) : mFrame(aFrame
)
593 nsSVGImageListener::Notify(imgIRequest
*aRequest
, int32_t aType
, const nsIntRect
* aData
)
596 return NS_ERROR_FAILURE
;
598 if (aType
== imgINotificationObserver::LOAD_COMPLETE
) {
599 mFrame
->InvalidateFrame();
600 nsSVGEffects::InvalidateRenderingObservers(mFrame
);
601 nsSVGUtils::ScheduleReflowSVG(mFrame
);
604 if (aType
== imgINotificationObserver::FRAME_UPDATE
) {
605 // No new dimensions, so we don't need to call
606 // nsSVGUtils::InvalidateAndScheduleBoundsUpdate.
607 nsSVGEffects::InvalidateRenderingObservers(mFrame
);
608 mFrame
->InvalidateFrame();
611 if (aType
== imgINotificationObserver::SIZE_AVAILABLE
) {
612 // Called once the resource's dimensions have been obtained.
613 aRequest
->GetImage(getter_AddRefs(mFrame
->mImageContainer
));
614 mFrame
->InvalidateFrame();
615 nsSVGEffects::InvalidateRenderingObservers(mFrame
);
616 nsSVGUtils::ScheduleReflowSVG(mFrame
);