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 // 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 74 // Maybe better to use clientWidth/height. See: 75 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html 76 var surface = this._getWebGLSurface(ctx, width || canvas.width, 77 height || canvas.height); 78 if (!surface) { 79 SkDebug('falling back from GPU implementation to a SW based one'); 80 return CanvasKit.MakeSWCanvasSurface(arg); 81 } 82 return surface; 83 }; 84 // Default to trying WebGL first. 85 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface; 86 }); 87}(Module)); // When this file is loaded in, the high level object is "Module"; 88