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 '../base/logging'; 16 17import {Engine, LoadingTracker} from './engine'; 18import {EngineWorkerInitMessage} from './worker_messages'; 19 20let bundlePath: string; 21let idleWasmWorker: Worker; 22let activeWasmWorker: Worker; 23 24export function initWasm(root: string) { 25 bundlePath = root + 'engine_bundle.js'; 26 idleWasmWorker = new Worker(bundlePath); 27} 28 29// This method is called trace_controller whenever a new trace is loaded. 30export function resetEngineWorker(): MessagePort { 31 const channel = new MessageChannel(); 32 const port = channel.port1; 33 34 // We keep always an idle worker around, the first one is created by the 35 // main() below, so we can hide the latency of the Wasm initialization. 36 if (activeWasmWorker !== undefined) { 37 activeWasmWorker.terminate(); 38 } 39 40 // Swap the active worker with the idle one and create a new idle worker 41 // for the next trace. 42 activeWasmWorker = assertExists(idleWasmWorker); 43 const msg: EngineWorkerInitMessage = {enginePort: port}; 44 activeWasmWorker.postMessage(msg, [port]); 45 idleWasmWorker = new Worker(bundlePath); 46 return channel.port2; 47} 48 49/** 50 * This implementation of Engine uses a WASM backend hosted in a separate 51 * worker thread. 52 */ 53export class WasmEngineProxy extends Engine { 54 readonly id: string; 55 private port: MessagePort; 56 57 constructor(id: string, port: MessagePort, loadingTracker?: LoadingTracker) { 58 super(loadingTracker); 59 this.id = id; 60 this.port = port; 61 this.port.onmessage = this.onMessage.bind(this); 62 } 63 64 onMessage(m: MessageEvent) { 65 assertTrue(m.data instanceof Uint8Array); 66 super.onRpcResponseBytes(m.data as Uint8Array); 67 } 68 69 rpcSendRequestBytes(data: Uint8Array): void { 70 // We deliberately don't use a transfer list because protobufjs reuses the 71 // same buffer when encoding messages (which is good, because creating a new 72 // TypedArray for each decode operation would be too expensive). 73 this.port.postMessage(data); 74 } 75} 76