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/SkImageFilter.h"
11 #include "include/core/SkPoint3.h"
12 #include "include/effects/SkImageFilters.h"
13 #include "modules/androidkit/src/Utils.h"
14
15 namespace {
16
ImageFilter_Release(JNIEnv * env,jobject,jlong native_imageFilter)17 static void ImageFilter_Release(JNIEnv* env, jobject, jlong native_imageFilter) {
18 SkSafeUnref(reinterpret_cast<SkImageFilter*>(native_imageFilter));
19 }
20
ImageFilter_DistantLitDiffuse(JNIEnv * env,jobject,jfloat x,jfloat y,jfloat z,jfloat r,jfloat g,jfloat b,jfloat surfaceScale,jfloat kd,jlong native_input)21 static long ImageFilter_DistantLitDiffuse(JNIEnv* env, jobject, jfloat x, jfloat y, jfloat z,
22 jfloat r, jfloat g, jfloat b,
23 jfloat surfaceScale, jfloat kd,
24 jlong native_input) {
25 SkPoint3 direction = SkPoint3::Make(x, y, z);
26 auto color = SkColor4f{r, g, b, 1}.toSkColor();
27 auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
28 auto filter = SkImageFilters::DistantLitDiffuse(direction, color, surfaceScale, kd, std::move(input));
29 return reinterpret_cast<jlong>(filter.release());
30 }
31
ImageFilter_Blur(JNIEnv * env,jobject,jfloat sigmaX,jfloat sigmaY,jint jTileMode,jlong native_input)32 static long ImageFilter_Blur(JNIEnv* env, jobject, jfloat sigmaX, jfloat sigmaY,
33 jint jTileMode, jlong native_input) {
34 auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
35 auto filter = SkImageFilters::Blur(sigmaX, sigmaY,
36 androidkit::utils::TileMode(jTileMode),
37 std::move(input));
38 return reinterpret_cast<jlong>(filter.release());
39 }
40
ImageFilter_DropShadow(JNIEnv * env,jobject,jfloat dx,jfloat dy,jfloat sigmaX,jfloat sigmaY,jfloat r,jfloat g,jfloat b,jlong native_input)41 static long ImageFilter_DropShadow(JNIEnv* env, jobject, jfloat dx, jfloat dy,
42 jfloat sigmaX, jfloat sigmaY,
43 jfloat r, jfloat g, jfloat b,
44 jlong native_input) {
45 auto color = SkColor4f{r, g, b, 1}.toSkColor();
46 auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
47 auto filter = SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input));
48 return reinterpret_cast<jlong>(filter.release());
49 }
50
ImageFilter_Blend(JNIEnv * env,jobject,jint bm,jlong background,jlong foreground)51 static long ImageFilter_Blend(JNIEnv* env, jobject, jint bm, jlong background, jlong foreground) {
52 auto bg = sk_ref_sp(reinterpret_cast<SkImageFilter*>(background));
53 auto fg = sk_ref_sp(reinterpret_cast<SkImageFilter*>(foreground));
54 auto filter = SkImageFilters::Blend(androidkit::utils::BlendMode(bm), std::move(bg), std::move(fg));
55 return reinterpret_cast<jlong>(filter.release());
56 }
57
ImageFilter_Image(JNIEnv * env,jobject,jlong native_image)58 static long ImageFilter_Image(JNIEnv* env, jobject, jlong native_image) {
59 auto image = sk_ref_sp(reinterpret_cast<SkImage*>(native_image));
60 auto filter = SkImageFilters::Image(std::move(image));
61 return reinterpret_cast<jlong>(filter.release());
62 }
63
64 } // namespace
65
register_androidkit_ImageFilter(JNIEnv * env)66 int register_androidkit_ImageFilter(JNIEnv* env) {
67 static const JNINativeMethod methods[] = {
68 {"nRelease" , "(J)V" , reinterpret_cast<void*>(ImageFilter_Release)},
69 {"nDistantLitDiffuse", "(FFFFFFFFJ)J", reinterpret_cast<void*>(ImageFilter_DistantLitDiffuse)},
70 {"nBlur" , "(FFIJ)J" , reinterpret_cast<void*>(ImageFilter_Blur)},
71 {"nDropShadow" , "(FFFFFFFJ)J" , reinterpret_cast<void*>(ImageFilter_DropShadow)},
72 {"nBlend" , "(IJJ)J" , reinterpret_cast<void*>(ImageFilter_Blend)},
73 {"nImage" , "(J)J" , reinterpret_cast<void*>(ImageFilter_Image)},
74 };
75
76 const auto clazz = env->FindClass("org/skia/androidkit/ImageFilter");
77 return clazz
78 ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
79 : JNI_ERR;
80 }
81