• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 a canvas element
5    DebuggerView.MakeSWCanvasSurface = function(canvas) {
6      // Set the canvas element to have a 2d non-gpu context. (constant until element is destroyed)
7      // We don't need the context in this scope, we just want the side effect.
8      canvas.getContext('2d', {
9          alpha: true,
10          depth: false
11        });
12      // Maybe better to use clientWidth/height.  See:
13      // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
14      var surface = DebuggerView.MakeSurface(canvas.width, canvas.height);
15      if (surface) {
16        surface._canvas = canvas;
17      }
18      console.log('Made HTML Canvas Surface');
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      var bufferLen = width * height * 4; // 4 bytes per pixel
29      // Allocate the buffer of pixels to be drawn into.
30      var pixelPtr = DebuggerView._malloc(bufferLen);
31      var imageInfo = {
32        'width':  width,
33        'height': height,
34        // RGBA 8888 is the only pixel format we can show on an HTML canvas
35        'colorType': DebuggerView.ColorType.RGBA_8888,
36        // We are sending these pixels directly into the HTML canvas,
37        // (and those pixels are un-premultiplied, i.e. straight r,g,b,a)
38        'alphaType': DebuggerView.AlphaType.Unpremul,
39        'imageAddress': 0, // unused here. field only relevant for some UI stuff in resource tab.
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._bufferLen = bufferLen;
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.HEAPU8.buffer, this._pixelPtr, this._bufferLen);
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";
87