Skip to content

Commit 5241856

Browse files
committed
fix(android): support runtime 8.2.0
1 parent 3be624b commit 5241856

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

src/canvas.android.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { layout } from '@nativescript/core/utils/utils';
66
import { Canvas as ICanvas, Paint as IPaint } from './canvas';
77
import { CanvasBase, hardwareAcceleratedProperty } from './canvas.common';
88

9+
declare global {
10+
const __runtimeVersion: string;
11+
}
12+
913
export * from './canvas.common';
1014
export {
1115
Canvas,
@@ -37,26 +41,44 @@ function getSDK() {
3741
return SDK_INT;
3842
}
3943

44+
let _runtimeVersion;
45+
let _supportsDirectArrayBuffers;
46+
export function supportsDirectArrayBuffers() {
47+
if (_supportsDirectArrayBuffers === undefined) {
48+
if (!_runtimeVersion) {
49+
_runtimeVersion = __runtimeVersion;
50+
}
51+
_supportsDirectArrayBuffers = parseInt(_runtimeVersion[0], 10) > 8 || (parseInt(_runtimeVersion[0], 10) === 8 && parseInt(_runtimeVersion[2], 10) >= 2);
52+
}
53+
return _supportsDirectArrayBuffers;
54+
}
55+
4056
export function createArrayBuffer(length: number, useInts = false) {
41-
let bb: java.nio.ByteBuffer;
42-
if (useInts) {
43-
bb = java.nio.ByteBuffer.allocateDirect(length);
44-
} else {
45-
bb = java.nio.ByteBuffer.allocateDirect(length * 4).order(java.nio.ByteOrder.LITTLE_ENDIAN);
46-
}
47-
const result = (ArrayBuffer as any).from(bb);
48-
return useInts ? new Int8Array(result) : new Float32Array(result);
57+
if (global.isAndroid && !supportsDirectArrayBuffers()) {
58+
let bb: java.nio.ByteBuffer;
59+
if (useInts) {
60+
bb = java.nio.ByteBuffer.allocateDirect(length);
61+
} else {
62+
bb = java.nio.ByteBuffer.allocateDirect(length * 4).order(java.nio.ByteOrder.LITTLE_ENDIAN);
63+
}
64+
const result = (ArrayBuffer as any).from(bb);
65+
return useInts ? new Int8Array(result) : new Float32Array(result);
66+
}
67+
return useInts ? new Int8Array(length) : new Float32Array(length);
4968
}
5069
export function pointsFromBuffer(typedArray: Float32Array | Int8Array, useInts = false) {
51-
if (useInts) {
70+
if (global.isAndroid && !supportsDirectArrayBuffers()) {
71+
if (useInts) {
72+
const buffer = typedArray.buffer;
73+
return ((buffer as any).nativeObject as java.nio.ByteBuffer).array();
74+
}
5275
const buffer = typedArray.buffer;
53-
return ((buffer as any).nativeObject as java.nio.ByteBuffer).array();
76+
const length = typedArray.length;
77+
const testArray = Array.create('float', length);
78+
((buffer as any).nativeObject as java.nio.ByteBuffer).asFloatBuffer().get(testArray, 0, length);
79+
return testArray as number[];
5480
}
55-
const buffer = typedArray.buffer;
56-
const length = typedArray.length;
57-
const testArray = Array.create('float', length);
58-
((buffer as any).nativeObject as java.nio.ByteBuffer).asFloatBuffer().get(testArray, 0, length);
59-
return testArray as number[];
81+
return typedArray;
6082
}
6183

6284
export function arrayToNativeArray(array, useInts = false) {
@@ -69,6 +91,26 @@ export function arrayToNativeArray(array, useInts = false) {
6991
return pointsFromBuffer(typedArray, useInts);
7092
}
7193

94+
// export const nativeArrayToArray = profile('nativeArrayToArray', function(array) {
95+
export function nativeArrayToArray(array) {
96+
if (global.isAndroid && !supportsDirectArrayBuffers()) {
97+
const result = [];
98+
for (let index = 0; index < array.length; index++) {
99+
result[index] = array[index];
100+
}
101+
102+
return result as number[];
103+
}
104+
return array;
105+
}
106+
export function createNativeArray(length) {
107+
if (global.isAndroid) {
108+
return Array.create('float', length);
109+
}
110+
// At least, set length to use it for iterations
111+
return new Array(length);
112+
}
113+
72114
export function parseDashEffect(value: string) {
73115
const array = value.split(' ').map(parseFloat);
74116
const length = array.length;

0 commit comments

Comments
 (0)