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