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