-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpicture_renderer.js
More file actions
322 lines (297 loc) · 12 KB
/
picture_renderer.js
File metadata and controls
322 lines (297 loc) · 12 KB
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
'use strict';
/**
* A relatively thin wrapper around a canvas and context used to render multiple Pictures.
* Maintains state that isn't specific to a single Picture, such as the compositor and brush texture collection.
* @constructor
* @param {string=} mode Either 'webgl', 'no-dynamic-webgl' or 'canvas'. Defaults to 'webgl'.
* @param {Array.<HTMLImageElement|HTMLCanvasElement>=} brushTextureData Set of brush textures to use. Can be undefined
* if no textures are needed.
*/
var PictureRenderer = function(mode, brushTextureData) {
if (mode === undefined) {
mode = 'webgl';
}
this.mode = mode;
this.brushTextureData = brushTextureData;
this.refCount = 0;
this.canvas = document.createElement('canvas');
this.sharedRasterizer = null;
this.currentEventRasterizer = null;
if (this.usesWebGl()) {
if (!this.setupGLState()) {
this.mode = undefined;
}
} else if (this.mode === 'canvas') {
this.ctx = this.canvas.getContext('2d');
this.compositor = new CanvasCompositor(this.ctx);
this.brushTextures = new CanvasBrushTextures();
this.initBrushTextures();
} else {
this.mode = undefined;
}
};
/**
* Create a renderer, choosing mode automatically.
* @param {Array.<string>} modesToTry Modes to try to initialize the picture.
* Can contain either 'webgl', 'no-dynamic-webgl', 'no-float-webgl' or 'canvas'.
* Modes are tried in the order they are in the array.
* @param {Array.<HTMLImageElement|HTMLCanvasElement>=} brushTextureData Set of brush textures to use. Can be undefined
* if no textures are needed.
* @return {PictureRenderer} A renderer or null if failed.
*/
PictureRenderer.create = function(modesToTry, brushTextureData) {
var i = 0;
var renderer = null;
while (i < modesToTry.length && renderer === null) {
var mode = modesToTry[i];
if (glUtils.supportsTextureUnits(4) || mode === 'canvas') {
renderer = new PictureRenderer(mode, brushTextureData);
if (renderer.mode === undefined) {
renderer = null;
}
}
i++;
}
return renderer;
};
/**
* Add a reference to the renderer. We allocate a lot of resources indirectly and don't rely on JS GC to free them so
* we do this manual refcounting.
*/
PictureRenderer.prototype.addRef = function() {
this.refCount += 1;
};
/**
* Remove a reference to the renderer. This will free resources if refCount goes to zero.
*/
PictureRenderer.prototype.removeRef = function() {
--this.refCount;
if (this.refCount == 0) {
if (this.gl) {
this.gl.finish();
if (this.loseContext) {
this.loseContext.loseContext();
}
}
}
};
/**
* True if WebGL context was initialized but a rendering test produced wrong results.
*/
PictureRenderer.hasFailedWebGLSanity = false;
/**
* @return {boolean} Does the renderer use WebGL?
*/
PictureRenderer.prototype.usesWebGl = function() {
return (this.mode === 'webgl' || this.mode === 'no-float-webgl' ||
this.mode === 'no-dynamic-webgl');
};
/**
* Call when the picture doing rendering operations with this renderer's context might have changed.
* @param {Picture} picture Picture that's going to do rendering operations with this renderer's context.
*/
PictureRenderer.prototype.setPicture = function(picture) {
if (this.usesWebGl()) {
this.gl.viewport(0, 0, picture.bitmapWidth(), picture.bitmapHeight());
}
};
/**
* Show the picture on the canvas of this renderer.
* @param {Picture} picture Picture about to be displayed on the canvas attached to this renderer.
*/
PictureRenderer.prototype.display = function(picture) {
if (picture.currentEvent) {
this.currentEventRasterizer.resetClip();
picture.currentEvent.drawTo(this.currentEventRasterizer, picture.pictureTransform,
picture.currentEventUntilCoord);
}
this.setPicture(picture);
this.canvas.width = picture.bitmapWidth();
this.canvas.height = picture.bitmapHeight();
if (this.usesWebGl()) {
this.gl.scissor(0, 0, this.canvas.width, this.canvas.height);
this.glManager.useFbo(null);
}
var compositor = this.compositor;
compositor.setTargetDimensions(this.canvas.width, this.canvas.height);
for (var i = 0; i < picture.buffers.length; ++i) {
if (picture.buffers[i].isComposited()) {
compositor.pushBuffer(picture.buffers[i]);
if (picture.currentEventAttachment === picture.buffers[i].id) {
if (picture.currentEvent) {
compositor.pushRasterizer(this.currentEventRasterizer,
picture.currentEventColor,
picture.currentEvent.opacity,
picture.currentEventMode,
picture.currentEvent.getBoundingBox(picture.bitmapRect,
picture.pictureTransform));
} else {
// Even if there's no picture.currentEvent at the moment, push
// so that the GLCompositor can avoid extra shader changes.
compositor.pushRasterizer(this.currentEventRasterizer,
[0, 0, 0], 0,
picture.currentEventMode,
null);
}
}
}
}
compositor.flush();
};
/**
* Create a single rasterizer using the mode specified for this renderer.
* @param {number} width Width of the rasterizer to create in pixels.
* @param {number} height Height of the rasterizer to create in pixels.
* @return {BaseRasterizer} The rasterizer.
*/
PictureRenderer.prototype.createRasterizer = function(width, height) {
if (this.glRasterizerCreator !== undefined) {
return this.glRasterizerCreator(this.gl, this.glManager, width, height, this.brushTextures);
} else {
return new Rasterizer(width, height, this.brushTextures);
}
};
/**
* Set the minimum size of the shared rasterizer in pixels.
* @param {number} width Width of the rasterizer that is required.
* @param {number} height Height of the rasterizer that is required.
*/
PictureRenderer.prototype.setSharedRasterizerSize = function(width, height) {
if (this.sharedRasterizer !== null) {
if (this.sharedRasterizer.width < width || this.sharedRasterizer.height < height) {
if (this.sharedRasterizer.width > width) {
width = this.sharedRasterizer.width;
}
if (this.sharedRasterizer.height > height) {
height = this.sharedRasterizer.height;
}
this.sharedRasterizer.free();
this.sharedRasterizer = null;
}
}
if (this.currentEventRasterizer !== null) {
if (this.currentEventRasterizer.width < width || this.currentEventRasterizer.height < height) {
if (this.currentEventRasterizer.width > width) {
width = this.currentEventRasterizer.width;
}
if (this.currentEventRasterizer.height > height) {
height = this.currentEventRasterizer.height;
}
this.currentEventRasterizer.free();
this.currentEventRasterizer = null;
}
}
if (this.sharedRasterizer === null) {
this.sharedRasterizer = this.createRasterizer(width, height);
}
if (this.currentEventRasterizer === null) {
this.currentEventRasterizer = this.createRasterizer(width, height);
}
};
/**
* @param {HTMLCanvasElement} canvas Canvas to use for rasterization.
* @param {boolean=} debugGL True to log every WebGL call made on the context. Defaults to false.
* @return {WebGLRenderingContext} Context to use or null if unsuccessful.
*/
PictureRenderer.initWebGL = function(canvas, debugGL) {
if (debugGL === undefined) {
debugGL = false;
}
var contextAttribs = {
antialias: false,
stencil: false,
depth: false,
premultipliedAlpha: false
};
var gl = glUtils.initGl(canvas, contextAttribs, 4);
if (!gl) {
return null;
}
if (debugGL) {
var logGLCall = function(functionName, args) {
console.log('gl.' + functionName + '(' + WebGLDebugUtils.glFunctionArgsToString(functionName, args) + ');');
};
gl = WebGLDebugUtils.makeDebugContext(gl, undefined, logGLCall);
}
gl.getExtension('OES_texture_float');
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
gl.enable(gl.SCISSOR_TEST); // scissor rect is initially set to canvas size.
gl.hint(gl.GENERATE_MIPMAP_HINT, gl.NICEST);
return gl;
};
/**
* Set up state in an existing gl context.
* @return {boolean} Whether initialization succeeded.
*/
PictureRenderer.prototype.setupGLState = function() {
var useFloatRasterizer = (this.mode === 'webgl' || this.mode === 'no-dynamic-webgl');
if (useFloatRasterizer && !glUtils.floatFboSupported) {
return false;
}
this.gl = PictureRenderer.initWebGL(this.canvas);
if (!this.gl) {
return false;
}
this.glManager = glStateManager(this.gl);
this.glManager.useQuadVertexBuffer(); // All drawing is done using the same vertex array
this.loseContext = this.gl.getExtension('WEBGL_lose_context');
this.brushTextures = new GLBrushTextures(this.gl, this.glManager);
this.initBrushTextures();
if (useFloatRasterizer) {
if (this.mode === 'webgl') {
this.glRasterizerCreator = function(gl, glManager, width, height, brushTextures) {
return GLFloatRasterizer.create(gl, glManager, width, height, brushTextures, true);
};
} else {
// TODO: assert(this.mode === 'no-dynamic-webgl');
this.glRasterizerCreator = function(gl, glManager, width, height, brushTextures) {
return GLFloatRasterizer.create(gl, glManager, width, height, brushTextures, false);
};
}
} else {
this.glRasterizerCreator = function(gl, glManager, width, height, brushTextures) {
return GLDoubleBufferedRasterizer.create(gl, glManager, width, height, brushTextures);
};
}
this.texBlitProgram = this.glManager.shaderProgram(blitShader.blitSrc,
blitShader.blitVertSrc,
{'uSrcTex': 'tex2d'});
this.rectBlitProgram = this.glManager.shaderProgram(blitShader.blitSrc,
blitShader.blitScaledTranslatedVertSrc,
{'uSrcTex': 'tex2d', 'uScale': '2fv', 'uTranslate': '2fv'});
this.compositor = new GLCompositor(this.glManager, this.gl, glUtils.maxTextureUnits);
this.setSharedRasterizerSize(128, 128);
if (!this.sharedRasterizer.checkSanity()) {
PictureRenderer.hasFailedWebGLSanity = true;
console.log('WebGL accelerated rasterizer did not pass sanity test ' +
'(mode ' + this.mode + '). Update your graphics drivers ' +
'or try switching browsers if possible.');
this.sharedRasterizer.free();
this.sharedRasterizer = null;
return false;
}
return true;
};
/**
* Clean up any allocated resources.
* Call setSharedRasterizerSize() to make the renderer usable again after this.
*/
PictureRenderer.prototype.free = function() {
this.currentEventRasterizer.free();
this.sharedRasterizer.free();
this.currentEventRasterizer = null;
this.sharedRasterizer = null;
};
/**
* Initialize brush textures to use in rasterizers from the given brush texture data.
* @protected
*/
PictureRenderer.prototype.initBrushTextures = function() {
if (!this.brushTextureData) {
return;
}
for (var i = 0; i < this.brushTextureData.length; ++i) {
this.brushTextures.addTexture(this.brushTextureData[i]);
}
};