• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// Functions in this file are supplemented by cpu.js.
4(function(CanvasKit){
5    CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
6    CanvasKit._extraInitializations.push(function() {
7      function get(obj, attr, defaultValue) {
8        if (obj && obj.hasOwnProperty(attr)) {
9          return obj[attr];
10        }
11        return defaultValue;
12      }
13
14      CanvasKit.GetWebGLContext = function(canvas, attrs) {
15        if (!canvas) {
16          throw 'null canvas passed into makeWebGLContext';
17        }
18        var contextAttributes = {
19          'alpha': get(attrs, 'alpha', 1),
20          'depth': get(attrs, 'depth', 1),
21          'stencil': get(attrs, 'stencil', 8),
22          'antialias': get(attrs, 'antialias', 0),
23          'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1),
24          'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0),
25          'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0),
26          'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0),
27          'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1),
28          'explicitSwapControl': get(attrs, 'explicitSwapControl', 0),
29          'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0),
30        };
31
32        if (attrs && attrs['majorVersion']) {
33          contextAttributes['majorVersion'] = attrs['majorVersion']
34        } else {
35          // Default to WebGL 2 if available and not specified.
36          contextAttributes['majorVersion'] = (typeof WebGL2RenderingContext !== 'undefined') ? 2 : 1;
37        }
38
39        // This check is from the emscripten version
40        if (contextAttributes['explicitSwapControl']) {
41          throw 'explicitSwapControl is not supported';
42        }
43        // Creates a WebGL context and sets it to be the current context.
44        // These functions are defined in emscripten's library_webgl.js
45        var handle = GL.createContext(canvas, contextAttributes);
46        if (!handle) {
47          return 0;
48        }
49        GL.makeContextCurrent(handle);
50        return handle;
51      };
52
53      CanvasKit.deleteContext = function(handle) {
54        GL.deleteContext(handle);
55      };
56
57      // idOrElement can be of types:
58      //  - String - in which case it is interpreted as an id of a
59      //          canvas element.
60      //  - HTMLCanvasElement - in which the provided canvas element will
61      //          be used directly.
62      // colorSpace - sk_sp<ColorSpace> - one of the supported color spaces:
63      //          CanvasKit.ColorSpace.SRGB
64      //          CanvasKit.ColorSpace.DISPLAY_P3
65      //          CanvasKit.ColorSpace.ADOBE_RGB
66      CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace, attrs) {
67        colorSpace = colorSpace || null;
68        var canvas = idOrElement;
69        var isHTMLCanvas = typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement;
70        var isOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas;
71        if (!isHTMLCanvas && !isOffscreenCanvas) {
72          canvas = document.getElementById(idOrElement);
73          if (!canvas) {
74            throw 'Canvas with id ' + idOrElement + ' was not found';
75          }
76        }
77
78        var ctx = this.GetWebGLContext(canvas, attrs);
79        if (!ctx || ctx < 0) {
80          throw 'failed to create webgl context: err ' + ctx;
81        }
82
83        var grcontext = this.MakeGrContext(ctx);
84
85        // Note that canvas.width/height here is used because it gives the size of the buffer we're
86        // rendering into. This may not be the same size the element is displayed on the page, which
87        // constrolled by css, and available in canvas.clientWidth/height.
88        var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace);
89        if (!surface) {
90          Debug('falling back from GPU implementation to a SW based one');
91          // we need to throw away the old canvas (which was locked to
92          // a webGL context) and create a new one so we can
93          var newCanvas = canvas.cloneNode(true);
94          var parent = canvas.parentNode;
95          parent.replaceChild(newCanvas, canvas);
96          // add a class so the user can detect that it was replaced.
97          newCanvas.classList.add('ck-replaced');
98
99          return CanvasKit.MakeSWCanvasSurface(newCanvas);
100        }
101        surface._context = ctx;
102        surface.grContext = grcontext;
103        surface.openGLversion = canvas.GLctxObject.version;
104        return surface;
105      };
106      // Default to trying WebGL first.
107      CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
108    });
109}(Module)); // When this file is loaded in, the high level object is "Module";
110