Skip to content

Commit 66b5673

Browse files
disable offscreen canvas for safari, since gl fencing is not working yet for its technology preview (#7436)
Co-authored-by: Matthew Soulanille <[email protected]>
1 parent 9ea4a8e commit 66b5673

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

tfjs-backend-webgl/src/canvas_util.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export function getWebGLContext(
7070
}
7171

7272
function createCanvas(webGLVersion: number) {
73-
if (typeof OffscreenCanvas !== 'undefined' && webGLVersion === 2) {
73+
// Use canvas element for Safari, since its offscreen canvas does not support
74+
// fencing.
75+
if (!env().getBool('IS_SAFARI') && typeof OffscreenCanvas !== 'undefined' &&
76+
webGLVersion === 2) {
7477
return new OffscreenCanvas(300, 150);
7578
} else if (typeof document !== 'undefined') {
7679
return document.createElement('canvas');
@@ -98,9 +101,10 @@ function getWebGLRenderingContext(
98101
}
99102

100103
if (webGLVersion === 1) {
101-
return (canvas.getContext('webgl', WEBGL_ATTRIBUTES) ||
102-
(canvas as HTMLCanvasElement)
103-
.getContext('experimental-webgl', WEBGL_ATTRIBUTES));
104+
return (
105+
canvas.getContext('webgl', WEBGL_ATTRIBUTES) ||
106+
(canvas as HTMLCanvasElement)
107+
.getContext('experimental-webgl', WEBGL_ATTRIBUTES));
104108
}
105109
return canvas.getContext('webgl2', WEBGL_ATTRIBUTES) as WebGLRenderingContext;
106110
}

tfjs-backend-webgl/src/gpgpu_context.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface FenceContext {
2929
isFencePassed(): boolean;
3030
}
3131

32-
type WebGLVao = WebGLVertexArrayObject | WebGLVertexArrayObjectOES;
32+
type WebGLVao = WebGLVertexArrayObject|WebGLVertexArrayObjectOES;
3333

3434
export interface GPGPUContextProgram extends WebGLProgram {
3535
vao: WebGLVao;
@@ -55,8 +55,8 @@ export class GPGPUContext {
5555
textureConfig: TextureConfig;
5656

5757
createVertexArray: () => WebGLVao | null;
58-
bindVertexArray: (vao: WebGLVao | null) => void;
59-
deleteVertexArray: (vao: WebGLVao | null) => void;
58+
bindVertexArray: (vao: WebGLVao|null) => void;
59+
deleteVertexArray: (vao: WebGLVao|null) => void;
6060
getVertexArray: () => WebGLVao | null;
6161

6262
constructor(gl?: WebGLRenderingContext) {
@@ -72,20 +72,19 @@ export class GPGPUContext {
7272
if (env().getNumber('WEBGL_VERSION') === 2) {
7373
const gl2 = gl as WebGL2RenderingContext;
7474
this.createVertexArray = () => {
75-
return webgl_util.callAndCheck(gl2,
76-
() => gl2.createVertexArray());
75+
return webgl_util.callAndCheck(gl2, () => gl2.createVertexArray());
7776
};
7877
this.bindVertexArray = (vao: WebGLVao|null) => {
79-
return webgl_util.callAndCheck(gl2,
80-
() => gl2.bindVertexArray(vao as WebGLVertexArrayObject));
78+
return webgl_util.callAndCheck(
79+
gl2, () => gl2.bindVertexArray(vao as WebGLVertexArrayObject));
8180
};
8281
this.deleteVertexArray = (vao: WebGLVao|null) => {
83-
return webgl_util.callAndCheck(gl2,
84-
() => gl2.deleteVertexArray(vao as WebGLVertexArrayObject));
82+
return webgl_util.callAndCheck(
83+
gl2, () => gl2.deleteVertexArray(vao as WebGLVertexArrayObject));
8584
};
8685
this.getVertexArray = () => {
87-
return webgl_util.callAndCheck(gl2,
88-
() => gl2.getParameter(gl2.VERTEX_ARRAY_BINDING));
86+
return webgl_util.callAndCheck(
87+
gl2, () => gl2.getParameter(gl2.VERTEX_ARRAY_BINDING));
8988
};
9089
} else if (gl != null) {
9190
const ext = gl.getExtension('OES_vertex_array_object');
@@ -95,20 +94,20 @@ export class GPGPUContext {
9594
' OES_vertex_array_object.');
9695
}
9796
this.createVertexArray = () => {
98-
return webgl_util.callAndCheck(gl,
99-
() => ext.createVertexArrayOES());
97+
return webgl_util.callAndCheck(gl, () => ext.createVertexArrayOES());
10098
};
10199
this.bindVertexArray = (vao: WebGLVao|null) => {
102-
return webgl_util.callAndCheck(gl,
103-
() => ext.bindVertexArrayOES(vao as WebGLVertexArrayObjectOES));
100+
return webgl_util.callAndCheck(
101+
gl, () => ext.bindVertexArrayOES(vao as WebGLVertexArrayObjectOES));
104102
};
105103
this.deleteVertexArray = (vao: WebGLVao|null) => {
106-
return webgl_util.callAndCheck(gl,
107-
() => ext.deleteVertexArrayOES(vao as WebGLVertexArrayObjectOES));
104+
return webgl_util.callAndCheck(
105+
gl,
106+
() => ext.deleteVertexArrayOES(vao as WebGLVertexArrayObjectOES));
108107
};
109108
this.getVertexArray = () => {
110-
return webgl_util.callAndCheck(gl,
111-
() => gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES));
109+
return webgl_util.callAndCheck(
110+
gl, () => gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES));
112111
};
113112
}
114113

@@ -352,9 +351,9 @@ export class GPGPUContext {
352351
webgl_util.callAndCheck(
353352
gl, () => gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer));
354353
console.assert(
355-
gpgpu_util.bindVertexProgramAttributeStreams(gl, program2,
356-
this.vertexBuffer),
357-
'gpgpu_util.bindVertexProgramAttributeStreams not fully successful.');
354+
gpgpu_util.bindVertexProgramAttributeStreams(
355+
gl, program2, this.vertexBuffer),
356+
'gpgpu_util.bindVertexProgramAttributeStreams not fully successful.');
358357

359358
if (this.debug) {
360359
webgl_util.validateProgram(gl, program2);
@@ -464,8 +463,9 @@ export class GPGPUContext {
464463
const gl = this.gl;
465464
if (this.debug) {
466465
const boundVao = this.getVertexArray();
467-
console.assert(boundVao === this.program.vao,
468-
'VAO changed between setProgram and executeProgram!');
466+
console.assert(
467+
boundVao === this.program.vao,
468+
'VAO changed between setProgram and executeProgram!');
469469

470470
this.debugValidate();
471471
}

tfjs-core/src/flags.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ ENV.registerFlag(
5252
navigator.userAgent != null && /Chrome/.test(navigator.userAgent) &&
5353
/Google Inc/.test(navigator.vendor));
5454

55+
/** Whether this browser is Safari. */
56+
ENV.registerFlag(
57+
'IS_SAFARI',
58+
() => typeof navigator !== 'undefined' && navigator != null &&
59+
navigator.userAgent != null && /Safari/.test(navigator.userAgent) &&
60+
/Apple/.test(navigator.vendor));
5561
/**
5662
* True when the environment is "production" where we disable safety checks
5763
* to gain performance.

0 commit comments

Comments
 (0)