From a98ee9147a9e8cfbb661c8ba431375fca9b53d1d Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Thu, 10 Jul 2025 09:17:08 -0700 Subject: [PATCH] Add noexcept specifications to Transform and Color methods (#52497) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52497 Changelog: [Internal] Is a good practice (and can reduce binary size): https://cpp-core-guidelines-docs.vercel.app/errors#Re-noexcept Reviewed By: philIip Differential Revision: D77988686 fbshipit-source-id: 575c18aa0c8f593f363f4450e9e06d41c97a3d1a --- .../react/renderer/graphics/Color.cpp | 8 ++-- .../react/renderer/graphics/Color.h | 8 ++-- .../react/renderer/graphics/Transform.cpp | 43 ++++++++++--------- .../react/renderer/graphics/Transform.h | 34 +++++++-------- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp index 08f43575864..913558e0f8e 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp @@ -28,22 +28,22 @@ ColorComponents colorComponentsFromColor(SharedColor sharedColor) { } // Read alpha channel in [0, 255] range -uint8_t alphaFromColor(SharedColor color) { +uint8_t alphaFromColor(SharedColor color) noexcept { return static_cast(std::round(alphaFromHostPlatformColor(*color))); } // Read red channel in [0, 255] range -uint8_t redFromColor(SharedColor color) { +uint8_t redFromColor(SharedColor color) noexcept { return static_cast(std::round(redFromHostPlatformColor(*color))); } // Read green channel in [0, 255] range -uint8_t greenFromColor(SharedColor color) { +uint8_t greenFromColor(SharedColor color) noexcept { return static_cast(std::round(greenFromHostPlatformColor(*color))); } // Read blue channel in [0, 255] range -uint8_t blueFromColor(SharedColor color) { +uint8_t blueFromColor(SharedColor color) noexcept { return static_cast(std::round(blueFromHostPlatformColor(*color))); } diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Color.h b/packages/react-native/ReactCommon/react/renderer/graphics/Color.h index 5c2dd9aa509..ec21ea74dc4 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Color.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Color.h @@ -61,10 +61,10 @@ bool isColorMeaningful(const SharedColor& color) noexcept; SharedColor colorFromComponents(ColorComponents components); ColorComponents colorComponentsFromColor(SharedColor color); -uint8_t alphaFromColor(SharedColor color); -uint8_t redFromColor(SharedColor color); -uint8_t greenFromColor(SharedColor color); -uint8_t blueFromColor(SharedColor color); +uint8_t alphaFromColor(SharedColor color) noexcept; +uint8_t redFromColor(SharedColor color) noexcept; +uint8_t greenFromColor(SharedColor color) noexcept; +uint8_t blueFromColor(SharedColor color) noexcept; SharedColor colorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a); SharedColor clearColor(); diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp index a4a7bd4688c..c6a70c6b40b 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.cpp @@ -15,19 +15,19 @@ namespace facebook::react { -Transform Transform::Identity() { +/* static */ Transform Transform::Identity() noexcept { return {}; } -Transform Transform::VerticalInversion() { +/* static */ Transform Transform::VerticalInversion() noexcept { return Transform::Scale(1, -1, 1); } -Transform Transform::HorizontalInversion() { +/* static */ Transform Transform::HorizontalInversion() noexcept { return Transform::Scale(-1, 1, 1); } -Transform Transform::Perspective(Float perspective) { +/* static */ Transform Transform::Perspective(Float perspective) noexcept { auto transform = Transform{}; auto Zero = ValueUnit(0, UnitType::Point); transform.operations.push_back(TransformOperation{ @@ -39,7 +39,7 @@ Transform Transform::Perspective(Float perspective) { return transform; } -Transform Transform::Scale(Float x, Float y, Float z) { +/* static */ Transform Transform::Scale(Float x, Float y, Float z) noexcept { auto transform = Transform{}; Float xprime = isZero(x) ? 0 : x; Float yprime = isZero(y) ? 0 : y; @@ -57,7 +57,8 @@ Transform Transform::Scale(Float x, Float y, Float z) { return transform; } -Transform Transform::Translate(Float x, Float y, Float z) { +/* static */ Transform +Transform::Translate(Float x, Float y, Float z) noexcept { auto transform = Transform{}; Float xprime = isZero(x) ? 0 : x; Float yprime = isZero(y) ? 0 : y; @@ -75,7 +76,7 @@ Transform Transform::Translate(Float x, Float y, Float z) { return transform; } -Transform Transform::Skew(Float x, Float y) { +/* static */ Transform Transform::Skew(Float x, Float y) noexcept { auto transform = Transform{}; Float xprime = isZero(x) ? 0 : x; Float yprime = isZero(y) ? 0 : y; @@ -89,7 +90,7 @@ Transform Transform::Skew(Float x, Float y) { return transform; } -Transform Transform::RotateX(Float radians) { +/* static */ Transform Transform::RotateX(Float radians) noexcept { auto transform = Transform{}; if (!isZero(radians)) { auto Zero = ValueUnit(0, UnitType::Point); @@ -106,7 +107,7 @@ Transform Transform::RotateX(Float radians) { return transform; } -Transform Transform::RotateY(Float radians) { +/* static */ Transform Transform::RotateY(Float radians) noexcept { auto transform = Transform{}; if (!isZero(radians)) { auto Zero = ValueUnit(0, UnitType::Point); @@ -123,7 +124,7 @@ Transform Transform::RotateY(Float radians) { return transform; } -Transform Transform::RotateZ(Float radians) { +/* static */ Transform Transform::RotateZ(Float radians) noexcept { auto transform = Transform{}; if (!isZero(radians)) { auto Zero = ValueUnit(0, UnitType::Point); @@ -140,7 +141,7 @@ Transform Transform::RotateZ(Float radians) { return transform; } -Transform Transform::Rotate(Float x, Float y, Float z) { +/* static */ Transform Transform::Rotate(Float x, Float y, Float z) noexcept { auto transform = Transform{}; if (!isZero(x)) { transform = transform * Transform::RotateX(x); @@ -154,7 +155,7 @@ Transform Transform::Rotate(Float x, Float y, Float z) { return transform; } -Transform Transform::FromTransformOperation( +/* static */ Transform Transform::FromTransformOperation( TransformOperation transformOperation, const Size& size, const Transform& transform) { @@ -197,7 +198,7 @@ Transform Transform::FromTransformOperation( return Transform::Identity(); } -TransformOperation Transform::DefaultTransformOperation( +/* static */ TransformOperation Transform::DefaultTransformOperation( TransformOperationType type) { auto Zero = ValueUnit{0, UnitType::Point}; auto One = ValueUnit{1, UnitType::Point}; @@ -225,7 +226,7 @@ TransformOperation Transform::DefaultTransformOperation( } } -Transform Transform::Interpolate( +/* static */ Transform Transform::Interpolate( Float animationProgress, const Transform& lhs, const Transform& rhs, @@ -301,15 +302,17 @@ Transform Transform::Interpolate( return result; } -bool Transform::isVerticalInversion(const Transform& transform) { +/* static */ bool Transform::isVerticalInversion( + const Transform& transform) noexcept { return floatEquality(transform.at(1, 1), static_cast(-1.0f)); } -bool Transform::isHorizontalInversion(const Transform& transform) { +/* static */ bool Transform::isHorizontalInversion( + const Transform& transform) noexcept { return floatEquality(transform.at(0, 0), static_cast(-1.0f)); } -bool Transform::operator==(const Transform& rhs) const { +bool Transform::operator==(const Transform& rhs) const noexcept { for (auto i = 0; i < 16; i++) { if (matrix[i] != rhs.matrix[i]) { return false; @@ -326,7 +329,7 @@ bool Transform::operator==(const Transform& rhs) const { return true; } -bool Transform::operator!=(const Transform& rhs) const { +bool Transform::operator!=(const Transform& rhs) const noexcept { return !(*this == rhs); } @@ -408,11 +411,11 @@ Transform Transform::operator*(const Transform& rhs) const { return result; } -Float& Transform::at(int i, int j) { +Float& Transform::at(int i, int j) noexcept { return matrix[(i * 4) + j]; } -const Float& Transform::at(int i, int j) const { +const Float& Transform::at(int i, int j) const noexcept { return matrix[(i * 4) + j]; } diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.h b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.h index 38c0041e85c..0473a969e42 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Transform.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Transform.h @@ -104,47 +104,47 @@ struct Transform { /* * Returns the identity transform (`[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]`). */ - static Transform Identity(); + static Transform Identity() noexcept; /* * Returns the vertival inversion transform (`[1 0 0 0; 0 -1 0 0; 0 0 1 0; 0 0 * 0 1]`). */ - static Transform VerticalInversion(); + static Transform VerticalInversion() noexcept; /* * Returns the horizontal inversion transform (`[-1 0 0 0; 0 1 0 0; 0 0 1 0; 0 * 0 0 1]`). */ - static Transform HorizontalInversion(); + static Transform HorizontalInversion() noexcept; /* * Returns a Perspective transform. */ - static Transform Perspective(Float perspective); + static Transform Perspective(Float perspective) noexcept; /* * Returns a Scale transform. */ - static Transform Scale(Float factorX, Float factorY, Float factorZ); + static Transform Scale(Float factorX, Float factorY, Float factorZ) noexcept; /* * Returns a Translate transform. */ - static Transform Translate(Float x, Float y, Float z); + static Transform Translate(Float x, Float y, Float z) noexcept; /* * Returns a Skew transform. */ - static Transform Skew(Float x, Float y); + static Transform Skew(Float x, Float y) noexcept; /* * Returns a transform that rotates by `angle` radians along the given axis. */ - static Transform RotateX(Float radians); - static Transform RotateY(Float radians); - static Transform RotateZ(Float radians); - static Transform Rotate(Float angleX, Float angleY, Float angleZ); + static Transform RotateX(Float radians) noexcept; + static Transform RotateY(Float radians) noexcept; + static Transform RotateZ(Float radians) noexcept; + static Transform Rotate(Float angleX, Float angleY, Float angleZ) noexcept; /** * Perform an interpolation between lhs and rhs, given progress. @@ -163,20 +163,20 @@ struct Transform { const Transform& rhs, const Size& size); - static bool isVerticalInversion(const Transform& transform); - static bool isHorizontalInversion(const Transform& transform); + static bool isVerticalInversion(const Transform& transform) noexcept; + static bool isHorizontalInversion(const Transform& transform) noexcept; /* * Equality operators. */ - bool operator==(const Transform& rhs) const; - bool operator!=(const Transform& rhs) const; + bool operator==(const Transform& rhs) const noexcept; + bool operator!=(const Transform& rhs) const noexcept; /* * Matrix subscript. */ - Float& at(int i, int j); - const Float& at(int i, int j) const; + Float& at(int i, int j) noexcept; + const Float& at(int i, int j) const noexcept; /* * Concatenates (multiplies) transform matrices.