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/jetski/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,jfloat l,jfloat t,jfloat r,jfloat b,jlong native_input)32 static long ImageFilter_Blur(JNIEnv* env, jobject, jfloat sigmaX, jfloat sigmaY,
33 jint jTileMode, jfloat l, jfloat t, jfloat r,
34 jfloat b, jlong native_input) {
35 auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
36 auto filter = SkImageFilters::Blur(sigmaX, sigmaY,
37 jetski::utils::TileMode(jTileMode),
38 std::move(input), SkRect::MakeLTRB(l,t,r,b));
39 return reinterpret_cast<jlong>(filter.release());
40 }
41
ImageFilter_DropShadow(JNIEnv * env,jobject,jfloat dx,jfloat dy,jfloat sigmaX,jfloat sigmaY,jfloat r,jfloat g,jfloat b,jlong native_input)42 static long ImageFilter_DropShadow(JNIEnv* env, jobject, jfloat dx, jfloat dy,
43 jfloat sigmaX, jfloat sigmaY,
44 jfloat r, jfloat g, jfloat b,
45 jlong native_input) {
46 auto color = SkColor4f{r, g, b, 1}.toSkColor();
47 auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
48 auto filter = SkImageFilters::DropShadow(dx, dy, sigmaX, sigmaY, color, std::move(input));
49 return reinterpret_cast<jlong>(filter.release());
50 }
51
ImageFilter_Blend(JNIEnv * env,jobject,jint bm,jlong background,jlong foreground)52 static long ImageFilter_Blend(JNIEnv* env, jobject, jint bm, jlong background, jlong foreground) {
53 auto bg = sk_ref_sp(reinterpret_cast<SkImageFilter*>(background));
54 auto fg = sk_ref_sp(reinterpret_cast<SkImageFilter*>(foreground));
55 auto filter = SkImageFilters::Blend(jetski::utils::BlendMode(bm), std::move(bg), std::move(fg));
56 return reinterpret_cast<jlong>(filter.release());
57 }
58
ImageFilter_Image(JNIEnv * env,jobject,jlong native_image)59 static long ImageFilter_Image(JNIEnv* env, jobject, jlong native_image) {
60 auto image = sk_ref_sp(reinterpret_cast<SkImage*>(native_image));
61 auto filter = SkImageFilters::Image(std::move(image), SkFilterMode::kLinear);
62 return reinterpret_cast<jlong>(filter.release());
63 }
64
65 } // namespace
66
register_jetski_ImageFilter(JNIEnv * env)67 int register_jetski_ImageFilter(JNIEnv* env) {
68 static const JNINativeMethod methods[] = {
69 {"nRelease" , "(J)V" , reinterpret_cast<void*>(ImageFilter_Release)},
70 {"nDistantLitDiffuse", "(FFFFFFFFJ)J" , reinterpret_cast<void*>(ImageFilter_DistantLitDiffuse)},
71 {"nBlur" , "(FFIFFFFJ)J" , reinterpret_cast<void*>(ImageFilter_Blur)},
72 {"nDropShadow" , "(FFFFFFFJ)J" , reinterpret_cast<void*>(ImageFilter_DropShadow)},
73 {"nBlend" , "(IJJ)J" , reinterpret_cast<void*>(ImageFilter_Blend)},
74 {"nImage" , "(J)J" , reinterpret_cast<void*>(ImageFilter_Image)},
75 };
76
77 const auto clazz = env->FindClass("org/skia/jetski/ImageFilter");
78 return clazz
79 ? env->RegisterNatives(clazz, methods, std::size(methods))
80 : JNI_ERR;
81 }
82