From 8a2bc8ef4dc12e1f5b5f482f61eccb4bb5b3f395 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Fri, 2 Aug 2024 10:15:50 +0200 Subject: [PATCH] Issue #1925 - Convert NS_FULL_TO_HALF_CORNER to a constexpr function. Note: Because the new function checks types, we need to change the fullCorner type in `nsComputedDOMStyle::GetEllipseRadii()` and `StyleAnimationValue::ExtractComputedValue()` to `Corner` instead of the underlying base type. --- gfx/2d/Types.h | 5 +++++ layout/style/StyleAnimationValue.cpp | 10 +++++----- layout/style/nsComputedDOMStyle.cpp | 10 +++++----- layout/style/nsComputedDOMStyle.h | 2 +- layout/style/nsRuleNode.cpp | 12 ++++++------ layout/style/nsStyleConsts.h | 2 -- layout/style/nsStyleCoord.cpp | 12 ++++++------ 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/gfx/2d/Types.h b/gfx/2d/Types.h index 4a3b26c811..923baf03e0 100644 --- a/gfx/2d/Types.h +++ b/gfx/2d/Types.h @@ -469,6 +469,11 @@ constexpr Corner HalfToFullCorner(HalfCorner aHalfCorner) return Corner(aHalfCorner / 2); } +constexpr HalfCorner FullToHalfCorner(Corner aCorner, bool aIsVertical) +{ + return HalfCorner(aCorner * 2 + aIsVertical); +} + } // namespace mozilla #endif /* MOZILLA_GFX_TYPES_H_ */ diff --git a/layout/style/StyleAnimationValue.cpp b/layout/style/StyleAnimationValue.cpp index e007d8335e..f7a9625211 100644 --- a/layout/style/StyleAnimationValue.cpp +++ b/layout/style/StyleAnimationValue.cpp @@ -3945,9 +3945,9 @@ StyleClipBasicShapeToCSSArray(const StyleClipPath& aClipPath, const nsStyleCorners& radii = shape->GetRadius(); NS_FOR_CSS_FULL_CORNERS(corner) { auto pair = MakeUnique(); - if (!StyleCoordToCSSValue(radii.Get(NS_FULL_TO_HALF_CORNER(corner, false)), + if (!StyleCoordToCSSValue(radii.Get(FullToHalfCorner(corner, false)), pair->mXValue) || - !StyleCoordToCSSValue(radii.Get(NS_FULL_TO_HALF_CORNER(corner, true)), + !StyleCoordToCSSValue(radii.Get(FullToHalfCorner(corner, true)), pair->mYValue)) { return false; } @@ -4401,11 +4401,11 @@ StyleAnimationValue::ExtractComputedValue(nsCSSPropertyID aProperty, const nsStyleCorners& corners = StyleDataAtOffset(styleStruct, ssOffset); - uint8_t fullCorner = animType - eStyleAnimType_Corner_TopLeft; + Corner fullCorner = Corner(animType - eStyleAnimType_Corner_TopLeft); const nsStyleCoord &horiz = - corners.Get(NS_FULL_TO_HALF_CORNER(fullCorner, false)); + corners.Get(FullToHalfCorner(fullCorner, false)); const nsStyleCoord &vert = - corners.Get(NS_FULL_TO_HALF_CORNER(fullCorner, true)); + corners.Get(FullToHalfCorner(fullCorner, true)); nsAutoPtr pair(new nsCSSValuePair); if (!StyleCoordToCSSValue(horiz, pair->mXValue) || !StyleCoordToCSSValue(vert, pair->mYValue)) { diff --git a/layout/style/nsComputedDOMStyle.cpp b/layout/style/nsComputedDOMStyle.cpp index 39449b697a..33a2836590 100644 --- a/layout/style/nsComputedDOMStyle.cpp +++ b/layout/style/nsComputedDOMStyle.cpp @@ -3440,10 +3440,10 @@ nsComputedDOMStyle::DoGetOutlineColor() already_AddRefed nsComputedDOMStyle::GetEllipseRadii(const nsStyleCorners& aRadius, - uint8_t aFullCorner) + Corner aFullCorner) { - nsStyleCoord radiusX = aRadius.Get(NS_FULL_TO_HALF_CORNER(aFullCorner, false)); - nsStyleCoord radiusY = aRadius.Get(NS_FULL_TO_HALF_CORNER(aFullCorner, true)); + nsStyleCoord radiusX = aRadius.Get(FullToHalfCorner(aFullCorner, false)); + nsStyleCoord radiusY = aRadius.Get(FullToHalfCorner(aFullCorner, true)); // for compatibility, return a single value if X and Y are equal if (radiusX == radiusY) { @@ -6002,9 +6002,9 @@ nsComputedDOMStyle::BasicShapeRadiiToString(nsAString& aCssText, nsAutoString horizontalString, verticalString; NS_FOR_CSS_FULL_CORNERS(corner) { horizontal.AppendElement( - aCorners.Get(NS_FULL_TO_HALF_CORNER(corner, false))); + aCorners.Get(FullToHalfCorner(corner, false))); vertical.AppendElement( - aCorners.Get(NS_FULL_TO_HALF_CORNER(corner, true))); + aCorners.Get(FullToHalfCorner(corner, true))); } BoxValuesToString(horizontalString, horizontal); BoxValuesToString(verticalString, vertical); diff --git a/layout/style/nsComputedDOMStyle.h b/layout/style/nsComputedDOMStyle.h index ce19ca51c1..f256d22a50 100644 --- a/layout/style/nsComputedDOMStyle.h +++ b/layout/style/nsComputedDOMStyle.h @@ -158,7 +158,7 @@ private: #undef STYLE_STRUCT already_AddRefed GetEllipseRadii(const nsStyleCorners& aRadius, - uint8_t aFullCorner); + mozilla::Corner aFullCorner); already_AddRefed GetOffsetWidthFor(mozilla::Side aSide); diff --git a/layout/style/nsRuleNode.cpp b/layout/style/nsRuleNode.cpp index 349669959c..d1d14d0f7a 100644 --- a/layout/style/nsRuleNode.cpp +++ b/layout/style/nsRuleNode.cpp @@ -7874,8 +7874,8 @@ nsRuleNode::ComputeBorderData(void* aStartStruct, const nsCSSPropertyID* subprops = nsCSSProps::SubpropertyEntryFor(eCSSProperty_border_radius); NS_FOR_CSS_FULL_CORNERS(corner) { - int cx = NS_FULL_TO_HALF_CORNER(corner, false); - int cy = NS_FULL_TO_HALF_CORNER(corner, true); + int cx = FullToHalfCorner(corner, false); + int cy = FullToHalfCorner(corner, true); const nsCSSValue& radius = *aRuleData->ValueFor(subprops[corner]); nsStyleCoord parentX = parentBorder->mBorderRadius.Get(cx); nsStyleCoord parentY = parentBorder->mBorderRadius.Get(cy); @@ -8066,8 +8066,8 @@ nsRuleNode::ComputeOutlineData(void* aStartStruct, const nsCSSPropertyID* subprops = nsCSSProps::SubpropertyEntryFor(eCSSProperty__moz_outline_radius); NS_FOR_CSS_FULL_CORNERS(corner) { - int cx = NS_FULL_TO_HALF_CORNER(corner, false); - int cy = NS_FULL_TO_HALF_CORNER(corner, true); + int cx = FullToHalfCorner(corner, false); + int cy = FullToHalfCorner(corner, true); const nsCSSValue& radius = *aRuleData->ValueFor(subprops[corner]); nsStyleCoord parentX = parentOutline->mOutlineRadius.Get(cx); nsStyleCoord parentY = parentOutline->mOutlineRadius.Get(cy); @@ -9915,8 +9915,8 @@ GetStyleBasicShapeFromCSSValue(const nsCSSValue& aValue, if (shapeFunction->Item(5).GetUnit() == eCSSUnit_Array) { nsCSSValue::Array* radiiArray = shapeFunction->Item(5).GetArrayValue(); NS_FOR_CSS_FULL_CORNERS(corner) { - int cx = NS_FULL_TO_HALF_CORNER(corner, false); - int cy = NS_FULL_TO_HALF_CORNER(corner, true); + int cx = FullToHalfCorner(corner, false); + int cy = FullToHalfCorner(corner, true); const nsCSSValue& radius = radiiArray->Item(corner); nsStyleCoord coordX, coordY; DebugOnly didSetRadii = SetPairCoords(radius, coordX, coordY, diff --git a/layout/style/nsStyleConsts.h b/layout/style/nsStyleConsts.h index e076025f4e..ca64bf1d5f 100644 --- a/layout/style/nsStyleConsts.h +++ b/layout/style/nsStyleConsts.h @@ -20,8 +20,6 @@ namespace mozilla { // nsStyleCoord.cpp. // Arguments must not have side effects. -#define NS_FULL_TO_HALF_CORNER(var_, vert_) ((var_)*2 + !!(vert_)) - #define NS_SIDE_IS_VERTICAL(side_) ((side_) % 2) #define NS_SIDE_TO_FULL_CORNER(side_, second_) \ (((side_) + !!(second_)) % 4) diff --git a/layout/style/nsStyleCoord.cpp b/layout/style/nsStyleCoord.cpp index a7b5413357..7e233949e9 100644 --- a/layout/style/nsStyleCoord.cpp +++ b/layout/style/nsStyleCoord.cpp @@ -354,7 +354,7 @@ CASE(eCornerBottomLeftY, false); // Validation of HalfToFullCorner. #define CASE(corner, result) \ - static_assert(HalfToFullCorner(corner) == result, \ + static_assert(HalfToFullCorner(corner) == result, \ "HalfToFullCorner is wrong") CASE(eCornerTopLeftX, eCornerTopLeft); CASE(eCornerTopLeftY, eCornerTopLeft); @@ -366,10 +366,10 @@ CASE(eCornerBottomLeftX, eCornerBottomLeft); CASE(eCornerBottomLeftY, eCornerBottomLeft); #undef CASE -// Validation of NS_FULL_TO_HALF_CORNER. +// Validation of FullToHalfCorner. #define CASE(corner, vert, result) \ - static_assert(NS_FULL_TO_HALF_CORNER(corner, vert) == result, \ - "NS_FULL_TO_HALF_CORNER is wrong") + static_assert(FullToHalfCorner(corner, vert) == result, \ + "FullToHalfCorner is wrong") CASE(eCornerTopLeft, false, eCornerTopLeftX); CASE(eCornerTopLeft, true, eCornerTopLeftY); CASE(eCornerTopRight, false, eCornerTopRightX); @@ -382,7 +382,7 @@ CASE(eCornerBottomLeft, true, eCornerBottomLeftY); // Validation of NS_SIDE_TO_{FULL,HALF}_CORNER. #define CASE(side, second, result) \ - static_assert(NS_SIDE_TO_FULL_CORNER(side, second) == result, \ + static_assert(NS_SIDE_TO_FULL_CORNER(side, second) == result, \ "NS_SIDE_TO_FULL_CORNER is wrong") CASE(eSideTop, false, eCornerTopLeft); CASE(eSideTop, true, eCornerTopRight); @@ -398,7 +398,7 @@ CASE(eSideLeft, true, eCornerTopLeft); #undef CASE #define CASE(side, second, parallel, result) \ - static_assert(NS_SIDE_TO_HALF_CORNER(side, second, parallel) == result, \ + static_assert(NS_SIDE_TO_HALF_CORNER(side, second, parallel) == result, \ "NS_SIDE_TO_HALF_CORNER is wrong") CASE(eSideTop, false, true, eCornerTopLeftX); CASE(eSideTop, false, false, eCornerTopLeftY);