cpu.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Don't over-write the MakeCanvasSurface set by gpu.js if it exists.
  24. if (!CanvasKit.MakeCanvasSurface) {
  25. CanvasKit.MakeCanvasSurface = CanvasKit.MakeSWCanvasSurface;
  26. }
  27. CanvasKit.MakeSurface = function(width, height) {
  28. /* @dict */
  29. var imageInfo = {
  30. 'width': width,
  31. 'height': height,
  32. 'colorType': CanvasKit.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': CanvasKit.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 = CanvasKit._malloc(pixelLen);
  40. var surface = this._getRasterDirectSurface(imageInfo, pixelPtr, width*4);
  41. if (surface) {
  42. surface._canvas = null;
  43. surface._width = width;
  44. surface._height = height;
  45. surface._pixelLen = pixelLen;
  46. surface._pixelPtr = pixelPtr;
  47. // rasterDirectSurface does not initialize the pixels, so we clear them
  48. // to transparent black.
  49. surface.getCanvas().clear(CanvasKit.TRANSPARENT);
  50. }
  51. return surface;
  52. };
  53. CanvasKit.SkSurface.prototype.flush = function() {
  54. this._flush();
  55. // Do we have an HTML canvas to write the pixels to?
  56. // We will not if this a GPU build or a raster surface, for example.
  57. if (this._canvas) {
  58. var pixels = new Uint8ClampedArray(CanvasKit.buffer, this._pixelPtr, this._pixelLen);
  59. var imageData = new ImageData(pixels, this._width, this._height);
  60. this._canvas.getContext('2d').putImageData(imageData, 0, 0);
  61. }
  62. };
  63. // Call dispose() instead of delete to clean up the underlying memory
  64. CanvasKit.SkSurface.prototype.dispose = function() {
  65. if (this._pixelPtr) {
  66. CanvasKit._free(this._pixelPtr);
  67. }
  68. this.delete();
  69. }
  70. CanvasKit.currentContext = CanvasKit.currentContext || function() {
  71. // no op if this is a cpu-only build.
  72. };
  73. CanvasKit.setCurrentContext = CanvasKit.setCurrentContext || function() {
  74. // no op if this is a cpu-only build.
  75. };
  76. });
  77. }(Module)); // When this file is loaded in, the high level object is "Module";