Skip to content

Commit

Permalink
fix: Correct the frequent GC issues caused by L.beginSection and L.en…
Browse files Browse the repository at this point in the history
…dSection methods.
  • Loading branch information
chaoluo10 authored and uni-cstar committed Apr 13, 2024
1 parent 4e1c3dc commit 4af3e59
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 83 deletions.
4 changes: 4 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/L.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static void setTraceEnabled(boolean enabled) {
}
}

public static boolean isTraceEnabled(){
return traceEnabled;
}

public static void setNetworkCacheEnabled(boolean enabled) {
networkCacheEnabled = enabled;
}
Expand Down
16 changes: 12 additions & 4 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ public void draw(@NonNull Canvas canvas) {
if (asyncUpdatesEnabled) {
setProgressDrawLock.acquire();
}
L.beginSection("Drawable#draw");
if (L.isTraceEnabled()) {
L.beginSection("Drawable#draw");
}

if (asyncUpdatesEnabled && shouldSetProgressBeforeDrawing()) {
setProgress(animator.getAnimatedValueAbsolute());
Expand Down Expand Up @@ -715,7 +717,9 @@ public void draw(@NonNull Canvas canvas) {
} catch (InterruptedException e) {
// Do nothing.
} finally {
L.endSection("Drawable#draw");
if (L.isTraceEnabled()) {
L.endSection("Drawable#draw");
}
if (asyncUpdatesEnabled) {
setProgressDrawLock.release();
if (compositionLayer.getProgress() != animator.getAnimatedValueAbsolute()) {
Expand Down Expand Up @@ -1137,9 +1141,13 @@ public void setProgress(@FloatRange(from = 0f, to = 1f) final float progress) {
lazyCompositionTasks.add(c -> setProgress(progress));
return;
}
L.beginSection("Drawable#setProgress");
if (L.isTraceEnabled()) {
L.beginSection("Drawable#setProgress");
}
animator.setFrame(composition.getFrameForProgress(progress));
L.endSection("Drawable#setProgress");
if (L.isTraceEnabled()) {
L.endSection("Drawable#setProgress");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,23 @@ public abstract class BaseStrokeContent
}

@Override public void draw(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
L.beginSection("StrokeContent#draw");
if (L.isTraceEnabled()) {
L.beginSection("StrokeContent#draw");
}
if (Utils.hasZeroScaleAxis(parentMatrix)) {
L.endSection("StrokeContent#draw");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#draw");
}
return;
}
int alpha = (int) ((parentAlpha / 255f * ((IntegerKeyframeAnimation) opacityAnimation).getIntValue() / 100f) * 255);
paint.setAlpha(clamp(alpha, 0, 255));
paint.setStrokeWidth(((FloatKeyframeAnimation) widthAnimation).getFloatValue() * Utils.getScale(parentMatrix));
if (paint.getStrokeWidth() <= 0) {
// Android draws a hairline stroke for 0, After Effects doesn't.
L.endSection("StrokeContent#draw");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#draw");
}
return;
}
applyDashPatternIfNeeded(parentMatrix);
Expand Down Expand Up @@ -195,24 +201,36 @@ public abstract class BaseStrokeContent
if (pathGroup.trimPath != null) {
applyTrimPath(canvas, pathGroup, parentMatrix);
} else {
L.beginSection("StrokeContent#buildPath");
if (L.isTraceEnabled()) {
L.beginSection("StrokeContent#buildPath");
}
path.reset();
for (int j = pathGroup.paths.size() - 1; j >= 0; j--) {
path.addPath(pathGroup.paths.get(j).getPath(), parentMatrix);
}
L.endSection("StrokeContent#buildPath");
L.beginSection("StrokeContent#drawPath");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#buildPath");
L.beginSection("StrokeContent#drawPath");
}
canvas.drawPath(path, paint);
L.endSection("StrokeContent#drawPath");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#drawPath");
}
}
}
L.endSection("StrokeContent#draw");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#draw");
}
}

private void applyTrimPath(Canvas canvas, PathGroup pathGroup, Matrix parentMatrix) {
L.beginSection("StrokeContent#applyTrimPath");
if (L.isTraceEnabled()) {
L.beginSection("StrokeContent#applyTrimPath");
}
if (pathGroup.trimPath == null) {
L.endSection("StrokeContent#applyTrimPath");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#applyTrimPath");
}
return;
}
path.reset();
Expand All @@ -226,7 +244,9 @@ private void applyTrimPath(Canvas canvas, PathGroup pathGroup, Matrix parentMatr
// If the start-end is ~100, consider it to be the full path.
if (animStartValue < 0.01f && animEndValue > 0.99f) {
canvas.drawPath(path, paint);
L.endSection("StrokeContent#applyTrimPath");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#applyTrimPath");
}
return;
}

Expand Down Expand Up @@ -282,11 +302,15 @@ private void applyTrimPath(Canvas canvas, PathGroup pathGroup, Matrix parentMatr
}
currentLength += length;
}
L.endSection("StrokeContent#applyTrimPath");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#applyTrimPath");
}
}

@Override public void getBounds(RectF outBounds, Matrix parentMatrix, boolean applyParents) {
L.beginSection("StrokeContent#getBounds");
if (L.isTraceEnabled()) {
L.beginSection("StrokeContent#getBounds");
}
path.reset();
for (int i = 0; i < pathGroups.size(); i++) {
PathGroup pathGroup = pathGroups.get(i);
Expand All @@ -307,13 +331,19 @@ private void applyTrimPath(Canvas canvas, PathGroup pathGroup, Matrix parentMatr
outBounds.right + 1,
outBounds.bottom + 1
);
L.endSection("StrokeContent#getBounds");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#getBounds");
}
}

private void applyDashPatternIfNeeded(Matrix parentMatrix) {
L.beginSection("StrokeContent#applyDashPattern");
if (L.isTraceEnabled()) {
L.beginSection("StrokeContent#applyDashPattern");
}
if (dashPatternAnimations.isEmpty()) {
L.endSection("StrokeContent#applyDashPattern");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#applyDashPattern");
}
return;
}

Expand All @@ -337,7 +367,9 @@ private void applyDashPatternIfNeeded(Matrix parentMatrix) {
}
float offset = dashPatternOffsetAnimation == null ? 0f : dashPatternOffsetAnimation.getValue() * scale;
paint.setPathEffect(new DashPathEffect(dashPatternValues, offset));
L.endSection("StrokeContent#applyDashPattern");
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#applyDashPattern");
}
}

@Override public void resolveKeyPath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public FillContent(final LottieDrawable lottieDrawable, BaseLayer layer, ShapeFi
if (hidden) {
return;
}
L.beginSection("FillContent#draw");
if (L.isTraceEnabled()) {
L.beginSection("FillContent#draw");
}
int color = ((ColorKeyframeAnimation) this.colorAnimation).getIntValue();
int alpha = (int) ((parentAlpha / 255f * opacityAnimation.getValue() / 100f) * 255);
paint.setColor((clamp(alpha, 0, 255) << 24) | (color & 0xFFFFFF));
Expand Down Expand Up @@ -131,7 +133,9 @@ public FillContent(final LottieDrawable lottieDrawable, BaseLayer layer, ShapeFi

canvas.drawPath(path, paint);

L.endSection("FillContent#draw");
if (L.isTraceEnabled()) {
L.endSection("FillContent#draw");
}
}

@Override public void getBounds(RectF outBounds, Matrix parentMatrix, boolean applyParents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public GradientFillContent(final LottieDrawable lottieDrawable, LottieCompositio
if (hidden) {
return;
}
L.beginSection("GradientFillContent#draw");
if (L.isTraceEnabled()) {
L.beginSection("GradientFillContent#draw");
}
path.reset();
for (int i = 0; i < paths.size(); i++) {
path.addPath(paths.get(i).getPath(), parentMatrix);
Expand Down Expand Up @@ -156,7 +158,9 @@ public GradientFillContent(final LottieDrawable lottieDrawable, LottieCompositio
paint.setAlpha(clamp(alpha, 0, 255));

canvas.drawPath(path, paint);
L.endSection("GradientFillContent#draw");
if (L.isTraceEnabled()) {
L.endSection("GradientFillContent#draw");
}
}

@Override public void getBounds(RectF outBounds, Matrix parentMatrix, boolean applyParents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public void addUpdateListener(AnimationListener listener) {
}

public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
L.beginSection("BaseKeyframeAnimation#setProgress");
if (L.isTraceEnabled()) {
L.beginSection("BaseKeyframeAnimation#setProgress");
}
if (keyframesWrapper.isEmpty()) {
L.endSection("BaseKeyframeAnimation#setProgress");
if (L.isTraceEnabled()) {
L.endSection("BaseKeyframeAnimation#setProgress");
}
return;
}
if (progress < getStartDelayProgress()) {
Expand All @@ -61,28 +65,40 @@ public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
}

if (progress == this.progress) {
L.endSection("BaseKeyframeAnimation#setProgress");
if (L.isTraceEnabled()) {
L.endSection("BaseKeyframeAnimation#setProgress");
}
return;
}
this.progress = progress;
if (keyframesWrapper.isValueChanged(progress)) {
notifyListeners();
}
L.endSection("BaseKeyframeAnimation#setProgress");
if (L.isTraceEnabled()) {
L.endSection("BaseKeyframeAnimation#setProgress");
}
}

public void notifyListeners() {
L.beginSection("BaseKeyframeAnimation#notifyListeners");
if (L.isTraceEnabled()) {
L.beginSection("BaseKeyframeAnimation#notifyListeners");
}
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onValueChanged();
}
L.endSection("BaseKeyframeAnimation#notifyListeners");
if (L.isTraceEnabled()) {
L.endSection("BaseKeyframeAnimation#notifyListeners");
}
}

protected Keyframe<K> getCurrentKeyframe() {
L.beginSection("BaseKeyframeAnimation#getCurrentKeyframe");
if (L.isTraceEnabled()) {
L.beginSection("BaseKeyframeAnimation#getCurrentKeyframe");
}
final Keyframe<K> keyframe = keyframesWrapper.getCurrentKeyframe();
L.endSection("BaseKeyframeAnimation#getCurrentKeyframe");
if (L.isTraceEnabled()) {
L.endSection("BaseKeyframeAnimation#getCurrentKeyframe");
}
return keyframe;
}

Expand Down
Loading

0 comments on commit 4af3e59

Please sign in to comment.