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