forked from mozilla-b2g/gaia
/
scenegraph.js
444 lines (403 loc) · 13.6 KB
/
scenegraph.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
var gUseGL = 1;
var gSnapToWholePixels = !gUseGL;
function abort(why) { alert(why); throw why; }
function assert(cond, msg) { if (!cond) abort(msg); }
// Return the current time of the "animation clock", which ticks on
// each frame drawn.
function GetAnimationClockTime() {
return window.mozAnimationStartTime ||
window.webkitAnimationStartTime ||
window.animationStartTime;
}
function RequestAnimationFrame() {
if (window.mozRequestAnimationFrame)
window.mozRequestAnimationFrame();
else if (window.webkitRequestAnimationFrame)
window.webkitRequestAnimationFrame();
else if (window.requestAnimationFrame)
window.requestAnimationFrame();
}
var Physics = {
Linear: function(elapsed, start, current, target) {
return start + (target - start) * elapsed;
},
Spring: function(elapsed, start, current, target) {
return current + (target - current) * elapsed;
}
};
function Sprite(width, height) {
var canvas = document.createElement('canvas');
this.width = canvas.width = width;
this.height = canvas.height = height;
this.canvas = canvas;
this.setPosition(0, 0);
this.setScale(1);
}
Sprite.prototype = {
getContext2D: function() {
var ctx = this.canvas.getContext('2d');
// XXX it appears that canvases aren't translated into GL
// coordinates before uploading, unlike <img>s :/. So hack
// here. Need to figure out if that's a FF bug or actually
// spec'd like that.
if (gUseGL) {
ctx.translate(0, this.height);
ctx.scale(1, -1);
}
return ctx;
},
setPosition: function(targetX, targetY, duration, fn) {
RequestAnimationFrame();
if (duration && (this.x != targetX || this.y != targetY)) {
this.startX = this.x;
this.startY = this.y;
this.targetX = targetX;
this.targetY = targetY;
this.moveStart = GetAnimationClockTime();
this.moveStop = this.moveStart + duration;
this.moveFunction = fn || Physics.Linear;
return;
}
this.x = targetX;
this.y = targetY;
this.moveFuncton = null;
},
setScale: function(targetScale, duration, fn) {
RequestAnimationFrame();
if (duration && this.scale != targetScale) {
this.startScale = this.scale;
this.targetScale = targetScale;
this.scaleStart = GetAnimationClockTime();
this.scaleStop = this.scaleStart + duration;
this.scaleFunction = fn || Physics.Linear;
return;
}
this.scale = targetScale;
this.scaleFunction = null;
},
animate: function(now) {
function GetElapsed(start, stop, now) {
return (now < start || now > stop) ? 1 : ((now - start) / (stop - start));
}
if (this.moveFunction) {
var elapsed = GetElapsed(this.moveStart, this.moveStop, now);
this.x = this.moveFunction(elapsed, this.startX, this.x, this.targetX);
this.y = this.moveFunction(elapsed, this.startY, this.y, this.targetY);
if (elapsed == 1)
this.moveFunction = null;
}
if (this.scaleFunction) {
var elapsed = GetElapsed(this.scaleStart, this.scaleStop, now);
this.scale = this.scaleFunction(elapsed, this.startScale,
this.scale, this.targetScale);
if (elapsed == 1)
this.scaleFunction = null;
}
return this.moveFunction || this.scaleFunction;
}
};
function SceneGraph(canvas) {
if (gUseGL) {
try {
this.gl = canvas.getContext('experimental-webgl');
} catch (e) {
// Fall back to 2D.
gUseGL = 0;
gSnapToWholePixels = 1;
}
}
this.blitter =
gUseGL ? new SpriteBlitterGL(canvas) : new SpriteBlitter2D(canvas);
this.canvas = canvas;
this.sprites = [];
this.x = 0;
this.y = 0;
var self = this;
window.addEventListener('MozBeforePaint', function(event) {
var now = GetAnimationClockTime();
// continue painting until we are run out of animations
if (self.animate(now))
RequestAnimationFrame();
self.draw();
}, false);
}
SceneGraph.prototype = {
// add a sprite to the scene graph
add: function(sprite) {
this.blitter.spriteAdded(sprite);
var sprites = this.sprites;
sprite.index = sprites.length;
sprites.push(sprite);
},
// remove a sprite from the scene graph
remove: function(sprite) {
var sprites = this.sprites;
sprites.splice(sprite.index, 1);
for (var i = 0; i < sprites.length; i++)
sprites[i].index = i;
this.blitter.spriteRemoved(sprite);
},
// animate the scene graph, returning false if the animation is done
animate: function(now) {
function GetElapsed(start, stop, now) {
return (now < start || now > stop) ? 1 : ((now - start) / (stop - start));
}
var more = false;
if (this.scrollFunction) {
var elapsed = GetElapsed(this.scrollStart, this.scrollStop, now);
this.x = this.scrollFunction(elapsed, this.startX, this.x, this.targetX);
this.y = this.scrollFunction(elapsed, this.startY, this.y, this.targetY);
if (elapsed == 1)
this.scrollFunction = null;
else
more = true;
}
var sprites = this.sprites;
for (var n = 0; n < sprites.length; ++n) {
var sprite = sprites[n];
if (sprite.animate(now))
more = true;
}
return more;
},
draw: function() {
var x = this.x, y = this.y;
if (gSnapToWholePixels) {
x |= 0;
y |= 0;
}
this.blitter.draw(x, y, this.sprites);
},
// walk over all sprites in the scene
forAll: function(callback) {
var sprites = this.sprites;
for (var n = 0; n < sprites.length; ++n)
callback(sprites[n]);
},
forHit: function(x, y, callback) {
var sprites = this.sprites;
for (var n = sprites.length - 1; n >= 0; --n) {
var sprite = sprites[n];
if (x >= sprite.x && x < sprite.x + sprite.width &&
y >= sprite.y && y < sprite.y + sprite.height) {
callback(sprite);
return;
}
}
},
setViewportTopLeft: function(targetX, targetY, duration, fn) {
RequestAnimationFrame();
if (duration && (this.x != targetX || this.y != targetY)) {
this.startX = this.x;
this.startY = this.y;
this.targetX = targetX;
this.targetY = targetY;
this.scrollStart = GetAnimationClockTime();
this.scrollStop = this.scrollStart + duration;
this.scrollFunction = fn || Physics.Linear;
return;
}
this.x = targetX;
this.y = targetY;
this.scrollFuncton = null;
}
};
// fallback 2D canvas backend
function SpriteBlitter2D(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
}
SpriteBlitter2D.prototype = {
draw: function(x, y, sprites) {
var canvas = this.canvas;
var ctx = this.ctx;
var width = canvas.width;
var height = canvas.height;
ctx.clearRect(0, 0, width, height);
for (var n = 0; n < sprites.length; ++n) {
var sprite = sprites[n];
var canvas = sprite.canvas;
if (canvas) {
var scale = sprite.scale;
ctx.drawImage(canvas,
sprite.x - x, sprite.y - y,
canvas.width * scale, canvas.height * scale);
}
}
},
// nothing to do here
spriteAdded: function(sprite) {},
spriteRemoved: function(sprite) {}
};
// blt using WebGL. This should be as fast or faster than using a 2D
// drawing context when the browser engine is drawing with OpenGL (and
// so doesn't need to read back from the WebGL context).
var kVertexShader = [
'attribute vec4 aPosAndTexCoord;',
'uniform mat4 uProjection;',
'varying vec2 vTexCoord;',
'',
'void main(void) {',
' vTexCoord = aPosAndTexCoord.zw;',
' vec4 transformedPos = vec4(aPosAndTexCoord.x, ',
' aPosAndTexCoord.y, 0.0, 1.0);',
' gl_Position = uProjection * transformedPos;',
'}'
].join('\n');
var kFragmentShader = [
'#ifdef GL_ES',
'precision highp float;',
'#endif',
'',
'varying vec2 vTexCoord;',
'uniform sampler2D uTexture;',
'',
'void main(void) {',
' vec4 texColor;',
' texColor = texture2D(uTexture, vTexCoord);',
' gl_FragColor = texColor;',
'}'
].join('\n');
var kMaxTextureSize = 0;
// NB: sprites really need a depth value since they can overlap. As
// it stands we should assume that they're drawn in an undefined
// z-order (GL may actually specify that, not sure).
function SpriteBlitterGL(canvas) {
this.canvas = canvas;
var gl =
this.gl = canvas.getContext('experimental-webgl');
this.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
this.posAndTexCoordArray = new Float32Array(4 /*vertices*/ *
4 /*elements per vertex*/);
this.posAndTexCoordBuffer = gl.createBuffer();
var program =
this.program = compileGLProgram(gl, kVertexShader, kFragmentShader);
this.projectionMatrix = new Float32Array(
[2.0, 0.0, 0.0, 0.0,
0.0, -2.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
-1.0, 1.0, 0.0, 1.0]);
this.viewportWidth = canvas.width;
this.viewportHeight = canvas.height;
gl.useProgram(program);
program.aPosAndTexCoord = gl.getAttribLocation(program, 'aPosAndTexCoord');
gl.enableVertexAttribArray(program.aPosAndTexCoord);
program.uProjection = gl.getUniformLocation(program, 'uProjection');
program.uTexture = gl.getUniformLocation(program, 'uTexture');
gl.clearColor(0.0, 0.0, 0.0, 0.0);
}
SpriteBlitterGL.prototype = {
draw: function(x, y, sprites) {
var gl = this.gl;
var posAndTexCoordArray = this.posAndTexCoordArray;
var posAndTexCoordBuffer = this.posAndTexCoordBuffer;
var program = this.program;
var viewportWidth = this.viewportWidth;
var viewportHeight = this.viewportHeight;
gl.viewport(0, 0, viewportWidth, viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT);
// XXX we need to enable blending to deal with overlapping
// sprites, but doing so causes the appearance of fonts to worsen.
// Need to investigate that.
gl.enable(gl.BLEND);
// "over" operator
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.uniformMatrix4fv(program.uProjection, false, this.projectionMatrix);
gl.uniform1i(program.uTexture, 0);
var translateX = x;
var translateY = y;
// Draw a quad at <x, y, width, height> textured by |texture|.
function drawTexturedQuad(texture, x, y, width, height) {
x /= viewportWidth;
y /= viewportHeight;
var xmost = x + width / viewportWidth;
var ymost = y + height / viewportHeight;
var i = 0;
// bottom left
posAndTexCoordArray[i++] = x;
posAndTexCoordArray[i++] = ymost;
posAndTexCoordArray[i++] = 0;
posAndTexCoordArray[i++] = 0;
// bottom right
posAndTexCoordArray[i++] = xmost;
posAndTexCoordArray[i++] = ymost;
posAndTexCoordArray[i++] = 1;
posAndTexCoordArray[i++] = 0;
// top left
posAndTexCoordArray[i++] = x;
posAndTexCoordArray[i++] = y;
posAndTexCoordArray[i++] = 0;
posAndTexCoordArray[i++] = 1;
// top right
posAndTexCoordArray[i++] = xmost;
posAndTexCoordArray[i++] = y;
posAndTexCoordArray[i++] = 1;
posAndTexCoordArray[i++] = 1;
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.bindBuffer(gl.ARRAY_BUFFER, posAndTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, posAndTexCoordArray, gl.STREAM_DRAW);
gl.vertexAttribPointer(program.aPosition, 4, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
for (var n = 0; n < sprites.length; ++n) {
var sprite = sprites[n];
var canvas = sprite.canvas;
var scale = sprite.scale;
var left = sprite.x - translateX;
var top = sprite.y - translateY;
var width = canvas.width * scale;
var height = canvas.height * scale;
drawTexturedQuad(sprite.texture, left, top, width, height);
}
gl.bindTexture(gl.TEXTURE_2D, null);
},
spriteAdded: function(sprite) {
if (('texture' in sprite) && sprite.texture !== null)
return;
var canvas = sprite.canvas;
assert(canvas.width <= this.maxTextureSize &&
canvas.height <= this.maxTextureSize,
'Sprite canvas must be smaller than max texture dimension');
var gl = this.gl;
var texture = sprite.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,
canvas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// XXX mipmap if we start downscaling a lot
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
},
spriteRemoved: function(sprite) {
this.gl.deleteTexture(sprite.texture);
sprite.texture = null;
}
};
function compileGLProgram(gl, vxShader, pixShader) {
function compileShader(type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
assert(gl.getShaderParameter(shader, gl.COMPILE_STATUS),
'Compile error for ' + type + ': ' + gl.getShaderInfoLog(shader));
return shader;
}
var p = gl.createProgram();
var vs = compileShader(gl.VERTEX_SHADER, vxShader);
var fs = compileShader(gl.FRAGMENT_SHADER, pixShader);
gl.attachShader(p, vs);
gl.deleteShader(vs);
gl.attachShader(p, fs);
gl.deleteShader(fs);
gl.linkProgram(p);
if (!gl.getProgramParameter(p, gl.LINK_STATUS))
// FIXME fall back on 2d
abort('Failed to compile shaders.');
return p;
}