Home | History | Annotate | Download | only in canvaskit
      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         // GL is an enscripten provided helper
     40         // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
     41         return GL.createContext(canvas, contextAttributes);
     42       }
     43 
     44       CanvasKit.GetWebGLContext = function(canvas, attrs) {
     45         return makeWebGLContext(canvas, attrs);
     46       };
     47 
     48       // arg can be of types:
     49       //  - String - in which case it is interpreted as an id of a
     50       //          canvas element.
     51       //  - HTMLCanvasElement - in which the provided canvas element will
     52       //          be used directly.
     53       // Width and height can be provided to override those on the canvas
     54       // element, or specify a height for when a context is provided.
     55       CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
     56         var canvas = arg;
     57         if (canvas.tagName !== 'CANVAS') {
     58           canvas = document.getElementById(arg);
     59           if (!canvas) {
     60             throw 'Canvas with id ' + arg + ' was not found';
     61           }
     62         }
     63         // we are ok with all the defaults
     64         var ctx = CanvasKit.GetWebGLContext(canvas);
     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         var grcontext = this.MakeGrContext(ctx);
     75         // Maybe better to use clientWidth/height.  See:
     76         // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
     77         var surface = this.MakeOnScreenGLSurface(grcontext,
     78                                                  width  || canvas.width,
     79                                                  height || canvas.height);
     80         if (!surface) {
     81           SkDebug('falling back from GPU implementation to a SW based one');
     82           return CanvasKit.MakeSWCanvasSurface(arg);
     83         }
     84         return surface;
     85       };
     86       // Default to trying WebGL first.
     87       CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
     88     });
     89 }(Module)); // When this file is loaded in, the high level object is "Module";
     90