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 package org.skia.androidkit;
9 
10 import org.skia.androidkit.Color;
11 import org.skia.androidkit.Matrix;
12 import org.skia.androidkit.Paint;
13 import org.skia.androidkit.Surface;
14 
15 public class Canvas {
16     private long mNativeInstance;
17     private Surface mSurface;
18 
save()19     public void save() {
20         nSave(mNativeInstance);
21     }
22 
restore()23     public void restore() {
24         nRestore(mNativeInstance);
25     }
26 
concat(Matrix m)27     public void concat(Matrix m) {
28         nConcat(mNativeInstance, m.getNativeInstance());
29     }
30 
concat(float[] rowMajorMatrix)31     public void concat(float[] rowMajorMatrix) {
32         if (rowMajorMatrix.length != 16) {
33             throw new java.lang.IllegalArgumentException("Expecting a 16 float array.");
34         }
35 
36         nConcat16f(mNativeInstance, rowMajorMatrix);
37     }
38 
drawRect(float left, float right, float top, float bottom, Paint paint)39     public void drawRect(float left, float right, float top, float bottom, Paint paint) {
40         nDrawRect(mNativeInstance, left, right, top, bottom, paint.getNativeInstance());
41     }
42 
drawColor(Color c)43     public void drawColor(Color c) {
44         nDrawColor(mNativeInstance, c.r(), c.g(), c.b(), c.a());
45     }
46 
drawColor(float r, float g, float b, float a)47     public void drawColor(float r, float g, float b, float a) {
48         nDrawColor(mNativeInstance, r, g, b, a);
49     }
50 
drawColor(int icolor)51     public void drawColor(int icolor) {
52         nDrawColor(mNativeInstance,
53             (float)((icolor >> 16) & 0xff) / 255,
54             (float)((icolor >>  8) & 0xff) / 255,
55             (float)((icolor >>  0) & 0xff) / 255,
56             (float)((icolor >> 24) & 0xff) / 255
57         );
58     }
59 
60     // package private
Canvas(Surface surface, long native_instance)61     Canvas(Surface surface, long native_instance) {
62         mNativeInstance = native_instance;
63         mSurface = surface;
64     }
65 
nSave(long nativeInstance)66     private static native void nSave(long nativeInstance);
nRestore(long nativeInstance)67     private static native void nRestore(long nativeInstance);
nConcat(long nativeInstance, long nativeMatrix)68     private static native void nConcat(long nativeInstance, long nativeMatrix);
nConcat16f(long nativeInstance, float[] floatMatrix)69     private static native void nConcat16f(long nativeInstance, float[] floatMatrix);
70 
nDrawColor(long nativeInstance, float r, float g, float b, float a)71     private static native void nDrawColor(long nativeInstance, float r, float g, float b, float a);
72 
nDrawRect(long nativeInstance, float left, float right, float top, float bottom, long nativePaint)73     private static native void nDrawRect(long nativeInstance,
74                                          float left, float right, float top, float bottom,
75                                          long nativePaint);
76 }
77