Skip to content

Commit 41c853b

Browse files
committed
feat(android): support runtime 8.2.0 and improved perfs with typed arrays
1 parent 712962c commit 41c853b

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.akylas.canvas;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Size;
5+
6+
public class CanvasMatrix extends android.graphics.Matrix {
7+
public void mapPointsBuffer (java.nio.FloatBuffer pts) {
8+
float[] ptsArray = new float[pts.capacity()];
9+
pts.get(ptsArray);
10+
mapPoints(ptsArray);
11+
pts.rewind();
12+
pts.put(ptsArray);
13+
pts.rewind();
14+
}
15+
public void mapPointsBuffer (java.nio.FloatBuffer dst, java.nio.FloatBuffer src) {
16+
float[] dstArray = new float[dst.capacity()];
17+
dst.get(dstArray);
18+
float[] srcArray = new float[src.capacity()];
19+
src.get(srcArray);
20+
mapPoints(dstArray, srcArray);
21+
dst.rewind();
22+
dst.put(dstArray);
23+
dst.rewind();
24+
src.rewind();
25+
src.put(srcArray);
26+
src.rewind();
27+
}
28+
public void mapPointsBuffer (java.nio.FloatBuffer dst, int dstIndex, java.nio.FloatBuffer src, int srcIndex, int pointCount) {
29+
float[] dstArray = new float[dst.capacity()];
30+
dst.get(dstArray);
31+
float[] srcArray = new float[src.capacity()];
32+
src.get(srcArray);
33+
mapPoints(dstArray, dstIndex, srcArray, srcIndex, pointCount);
34+
dst.rewind();
35+
dst.put(dstArray);
36+
dst.rewind();
37+
src.rewind();
38+
src.put(srcArray);
39+
src.rewind();
40+
}
41+
}

plugin/platforms/android/java/com/akylas/canvas/CanvasPath.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ public CanvasPath(android.graphics.Path path) {
1616
public void addLines(@Size(multiple = 2, min = 4) @NonNull float[] points) {
1717
addLines(points, 0, points.length, false);
1818
}
19+
public void addLinesBuffer(@NonNull java.nio.FloatBuffer points) {
20+
addLinesBuffer(points, 0, points.capacity(), false);
21+
}
1922

2023
public void addLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int offset) {
2124
addLines(points, offset, points.length - offset, false);
2225
}
26+
public void addLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset) {
27+
addLinesBuffer(points, offset, points.capacity() - offset, false);
28+
}
2329

2430
public void addLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int offset, int length) {
2531
addLines(points, offset, length, false);
2632
}
33+
public void addLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length) {
34+
addLinesBuffer(points, offset, length, false);
35+
}
2736

2837
public void addPath(CanvasPath path) {
2938
super.addPath(path);
@@ -40,35 +49,73 @@ public void addLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int o
4049
close();
4150
}
4251
}
52+
public void addLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length, boolean close) {
53+
final int l = offset + length;
54+
55+
moveTo(points.get(offset), points.get(offset + 1));
56+
for (int i = offset + 2; i < l; i += 2) {
57+
lineTo(points.get(i), points.get(i + 1));
58+
}
59+
if (close) {
60+
close();
61+
}
62+
points.rewind();
63+
}
4364

4465
public void setLines(@Size(multiple = 2, min = 4) @NonNull float[] points) {
4566
setLines(points, 0, points.length, false);
4667
}
4768

69+
public void setLinesBuffer(@NonNull java.nio.FloatBuffer points) {
70+
setLinesBuffer(points, 0, points.capacity(), false);
71+
}
72+
4873
public void setLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int offset) {
4974
setLines(points, offset, points.length - offset, false);
5075
}
76+
public void setLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset) {
77+
setLinesBuffer(points, offset, points.capacity() - offset, false);
78+
}
5179

5280
public void setLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int offset, int length) {
5381
setLines(points, offset, length, false);
5482
}
5583

84+
public void setLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length) {
85+
setLinesBuffer(points, offset, length, false);
86+
}
87+
5688
public void setLines(@Size(multiple = 2, min = 4) @NonNull float[] points, int offset, int length, boolean close) {
5789
reset();
5890
addLines(points, offset, length, close);
5991
}
92+
public void setLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length, boolean close) {
93+
reset();
94+
addLinesBuffer(points, offset, length, close);
95+
}
6096

6197
public void addCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points) {
6298
addCubicLines(points, 0, points.length, false);
6399
}
64100

101+
public void addCubicLinesBuffer(@NonNull java.nio.FloatBuffer points) {
102+
addCubicLinesBuffer(points, 0, points.capacity(), false);
103+
}
104+
65105
public void addCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points, int offset) {
66106
addCubicLines(points, offset, points.length - offset, false);
67107
}
68108

109+
public void addCubicLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset) {
110+
addCubicLinesBuffer(points, offset, points.capacity() - offset, false);
111+
}
112+
69113
public void addCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points, int offset, int length) {
70114
addCubicLines(points, offset, length, false);
71115
}
116+
public void addCubicLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length) {
117+
addCubicLinesBuffer(points, offset, length, false);
118+
}
72119

73120
public void addCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points, int offset, int length,
74121
boolean close) {
@@ -81,18 +128,41 @@ public void addCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points,
81128
close();
82129
}
83130
}
131+
public void addCubicLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length,
132+
boolean close) {
133+
final int l = length;
134+
moveTo(points.get(offset), points.get(offset + 1));
135+
for (int i = offset + 2; i < l; i += 6) {
136+
cubicTo(points.get(i), points.get(offset + 1), points.get(offset + 2), points.get(offset + 3), points.get(offset + 4), points.get(offset + 5));
137+
}
138+
if (close) {
139+
close();
140+
}
141+
points.rewind();
142+
}
84143

85144
public void setCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points) {
86145
setCubicLines(points, 0, points.length, false);
87146
}
147+
public void setCubicLinesBuffer(@NonNull java.nio.FloatBuffer points) {
148+
setCubicLinesBuffer(points, 0, points.capacity(), false);
149+
}
88150

89151
public void setCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points, int offset, int length) {
90152
setCubicLines(points, offset, length, false);
91153
}
154+
public void setCubicLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length) {
155+
setCubicLinesBuffer(points, offset, length, false);
156+
}
92157

93158
public void setCubicLines(@Size(multiple = 2, min = 8) @NonNull float[] points, int offset, int length,
94159
boolean close) {
95160
reset();
96161
addCubicLines(points, offset, length, close);
97162
}
163+
public void setCubicLinesBuffer(@NonNull java.nio.FloatBuffer points, int offset, int length,
164+
boolean close) {
165+
reset();
166+
addCubicLinesBuffer(points, offset, length, close);
167+
}
98168
}

src/canvas.android.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function pointsFromBuffer(buffer: ArrayBuffer, useInts = false) {
6060
}
6161

6262
export function arrayoNativeArray(array, useInts = false) {
63+
console.log('arrayoNativeArray', Array.isArray(array));
6364
if (!Array.isArray(array)) {
6465
return array;
6566
}
@@ -401,11 +402,11 @@ export class Path {
401402
const element = args[index];
402403
if (element && element._native) {
403404
args[index] = element.getNative();
405+
} else if (Array.isArray(element)) {
406+
console.log('test', 'methodName', index, element);
407+
args[index] = arrayoNativeArray(element);
404408
}
405409
}
406-
if (methodName === 'addLines' || methodName === 'setLines' || methodName === 'addCubicLines' || methodName === 'setCubicLines') {
407-
args[0] = arrayoNativeArray(args[0]);
408-
}
409410
return native[methodName](...args);
410411
};
411412
} else {
@@ -549,7 +550,7 @@ function initClasses() {
549550
Op = android.graphics.Region.Op;
550551
Direction = android.graphics.Path.Direction;
551552
FillType = android.graphics.Path.FillType;
552-
Matrix = android.graphics.Matrix;
553+
Matrix = com.akylas.canvas.CanvasMatrix;
553554
TileMode = android.graphics.Shader.TileMode;
554555
LayoutAlignment = android.text.Layout.Alignment;
555556
PorterDuffMode = android.graphics.PorterDuff.Mode;

src/typings/android.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/* eslint-disable @typescript-eslint/unified-signatures */
12
declare namespace com {
23
export namespace akylas {
34
export namespace canvas {
5+
export class CanvasMatrix extends globalAndroid.graphics.Matrix {
6+
mapPoints(param0: Float32Array | native.Array<number>, param1: Float32Array | native.Array<number>): void;
7+
mapPoints(param0: Float32Array | native.Array<number>, param1: number, param2: Float32Array | native.Array<number>, param3: number, param4: number): void;
8+
mapPoints(param0: Float32Array | native.Array<number>): void;
9+
}
410
export class CanvasPath extends globalAndroid.graphics.Path {
511
addLines(points: number[], offset?: number, length?: number, close?: boolean);
612
setLines(points: number[], offset?: number, length?: number, close?: boolean);

0 commit comments

Comments
 (0)