mln-rs-runtime 0.0.11

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
//! 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, Mutex, OnceLock};
use std::thread::ThreadId;

use mln_actor::{RunLoop, RunLoopHandle};
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::sync::MutexExt;

use super::driver::DriverWork;
use super::frame::{FrameDemand, FrameResult};
use super::local::Pending;
use super::query::RenderedQueryGeometry;
use super::target::{
    AttachOptions, BorrowedRing, ConsumerSync, Device, Driver, ExportKind, FrameTexture, GpuSync, RenderTarget,
    Surface, SyncKind, SyncKinds, Wake,
};
use super::worker::{self, Init, TargetInit, Worker};
use crate::completion::{Completion, Home, Resolver};
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. 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: Send {
    fn fail(self: Box<Self>, error: Error);
    fn into_any(self: Box<Self>) -> Box<dyn Any + Send>;
}

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

    fn into_any(self: Box<Self>) -> Box<dyn Any + Send> {
        self
    }
}

/// 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.
    pub(crate) redraw_armed: bool,
    /// 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,
    /// 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,
}

pub(crate) struct SessionShared {
    pub(crate) id: SessionId,
    pub(crate) driver: Driver,
    pub(crate) run_loop: RunLoopHandle,
    /// 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<()>,
    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>,
}

impl SessionShared {
    /// A completion that the session's thread resolves.
    fn completion<T: Send + 'static>(&self) -> (Completion<T>, Resolver<T>) {
        Completion::at(Home::Later(self.thread.clone(), 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 = self.state.lock_unpoisoned().pending.remove(&id)?;
        work.into_any().downcast::<Resolver<T>>().ok().map(|resolver| *resolver)
    }

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

    /// 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.run_loop, worker::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 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_unpoisoned();
                if state.released {
                    return Err(released());
                }
                state.released = true;
                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_unpoisoned();
                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_unpoisoned();
        if state.released {
            return Err(released());
        }
        state.released = true;
        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);
                if self.abandon(Abandoner::Library).is_err() {
                    // Only the session's own work could hold the gate here.
                    let mut state = self.state.lock_unpoisoned();
                    if state.lifecycle != RenderSessionState::Abandoned {
                        self.start_detach(&mut state);
                    }
                }
                return Ok(());
            }
            RenderSessionState::Attaching | RenderSessionState::Attached | RenderSessionState::Failed => {
                self.start_detach(&mut state);
            }
        }
        state.release_waiters.extend(done);
        Ok(())
    }

    /// Stops the session at once, as [`RenderSession::abandon`] documents.
    /// The library may abandon a released session too.
    pub(crate) fn abandon(&self, by: Abandoner) -> Result<(), Error> {
        with_unlocked_wakes(|| self.abandon_locked(by, false)).map(drop)
    }

    /// Abandons the session, which keeps its graphics objects until the
    /// process exits if `keep_graphics`, and returns how many it keeps.
    fn abandon_locked(&self, by: Abandoner, keep_graphics: bool) -> Result<u32, Error> {
        let on_driver = self.thread.get() == Some(&std::thread::current().id());
        let gate = if on_driver {
            self.gate.try_lock().map_err(|_| Error::busy("the render session is rendering on this thread"))?
        } else {
            self.gate.lock_unpoisoned()
        };
        let (waiters, map, kept) = {
            let mut state = self.state.lock_unpoisoned();
            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;
            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.run_loop.clone(), super::worker::Worker::finish);
                map.release_session_claim(self.id);
            }
            None => worker::post(&self.run_loop, worker::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.
        let torn_down = (self.driver == Driver::CoreWorker && !on_driver).then(|| {
            let (sender, receiver) = std::sync::mpsc::sync_channel::<()>(1);
            worker::post(&self.run_loop, 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();
        }
        let lost = || Error::target_lost("the render session was abandoned");
        waiters.settle(&Err(lost()), lost);
        drop(map);
        Ok(kept)
    }
}

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) };
}

/// Runs `call`, which may post to a session's loop 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(wake.clone());
            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.
#[derive(Clone)]
pub struct RenderSession {
    handle: Arc<SessionHandle>,
}

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

impl Drop for SessionHandle {
    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.
pub(crate) fn attach(
    map: &Map,
    target: RenderTarget,
    options: AttachOptions,
) -> Result<(RenderSession, Completion<()>), Error> {
    static NEXT_ID: AtomicU64 = AtomicU64::new(1);
    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()?;
            (size, TargetInit::Surface { surface, 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 presentation = matches!(target, TargetInit::Surface { .. });
    let run_loop = RunLoopHandle::new();
    let driver_armed = Arc::new(AtomicBool::new(true));
    if options.driver == Driver::CallerThread
        && let Some(wake) = options.driver_work_wake
    {
        let armed = driver_armed.clone();
        run_loop.set_waker(Some(Arc::new(move || {
            if armed.swap(false, Ordering::SeqCst) {
                wake_unlocked(&wake);
            }
        })));
    }
    let id = SessionId(NEXT_ID.fetch_add(1, Ordering::Relaxed));
    let scale_factor = map.scale_factor();
    let shared = Arc::new(SessionShared {
        id,
        driver: options.driver,
        run_loop: run_loop.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,
            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(),
                tile_cache_enabled: map.tile_cache_enabled(),
            })),
            resizes_sent: 0,
            graphics_objects: 0,
            keep_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(()),
        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,
    });
    let (completion, resolver) = shared.completion();
    shared.state.lock_unpoisoned().attach = Some(resolver);
    map.claim_session(id, Arc::downgrade(&shared))?;
    if options.driver == Driver::CoreWorker {
        match start_thread(shared.clone()) {
            Ok(thread) => {
                let _ = shared.thread.set(thread);
            }
            Err(error) => {
                map.release_session_claim(id);
                return Err(error);
            }
        }
    }
    let session = RenderSession { handle: Arc::new(SessionHandle { shared }) };
    if let Err(error) = map.bind_session(id, run_loop, size) {
        // The map was released meanwhile: the session lets go of it.
        drop(session);
        return Err(error);
    }
    Ok((session, completion))
}

/// Starts a core worker's thread, which builds the session's renderer, then
/// runs the session's loop until the session ends.
fn start_thread(shared: Arc<SessionShared>) -> Result<ThreadId, Error> {
    let thread = mln_util::thread::builder("RenderSession")
        .spawn(move || {
            let Some(run_loop) = RunLoop::adopt(&shared.run_loop) else { return };
            let _current = run_loop.enter();
            let slot = worker::Slot::default();
            let entered = worker::enter(&slot);
            worker::build(&shared, &slot);
            // The worker holds the session from here on.
            drop(shared);
            run_loop.run();
            let worker = slot.borrow_mut().take();
            drop(worker);
            drop(entered);
        })
        .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, so nothing joins it.
    drop(thread);
    Ok(thread_id)
}

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

    /// 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> {
        self.settled_operation(|worker, outbox| task(worker, outbox).map(|value| (value, None)))
    }

    /// Sends `task` as [`operation`](Self::operation) does, and completes
    /// once the host's reads that it returns end, or a second passes.
    fn settled_operation<T: Send + 'static>(
        &self,
        task: impl FnOnce(&mut Worker, &mut Outbox) -> Result<(T, Pending), Error> + Send + 'static,
    ) -> Result<Completion<T>, Error> {
        let shared = self.shared();
        with_unlocked_wakes(|| {
            let mut state = shared.state.lock_unpoisoned();
            check_usable(&state)?;
            let (completion, resolver) = shared.completion();
            let work = state.register(resolver);
            let resizes = state.resizes_sent;
            worker::post(&shared.run_loop, move |worker, outbox| {
                let task: worker::Task = Box::new(move |worker, outbox| {
                    let Some(resolver) = worker.take_work(work) else { return };
                    match task(worker, outbox) {
                        Ok((value, pending)) => worker.settle_then(
                            pending,
                            move |outbox, settled| outbox.resolve(resolver, settled.map(|()| value)),
                            outbox,
                        ),
                        Err(error) => outbox.resolve(resolver, Err(error)),
                    }
                });
                worker.after_resizes(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 run_loop = RunLoop::adopt(&shared.run_loop)
            .ok_or_else(|| Error::invalid_state("the render session's driver work was taken already"))?;
        let _ = shared.thread.set(std::thread::current().id());
        Ok(DriverWork::new(shared.clone(), run_loop))
    }

    /// 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_unpoisoned();
        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_unpoisoned();
        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_unpoisoned();
        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: shared.clone(), slot: index, frame, done: false }))
    }

    /// Gives a borrowed ring new textures, of `size` logical pixels: as many
    /// as it has, of its format. Once the completion reports success, the
    /// session no longer uses the old textures, and no longer waits for the
    /// consumer sync that they were given back with: the host may reuse or
    /// destroy them. The session's thread polls that sync on its later turns,
    /// for a second at most, and logs a wait that runs out. Frames wait for
    /// it meanwhile.
    ///
    /// 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>, Error> {
        let shared = self.shared().clone();
        {
            let mut state = shared.state.lock_unpoisoned();
            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(shared.clone());
        // 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, outbox| {
            let _guard = guard;
            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. 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(|| {
            let mut state = shared.state.lock_unpoisoned();
            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 = state.map.as_ref().map_or(0, Map::sent_sequence);
            state.redraw_armed = true;
            worker::post(&shared.run_loop, move |worker, outbox| worker.demand(demand, sequence, outbox));
            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_unpoisoned();
        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 with [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an
    /// empty size, or a scale factor that isn't finite and positive, and
    /// with [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a borrowed
    /// ring, which [`set_ring`](Self::set_ring) resizes.
    pub fn resize(&self, extent: LogicalExtent) -> Result<Completion<Applied>, Error> {
        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, outbox| {
            worker.resize(size, scale_factor, outbox).map(|()| None)
        })
    }

    /// 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, which resizes or replaces the
    /// target.
    fn resize_then(
        &self,
        size: Size,
        scale_factor: Option<f64>,
        change: impl FnOnce(&mut Worker, Size, f64, &mut Outbox) -> Result<Pending, Error> + Send + 'static,
    ) -> Result<Completion<Applied>, 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_unpoisoned();
            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.run_loop.clone());
            let sent =
                map.resize_for_session(size, scale_factor, shared.run_loop.clone(), move |worker, result, outbox| {
                    if let Some(resolver) = worker.take_work::<Applied>(work) {
                        match result.and_then(|applied| Ok((applied, change(worker, size, scale_factor, outbox)?))) {
                            // A replacement of a ring's textures completes
                            // once the host's reads of those it let go of end.
                            Ok((applied, pending)) => worker.settle_then(
                                pending,
                                move |outbox, settled| outbox.resolve(resolver, settled.map(|()| applied)),
                                outbox,
                            ),
                            Err(error) => outbox.resolve(resolver, Err(error)),
                        }
                    }
                    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, or a ring's textures, and no longer waits
    /// for the consumer sync that the textures were given back with: the host
    /// may reuse or destroy them. The session's thread polls that sync on its
    /// later turns, for a second at most, and logs a wait that runs out. The
    /// session 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.
    pub fn release_target(&self) -> Result<Completion<()>, 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 = self.shared().clone();
        let capabilities = shared.capabilities;
        if !capabilities.presentation && !capabilities.frame_acquisition {
            check_usable(&shared.state.lock_unpoisoned())?;
            return Err(Error::unsupported("an offscreen session keeps its target"));
        }
        let guard = {
            let mut state = shared.state.lock_unpoisoned();
            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(shared.clone())
            })
        };
        self.settled_operation(move |worker, outbox| {
            let _guard = guard;
            worker.release_target(outbox).map(|pending| ((), pending))
        })
    }

    /// 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_unpoisoned())?;
        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()?;
        self.resize_then(size, None, move |worker, size, scale_factor, outbox| {
            worker.set_surface(size, scale_factor, surface, outbox).map(|()| None)
        })
    }

    /// Ends the frame demands on their way, including those that wait for a
    /// map update, which finish as
    /// [`FrameDisposition::NoUpdate`](super::FrameDisposition), and completes
    /// once the work sent before it has run.
    pub fn barrier(&self) -> Result<Completion<()>, Error> {
        self.operation(|worker, outbox| {
            worker.end_demands(super::FrameDisposition::NoUpdate, 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::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_unpoisoned())?;
        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.
    /// 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_unpoisoned();
            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.
    ///
    /// A caller-driven session lets go of its graphics objects on the host's
    /// thread, at its next service or when its [`DriverWork`] drops.
    ///
    /// It waits for a frame that the session is rendering. Fails with
    /// [`ErrorKind::Busy`](mln_util::ErrorKind) on the session's own thread
    /// while that runs, and with [`ErrorKind::InvalidState`](mln_util::ErrorKind)
    /// once the session is detached, abandoned or released.
    pub fn abandon(&self) -> Result<(), 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. 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_unpoisoned();
            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))?;
        Ok(completion)
    }

    /// Releases the session in any state, without waiting. Dropping the last
    /// handle does the same.
    pub fn dispose(&self) -> Result<(), Error> {
        self.shared().dispose(None)
    }

    /// 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.
    pub fn dispose_keeping_graphics(&self) -> Result<(), Error> {
        self.shared().dispose_keeping_graphics()
    }
}

/// 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_unpoisoned();
        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>,
    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_unpoisoned();
        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: self.shared.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_unpoisoned().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.
    #[allow(clippy::result_large_err)]
    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>,
}

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_unpoisoned();
            state.views = state.views.saturating_sub(1);
            state.views == 0 && std::mem::take(&mut state.viewed_teardown)
        };
        if release {
            worker::post(&self.shared.run_loop, |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_unpoisoned();
        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.run_loop, 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.run_loop, 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>();
};