Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Save
  • Loading branch information
nkreeger committed Oct 17, 2019
commit a2585b60118ce35a9f36a3b3d358b277efc57b4e
7 changes: 3 additions & 4 deletions tfjs-core/src/backends/webgl/canvas_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ export function createCanvas(webGLVersion: number) {
}
}

export function cleanupDOMCanvasWebGLRenderingContext(
context: WebGLRenderingContext) {
export function browserContextCleanup(context: WebGLRenderingContext) {
if (context == null) {
throw new Error('Shold not hit this case');
throw new Error('Unexpected exception: context to be cleaned up is null!');
}
const canvas = context.canvas;
if (canvas != null && canvas.remove != null) {
canvas.remove();
}
}

export function createDOMCanvasWebGLRenderingContext(webGLVersion: number):
export function browserContextFactory(webGLVersion: number):
WebGLRenderingContext {
if (webGLVersion !== 1 && webGLVersion !== 2) {
throw new Error('Cannot get WebGL rendering context, WebGL is disabled.');
Expand Down
8 changes: 4 additions & 4 deletions tfjs-core/src/backends/webgl/canvas_util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import * as tf from '../../index';
import {BROWSER_ENVS, describeWithFlags} from '../../jasmine_util';

import {createDOMCanvasWebGLRenderingContext} from './canvas_util';
import {browserContextFactory} from './canvas_util';

describeWithFlags('canvas_util', BROWSER_ENVS, () => {
it('Returns a valid canvas', () => {
const canvas = createDOMCanvasWebGLRenderingContext(
const canvas = browserContextFactory(
tf.env().getNumber('WEBGL_VERSION'))
.canvas as (HTMLCanvasElement | OffscreenCanvas);
expect(
Expand All @@ -31,15 +31,15 @@ describeWithFlags('canvas_util', BROWSER_ENVS, () => {
});

it('Returns a valid gl context', () => {
const gl = createDOMCanvasWebGLRenderingContext(
const gl = browserContextFactory(
tf.env().getNumber('WEBGL_VERSION'));
expect(gl.isContextLost()).toBe(false);
});
});

describeWithFlags('canvas_util webgl2', {flags: {WEBGL_VERSION: 2}}, () => {
it('is ok when the user requests webgl 1 canvas', () => {
const canvas = createDOMCanvasWebGLRenderingContext(1).canvas;
const canvas = browserContextFactory(1).canvas;
expect((canvas instanceof HTMLCanvasElement)).toBe(true);
});
});
2 changes: 0 additions & 2 deletions tfjs-core/src/backends/webgl/flags_webgl_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ describe('WEBGL_MAX_TEXTURES_IN_SHADER', () => {
beforeEach(() => {
tf.env().reset();
webgl_util.resetMaxTexturesInShader();
// Forge the max textures flag between different tests.
delete ENV.getFlags()['WEBGL_MAX_TEXTURES_IN_SHADER'];

spyOn(webgl_context_manager, 'getContextByVersion').and.callFake(() => {
return {
Expand Down
6 changes: 3 additions & 3 deletions tfjs-core/src/backends/webgl/webgl_context_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import {ENV} from '../../environment';

import {cleanupDOMCanvasWebGLRenderingContext, createDOMCanvasWebGLRenderingContext} from './canvas_util';
import {browserContextCleanup, browserContextFactory} from './canvas_util';
import {checkWebGLError} from './webgl_check';

let count = 0;
Expand Down Expand Up @@ -60,7 +60,7 @@ export function getContextByVersion(version: number): WebGLRenderingContext {
if (contextFactory == null) {
if (ENV.getBool('IS_BROWSER')) {
// TODO(kreeger): Consider moving to global space.
contextFactory = createDOMCanvasWebGLRenderingContext;
contextFactory = browserContextFactory;
} else {
throw new Error('Default WebGLRenderingContext factory was not set!');
}
Expand All @@ -86,7 +86,7 @@ function disposeWebGLContext(version: number) {
if (contextCleanup == null) {
if (ENV.getBool('IS_BROWSER')) {
// TODO(kreeger): Consider moving to global space.
contextCleanup = cleanupDOMCanvasWebGLRenderingContext;
contextCleanup = browserContextCleanup;
}
}
if (contextCleanup != null) {
Expand Down