gpu.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Adds compile-time JS functions to augment the CanvasKit interface.
  2. // Specifically, anything that should only be on the GPU version of canvaskit.
  3. (function(CanvasKit){
  4. CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
  5. CanvasKit._extraInitializations.push(function() {
  6. function get(obj, attr, defaultValue) {
  7. if (obj && obj.hasOwnProperty(attr)) {
  8. return obj[attr];
  9. }
  10. return defaultValue;
  11. }
  12. function makeWebGLContext(canvas, attrs) {
  13. // These defaults come from the emscripten _emscripten_webgl_create_context
  14. var contextAttributes = {
  15. alpha: get(attrs, 'alpha', 1),
  16. depth: get(attrs, 'depth', 1),
  17. stencil: get(attrs, 'stencil', 0),
  18. antialias: get(attrs, 'antialias', 1),
  19. premultipliedAlpha: get(attrs, 'premultipliedAlpha', 1),
  20. preserveDrawingBuffer: get(attrs, 'preserveDrawingBuffer', 0),
  21. preferLowPowerToHighPerformance: get(attrs, 'preferLowPowerToHighPerformance', 0),
  22. failIfMajorPerformanceCaveat: get(attrs, 'failIfMajorPerformanceCaveat', 0),
  23. majorVersion: get(attrs, 'majorVersion', 1),
  24. minorVersion: get(attrs, 'minorVersion', 0),
  25. enableExtensionsByDefault: get(attrs, 'enableExtensionsByDefault', 1),
  26. explicitSwapControl: get(attrs, 'explicitSwapControl', 0),
  27. renderViaOffscreenBackBuffer: get(attrs, 'renderViaOffscreenBackBuffer', 0),
  28. };
  29. if (!canvas) {
  30. SkDebug('null canvas passed into makeWebGLContext');
  31. return 0;
  32. }
  33. // This check is from the emscripten version
  34. if (contextAttributes['explicitSwapControl']) {
  35. SkDebug('explicitSwapControl is not supported');
  36. return 0;
  37. }
  38. // GL is an enscripten provided helper
  39. // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
  40. return GL.createContext(canvas, contextAttributes);
  41. }
  42. CanvasKit.GetWebGLContext = function(canvas, attrs) {
  43. return makeWebGLContext(canvas, attrs);
  44. };
  45. // arg can be of types:
  46. // - String - in which case it is interpreted as an id of a
  47. // canvas element.
  48. // - HTMLCanvasElement - in which the provided canvas element will
  49. // be used directly.
  50. // Width and height can be provided to override those on the canvas
  51. // element, or specify a height for when a context is provided.
  52. CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
  53. var canvas = arg;
  54. if (canvas.tagName !== 'CANVAS') {
  55. canvas = document.getElementById(arg);
  56. if (!canvas) {
  57. throw 'Canvas with id ' + arg + ' was not found';
  58. }
  59. }
  60. // we are ok with all the defaults
  61. var ctx = this.GetWebGLContext(canvas);
  62. if (!ctx || ctx < 0) {
  63. throw 'failed to create webgl context: err ' + ctx;
  64. }
  65. if (!canvas && (!width || !height)) {
  66. throw 'height and width must be provided with context';
  67. }
  68. var grcontext = this.MakeGrContext(ctx);
  69. // Bump the default resource cache limit.
  70. var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
  71. grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
  72. // Maybe better to use clientWidth/height. See:
  73. // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
  74. var surface = this.MakeOnScreenGLSurface(grcontext,
  75. width || canvas.width,
  76. height || canvas.height);
  77. if (!surface) {
  78. SkDebug('falling back from GPU implementation to a SW based one');
  79. // we need to throw away the old canvas (which was locked to
  80. // a webGL context) and create a new one so we can
  81. var newCanvas = canvas.cloneNode(true);
  82. var parent = canvas.parentNode;
  83. parent.replaceChild(newCanvas, canvas);
  84. // add a class so the user can detect that it was replaced.
  85. newCanvas.classList.add('ck-replaced');
  86. return CanvasKit.MakeSWCanvasSurface(newCanvas);
  87. }
  88. surface._context = ctx;
  89. surface.grContext = grcontext;
  90. return surface;
  91. };
  92. // Default to trying WebGL first.
  93. CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
  94. });
  95. }(Module)); // When this file is loaded in, the high level object is "Module";