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/SkColorFilter.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h"
13 #include "include/effects/SkImageFilters.h"
14
15 namespace {
16
Paint_Create(JNIEnv *,jobject)17 static jlong Paint_Create(JNIEnv*, jobject) {
18 return reinterpret_cast<jlong>(new SkPaint);
19 }
20
Paint_Release(JNIEnv *,jobject,jlong native_paint)21 static void Paint_Release(JNIEnv*, jobject, jlong native_paint) {
22 delete reinterpret_cast<SkPaint*>(native_paint);
23 }
24
Paint_SetColor(JNIEnv *,jobject,jlong native_paint,float r,float g,float b,float a)25 static void Paint_SetColor(JNIEnv*, jobject, jlong native_paint,
26 float r, float g, float b, float a) {
27 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
28 paint->setColor4f({r, g, b, a});
29 }
30 }
31
Paint_SetStroke(JNIEnv *,jobject,jlong native_paint,jboolean stroke)32 static void Paint_SetStroke(JNIEnv*, jobject, jlong native_paint, jboolean stroke) {
33 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
34 paint->setStroke(stroke);
35 }
36 }
37
Paint_SetStrokeWidth(JNIEnv *,jobject,jlong native_paint,jfloat width)38 static void Paint_SetStrokeWidth(JNIEnv*, jobject, jlong native_paint, jfloat width) {
39 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
40 paint->setStrokeWidth(width);
41 }
42 }
43
Paint_SetStrokeCap(JNIEnv * env,jobject,jlong native_paint,jint native_cap)44 static void Paint_SetStrokeCap(JNIEnv* env, jobject, jlong native_paint, jint native_cap) {
45 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
46 switch (native_cap)
47 {
48 case 0:
49 paint->setStrokeCap(SkPaint::kButt_Cap);
50 break;
51 case 1:
52 paint->setStrokeCap(SkPaint::kRound_Cap);
53 break;
54 case 2:
55 paint->setStrokeCap(SkPaint::kSquare_Cap);
56 break;
57 default:
58 break;
59 }
60 }
61 }
62
Paint_SetStrokeJoin(JNIEnv * env,jobject,jlong native_paint,jint native_join)63 static void Paint_SetStrokeJoin(JNIEnv* env, jobject, jlong native_paint, jint native_join) {
64 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
65 switch (native_join)
66 {
67 case 0:
68 paint->setStrokeJoin(SkPaint::kMiter_Join);
69 break;
70 case 1:
71 paint->setStrokeJoin(SkPaint::kRound_Join);
72 break;
73 case 2:
74 paint->setStrokeJoin(SkPaint::kBevel_Join);
75 break;
76 default:
77 break;
78 }
79 }
80 }
81
Paint_SetStrokeMiter(JNIEnv * env,jobject,jlong native_paint,jfloat limit)82 static void Paint_SetStrokeMiter(JNIEnv* env, jobject, jlong native_paint, jfloat limit) {
83 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
84 paint->setStrokeMiter(limit);
85 }
86 }
87
Paint_SetColorFilter(JNIEnv *,jobject,jlong native_paint,jlong native_cf)88 static void Paint_SetColorFilter(JNIEnv*, jobject, jlong native_paint, jlong native_cf) {
89 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
90 paint->setColorFilter(sk_ref_sp(reinterpret_cast<SkColorFilter*>(native_cf)));
91 }
92 }
93
Paint_SetShader(JNIEnv *,jobject,jlong native_paint,jlong native_shader)94 static void Paint_SetShader(JNIEnv*, jobject, jlong native_paint, jlong native_shader) {
95 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
96 paint->setShader(sk_ref_sp(reinterpret_cast<SkShader*>(native_shader)));
97 }
98 }
99
Paint_SetImageFilter(JNIEnv *,jobject,jlong native_paint,jlong native_filter)100 static void Paint_SetImageFilter(JNIEnv*, jobject, jlong native_paint, jlong native_filter) {
101 if (auto* paint = reinterpret_cast<SkPaint*>(native_paint)) {
102 paint->setImageFilter(sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_filter)));
103 }
104 }
105
106 } // namespace
107
register_jetski_Paint(JNIEnv * env)108 int register_jetski_Paint(JNIEnv* env) {
109 static const JNINativeMethod methods[] = {
110 {"nCreate" , "()J" , reinterpret_cast<void*>(Paint_Create)},
111 {"nRelease" , "(J)V" , reinterpret_cast<void*>(Paint_Release)},
112 {"nSetColor" , "(JFFFF)V", reinterpret_cast<void*>(Paint_SetColor)},
113 {"nSetStroke" , "(JZ)V" , reinterpret_cast<void*>(Paint_SetStroke)},
114 {"nSetStrokeWidth" , "(JF)V" , reinterpret_cast<void*>(Paint_SetStrokeWidth)},
115 {"nSetStrokeCap" , "(JI)V" , reinterpret_cast<void*>(Paint_SetStrokeCap)},
116 {"nSetStrokeJoin" , "(JI)V" , reinterpret_cast<void*>(Paint_SetStrokeJoin)},
117 {"nSetStrokeMiter" , "(JF)V" , reinterpret_cast<void*>(Paint_SetStrokeMiter)},
118 {"nSetColorFilter" , "(JJ)V" , reinterpret_cast<void*>(Paint_SetColorFilter)},
119 {"nSetShader" , "(JJ)V" , reinterpret_cast<void*>(Paint_SetShader)},
120 {"nSetImageFilter" , "(JJ)V" , reinterpret_cast<void*>(Paint_SetImageFilter)},
121 };
122
123 const auto clazz = env->FindClass("org/skia/jetski/Paint");
124 return clazz
125 ? env->RegisterNatives(clazz, methods, std::size(methods))
126 : JNI_ERR;
127 }
128