mln-rs-runtime 0.0.17

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
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
//! The handle of a render session, and the state that its thread shares
//! with the handle.

use std::any::Any;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, OnceLock, Weak};
use std::thread::ThreadId;
use std::time::Duration;

use mln_frame_pacer::{FramePacer, RefreshWaiter};
use mln_map::LogicalExtent;
use mln_renderer::query::{RenderedQueryOptions, SourceQueryOptions};
use mln_util::error::Error;
use mln_util::feature::Feature;
use mln_util::image::PremultipliedImage;
use mln_util::size::Size;
use mln_util::thread::Joined;
use parking_lot::Mutex;

use super::driver::DriverWork;
use super::frame::{FrameDemand, FrameResult};
use super::query::RenderedQueryGeometry;
use super::target::{
    AttachOptions, BorrowedRing, ConsumerSync, Device, DeviceLease, Driver, ExportKind, FrameTexture, GpuSync,
    RenderTarget, SentSurface, Surface, SyncKind, SyncKinds, Wake,
};
use super::worker::{self, Init, SessionQueue, TargetInit, Worker};
use crate::completion::{Completion, Home, Resolver};
use crate::log_target;
use crate::map::{Applied, Map};
use crate::runtime::Outbox;

/// The ID of a render session, unique in the process.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct SessionId(u64);

/// Where a render session is in its life.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RenderSessionState {
    /// The session is starting: it builds its renderer and binds to its map.
    Attaching,
    Attached,
    /// A detach or release is on its way.
    Detaching,
    /// The session let go of its map and its graphics objects.
    Detached,
    /// The session couldn't build its renderer, or failed while it rendered,
    /// as when a panic in its work stopped it. Its snapshot says why.
    Failed,
    Abandoned,
}

/// The state of a render session at one moment.
#[derive(Clone, Debug, PartialEq)]
pub struct RenderSessionSnapshot {
    pub state: RenderSessionState,
    /// The target's logical size, and the map's pixel ratio.
    pub extent: LogicalExtent,
    /// Grows each time the target changes size or is replaced.
    pub generation: u64,
    /// Counts the frames that the session rendered.
    pub frame_generation: u64,
    /// Why the session failed.
    pub failure: Option<Error>,
    /// Whether the session is attached and has a target to render to. It is
    /// false from a [`release_target`](RenderSession::release_target) until
    /// a [`set_surface`](RenderSession::set_surface) gives it a new one.
    pub has_target: bool,
    /// How many of a borrowed ring's frames the host holds.
    pub acquired_frames: u32,
}

/// What a render session can do.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RenderSessionCapabilities {
    pub driver: Driver,
    /// How many textures the session's texture ring has.
    pub texture_ring_depth: u32,
    /// Whether the host can take the frames it renders.
    pub frame_acquisition: bool,
    /// Whether [`RenderSession::read_texture`] can read its pixels.
    pub readback: bool,
    /// Whether it shows frames on a surface.
    pub presentation: bool,
    /// The kind of sync that a borrowed ring's frames carry.
    pub producer_sync_kind: Option<SyncKind>,
    /// The kinds of sync that [`AcquiredFrame::release`] takes.
    pub consumer_sync_kinds: SyncKinds,
    /// How an exported ring shares its images, for an exported ring.
    pub export_kind: Option<ExportKind>,
}

/// A frame of a borrowed ring that the session published, and what the
/// host waits for before it reads it.
#[derive(Clone, Debug)]
pub(crate) struct Published {
    pub(crate) result: FrameResult,
    pub(crate) sync: GpuSync,
    pub(crate) texture: Option<FrameTexture>,
}

/// One texture of a borrowed ring.
#[derive(Clone, Debug, Default)]
pub(crate) struct RingSlot {
    /// The newest frame rendered into the texture, which the host hasn't
    /// acquired.
    pub(crate) frame: Option<Published>,
    pub(crate) acquired: bool,
    pub(crate) rendering: bool,
    /// The host's GPU may still read the texture, so the session renders
    /// into it no more, even when a new ring has it again, until a release
    /// of the ring.
    pub(crate) quarantined: bool,
    /// The host holds an exported ring's image from before a resize, which
    /// gave the slot a new one that never reached the host: giving the old
    /// one up quarantines nothing.
    pub(crate) retired: bool,
}

impl RingSlot {
    pub(crate) fn busy(&self) -> bool {
        self.acquired || self.rendering || self.quarantined
    }
}

/// Work sent to a session's thread, whose completion an abandon can fail at
/// once instead of waiting for the thread to run it.
pub(crate) trait PendingWork: Any + Send {
    fn fail(self: Box<Self>, error: Error);
}

impl<T: Send + 'static> PendingWork for Resolver<T> {
    fn fail(self: Box<Self>, error: Error) {
        (*self).resolve(Err(error));
    }
}

/// The ID of a piece of work that waits for a session's thread.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct WorkId(u64);

/// The state that a session's handle and its thread share.
pub(crate) struct SessionState {
    pub(crate) lifecycle: RenderSessionState,
    pub(crate) released: bool,
    pub(crate) size: Size,
    /// The scale factor that the target has, as `size` does.
    pub(crate) scale_factor: f64,
    /// The scale factor of the last resize sent, which a later resize that
    /// names none keeps.
    pub(crate) sent_scale_factor: f64,
    pub(crate) generation: u64,
    pub(crate) frame_generation: u64,
    pub(crate) failure: Option<Error>,
    /// Whether the session has a target to render to: false from a release
    /// of its target until it gets a new one.
    pub(crate) has_target: bool,
    /// The frame results that wait for a drain.
    pub(crate) results: Vec<FrameResult>,
    /// Whether the frame wake ran since the last drain.
    pub(crate) frame_woken: bool,
    /// Whether the redraw wake may run: from the last demand until it runs.
    /// Once it ran, the map has something new to draw that no demand has
    /// answered.
    pub(crate) redraw_armed: bool,
    /// The frame pacer that takes the redraw wake's place, while one paces
    /// the session.
    pacing: Option<Pacing>,
    /// The attach's completion, until the map binds the session.
    pub(crate) attach: Option<Resolver<()>>,
    /// The completions of the detaches on their way.
    pub(crate) detach_waiters: Vec<Resolver<()>>,
    /// The completions of the releases on their way.
    pub(crate) release_waiters: Vec<Resolver<()>>,
    /// The completions of the work that waits for the session's thread.
    pending: BTreeMap<WorkId, Box<dyn PendingWork>>,
    next_work: u64,
    /// The session's map, until the session lets go of it.
    pub(crate) map: Option<Map>,
    /// What the session's thread builds the renderer from, until it does:
    /// an [`Init`]. It is boxed as `Any` so that the state names no
    /// graphics types, whose auto traits are deep to prove for every type
    /// that holds a map handle.
    pub(crate) init: Option<Box<dyn Any + Send>>,
    /// Counts the resizes sent, which run on the map's thread before they
    /// reach the session's.
    pub(crate) resizes_sent: u64,
    /// How many graphics objects the session's thread holds.
    pub(crate) graphics_objects: u32,
    /// Whether the session's thread keeps its graphics objects until the
    /// process exits, instead of freeing them, when the session ends.
    pub(crate) keep_graphics: bool,
    /// Whether an abandon keeps the graphics objects on any thread, as
    /// `Device::abandon_keeps_graphics` says of the session's device.
    pub(crate) abandon_keeps_graphics: bool,
    /// The slots of a borrowed ring, and none for another target.
    pub(crate) ring: Vec<RingSlot>,
    /// How many of the ring's frames the host holds.
    pub(crate) acquired_frames: u32,
    /// The ring replacements and releases on their way: no frame is
    /// acquired until they ran, since the frames published before one name
    /// textures it replaces or lets go of.
    pub(crate) pending_retargets: u32,
    /// How many views are open on the session's acquired frames.
    pub(crate) views: u32,
    /// Whether the session's thread keeps graphics objects that the open
    /// views name, which it lets go of once the last view ends.
    pub(crate) viewed_teardown: bool,
}

impl SessionState {
    /// Keeps `resolver` until the session's thread takes it.
    fn register<T: Send + 'static>(&mut self, resolver: Resolver<T>) -> WorkId {
        self.next_work += 1;
        let id = WorkId(self.next_work);
        self.pending.insert(id, Box::new(resolver));
        id
    }

    /// What an end of the session completes, once the state is unlocked.
    pub(crate) fn take_waiters(&mut self) -> Waiters {
        Waiters {
            attach: self.attach.take(),
            pending: std::mem::take(&mut self.pending).into_values().collect(),
            detaches: std::mem::take(&mut self.detach_waiters),
            releases: std::mem::take(&mut self.release_waiters),
        }
    }
}

/// The completions that wait for a session when it ends.
pub(crate) struct Waiters {
    attach: Option<Resolver<()>>,
    pending: Vec<Box<dyn PendingWork>>,
    detaches: Vec<Resolver<()>>,
    releases: Vec<Resolver<()>>,
}

impl Waiters {
    /// Completes the releases, and the detaches with `detached`. The attach
    /// and the work that never ran fail with `lost`.
    pub(crate) fn settle(self, detached: &Result<(), Error>, lost: impl Fn() -> Error) {
        if let Some(attach) = self.attach {
            attach.resolve(Err(lost()));
        }
        for work in self.pending {
            work.fail(lost());
        }
        for waiter in self.detaches {
            waiter.resolve(detached.clone());
        }
        for waiter in self.releases {
            waiter.resolve(Ok(()));
        }
    }
}

/// Who abandons a session.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Abandoner {
    /// The host, through the handle.
    Host,
    /// The library, for a session that the host released, or stopped
    /// servicing.
    Library,
}

/// A session's frame pacer, and what waits for its refreshes.
struct Pacing {
    pacer: Arc<FramePacer>,
    /// The session, as the pacer calls it back. Each pacing has its own, so
    /// a refresh that an earlier pacing asked for demands nothing.
    waiter: Arc<dyn RefreshWaiter>,
}

impl Pacing {
    fn new(pacer: Arc<FramePacer>, session: Weak<SessionShared>) -> Self {
        Self { pacer, waiter: Arc::new(PacedSession(session)) }
    }

    /// What asks the pacer for a refresh, in place of the redraw wake.
    fn request(&self) -> Wake {
        let (pacer, waiter) = (Arc::clone(&self.pacer), Arc::clone(&self.waiter));
        Arc::new(move || pacer.request(&waiter))
    }
}

/// A paced session, as its pacer holds it while it waits for a refresh.
struct PacedSession(Weak<SessionShared>);

impl RefreshWaiter for PacedSession {
    fn refresh(&self, _time: Duration) {
        if let Some(shared) = self.0.upgrade() {
            shared.demand_paced(self);
        }
    }
}

pub(crate) struct SessionShared {
    pub(crate) id: SessionId,
    pub(crate) driver: Driver,
    /// The work of the thread that runs the session.
    pub(crate) queue: SessionQueue,
    /// The thread that runs the session's work, once it is known: a core
    /// worker's from the attach, and a caller thread's from when the host
    /// takes the driver work.
    pub(crate) thread: Arc<OnceLock<ThreadId>>,
    /// The thread of the map's runtime, which runs the map's side of the
    /// session's work.
    runtime_thread: ThreadId,
    pub(crate) state: Mutex<SessionState>,
    /// Held while the session's thread runs a task, so an abandon waits for
    /// the graphics work in progress.
    pub(crate) gate: Mutex<()>,
    /// The session's lease on the host's device, which an abandon revokes,
    /// since graphics objects that it keeps hold the device.
    lease: DeviceLease,
    /// Held while [`RenderSession::send_then`] sends a command and attaches
    /// its callback, and while an attach hands its completion out, which the
    /// session's thread waits for before it delivers results.
    sending: Mutex<()>,
    /// Whether the session's thread is inside a driver call: a core worker
    /// running a task and delivering what it sent the host, or a service of
    /// a caller-driven session.
    pub(crate) in_call: AtomicBool,
    pub(crate) frame_wake: Option<Wake>,
    pub(crate) redraw_wake: Option<Wake>,
    capabilities: RenderSessionCapabilities,
    driver_work_taken: AtomicBool,
    /// Whether the driver-work wake may run: from the start of the last
    /// service until it runs.
    pub(crate) driver_armed: Arc<AtomicBool>,
    /// The first of the map's commands that the session's work waits for,
    /// or `u64::MAX` for none: the map tells the session's thread when it
    /// ran a command only from that one on, so a command that no work waits
    /// for wakes no caller's driver.
    pub(crate) awaited_command: Arc<AtomicU64>,
    /// In the browser, whether a caller-driven session waits for the device
    /// that it asked the browser's WebGPU for: its services build nothing
    /// until the device is there, and the driver-work wake runs then.
    pub(crate) preparing: AtomicBool,
}

impl SessionShared {
    /// A completion that the session's thread resolves.
    fn completion<T: Send + 'static>(&self) -> (Completion<T>, Resolver<T>) {
        Completion::at(Home::Later(Arc::clone(&self.thread), self.runtime_thread))
    }

    /// The completion of the work `id`, unless an end of the session
    /// completed it first.
    pub(crate) fn take_work<T: Send + 'static>(&self, id: WorkId) -> Option<Resolver<T>> {
        let work: Box<dyn Any + Send> = self.state.lock().pending.remove(&id)?;
        work.downcast::<Resolver<T>>().ok().map(|resolver| *resolver)
    }

    pub(crate) fn is_released(&self) -> bool {
        self.state.lock().released
    }

    /// The redraw wake that tells the host the map has something new to
    /// draw, unless the host was told since its last demand, or the session
    /// has no target. The host isn't told again until its next demand.
    pub(crate) fn take_redraw_wake(&self) -> Option<Wake> {
        let mut state = self.state.lock();
        let wake = state.redraw_armed && state.has_target && state.lifecycle == RenderSessionState::Attached;
        state.redraw_armed &= !wake;
        if !wake {
            return None;
        }
        match &state.pacing {
            Some(pacing) => Some(pacing.request()),
            None => self.redraw_wake.clone(),
        }
    }

    /// Sends `demand` to the session's thread. The state is locked.
    fn demand_locked(&self, state: &mut SessionState, demand: FrameDemand) -> Result<(), Error> {
        check_usable(state)?;
        if !state.ring.is_empty() && state.ring.iter().all(|slot| slot.quarantined) {
            return Err(Error::invalid_state(
                "every texture of the ring is quarantined: give the session new textures",
            ));
        }
        let (sequence, sample) = state.map.as_ref().map_or((0, 0), |map| (map.sent_sequence(), map.frame_demanded()));
        state.redraw_armed = true;
        worker::post(&self.queue, move |worker, outbox| worker.demand(demand, sequence, sample, outbox));
        Ok(())
    }

    /// Demands the frame that the map has to draw at `waiter`'s refresh,
    /// unless a demand came first or `waiter`'s pacing ended.
    fn demand_paced(&self, waiter: &PacedSession) {
        with_unlocked_wakes(|| {
            let mut state = self.state.lock();
            let current = state
                .pacing
                .as_ref()
                .is_some_and(|pacing| core::ptr::addr_eq(Arc::as_ptr(&pacing.waiter), core::ptr::from_ref(waiter)));
            if current && !state.redraw_armed {
                // A session that failed or ended since it asked takes no demand.
                let _ = self.demand_locked(&mut state, FrameDemand::new(super::DemandMode::IfChanged, 0));
            }
        });
    }

    /// Starts a detach on the session's thread, after the work sent before.
    /// The state is locked, and the session usable or failed.
    fn start_detach(&self, state: &mut SessionState) {
        state.lifecycle = RenderSessionState::Detaching;
        worker::post(&self.queue, Worker::detach);
    }

    /// Releases the session without waiting: it detaches, unless it is
    /// detached or abandoned already, and every call on it fails after. A
    /// caller-driven session that isn't attached, or whose map was released,
    /// is abandoned instead, so the release doesn't wait for the host to
    /// service it.
    fn dispose(&self, done: Option<Resolver<()>>) -> Result<(), Error> {
        with_unlocked_wakes(|| self.dispose_locked(done))
    }

    /// Releases the session without waiting and without graphics calls past
    /// the one in progress: a session that hasn't ended is abandoned, and
    /// keeps its graphics objects until the process exits.
    fn dispose_keeping_graphics(&self) -> Result<(), Error> {
        with_unlocked_wakes(|| {
            {
                let mut state = self.state.lock();
                if state.released {
                    return Err(released());
                }
                state.released = true;
                state.pacing = None;
                state.keep_graphics = true;
                if matches!(state.lifecycle, RenderSessionState::Detached | RenderSessionState::Abandoned) {
                    return Ok(());
                }
            }
            if self.abandon_locked(Abandoner::Library, true).is_err() {
                // Only the session's own work could hold the gate here: the
                // session detaches after it, keeping its graphics objects.
                let mut state = self.state.lock();
                if !matches!(
                    state.lifecycle,
                    RenderSessionState::Detaching | RenderSessionState::Detached | RenderSessionState::Abandoned
                ) {
                    self.start_detach(&mut state);
                }
            }
            Ok(())
        })
    }

    fn dispose_locked(&self, done: Option<Resolver<()>>) -> Result<(), Error> {
        let mut state = self.state.lock();
        if state.released {
            return Err(released());
        }
        state.released = true;
        state.pacing = None;
        // A released map waits for no service of its session's detach.
        let map_released = state.map.as_ref().is_some_and(Map::is_released);
        match state.lifecycle {
            RenderSessionState::Detached | RenderSessionState::Abandoned => {
                drop(state);
                if let Some(done) = done {
                    done.resolve(Ok(()));
                }
                return Ok(());
            }
            RenderSessionState::Detaching => {}
            RenderSessionState::Attaching | RenderSessionState::Failed if self.driver == Driver::CallerThread => {
                state.release_waiters.extend(done);
                drop(state);
                self.abandon_or_detach();
                return Ok(());
            }
            RenderSessionState::Attached if self.driver == Driver::CallerThread && map_released => {
                state.release_waiters.extend(done);
                drop(state);
                self.abandon_or_detach();
                return Ok(());
            }
            RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Failed => {
                self.start_detach(&mut state);
            }
        }
        state.release_waiters.extend(done);
        Ok(())
    }

    /// Abandons the released session, or detaches it when its own work
    /// holds the gate, which only it could here.
    fn abandon_or_detach(&self) {
        if self.abandon(Abandoner::Library).is_err() {
            let mut state = self.state.lock();
            if state.lifecycle != RenderSessionState::Abandoned {
                self.start_detach(&mut state);
            }
        }
    }

    /// Stops the session at once, as [`RenderSession::abandon`] documents,
    /// and returns how many graphics objects it keeps. The library may
    /// abandon a released session too.
    pub(crate) fn abandon(&self, by: Abandoner) -> Result<u32, Error> {
        with_unlocked_wakes(|| self.abandon_locked(by, false))
    }

    /// Waits for a [`RenderSession::send_then`] in progress to attach its
    /// callback. The session's thread calls it before it delivers what it
    /// sent the host, so that a command's result reaches the callback there,
    /// inside the driver call.
    pub(crate) fn wait_for_senders(&self) {
        drop(self.sending.lock());
    }

    /// Takes the sending lock, which the session's thread waits for before
    /// it delivers results, unless this thread holds it already: a send
    /// inside another, such as from a completion that a release settles at
    /// once on this thread, is delivered after the outer one too.
    fn hold_sending(&self) -> Option<Sending<'_>> {
        if SENDING.with(|sending| sending.borrow().contains(&self.id)) {
            return None;
        }
        let guard = self.sending.lock();
        SENDING.with(|sending| sending.borrow_mut().push(self.id));
        Some(Sending { id: self.id, _guard: guard })
    }

    /// Marks the session's thread as inside a driver call until the guard
    /// drops.
    pub(crate) fn enter_call(&self) -> InCall<'_> {
        InCall { shared: self, previous: self.in_call.swap(true, Ordering::SeqCst) }
    }

    /// Abandons the session, which keeps its graphics objects until the
    /// process exits if `keep_graphics`, and returns how many it keeps.
    ///
    /// A driver call can't be waited for on its own thread, where the
    /// session's callbacks run inside it, and a host's service on another
    /// thread is the host's to end, so either is busy. A core worker's call
    /// on another thread is waited for. A caller-driven session that is
    /// abandoned off its thread keeps its graphics objects, which only its
    /// thread may free: once this returns, the host may destroy what they
    /// were made on.
    fn abandon_locked(&self, by: Abandoner, keep_graphics: bool) -> Result<u32, Error> {
        let on_driver = self.thread.get() == Some(&std::thread::current().id());
        if self.in_call.load(Ordering::SeqCst) {
            if on_driver {
                return Err(Error::busy("the render session's driver call is running on this thread"));
            }
            if self.driver == Driver::CallerThread && by == Abandoner::Host {
                return Err(Error::busy("the render session's driver work is running on another thread"));
            }
        }
        let off_thread = self.driver == Driver::CallerThread && !on_driver;
        let gate = if on_driver {
            self.gate.try_lock().ok_or_else(|| Error::busy("the render session is rendering on this thread"))?
        } else {
            self.gate.lock()
        };
        let (waiters, map, kept) = {
            let mut state = self.state.lock();
            if by == Abandoner::Host && state.released {
                return Err(released());
            }
            match state.lifecycle {
                RenderSessionState::Attaching
                | RenderSessionState::Attached
                | RenderSessionState::Detaching
                | RenderSessionState::Failed => {}
                RenderSessionState::Detached | RenderSessionState::Abandoned => {
                    return Err(Error::invalid_state("the render session was detached or abandoned"));
                }
            }
            // The views keep the frames' sync objects in place, which the
            // abandon would let go of. A library's abandon, of a session
            // that the host released, keeps them until the views end.
            if by == Abandoner::Host && state.views != 0 {
                return Err(Error::busy("a view is open on one of the render session's acquired frames"));
            }
            state.lifecycle = RenderSessionState::Abandoned;
            state.keep_graphics |= keep_graphics || off_thread || state.abandon_keeps_graphics;
            let kept = if state.keep_graphics { state.graphics_objects } else { 0 };
            (state.take_waiters(), state.map.take(), kept)
        };
        match &map {
            // The map unbinds the session before another can attach.
            Some(map) => {
                map.unbind_session_then(self.id, self.queue.clone(), Worker::finish);
                map.release_session_claim(self.id);
            }
            None => worker::post(&self.queue, Worker::finish),
        }
        // A core worker lets go of its graphics objects at its next task,
        // which runs before this returns: the host may destroy what they were
        // made on once it does.
        // In the browser the session's queue runs on this thread, which runs
        // that task here instead, unless it is inside the session's work.
        #[cfg(target_family = "wasm")]
        let run_now = self.driver == Driver::CoreWorker
            && !self.in_call.load(Ordering::SeqCst)
            && !SENDING.with(|sending| sending.borrow().contains(&self.id));
        let torn_down = (self.driver == Driver::CoreWorker && !on_driver).then(|| {
            let (sender, receiver) = std::sync::mpsc::sync_channel::<()>(1);
            worker::post(&self.queue, move |_, _| {
                let _ = sender.send(());
            });
            receiver
        });
        drop(gate);
        if let Some(receiver) = torn_down {
            // A worker that ended drops the task, and the sender with it.
            let _ = receiver.recv();
        }
        #[cfg(target_family = "wasm")]
        if run_now {
            worker::post(&self.queue, |_, _| {});
            crate::web::run_session_now(self.id);
        }
        // Off the session's thread, the session makes no more graphics
        // calls: a core worker tore down above, and a caller-driven session
        // keeps its graphics objects. On it, the teardown that follows
        // still needs the device.
        if !on_driver {
            self.lease.revoke();
        }
        if kept != 0 {
            let reason = if keep_graphics {
                "it was abandoned without graphics calls"
            } else if off_thread {
                "only the thread that services it may free them"
            } else {
                "only a detach frees OpenGL and WebGPU objects"
            };
            log::warn!(
                target: log_target::RENDER,
                "An abandoned render session keeps {kept} graphics objects until the process exits: {reason}"
            );
        }
        let lost = || Error::target_lost("the render session was abandoned");
        waiters.settle(&Err(lost()), lost);
        drop(map);
        Ok(kept)
    }
}

/// Marks a session's thread as inside a driver call, as
/// [`SessionShared::enter_call`] does.
pub(crate) struct InCall<'a> {
    shared: &'a SessionShared,
    previous: bool,
}

impl Drop for InCall<'_> {
    fn drop(&mut self) {
        self.shared.in_call.store(self.previous, Ordering::SeqCst);
    }
}

fn released() -> Error {
    Error::invalid_state("the render session was released")
}

/// Fails unless the session takes work: it is attaching or attached, and
/// not released.
fn check_usable(state: &SessionState) -> Result<(), Error> {
    if state.released {
        return Err(released());
    }
    match state.lifecycle {
        RenderSessionState::Attaching | RenderSessionState::Attached => Ok(()),
        RenderSessionState::Failed => {
            Err(state.failure.clone().unwrap_or_else(|| Error::native("the render session failed")))
        }
        RenderSessionState::Detaching | RenderSessionState::Detached | RenderSessionState::Abandoned => {
            Err(Error::invalid_state("the render session is not attached: it was detached or abandoned"))
        }
    }
}

thread_local! {
    /// The driver wakes that this thread's posts asked for during a handle
    /// call, which may hold the session's locks.
    static DEFERRED_WAKES: RefCell<Option<Vec<Wake>>> = const { RefCell::new(None) };
    /// The sessions whose sending lock this thread holds.
    static SENDING: RefCell<Vec<SessionId>> = const { RefCell::new(Vec::new()) };
}

/// A session's sending lock, which [`SessionShared::hold_sending`] took.
struct Sending<'a> {
    id: SessionId,
    _guard: parking_lot::MutexGuard<'a, ()>,
}

impl Drop for Sending<'_> {
    fn drop(&mut self) {
        SENDING.with(|sending| sending.borrow_mut().retain(|id| *id != self.id));
    }
}

/// Runs `call`, which may post to a session's queue with the session's locks
/// held, then the driver wakes that its posts asked for, with nothing locked.
fn with_unlocked_wakes<R>(call: impl FnOnce() -> R) -> R {
    let outer = DEFERRED_WAKES.with(|deferred| deferred.borrow_mut().replace(Vec::new()));
    let result = call();
    let wakes = DEFERRED_WAKES.with(|deferred| {
        let mut deferred = deferred.borrow_mut();
        let wakes = deferred.take().unwrap_or_default();
        match outer {
            // The outer call runs them.
            Some(mut outer) => {
                outer.extend(wakes);
                *deferred = Some(outer);
                Vec::new()
            }
            None => wakes,
        }
    });
    for wake in wakes {
        wake();
    }
    result
}

/// Runs `wake` now, or once the handle call on this thread returns.
fn wake_unlocked(wake: &Wake) {
    let deferred = DEFERRED_WAKES.with(|deferred| match deferred.borrow_mut().as_mut() {
        Some(wakes) => {
            wakes.push(Arc::clone(wake));
            true
        }
        None => false,
    });
    if !deferred {
        wake();
    }
}

/// Sizes in pixels a target of `size` logical pixels at `pixel_ratio`.
pub(crate) fn physical_size(size: Size, pixel_ratio: f32) -> Size {
    Size::new((size.width as f32 * pixel_ratio) as u32, (size.height as f32 * pixel_ratio) as u32)
}

/// A render session: a renderer that draws a map into a target, on a thread
/// of its own or on the host's.
///
/// The host demands frames, which finish with [`FrameResult`]s that
/// [`drain_frame_results`](Self::drain_frame_results) takes. A demand shows
/// the map's commands sent before it, except the changes that an open
/// command group holds back. The session's work runs on its thread in the
/// order it was sent, and reports through a [`Completion`]. `Clone` is
/// another handle to the same session, and any thread may use one.
///
/// A session that attached with [`Driver::CallerThread`] runs its work only
/// when the host services its [`DriverWork`], which
/// [`take_driver_work`](Self::take_driver_work) hands out.
///
/// The session lives until [`detach`](Self::detach),
/// [`release`](Self::release), [`dispose`](Self::dispose) or
/// [`abandon`](Self::abandon), or until its last handle drops, and it holds
/// its map until then. A call that sends work to a session that no longer
/// takes it fails with [`ErrorKind::InvalidState`](mln_util::ErrorKind), or
/// with the failure of a session that failed.
///
/// Unless the session was released or disposed, the last drop of its
/// handles, [`AcquiredFrame`]s and [`FrameView`]s waits for a core worker's
/// thread to end, once the session has let go of its graphics objects and
/// its map. A drop on a thread of the library, such as in a callback or a
/// wake that the library runs, doesn't wait: it leaves the session's thread
/// to end alone.
#[derive(Clone)]
pub struct RenderSession {
    handle: Arc<SessionHandle>,
}

/// Disposes of the session when the last handle drops.
struct SessionHandle {
    shared: Arc<SessionShared>,
    thread_end: ThreadEnd,
}

/// A core worker's thread, which the last of the host's handles, frames and
/// views of the session to drop waits for. A caller-driven session has no
/// thread, and in the browser the thread is the host's.
type ThreadEnd = Option<Arc<Joined>>;

impl Drop for SessionHandle {
    /// Disposes of the session, which ends its thread once the frames and
    /// views of it end too, then waits for the thread as `thread_end` drops,
    /// unless they hold it.
    fn drop(&mut self) {
        let _ = self.shared.dispose(None);
    }
}

impl std::fmt::Debug for RenderSession {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RenderSession").field("id", &self.shared().id).finish()
    }
}

impl PartialEq for RenderSession {
    fn eq(&self, other: &RenderSession) -> bool {
        Arc::ptr_eq(&self.handle, &other.handle)
    }
}

impl Eq for RenderSession {}

/// Attaches a session to `map`, as [`Map::attach`] documents, and runs `then`
/// with the session and the attach's completion before the session's thread
/// may deliver that, as [`Map::attach_then`] documents.
pub(crate) fn attach<R>(
    map: &Map,
    target: RenderTarget,
    options: AttachOptions,
    then: impl FnOnce(&RenderSession, Completion<()>) -> R,
) -> Result<R, Error> {
    // The wakes that the attach runs, such as a caller-driven session's
    // driver-work wake, wait for the release of the session's sending lock,
    // so a host's wake that services the session can't wait for it.
    with_unlocked_wakes(|| attach_unlocked(map, target, options, then))
}

fn attach_unlocked<R>(
    map: &Map,
    target: RenderTarget,
    options: AttachOptions,
    then: impl FnOnce(&RenderSession, Completion<()>) -> R,
) -> Result<R, Error> {
    static NEXT_ID: AtomicU64 = AtomicU64::new(1);
    let id = SessionId(NEXT_ID.fetch_add(1, Ordering::Relaxed));
    let mut ring = None;
    let mut export_kind = None;
    let (size, target) = match target {
        RenderTarget::Offscreen { size, device } => (size, TargetInit::Offscreen(device)),
        RenderTarget::Surface { size, surface, device } => {
            if device.is_none() && !surface.needs_no_device() {
                return Err(Error::invalid_argument("a surface that the host made needs the host's device"));
            }
            surface.check_area()?;
            surface.check_pixel_format(device.as_ref())?;
            (size, TargetInit::Surface { surface: SentSurface::new(surface, id), device })
        }
        RenderTarget::Borrowed { size, ring: borrowed, device } => {
            let (producer, consumer) = device.borrowed_sync_kinds(&borrowed)?;
            let picked = options.producer_sync_kinds.pick(producer).ok_or_else(|| {
                Error::unsupported("render target supports none of the requested producer sync kinds")
            })?;
            if borrowed.depth() == 0 || borrowed.physical_size().is_empty() {
                return Err(Error::invalid_argument("a borrowed ring needs textures of a size"));
            }
            ring = Some((borrowed.depth(), picked, consumer));
            (size, TargetInit::Borrowed { ring: borrowed, device, producer: picked })
        }
        RenderTarget::Exported { size, ring: exported, device } => {
            if exported.depth == 0 {
                return Err(Error::invalid_argument("an exported ring needs at least one image"));
            }
            if exported.kinds.is_empty() {
                return Err(Error::invalid_argument("an exported ring needs a kind of image to share"));
            }
            if size.is_empty() {
                return Err(Error::invalid_argument("the target's size must not be empty"));
            }
            let device = match device {
                Some(device) => device,
                None => Device::for_exported_ring(&exported)?,
            };
            let (kind, producer, consumer) = device.export_plan(&exported)?;
            let picked = options.producer_sync_kinds.pick(producer).ok_or_else(|| {
                Error::unsupported("render target supports none of the requested producer sync kinds")
            })?;
            ring = Some((exported.depth, picked, consumer));
            export_kind = Some(kind);
            (size, TargetInit::Exported { ring: exported, device, kind, producer: picked })
        }
    };
    if size.is_empty() {
        return Err(Error::invalid_argument("the target's size must not be empty"));
    }
    let (target, lease) = target.lease();
    let presentation = matches!(target, TargetInit::Surface { .. });
    let queue = SessionQueue::default();
    let driver_armed = Arc::new(AtomicBool::new(true));
    if options.driver == Driver::CallerThread
        && let Some(wake) = options.driver_work_wake
    {
        let armed = Arc::clone(&driver_armed);
        queue.set_waker(Some(Arc::new(move || {
            if armed.swap(false, Ordering::SeqCst) {
                wake_unlocked(&wake);
            }
        })));
    }
    let scale_factor = map.scale_factor();
    let shared = Arc::new(SessionShared {
        id,
        driver: options.driver,
        queue: queue.clone(),
        thread: Arc::default(),
        runtime_thread: map.runtime_thread(),
        state: Mutex::new(SessionState {
            lifecycle: RenderSessionState::Attaching,
            released: false,
            size,
            scale_factor,
            sent_scale_factor: scale_factor,
            generation: 1,
            frame_generation: 0,
            failure: None,
            has_target: true,
            results: Vec::new(),
            frame_woken: false,
            redraw_armed: true,
            pacing: None,
            attach: None,
            detach_waiters: Vec::new(),
            release_waiters: Vec::new(),
            pending: BTreeMap::new(),
            next_work: 0,
            map: Some(map.clone()),
            init: Some(Box::new(Init {
                target,
                size,
                scale_factor,
                glyph_cache: map.glyph_cache(),
                font_face_engine: map.font_face_engine(),
                registry: Arc::clone(map.registry()),
                tile_cache_enabled: map.tile_cache_enabled(),
                local_glyphs: map.local_glyphs(),
                failure: None,
                pool: map.pool(),
            })),
            resizes_sent: 0,
            graphics_objects: 0,
            keep_graphics: false,
            abandon_keeps_graphics: false,
            ring: vec![RingSlot::default(); ring.map_or(0, |(depth, _, _)| depth)],
            acquired_frames: 0,
            pending_retargets: 0,
            views: 0,
            viewed_teardown: false,
        }),
        gate: Mutex::new(()),
        lease,
        frame_wake: options.frame_wake,
        redraw_wake: options.redraw_wake,
        capabilities: RenderSessionCapabilities {
            driver: options.driver,
            texture_ring_depth: ring.map_or(1, |(depth, _, _)| depth as u32),
            frame_acquisition: ring.is_some(),
            readback: !presentation && ring.is_none(),
            presentation,
            producer_sync_kind: ring.map(|(_, producer, _)| producer),
            consumer_sync_kinds: ring.map_or(SyncKinds::NONE, |(_, _, consumer)| consumer),
            export_kind,
        },
        driver_work_taken: AtomicBool::new(false),
        driver_armed,
        awaited_command: Arc::new(AtomicU64::new(u64::MAX)),
        preparing: AtomicBool::new(false),
        in_call: AtomicBool::new(false),
        sending: Mutex::new(()),
    });
    let (completion, resolver) = shared.completion();
    shared.state.lock().attach = Some(resolver);
    // Held until `then` returns: the session's thread delivers the attach's
    // result after, inside its driver call.
    let sending = Arc::clone(&shared);
    let held = sending.hold_sending();
    map.claim_session(id, Arc::downgrade(&shared))?;
    let mut thread_end = None;
    if options.driver == Driver::CoreWorker {
        match start_thread(Arc::clone(&shared)) {
            Ok((thread, end)) => {
                let _ = shared.thread.set(thread);
                thread_end = end.map(Arc::new);
            }
            Err(error) => {
                map.release_session_claim(id);
                return Err(error);
            }
        }
    }
    let session = RenderSession { handle: Arc::new(SessionHandle { shared, thread_end }) };
    if let Err(error) = map.bind_session(id, queue, Arc::clone(&session.shared().awaited_command), size) {
        // The map was released meanwhile: the session lets go of it. Its
        // drop waits for its thread, which waits for the sending lock.
        drop(held);
        drop(session);
        return Err(error);
    }
    if options.driver == Driver::CallerThread {
        #[cfg(target_family = "wasm")]
        prepare_caller_driven(session.shared());
        // The first service builds the renderer, which is work that is ready
        // now: this wakes the host, unless the map's first update, posted
        // from the runtime's thread, woke it already.
        session.shared().queue.wake();
    }
    let attached = then(&session, completion);
    // Before the session, whose drop may wait for its thread, which waits
    // for the sending lock.
    drop(held);
    Ok(attached)
}

/// Starts a core worker's thread, which builds the session's renderer, then
/// runs the session's queue until the session ends.
#[cfg(not(target_family = "wasm"))]
fn start_thread(shared: Arc<SessionShared>) -> Result<(ThreadId, Option<Joined>), Error> {
    let thread = mln_util::thread::spawn("RenderSession", move || {
        let queue = shared.queue.clone();
        let mut worker = None;
        worker::build(&shared, &mut worker, &mut None);
        // The worker holds the session from here on.
        drop(shared);
        queue.run(&mut worker);
        // The session's work that comes from now on drops at once, so tasks
        // that hold the queue can't keep it alive.
        queue.close();
        drop(worker);
    })
    .map_err(|error| Error::native(format!("can't start the render session's thread: {error}")))?;
    let thread_id = thread.thread().id();
    // The thread ends by itself once the session ends.
    Ok((thread_id, Some(Joined::new(thread))))
}

/// Runs a core worker on this thread, the browser's one, in the instance's
/// pump: it builds the session's renderer once its device is ready, which
/// may take the browser's WebGPU a while, then runs the session's queue until
/// the session ends.
#[cfg(target_family = "wasm")]
#[expect(clippy::unnecessary_wraps, reason = "the threads of other builds can fail to start")]
fn start_thread(shared: Arc<SessionShared>) -> Result<(ThreadId, Option<Joined>), Error> {
    let prepared = worker::prepare(&shared);
    wasm_bindgen_futures::spawn_local(async move {
        prepared.await;
        let queue = shared.queue.clone();
        let mut worker = None;
        worker::build(&shared, &mut worker, &mut None);
        let id = shared.id;
        // The worker holds the session from here on.
        drop(shared);
        let worker = RefCell::new(worker);
        crate::web::drive_session(id, queue.clone(), move || {
            queue.run_once(&mut worker.borrow_mut());
            if queue.is_stopped() {
                queue.close();
                drop(worker.borrow_mut().take());
            }
        });
    });
    Ok((std::thread::current().id(), None))
}

/// In the browser, asks for the device that a caller-driven session's build
/// needs, as a core worker does before it builds, and wakes the host to
/// service the session once the device is there.
#[cfg(target_family = "wasm")]
fn prepare_caller_driven(shared: &Arc<SessionShared>) {
    shared.preparing.store(true, Ordering::SeqCst);
    let prepared = worker::prepare(shared);
    let shared = Arc::downgrade(shared);
    wasm_bindgen_futures::spawn_local(async move {
        prepared.await;
        if let Some(shared) = shared.upgrade() {
            shared.preparing.store(false, Ordering::SeqCst);
            shared.queue.wake();
        }
    });
}

impl RenderSession {
    fn shared(&self) -> &Arc<SessionShared> {
        &self.handle.shared
    }

    /// Sends a command with `send`, such as [`barrier`](Self::barrier), and
    /// calls `callback` once with its result, where the session delivers
    /// it: on the session's thread, inside its driver call, even when the
    /// command runs before `send` returns. A [`Completion::on_complete`]
    /// after the command returned would run at once on this thread instead.
    /// The callback doesn't run when `send` fails.
    ///
    /// Fails as `send` does.
    pub fn send_then<T: Send + 'static>(
        &self,
        send: impl FnOnce(&RenderSession) -> Result<Completion<T>, Error>,
        callback: impl FnOnce(Result<T, Error>) + Send + 'static,
    ) -> Result<(), Error> {
        // The wakes that the command runs wait for the lock's release, so a
        // host's wake that services the session can't wait for it.
        with_unlocked_wakes(|| {
            let ready = {
                let _sending = self.shared().hold_sending();
                send(self)?.on_complete_unless_ready(callback)
            };
            // A result that came without the session's thread, such as a
            // command dropped unrun, goes to the callback here.
            if let Some((callback, result)) = ready {
                callback(result);
            }
            Ok(())
        })
    }

    /// Sends `task` to the session's thread, after the work sent before,
    /// resizes included, and completes with its result. A session that lost
    /// its graphics objects before the task runs completes it with
    /// [`ErrorKind::TargetLost`](mln_util::ErrorKind).
    fn operation<T: Send + 'static>(
        &self,
        task: impl FnOnce(&mut Worker, &mut Outbox) -> Result<T, Error> + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        let shared = self.shared();
        with_unlocked_wakes(|| {
            let mut state = shared.state.lock();
            check_usable(&state)?;
            let (completion, resolver) = shared.completion();
            let work = state.register(resolver);
            let resizes = state.resizes_sent;
            worker::post(&shared.queue, move |worker, outbox| {
                let task: worker::Task = Box::new(move |worker, outbox| {
                    let Some(resolver) = worker.take_work(work) else { return };
                    let result = task(worker, outbox);
                    outbox.resolve(resolver, result);
                });
                worker.after_earlier_work(resizes, task, outbox);
            });
            drop(state);
            Ok(completion)
        })
    }

    /// Hands out the work of a [`Driver::CallerThread`] session, for the
    /// calling thread to run: the session's graphics work runs only in its
    /// [`service`](DriverWork::service), and on no other thread.
    ///
    /// Take it once, on the host's graphics thread, from the attach on: the
    /// attach itself completes in a service. It stays available after a
    /// release, which the host services to its end.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a
    /// session on a core worker, and with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) once it was taken.
    pub fn take_driver_work(&self) -> Result<DriverWork, Error> {
        let shared = self.shared();
        if shared.driver != Driver::CallerThread {
            return Err(Error::unsupported("the render session runs on a core worker"));
        }
        if shared.driver_work_taken.swap(true, Ordering::SeqCst) {
            return Err(Error::invalid_state("the render session's driver work was taken already"));
        }
        let _ = shared.thread.set(std::thread::current().id());
        Ok(DriverWork::new(Arc::clone(shared)))
    }

    /// What the session can do. Fails with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) once it is released.
    pub fn capabilities(&self) -> Result<RenderSessionCapabilities, Error> {
        let state = self.shared().state.lock();
        if state.released {
            return Err(released());
        }
        Ok(self.shared().capabilities)
    }

    /// The session's state now. Fails with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) once it is released.
    pub fn snapshot(&self) -> Result<RenderSessionSnapshot, Error> {
        let state = self.shared().state.lock();
        if state.released {
            return Err(released());
        }
        Ok(RenderSessionSnapshot {
            state: state.lifecycle,
            extent: LogicalExtent { size: state.size, scale_factor: state.scale_factor },
            generation: state.generation,
            frame_generation: state.frame_generation,
            failure: state.failure.clone(),
            has_target: state.lifecycle == RenderSessionState::Attached && state.has_target,
            acquired_frames: state.acquired_frames,
        })
    }

    /// Takes the newest frame that a borrowed ring rendered, if one waits:
    /// the older frames that the host didn't acquire are dropped, and the
    /// session may render into their textures again. While the host holds a
    /// frame in every texture, demands wait for a release.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a
    /// session without a ring, and as a call that sends work does once the
    /// session no longer takes work.
    pub fn acquire_frame(&self) -> Result<Option<AcquiredFrame>, Error> {
        let shared = self.shared();
        let mut state = shared.state.lock();
        check_usable(&state)?;
        if !shared.capabilities.frame_acquisition {
            return Err(Error::unsupported("render session does not expose acquired texture frames"));
        }
        if state.pending_retargets != 0 {
            return Ok(None);
        }
        let newest = state
            .ring
            .iter()
            .enumerate()
            .filter(|(_, slot)| !slot.acquired && slot.frame.is_some())
            .max_by_key(|(_, slot)| slot.frame.as_ref().map_or(0, |frame| frame.result.frame_generation))
            .map(|(index, _)| index);
        let Some(index) = newest else { return Ok(None) };
        for (other, slot) in state.ring.iter_mut().enumerate() {
            if other != index && !slot.acquired {
                slot.frame = None;
            }
        }
        let slot = &mut state.ring[index];
        let Some(frame) = slot.frame.take() else { return Ok(None) };
        slot.acquired = true;
        state.acquired_frames += 1;
        Ok(Some(AcquiredFrame {
            shared: Arc::clone(shared),
            thread_end: self.handle.thread_end.clone(),
            slot: index,
            frame,
            done: false,
        }))
    }

    /// Gives a borrowed ring new textures, of `size` logical pixels: as many
    /// as it has, of its format. The completion reports the map's change and
    /// the sync after which the host may use the old textures that the new
    /// ring doesn't have, as [`release_target`](Self::release_target) says.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a
    /// session without a borrowed ring, with
    /// [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for another number
    /// of textures, and with [`ErrorKind::InvalidState`](mln_util::ErrorKind)
    /// while the host holds one of the ring's frames. Completes with
    /// [`ErrorKind::Unsupported`](mln_util::ErrorKind) for textures of
    /// another format or kind.
    pub fn set_ring(&self, size: Size, ring: BorrowedRing) -> Result<Completion<(Applied, GpuSync)>, Error> {
        let shared = Arc::clone(self.shared());
        {
            let mut state = shared.state.lock();
            check_usable(&state)?;
            if shared.capabilities.readback {
                return Err(Error::unsupported("an offscreen session keeps its target"));
            }
            if !shared.capabilities.frame_acquisition {
                return Err(Error::unsupported("a surface session takes no caller-owned texture"));
            }
            if shared.capabilities.export_kind.is_some() {
                return Err(Error::unsupported("an exported ring resizes instead of taking new textures"));
            }
            if state.acquired_frames != 0 {
                return Err(Error::invalid_state("cannot replace the render target while a texture frame is acquired"));
            }
            if ring.depth() != state.ring.len() {
                return Err(Error::invalid_argument(
                    "texture_count must equal the ring depth the session attached with",
                ));
            }
            state.pending_retargets += 1;
        }
        let guard = RetargetGuard(Arc::clone(&shared));
        // The guard goes with the closure, whether the closure runs or the
        // resize drops it.
        self.resize_then(size, None, move |worker, size, scale_factor, applied, outbox| {
            let _guard = guard;
            Ok((applied, worker.set_ring(size, scale_factor, &ring, outbox)?))
        })
    }

    /// Asks the session to render a frame, which shows the map commands sent
    /// before the call, except the changes that an open command group holds
    /// back, and the map's animations as they are at the call: demand each
    /// frame at the display's pace. A demand sent while the session attaches
    /// waits for it.
    ///
    /// Every demand finishes with one [`FrameResult`] that carries its token.
    /// A demand that isn't [`DemandMode::Force`](super::DemandMode::Force)
    /// replaces the demands with the same mode still on their way, which
    /// finish as [`FrameDisposition::Superseded`](super::FrameDisposition).
    /// While the session has no target, or a surface has no texture to
    /// render into, a demand finishes as
    /// [`FrameDisposition::TargetNotReady`](super::FrameDisposition).
    pub fn demand_frame(&self, demand: FrameDemand) -> Result<(), Error> {
        let shared = self.shared();
        with_unlocked_wakes(|| shared.demand_locked(&mut shared.state.lock(), demand))
    }

    /// Paces the session's frames with `pacer`, or with none, stops. At the
    /// first refresh that the pacer allows after the map has something new to
    /// draw, it demands a frame in
    /// [`DemandMode::IfChanged`](super::DemandMode::IfChanged) with token 0,
    /// unless a demand came first. One pacer can pace many sessions.
    ///
    /// Fails with [`ErrorKind::InvalidState`](mln_util::ErrorKind) for a
    /// session with a redraw wake.
    pub fn set_frame_pacer(&self, pacer: Option<Arc<FramePacer>>) -> Result<(), Error> {
        let shared = self.shared();
        let request = {
            let mut state = shared.state.lock();
            check_usable(&state)?;
            if shared.redraw_wake.is_some() {
                return Err(Error::invalid_state("a render session with a redraw wake takes no frame pacer"));
            }
            let same = match (&state.pacing, &pacer) {
                (Some(pacing), Some(pacer)) => Arc::ptr_eq(&pacing.pacer, pacer),
                (None, None) => true,
                _ => false,
            };
            if same {
                return Ok(());
            }
            state.pacing = pacer.map(|pacer| Pacing::new(pacer, Arc::downgrade(shared)));
            state.pacing.as_ref().filter(|_| !state.redraw_armed).map(Pacing::request)
        };
        if let Some(request) = request {
            request();
        }
        Ok(())
    }

    /// Takes the frame results that wait, oldest first. Fails with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) once the session is
    /// released.
    pub fn drain_frame_results(&self) -> Result<Vec<FrameResult>, Error> {
        let mut state = self.shared().state.lock();
        if state.released {
            return Err(released());
        }
        state.frame_woken = false;
        Ok(std::mem::take(&mut state.results))
    }

    /// Resizes the target, and the map with it, to `extent`'s size in
    /// logical pixels, and sets the map's scale factor to `extent`'s, such
    /// as when a window moves to a display of another density. The map keeps
    /// its tiles, and the renderer draws the next frame at the new scale
    /// factor. A later demand shows the new extent, and one sent before
    /// shows the old. An exported ring gets images of the new size, and the
    /// frames that the host holds keep theirs until it gives them back.
    ///
    /// Fails first as a call that sends work does once the session no
    /// longer takes work. Then fails with
    /// [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a borrowed ring,
    /// which [`set_ring`](Self::set_ring) resizes, and with
    /// [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an empty size,
    /// or a scale factor that isn't finite and positive.
    pub fn resize(&self, extent: LogicalExtent) -> Result<Completion<Applied>, Error> {
        check_usable(&self.shared().state.lock())?;
        let capabilities = self.shared().capabilities;
        if capabilities.frame_acquisition && capabilities.export_kind.is_none() {
            return Err(Error::unsupported("a borrowed ring is sized by its owner: give it new textures instead"));
        }
        let LogicalExtent { size, scale_factor } = extent;
        if !scale_factor.is_finite() || scale_factor <= 0.0 {
            return Err(Error::invalid_argument(format!("scale factor {scale_factor} isn't finite and positive")));
        }
        self.resize_then(size, Some(scale_factor), |worker, size, scale_factor, applied, outbox| {
            worker.resize(size, scale_factor, outbox).map(|()| applied)
        })
    }

    /// Sends a map command that resizes the map to `size`, at
    /// `scale_factor` or the session's when that is `None`, then runs
    /// `change` on the session's thread with the map's change, which resizes
    /// or replaces the target, and completes with its result.
    fn resize_then<T: Send + 'static>(
        &self,
        size: Size,
        scale_factor: Option<f64>,
        change: impl FnOnce(&mut Worker, Size, f64, Applied, &mut Outbox) -> Result<T, Error> + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        if size.is_empty() {
            return Err(Error::invalid_argument("the target's size must not be empty"));
        }
        let shared = self.shared();
        with_unlocked_wakes(|| {
            let mut state = shared.state.lock();
            check_usable(&state)?;
            let map = state.map.clone().ok_or_else(|| Error::invalid_state("the render session has no map"))?;
            let scale_factor = scale_factor.unwrap_or(state.sent_scale_factor);
            state.sent_scale_factor = scale_factor;
            let (completion, resolver) = shared.completion();
            let work = state.register(resolver);
            // The work sent after waits for the resize to land, which it
            // does even when the map drops it.
            state.resizes_sent += 1;
            let landing = worker::ResizeLanding::new(shared.queue.clone());
            let sent =
                map.resize_for_session(size, scale_factor, shared.queue.clone(), move |worker, result, outbox| {
                    if let Some(resolver) = worker.take_work::<T>(work) {
                        let result = result.and_then(|applied| change(worker, size, scale_factor, applied, outbox));
                        outbox.resolve(resolver, result);
                    }
                    landing.land(worker, outbox);
                });
            if let Err(error) = sent {
                state.pending.remove(&work);
                return Err(error);
            }
            drop(state);
            Ok(completion)
        })
    }

    /// Lets go of a surface session's surface, and keeps the rest of the
    /// session, such as when the platform takes the window away. The demands
    /// on their way, and later ones until
    /// [`set_surface`](Self::set_surface) gives the session a surface again,
    /// finish as [`FrameDisposition::TargetNotReady`](super::FrameDisposition).
    /// Once the release completes, the session no longer uses the surface or
    /// the window it was made from. For a ring, it completes with a sync of
    /// the session's producer kind, after which the host may use or destroy
    /// the textures: the session's GPU work on them, and its waits for the
    /// consumer syncs that they were given back with, end there. The session
    /// keeps the objects that the sync names until its next release or
    /// replacement of the target completes, or it ends. It keeps nothing of
    /// the released textures: one that a later ring has again starts fresh,
    /// even one that the host gave up.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for an
    /// offscreen session or an exported ring, whose targets are their own.
    /// Completes with [`ErrorKind::Native`](mln_util::ErrorKind) when a ring
    /// can't submit its work on the textures, which it keeps then.
    pub fn release_target(&self) -> Result<Completion<Option<GpuSync>>, Error> {
        if self.shared().capabilities.export_kind.is_some() {
            return Err(Error::unsupported("an exported ring keeps its images until the session ends"));
        }
        let shared = Arc::clone(self.shared());
        let capabilities = shared.capabilities;
        if !capabilities.presentation && !capabilities.frame_acquisition {
            check_usable(&shared.state.lock())?;
            return Err(Error::unsupported("an offscreen session keeps its target"));
        }
        let guard = {
            let mut state = shared.state.lock();
            check_usable(&state)?;
            if state.acquired_frames != 0 {
                return Err(Error::invalid_state("release every acquired frame before the target"));
            }
            // No frame is acquired until the release runs: its textures go
            // with it.
            capabilities.frame_acquisition.then(|| {
                state.pending_retargets += 1;
                RetargetGuard(Arc::clone(&shared))
            })
        };
        self.operation(move |worker, outbox| {
            let _guard = guard;
            worker.release_target(outbox)
        })
    }

    /// Gives a surface session a new surface of `size` logical pixels, after
    /// [`release_target`](Self::release_target), or in place of the one it
    /// has, and resizes the map to it. Once the completion reports success,
    /// the session no longer uses the old surface. The session presents to
    /// it on its device: a window becomes a surface of the device's backend,
    /// and a `Surface::wgpu` must come from the instance of a WebGPU
    /// device. Where the backend made for the old surface can't present to
    /// the new one, as OpenGL ES's context can't to another window, the
    /// session makes its backend and renderer again.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an
    /// empty size, with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for
    /// an offscreen session, and with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) for a window that is
    /// minimized or has no client area. Completes with
    /// [`ErrorKind::Unsupported`](mln_util::ErrorKind) when the session's
    /// device can't present to the surface in the format that the session
    /// renders in: attach a new session instead. Completes with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) when the window had no
    /// area to present to by then. The session then keeps the surface it had,
    /// at the new size.
    pub fn set_surface(&self, size: Size, surface: Surface) -> Result<Completion<Applied>, Error> {
        check_usable(&self.shared().state.lock())?;
        let capabilities = self.shared().capabilities;
        if capabilities.frame_acquisition {
            return Err(Error::unsupported("a borrowed texture session takes no native surface"));
        }
        if !capabilities.presentation {
            return Err(Error::unsupported("an offscreen session keeps its target"));
        }
        surface.check_area()?;
        let surface = SentSurface::new(surface, self.shared().id);
        self.resize_then(size, None, move |worker, size, scale_factor, applied, outbox| {
            worker.set_surface(size, scale_factor, surface.receive()?, outbox).map(|()| applied)
        })
    }

    /// Ends the frame demands on their way, including those that wait for a
    /// map update and the demand of a still image, which finish as
    /// [`FrameDisposition::NoUpdate`](super::FrameDisposition), and completes
    /// once the work sent before it has run. A still image that still waits
    /// gets a new demand once the barrier ran.
    pub fn barrier(&self) -> Result<Completion<()>, Error> {
        self.operation(|worker, outbox| {
            worker.barrier(outbox);
            Ok(())
        })
    }

    /// Frees memory that the renderer can rebuild, such as caches.
    pub fn reduce_memory_use(&self) -> Result<Completion<()>, Error> {
        self.operation(|worker, _| worker.reduce_memory_use())
    }

    /// Clears what the renderer holds, such as loaded tiles, so that the
    /// next frame loads and draws the map again.
    pub fn clear_data(&self) -> Result<Completion<()>, Error> {
        self.operation(Worker::clear_data)
    }

    /// Logs the renderer's sources and tiles.
    pub fn dump_debug_logs(&self) -> Result<Completion<()>, Error> {
        self.operation(|worker, _| worker.dump_debug_logs())
    }

    /// The pixels of the last frame that an offscreen session rendered:
    /// premultiplied RGBA, in rows from top to bottom, at the target's size
    /// in pixels.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a
    /// surface session. Completes with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) when the session
    /// hasn't rendered a frame since it attached or resized.
    pub fn read_texture(&self) -> Result<Completion<PremultipliedImage>, Error> {
        check_usable(&self.shared().state.lock())?;
        if !self.shared().capabilities.readback {
            return Err(Error::unsupported("a surface session can't read its frames back"));
        }
        self.operation(|worker, _| worker.read_texture())
    }

    /// The features that the session drew in an area of the screen, in its
    /// last frame. A feature that several layers draw appears once for each.
    /// The topmost layer's features come first, and the bottom layer's last.
    /// A box covers only its part inside the target, so one past every edge
    /// covers the whole target.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for a
    /// point that isn't finite or a line string without points.
    pub fn query_rendered_features(
        &self,
        geometry: RenderedQueryGeometry,
        options: RenderedQueryOptions,
    ) -> Result<Completion<Vec<Feature>>, Error> {
        geometry.points()?;
        self.operation(move |worker, _| match geometry.within(worker.size()).map(|geometry| geometry.points()) {
            Some(points) => worker.renderer()?.query_rendered_features(&points?, &options),
            None => Ok(Vec::new()),
        })
    }

    /// The features of the source `source_id` in the tiles that it loaded
    /// for the current view. A source loads tiles only while a visible layer
    /// uses it.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an
    /// empty `source_id`.
    pub fn query_source_features(
        &self,
        source_id: impl Into<String>,
        options: SourceQueryOptions,
    ) -> Result<Completion<Vec<Feature>>, Error> {
        let source_id = source_id_of(source_id)?;
        self.operation(move |worker, _| worker.renderer()?.query_source_features(&source_id, &options))
    }

    /// The clusters and points that a cluster of a clustered GeoJSON source
    /// splits into at the next zoom level. `cluster_id` is a cluster
    /// feature's `cluster_id` property.
    ///
    /// The query reads the source as the session's most recent render
    /// attempt had it. A cluster ID belongs to one version of the source's
    /// data, so query the features again after the data changes.
    ///
    /// Completes with [`ErrorKind::NotFound`](mln_util::ErrorKind) when that
    /// render had no source `source_id`, or the source has no cluster
    /// `cluster_id`, and with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind)
    /// when the source isn't a clustered GeoJSON source.
    pub fn cluster_children(
        &self,
        source_id: impl Into<String>,
        cluster_id: u32,
    ) -> Result<Completion<Vec<feature::Feature<f64>>>, Error> {
        let source_id = source_id_of(source_id)?;
        self.operation(move |worker, _| worker.cluster(&source_id, cluster_id, |_, children| Ok(children)))
    }

    /// The points in a cluster of a clustered GeoJSON source: it skips the
    /// first `offset`, then holds up to `limit`. It fails as
    /// [`cluster_children`](Self::cluster_children) does.
    pub fn cluster_leaves(
        &self,
        source_id: impl Into<String>,
        cluster_id: u32,
        limit: u32,
        offset: u32,
    ) -> Result<Completion<Vec<feature::Feature<f64>>>, Error> {
        let source_id = source_id_of(source_id)?;
        self.operation(move |worker, _| {
            worker.cluster(&source_id, cluster_id, |index, _| index.get_leaves(cluster_id, limit, offset))
        })
    }

    /// The zoom level at which a cluster of a clustered GeoJSON source splits
    /// into two or more clusters or points. It fails as
    /// [`cluster_children`](Self::cluster_children) does.
    pub fn cluster_expansion_zoom(
        &self,
        source_id: impl Into<String>,
        cluster_id: u32,
    ) -> Result<Completion<u8>, Error> {
        let source_id = source_id_of(source_id)?;
        self.operation(move |worker, _| {
            worker.cluster(&source_id, cluster_id, |index, _| index.get_cluster_expansion_zoom(cluster_id))
        })
    }

    /// Detaches the session from its map and frees its graphics objects. The
    /// demands on their way finish as
    /// [`FrameDisposition::TargetNotReady`](super::FrameDisposition). Once
    /// the detach completes, another session can attach to the map.
    pub fn detach(&self) -> Result<Completion<()>, Error> {
        let shared = self.shared();
        with_unlocked_wakes(|| {
            let mut state = shared.state.lock();
            if !matches!(state.lifecycle, RenderSessionState::Failed) {
                check_usable(&state)?;
            } else if state.released {
                return Err(released());
            }
            if state.acquired_frames != 0 {
                return Err(Error::invalid_state("release every acquired frame before the session detaches"));
            }
            let (completion, resolver) = shared.completion();
            state.detach_waiters.push(resolver);
            shared.start_detach(&mut state);
            Ok(completion)
        })
    }

    /// Stops the session at once, without its pending graphics work: that
    /// work completes with [`ErrorKind::TargetLost`](mln_util::ErrorKind)
    /// before this returns, and the demands on their way finish as
    /// [`FrameDisposition::TargetNotReady`](super::FrameDisposition). Once it
    /// returns, the session renders no more, and another session can attach
    /// to the map. Release the session afterward.
    ///
    /// Returns how many graphics objects the session keeps until the
    /// process exits. A caller-driven session abandoned on the thread that
    /// services it lets go of them there, at its next service or when its
    /// [`DriverWork`] drops, and keeps none. Abandoned on another thread, it
    /// keeps them, since only that thread may free them, and logs a warning.
    ///
    /// It waits for a core worker's driver call on another thread, such as a
    /// frame that it renders. Fails with
    /// [`ErrorKind::Busy`](mln_util::ErrorKind) inside a driver call on the
    /// session's own thread, such as from a completion that the session
    /// delivers, and while a caller-driven session's service runs on another
    /// thread. Fails with [`ErrorKind::InvalidState`](mln_util::ErrorKind)
    /// once the session is detached, abandoned or released.
    pub fn abandon(&self) -> Result<u32, Error> {
        self.shared().abandon(Abandoner::Host)
    }

    /// Abandons the session as [`abandon`](Self::abandon) does, but makes no
    /// graphics call: the session keeps its graphics objects until the
    /// process exits. Returns how many it keeps, such as its renderer and
    /// the backend it draws with.
    ///
    /// This is for a host whose shutdown may already have destroyed the
    /// graphics objects behind the session.
    pub fn abandon_keeping_graphics(&self) -> Result<u32, Error> {
        with_unlocked_wakes(|| self.shared().abandon_locked(Abandoner::Host, true))
    }

    /// Releases the session, detaching it first when it is attached. The
    /// completion reports when the session has let go of its map and its
    /// graphics objects, and nothing waits for its thread to end after
    /// that. Every call on the session fails with
    /// [`ErrorKind::InvalidState`](mln_util::ErrorKind) after.
    ///
    /// A caller-driven session detaches at the host's next service, so keep
    /// servicing it until the release completes. One that isn't attached,
    /// such as while it attaches or after it failed, is abandoned instead,
    /// and so is one whose map is released before that service.
    pub fn release(&self) -> Result<Completion<()>, Error> {
        {
            let state = self.shared().state.lock();
            let live = matches!(state.lifecycle, RenderSessionState::Attaching | RenderSessionState::Attached);
            if live && !state.released && state.acquired_frames != 0 {
                return Err(Error::invalid_state("release every acquired frame before the session is released"));
            }
        }
        let (completion, resolver) = self.shared().completion();
        self.shared().dispose(Some(resolver))?;
        self.detach_thread();
        Ok(completion)
    }

    /// Releases the session in any state, without waiting, and nothing waits
    /// for its thread to end. Dropping the last handle does the same, then
    /// waits for the thread.
    pub fn dispose(&self) -> Result<(), Error> {
        self.shared().dispose(None)?;
        self.detach_thread();
        Ok(())
    }

    /// Releases the session in any state, without waiting and without a
    /// graphics call past the one in progress. A session that hasn't ended
    /// is abandoned, keeping its graphics objects until the process exits,
    /// as [`abandon_keeping_graphics`](Self::abandon_keeping_graphics) does.
    /// Nothing waits for its thread to end.
    pub fn dispose_keeping_graphics(&self) -> Result<(), Error> {
        self.shared().dispose_keeping_graphics()?;
        self.detach_thread();
        Ok(())
    }

    /// Leaves a core worker's thread to end alone, for a release or
    /// disposal, which don't wait for it.
    fn detach_thread(&self) {
        if let Some(thread) = &self.handle.thread_end {
            thread.detach();
        }
    }
}

/// Counts a ring replacement or release on its way until it runs, or never
/// will.
struct RetargetGuard(Arc<SessionShared>);

impl Drop for RetargetGuard {
    fn drop(&mut self) {
        let mut state = self.0.state.lock();
        state.pending_retargets = state.pending_retargets.saturating_sub(1);
    }
}

/// A frame of a borrowed ring that the host acquired, until it gives it
/// back. Dropping it gives it back as [`release`](Self::release) with
/// [`SyncKind::CpuComplete`] does.
pub struct AcquiredFrame {
    shared: Arc<SessionShared>,
    /// Dropped after the frame is given back.
    thread_end: ThreadEnd,
    slot: usize,
    frame: Published,
    done: bool,
}

impl std::fmt::Debug for AcquiredFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AcquiredFrame").field("slot", &self.slot).field("frame", &self.frame).finish_non_exhaustive()
    }
}

impl AcquiredFrame {
    /// The index of the frame's texture in the ring.
    #[must_use]
    pub fn slot(&self) -> usize {
        self.slot
    }

    /// The result of the demand that rendered the frame.
    #[must_use]
    pub fn result(&self) -> &FrameResult {
        &self.frame.result
    }

    /// What to wait for before reading the frame's texture. The object it
    /// names is the session's, and stays valid until the frame is given
    /// back.
    #[must_use]
    pub fn producer_sync(&self) -> GpuSync {
        self.frame.sync
    }

    /// The host's texture that holds the frame.
    #[must_use]
    pub fn texture(&self) -> Option<&FrameTexture> {
        self.frame.texture.as_ref()
    }

    /// Whether [`release`](Self::release) takes a sync of `kind`.
    #[must_use]
    pub fn accepts(&self, kind: SyncKind) -> bool {
        self.shared.capabilities.consumer_sync_kinds.contains(kind)
    }

    /// Opens a view of the frame, which keeps the frame's texture and sync
    /// objects in place until the view drops: an
    /// [`abandon`](RenderSession::abandon) fails while one is open, and a
    /// dispose of the session keeps them until the last view ends. Keep the
    /// frame until its views end.
    ///
    /// Fails with [`ErrorKind::TargetLost`](mln_util::ErrorKind) once the
    /// session can no longer render.
    pub fn view(&self) -> Result<FrameView, Error> {
        let mut state = self.shared.state.lock();
        if !matches!(
            state.lifecycle,
            RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
        ) {
            return Err(Error::target_lost("the render session failed, or was detached, abandoned, or disposed"));
        }
        state.views += 1;
        Ok(FrameView { shared: Arc::clone(&self.shared), _thread_end: self.thread_end.clone() })
    }

    /// Whether the session can still render: the frame's objects stay valid
    /// until it ends.
    #[must_use]
    pub fn session_live(&self) -> bool {
        matches!(
            self.shared.state.lock().lifecycle,
            RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
        )
    }

    /// Gives the frame back once the host's reads of its texture reach
    /// `consumer`: the session waits for it before it renders into the
    /// texture again, and takes the sync file or GL fence that it names.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind), handing
    /// back the frame and `consumer`, for a kind that the session's
    /// [`consumer_sync_kinds`](RenderSessionCapabilities::consumer_sync_kinds)
    /// lacks.
    #[expect(
        clippy::result_large_err,
        reason = "the error hands back the frame and the consumer's sync, so that the caller can release it another way"
    )]
    pub fn release(mut self, consumer: ConsumerSync) -> Result<(), (AcquiredFrame, ConsumerSync, Error)> {
        if !self.shared.capabilities.consumer_sync_kinds.contains(consumer.sync.kind) {
            return Err((self, consumer, Error::unsupported("render backend does not support this gpu sync kind")));
        }
        self.done = true;
        give_back(&self.shared, self.slot, Some(consumer));
        Ok(())
    }

    /// Gives the frame up without saying when the host's reads end: the
    /// session never renders into its texture again, even when
    /// [`set_ring`](RenderSession::set_ring) gives the ring the texture
    /// again, until a [`release_target`](RenderSession::release_target) of
    /// the ring.
    pub fn discard(mut self) {
        self.done = true;
        give_back(&self.shared, self.slot, None);
    }
}

/// A view of an acquired frame, which [`AcquiredFrame::view`] opens, until
/// it drops.
pub struct FrameView {
    shared: Arc<SessionShared>,
    /// Dropped after the view ends.
    _thread_end: ThreadEnd,
}

impl std::fmt::Debug for FrameView {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FrameView").finish_non_exhaustive()
    }
}

impl Drop for FrameView {
    fn drop(&mut self) {
        let release = {
            let mut state = self.shared.state.lock();
            state.views = state.views.saturating_sub(1);
            state.views == 0 && std::mem::take(&mut state.viewed_teardown)
        };
        if release {
            worker::post(&self.shared.queue, |worker, _| worker.release_viewed());
        }
    }
}

impl Drop for AcquiredFrame {
    fn drop(&mut self) {
        if !self.done {
            give_back(&self.shared, self.slot, Some(ConsumerSync::of(GpuSync::of(SyncKind::CpuComplete))));
        }
    }
}

/// Gives the ring's `slot` back to the session, with the host's `consumer`
/// sync, or discarded without one.
fn give_back(shared: &Arc<SessionShared>, slot: usize, consumer: Option<ConsumerSync>) {
    with_unlocked_wakes(|| {
        let mut state = shared.state.lock();
        state.acquired_frames = state.acquired_frames.saturating_sub(1);
        let live = matches!(
            state.lifecycle,
            RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Detaching
        );
        if !live {
            if let Some(entry) = state.ring.get_mut(slot) {
                entry.acquired = false;
                // The host's GPU may still read a texture whose wait never
                // ran.
                entry.quarantined |=
                    consumer.as_ref().is_none_or(|consumer| consumer.sync.kind != SyncKind::CpuComplete);
            }
            // A sync file that no backend waits on closes with `consumer`.
            return;
        }
        if let Some(sync) = consumer {
            worker::post(&shared.queue, move |worker, outbox| worker.release_slot(slot, sync, outbox));
        } else {
            if let Some(entry) = state.ring.get_mut(slot) {
                entry.acquired = false;
                entry.quarantined = !entry.retired;
                entry.retired = false;
            }
            worker::post(&shared.queue, move |worker, outbox| worker.discard_slot(slot, outbox));
        }
    });
}

fn source_id_of(source_id: impl Into<String>) -> Result<String, Error> {
    let source_id = source_id.into();
    if source_id.is_empty() {
        return Err(Error::invalid_argument("the source ID must not be empty"));
    }
    Ok(source_id)
}

// Handles cross threads; the renderer never leaves its own.
const _: () = {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<RenderSession>();
};