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