2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
#include "SVGImageFrame.h"
|
2017-05-23 22:11:13 +03:00
|
|
|
|
2012-03-26 15:58:59 +04:00
|
|
|
// Keep in (case-insensitive) order:
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
2014-03-31 15:53:54 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2023-03-01 22:04:17 +03:00
|
|
|
#include "mozilla/ComputedStyleInlines.h"
|
2021-11-27 14:47:40 +03:00
|
|
|
#include "mozilla/image/WebRenderImageProvider.h"
|
2020-05-15 21:49:52 +03:00
|
|
|
#include "mozilla/layers/RenderRootStateManager.h"
|
|
|
|
#include "mozilla/layers/WebRenderLayerManager.h"
|
2004-12-18 02:45:30 +03:00
|
|
|
#include "imgIContainer.h"
|
2021-05-13 19:24:07 +03:00
|
|
|
#include "ImageRegion.h"
|
2014-05-25 02:20:39 +04:00
|
|
|
#include "nsContainerFrame.h"
|
2013-01-06 13:32:01 +04:00
|
|
|
#include "nsIImageLoadingContent.h"
|
2009-04-05 13:52:14 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2012-10-12 20:11:22 +04:00
|
|
|
#include "imgINotificationObserver.h"
|
2019-05-24 15:40:12 +03:00
|
|
|
#include "SVGGeometryProperty.h"
|
2023-02-28 22:23:00 +03:00
|
|
|
#include "SVGGeometryFrame.h"
|
2019-04-06 09:02:28 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2021-05-13 19:24:07 +03:00
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
2020-07-11 05:20:20 +03:00
|
|
|
#include "mozilla/SVGContentUtils.h"
|
|
|
|
#include "mozilla/SVGImageContext.h"
|
|
|
|
#include "mozilla/SVGObserverUtils.h"
|
|
|
|
#include "mozilla/SVGUtils.h"
|
2018-02-09 19:17:10 +03:00
|
|
|
#include "mozilla/dom/MutationEventBinding.h"
|
2013-01-06 13:32:01 +04:00
|
|
|
#include "mozilla/dom/SVGImageElement.h"
|
2013-09-15 04:05:05 +04:00
|
|
|
#include "nsIReflowCallback.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2011-10-19 19:37:49 +04:00
|
|
|
|
2013-01-06 13:32:01 +04:00
|
|
|
using namespace mozilla::dom;
|
2014-03-31 15:53:54 +04:00
|
|
|
using namespace mozilla::gfx;
|
2016-07-14 07:47:06 +03:00
|
|
|
using namespace mozilla::image;
|
2020-05-15 21:49:52 +03:00
|
|
|
using namespace mozilla::dom::SVGPreserveAspectRatio_Binding;
|
2019-05-24 15:40:12 +03:00
|
|
|
namespace SVGT = SVGGeometryProperty::Tags;
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class SVGImageListener final : public imgINotificationObserver {
|
|
|
|
public:
|
|
|
|
explicit SVGImageListener(SVGImageFrame* aFrame);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_IMGINOTIFICATIONOBSERVER
|
|
|
|
|
|
|
|
void SetFrame(SVGImageFrame* frame) { mFrame = frame; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SVGImageListener() = default;
|
|
|
|
|
|
|
|
SVGImageFrame* mFrame;
|
|
|
|
};
|
|
|
|
|
2017-05-23 22:11:13 +03:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// nsQueryFrame methods
|
2020-06-25 23:42:49 +03:00
|
|
|
NS_QUERYFRAME_HEAD(SVGImageFrame)
|
|
|
|
NS_QUERYFRAME_ENTRY(SVGImageFrame)
|
2023-02-28 22:23:00 +03:00
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(SVGGeometryFrame)
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
nsIFrame* NS_NewSVGImageFrame(mozilla::PresShell* aPresShell,
|
|
|
|
mozilla::ComputedStyle* aStyle) {
|
|
|
|
return new (aPresShell)
|
|
|
|
mozilla::SVGImageFrame(aStyle, aPresShell->GetPresContext());
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(SVGImageFrame)
|
2009-09-12 20:49:24 +04:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
SVGImageFrame::~SVGImageFrame() {
|
2004-12-18 02:45:30 +03:00
|
|
|
// set the frame to null so we don't send messages to a dead object.
|
|
|
|
if (mListener) {
|
2017-08-27 01:58:38 +03:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
2004-12-18 02:45:30 +03:00
|
|
|
if (imageLoader) {
|
2017-07-19 21:15:12 +03:00
|
|
|
imageLoader->RemoveNativeObserver(mListener);
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
2020-06-25 23:42:49 +03:00
|
|
|
reinterpret_cast<SVGImageListener*>(mListener.get())->SetFrame(nullptr);
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mListener = nullptr;
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow) {
|
2015-03-03 14:08:59 +03:00
|
|
|
NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::image),
|
2013-01-08 07:22:41 +04:00
|
|
|
"Content is not an SVG image!");
|
2009-01-19 21:31:34 +03:00
|
|
|
|
2023-02-28 22:23:00 +03:00
|
|
|
SVGGeometryFrame::Init(aContent, aParent, aPrevInFlow);
|
2013-01-08 07:22:41 +04:00
|
|
|
|
2020-06-27 17:07:43 +03:00
|
|
|
if (HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
|
2016-03-26 19:07:03 +03:00
|
|
|
// Non-display frames are likely to be patterns, masks or the like.
|
|
|
|
// Treat them as always visible.
|
2017-03-22 08:32:48 +03:00
|
|
|
// This call must happen before the FrameCreated. This is because the
|
|
|
|
// primary frame pointer on our content node isn't set until after this
|
|
|
|
// function ends, so there is no way for the resulting OnVisibilityChange
|
|
|
|
// notification to get a frame. FrameCreated has a workaround for this in
|
|
|
|
// that it passes our frame around so it can be accessed. OnVisibilityChange
|
|
|
|
// doesn't have that workaround.
|
2016-09-12 08:19:08 +03:00
|
|
|
IncApproximateVisibleCount();
|
2016-03-26 19:07:03 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
mListener = new SVGImageListener(this);
|
2017-08-27 01:58:38 +03:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
2013-03-20 05:47:48 +04:00
|
|
|
if (!imageLoader) {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("Why is this not an image loading content?");
|
2013-03-20 05:47:48 +04:00
|
|
|
}
|
2011-02-01 07:50:01 +03:00
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
// We should have a PresContext now, so let's notify our image loader that
|
|
|
|
// we need to register any image animations with the refresh driver.
|
|
|
|
imageLoader->FrameCreated(this);
|
|
|
|
|
2017-07-19 21:15:12 +03:00
|
|
|
imageLoader->AddNativeObserver(mListener);
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::DestroyFrom(nsIFrame* aDestructRoot,
|
|
|
|
PostDestroyData& aPostDestroyData) {
|
2020-06-27 17:07:43 +03:00
|
|
|
if (HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
|
2016-09-12 08:19:08 +03:00
|
|
|
DecApproximateVisibleCount();
|
2016-03-26 19:07:03 +03:00
|
|
|
}
|
|
|
|
|
2013-09-15 04:05:05 +04:00
|
|
|
if (mReflowCallbackPosted) {
|
2017-11-09 05:00:48 +03:00
|
|
|
PresShell()->CancelReflowCallback(this);
|
2013-09-15 04:05:05 +04:00
|
|
|
mReflowCallbackPosted = false;
|
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
2020-07-07 01:29:42 +03:00
|
|
|
do_QueryInterface(nsIFrame::mContent);
|
2011-11-10 01:39:16 +04:00
|
|
|
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->FrameDestroyed(this);
|
|
|
|
}
|
|
|
|
|
2020-07-07 01:29:42 +03:00
|
|
|
nsIFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
2020-05-18 03:04:03 +03:00
|
|
|
/* virtual */
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::DidSetComputedStyle(ComputedStyle* aOldStyle) {
|
2023-02-28 22:23:00 +03:00
|
|
|
SVGGeometryFrame::DidSetComputedStyle(aOldStyle);
|
2020-05-18 03:04:03 +03:00
|
|
|
|
|
|
|
if (!mImageContainer || !aOldStyle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:56:57 +03:00
|
|
|
nsCOMPtr<imgIRequest> currentRequest;
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(currentRequest));
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleImageOrientation newOrientation =
|
|
|
|
StyleVisibility()->UsedImageOrientation(currentRequest);
|
|
|
|
StyleImageOrientation oldOrientation =
|
|
|
|
aOldStyle->StyleVisibility()->UsedImageOrientation(currentRequest);
|
2020-05-18 03:04:03 +03:00
|
|
|
|
2022-09-28 06:56:57 +03:00
|
|
|
if (oldOrientation != newOrientation) {
|
2020-05-18 03:04:03 +03:00
|
|
|
nsCOMPtr<imgIContainer> image(mImageContainer->Unwrap());
|
|
|
|
mImageContainer = nsLayoutUtils::OrientImage(image, newOrientation);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(heycam): We should handle aspect-ratio, like nsImageFrame does.
|
|
|
|
}
|
|
|
|
|
2004-12-18 02:45:30 +03:00
|
|
|
//----------------------------------------------------------------------
|
2006-03-02 23:22:19 +03:00
|
|
|
// nsIFrame methods:
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
nsresult SVGImageFrame::AttributeChanged(int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute, int32_t aModType) {
|
2012-03-20 16:15:53 +04:00
|
|
|
if (aNameSpaceID == kNameSpaceID_None) {
|
2019-04-20 13:26:19 +03:00
|
|
|
if (aAttribute == nsGkAtoms::preserveAspectRatio) {
|
2013-11-24 23:50:45 +04:00
|
|
|
// We don't paint the content of the image using display lists, therefore
|
|
|
|
// we have to invalidate for this children-only transform changes since
|
|
|
|
// there is no layer tree to notice that the transform changed and
|
|
|
|
// recomposite.
|
|
|
|
InvalidateFrame();
|
2012-03-20 16:15:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-19 19:37:49 +04:00
|
|
|
}
|
2017-05-06 22:51:59 +03:00
|
|
|
|
|
|
|
// Currently our SMIL implementation does not modify the DOM attributes. Once
|
|
|
|
// we implement the SVG 2 SMIL behaviour this can be removed
|
|
|
|
// SVGImageElement::AfterSetAttr's implementation will be sufficient.
|
2018-06-26 00:20:54 +03:00
|
|
|
if (aModType == MutationEvent_Binding::SMIL &&
|
2017-05-06 22:51:59 +03:00
|
|
|
aAttribute == nsGkAtoms::href &&
|
|
|
|
(aNameSpaceID == kNameSpaceID_XLink ||
|
|
|
|
aNameSpaceID == kNameSpaceID_None)) {
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* element = static_cast<SVGImageElement*>(GetContent());
|
2011-10-19 19:37:49 +04:00
|
|
|
|
2016-07-05 12:35:24 +03:00
|
|
|
bool hrefIsSet =
|
|
|
|
element->mStringAttributes[SVGImageElement::HREF].IsExplicitlySet() ||
|
|
|
|
element->mStringAttributes[SVGImageElement::XLINK_HREF]
|
|
|
|
.IsExplicitlySet();
|
|
|
|
if (hrefIsSet) {
|
2011-10-19 19:37:49 +04:00
|
|
|
element->LoadSVGImage(true, true);
|
|
|
|
} else {
|
|
|
|
element->CancelImageRequests(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:01:04 +03:00
|
|
|
return NS_OK;
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::OnVisibilityChange(
|
2017-02-13 20:07:40 +03:00
|
|
|
Visibility aNewVisibility, const Maybe<OnNonvisible>& aNonvisibleAction) {
|
2017-08-27 01:58:38 +03:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
2023-02-14 08:57:41 +03:00
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->OnVisibilityChange(aNewVisibility, aNonvisibleAction);
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-28 22:23:00 +03:00
|
|
|
SVGGeometryFrame::OnVisibilityChange(aNewVisibility, aNonvisibleAction);
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
gfx::Matrix SVGImageFrame::GetRasterImageTransform(int32_t aNativeWidth,
|
|
|
|
int32_t aNativeHeight) {
|
2006-09-19 20:35:29 +04:00
|
|
|
float x, y, width, height;
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* element = static_cast<SVGImageElement*>(GetContent());
|
2019-05-24 15:40:12 +03:00
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y, SVGT::Width, SVGT::Height>(
|
|
|
|
element, &x, &y, &width, &height);
|
2006-09-19 20:35:29 +04:00
|
|
|
|
2013-03-03 12:11:03 +04:00
|
|
|
Matrix viewBoxTM = SVGContentUtils::GetViewBoxTransform(
|
2012-09-22 23:26:05 +04:00
|
|
|
width, height, 0, 0, aNativeWidth, aNativeHeight,
|
|
|
|
element->mPreserveAspectRatio);
|
2009-07-23 12:35:59 +04:00
|
|
|
|
2014-08-29 23:42:07 +04:00
|
|
|
return viewBoxTM * gfx::Matrix::Translation(x, y);
|
2006-09-19 20:35:29 +04:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
gfx::Matrix SVGImageFrame::GetVectorImageTransform() {
|
2019-06-08 03:33:36 +03:00
|
|
|
float x, y;
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* element = static_cast<SVGImageElement*>(GetContent());
|
2019-06-08 03:33:36 +03:00
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y>(element, &x, &y);
|
2010-12-20 03:45:29 +03:00
|
|
|
|
|
|
|
// No viewBoxTM needed here -- our height/width overrides any concept of
|
|
|
|
// "native size" that the SVG image has, and it will handle viewBox and
|
|
|
|
// preserveAspectRatio on its own once we give it a region to draw into.
|
|
|
|
|
2014-08-29 23:42:07 +04:00
|
|
|
return gfx::Matrix::Translation(x, y);
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
bool SVGImageFrame::GetIntrinsicImageDimensions(
|
2019-06-08 03:33:36 +03:00
|
|
|
mozilla::gfx::Size& aSize, mozilla::AspectRatio& aAspectRatio) const {
|
2019-05-24 15:40:12 +03:00
|
|
|
if (!mImageContainer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 12:41:24 +03:00
|
|
|
ImageResolution resolution = mImageContainer->GetResolution();
|
|
|
|
|
2019-05-24 15:40:12 +03:00
|
|
|
int32_t width, height;
|
|
|
|
if (NS_FAILED(mImageContainer->GetWidth(&width))) {
|
2019-06-08 03:33:36 +03:00
|
|
|
aSize.width = -1;
|
|
|
|
} else {
|
|
|
|
aSize.width = width;
|
2021-05-05 12:41:24 +03:00
|
|
|
resolution.ApplyXTo(aSize.width);
|
2019-05-24 15:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(mImageContainer->GetHeight(&height))) {
|
2019-06-08 03:33:36 +03:00
|
|
|
aSize.height = -1;
|
|
|
|
} else {
|
|
|
|
aSize.height = height;
|
2021-05-05 12:41:24 +03:00
|
|
|
resolution.ApplyYTo(aSize.height);
|
2019-05-24 15:40:12 +03:00
|
|
|
}
|
|
|
|
|
2019-06-08 03:33:36 +03:00
|
|
|
Maybe<AspectRatio> asp = mImageContainer->GetIntrinsicRatio();
|
|
|
|
aAspectRatio = asp.valueOr(AspectRatio{});
|
2019-05-24 15:40:12 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
bool SVGImageFrame::TransformContextForPainting(gfxContext* aGfxContext,
|
|
|
|
const gfxMatrix& aTransform) {
|
2013-12-31 01:42:26 +04:00
|
|
|
gfx::Matrix imageTransform;
|
2010-12-20 03:45:29 +03:00
|
|
|
if (mImageContainer->GetType() == imgIContainer::TYPE_VECTOR) {
|
2014-08-29 23:42:07 +04:00
|
|
|
imageTransform = GetVectorImageTransform() * ToMatrix(aTransform);
|
2010-12-20 03:45:29 +03:00
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nativeWidth, nativeHeight;
|
2010-12-20 03:45:29 +03:00
|
|
|
if (NS_FAILED(mImageContainer->GetWidth(&nativeWidth)) ||
|
|
|
|
NS_FAILED(mImageContainer->GetHeight(&nativeHeight)) ||
|
|
|
|
nativeWidth == 0 || nativeHeight == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
2021-05-05 12:41:24 +03:00
|
|
|
mImageContainer->GetResolution().ApplyTo(nativeWidth, nativeHeight);
|
2014-08-29 23:42:07 +04:00
|
|
|
imageTransform = GetRasterImageTransform(nativeWidth, nativeHeight) *
|
|
|
|
ToMatrix(aTransform);
|
2013-11-09 03:14:53 +04:00
|
|
|
|
|
|
|
// NOTE: We need to cancel out the effects of Full-Page-Zoom, or else
|
|
|
|
// it'll get applied an extra time by DrawSingleUnscaledImage.
|
|
|
|
nscoord appUnitsPerDevPx = PresContext()->AppUnitsPerDevPixel();
|
|
|
|
gfxFloat pageZoomFactor =
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(appUnitsPerDevPx);
|
2014-09-10 21:29:35 +04:00
|
|
|
imageTransform.PreScale(pageZoomFactor, pageZoomFactor);
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
|
|
|
|
2011-01-11 08:57:19 +03:00
|
|
|
if (imageTransform.IsSingular()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-01-11 08:57:19 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 01:42:26 +04:00
|
|
|
aGfxContext->Multiply(ThebesMatrix(imageTransform));
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
|
|
|
|
2004-12-18 02:45:30 +03:00
|
|
|
//----------------------------------------------------------------------
|
2020-07-09 21:34:54 +03:00
|
|
|
// ISVGDisplayableFrame methods:
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
|
|
|
|
imgDrawingParams& aImgParams,
|
|
|
|
const nsIntRect* aDirtyRect) {
|
2017-05-18 23:03:41 +03:00
|
|
|
if (!StyleVisibility()->IsVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
2005-01-25 06:55:03 +03:00
|
|
|
|
2007-07-12 19:40:46 +04:00
|
|
|
float x, y, width, height;
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* imgElem = static_cast<SVGImageElement*>(GetContent());
|
2019-05-24 15:40:12 +03:00
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y, SVGT::Width, SVGT::Height>(
|
|
|
|
imgElem, &x, &y, &width, &height);
|
2012-03-03 13:21:09 +04:00
|
|
|
NS_ASSERTION(width > 0 && height > 0,
|
|
|
|
"Should only be painting things with valid width/height");
|
2007-07-12 19:40:46 +04:00
|
|
|
|
2006-12-07 01:23:10 +03:00
|
|
|
if (!mImageContainer) {
|
2004-12-18 02:45:30 +03:00
|
|
|
nsCOMPtr<imgIRequest> currentRequest;
|
2017-08-27 01:58:38 +03:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
2004-12-18 02:45:30 +03:00
|
|
|
if (imageLoader)
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(currentRequest));
|
|
|
|
|
|
|
|
if (currentRequest)
|
2006-12-07 01:23:10 +03:00
|
|
|
currentRequest->GetImage(getter_AddRefs(mImageContainer));
|
|
|
|
}
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2010-10-12 00:08:19 +04:00
|
|
|
if (mImageContainer) {
|
2014-10-31 23:08:54 +03:00
|
|
|
gfxContextAutoSaveRestore autoRestorer(&aContext);
|
2006-11-27 20:30:57 +03:00
|
|
|
|
2013-02-17 01:51:02 +04:00
|
|
|
if (StyleDisplay()->IsScrollableOverflow()) {
|
2010-10-12 00:08:19 +04:00
|
|
|
gfxRect clipRect =
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::GetClipRectForFrame(this, x, y, width, height);
|
|
|
|
SVGUtils::SetClipRect(&aContext, aTransform, clipRect);
|
2006-03-23 02:17:26 +03:00
|
|
|
}
|
|
|
|
|
2014-10-31 23:08:54 +03:00
|
|
|
if (!TransformContextForPainting(&aContext, aTransform)) {
|
2017-05-18 23:03:41 +03:00
|
|
|
return;
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
2010-10-12 00:08:19 +04:00
|
|
|
|
2007-01-04 18:05:39 +03:00
|
|
|
// fill-opacity doesn't affect <image>, so if we're allowed to
|
|
|
|
// optimize group opacity, the opacity used for compositing the
|
|
|
|
// image into the current canvas is just the group opacity.
|
|
|
|
float opacity = 1.0f;
|
2020-07-11 05:20:20 +03:00
|
|
|
if (SVGUtils::CanOptimizeOpacity(this)) {
|
2016-04-12 08:52:43 +03:00
|
|
|
opacity = StyleEffects()->mOpacity;
|
2007-01-04 18:05:39 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 08:52:42 +03:00
|
|
|
if (opacity != 1.0f ||
|
2020-03-15 00:24:11 +03:00
|
|
|
StyleEffects()->mMixBlendMode != StyleBlend::Normal) {
|
2015-11-11 18:15:39 +03:00
|
|
|
aContext.PushGroupForBlendBack(gfxContentType::COLOR_ALPHA, opacity);
|
2010-10-12 00:08:19 +04:00
|
|
|
}
|
2008-03-12 20:19:51 +03:00
|
|
|
|
2010-12-20 03:45:29 +03:00
|
|
|
nscoord appUnitsPerDevPx = PresContext()->AppUnitsPerDevPixel();
|
2010-10-12 00:08:19 +04:00
|
|
|
nsRect dirtyRect; // only used if aDirtyRect is non-null
|
|
|
|
if (aDirtyRect) {
|
2023-02-05 21:14:31 +03:00
|
|
|
NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
|
2012-07-20 22:12:29 +04:00
|
|
|
"Display lists handle dirty rect intersection test");
|
2015-04-21 18:04:57 +03:00
|
|
|
dirtyRect = ToAppUnits(*aDirtyRect, appUnitsPerDevPx);
|
2019-06-06 17:54:40 +03:00
|
|
|
|
|
|
|
// dirtyRect is relative to the outer <svg>, we should transform it
|
|
|
|
// down to <image>.
|
|
|
|
Rect dir(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height);
|
|
|
|
dir.Scale(1.f / AppUnitsPerCSSPixel());
|
|
|
|
|
|
|
|
// FIXME: This isn't correct if there is an inner <svg> enclosing
|
|
|
|
// the <image>. But that seems to be a quite obscure usecase, we can
|
|
|
|
// add a dedicated utility for that purpose to replace the GetCTM
|
|
|
|
// here if necessary.
|
2023-02-14 08:57:41 +03:00
|
|
|
auto mat = SVGContentUtils::GetCTM(imgElem, false);
|
2019-06-06 17:54:40 +03:00
|
|
|
if (mat.IsSingular()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mat.Invert();
|
|
|
|
dir = mat.TransformRect(dir);
|
|
|
|
|
|
|
|
// x, y offset of <image> is not included in CTM.
|
|
|
|
dir.MoveBy(-x, -y);
|
|
|
|
|
|
|
|
dir.Scale(AppUnitsPerCSSPixel());
|
|
|
|
dir.Round();
|
|
|
|
dirtyRect = nsRect(dir.x, dir.y, dir.width, dir.height);
|
2010-10-12 00:08:19 +04:00
|
|
|
}
|
2006-03-23 02:17:26 +03:00
|
|
|
|
2017-05-23 22:11:13 +03:00
|
|
|
uint32_t flags = aImgParams.imageFlags;
|
|
|
|
if (mForceSyncDecoding) {
|
|
|
|
flags |= imgIContainer::FLAG_SYNC_DECODE;
|
|
|
|
}
|
|
|
|
|
2010-12-20 03:45:29 +03:00
|
|
|
if (mImageContainer->GetType() == imgIContainer::TYPE_VECTOR) {
|
2013-03-01 00:22:43 +04:00
|
|
|
// Package up the attributes of this image element which can override the
|
2015-04-03 22:48:12 +03:00
|
|
|
// attributes of mImageContainer's internal SVG document. The 'width' &
|
|
|
|
// 'height' values we're passing in here are in CSS units (though they
|
|
|
|
// come from width/height *attributes* in SVG). They influence the region
|
|
|
|
// of the SVG image's internal document that is visible, in combination
|
|
|
|
// with preserveAspectRatio and viewBox.
|
2022-07-14 19:03:25 +03:00
|
|
|
const SVGImageContext context(
|
|
|
|
Some(CSSIntSize::Truncate(width, height)),
|
|
|
|
Some(imgElem->mPreserveAspectRatio.GetAnimValue()));
|
2013-03-01 00:22:43 +04:00
|
|
|
|
2015-04-03 22:48:12 +03:00
|
|
|
// For the actual draw operation to draw crisply (and at the right size),
|
|
|
|
// our destination rect needs to be |width|x|height|, *in dev pixels*.
|
|
|
|
LayoutDeviceSize devPxSize(width, height);
|
|
|
|
nsRect destRect(nsPoint(), LayoutDevicePixel::ToAppUnits(
|
|
|
|
devPxSize, appUnitsPerDevPx));
|
2010-12-20 03:45:29 +03:00
|
|
|
|
|
|
|
// Note: Can't use DrawSingleUnscaledImage for the TYPE_VECTOR case.
|
|
|
|
// That method needs our image to have a fixed native width & height,
|
|
|
|
// and that's not always true for TYPE_VECTOR images.
|
2017-05-18 23:03:41 +03:00
|
|
|
aImgParams.result &= nsLayoutUtils::DrawSingleImage(
|
2021-05-05 12:41:24 +03:00
|
|
|
aContext, PresContext(), mImageContainer,
|
2016-05-25 19:01:18 +03:00
|
|
|
nsLayoutUtils::GetSamplingFilterForFrame(this), destRect,
|
2010-12-20 03:45:29 +03:00
|
|
|
aDirtyRect ? dirtyRect : destRect, context, flags);
|
|
|
|
} else { // mImageContainer->GetType() == TYPE_RASTER
|
2017-05-18 23:03:41 +03:00
|
|
|
aImgParams.result &= nsLayoutUtils::DrawSingleUnscaledImage(
|
2010-12-20 03:45:29 +03:00
|
|
|
aContext, PresContext(), mImageContainer,
|
2016-05-25 19:01:18 +03:00
|
|
|
nsLayoutUtils::GetSamplingFilterForFrame(this), nsPoint(0, 0),
|
2022-07-14 19:03:25 +03:00
|
|
|
aDirtyRect ? &dirtyRect : nullptr, SVGImageContext(), flags);
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
2010-10-12 00:08:19 +04:00
|
|
|
|
2016-04-12 08:52:42 +03:00
|
|
|
if (opacity != 1.0f ||
|
2020-03-15 00:24:11 +03:00
|
|
|
StyleEffects()->mMixBlendMode != StyleBlend::Normal) {
|
2015-11-11 18:15:39 +03:00
|
|
|
aContext.PopGroupAndBlend();
|
2010-10-12 00:08:19 +04:00
|
|
|
}
|
|
|
|
// gfxContextAutoSaveRestore goes out of scope & cleans up our gfxContext
|
2004-12-18 02:45:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
bool SVGImageFrame::CreateWebRenderCommands(
|
2020-05-15 21:49:52 +03:00
|
|
|
mozilla::wr::DisplayListBuilder& aBuilder,
|
|
|
|
mozilla::wr::IpcResourceUpdateQueue& aResources,
|
|
|
|
const mozilla::layers::StackingContextHelper& aSc,
|
|
|
|
mozilla::layers::RenderRootStateManager* aManager,
|
2023-02-28 22:23:00 +03:00
|
|
|
nsDisplayListBuilder* aDisplayListBuilder, DisplaySVGGeometry* aItem,
|
2020-05-15 21:49:52 +03:00
|
|
|
bool aDryRun) {
|
|
|
|
if (!StyleVisibility()->IsVisible()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float opacity = 1.0f;
|
2020-07-11 05:20:20 +03:00
|
|
|
if (SVGUtils::CanOptimizeOpacity(this)) {
|
2020-05-15 21:49:52 +03:00
|
|
|
opacity = StyleEffects()->mOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opacity != 1.0f) {
|
|
|
|
// FIXME: not implemented, might be trivial
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (StyleEffects()->mMixBlendMode != StyleBlend::Normal) {
|
|
|
|
// FIXME: not implemented
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to setup the image
|
|
|
|
if (!mImageContainer) {
|
|
|
|
nsCOMPtr<imgIRequest> currentRequest;
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader =
|
|
|
|
do_QueryInterface(GetContent());
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(currentRequest));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentRequest) {
|
|
|
|
currentRequest->GetImage(getter_AddRefs(mImageContainer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mImageContainer) {
|
|
|
|
// nothing to draw (yet)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-11 12:05:20 +03:00
|
|
|
uint32_t flags = aDisplayListBuilder->GetImageDecodeFlags();
|
2021-10-22 14:50:09 +03:00
|
|
|
if (mForceSyncDecoding) {
|
|
|
|
flags |= imgIContainer::FLAG_SYNC_DECODE;
|
|
|
|
}
|
2020-05-15 21:49:52 +03:00
|
|
|
|
|
|
|
// Compute bounds of the image
|
|
|
|
nscoord appUnitsPerDevPx = PresContext()->AppUnitsPerDevPixel();
|
|
|
|
int32_t appUnitsPerCSSPixel = AppUnitsPerCSSPixel();
|
|
|
|
|
|
|
|
float x, y, width, height;
|
|
|
|
SVGImageElement* imgElem = static_cast<SVGImageElement*>(GetContent());
|
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y, SVGT::Width, SVGT::Height>(
|
|
|
|
imgElem, &x, &y, &width, &height);
|
|
|
|
NS_ASSERTION(width > 0 && height > 0,
|
|
|
|
"Should only be painting things with valid width/height");
|
|
|
|
|
|
|
|
auto toReferenceFrame = aItem->ToReferenceFrame();
|
|
|
|
auto appRect = nsLayoutUtils::RoundGfxRectToAppRect(Rect(0, 0, width, height),
|
|
|
|
appUnitsPerCSSPixel);
|
|
|
|
appRect += toReferenceFrame;
|
|
|
|
auto destRect = LayoutDeviceRect::FromAppUnits(appRect, appUnitsPerDevPx);
|
2022-03-23 23:28:55 +03:00
|
|
|
auto clipRect = destRect;
|
|
|
|
|
2020-05-15 21:49:52 +03:00
|
|
|
if (StyleDisplay()->IsScrollableOverflow()) {
|
2022-03-23 23:28:55 +03:00
|
|
|
// Apply potential non-trivial clip
|
|
|
|
auto cssClip = SVGUtils::GetClipRectForFrame(this, 0, 0, width, height);
|
|
|
|
auto appClip =
|
|
|
|
nsLayoutUtils::RoundGfxRectToAppRect(cssClip, appUnitsPerCSSPixel);
|
|
|
|
appClip += toReferenceFrame;
|
|
|
|
clipRect = LayoutDeviceRect::FromAppUnits(appClip, appUnitsPerDevPx);
|
|
|
|
|
2020-05-15 21:49:52 +03:00
|
|
|
// Apply preserveAspectRatio
|
|
|
|
if (mImageContainer->GetType() == imgIContainer::TYPE_RASTER) {
|
|
|
|
int32_t nativeWidth, nativeHeight;
|
|
|
|
if (NS_FAILED(mImageContainer->GetWidth(&nativeWidth)) ||
|
|
|
|
NS_FAILED(mImageContainer->GetHeight(&nativeHeight)) ||
|
|
|
|
nativeWidth == 0 || nativeHeight == 0) {
|
|
|
|
// Image has no size; nothing to draw
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:28:55 +03:00
|
|
|
mImageContainer->GetResolution().ApplyTo(nativeWidth, nativeHeight);
|
|
|
|
|
2020-05-15 21:49:52 +03:00
|
|
|
auto preserveAspectRatio = imgElem->mPreserveAspectRatio.GetAnimValue();
|
|
|
|
uint16_t align = preserveAspectRatio.GetAlign();
|
|
|
|
uint16_t meetOrSlice = preserveAspectRatio.GetMeetOrSlice();
|
|
|
|
|
|
|
|
// default to the defaults
|
|
|
|
if (align == SVG_PRESERVEASPECTRATIO_UNKNOWN) {
|
|
|
|
align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
|
|
|
|
}
|
|
|
|
if (meetOrSlice == SVG_MEETORSLICE_UNKNOWN) {
|
|
|
|
meetOrSlice = SVG_MEETORSLICE_MEET;
|
|
|
|
}
|
|
|
|
|
|
|
|
// aspect > 1 is horizontal
|
|
|
|
// aspect < 1 is vertical
|
|
|
|
float nativeAspect = ((float)nativeWidth) / ((float)nativeHeight);
|
|
|
|
float viewAspect = width / height;
|
|
|
|
|
|
|
|
// "Meet" is "fit image to view"; "Slice" is "cover view with image".
|
|
|
|
//
|
|
|
|
// Whether we meet or slice, one side of the destRect will always be
|
|
|
|
// perfectly spanned by our image. The only questions to answer are
|
|
|
|
// "which side won't span perfectly" and "should that side be grown
|
|
|
|
// or shrunk".
|
|
|
|
//
|
|
|
|
// Because we fit our image to the destRect, this all just reduces to:
|
|
|
|
// "if meet, shrink to fit. if slice, grow to fit."
|
|
|
|
if (align != SVG_PRESERVEASPECTRATIO_NONE && nativeAspect != viewAspect) {
|
|
|
|
// Slightly redundant bools, but they make the conditions clearer
|
|
|
|
bool tooTall = nativeAspect > viewAspect;
|
|
|
|
bool tooWide = nativeAspect < viewAspect;
|
|
|
|
if ((meetOrSlice == SVG_MEETORSLICE_MEET && tooTall) ||
|
|
|
|
(meetOrSlice == SVG_MEETORSLICE_SLICE && tooWide)) {
|
|
|
|
// Adjust height and realign y
|
|
|
|
auto oldHeight = destRect.height;
|
|
|
|
destRect.height = destRect.width / nativeAspect;
|
|
|
|
auto heightChange = oldHeight - destRect.height;
|
|
|
|
switch (align) {
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
|
|
|
|
// align to top (no-op)
|
|
|
|
break;
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMID:
|
|
|
|
// align to center
|
|
|
|
destRect.y += heightChange / 2.0f;
|
|
|
|
break;
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMAX:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
|
|
|
|
// align to bottom
|
|
|
|
destRect.y += heightChange;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown value for align");
|
|
|
|
}
|
|
|
|
} else if ((meetOrSlice == SVG_MEETORSLICE_MEET && tooWide) ||
|
|
|
|
(meetOrSlice == SVG_MEETORSLICE_SLICE && tooTall)) {
|
|
|
|
// Adjust width and realign x
|
|
|
|
auto oldWidth = destRect.width;
|
|
|
|
destRect.width = destRect.height * nativeAspect;
|
|
|
|
auto widthChange = oldWidth - destRect.width;
|
|
|
|
switch (align) {
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMINYMAX:
|
|
|
|
// align to left (no-op)
|
|
|
|
break;
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
|
|
|
|
// align to center
|
|
|
|
destRect.x += widthChange / 2.0f;
|
|
|
|
break;
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMID:
|
|
|
|
case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
|
|
|
|
// align to right
|
|
|
|
destRect.x += widthChange;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown value for align");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 19:03:25 +03:00
|
|
|
SVGImageContext svgContext;
|
2020-05-15 21:49:52 +03:00
|
|
|
if (mImageContainer->GetType() == imgIContainer::TYPE_VECTOR) {
|
2021-05-13 19:24:07 +03:00
|
|
|
if (StaticPrefs::image_svg_blob_image()) {
|
|
|
|
flags |= imgIContainer::FLAG_RECORD_BLOB;
|
|
|
|
}
|
2020-05-15 21:49:52 +03:00
|
|
|
// Forward preserveAspectRatio to inner SVGs
|
2022-07-14 19:03:25 +03:00
|
|
|
svgContext.SetViewportSize(Some(CSSIntSize::Truncate(width, height)));
|
|
|
|
svgContext.SetPreserveAspectRatio(
|
|
|
|
Some(imgElem->mPreserveAspectRatio.GetAnimValue()));
|
2020-05-15 21:49:52 +03:00
|
|
|
}
|
|
|
|
|
2021-05-13 19:24:07 +03:00
|
|
|
Maybe<ImageIntRegion> region;
|
2020-05-15 21:49:52 +03:00
|
|
|
IntSize decodeSize = nsLayoutUtils::ComputeImageContainerDrawingParameters(
|
2022-03-23 23:28:55 +03:00
|
|
|
mImageContainer, this, destRect, clipRect, aSc, flags, svgContext,
|
2021-05-13 19:24:07 +03:00
|
|
|
region);
|
2020-05-15 21:49:52 +03:00
|
|
|
|
2021-11-27 14:47:40 +03:00
|
|
|
RefPtr<image::WebRenderImageProvider> provider;
|
|
|
|
ImgDrawResult drawResult = mImageContainer->GetImageProvider(
|
2021-05-13 19:24:07 +03:00
|
|
|
aManager->LayerManager(), decodeSize, svgContext, region, flags,
|
2021-11-27 14:47:40 +03:00
|
|
|
getter_AddRefs(provider));
|
2020-05-15 21:49:52 +03:00
|
|
|
|
|
|
|
// While we got a container, it may not contain a fully decoded surface. If
|
|
|
|
// that is the case, and we have an image we were previously displaying which
|
|
|
|
// has a fully decoded surface, then we should prefer the previous image.
|
|
|
|
switch (drawResult) {
|
|
|
|
case ImgDrawResult::NOT_READY:
|
|
|
|
case ImgDrawResult::TEMPORARY_ERROR:
|
|
|
|
// nothing to draw (yet)
|
|
|
|
return true;
|
|
|
|
case ImgDrawResult::NOT_SUPPORTED:
|
|
|
|
// things we haven't implemented for WR yet
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
// image is ready to draw
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do any actual mutations to state if we're doing a dry run
|
|
|
|
// (used to decide if we're making this into an active layer)
|
|
|
|
if (!aDryRun) {
|
|
|
|
// If the image container is empty, we don't want to fallback. Any other
|
|
|
|
// failure will be due to resource constraints and fallback is unlikely to
|
|
|
|
// help us. Hence we can ignore the return value from PushImage.
|
2021-11-27 14:47:40 +03:00
|
|
|
if (provider) {
|
2021-12-03 06:14:01 +03:00
|
|
|
aManager->CommandBuilder().PushImageProvider(aItem, provider, drawResult,
|
|
|
|
aBuilder, aResources,
|
2022-03-23 23:28:55 +03:00
|
|
|
destRect, clipRect);
|
2020-05-15 21:49:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
nsIFrame* SVGImageFrame::GetFrameForPoint(const gfxPoint& aPoint) {
|
2020-06-27 17:07:43 +03:00
|
|
|
if (!HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD) && !GetHitTestFlags()) {
|
2015-01-19 18:06:01 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-07 11:09:31 +04:00
|
|
|
Rect rect;
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* element = static_cast<SVGImageElement*>(GetContent());
|
2019-05-24 15:40:12 +03:00
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y, SVGT::Width, SVGT::Height>(
|
|
|
|
element, &rect.x, &rect.y, &rect.width, &rect.height);
|
2014-08-07 11:09:31 +04:00
|
|
|
|
|
|
|
if (!rect.Contains(ToPoint(aPoint))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-12-20 03:45:29 +03:00
|
|
|
// Special case for raster images -- we only want to accept points that fall
|
2014-08-07 11:09:31 +04:00
|
|
|
// in the underlying image's (scaled to fit) native bounds. That region
|
|
|
|
// doesn't necessarily map to our <image> element's [x,y,width,height] if the
|
|
|
|
// raster image's aspect ratio is being preserved. We have to look up the
|
|
|
|
// native image size & our viewBox transform in order to filter out points
|
|
|
|
// that fall outside that area. (This special case doesn't apply to vector
|
|
|
|
// images because they don't limit their drawing to explicit "native
|
|
|
|
// bounds" -- they have an infinite canvas on which to place content.)
|
2013-02-17 01:51:02 +04:00
|
|
|
if (StyleDisplay()->IsScrollableOverflow() && mImageContainer) {
|
2010-12-20 03:45:29 +03:00
|
|
|
if (mImageContainer->GetType() == imgIContainer::TYPE_RASTER) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nativeWidth, nativeHeight;
|
2010-12-20 03:45:29 +03:00
|
|
|
if (NS_FAILED(mImageContainer->GetWidth(&nativeWidth)) ||
|
|
|
|
NS_FAILED(mImageContainer->GetHeight(&nativeHeight)) ||
|
|
|
|
nativeWidth == 0 || nativeHeight == 0) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
2021-05-05 12:41:24 +03:00
|
|
|
mImageContainer->GetResolution().ApplyTo(nativeWidth, nativeHeight);
|
2014-08-07 11:09:31 +04:00
|
|
|
Matrix viewBoxTM = SVGContentUtils::GetViewBoxTransform(
|
|
|
|
rect.width, rect.height, 0, 0, nativeWidth, nativeHeight,
|
|
|
|
element->mPreserveAspectRatio);
|
2020-07-11 05:20:20 +03:00
|
|
|
if (!SVGUtils::HitTestRect(viewBoxTM, 0, 0, nativeWidth, nativeHeight,
|
|
|
|
aPoint.x - rect.x, aPoint.y - rect.y)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-12-20 03:45:29 +03:00
|
|
|
}
|
2006-09-19 20:35:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 11:09:31 +04:00
|
|
|
return this;
|
2006-09-19 20:35:29 +04:00
|
|
|
}
|
|
|
|
|
2006-06-21 19:42:28 +04:00
|
|
|
//----------------------------------------------------------------------
|
2023-02-28 22:23:00 +03:00
|
|
|
// SVGGeometryFrame methods:
|
|
|
|
|
|
|
|
// Lie about our fill/stroke so that covered region and hit detection work
|
|
|
|
// properly
|
2009-08-31 15:26:23 +04:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::ReflowSVG() {
|
2020-07-11 05:20:20 +03:00
|
|
|
NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
|
2012-07-22 04:01:44 +04:00
|
|
|
"This call is probably a wasteful mistake");
|
2012-03-20 16:15:53 +04:00
|
|
|
|
2020-06-27 17:07:43 +03:00
|
|
|
MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
|
2015-02-10 01:34:50 +03:00
|
|
|
"ReflowSVG mechanism not designed for this");
|
2012-03-20 16:15:53 +04:00
|
|
|
|
2020-07-11 05:20:20 +03:00
|
|
|
if (!SVGUtils::NeedsReflowSVG(this)) {
|
2012-03-20 16:15:53 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-08-31 15:26:23 +04:00
|
|
|
|
2014-03-31 15:53:54 +04:00
|
|
|
float x, y, width, height;
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGImageElement* element = static_cast<SVGImageElement*>(GetContent());
|
2019-05-24 15:40:12 +03:00
|
|
|
SVGGeometryProperty::ResolveAll<SVGT::X, SVGT::Y, SVGT::Width, SVGT::Height>(
|
|
|
|
element, &x, &y, &width, &height);
|
2014-03-31 15:53:54 +04:00
|
|
|
|
|
|
|
Rect extent(x, y, width, height);
|
2009-08-31 15:26:23 +04:00
|
|
|
|
|
|
|
if (!extent.IsEmpty()) {
|
2012-02-10 16:33:18 +04:00
|
|
|
mRect = nsLayoutUtils::RoundGfxRectToAppRect(extent, AppUnitsPerCSSPixel());
|
2012-03-20 16:15:53 +04:00
|
|
|
} else {
|
|
|
|
mRect.SetEmpty();
|
2009-08-31 15:26:23 +04:00
|
|
|
}
|
|
|
|
|
2023-02-14 11:28:48 +03:00
|
|
|
if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
|
2012-05-17 08:05:09 +04:00
|
|
|
// Make sure we have our filter property (if any) before calling
|
|
|
|
// FinishAndStoreOverflow (subsequent filter changes are handled off
|
|
|
|
// nsChangeHint_UpdateEffects):
|
2017-08-30 17:58:31 +03:00
|
|
|
SVGObserverUtils::UpdateEffects(this);
|
2013-09-15 04:05:05 +04:00
|
|
|
|
|
|
|
if (!mReflowCallbackPosted) {
|
2013-09-15 04:05:06 +04:00
|
|
|
mReflowCallbackPosted = true;
|
2019-04-06 09:02:28 +03:00
|
|
|
PresShell()->PostReflowCallback(this);
|
2013-09-15 04:05:05 +04:00
|
|
|
}
|
2012-05-17 08:05:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRect overflow = nsRect(nsPoint(0, 0), mRect.Size());
|
2020-11-18 03:08:12 +03:00
|
|
|
OverflowAreas overflowAreas(overflow, overflow);
|
2012-05-17 08:05:09 +04:00
|
|
|
FinishAndStoreOverflow(overflowAreas, mRect.Size());
|
|
|
|
|
2017-08-24 12:55:28 +03:00
|
|
|
RemoveStateBits(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
|
|
|
|
NS_FRAME_HAS_DIRTY_CHILDREN);
|
2012-03-20 16:15:53 +04:00
|
|
|
|
2012-11-28 14:34:58 +04:00
|
|
|
// Invalidate, but only if this is not our first reflow (since if it is our
|
|
|
|
// first reflow then we haven't had our first paint yet).
|
2020-06-27 17:07:43 +03:00
|
|
|
if (!GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
|
2012-11-28 14:34:58 +04:00
|
|
|
InvalidateFrame();
|
2012-03-20 16:15:53 +04:00
|
|
|
}
|
2009-08-31 15:26:23 +04:00
|
|
|
}
|
2005-04-02 00:49:42 +04:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
bool SVGImageFrame::ReflowFinished() {
|
2013-09-15 04:05:05 +04:00
|
|
|
mReflowCallbackPosted = false;
|
|
|
|
|
2016-03-26 00:49:43 +03:00
|
|
|
// XXX(seth): We don't need this. The purpose of updating visibility
|
|
|
|
// synchronously is to ensure that animated images start animating
|
|
|
|
// immediately. In the short term, however,
|
|
|
|
// nsImageLoadingContent::OnUnlockedDraw() is enough to ensure that
|
|
|
|
// animations start as soon as the image is painted for the first time, and in
|
|
|
|
// the long term we want to update visibility information from the display
|
|
|
|
// list whenever we paint, so we don't actually need to do this. However, to
|
|
|
|
// avoid behavior changes during the transition from the old image visibility
|
|
|
|
// code, we'll leave it in for now.
|
|
|
|
UpdateVisibilitySynchronously();
|
2013-09-15 04:05:05 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageFrame::ReflowCallbackCanceled() { mReflowCallbackPosted = false; }
|
2013-09-15 04:05:05 +04:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
uint16_t SVGImageFrame::GetHitTestFlags() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t flags = 0;
|
2005-04-02 00:49:42 +04:00
|
|
|
|
2021-10-07 20:00:36 +03:00
|
|
|
switch (Style()->PointerEvents()) {
|
2019-12-12 13:24:16 +03:00
|
|
|
case StylePointerEvents::None:
|
2005-04-02 00:49:42 +04:00
|
|
|
break;
|
2019-12-12 13:24:16 +03:00
|
|
|
case StylePointerEvents::Visiblepainted:
|
|
|
|
case StylePointerEvents::Auto:
|
2013-02-17 01:51:02 +04:00
|
|
|
if (StyleVisibility()->IsVisible()) {
|
2005-04-02 00:49:42 +04:00
|
|
|
/* XXX: should check pixel transparency */
|
2011-07-08 17:20:14 +04:00
|
|
|
flags |= SVG_HIT_TEST_FILL;
|
2005-04-02 00:49:42 +04:00
|
|
|
}
|
|
|
|
break;
|
2019-12-12 13:24:16 +03:00
|
|
|
case StylePointerEvents::Visiblefill:
|
|
|
|
case StylePointerEvents::Visiblestroke:
|
|
|
|
case StylePointerEvents::Visible:
|
2013-02-17 01:51:02 +04:00
|
|
|
if (StyleVisibility()->IsVisible()) {
|
2011-07-08 17:20:14 +04:00
|
|
|
flags |= SVG_HIT_TEST_FILL;
|
2005-04-02 00:49:42 +04:00
|
|
|
}
|
|
|
|
break;
|
2019-12-12 13:24:16 +03:00
|
|
|
case StylePointerEvents::Painted:
|
2005-04-02 00:49:42 +04:00
|
|
|
/* XXX: should check pixel transparency */
|
2011-07-08 17:20:14 +04:00
|
|
|
flags |= SVG_HIT_TEST_FILL;
|
2005-04-02 00:49:42 +04:00
|
|
|
break;
|
2019-12-12 13:24:16 +03:00
|
|
|
case StylePointerEvents::Fill:
|
|
|
|
case StylePointerEvents::Stroke:
|
|
|
|
case StylePointerEvents::All:
|
2011-07-08 17:20:14 +04:00
|
|
|
flags |= SVG_HIT_TEST_FILL;
|
2005-04-02 00:49:42 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("not reached");
|
|
|
|
break;
|
|
|
|
}
|
2006-06-20 21:01:09 +04:00
|
|
|
|
2011-07-08 17:20:14 +04:00
|
|
|
return flags;
|
2005-04-02 00:49:42 +04:00
|
|
|
}
|
|
|
|
|
2023-03-13 19:57:30 +03:00
|
|
|
void SVGImageFrame::NotifySVGChanged(uint32_t aFlags) {
|
|
|
|
MOZ_ASSERT(aFlags & (TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED),
|
|
|
|
"Invalidation logic may need adjusting");
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:47:47 +03:00
|
|
|
SVGBBox SVGImageFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
if (aToBBoxUserspace.IsSingular()) {
|
|
|
|
// XXX ReportToConsole
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aFlags & SVGUtils::eForGetClientRects) &&
|
|
|
|
aToBBoxUserspace.PreservesAxisAlignedRectangles()) {
|
|
|
|
Rect rect = NSRectToRect(mRect, AppUnitsPerCSSPixel());
|
|
|
|
return aToBBoxUserspace.TransformBounds(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* element = static_cast<SVGImageElement*>(GetContent());
|
|
|
|
|
|
|
|
return element->GeometryBounds(aToBBoxUserspace);
|
|
|
|
}
|
|
|
|
|
2004-12-18 02:45:30 +03:00
|
|
|
//----------------------------------------------------------------------
|
2020-06-25 23:42:49 +03:00
|
|
|
// SVGImageListener implementation
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
NS_IMPL_ISUPPORTS(SVGImageListener, imgINotificationObserver)
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
SVGImageListener::SVGImageListener(SVGImageFrame* aFrame) : mFrame(aFrame) {}
|
2004-12-18 02:45:30 +03:00
|
|
|
|
2020-06-25 23:42:49 +03:00
|
|
|
void SVGImageListener::Notify(imgIRequest* aRequest, int32_t aType,
|
|
|
|
const nsIntRect* aData) {
|
2012-10-12 05:58:24 +04:00
|
|
|
if (!mFrame) {
|
2020-03-12 07:25:34 +03:00
|
|
|
return;
|
2018-12-12 09:32:44 +03:00
|
|
|
}
|
2007-01-18 20:46:36 +03:00
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
|
2013-06-10 04:14:01 +04:00
|
|
|
mFrame->InvalidateFrame();
|
2014-11-06 12:30:11 +03:00
|
|
|
nsLayoutUtils::PostRestyleEvent(mFrame->GetContent()->AsElement(),
|
2019-03-14 14:47:50 +03:00
|
|
|
RestyleHint{0},
|
2014-11-06 12:30:11 +03:00
|
|
|
nsChangeHint_InvalidateRenderingObservers);
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::ScheduleReflowSVG(mFrame);
|
2012-10-12 20:11:22 +04:00
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::FRAME_UPDATE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
// No new dimensions, so we don't need to call
|
2020-07-11 05:20:20 +03:00
|
|
|
// SVGUtils::InvalidateAndScheduleBoundsUpdate.
|
2014-11-06 12:30:11 +03:00
|
|
|
nsLayoutUtils::PostRestyleEvent(mFrame->GetContent()->AsElement(),
|
2019-03-14 14:47:50 +03:00
|
|
|
RestyleHint{0},
|
2014-11-06 12:30:11 +03:00
|
|
|
nsChangeHint_InvalidateRenderingObservers);
|
2013-06-10 04:14:01 +04:00
|
|
|
mFrame->InvalidateFrame();
|
2012-10-12 20:11:22 +04:00
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
// Called once the resource's dimensions have been obtained.
|
2018-10-12 21:25:49 +03:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
aRequest->GetImage(getter_AddRefs(image));
|
|
|
|
if (image) {
|
2022-09-28 06:56:57 +03:00
|
|
|
StyleImageOrientation orientation =
|
|
|
|
mFrame->StyleVisibility()->UsedImageOrientation(aRequest);
|
|
|
|
image = nsLayoutUtils::OrientImage(image, orientation);
|
2018-10-12 21:25:49 +03:00
|
|
|
image->SetAnimationMode(mFrame->PresContext()->ImageAnimationMode());
|
2020-02-13 17:38:48 +03:00
|
|
|
mFrame->mImageContainer = std::move(image);
|
2018-10-12 21:25:49 +03:00
|
|
|
}
|
2013-06-10 04:14:01 +04:00
|
|
|
mFrame->InvalidateFrame();
|
2014-11-06 12:30:11 +03:00
|
|
|
nsLayoutUtils::PostRestyleEvent(mFrame->GetContent()->AsElement(),
|
2019-03-14 14:47:50 +03:00
|
|
|
RestyleHint{0},
|
2014-11-06 12:30:11 +03:00
|
|
|
nsChangeHint_InvalidateRenderingObservers);
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::ScheduleReflowSVG(mFrame);
|
2012-10-12 20:11:22 +04:00
|
|
|
}
|
2007-01-18 20:46:36 +03:00
|
|
|
}
|
2020-06-25 23:42:49 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|