Skip to content

Commit c211b49

Browse files
pyu10055dsmilkov
authored andcommitted
Support WeChat mini app environment (tensorflow#1510)
To compensate the differences between browser and WeChat mini app: - WeChat mini app runs on JS core (ios) which does not have document, window, and setImmediate function or objects. - When creating a GPGUContext with a existing context, it needs to store the context for the GL version, otherwise it would be picked later. This PR also fix the inconsistency issue with GPGPUContext constructor, it should always cache the rendering context.
1 parent ef29cc1 commit c211b49

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

src/browser_util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
* =============================================================================
1616
*/
1717

18-
const delayCallback: Function = typeof requestAnimationFrame !== 'undefined' ?
19-
requestAnimationFrame : // Browsers
20-
setImmediate; // Node.js
18+
const delayCallback: Function = (() => {
19+
if (typeof requestAnimationFrame !== 'undefined') {
20+
return requestAnimationFrame;
21+
} else if (typeof setImmediate !== 'undefined') {
22+
return setImmediate;
23+
}
24+
return (f: Function) => f(); // no delays
25+
})();
2126

2227
/**
2328
* Returns a promise that resolve when a requestAnimationFrame has completed.

src/canvas_util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ const WEBGL_ATTRIBUTES: WebGLContextAttributes = {
2727
failIfMajorPerformanceCaveat: true
2828
};
2929

30+
export function setWebGLContext(
31+
webGLVersion: number, gl: WebGLRenderingContext) {
32+
contexts[webGLVersion] = gl;
33+
}
34+
3035
export function getWebGLContext(webGLVersion: number): WebGLRenderingContext {
3136
if (!(webGLVersion in contexts)) {
3237
contexts[webGLVersion] = getWebGLRenderingContext(webGLVersion);

src/environment.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,29 @@ export class Environment {
457457
new Engine(backend, false /* safeMode */, () => this.get('DEBUG'));
458458
}
459459
}
460+
461+
get global(): {ENV: Environment} {
462+
return getGlobalNamespace();
463+
}
460464
}
461465

466+
let _global: {ENV: Environment};
462467
function getGlobalNamespace(): {ENV: Environment} {
463-
// tslint:disable-next-line:no-any
464-
let ns: any;
465-
if (typeof (window) !== 'undefined') {
466-
ns = window;
467-
} else if (typeof (process) !== 'undefined') {
468-
ns = process;
469-
} else {
470-
throw new Error('Could not find a global object');
468+
if (_global == null) {
469+
// tslint:disable-next-line:no-any
470+
let ns: any;
471+
if (typeof (window) !== 'undefined') {
472+
ns = window;
473+
} else if (typeof (global) !== 'undefined') {
474+
ns = global;
475+
} else if (typeof (process) !== 'undefined') {
476+
ns = process;
477+
} else {
478+
throw new Error('Could not find a global object');
479+
}
480+
_global = ns;
471481
}
472-
return ns;
482+
return _global;
473483
}
474484

475485
function getOrMakeEnvironment(): Environment {

src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import './kernels/backend_cpu';
2323

2424
import {nextFrame} from './browser_util';
2525
import * as environment from './environment';
26-
import {Environment, enableProdMode} from './environment';
27-
26+
import {enableProdMode, Environment} from './environment';
2827
// Serialization.
2928
import * as io from './io/io';
3029
import * as math from './math';
@@ -36,7 +35,6 @@ import {version} from './version';
3635
import * as webgl from './webgl';
3736

3837
export {InferenceModel, ModelPredictConfig} from './model_types';
39-
4038
// Optimizers.
4139
export {AdadeltaOptimizer} from './optimizers/adadelta_optimizer';
4240
export {AdagradOptimizer} from './optimizers/adagrad_optimizer';
@@ -70,7 +68,16 @@ export {version as version_core};
7068
export {nextFrame};
7169

7270
// Second level exports.
73-
export {environment, io, math, serialization, test_util, util, webgl, enableProdMode};
71+
export {
72+
environment,
73+
io,
74+
math,
75+
serialization,
76+
test_util,
77+
util,
78+
webgl,
79+
enableProdMode
80+
};
7481

7582
// Backend specific.
7683
export {KernelBackend, BackendTimingInfo, DataMover, DataStorage} from './kernels/backend';

src/io/browser_http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
import {assert} from '../util';
25+
2526
import {concatenateArrayBuffers, getModelArtifactsInfoForJSON} from './io_utils';
2627
import {IORouter, IORouterRegistry} from './router_registry';
2728
import {IOHandler, ModelArtifacts, SaveResult, WeightsManifestConfig, WeightsManifestEntry} from './types';
@@ -375,7 +376,6 @@ IORouterRegistry.registerLoadRouter(httpRequestRouter);
375376
* import tensorflowjs as tfjs
376377
* import werkzeug.formparser
377378
*
378-
*
379379
* class ModelReceiver(object):
380380
*
381381
* def __init__(self):

src/kernels/webgl/gpgpu_context.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* =============================================================================
1616
*/
1717

18-
import {getWebGLContext} from '../../canvas_util';
18+
import {getWebGLContext, setWebGLContext} from '../../canvas_util';
1919
import {ENV} from '../../environment';
2020
import * as util from '../../util';
2121
import * as gpgpu_util from './gpgpu_util';
@@ -48,10 +48,12 @@ export class GPGPUContext {
4848
private textureConfig: TextureConfig;
4949

5050
constructor(gl?: WebGLRenderingContext) {
51+
const glVersion = ENV.get('WEBGL_VERSION');
5152
if (gl != null) {
5253
this.gl = gl;
54+
setWebGLContext(glVersion, gl);
5355
} else {
54-
this.gl = getWebGLContext(ENV.get('WEBGL_VERSION'));
56+
this.gl = getWebGLContext(glVersion);
5557
}
5658
// WebGL 2.0 enables texture floats without an extension.
5759
if (ENV.get('WEBGL_VERSION') === 1) {

0 commit comments

Comments
 (0)