blob: 4e4eb8004051a7cd8aa01dce4aae612133c85e55 [file] [log] [blame]
Kevin Lubick3088c692020-04-24 15:06:19 -04001(function(SkottieKit){
2SkottieKit._extraInitializations = SkottieKit._extraInitializations || [];
3SkottieKit._extraInitializations.push(function() {
4 function get(obj, attr, defaultValue) {
5 if (obj && obj.hasOwnProperty(attr)) {
6 return obj[attr];
7 }
8 return defaultValue;
9 }
10
11 function makeWebGLContext(canvas, attrs) {
12 var contextAttributes = {
13 alpha: get(attrs, 'alpha', 1),
14 depth: get(attrs, 'depth', 1),
15 stencil: get(attrs, 'stencil', 8),
16 antialias: get(attrs, 'antialias', 1),
17 premultipliedAlpha: get(attrs, 'premultipliedAlpha', 1),
18 preserveDrawingBuffer: get(attrs, 'preserveDrawingBuffer', 0),
19 preferLowPowerToHighPerformance: get(attrs, 'preferLowPowerToHighPerformance', 0),
20 failIfMajorPerformanceCaveat: get(attrs, 'failIfMajorPerformanceCaveat', 0),
21 majorVersion: get(attrs, 'majorVersion', 2),
22 minorVersion: get(attrs, 'minorVersion', 0),
23 enableExtensionsByDefault: get(attrs, 'enableExtensionsByDefault', 1),
24 explicitSwapControl: get(attrs, 'explicitSwapControl', 0),
25 renderViaOffscreenBackBuffer: get(attrs, 'renderViaOffscreenBackBuffer', 0),
26 };
27 if (!canvas) {
28 SkDebug('null canvas passed into makeWebGLContext');
29 return 0;
30 }
31 // This check is from the emscripten version
32 if (contextAttributes['explicitSwapControl']) {
33 SkDebug('explicitSwapControl is not supported');
34 return 0;
35 }
36 // GL is an enscripten provided helper
37 // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
38 var ctx = GL.createContext(canvas, contextAttributes);
39
40 if (!ctx && contextAttributes.majorVersion > 1) {
41 contextAttributes.majorVersion = 1; // fall back to WebGL 1.0
42 contextAttributes.minorVersion = 0;
43 ctx = GL.createContext(canvas, contextAttributes);
44 }
45 return ctx;
46 }
47
48 SkottieKit.GetWebGLContext = function(canvas, attrs) {
49 return makeWebGLContext(canvas, attrs);
50 };
51
52 // arg can be of types:
53 // - String - in which case it is interpreted as an id of a
54 // canvas element.
55 // - HTMLCanvasElement - in which the provided canvas element will
56 // be used directly.
57 // Width and height can be provided to override those on the canvas
58 // element, or specify a height for when a context is provided.
59 SkottieKit.MakeWebGLCanvasSurface = function(arg, width, height) {
60 var canvas = arg;
61 if (canvas.tagName !== 'CANVAS') {
62 canvas = document.getElementById(arg);
63 if (!canvas) {
64 throw 'Canvas with id ' + arg + ' was not found';
65 }
66 }
67 // we are ok with all the defaults
68 var ctx = this.GetWebGLContext(canvas);
69
70 if (!ctx || ctx < 0) {
71 throw 'failed to create webgl context: err ' + ctx;
72 }
73
74 if (!canvas && (!width || !height)) {
75 throw 'height and width must be provided with context';
76 }
77
78 var grcontext = this.MakeGrContext(ctx);
79
80 // if (grcontext) {
81 // // Bump the default resource cache limit.
82 // var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
83 // grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
84 // }
85
86
87 // Maybe better to use clientWidth/height. See:
88 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
89 var surface = this.MakeOnScreenGLSurface(grcontext,
90 width || canvas.width,
91 height || canvas.height);
92 if (!surface) {
93 SkDebug('falling back from GPU implementation to a SW based one');
94 // we need to throw away the old canvas (which was locked to
95 // a webGL context) and create a new one so we can
96 var newCanvas = canvas.cloneNode(true);
97 var parent = canvas.parentNode;
98 parent.replaceChild(newCanvas, canvas);
99 // add a class so the user can detect that it was replaced.
100 newCanvas.classList.add('ck-replaced');
101
102 return SkottieKit.MakeSWCanvasSurface(newCanvas);
103 }
104 surface._context = ctx;
105 surface.grContext = grcontext;
106 return surface;
107 };
108 // Default to trying WebGL first.
109 SkottieKit.MakeCanvasSurface = SkottieKit.MakeWebGLCanvasSurface;
110});
111}(Module)); // When this file is loaded in, the high level object is "Module";