• 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 "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkPaint.h"
11 
12 #include <jni.h>
13 
14 namespace {
15 
Canvas_Save(JNIEnv * env,jobject,jlong native_instance)16 void Canvas_Save(JNIEnv* env, jobject, jlong native_instance) {
17     if (auto* canvas = reinterpret_cast<SkCanvas*>(native_instance)) {
18         canvas->save();
19     }
20 }
21 
Canvas_Restore(JNIEnv * env,jobject,jlong native_instance)22 void Canvas_Restore(JNIEnv* env, jobject, jlong native_instance) {
23     if (auto* canvas = reinterpret_cast<SkCanvas*>(native_instance)) {
24         canvas->restore();
25     }
26 }
27 
Canvas_Concat(JNIEnv * env,jobject,jlong native_instance,jlong native_matrix)28 void Canvas_Concat(JNIEnv* env, jobject, jlong native_instance, jlong native_matrix) {
29     auto* canvas = reinterpret_cast<SkCanvas*>(native_instance);
30     auto* matrix = reinterpret_cast<SkM44*   >(native_matrix);
31 
32     if (canvas && matrix) {
33         canvas->concat(*matrix);
34     }
35 }
36 
Canvas_Concat16f(JNIEnv * env,jobject,jlong native_instance,jfloatArray jmatrix)37 void Canvas_Concat16f(JNIEnv* env, jobject, jlong native_instance, jfloatArray jmatrix) {
38     SkASSERT(env->GetArrayLength(jmatrix) == 16);
39 
40     if (auto* canvas = reinterpret_cast<SkCanvas*>(native_instance)) {
41         auto* m = env->GetFloatArrayElements(jmatrix, nullptr);
42         canvas->concat(SkM44::RowMajor(m));
43         env->ReleaseFloatArrayElements(jmatrix, m, 0);
44     }
45 }
46 
Canvas_DrawColor(JNIEnv * env,jobject,jlong native_instance,float r,float g,float b,float a)47 void Canvas_DrawColor(JNIEnv* env, jobject, jlong native_instance,
48                       float r, float g, float b, float a) {
49     if (auto* canvas = reinterpret_cast<SkCanvas*>(native_instance)) {
50         canvas->drawColor(SkColor4f{r, g, b, a});
51     }
52 }
53 
Canvas_DrawRect(JNIEnv * env,jobject,jlong native_instance,jfloat left,jfloat top,jfloat right,jfloat bottom,jlong native_paint)54 void Canvas_DrawRect(JNIEnv* env, jobject, jlong native_instance,
55                      jfloat left, jfloat top, jfloat right, jfloat bottom,
56                      jlong native_paint) {
57     auto* canvas = reinterpret_cast<SkCanvas*>(native_instance);
58     auto* paint  = reinterpret_cast<SkPaint* >(native_paint);
59     if (canvas && paint) {
60         canvas->drawRect(SkRect::MakeLTRB(left, top, right, bottom), *paint);
61     }
62 }
63 
64 }  // namespace
65 
register_androidkit_Canvas(JNIEnv * env)66 int register_androidkit_Canvas(JNIEnv* env) {
67     static const JNINativeMethod methods[] = {
68         {"nSave"     , "(J)V"     , reinterpret_cast<void*>(Canvas_Save)     },
69         {"nRestore"  , "(J)V"     , reinterpret_cast<void*>(Canvas_Restore)  },
70         {"nConcat"   , "(JJ)V"    , reinterpret_cast<void*>(Canvas_Concat)   },
71         {"nConcat16f", "(J[F)V"   , reinterpret_cast<void*>(Canvas_Concat16f)},
72         {"nDrawColor", "(JFFFF)V" , reinterpret_cast<void*>(Canvas_DrawColor)},
73         {"nDrawRect" , "(JFFFFJ)V", reinterpret_cast<void*>(Canvas_DrawRect) },
74     };
75 
76     const auto clazz = env->FindClass("org/skia/androidkit/Canvas");
77     return clazz
78         ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
79         : JNI_ERR;
80 }
81