blob: 6dca353dabbe13e4648f8f284f7d23ce3ff97242 [file] [log] [blame]
Kevin Lubick5b90b842018-10-17 07:57:18 -04001// Adds compile-time JS functions to augment the CanvasKit interface.
2// Specifically, anything that should only be on the GPU version of canvaskit.
3(function(CanvasKit){
4 CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
5 CanvasKit._extraInitializations.push(function() {
Kevin Lubick5f1692c2019-01-03 16:20:04 -05006 function get(obj, attr, defaultValue) {
7 if (obj && obj.hasOwnProperty(attr)) {
8 return obj[attr];
Kevin Lubick5b90b842018-10-17 07:57:18 -04009 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050010 return defaultValue;
11 }
12
13 function makeWebGLContext(canvas, attrs) {
14 // These defaults come from the emscripten _emscripten_webgl_create_context
15 var contextAttributes = {
16 'alpha': get(attrs, 'alpha', 1),
17 'depth': get(attrs, 'depth', 1),
18 'stencil': get(attrs, 'stencil', 0),
19 'antialias': get(attrs, 'antialias', 1),
20 'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1),
21 'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0),
22 'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0),
23 'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0),
24 'majorVersion': get(attrs, 'majorVersion', 1),
25 'minorVersion': get(attrs, 'minorVersion', 0),
26 'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1),
27 'explicitSwapControl': get(attrs, 'explicitSwapControl', 0),
28 'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0),
29 };
30 if (!canvas) {
31 SkDebug('null canvas passed into makeWebGLContext');
32 return 0;
33 }
34 // This check is from the emscripten version
35 if (contextAttributes['explicitSwapControl']) {
36 SkDebug('explicitSwapControl is not supported');
37 return 0;
38 }
39 return GL.createContext(canvas, contextAttributes);
40 }
41
42 // arg can be of types:
43 // - String - in which case it is interpreted as an id of a
44 // canvas element.
45 // - HTMLCanvasElement - in which the provided canvas element will
46 // be used directly.
47 // - int - in which case it will be used as a WebGLContext. Only 1.0
48 // contexts are known to work for now.
49 // Width and height can be provided to override those on the canvas
50 // element, or specify a height for when a context is provided.
51 CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
52 var ctx = arg;
53 // ctx is only > 0 if it's an int, and thus a valid context
54 if (!(ctx > 0)) {
55 var canvas = arg;
56 if (canvas.tagName !== 'CANVAS') {
57 canvas = document.getElementById(arg);
58 if (!canvas) {
59 throw 'Canvas with id ' + arg + ' was not found';
60 }
61 }
62 // we are ok with all the defaults
63 ctx = makeWebGLContext(canvas);
64 }
65
66 if (!ctx || ctx < 0) {
67 throw 'failed to create webgl context: err ' + ctx;
68 }
69
70 if (!canvas && (!width || !height)) {
71 throw 'height and width must be provided with context';
72 }
73
Kevin Lubick5b90b842018-10-17 07:57:18 -040074 // Maybe better to use clientWidth/height. See:
75 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
Kevin Lubick5f1692c2019-01-03 16:20:04 -050076 var surface = this._getWebGLSurface(ctx, width || canvas.width,
77 height || canvas.height);
Kevin Lubickb07204a2018-11-20 14:07:42 -050078 if (!surface) {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050079 SkDebug('falling back from GPU implementation to a SW based one');
Kevin Lubick5f1692c2019-01-03 16:20:04 -050080 return CanvasKit.MakeSWCanvasSurface(arg);
Kevin Lubickb07204a2018-11-20 14:07:42 -050081 }
82 return surface;
Kevin Lubick5b90b842018-10-17 07:57:18 -040083 };
Kevin Lubickb07204a2018-11-20 14:07:42 -050084 // Default to trying WebGL first.
85 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
Kevin Lubick5b90b842018-10-17 07:57:18 -040086 });
87}(Module)); // When this file is loaded in, the high level object is "Module";