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