utils.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. import Fuse from 'fuse.js';
  2. import {mat3, vec2} from 'gl-matrix';
  3. import {FlamegraphFrame} from 'sentry/utils/profiling/flamegraphFrame';
  4. import {FlamegraphRenderer} from 'sentry/utils/profiling/renderers/flamegraphRenderer';
  5. import {clamp} from '../colors/utils';
  6. export function createShader(
  7. gl: WebGLRenderingContext,
  8. type: WebGLRenderingContext['VERTEX_SHADER'] | WebGLRenderingContext['FRAGMENT_SHADER'],
  9. source: string
  10. ): WebGLShader {
  11. const shader = gl.createShader(type);
  12. if (!shader) {
  13. throw new Error('Could not create shader');
  14. }
  15. gl.shaderSource(shader, source);
  16. gl.compileShader(shader);
  17. const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  18. if (success) {
  19. return shader;
  20. }
  21. gl.deleteShader(shader);
  22. throw new Error('Failed to compile shader');
  23. }
  24. export function createProgram(
  25. gl: WebGLRenderingContext,
  26. vertexShader: WebGLShader,
  27. fragmentShader: WebGLShader
  28. ): WebGLProgram {
  29. const program = gl.createProgram();
  30. if (!program) {
  31. throw new Error('Could not create program');
  32. }
  33. gl.attachShader(program, vertexShader);
  34. gl.attachShader(program, fragmentShader);
  35. gl.linkProgram(program);
  36. const success = gl.getProgramParameter(program, gl.LINK_STATUS);
  37. if (success) {
  38. return program;
  39. }
  40. gl.deleteProgram(program);
  41. throw new Error('Failed to create program');
  42. }
  43. // Create a projection matrix with origins at 0,0 in top left corner, scaled to width/height
  44. export function makeProjectionMatrix(width: number, height: number): mat3 {
  45. const projectionMatrix = mat3.create();
  46. mat3.identity(projectionMatrix);
  47. mat3.translate(projectionMatrix, projectionMatrix, vec2.fromValues(-1, 1));
  48. mat3.scale(
  49. projectionMatrix,
  50. projectionMatrix,
  51. vec2.divide(vec2.create(), vec2.fromValues(2, -2), vec2.fromValues(width, height))
  52. );
  53. return projectionMatrix;
  54. }
  55. const canvasToDisplaySizeMap = new Map<HTMLCanvasElement, [number, number]>();
  56. function onResize(entries: ResizeObserverEntry[]) {
  57. for (const entry of entries) {
  58. let width;
  59. let height;
  60. let dpr = window.devicePixelRatio;
  61. // @ts-ignore use as a progressive enhancement, some browsers don't support this yet
  62. if (entry.devicePixelContentBoxSize) {
  63. // NOTE: Only this path gives the correct answer
  64. // The other paths are imperfect fallbacks
  65. // for browsers that don't provide anyway to do this
  66. // @ts-ignore
  67. width = entry.devicePixelContentBoxSize[0].inlineSize;
  68. // @ts-ignore
  69. height = entry.devicePixelContentBoxSize[0].blockSize;
  70. dpr = 1; // it's already in width and height
  71. } else if (entry.contentBoxSize) {
  72. if (entry.contentBoxSize[0]) {
  73. width = entry.contentBoxSize[0].inlineSize;
  74. height = entry.contentBoxSize[0].blockSize;
  75. } else {
  76. // @ts-ignore
  77. width = entry.contentBoxSize.inlineSize;
  78. // @ts-ignore
  79. height = entry.contentBoxSize.blockSize;
  80. }
  81. } else {
  82. width = entry.contentRect.width;
  83. height = entry.contentRect.height;
  84. }
  85. const displayWidth = Math.round(width * dpr);
  86. const displayHeight = Math.round(height * dpr);
  87. canvasToDisplaySizeMap.set(entry.target as HTMLCanvasElement, [
  88. displayWidth,
  89. displayHeight,
  90. ]);
  91. resizeCanvasToDisplaySize(entry.target as HTMLCanvasElement);
  92. }
  93. }
  94. export const watchForResize = (
  95. canvas: HTMLCanvasElement[],
  96. callback?: () => void
  97. ): ResizeObserver => {
  98. const handler: ResizeObserverCallback = entries => {
  99. onResize(entries);
  100. callback?.();
  101. };
  102. for (const c of canvas) {
  103. canvasToDisplaySizeMap.set(c, [c.width, c.height]);
  104. }
  105. const resizeObserver = new ResizeObserver(handler);
  106. try {
  107. // only call us of the number of device pixels changed
  108. canvas.forEach(c => {
  109. resizeObserver.observe(c, {box: 'device-pixel-content-box'});
  110. });
  111. } catch (ex) {
  112. // device-pixel-content-box is not supported so fallback to this
  113. canvas.forEach(c => {
  114. resizeObserver.observe(c, {box: 'content-box'});
  115. });
  116. }
  117. return resizeObserver;
  118. };
  119. export function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement): boolean {
  120. // Get the size the browser is displaying the canvas in device pixels.
  121. const size = canvasToDisplaySizeMap.get(canvas);
  122. if (!size) {
  123. const displayWidth = canvas.clientWidth * window.devicePixelRatio;
  124. const displayHeight = canvas.clientHeight * window.devicePixelRatio;
  125. canvas.width = displayWidth;
  126. canvas.height = displayHeight;
  127. return false;
  128. }
  129. const [displayWidth, displayHeight] = size;
  130. // Check if the canvas is not the same size.
  131. const needResize = canvas.width !== displayWidth || canvas.height !== displayHeight;
  132. if (needResize) {
  133. // Make the canvas the same size
  134. canvas.width = displayWidth;
  135. canvas.height = displayHeight;
  136. }
  137. return needResize;
  138. }
  139. export const Transform = {
  140. betweenRect(from: Rect, to: Rect): Rect {
  141. return new Rect(to.x, to.y, to.width / from.width, to.height / from.height);
  142. },
  143. transformMatrixBetweenRect(from: Rect, to: Rect): mat3 {
  144. return mat3.fromValues(
  145. to.width / from.width,
  146. 0,
  147. 0,
  148. 0,
  149. to.height / from.height,
  150. 0,
  151. to.x - from.x * (to.width / from.width),
  152. to.y - from.y * (to.height / from.height),
  153. 1
  154. );
  155. },
  156. };
  157. export class Rect {
  158. origin: vec2;
  159. size: vec2;
  160. constructor(x: number, y: number, width: number, height: number) {
  161. this.origin = vec2.fromValues(x, y);
  162. this.size = vec2.fromValues(width, height);
  163. }
  164. clone(): Rect {
  165. return Rect.From(this);
  166. }
  167. isValid(): boolean {
  168. return this.toMatrix().every(n => !isNaN(n));
  169. }
  170. isEmpty(): boolean {
  171. return this.width === 0 && this.height === 0;
  172. }
  173. static Empty(): Rect {
  174. return new Rect(0, 0, 0, 0);
  175. }
  176. static From(rect: Rect): Rect {
  177. return new Rect(rect.x, rect.y, rect.width, rect.height);
  178. }
  179. get x(): number {
  180. return this.origin[0];
  181. }
  182. get y(): number {
  183. return this.origin[1];
  184. }
  185. get width(): number {
  186. return this.size[0];
  187. }
  188. get height(): number {
  189. return this.size[1];
  190. }
  191. get left(): number {
  192. return this.x;
  193. }
  194. get right(): number {
  195. return this.left + this.width;
  196. }
  197. get top(): number {
  198. return this.y;
  199. }
  200. get bottom(): number {
  201. return this.top + this.height;
  202. }
  203. static decode(query: string | ReadonlyArray<string> | null | undefined): Rect | null {
  204. let maybeEncodedRect = query;
  205. if (typeof query === 'string') {
  206. maybeEncodedRect = query.split(',');
  207. }
  208. if (!Array.isArray(maybeEncodedRect)) {
  209. return null;
  210. }
  211. if (maybeEncodedRect.length !== 4) {
  212. return null;
  213. }
  214. const rect = new Rect(
  215. ...(maybeEncodedRect.map(p => parseFloat(p)) as [number, number, number, number])
  216. );
  217. if (rect.isValid()) {
  218. return rect;
  219. }
  220. return null;
  221. }
  222. static encode(rect: Rect): string {
  223. return rect.toString();
  224. }
  225. toString() {
  226. return [this.x, this.y, this.width, this.height].map(n => Math.round(n)).join(',');
  227. }
  228. toMatrix(): mat3 {
  229. const {width: w, height: h, x, y} = this;
  230. // it's easier to display a matrix as a 3x3 array. WebGl matrices are row first and not column first
  231. // https://webglfundamentals.org/webgl/lessons/webgl-matrix-vs-math.html
  232. // prettier-ignore
  233. return mat3.fromValues(
  234. w, 0, 0,
  235. 0, h, 0,
  236. x, y, 1
  237. )
  238. }
  239. hasIntersectionWith(other: Rect): boolean {
  240. const top = Math.max(this.top, other.top);
  241. const bottom = Math.max(top, Math.min(this.bottom, other.bottom));
  242. if (bottom - top === 0) {
  243. return false;
  244. }
  245. const left = Math.max(this.left, other.left);
  246. const right = Math.max(left, Math.min(this.right, other.right));
  247. if (right - left === 0) {
  248. return false;
  249. }
  250. return true;
  251. }
  252. containsX(vec: vec2): boolean {
  253. return vec[0] >= this.left && vec[0] <= this.right;
  254. }
  255. containsY(vec: vec2): boolean {
  256. return vec[1] >= this.top && vec[1] <= this.bottom;
  257. }
  258. contains(vec: vec2): boolean {
  259. return this.containsX(vec) && this.containsY(vec);
  260. }
  261. containsRect(rect: Rect): boolean {
  262. return (
  263. // left bound
  264. this.left <= rect.left &&
  265. // right bound
  266. rect.right <= this.right &&
  267. // top bound
  268. this.top <= rect.top &&
  269. // bottom bound
  270. rect.bottom <= this.bottom
  271. );
  272. }
  273. leftOverlapsWith(rect: Rect): boolean {
  274. return rect.left <= this.left && rect.right >= this.left;
  275. }
  276. rightOverlapsWith(rect: Rect): boolean {
  277. return this.right >= rect.left && this.right <= rect.right;
  278. }
  279. overlapsX(other: Rect): boolean {
  280. return this.left <= other.right && this.right >= other.left;
  281. }
  282. overlapsY(other: Rect): boolean {
  283. return this.top <= other.bottom && this.bottom >= other.top;
  284. }
  285. overlaps(other: Rect): boolean {
  286. return this.overlapsX(other) && this.overlapsY(other);
  287. }
  288. transformRect(transform: mat3): Rect {
  289. const x = this.x * transform[0] + this.y * transform[3] + transform[6];
  290. const y = this.x * transform[1] + this.y * transform[4] + transform[7];
  291. const width = this.width * transform[0] + this.height * transform[3];
  292. const height = this.width * transform[1] + this.height * transform[4];
  293. return new Rect(
  294. x + (width < 0 ? width : 0),
  295. y + (height < 0 ? height : 0),
  296. Math.abs(width),
  297. Math.abs(height)
  298. );
  299. }
  300. /**
  301. * Returns a transform that inverts the y axis within the rect.
  302. * This causes the bottom of the rect to be the top of the rect and vice versa.
  303. */
  304. invertYTransform(): mat3 {
  305. return mat3.fromValues(1, 0, 0, 0, -1, 0, 0, this.y * 2 + this.height, 1);
  306. }
  307. withHeight(height: number): Rect {
  308. return new Rect(this.x, this.y, this.width, height);
  309. }
  310. withWidth(width: number): Rect {
  311. return new Rect(this.x, this.y, width, this.height);
  312. }
  313. withX(x: number): Rect {
  314. return new Rect(x, this.y, this.width, this.height);
  315. }
  316. withY(y: number) {
  317. return new Rect(this.x, y, this.width, this.height);
  318. }
  319. toBounds(): [number, number, number, number] {
  320. return [this.x, this.y, this.x + this.width, this.y + this.height];
  321. }
  322. toArray(): [number, number, number, number] {
  323. return [this.x, this.y, this.width, this.height];
  324. }
  325. between(to: Rect): Rect {
  326. return new Rect(to.x, to.y, to.width / this.width, to.height / this.height);
  327. }
  328. translate(x: number, y: number): Rect {
  329. return new Rect(x, y, this.width, this.height);
  330. }
  331. translateX(x: number): Rect {
  332. return new Rect(x, this.y, this.width, this.height);
  333. }
  334. translateY(y: number): Rect {
  335. return new Rect(this.x, y, this.width, this.height);
  336. }
  337. scaleX(x: number): Rect {
  338. return new Rect(this.x, this.y, this.width * x, this.height);
  339. }
  340. scaleY(y: number): Rect {
  341. return new Rect(this.x, this.y, this.width, this.height * y);
  342. }
  343. scale(x: number, y: number): Rect {
  344. return new Rect(this.x * x, this.y * y, this.width * x, this.height * y);
  345. }
  346. scaleOriginBy(x: number, y: number): Rect {
  347. return new Rect(this.x * x, this.y * y, this.width, this.height);
  348. }
  349. scaledBy(x: number, y: number): Rect {
  350. return new Rect(this.x, this.y, this.width * x, this.height * y);
  351. }
  352. equals(rect: Rect): boolean {
  353. if (this.x !== rect.x) {
  354. return false;
  355. }
  356. if (this.y !== rect.y) {
  357. return false;
  358. }
  359. if (this.width !== rect.width) {
  360. return false;
  361. }
  362. if (this.height !== rect.height) {
  363. return false;
  364. }
  365. return true;
  366. }
  367. notEqualTo(rect: Rect): boolean {
  368. return !this.equals(rect);
  369. }
  370. }
  371. function getContext(canvas: HTMLCanvasElement, context: '2d'): CanvasRenderingContext2D;
  372. function getContext(canvas: HTMLCanvasElement, context: 'webgl'): WebGLRenderingContext;
  373. function getContext(canvas: HTMLCanvasElement, context: string): RenderingContext {
  374. const ctx =
  375. context === 'webgl'
  376. ? canvas.getContext(context, {antialias: false})
  377. : canvas.getContext(context);
  378. if (!ctx) {
  379. throw new Error(`Could not get context ${context}`);
  380. }
  381. return ctx;
  382. }
  383. // Exporting this like this instead of writing export function for each overload as
  384. // it breaks the lines and makes it harder to read.
  385. export {getContext};
  386. export function measureText(string: string, ctx?: CanvasRenderingContext2D): Rect {
  387. if (!string) {
  388. return Rect.Empty();
  389. }
  390. const context = ctx || getContext(document.createElement('canvas'), '2d');
  391. const measures = context.measureText(string);
  392. return new Rect(
  393. 0,
  394. 0,
  395. measures.width,
  396. // https://stackoverflow.com/questions/1134586/how-can-you-find-the-height-of-text-on-an-html-canvas
  397. measures.actualBoundingBoxAscent + measures.actualBoundingBoxDescent
  398. );
  399. }
  400. /** Find closest min and max value to target */
  401. export function findRangeBinarySearch(
  402. {low, high}: {high: number; low: number},
  403. fn: (val: number) => number,
  404. target: number,
  405. precision = 1
  406. ): [number, number] {
  407. // eslint-disable-next-line
  408. while (true) {
  409. if (high - low <= precision) {
  410. return [low, high];
  411. }
  412. const mid = (high + low) / 2;
  413. if (fn(mid) < target) {
  414. low = mid;
  415. } else {
  416. high = mid;
  417. }
  418. }
  419. }
  420. export function formatColorForFrame(
  421. frame: FlamegraphFrame,
  422. renderer: FlamegraphRenderer
  423. ): string {
  424. const color = renderer.getColorForFrame(frame);
  425. if (color.length === 4) {
  426. return `rgba(${color
  427. .slice(0, 3)
  428. .map(n => n * 255)
  429. .join(',')}, ${color[3]})`;
  430. }
  431. return `rgba(${color.map(n => n * 255).join(',')}, 1.0)`;
  432. }
  433. export const ELLIPSIS = '\u2026';
  434. type TrimTextCenter = {
  435. end: number;
  436. length: number;
  437. start: number;
  438. text: string;
  439. };
  440. export function trimTextCenter(text: string, low: number): TrimTextCenter {
  441. if (low >= text.length) {
  442. return {
  443. text,
  444. start: 0,
  445. end: 0,
  446. length: 0,
  447. };
  448. }
  449. const prefixLength = Math.floor(low / 2);
  450. // Use 1 character less than the low value to account for ellipsis and favor displaying the prefix
  451. const postfixLength = low - prefixLength - 1;
  452. const start = prefixLength;
  453. const end = Math.floor(text.length - postfixLength + ELLIPSIS.length);
  454. const trimText = `${text.substring(0, start)}${ELLIPSIS}${text.substring(end)}`;
  455. return {
  456. text: trimText,
  457. start,
  458. end,
  459. length: end - start,
  460. };
  461. }
  462. export function computeClampedConfigView(
  463. newConfigView: Rect,
  464. {width, height}: {height: {max: number; min: number}; width: {max: number; min: number}}
  465. ) {
  466. if (!newConfigView.isValid()) {
  467. throw new Error(newConfigView.toString());
  468. }
  469. const clampedWidth = clamp(newConfigView.width, width.min, width.max);
  470. const clampedHeight = clamp(newConfigView.height, height.min, height.max);
  471. const maxX = width.max - clampedWidth;
  472. const maxY = clampedHeight >= height.max ? 0 : height.max - clampedHeight;
  473. const clampedX = clamp(newConfigView.x, 0, maxX);
  474. const clampedY = clamp(newConfigView.y, 0, maxY);
  475. return new Rect(clampedX, clampedY, clampedWidth, clampedHeight);
  476. }
  477. /**
  478. * computeHighlightedBounds determines if a supplied boundary should be reduced in size
  479. * or shifted based on the results of a trim operation
  480. */
  481. export function computeHighlightedBounds(
  482. bounds: Fuse.RangeTuple,
  483. trim: TrimTextCenter
  484. ): Fuse.RangeTuple {
  485. if (!trim.length) {
  486. return bounds;
  487. }
  488. const isStartBetweenTrim = bounds[0] >= trim.start && bounds[0] <= trim.end;
  489. const isEndBetweenTrim = bounds[1] >= trim.start && bounds[1] <= trim.end;
  490. const isFullyTruncated = isStartBetweenTrim && isEndBetweenTrim;
  491. // example:
  492. // -[UIScrollView _smoothScrollDisplayLink:]
  493. // "smooth" in "-[UIScrollView _…ScrollDisplayLink:]"
  494. // ^^
  495. if (isFullyTruncated) {
  496. return [trim.start, trim.start + 1];
  497. }
  498. if (bounds[0] < trim.start) {
  499. // "ScrollView" in '-[UIScrollView _sm…rollDisplayLink:]'
  500. // ^--------^
  501. if (bounds[1] < trim.start) {
  502. return [bounds[0], bounds[1]];
  503. }
  504. // "smoothScroll" in -[UIScrollView _smooth…DisplayLink:]'
  505. // ^-----^
  506. if (isEndBetweenTrim) {
  507. return [bounds[0], trim.start + 1];
  508. }
  509. // "smoothScroll" in -[UIScrollView _sm…llDisplayLink:]'
  510. // ^---^
  511. if (bounds[1] > trim.end) {
  512. return [bounds[0], bounds[1] - trim.length + 1];
  513. }
  514. }
  515. // "smoothScroll" in -[UIScrollView _…scrollDisplayLink:]'
  516. // ^-----^
  517. if (isStartBetweenTrim && bounds[1] > trim.end) {
  518. return [trim.start, bounds[1] - trim.length + 1];
  519. }
  520. // "display" in -[UIScrollView _…scrollDisplayLink:]'
  521. // ^-----^
  522. if (bounds[0] > trim.end) {
  523. return [bounds[0] - trim.length + 1, bounds[1] - trim.length + 1];
  524. }
  525. throw new Error(`Unhandled case: ${JSON.stringify(bounds)} ${trim}`);
  526. }
  527. export function computeConfigViewWithStategy(
  528. strategy: 'min' | 'exact',
  529. view: Rect,
  530. frame: Rect
  531. ): Rect {
  532. if (strategy === 'exact') {
  533. return frame.withHeight(view.height);
  534. }
  535. if (strategy === 'min') {
  536. if (view.width <= frame.width) {
  537. // If view width <= frame width, we need to zoom out, so the behavior is the
  538. // same as if we were using 'exact'
  539. return frame.withHeight(view.height);
  540. }
  541. if (view.containsRect(frame)) {
  542. // If frame is in view, do nothing
  543. return view;
  544. }
  545. let offset = view.clone();
  546. if (frame.left < view.left) {
  547. // If frame is to the left of the view, translate it left
  548. // to frame.x so that start of the frame is in the view
  549. offset = offset.withX(frame.x);
  550. } else if (frame.right > view.right) {
  551. // If the right boundary of a frame is outside of the view, translate the view
  552. // by the difference between the right edge of the frame and the right edge of the view
  553. offset = view.withX(offset.x + frame.right - offset.right);
  554. }
  555. if (frame.bottom < view.top) {
  556. // If frame is above the view, translate view to top of frame
  557. offset = offset.withY(frame.top);
  558. } else if (frame.bottom > view.bottom) {
  559. // If frame is below the view, translate view by the difference
  560. // of the bottom edge of the frame and the view
  561. offset = offset.translateY(offset.y + frame.bottom - offset.bottom);
  562. }
  563. return offset;
  564. }
  565. return frame.withHeight(view.height);
  566. }