1// Adds compile-time JS functions to augment the CanvasKit interface. 2// Specifically, anything that should only be on the CPU version of canvaskit. 3(function(CanvasKit){ 4 CanvasKit._extraInitializations = CanvasKit._extraInitializations || []; 5 CanvasKit._extraInitializations.push(function() { 6 // Takes in an html id or a canvas element 7 CanvasKit.MakeSWCanvasSurface = function(idOrElement) { 8 var canvas = idOrElement; 9 if (canvas.tagName !== 'CANVAS') { 10 canvas = document.getElementById(idOrElement); 11 if (!canvas) { 12 throw 'Canvas with id ' + idOrElement + ' was not found'; 13 } 14 } 15 // Maybe better to use clientWidth/height. See: 16 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html 17 var surface = CanvasKit.MakeSurface(canvas.width, canvas.height); 18 if (surface) { 19 surface._canvas = canvas; 20 } 21 return surface; 22 }; 23 24 // Don't over-write the MakeCanvasSurface set by gpu.js if it exists. 25 if (!CanvasKit.MakeCanvasSurface) { 26 CanvasKit.MakeCanvasSurface = CanvasKit.MakeSWCanvasSurface; 27 } 28 29 CanvasKit.MakeSurface = function(width, height) { 30 /* @dict */ 31 var imageInfo = { 32 'width': width, 33 'height': height, 34 'colorType': CanvasKit.ColorType.RGBA_8888, 35 // Since we are sending these pixels directly into the HTML canvas, 36 // (and those pixels are un-premultiplied, i.e. straight r,g,b,a) 37 'alphaType': CanvasKit.AlphaType.Unpremul, 38 } 39 var pixelLen = width * height * 4; // it's 8888, so 4 bytes per pixel 40 // Allocate the buffer of pixels to be drawn into. 41 var pixelPtr = CanvasKit._malloc(pixelLen); 42 43 var surface = this._getRasterDirectSurface(imageInfo, pixelPtr, width*4); 44 if (surface) { 45 surface._canvas = null; 46 surface._width = width; 47 surface._height = height; 48 surface._pixelLen = pixelLen; 49 50 surface._pixelPtr = pixelPtr; 51 // rasterDirectSurface does not initialize the pixels, so we clear them 52 // to transparent black. 53 surface.getCanvas().clear(CanvasKit.TRANSPARENT); 54 } 55 return surface; 56 }; 57 58 CanvasKit.SkSurface.prototype.flush = function() { 59 this._flush(); 60 // Do we have an HTML canvas to write the pixels to? 61 // We will not if this a GPU build or a raster surface, for example. 62 if (this._canvas) { 63 var pixels = new Uint8ClampedArray(CanvasKit.HEAPU8.buffer, this._pixelPtr, this._pixelLen); 64 var imageData = new ImageData(pixels, this._width, this._height); 65 66 this._canvas.getContext('2d').putImageData(imageData, 0, 0); 67 } 68 }; 69 70 // Call dispose() instead of delete to clean up the underlying memory 71 CanvasKit.SkSurface.prototype.dispose = function() { 72 if (this._pixelPtr) { 73 CanvasKit._free(this._pixelPtr); 74 } 75 this.delete(); 76 } 77 78 CanvasKit.currentContext = CanvasKit.currentContext || function() { 79 // no op if this is a cpu-only build. 80 }; 81 82 CanvasKit.setCurrentContext = CanvasKit.setCurrentContext || function() { 83 // no op if this is a cpu-only build. 84 }; 85 }); 86}(Module)); // When this file is loaded in, the high level object is "Module"; 87