Skip to content

Commit 9e4e8d9

Browse files
committed
chore: refactor
1 parent dad29b2 commit 9e4e8d9

File tree

4 files changed

+8
-118
lines changed

4 files changed

+8
-118
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@angular/router": "~10.1.0",
3535
"@commitlint/cli": "^13.2.0",
3636
"@commitlint/config-conventional": "^13.2.0",
37+
"@nativescript-community/arraybuffers": "^1.0.0",
3738
"@nativescript/angular": "10.1.0",
3839
"@nativescript/core": "8.1.3",
3940
"@nativescript/types-android": "8.1.0",

plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
},
3232
"license": "Apache-2.0",
3333
"readmeFilename": "README.md",
34+
"dependencies": {
35+
"@nativescript-community/arraybuffers": "^1.0.0"
36+
},
3437
"gitHead": "a1f853cec62ac3a7986af8121169926df2ff5d6a"
3538
}

src/canvas.android.ts

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { android as androidApp } from '@nativescript/core/application';
33
import { FontStyle, FontWeight } from '@nativescript/core/ui/styling/font';
44
import lazy from '@nativescript/core/utils/lazy';
55
import { layout } from '@nativescript/core/utils/utils';
6-
import { Canvas as ICanvas, Paint as IPaint, TypedArray } from './canvas';
6+
import { Canvas as ICanvas, Paint as IPaint } from './canvas';
77
import { CanvasBase, hardwareAcceleratedProperty } from './canvas.common';
88

9+
import { arrayToNativeArray } from '@nativescript-community/arraybuffers';
10+
911
declare global {
1012
const __runtimeVersion: string;
1113
}
@@ -42,94 +44,6 @@ function getSDK() {
4244
return SDK_INT;
4345
}
4446

45-
let _runtimeVersion;
46-
let _supportsDirectArrayBuffers;
47-
export function supportsDirectArrayBuffers() {
48-
if (_supportsDirectArrayBuffers === undefined) {
49-
if (!_runtimeVersion) {
50-
_runtimeVersion = __runtimeVersion;
51-
}
52-
_supportsDirectArrayBuffers = parseInt(_runtimeVersion[0], 10) > 8 || (parseInt(_runtimeVersion[0], 10) === 8 && parseInt(_runtimeVersion[2], 10) >= 2);
53-
}
54-
return _supportsDirectArrayBuffers;
55-
}
56-
57-
export function createArrayBufferOrNativeArray(length: number, useInts = false, canReturnBuffer = true) {
58-
// if (!supportsDirectArrayBuffers() || !canReturnBuffer) {
59-
// return createNativeArray(length, useInts);
60-
// } else {
61-
return createArrayBuffer(length, useInts, canReturnBuffer);
62-
// }
63-
}
64-
export function createArrayBuffer(length: number, useInts = false, canReturnBuffer = true): TypedArray {
65-
if (!supportsDirectArrayBuffers() || !canReturnBuffer) {
66-
let bb: java.nio.ByteBuffer;
67-
if (useInts) {
68-
bb = java.nio.ByteBuffer.allocateDirect(length);
69-
} else {
70-
bb = java.nio.ByteBuffer.allocateDirect(length * 4).order(java.nio.ByteOrder.LITTLE_ENDIAN);
71-
}
72-
const result = (ArrayBuffer as any).from(bb);
73-
//@ts-ignore
74-
return useInts ? new Int8Array(result) : new Float32Array(result);
75-
}
76-
//@ts-ignore
77-
return useInts ? new Int8Array(length) : new Float32Array(length);
78-
}
79-
export function pointsFromBuffer(typedArray: TypedArray, useInts = false, canReturnBuffer = true) {
80-
if (!supportsDirectArrayBuffers() || !canReturnBuffer) {
81-
if (useInts) {
82-
const buffer = typedArray.buffer;
83-
return ((buffer as any).nativeObject as java.nio.ByteBuffer).array();
84-
}
85-
const buffer = typedArray.buffer;
86-
const length = typedArray.length;
87-
const testArray = Array.create('float', length);
88-
((buffer as any).nativeObject as java.nio.ByteBuffer).asFloatBuffer().get(testArray, 0, length);
89-
return testArray as number[];
90-
}
91-
return typedArray;
92-
}
93-
94-
export function arrayToNativeArray(array: number[] | TypedArray, useInts = false, canReturnBuffer = true) {
95-
const isBufferView = ArrayBuffer.isView(array);
96-
if (!Array.isArray(array) && !isBufferView) {
97-
return array;
98-
}
99-
// for now we cant do it the old way
100-
if (!isBufferView && supportsDirectArrayBuffers()) {
101-
const nArray = createNativeArray(array.length, useInts);
102-
for (let index = 0; index < array.length; index++) {
103-
nArray[index] = array[index];
104-
}
105-
return nArray;
106-
}
107-
const length = array.length;
108-
const typedArray = ArrayBuffer.isView(array) ? (array as any as TypedArray) : createArrayBuffer(length, useInts);
109-
typedArray.set(array);
110-
return pointsFromBuffer(typedArray, useInts, canReturnBuffer);
111-
}
112-
113-
// export const nativeArrayToArray = profile('nativeArrayToArray', function(array) {
114-
export function nativeArrayToArray(array): number[] {
115-
if (!supportsDirectArrayBuffers()) {
116-
const result = [];
117-
for (let index = 0; index < array.length; index++) {
118-
result[index] = array[index];
119-
}
120-
121-
return result as number[];
122-
}
123-
return array;
124-
}
125-
export function createNativeArray(length, useInts = false): number[] {
126-
if (useInts) {
127-
return Array.create('int', length);
128-
} else {
129-
return Array.create('float', length);
130-
}
131-
}
132-
13347
export function parseDashEffect(value: string) {
13448
const array = value.split(' ').map(parseFloat);
13549
const length = array.length;
@@ -483,9 +397,7 @@ export class LinearGradient extends ProxyClass<android.graphics.LinearGradient>
483397
}
484398
if (param5 != null) {
485399
if (Array.isArray(param5)) {
486-
const testArray = Array.create('float', param4.length);
487-
param5.forEach((c, i) => (testArray[i] = c));
488-
param5 = testArray;
400+
param5 = arrayToNativeArray(param5, false, false);
489401
} else {
490402
param5 = createColorParam(param5);
491403
}

src/canvas.ios.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,29 +2323,3 @@ export class StaticLayout {
23232323
return result;
23242324
}
23252325
}
2326-
export function createArrayBufferOrNativeArray(length: number, useInts = false) {
2327-
return createArrayBuffer(length, useInts);
2328-
}
2329-
export function createArrayBuffer(length: number, useInts = false) {
2330-
if (useInts) {
2331-
return new Int8Array(length);
2332-
}
2333-
return new FloatConstructor(length);
2334-
}
2335-
export function pointsFromBuffer(typedArray: Float32Array | Int8Array, useInts = false) {
2336-
return typedArray;
2337-
}
2338-
2339-
export function arrayToNativeArray(array, useInts = false) {
2340-
return array;
2341-
}
2342-
export function nativeArrayToArray(array): number[] {
2343-
return array;
2344-
}
2345-
export function createNativeArray(length: number, useInts?): number[] {
2346-
return new Array(length);
2347-
}
2348-
2349-
export function supportsDirectArrayBuffers() {
2350-
return true;
2351-
}

0 commit comments

Comments
 (0)