summaryrefslogtreecommitdiffstats
path: root/gfx/wr/webrender/src/profiler.rs
blob: ccf86e86470a3e579453bee6fd9c5f2f2f33f0e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! # Overlay profiler
//!
//! ## Profiler UI string syntax
//!
//! Comma-separated list of of tokens with trailing and leading spaces trimmed.
//! Each tokens can be:
//! - A counter name with an optional prefix. The name corresponds to the displayed name (see the
//!   counters vector below.
//!   - By default (no prefix) the counter is shown as average + max over half a second.
//!   - With a '#' prefix the counter is shown as a graph.
//!   - With a '*' prefix the counter is shown as a change indicator.
//!   - Some special counters such as GPU time queries have specific visualizations ignoring prefixes.
//! - A preset name to append the preset to the UI (see PROFILER_PRESETS).
//! - An empty token to insert a bit of vertical space.
//! - A '|' token to start a new column.
//! - A '_' token to start a new row.

use api::{ColorF, ColorU};
use glyph_rasterizer::profiler::GlyphRasterizeProfiler;
use crate::renderer::DebugRenderer;
use crate::device::query::GpuTimer;
use euclid::{Point2D, Rect, Size2D, vec2, default};
use crate::internal_types::FastHashMap;
use crate::renderer::{FullFrameStats, MAX_VERTEX_TEXTURE_WIDTH, init::wr_has_been_initialized};
use api::units::DeviceIntSize;
use std::collections::vec_deque::VecDeque;
use std::fmt::{Write, Debug};
use std::f32;
use std::ops::Range;
use std::time::Duration;
use time::precise_time_ns;

macro_rules! set_text {
    ($dst:expr, $($arg:tt)*) => {
        $dst.clear();
        write!($dst, $($arg)*).unwrap();
    };
}

const GRAPH_WIDTH: f32 = 1024.0;
const GRAPH_HEIGHT: f32 = 320.0;
const GRAPH_PADDING: f32 = 8.0;
const GRAPH_FRAME_HEIGHT: f32 = 16.0;
const PROFILE_SPACING: f32 = 15.0;
const PROFILE_PADDING: f32 = 10.0;
const BACKGROUND_COLOR: ColorU = ColorU { r: 20, g: 20, b: 20, a: 220 };

const ONE_SECOND_NS: u64 = 1_000_000_000;

/// Profiler UI string presets. Defined in the profiler UI string syntax, can contain other presets.
static PROFILER_PRESETS: &'static[(&'static str, &'static str)] = &[
    // Default view, doesn't show everything, but still shows quite a bit.
    (&"Default", &"FPS,|,Slow indicators,_,Time graphs,|,Frame times, ,Transaction times, ,Frame stats, ,Memory, ,Interners,_,GPU time queries,_,Paint phase graph"),
    // Smaller, less intrusive overview
    (&"Compact", &"FPS, ,Frame times, ,Frame stats"),
    // Even less intrusive, only slow transactions and frame indicators.
    (&"Slow indicators", &"*Slow transaction,*Slow frame"),

    // Counters:

    // Timing information for per layout transaction stages.
    (&"Transaction times", &"DisplayList,Scene building,Content send,API send"),
    // Timing information for per-frame stages.
    (&"Frame times", &"Frame CPU total,Frame building,Visibility,Prepare,Batching,Glyph resolve,Texture cache update,Shader build time,Renderer,GPU"),
    // Stats about the content of the frame.
    (&"Frame stats", &"Primitives,Visible primitives,Draw calls,Vertices,Color passes,Alpha passes,Rendered picture tiles,Rasterized glyphs"),
    // Texture cache allocation stats.
    (&"Texture cache stats", &"Atlas textures mem, Standalone textures mem, Picture tiles mem, Render targets mem, Depth targets mem, Atlas items mem,
        Texture cache standalone pressure, Texture cache eviction count, Texture cache youngest evicted, ,
        Atlas RGBA8 linear pixels, Atlas RGBA8 glyphs pixels, Atlas A8 glyphs pixels, Atlas A8 pixels, Atlas A16 pixels, Atlas RGBA8 nearest pixels,
        Atlas RGBA8 linear textures, Atlas RGBA8 glyphs textures, Atlas A8 glyphs textures, Atlas A8 textures, Atlas A16 textures, Atlas RGBA8 nearest textures,
        Atlas RGBA8 linear pressure, Atlas RGBA8 glyphs pressure, Atlas A8 glyphs pressure, Atlas A8 pressure, Atlas A16 pressure, Atlas RGBA8 nearest pressure,"
    ),
    // Graphs to investigate driver overhead of texture cache updates.
    (&"Texture upload perf", &"#Texture cache update,#Texture cache upload, ,#Staging CPU allocation,#Staging GPU allocation,#Staging CPU copy,#Staging GPU copy,#Upload time, ,#Upload copy batches,#Rasterized glyphs, ,#Cache texture creation,#Cache texture deletion"),

    // Graphs:

    // Graph overview of time spent in WebRender's main stages.
    (&"Time graphs", &"#DisplayList,#Scene building,#Blob rasterization, ,#Frame CPU total,#Frame building,#Renderer,#Texture cache update, ,#GPU,"),
    // Useful when investigating render backend bottlenecks.
    (&"Backend graphs", &"#Frame building, #Visibility, #Prepare, #Batching, #Glyph resolve"),
    // Useful when investigating renderer bottlenecks.
    (&"Renderer graphs", &"#Rendered picture tiles,#Draw calls,#Rasterized glyphs,#Texture uploads,#Texture uploads mem, ,#Texture cache update,#Renderer,"),

    // Misc:

    (&"Memory", &"Image templates,Image templates mem,Font templates,Font templates mem,DisplayList mem,Picture tiles mem"),
    (&"Interners", "Interned primitives,Interned clips,Interned pictures,Interned text runs,Interned normal borders,Interned image borders,Interned images,Interned YUV images,Interned line decorations,Interned linear gradients,Interned radial gradients,Interned conic gradients,Interned filter data,Interned backdrops"),
    // Gpu sampler queries (need the pref gfx.webrender.debug.gpu-sampler-queries).
    (&"GPU samplers", &"Alpha targets samplers,Transparent pass samplers,Opaque pass samplers,Total samplers"),

    (&"Render reasons", &"Reason scene, Reason animated property, Reason resource update, Reason async image, Reason clear resources, Reason APZ, Reason resize, Reason widget, Reason cache flush, Reason snapshot, Reason resource hook, Reason config change, Reason content sync, Reason flush, On vsync, Reason testing, Reason other"),
];

fn find_preset(name: &str) -> Option<&'static str> {
    for preset in PROFILER_PRESETS {
        if preset.0 == name {
            return Some(preset.1);
        }
    }

    None
}

// The indices here must match the PROFILE_COUNTERS array (checked at runtime).
pub const FRAME_BUILDING_TIME: usize = 0;
pub const FRAME_VISIBILITY_TIME: usize = 1;
pub const FRAME_PREPARE_TIME: usize = 2;
pub const FRAME_BATCHING_TIME: usize = 3;

pub const RENDERER_TIME: usize = 4;
pub const TOTAL_FRAME_CPU_TIME: usize = 5;
pub const GPU_TIME: usize = 6;

pub const CONTENT_SEND_TIME: usize = 7;
pub const API_SEND_TIME: usize = 8;

pub const DISPLAY_LIST_BUILD_TIME: usize = 9;
pub const DISPLAY_LIST_MEM: usize = 10;

pub const SCENE_BUILD_TIME: usize = 11;

pub const SLOW_FRAME: usize = 12;
pub const SLOW_TXN: usize = 13;

pub const FRAME_TIME: usize = 14;

pub const TEXTURE_UPLOADS: usize = 15;
pub const TEXTURE_UPLOADS_MEM: usize = 16;
pub const TEXTURE_CACHE_UPDATE_TIME: usize = 17;
pub const CPU_TEXTURE_ALLOCATION_TIME: usize = 18;
pub const STAGING_TEXTURE_ALLOCATION_TIME: usize = 19;
pub const UPLOAD_CPU_COPY_TIME: usize = 20;
pub const UPLOAD_GPU_COPY_TIME: usize = 21;
pub const UPLOAD_TIME: usize = 22;
pub const UPLOAD_NUM_COPY_BATCHES: usize = 23;
pub const TOTAL_UPLOAD_TIME: usize = 24;
pub const CREATE_CACHE_TEXTURE_TIME: usize = 25;
pub const DELETE_CACHE_TEXTURE_TIME: usize = 26;
pub const GPU_CACHE_UPLOAD_TIME: usize = 27;

pub const RASTERIZED_BLOBS: usize = 28;
pub const RASTERIZED_BLOB_TILES: usize = 29;
pub const RASTERIZED_BLOBS_PX: usize = 30;
pub const BLOB_RASTERIZATION_TIME: usize = 31;

pub const RASTERIZED_GLYPHS: usize = 32;
pub const GLYPH_RESOLVE_TIME: usize = 33;

pub const DRAW_CALLS: usize = 34;
pub const VERTICES: usize = 35;
pub const PRIMITIVES: usize = 36;
pub const VISIBLE_PRIMITIVES: usize = 37;

pub const USED_TARGETS: usize = 38;
pub const CREATED_TARGETS: usize = 39;
pub const PICTURE_CACHE_SLICES: usize = 40;

pub const COLOR_PASSES: usize = 41;
pub const ALPHA_PASSES: usize = 42;
pub const PICTURE_TILES: usize = 43;
pub const RENDERED_PICTURE_TILES: usize = 44;

pub const FONT_TEMPLATES: usize = 45;
pub const FONT_TEMPLATES_MEM: usize = 46;
pub const IMAGE_TEMPLATES: usize = 47;
pub const IMAGE_TEMPLATES_MEM: usize = 48;

pub const GPU_CACHE_ROWS_TOTAL: usize = 49;
pub const GPU_CACHE_ROWS_UPDATED: usize = 50;
pub const GPU_CACHE_BLOCKS_TOTAL: usize = 51;
pub const GPU_CACHE_BLOCKS_UPDATED: usize = 52;
pub const GPU_CACHE_BLOCKS_SAVED: usize = 53;

// Atlas items represents the area occupied by items in the cache textures.
// The actual texture memory allocated is ATLAS_TEXTURES_MEM.
pub const ATLAS_ITEMS_MEM: usize = 54;
pub const ATLAS_A8_PIXELS: usize = 55;
pub const ATLAS_A8_TEXTURES: usize = 56;
pub const ATLAS_A16_PIXELS: usize = 57;
pub const ATLAS_A16_TEXTURES: usize = 58;
pub const ATLAS_RGBA8_LINEAR_PIXELS: usize = 59;
pub const ATLAS_RGBA8_LINEAR_TEXTURES: usize = 60;
pub const ATLAS_RGBA8_NEAREST_PIXELS: usize = 61;
pub const ATLAS_RGBA8_NEAREST_TEXTURES: usize = 62;
pub const ATLAS_RGBA8_GLYPHS_PIXELS: usize = 63;
pub const ATLAS_RGBA8_GLYPHS_TEXTURES: usize = 64;
pub const ATLAS_A8_GLYPHS_PIXELS: usize = 65;
pub const ATLAS_A8_GLYPHS_TEXTURES: usize = 66;
pub const ATLAS_COLOR8_LINEAR_PRESSURE: usize = 67;
pub const ATLAS_COLOR8_NEAREST_PRESSURE: usize = 68;
pub const ATLAS_COLOR8_GLYPHS_PRESSURE: usize = 69;
pub const ATLAS_ALPHA8_PRESSURE: usize = 70;
pub const ATLAS_ALPHA8_GLYPHS_PRESSURE: usize = 71;
pub const ATLAS_ALPHA16_PRESSURE: usize = 72;
pub const ATLAS_STANDALONE_PRESSURE: usize = 73;

pub const TEXTURE_CACHE_EVICTION_COUNT: usize = 74;
pub const TEXTURE_CACHE_YOUNGEST_EVICTION: usize = 75;
pub const EXTERNAL_IMAGE_BYTES: usize = 76;
pub const ATLAS_TEXTURES_MEM: usize = 77;
pub const STANDALONE_TEXTURES_MEM: usize = 78;
pub const PICTURE_TILES_MEM: usize = 79;
pub const RENDER_TARGET_MEM: usize = 80;

pub const ALPHA_TARGETS_SAMPLERS: usize = 81;
pub const TRANSPARENT_PASS_SAMPLERS: usize = 82;
pub const OPAQUE_PASS_SAMPLERS: usize = 83;
pub const TOTAL_SAMPLERS: usize = 84;

pub const INTERNED_PRIMITIVES: usize = 85;
pub const INTERNED_CLIPS: usize = 86;
pub const INTERNED_TEXT_RUNS: usize = 87;
pub const INTERNED_NORMAL_BORDERS: usize = 88;
pub const INTERNED_IMAGE_BORDERS: usize = 89;
pub const INTERNED_IMAGES: usize = 90;
pub const INTERNED_YUV_IMAGES: usize = 91;
pub const INTERNED_LINE_DECORATIONS: usize = 92;
pub const INTERNED_LINEAR_GRADIENTS: usize = 93;
pub const INTERNED_RADIAL_GRADIENTS: usize = 94;
pub const INTERNED_CONIC_GRADIENTS: usize = 95;
pub const INTERNED_PICTURES: usize = 96;
pub const INTERNED_FILTER_DATA: usize = 97;
pub const INTERNED_BACKDROP_CAPTURES: usize = 98;
pub const INTERNED_BACKDROP_RENDERS: usize = 99;
pub const INTERNED_POLYGONS: usize = 100;

pub const DEPTH_TARGETS_MEM: usize = 101;

pub const SHADER_BUILD_TIME: usize = 102;

pub const RENDER_REASON_FIRST: usize = 103;
pub const RENDER_REASON_SCENE: usize = 103;
pub const RENDER_REASON_ANIMATED_PROPERTY: usize = 104;
pub const RENDER_REASON_RESOURCE_UPDATE: usize = 105;
pub const RENDER_REASON_ASYNC_IMAGE: usize = 106;
pub const RENDER_REASON_CLEAR_RESOURCES: usize = 107;
pub const RENDER_REASON_APZ: usize = 108;
pub const RENDER_REASON_RESIZE: usize = 109;
pub const RENDER_REASON_WIDGET: usize = 110;
pub const RENDER_REASON_TEXTURE_CACHE_FLUSH: usize = 111;
pub const RENDER_REASON_SNAPSHOT: usize = 112;
pub const RENDER_REASON_POST_RESOURCE_UPDATE_HOOKS: usize = 113;
pub const RENDER_REASON_CONFIG_CHANGE: usize = 114;
pub const RENDER_REASON_CONTENT_SYNC: usize = 115;
pub const RENDER_REASON_FLUSH: usize = 116;
pub const RENDER_REASON_TESTING: usize = 117;
pub const RENDER_REASON_OTHER: usize = 118;
pub const RENDER_REASON_VSYNC: usize = 119;

pub const TEXTURES_CREATED: usize = 120;
pub const TEXTURES_DELETED: usize = 121;

pub const NUM_PROFILER_EVENTS: usize = 122;

pub struct Profiler {
    counters: Vec<Counter>,
    gpu_frames: ProfilerFrameCollection,
    frame_stats: ProfilerFrameCollection,

    start: u64,
    avg_over_period: u64,
    num_graph_samples: usize,

    // For FPS computation. Updated in update().
    frame_timestamps_within_last_second: Vec<u64>,

    ui: Vec<Item>,
}

impl Profiler {
    pub fn new() -> Self {

        fn float(name: &'static str, unit: &'static str, index: usize, expected: Expected<f64>) -> CounterDescriptor {
            CounterDescriptor { name, unit, show_as: ShowAs::Float, index, expected }
        }

        fn int(name: &'static str, unit: &'static str, index: usize, expected: Expected<i64>) -> CounterDescriptor {
            CounterDescriptor { name, unit, show_as: ShowAs::Int, index, expected: expected.into_float() }
        }

        // Not in the list below:
        // - "GPU time queries" shows the details of the GPU time queries if selected as a graph.
        // - "GPU cache bars" shows some info about the GPU cache.

        // TODO: This should be a global variable but to keep things readable we need to be able to
        // use match in const fn which isn't supported by the current rustc version in gecko's build
        // system.
        let profile_counters = &[
            float("Frame building", "ms", FRAME_BUILDING_TIME, expected(0.0..6.0).avg(0.0..3.0)),
            float("Visibility", "ms", FRAME_VISIBILITY_TIME, expected(0.0..3.0).avg(0.0..2.0)),
            float("Prepare", "ms", FRAME_PREPARE_TIME, expected(0.0..3.0).avg(0.0..2.0)),
            float("Batching", "ms", FRAME_BATCHING_TIME, expected(0.0..3.0).avg(0.0..2.0)),

            float("Renderer", "ms", RENDERER_TIME, expected(0.0..8.0).avg(0.0..5.0)),
            float("Frame CPU total", "ms", TOTAL_FRAME_CPU_TIME, expected(0.0..15.0).avg(0.0..6.0)),
            float("GPU", "ms", GPU_TIME, expected(0.0..15.0).avg(0.0..8.0)),

            float("Content send", "ms", CONTENT_SEND_TIME, expected(0.0..1.0).avg(0.0..1.0)),
            float("API send", "ms", API_SEND_TIME, expected(0.0..1.0).avg(0.0..0.4)),
            float("DisplayList", "ms", DISPLAY_LIST_BUILD_TIME, expected(0.0..5.0).avg(0.0..3.0)),
            float("DisplayList mem", "MB", DISPLAY_LIST_MEM, expected(0.0..20.0)),
            float("Scene building", "ms", SCENE_BUILD_TIME, expected(0.0..4.0).avg(0.0..3.0)),

            float("Slow frame", "", SLOW_FRAME, expected(0.0..0.0)),
            float("Slow transaction", "", SLOW_TXN, expected(0.0..0.0)),

            float("Frame", "ms", FRAME_TIME, Expected::none()),

            int("Texture uploads", "", TEXTURE_UPLOADS, expected(0..10)),
            float("Texture uploads mem", "MB", TEXTURE_UPLOADS_MEM, expected(0.0..10.0)),
            float("Texture cache update", "ms", TEXTURE_CACHE_UPDATE_TIME, expected(0.0..3.0)),
            float("Staging CPU allocation", "ms", CPU_TEXTURE_ALLOCATION_TIME, Expected::none()),
            float("Staging GPU allocation", "ms", STAGING_TEXTURE_ALLOCATION_TIME, Expected::none()),
            float("Staging CPU copy", "ms", UPLOAD_CPU_COPY_TIME, Expected::none()),
            float("Staging GPU copy", "ms", UPLOAD_GPU_COPY_TIME, Expected::none()),
            float("Upload time", "ms", UPLOAD_TIME, Expected::none()),
            int("Upload copy batches", "", UPLOAD_NUM_COPY_BATCHES, Expected::none()),
            float("Texture cache upload", "ms", TOTAL_UPLOAD_TIME, expected(0.0..5.0)),
            float("Cache texture creation", "ms", CREATE_CACHE_TEXTURE_TIME, expected(0.0..2.0)),
            float("Cache texture deletion", "ms", DELETE_CACHE_TEXTURE_TIME, expected(0.0..1.0)),
            float("GPU cache upload", "ms", GPU_CACHE_UPLOAD_TIME, expected(0.0..2.0)),

            int("Rasterized blobs", "", RASTERIZED_BLOBS, expected(0..15)),
            int("Rasterized blob tiles", "", RASTERIZED_BLOB_TILES, expected(0..15)),
            int("Rasterized blob pixels", "px", RASTERIZED_BLOBS_PX, expected(0..300_000)),
            float("Blob rasterization", "ms", BLOB_RASTERIZATION_TIME, expected(0.0..8.0)),

            int("Rasterized glyphs", "", RASTERIZED_GLYPHS, expected(0..15)),
            float("Glyph resolve", "ms", GLYPH_RESOLVE_TIME, expected(0.0..4.0)),

            int("Draw calls", "", DRAW_CALLS, expected(1..120).avg(1..90)),
            int("Vertices", "", VERTICES, expected(10..5000)),
            int("Primitives", "", PRIMITIVES, expected(10..5000)),
            int("Visible primitives", "", VISIBLE_PRIMITIVES, expected(1..5000)),

            int("Used targets", "", USED_TARGETS, expected(1..4)),
            int("Created targets", "", CREATED_TARGETS, expected(0..3)),
            int("Picture cache slices", "", PICTURE_CACHE_SLICES, expected(0..5)),

            int("Color passes", "", COLOR_PASSES, expected(1..4)),
            int("Alpha passes", "", ALPHA_PASSES, expected(0..3)),
            int("Picture tiles", "", PICTURE_TILES, expected(0..15)),
            int("Rendered picture tiles", "", RENDERED_PICTURE_TILES, expected(0..5)),

            int("Font templates", "", FONT_TEMPLATES, expected(0..40)),
            float("Font templates mem", "MB", FONT_TEMPLATES_MEM, expected(0.0..20.0)),
            int("Image templates", "", IMAGE_TEMPLATES, expected(0..100)),
            float("Image templates mem", "MB", IMAGE_TEMPLATES_MEM, expected(0.0..50.0)),

            int("GPU cache rows total", "", GPU_CACHE_ROWS_TOTAL, expected(1..50)),
            int("GPU cache rows updated", "", GPU_CACHE_ROWS_UPDATED, expected(0..25)),
            int("GPU blocks total", "", GPU_CACHE_BLOCKS_TOTAL, expected(1..65_000)),
            int("GPU blocks updated", "", GPU_CACHE_BLOCKS_UPDATED, expected(0..1000)),
            int("GPU blocks saved", "", GPU_CACHE_BLOCKS_SAVED, expected(0..50_000)),

            float("Atlas items mem", "MB", ATLAS_ITEMS_MEM, expected(0.0..100.0)),
            int("Atlas A8 pixels", "px", ATLAS_A8_PIXELS, expected(0..1_000_000)),
            int("Atlas A8 textures", "", ATLAS_A8_TEXTURES, expected(0..2)),
            int("Atlas A16 pixels", "px", ATLAS_A16_PIXELS, expected(0..260_000)),
            int("Atlas A16 textures", "", ATLAS_A16_TEXTURES, expected(0..2)),
            int("Atlas RGBA8 linear pixels", "px", ATLAS_RGBA8_LINEAR_PIXELS, expected(0..8_000_000)),
            int("Atlas RGBA8 linear textures", "", ATLAS_RGBA8_LINEAR_TEXTURES, expected(0..3)),
            int("Atlas RGBA8 nearest pixels", "px", ATLAS_RGBA8_NEAREST_PIXELS, expected(0..260_000)),
            int("Atlas RGBA8 nearest textures", "", ATLAS_RGBA8_NEAREST_TEXTURES, expected(0..2)),
            int("Atlas RGBA8 glyphs pixels", "px", ATLAS_RGBA8_GLYPHS_PIXELS, expected(0..4_000_000)),
            int("Atlas RGBA8 glyphs textures", "", ATLAS_RGBA8_GLYPHS_TEXTURES, expected(0..2)),
            int("Atlas A8 glyphs pixels", "px", ATLAS_A8_GLYPHS_PIXELS, expected(0..4_000_000)),
            int("Atlas A8 glyphs textures", "", ATLAS_A8_GLYPHS_TEXTURES, expected(0..2)),
            float("Atlas RGBA8 linear pressure", "", ATLAS_COLOR8_LINEAR_PRESSURE, expected(0.0..1.0)),
            float("Atlas RGBA8 nearest pressure", "", ATLAS_COLOR8_NEAREST_PRESSURE, expected(0.0..1.0)),
            float("Atlas RGBA8 glyphs pressure", "", ATLAS_COLOR8_GLYPHS_PRESSURE, expected(0.0..1.0)),
            float("Atlas A8 pressure", "", ATLAS_ALPHA8_PRESSURE, expected(0.0..1.0)),
            float("Atlas A8 glyphs pressure", "", ATLAS_ALPHA8_GLYPHS_PRESSURE, expected(0.0..1.0)),
            float("Atlas A16 pressure", "", ATLAS_ALPHA16_PRESSURE, expected(0.0..1.0)),
            float("Texture cache standalone pressure", "", ATLAS_STANDALONE_PRESSURE, expected(0.0..1.0)),

            int("Texture cache eviction count", "items", TEXTURE_CACHE_EVICTION_COUNT, Expected::none()),
            int("Texture cache youngest evicted", "frames", TEXTURE_CACHE_YOUNGEST_EVICTION, Expected::none()),
            float("External image mem", "MB", EXTERNAL_IMAGE_BYTES, Expected::none()),
            float("Atlas textures mem", "MB", ATLAS_TEXTURES_MEM, Expected::none()),
            float("Standalone textures mem", "MB", STANDALONE_TEXTURES_MEM, Expected::none()),
            float("Picture tiles mem", "MB", PICTURE_TILES_MEM, expected(0.0..150.0)),
            float("Render targets mem", "MB", RENDER_TARGET_MEM, Expected::none()),

            float("Alpha targets samplers", "%", ALPHA_TARGETS_SAMPLERS, Expected::none()),
            float("Transparent pass samplers", "%", TRANSPARENT_PASS_SAMPLERS, Expected::none()),
            float("Opaque pass samplers", "%", OPAQUE_PASS_SAMPLERS, Expected::none()),
            float("Total samplers", "%", TOTAL_SAMPLERS, Expected::none()),

            int("Interned primitives", "", INTERNED_PRIMITIVES, Expected::none()),
            int("Interned clips", "", INTERNED_CLIPS, Expected::none()),
            int("Interned text runs", "", INTERNED_TEXT_RUNS, Expected::none()),
            int("Interned normal borders", "", INTERNED_NORMAL_BORDERS, Expected::none()),
            int("Interned image borders", "", INTERNED_IMAGE_BORDERS, Expected::none()),
            int("Interned images", "", INTERNED_IMAGES, Expected::none()),
            int("Interned YUV images", "", INTERNED_YUV_IMAGES, Expected::none()),
            int("Interned line decorations", "", INTERNED_LINE_DECORATIONS, Expected::none()),
            int("Interned linear gradients", "", INTERNED_LINEAR_GRADIENTS, Expected::none()),
            int("Interned radial gradients", "", INTERNED_RADIAL_GRADIENTS, Expected::none()),
            int("Interned conic gradients", "", INTERNED_CONIC_GRADIENTS, Expected::none()),
            int("Interned pictures", "", INTERNED_PICTURES, Expected::none()),
            int("Interned filter data", "", INTERNED_FILTER_DATA, Expected::none()),
            int("Interned backdrop captures", "", INTERNED_BACKDROP_CAPTURES, Expected::none()),
            int("Interned backdrop renders", "", INTERNED_BACKDROP_RENDERS, Expected::none()),
            int("Interned polygons", "", INTERNED_POLYGONS, Expected::none()),

            float("Depth targets mem", "MB", DEPTH_TARGETS_MEM, Expected::none()),
            float("Shader build time", "ms", SHADER_BUILD_TIME, Expected::none()),
            // We use the expected range to highlight render reasons that are happening.
            float("Reason scene", "", RENDER_REASON_SCENE, expected(0.0..0.01)),
            float("Reason animated property", "", RENDER_REASON_ANIMATED_PROPERTY, expected(0.0..0.01)),
            float("Reason resource update", "", RENDER_REASON_RESOURCE_UPDATE, expected(0.0..0.01)),
            float("Reason async image", "", RENDER_REASON_ASYNC_IMAGE, expected(0.0..0.01)),
            float("Reason clear resources", "", RENDER_REASON_CLEAR_RESOURCES, expected(0.0..0.01)),
            float("Reason APZ", "", RENDER_REASON_APZ, expected(0.0..0.01)),
            float("Reason resize", "", RENDER_REASON_RESIZE, expected(0.0..0.01)),
            float("Reason widget", "", RENDER_REASON_WIDGET, expected(0.0..0.01)),
            float("Reason cache flush", "", RENDER_REASON_TEXTURE_CACHE_FLUSH, expected(0.0..0.01)),
            float("Reason snapshot", "", RENDER_REASON_SNAPSHOT, expected(0.0..0.01)),
            float("Reason resource hook", "", RENDER_REASON_POST_RESOURCE_UPDATE_HOOKS, expected(0.0..0.01)),
            float("Reason config change", "", RENDER_REASON_CONFIG_CHANGE, expected(0.0..0.01)),
            float("Reason content sync", "", RENDER_REASON_CONTENT_SYNC, expected(0.0..0.01)),
            float("Reason flush", "", RENDER_REASON_FLUSH, expected(0.0..0.01)),
            float("Reason testing", "", RENDER_REASON_TESTING, expected(0.0..0.01)),
            float("Reason other", "", RENDER_REASON_OTHER, expected(0.0..0.01)),
            float("On vsync", "", RENDER_REASON_VSYNC, expected(0.0..0.01)),

            int("Textures created", "", TEXTURES_CREATED, expected(0..5)),
            int("Textures deleted", "", TEXTURES_DELETED, Expected::none()),
        ];

        let mut counters = Vec::with_capacity(profile_counters.len());

        for (idx, descriptor) in profile_counters.iter().enumerate() {
            debug_assert_eq!(descriptor.index, idx);
            counters.push(Counter::new(descriptor));
        }

        Profiler {
            gpu_frames: ProfilerFrameCollection::new(),
            frame_stats: ProfilerFrameCollection::new(),

            counters,
            start: precise_time_ns(),
            avg_over_period: ONE_SECOND_NS / 2,

            num_graph_samples: 500, // Would it be useful to control this via a pref?
            frame_timestamps_within_last_second: Vec::new(),
            ui: Vec::new(),
        }
    }

    /// Sum a few counters and if the total amount is larger than a threshold, update
    /// a specific counter.
    ///
    /// This is useful to monitor slow frame and slow transactions.
    fn update_slow_event(&mut self, dst_counter: usize, counters: &[usize], threshold: f64) {
        let mut total = 0.0;
        for &counter in counters {
            if self.counters[counter].value.is_finite() {
                total += self.counters[counter].value;
            }
        }

        if total > threshold {
            self.counters[dst_counter].set(total);
        }
    }

    // Call at the end of every frame, after setting the counter values and before drawing the counters.
    pub fn update(&mut self) {
        let now = precise_time_ns();
        let update_avg = (now - self.start) > self.avg_over_period;
        if update_avg {
            self.start = now;
        }
        let one_second_ago = now - ONE_SECOND_NS;
        self.frame_timestamps_within_last_second.retain(|t| *t > one_second_ago);
        self.frame_timestamps_within_last_second.push(now);

        self.update_slow_event(
            SLOW_FRAME,
            &[TOTAL_FRAME_CPU_TIME],
            15.0,
        );
        self.update_slow_event(
            SLOW_TXN,
            &[DISPLAY_LIST_BUILD_TIME, CONTENT_SEND_TIME, SCENE_BUILD_TIME],
            80.0
        );

        for counter in &mut self.counters {
            counter.update(update_avg);
        }
    }

    pub fn update_frame_stats(&mut self, stats: FullFrameStats) {
        if stats.gecko_display_list_time != 0.0 {
          self.frame_stats.push(stats.into());
        }
    }

    pub fn set_gpu_time_queries(&mut self, gpu_queries: Vec<GpuTimer>) {
        let mut gpu_time_ns = 0;
        for sample in &gpu_queries {
            gpu_time_ns += sample.time_ns;
        }

        self.gpu_frames.push(ProfilerFrame {
          total_time: gpu_time_ns,
          samples: gpu_queries
        });

        self.counters[GPU_TIME].set_f64(ns_to_ms(gpu_time_ns));
    }

    // Find the index of a counter by its name.
    pub fn index_of(&self, name: &str) -> Option<usize> {
        self.counters.iter().position(|counter| counter.name == name)
    }

    // Define the profiler UI, see comment about the syntax at the top of this file.
    pub fn set_ui(&mut self, names: &str) {
        let mut selection = Vec::new();

        self.append_to_ui(&mut selection, names);

        if selection == self.ui {
            return;
        }

        for counter in &mut self.counters {
            counter.disable_graph();
        }

        for item in &selection {
            if let Item::Graph(idx) = item {
                self.counters[*idx].enable_graph(self.num_graph_samples);
            }
        }

        self.ui = selection;
    }

    fn append_to_ui(&mut self, selection: &mut Vec<Item>, names: &str) {
        // Group successive counters together.
        fn flush_counters(counters: &mut Vec<usize>, selection: &mut Vec<Item>) {
            if !counters.is_empty() {
                selection.push(Item::Counters(std::mem::take(counters)))
            }
        }

        let mut counters = Vec::new();

        for name in names.split(",") {
            let name = name.trim();
            let is_graph = name.starts_with("#");
            let is_indicator = name.starts_with("*");
            let name = if is_graph || is_indicator {
                &name[1..]
            } else {
                name
            };
            // See comment about the ui string syntax at the top of this file.
            match name {
                "" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::Space);
                }
                "|" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::Column);
                }
                "_" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::Row);
                }
                "FPS" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::Fps);
                }
                "GPU time queries" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::GpuTimeQueries);
                }
                "GPU cache bars" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::GpuCacheBars);
                }
                "Paint phase graph" => {
                    flush_counters(&mut counters, selection);
                    selection.push(Item::PaintPhaseGraph);
                }
                _ => {
                    if let Some(idx) = self.index_of(name) {
                        if is_graph {
                            flush_counters(&mut counters, selection);
                            selection.push(Item::Graph(idx));
                        } else if is_indicator {
                            flush_counters(&mut counters, selection);
                            selection.push(Item::ChangeIndicator(idx));
                        } else {
                            counters.push(idx);
                        }
                    } else if let Some(preset_str) = find_preset(name) {
                        flush_counters(&mut counters, selection);
                        self.append_to_ui(selection, preset_str);
                    } else {
                        selection.push(Item::Text(format!("Unknonw counter: {}", name)));
                    }
                }
            }
        }

        flush_counters(&mut counters, selection);
    }

    pub fn set_counters(&mut self, counters: &mut TransactionProfile) {
        for (id, evt) in counters.events.iter_mut().enumerate() {
            if let Event::Value(val) = *evt {
                self.counters[id].set(val);
            }
            *evt = Event::None;
        }
    }

    pub fn get(&self, id: usize) -> Option<f64> {
        self.counters[id].get()
    }

    fn draw_counters(
        counters: &[Counter],
        selected: &[usize],
        mut x: f32, mut y: f32,
        text_buffer: &mut String,
        debug_renderer: &mut DebugRenderer,
    ) -> default::Rect<f32> {
        let line_height = debug_renderer.line_height();

        x += PROFILE_PADDING;
        y += PROFILE_PADDING;
        let origin = default::Point2D::new(x, y);
        y += line_height * 0.5;

        let mut total_rect = Rect::zero();

        let mut color_index = 0;
        let colors = [
            // Regular values,
            ColorU::new(255, 255, 255, 255),
            ColorU::new(255, 255, 0, 255),
            // Unexpected values,
            ColorU::new(255, 80, 0, 255),
            ColorU::new(255, 0, 0, 255),
        ];

        for idx in selected {
            // If The index is invalid, add some vertical space.
            let counter = &counters[*idx];

            let rect = debug_renderer.add_text(
                x, y,
                counter.name,
                colors[color_index],
                None,
            );
            color_index = (color_index + 1) % 2;

            total_rect = total_rect.union(&rect);
            y += line_height;
        }

        color_index = 0;
        x = total_rect.max_x() + 60.0;
        y = origin.y + line_height * 0.5;

        for idx in selected {
            let counter = &counters[*idx];
            let expected_offset = if counter.has_unexpected_avg_max() { 2 } else { 0 };

            counter.write_value(text_buffer);

            let rect = debug_renderer.add_text(
                x,
                y,
                &text_buffer,
                colors[color_index + expected_offset],
                None,
            );
            color_index = (color_index + 1) % 2;

            total_rect = total_rect.union(&rect);
            y += line_height;
        }

        total_rect = total_rect
            .union(&Rect { origin, size: Size2D::new(1.0, 1.0) })
            .inflate(PROFILE_PADDING, PROFILE_PADDING);

        debug_renderer.add_quad(
            total_rect.min_x(),
            total_rect.min_y(),
            total_rect.max_x(),
            total_rect.max_y(),
            BACKGROUND_COLOR,
            BACKGROUND_COLOR,
        );

        total_rect
    }

    fn draw_graph(
        counter: &Counter,
        x: f32,
        y: f32,
        text_buffer: &mut String,
        debug_renderer: &mut DebugRenderer,
    ) -> default::Rect<f32> {
        let graph = counter.graph.as_ref().unwrap();

        let max_samples = graph.values.capacity() as f32;

        let size = Size2D::new(max_samples, 100.0);
        let line_height = debug_renderer.line_height();
        let graph_rect = Rect::new(Point2D::new(x + PROFILE_PADDING, y + PROFILE_PADDING), size);
        let mut rect = graph_rect.inflate(PROFILE_PADDING, PROFILE_PADDING);

        let stats = graph.stats();

        let text_color = ColorU::new(255, 255, 0, 255);
        let text_origin = rect.origin + vec2(rect.size.width, 25.0);
        set_text!(text_buffer, "{} ({})", counter.name, counter.unit);
        debug_renderer.add_text(
            text_origin.x,
            text_origin.y,
            if counter.unit == "" { counter.name } else { text_buffer },
            ColorU::new(0, 255, 0, 255),
            None,
        );

        set_text!(text_buffer, "Samples: {}", stats.samples);

        debug_renderer.add_text(
            text_origin.x,
            text_origin.y + line_height,
            text_buffer,
            text_color,
            None,
        );

        if stats.samples > 0 {
            set_text!(text_buffer, "Min: {:.2} {}", stats.min, counter.unit);
            debug_renderer.add_text(
                text_origin.x,
                text_origin.y + line_height * 2.0,
                text_buffer,
                text_color,
                None,
            );

            set_text!(text_buffer, "Avg: {:.2} {}", stats.avg, counter.unit);
            debug_renderer.add_text(
                text_origin.x,
                text_origin.y + line_height * 3.0,
                text_buffer,
                text_color,
                None,
            );

            set_text!(text_buffer, "Max: {:.2} {}", stats.max, counter.unit);
            debug_renderer.add_text(
                text_origin.x,
                text_origin.y + line_height * 4.0,
                text_buffer,
                text_color,
                None,
            );
        }

        rect.size.width += 220.0;
        debug_renderer.add_quad(
            rect.min_x(),
            rect.min_y(),
            rect.max_x(),
            rect.max_y(),
            BACKGROUND_COLOR,
            BACKGROUND_COLOR,
        );

        let bx1 = graph_rect.max_x();
        let by1 = graph_rect.max_y();

        let w = graph_rect.size.width / max_samples;
        let h = graph_rect.size.height;

        let color_t0 = ColorU::new(0, 255, 0, 255);
        let color_b0 = ColorU::new(0, 180, 0, 255);

        let color_t2 = ColorU::new(255, 0, 0, 255);
        let color_b2 = ColorU::new(180, 0, 0, 255);

        for (index, sample) in graph.values.iter().enumerate() {
            if !sample.is_finite() {
                // NAN means no sample this frame.
                continue;
            }
            let sample = *sample as f32;
            let x1 = bx1 - index as f32 * w;
            let x0 = x1 - w;

            let y0 = by1 - (sample / stats.max as f32) as f32 * h;
            let y1 = by1;

            let (color_top, color_bottom) = if counter.is_unexpected_value(sample as f64) {
                (color_t2, color_b2)
            } else {
                (color_t0, color_b0)
            };

            debug_renderer.add_quad(x0, y0, x1, y1, color_top, color_bottom);
        }

        rect
    }


    fn draw_change_indicator(
        counter: &Counter,
        x: f32, y: f32,
        debug_renderer: &mut DebugRenderer
    ) -> default::Rect<f32> {
        let height = 10.0;
        let width = 20.0;

        // Draw the indicator red instead of blue if is is not within expected ranges.
        let color = if counter.has_unexpected_value() || counter.has_unexpected_avg_max() {
            ColorU::new(255, 20, 20, 255)
        } else {
            ColorU::new(0, 100, 250, 255)
        };

        let tx = counter.change_indicator as f32 * width;
        debug_renderer.add_quad(
            x,
            y,
            x + 15.0 * width,
            y + height,
            ColorU::new(0, 0, 0, 150),
            ColorU::new(0, 0, 0, 150),
        );

        debug_renderer.add_quad(
            x + tx,
            y,
            x + tx + width,
            y + height,
            color,
            ColorU::new(25, 25, 25, 255),
        );

        Rect {
            origin: Point2D::new(x, y),
            size: Size2D::new(15.0 * width + 20.0, height),
        }
    }

    fn draw_bar(
        label: &str,
        label_color: ColorU,
        counters: &[(ColorU, usize)],
        x: f32, y: f32,
        debug_renderer: &mut DebugRenderer,
    ) -> default::Rect<f32> {
        let x = x + 8.0;
        let y = y + 24.0;
        let text_rect = debug_renderer.add_text(
            x, y,
            label,
            label_color,
            None,
        );

        let x_base = text_rect.max_x() + 10.0;
        let width = 300.0;
        let total_value = counters.last().unwrap().1;
        let scale = width / total_value as f32;
        let mut x_current = x_base;

        for &(color, counter) in counters {
            let x_stop = x_base + counter as f32 * scale;
            debug_renderer.add_quad(
                x_current,
                text_rect.origin.y,
                x_stop,
                text_rect.max_y(),
                color,
                color,
            );
            x_current = x_stop;

        }

        let mut total_rect = text_rect;
        total_rect.size.width += width + 10.0;

        total_rect
    }

    fn draw_gpu_cache_bars(&self, x: f32, mut y: f32, text_buffer: &mut String, debug_renderer: &mut DebugRenderer) -> default::Rect<f32> {
        let color_updated = ColorU::new(0xFF, 0, 0, 0xFF);
        let color_free = ColorU::new(0, 0, 0xFF, 0xFF);
        let color_saved = ColorU::new(0, 0xFF, 0, 0xFF);

        let updated_blocks = self.get(GPU_CACHE_BLOCKS_UPDATED).unwrap_or(0.0) as usize;
        let saved_blocks = self.get(GPU_CACHE_BLOCKS_SAVED).unwrap_or(0.0) as usize;
        let allocated_blocks = self.get(GPU_CACHE_BLOCKS_TOTAL).unwrap_or(0.0) as usize;
        let allocated_rows = self.get(GPU_CACHE_ROWS_TOTAL).unwrap_or(0.0) as usize;
        let updated_rows = self.get(GPU_CACHE_ROWS_UPDATED).unwrap_or(0.0) as usize;
        let requested_blocks = updated_blocks + saved_blocks;
        let total_blocks = allocated_rows * MAX_VERTEX_TEXTURE_WIDTH;

        set_text!(text_buffer, "GPU cache rows ({}):", allocated_rows);

        let rect0 = Profiler::draw_bar(
            text_buffer,
            ColorU::new(0xFF, 0xFF, 0xFF, 0xFF),
            &[
                (color_updated, updated_rows),
                (color_free, allocated_rows),
            ],
            x, y,
            debug_renderer,
        );

        y = rect0.max_y();

        let rect1 = Profiler::draw_bar(
            "GPU cache blocks",
            ColorU::new(0xFF, 0xFF, 0, 0xFF),
            &[
                (color_updated, updated_blocks),
                (color_saved, requested_blocks),
                (color_free, allocated_blocks),
                (ColorU::new(0, 0, 0, 0xFF), total_blocks),
            ],
            x, y,
            debug_renderer,
        );

        let total_rect = rect0.union(&rect1).inflate(10.0, 10.0);
        debug_renderer.add_quad(
            total_rect.origin.x,
            total_rect.origin.y,
            total_rect.origin.x + total_rect.size.width,
            total_rect.origin.y + total_rect.size.height,
            ColorF::new(0.1, 0.1, 0.1, 0.8).into(),
            ColorF::new(0.2, 0.2, 0.2, 0.8).into(),
        );

        total_rect
    }

    // Draws a frame graph for a given frame collection.
    fn draw_frame_graph(
        frame_collection: &ProfilerFrameCollection,
        x: f32, y: f32,
        debug_renderer: &mut DebugRenderer,
    ) -> default::Rect<f32> {
        let mut has_data = false;
        for frame in &frame_collection.frames {
            if !frame.samples.is_empty() {
                has_data = true;
                break;
            }
        }

        if !has_data {
            return Rect::zero();
        }

        let graph_rect = Rect::new(
            Point2D::new(x + GRAPH_PADDING, y + GRAPH_PADDING),
            Size2D::new(GRAPH_WIDTH, GRAPH_HEIGHT),
        );
        let bounding_rect = graph_rect.inflate(GRAPH_PADDING, GRAPH_PADDING);

        debug_renderer.add_quad(
            bounding_rect.origin.x,
            bounding_rect.origin.y,
            bounding_rect.origin.x + bounding_rect.size.width,
            bounding_rect.origin.y + bounding_rect.size.height,
            BACKGROUND_COLOR,
            BACKGROUND_COLOR,
        );

        let w = graph_rect.size.width;
        let mut y0 = graph_rect.origin.y;

        let mut max_time = frame_collection.frames
            .iter()
            .max_by_key(|f| f.total_time)
            .unwrap()
            .total_time as f32;

        // If the max time is lower than 16ms, fix the scale
        // at 16ms so that the graph is easier to interpret.
        let baseline_ns = 16_000_000.0; // 16ms
        max_time = max_time.max(baseline_ns);

        let mut tags_present = FastHashMap::default();

        for frame in &frame_collection.frames {
            let y1 = y0 + GRAPH_FRAME_HEIGHT;

            let mut current_ns = 0;
            for sample in &frame.samples {
                let x0 = graph_rect.origin.x + w * current_ns as f32 / max_time;
                current_ns += sample.time_ns;
                let x1 = graph_rect.origin.x + w * current_ns as f32 / max_time;
                let mut bottom_color = sample.tag.color;
                bottom_color.a *= 0.5;

                debug_renderer.add_quad(
                    x0,
                    y0,
                    x1,
                    y1,
                    sample.tag.color.into(),
                    bottom_color.into(),
                );

                tags_present.insert(sample.tag.label, sample.tag.color);
            }

            y0 = y1;
        }

        // If the max time is higher than 16ms, show a vertical line at the
        // 16ms mark.
        if max_time > baseline_ns {
            let x = graph_rect.origin.x + w * baseline_ns as f32 / max_time;
            let height = frame_collection.frames.len() as f32 * GRAPH_FRAME_HEIGHT;

            debug_renderer.add_quad(
                x,
                graph_rect.origin.y,
                x + 4.0,
                graph_rect.origin.y + height,
                ColorU::new(120, 00, 00, 150),
                ColorU::new(120, 00, 00, 100),
            );
        }


        // Add a legend to see which color correspond to what primitive.
        const LEGEND_SIZE: f32 = 20.0;
        const PADDED_LEGEND_SIZE: f32 = 25.0;
        if !tags_present.is_empty() {
            debug_renderer.add_quad(
                bounding_rect.max_x() + GRAPH_PADDING,
                bounding_rect.origin.y,
                bounding_rect.max_x() + GRAPH_PADDING + 200.0,
                bounding_rect.origin.y + tags_present.len() as f32 * PADDED_LEGEND_SIZE + GRAPH_PADDING,
                BACKGROUND_COLOR,
                BACKGROUND_COLOR,
            );
        }

        for (i, (label, &color)) in tags_present.iter().enumerate() {
            let x0 = bounding_rect.origin.x + bounding_rect.size.width + GRAPH_PADDING * 2.0;
            let y0 = bounding_rect.origin.y + GRAPH_PADDING + i as f32 * PADDED_LEGEND_SIZE;

            debug_renderer.add_quad(
                x0, y0, x0 + LEGEND_SIZE, y0 + LEGEND_SIZE,
                color.into(),
                color.into(),
            );

            debug_renderer.add_text(
                x0 + PADDED_LEGEND_SIZE,
                y0 + LEGEND_SIZE * 0.75,
                label,
                ColorU::new(255, 255, 0, 255),
                None,
            );
        }

        bounding_rect
    }

    pub fn draw_profile(
        &mut self,
        _frame_index: u64,
        debug_renderer: &mut DebugRenderer,
        device_size: DeviceIntSize,
    ) {
        let x_start = 20.0;
        let mut y_start = 150.0;
        let default_column_width = 400.0;

        // set_text!(..) into this string instead of using format!(..) to avoid
        // unnecessary allocations.
        let mut text_buffer = String::with_capacity(32);

        let mut column_width = default_column_width;
        let mut max_y = y_start;

        let mut x = x_start;
        let mut y = y_start;

        for elt in &self.ui {
            let rect = match elt {
                Item::Counters(indices) => {
                    Profiler::draw_counters(&self.counters, &indices, x, y, &mut text_buffer, debug_renderer)
                }
                Item::Graph(idx) => {
                    Profiler::draw_graph(&self.counters[*idx], x, y, &mut text_buffer, debug_renderer)
                }
                Item::ChangeIndicator(idx) => {
                    Profiler::draw_change_indicator(&self.counters[*idx], x, y, debug_renderer)
                }
                Item::GpuTimeQueries => {
                    Profiler::draw_frame_graph(&self.gpu_frames, x, y, debug_renderer)
                }
                Item::GpuCacheBars => {
                    self.draw_gpu_cache_bars(x, y, &mut text_buffer, debug_renderer)
                }
                Item::PaintPhaseGraph => {
                    Profiler::draw_frame_graph(&self.frame_stats, x, y, debug_renderer)
                }
                Item::Text(text) => {
                    let p = 10.0;
                    let mut rect = debug_renderer.add_text(
                        x + p,
                        y + p,
                        &text,
                        ColorU::new(255, 255, 255, 255),
                        None,
                    );
                    rect = rect.inflate(p, p);

                    debug_renderer.add_quad(
                        rect.origin.x,
                        rect.origin.y,
                        rect.max_x(),
                        rect.max_y(),
                        BACKGROUND_COLOR,
                        BACKGROUND_COLOR,
                    );

                    rect
                }
                Item::Fps => {
                    let fps = self.frame_timestamps_within_last_second.len();
                    set_text!(&mut text_buffer, "{} fps", fps);
                    let mut rect = debug_renderer.add_text(
                        x + PROFILE_PADDING,
                        y + PROFILE_PADDING + 5.0,
                        &text_buffer,
                        ColorU::new(255, 255, 255, 255),
                        None,
                    );
                    rect = rect.inflate(PROFILE_PADDING, PROFILE_PADDING);

                    debug_renderer.add_quad(
                        rect.min_x(),
                        rect.min_y(),
                        rect.max_x(),
                        rect.max_y(),
                        BACKGROUND_COLOR,
                        BACKGROUND_COLOR,
                    );

                    rect
                }
                Item::Space => {
                    Rect { origin: Point2D::new(x, y), size: Size2D::new(0.0, PROFILE_SPACING) }
                }
                Item::Column => {
                    max_y = max_y.max(y);
                    x += column_width + PROFILE_SPACING;
                    y = y_start;
                    column_width = default_column_width;

                    continue;
                }
                Item::Row => {
                    max_y = max_y.max(y);
                    y_start = max_y + PROFILE_SPACING;
                    y = y_start;
                    x = x_start;
                    column_width = default_column_width;

                    continue;
                }
            };

            column_width = column_width.max(rect.size.width);
            y = rect.max_y();

            if y > device_size.height as f32 - 100.0 {
                max_y = max_y.max(y);
                x += column_width + PROFILE_SPACING;
                y = y_start;
                column_width = default_column_width;
            }
        }
    }

    #[cfg(feature = "capture")]
    pub fn dump_stats(&self, sink: &mut dyn std::io::Write) -> std::io::Result<()> {
        for counter in &self.counters {
            if counter.value.is_finite() {
                writeln!(sink, "{} {:?}{}", counter.name, counter.value, counter.unit)?;
            }
        }

        Ok(())
    }
}

/// Defines the interface for hooking up an external profiler to WR.
pub trait ProfilerHooks : Send + Sync {
    /// Register a thread with the profiler.
    fn register_thread(&self, thread_name: &str);

    /// Unregister a thread with the profiler.
    fn unregister_thread(&self);

    /// Called at the beginning of a profile scope.
    fn begin_marker(&self, label: &str);

    /// Called at the end of a profile scope.
    fn end_marker(&self, label: &str);

    /// Called to mark an event happening.
    fn event_marker(&self, label: &str);

    /// Called with a duration to indicate a text marker that just ended. Text
    /// markers allow different types of entries to be recorded on the same row
    /// in the timeline, by adding labels to the entry.
    ///
    /// This variant is also useful when the caller only wants to record events
    /// longer than a certain threshold, and thus they don't know in advance
    /// whether the event will qualify.
    fn add_text_marker(&self, label: &str, text: &str, duration: Duration);

    /// Returns true if the current thread is being profiled.
    fn thread_is_being_profiled(&self) -> bool;
}

/// The current global profiler callbacks, if set by embedder.
pub static mut PROFILER_HOOKS: Option<&'static dyn ProfilerHooks> = None;

/// Set the profiler callbacks, or None to disable the profiler.
/// This function must only ever be called before any WR instances
/// have been created, or the hooks will not be set.
pub fn set_profiler_hooks(hooks: Option<&'static dyn ProfilerHooks>) {
    if !wr_has_been_initialized() {
        unsafe {
            PROFILER_HOOKS = hooks;
        }
    }
}

/// A simple RAII style struct to manage a profile scope.
pub struct ProfileScope {
    name: &'static str,
}


/// Register a thread with the Gecko Profiler.
pub fn register_thread(thread_name: &str) {
    unsafe {
        if let Some(ref hooks) = PROFILER_HOOKS {
            hooks.register_thread(thread_name);
        }
    }
}


/// Unregister a thread with the Gecko Profiler.
pub fn unregister_thread() {
    unsafe {
        if let Some(ref hooks) = PROFILER_HOOKS {
            hooks.unregister_thread();
        }
    }
}

/// Records a marker of the given duration that just ended.
pub fn add_text_marker(label: &str, text: &str, duration: Duration) {
    unsafe {
        if let Some(ref hooks) = PROFILER_HOOKS {
            hooks.add_text_marker(label, text, duration);
        }
    }
}

/// Records a marker of the given duration that just ended.
pub fn add_event_marker(label: &str) {
    unsafe {
        if let Some(ref hooks) = PROFILER_HOOKS {
            hooks.event_marker(label);
        }
    }
}

/// Returns true if the current thread is being profiled.
pub fn thread_is_being_profiled() -> bool {
    unsafe {
        PROFILER_HOOKS.map_or(false, |h| h.thread_is_being_profiled())
    }
}

impl ProfileScope {
    /// Begin a new profile scope
    pub fn new(name: &'static str) -> Self {
        unsafe {
            if let Some(ref hooks) = PROFILER_HOOKS {
                hooks.begin_marker(name);
            }
        }

        ProfileScope {
            name,
        }
    }
}

impl Drop for ProfileScope {
    fn drop(&mut self) {
        unsafe {
            if let Some(ref hooks) = PROFILER_HOOKS {
                hooks.end_marker(self.name);
            }
        }
    }
}

/// A helper macro to define profile scopes.
macro_rules! profile_marker {
    ($string:expr) => {
        let _scope = $crate::profiler::ProfileScope::new($string);
    };
}

#[derive(Debug, Clone)]
pub struct GpuProfileTag {
    pub label: &'static str,
    pub color: ColorF,
}

/// Ranges of expected value for a profile counter.
#[derive(Clone, Debug)]
pub struct Expected<T> {
    pub range: Option<Range<T>>,
    pub avg: Option<Range<T>>,
}

impl<T> Expected<T> {
     const fn none() -> Self {
        Expected {
            range: None,
            avg: None,
        }
    }
}

const fn expected<T>(range: Range<T>) -> Expected<T> {
    Expected {
        range: Some(range),
        avg: None,
    }
}

impl Expected<f64> {
    const fn avg(mut self, avg: Range<f64>) -> Self {
        self.avg = Some(avg);
        self
    }
}

impl Expected<i64> {
    const fn avg(mut self, avg: Range<i64>) -> Self {
        self.avg = Some(avg);
        self
    }

    fn into_float(self) -> Expected<f64> {
        Expected {
            range: match self.range {
                Some(r) => Some(r.start as f64 .. r.end as f64),
                None => None,
            },
            avg: match self.avg {
                Some(r) => Some(r.start as f64 .. r.end as f64),
                None => None,
            },
        }
    }
}

pub struct CounterDescriptor {
    pub name: &'static str,
    pub unit: &'static str,
    pub index: usize,
    pub show_as: ShowAs,
    pub expected: Expected<f64>,
}

#[derive(Debug)]
pub struct Counter {
    pub name: &'static str,
    pub unit: &'static str,
    pub show_as: ShowAs,
    pub expected: Expected<f64>,

    ///
    value: f64,
    /// Number of samples in the current time slice.
    num_samples: u64,
    /// Sum of the values recorded during the current time slice.
    sum: f64,
    /// The max value in in-progress time slice.
    next_max: f64,
    /// The max value of the previous time slice (displayed).
    max: f64,
    /// The average value of the previous time slice (displayed).
    avg: f64,
    /// Incremented when the counter changes.
    change_indicator: u8,

    graph: Option<Graph>,
}

impl Counter {
    pub fn new(descriptor: &CounterDescriptor) -> Self {
        Counter {
            name: descriptor.name,
            unit: descriptor.unit,
            show_as: descriptor.show_as,
            expected: descriptor.expected.clone(),
            value: std::f64::NAN,
            num_samples: 0,
            sum: 0.0,
            next_max: 0.0,
            max: 0.0,
            avg: 0.0,
            change_indicator: 0,
            graph: None,
        }
    }
    pub fn set_f64(&mut self, val: f64) {
        self.value = val;
    }

    pub fn set<T>(&mut self, val: T) where T: Into<f64> {
        self.set_f64(val.into());
    }

    pub fn get(&self) -> Option<f64> {
        if self.value.is_finite() {
            Some(self.value)
        } else {
            None
        }
    }

    pub fn write_value(&self, output: &mut String) {
        match self.show_as {
            ShowAs::Float => {
                set_text!(output, "{:.2} {} (max: {:.2})", self.avg, self.unit, self.max);
            }
            ShowAs::Int => {
                set_text!(output, "{:.0} {} (max: {:.0})", self.avg.round(), self.unit, self.max.round());
            }
        }
    }

    pub fn enable_graph(&mut self, max_samples: usize) {
        if self.graph.is_some() {
            return;
        }

        self.graph = Some(Graph::new(max_samples));
    }

    pub fn disable_graph(&mut self) {
        self.graph = None;
    }

    pub fn is_unexpected_value(&self, value: f64) -> bool {
        if let Some(range) = &self.expected.range {
            return value.is_finite() && value >= range.end;
        }

        false
    }

    pub fn has_unexpected_value(&self) -> bool {
        self.is_unexpected_value(self.value)
    }

    pub fn has_unexpected_avg_max(&self) -> bool {
        if let Some(range) = &self.expected.range {
            if self.max.is_finite() && self.max >= range.end {
                return true;
            }
        }

        if let Some(range) = &self.expected.avg {
            if self.avg < range.start || self.avg >= range.end {
                return true;
            }
        }

        false
    }

    fn update(&mut self, update_avg: bool) {
        let updated = self.value.is_finite();
        if updated {
            self.next_max = self.next_max.max(self.value);
            self.sum += self.value;
            self.num_samples += 1;
            self.change_indicator = (self.change_indicator + 1) % 15;
        }

        if let Some(graph) = &mut self.graph {
            graph.set(self.value);
        }

        self.value = std::f64::NAN;

        if update_avg {
            if self.num_samples > 0 {
                self.avg = self.sum / self.num_samples as f64;
                self.max = self.next_max;
            } else {
                // There has been no sample in the averaging window, just show zero.
                self.avg = 0.0;
                self.max = 0.0;
            }
            self.sum = 0.0;
            self.num_samples = 0;
            self.next_max = std::f64::MIN;
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Event {
    Start(u64),
    Value(f64),
    None,
}

// std::convert::From/TryFrom can't deal with integer to f64 so we roll our own...
pub trait EventValue {
    fn into_f64(self) -> f64;
}

impl EventValue for f64 { fn into_f64(self) -> f64 { self } }
impl EventValue for f32 { fn into_f64(self) -> f64 { self as f64 } }
impl EventValue for u32 { fn into_f64(self) -> f64 { self as f64 } }
impl EventValue for i32 { fn into_f64(self) -> f64 { self as f64 } }
impl EventValue for u64 { fn into_f64(self) -> f64 { self as f64 } }
impl EventValue for usize { fn into_f64(self) -> f64 { self as f64 } }

/// A container for profiling information that moves along the rendering pipeline
/// and is handed off to the profiler at the end.
pub struct TransactionProfile {
    pub events: Vec<Event>,
}

impl TransactionProfile {
    pub fn new() -> Self {
        TransactionProfile {
            events: vec![Event::None; NUM_PROFILER_EVENTS],
        }
    }

    pub fn start_time(&mut self, id: usize) {
        let ns = precise_time_ns();
        self.events[id] = Event::Start(ns);
    }

    pub fn end_time(&mut self, id: usize) -> f64 {
        self.end_time_if_started(id).unwrap()
    }

    /// Similar to end_time, but doesn't panic if not matched with start_time.
    pub fn end_time_if_started(&mut self, id: usize) -> Option<f64> {
        if let Event::Start(start) = self.events[id] {
            let now = precise_time_ns();
            let time_ns = now - start;

            let time_ms = ns_to_ms(time_ns);
            self.events[id] = Event::Value(time_ms);

            Some(time_ms)
        } else {
            None
        }
    }

    pub fn set<T>(&mut self, id: usize, value: T) where T: EventValue {
        self.set_f64(id, value.into_f64());
    }


    pub fn set_f64(&mut self, id: usize, value: f64) {
        self.events[id] = Event::Value(value);
    }

    pub fn get(&self, id: usize) -> Option<f64> {
        if let Event::Value(val) = self.events[id] {
            Some(val)
        } else {
            None
        }
    }

    pub fn get_or(&self, id: usize, or: f64) -> f64 {
        self.get(id).unwrap_or(or)
    }

    pub fn add<T>(&mut self, id: usize, n: T) where T: EventValue {
        let n = n.into_f64();

        let evt = &mut self.events[id];

        let val = match *evt {
            Event::Value(v) => v + n,
            Event::None => n,
            Event::Start(..) => { panic!(); }
        };

        *evt = Event::Value(val);
    }

    pub fn inc(&mut self, id: usize) {
        self.add(id, 1.0);
    }

    pub fn take(&mut self) -> Self {
        TransactionProfile {
            events: std::mem::take(&mut self.events),
        }
    }

    pub fn take_and_reset(&mut self) -> Self {
        let events = std::mem::take(&mut self.events);

        *self = TransactionProfile::new();

        TransactionProfile { events }
    }

    pub fn merge(&mut self, other: &mut Self) {
        for i in 0..self.events.len() {
            match (self.events[i], other.events[i]) {
                (Event::Value(v1), Event::Value(v2)) => {
                    self.events[i] = Event::Value(v1.max(v2));
                }
                (Event::Value(_), _) => {}
                (_, Event::Value(v2)) => {
                    self.events[i] = Event::Value(v2);
                }
                (Event::None, evt) => {
                    self.events[i] = evt;
                }
                (Event::Start(s1), Event::Start(s2)) => {
                    self.events[i] = Event::Start(s1.max(s2));
                }
                _=> {}
            }
            other.events[i] = Event::None;
        }
    }

    pub fn clear(&mut self) {
        for evt in &mut self.events {
            *evt = Event::None;
        }
    }
}

impl GlyphRasterizeProfiler for TransactionProfile {
    fn start_time(&mut self) {
        let id = GLYPH_RESOLVE_TIME;
        let ns = precise_time_ns();
        self.events[id] = Event::Start(ns);
    }

    fn end_time(&mut self) -> f64 {
        let id = GLYPH_RESOLVE_TIME;
        self.end_time_if_started(id).unwrap()
    }

    fn set(&mut self, value: f64) {
        let id = RASTERIZED_GLYPHS;
        self.set_f64(id, value);
    }
}

#[derive(Debug)]
pub struct GraphStats {
    pub min: f64,
    pub avg: f64,
    pub max: f64,
    pub sum: f64,
    pub samples: usize,
}

#[derive(Debug)]
pub struct Graph {
    values: VecDeque<f64>,
}

impl Graph {
    fn new(max_samples: usize) -> Self {
        let mut values = VecDeque::new();
        values.reserve(max_samples);

        Graph { values }
    }

    fn set(&mut self, val: f64) {
        if self.values.len() == self.values.capacity() {
            self.values.pop_back();
        }
        self.values.push_front(val);
    }

    pub fn stats(&self) -> GraphStats {
        let mut stats = GraphStats {
            min: f64::MAX,
            avg: 0.0,
            max: -f64::MAX,
            sum: 0.0,
            samples: 0,
        };

        let mut samples = 0;
        for value in &self.values {
            if value.is_finite() {
                stats.min = stats.min.min(*value);
                stats.max = stats.max.max(*value);
                stats.sum += *value;
                samples += 1;
            }
        }

        if samples > 0 {
            stats.avg = stats.sum / samples as f64;
            stats.samples = samples;
        }

        stats
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ShowAs {
    Float,
    Int,
}

struct ProfilerFrame {
    total_time: u64,
    samples: Vec<GpuTimer>,
}

struct ProfilerFrameCollection {
    frames: VecDeque<ProfilerFrame>,
}

impl ProfilerFrameCollection {
    fn new() -> Self {
        ProfilerFrameCollection {
            frames: VecDeque::new(),
        }
    }

    fn push(&mut self, frame: ProfilerFrame) {
        if self.frames.len() == 20 {
            self.frames.pop_back();
        }
        self.frames.push_front(frame);
    }
}

impl From<FullFrameStats> for ProfilerFrame {
  fn from(stats: FullFrameStats) -> ProfilerFrame {
    let new_sample = |time, label, color| -> GpuTimer {
      let tag = GpuProfileTag {
        label,
        color
      };

      let time_ns = ms_to_ns(time);

      GpuTimer {
        tag, time_ns
      }
    };

    let samples = vec![
      new_sample(stats.gecko_display_list_time, "Gecko DL", ColorF { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }),
      new_sample(stats.wr_display_list_time, "WR DL", ColorF { r: 0.0, g: 1.0, b: 1.0, a: 1.0 }),
      new_sample(stats.scene_build_time, "Scene Build", ColorF { r: 1.0, g: 0.0, b: 1.0, a: 1.0 }),
      new_sample(stats.frame_build_time, "Frame Build", ColorF { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }),
    ];

    ProfilerFrame {
      total_time: ms_to_ns(stats.total()),
      samples
    }
  }
}

pub fn ns_to_ms(ns: u64) -> f64 {
    ns as f64 / 1_000_000.0
}

pub fn ms_to_ns(ms: f64) -> u64 {
  (ms * 1_000_000.0) as u64
}

pub fn bytes_to_mb(bytes: usize) -> f64 {
    bytes as f64 / 1_000_000.0
}

#[derive(Debug, PartialEq)]
enum Item {
    Counters(Vec<usize>),
    Graph(usize),
    ChangeIndicator(usize),
    Fps,
    GpuTimeQueries,
    GpuCacheBars,
    PaintPhaseGraph,
    Text(String),
    Space,
    Column,
    Row,
}