diff --git a/graphics/java/android/graphics/drawable/LottieDrawable.java b/graphics/java/android/graphics/drawable/LottieDrawable.java
new file mode 100644
index 0000000000000..c1f1b50cf3397
--- /dev/null
+++ b/graphics/java/android/graphics/drawable/LottieDrawable.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics.drawable;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SuppressLint;
+import android.graphics.Canvas;
+import android.graphics.ColorFilter;
+import android.graphics.PixelFormat;
+
+import dalvik.annotation.optimization.FastNative;
+
+import libcore.util.NativeAllocationRegistry;
+
+import java.io.IOException;
+
+/**
+ * {@link Drawable} for drawing Lottie files.
+ *
+ *
The framework handles decoding subsequent frames in another thread and
+ * updating when necessary. The drawable will only animate while it is being
+ * displayed.
+ *
+ * @hide
+ */
+@SuppressLint("NotCloseable")
+public class LottieDrawable extends Drawable implements Animatable {
+ private long mNativePtr;
+
+ /**
+ * Create an animation from the provided JSON string
+ * @hide
+ */
+ private LottieDrawable(@NonNull String animationJson) throws IOException {
+ mNativePtr = nCreate(animationJson);
+ if (mNativePtr == 0) {
+ throw new IOException("could not make LottieDrawable from json");
+ }
+
+ final long nativeSize = nNativeByteSize(mNativePtr);
+ NativeAllocationRegistry registry = new NativeAllocationRegistry(
+ LottieDrawable.class.getClassLoader(), nGetNativeFinalizer(), nativeSize);
+ registry.registerNativeAllocation(this, mNativePtr);
+ }
+
+ /**
+ * Factory for LottieDrawable, throws IOException if native Skottie Builder fails to parse
+ */
+ public static LottieDrawable makeLottieDrawable(@NonNull String animationJson)
+ throws IOException {
+ return new LottieDrawable(animationJson);
+ }
+
+
+
+ /**
+ * Draw the current frame to the Canvas.
+ * @hide
+ */
+ @Override
+ public void draw(@NonNull Canvas canvas) {
+ if (mNativePtr == 0) {
+ throw new IllegalStateException("called draw on empty LottieDrawable");
+ }
+
+ nDraw(mNativePtr, canvas.getNativeCanvasWrapper());
+ }
+
+ /**
+ * Start the animation. Needs to be called before draw calls.
+ * @hide
+ */
+ @Override
+ public void start() {
+ if (mNativePtr == 0) {
+ throw new IllegalStateException("called start on empty LottieDrawable");
+ }
+
+ if (nStart(mNativePtr)) {
+ invalidateSelf();
+ }
+ }
+
+ /**
+ * Stops the animation playback. Does not release anything.
+ * @hide
+ */
+ @Override
+ public void stop() {
+ if (mNativePtr == 0) {
+ throw new IllegalStateException("called stop on empty LottieDrawable");
+ }
+ nStop(mNativePtr);
+ }
+
+ /**
+ * Return whether the animation is currently running.
+ */
+ @Override
+ public boolean isRunning() {
+ if (mNativePtr == 0) {
+ throw new IllegalStateException("called isRunning on empty LottieDrawable");
+ }
+ return nIsRunning(mNativePtr);
+ }
+
+ @Override
+ public int getOpacity() {
+ // We assume translucency to avoid checking each pixel.
+ return PixelFormat.TRANSLUCENT;
+ }
+
+ @Override
+ public void setAlpha(int alpha) {
+ //TODO
+ }
+
+ @Override
+ public void setColorFilter(@Nullable ColorFilter colorFilter) {
+ //TODO
+ }
+
+ private static native long nCreate(String json);
+ private static native void nDraw(long nativeInstance, long nativeCanvas);
+ @FastNative
+ private static native long nGetNativeFinalizer();
+ @FastNative
+ private static native long nNativeByteSize(long nativeInstance);
+ @FastNative
+ private static native boolean nIsRunning(long nativeInstance);
+ @FastNative
+ private static native boolean nStart(long nativeInstance);
+ @FastNative
+ private static native boolean nStop(long nativeInstance);
+
+}
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 3e3d77b89a9a2..9c4ea0ecbe308 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -78,6 +78,7 @@ cc_defaults {
"external/skia/src/utils",
"external/skia/src/gpu",
"external/skia/src/shaders",
+ "external/skia/modules/skottie",
],
},
host: {
@@ -374,6 +375,7 @@ cc_defaults {
"external/skia/src/effects",
"external/skia/src/image",
"external/skia/src/images",
+ "external/skia/modules/skottie",
],
shared_libs: [
@@ -400,6 +402,7 @@ cc_defaults {
"jni/BitmapRegionDecoder.cpp",
"jni/GIFMovie.cpp",
"jni/GraphicsStatsService.cpp",
+ "jni/LottieDrawable.cpp",
"jni/Movie.cpp",
"jni/MovieImpl.cpp",
"jni/pdf/PdfDocument.cpp",
@@ -507,6 +510,7 @@ cc_defaults {
"hwui/BlurDrawLooper.cpp",
"hwui/Canvas.cpp",
"hwui/ImageDecoder.cpp",
+ "hwui/LottieDrawable.cpp",
"hwui/MinikinSkia.cpp",
"hwui/MinikinUtils.cpp",
"hwui/PaintImpl.cpp",
diff --git a/libs/hwui/SkiaCanvas.cpp b/libs/hwui/SkiaCanvas.cpp
index d83d78f650aad..a1c4b49b67422 100644
--- a/libs/hwui/SkiaCanvas.cpp
+++ b/libs/hwui/SkiaCanvas.cpp
@@ -736,6 +736,10 @@ double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
return imgDrawable->drawStaging(mCanvas);
}
+void SkiaCanvas::drawLottie(LottieDrawable* lottieDrawable) {
+ lottieDrawable->drawStaging(mCanvas);
+}
+
void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
vectorDrawable->drawStaging(this);
}
diff --git a/libs/hwui/SkiaCanvas.h b/libs/hwui/SkiaCanvas.h
index 31e3b4c3c7e22..fd8b6cdb7ca01 100644
--- a/libs/hwui/SkiaCanvas.h
+++ b/libs/hwui/SkiaCanvas.h
@@ -145,6 +145,7 @@ public:
float dstTop, float dstRight, float dstBottom,
const Paint* paint) override;
virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
+ virtual void drawLottie(LottieDrawable* lottieDrawable) override;
virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
diff --git a/libs/hwui/apex/jni_runtime.cpp b/libs/hwui/apex/jni_runtime.cpp
index e6cfa7bcaf700..5a96174ebac0d 100644
--- a/libs/hwui/apex/jni_runtime.cpp
+++ b/libs/hwui/apex/jni_runtime.cpp
@@ -37,6 +37,7 @@ extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
extern int register_android_graphics_Graphics(JNIEnv* env);
extern int register_android_graphics_ImageDecoder(JNIEnv*);
extern int register_android_graphics_drawable_AnimatedImageDrawable(JNIEnv*);
+extern int register_android_graphics_drawable_LottieDrawable(JNIEnv*);
extern int register_android_graphics_Interpolator(JNIEnv* env);
extern int register_android_graphics_MaskFilter(JNIEnv* env);
extern int register_android_graphics_Movie(JNIEnv* env);
@@ -116,6 +117,7 @@ extern int register_android_view_ThreadedRenderer(JNIEnv* env);
REG_JNI(register_android_graphics_HardwareRendererObserver),
REG_JNI(register_android_graphics_ImageDecoder),
REG_JNI(register_android_graphics_drawable_AnimatedImageDrawable),
+ REG_JNI(register_android_graphics_drawable_LottieDrawable),
REG_JNI(register_android_graphics_Interpolator),
REG_JNI(register_android_graphics_MaskFilter),
REG_JNI(register_android_graphics_Matrix),
diff --git a/libs/hwui/hwui/Canvas.h b/libs/hwui/hwui/Canvas.h
index 2a2019199bda2..07e2fe24c9390 100644
--- a/libs/hwui/hwui/Canvas.h
+++ b/libs/hwui/hwui/Canvas.h
@@ -60,6 +60,7 @@ typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
typedef std::function ReadGlyphFunc;
class AnimatedImageDrawable;
+class LottieDrawable;
class Bitmap;
class Paint;
struct Typeface;
@@ -242,6 +243,7 @@ public:
const Paint* paint) = 0;
virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) = 0;
+ virtual void drawLottie(LottieDrawable* lottieDrawable) = 0;
virtual void drawPicture(const SkPicture& picture) = 0;
/**
diff --git a/libs/hwui/hwui/LottieDrawable.cpp b/libs/hwui/hwui/LottieDrawable.cpp
new file mode 100644
index 0000000000000..92dc51e01a855
--- /dev/null
+++ b/libs/hwui/hwui/LottieDrawable.cpp
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LottieDrawable.h"
+
+#include
+#include
+#include
+
+namespace android {
+
+sk_sp LottieDrawable::Make(sk_sp animation, size_t bytesUsed) {
+ if (animation) {
+ return sk_sp(new LottieDrawable(std::move(animation), bytesUsed));
+ }
+ return nullptr;
+}
+LottieDrawable::LottieDrawable(sk_sp animation, size_t bytesUsed)
+ : mAnimation(std::move(animation)), mBytesUsed(bytesUsed) {}
+
+bool LottieDrawable::start() {
+ if (mRunning) {
+ return false;
+ }
+
+ mRunning = true;
+ return true;
+}
+
+bool LottieDrawable::stop() {
+ bool wasRunning = mRunning;
+ mRunning = false;
+ return wasRunning;
+}
+
+bool LottieDrawable::isRunning() {
+ return mRunning;
+}
+
+// TODO: Check to see if drawable is actually dirty
+bool LottieDrawable::isDirty() {
+ return true;
+}
+
+void LottieDrawable::onDraw(SkCanvas* canvas) {
+ if (mRunning) {
+ const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
+
+ nsecs_t t = 0;
+ if (mStartTime == 0) {
+ mStartTime = currentTime;
+ } else {
+ t = currentTime - mStartTime;
+ }
+ double seekTime = std::fmod((double)t * 1e-9, mAnimation->duration());
+ mAnimation->seekFrameTime(seekTime);
+ mAnimation->render(canvas);
+ }
+}
+
+void LottieDrawable::drawStaging(SkCanvas* canvas) {
+ onDraw(canvas);
+}
+
+SkRect LottieDrawable::onGetBounds() {
+ // We do not actually know the bounds, so give a conservative answer.
+ return SkRectMakeLargest();
+}
+
+} // namespace android
diff --git a/libs/hwui/hwui/LottieDrawable.h b/libs/hwui/hwui/LottieDrawable.h
new file mode 100644
index 0000000000000..9cc34bf12f4fb
--- /dev/null
+++ b/libs/hwui/hwui/LottieDrawable.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include
+#include
+#include
+
+class SkCanvas;
+
+namespace android {
+
+/**
+ * Native component of android.graphics.drawable.LottieDrawable.java.
+ * This class can be drawn into Canvas.h and maintains the state needed to drive
+ * the animation from the RenderThread.
+ */
+class LottieDrawable : public SkDrawable {
+public:
+ static sk_sp Make(sk_sp animation, size_t bytes);
+
+ // Draw to software canvas
+ void drawStaging(SkCanvas* canvas);
+
+ // Returns true if the animation was started; false otherwise (e.g. it was
+ // already running)
+ bool start();
+ // Returns true if the animation was stopped; false otherwise (e.g. it was
+ // already stopped)
+ bool stop();
+ bool isRunning();
+
+ // TODO: Is dirty should take in a time til next frame to determine if it is dirty
+ bool isDirty();
+
+ SkRect onGetBounds() override;
+
+ size_t byteSize() const { return sizeof(*this) + mBytesUsed; }
+
+protected:
+ void onDraw(SkCanvas* canvas) override;
+
+private:
+ LottieDrawable(sk_sp animation, size_t bytes_used);
+
+ sk_sp mAnimation;
+ bool mRunning = false;
+ // The start time for the drawable itself.
+ nsecs_t mStartTime = 0;
+ const size_t mBytesUsed = 0;
+};
+
+} // namespace android
diff --git a/libs/hwui/jni/LottieDrawable.cpp b/libs/hwui/jni/LottieDrawable.cpp
new file mode 100644
index 0000000000000..fb6eede213a85
--- /dev/null
+++ b/libs/hwui/jni/LottieDrawable.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include
+#include
+#include
+#include
+
+#include "GraphicsJNI.h"
+#include "Utils.h"
+
+using namespace android;
+
+static jclass gLottieDrawableClass;
+
+static jlong LottieDrawable_nCreate(JNIEnv* env, jobject, jstring jjson) {
+ const ScopedUtfChars cstr(env, jjson);
+ // TODO(b/259267150) provide more accurate byteSize
+ size_t bytes = strlen(cstr.c_str());
+ auto animation = skottie::Animation::Builder().make(cstr.c_str(), bytes);
+ sk_sp drawable(LottieDrawable::Make(std::move(animation), bytes));
+ if (!drawable) {
+ return 0;
+ }
+ return reinterpret_cast(drawable.release());
+}
+
+static void LottieDrawable_destruct(LottieDrawable* drawable) {
+ SkSafeUnref(drawable);
+}
+
+static jlong LottieDrawable_nGetNativeFinalizer(JNIEnv* /*env*/, jobject /*clazz*/) {
+ return static_cast(reinterpret_cast(&LottieDrawable_destruct));
+}
+
+static void LottieDrawable_nDraw(JNIEnv* env, jobject /*clazz*/, jlong nativePtr, jlong canvasPtr) {
+ auto* drawable = reinterpret_cast(nativePtr);
+ auto* canvas = reinterpret_cast