summaryrefslogtreecommitdiffstats
path: root/layout/generic/nsImageFrame.cpp
blob: 79f69167b33a2670623fea6fb3530b0bbc1dc59c (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
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

/* rendering object for replaced elements with image data */

#include "nsImageFrame.h"

#include "TextDrawTarget.h"
#include "gfx2DGlue.h"
#include "gfxContext.h"
#include "gfxUtils.h"
#include "mozilla/dom/NameSpaceConstants.h"
#include "mozilla/intl/BidiEmbeddingLevel.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Encoding.h"
#include "mozilla/HTMLEditor.h"
#include "mozilla/dom/ImageTracker.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/dom/GeneratedImageContent.h"
#include "mozilla/dom/HTMLAreaElement.h"
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/ResponsiveImageSelector.h"
#include "mozilla/image/WebRenderImageProvider.h"
#include "mozilla/layers/RenderRootStateManager.h"
#include "mozilla/layers/WebRenderLayerManager.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_image.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/SVGImageContext.h"
#include "mozilla/Unused.h"

#include "nsCOMPtr.h"
#include "nsFontMetrics.h"
#include "nsIFrameInlines.h"
#include "nsIImageLoadingContent.h"
#include "nsImageLoadingContent.h"
#include "nsImageRenderer.h"
#include "nsObjectLoadingContent.h"
#include "nsString.h"
#include "nsPrintfCString.h"
#include "nsPresContext.h"
#include "nsGkAtoms.h"
#include "mozilla/dom/Document.h"
#include "nsContentUtils.h"
#include "nsCSSAnonBoxes.h"
#include "nsStyleConsts.h"
#include "nsStyleUtil.h"
#include "nsTransform2D.h"
#include "nsImageMap.h"
#include "nsILoadGroup.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "nsCSSRendering.h"
#include "nsNameSpaceManager.h"
#include <algorithm>
#ifdef ACCESSIBILITY
#  include "nsAccessibilityService.h"
#endif
#include "nsLayoutUtils.h"
#include "nsDisplayList.h"
#include "nsIContent.h"
#include "mozilla/dom/Selection.h"
#include "nsIURIMutator.h"

#include "imgIContainer.h"
#include "imgLoader.h"
#include "imgRequestProxy.h"

#include "nsCSSFrameConstructor.h"
#include "nsRange.h"

#include "nsError.h"
#include "nsBidiUtils.h"
#include "nsBidiPresUtils.h"

#include "gfxRect.h"
#include "ImageRegion.h"
#include "ImageContainer.h"
#include "mozilla/ServoStyleSet.h"
#include "nsBlockFrame.h"
#include "nsStyleStructInlines.h"

#include "mozilla/Preferences.h"

#include "mozilla/dom/Link.h"
#include "mozilla/dom/HTMLAnchorElement.h"
#include "mozilla/dom/BrowserChild.h"

using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::image;
using namespace mozilla::layers;

using mozilla::layout::TextDrawTarget;

class nsDisplayGradient final : public nsPaintedDisplayItem {
 public:
  nsDisplayGradient(nsDisplayListBuilder* aBuilder, nsImageFrame* aFrame)
      : nsPaintedDisplayItem(aBuilder, aFrame) {
    MOZ_COUNT_CTOR(nsDisplayGradient);
  }
  ~nsDisplayGradient() final { MOZ_COUNT_DTOR(nsDisplayGradient); }

  nsRect GetBounds(bool* aSnap) const {
    *aSnap = true;
    return Frame()->GetContentRectRelativeToSelf() + ToReferenceFrame();
  }

  nsRect GetBounds(nsDisplayListBuilder*, bool* aSnap) const final {
    return GetBounds(aSnap);
  }

  void Paint(nsDisplayListBuilder*, gfxContext* aCtx) final;

  bool CreateWebRenderCommands(mozilla::wr::DisplayListBuilder&,
                               mozilla::wr::IpcResourceUpdateQueue&,
                               const StackingContextHelper&,
                               mozilla::layers::RenderRootStateManager*,
                               nsDisplayListBuilder*) final;

  NS_DISPLAY_DECL_NAME("Gradient", TYPE_GRADIENT)
};

void nsDisplayGradient::Paint(nsDisplayListBuilder* aBuilder,
                              gfxContext* aCtx) {
  auto* frame = static_cast<nsImageFrame*>(Frame());
  nsImageRenderer imageRenderer(frame, frame->GetImageFromStyle(),
                                aBuilder->GetImageRendererFlags());
  nsSize size = frame->GetSize();
  imageRenderer.SetPreferredSize({}, size);

  ImgDrawResult result;
  if (!imageRenderer.PrepareImage()) {
    result = imageRenderer.PrepareResult();
  } else {
    nsRect dest(ToReferenceFrame(), size);
    result = imageRenderer.DrawLayer(
        frame->PresContext(), *aCtx, dest, dest, dest.TopLeft(),
        GetPaintRect(aBuilder, aCtx), dest.Size(), /* aOpacity = */ 1.0f);
  }
  Unused << result;
}

bool nsDisplayGradient::CreateWebRenderCommands(
    wr::DisplayListBuilder& aBuilder, wr::IpcResourceUpdateQueue& aResources,
    const StackingContextHelper& aSc,
    mozilla::layers::RenderRootStateManager* aManager,
    nsDisplayListBuilder* aDisplayListBuilder) {
  auto* frame = static_cast<nsImageFrame*>(Frame());
  nsImageRenderer imageRenderer(frame, frame->GetImageFromStyle(),
                                aDisplayListBuilder->GetImageRendererFlags());
  nsSize size = frame->GetSize();
  imageRenderer.SetPreferredSize({}, size);

  ImgDrawResult result;
  if (!imageRenderer.PrepareImage()) {
    result = imageRenderer.PrepareResult();
  } else {
    nsRect dest(ToReferenceFrame(), size);
    result = imageRenderer.BuildWebRenderDisplayItemsForLayer(
        frame->PresContext(), aBuilder, aResources, aSc, aManager, this, dest,
        dest, dest.TopLeft(), dest, dest.Size(),
        /* aOpacity = */ 1.0f);
    if (result == ImgDrawResult::NOT_SUPPORTED) {
      return false;
    }
  }
  return true;
}

// sizes (pixels) for image icon, padding and border frame
#define ICON_SIZE (16)
#define ICON_PADDING (3)
#define ALT_BORDER_WIDTH (1)

// Default alignment value (so we can tell an unset value from a set value)
#define ALIGN_UNSET uint8_t(-1)

class BrokenImageIcon final : public imgINotificationObserver {
  // private class that wraps the data and logic needed for
  // broken image and loading image icons
 public:
  explicit BrokenImageIcon(const nsImageFrame& aFrame);
  static void Shutdown();

  NS_DECL_ISUPPORTS
  NS_DECL_IMGINOTIFICATIONOBSERVER

  static imgRequestProxy* GetImage(nsImageFrame* aFrame) {
    return Get(*aFrame).mImage.get();
  }

  static void AddObserver(nsImageFrame* aFrame) {
    auto& instance = Get(*aFrame);
    MOZ_ASSERT(!instance.mObservers.Contains(aFrame),
               "Observer shouldn't aleady be in array");
    instance.mObservers.AppendElement(aFrame);
  }

  static void RemoveObserver(nsImageFrame* aFrame) {
    auto& instance = Get(*aFrame);
    DebugOnly<bool> didRemove = instance.mObservers.RemoveElement(aFrame);
    MOZ_ASSERT(didRemove, "Observer not in array");
  }

 private:
  static BrokenImageIcon& Get(const nsImageFrame& aFrame) {
    if (!gSingleton) {
      gSingleton = new BrokenImageIcon(aFrame);
    }
    return *gSingleton;
  }

  ~BrokenImageIcon() = default;

  nsTObserverArray<nsImageFrame*> mObservers;
  RefPtr<imgRequestProxy> mImage;

  static StaticRefPtr<BrokenImageIcon> gSingleton;
};

StaticRefPtr<BrokenImageIcon> BrokenImageIcon::gSingleton;

NS_IMPL_ISUPPORTS(BrokenImageIcon, imgINotificationObserver)

BrokenImageIcon::BrokenImageIcon(const nsImageFrame& aFrame) {
  constexpr auto brokenSrc = u"resource://gre-resources/broken-image.png"_ns;
  nsCOMPtr<nsIURI> realURI;
  NS_NewURI(getter_AddRefs(realURI), brokenSrc);

  MOZ_ASSERT(realURI, "how?");
  if (NS_WARN_IF(!realURI)) {
    return;
  }

  nsPresContext* pc = aFrame.PresContext();
  Document* doc = pc->Document();
  RefPtr<imgLoader> il = nsContentUtils::GetImgLoaderForDocument(doc);

  nsCOMPtr<nsILoadGroup> loadGroup = doc->GetDocumentLoadGroup();

  // For icon loads, we don't need to merge with the loadgroup flags
  const nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL;
  const nsContentPolicyType contentPolicyType =
      nsIContentPolicy::TYPE_INTERNAL_IMAGE;

  nsresult rv =
      il->LoadImage(realURI, /* icon URI */
                    nullptr, /* initial document URI; this is only
                               relevant for cookies, so does not
                               apply to icons. */
                    nullptr, /* referrer (not relevant for icons) */
                    nullptr, /* principal (not relevant for icons) */
                    0, loadGroup, this, nullptr, /* No context */
                    nullptr, /* Not associated with any particular document */
                    loadFlags, nullptr, contentPolicyType, u""_ns,
                    false, /* aUseUrgentStartForChannel */
                    false, /* aLinkPreload */
                    0, getter_AddRefs(mImage));
  Unused << NS_WARN_IF(NS_FAILED(rv));
}

void BrokenImageIcon::Shutdown() {
  if (!gSingleton) {
    return;
  }
  if (gSingleton->mImage) {
    gSingleton->mImage->CancelAndForgetObserver(NS_ERROR_FAILURE);
    gSingleton->mImage = nullptr;
  }
  gSingleton = nullptr;
}

void BrokenImageIcon::Notify(imgIRequest* aRequest, int32_t aType,
                             const nsIntRect* aData) {
  MOZ_ASSERT(aRequest);

  if (aType != imgINotificationObserver::LOAD_COMPLETE &&
      aType != imgINotificationObserver::FRAME_UPDATE) {
    return;
  }

  if (aType == imgINotificationObserver::LOAD_COMPLETE) {
    nsCOMPtr<imgIContainer> image;
    aRequest->GetImage(getter_AddRefs(image));
    if (!image) {
      return;
    }

    // Retrieve the image's intrinsic size.
    int32_t width = 0;
    int32_t height = 0;
    image->GetWidth(&width);
    image->GetHeight(&height);

    // Request a decode at that size.
    image->RequestDecodeForSize(IntSize(width, height),
                                imgIContainer::DECODE_FLAGS_DEFAULT |
                                    imgIContainer::FLAG_HIGH_QUALITY_SCALING);
  }

  for (nsImageFrame* frame : mObservers.ForwardRange()) {
    frame->InvalidateFrame();
  }
}

// test if the width and height are fixed, looking at the style data
// This is used by nsImageFrame::ImageFrameTypeFor and should not be used for
// layout decisions.
static bool HaveSpecifiedSize(const nsStylePosition* aStylePosition) {
  // check the width and height values in the reflow input's style struct
  // - if width and height are specified as either coord or percentage, then
  //   the size of the image frame is constrained
  return aStylePosition->mWidth.IsLengthPercentage() &&
         aStylePosition->mHeight.IsLengthPercentage();
}

template <typename SizeOrMaxSize>
static bool DependsOnIntrinsicSize(const SizeOrMaxSize& aMinOrMaxSize) {
  auto length = nsIFrame::ToExtremumLength(aMinOrMaxSize);
  if (!length) {
    return false;
  }
  switch (*length) {
    case nsIFrame::ExtremumLength::MinContent:
    case nsIFrame::ExtremumLength::MaxContent:
    case nsIFrame::ExtremumLength::FitContent:
    case nsIFrame::ExtremumLength::FitContentFunction:
      return true;
    case nsIFrame::ExtremumLength::MozAvailable:
      return false;
  }
  MOZ_ASSERT_UNREACHABLE("Unknown sizing keyword?");
  return false;
}

// Decide whether we can optimize away reflows that result from the
// image's intrinsic size changing.
static bool SizeDependsOnIntrinsicSize(const ReflowInput& aReflowInput) {
  const auto& position = *aReflowInput.mStylePosition;
  WritingMode wm = aReflowInput.GetWritingMode();
  // Don't try to make this optimization when an image has percentages
  // in its 'width' or 'height'.  The percentages might be treated like
  // auto (especially for intrinsic width calculations and for heights).
  //
  // min-width: min-content and such can also affect our intrinsic size.
  // but note that those keywords on the block axis behave like auto, so we
  // don't need to check them.
  //
  // Flex item's min-[width|height]:auto resolution depends on intrinsic size.
  return !position.mHeight.ConvertsToLength() ||
         !position.mWidth.ConvertsToLength() ||
         DependsOnIntrinsicSize(position.MinISize(wm)) ||
         DependsOnIntrinsicSize(position.MaxISize(wm)) ||
         aReflowInput.mFrame->IsFlexItem();
}

nsIFrame* NS_NewImageFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
  return new (aPresShell) nsImageFrame(aStyle, aPresShell->GetPresContext(),
                                       nsImageFrame::Kind::ImageLoadingContent);
}

nsIFrame* NS_NewXULImageFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
  return new (aPresShell) nsImageFrame(aStyle, aPresShell->GetPresContext(),
                                       nsImageFrame::Kind::XULImage);
}

nsIFrame* NS_NewImageFrameForContentProperty(PresShell* aPresShell,
                                             ComputedStyle* aStyle) {
  return new (aPresShell) nsImageFrame(aStyle, aPresShell->GetPresContext(),
                                       nsImageFrame::Kind::ContentProperty);
}

nsIFrame* NS_NewImageFrameForGeneratedContentIndex(PresShell* aPresShell,
                                                   ComputedStyle* aStyle) {
  return new (aPresShell)
      nsImageFrame(aStyle, aPresShell->GetPresContext(),
                   nsImageFrame::Kind::ContentPropertyAtIndex);
}

nsIFrame* NS_NewImageFrameForListStyleImage(PresShell* aPresShell,
                                            ComputedStyle* aStyle) {
  return new (aPresShell) nsImageFrame(aStyle, aPresShell->GetPresContext(),
                                       nsImageFrame::Kind::ListStyleImage);
}

bool nsImageFrame::ShouldShowBrokenImageIcon() const {
  // NOTE(emilio, https://github.com/w3c/csswg-drafts/issues/2832): WebKit and
  // Blink behave differently here for content: url(..), for now adapt to
  // Blink's behavior.
  if (mKind != Kind::ImageLoadingContent) {
    return false;
  }

  if (!StaticPrefs::browser_display_show_image_placeholders()) {
    return false;
  }

  // <img alt=""> is special, and it shouldn't draw the broken image icon,
  // unlike the no-alt attribute or non-empty-alt-attribute case.
  if (auto* image = HTMLImageElement::FromNode(mContent)) {
    const nsAttrValue* alt = image->GetParsedAttr(nsGkAtoms::alt);
    if (alt && alt->IsEmptyString()) {
      return false;
    }
  }

  // check for broken images. valid null images (eg. img src="") are
  // not considered broken because they have no image requests
  if (nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest()) {
    uint32_t imageStatus;
    return NS_SUCCEEDED(currentRequest->GetImageStatus(&imageStatus)) &&
           (imageStatus & imgIRequest::STATUS_ERROR);
  }

  nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
  MOZ_ASSERT(imageLoader);
  // Show the broken image icon only if we've tried to perform a load at all
  // (that is, if we have a current uri).
  nsCOMPtr<nsIURI> currentURI = imageLoader->GetCurrentURI();
  return !!currentURI;
}

nsImageFrame* nsImageFrame::CreateContinuingFrame(
    mozilla::PresShell* aPresShell, ComputedStyle* aStyle) const {
  return new (aPresShell)
      nsImageFrame(aStyle, aPresShell->GetPresContext(), mKind);
}

NS_IMPL_FRAMEARENA_HELPERS(nsImageFrame)

nsImageFrame::nsImageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext,
                           ClassID aID, Kind aKind)
    : nsAtomicContainerFrame(aStyle, aPresContext, aID),
      mIntrinsicSize(0, 0),
      mKind(aKind) {
  EnableVisibilityTracking();
}

nsImageFrame::~nsImageFrame() = default;

NS_QUERYFRAME_HEAD(nsImageFrame)
  NS_QUERYFRAME_ENTRY(nsImageFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsAtomicContainerFrame)

#ifdef ACCESSIBILITY
a11y::AccType nsImageFrame::AccessibleType() {
  if (mKind == Kind::ListStyleImage) {
    // This is an HTMLListBulletAccessible.
    return a11y::eNoType;
  }

  // Don't use GetImageMap() to avoid reentrancy into accessibility.
  if (HasImageMap()) {
    return a11y::eHTMLImageMapType;
  }

  return a11y::eImageType;
}
#endif

void nsImageFrame::DisconnectMap() {
  if (!mImageMap) {
    return;
  }

  mImageMap->Destroy();
  mImageMap = nullptr;

#ifdef ACCESSIBILITY
  if (nsAccessibilityService* accService = GetAccService()) {
    accService->RecreateAccessible(PresShell(), mContent);
  }
#endif
}

void nsImageFrame::Destroy(DestroyContext& aContext) {
  MaybeSendIntrinsicSizeAndRatioToEmbedder(Nothing(), Nothing());

  if (mReflowCallbackPosted) {
    PresShell()->CancelReflowCallback(this);
    mReflowCallbackPosted = false;
  }

  // Tell our image map, if there is one, to clean up
  // This causes the nsImageMap to unregister itself as
  // a DOM listener.
  DisconnectMap();

  MOZ_ASSERT(mListener);

  if (mKind == Kind::ImageLoadingContent) {
    MOZ_ASSERT(!mOwnedRequest);
    MOZ_ASSERT(!mOwnedRequestRegistered);
    nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
    MOZ_ASSERT(imageLoader);

    // Notify our image loading content that we are going away so it can
    // deregister with our refresh driver.
    imageLoader->FrameDestroyed(this);
    imageLoader->RemoveNativeObserver(mListener);
  } else {
    DeinitOwnedRequest();
  }

  // set the frame to null so we don't send messages to a dead object.
  mListener->SetFrame(nullptr);
  mListener = nullptr;

  // If we were displaying an icon, take ourselves off the list
  if (mDisplayingIcon) {
    BrokenImageIcon::RemoveObserver(this);
  }

  nsAtomicContainerFrame::Destroy(aContext);
}

void nsImageFrame::DeinitOwnedRequest() {
  MOZ_ASSERT(mKind != Kind::ImageLoadingContent);
  if (!mOwnedRequest) {
    return;
  }
  PresContext()->Document()->ImageTracker()->Remove(mOwnedRequest);
  nsLayoutUtils::DeregisterImageRequest(PresContext(), mOwnedRequest,
                                        &mOwnedRequestRegistered);
  mOwnedRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
  mOwnedRequest = nullptr;
}

void nsImageFrame::DidSetComputedStyle(ComputedStyle* aOldStyle) {
  nsAtomicContainerFrame::DidSetComputedStyle(aOldStyle);

  // A ::marker's default size is calculated from the font's em-size.
  if (IsForMarkerPseudo()) {
    mIntrinsicSize = IntrinsicSize(0, 0);
    UpdateIntrinsicSize();
  }

  // Normal "owned" images reframe when `content` or `list-style-image` change,
  // but XUL images don't (and we don't really need to). So deal with the
  // dynamic list-style-image change in that case.
  //
  // TODO(emilio): We might want to do the same for regular list-style-image or
  // even simple content: url() changes.
  if (mKind == Kind::XULImage) {
    if (!mContent->AsElement()->HasNonEmptyAttr(nsGkAtoms::src) && aOldStyle &&
        aOldStyle->StyleList()->mListStyleImage !=
            StyleList()->mListStyleImage) {
      UpdateXULImage();
    }
    if (!mOwnedRequest && aOldStyle &&
        aOldStyle->StyleDisplay()->EffectiveAppearance() !=
            StyleDisplay()->EffectiveAppearance()) {
      UpdateIntrinsicSize();
    }
  }

  // We need to update our orientation either if we had no ComputedStyle before
  // because this is the first time it's been set, or if the image-orientation
  // property changed from its previous value.
  bool shouldUpdateOrientation = false;
  nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest();
  const auto newOrientation =
      StyleVisibility()->UsedImageOrientation(currentRequest);
  if (mImage) {
    if (aOldStyle) {
      auto oldOrientation =
          aOldStyle->StyleVisibility()->UsedImageOrientation(currentRequest);
      shouldUpdateOrientation = oldOrientation != newOrientation;
    } else {
      shouldUpdateOrientation = true;
    }
  }

  if (shouldUpdateOrientation) {
    nsCOMPtr<imgIContainer> image(mImage->Unwrap());
    mImage = nsLayoutUtils::OrientImage(image, newOrientation);

    UpdateIntrinsicSize();
    UpdateIntrinsicRatio();
  } else if (!aOldStyle || aOldStyle->StylePosition()->mAspectRatio !=
                               StylePosition()->mAspectRatio) {
    UpdateIntrinsicRatio();
  }
}

static bool SizeIsAvailable(imgIRequest* aRequest) {
  if (!aRequest) {
    return false;
  }

  uint32_t imageStatus = 0;
  nsresult rv = aRequest->GetImageStatus(&imageStatus);
  return NS_SUCCEEDED(rv) && (imageStatus & imgIRequest::STATUS_SIZE_AVAILABLE);
}

const StyleImage* nsImageFrame::GetImageFromStyle() const {
  switch (mKind) {
    case Kind::ImageLoadingContent:
      break;
    case Kind::ListStyleImage:
      MOZ_ASSERT(
          GetParent()->GetContent()->IsGeneratedContentContainerForMarker());
      MOZ_ASSERT(mContent->IsHTMLElement(nsGkAtoms::mozgeneratedcontentimage));
      return &StyleList()->mListStyleImage;
    case Kind::XULImage:
      MOZ_ASSERT(!mContent->AsElement()->HasNonEmptyAttr(nsGkAtoms::src));
      return &StyleList()->mListStyleImage;
    case Kind::ContentProperty:
    case Kind::ContentPropertyAtIndex: {
      uint32_t contentIndex = 0;
      const nsStyleContent* styleContent = StyleContent();
      if (mKind == Kind::ContentPropertyAtIndex) {
        MOZ_RELEASE_ASSERT(
            mContent->IsHTMLElement(nsGkAtoms::mozgeneratedcontentimage));
        contentIndex =
            static_cast<GeneratedImageContent*>(mContent.get())->Index();

        // TODO(emilio): Consider inheriting the `content` property instead of
        // doing this parent traversal?
        nsIFrame* parent = GetParent();
        MOZ_DIAGNOSTIC_ASSERT(
            parent->GetContent()->IsGeneratedContentContainerForMarker() ||
            parent->GetContent()->IsGeneratedContentContainerForAfter() ||
            parent->GetContent()->IsGeneratedContentContainerForBefore());
        nsIFrame* nonAnonymousParent = parent;
        while (nonAnonymousParent->Style()->IsAnonBox()) {
          nonAnonymousParent = nonAnonymousParent->GetParent();
        }
        MOZ_DIAGNOSTIC_ASSERT(parent->GetContent() ==
                              nonAnonymousParent->GetContent());
        styleContent = nonAnonymousParent->StyleContent();
      }
      MOZ_RELEASE_ASSERT(contentIndex < styleContent->ContentCount());
      auto& contentItem = styleContent->ContentAt(contentIndex);
      MOZ_RELEASE_ASSERT(contentItem.IsImage());
      return &contentItem.AsImage();
    }
  }
  MOZ_ASSERT_UNREACHABLE("Don't call me");
  return nullptr;
}

void nsImageFrame::UpdateXULImage() {
  MOZ_ASSERT(mKind == Kind::XULImage);
  DeinitOwnedRequest();

  nsAutoString src;
  nsPresContext* pc = PresContext();
  if (mContent->AsElement()->GetAttr(nsGkAtoms::src, src) && !src.IsEmpty()) {
    nsContentPolicyType contentPolicyType;
    nsCOMPtr<nsIPrincipal> triggeringPrincipal;
    uint64_t requestContextID = 0;
    nsContentUtils::GetContentPolicyTypeForUIImageLoading(
        mContent, getter_AddRefs(triggeringPrincipal), contentPolicyType,
        &requestContextID);
    nsCOMPtr<nsIURI> uri;
    nsContentUtils::NewURIWithDocumentCharset(
        getter_AddRefs(uri), src, pc->Document(), mContent->GetBaseURI());
    if (uri) {
      auto referrerInfo = MakeRefPtr<ReferrerInfo>(*mContent->AsElement());
      nsContentUtils::LoadImage(
          uri, mContent, pc->Document(), triggeringPrincipal, requestContextID,
          referrerInfo, mListener, nsIRequest::LOAD_NORMAL, u""_ns,
          getter_AddRefs(mOwnedRequest), contentPolicyType);
      SetupOwnedRequest();
    }
  } else {
    const auto* image = GetImageFromStyle();
    if (image->IsImageRequestType()) {
      if (imgRequestProxy* proxy = image->GetImageRequest()) {
        proxy->Clone(mListener, pc->Document(), getter_AddRefs(mOwnedRequest));
        SetupOwnedRequest();
      }
    }
  }

  if (!mOwnedRequest) {
    UpdateImage(nullptr, nullptr);
  }
}

void nsImageFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
                        nsIFrame* aPrevInFlow) {
  MOZ_ASSERT_IF(aPrevInFlow,
                aPrevInFlow->Type() == Type() &&
                    static_cast<nsImageFrame*>(aPrevInFlow)->mKind == mKind);

  nsAtomicContainerFrame::Init(aContent, aParent, aPrevInFlow);

  mListener = new nsImageListener(this);

  GetImageMap();  // Ensure to init the image map asap. This is important to
                  // make <area> elements focusable.

  if (StaticPrefs::layout_image_eager_broken_image_icon()) {
    Unused << BrokenImageIcon::GetImage(this);
  }

  nsPresContext* pc = PresContext();
  if (mKind == Kind::ImageLoadingContent) {
    nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(aContent);
    MOZ_ASSERT(imageLoader);
    imageLoader->AddNativeObserver(mListener);
    // We have a PresContext now, so we need to notify the image content node
    // that it can register images.
    imageLoader->FrameCreated(this);
    AssertSyncDecodingHintIsInSync();
    if (nsIDocShell* docShell = pc->GetDocShell()) {
      RefPtr<BrowsingContext> bc = docShell->GetBrowsingContext();
      mIsInObjectOrEmbed = bc->IsEmbedderTypeObjectOrEmbed() &&
                           pc->Document()->IsImageDocument();
    }
  } else if (mKind == Kind::XULImage) {
    UpdateXULImage();
  } else {
    const StyleImage* image = GetImageFromStyle();
    if (image->IsImageRequestType()) {
      if (imgRequestProxy* proxy = image->GetImageRequest()) {
        proxy->Clone(mListener, pc->Document(), getter_AddRefs(mOwnedRequest));
        SetupOwnedRequest();
      }
    }
  }

  // Give image loads associated with an image frame a small priority boost.
  if (nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest()) {
    uint32_t categoryToBoostPriority = imgIRequest::CATEGORY_FRAME_INIT;

    // Increase load priority further if intrinsic size might be important for
    // layout.
    if (!HaveSpecifiedSize(StylePosition())) {
      categoryToBoostPriority |= imgIRequest::CATEGORY_SIZE_QUERY;
    }

    currentRequest->BoostPriority(categoryToBoostPriority);
  }

  MaybeSendIntrinsicSizeAndRatioToEmbedder();
}

void nsImageFrame::SetupOwnedRequest() {
  MOZ_ASSERT(mKind != Kind::ImageLoadingContent);
  if (!mOwnedRequest) {
    return;
  }

  // We're not using AssociateRequestToFrame for the content property, so we
  // need to add it to the image tracker manually.
  PresContext()->Document()->ImageTracker()->Add(mOwnedRequest);

  uint32_t status = 0;
  nsresult rv = mOwnedRequest->GetImageStatus(&status);
  if (NS_FAILED(rv)) {
    return;
  }

  if (status & imgIRequest::STATUS_SIZE_AVAILABLE) {
    nsCOMPtr<imgIContainer> image;
    mOwnedRequest->GetImage(getter_AddRefs(image));
    OnSizeAvailable(mOwnedRequest, image);
  }

  if (status & imgIRequest::STATUS_FRAME_COMPLETE) {
    mFirstFrameComplete = true;
  }

  if (status & imgIRequest::STATUS_IS_ANIMATED) {
    nsLayoutUtils::RegisterImageRequest(PresContext(), mOwnedRequest,
                                        &mOwnedRequestRegistered);
  }
}

static void ScaleIntrinsicSizeForDensity(IntrinsicSize& aSize,
                                         const ImageResolution& aResolution) {
  if (aSize.width) {
    aResolution.ApplyXTo(aSize.width.ref());
  }
  if (aSize.height) {
    aResolution.ApplyYTo(aSize.height.ref());
  }
}

static void ScaleIntrinsicSizeForDensity(imgIContainer* aImage,
                                         nsIContent& aContent,
                                         IntrinsicSize& aSize) {
  ImageResolution resolution = aImage->GetResolution();
  if (auto* image = HTMLImageElement::FromNode(aContent)) {
    if (auto* selector = image->GetResponsiveImageSelector()) {
      resolution.ScaleBy(selector->GetSelectedImageDensity());
    }
  }
  ScaleIntrinsicSizeForDensity(aSize, resolution);
}

static nscoord ListImageDefaultLength(const nsImageFrame& aFrame) {
  // https://drafts.csswg.org/css-lists-3/#image-markers
  // The spec says we should use 1em x 1em, but that seems too large.
  // See disussion in https://github.com/w3c/csswg-drafts/issues/4207
  auto* pc = aFrame.PresContext();
  RefPtr<nsFontMetrics> fm =
      nsLayoutUtils::GetFontMetricsForComputedStyle(aFrame.Style(), pc);
  RefPtr<gfxFont> font = fm->GetThebesFontGroup()->GetFirstValidFont();
  auto emAU =
      font->GetMetrics(fm->Orientation()).emHeight * pc->AppUnitsPerDevPixel();
  return std::max(NSToCoordRound(0.4f * emAU),
                  nsPresContext::CSSPixelsToAppUnits(1));
}

static IntrinsicSize ComputeIntrinsicSize(imgIContainer* aImage,
                                          bool aUseMappedRatio,
                                          nsImageFrame::Kind aKind,
                                          const nsImageFrame& aFrame) {
  const auto containAxes = aFrame.GetContainSizeAxes();
  if (containAxes.IsBoth()) {
    return containAxes.ContainIntrinsicSize(IntrinsicSize(0, 0), aFrame);
  }

  nsSize size;
  if (aImage && NS_SUCCEEDED(aImage->GetIntrinsicSize(&size))) {
    IntrinsicSize intrinsicSize;
    intrinsicSize.width = size.width == -1 ? Nothing() : Some(size.width);
    intrinsicSize.height = size.height == -1 ? Nothing() : Some(size.height);
    if (aKind == nsImageFrame::Kind::ListStyleImage) {
      if (intrinsicSize.width.isNothing() || intrinsicSize.height.isNothing()) {
        nscoord defaultLength = ListImageDefaultLength(aFrame);
        if (intrinsicSize.width.isNothing()) {
          intrinsicSize.width = Some(defaultLength);
        }
        if (intrinsicSize.height.isNothing()) {
          intrinsicSize.height = Some(defaultLength);
        }
      }
    }
    if (aKind == nsImageFrame::Kind::ImageLoadingContent ||
        (aKind == nsImageFrame::Kind::XULImage &&
         aFrame.GetContent()->AsElement()->HasNonEmptyAttr(nsGkAtoms::src))) {
      ScaleIntrinsicSizeForDensity(aImage, *aFrame.GetContent(), intrinsicSize);
    } else {
      ScaleIntrinsicSizeForDensity(intrinsicSize,
                                   aFrame.GetImageFromStyle()->GetResolution());
    }
    return containAxes.ContainIntrinsicSize(intrinsicSize, aFrame);
  }

  if (aKind == nsImageFrame::Kind::ListStyleImage) {
    // Note: images are handled above, this handles gradients etc.
    nscoord defaultLength = ListImageDefaultLength(aFrame);
    return containAxes.ContainIntrinsicSize(
        IntrinsicSize(defaultLength, defaultLength), aFrame);
  }

  if (aKind == nsImageFrame::Kind::XULImage && aFrame.IsThemed()) {
    nsPresContext* pc = aFrame.PresContext();
    // FIXME: const_cast here is a bit evil but IsThemed and so does the same.
    const auto widgetSize = pc->Theme()->GetMinimumWidgetSize(
        pc, const_cast<nsImageFrame*>(&aFrame),
        aFrame.StyleDisplay()->EffectiveAppearance());
    const IntrinsicSize intrinsicSize(
        LayoutDeviceIntSize::ToAppUnits(widgetSize, pc->AppUnitsPerDevPixel()));
    return containAxes.ContainIntrinsicSize(intrinsicSize, aFrame);
  }

  if (aFrame.ShouldShowBrokenImageIcon()) {
    nscoord edgeLengthToUse = nsPresContext::CSSPixelsToAppUnits(
        ICON_SIZE + (2 * (ICON_PADDING + ALT_BORDER_WIDTH)));
    return containAxes.ContainIntrinsicSize(
        IntrinsicSize(edgeLengthToUse, edgeLengthToUse), aFrame);
  }

  if (aUseMappedRatio && aFrame.StylePosition()->mAspectRatio.HasRatio()) {
    return IntrinsicSize();
  }

  return IntrinsicSize(0, 0);
}

// For compat reasons, see bug 1602047, we don't use the intrinsic ratio from
// width="" and height="" for images with no src attribute (no request).
//
// But we shouldn't get fooled by <img loading=lazy>. We do want to apply the
// ratio then...
bool nsImageFrame::ShouldUseMappedAspectRatio() const {
  if (mKind != Kind::ImageLoadingContent) {
    return true;
  }
  nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest();
  if (currentRequest) {
    return true;
  }
  // TODO(emilio): Investigate the compat situation of the above check, maybe we
  // can just check for empty src attribute or something...
  auto* image = HTMLImageElement::FromNode(mContent);
  return image && image->IsAwaitingLoadOrLazyLoading();
}

bool nsImageFrame::UpdateIntrinsicSize() {
  IntrinsicSize oldIntrinsicSize = mIntrinsicSize;
  mIntrinsicSize =
      ComputeIntrinsicSize(mImage, ShouldUseMappedAspectRatio(), mKind, *this);
  return mIntrinsicSize != oldIntrinsicSize;
}

static AspectRatio ComputeIntrinsicRatio(imgIContainer* aImage,
                                         bool aUseMappedRatio,
                                         const nsImageFrame& aFrame) {
  if (aFrame.GetContainSizeAxes().IsAny()) {
    return AspectRatio();
  }

  if (aImage) {
    if (Maybe<AspectRatio> fromImage = aImage->GetIntrinsicRatio()) {
      return *fromImage;
    }
  }
  if (aUseMappedRatio) {
    const StyleAspectRatio& ratio = aFrame.StylePosition()->mAspectRatio;
    if (ratio.auto_ && ratio.HasRatio()) {
      // Return the mapped intrinsic aspect ratio stored in
      // nsStylePosition::mAspectRatio.
      return ratio.ratio.AsRatio().ToLayoutRatio(UseBoxSizing::Yes);
    }
  }
  if (aFrame.ShouldShowBrokenImageIcon()) {
    return AspectRatio(1.0f);
  }
  return AspectRatio();
}

bool nsImageFrame::UpdateIntrinsicRatio() {
  AspectRatio oldIntrinsicRatio = mIntrinsicRatio;
  mIntrinsicRatio =
      ComputeIntrinsicRatio(mImage, ShouldUseMappedAspectRatio(), *this);
  return mIntrinsicRatio != oldIntrinsicRatio;
}

bool nsImageFrame::GetSourceToDestTransform(nsTransform2D& aTransform) {
  // First, figure out destRect (the rect we're rendering into).
  // NOTE: We use mComputedSize instead of just GetContentRectRelativeToSelf()'s
  // own size here, because GetContentRectRelativeToSelf() might be smaller if
  // we're fragmented, whereas mComputedSize has our full content-box size
  // (which we need for ComputeObjectDestRect to work correctly).
  nsRect constraintRect(GetContentRectRelativeToSelf().TopLeft(),
                        mComputedSize);
  constraintRect.y -= GetContinuationOffset();

  nsRect destRect = nsLayoutUtils::ComputeObjectDestRect(
      constraintRect, mIntrinsicSize, mIntrinsicRatio, StylePosition());
  // Set the translation components, based on destRect
  // XXXbz does this introduce rounding errors because of the cast to
  // float?  Should we just manually add that stuff in every time
  // instead?
  aTransform.SetToTranslate(float(destRect.x), float(destRect.y));

  // NOTE(emilio): This intrinsicSize is not the same as the layout intrinsic
  // size (mIntrinsicSize), which can be scaled due to ResponsiveImageSelector,
  // see ScaleIntrinsicSizeForDensity.
  nsSize intrinsicSize;
  if (!mImage || !NS_SUCCEEDED(mImage->GetIntrinsicSize(&intrinsicSize)) ||
      intrinsicSize.IsEmpty()) {
    return false;
  }

  aTransform.SetScale(float(destRect.width) / float(intrinsicSize.width),
                      float(destRect.height) / float(intrinsicSize.height));
  return true;
}

// This function checks whether the given request is the current request for our
// mContent.
bool nsImageFrame::IsPendingLoad(imgIRequest* aRequest) const {
  // Default to pending load in case of errors
  if (mKind != Kind::ImageLoadingContent) {
    MOZ_ASSERT(aRequest == mOwnedRequest);
    return false;
  }

  nsCOMPtr<nsIImageLoadingContent> imageLoader(do_QueryInterface(mContent));
  MOZ_ASSERT(imageLoader);

  int32_t requestType = nsIImageLoadingContent::UNKNOWN_REQUEST;
  imageLoader->GetRequestType(aRequest, &requestType);

  return requestType != nsIImageLoadingContent::CURRENT_REQUEST;
}

nsRect nsImageFrame::SourceRectToDest(const nsIntRect& aRect) {
  // When scaling the image, row N of the source image may (depending on
  // the scaling function) be used to draw any row in the destination image
  // between floor(F * (N-1)) and ceil(F * (N+1)), where F is the
  // floating-point scaling factor.  The same holds true for columns.
  // So, we start by computing that bound without the floor and ceiling.

  nsRect r(nsPresContext::CSSPixelsToAppUnits(aRect.x - 1),
           nsPresContext::CSSPixelsToAppUnits(aRect.y - 1),
           nsPresContext::CSSPixelsToAppUnits(aRect.width + 2),
           nsPresContext::CSSPixelsToAppUnits(aRect.height + 2));

  nsTransform2D sourceToDest;
  if (!GetSourceToDestTransform(sourceToDest)) {
    // Failed to generate transform matrix. Return our whole content area,
    // to be on the safe side (since this method is used for generating
    // invalidation rects).
    return GetContentRectRelativeToSelf();
  }

  sourceToDest.TransformCoord(&r.x, &r.y, &r.width, &r.height);

  // Now, round the edges out to the pixel boundary.
  nscoord scale = nsPresContext::CSSPixelsToAppUnits(1);
  nscoord right = r.x + r.width;
  nscoord bottom = r.y + r.height;

  r.x -= (scale + (r.x % scale)) % scale;
  r.y -= (scale + (r.y % scale)) % scale;
  r.width = right + ((scale - (right % scale)) % scale) - r.x;
  r.height = bottom + ((scale - (bottom % scale)) % scale) - r.y;

  return r;
}

static bool ImageOk(ElementState aState) {
  return !aState.HasState(ElementState::BROKEN);
}

static bool HasAltText(const Element& aElement) {
  // We always return some alternate text for <input>, see
  // nsCSSFrameConstructor::GetAlternateTextFor.
  if (aElement.IsHTMLElement(nsGkAtoms::input)) {
    return true;
  }

  MOZ_ASSERT(aElement.IsHTMLElement(nsGkAtoms::img));
  return aElement.HasNonEmptyAttr(nsGkAtoms::alt);
}

bool nsImageFrame::ShouldCreateImageFrameForContentProperty(
    const Element& aElement, const ComputedStyle& aStyle) {
  if (aElement.IsRootOfNativeAnonymousSubtree()) {
    return false;
  }
  const auto& content = aStyle.StyleContent()->mContent;
  if (!content.IsItems()) {
    return false;
  }
  Span<const StyleContentItem> items = content.AsItems().AsSpan();
  return items.Length() == 1 && items[0].IsImage();
}

// Check if we want to use an image frame or just let the frame constructor make
// us into an inline, and if so, which kind of image frame should we create.
/* static */
auto nsImageFrame::ImageFrameTypeFor(const Element& aElement,
                                     const ComputedStyle& aStyle)
    -> ImageFrameType {
  if (ShouldCreateImageFrameForContentProperty(aElement, aStyle)) {
    // Prefer the content property, for compat reasons, see bug 1484928.
    return ImageFrameType::ForContentProperty;
  }

  if (ImageOk(aElement.State())) {
    // Image is fine or loading; do the image frame thing
    return ImageFrameType::ForElementRequest;
  }

  if (aStyle.StyleUIReset()->mMozForceBrokenImageIcon) {
    return ImageFrameType::ForElementRequest;
  }

  if (!HasAltText(aElement)) {
    return ImageFrameType::ForElementRequest;
  }

  // FIXME(emilio, bug 1788767): We definitely don't reframe when
  // HaveSpecifiedSize changes...
  if (aElement.OwnerDoc()->GetCompatibilityMode() == eCompatibility_NavQuirks &&
      HaveSpecifiedSize(aStyle.StylePosition())) {
    return ImageFrameType::ForElementRequest;
  }

  return ImageFrameType::None;
}

void nsImageFrame::Notify(imgIRequest* aRequest, int32_t aType,
                          const nsIntRect* aRect) {
  if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
    nsCOMPtr<imgIContainer> image;
    aRequest->GetImage(getter_AddRefs(image));
    return OnSizeAvailable(aRequest, image);
  }

  if (aType == imgINotificationObserver::FRAME_UPDATE) {
    return OnFrameUpdate(aRequest, aRect);
  }

  if (aType == imgINotificationObserver::FRAME_COMPLETE) {
    mFirstFrameComplete = true;
  }

  if (aType == imgINotificationObserver::IS_ANIMATED &&
      mKind != Kind::ImageLoadingContent) {
    nsLayoutUtils::RegisterImageRequest(PresContext(), mOwnedRequest,
                                        &mOwnedRequestRegistered);
  }

  if (aType == imgINotificationObserver::LOAD_COMPLETE) {
    uint32_t imgStatus;
    aRequest->GetImageStatus(&imgStatus);
    nsresult status =
        imgStatus & imgIRequest::STATUS_ERROR ? NS_ERROR_FAILURE : NS_OK;
    return OnLoadComplete(aRequest, status);
  }
}

void nsImageFrame::OnSizeAvailable(imgIRequest* aRequest,
                                   imgIContainer* aImage) {
  if (!aImage) {
    return;
  }

  /* Get requested animation policy from the pres context:
   *   normal = 0
   *   one frame = 1
   *   one loop = 2
   */
  aImage->SetAnimationMode(PresContext()->ImageAnimationMode());

  if (IsPendingLoad(aRequest)) {
    // We don't care
    return;
  }

  UpdateImage(aRequest, aImage);
}

void nsImageFrame::UpdateImage(imgIRequest* aRequest, imgIContainer* aImage) {
  if (SizeIsAvailable(aRequest)) {
    StyleImageOrientation orientation =
        StyleVisibility()->UsedImageOrientation(aRequest);
    // This is valid and for the current request, so update our stored image
    // container, orienting according to our style.
    mImage = nsLayoutUtils::OrientImage(aImage, orientation);
    MOZ_ASSERT(mImage);
  } else {
    // We no longer have a valid image, so release our stored image container.
    mImage = mPrevImage = nullptr;
    if (mKind == Kind::ListStyleImage) {
      auto* genContent = static_cast<GeneratedImageContent*>(GetContent());
      genContent->NotifyLoadFailed();
      // No need to continue below since the above state change will destroy
      // this frame.
      return;
    }
  }

  UpdateIntrinsicSizeAndRatio();

  if (!GotInitialReflow()) {
    return;
  }

  // We're going to need to repaint now either way.
  InvalidateFrame();
}

void nsImageFrame::OnFrameUpdate(imgIRequest* aRequest,
                                 const nsIntRect* aRect) {
  if (NS_WARN_IF(!aRect)) {
    return;
  }

  if (!GotInitialReflow()) {
    // Don't bother to do anything; we have a reflow coming up!
    return;
  }

  if (mFirstFrameComplete && !StyleVisibility()->IsVisible()) {
    return;
  }

  if (IsPendingLoad(aRequest)) {
    // We don't care
    return;
  }

  nsIntRect layerInvalidRect =
      mImage ? mImage->GetImageSpaceInvalidationRect(*aRect) : *aRect;

  if (layerInvalidRect.IsEqualInterior(GetMaxSizedIntRect())) {
    // Invalidate our entire area.
    InvalidateSelf(nullptr, nullptr);
    return;
  }

  nsRect frameInvalidRect = SourceRectToDest(layerInvalidRect);
  InvalidateSelf(&layerInvalidRect, &frameInvalidRect);
}

void nsImageFrame::InvalidateSelf(const nsIntRect* aLayerInvalidRect,
                                  const nsRect* aFrameInvalidRect) {
  // Check if WebRender has interacted with this frame. If it has
  // we need to let it know that things have changed.
  const auto type = DisplayItemType::TYPE_IMAGE;
  const auto providerId = mImage ? mImage->GetProviderId() : 0;
  if (WebRenderUserData::ProcessInvalidateForImage(this, type, providerId)) {
    return;
  }

  InvalidateLayer(type, aLayerInvalidRect, aFrameInvalidRect);

  if (!mFirstFrameComplete) {
    InvalidateLayer(DisplayItemType::TYPE_ALT_FEEDBACK, aLayerInvalidRect,
                    aFrameInvalidRect);
  }
}

void nsImageFrame::MaybeSendIntrinsicSizeAndRatioToEmbedder() {
  MaybeSendIntrinsicSizeAndRatioToEmbedder(Some(GetIntrinsicSize()),
                                           Some(GetAspectRatio()));
}

void nsImageFrame::MaybeSendIntrinsicSizeAndRatioToEmbedder(
    Maybe<IntrinsicSize> aIntrinsicSize, Maybe<AspectRatio> aIntrinsicRatio) {
  if (!mIsInObjectOrEmbed || !mImage) {
    return;
  }

  nsCOMPtr<nsIDocShell> docShell = PresContext()->GetDocShell();
  if (!docShell) {
    return;
  }

  BrowsingContext* bc = docShell->GetBrowsingContext();
  if (!bc) {
    return;
  }
  MOZ_ASSERT(bc->IsContentSubframe());

  if (bc->GetParent()->IsInProcess()) {
    if (Element* embedder = bc->GetEmbedderElement()) {
      if (nsCOMPtr<nsIObjectLoadingContent> olc = do_QueryInterface(embedder)) {
        static_cast<nsObjectLoadingContent*>(olc.get())
            ->SubdocumentIntrinsicSizeOrRatioChanged(aIntrinsicSize,
                                                     aIntrinsicRatio);
      } else {
        MOZ_ASSERT_UNREACHABLE("Got out of sync?");
      }
      return;
    }
  }

  if (BrowserChild* browserChild = BrowserChild::GetFrom(docShell)) {
    Unused << browserChild->SendIntrinsicSizeOrRatioChanged(aIntrinsicSize,
                                                            aIntrinsicRatio);
  }
}

void nsImageFrame::OnLoadComplete(imgIRequest* aRequest, nsresult aStatus) {
  NotifyNewCurrentRequest(aRequest, aStatus);
}

void nsImageFrame::ElementStateChanged(ElementState aStates) {
  if (!(aStates & ElementState::BROKEN)) {
    return;
  }
  if (mKind != Kind::ImageLoadingContent) {
    return;
  }
  if (!ImageOk(mContent->AsElement()->State())) {
    UpdateImage(nullptr, nullptr);
  }
}

void nsImageFrame::ResponsiveContentDensityChanged() {
  UpdateIntrinsicSizeAndRatio();
}

void nsImageFrame::UpdateIntrinsicSizeAndRatio() {
  bool intrinsicSizeOrRatioChanged = [&] {
    // NOTE(emilio): We intentionally want to call both functions and avoid
    // short-circuiting.
    bool intrinsicSizeChanged = UpdateIntrinsicSize();
    bool intrinsicRatioChanged = UpdateIntrinsicRatio();
    return intrinsicSizeChanged || intrinsicRatioChanged;
  }();

  if (!intrinsicSizeOrRatioChanged) {
    return;
  }

  // Our aspect-ratio property value changed, and an embedding <object> or
  // <embed> might care about that.
  MaybeSendIntrinsicSizeAndRatioToEmbedder();

  if (!GotInitialReflow()) {
    return;
  }

  // Now we need to reflow if we have an unconstrained size and have
  // already gotten the initial reflow.
  if (!HasAnyStateBits(IMAGE_SIZECONSTRAINED)) {
    PresShell()->FrameNeedsReflow(
        this, IntrinsicDirty::FrameAncestorsAndDescendants, NS_FRAME_IS_DIRTY);
  } else if (PresShell()->IsActive()) {
    // We've already gotten the initial reflow, and our size hasn't changed,
    // so we're ready to request a decode.
    MaybeDecodeForPredictedSize();
  }
}

void nsImageFrame::NotifyNewCurrentRequest(imgIRequest* aRequest,
                                           nsresult aStatus) {
  nsCOMPtr<imgIContainer> image;
  aRequest->GetImage(getter_AddRefs(image));
  NS_ASSERTION(image || NS_FAILED(aStatus),
               "Successful load with no container?");
  UpdateImage(aRequest, image);
}

void nsImageFrame::MaybeDecodeForPredictedSize() {
  // Check that we're ready to decode.
  if (!mImage) {
    return;  // Nothing to do yet.
  }

  if (mComputedSize.IsEmpty()) {
    return;  // We won't draw anything, so no point in decoding.
  }

  if (GetVisibility() != Visibility::ApproximatelyVisible) {
    return;  // We're not visible, so don't decode.
  }

  // OK, we're ready to decode. Compute the scale to the screen...
  mozilla::PresShell* presShell = PresContext()->PresShell();
  MatrixScales scale =
      ScaleFactor<UnknownUnits, UnknownUnits>(
          presShell->GetCumulativeResolution()) *
      nsLayoutUtils::GetTransformToAncestorScaleExcludingAnimated(this);
  auto resolutionToScreen = ViewAs<LayoutDeviceToScreenScale2D>(scale);

  // If we are in a remote browser, then apply scaling from ancestor browsers
  if (BrowserChild* browserChild = BrowserChild::GetFrom(presShell)) {
    resolutionToScreen =
        resolutionToScreen * ViewAs<ScreenToScreenScale2D>(
                                 browserChild->GetEffectsInfo().mRasterScale);
  }

  // ...and this frame's content box...
  const nsPoint offset =
      GetOffsetToCrossDoc(nsLayoutUtils::GetReferenceFrame(this));
  const nsRect frameContentBox = GetContentRectRelativeToSelf() + offset;

  // ...and our predicted dest rect...
  const int32_t factor = PresContext()->AppUnitsPerDevPixel();
  const LayoutDeviceRect destRect = LayoutDeviceRect::FromAppUnits(
      PredictedDestRect(frameContentBox), factor);

  // ...and use them to compute our predicted size in screen pixels.
  const ScreenSize predictedScreenSize = destRect.Size() * resolutionToScreen;
  const ScreenIntSize predictedScreenIntSize =
      RoundedToInt(predictedScreenSize);
  if (predictedScreenIntSize.IsEmpty()) {
    return;
  }

  // Determine the optimal image size to use.
  uint32_t flags = imgIContainer::FLAG_HIGH_QUALITY_SCALING |
                   imgIContainer::FLAG_ASYNC_NOTIFY;
  SamplingFilter samplingFilter =
      nsLayoutUtils::GetSamplingFilterForFrame(this);
  gfxSize gfxPredictedScreenSize =
      gfxSize(predictedScreenIntSize.width, predictedScreenIntSize.height);
  nsIntSize predictedImageSize = mImage->OptimalImageSizeForDest(
      gfxPredictedScreenSize, imgIContainer::FRAME_CURRENT, samplingFilter,
      flags);

  // Request a decode.
  mImage->RequestDecodeForSize(predictedImageSize, flags);
}

nsRect nsImageFrame::PredictedDestRect(const nsRect& aFrameContentBox) {
  // Note: To get the "dest rect", we have to provide the "constraint rect"
  // (which is the content-box, with the effects of fragmentation undone).
  nsRect constraintRect(aFrameContentBox.TopLeft(), mComputedSize);
  constraintRect.y -= GetContinuationOffset();

  return nsLayoutUtils::ComputeObjectDestRect(constraintRect, mIntrinsicSize,
                                              mIntrinsicRatio, StylePosition());
}

bool nsImageFrame::IsForMarkerPseudo() const {
  if (mKind == Kind::ImageLoadingContent) {
    return false;
  }
  auto* subtreeRoot = GetContent()->GetClosestNativeAnonymousSubtreeRoot();
  return subtreeRoot && subtreeRoot->IsGeneratedContentContainerForMarker();
}

void nsImageFrame::EnsureIntrinsicSizeAndRatio() {
  const auto containAxes = GetContainSizeAxes();
  if (containAxes.IsBoth()) {
    // If we have 'contain:size', then we have no intrinsic aspect ratio,
    // and the intrinsic size is determined by contain-intrinsic-size,
    // regardless of what our underlying image may think.
    mIntrinsicSize =
        containAxes.ContainIntrinsicSize(IntrinsicSize(0, 0), *this);
    mIntrinsicRatio = AspectRatio();
    return;
  }

  // If mIntrinsicSize.width and height are 0, then we need to update from the
  // image container.  Note that we handle ::marker intrinsic size/ratio in
  // DidSetComputedStyle.
  if (mIntrinsicSize != IntrinsicSize(0, 0) && !IsForMarkerPseudo()) {
    return;
  }

  bool intrinsicSizeOrRatioChanged = UpdateIntrinsicSize();
  intrinsicSizeOrRatioChanged =
      UpdateIntrinsicRatio() || intrinsicSizeOrRatioChanged;

  if (intrinsicSizeOrRatioChanged) {
    // Our aspect-ratio property value changed, and an embedding <object> or
    // <embed> might care about that.
    MaybeSendIntrinsicSizeAndRatioToEmbedder();
  }
}

nsIFrame::SizeComputationResult nsImageFrame::ComputeSize(
    gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
    nscoord aAvailableISize, const LogicalSize& aMargin,
    const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
    ComputeSizeFlags aFlags) {
  EnsureIntrinsicSizeAndRatio();
  return {ComputeSizeWithIntrinsicDimensions(
              aRenderingContext, aWM, mIntrinsicSize, GetAspectRatio(), aCBSize,
              aMargin, aBorderPadding, aSizeOverrides, aFlags),
          AspectRatioUsage::None};
}

Element* nsImageFrame::GetMapElement() const {
  nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
  return imageLoader ? static_cast<nsImageLoadingContent*>(imageLoader.get())
                           ->FindImageMap()
                     : nullptr;
}

// get the offset into the content area of the image where aImg starts if it is
// a continuation.
nscoord nsImageFrame::GetContinuationOffset() const {
  nscoord offset = 0;
  for (nsIFrame* f = GetPrevInFlow(); f; f = f->GetPrevInFlow()) {
    offset += f->GetContentRect().height;
  }
  NS_ASSERTION(offset >= 0, "bogus GetContentRect");
  return offset;
}

nscoord nsImageFrame::GetMinISize(gfxContext* aRenderingContext) {
  // XXX The caller doesn't account for constraints of the block-size,
  // min-block-size, and max-block-size properties.
  DebugOnly<nscoord> result;
  DISPLAY_MIN_INLINE_SIZE(this, result);
  EnsureIntrinsicSizeAndRatio();
  const auto& iSize = GetWritingMode().IsVertical() ? mIntrinsicSize.height
                                                    : mIntrinsicSize.width;
  return iSize.valueOr(0);
}

nscoord nsImageFrame::GetPrefISize(gfxContext* aRenderingContext) {
  // XXX The caller doesn't account for constraints of the block-size,
  // min-block-size, and max-block-size properties.
  DebugOnly<nscoord> result;
  DISPLAY_PREF_INLINE_SIZE(this, result);
  EnsureIntrinsicSizeAndRatio();
  const auto& iSize = GetWritingMode().IsVertical() ? mIntrinsicSize.height
                                                    : mIntrinsicSize.width;
  // convert from normal twips to scaled twips (printing...)
  return iSize.valueOr(0);
}

void nsImageFrame::ReflowChildren(nsPresContext* aPresContext,
                                  const ReflowInput& aReflowInput,
                                  const LogicalSize& aImageSize) {
  for (nsIFrame* child : mFrames) {
    ReflowOutput childDesiredSize(aReflowInput);
    WritingMode wm = GetWritingMode();
    // Shouldn't be hard to support if we want, but why bother.
    MOZ_ASSERT(
        wm == child->GetWritingMode(),
        "We don't expect mismatched writing-modes in content we control");
    nsReflowStatus childStatus;

    LogicalPoint childOffset(wm);
    ReflowInput childReflowInput(aPresContext, aReflowInput, child, aImageSize);
    const nsSize containerSize = aImageSize.GetPhysicalSize(wm);
    ReflowChild(child, aPresContext, childDesiredSize, childReflowInput, wm,
                childOffset, containerSize, ReflowChildFlags::Default,
                childStatus);

    FinishReflowChild(child, aPresContext, childDesiredSize, &childReflowInput,
                      wm, childOffset, containerSize,
                      ReflowChildFlags::Default);
  }
}

void nsImageFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
                          const ReflowInput& aReflowInput,
                          nsReflowStatus& aStatus) {
  MarkInReflow();
  DO_GLOBAL_REFLOW_COUNT("nsImageFrame");
  DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
  MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
  NS_FRAME_TRACE(
      NS_FRAME_TRACE_CALLS,
      ("enter nsImageFrame::Reflow: availSize=%d,%d",
       aReflowInput.AvailableWidth(), aReflowInput.AvailableHeight()));

  MOZ_ASSERT(HasAnyStateBits(NS_FRAME_IN_REFLOW), "frame is not in reflow");

  // see if we have a frozen size (i.e. a fixed width and height)
  if (!SizeDependsOnIntrinsicSize(aReflowInput)) {
    AddStateBits(IMAGE_SIZECONSTRAINED);
  } else {
    RemoveStateBits(IMAGE_SIZECONSTRAINED);
  }

  mComputedSize =
      nsSize(aReflowInput.ComputedWidth(), aReflowInput.ComputedHeight());

  aMetrics.Width() = mComputedSize.width;
  aMetrics.Height() = mComputedSize.height;

  // add borders and padding
  aMetrics.Width() += aReflowInput.ComputedPhysicalBorderPadding().LeftRight();
  aMetrics.Height() += aReflowInput.ComputedPhysicalBorderPadding().TopBottom();

  if (GetPrevInFlow()) {
    aMetrics.Width() = GetPrevInFlow()->GetSize().width;
    nscoord y = GetContinuationOffset();
    aMetrics.Height() -= y + aReflowInput.ComputedPhysicalBorderPadding().top;
    aMetrics.Height() = std::max(0, aMetrics.Height());
  }

  // we have to split images if we are:
  //  in Paginated mode, we need to have a constrained height, and have a height
  //  larger than our available height
  uint32_t loadStatus = imgIRequest::STATUS_NONE;
  if (nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest()) {
    currentRequest->GetImageStatus(&loadStatus);
  }

  if (aPresContext->IsPaginated() &&
      ((loadStatus & imgIRequest::STATUS_SIZE_AVAILABLE) ||
       HasAnyStateBits(IMAGE_SIZECONSTRAINED)) &&
      NS_UNCONSTRAINEDSIZE != aReflowInput.AvailableHeight() &&
      aMetrics.Height() > aReflowInput.AvailableHeight()) {
    // our desired height was greater than 0, so to avoid infinite
    // splitting, use 1 pixel as the min
    aMetrics.Height() = std::max(nsPresContext::CSSPixelsToAppUnits(1),
                                 aReflowInput.AvailableHeight());
    aStatus.SetIncomplete();
  }

  aMetrics.SetOverflowAreasToDesiredBounds();
  bool imageOK = mKind != Kind::ImageLoadingContent ||
                 ImageOk(mContent->AsElement()->State());

  // Determine if the size is available
  bool haveSize = false;
  if (loadStatus & imgIRequest::STATUS_SIZE_AVAILABLE) {
    haveSize = true;
  }

  if (!imageOK || !haveSize) {
    nsRect altFeedbackSize(
        0, 0,
        nsPresContext::CSSPixelsToAppUnits(
            ICON_SIZE + 2 * (ICON_PADDING + ALT_BORDER_WIDTH)),
        nsPresContext::CSSPixelsToAppUnits(
            ICON_SIZE + 2 * (ICON_PADDING + ALT_BORDER_WIDTH)));
    // We include the altFeedbackSize in our ink overflow, but not in our
    // scrollable overflow, since it doesn't really need to be scrolled to
    // outside the image.
    nsRect& inkOverflow = aMetrics.InkOverflow();
    inkOverflow.UnionRect(inkOverflow, altFeedbackSize);
  } else if (PresShell()->IsActive()) {
    // We've just reflowed and we should have an accurate size, so we're ready
    // to request a decode.
    MaybeDecodeForPredictedSize();
  }
  FinishAndStoreOverflow(&aMetrics, aReflowInput.mStyleDisplay);

  // Reflow the child frames. Our children can't affect our size in any way.
  ReflowChildren(aPresContext, aReflowInput, aMetrics.Size(GetWritingMode()));

  if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && !mReflowCallbackPosted) {
    mReflowCallbackPosted = true;
    PresShell()->PostReflowCallback(this);
  }

  NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("exit nsImageFrame::Reflow: size=%d,%d",
                                        aMetrics.Width(), aMetrics.Height()));
}

bool nsImageFrame::ReflowFinished() {
  mReflowCallbackPosted = false;

  // XXX(seth): We don't need this. The purpose of updating visibility
  // synchronously is to ensure that animated images start animating
  // immediately. In the short term, however,
  // nsImageLoadingContent::OnUnlockedDraw() is enough to ensure that
  // animations start as soon as the image is painted for the first time, and in
  // the long term we want to update visibility information from the display
  // list whenever we paint, so we don't actually need to do this. However, to
  // avoid behavior changes during the transition from the old image visibility
  // code, we'll leave it in for now.
  UpdateVisibilitySynchronously();

  return false;
}

void nsImageFrame::ReflowCallbackCanceled() { mReflowCallbackPosted = false; }

// Computes the width of the specified string. aMaxWidth specifies the maximum
// width available. Once this limit is reached no more characters are measured.
// The number of characters that fit within the maximum width are returned in
// aMaxFit. NOTE: it is assumed that the fontmetrics have already been selected
// into the rendering context before this is called (for performance). MMP
nscoord nsImageFrame::MeasureString(const char16_t* aString, int32_t aLength,
                                    nscoord aMaxWidth, uint32_t& aMaxFit,
                                    gfxContext& aContext,
                                    nsFontMetrics& aFontMetrics) {
  nscoord totalWidth = 0;
  aFontMetrics.SetTextRunRTL(false);
  nscoord spaceWidth = aFontMetrics.SpaceWidth();

  aMaxFit = 0;
  while (aLength > 0) {
    // Find the next place we can line break
    uint32_t len = aLength;
    bool trailingSpace = false;
    for (int32_t i = 0; i < aLength; i++) {
      if (dom::IsSpaceCharacter(aString[i]) && (i > 0)) {
        len = i;  // don't include the space when measuring
        trailingSpace = true;
        break;
      }
    }

    // Measure this chunk of text, and see if it fits
    nscoord width = nsLayoutUtils::AppUnitWidthOfStringBidi(
        aString, len, this, aFontMetrics, aContext);
    bool fits = (totalWidth + width) <= aMaxWidth;

    // If it fits on the line, or it's the first word we've processed then
    // include it
    if (fits || (0 == totalWidth)) {
      // New piece fits
      totalWidth += width;

      // If there's a trailing space then see if it fits as well
      if (trailingSpace) {
        if ((totalWidth + spaceWidth) <= aMaxWidth) {
          totalWidth += spaceWidth;
        } else {
          // Space won't fit. Leave it at the end but don't include it in
          // the width
          fits = false;
        }

        len++;
      }

      aMaxFit += len;
      aString += len;
      aLength -= len;
    }

    if (!fits) {
      break;
    }
  }
  return totalWidth;
}

// Formats the alt-text to fit within the specified rectangle. Breaks lines
// between words if a word would extend past the edge of the rectangle
void nsImageFrame::DisplayAltText(nsPresContext* aPresContext,
                                  gfxContext& aRenderingContext,
                                  const nsString& aAltText,
                                  const nsRect& aRect) {
  // Set font and color
  aRenderingContext.SetColor(
      sRGBColor::FromABGR(StyleText()->mColor.ToColor()));
  RefPtr<nsFontMetrics> fm =
      nsLayoutUtils::GetInflatedFontMetricsForFrame(this);

  // Format the text to display within the formatting rect

  nscoord maxAscent = fm->MaxAscent();
  nscoord maxDescent = fm->MaxDescent();
  nscoord lineHeight = fm->MaxHeight();  // line-relative, so an x-coordinate
                                         // length if writing mode is vertical

  WritingMode wm = GetWritingMode();
  bool isVertical = wm.IsVertical();

  fm->SetVertical(isVertical);
  fm->SetTextOrientation(StyleVisibility()->mTextOrientation);

  // XXX It would be nice if there was a way to have the font metrics tell
  // use where to break the text given a maximum width. At a minimum we need
  // to be able to get the break character...
  const char16_t* str = aAltText.get();
  int32_t strLen = aAltText.Length();
  nsPoint pt = wm.IsVerticalRL() ? aRect.TopRight() - nsPoint(lineHeight, 0)
                                 : aRect.TopLeft();
  nscoord iSize = isVertical ? aRect.height : aRect.width;

  if (!aPresContext->BidiEnabled() && HasRTLChars(aAltText)) {
    aPresContext->SetBidiEnabled();
  }

  // Always show the first line, even if we have to clip it below
  bool firstLine = true;
  while (strLen > 0) {
    if (!firstLine) {
      // If we've run out of space, break out of the loop
      if ((!isVertical && (pt.y + maxDescent) >= aRect.YMost()) ||
          (wm.IsVerticalRL() && (pt.x + maxDescent < aRect.x)) ||
          (wm.IsVerticalLR() && (pt.x + maxDescent >= aRect.XMost()))) {
        break;
      }
    }

    // Determine how much of the text to display on this line
    uint32_t maxFit;  // number of characters that fit
    nscoord strWidth =
        MeasureString(str, strLen, iSize, maxFit, aRenderingContext, *fm);

    // Display the text
    nsresult rv = NS_ERROR_FAILURE;

    if (aPresContext->BidiEnabled()) {
      mozilla::intl::BidiEmbeddingLevel level;
      nscoord x, y;

      if (isVertical) {
        x = pt.x + maxDescent;
        if (wm.IsBidiLTR()) {
          y = aRect.y;
          level = mozilla::intl::BidiEmbeddingLevel::LTR();
        } else {
          y = aRect.YMost() - strWidth;
          level = mozilla::intl::BidiEmbeddingLevel::RTL();
        }
      } else {
        y = pt.y + maxAscent;
        if (wm.IsBidiLTR()) {
          x = aRect.x;
          level = mozilla::intl::BidiEmbeddingLevel::LTR();
        } else {
          x = aRect.XMost() - strWidth;
          level = mozilla::intl::BidiEmbeddingLevel::RTL();
        }
      }

      rv = nsBidiPresUtils::RenderText(
          str, maxFit, level, aPresContext, aRenderingContext,
          aRenderingContext.GetDrawTarget(), *fm, x, y);
    }
    if (NS_FAILED(rv)) {
      nsLayoutUtils::DrawUniDirString(str, maxFit,
                                      isVertical
                                          ? nsPoint(pt.x + maxDescent, pt.y)
                                          : nsPoint(pt.x, pt.y + maxAscent),
                                      *fm, aRenderingContext);
    }

    // Move to the next line
    str += maxFit;
    strLen -= maxFit;
    if (wm.IsVerticalRL()) {
      pt.x -= lineHeight;
    } else if (wm.IsVerticalLR()) {
      pt.x += lineHeight;
    } else {
      pt.y += lineHeight;
    }

    firstLine = false;
  }
}

struct nsRecessedBorder : public nsStyleBorder {
  nsRecessedBorder(nscoord aBorderWidth, nsPresContext* aPresContext)
      : nsStyleBorder(*aPresContext->Document()) {
    for (const auto side : mozilla::AllPhysicalSides()) {
      BorderColorFor(side) = StyleColor::Black();
      mBorder.Side(side) = aBorderWidth;
      // Note: use SetBorderStyle here because we want to affect
      // mComputedBorder
      SetBorderStyle(side, StyleBorderStyle::Inset);
    }
  }
};

class nsDisplayAltFeedback final : public nsPaintedDisplayItem {
 public:
  nsDisplayAltFeedback(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame)
      : nsPaintedDisplayItem(aBuilder, aFrame) {}

  nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) const final {
    *aSnap = false;
    return mFrame->InkOverflowRectRelativeToSelf() + ToReferenceFrame();
  }

  void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) final {
    // Always sync decode, because these icons are UI, and since they're not
    // discardable we'll pay the price of sync decoding at most once.
    uint32_t flags = imgIContainer::FLAG_SYNC_DECODE;

    nsImageFrame* f = static_cast<nsImageFrame*>(mFrame);
    Unused << f->DisplayAltFeedback(*aCtx, GetPaintRect(aBuilder, aCtx),
                                    ToReferenceFrame(), flags);
  }

  bool CreateWebRenderCommands(
      mozilla::wr::DisplayListBuilder& aBuilder,
      mozilla::wr::IpcResourceUpdateQueue& aResources,
      const StackingContextHelper& aSc,
      mozilla::layers::RenderRootStateManager* aManager,
      nsDisplayListBuilder* aDisplayListBuilder) final {
    // Always sync decode, because these icons are UI, and since they're not
    // discardable we'll pay the price of sync decoding at most once.
    uint32_t flags =
        imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY;
    nsImageFrame* f = static_cast<nsImageFrame*>(mFrame);
    ImgDrawResult result = f->DisplayAltFeedbackWithoutLayer(
        this, aBuilder, aResources, aSc, aManager, aDisplayListBuilder,
        ToReferenceFrame(), flags);

    return result == ImgDrawResult::SUCCESS;
  }

  NS_DISPLAY_DECL_NAME("AltFeedback", TYPE_ALT_FEEDBACK)
};

ImgDrawResult nsImageFrame::DisplayAltFeedback(gfxContext& aRenderingContext,
                                               const nsRect& aDirtyRect,
                                               nsPoint aPt, uint32_t aFlags) {
  // Whether we draw the broken or loading icon.
  bool isLoading = mKind != Kind::ImageLoadingContent ||
                   ImageOk(mContent->AsElement()->State());

  // Calculate the content area.
  nsRect inner = GetContentRectRelativeToSelf() + aPt;

  // Display a recessed one pixel border
  nscoord borderEdgeWidth =
      nsPresContext::CSSPixelsToAppUnits(ALT_BORDER_WIDTH);

  // if inner area is empty, then make it big enough for at least the icon
  if (inner.IsEmpty()) {
    inner.SizeTo(2 * (nsPresContext::CSSPixelsToAppUnits(
                         ICON_SIZE + ICON_PADDING + ALT_BORDER_WIDTH)),
                 2 * (nsPresContext::CSSPixelsToAppUnits(
                         ICON_SIZE + ICON_PADDING + ALT_BORDER_WIDTH)));
  }

  // Make sure we have enough room to actually render the border within
  // our frame bounds
  if ((inner.width < 2 * borderEdgeWidth) ||
      (inner.height < 2 * borderEdgeWidth)) {
    return ImgDrawResult::SUCCESS;
  }

  // Paint the border
  if (!isLoading) {
    nsRecessedBorder recessedBorder(borderEdgeWidth, PresContext());

    // Assert that we're not drawing a border-image here; if we were, we
    // couldn't ignore the ImgDrawResult that PaintBorderWithStyleBorder
    // returns.
    MOZ_ASSERT(recessedBorder.mBorderImageSource.IsNone());

    Unused << nsCSSRendering::PaintBorderWithStyleBorder(
        PresContext(), aRenderingContext, this, inner, inner, recessedBorder,
        mComputedStyle, PaintBorderFlags::SyncDecodeImages);
  }

  // Adjust the inner rect to account for the one pixel recessed border,
  // and a six pixel padding on each edge
  inner.Deflate(
      nsPresContext::CSSPixelsToAppUnits(ICON_PADDING + ALT_BORDER_WIDTH),
      nsPresContext::CSSPixelsToAppUnits(ICON_PADDING + ALT_BORDER_WIDTH));
  if (inner.IsEmpty()) {
    return ImgDrawResult::SUCCESS;
  }

  DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();

  // Clip so we don't render outside the inner rect
  aRenderingContext.Save();
  aRenderingContext.Clip(NSRectToSnappedRect(
      inner, PresContext()->AppUnitsPerDevPixel(), *drawTarget));

  ImgDrawResult result = ImgDrawResult::SUCCESS;

  // Check if we should display image placeholders
  if (ShouldShowBrokenImageIcon()) {
    result = ImgDrawResult::NOT_READY;
    nscoord size = nsPresContext::CSSPixelsToAppUnits(ICON_SIZE);
    imgIRequest* request = BrokenImageIcon::GetImage(this);

    // If we weren't previously displaying an icon, register ourselves
    // as an observer for load and animation updates and flag that we're
    // doing so now.
    if (request && !mDisplayingIcon) {
      BrokenImageIcon::AddObserver(this);
      mDisplayingIcon = true;
    }

    WritingMode wm = GetWritingMode();
    bool flushRight = wm.IsPhysicalRTL();

    // If the icon in question is loaded, draw it.
    uint32_t imageStatus = 0;
    if (request) request->GetImageStatus(&imageStatus);
    if (imageStatus & imgIRequest::STATUS_LOAD_COMPLETE &&
        !(imageStatus & imgIRequest::STATUS_ERROR)) {
      nsCOMPtr<imgIContainer> imgCon;
      request->GetImage(getter_AddRefs(imgCon));
      MOZ_ASSERT(imgCon, "Load complete, but no image container?");
      nsRect dest(flushRight ? inner.XMost() - size : inner.x, inner.y, size,
                  size);
      result = nsLayoutUtils::DrawSingleImage(
          aRenderingContext, PresContext(), imgCon,
          nsLayoutUtils::GetSamplingFilterForFrame(this), dest, aDirtyRect,
          SVGImageContext(), aFlags);
    }

    // If we could not draw the icon, just draw some graffiti in the mean time.
    if (result == ImgDrawResult::NOT_READY) {
      ColorPattern color(ToDeviceColor(sRGBColor(1.f, 0.f, 0.f, 1.f)));

      nscoord iconXPos = flushRight ? inner.XMost() - size : inner.x;

      // stroked rect:
      nsRect rect(iconXPos, inner.y, size, size);
      Rect devPxRect = ToRect(nsLayoutUtils::RectToGfxRect(
          rect, PresContext()->AppUnitsPerDevPixel()));
      drawTarget->StrokeRect(devPxRect, color);

      // filled circle in bottom right quadrant of stroked rect:
      nscoord twoPX = nsPresContext::CSSPixelsToAppUnits(2);
      rect = nsRect(iconXPos + size / 2, inner.y + size / 2, size / 2 - twoPX,
                    size / 2 - twoPX);
      devPxRect = ToRect(nsLayoutUtils::RectToGfxRect(
          rect, PresContext()->AppUnitsPerDevPixel()));
      RefPtr<PathBuilder> builder = drawTarget->CreatePathBuilder();
      AppendEllipseToPath(builder, devPxRect.Center(), devPxRect.Size());
      RefPtr<Path> ellipse = builder->Finish();
      drawTarget->Fill(ellipse, color);
    }

    // Reduce the inner rect by the width of the icon, and leave an
    // additional ICON_PADDING pixels for padding
    int32_t paddedIconSize =
        nsPresContext::CSSPixelsToAppUnits(ICON_SIZE + ICON_PADDING);
    if (wm.IsVertical()) {
      inner.y += paddedIconSize;
      inner.height -= paddedIconSize;
    } else {
      if (!flushRight) {
        inner.x += paddedIconSize;
      }
      inner.width -= paddedIconSize;
    }
  }

  // If there's still room, display the alt-text
  if (!inner.IsEmpty()) {
    nsAutoString altText;
    nsCSSFrameConstructor::GetAlternateTextFor(*mContent->AsElement(), altText);
    DisplayAltText(PresContext(), aRenderingContext, altText, inner);
  }

  aRenderingContext.Restore();

  return result;
}

ImgDrawResult nsImageFrame::DisplayAltFeedbackWithoutLayer(
    nsDisplayItem* aItem, mozilla::wr::DisplayListBuilder& aBuilder,
    mozilla::wr::IpcResourceUpdateQueue& aResources,
    const StackingContextHelper& aSc,
    mozilla::layers::RenderRootStateManager* aManager,
    nsDisplayListBuilder* aDisplayListBuilder, nsPoint aPt, uint32_t aFlags) {
  // Whether we draw the broken or loading icon.
  bool isLoading = mKind != Kind::ImageLoadingContent ||
                   ImageOk(mContent->AsElement()->State());

  // Calculate the content area.
  nsRect inner = GetContentRectRelativeToSelf() + aPt;

  // Display a recessed one pixel border
  nscoord borderEdgeWidth =
      nsPresContext::CSSPixelsToAppUnits(ALT_BORDER_WIDTH);

  // if inner area is empty, then make it big enough for at least the icon
  if (inner.IsEmpty()) {
    inner.SizeTo(2 * (nsPresContext::CSSPixelsToAppUnits(
                         ICON_SIZE + ICON_PADDING + ALT_BORDER_WIDTH)),
                 2 * (nsPresContext::CSSPixelsToAppUnits(
                         ICON_SIZE + ICON_PADDING + ALT_BORDER_WIDTH)));
  }

  // Make sure we have enough room to actually render the border within
  // our frame bounds
  if ((inner.width < 2 * borderEdgeWidth) ||
      (inner.height < 2 * borderEdgeWidth)) {
    return ImgDrawResult::SUCCESS;
  }

  // If the TextDrawTarget requires fallback we need to rollback everything we
  // may have added to the display list, but we don't find that out until the
  // end.
  bool textDrawResult = true;
  class AutoSaveRestore {
   public:
    explicit AutoSaveRestore(mozilla::wr::DisplayListBuilder& aBuilder,
                             bool& aTextDrawResult)
        : mBuilder(aBuilder), mTextDrawResult(aTextDrawResult) {
      mBuilder.Save();
    }
    ~AutoSaveRestore() {
      // If we have to use fallback for the text restore the builder and remove
      // anything else we added to the display list, we need to use fallback.
      if (mTextDrawResult) {
        mBuilder.ClearSave();
      } else {
        mBuilder.Restore();
      }
    }

   private:
    mozilla::wr::DisplayListBuilder& mBuilder;
    bool& mTextDrawResult;
  };

  AutoSaveRestore autoSaveRestore(aBuilder, textDrawResult);

  // Paint the border
  if (!isLoading) {
    nsRecessedBorder recessedBorder(borderEdgeWidth, PresContext());
    // Assert that we're not drawing a border-image here; if we were, we
    // couldn't ignore the ImgDrawResult that PaintBorderWithStyleBorder
    // returns.
    MOZ_ASSERT(recessedBorder.mBorderImageSource.IsNone());

    nsRect rect = nsRect(aPt, GetSize());
    Unused << nsCSSRendering::CreateWebRenderCommandsForBorderWithStyleBorder(
        aItem, this, rect, aBuilder, aResources, aSc, aManager,
        aDisplayListBuilder, recessedBorder);
  }

  // Adjust the inner rect to account for the one pixel recessed border,
  // and a six pixel padding on each edge
  inner.Deflate(
      nsPresContext::CSSPixelsToAppUnits(ICON_PADDING + ALT_BORDER_WIDTH),
      nsPresContext::CSSPixelsToAppUnits(ICON_PADDING + ALT_BORDER_WIDTH));
  if (inner.IsEmpty()) {
    return ImgDrawResult::SUCCESS;
  }

  // Clip to this rect so we don't render outside the inner rect
  const auto bounds = LayoutDeviceRect::FromAppUnits(
      inner, PresContext()->AppUnitsPerDevPixel());
  auto wrBounds = wr::ToLayoutRect(bounds);

  // Check if we should display image placeholders
  if (ShouldShowBrokenImageIcon()) {
    ImgDrawResult result = ImgDrawResult::NOT_READY;
    nscoord size = nsPresContext::CSSPixelsToAppUnits(ICON_SIZE);
    imgIRequest* request = BrokenImageIcon::GetImage(this);

    // If we weren't previously displaying an icon, register ourselves
    // as an observer for load and animation updates and flag that we're
    // doing so now.
    if (request && !mDisplayingIcon) {
      BrokenImageIcon::AddObserver(this);
      mDisplayingIcon = true;
    }

    WritingMode wm = GetWritingMode();
    const bool flushRight = wm.IsPhysicalRTL();

    // If the icon in question is loaded, draw it.
    uint32_t imageStatus = 0;
    if (request) request->GetImageStatus(&imageStatus);
    if (imageStatus & imgIRequest::STATUS_LOAD_COMPLETE &&
        !(imageStatus & imgIRequest::STATUS_ERROR)) {
      nsCOMPtr<imgIContainer> imgCon;
      request->GetImage(getter_AddRefs(imgCon));
      MOZ_ASSERT(imgCon, "Load complete, but no image container?");

      nsRect dest(flushRight ? inner.XMost() - size : inner.x, inner.y, size,
                  size);

      const int32_t factor = PresContext()->AppUnitsPerDevPixel();
      const auto destRect = LayoutDeviceRect::FromAppUnits(dest, factor);

      SVGImageContext svgContext;
      Maybe<ImageIntRegion> region;
      IntSize decodeSize =
          nsLayoutUtils::ComputeImageContainerDrawingParameters(
              imgCon, this, destRect, destRect, aSc, aFlags, svgContext,
              region);
      RefPtr<image::WebRenderImageProvider> provider;
      result = imgCon->GetImageProvider(aManager->LayerManager(), decodeSize,
                                        svgContext, region, aFlags,
                                        getter_AddRefs(provider));
      if (provider) {
        bool wrResult = aManager->CommandBuilder().PushImageProvider(
            aItem, provider, result, aBuilder, aResources, destRect, bounds);
        result &= wrResult ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY;
      } else {
        // We don't use &= here because we want the result to be NOT_READY so
        // the next block executes.
        result = ImgDrawResult::NOT_READY;
      }
    }

    // If we could not draw the icon, just draw some graffiti in the mean time.
    if (result == ImgDrawResult::NOT_READY) {
      auto color = wr::ColorF{1.0f, 0.0f, 0.0f, 1.0f};
      bool isBackfaceVisible = !aItem->BackfaceIsHidden();

      nscoord iconXPos = flushRight ? inner.XMost() - size : inner.x;

      // stroked rect:
      nsRect rect(iconXPos, inner.y, size, size);
      auto devPxRect = LayoutDeviceRect::FromAppUnits(
          rect, PresContext()->AppUnitsPerDevPixel());
      auto dest = wr::ToLayoutRect(devPxRect);

      auto borderWidths = wr::ToBorderWidths(1.0, 1.0, 1.0, 1.0);
      wr::BorderSide side = {color, wr::BorderStyle::Solid};
      wr::BorderSide sides[4] = {side, side, side, side};
      Range<const wr::BorderSide> sidesRange(sides, 4);
      aBuilder.PushBorder(dest, wrBounds, isBackfaceVisible, borderWidths,
                          sidesRange, wr::EmptyBorderRadius());

      // filled circle in bottom right quadrant of stroked rect:
      nscoord twoPX = nsPresContext::CSSPixelsToAppUnits(2);
      rect = nsRect(iconXPos + size / 2, inner.y + size / 2, size / 2 - twoPX,
                    size / 2 - twoPX);
      devPxRect = LayoutDeviceRect::FromAppUnits(
          rect, PresContext()->AppUnitsPerDevPixel());
      dest = wr::ToLayoutRect(devPxRect);

      aBuilder.PushRoundedRect(dest, wrBounds, isBackfaceVisible, color);
    }

    // Reduce the inner rect by the width of the icon, and leave an
    // additional ICON_PADDING pixels for padding
    int32_t paddedIconSize =
        nsPresContext::CSSPixelsToAppUnits(ICON_SIZE + ICON_PADDING);
    if (wm.IsVertical()) {
      inner.y += paddedIconSize;
      inner.height -= paddedIconSize;
    } else {
      if (!flushRight) {
        inner.x += paddedIconSize;
      }
      inner.width -= paddedIconSize;
    }
  }

  // Draw text
  if (!inner.IsEmpty()) {
    RefPtr<TextDrawTarget> textDrawer =
        new TextDrawTarget(aBuilder, aResources, aSc, aManager, aItem, inner,
                           /* aCallerDoesSaveRestore = */ true);
    MOZ_ASSERT(textDrawer->IsValid());
    if (textDrawer->IsValid()) {
      gfxContext captureCtx(textDrawer);

      nsAutoString altText;
      nsCSSFrameConstructor::GetAlternateTextFor(*mContent->AsElement(),
                                                 altText);
      DisplayAltText(PresContext(), captureCtx, altText, inner);

      textDrawer->TerminateShadows();
      textDrawResult = !textDrawer->CheckHasUnsupportedFeatures();
    }
  }

  // Purposely ignore local DrawResult because we handled it not being success
  // already.
  return textDrawResult ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY;
}

// We want to sync-decode in this case, as otherwise we either need to flash
// white while waiting to decode the new image, or paint the old image with a
// different aspect-ratio, which would be bad as it'd be stretched.
//
// See bug 1589955.
static bool OldImageHasDifferentRatio(const nsImageFrame& aFrame,
                                      imgIContainer& aImage,
                                      imgIContainer* aPrevImage) {
  if (!aPrevImage || aPrevImage == &aImage) {
    return false;
  }

  // If we don't depend on our intrinsic image size / ratio, we're good.
  //
  // FIXME(emilio): There's the case of the old image being painted
  // intrinsically, and src and styles changing at the same time... Maybe we
  // should keep track of the old GetPaintRect()'s ratio and the image's ratio,
  // instead of checking this bit?
  if (aFrame.HasAnyStateBits(IMAGE_SIZECONSTRAINED)) {
    return false;
  }

  auto currentRatio = aFrame.GetIntrinsicRatio();
  // If we have an image, we need to have a current request.
  // Same if we had an image.
  const bool hasRequest = true;
#ifdef DEBUG
  auto currentRatioRecomputed =
      ComputeIntrinsicRatio(&aImage, hasRequest, aFrame);
  // If the image encounters an error after decoding the size (and we run
  // UpdateIntrinsicRatio) then the image will return the empty AspectRatio and
  // the aspect ratio we compute here will be different from what was computed
  // and stored before the image went into error state. It would be better to
  // check that the image has an error here but we need an imgIRequest for that,
  // not an imgIContainer. In lieu of that we check that
  // aImage.GetIntrinsicRatio() returns Nothing() as it does when the image is
  // in the error state and that the recomputed ratio is the zero ratio.
  MOZ_ASSERT(
      (!currentRatioRecomputed && aImage.GetIntrinsicRatio() == Nothing()) ||
          currentRatio == currentRatioRecomputed,
      "aspect-ratio got out of sync during paint? How?");
#endif
  auto oldRatio = ComputeIntrinsicRatio(aPrevImage, hasRequest, aFrame);
  return oldRatio != currentRatio;
}

#ifdef DEBUG
void nsImageFrame::AssertSyncDecodingHintIsInSync() const {
  if (!IsForImageLoadingContent()) {
    return;
  }
  nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
  MOZ_ASSERT(imageLoader);

  // The opposite is not true, we might have some other heuristics which force
  // sync-decoding of images.
  MOZ_ASSERT_IF(imageLoader->GetSyncDecodingHint(), mForceSyncDecoding);
}
#endif

void nsDisplayImage::Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) {
  MOZ_ASSERT(mImage);
  auto* frame = static_cast<nsImageFrame*>(mFrame);
  frame->AssertSyncDecodingHintIsInSync();

  const bool oldImageIsDifferent =
      OldImageHasDifferentRatio(*frame, *mImage, mPrevImage);

  uint32_t flags = aBuilder->GetImageDecodeFlags();
  if (aBuilder->ShouldSyncDecodeImages() || oldImageIsDifferent ||
      frame->mForceSyncDecoding) {
    flags |= imgIContainer::FLAG_SYNC_DECODE;
  }

  ImgDrawResult result = frame->PaintImage(
      *aCtx, ToReferenceFrame(), GetPaintRect(aBuilder, aCtx), mImage, flags);

  if (result == ImgDrawResult::NOT_READY ||
      result == ImgDrawResult::INCOMPLETE ||
      result == ImgDrawResult::TEMPORARY_ERROR) {
    // If the current image failed to paint because it's still loading or
    // decoding, try painting the previous image.
    if (mPrevImage) {
      result =
          frame->PaintImage(*aCtx, ToReferenceFrame(),
                            GetPaintRect(aBuilder, aCtx), mPrevImage, flags);
    }
  }
}

nsRect nsDisplayImage::GetDestRect() const {
  bool snap = true;
  const nsRect frameContentBox = GetBounds(&snap);

  nsImageFrame* imageFrame = static_cast<nsImageFrame*>(mFrame);
  return imageFrame->PredictedDestRect(frameContentBox);
}

nsRegion nsDisplayImage::GetOpaqueRegion(nsDisplayListBuilder* aBuilder,
                                         bool* aSnap) const {
  *aSnap = false;
  if (mImage && mImage->WillDrawOpaqueNow()) {
    const nsRect frameContentBox = GetBounds(aSnap);
    return GetDestRect().Intersect(frameContentBox);
  }
  return nsRegion();
}

bool nsDisplayImage::CreateWebRenderCommands(
    mozilla::wr::DisplayListBuilder& aBuilder,
    mozilla::wr::IpcResourceUpdateQueue& aResources,
    const StackingContextHelper& aSc, RenderRootStateManager* aManager,
    nsDisplayListBuilder* aDisplayListBuilder) {
  if (!mImage) {
    return false;
  }

  MOZ_ASSERT(mFrame->IsImageFrame() || mFrame->IsImageControlFrame());
  // Image layer doesn't support draw focus ring for image map.
  auto* frame = static_cast<nsImageFrame*>(mFrame);
  if (frame->HasImageMap()) {
    return false;
  }

  frame->AssertSyncDecodingHintIsInSync();
  const bool oldImageIsDifferent =
      OldImageHasDifferentRatio(*frame, *mImage, mPrevImage);

  uint32_t flags = aDisplayListBuilder->GetImageDecodeFlags();
  if (aDisplayListBuilder->ShouldSyncDecodeImages() || oldImageIsDifferent ||
      frame->mForceSyncDecoding) {
    flags |= imgIContainer::FLAG_SYNC_DECODE;
  }
  if (StaticPrefs::image_svg_blob_image() &&
      mImage->GetType() == imgIContainer::TYPE_VECTOR) {
    flags |= imgIContainer::FLAG_RECORD_BLOB;
  }

  const int32_t factor = mFrame->PresContext()->AppUnitsPerDevPixel();
  LayoutDeviceRect destRect(
      LayoutDeviceRect::FromAppUnits(GetDestRect(), factor));

  SVGImageContext svgContext;
  Maybe<ImageIntRegion> region;
  IntSize decodeSize = nsLayoutUtils::ComputeImageContainerDrawingParameters(
      mImage, mFrame, destRect, destRect, aSc, flags, svgContext, region);

  RefPtr<image::WebRenderImageProvider> provider;
  ImgDrawResult drawResult =
      mImage->GetImageProvider(aManager->LayerManager(), decodeSize, svgContext,
                               region, flags, getter_AddRefs(provider));

  // While we got a container, it may not contain a fully decoded surface. If
  // that is the case, and we have an image we were previously displaying which
  // has a fully decoded surface, then we should prefer the previous image.
  bool updatePrevImage = false;
  switch (drawResult) {
    case ImgDrawResult::NOT_READY:
    case ImgDrawResult::INCOMPLETE:
    case ImgDrawResult::TEMPORARY_ERROR:
      if (mPrevImage && mPrevImage != mImage) {
        // The current image and the previous image might be switching between
        // rasterized surfaces and blob recordings, so we need to update the
        // flags appropriately.
        uint32_t prevFlags = flags;
        if (StaticPrefs::image_svg_blob_image() &&
            mPrevImage->GetType() == imgIContainer::TYPE_VECTOR) {
          prevFlags |= imgIContainer::FLAG_RECORD_BLOB;
        } else {
          prevFlags &= ~imgIContainer::FLAG_RECORD_BLOB;
        }

        RefPtr<image::WebRenderImageProvider> prevProvider;
        ImgDrawResult prevDrawResult = mPrevImage->GetImageProvider(
            aManager->LayerManager(), decodeSize, svgContext, region, prevFlags,
            getter_AddRefs(prevProvider));
        if (prevProvider && (prevDrawResult == ImgDrawResult::SUCCESS ||
                             prevDrawResult == ImgDrawResult::WRONG_SIZE)) {
          // We use WRONG_SIZE here to ensure that when the frame next tries to
          // invalidate due to a frame update from the current image, we don't
          // consider the result from the previous image to be a valid result to
          // avoid redrawing.
          drawResult = ImgDrawResult::WRONG_SIZE;
          provider = std::move(prevProvider);
          flags = prevFlags;
          break;
        }

        // Previous image was unusable; we can forget about it.
        updatePrevImage = true;
      }
      break;
    case ImgDrawResult::NOT_SUPPORTED:
      return false;
    default:
      updatePrevImage = mPrevImage != mImage;
      break;
  }

  // The previous image was not used, and is different from the current image.
  // We should forget about it. We need to update the frame as well because the
  // display item may get recreated.
  if (updatePrevImage) {
    mPrevImage = mImage;
    frame->mPrevImage = frame->mImage;
  }

  // If the image provider is null, we don't want to fallback. Any other
  // failure will be due to resource constraints and fallback is unlikely to
  // help us. Hence we can ignore the return value from PushImage.
  aManager->CommandBuilder().PushImageProvider(
      this, provider, drawResult, aBuilder, aResources, destRect, destRect);
  return true;
}

ImgDrawResult nsImageFrame::PaintImage(gfxContext& aRenderingContext,
                                       nsPoint aPt, const nsRect& aDirtyRect,
                                       imgIContainer* aImage, uint32_t aFlags) {
  DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();

  // Render the image into our content area (the area inside
  // the borders and padding)
  NS_ASSERTION(GetContentRectRelativeToSelf().width == mComputedSize.width,
               "bad width");

  // NOTE: We use mComputedSize instead of just GetContentRectRelativeToSelf()'s
  // own size here, because GetContentRectRelativeToSelf() might be smaller if
  // we're fragmented, whereas mComputedSize has our full content-box size
  // (which we need for ComputeObjectDestRect to work correctly).
  nsRect constraintRect(aPt + GetContentRectRelativeToSelf().TopLeft(),
                        mComputedSize);
  constraintRect.y -= GetContinuationOffset();

  nsPoint anchorPoint;
  nsRect dest = nsLayoutUtils::ComputeObjectDestRect(
      constraintRect, mIntrinsicSize, mIntrinsicRatio, StylePosition(),
      &anchorPoint);

  SVGImageContext svgContext;
  SVGImageContext::MaybeStoreContextPaint(svgContext, this, aImage);

  ImgDrawResult result = nsLayoutUtils::DrawSingleImage(
      aRenderingContext, PresContext(), aImage,
      nsLayoutUtils::GetSamplingFilterForFrame(this), dest, aDirtyRect,
      svgContext, aFlags, &anchorPoint);

  if (nsImageMap* map = GetImageMap()) {
    gfxPoint devPixelOffset = nsLayoutUtils::PointToGfxPoint(
        dest.TopLeft(), PresContext()->AppUnitsPerDevPixel());
    AutoRestoreTransform autoRestoreTransform(drawTarget);
    drawTarget->SetTransform(
        drawTarget->GetTransform().PreTranslate(ToPoint(devPixelOffset)));

    // solid white stroke:
    ColorPattern white(ToDeviceColor(sRGBColor::OpaqueWhite()));
    map->Draw(this, *drawTarget, white);

    // then dashed black stroke over the top:
    ColorPattern black(ToDeviceColor(sRGBColor::OpaqueBlack()));
    StrokeOptions strokeOptions;
    nsLayoutUtils::InitDashPattern(strokeOptions, StyleBorderStyle::Dotted);
    map->Draw(this, *drawTarget, black, strokeOptions);
  }

  if (result == ImgDrawResult::SUCCESS) {
    mPrevImage = aImage;
  } else if (result == ImgDrawResult::BAD_IMAGE) {
    mPrevImage = nullptr;
  }

  return result;
}

already_AddRefed<imgIRequest> nsImageFrame::GetCurrentRequest() const {
  if (mKind != Kind::ImageLoadingContent) {
    return do_AddRef(mOwnedRequest);
  }

  MOZ_ASSERT(!mOwnedRequest);

  nsCOMPtr<imgIRequest> request;
  nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
  MOZ_ASSERT(imageLoader);
  imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
                          getter_AddRefs(request));
  return request.forget();
}

void nsImageFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
                                    const nsDisplayListSet& aLists) {
  if (!IsVisibleForPainting()) {
    return;
  }

  DisplayBorderBackgroundOutline(aBuilder, aLists);

  if (HidesContent()) {
    DisplaySelectionOverlay(aBuilder, aLists.Content(),
                            nsISelectionDisplay::DISPLAY_IMAGES);
    return;
  }

  uint32_t clipFlags =
      nsStyleUtil::ObjectPropsMightCauseOverflow(StylePosition())
          ? 0
          : DisplayListClipState::ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT;

  DisplayListClipState::AutoClipContainingBlockDescendantsToContentBox clip(
      aBuilder, this, clipFlags);

  if (mComputedSize.width != 0 && mComputedSize.height != 0) {
    const bool imageOK = mKind != Kind::ImageLoadingContent ||
                         ImageOk(mContent->AsElement()->State());

    nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest();

    const bool isImageFromStyle =
        mKind != Kind::ImageLoadingContent && mKind != Kind::XULImage;
    const bool drawAltFeedback = [&] {
      if (!imageOK) {
        return true;
      }
      // If we're a gradient, we don't need to draw alt feedback.
      if (isImageFromStyle && !GetImageFromStyle()->IsImageRequestType()) {
        return false;
      }
      // XXX(seth): The SizeIsAvailable check here should not be necessary - the
      // intention is that a non-null mImage means we have a size, but there is
      // currently some code that violates this invariant.
      return !mImage || !SizeIsAvailable(currentRequest);
    }();

    if (drawAltFeedback) {
      // No image yet, or image load failed. Draw the alt-text and an icon
      // indicating the status
      aLists.Content()->AppendNewToTop<nsDisplayAltFeedback>(aBuilder, this);

      // This image is visible (we are being asked to paint it) but it's not
      // decoded yet. And we are not going to ask the image to draw, so this
      // may be the only chance to tell it that it should decode.
      if (currentRequest) {
        uint32_t status = 0;
        currentRequest->GetImageStatus(&status);
        if (!(status & imgIRequest::STATUS_DECODE_COMPLETE)) {
          MaybeDecodeForPredictedSize();
        }
        // Increase loading priority if the image is ready to be displayed.
        if (!(status & imgIRequest::STATUS_LOAD_COMPLETE)) {
          currentRequest->BoostPriority(imgIRequest::CATEGORY_DISPLAY);
        }
      }
    } else {
      if (mImage) {
        aLists.Content()->AppendNewToTop<nsDisplayImage>(aBuilder, this, mImage,
                                                         mPrevImage);
      } else if (isImageFromStyle) {
        aLists.Content()->AppendNewToTop<nsDisplayGradient>(aBuilder, this);
      }

      // If we were previously displaying an icon, we're not anymore
      if (mDisplayingIcon) {
        BrokenImageIcon::RemoveObserver(this);
        mDisplayingIcon = false;
      }
    }
  }

  if (ShouldDisplaySelection()) {
    DisplaySelectionOverlay(aBuilder, aLists.Content(),
                            nsISelectionDisplay::DISPLAY_IMAGES);
  }

  BuildDisplayListForNonBlockChildren(aBuilder, aLists);
}

bool nsImageFrame::ShouldDisplaySelection() {
  int16_t displaySelection = PresShell()->GetSelectionFlags();
  if (!(displaySelection & nsISelectionDisplay::DISPLAY_IMAGES)) {
    // no need to check the blue border, we cannot be drawn selected.
    return false;
  }

  if (displaySelection != nsISelectionDisplay::DISPLAY_ALL) {
    return true;
  }

  // If the image is currently resize target of the editor, don't draw the
  // selection overlay.
  HTMLEditor* htmlEditor = nsContentUtils::GetHTMLEditor(PresContext());
  if (!htmlEditor) {
    return true;
  }

  return htmlEditor->GetResizerTarget() != mContent;
}

nsImageMap* nsImageFrame::GetImageMap() {
  if (!mImageMap) {
    if (nsIContent* map = GetMapElement()) {
      mImageMap = new nsImageMap();
      mImageMap->Init(this, map);
    }
  }

  return mImageMap;
}

bool nsImageFrame::IsServerImageMap() {
  return mContent->AsElement()->HasAttr(nsGkAtoms::ismap);
}

CSSIntPoint nsImageFrame::TranslateEventCoords(const nsPoint& aPoint) {
  const nsRect contentRect = GetContentRectRelativeToSelf();
  // Subtract out border and padding here so that the coordinates are
  // now relative to the content area of this frame.
  return CSSPixel::FromAppUnitsRounded(aPoint - contentRect.TopLeft());
}

bool nsImageFrame::GetAnchorHREFTargetAndNode(nsIURI** aHref, nsString& aTarget,
                                              nsIContent** aNode) {
  aTarget.Truncate();
  *aHref = nullptr;
  *aNode = nullptr;

  // Walk up the content tree, looking for an nsIDOMAnchorElement
  for (nsIContent* content = mContent->GetParent(); content;
       content = content->GetParent()) {
    nsCOMPtr<dom::Link> link = do_QueryInterface(content);
    if (!link) {
      continue;
    }
    if (nsCOMPtr<nsIURI> href = link->GetURI()) {
      href.forget(aHref);
    }

    if (auto* anchor = HTMLAnchorElement::FromNode(content)) {
      anchor->GetTarget(aTarget);
    }
    NS_ADDREF(*aNode = content);
    return *aHref != nullptr;
  }
  return false;
}

bool nsImageFrame::IsLeafDynamic() const {
  if (mKind != Kind::ImageLoadingContent) {
    // Image frames created for "content: url()" could have an author-controlled
    // shadow root, we want to be a regular leaf for those.
    return true;
  }
  // For elements that create image frames, calling attachShadow() will throw,
  // so the only ShadowRoot we could ever have is a UA widget.
  const auto* shadow = mContent->AsElement()->GetShadowRoot();
  MOZ_ASSERT_IF(shadow, shadow->IsUAWidget());
  return !shadow;
}

nsresult nsImageFrame::GetContentForEvent(const WidgetEvent* aEvent,
                                          nsIContent** aContent) {
  NS_ENSURE_ARG_POINTER(aContent);

  nsIFrame* f = nsLayoutUtils::GetNonGeneratedAncestor(this);
  if (f != this) {
    return f->GetContentForEvent(aEvent, aContent);
  }

  // XXX We need to make this special check for area element's capturing the
  // mouse due to bug 135040. Remove it once that's fixed.
  nsIContent* capturingContent = aEvent->HasMouseEventMessage()
                                     ? PresShell::GetCapturingContent()
                                     : nullptr;
  if (capturingContent && capturingContent->GetPrimaryFrame() == this) {
    *aContent = capturingContent;
    NS_IF_ADDREF(*aContent);
    return NS_OK;
  }

  if (nsImageMap* map = GetImageMap()) {
    const CSSIntPoint p = TranslateEventCoords(
        nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, RelativeTo{this}));
    nsCOMPtr<nsIContent> area = map->GetArea(p);
    if (area) {
      area.forget(aContent);
      return NS_OK;
    }
  }

  *aContent = GetContent();
  NS_IF_ADDREF(*aContent);
  return NS_OK;
}

// XXX what should clicks on transparent pixels do?
nsresult nsImageFrame::HandleEvent(nsPresContext* aPresContext,
                                   WidgetGUIEvent* aEvent,
                                   nsEventStatus* aEventStatus) {
  NS_ENSURE_ARG_POINTER(aEventStatus);

  if ((aEvent->mMessage == eMouseClick &&
       aEvent->AsMouseEvent()->mButton == MouseButton::ePrimary) ||
      aEvent->mMessage == eMouseMove) {
    nsImageMap* map = GetImageMap();
    bool isServerMap = IsServerImageMap();
    if (map || isServerMap) {
      CSSIntPoint p =
          TranslateEventCoords(nsLayoutUtils::GetEventCoordinatesRelativeTo(
              aEvent, RelativeTo{this}));

      // Even though client-side image map triggering happens
      // through content, we need to make sure we're not inside
      // (in case we deal with a case of both client-side and
      // sever-side on the same image - it happens!)
      const bool inside = map && map->GetArea(p);

      if (!inside && isServerMap) {
        // Server side image maps use the href in a containing anchor
        // element to provide the basis for the destination url.
        nsCOMPtr<nsIURI> uri;
        nsAutoString target;
        nsCOMPtr<nsIContent> anchorNode;
        if (GetAnchorHREFTargetAndNode(getter_AddRefs(uri), target,
                                       getter_AddRefs(anchorNode))) {
          // XXX if the mouse is over/clicked in the border/padding area
          // we should probably just pretend nothing happened. Nav4
          // keeps the x,y coordinates positive as we do; IE doesn't
          // bother. Both of them send the click through even when the
          // mouse is over the border.
          if (p.x < 0) p.x = 0;
          if (p.y < 0) p.y = 0;

          nsAutoCString spec;
          nsresult rv = uri->GetSpec(spec);
          NS_ENSURE_SUCCESS(rv, rv);

          spec += nsPrintfCString("?%d,%d", p.x.value, p.y.value);
          rv = NS_MutateURI(uri).SetSpec(spec).Finalize(uri);
          NS_ENSURE_SUCCESS(rv, rv);

          bool clicked = false;
          if (aEvent->mMessage == eMouseClick && !aEvent->DefaultPrevented()) {
            *aEventStatus = nsEventStatus_eConsumeDoDefault;
            clicked = true;
          }
          nsContentUtils::TriggerLink(anchorNode, uri, target, clicked,
                                      /* isTrusted */ true);
        }
      }
    }
  }

  return nsAtomicContainerFrame::HandleEvent(aPresContext, aEvent,
                                             aEventStatus);
}

Maybe<nsIFrame::Cursor> nsImageFrame::GetCursor(const nsPoint& aPoint) {
  nsImageMap* map = GetImageMap();
  if (!map) {
    return nsIFrame::GetCursor(aPoint);
  }
  const CSSIntPoint p = TranslateEventCoords(aPoint);
  HTMLAreaElement* area = map->GetArea(p);
  if (!area) {
    return nsIFrame::GetCursor(aPoint);
  }

  // Use the cursor from the style of the *area* element.
  RefPtr<ComputedStyle> areaStyle =
      PresShell()->StyleSet()->ResolveStyleLazily(*area);

  // This is one of the cases, like the <xul:tree> pseudo-style stuff, where we
  // get styles out of the blue and expect to trigger image loads for those.
  areaStyle->StartImageLoads(*PresContext()->Document());

  StyleCursorKind kind = areaStyle->StyleUI()->Cursor().keyword;
  if (kind == StyleCursorKind::Auto) {
    kind = StyleCursorKind::Default;
  }
  return Some(Cursor{kind, AllowCustomCursorImage::Yes, std::move(areaStyle)});
}

nsresult nsImageFrame::AttributeChanged(int32_t aNameSpaceID,
                                        nsAtom* aAttribute, int32_t aModType) {
  nsresult rv = nsAtomicContainerFrame::AttributeChanged(aNameSpaceID,
                                                         aAttribute, aModType);
  if (NS_FAILED(rv)) {
    return rv;
  }
  if (nsGkAtoms::alt == aAttribute) {
    PresShell()->FrameNeedsReflow(
        this, IntrinsicDirty::FrameAncestorsAndDescendants, NS_FRAME_IS_DIRTY);
  }
  if (mKind == Kind::XULImage && aAttribute == nsGkAtoms::src &&
      aNameSpaceID == kNameSpaceID_None) {
    UpdateXULImage();
  }
  return NS_OK;
}

void nsImageFrame::OnVisibilityChange(
    Visibility aNewVisibility, const Maybe<OnNonvisible>& aNonvisibleAction) {
  if (mKind == Kind::ImageLoadingContent) {
    nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mContent);
    imageLoader->OnVisibilityChange(aNewVisibility, aNonvisibleAction);
  }

  if (aNewVisibility == Visibility::ApproximatelyVisible &&
      PresShell()->IsActive()) {
    MaybeDecodeForPredictedSize();
  }

  nsAtomicContainerFrame::OnVisibilityChange(aNewVisibility, aNonvisibleAction);
}

#ifdef DEBUG_FRAME_DUMP
nsresult nsImageFrame::GetFrameName(nsAString& aResult) const {
  return MakeFrameName(u"ImageFrame"_ns, aResult);
}

void nsImageFrame::List(FILE* out, const char* aPrefix,
                        ListFlags aFlags) const {
  nsCString str;
  ListGeneric(str, aPrefix, aFlags);

  // output the img src url
  if (nsCOMPtr<imgIRequest> currentRequest = GetCurrentRequest()) {
    nsCOMPtr<nsIURI> uri = currentRequest->GetURI();
    nsAutoCString uristr;
    uri->GetAsciiSpec(uristr);
    str += nsPrintfCString(" [src=%s]", uristr.get());
  }
  fprintf_stderr(out, "%s\n", str.get());
}
#endif

LogicalSides nsImageFrame::GetLogicalSkipSides() const {
  LogicalSides skip(mWritingMode);
  if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
                   StyleBoxDecorationBreak::Clone)) {
    return skip;
  }
  if (GetPrevInFlow()) {
    skip |= eLogicalSideBitsBStart;
  }
  if (GetNextInFlow()) {
    skip |= eLogicalSideBitsBEnd;
  }
  return skip;
}
NS_IMPL_ISUPPORTS(nsImageListener, imgINotificationObserver)

nsImageListener::nsImageListener(nsImageFrame* aFrame) : mFrame(aFrame) {}

nsImageListener::~nsImageListener() = default;

void nsImageListener::Notify(imgIRequest* aRequest, int32_t aType,
                             const nsIntRect* aData) {
  if (!mFrame) {
    return;
  }

  return mFrame->Notify(aRequest, aType, aData);
}

static bool IsInAutoWidthTableCellForQuirk(nsIFrame* aFrame) {
  if (eCompatibility_NavQuirks != aFrame->PresContext()->CompatibilityMode())
    return false;
  // Check if the parent of the closest nsBlockFrame has auto width.
  nsBlockFrame* ancestor = nsLayoutUtils::FindNearestBlockAncestor(aFrame);
  if (ancestor->Style()->GetPseudoType() == PseudoStyleType::cellContent) {
    // Assume direct parent is a table cell frame.
    nsIFrame* grandAncestor = static_cast<nsIFrame*>(ancestor->GetParent());
    return grandAncestor && grandAncestor->StylePosition()->mWidth.IsAuto();
  }
  return false;
}

void nsImageFrame::AddInlineMinISize(gfxContext* aRenderingContext,
                                     nsIFrame::InlineMinISizeData* aData) {
  nscoord isize = nsLayoutUtils::IntrinsicForContainer(
      aRenderingContext, this, IntrinsicISizeType::MinISize);
  bool canBreak = !IsInAutoWidthTableCellForQuirk(this);
  aData->DefaultAddInlineMinISize(this, isize, canBreak);
}

void nsImageFrame::ReleaseGlobals() { BrokenImageIcon::Shutdown(); }