Skip to content

Commit ef28823

Browse files
authored
Merge pull request #31 from farfromrefug/toNative_android
fix(android): handle typed array backed by native buffer
2 parents 934b5a3 + fb5ca0f commit ef28823

File tree

1 file changed

+110
-74
lines changed

1 file changed

+110
-74
lines changed

packages/canvas/WebGL/WebGLRenderingContext/index.android.ts

Lines changed: 110 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,24 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
249249
}
250250
//this.context.bufferData(target, this.toNativeArray(new Uint8Array(srcData as any) as any, 'byte'), usage);
251251
} else if (srcData && srcData.buffer instanceof ArrayBuffer) {
252-
if (srcData instanceof Uint8Array || srcData instanceof Uint8ClampedArray) {
253-
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'byte'), usage);
254-
this.context.bufferDataByte(target, Array.from(srcData), usage);
255-
} else if (srcData instanceof Uint16Array || srcData instanceof Int16Array) {
256-
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'short'), usage);
257-
this.context.bufferDataShort(target, Array.from(srcData), usage);
258-
} else if (srcData instanceof Uint32Array || srcData instanceof Int32Array) {
259-
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'int'), usage);
260-
this.context.bufferDataInt(target, Array.from(srcData), usage);
261-
} else if (srcData instanceof Float32Array) {
262-
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'float'), usage);
263-
this.context.bufferDataFloat(target, Array.from(srcData), usage);
252+
// @ts-ignore
253+
if (srcData.buffer.nativeObject) {
254+
// @ts-ignore
255+
this.context.bufferData(target, offset, srcData.buffer.nativeObject);
256+
} else {
257+
if (srcData instanceof Uint8Array || srcData instanceof Uint8ClampedArray) {
258+
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'byte'), usage);
259+
this.context.bufferDataByte(target, Array.from(srcData), usage);
260+
} else if (srcData instanceof Uint16Array || srcData instanceof Int16Array) {
261+
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'short'), usage);
262+
this.context.bufferDataShort(target, Array.from(srcData), usage);
263+
} else if (srcData instanceof Uint32Array || srcData instanceof Int32Array) {
264+
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'int'), usage);
265+
this.context.bufferDataInt(target, Array.from(srcData), usage);
266+
} else if (srcData instanceof Float32Array) {
267+
//this.context.bufferData(target, this.toNativeArray(srcData as any, 'float'), usage);
268+
this.context.bufferDataFloat(target, Array.from(srcData), usage);
269+
}
264270
}
265271
} else if (arguments.length === 3 && !srcData) {
266272
this.context.bufferData(target, 0, usage);
@@ -280,18 +286,24 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
280286
this.context.bufferSubDataByte(target, offset, Array.from(new Uint8Array(srcData as any)));
281287
}
282288
} else if (srcData && srcData.buffer instanceof ArrayBuffer) {
283-
if (srcData instanceof Uint8Array || srcData instanceof Uint8ClampedArray) {
284-
//this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'byte'));
285-
this.context.bufferSubDataByte(target, offset, Array.from(srcData));
286-
} else if (srcData instanceof Uint16Array || srcData instanceof Int16Array) {
287-
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'short'));
288-
this.context.bufferSubDataShort(target, offset, Array.from(srcData));
289-
} else if (srcData instanceof Uint32Array || srcData instanceof Int32Array) {
290-
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'int'));
291-
this.context.bufferSubDataInt(target, offset, Array.from(srcData));
292-
} else if (srcData instanceof Float32Array) {
293-
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'float'));
294-
this.context.bufferSubDataFloat(target, offset, Array.from(srcData));
289+
// @ts-ignore
290+
if (srcData.buffer.nativeObject) {
291+
// @ts-ignore
292+
this.context.bufferSubData(target, offset, srcData.buffer.nativeObject);
293+
} else {
294+
if (srcData instanceof Uint8Array || srcData instanceof Uint8ClampedArray) {
295+
//this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'byte'));
296+
this.context.bufferSubDataByte(target, offset, Array.from(srcData));
297+
} else if (srcData instanceof Uint16Array || srcData instanceof Int16Array) {
298+
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'short'));
299+
this.context.bufferSubDataShort(target, offset, Array.from(srcData));
300+
} else if (srcData instanceof Uint32Array || srcData instanceof Int32Array) {
301+
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'int'));
302+
this.context.bufferSubDataInt(target, offset, Array.from(srcData));
303+
} else if (srcData instanceof Float32Array) {
304+
// this.context.bufferSubData(target, offset, this.toNativeArray(srcData as any, 'float'));
305+
this.context.bufferSubDataFloat(target, offset, Array.from(srcData));
306+
}
295307
}
296308
}
297309
}
@@ -344,23 +356,30 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
344356
this.context.compileShader(value);
345357
}
346358

347-
compressedTexImage2D(target: number, level: number, internalformat: number, width: number, height: number, border: number, pixels: ArrayBufferView): void {
359+
compressedTexImage2D(target: number, level: number, internalformat: number, width: number, height: number, border: number, pixels: ArrayBuffer | ArrayBufferView): void {
348360
this._glCheckError('compressedTexImage2D');
349361
this._checkArgs('compressedTexImage2D', arguments);
350362

363+
// @ts-ignore
351364
if (pixels && pixels.buffer instanceof ArrayBuffer) {
352-
if (pixels instanceof Uint8Array) {
353-
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'byte'));
354-
this.context.compressedTexImage2DByte(target, level, internalformat, width, height, border, Array.from(pixels));
355-
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
356-
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'short'));
357-
this.context.compressedTexImage2DShort(target, level, internalformat, width, height, border, Array.from(pixels));
358-
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
359-
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'int'));
360-
this.context.compressedTexImage2DInt(target, level, internalformat, width, height, border, Array.from(pixels));
361-
} else if (pixels instanceof Float32Array) {
362-
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'float'));
363-
this.context.compressedTexImage2DFloat(target, level, internalformat, width, height, border, Array.from(pixels));
365+
// @ts-ignore
366+
if (pixels.buffer.nativeObject) {
367+
// @ts-ignore
368+
this.context.compressedTexImage2D(target, level, internalformat, width, height, border, pixels.buffer.nativeObject);
369+
} else {
370+
if (pixels instanceof Uint8Array) {
371+
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'byte'));
372+
this.context.compressedTexImage2DByte(target, level, internalformat, width, height, border, Array.from(pixels));
373+
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
374+
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'short'));
375+
this.context.compressedTexImage2DShort(target, level, internalformat, width, height, border, Array.from(pixels));
376+
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
377+
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'int'));
378+
this.context.compressedTexImage2DInt(target, level, internalformat, width, height, border, Array.from(pixels));
379+
} else if (pixels instanceof Float32Array) {
380+
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(pixels as any, 'float'));
381+
this.context.compressedTexImage2DFloat(target, level, internalformat, width, height, border, Array.from(pixels));
382+
}
364383
}
365384
} else if (pixels instanceof ArrayBuffer) {
366385
// this.context.compressedTexImage2D(target, level, internalformat, width, height, border, this.toNativeArray(new Uint8Array(pixels as any) as any, 'byte'));
@@ -978,22 +997,29 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
978997
this.context.polygonOffset(factor, units);
979998
}
980999

981-
readPixels(x: number, y: number, width: number, height: number, format: number, type: number, pixels: ArrayBufferView): void {
1000+
readPixels(x: number, y: number, width: number, height: number, format: number, type: number, pixels: ArrayBuffer | ArrayBufferView): void {
9821001
this._glCheckError('readPixels');
9831002
this._checkArgs('readPixels', arguments);
1003+
// @ts-ignore
9841004
if (pixels && pixels.buffer instanceof ArrayBuffer) {
985-
if (pixels instanceof Uint8Array) {
986-
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'byte'));
987-
this.context.readPixelsByte(x, y, width, height, format, type, Array.from(pixels));
988-
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
989-
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'short'));
990-
this.context.readPixelsShort(x, y, width, height, format, type, Array.from(pixels));
991-
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
992-
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'int'));
993-
this.context.readPixelsInt(x, y, width, height, format, type, Array.from(pixels));
994-
} else if (pixels instanceof Float32Array) {
995-
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'float'));
996-
this.context.readPixelsFloat(x, y, width, height, format, type, Array.from(pixels));
1005+
// @ts-ignore
1006+
if (pixels.buffer.nativeObject) {
1007+
// @ts-ignore
1008+
this.context.readPixels(x, y, width, height, format, type, pixels.buffer.nativeObject);
1009+
} else {
1010+
if (pixels instanceof Uint8Array) {
1011+
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'byte'));
1012+
this.context.readPixelsByte(x, y, width, height, format, type, Array.from(pixels));
1013+
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
1014+
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'short'));
1015+
this.context.readPixelsShort(x, y, width, height, format, type, Array.from(pixels));
1016+
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
1017+
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'int'));
1018+
this.context.readPixelsInt(x, y, width, height, format, type, Array.from(pixels));
1019+
} else if (pixels instanceof Float32Array) {
1020+
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(pixels as any, 'float'));
1021+
this.context.readPixelsFloat(x, y, width, height, format, type, Array.from(pixels));
1022+
}
9971023
}
9981024
} else if (pixels instanceof ArrayBuffer) {
9991025
// this.context.readPixels(x, y, width, height, format, type, this.toNativeArray(new Uint8Array(pixels as any) as any, 'byte'));
@@ -1086,18 +1112,23 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
10861112
/* TODO */
10871113
if (arguments.length === 9) {
10881114
if (pixels && pixels.buffer instanceof ArrayBuffer) {
1089-
if (pixels instanceof Uint8Array) {
1090-
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'byte'));
1091-
this.context.texImage2DByte(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1092-
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
1093-
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'short'));
1094-
this.context.texSubImage2DShort(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1095-
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
1096-
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'int'));
1097-
this.context.texImage2DInt(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1098-
} else if (pixels instanceof Float32Array) {
1099-
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'float'));
1100-
this.context.texImage2DFloat(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1115+
if (pixels.buffer.nativeObject) {
1116+
// @ts-ignore
1117+
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, pixels.buffer.nativeObject);
1118+
} else {
1119+
if (pixels instanceof Uint8Array) {
1120+
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'byte'));
1121+
this.context.texImage2DByte(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1122+
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
1123+
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'short'));
1124+
this.context.texSubImage2DShort(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1125+
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
1126+
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'int'));
1127+
this.context.texImage2DInt(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1128+
} else if (pixels instanceof Float32Array) {
1129+
// this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'float'));
1130+
this.context.texImage2DFloat(target, level, internalformat, width, height, border, format, type, Array.from(pixels));
1131+
}
11011132
}
11021133
} else if (pixels instanceof ArrayBuffer) {
11031134
// @ts-ignore // ArrayBuffer backed by nio buffer
@@ -1163,19 +1194,24 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
11631194
this._checkArgs('texSubImage2D', arguments);
11641195
if (arguments.length === 9) {
11651196
if (pixels && pixels.buffer) {
1166-
if (pixels instanceof Uint8Array) {
1167-
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'byte'));
1168-
this.context.texSubImage2DByte(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1169-
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
1170-
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'short'));
1171-
this.context.texSubImage2DShort(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1172-
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
1173-
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'int'));
1174-
this.context.texSubImage2DInt(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1175-
} else if (pixels instanceof Float32Array) {
1176-
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'float'));
1177-
this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1197+
if (pixels.buffer.nativeObject) {
1198+
this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels.buffer.nativeObject);
1199+
} else {
1200+
if (pixels instanceof Uint8Array) {
1201+
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'byte'));
1202+
this.context.texSubImage2DByte(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1203+
} else if (pixels instanceof Uint16Array || pixels instanceof Int16Array) {
1204+
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'short'));
1205+
this.context.texSubImage2DShort(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1206+
} else if (pixels instanceof Uint32Array || pixels instanceof Int32Array) {
1207+
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'int'));
1208+
this.context.texSubImage2DInt(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1209+
} else if (pixels instanceof Float32Array) {
1210+
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(pixels as any, 'float'));
1211+
this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, Array.from(pixels));
1212+
}
11781213
}
1214+
11791215
} else if (pixels instanceof ArrayBuffer) {
11801216
// this.context.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.toNativeArray(new Uint8Array(pixels as any) as any, 'byte'));
11811217
// @ts-ignore

0 commit comments

Comments
 (0)