canvasView.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import {mat3, vec2} from 'gl-matrix';
  2. import {FlamegraphCanvas} from 'sentry/utils/profiling/flamegraphCanvas';
  3. import {
  4. computeClampedConfigView,
  5. transformMatrixBetweenRect,
  6. } from 'sentry/utils/profiling/gl/utils';
  7. import {Rect} from 'sentry/utils/profiling/speedscope';
  8. export class CanvasView<T extends {configSpace: Rect}> {
  9. configView: Rect = Rect.Empty();
  10. configSpace: Readonly<Rect> = Rect.Empty();
  11. configSpaceTransform: mat3 = mat3.create();
  12. inverted: boolean;
  13. minWidth: number;
  14. maxHeight: number;
  15. minHeight: number;
  16. depthOffset: number;
  17. barHeight: number;
  18. model: T;
  19. canvas: FlamegraphCanvas;
  20. mode: 'anchorTop' | 'anchorBottom' | 'stretchToFit' = 'anchorTop';
  21. constructor({
  22. canvas,
  23. options,
  24. model,
  25. mode,
  26. }: {
  27. canvas: FlamegraphCanvas;
  28. model: T;
  29. options: {
  30. barHeight: number;
  31. configSpaceTransform?: Rect;
  32. depthOffset?: number;
  33. inverted?: boolean;
  34. maxHeight?: number;
  35. minHeight?: number;
  36. minWidth?: number;
  37. };
  38. mode?: CanvasView<T>['mode'];
  39. }) {
  40. this.mode = mode || this.mode;
  41. this.inverted = !!options.inverted;
  42. this.minWidth = options.minWidth ?? 0;
  43. this.maxHeight = options.maxHeight ?? 0;
  44. this.minHeight = options.minHeight ?? 0;
  45. this.model = model;
  46. this.canvas = canvas;
  47. this.depthOffset = options.depthOffset ?? 0;
  48. this.barHeight = options.barHeight ? options.barHeight * window.devicePixelRatio : 1;
  49. // This is a transformation matrix that is applied to the configView, it allows us to
  50. // transform an entire view and render it without having to recompute the models.
  51. // This is useful for example when we want to offset a profile by some duration.
  52. this.configSpaceTransform = options.configSpaceTransform
  53. ? mat3.fromValues(
  54. options.configSpaceTransform.width || 1,
  55. 0,
  56. 0,
  57. 0,
  58. options.configSpaceTransform.height || 1,
  59. 0,
  60. options.configSpaceTransform.x || 0,
  61. options.configSpaceTransform.y || 0,
  62. 1
  63. )
  64. : mat3.create();
  65. this.initConfigSpace(canvas);
  66. }
  67. setMinWidth(minWidth: number) {
  68. if (minWidth < 0) {
  69. throw new Error('View min width cannot be negative');
  70. }
  71. this.minWidth = minWidth;
  72. }
  73. isViewAtTopEdgeOf(space: Rect): boolean {
  74. return this.inverted
  75. ? space.bottom === this.configView.bottom
  76. : space.top === this.configView.top;
  77. }
  78. isViewAtBottomEdgeOf(space: Rect): boolean {
  79. return this.inverted
  80. ? space.top === this.configView.top
  81. : space.bottom === this.configView.bottom;
  82. }
  83. private _initConfigSpace(canvas: FlamegraphCanvas): void {
  84. switch (this.mode) {
  85. case 'stretchToFit': {
  86. this.configSpace = new Rect(
  87. 0,
  88. 0,
  89. this.model.configSpace.width,
  90. this.model.configSpace.height + this.depthOffset
  91. );
  92. return;
  93. }
  94. case 'anchorBottom':
  95. case 'anchorTop':
  96. default: {
  97. this.configSpace = new Rect(
  98. 0,
  99. 0,
  100. this.model.configSpace.width,
  101. this.maxHeight ||
  102. Math.max(
  103. this.model.configSpace.height + this.depthOffset,
  104. canvas.physicalSpace.height / this.barHeight
  105. )
  106. );
  107. }
  108. }
  109. }
  110. private _initConfigView(canvas: FlamegraphCanvas, space: Rect): void {
  111. switch (this.mode) {
  112. case 'stretchToFit': {
  113. this.setConfigView(Rect.From(space));
  114. return;
  115. }
  116. case 'anchorBottom': {
  117. const newHeight = this.maxHeight || canvas.physicalSpace.height / this.barHeight;
  118. const newY = Math.max(0, Math.ceil(space.y - (newHeight - space.height)));
  119. this.setConfigView(Rect.From(space).withHeight(newHeight).withY(newY));
  120. return;
  121. }
  122. case 'anchorTop': {
  123. this.setConfigView(
  124. Rect.From(space).withHeight(
  125. this.maxHeight || canvas.physicalSpace.height / this.barHeight
  126. )
  127. );
  128. return;
  129. }
  130. default:
  131. throw new Error(`Unknown CanvasView mode: ${this.mode}`);
  132. }
  133. }
  134. initConfigSpace(canvas: FlamegraphCanvas): void {
  135. this._initConfigSpace(canvas);
  136. this._initConfigView(canvas, this.configSpace);
  137. }
  138. resizeConfigSpace(canvas: FlamegraphCanvas): void {
  139. this._initConfigSpace(canvas);
  140. this._initConfigView(canvas, this.configView);
  141. }
  142. resetConfigView(canvas: FlamegraphCanvas): void {
  143. this._initConfigView(canvas, this.configSpace);
  144. }
  145. setConfigView(
  146. configView: Rect,
  147. overrides?: {
  148. width: {max?: number; min?: number};
  149. height?: {max?: number; min?: number};
  150. }
  151. ) {
  152. this.configView = computeClampedConfigView(configView, {
  153. width: {
  154. min: this.minWidth,
  155. max: this.configSpace.width,
  156. ...(overrides?.width ?? {}),
  157. },
  158. height: {
  159. min: this.minHeight,
  160. max: this.configSpace.height,
  161. ...(overrides?.height ?? {}),
  162. },
  163. });
  164. }
  165. transformConfigView(transformation: mat3) {
  166. this.setConfigView(this.configView.transformRect(transformation));
  167. }
  168. toConfigSpace(space: Rect): mat3 {
  169. const toConfigSpace = transformMatrixBetweenRect(space, this.configSpace);
  170. if (this.inverted) {
  171. mat3.multiply(toConfigSpace, this.configSpace.invertYTransform(), toConfigSpace);
  172. }
  173. return toConfigSpace;
  174. }
  175. toConfigView(space: Rect): mat3 {
  176. const toConfigView = transformMatrixBetweenRect(space, this.configView);
  177. if (this.inverted) {
  178. mat3.multiply(toConfigView, this.configView.invertYTransform(), toConfigView);
  179. }
  180. return toConfigView;
  181. }
  182. fromConfigSpace(space: Rect): mat3 {
  183. const fromConfigSpace = transformMatrixBetweenRect(this.configSpace, space);
  184. if (this.inverted) {
  185. mat3.multiply(fromConfigSpace, space.invertYTransform(), fromConfigSpace);
  186. }
  187. return fromConfigSpace;
  188. }
  189. fromConfigView(space: Rect): mat3 {
  190. const fromConfigView = transformMatrixBetweenRect(this.configView, space);
  191. if (this.inverted) {
  192. mat3.multiply(fromConfigView, space.invertYTransform(), fromConfigView);
  193. }
  194. return fromConfigView;
  195. }
  196. fromTransformedConfigView(space: Rect): mat3 {
  197. const fromConfigView = mat3.multiply(
  198. mat3.create(),
  199. transformMatrixBetweenRect(this.configView, space),
  200. this.configSpaceTransform
  201. );
  202. if (this.inverted) {
  203. mat3.multiply(fromConfigView, space.invertYTransform(), fromConfigView);
  204. }
  205. return fromConfigView;
  206. }
  207. fromTransformedConfigSpace(space: Rect): mat3 {
  208. const fromConfigView = mat3.multiply(
  209. mat3.create(),
  210. transformMatrixBetweenRect(this.configSpace, space),
  211. this.configSpaceTransform
  212. );
  213. if (this.inverted) {
  214. mat3.multiply(fromConfigView, space.invertYTransform(), fromConfigView);
  215. }
  216. return fromConfigView;
  217. }
  218. getConfigSpaceCursor(logicalSpaceCursor: vec2, canvas: FlamegraphCanvas): vec2 {
  219. const physicalSpaceCursor = vec2.transformMat3(
  220. vec2.create(),
  221. logicalSpaceCursor,
  222. canvas.logicalToPhysicalSpace
  223. );
  224. return vec2.transformMat3(
  225. vec2.create(),
  226. physicalSpaceCursor,
  227. this.toConfigSpace(canvas.physicalSpace)
  228. );
  229. }
  230. getTransformedConfigSpaceCursor(
  231. logicalSpaceCursor: vec2,
  232. canvas: FlamegraphCanvas
  233. ): vec2 {
  234. const physicalSpaceCursor = vec2.transformMat3(
  235. vec2.create(),
  236. logicalSpaceCursor,
  237. canvas.logicalToPhysicalSpace
  238. );
  239. const finalMatrix = mat3.multiply(
  240. mat3.create(),
  241. mat3.invert(mat3.create(), this.configSpaceTransform),
  242. this.toConfigSpace(canvas.physicalSpace)
  243. );
  244. const configViewCursor = vec2.transformMat3(
  245. vec2.create(),
  246. physicalSpaceCursor,
  247. finalMatrix
  248. );
  249. return configViewCursor;
  250. }
  251. getConfigViewCursor(logicalSpaceCursor: vec2, canvas: FlamegraphCanvas): vec2 {
  252. const physicalSpaceCursor = vec2.transformMat3(
  253. vec2.create(),
  254. logicalSpaceCursor,
  255. canvas.logicalToPhysicalSpace
  256. );
  257. return vec2.transformMat3(
  258. vec2.create(),
  259. physicalSpaceCursor,
  260. this.toConfigView(canvas.physicalSpace)
  261. );
  262. }
  263. getTransformedConfigViewCursor(
  264. logicalSpaceCursor: vec2,
  265. canvas: FlamegraphCanvas
  266. ): vec2 {
  267. const physicalSpaceCursor = vec2.transformMat3(
  268. vec2.create(),
  269. logicalSpaceCursor,
  270. canvas.logicalToPhysicalSpace
  271. );
  272. const finalMatrix = mat3.multiply(
  273. mat3.create(),
  274. mat3.invert(mat3.create(), this.configSpaceTransform),
  275. this.toConfigView(canvas.physicalSpace)
  276. );
  277. const configViewCursor = vec2.transformMat3(
  278. vec2.create(),
  279. physicalSpaceCursor,
  280. finalMatrix
  281. );
  282. return configViewCursor;
  283. }
  284. /**
  285. * Applies the inverse of the config space transform to the given config space rect
  286. * @returns Rect
  287. */
  288. toOriginConfigView(space: Rect): Rect {
  289. return space.transformRect(mat3.invert(mat3.create(), this.configSpaceTransform));
  290. }
  291. }