Skip to content
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

[Breaking Change] Automatic hardware acceleration detection #1048

Merged
merged 1 commit into from Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -181,12 +181,6 @@ class PlayerFragment : BaseMvRxFragment() {
trimContainer.animateVisible(it)
}

hardwareAccelerationToggle.setOnClickListener { viewModel.toggleHardwareAcceleration() }
viewModel.selectSubscribe(PlayerState::useHardwareAcceleration) {
hardwareAccelerationToggle.isActivated = it
animationView.useHardwareAcceleration(it)
}

mergePathsToggle.setOnClickListener { viewModel.toggleMergePaths() }
viewModel.selectSubscribe(PlayerState::useMergePaths) {
animationView.enableMergePathsForKitKatAndAbove(it)
Expand Down Expand Up @@ -411,6 +405,7 @@ class PlayerFragment : BaseMvRxFragment() {
composition ?: return

animationView.setComposition(composition)
hardwareAccelerationToggle.isActivated = animationView.layerType == View.LAYER_TYPE_HARDWARE
animationView.setPerformanceTrackingEnabled(true)
var renderTimeGraphRange = 4f
animationView.performanceTracker?.addFrameListener { ms ->
Expand Down
56 changes: 5 additions & 51 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Expand Up @@ -8,14 +8,14 @@
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.FloatRange;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.AttributeSet;
Expand Down Expand Up @@ -73,7 +73,6 @@
private @RawRes int animationResId;
private boolean wasAnimatingWhenDetached = false;
private boolean autoPlay = false;
private boolean useHardwareLayer = false;
private Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();

@Nullable private LottieTask<LottieComposition> compositionTask;
Expand Down Expand Up @@ -261,54 +260,6 @@ public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieDrawable.isMergePathsEnabledForKitKatAndAbove();
}

/**
* @see #useHardwareAcceleration(boolean)
*/
@Deprecated
public void useExperimentalHardwareAcceleration() {
useHardwareAcceleration(true);
}


/**
* @see #useHardwareAcceleration(boolean)
*/
@Deprecated
public void useExperimentalHardwareAcceleration(boolean use) {
useHardwareAcceleration(use);
}

/**
* @see #useHardwareAcceleration(boolean)
*/
public void useHardwareAcceleration() {
useHardwareAcceleration(true);
}

/**
* Enable hardware acceleration for this view.
* READ THIS BEFORE ENABLING HARDWARE ACCELERATION:
* 1) Test your animation on the minimum API level you support. Some drawing features such as
* dashes and stroke caps have min api levels
* (https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported)
* 2) Enabling hardware acceleration is not always more performant. Check it with your specific
* animation only if you are having performance issues with software rendering.
* 3) Software rendering is safer and will be consistent across devices. Manufacturers can
* potentially break hardware rendering with bugs in their SKIA engine. Lottie cannot do
* anything about that.
*/
public void useHardwareAcceleration(boolean use) {
if (useHardwareLayer == use) {
return;
}
useHardwareLayer = use;
enableOrDisableHardwareLayer();
}

public boolean getUseHardwareAcceleration() {
return useHardwareLayer;
}

/**
* Sets the animation from a file in the raw directory.
* This will load and deserialize the file asynchronously.
Expand Down Expand Up @@ -794,7 +745,10 @@ private void clearComposition() {
}

private void enableOrDisableHardwareLayer() {
boolean useHardwareLayer = this.useHardwareLayer && lottieDrawable.isAnimating();
boolean useHardwareLayer = true;
if (composition != null && composition.hasDashPattern() && Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
useHardwareLayer = false;
}
setLayerType(useHardwareLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_SOFTWARE, null);
}

Expand Down
17 changes: 17 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieComposition.java
Expand Up @@ -51,7 +51,12 @@ public class LottieComposition {
private float startFrame;
private float endFrame;
private float frameRate;
/**
* Used to determine if an animation can be drawn with hardware acceleration.
*/
private boolean hasDashPattern;

@RestrictTo(RestrictTo.Scope.LIBRARY)
public void init(Rect bounds, float startFrame, float endFrame, float frameRate,
List<Layer> layers, LongSparseArray<Layer> layerMap, Map<String,
List<Layer>> precomps, Map<String, LottieImageAsset> images,
Expand All @@ -74,6 +79,18 @@ public void addWarning(String warning) {
warnings.add(warning);
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
public void setHasDashPattern(boolean hasDashPattern) {
this.hasDashPattern = hasDashPattern;
}

/**
* Used to determine if an animation can be drawn with hardware acceleration.
*/
public boolean hasDashPattern() {
return hasDashPattern;
}

public ArrayList<String> getWarnings() {
return new ArrayList<>(Arrays.asList(warnings.toArray(new String[warnings.size()])));
}
Expand Down
Expand Up @@ -109,6 +109,7 @@ static GradientStroke parse(
if (n.equals("o")) {
offset = val;
} else if (n.equals("d") || n.equals("g")) {
composition.setHasDashPattern(true);
lineDashPattern.add(val);
}
}
Expand Down
Expand Up @@ -83,6 +83,7 @@ static ShapeStroke parse(
break;
case "d":
case "g":
composition.setHasDashPattern(true);
lineDashPattern.add(val);
break;
}
Expand Down