Bug 1922208 - Don't use Maybe<> to represent no ratio in images. r=dholbert

AspectRatio already has a way of representing a null / invalid ratio,
and there are some cases (broken sizes or invalid SVGOuterSVGFrame
ratio) where we might get Some(AspectRatio()).

It's not a meaningfully different state, and IMO we shouldn't treat it
differently.

Differential Revision: https://phabricator.services.mozilla.com/D224212
This commit is contained in:
Emilio Cobos Álvarez
2024-10-04 07:59:28 +00:00
parent c7589efeac
commit d601856426
14 changed files with 36 additions and 60 deletions

View File

@@ -205,12 +205,11 @@ ClippedImage::GetIntrinsicSize(nsSize* aSize) {
return NS_OK; return NS_OK;
} }
Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() { AspectRatio ClippedImage::GetIntrinsicRatio() {
if (!ShouldClip()) { if (!ShouldClip()) {
return InnerImage()->GetIntrinsicRatio(); return InnerImage()->GetIntrinsicRatio();
} }
return AspectRatio::FromSize(mClip.Width(), mClip.Height());
return Some(AspectRatio::FromSize(mClip.Width(), mClip.Height()));
} }
NS_IMETHODIMP_(already_AddRefed<SourceSurface>) NS_IMETHODIMP_(already_AddRefed<SourceSurface>)

View File

@@ -36,7 +36,7 @@ class ClippedImage : public ImageWrapper {
NS_IMETHOD GetWidth(int32_t* aWidth) override; NS_IMETHOD GetWidth(int32_t* aWidth) override;
NS_IMETHOD GetHeight(int32_t* aHeight) override; NS_IMETHOD GetHeight(int32_t* aHeight) override;
NS_IMETHOD GetIntrinsicSize(nsSize* aSize) override; NS_IMETHOD GetIntrinsicSize(nsSize* aSize) override;
Maybe<AspectRatio> GetIntrinsicRatio() override; AspectRatio GetIntrinsicRatio() override;
NS_IMETHOD_(already_AddRefed<SourceSurface>) NS_IMETHOD_(already_AddRefed<SourceSurface>)
GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override; GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override;
NS_IMETHOD_(already_AddRefed<SourceSurface>) NS_IMETHOD_(already_AddRefed<SourceSurface>)

View File

@@ -104,9 +104,9 @@ DynamicImage::GetIntrinsicSize(nsSize* aSize) {
return NS_OK; return NS_OK;
} }
Maybe<AspectRatio> DynamicImage::GetIntrinsicRatio() { AspectRatio DynamicImage::GetIntrinsicRatio() {
auto size = mDrawable->Size(); auto size = mDrawable->Size();
return Some(AspectRatio::FromSize(size.width, size.height)); return AspectRatio::FromSize(size.width, size.height);
} }
NS_IMETHODIMP_(Orientation) NS_IMETHODIMP_(Orientation)

View File

@@ -119,7 +119,7 @@ ImageWrapper::GetIntrinsicSize(nsSize* aSize) {
return mInnerImage->GetIntrinsicSize(aSize); return mInnerImage->GetIntrinsicSize(aSize);
} }
Maybe<AspectRatio> ImageWrapper::GetIntrinsicRatio() { AspectRatio ImageWrapper::GetIntrinsicRatio() {
return mInnerImage->GetIntrinsicRatio(); return mInnerImage->GetIntrinsicRatio();
} }

View File

@@ -66,10 +66,10 @@ OrientedImage::GetIntrinsicSize(nsSize* aSize) {
return rv; return rv;
} }
Maybe<AspectRatio> OrientedImage::GetIntrinsicRatio() { AspectRatio OrientedImage::GetIntrinsicRatio() {
Maybe<AspectRatio> ratio = InnerImage()->GetIntrinsicRatio(); AspectRatio ratio = InnerImage()->GetIntrinsicRatio();
if (ratio && mOrientation.SwapsWidthAndHeight()) { if (ratio && mOrientation.SwapsWidthAndHeight()) {
ratio = Some(ratio->Inverted()); ratio = ratio.Inverted();
} }
return ratio; return ratio;
} }

View File

@@ -31,7 +31,7 @@ class OrientedImage : public ImageWrapper {
NS_IMETHOD GetHeight(int32_t* aHeight) override; NS_IMETHOD GetHeight(int32_t* aHeight) override;
nsresult GetNativeSizes(nsTArray<gfx::IntSize>& aNativeSizes) override; nsresult GetNativeSizes(nsTArray<gfx::IntSize>& aNativeSizes) override;
NS_IMETHOD GetIntrinsicSize(nsSize* aSize) override; NS_IMETHOD GetIntrinsicSize(nsSize* aSize) override;
Maybe<AspectRatio> GetIntrinsicRatio() override; AspectRatio GetIntrinsicRatio() override;
NS_IMETHOD_(already_AddRefed<SourceSurface>) NS_IMETHOD_(already_AddRefed<SourceSurface>)
GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override; GetFrame(uint32_t aWhichFrame, uint32_t aFlags) override;
NS_IMETHOD_(already_AddRefed<SourceSurface>) NS_IMETHOD_(already_AddRefed<SourceSurface>)

View File

@@ -256,12 +256,11 @@ RasterImage::GetIntrinsicSize(nsSize* aSize) {
} }
//****************************************************************************** //******************************************************************************
Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() { AspectRatio RasterImage::GetIntrinsicRatio() {
if (mError) { if (mError) {
return Nothing(); return {};
} }
return AspectRatio::FromSize(mSize.width, mSize.height);
return Some(AspectRatio::FromSize(mSize.width, mSize.height));
} }
NS_IMETHODIMP_(Orientation) NS_IMETHODIMP_(Orientation)

View File

@@ -596,10 +596,9 @@ class ImageSurfaceCache {
// available. If our guess was too small, don't use factor-of-scaling. // available. If our guess was too small, don't use factor-of-scaling.
MOZ_ASSERT(mIsVectorImage); MOZ_ASSERT(mIsVectorImage);
factorSize = IntSize(100, 100); factorSize = IntSize(100, 100);
Maybe<AspectRatio> aspectRatio = image->GetIntrinsicRatio(); if (AspectRatio aspectRatio = image->GetIntrinsicRatio()) {
if (aspectRatio && *aspectRatio) {
factorSize.width = factorSize.width =
NSToIntRound(aspectRatio->ApplyToFloat(float(factorSize.height))); NSToIntRound(aspectRatio.ApplyToFloat(float(factorSize.height)));
if (factorSize.IsEmpty()) { if (factorSize.IsEmpty()) {
return aSize; return aSize;
} }

View File

@@ -580,17 +580,15 @@ VectorImage::GetIntrinsicSize(nsSize* aSize) {
} }
//****************************************************************************** //******************************************************************************
Maybe<AspectRatio> VectorImage::GetIntrinsicRatio() { AspectRatio VectorImage::GetIntrinsicRatio() {
if (mError || !mIsFullyLoaded) { if (mError || !mIsFullyLoaded) {
return Nothing(); return {};
} }
nsIFrame* rootFrame = mSVGDocumentWrapper->GetRootLayoutFrame(); nsIFrame* rootFrame = mSVGDocumentWrapper->GetRootLayoutFrame();
if (!rootFrame) { if (!rootFrame) {
return Nothing(); return {};
} }
return rootFrame->GetIntrinsicRatio();
return Some(rootFrame->GetIntrinsicRatio());
} }
NS_IMETHODIMP_(Orientation) NS_IMETHODIMP_(Orientation)

View File

@@ -21,9 +21,12 @@ webidl Document;
#include "limits.h" #include "limits.h"
class gfxContext; class gfxContext;
class nsIFrame;
namespace mozilla { namespace mozilla {
struct AspectRatio; class TimeStamp;
class SVGImageContext;
struct MediaFeatureChange;
namespace gfx { namespace gfx {
class SourceSurface; class SourceSurface;
@@ -33,31 +36,19 @@ class WindowRenderer;
namespace layers { namespace layers {
class ImageContainer; class ImageContainer;
} }
}
class nsIFrame;
namespace mozilla {
class TimeStamp;
class SVGImageContext;
struct MediaFeatureChange;
}
namespace mozilla {
namespace image { namespace image {
class ImageRegion; class ImageRegion;
class ImageIntRegion; class ImageIntRegion;
class WebRenderImageProvider; class WebRenderImageProvider;
struct Orientation; struct Orientation;
struct Resolution; struct Resolution;
} }
} }
%} %}
native MaybeAspectRatio(mozilla::Maybe<mozilla::AspectRatio>); native AspectRatio(mozilla::AspectRatio);
native ImgDrawResult(mozilla::image::ImgDrawResult); native ImgDrawResult(mozilla::image::ImgDrawResult);
[ptr] native gfxContext(gfxContext); [ptr] native gfxContext(gfxContext);
[ref] native gfxMatrix(gfxMatrix); [ref] native gfxMatrix(gfxMatrix);
@@ -115,10 +106,11 @@ interface imgIContainer : nsISupports
[noscript] readonly attribute nsSize intrinsicSize; [noscript] readonly attribute nsSize intrinsicSize;
/** /**
* The (dimensionless) intrinsic ratio of this image. In the case of any * The (dimensionless) intrinsic ratio of this image. Might return a
* error, Nothing() will be returned. * degenerate ratio (one that returns 'false' when coerced to a bool)
* if the image is in an error state, or there's no ratio.
*/ */
[notxpcom, nostdcall] readonly attribute MaybeAspectRatio intrinsicRatio; [notxpcom, nostdcall] readonly attribute AspectRatio intrinsicRatio;
/** /**
* The x coordinate of the image's hotspot, or 0 if there is no hotspot. * The x coordinate of the image's hotspot, or 0 if there is no hotspot.

View File

@@ -6215,8 +6215,7 @@ void nsLayoutUtils::ComputeSizeForDrawing(
/* outparam */ bool& aGotHeight) { /* outparam */ bool& aGotHeight) {
aGotWidth = NS_SUCCEEDED(aImage->GetWidth(&aImageSize.width)); aGotWidth = NS_SUCCEEDED(aImage->GetWidth(&aImageSize.width));
aGotHeight = NS_SUCCEEDED(aImage->GetHeight(&aImageSize.height)); aGotHeight = NS_SUCCEEDED(aImage->GetHeight(&aImageSize.height));
Maybe<AspectRatio> intrinsicRatio = aImage->GetIntrinsicRatio(); aIntrinsicRatio = aImage->GetIntrinsicRatio();
aIntrinsicRatio = intrinsicRatio.valueOr(AspectRatio());
if (aGotWidth) { if (aGotWidth) {
aResolution.ApplyXTo(aImageSize.width); aResolution.ApplyXTo(aImageSize.width);
@@ -6224,13 +6223,6 @@ void nsLayoutUtils::ComputeSizeForDrawing(
if (aGotHeight) { if (aGotHeight) {
aResolution.ApplyYTo(aImageSize.height); aResolution.ApplyYTo(aImageSize.height);
} }
if (!(aGotWidth && aGotHeight) && intrinsicRatio.isNothing()) {
// We hit an error (say, because the image failed to load or couldn't be
// decoded) and should return zero size.
aGotWidth = aGotHeight = true;
aImageSize = CSSIntSize(0, 0);
}
} }
/* static */ /* static */

View File

@@ -924,8 +924,8 @@ AspectRatio nsImageFrame::ComputeIntrinsicRatioForImage(
} }
if (aImage) { if (aImage) {
if (Maybe<AspectRatio> fromImage = aImage->GetIntrinsicRatio()) { if (AspectRatio fromImage = aImage->GetIntrinsicRatio()) {
return *fromImage; return fromImage;
} }
} }
if (ShouldUseMappedAspectRatio()) { if (ShouldUseMappedAspectRatio()) {

View File

@@ -249,7 +249,6 @@ bool SVGImageFrame::GetIntrinsicImageDimensions(
} }
ImageResolution resolution = mImageContainer->GetResolution(); ImageResolution resolution = mImageContainer->GetResolution();
int32_t width, height; int32_t width, height;
if (NS_FAILED(mImageContainer->GetWidth(&width))) { if (NS_FAILED(mImageContainer->GetWidth(&width))) {
aSize.width = -1; aSize.width = -1;
@@ -265,9 +264,7 @@ bool SVGImageFrame::GetIntrinsicImageDimensions(
resolution.ApplyYTo(aSize.height); resolution.ApplyYTo(aSize.height);
} }
Maybe<AspectRatio> asp = mImageContainer->GetIntrinsicRatio(); aAspectRatio = mImageContainer->GetIntrinsicRatio();
aAspectRatio = asp.valueOr(AspectRatio{});
return true; return true;
} }

View File

@@ -515,16 +515,16 @@ nsresult nsCocoaUtils::CreateNSImageFromImageContainer(
{ {
const bool gotWidth = NS_SUCCEEDED(aImage->GetWidth(&width)); const bool gotWidth = NS_SUCCEEDED(aImage->GetWidth(&width));
const bool gotHeight = NS_SUCCEEDED(aImage->GetHeight(&height)); const bool gotHeight = NS_SUCCEEDED(aImage->GetHeight(&height));
if (auto ratio = aImage->GetIntrinsicRatio(); ratio && *ratio) { if (auto ratio = aImage->GetIntrinsicRatio()) {
if (gotWidth != gotHeight) { if (gotWidth != gotHeight) {
if (gotWidth) { if (gotWidth) {
height = ratio->Inverted().ApplyTo(width); height = ratio.Inverted().ApplyTo(width);
} else { } else {
width = ratio->ApplyTo(height); width = ratio.ApplyTo(height);
} }
} else if (!gotWidth) { } else if (!gotWidth) {
height = std::ceil(aPreferredSize.height); height = std::ceil(aPreferredSize.height);
width = ratio->ApplyTo(height); width = ratio.ApplyTo(height);
} }
} }
} }