@@ -3,9 +3,11 @@ import { android as androidApp } from '@nativescript/core/application';
33import { FontStyle , FontWeight } from '@nativescript/core/ui/styling/font' ;
44import lazy from '@nativescript/core/utils/lazy' ;
55import { 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' ;
77import { CanvasBase , hardwareAcceleratedProperty } from './canvas.common' ;
88
9+ import { arrayToNativeArray } from '@nativescript-community/arraybuffers' ;
10+
911declare 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-
13347export 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 }
0 commit comments