profiler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. let plugin = new arm.Plugin();
  2. let h1 = new zui.Handle();
  3. let first = true;
  4. let lastTime = 0.0;
  5. let frameTime = 0.0;
  6. let totalTime = 0.0;
  7. let frames = 0;
  8. let frameTimeAvg = 0.0;
  9. let graph = null;
  10. let graphA = null;
  11. let graphB = null;
  12. let lrow = [1/2, 1/2];
  13. plugin.drawUI = function(ui) {
  14. if (ui.panel(h1, "Profiler")) {
  15. let avg = Math.round(frameTimeAvg * 10000) / 10;
  16. let fpsAvg = avg > 0 ? Math.round(1000 / avg) : 0;
  17. if (first) {
  18. first = false;
  19. iron.App.notifyOnRender2D(tick);
  20. }
  21. if (graph != null) ui.image(graph);
  22. ui.indent();
  23. ui.row(lrow);
  24. ui.text('Frame');
  25. ui.text(`${avg} ms / ${fpsAvg} fps`, 2); // Align.Right
  26. ui.unindent();
  27. }
  28. }
  29. let updateGraph = function() {
  30. if (graph === null) {
  31. graphA = arm.Image.createRenderTarget(280, 33);
  32. graphB = arm.Image.createRenderTarget(280, 33);
  33. graph = graphA;
  34. }
  35. else graph = graph === graphA ? graphB : graphA;
  36. let graphPrev = graph === graphA ? graphB : graphA;
  37. let g2 = graph.get_g2();
  38. g2.begin(true, 0x00000000);
  39. g2.set_color(0xffffffff);
  40. g2.drawImage(graphPrev, -3, 0);
  41. let avg = Math.round(frameTimeAvg * 1000);
  42. let miss = avg > 16.7 ? (avg - 16.7) / 16.7 : 0.0;
  43. g2.set_color(arm.colorFromFloats(miss, 1 - miss, 0, 1.0));
  44. g2.fillRect(280 - 3, 33 - avg, 3, avg);
  45. g2.set_color(0xff000000);
  46. g2.fillRect(280 - 3, 33 - 17, 3, 1);
  47. g2.end();
  48. }
  49. let tick = function(g2) {
  50. totalTime += frameTime;
  51. frames++;
  52. if (totalTime > 1.0) {
  53. arm.UITrait.inst.hwnd.redraws = 1;
  54. frameTimeAvg = totalTime / frames;
  55. totalTime = 0;
  56. frames = 0;
  57. g2.end();
  58. updateGraph();
  59. g2.begin(false);
  60. }
  61. frameTime = arm.Scheduler.realTime() - lastTime;
  62. lastTime = arm.Scheduler.realTime();
  63. }