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

More masks and matte improvements #169

Merged
merged 2 commits into from
Feb 28, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified LottieSample/screenshots/Tests_Laugh4 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 LottieSample/screenshots/Tests_Laugh4 10.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 LottieSample/screenshots/Tests_Laugh4 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 LottieSample/screenshots/Tests_Laugh4 20.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 LottieSample/screenshots/Tests_Laugh4 30.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 LottieSample/screenshots/Tests_Laugh4 40.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 LottieSample/screenshots/Tests_Laugh4 5.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 LottieSample/screenshots/Tests_Laugh4 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 LottieSample/screenshots/Tests_Laugh4 60.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 LottieSample/screenshots/Tests_Laugh4 70.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 LottieSample/screenshots/Tests_Laugh4 80.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 LottieSample/screenshots/Tests_Laugh4 90.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 LottieSample/screenshots/Tests_Laugh4 95.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ If you use `LottieDrawable` directly, you must call `recycleBitmaps` when you ar

## Performance and Memory
1. If the composition has no masks or mattes then the performance and memory overhead should be quite good. No bitmaps are created and most operations are simple canvas draw operations.
2. If the composition has masks or mattes, a bitmap will be created and there will be a minor performance hit has it gets drawn.
2. If the composition has masks or mattes, offscreen buffers will be used and there will
be a performance hit has it gets drawn.
3. If you are using your animation in a list, it is recommended to use a CacheStrategy in
LottieAnimationView.setAnimation(String, CacheStrategy) so the animations do not have to be deserialized every time.

Expand Down
18 changes: 0 additions & 18 deletions lottie/src/main/java/com/airbnb/lottie/BitmapCanvas.java

This file was deleted.

81 changes: 0 additions & 81 deletions lottie/src/main/java/com/airbnb/lottie/CanvasPool.java

This file was deleted.

92 changes: 43 additions & 49 deletions lottie/src/main/java/com/airbnb/lottie/LayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@
import java.util.List;

class LayerView extends AnimatableLayer {
private static final int SAVE_FLAGS = Canvas.CLIP_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
private static final int SAVE_FLAGS = Canvas.CLIP_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG |
Canvas.MATRIX_SAVE_FLAG;
private MaskKeyframeAnimation mask;
private LayerView matteLayer;

private final PorterDuffXfermode DST_OUT = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
private final PorterDuffXfermode DST_IN = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
private final RectF rect = new RectF();
private final List<LayerView> transformLayers = new ArrayList<>();
private final Paint mainCanvasPaint = new Paint();
private final Paint mainCanvasPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mattePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint imagePaint =
new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

private final Layer layerModel;
private final LottieComposition composition;
private final CanvasPool canvasPool;

@Nullable private LayerView parentLayer;
/**
Expand All @@ -44,18 +42,18 @@ class LayerView extends AnimatableLayer {
private int precompWidth;
private int precompHeight;

LayerView(Layer layerModel, LottieComposition composition, Callback callback, CanvasPool canvasPool) {
LayerView(Layer layerModel, LottieComposition composition, Callback callback) {
super(callback);
this.layerModel = layerModel;
this.composition = composition;
this.canvasPool = canvasPool;
setBounds(composition.getBounds());

if (layerModel.getMatteType() == Layer.MatteType.Invert) {
mattePaint.setXfermode(DST_OUT);
mattePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
} else {
mattePaint.setXfermode(DST_IN);
mattePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

setupForModel();
}
Expand Down Expand Up @@ -172,7 +170,7 @@ private void setupPreCompLayer() {
for (int i = precompLayers.size() - 1; i >= 0; i--) {
Layer layer = precompLayers.get(i);
LayerView layerView =
new LayerView(layer, composition, getCallback(), canvasPool);
new LayerView(layer, composition, getCallback());
layerView.setPrecompSize(layerModel.getPreCompWidth(), layerModel.getPreCompHeight());
if (mattedLayer != null) {
mattedLayer.setMatteLayer(layerView);
Expand Down Expand Up @@ -248,11 +246,12 @@ void setMatteLayer(LayerView matteLayer) {
parent = parent.getParentLayer();
}

if (precompWidth != 0 || precompHeight != 0) {
canvas.clipRect(0, 0, precompWidth, precompHeight);
}

if (!hasMasks() && !hasMatte()) {
int mainCanvasCount = saveCanvas(canvas);
if (precompWidth != 0 || precompHeight != 0) {
canvas.clipRect(0, 0, precompWidth, precompHeight);
}
// Now apply the parent transformations from the top down.
for (int i = transformLayers.size() - 1; i >= 0; i--) {
LayerView layer = transformLayers.get(i);
Expand All @@ -264,60 +263,55 @@ void setMatteLayer(LayerView matteLayer) {
return;
}


BitmapCanvas bitmapCanvas =
canvasPool.acquire(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);

// Now apply the parent transformations from the top down.
bitmapCanvas.save();
drawImageIfNeeded(bitmapCanvas);
rect.set(0, 0, composition.getBounds().width(), composition.getBounds().height());
canvas.saveLayer(rect, mainCanvasPaint, Canvas.ALL_SAVE_FLAG);

drawImageIfNeeded(canvas);
canvas.save();
for (int i = transformLayers.size() - 1; i >= 0; i--) {
LayerView layer = transformLayers.get(i);
applyTransformForLayer(bitmapCanvas, layer);
applyTransformForLayer(canvas, layer);
}
super.draw(bitmapCanvas);
super.draw(canvas);
canvas.restore();

rect.set(0, 0, canvas.getWidth(), canvas.getHeight());
if (hasMasks()) {
List<Mask> masks = mask.getMasks();
List<BaseKeyframeAnimation<?, Path>> maskAnimations = mask.getMaskAnimations();
for (int i = 0; i < masks.size(); i++) {
applyMask(bitmapCanvas, masks.get(i), maskAnimations.get(i));
}
applyMasks(canvas);
}
bitmapCanvas.restore();

if (hasMatte()) {
bitmapCanvas.saveLayer(rect, mattePaint, SAVE_FLAGS);
matteLayer.draw(bitmapCanvas);
bitmapCanvas.restore();
canvas.saveLayer(rect, mattePaint, SAVE_FLAGS);
matteLayer.draw(canvas);
canvas.restore();
}

if (precompWidth != 0 || precompHeight != 0) {
canvas.clipRect(0, 0, precompWidth, precompHeight);
}
canvas.drawBitmap(bitmapCanvas.getBitmap(), 0, 0, null);
canvasPool.release(bitmapCanvas);
canvas.restore();
}

private void applyMask(BitmapCanvas canvas, Mask mask,
BaseKeyframeAnimation<?, Path> maskAnimation) {
switch (mask.getMaskMode()) {
case MaskModeSubtract:
maskPaint.setXfermode(DST_OUT);
break;
case MaskModeAdd:
default:
maskPaint.setXfermode(DST_IN);
}

private void applyMasks(Canvas canvas) {
canvas.saveLayer(rect, maskPaint, SAVE_FLAGS);

for (int i = transformLayers.size() - 1; i >= 0; i--) {
LayerView layer = transformLayers.get(i);
applyTransformForLayer(canvas, layer);
}
applyTransformForLayer(canvas, this);
canvas.drawPath(maskAnimation.getValue(), mainCanvasPaint);

int size = mask.getMasks().size();
for (int i = 0; i < size; i++) {
Mask mask = this.mask.getMasks().get(i);
BaseKeyframeAnimation<?, Path> maskAnimation = this.mask.getMaskAnimations().get(i);
Path maskPath = maskAnimation.getValue();
switch (mask.getMaskMode()) {
case MaskModeSubtract:
maskPath.setFillType(Path.FillType.INVERSE_WINDING);
break;
case MaskModeAdd:
default:
maskPath.setFillType(Path.FillType.WINDING);
}
canvas.drawPath(maskAnimation.getValue(), mainCanvasPaint);
}
canvas.restore();
}

Expand Down
4 changes: 1 addition & 3 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class LottieDrawable extends AnimatableLayer implements Drawable.Callback
private final ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
private float speed = 1f;

private final CanvasPool canvasPool = new CanvasPool();
@Nullable private ImageAssetBitmapManager imageAssetBitmapManager;
@Nullable private String imageAssetsFolder;
private boolean playAnimationWhenLayerAdded;
Expand Down Expand Up @@ -109,7 +108,6 @@ public class LottieDrawable extends AnimatableLayer implements Drawable.Callback
*
*/
@SuppressWarnings("WeakerAccess") public void recycleBitmaps() {
canvasPool.recycleBitmaps();
if (imageAssetBitmapManager != null) {
imageAssetBitmapManager.recycleBitmaps();
}
Expand Down Expand Up @@ -157,7 +155,7 @@ private void buildLayersForComposition(LottieComposition composition) {
for (int i = composition.getLayers().size() - 1; i >= 0; i--) {
Layer layer = composition.getLayers().get(i);
LayerView layerView;
layerView = new LayerView(layer, composition, this, canvasPool);
layerView = new LayerView(layer, composition, this);
layerMap.put(layerView.getId(), layerView);
if (mattedLayer != null) {
mattedLayer.setMatteLayer(layerView);
Expand Down