-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drop shadow support to main thread rendering engine (#2423)
Co-authored-by: Lan Xu <[email protected]> Co-authored-by: Cal Stephens <[email protected]>
- Loading branch information
1 parent
73b1a42
commit fca6f33
Showing
29 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Sources/Private/MainThread/NodeRenderSystem/Nodes/LayerEffectNodes/DropShadowNode.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Created by Lan Xu on 2024/6/7. | ||
// Copyright © 2024 Airbnb Inc. All rights reserved. | ||
|
||
import QuartzCore | ||
|
||
// MARK: - DropShadowNode | ||
|
||
final class DropShadowNode: LayerEffectNode { | ||
|
||
// MARK: Lifecycle | ||
|
||
init(model: DropShadowModel) { | ||
properties = DropShadowNodeProperties(model: model) | ||
} | ||
|
||
// MARK: Internal | ||
|
||
var properties: DropShadowNodeProperties | ||
|
||
var propertyMap: any NodePropertyMap { | ||
properties | ||
} | ||
|
||
func applyEffect(to layer: CALayer) { | ||
if let opacity = properties.opacity?.value.cgFloatValue { | ||
// Lottie animation files express opacity as a numerical percentage value | ||
// (e.g. 0%, 50%, 100%) so we divide by 100 to get the decimal values | ||
// expected by Core Animation (e.g. 0.0, 0.5, 1.0). | ||
layer.shadowOpacity = Float(opacity / 100) | ||
} | ||
if | ||
let angleDegrees = properties.angle?.value.cgFloatValue, | ||
let distance = properties.distance?.value.cgFloatValue | ||
{ | ||
// Lottie animation files express rotation in degrees | ||
// (e.g. 90º, 180º, 360º) so we convert to radians to get the | ||
// values expected by Core Animation (e.g. π/2, π, 2π) | ||
let angleRadians = (angleDegrees * .pi) / 180 | ||
|
||
// Lottie animation files express the `shadowOffset` as (angle, distance) pair, | ||
// which we convert to the expected x / y offset values: | ||
let offsetX = distance * cos(angleRadians) | ||
let offsetY = distance * sin(angleRadians) | ||
layer.shadowOffset = .init(width: offsetX, height: offsetY) | ||
} | ||
layer.shadowColor = properties.color?.value.cgColorValue | ||
layer.shadowRadius = properties.radius?.value.cgFloatValue ?? 0 | ||
} | ||
|
||
} | ||
|
||
// MARK: - DropShadowNodeProperties | ||
|
||
final class DropShadowNodeProperties: NodePropertyMap { | ||
|
||
// MARK: Lifecycle | ||
|
||
init(model: DropShadowModel) { | ||
if let opacityKeyframes = model._opacity?.keyframes { | ||
opacity = NodeProperty(provider: KeyframeInterpolator(keyframes: opacityKeyframes)) | ||
propertyMap[PropertyName.opacity.rawValue] = opacity | ||
} else { | ||
opacity = nil | ||
} | ||
if let radiusKeyframes = model._radius?.keyframes { | ||
radius = NodeProperty(provider: KeyframeInterpolator(keyframes: radiusKeyframes)) | ||
propertyMap["Radius"] = radius | ||
} else { | ||
radius = nil | ||
} | ||
if let colorKeyFrames = model._color?.keyframes { | ||
color = NodeProperty(provider: KeyframeInterpolator(keyframes: colorKeyFrames)) | ||
propertyMap[PropertyName.color.rawValue] = color | ||
} else { | ||
color = nil | ||
} | ||
if let angleKeyFrames = model._angle?.keyframes { | ||
angle = NodeProperty(provider: KeyframeInterpolator(keyframes: angleKeyFrames)) | ||
propertyMap["Angle"] = angle | ||
} else { | ||
angle = nil | ||
} | ||
if let distanceKeyFrame = model._distance?.keyframes { | ||
distance = NodeProperty(provider: KeyframeInterpolator(keyframes: distanceKeyFrame)) | ||
propertyMap["Distance"] = distance | ||
} else { | ||
distance = nil | ||
} | ||
properties = Array(propertyMap.values) | ||
} | ||
|
||
// MARK: Internal | ||
|
||
var propertyMap: [String: AnyNodeProperty] = [:] | ||
var properties: [AnyNodeProperty] | ||
|
||
let opacity: NodeProperty<LottieVector1D>? | ||
let radius: NodeProperty<LottieVector1D>? | ||
let color: NodeProperty<LottieColor>? | ||
let angle: NodeProperty<LottieVector1D>? | ||
let distance: NodeProperty<LottieVector1D>? | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/Private/MainThread/NodeRenderSystem/Nodes/LayerEffectNodes/LayerEffectNode.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Created by Lan Xu on 2024/6/8. | ||
// Copyright © 2024 Airbnb Inc. All rights reserved. | ||
|
||
import QuartzCore | ||
|
||
// MARK: - LayerEffectNode | ||
|
||
protocol LayerEffectNode { | ||
func applyEffect(to layer: CALayer) | ||
var propertyMap: NodePropertyMap { get } | ||
} | ||
|
||
extension LayerEffectNode { | ||
|
||
func updateWithFrame(layer: CALayer, frame: CGFloat) { | ||
for property in propertyMap.properties { | ||
if property.needsUpdate(frame: frame) { | ||
property.update(frame: frame) | ||
} | ||
} | ||
applyEffect(to: layer) | ||
} | ||
|
||
} |
Binary file modified
BIN
+12 Bytes
(100%)
...napshotTests/testMainThreadRenderingEngine.Issues-issue_1169_four_shadows-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+12 Bytes
(100%)
...pshotTests/testMainThreadRenderingEngine.Issues-issue_1169_four_shadows-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+80.9 KB
(330%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_four_shadows-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+59.6 KB
(270%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_four_shadows-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+28.4 KB
(190%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_four_shadows-75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...shotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...pshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...pshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...pshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect-75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.98 KB
(180%)
...ts/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect_animated-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+19.4 KB
(420%)
.../testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect_animated-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+8.68 KB
(240%)
...s/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect_animated-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+15.1 KB
(350%)
...s/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect_animated-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+19.4 KB
(420%)
...s/testMainThreadRenderingEngine.Issues-issue_1169_shadow_effect_animated-75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.6 KB
(310%)
...napshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.6 KB
(310%)
...pshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.6 KB
(310%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.6 KB
(310%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.6 KB
(310%)
...apshotTests/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style-75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+10.8 KB
(320%)
...sts/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style_animated-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+8.03 KB
(240%)
...s/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style_animated-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+8.65 KB
(250%)
...ts/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style_animated-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+8.03 KB
(240%)
...ts/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style_animated-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+8.03 KB
(240%)
...ts/testMainThreadRenderingEngine.Issues-issue_1169_shadow_style_animated-75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.