summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/canvas.cpp
blob: cfdb96673917036ab87f46fbce558025b163b0ba (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Tavmjong Bah
 *   PBS <pbs3141@gmail.com>
 *
 * Copyright (C) 2022 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <iostream> // Logging
#include <algorithm> // Sort
#include <set> // Coarsener
#include <thread>
#include <mutex>
#include <array>
#include <cassert>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>
#include <2geom/convex-hull.h>

#include "canvas.h"
#include "canvas-grid.h"

#include "color.h"          // Background color
#include "cms-system.h"     // Color correction
#include "desktop.h"
#include "document.h"
#include "preferences.h"
#include "ui/util.h"
#include "helper/geom.h"

#include "canvas/prefs.h"
#include "canvas/fragment.h"
#include "canvas/util.h"
#include "canvas/stores.h"
#include "canvas/graphics.h"
#include "canvas/synchronizer.h"
#include "display/drawing.h"
#include "display/control/canvas-item-drawing.h"
#include "display/control/canvas-item-group.h"
#include "display/control/snap-indicator.h"

#include "ui/tools/tool-base.h"      // Default cursor

#include "canvas/updaters.h"         // Update strategies
#include "canvas/framecheck.h"       // For frame profiling
#define framecheck_whole_function(D) \
    auto framecheckobj = D->prefs.debug_framecheck ? FrameCheck::Event(__func__) : FrameCheck::Event();

/*
 *   The canvas is responsible for rendering the SVG drawing with various "control"
 *   items below and on top of the drawing. Rendering is triggered by a call to one of:
 *
 *
 *   * redraw_all()     Redraws the entire canvas by calling redraw_area() with the canvas area.
 *
 *   * redraw_area()    Redraws the indicated area. Use when there is a change that doesn't affect
 *                      a CanvasItem's geometry or size.
 *
 *   * request_update() Redraws after recalculating bounds for changed CanvasItems. Use if a
 *                      CanvasItem's geometry or size has changed.
 *
 *   The first three functions add a request to the Gtk's "idle" list via
 *
 *   * add_idle()       Which causes Gtk to call when resources are available:
 *
 *   * on_idle()        Which sets up the backing stores, divides the area of the canvas that has been marked
 *                      unclean into rectangles that are small enough to render quickly, and renders them outwards
 *                      from the mouse with a call to:
 *
 *   * paint_rect_internal() Which paints the rectangle using paint_single_buffer(). It renders onto a Cairo
 *                           surface "backing_store". After a piece is rendered there is a call to:
 *
 *   * queue_draw_area() A Gtk function for marking areas of the window as needing a repaint, which when
 *                       the time is right calls:
 *
 *   * on_draw()        Which blits the Cairo surface to the screen.
 *
 *   The other responsibility of the canvas is to determine where to send GUI events. It does this
 *   by determining which CanvasItem is "picked" and then forwards the events to that item. Not all
 *   items can be picked. As a last resort, the "CatchAll" CanvasItem will be picked as it is the
 *   lowest CanvasItem in the stack (except for the "root" CanvasItem). With a small be of work, it
 *   should be possible to make the "root" CanvasItem a "CatchAll" eliminating the need for a
 *   dedicated "CatchAll" CanvasItem. There probably could be efficiency improvements as some
 *   items that are not pickable probably should be which would save having to effectively pick
 *   them "externally" (e.g. gradient CanvasItemCurves).
 */

namespace Inkscape::UI::Widget {
namespace {

/*
 * Utilities
 */

// GdkEvents can only be safely copied using gdk_event_copy. Since this function allocates, we need the following smart pointer to wrap the result.
struct GdkEventFreer {void operator()(GdkEvent *ev) const {gdk_event_free(ev);}};
using GdkEventUniqPtr = std::unique_ptr<GdkEvent, GdkEventFreer>;

// Copies a GdkEvent, returning the result as a smart pointer.
auto make_unique_copy(GdkEvent const *ev) { return GdkEventUniqPtr(gdk_event_copy(ev)); }

// Convert an integer received from preferences into an Updater enum.
auto pref_to_updater(int index)
{
    constexpr auto arr = std::array{Updater::Strategy::Responsive,
                                    Updater::Strategy::FullRedraw,
                                    Updater::Strategy::Multiscale};
    assert(1 <= index && index <= arr.size());
    return arr[index - 1];
}

// Represents the raster data and location of an in-flight tile (one that is drawn, but not yet pasted into the stores).
struct Tile
{
    Fragment fragment;
    Cairo::RefPtr<Cairo::ImageSurface> surface;
    Cairo::RefPtr<Cairo::ImageSurface> outline_surface;
};

// The urgency with which the async redraw process should exit.
enum class AbortFlags : int
{
    None = 0,
    Soft = 1, // exit if reached prerender phase
    Hard = 2  // exit in any phase
};

// A copy of all the data the async redraw process needs access to, along with its internal state.
struct RedrawData
{
    // Data on what/how to draw.
    Geom::IntPoint mouse_loc;
    Geom::IntRect visible;
    Fragment store;
    bool decoupled_mode;
    Cairo::RefPtr<Cairo::Region> snapshot_drawn;
    Geom::OptIntRect grabbed;

    // Saved prefs
    int coarsener_min_size;
    int coarsener_glue_size;
    double coarsener_min_fullness;
    int tile_size;
    int preempt;
    int margin;
    std::optional<int> redraw_delay;
    int render_time_limit;
    int numthreads;
    bool background_in_stores_required;
    uint64_t page, desk;
    bool debug_framecheck;
    bool debug_show_redraw;

    // State
    std::mutex mutex;
    gint64 start_time;
    int numactive;
    int phase;
    Geom::OptIntRect vis_store;

    Geom::IntRect bounds;
    Cairo::RefPtr<Cairo::Region> clean;
    bool interruptible;
    bool preemptible;
    std::vector<Geom::IntRect> rects;
    int effective_tile_size;

    // Results
    std::mutex tiles_mutex;
    std::vector<Tile> tiles;
    bool timeoutflag;

    // Return comparison object for sorting rectangles by distance from mouse point.
    auto getcmp() const
    {
        return [mouse_loc = mouse_loc] (Geom::IntRect const &a, Geom::IntRect const &b) {
            return a.distanceSq(mouse_loc) > b.distanceSq(mouse_loc);
        };
    }
};

} // namespace

/*
 * Implementation class
 */

class CanvasPrivate
{
public:
    friend class Canvas;
    Canvas *q;
    CanvasPrivate(Canvas *q)
        : q(q)
        , stores(prefs) {}

    // Lifecycle
    bool active = false;
    void activate();
    void deactivate();

    // CanvasItem tree
    std::optional<CanvasItemContext> canvasitem_ctx;

    // Preferences
    Prefs prefs;

    // Stores
    Stores stores;
    void handle_stores_action(Stores::Action action);

    // Invalidation
    std::unique_ptr<Updater> updater; // Tracks the unclean region and decides how to redraw it.
    Cairo::RefPtr<Cairo::Region> invalidated; // Buffers invalidations while the updater is in use by the background process.

    // Graphics state; holds all the graphics resources, including the drawn content.
    std::unique_ptr<Graphics> graphics;
    void activate_graphics();
    void deactivate_graphics();

    // Redraw process management.
    bool redraw_active = false;
    bool redraw_requested = false;
    sigc::connection schedule_redraw_conn;
    void schedule_redraw();
    void launch_redraw();
    void after_redraw();
    void commit_tiles();

    // Event handling.
    bool process_event(const GdkEvent*);
    bool pick_current_item(const GdkEvent*);
    bool emit_event(const GdkEvent*);
    Inkscape::CanvasItem *pre_scroll_grabbed_item;

    // Various state affecting what is drawn.
    uint32_t desk   = 0xffffffff; // The background colour, with the alpha channel used to control checkerboard.
    uint32_t border = 0x000000ff; // The border colour, used only to control shadow colour.
    uint32_t page   = 0xffffffff; // The page colour, also with alpha channel used to control checkerboard.

    bool clip_to_page = false; // Whether to enable clip-to-page mode.
    PageInfo pi; // The list of page rectangles.
    std::optional<Geom::PathVector> calc_page_clip() const; // Union of the page rectangles if in clip-to-page mode, otherwise no clip.

    int scale_factor = 1; // The device scale the stores are drawn at.

    bool outlines_enabled = false; // Whether to enable the outline layer.
    bool outlines_required() const { return q->_split_mode != Inkscape::SplitMode::NORMAL || q->_render_mode == Inkscape::RenderMode::OUTLINE_OVERLAY; }

    bool background_in_stores_enabled = false; // Whether the page and desk should be drawn into the stores/tiles; if not then transparency is used instead.
    bool background_in_stores_required() const { return !q->get_opengl_enabled() && SP_RGBA32_A_U(page) == 255 && SP_RGBA32_A_U(desk) == 255; } // Enable solid colour optimisation if both page and desk are solid (as opposed to checkerboard).

    // Async redraw process.
    std::optional<boost::asio::thread_pool> pool;
    int numthreads;
    int get_numthreads() const;

    Synchronizer sync;
    RedrawData rd;
    std::atomic<int> abort_flags;

    void init_tiler();
    bool init_redraw();
    bool end_redraw(); // returns true to indicate further redraw cycles required
    void process_redraw(Geom::IntRect const &bounds, Cairo::RefPtr<Cairo::Region> clean, bool interruptible = true, bool preemptible = true);
    void render_tile(int debug_id);
    void paint_rect(Geom::IntRect const &rect);
    void paint_single_buffer(const Cairo::RefPtr<Cairo::ImageSurface> &surface, const Geom::IntRect &rect, bool need_background, bool outline_pass);
    void paint_error_buffer(const Cairo::RefPtr<Cairo::ImageSurface> &surface);

    // Trivial overload of GtkWidget function.
    void queue_draw_area(Geom::IntRect const &rect);

    // For tracking the last known mouse position. (The function Gdk::Window::get_device_position cannot be used because of slow X11 round-trips. Remove this workaround when X11 dies.)
    std::optional<Geom::IntPoint> last_mouse;

    // Auto-scrolling.
    std::optional<guint> tick_callback;
    std::optional<gint64> last_time;
    Geom::IntPoint strain;
    Geom::Point displacement, velocity;
    void autoscroll_begin(Geom::IntPoint const &to);
    void autoscroll_end();
};

/*
 * Lifecycle
 */

Canvas::Canvas()
    : d(std::make_unique<CanvasPrivate>(this))
{
    set_name("InkscapeCanvas");

    // Events
    add_events(Gdk::BUTTON_PRESS_MASK   |
               Gdk::BUTTON_RELEASE_MASK |
               Gdk::ENTER_NOTIFY_MASK   |
               Gdk::LEAVE_NOTIFY_MASK   |
               Gdk::FOCUS_CHANGE_MASK   |
               Gdk::KEY_PRESS_MASK      |
               Gdk::KEY_RELEASE_MASK    |
               Gdk::POINTER_MOTION_MASK |
               Gdk::SCROLL_MASK         |
               Gdk::SMOOTH_SCROLL_MASK  );

    // Updater
    d->updater = Updater::create(pref_to_updater(d->prefs.update_strategy));
    d->updater->reset();
    d->invalidated = Cairo::Region::create();

    // Preferences
    d->prefs.grabsize.action = [=] { d->canvasitem_ctx->root()->update_canvas_item_ctrl_sizes(d->prefs.grabsize); };
    d->prefs.debug_show_unclean.action = [=] { queue_draw(); };
    d->prefs.debug_show_clean.action = [=] { queue_draw(); };
    d->prefs.debug_disable_redraw.action = [=] { d->schedule_redraw(); };
    d->prefs.debug_sticky_decoupled.action = [=] { d->schedule_redraw(); };
    d->prefs.debug_animate.action = [=] { queue_draw(); };
    d->prefs.outline_overlay_opacity.action = [=] { queue_draw(); };
    d->prefs.softproof.action = [=] { redraw_all(); };
    d->prefs.displayprofile.action = [=] { redraw_all(); };
    d->prefs.request_opengl.action = [=] {
        if (get_realized()) {
            d->deactivate();
            d->deactivate_graphics();
            set_opengl_enabled(d->prefs.request_opengl);
            d->updater->reset();
            d->activate_graphics();
            d->activate();
        }
    };
    d->prefs.pixelstreamer_method.action = [=] {
        if (get_realized() && get_opengl_enabled()) {
            d->deactivate();
            d->deactivate_graphics();
            d->activate_graphics();
            d->activate();
        }
    };
    d->prefs.numthreads.action = [=] {
        if (!d->active) return;
        int const new_numthreads = d->get_numthreads();
        if (d->numthreads == new_numthreads) return;
        d->numthreads = new_numthreads;
        d->deactivate();
        d->deactivate_graphics();
        d->pool.emplace(d->numthreads);
        d->activate_graphics();
        d->activate();
    };

    // Canvas item tree
    d->canvasitem_ctx.emplace(this);

    // Split view.
    _split_direction = Inkscape::SplitDirection::EAST;
    _split_frac = {0.5, 0.5};

    // Recreate stores on HiDPI change.
    property_scale_factor().signal_changed().connect([this] { d->schedule_redraw(); });

    // OpenGL switch.
    set_opengl_enabled(d->prefs.request_opengl);

    // Async redraw process.
    d->numthreads = d->get_numthreads();
    d->pool.emplace(d->numthreads);

    d->sync.connectExit([this] { d->after_redraw(); });
}

int CanvasPrivate::get_numthreads() const
{
    if (int n = prefs.numthreads; n > 0) {
        // First choice is the value set in preferences.
        return n;
    } else if (int n = std::thread::hardware_concurrency(); n > 0) {
        // If not set, use the number of processors minus one. (Using all of them causes stuttering.)
        return n == 1 ? 1 : n - 1;
    } else {
        // If not reported, use a sensible fallback.
        return 4;
    }
}

// Graphics becomes active when the widget is realized.
void CanvasPrivate::activate_graphics()
{
    if (q->get_opengl_enabled()) {
        q->make_current();
        graphics = Graphics::create_gl(prefs, stores, pi);
    } else {
        graphics = Graphics::create_cairo(prefs, stores, pi);
    }
    stores.set_graphics(graphics.get());
    stores.reset();
}

// After graphics becomes active, the canvas becomes active when additionally a drawing is set.
void CanvasPrivate::activate()
{
    // Event handling/item picking
    q->_pick_event.type = GDK_LEAVE_NOTIFY;
    q->_pick_event.crossing.x = 0;
    q->_pick_event.crossing.y = 0;

    q->_in_repick         = false;
    q->_left_grabbed_item = false;
    q->_all_enter_events  = false;
    q->_is_dragging       = false;
    q->_state             = 0;

    q->_current_canvas_item     = nullptr;
    q->_current_canvas_item_new = nullptr;
    q->_grabbed_canvas_item     = nullptr;
    q->_grabbed_event_mask = (Gdk::EventMask)0;
    pre_scroll_grabbed_item = nullptr;

    // Drawing
    q->_need_update = true;

    // Split view
    q->_split_dragging = false;

    // Todo: Disable GTK event compression again when doing so is no longer buggy.
    // Note: ToolBase::set_high_motion_precision() will keep turning it back on.
    // q->get_window()->set_event_compression(false);

    active = true;

    schedule_redraw();
}

void CanvasPrivate::deactivate()
{
    active = false;

    if (redraw_active) {
        if (schedule_redraw_conn.connected()) {
            // In first link in chain, from schedule_redraw() to launch_redraw(). Break the link and exit.
            schedule_redraw_conn.disconnect();
        } else {
            // Otherwise, the background process is running. Interrupt the signal chain at exit.
            abort_flags.store((int)AbortFlags::Hard, std::memory_order_relaxed);
            if (prefs.debug_logging) std::cout << "Hard exit request" << std::endl;
            sync.waitForExit();

            // Unsnapshot the CanvasItems and DrawingItems.
            canvasitem_ctx->unsnapshot();
            q->_drawing->unsnapshot();
        }

        redraw_active = false;
        redraw_requested = false;
        assert(!schedule_redraw_conn.connected());
    }
}

void CanvasPrivate::deactivate_graphics()
{
    if (q->get_opengl_enabled()) q->make_current();
    commit_tiles();
    stores.set_graphics(nullptr);
    graphics.reset();
}

Canvas::~Canvas()
{
    // Remove entire CanvasItem tree.
    d->canvasitem_ctx.reset();
}

void Canvas::set_drawing(Drawing *drawing)
{
    if (d->active && !drawing) d->deactivate();
    _drawing = drawing;
    if (_drawing) {
        _drawing->setRenderMode(_render_mode == RenderMode::OUTLINE_OVERLAY ? RenderMode::NORMAL : _render_mode);
        _drawing->setColorMode(_color_mode);
        _drawing->setOutlineOverlay(d->outlines_required());
    }
    if (!d->active && get_realized() && drawing) d->activate();
}

CanvasItemGroup *Canvas::get_canvas_item_root() const
{
    return d->canvasitem_ctx->root();
}

void Canvas::on_realize()
{
    parent_type::on_realize();
    d->activate_graphics();
    if (_drawing) d->activate();
}

void Canvas::on_unrealize()
{
    if (_drawing) d->deactivate();
    d->deactivate_graphics();
    parent_type::on_unrealize();
}

/*
 * Redraw process managment
 */

// Schedule another redraw iteration to take place, waiting for the current one to finish if necessary.
void CanvasPrivate::schedule_redraw()
{
    if (!active) {
        // We can safely discard calls until active, because we will run an iteration on activation later in initialisation.
        return;
    }

    // Ensure another iteration is performed if one is in progress.
    redraw_requested = true;

    if (redraw_active) {
        return;
    }

    redraw_active = true;

    // Call run_redraw() as soon as possible on the main loop. (Cannot run now since CanvasItem tree could be in an invalid intermediate state.)
    assert(!schedule_redraw_conn.connected());
    schedule_redraw_conn = Glib::signal_idle().connect([this] {
        if (q->get_opengl_enabled()) {
            q->make_current();
        }
        if (prefs.debug_logging) std::cout << "Redraw start" << std::endl;
        launch_redraw();
        return false;
    }, Glib::PRIORITY_HIGH);
}

// Update state and launch redraw process in background. Requires a current OpenGL context.
void CanvasPrivate::launch_redraw()
{
    assert(redraw_active);

    // Determine whether the rendering parameters have changed, and trigger full store recreation if so.
    if ((outlines_required() && !outlines_enabled) || scale_factor != q->get_scale_factor()) {
        stores.reset();
    }

    outlines_enabled = outlines_required();
    scale_factor = q->get_scale_factor();

    graphics->set_outlines_enabled(outlines_enabled);
    graphics->set_scale_factor(scale_factor);

    /*
     * Update state.
     */

    // Page information.
    pi.pages.clear();
    canvasitem_ctx->root()->visit_page_rects([this] (auto &rect) {
        pi.pages.emplace_back(rect);
    });

    graphics->set_colours(page, desk, border);
    graphics->set_background_in_stores(background_in_stores_required());

    q->_drawing->setClip(calc_page_clip());

    // Stores.
    handle_stores_action(stores.update(Fragment{ q->_affine, q->get_area_world() }));

    // Geometry.
    bool const affine_changed = canvasitem_ctx->affine() != stores.store().affine;
    if (q->_need_update || affine_changed) {
        FrameCheck::Event fc;
        if (prefs.debug_framecheck) fc = FrameCheck::Event("update");
        q->_need_update = false;
        canvasitem_ctx->setAffine(stores.store().affine);
        canvasitem_ctx->root()->update(affine_changed);
    }

    // Update strategy.
    auto const strategy = pref_to_updater(prefs.update_strategy);
    if (updater->get_strategy() != strategy) {
        auto new_updater = Updater::create(strategy);
        new_updater->clean_region = std::move(updater->clean_region);
        updater = std::move(new_updater);
    }

    updater->mark_dirty(invalidated);
    invalidated = Cairo::Region::create();

    updater->next_frame();

    /*
     * Launch redraw process in background.
     */

    // If asked to, don't paint anything and instead halt the redraw process.
    if (prefs.debug_disable_redraw) {
        redraw_active = false;
        return;
    }

    // Snapshot the CanvasItems and DrawingItems.
    canvasitem_ctx->snapshot();
    q->_drawing->snapshot();

    // Get the mouse position in screen space.
    rd.mouse_loc = last_mouse.value_or((Geom::Point(q->get_dimensions()) / 2).round());

    // Map the mouse to canvas space.
    rd.mouse_loc += q->_pos;
    if (stores.mode() == Stores::Mode::Decoupled) {
        rd.mouse_loc = (Geom::Point(rd.mouse_loc) * q->_affine.inverse() * stores.store().affine).round();
    }

    // Get the visible rect.
    rd.visible = q->get_area_world();
    if (stores.mode() == Stores::Mode::Decoupled) {
        rd.visible = (Geom::Parallelogram(rd.visible) * q->_affine.inverse() * stores.store().affine).bounds().roundOutwards();
    }

    // Get other misc data.
    rd.store = Fragment{ stores.store().affine, stores.store().rect };
    rd.decoupled_mode = stores.mode() == Stores::Mode::Decoupled;
    rd.coarsener_min_size = prefs.coarsener_min_size;
    rd.coarsener_glue_size = prefs.coarsener_glue_size;
    rd.coarsener_min_fullness = prefs.coarsener_min_fullness;
    rd.tile_size = prefs.tile_size;
    rd.preempt = prefs.preempt;
    rd.margin = prefs.prerender;
    rd.redraw_delay = prefs.debug_delay_redraw ? std::make_optional<int>(prefs.debug_delay_redraw_time) : std::nullopt;
    rd.render_time_limit = prefs.render_time_limit;
    rd.numthreads = get_numthreads();
    rd.background_in_stores_required = background_in_stores_required();
    rd.page = page;
    rd.desk = desk;
    rd.debug_framecheck = prefs.debug_framecheck;
    rd.debug_show_redraw = prefs.debug_show_redraw;

    rd.snapshot_drawn = stores.snapshot().drawn ? stores.snapshot().drawn->copy() : Cairo::RefPtr<Cairo::Region>();
    rd.grabbed = q->_grabbed_canvas_item && prefs.block_updates ? (roundedOutwards(q->_grabbed_canvas_item->get_bounds()) & rd.visible & rd.store.rect).regularized() : Geom::OptIntRect();

    abort_flags.store((int)AbortFlags::None, std::memory_order_relaxed);

    boost::asio::post(*pool, [this] { init_tiler(); });
}

void CanvasPrivate::after_redraw()
{
    assert(redraw_active);

    // Unsnapshot the CanvasItems and DrawingItems.
    canvasitem_ctx->unsnapshot();
    q->_drawing->unsnapshot();

    // OpenGL context needed for commit_tiles(), stores.finished_draw(), and launch_redraw().
    if (q->get_opengl_enabled()) {
        q->make_current();
    }

    // Commit tiles before stores.finished_draw() to avoid changing stores while tiles are still pending.
    commit_tiles();

    // Handle any pending stores action.
    bool stores_changed = false;
    if (!rd.timeoutflag) {
        auto const ret = stores.finished_draw(Fragment{ q->_affine, q->get_area_world() });
        handle_stores_action(ret);
        if (ret != Stores::Action::None) {
            stores_changed = true;
        }
    }

    // Relaunch or stop as necessary.
    if (rd.timeoutflag || redraw_requested || stores_changed) {
        if (prefs.debug_logging) std::cout << "Continuing redrawing" << std::endl;
        redraw_requested = false;
        launch_redraw();
    } else {
        if (prefs.debug_logging) std::cout << "Redraw exit" << std::endl;
        redraw_active = false;
    }
}

void CanvasPrivate::handle_stores_action(Stores::Action action)
{
    switch (action) {
        case Stores::Action::Recreated:
            // Set everything as needing redraw.
            invalidated->do_union(geom_to_cairo(stores.store().rect));
            updater->reset();

            if (prefs.debug_show_unclean) q->queue_draw();
            break;

        case Stores::Action::Shifted:
            invalidated->intersect(geom_to_cairo(stores.store().rect));
            updater->intersect(stores.store().rect);

            if (prefs.debug_show_unclean) q->queue_draw();
            break;

        default:
            break;
    }

    if (action != Stores::Action::None) {
        q->_drawing->setCacheLimit(stores.store().rect);
    }
}

// Commit all in-flight tiles to the stores. Requires a current OpenGL context (for graphics->draw_tile).
void CanvasPrivate::commit_tiles()
{
    framecheck_whole_function(this)

    decltype(rd.tiles) tiles;

    {
        auto lock = std::lock_guard(rd.tiles_mutex);
        tiles = std::move(rd.tiles);
    }

    for (auto &tile : tiles) {
        // Todo: Make CMS system thread-safe, then move this to render thread too.
        if (q->_cms_active) {
            auto transf = prefs.from_display
                        ? Inkscape::CMSSystem::getDisplayPer(q->_cms_key)
                        : Inkscape::CMSSystem::getDisplayTransform();
            if (transf) {
                tile.surface->flush();
                auto px = tile.surface->get_data();
                int stride = tile.surface->get_stride();
                for (int i = 0; i < tile.surface->get_height(); i++) {
                    auto row = px + i * stride;
                    Inkscape::CMSSystem::doTransform(transf, row, row, tile.surface->get_width());
                }
                tile.surface->mark_dirty();
            }
        }

        // Paste tile content onto stores.
        graphics->draw_tile(tile.fragment, std::move(tile.surface), std::move(tile.outline_surface));

        // Add to drawn region.
        assert(stores.store().rect.contains(tile.fragment.rect));
        stores.mark_drawn(tile.fragment.rect);

        // Get the rectangle of screen-space needing repaint.
        Geom::IntRect repaint_rect;
        if (stores.mode() == Stores::Mode::Normal) {
            // Simply translate to get back to screen space.
            repaint_rect = tile.fragment.rect - q->_pos;
        } else {
            // Transform into screen space, take bounding box, and round outwards.
            auto pl = Geom::Parallelogram(tile.fragment.rect);
            pl *= stores.store().affine.inverse() * q->_affine;
            pl *= Geom::Translate(-q->_pos);
            repaint_rect = pl.bounds().roundOutwards();
        }

        // Check if repaint is necessary - some rectangles could be entirely off-screen.
        auto screen_rect = Geom::IntRect({0, 0}, q->get_dimensions());
        if ((repaint_rect & screen_rect).regularized()) {
            // Schedule repaint.
            queue_draw_area(repaint_rect);
        }
    }
}

/*
 * Auto-scrolling
 */

static Geom::Point cap_length(Geom::Point const &pt, double max)
{
    auto const r = pt.length();
    return r <= max ? pt : pt * max / r;
}

static double profile(double r)
{
    constexpr double max_speed = 30.0;
    constexpr double max_distance = 25.0;
    return std::clamp(Geom::sqr(r / max_distance) * max_speed, 1.0, max_speed);
}

static Geom::Point apply_profile(Geom::Point const &pt)
{
    auto const r = pt.length();
    if (r <= Geom::EPSILON) return {};
    return pt * profile(r) / r;
}

void CanvasPrivate::autoscroll_begin(Geom::IntPoint const &to)
{
    if (!q->_desktop) {
        return;
    }

    auto const rect = expandedBy(Geom::IntRect({}, q->get_dimensions()), -(int)prefs.autoscrolldistance);
    strain = to - rect.clamp(to);

    if (strain == Geom::IntPoint(0, 0) || tick_callback) {
        return;
    }

    tick_callback = q->add_tick_callback([this] (Glib::RefPtr<Gdk::FrameClock> const &clock) {
        auto timings = clock->get_current_timings();
        auto const t = timings->get_frame_time();
        double dt;
        if (last_time) {
            dt = t - *last_time;
        } else {
            dt = timings->get_refresh_interval();
        }
        last_time = t;
        dt *= 60.0 / 1e6 * prefs.autoscrollspeed;

        bool const strain_zero = strain == Geom::IntPoint(0, 0);

        if (strain.x() * velocity.x() < 0) velocity.x() = 0;
        if (strain.y() * velocity.y() < 0) velocity.y() = 0;
        auto const tgtvel = apply_profile(strain);
        auto const max_accel = strain_zero ? 3 : 2;
        velocity += cap_length(tgtvel - velocity, max_accel * dt);
        displacement += velocity * dt;
        auto const dpos = displacement.round();
        q->_desktop->scroll_relative(-dpos);
        displacement -= dpos;

        if (last_mouse) {
            GdkEventMotion event;
            memset(&event, 0, sizeof(GdkEventMotion));
            event.type = GDK_MOTION_NOTIFY;
            event.x = last_mouse->x();
            event.y = last_mouse->y();
            event.state = q->_state;
            emit_event(reinterpret_cast<GdkEvent*>(&event));
        }

        if (strain_zero && velocity.length() <= 0.1) {
            tick_callback = {};
            last_time = {};
            displacement = velocity = {};
            return false;
        }

        q->queue_draw();

        return true;
    });
}

void CanvasPrivate::autoscroll_end()
{
    strain = {};
}

// Allow auto-scrolling to take place if the mouse reaches the edge.
// The effect wears off when the mouse is next released.
void Canvas::enable_autoscroll()
{
    if (d->last_mouse) {
        d->autoscroll_begin(*d->last_mouse);
    } else {
        d->autoscroll_end();
    }
}

/*
 * Event handling
 */

bool Canvas::on_scroll_event(GdkEventScroll *scroll_event)
{
    return d->process_event(reinterpret_cast<GdkEvent*>(scroll_event));
}

bool Canvas::on_button_press_event(GdkEventButton *button_event)
{
    return on_button_event(button_event);
}

bool Canvas::on_button_release_event(GdkEventButton *button_event)
{
    if (button_event->button == 1) {
        d->autoscroll_end();
    }

    return on_button_event(button_event);
}

// Unified handler for press and release events.
bool Canvas::on_button_event(GdkEventButton *button_event)
{
    // Sanity-check event type.
    switch (button_event->type) {
        case GDK_BUTTON_PRESS:
        case GDK_2BUTTON_PRESS:
        case GDK_3BUTTON_PRESS:
        case GDK_BUTTON_RELEASE:
            break; // Good
        default:
            std::cerr << "Canvas::on_button_event: illegal event type!" << std::endl;
            return false;
    }

    // Drag the split view controller.
    if (_split_mode == Inkscape::SplitMode::SPLIT) {
        auto cursor_position = Geom::IntPoint(button_event->x, button_event->y);
        switch (button_event->type) {
            case GDK_BUTTON_PRESS:
                if (_hover_direction != Inkscape::SplitDirection::NONE) {
                    _split_dragging = true;
                    _split_drag_start = cursor_position;
                    return true;
                }
                break;
            case GDK_2BUTTON_PRESS:
                if (_hover_direction != Inkscape::SplitDirection::NONE) {
                    _split_direction = _hover_direction;
                    _split_dragging = false;
                    queue_draw();
                    return true;
                }
                break;
            case GDK_BUTTON_RELEASE:
                if (!_split_dragging) break;
                _split_dragging = false;

                // Check if we are near the edge. If so, revert to normal mode.
                if (cursor_position.x() < 5                                 ||
                    cursor_position.y() < 5                                 ||
                    cursor_position.x() > get_allocation().get_width()  - 5 ||
                    cursor_position.y() > get_allocation().get_height() - 5)
                {
                    // Reset everything.
                    _split_frac = {0.5, 0.5};
                    set_cursor();
                    set_split_mode(Inkscape::SplitMode::NORMAL);

                    // Update action (turn into utility function?).
                    auto window = dynamic_cast<Gtk::ApplicationWindow*>(get_toplevel());
                    if (!window) {
                        std::cerr << "Canvas::on_motion_notify_event: window missing!" << std::endl;
                        return true;
                    }

                    auto action = window->lookup_action("canvas-split-mode");
                    if (!action) {
                        std::cerr << "Canvas::on_motion_notify_event: action 'canvas-split-mode' missing!" << std::endl;
                        return true;
                    }

                    auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
                    if (!saction) {
                        std::cerr << "Canvas::on_motion_notify_event: action 'canvas-split-mode' not SimpleAction!" << std::endl;
                        return true;
                    }

                    saction->change_state(static_cast<int>(Inkscape::SplitMode::NORMAL));
                }

                break;

            default:
                break;
        }
    }

    return d->process_event(reinterpret_cast<GdkEvent*>(button_event));
}

bool Canvas::on_enter_notify_event(GdkEventCrossing *crossing_event)
{
    if (crossing_event->window != get_window()->gobj()) {
        return false;
    }
    return d->process_event(reinterpret_cast<GdkEvent*>(crossing_event));
}

bool Canvas::on_leave_notify_event(GdkEventCrossing *crossing_event)
{
    if (crossing_event->window != get_window()->gobj()) {
        return false;
    }
    d->last_mouse = {};
    return d->process_event(reinterpret_cast<GdkEvent*>(crossing_event));
}

bool Canvas::on_focus_in_event(GdkEventFocus *focus_event)
{
    grab_focus();
    return false;
}

bool Canvas::on_key_press_event(GdkEventKey *key_event)
{
    return d->process_event(reinterpret_cast<GdkEvent*>(key_event));
}

bool Canvas::on_key_release_event(GdkEventKey *key_event)
{
    return d->process_event(reinterpret_cast<GdkEvent*>(key_event));
}

bool Canvas::on_motion_notify_event(GdkEventMotion *motion_event)
{
    // Record the last mouse position.
    d->last_mouse = Geom::IntPoint(motion_event->x, motion_event->y);

    // Handle interactions with the split view controller.
    if (_split_mode == Inkscape::SplitMode::XRAY) {
        queue_draw();
    } else if (_split_mode == Inkscape::SplitMode::SPLIT) {
        auto cursor_position = Geom::IntPoint(motion_event->x, motion_event->y);

        // Move controller.
        if (_split_dragging) {
            auto delta = cursor_position - _split_drag_start;
            if (_hover_direction == Inkscape::SplitDirection::HORIZONTAL) {
                delta.x() = 0;
            } else if (_hover_direction == Inkscape::SplitDirection::VERTICAL) {
                delta.y() = 0;
            }
            _split_frac += Geom::Point(delta) / get_dimensions();
            _split_drag_start = cursor_position;
            queue_draw();
            return true;
        }

        auto split_position = (_split_frac * get_dimensions()).round();
        auto diff = cursor_position - split_position;
        auto hover_direction = Inkscape::SplitDirection::NONE;
        if (Geom::Point(diff).length() < 20.0) {
            // We're hovering over circle, figure out which direction we are in.
            if (diff.y() - diff.x() > 0) {
                if (diff.y() + diff.x() > 0) {
                    hover_direction = Inkscape::SplitDirection::SOUTH;
                } else {
                    hover_direction = Inkscape::SplitDirection::WEST;
                }
            } else {
                if (diff.y() + diff.x() > 0) {
                    hover_direction = Inkscape::SplitDirection::EAST;
                } else {
                    hover_direction = Inkscape::SplitDirection::NORTH;
                }
            }
        } else if (_split_direction == Inkscape::SplitDirection::NORTH ||
                   _split_direction == Inkscape::SplitDirection::SOUTH)
        {
            if (std::abs(diff.y()) < 3) {
                // We're hovering over the horizontal line.
                hover_direction = Inkscape::SplitDirection::HORIZONTAL;
            }
        } else {
            if (std::abs(diff.x()) < 3) {
                // We're hovering over the vertical line.
                hover_direction = Inkscape::SplitDirection::VERTICAL;
            }
        }

        if (_hover_direction != hover_direction) {
            _hover_direction = hover_direction;
            set_cursor();
            queue_draw();
        }

        if (_hover_direction != Inkscape::SplitDirection::NONE) {
            // We're hovering, don't pick or emit event.
            return true;
        }
    }

    // Avoid embarrassing neverending autoscroll in case the button-released handler somehow doesn't fire.
    if (!(motion_event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK))) {
        d->autoscroll_end();
    }

    return d->process_event(reinterpret_cast<GdkEvent*>(motion_event));
}

// Unified handler for all events.
bool CanvasPrivate::process_event(const GdkEvent *event)
{
    framecheck_whole_function(this)

    if (!active) {
        std::cerr << "Canvas::process_event: Called while not active!" << std::endl;
        return false;
    }

    auto calc_button_mask = [&] () -> int {
        switch (event->button.button) {
            case 1:  return GDK_BUTTON1_MASK; break;
            case 2:  return GDK_BUTTON2_MASK; break;
            case 3:  return GDK_BUTTON3_MASK; break;
            case 4:  return GDK_BUTTON4_MASK; break;
            case 5:  return GDK_BUTTON5_MASK; break;
            default: return 0; // Buttons can range at least to 9 but mask defined only to 5.
        }
    };

    // Do event-specific processing.
    switch (event->type) {
        case GDK_SCROLL:
        {
            // Save the current event-receiving item just before scrolling starts. It will continue to receive scroll events until the mouse is moved.
            if (!pre_scroll_grabbed_item) {
                pre_scroll_grabbed_item = q->_current_canvas_item;
                if (q->_grabbed_canvas_item && !q->_current_canvas_item->is_descendant_of(q->_grabbed_canvas_item)) {
                    pre_scroll_grabbed_item = q->_grabbed_canvas_item;
                }
            }

            // Process the scroll event...
            bool retval = emit_event(event);

            // ...then repick.
            q->_state = event->scroll.state;
            pick_current_item(event);

            return retval;
        }

        case GDK_BUTTON_PRESS:
        case GDK_2BUTTON_PRESS:
        case GDK_3BUTTON_PRESS:
        {
            pre_scroll_grabbed_item = nullptr;

            // Pick the current item as if the button were not pressed...
            q->_state = event->button.state;
            pick_current_item(event);

            // ...then process the event.
            q->_state ^= calc_button_mask();
            return emit_event(event);
        }

        case GDK_BUTTON_RELEASE:
        {
            pre_scroll_grabbed_item = nullptr;

            // Process the event as if the button were pressed...
            q->_state = event->button.state;
            bool retval = emit_event(event);

            // ...then repick after the button has been released.
            auto event_copy = make_unique_copy(event);
            event_copy->button.state ^= calc_button_mask();
            q->_state = event_copy->button.state;
            pick_current_item(event_copy.get());

            return retval;
        }

        case GDK_ENTER_NOTIFY:
            pre_scroll_grabbed_item = nullptr;
            q->_state = event->crossing.state;
            return pick_current_item(event);

        case GDK_LEAVE_NOTIFY:
            pre_scroll_grabbed_item = nullptr;
            q->_state = event->crossing.state;
            // This is needed to remove alignment or distribution snap indicators.
            if (q->_desktop) {
                q->_desktop->snapindicator->remove_snaptarget();
            }
            return pick_current_item(event);

        case GDK_KEY_PRESS:
        case GDK_KEY_RELEASE:
            return emit_event(event);

        case GDK_MOTION_NOTIFY:
            pre_scroll_grabbed_item = nullptr;
            q->_state = event->motion.state;
            pick_current_item(event);
            return emit_event(event);

        default:
            return false;
    }
}

// This function is called by 'process_event' to manipulate the state variables relating
// to the current object under the mouse, for example, to generate enter and leave events.
//
// This routine reacts to events from the canvas. Its main purpose is to find the canvas item
// closest to the cursor where the event occurred and then send the event (sometimes modified) to
// that item. The event then bubbles up the canvas item tree until an object handles it. If the
// widget is redrawn, this routine may be called again for the same event.
//
// Canvas items register their interest by connecting to the "event" signal.
// Example in desktop.cpp:
//   canvas_catchall->connect_event(sigc::bind(sigc::ptr_fun(sp_desktop_root_handler), this));
bool CanvasPrivate::pick_current_item(const GdkEvent *event)
{
    // Ensure requested geometry updates are performed first.
    if (q->_need_update && !q->_drawing->snapshotted() && !canvasitem_ctx->snapshotted()) {
        FrameCheck::Event fc;
        if (prefs.debug_framecheck) fc = FrameCheck::Event("update", 1);
        q->_need_update = false;
        canvasitem_ctx->root()->update(false);
    }

    int button_down = 0;
    if (!q->_all_enter_events) {
        // Only set true in connector-tool.cpp.

        // If a button is down, we'll perform enter and leave events on the
        // current item, but not enter on any other item.  This is more or
        // less like X pointer grabbing for canvas items.
        button_down = q->_state & (GDK_BUTTON1_MASK |
                                   GDK_BUTTON2_MASK |
                                   GDK_BUTTON3_MASK |
                                   GDK_BUTTON4_MASK |
                                   GDK_BUTTON5_MASK);
        if (!button_down) q->_left_grabbed_item = false;
    }

    // Save the event in the canvas.  This is used to synthesize enter and
    // leave events in case the current item changes.  It is also used to
    // re-pick the current item if the current one gets deleted.  Also,
    // synthesize an enter event.
    if (event != &q->_pick_event) {
        if (event->type == GDK_MOTION_NOTIFY || event->type == GDK_SCROLL || event->type == GDK_BUTTON_RELEASE) {
            // Convert to GDK_ENTER_NOTIFY

            // These fields have the same offsets in all types of events.
            q->_pick_event.crossing.type       = GDK_ENTER_NOTIFY;
            q->_pick_event.crossing.window     = event->motion.window;
            q->_pick_event.crossing.send_event = event->motion.send_event;
            q->_pick_event.crossing.subwindow  = nullptr;
            q->_pick_event.crossing.x          = event->motion.x;
            q->_pick_event.crossing.y          = event->motion.y;
            q->_pick_event.crossing.mode       = GDK_CROSSING_NORMAL;
            q->_pick_event.crossing.detail     = GDK_NOTIFY_NONLINEAR;
            q->_pick_event.crossing.focus      = false;

            // These fields don't have the same offsets in all types of events.
            switch (event->type)
            {
                case GDK_MOTION_NOTIFY:
                    q->_pick_event.crossing.state  = event->motion.state;
                    q->_pick_event.crossing.x_root = event->motion.x_root;
                    q->_pick_event.crossing.y_root = event->motion.y_root;
                    break;
                case GDK_SCROLL:
                    q->_pick_event.crossing.state  = event->scroll.state;
                    q->_pick_event.crossing.x_root = event->scroll.x_root;
                    q->_pick_event.crossing.y_root = event->scroll.y_root;
                    break;
                case GDK_BUTTON_RELEASE:
                    q->_pick_event.crossing.state  = event->button.state;
                    q->_pick_event.crossing.x_root = event->button.x_root;
                    q->_pick_event.crossing.y_root = event->button.y_root;
                    break;
                default:
                    assert(false);
            }

        } else {
            q->_pick_event = *event;
        }
    }

    if (q->_in_repick) {
        // Don't do anything else if this is a recursive call.
        return false;
    }

    // Find new item
    q->_current_canvas_item_new = nullptr;

    if (q->_pick_event.type != GDK_LEAVE_NOTIFY && canvasitem_ctx->root()->is_visible()) {
        // Leave notify means there is no current item.
        // Find closest item.
        double x = 0.0;
        double y = 0.0;

        if (q->_pick_event.type == GDK_ENTER_NOTIFY) {
            x = q->_pick_event.crossing.x;
            y = q->_pick_event.crossing.y;
        } else {
            x = q->_pick_event.motion.x;
            y = q->_pick_event.motion.y;
        }

        // Look at where the cursor is to see if one should pick with outline mode.
        bool outline = q->canvas_point_in_outline_zone({ x, y });

        // Convert to world coordinates.
        auto p = Geom::Point(x, y) + q->_pos;
        if (stores.mode() == Stores::Mode::Decoupled) {
            p *= q->_affine.inverse() * canvasitem_ctx->affine();
        }

        q->_drawing->getCanvasItemDrawing()->set_pick_outline(outline);
        q->_current_canvas_item_new = canvasitem_ctx->root()->pick_item(p);
        // if (q->_current_canvas_item_new) {
        //     std::cout << "  PICKING: FOUND ITEM: " << q->_current_canvas_item_new->get_name() << std::endl;
        // } else {
        //     std::cout << "  PICKING: DID NOT FIND ITEM" << std::endl;
        // }
    }

    if (q->_current_canvas_item_new == q->_current_canvas_item && !q->_left_grabbed_item) {
        // Current item did not change!
        return false;
    }

    // Synthesize events for old and new current items.
    bool retval = false;
    if (q->_current_canvas_item_new != q->_current_canvas_item &&
        q->_current_canvas_item != nullptr                     &&
        !q->_left_grabbed_item                                 ) {

        GdkEvent new_event;
        new_event = q->_pick_event;
        new_event.type = GDK_LEAVE_NOTIFY;
        new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
        new_event.crossing.subwindow = nullptr;
        q->_in_repick = true;
        retval = emit_event(&new_event);
        q->_in_repick = false;
    }

    if (q->_all_enter_events == false) {
        // new_current_item may have been set to nullptr during the call to emitEvent() above.
        if (q->_current_canvas_item_new != q->_current_canvas_item && button_down) {
            q->_left_grabbed_item = true;
            return retval;
        }
    }

    // Handle the rest of cases
    q->_left_grabbed_item = false;
    q->_current_canvas_item = q->_current_canvas_item_new;

    if (q->_current_canvas_item != nullptr) {
        GdkEvent new_event;
        new_event = q->_pick_event;
        new_event.type = GDK_ENTER_NOTIFY;
        new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
        new_event.crossing.subwindow = nullptr;
        retval = emit_event(&new_event);
    }

    return retval;
}

// Fires an event at the canvas, after a little pre-processing. Returns true if handled.
bool CanvasPrivate::emit_event(const GdkEvent *event)
{
    // Handle grabbed items.
    if (q->_grabbed_canvas_item) {
        auto mask = (Gdk::EventMask)0;

        switch (event->type) {
            case GDK_ENTER_NOTIFY:
                mask = Gdk::ENTER_NOTIFY_MASK;
                break;
            case GDK_LEAVE_NOTIFY:
                mask = Gdk::LEAVE_NOTIFY_MASK;
                break;
            case GDK_MOTION_NOTIFY:
                mask = Gdk::POINTER_MOTION_MASK;
                break;
            case GDK_BUTTON_PRESS:
            case GDK_2BUTTON_PRESS:
            case GDK_3BUTTON_PRESS:
                mask = Gdk::BUTTON_PRESS_MASK;
                break;
            case GDK_BUTTON_RELEASE:
                mask = Gdk::BUTTON_RELEASE_MASK;
                break;
            case GDK_KEY_PRESS:
                mask = Gdk::KEY_PRESS_MASK;
                break;
            case GDK_KEY_RELEASE:
                mask = Gdk::KEY_RELEASE_MASK;
                break;
            case GDK_SCROLL:
                mask = Gdk::SCROLL_MASK;
                mask |= Gdk::SMOOTH_SCROLL_MASK;
                break;
            default:
                break;
        }

        if (!(mask & q->_grabbed_event_mask)) {
            return false;
        }
    }

    // Convert to world coordinates. We have two different cases due to different event structures.
    auto conv = [&, this] (double &x, double &y) {
        auto p = Geom::Point(x, y) + q->_pos;
        if (stores.mode() == Stores::Mode::Decoupled) {
            p *= q->_affine.inverse() * canvasitem_ctx->affine();
        }
        x = p.x();
        y = p.y();
    };

    auto event_copy = make_unique_copy(event);

    switch (event->type) {
        case GDK_ENTER_NOTIFY:
        case GDK_LEAVE_NOTIFY:
            conv(event_copy->crossing.x, event_copy->crossing.y);
            break;
        case GDK_MOTION_NOTIFY:
        case GDK_BUTTON_PRESS:
        case GDK_2BUTTON_PRESS:
        case GDK_3BUTTON_PRESS:
        case GDK_BUTTON_RELEASE:
            conv(event_copy->motion.x, event_copy->motion.y);
            break;
        default:
            break;
    }

    // Block undo/redo while anything is dragged.
    if (event->type == GDK_BUTTON_PRESS && event->button.button == 1) {
        q->_is_dragging = true;
    } else if (event->type == GDK_BUTTON_RELEASE) {
        q->_is_dragging = false;
    }

    if (q->_current_canvas_item) {
        // Choose where to send event.
        auto item = q->_current_canvas_item;

        if (q->_grabbed_canvas_item && !q->_current_canvas_item->is_descendant_of(q->_grabbed_canvas_item)) {
            item = q->_grabbed_canvas_item;
        }

        if (pre_scroll_grabbed_item && event->type == GDK_SCROLL) {
            item = pre_scroll_grabbed_item;
        }

        // Propagate the event up the canvas item hierarchy until handled.
        while (item) {
            if (item->handle_event(event_copy.get())) return true;
            item = item->get_parent();
        }
    }

    return false;
}

/*
 * Protected functions
 */

Geom::IntPoint Canvas::get_dimensions() const
{
    return dimensions(get_allocation());
}

/**
 * Is world point inside canvas area?
 */
bool Canvas::world_point_inside_canvas(Geom::Point const &world) const
{
    return get_area_world().contains(world.floor());
}

/**
 * Translate point in canvas to world coordinates.
 */
Geom::Point Canvas::canvas_to_world(Geom::Point const &point) const
{
    return point + _pos;
}

/**
 * Return the area shown in the canvas in world coordinates.
 */
Geom::IntRect Canvas::get_area_world() const
{
    return Geom::IntRect(_pos, _pos + get_dimensions());
}

/**
 * Return whether a point in screen space / canvas coordinates is inside the region
 * of the canvas where things respond to mouse clicks as if they are in outline mode.
 */
bool Canvas::canvas_point_in_outline_zone(Geom::Point const &p) const
{
    if (_render_mode == RenderMode::OUTLINE || _render_mode == RenderMode::OUTLINE_OVERLAY) {
        return true;
    } else if (_split_mode == SplitMode::SPLIT) {
        auto split_position = _split_frac * get_dimensions();
        switch (_split_direction) {
            case SplitDirection::NORTH: return p.y() > split_position.y();
            case SplitDirection::SOUTH: return p.y() < split_position.y();
            case SplitDirection::WEST:  return p.x() > split_position.x();
            case SplitDirection::EAST:  return p.x() < split_position.x();
            default: return false;
        }
    } else {
        return false;
    }
}

/**
 * Return the last known mouse position of center if off-canvas.
 */
std::optional<Geom::Point> Canvas::get_last_mouse() const
{
    return d->last_mouse;
}

const Geom::Affine &Canvas::get_geom_affine() const
{
    return d->canvasitem_ctx->affine();
}

void CanvasPrivate::queue_draw_area(const Geom::IntRect &rect)
{
    if (q->get_opengl_enabled()) {
        // Note: GTK glitches out when you use queue_draw_area in OpenGL mode.
        // It's also pointless, because it seems to just call queue_draw anyway.
        q->queue_draw();
    } else {
        q->queue_draw_area(rect.left(), rect.top(), rect.width(), rect.height());
    }
}

/**
 * Invalidate drawing and redraw during idle.
 */
void Canvas::redraw_all()
{
    if (!d->active) {
        // CanvasItems redraw their area when being deleted... which happens when the Canvas is destroyed.
        // We need to ignore their requests!
        return;
    }
    d->invalidated->do_union(geom_to_cairo(d->stores.store().rect));
    d->schedule_redraw();
    if (d->prefs.debug_show_unclean) queue_draw();
}

/**
 * Redraw the given area during idle.
 */
void Canvas::redraw_area(int x0, int y0, int x1, int y1)
{
    if (!d->active) {
        // CanvasItems redraw their area when being deleted... which happens when the Canvas is destroyed.
        // We need to ignore their requests!
        return;
    }

    // Clamp area to Cairo's technically supported max size (-2^30..+2^30-1).
    // This ensures that the rectangle dimensions don't overflow and wrap around.
    constexpr int min_coord = -(1 << 30);
    constexpr int max_coord = (1 << 30) - 1;

    x0 = std::clamp(x0, min_coord, max_coord);
    y0 = std::clamp(y0, min_coord, max_coord);
    x1 = std::clamp(x1, min_coord, max_coord);
    y1 = std::clamp(y1, min_coord, max_coord);

    if (x0 >= x1 || y0 >= y1) {
        return;
    }

    if (d->redraw_active && d->invalidated->empty()) {
        d->abort_flags.store((int)AbortFlags::Soft, std::memory_order_relaxed); // responding to partial invalidations takes priority over prerendering
        if (d->prefs.debug_logging) std::cout << "Soft exit request" << std::endl;
    }

    auto const rect = Geom::IntRect(x0, y0, x1, y1);
    d->invalidated->do_union(geom_to_cairo(rect));
    d->schedule_redraw();
    if (d->prefs.debug_show_unclean) queue_draw();
}

void Canvas::redraw_area(Geom::Coord x0, Geom::Coord y0, Geom::Coord x1, Geom::Coord y1)
{
    // Handle overflow during conversion gracefully.
    // Round outward to make sure integral coordinates cover the entire area.
    constexpr Geom::Coord min_int = std::numeric_limits<int>::min();
    constexpr Geom::Coord max_int = std::numeric_limits<int>::max();

    redraw_area(
        (int)std::floor(std::clamp(x0, min_int, max_int)),
        (int)std::floor(std::clamp(y0, min_int, max_int)),
        (int)std::ceil (std::clamp(x1, min_int, max_int)),
        (int)std::ceil (std::clamp(y1, min_int, max_int))
    );
}

void Canvas::redraw_area(Geom::Rect const &area)
{
    redraw_area(area.left(), area.top(), area.right(), area.bottom());
}

/**
 * Redraw after changing canvas item geometry.
 */
void Canvas::request_update()
{
    // Flag geometry as needing update.
    _need_update = true;

    // Trigger the redraw process to perform the update.
    d->schedule_redraw();
}

/**
 * Scroll window so drawing point 'pos' is at upper left corner of canvas.
 */
void Canvas::set_pos(Geom::IntPoint const &pos)
{
    if (pos == _pos) {
        return;
    }

    _pos = pos;

    d->schedule_redraw();
    queue_draw();
}

/**
 * Set the affine for the canvas.
 */
void Canvas::set_affine(Geom::Affine const &affine)
{
    if (_affine == affine) {
        return;
    }

    _affine = affine;

    d->schedule_redraw();
    queue_draw();
}

/**
 * Set the desk colour. Transparency is interpreted as amount of checkerboard.
 */
void Canvas::set_desk(uint32_t rgba)
{
    if (d->desk == rgba) return;
    bool invalidated = d->background_in_stores_enabled;
    d->desk = rgba;
    invalidated |= d->background_in_stores_enabled = d->background_in_stores_required();
    if (get_realized() && invalidated) redraw_all();
    queue_draw();
}

/**
 * Set the page border colour. Although we don't draw the borders, this colour affects the shadows which we do draw (in OpenGL mode).
 */
void Canvas::set_border(uint32_t rgba)
{
    if (d->border == rgba) return;
    d->border = rgba;
    if (get_realized() && get_opengl_enabled()) queue_draw();
}

/**
 * Set the page colour. Like the desk colour, transparency is interpreted as checkerboard.
 */
void Canvas::set_page(uint32_t rgba)
{
    if (d->page == rgba) return;
    bool invalidated = d->background_in_stores_enabled;
    d->page = rgba;
    invalidated |= d->background_in_stores_enabled = d->background_in_stores_required();
    if (get_realized() && invalidated) redraw_all();
    queue_draw();
}

uint32_t Canvas::get_effective_background() const
{
    auto arr = checkerboard_darken(rgb_to_array(d->desk), 1.0f - 0.5f * SP_RGBA32_A_U(d->desk) / 255.0f);
    return SP_RGBA32_F_COMPOSE(arr[0], arr[1], arr[2], 1.0);
}

void Canvas::set_render_mode(Inkscape::RenderMode mode)
{
    if ((_render_mode == RenderMode::OUTLINE_OVERLAY) != (mode == RenderMode::OUTLINE_OVERLAY) && !get_opengl_enabled()) {
        queue_draw();
    }
    _render_mode = mode;
    if (_drawing) {
        _drawing->setRenderMode(_render_mode == RenderMode::OUTLINE_OVERLAY ? RenderMode::NORMAL : _render_mode);
        _drawing->setOutlineOverlay(d->outlines_required());
    }
    if (_desktop) {
        _desktop->setWindowTitle(); // Mode is listed in title.
    }
}

void Canvas::set_color_mode(Inkscape::ColorMode mode)
{
    _color_mode = mode;
    if (_drawing) {
        _drawing->setColorMode(_color_mode);
    }
    if (_desktop) {
        _desktop->setWindowTitle(); // Mode is listed in title.
    }
}

void Canvas::set_split_mode(Inkscape::SplitMode mode)
{
    if (_split_mode != mode) {
        _split_mode = mode;
        if (_split_mode == Inkscape::SplitMode::SPLIT) {
            _hover_direction = Inkscape::SplitDirection::NONE;
        }
        if (_drawing) {
            _drawing->setOutlineOverlay(d->outlines_required());
        }
        redraw_all();
    }
}

void Canvas::set_clip_to_page_mode(bool clip)
{
    if (clip != d->clip_to_page) {
        d->clip_to_page = clip;
        d->schedule_redraw();
    }
}

void Canvas::set_cms_key(std::string key)
{
    _cms_key = std::move(key);
    _cms_active = !_cms_key.empty();
    redraw_all();
}

/**
 * Clear current and grabbed items.
 */
void Canvas::canvas_item_destructed(Inkscape::CanvasItem *item)
{
    if (!d->active) {
        return;
    }

    if (item == _current_canvas_item) {
        _current_canvas_item = nullptr;
    }

    if (item == _current_canvas_item_new) {
        _current_canvas_item_new = nullptr;
    }

    if (item == _grabbed_canvas_item) {
        _grabbed_canvas_item = nullptr;
        auto const display = Gdk::Display::get_default();
        auto const seat    = display->get_default_seat();
        seat->ungrab();
    }

    if (item == d->pre_scroll_grabbed_item) {
        d->pre_scroll_grabbed_item = nullptr;
    }
}

std::optional<Geom::PathVector> CanvasPrivate::calc_page_clip() const
{
    if (!clip_to_page) {
        return {};
    }

    Geom::PathVector pv;
    for (auto &rect : pi.pages) {
        pv.push_back(Geom::Path(rect));
    }
    return pv;
}

// Change cursor
void Canvas::set_cursor()
{
    if (!_desktop) {
        return;
    }

    auto display = Gdk::Display::get_default();

    switch (_hover_direction) {
        case Inkscape::SplitDirection::NONE:
            _desktop->event_context->use_tool_cursor();
            break;

        case Inkscape::SplitDirection::NORTH:
        case Inkscape::SplitDirection::EAST:
        case Inkscape::SplitDirection::SOUTH:
        case Inkscape::SplitDirection::WEST:
        {
            auto cursor = Gdk::Cursor::create(display, "pointer");
            get_window()->set_cursor(cursor);
            break;
        }

        case Inkscape::SplitDirection::HORIZONTAL:
        {
            auto cursor = Gdk::Cursor::create(display, "ns-resize");
            get_window()->set_cursor(cursor);
            break;
        }

        case Inkscape::SplitDirection::VERTICAL:
        {
            auto cursor = Gdk::Cursor::create(display, "ew-resize");
            get_window()->set_cursor(cursor);
            break;
        }

        default:
            // Shouldn't reach.
            std::cerr << "Canvas::set_cursor: Unknown hover direction!" << std::endl;
    }
}

void Canvas::get_preferred_width_vfunc(int &minimum_width, int &natural_width) const
{
    minimum_width = natural_width = 256;
}

void Canvas::get_preferred_height_vfunc(int &minimum_height, int &natural_height) const
{
    minimum_height = natural_height = 256;
}

void Canvas::on_size_allocate(Gtk::Allocation &allocation)
{
    auto const old_dimensions = get_dimensions();
    parent_type::on_size_allocate(allocation);
    auto const new_dimensions = get_dimensions();

    // Necessary as GTK seems to somehow invalidate the current pipeline state upon resize.
    if (d->active) {
        d->graphics->invalidated_glstate();
    }

    // Trigger the size update to be applied to the stores before the next redraw of the window.
    d->schedule_redraw();

    // Keep canvas centered and optionally zoomed in.
    if (_desktop && new_dimensions != old_dimensions) {
        auto const midpoint = _desktop->w2d(_pos + Geom::Point(old_dimensions) * 0.5);
        double zoom = _desktop->current_zoom();

        auto prefs = Preferences::get();
        if (prefs->getBool("/options/stickyzoom/value", false)) {
            // Calculate adjusted zoom.
            auto const old_minextent = min(old_dimensions);
            auto const new_minextent = min(new_dimensions);
            if (old_minextent != 0) {
                zoom *= (double)new_minextent / old_minextent;
            }
        }

        _desktop->zoom_absolute(midpoint, zoom, false);
    }
}

Glib::RefPtr<Gdk::GLContext> Canvas::create_context()
{
    Glib::RefPtr<Gdk::GLContext> result;

    try {
        result = get_window()->create_gl_context();
    } catch (const Gdk::GLError &e) {
        std::cerr << "Failed to create OpenGL context: " << e.what().raw() << std::endl;
        return {};
    }

    try {
        result->realize();
    } catch (const Glib::Error &e) {
        std::cerr << "Failed to realize OpenGL context: " << e.what().raw() << std::endl;
        return {};
    }

    return result;
}

void Canvas::paint_widget(Cairo::RefPtr<Cairo::Context> const &cr)
{
    framecheck_whole_function(d)

    if (!d->active) {
        std::cerr << "Canvas::paint_widget: Called while not active!" << std::endl;
        return;
    }

    if constexpr (false) d->canvasitem_ctx->root()->canvas_item_print_tree();

    // Although launch_redraw() is scheduled at a priority higher than draw, and should therefore always be called first if
    // asked, there are times when GTK simply decides to call on_draw anyway. Since launch_redraw() is required to have been
    // called at least once to perform vital initalisation, if it has not been called, we have to exit.
    if (d->stores.mode() == Stores::Mode::None) {
        return;
    }

    // Commit pending tiles in case GTK called on_draw even though after_redraw() is scheduled at higher priority.
    if (!d->redraw_active) {
        d->commit_tiles();
    }

    if (get_opengl_enabled()) {
        bind_framebuffer();
    }

    Graphics::PaintArgs args;
    args.mouse = d->last_mouse;
    args.render_mode = _render_mode;
    args.splitmode = _split_mode;
    args.splitfrac = _split_frac;
    args.splitdir = _split_direction;
    args.hoverdir = _hover_direction;
    args.yaxisdir = _desktop ? _desktop->yaxisdir() : 1.0;

    d->graphics->paint_widget(Fragment{ _affine, get_area_world() }, args, cr);

    // If asked, run an animation loop.
    if (d->prefs.debug_animate) {
        auto t = g_get_monotonic_time() / 1700000.0;
        auto affine = Geom::Rotate(t * 5) * Geom::Scale(1.0 + 0.6 * cos(t * 2));
        set_affine(affine);
        auto dim = _desktop && _desktop->doc() ? _desktop->doc()->getDimensions() : Geom::Point();
        set_pos(Geom::Point((0.5 + 0.3 * cos(t * 2)) * dim.x(), (0.5 + 0.3 * sin(t * 3)) * dim.y()) * affine - Geom::Point(get_dimensions()) * 0.5);
    }
}

/*
 * Async redrawing process
 */

// Replace a region with a larger region consisting of fewer, larger rectangles. (Allowed to slightly overlap.)
auto coarsen(const Cairo::RefPtr<Cairo::Region> &region, int min_size, int glue_size, double min_fullness)
{
    // Sort the rects by minExtent.
    struct Compare
    {
        bool operator()(const Geom::IntRect &a, const Geom::IntRect &b) const {
            return a.minExtent() < b.minExtent();
        }
    };
    std::multiset<Geom::IntRect, Compare> rects;
    int nrects = region->get_num_rectangles();
    for (int i = 0; i < nrects; i++) {
        rects.emplace(cairo_to_geom(region->get_rectangle(i)));
    }

    // List of processed rectangles.
    std::vector<Geom::IntRect> processed;
    processed.reserve(nrects);

    // Removal lists.
    std::vector<decltype(rects)::iterator> remove_rects;
    std::vector<int> remove_processed;

    // Repeatedly expand small rectangles by absorbing their nearby small rectangles.
    while (!rects.empty() && rects.begin()->minExtent() < min_size) {
        // Extract the smallest unprocessed rectangle.
        auto rect = *rects.begin();
        rects.erase(rects.begin());

        // Initialise the effective glue size.
        int effective_glue_size = glue_size;

        while (true) {
            // Find the glue zone.
            auto glue_zone = rect;
            glue_zone.expandBy(effective_glue_size);

            // Absorb rectangles in the glue zone. We could do better algorithmically speaking, but in real life it's already plenty fast.
            auto newrect = rect;
            int absorbed_area = 0;

            remove_rects.clear();
            for (auto it = rects.begin(); it != rects.end(); ++it) {
                if (glue_zone.contains(*it)) {
                    newrect.unionWith(*it);
                    absorbed_area += it->area();
                    remove_rects.emplace_back(it);
                }
            }

            remove_processed.clear();
            for (int i = 0; i < processed.size(); i++) {
                auto &r = processed[i];
                if (glue_zone.contains(r)) {
                    newrect.unionWith(r);
                    absorbed_area += r.area();
                    remove_processed.emplace_back(i);
                }
            }

            // If the result was too empty, try again with a smaller glue size.
            double fullness = (double)(rect.area() + absorbed_area) / newrect.area();
            if (fullness < min_fullness) {
                effective_glue_size /= 2;
                continue;
            }

            // Commit the change.
            rect = newrect;

            for (auto &it : remove_rects) {
                rects.erase(it);
            }

            for (int j = (int)remove_processed.size() - 1; j >= 0; j--) {
                int i = remove_processed[j];
                processed[i] = processed.back();
                processed.pop_back();
            }

            // Stop growing if not changed or now big enough.
            bool finished = absorbed_area == 0 || rect.minExtent() >= min_size;
            if (finished) {
                break;
            }

            // Otherwise, continue normally.
            effective_glue_size = glue_size;
        }

        // Put the finished rectangle in processed.
        processed.emplace_back(rect);
    }

    // Put any remaining rectangles in processed.
    for (auto &rect : rects) {
        processed.emplace_back(rect);
    }

    return processed;
}

static std::optional<Geom::Dim2> bisect(Geom::IntRect const &rect, int tile_size)
{
    int bw = rect.width();
    int bh = rect.height();

    // Chop in half along the bigger dimension if the bigger dimension is too big.
    if (bw > bh) {
        if (bw > tile_size) {
            return Geom::X;
        }
    } else {
        if (bh > tile_size) {
            return Geom::Y;
        }
    }

    return {};
}

void CanvasPrivate::init_tiler()
{
    // Begin processing redraws.
    rd.start_time = g_get_monotonic_time();
    rd.phase = 0;
    rd.vis_store = (rd.visible & rd.store.rect).regularized();

    if (!init_redraw()) {
        sync.signalExit();
        return;
    }

    // Launch render threads to process tiles.
    rd.timeoutflag = false;

    rd.numactive = rd.numthreads;

    for (int i = 0; i < rd.numthreads - 1; i++) {
        boost::asio::post(*pool, [=] { render_tile(i); });
    }

    render_tile(rd.numthreads - 1);
}

bool CanvasPrivate::init_redraw()
{
    assert(rd.rects.empty());

    switch (rd.phase) {
        case 0:
            if (rd.vis_store && rd.decoupled_mode) {
                // The highest priority to redraw is the region that is visible but not covered by either clean or snapshot content, if in decoupled mode.
                // If this is not rendered immediately, it will be perceived as edge flicker, most noticeably on zooming out, but also on rotation too.
                process_redraw(*rd.vis_store, unioned(updater->clean_region->copy(), rd.snapshot_drawn));
                return true;
            } else {
                rd.phase++;
                // fallthrough
            }

        case 1:
            // Another high priority to redraw is the grabbed canvas item, if the user has requested block updates.
            if (rd.grabbed) {
                process_redraw(*rd.grabbed, updater->clean_region, false, false); // non-interruptible, non-preemptible
                return true;
            } else {
                rd.phase++;
                // fallthrough
            }

        case 2:
            if (rd.vis_store) {
                // The main priority to redraw, and the bread and butter of Inkscape's painting, is the visible content that is not clean.
                // This may be done over several cycles, at the direction of the Updater, each outwards from the mouse.
                process_redraw(*rd.vis_store, updater->get_next_clean_region());
                return true;
            } else {
                rd.phase++;
                // fallthrough
            }

        case 3: {
            // The lowest priority to redraw is the prerender margin around the visible rectangle.
            // (This is in addition to any opportunistic prerendering that may have already occurred in the above steps.)
            auto prerender = expandedBy(rd.visible, rd.margin);
            auto prerender_store = (prerender & rd.store.rect).regularized();
            if (prerender_store) {
                process_redraw(*prerender_store, updater->clean_region);
                return true;
            } else {
                return false;
            }
        }

        default:
            assert(false);
            return false;
    }
}

// Paint a given subrectangle of the store given by 'bounds', but avoid painting the part of it within 'clean' if possible.
// Some parts both outside the bounds and inside the clean region may also be painted if it helps reduce fragmentation.
void CanvasPrivate::process_redraw(Geom::IntRect const &bounds, Cairo::RefPtr<Cairo::Region> clean, bool interruptible, bool preemptible)
{
    rd.bounds = bounds;
    rd.clean = std::move(clean);
    rd.interruptible = interruptible;
    rd.preemptible = preemptible;

    // Assert that we do not render outside of store.
    assert(rd.store.rect.contains(rd.bounds));

    // Get the region we are asked to paint.
    auto region = Cairo::Region::create(geom_to_cairo(rd.bounds));
    region->subtract(rd.clean);

    // Get the list of rectangles to paint, coarsened to avoid fragmentation.
    rd.rects = coarsen(region,
                       std::min<int>(rd.coarsener_min_size, rd.tile_size / 2),
                       std::min<int>(rd.coarsener_glue_size, rd.tile_size / 2),
                       rd.coarsener_min_fullness);

    // Put the rectangles into a heap sorted by distance from mouse.
    std::make_heap(rd.rects.begin(), rd.rects.end(), rd.getcmp());

    // Adjust the effective tile size proportional to the painting area.
    double adjust = (double)cairo_to_geom(region->get_extents()).maxExtent() / rd.visible.maxExtent();
    adjust = std::clamp(adjust, 0.3, 1.0);
    rd.effective_tile_size = rd.tile_size * adjust;
}

// Process rectangles until none left or timed out.
void CanvasPrivate::render_tile(int debug_id)
{
    rd.mutex.lock();

    std::string fc_str;
    FrameCheck::Event fc;
    if (rd.debug_framecheck) {
        fc_str = "render_thread_" + std::to_string(debug_id + 1);
        fc = FrameCheck::Event(fc_str.c_str());
    }

    while (true) {
        // If we've run out of rects, try to start a new redraw cycle.
        if (rd.rects.empty()) {
            if (end_redraw()) {
                // More redraw cycles to do.
                continue;
            } else {
                // All finished.
                break;
            }
        }

        // Check for cancellation.
        auto const flags = abort_flags.load(std::memory_order_relaxed);
        bool const soft = flags & (int)AbortFlags::Soft;
        bool const hard = flags & (int)AbortFlags::Hard;
        if (hard || (rd.phase == 3 && soft)) {
            break;
        }

        // Extract the closest rectangle to the mouse.
        std::pop_heap(rd.rects.begin(), rd.rects.end(), rd.getcmp());
        auto rect = rd.rects.back();
        rd.rects.pop_back();

        // Cull empty rectangles.
        if (rect.hasZeroArea()) {
            continue;
        }

        // Cull rectangles that lie entirely inside the clean region.
        // (These can be generated by coarsening; they must be discarded to avoid getting stuck re-rendering the same rectangles.)
        if (rd.clean->contains_rectangle(geom_to_cairo(rect)) == Cairo::REGION_OVERLAP_IN) {
            continue;
        }

        // Lambda to add a rectangle to the heap.
        auto add_rect = [&] (Geom::IntRect const &rect) {
            rd.rects.emplace_back(rect);
            std::push_heap(rd.rects.begin(), rd.rects.end(), rd.getcmp());
        };

        // If the rectangle needs bisecting, bisect it and put it back on the heap.
        if (auto axis = bisect(rect, rd.effective_tile_size)) {
            int mid = rect[*axis].middle();
            auto lo = rect; lo[*axis].setMax(mid); add_rect(lo);
            auto hi = rect; hi[*axis].setMin(mid); add_rect(hi);
            continue;
        }

        // Extend thin rectangles at the edge of the bounds rect to at least some minimum size, being sure to keep them within the store.
        // (This ensures we don't end up rendering one thin rectangle at the edge every frame while the view is moved continuously.)
        if (rd.preemptible) {
            if (rect.width() < rd.preempt) {
                if (rect.left()  == rd.bounds.left() ) rect.setLeft (std::max(rect.right() - rd.preempt, rd.store.rect.left() ));
                if (rect.right() == rd.bounds.right()) rect.setRight(std::min(rect.left()  + rd.preempt, rd.store.rect.right()));
            }
            if (rect.height() < rd.preempt) {
                if (rect.top()    == rd.bounds.top()   ) rect.setTop   (std::max(rect.bottom() - rd.preempt, rd.store.rect.top()   ));
                if (rect.bottom() == rd.bounds.bottom()) rect.setBottom(std::min(rect.top()    + rd.preempt, rd.store.rect.bottom()));
            }
        }

        // Mark the rectangle as clean.
        updater->mark_clean(rect);

        rd.mutex.unlock();

        // Paint the rectangle.
        paint_rect(rect);

        rd.mutex.lock();

        // Check for timeout.
        if (rd.interruptible) {
            auto now = g_get_monotonic_time();
            auto elapsed = now - rd.start_time;
            if (elapsed > rd.render_time_limit * 1000) {
                // Timed out. Temporarily return to GTK main loop, and come back here when next idle.
                rd.timeoutflag = true;
                break;
            }
        }
    }

    if (rd.debug_framecheck && rd.timeoutflag) {
        fc.subtype = 1;
    }

    rd.numactive--;
    bool const done = rd.numactive == 0;

    rd.mutex.unlock();

    if (done) {
        rd.rects.clear();
        sync.signalExit();
    }
}

bool CanvasPrivate::end_redraw()
{
    switch (rd.phase) {
        case 0:
            rd.phase++;
            return init_redraw();

        case 1:
            rd.phase++;
            // Reset timeout to leave the normal amount of time for clearing up artifacts.
            rd.start_time = g_get_monotonic_time();
            return init_redraw();

        case 2:
            if (!updater->report_finished()) {
                rd.phase++;
            }
            return init_redraw();

        case 3:
            return false;

        default:
            assert(false);
            return false;
    }
}

void CanvasPrivate::paint_rect(Geom::IntRect const &rect)
{
    // Make sure the paint rectangle lies within the store.
    assert(rd.store.rect.contains(rect));

    auto paint = [&, this] (bool need_background, bool outline_pass) {

        auto surface = graphics->request_tile_surface(rect, true);
        if (!surface) {
            sync.runInMain([&] {
                if (prefs.debug_logging) std::cout << "Blocked - buffer mapping" << std::endl;
                if (q->get_opengl_enabled()) q->make_current();
                surface = graphics->request_tile_surface(rect, false);
            });
        }

        try {

            paint_single_buffer(surface, rect, need_background, outline_pass);

        } catch (std::bad_alloc const &) {
            // Note: std::bad_alloc actually indicates a Cairo error that occurs regularly at high zoom, and we must handle it.
            // See https://gitlab.com/inkscape/inkscape/-/issues/3975
            sync.runInMain([&] {
                std::cerr << "Rendering failure. You probably need to zoom out!" << std::endl;
                if (q->get_opengl_enabled()) q->make_current();
                graphics->junk_tile_surface(std::move(surface));
                surface = graphics->request_tile_surface(rect, false);
                paint_error_buffer(surface);
            });
        }

        return surface;
    };

    // Create and render the tile.
    Tile tile;
    tile.fragment.affine = rd.store.affine;
    tile.fragment.rect = rect;
    tile.surface = paint(background_in_stores_required(), false);
    if (outlines_enabled) {
        tile.outline_surface = paint(false, true);
    }

    // Introduce an artificial delay for each rectangle.
    if (rd.redraw_delay) g_usleep(*rd.redraw_delay);

    // Stick the tile on the list of tiles to reap.
    {
        auto g = std::lock_guard(rd.tiles_mutex);
        rd.tiles.emplace_back(std::move(tile));
    }
}

void CanvasPrivate::paint_single_buffer(Cairo::RefPtr<Cairo::ImageSurface> const &surface, Geom::IntRect const &rect, bool need_background, bool outline_pass)
{
    // Create Cairo context.
    auto cr = Cairo::Context::create(surface);

    // Clear background.
    cr->save();
    if (need_background) {
        Graphics::paint_background(Fragment{ rd.store.affine, rect }, pi, rd.page, rd.desk, cr);
    } else {
        cr->set_operator(Cairo::OPERATOR_CLEAR);
        cr->paint();
    }
    cr->restore();

    // Render drawing on top of background.
    auto buf = Inkscape::CanvasItemBuffer{ rect, scale_factor, cr, outline_pass };
    canvasitem_ctx->root()->render(buf);

    // Paint over newly drawn content with a translucent random colour.
    if (rd.debug_show_redraw) {
        cr->set_source_rgba((rand() % 256) / 255.0, (rand() % 256) / 255.0, (rand() % 256) / 255.0, 0.2);
        cr->set_operator(Cairo::OPERATOR_OVER);
        cr->paint();
    }
}

void CanvasPrivate::paint_error_buffer(Cairo::RefPtr<Cairo::ImageSurface> const &surface)
{
    // Paint something into surface to represent an "error" state for that tile.
    // Currently just paints solid black.
    auto cr = Cairo::Context::create(surface);
    cr->set_source_rgb(0, 0, 0);
    cr->paint();
}

} // namespace Inkscape::UI::Widget

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :