blob: ddd0ac16d7a428a6294a3dfa071c65eb44d5ffe0 [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) {
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -04006 let canvas = idOrElement;
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -05007 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
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040015 let surface = DebuggerView.MakeSurface(canvas.width, canvas.height);
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050016 if (surface) {
17 surface._canvas = canvas;
18 }
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040019 console.log('Made HTML Canvas Surface');
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050020 return surface;
21 };
22
23 // Don't over-write the MakeCanvasSurface set by gpu.js if it exists.
24 if (!DebuggerView.MakeCanvasSurface) {
25 DebuggerView.MakeCanvasSurface = DebuggerView.MakeSWCanvasSurface;
26 }
27
28 DebuggerView.MakeSurface = function(width, height) {
29 /* @dict */
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040030 let imageInfo = {
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050031 'width': width,
32 'height': height,
33 'colorType': DebuggerView.ColorType.RGBA_8888,
34 // Since we are sending these pixels directly into the HTML canvas,
35 // (and those pixels are un-premultiplied, i.e. straight r,g,b,a)
36 'alphaType': DebuggerView.AlphaType.Unpremul,
37 }
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040038 let pixelLen = width * height * 4; // it's 8888, so 4 bytes per pixel
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050039 // Allocate the buffer of pixels to be drawn into.
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040040 let pixelPtr = DebuggerView._malloc(pixelLen);
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050041
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040042 let surface = this._getRasterDirectSurface(imageInfo, pixelPtr, width*4);
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050043 if (surface) {
44 surface._canvas = null;
45 surface._width = width;
46 surface._height = height;
47 surface._pixelLen = pixelLen;
48
49 surface._pixelPtr = pixelPtr;
50 // rasterDirectSurface does not initialize the pixels, so we clear them
51 // to transparent black.
52 surface.getCanvas().clear(DebuggerView.TRANSPARENT);
53 }
54 return surface;
55 };
56
57
58 DebuggerView.onRuntimeInitialized = function() {
59
60 DebuggerView.SkSurface.prototype.flush = function() {
61 this._flush();
62 // Do we have an HTML canvas to write the pixels to?
63 // We will not if this a GPU build or a raster surface, for example.
64 if (this._canvas) {
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040065 let pixels = new Uint8ClampedArray(DebuggerView.buffer, this._pixelPtr, this._pixelLen);
66 let imageData = new ImageData(pixels, this._width, this._height);
Nathaniel Nifongad5f6cd2019-03-05 10:45:40 -050067 this._canvas.getContext('2d').putImageData(imageData, 0, 0);
68 }
69 };
70
71 // Call dispose() instead of delete to clean up the underlying memory
72 DebuggerView.SkSurface.prototype.dispose = function() {
73 if (this._pixelPtr) {
74 DebuggerView._free(this._pixelPtr);
75 }
76 this.delete();
77 }
78 }
79
80 DebuggerView.currentContext = DebuggerView.currentContext || function() {
81 // no op if this is a cpu-only build.
82 };
83
84 DebuggerView.setCurrentContext = DebuggerView.setCurrentContext || function() {
85 // no op if this is a cpu-only build.
86 };
87}(Module)); // When this file is loaded in, the high level object is "Module";