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 "include/core/SkImage.h"
11cb93a386Sopenharmony_ci#include "include/core/SkTileMode.h"
12cb93a386Sopenharmony_ci#include "modules/androidkit/src/Utils.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_cinamespace {
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cijlong Image_Create(JNIEnv* env, jobject, jbyteArray jdata) {
17cb93a386Sopenharmony_ci    auto  size = env->GetArrayLength(jdata);
18cb93a386Sopenharmony_ci    auto* data = env->GetByteArrayElements(jdata, nullptr);
19cb93a386Sopenharmony_ci    auto image = SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, SkToSizeT(size)));
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci    env->ReleaseByteArrayElements(jdata, data, 0);
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci    return reinterpret_cast<jlong>(image.release());
24cb93a386Sopenharmony_ci}
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_civoid Image_Release(JNIEnv*, jobject, jlong native_instance) {
27cb93a386Sopenharmony_ci    SkSafeUnref(reinterpret_cast<const SkImage*>(native_instance));
28cb93a386Sopenharmony_ci}
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_cijint Image_GetWidth(JNIEnv*, jobject, jlong native_instance) {
31cb93a386Sopenharmony_ci    const auto* image = reinterpret_cast<const SkImage*>(native_instance);
32cb93a386Sopenharmony_ci    return image ? image->width() : 0;
33cb93a386Sopenharmony_ci}
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_cijint Image_GetHeight(JNIEnv*, jobject, jlong native_instance) {
36cb93a386Sopenharmony_ci    const auto* image = reinterpret_cast<const SkImage*>(native_instance);
37cb93a386Sopenharmony_ci    return image ? image->height() : 0;
38cb93a386Sopenharmony_ci}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_cijlong Image_MakeShader(JNIEnv*, jobject, jlong native_instance, jint jtmx, jint jtmy,
41cb93a386Sopenharmony_ci                       jint sampling_desc, jfloat sampling_b, jfloat sampling_c,
42cb93a386Sopenharmony_ci                       jlong native_matrix) {
43cb93a386Sopenharmony_ci    sk_sp<SkShader> shader;
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ci    if (const auto* image = reinterpret_cast<const SkImage*>(native_instance)) {
46cb93a386Sopenharmony_ci        const auto tmx = androidkit::utils::TileMode(jtmx),
47cb93a386Sopenharmony_ci                   tmy = androidkit::utils::TileMode(jtmy);
48cb93a386Sopenharmony_ci        const auto sampling = androidkit::utils::SamplingOptions(sampling_desc,
49cb93a386Sopenharmony_ci                                                                 sampling_b, sampling_c);
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci        const auto* lm = reinterpret_cast<const SkM44*>(native_matrix);
52cb93a386Sopenharmony_ci        shader = lm
53cb93a386Sopenharmony_ci            ? image->makeShader(tmx, tmy, sampling, lm->asM33())
54cb93a386Sopenharmony_ci            : image->makeShader(tmx, tmy, sampling);
55cb93a386Sopenharmony_ci    }
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    return reinterpret_cast<jlong>(shader.release());
58cb93a386Sopenharmony_ci}
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci} // namespace
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ciint register_androidkit_Image(JNIEnv* env) {
63cb93a386Sopenharmony_ci    static const JNINativeMethod methods[] = {
64cb93a386Sopenharmony_ci        {"nCreate"    , "([B)J"      , reinterpret_cast<void*>(Image_Create)   },
65cb93a386Sopenharmony_ci        {"nRelease"   , "(J)V"       , reinterpret_cast<void*>(Image_Release)  },
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci        {"nGetWidth"  , "(J)I"       , reinterpret_cast<void*>(Image_GetWidth) },
68cb93a386Sopenharmony_ci        {"nGetHeight" , "(J)I"       , reinterpret_cast<void*>(Image_GetHeight)},
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci        {"nMakeShader", "(JIIIFFJ)J" , reinterpret_cast<void*>(Image_MakeShader)},
71cb93a386Sopenharmony_ci    };
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    const auto clazz = env->FindClass("org/skia/androidkit/Image");
74cb93a386Sopenharmony_ci    return clazz
75cb93a386Sopenharmony_ci        ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
76cb93a386Sopenharmony_ci        : JNI_ERR;
77cb93a386Sopenharmony_ci}
78