2019-03-05 15:45:40 +00:00
|
|
|
// Adds compile-time JS functions to handle creation and flushing of wasm's offscreen buffer
|
|
|
|
// to a visible element on the page.
|
|
|
|
(function(DebuggerView){
|
2019-03-28 17:08:51 +00:00
|
|
|
// Takes a canvas element
|
|
|
|
DebuggerView.MakeSWCanvasSurface = function(canvas) {
|
|
|
|
// Set the canvas element to have a 2d non-gpu context. (constant until element is destroyed)
|
|
|
|
// We don't need the context in this scope, we just want the side effect.
|
|
|
|
canvas.getContext('2d', {
|
|
|
|
alpha: true,
|
|
|
|
depth: false
|
|
|
|
});
|
2019-03-05 15:45:40 +00:00
|
|
|
// Maybe better to use clientWidth/height. See:
|
|
|
|
// https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
|
2019-03-28 17:08:51 +00:00
|
|
|
var surface = DebuggerView.MakeSurface(canvas.width, canvas.height);
|
2019-03-05 15:45:40 +00:00
|
|
|
if (surface) {
|
|
|
|
surface._canvas = canvas;
|
|
|
|
}
|
2019-03-15 14:58:31 +00:00
|
|
|
console.log('Made HTML Canvas Surface');
|
2019-03-05 15:45:40 +00:00
|
|
|
return surface;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't over-write the MakeCanvasSurface set by gpu.js if it exists.
|
|
|
|
if (!DebuggerView.MakeCanvasSurface) {
|
|
|
|
DebuggerView.MakeCanvasSurface = DebuggerView.MakeSWCanvasSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebuggerView.MakeSurface = function(width, height) {
|
2019-03-28 17:08:51 +00:00
|
|
|
var bufferLen = width * height * 4; // 4 bytes per pixel
|
|
|
|
// Allocate the buffer of pixels to be drawn into.
|
|
|
|
var pixelPtr = DebuggerView._malloc(bufferLen);
|
|
|
|
var imageInfo = {
|
2019-03-05 15:45:40 +00:00
|
|
|
'width': width,
|
|
|
|
'height': height,
|
2019-03-28 17:08:51 +00:00
|
|
|
// RGBA 8888 is the only pixel format we can show on an HTML canvas
|
2019-03-05 15:45:40 +00:00
|
|
|
'colorType': DebuggerView.ColorType.RGBA_8888,
|
2019-03-28 17:08:51 +00:00
|
|
|
// We are sending these pixels directly into the HTML canvas,
|
2019-03-05 15:45:40 +00:00
|
|
|
// (and those pixels are un-premultiplied, i.e. straight r,g,b,a)
|
|
|
|
'alphaType': DebuggerView.AlphaType.Unpremul,
|
2019-12-19 17:54:15 +00:00
|
|
|
'imageAddress': 0, // unused here. field only relevant for some UI stuff in resource tab.
|
2019-03-05 15:45:40 +00:00
|
|
|
}
|
2019-03-28 17:08:51 +00:00
|
|
|
var surface = this._getRasterDirectSurface(imageInfo, pixelPtr, width * 4);
|
2019-03-05 15:45:40 +00:00
|
|
|
if (surface) {
|
|
|
|
surface._canvas = null;
|
|
|
|
surface._width = width;
|
|
|
|
surface._height = height;
|
2019-03-28 17:08:51 +00:00
|
|
|
surface._bufferLen = bufferLen;
|
2019-03-05 15:45:40 +00:00
|
|
|
|
|
|
|
surface._pixelPtr = pixelPtr;
|
|
|
|
// rasterDirectSurface does not initialize the pixels, so we clear them
|
|
|
|
// to transparent black.
|
|
|
|
surface.getCanvas().clear(DebuggerView.TRANSPARENT);
|
|
|
|
}
|
|
|
|
return surface;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DebuggerView.onRuntimeInitialized = function() {
|
|
|
|
|
|
|
|
DebuggerView.SkSurface.prototype.flush = function() {
|
|
|
|
this._flush();
|
|
|
|
// Do we have an HTML canvas to write the pixels to?
|
|
|
|
// We will not if this a GPU build or a raster surface, for example.
|
|
|
|
if (this._canvas) {
|
2019-10-10 11:35:05 +00:00
|
|
|
var pixels = new Uint8ClampedArray(DebuggerView.HEAPU8.buffer, this._pixelPtr, this._bufferLen);
|
2019-03-28 17:08:51 +00:00
|
|
|
var imageData = new ImageData(pixels, this._width, this._height);
|
2019-03-05 15:45:40 +00:00
|
|
|
this._canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Call dispose() instead of delete to clean up the underlying memory
|
|
|
|
DebuggerView.SkSurface.prototype.dispose = function() {
|
|
|
|
if (this._pixelPtr) {
|
|
|
|
DebuggerView._free(this._pixelPtr);
|
|
|
|
}
|
|
|
|
this.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DebuggerView.currentContext = DebuggerView.currentContext || function() {
|
|
|
|
// no op if this is a cpu-only build.
|
|
|
|
};
|
|
|
|
|
|
|
|
DebuggerView.setCurrentContext = DebuggerView.setCurrentContext || function() {
|
|
|
|
// no op if this is a cpu-only build.
|
|
|
|
};
|
|
|
|
}(Module)); // When this file is loaded in, the high level object is "Module";
|