canvas.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const protocol = require("devtools/shared/protocol");
  6. const {Arg, Option, RetVal, generateActorSpec} = protocol;
  7. /**
  8. * Type representing an ArrayBufferView, serialized fast(er).
  9. *
  10. * Don't create a new array buffer view from the parsed array on the frontend.
  11. * Consumers may copy the data into an existing buffer, or create a new one if
  12. * necesasry. For example, this avoids the need for a redundant copy when
  13. * populating ImageData objects, at the expense of transferring char views
  14. * of a pixel buffer over the protocol instead of a packed int view.
  15. *
  16. * XXX: It would be nice if on local connections (only), we could just *give*
  17. * the buffer directly to the front, instead of going through all this
  18. * serialization redundancy.
  19. */
  20. protocol.types.addType("array-buffer-view", {
  21. write: (v) => "[" + Array.join(v, ",") + "]",
  22. read: (v) => JSON.parse(v)
  23. });
  24. /**
  25. * Type describing a thumbnail or screenshot in a recorded animation frame.
  26. */
  27. protocol.types.addDictType("snapshot-image", {
  28. index: "number",
  29. width: "number",
  30. height: "number",
  31. scaling: "number",
  32. flipped: "boolean",
  33. pixels: "array-buffer-view"
  34. });
  35. /**
  36. * Type describing an overview of a recorded animation frame.
  37. */
  38. protocol.types.addDictType("snapshot-overview", {
  39. calls: "array:function-call",
  40. thumbnails: "array:snapshot-image",
  41. screenshot: "snapshot-image"
  42. });
  43. exports.CANVAS_CONTEXTS = [
  44. "CanvasRenderingContext2D",
  45. "WebGLRenderingContext"
  46. ];
  47. exports.ANIMATION_GENERATORS = [
  48. "requestAnimationFrame"
  49. ];
  50. exports.LOOP_GENERATORS = [
  51. "setTimeout"
  52. ];
  53. exports.DRAW_CALLS = [
  54. // 2D canvas
  55. "fill",
  56. "stroke",
  57. "clearRect",
  58. "fillRect",
  59. "strokeRect",
  60. "fillText",
  61. "strokeText",
  62. "drawImage",
  63. // WebGL
  64. "clear",
  65. "drawArrays",
  66. "drawElements",
  67. "finish",
  68. "flush"
  69. ];
  70. exports.INTERESTING_CALLS = [
  71. // 2D canvas
  72. "save",
  73. "restore",
  74. // WebGL
  75. "useProgram"
  76. ];
  77. const frameSnapshotSpec = generateActorSpec({
  78. typeName: "frame-snapshot",
  79. methods: {
  80. getOverview: {
  81. response: { overview: RetVal("snapshot-overview") }
  82. },
  83. generateScreenshotFor: {
  84. request: { call: Arg(0, "function-call") },
  85. response: { screenshot: RetVal("snapshot-image") }
  86. },
  87. },
  88. });
  89. exports.frameSnapshotSpec = frameSnapshotSpec;
  90. const canvasSpec = generateActorSpec({
  91. typeName: "canvas",
  92. methods: {
  93. setup: {
  94. request: { reload: Option(0, "boolean") },
  95. oneway: true
  96. },
  97. finalize: {
  98. oneway: true
  99. },
  100. isInitialized: {
  101. response: { initialized: RetVal("boolean") }
  102. },
  103. isRecording: {
  104. response: { recording: RetVal("boolean") }
  105. },
  106. recordAnimationFrame: {
  107. response: { snapshot: RetVal("nullable:frame-snapshot") }
  108. },
  109. stopRecordingAnimationFrame: {
  110. oneway: true
  111. },
  112. }
  113. });
  114. exports.canvasSpec = canvasSpec;