license import * as webgl_backend from '@tensorflow/tfjs-backend-webgl' ; import * as tfjs from '@tensorflow/tfjs-core' ; import { atob , btoa } from 'abab' ; import { TextDecoder , TextEncoder } from 'text-encoder' ; export interface SystemConfig { /**
     * A function used to override the `window.fetch` function.
     */ fetchFunc : Function ; /**
     * TensorFlow.js exported root object.
     */ tf : any ; /**
     * TensorFlow.js webgl backend object.
     */ webgl : any ; /**
     * The WeChat offline canvas, can be created by calling
     * wx.createOfflineCanvas()
     */ canvas : any ; /**
     * Optional name of wechat webgl backend.
     */ backendName ? : string ; } export let systemFetchFunc ; export class PlatformWeChat implements tfjs . Platform { constructor ( fetchFunc ) { systemFetchFunc = fetchFunc ; } fetch ( path , requestInits ? ) { return systemFetchFunc ( path , requestInits ) ; } now ( ) { return Date . now ( ) ; } encode ( text , encoding ) { if ( encoding !== 'utf-8' && encoding !== 'utf8' ) { throw new Error ( `Browser's encoder only supports utf-8, but got ${ encoding }` ) ; } return new TextEncoder ( encoding ) . encode ( text ) ; } decode ( bytes , encoding ) { return new TextDecoder ( encoding ) . decode ( bytes ) ; } } export const WECHAT_WEBGL_BACKEND_NAME = 'wechat-webgl' ; param config param debug export function setupWechatPlatform ( config , debug = false ) { const tf = config . tf as typeof tfjs ; const backendName = config . backendName || WECHAT_WEBGL_BACKEND_NAME ; if ( debug ) { console . log ( tf ) ; } if ( tf . getBackend ( ) === backendName ) { return ; } const webgl = config . webgl as typeof webgl_backend ; tf . ENV . setPlatform ( 'wechat' , new PlatformWeChat ( config . fetchFunc ) ) ; setBase64Methods ( tf ) ; if ( config . webgl && config . canvas ) { initWebGL ( tf , webgl , config . canvas , backendName , debug ) ; } else { console . log ( 'webgl backend is not initialized, ' + 'please inject webgl backend and the offscreen canvas.' ) ; } } /**
 * Polyfill btoa and atob method on the global scope which will be used by
 * model parser.
 */ export function setBase64Methods ( tf ) { tf . ENV . global . btoa = btoa ; tf . ENV . global . atob = atob ; } param canvas param platform param debug const BACKEND_PRIORITY = 2 ; export function initWebGL ( tf , webgl , canvas , backendName = WECHAT_WEBGL_BACKEND_NAME , debug = false ) { if ( tf . findBackend ( backendName ) == null ) { const WEBGL_ATTRIBUTES = { alpha : false , antialias : false , premultipliedAlpha : false , preserveDrawingBuffer : false , depth : false , stencil : false , failIfMajorPerformanceCaveat : true } ; const gl = canvas . getContext ( 'webgl' , WEBGL_ATTRIBUTES ) ; if ( debug ) { console . log ( 'start backend registration' ) ; } webgl . setWebGLContext ( 1 , gl ) ; tf . ENV . set ( 'WEBGL_VERSION' , 1 ) ; try { tf . registerBackend ( backendName , ( ) => { const context = new webgl . GPGPUContext ( gl ) ; return new webgl . MathBackendWebGL ( context ) ; } , BACKEND_PRIORITY ) ; const kernels = tf . getKernelsForBackend ( 'webgl' ) ; kernels . forEach ( kernelConfig => { const newKernelConfig = Object . assign ( { } , kernelConfig , { backendName } ) ; tf . registerKernel ( newKernelConfig ) ; } ) ; } catch ( e ) { throw ( new Error ( `Failed to register Webgl backend: ${ e . message }` ) ) ; } } tf . setBackend ( backendName ) ; if ( debug ) { console . log ( 'current backend = ' , tf . getBackend ( ) ) ; } }