Skip to content

Use CA shadow apis for box shadow - #50636

Closed
joevilches wants to merge 1 commit into
react:mainfrom
joevilches:export-D72823334
Closed

joevilches wants to merge 1 commit into
react:mainfrom
joevilches:export-D72823334

Conversation

@joevilches

Copy link
Copy Markdown
Contributor

Summary:
A while ago someone on GitHub reported that box shadow on iOS was causing frame drops when animating a large, pretty blurry shadow: #49128. This week I finally got around to fixing this!

The slowness was happening since we were using CG to draw this shadow, which is very CPU intensive and, to my knowledge, does not take advantage of GPUs to do anything. Couple that with an animating, large, blurry shadow and we have frame issues. These shadows were taking very long to draw, to get the image of the shadow (which then needs to be copied into some texture 3 times as big, composited, put on the screen etc) took 12-14ms :o, thats very slow.

To fix this I figured out how to get CA's shadow APIs working, which take advantage of the GPU. The enable inset shadows and spread you have to get creative with a mix of shadowPath and mask with a CAShapeLayer, but we got it done! Things are much faster, I am not sure how to time this but using a real device shows no frame drops :D

Changelog: [iOS][Fixed] - Box shadows on iOS are faster

Differential Revision: D72823334

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 10, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D72823334

Summary:
Pull Request resolved: react#50636

A while ago someone on GitHub reported that box shadow on iOS was causing frame drops when animating a large, pretty blurry shadow: react#49128. This week I finally got around to fixing this!

The slowness was happening since we were using CG to draw this shadow, which is very CPU intensive and, to my knowledge, does not take advantage of GPUs to do anything. Couple that with an animating, large, blurry shadow and we have frame issues. These shadows were taking very long to draw, to get the image of the shadow (which then needs to be copied into some texture 3 times as big, composited, put on the screen etc) took 12-14ms :o, thats very slow.

To fix this I figured out how to get CA's shadow APIs working, which take advantage of the GPU. The enable inset shadows and spread you have to get creative with a mix of `shadowPath` and `mask` with a `CAShapeLayer`, but we got it done! Things are much faster, I am not sure how to time this but using a real device shows no frame drops :D

Changelog: [iOS][Fixed] - Box shadows on iOS are faster

Reviewed By: lenaic

Differential Revision: D72823334
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D72823334

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 11, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request has been merged in 52173ab.

@react-native-bot

Copy link
Copy Markdown
Collaborator
uffoltzl pushed a commit to uffoltzl/react-native that referenced this pull request Apr 18, 2025
Summary:
Pull Request resolved: react#50636

A while ago someone on GitHub reported that box shadow on iOS was causing frame drops when animating a large, pretty blurry shadow: react#49128. This week I finally got around to fixing this!

The slowness was happening since we were using CG to draw this shadow, which is very CPU intensive and, to my knowledge, does not take advantage of GPUs to do anything. Couple that with an animating, large, blurry shadow and we have frame issues. These shadows were taking very long to draw, to get the image of the shadow (which then needs to be copied into some texture 3 times as big, composited, put on the screen etc) took 12-14ms :o, thats very slow.

To fix this I figured out how to get CA's shadow APIs working, which take advantage of the GPU. The enable inset shadows and spread you have to get creative with a mix of `shadowPath` and `mask` with a `CAShapeLayer`, but we got it done! Things are much faster, I am not sure how to time this but using a real device shows no frame drops :D

Changelog: [iOS][Fixed] - Box shadows on iOS are faster

Reviewed By: lenaic

Differential Revision: D72823334

fbshipit-source-id: 460339c9d77e7423ce59a1a9178b6b3ad527e4b0
meta-codesync Bot pushed a commit that referenced this pull request Dec 1, 2026
…ve spread on small views

## Summary:

Fixes #60302.

On iOS, an outset `boxShadow` with a negative spread logs CoreGraphics `invalid numeric value (NaN)` errors when the view is smaller than twice the spread on either axis. The most common case is a view with zero size, such as the first frame of a measure-then-animate indicator. Tailwind's `shadow-sm` is enough to trigger it, because its second layer has `-1px` spread.

`RCTGetOutsetBoxShadowLayer` builds the shadow rect with `CGRectInset(bounds, -spread, -spread)`. When that would make the width or height negative, `CGRectInset` returns `CGRectNull` (origin at infinity). The null rect then goes into `RCTPathCreateWithRoundedRect` for both the shadow path and the padded mask path, and `CGPathAddArc` receives NaN. This has been happening since #50636 (0.80). Before that, the shadow rect size was clamped with `fmax(…, 0)`.

This change caps each axis's inset at half the layer's size, so a negative spread can shrink the rect to zero but not past it. The rect stays centred on the layer, as in 0.79. For any spread that doesn't overshrink the rect, `CGRectInset` gets exactly the same arguments as before, so zero, positive and small negative spreads are unchanged. With a zero-sized rect, `RCTPathCreateWithRoundedRect` already clamps every corner radius to 0, so no NaN reaches CoreGraphics. The inset branch in the same file already handles this case with a `CGRectIsNull` check.

## Changelog:

[IOS] [FIXED] - Fix NaN passed to CoreGraphics when an outset `boxShadow` with negative spread is applied to a view smaller than the spread, e.g. a zero-sized view

## Test Plan:

Added `React/Tests/Fabric/RCTBoxShadowTests.mm` (5 tests), which checks the shadow path and mask path bounds from `RCTGetBoxShadowLayer`:
- a zero-sized layer with negative spread gives an empty shadow rect at the offset, and the mask still includes the padded rect
- each axis is clamped on its own (100×1 layer with `-1` spread → 98×0 rect, centred)
- small negative, zero and positive spread give the same rects as before

To run them, I enabled the `React-RCTFabric` test spec locally (`:testspecs => ['Tests']` in `scripts/cocoapods/fabric.rb`), ran `pod install` in `packages/rn-tester` (plus `pod update hermes-engine --no-repo-update` to match the local Hermes version), then:

```
xcodebuild test -workspace RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests -destination 'platform=iOS Simulator,…'
```

- Before the fix: 3 of 5 new tests fail. The shadow path bounds are `{{inf, inf}, {0, 0}}`, and the log has 16 `invalid numeric value (NaN…) to CoreGraphics API` messages, 8 per failing test, matching the issue's 2 paths × 4 corner arcs.
- After the fix: `Executed 27 tests, with 0 failures`, covering all of `React/Tests/Fabric`, including the existing `RCTGradientUtilsTests` and `AppleEventBeatTests`. No CoreGraphics NaN messages.

Notes:
- CI does not build this test spec, the same as `RCTGradientUtilsTests` (#59774).
- With the full `Tests/**/*.mm` glob, the test spec currently fails to compile on `main` for an unrelated reason: `React/Tests/Text/RCTSelectableParagraphComponentViewTests.mm` can't find `react/renderer/element/ComponentBuilder.h`. I narrowed the glob to `Tests/Fabric/*.mm` locally to run these tests. That change is not part of this PR.
- `yarn format-check-cpp` passes.
- Not run: the issue's Snack in RNTester while watching the simulator logs.

Differential Revision: D135061332

Pull Request resolved: #60323
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook Partner

3 participants