-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop shadow overhaul: improve correctness and performance #2548
Merged
+1,011
−181
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f213fc4
Drop shadow overhaul: improve correctness and perf
geomaster 9729a75
More PR fixes
geomaster 42fdba4
More warning fixes
geomaster 1dac2c2
Fix some regressions
geomaster ef2ba99
Fix null check bug
geomaster 2c6b25d
Better guarding against unsupported APIs
geomaster 6fedc28
Regenerate .api file
geomaster 9527586
Apply DP scaling to canvas in software rendering
geomaster 0411409
Fix image bounds calculation
geomaster fa7032d
Fix behavior when !clipToCompositionBounds
geomaster 9b51a7b
Fix transformation in software rendering
geomaster 656398e
Remove newly-introduced hot path allocations
geomaster 18fe720
Another fix for this part of the code
geomaster c35b2a3
Improve sub-pixel shadow precision
geomaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,13 @@ | |
|
||
import android.graphics.Canvas; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.graphics.RectF; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import com.airbnb.lottie.LottieComposition; | ||
import com.airbnb.lottie.LottieDrawable; | ||
import com.airbnb.lottie.animation.LPaint; | ||
import com.airbnb.lottie.animation.keyframe.BaseKeyframeAnimation; | ||
import com.airbnb.lottie.animation.keyframe.TransformKeyframeAnimation; | ||
import com.airbnb.lottie.model.KeyPath; | ||
|
@@ -19,7 +17,8 @@ | |
import com.airbnb.lottie.model.content.ContentModel; | ||
import com.airbnb.lottie.model.content.ShapeGroup; | ||
import com.airbnb.lottie.model.layer.BaseLayer; | ||
import com.airbnb.lottie.utils.Utils; | ||
import com.airbnb.lottie.utils.DropShadow; | ||
import com.airbnb.lottie.utils.OffscreenLayer; | ||
import com.airbnb.lottie.value.LottieValueCallback; | ||
|
||
import java.util.ArrayList; | ||
|
@@ -28,8 +27,9 @@ | |
public class ContentGroup implements DrawingContent, PathContent, | ||
BaseKeyframeAnimation.AnimationListener, KeyPathElement { | ||
|
||
private final Paint offScreenPaint = new LPaint(); | ||
private final OffscreenLayer.ComposeOp offscreenOp = new OffscreenLayer.ComposeOp(); | ||
private final RectF offScreenRectF = new RectF(); | ||
private final OffscreenLayer offscreenLayer = new OffscreenLayer(); | ||
|
||
private static List<Content> contentsFromModels(LottieDrawable drawable, LottieComposition composition, BaseLayer layer, | ||
List<ContentModel> contentModels) { | ||
|
@@ -160,7 +160,7 @@ Matrix getTransformationMatrix() { | |
return path; | ||
} | ||
|
||
@Override public void draw(Canvas canvas, Matrix parentMatrix, int parentAlpha) { | ||
@Override public void draw(Canvas canvas, Matrix parentMatrix, int parentAlpha, @Nullable DropShadow shadowToApply) { | ||
if (hidden) { | ||
return; | ||
} | ||
|
@@ -175,24 +175,40 @@ Matrix getTransformationMatrix() { | |
} | ||
|
||
// Apply off-screen rendering only when needed in order to improve rendering performance. | ||
boolean isRenderingWithOffScreen = lottieDrawable.isApplyingOpacityToLayersEnabled() && hasTwoOrMoreDrawableContent() && layerAlpha != 255; | ||
boolean isRenderingWithOffScreen = | ||
(lottieDrawable.isApplyingOpacityToLayersEnabled() && hasTwoOrMoreDrawableContent() && layerAlpha != 255) || | ||
(shadowToApply != null && lottieDrawable.isApplyingShadowToLayersEnabled() && hasTwoOrMoreDrawableContent()); | ||
int childAlpha = isRenderingWithOffScreen ? 255 : layerAlpha; | ||
|
||
Canvas contentCanvas = canvas; | ||
if (isRenderingWithOffScreen) { | ||
offScreenRectF.set(0, 0, 0, 0); | ||
getBounds(offScreenRectF, matrix, true); | ||
offScreenPaint.setAlpha(layerAlpha); | ||
Utils.saveLayerCompat(canvas, offScreenRectF, offScreenPaint); | ||
getBounds(offScreenRectF, parentMatrix, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one got lost in the diff, but is very important - |
||
offscreenOp.alpha = layerAlpha; | ||
if (shadowToApply != null) { | ||
shadowToApply.applyTo(offscreenOp); | ||
shadowToApply = null; // Don't pass it to children - OffscreenLayer now takes care of this | ||
geomaster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
offscreenOp.shadow = null; | ||
} | ||
|
||
contentCanvas = offscreenLayer.start(canvas, offScreenRectF, offscreenOp); | ||
} else { | ||
if (shadowToApply != null) { | ||
shadowToApply = new DropShadow(shadowToApply); | ||
shadowToApply.multiplyOpacity(childAlpha); | ||
} | ||
} | ||
|
||
int childAlpha = isRenderingWithOffScreen ? 255 : layerAlpha; | ||
for (int i = contents.size() - 1; i >= 0; i--) { | ||
Object content = contents.get(i); | ||
if (content instanceof DrawingContent) { | ||
((DrawingContent) content).draw(canvas, matrix, childAlpha); | ||
((DrawingContent) content).draw(contentCanvas, matrix, childAlpha, shadowToApply); | ||
} | ||
} | ||
|
||
if (isRenderingWithOffScreen) { | ||
canvas.restore(); | ||
offscreenLayer.finish(); | ||
} | ||
} | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applying this clamp right away, here and elsewhere, fixes issues with >255 or <0 alpha values, which caused shadows to not appear.
Such alpha values are possible when interpolating using an easing that evaluates to <0 or >1 at some point along its curve.