Skip to content

Commit 0feae41

Browse files
committed
feat(canvas): support ByteBuffer
1 parent 1e9c67f commit 0feae41

File tree

4 files changed

+103
-27
lines changed

4 files changed

+103
-27
lines changed

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,18 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
133133
this.context.bindRenderbuffer(target, value);
134134
}
135135

136-
_lastTexture : {
137-
target: number,
138-
texture: number
139-
} = {target: 0, texture: 0}
136+
_lastTexture: {
137+
target: number;
138+
texture: number;
139+
} = { target: 0, texture: 0 };
140140
bindTexture(target: number, texture: WebGLTexture): void {
141141
this._glCheckError('bindTexture');
142142
this._checkArgs('bindTexture', arguments);
143143
const value = texture ? texture.native : 0;
144-
if(value > 0){
144+
if (value > 0) {
145145
this._lastTexture = {
146-
target, texture: value
146+
target,
147+
texture: value,
147148
};
148149
}
149150

@@ -695,19 +696,19 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
695696
return new Float32Array(this.getJSArray(value));
696697
case this.ARRAY_BUFFER_BINDING:
697698
case this.ELEMENT_ARRAY_BUFFER_BINDING:
698-
if(value){
699+
if (value) {
699700
new WebGLBuffer(WebGLRenderingContext.toPrimitive(value));
700701
}
701702
return null;
702703
case this.CURRENT_PROGRAM:
703-
if(value){
704+
if (value) {
704705
return new WebGLProgram(WebGLRenderingContext.toPrimitive(value));
705706
}
706707
return null;
707708
case this.COMPRESSED_TEXTURE_FORMATS:
708709
return new Uint32Array(this.getJSArray(value));
709710
case this.RENDERBUFFER_BINDING:
710-
if(value){
711+
if (value) {
711712
return new WebGLRenderbuffer(WebGLRenderingContext.toPrimitive(value));
712713
}
713714
return null;
@@ -1039,7 +1040,13 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
10391040
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(pixels as any, 'float'));
10401041
}
10411042
} else if (pixels instanceof ArrayBuffer) {
1042-
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(new Uint8Array(pixels as any) as any, 'byte'));
1043+
// @ts-ignore // ArrayBuffer backed by nio buffer
1044+
if (pixels.nativeObject) {
1045+
// @ts-ignore
1046+
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, pixels.nativeObject);
1047+
} else {
1048+
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, this.toNativeArray(new Uint8Array(pixels as any) as any, 'byte'));
1049+
}
10431050
} else {
10441051
this.context.texImage2D(target, level, internalformat, width, height, border, format, type, pixels as any);
10451052
}
@@ -1147,7 +1154,7 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
11471154
uniform1fv(location: WebGLUniformLocation, value: number[]): void {
11481155
this._glCheckError('uniform1fv');
11491156
this._checkArgs('uniform1fv', arguments);
1150-
value = Array.from(value);//this.toNativeArray(value, 'float');
1157+
value = Array.from(value); //this.toNativeArray(value, 'float');
11511158
const loc = location ? location.native : 0;
11521159
this.context.uniform1fv(loc, value);
11531160
}
@@ -1169,15 +1176,15 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
11691176
uniform2iv(location: WebGLUniformLocation, value: number[]): void {
11701177
this._glCheckError('uniform2iv');
11711178
this._checkArgs('uniform2iv', arguments);
1172-
value = Array.from(value);//this.toNativeArray(value, 'int');
1179+
value = Array.from(value); //this.toNativeArray(value, 'int');
11731180
const loc = location ? location.native : 0;
11741181
this.context.uniform2iv(loc, value);
11751182
}
11761183

11771184
uniform2fv(location: WebGLUniformLocation, value: number[]): void {
11781185
this._glCheckError('uniform2fv');
11791186
this._checkArgs('uniform2fv', arguments);
1180-
value = Array.from(value);//this.toNativeArray(value, 'float');
1187+
value = Array.from(value); //this.toNativeArray(value, 'float');
11811188
const loc = location ? location.native : 0;
11821189
this.context.uniform2fv(loc, value);
11831190
}
@@ -1199,15 +1206,15 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
11991206
uniform3iv(location: WebGLUniformLocation, value: number[]): void {
12001207
this._glCheckError('uniform3iv');
12011208
this._checkArgs('uniform3iv', arguments);
1202-
value = Array.from(value);//this.toNativeArray(value, 'int');
1209+
value = Array.from(value); //this.toNativeArray(value, 'int');
12031210
const loc = location ? location.native : 0;
12041211
this.context.uniform3iv(loc, value);
12051212
}
12061213

12071214
uniform3fv(location: WebGLUniformLocation, value: number[]): void {
12081215
this._glCheckError('uniform3fv');
12091216
this._checkArgs('uniform3fv', arguments);
1210-
value = Array.from(value);//this.toNativeArray(value, 'float');
1217+
value = Array.from(value); //this.toNativeArray(value, 'float');
12111218
const loc = location ? location.native : 0;
12121219
this.context.uniform3fv(loc, value);
12131220
}
@@ -1229,15 +1236,15 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
12291236
uniform4iv(location: WebGLUniformLocation, value: number[]): void {
12301237
this._glCheckError('uniform4iv');
12311238
this._checkArgs('uniform4iv', arguments);
1232-
value = Array.from(value);//this.toNativeArray(value, 'int');
1239+
value = Array.from(value); //this.toNativeArray(value, 'int');
12331240
const loc = location ? location.native : 0;
12341241
this.context.uniform4iv(loc, value);
12351242
}
12361243

12371244
uniform4fv(location: WebGLUniformLocation, value: number[]): void {
12381245
this._glCheckError('uniform4fv');
12391246
this._checkArgs('uniform4fv', arguments);
1240-
value = Array.from(value);//this.toNativeArray(value, 'float');
1247+
value = Array.from(value); //this.toNativeArray(value, 'float');
12411248
const loc = location ? location.native : 0;
12421249
this.context.uniform4fv(loc, value);
12431250
}
@@ -1252,23 +1259,23 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
12521259
uniformMatrix2fv(location: WebGLUniformLocation, transpose: boolean, value: number[]): void {
12531260
this._glCheckError('uniformMatrix2fv');
12541261
this._checkArgs('uniformMatrix2fv', arguments);
1255-
value = Array.from(value);//this.toNativeArray(value, 'float');
1262+
value = Array.from(value); //this.toNativeArray(value, 'float');
12561263
const loc = location ? location.native : 0;
12571264
this.context.uniformMatrix2fv(loc, transpose, value);
12581265
}
12591266

12601267
uniformMatrix3fv(location: WebGLUniformLocation, transpose: boolean, value: number[]): void {
12611268
this._glCheckError('uniformMatrix3fv');
12621269
this._checkArgs('uniformMatrix3fv', arguments);
1263-
value = Array.from(value);//this.toNativeArray(value, 'float');
1270+
value = Array.from(value); //this.toNativeArray(value, 'float');
12641271
const loc = location ? location.native : 0;
12651272
this.context.uniformMatrix3fv(loc, transpose, value);
12661273
}
12671274

12681275
uniformMatrix4fv(location: WebGLUniformLocation, transpose: boolean, value: number[]): void {
12691276
this._glCheckError('uniformMatrix4fv');
12701277
this._checkArgs('uniformMatrix4fv', arguments);
1271-
value = Array.from(value);//this.toNativeArray(value, 'float');
1278+
value = Array.from(value); //this.toNativeArray(value, 'float');
12721279
const loc = location ? location.native : 0;
12731280
this.context.uniformMatrix4fv(loc, transpose, value);
12741281
}
@@ -1296,7 +1303,7 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
12961303
vertexAttrib1fv(index: number, value: number[]): void {
12971304
this._glCheckError('vertexAttrib1fv');
12981305
this._checkArgs('vertexAttrib1fv', arguments);
1299-
value = Array.from(value);//this.toNativeArray(value, 'float');
1306+
value = Array.from(value); //this.toNativeArray(value, 'float');
13001307
this.context.vertexAttrib1fv(index, value);
13011308
}
13021309

@@ -1309,7 +1316,7 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
13091316
vertexAttrib2fv(index: number, value: number[]): void {
13101317
this._glCheckError('vertexAttrib2fv');
13111318
this._checkArgs('vertexAttrib2fv', arguments);
1312-
value = Array.from(value);//this.toNativeArray(value, 'float');
1319+
value = Array.from(value); //this.toNativeArray(value, 'float');
13131320
this.context.vertexAttrib2fv(index, value);
13141321
}
13151322

@@ -1322,7 +1329,7 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
13221329
vertexAttrib3fv(index: number, value: number[]): void {
13231330
this._glCheckError('vertexAttrib3fv');
13241331
this._checkArgs('vertexAttrib3fv', arguments);
1325-
value = Array.from(value);//this.toNativeArray(value, 'float');
1332+
value = Array.from(value); //this.toNativeArray(value, 'float');
13261333
this.context.vertexAttrib3fv(index, value);
13271334
}
13281335

@@ -1335,7 +1342,7 @@ export class WebGLRenderingContext extends WebGLRenderingContextBase {
13351342
vertexAttrib4fv(index: number, value: number[]): void {
13361343
this._glCheckError('vertexAttrib4fv');
13371344
this._checkArgs('vertexAttrib4fv', arguments);
1338-
value = Array.from(value);//this.toNativeArray(value, 'float');
1345+
value = Array.from(value); //this.toNativeArray(value, 'float');
13391346
this.context.vertexAttrib4fv(index, value);
13401347
}
13411348

packages/canvas/platforms/android/include.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ allprojects {
66
}
77

88
dependencies {
9-
implementation 'com.github.triniwiz:canvas:0.9.12'
9+
implementation 'com.github.triniwiz:canvas:0.9.13'
1010
}

packages/canvas/src-native/canvas-android/canvas/publish.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'maven-publish'
22
apply plugin: 'com.jfrog.bintray'
33

44
group 'com.github.triniwiz'
5-
version '0.9.12'
5+
version '0.9.13'
66

77
afterEvaluate {
88
publishing {
@@ -15,7 +15,7 @@ afterEvaluate {
1515
// You can then customize attributes of the publication as shown below.
1616
groupId = 'com.github.triniwiz'
1717
artifactId = 'canvas'
18-
version = '0.9.12'
18+
version = '0.9.13'
1919
}
2020
}
2121
}

packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas/TNSWebGLRenderingContext.kt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,75 @@ open class TNSWebGLRenderingContext : TNSCanvasRenderingContext {
23622362
}
23632363
}
23642364

2365+
fun texImage2D(
2366+
target: Int,
2367+
level: Int,
2368+
internalformat: Int,
2369+
width: Int,
2370+
height: Int,
2371+
border: Int,
2372+
format: Int,
2373+
type: Int,
2374+
pixels: ByteBuffer?
2375+
) {
2376+
val lock = CountDownLatch(1)
2377+
runOnGLThread(Runnable {
2378+
pixels?.let {
2379+
// val buf = ByteBuffer.allocateDirect(it.size).order(ByteOrder.nativeOrder())
2380+
// buf.put(pixels)
2381+
// buf.rewind()
2382+
if(it.isDirect){
2383+
nativeTexImage2DBuffer(
2384+
target,
2385+
level,
2386+
internalformat,
2387+
width,
2388+
height,
2389+
border,
2390+
format,
2391+
type,
2392+
it,
2393+
flipYWebGL
2394+
)
2395+
}else {
2396+
nativeTexImage2DByteArray(
2397+
target,
2398+
level,
2399+
internalformat,
2400+
width,
2401+
height,
2402+
border,
2403+
format,
2404+
type,
2405+
it.array(),
2406+
flipYWebGL
2407+
)
2408+
}
2409+
2410+
} ?: run {
2411+
GLES20.glTexImage2D(
2412+
target,
2413+
level,
2414+
internalformat,
2415+
width,
2416+
height,
2417+
border,
2418+
format,
2419+
type,
2420+
null
2421+
)
2422+
}
2423+
lock.countDown()
2424+
})
2425+
try {
2426+
lock.await()
2427+
} catch (ignored: InterruptedException) {
2428+
}
2429+
}
2430+
2431+
2432+
2433+
23652434
fun texImage2D(
23662435
target: Int,
23672436
level: Int,

0 commit comments

Comments
 (0)