1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {assertExists, assertTrue} from './logging'; 16import {Engine, LoadingTracker} from './engine'; 17 18let bundlePath: string; 19let idleWasmWorker: Worker; 20let activeWasmWorker: Worker; 21 22export function initWasm(root: string) { 23 bundlePath = root + 'engine_bundle.js'; 24 idleWasmWorker = new Worker(bundlePath); 25} 26 27// This method is called trace_controller whenever a new trace is loaded. 28export function resetEngineWorker(): MessagePort { 29 const channel = new MessageChannel(); 30 const port = channel.port1; 31 32 // We keep always an idle worker around, the first one is created by the 33 // main() below, so we can hide the latency of the Wasm initialization. 34 if (activeWasmWorker !== undefined) { 35 activeWasmWorker.terminate(); 36 } 37 38 // Swap the active worker with the idle one and create a new idle worker 39 // for the next trace. 40 activeWasmWorker = assertExists(idleWasmWorker); 41 activeWasmWorker.postMessage(port, [port]); 42 idleWasmWorker = new Worker(bundlePath); 43 return channel.port2; 44} 45 46/** 47 * This implementation of Engine uses a WASM backend hosted in a separate 48 * worker thread. 49 */ 50export class WasmEngineProxy extends Engine { 51 readonly id: string; 52 private port: MessagePort; 53 54 constructor(id: string, port: MessagePort, loadingTracker?: LoadingTracker) { 55 super(loadingTracker); 56 this.id = id; 57 this.port = port; 58 this.port.onmessage = this.onMessage.bind(this); 59 } 60 61 onMessage(m: MessageEvent) { 62 assertTrue(m.data instanceof Uint8Array); 63 super.onRpcResponseBytes(m.data as Uint8Array); 64 } 65 66 rpcSendRequestBytes(data: Uint8Array): void { 67 // We deliberately don't use a transfer list because protobufjs reuses the 68 // same buffer when encoding messages (which is good, because creating a new 69 // TypedArray for each decode operation would be too expensive). 70 this.port.postMessage(data); 71 } 72} 73