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