forked from kripken/webgl-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyWorker.js
402 lines (359 loc) · 11.1 KB
/
proxyWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
if (typeof console === 'undefined') {
// we can't call Module.printErr because that might be circular
var console = {
log: function(x) {
if (typeof dump === 'function') dump('log: ' + x + '\n');
},
debug: function(x) {
if (typeof dump === 'function') dump('debug: ' + x + '\n');
},
info: function(x) {
if (typeof dump === 'function') dump('info: ' + x + '\n');
},
warn: function(x) {
if (typeof dump === 'function') dump('warn: ' + x + '\n');
},
error: function(x) {
if (typeof dump === 'function') dump('error: ' + x + '\n');
},
};
}
/*
function proxify(object, nick) {
return new Proxy(object, {
get: function(target, name) {
var ret = target[name];
if (ret === undefined) console.log('PROXY ' + [nick, target, name, ret, typeof ret]);
return ret;
}
});
}
*/
function FPSTracker(text) {
var last = 0;
var mean = 0;
var counter = 0;
this.tick = function() {
var now = Date.now();
if (last > 0) {
var diff = now - last;
mean = 0.99*mean + 0.01*diff;
if (counter++ === 60) {
counter = 0;
dump(text + ' fps: ' + (1000/mean).toFixed(2) + '\n');
}
}
last = now;
}
}
function Element() { throw 'TODO: Element' }
function HTMLCanvasElement() { throw 'TODO: HTMLCanvasElement' }
function HTMLVideoElement() { throw 'TODO: HTMLVideoElement' }
function PropertyBag() {
this.addProperty = function(){};
this.removeProperty = function(){};
};
var IndexedObjects = {
nextId: 1,
cache: {},
add: function(object) {
object.id = this.nextId++;
this.cache[object.id] = object;
}
};
function EventListener() {
this.listeners = {};
this.addEventListener = function addEventListener(event, func) {
if (!this.listeners[event]) this.listeners[event] = [];
this.listeners[event].push(func);
};
this.removeEventListener = function(event, func) {
var list = this.listeners[event];
if (!list) return;
var me = list.indexOf(func);
if (me < 0) return;
list.splice(me, 1);
};
this.fireEvent = function fireEvent(event) {
event.preventDefault = function(){};
if (event.type in this.listeners) {
this.listeners[event.type].forEach(function(listener) {
listener(event);
});
}
};
}
function Image() {
IndexedObjects.add(this);
EventListener.call(this);
var src = '';
Object.defineProperty(this, 'src', {
set: function(value) {
src = value;
assert(this.id);
postMessage({ target: 'Image', method: 'src', src: src, id: this.id });
},
get: function() {
return src;
}
});
}
Image.prototype.onload = function(){};
Image.prototype.onerror = function(){};
var HTMLImageElement = Image;
var window = this;
var windowExtra = new EventListener();
for (var x in windowExtra) window[x] = windowExtra[x];
window.close = function window_close() {
postMessage({ target: 'window', method: 'close' });
};
window.alert = function(text) {
Module.printErr('alert forever: ' + text);
while (1){};
};
window.scrollX = window.scrollY = 0; // TODO: proxy these
window.WebGLRenderingContext = WebGLWorker;
window.requestAnimationFrame = (function() {
// similar to Browser.requestAnimationFrame
var nextRAF = 0;
return function(func) {
// try to keep 60fps between calls to here
var now = Date.now();
if (nextRAF === 0) {
nextRAF = now + 1000/60;
} else {
while (now + 2 >= nextRAF) { // fudge a little, to avoid timer jitter causing us to do lots of delay:0
nextRAF += 1000/60;
}
}
var delay = Math.max(nextRAF - now, 0);
setTimeout(func, delay);
};
})();
var webGLWorker = new WebGLWorker();
var document = new EventListener();
document.createElement = function document_createElement(what) {
switch(what) {
case 'canvas': {
var canvas = new EventListener();
canvas.ensureData = function canvas_ensureData() {
if (!canvas.data || canvas.data.width !== canvas.width || canvas.data.height !== canvas.height) {
canvas.data = {
width: canvas.width,
height: canvas.height,
data: new Uint8Array(canvas.width*canvas.height*4)
};
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'resize', width: canvas.width, height: canvas.height });
}
}
};
canvas.getContext = function canvas_getContext(type, attributes) {
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'getContext', type: type, attributes: attributes });
}
if (type === '2d') {
return {
getImageData: function(x, y, w, h) {
assert(x == 0 && y == 0 && w == canvas.width && h == canvas.height);
canvas.ensureData();
return {
width: canvas.data.width,
height: canvas.data.height,
data: new Uint8Array(canvas.data.data) // TODO: can we avoid this copy?
};
},
putImageData: function(image, x, y) {
canvas.ensureData();
assert(x == 0 && y == 0 && image.width == canvas.width && image.height == canvas.height);
canvas.data.data.set(image.data); // TODO: can we avoid this copy?
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'render', image: canvas.data });
}
},
drawImage: function(image, x, y, w, h, ox, oy, ow, oh) {
assert (!x && !y && !ox && !oy);
assert(w === ow && h === oh);
assert(canvas.width === w || w === undefined);
assert(canvas.height === h || h === undefined);
assert(image.width === canvas.width && image.height === canvas.height);
canvas.ensureData();
canvas.data.data.set(image.data.data); // TODO: can we avoid this copy?
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'render', image: canvas.data });
}
}
};
} else {
return webGLWorker;
}
};
canvas.boundingClientRect = {};
canvas.getBoundingClientRect = function canvas_getBoundingClientRect() {
return {
width: canvas.boundingClientRect.width,
height: canvas.boundingClientRect.height,
top: canvas.boundingClientRect.top,
left: canvas.boundingClientRect.left,
bottom: canvas.boundingClientRect.bottom,
right: canvas.boundingClientRect.right
};
};
canvas.style = new PropertyBag();
canvas.exitPointerLock = function(){};
canvas.width_ = canvas.width_ || 0;
canvas.height_ = canvas.height_ || 0;
Object.defineProperty(canvas, 'width', {
set: function(value) {
canvas.width_ = value;
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'resize', width: canvas.width_, height: canvas.height_ });
}
},
get: function() {
return canvas.width_;
}
});
Object.defineProperty(canvas, 'height', {
set: function(value) {
canvas.height_ = value;
if (canvas === Module['canvas']) {
postMessage({ target: 'canvas', op: 'resize', width: canvas.width_, height: canvas.height_ });
}
},
get: function() {
return canvas.height_;
}
});
return canvas;
}
default: throw 'document.createElement ' + what;
}
};
document.getElementById = function(id) {
if (id === 'canvas' || id === 'application-canvas') {
return Module.canvas;
}
throw 'document.getElementById failed on ' + id;
};
document.documentElement = {};
document.styleSheets = [{
cssRules: [], // TODO: forward to client
insertRule: function(rule, i) {
this.cssRules.splice(i, 0, rule);
}
}];
function Audio() {
Runtime.warnOnce('faking Audio elements, no actual sound will play');
}
Audio.prototype = new EventListener();
Object.defineProperty(Audio.prototype, 'src', {
set: function(value) {
if (value[0] === 'd') return; // ignore data urls
this.onerror();
},
});
Audio.prototype.play = function(){};
Audio.prototype.pause = function(){};
Audio.prototype.cloneNode = function() {
return new Audio;
}
Module.canvas = document.createElement('canvas');
Module.setStatus = function(){};
Module.print = function Module_print(x) {
//dump('OUT: ' + x + '\n');
postMessage({ target: 'stdout', content: x });
};
Module.printErr = function Module_printErr(x) {
//dump('ERR: ' + x + '\n');
postMessage({ target: 'stderr', content: x });
};
// Frame throttling
var frameId = 0;
var clientFrameId = 0;
var postMainLoop = Module['postMainLoop'];
Module['postMainLoop'] = function() {
if (postMainLoop) postMainLoop();
// frame complete, send a frame id
postMessage({ target: 'tick', id: frameId++ });
commandBuffer = [];
};
// Wait to start running until we receive some info from the client
addRunDependency('gl-prefetch');
addRunDependency('worker-init');
// buffer messages until the program starts to run
var messageBuffer = null;
function messageResender() {
if (calledMain) {
assert(messageBuffer && messageBuffer.length > 0);
messageBuffer.forEach(function(message) {
onmessage(message);
});
messageBuffer = null;
} else {
setTimeout(messageResender, 100);
}
}
onmessage = function onmessage(message) {
if (!calledMain && !message.data.preMain) {
if (!messageBuffer) {
messageBuffer = [];
setTimeout(messageResender, 100);
}
messageBuffer.push(message);
return;
}
//dump('worker got ' + JSON.stringify(message.data) + '\n');
switch (message.data.target) {
case 'document': {
document.fireEvent(message.data.event);
break;
}
case 'window': {
window.fireEvent(message.data.event);
break;
}
case 'canvas': {
if (message.data.event) {
Module.canvas.fireEvent(message.data.event);
} else if (message.data.boundingClientRect) {
Module.canvas.boundingClientRect = message.data.boundingClientRect;
} else throw 'ey?';
break;
}
case 'gl': {
webGLWorker.onmessage(message.data);
break;
}
case 'tock': {
clientFrameId = message.data.id;
break;
}
case 'Image': {
var img = IndexedObjects.cache[message.data.id];
switch (message.data.method) {
case 'onload': {
img.width = message.data.width;
img.height = message.data.height;
img.data = { width: img.width, height: img.height, data: message.data.data };
img.complete = true;
img.onload();
break;
}
case 'onerror': {
img.onerror({ srcElement: img });
break;
}
}
break;
}
case 'worker-init': {
Module.canvas = document.createElement('canvas');
Module.canvas.width_ = message.data.width;
Module.canvas.height_ = message.data.height;
removeRunDependency('worker-init');
break;
}
default: throw 'wha? ' + message.data.target;
}
};