xref: /aosp_15_r20/external/skia/modules/jetski/src/SkottieAnimation.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2021 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include <jni.h>
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include "modules/jetski/src/Utils.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "modules/skottie/include/Skottie.h"
12*c8dee2aaSAndroid Build Coastguard Worker 
13*c8dee2aaSAndroid Build Coastguard Worker using namespace skottie;
14*c8dee2aaSAndroid Build Coastguard Worker 
15*c8dee2aaSAndroid Build Coastguard Worker namespace {
16*c8dee2aaSAndroid Build Coastguard Worker 
Animation_Create(JNIEnv * env,jobject,jstring jjson)17*c8dee2aaSAndroid Build Coastguard Worker static jlong Animation_Create(JNIEnv* env, jobject, jstring jjson) {
18*c8dee2aaSAndroid Build Coastguard Worker     const jetski::utils::CString cstr(env, jjson);
19*c8dee2aaSAndroid Build Coastguard Worker 
20*c8dee2aaSAndroid Build Coastguard Worker     // TODO: more builder opts
21*c8dee2aaSAndroid Build Coastguard Worker     auto animation = Animation::Builder().make(cstr, strlen(cstr));
22*c8dee2aaSAndroid Build Coastguard Worker 
23*c8dee2aaSAndroid Build Coastguard Worker     return reinterpret_cast<jlong>(animation.release());
24*c8dee2aaSAndroid Build Coastguard Worker }
25*c8dee2aaSAndroid Build Coastguard Worker 
Animation_Release(JNIEnv,jobject,jlong native_animation)26*c8dee2aaSAndroid Build Coastguard Worker static void Animation_Release(JNIEnv, jobject, jlong native_animation) {
27*c8dee2aaSAndroid Build Coastguard Worker     SkSafeUnref(reinterpret_cast<Animation*>(native_animation));
28*c8dee2aaSAndroid Build Coastguard Worker }
29*c8dee2aaSAndroid Build Coastguard Worker 
Animation_GetDuration(JNIEnv,jobject,jlong native_animation)30*c8dee2aaSAndroid Build Coastguard Worker static jdouble Animation_GetDuration(JNIEnv, jobject, jlong native_animation) {
31*c8dee2aaSAndroid Build Coastguard Worker     const auto* animation = reinterpret_cast<const Animation*>(native_animation);
32*c8dee2aaSAndroid Build Coastguard Worker     return animation ? animation->duration() : 0;
33*c8dee2aaSAndroid Build Coastguard Worker }
34*c8dee2aaSAndroid Build Coastguard Worker 
Animation_GetFrameCnt(JNIEnv,jobject,jlong native_animation)35*c8dee2aaSAndroid Build Coastguard Worker static jdouble Animation_GetFrameCnt(JNIEnv, jobject, jlong native_animation) {
36*c8dee2aaSAndroid Build Coastguard Worker     const auto* animation = reinterpret_cast<const Animation*>(native_animation);
37*c8dee2aaSAndroid Build Coastguard Worker     return animation ? animation->outPoint() : 0;
38*c8dee2aaSAndroid Build Coastguard Worker }
39*c8dee2aaSAndroid Build Coastguard Worker 
Animation_GetWidth(JNIEnv,jobject,jlong native_animation)40*c8dee2aaSAndroid Build Coastguard Worker static jfloat Animation_GetWidth(JNIEnv, jobject, jlong native_animation) {
41*c8dee2aaSAndroid Build Coastguard Worker     const auto* animation = reinterpret_cast<const Animation*>(native_animation);
42*c8dee2aaSAndroid Build Coastguard Worker     return animation ? animation->size().width() : 0;
43*c8dee2aaSAndroid Build Coastguard Worker }
44*c8dee2aaSAndroid Build Coastguard Worker 
Animation_GetHeight(JNIEnv,jobject,jlong native_animation)45*c8dee2aaSAndroid Build Coastguard Worker static jfloat Animation_GetHeight(JNIEnv, jobject, jlong native_animation) {
46*c8dee2aaSAndroid Build Coastguard Worker     const auto* animation = reinterpret_cast<const Animation*>(native_animation);
47*c8dee2aaSAndroid Build Coastguard Worker     return animation ? animation->size().height() : 0;
48*c8dee2aaSAndroid Build Coastguard Worker }
49*c8dee2aaSAndroid Build Coastguard Worker 
Animation_SeekFrame(JNIEnv,jobject,jlong native_animation,jdouble frame)50*c8dee2aaSAndroid Build Coastguard Worker static void Animation_SeekFrame(JNIEnv, jobject, jlong native_animation, jdouble frame) {
51*c8dee2aaSAndroid Build Coastguard Worker     if (auto* animation = reinterpret_cast<Animation*>(native_animation)) {
52*c8dee2aaSAndroid Build Coastguard Worker         animation->seekFrame(frame);
53*c8dee2aaSAndroid Build Coastguard Worker     }
54*c8dee2aaSAndroid Build Coastguard Worker }
55*c8dee2aaSAndroid Build Coastguard Worker 
Animation_SeekTime(JNIEnv,jobject,jlong native_animation,jdouble t)56*c8dee2aaSAndroid Build Coastguard Worker static void Animation_SeekTime(JNIEnv, jobject, jlong native_animation, jdouble t) {
57*c8dee2aaSAndroid Build Coastguard Worker     if (auto* animation = reinterpret_cast<Animation*>(native_animation)) {
58*c8dee2aaSAndroid Build Coastguard Worker         animation->seekFrameTime(t);
59*c8dee2aaSAndroid Build Coastguard Worker     }
60*c8dee2aaSAndroid Build Coastguard Worker }
61*c8dee2aaSAndroid Build Coastguard Worker 
Animation_Render(JNIEnv,jobject,jlong native_animation,jlong native_canvas)62*c8dee2aaSAndroid Build Coastguard Worker static void Animation_Render(JNIEnv, jobject, jlong native_animation, jlong native_canvas) {
63*c8dee2aaSAndroid Build Coastguard Worker     const auto* animation = reinterpret_cast<const Animation*>(native_animation);
64*c8dee2aaSAndroid Build Coastguard Worker     auto* canvas = reinterpret_cast<SkCanvas*>(native_canvas);
65*c8dee2aaSAndroid Build Coastguard Worker     if (animation && canvas) {
66*c8dee2aaSAndroid Build Coastguard Worker         animation->render(canvas);
67*c8dee2aaSAndroid Build Coastguard Worker     }
68*c8dee2aaSAndroid Build Coastguard Worker }
69*c8dee2aaSAndroid Build Coastguard Worker 
70*c8dee2aaSAndroid Build Coastguard Worker } // namespace
71*c8dee2aaSAndroid Build Coastguard Worker 
register_jetski_SkottieAnimation(JNIEnv * env)72*c8dee2aaSAndroid Build Coastguard Worker int register_jetski_SkottieAnimation(JNIEnv* env) {
73*c8dee2aaSAndroid Build Coastguard Worker     static const JNINativeMethod methods[] = {
74*c8dee2aaSAndroid Build Coastguard Worker         {"nCreate"       , "(Ljava/lang/String;)J", reinterpret_cast<void*>(Animation_Create)     },
75*c8dee2aaSAndroid Build Coastguard Worker         {"nRelease"      , "(J)V"                 , reinterpret_cast<void*>(Animation_Release)    },
76*c8dee2aaSAndroid Build Coastguard Worker 
77*c8dee2aaSAndroid Build Coastguard Worker         {"nGetDuration"  , "(J)D"                 , reinterpret_cast<void*>(Animation_GetDuration)},
78*c8dee2aaSAndroid Build Coastguard Worker         {"nGetFrameCount", "(J)D"                 , reinterpret_cast<void*>(Animation_GetFrameCnt)},
79*c8dee2aaSAndroid Build Coastguard Worker         {"nGetWidth"     , "(J)F"                 , reinterpret_cast<void*>(Animation_GetWidth)   },
80*c8dee2aaSAndroid Build Coastguard Worker         {"nGetHeight"    , "(J)F"                 , reinterpret_cast<void*>(Animation_GetHeight)  },
81*c8dee2aaSAndroid Build Coastguard Worker 
82*c8dee2aaSAndroid Build Coastguard Worker         {"nSeekFrame"    , "(JD)V"                , reinterpret_cast<void*>(Animation_SeekFrame)  },
83*c8dee2aaSAndroid Build Coastguard Worker         {"nSeekTime"     , "(JD)V"                , reinterpret_cast<void*>(Animation_SeekTime)   },
84*c8dee2aaSAndroid Build Coastguard Worker         {"nRender"       , "(JJ)V"                , reinterpret_cast<void*>(Animation_Render)     },
85*c8dee2aaSAndroid Build Coastguard Worker     };
86*c8dee2aaSAndroid Build Coastguard Worker 
87*c8dee2aaSAndroid Build Coastguard Worker     const auto clazz = env->FindClass("org/skia/jetski/SkottieAnimation");
88*c8dee2aaSAndroid Build Coastguard Worker     return clazz
89*c8dee2aaSAndroid Build Coastguard Worker         ? env->RegisterNatives(clazz, methods, std::size(methods))
90*c8dee2aaSAndroid Build Coastguard Worker         : JNI_ERR;
91*c8dee2aaSAndroid Build Coastguard Worker }
92