Skip to content

Commit

Permalink
Prevent a StackOverflowException on 4.4 (#1332)
Browse files Browse the repository at this point in the history
Fixes #1298
  • Loading branch information
gpeal committed Jul 22, 2019
1 parent 9ff969e commit 474586a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
private boolean autoPlay = false;
private RenderMode renderMode = RenderMode.AUTOMATIC;
private Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();
/**
* Prevents a StackOverflowException on 4.4 in which getDrawingCache() calls buildDrawingCache().
* This isn't a great solution but it works and has very little performance overhead.
* At some point in the future, the original goal of falling back to hardware rendering when
* the animation is set to software rendering but it is too large to fit in a software bitmap
* should be reevaluated.
*/
private int buildDrawingCacheDepth = 0;

@Nullable private LottieTask<LottieComposition> compositionTask;
/** Can be null because it is created async */
Expand Down Expand Up @@ -884,10 +892,12 @@ private void clearComposition() {
*/
@Override
public void buildDrawingCache(boolean autoScale) {
buildDrawingCacheDepth++;
super.buildDrawingCache(autoScale);
if (getLayerType() == LAYER_TYPE_SOFTWARE && getDrawingCache(autoScale) == null) {
if (buildDrawingCacheDepth == 1 && getLayerType() == LAYER_TYPE_SOFTWARE && getDrawingCache(autoScale) == null) {
setRenderMode(HARDWARE);
}
buildDrawingCacheDepth--;
}

/**
Expand Down

0 comments on commit 474586a

Please sign in to comment.