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
//! What a render session renders into, and how it attaches.

use std::sync::Arc;

use mln_gfx::Window;
pub use mln_gfx::backend::Type as BackendType;
pub use mln_gfx::{
    ConsumerSync, DmaBufFormat, DmaBufPlane, ExportKind, ExportKinds, ExportedImage, ExportedRing, GpuSync, SyncKind,
    SyncKinds,
};
use mln_util::error::Error;
use mln_util::size::Size;

/// A callback that tells the host about something it should act on, such as
/// frame results to drain. It runs on a library thread and must not block.
pub type Wake = Arc<dyn Fn() + Send + Sync>;

/// The thread that runs a render session's graphics work.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Driver {
    /// A thread of the session's own.
    #[default]
    CoreWorker,
    /// The host's graphics thread, which runs the work through the session's
    /// [`DriverWork`](crate::DriverWork).
    CallerThread,
}

/// How a session attaches to its map.
#[derive(Clone, Default)]
pub struct AttachOptions {
    pub driver: Driver,
    /// Runs when frame results wait for
    /// [`drain_frame_results`](crate::RenderSession::drain_frame_results),
    /// and not again until a drain takes them.
    pub frame_wake: Option<Wake>,
    /// Runs when the map has something new to draw, and not again until the
    /// host demands a frame, or while the session has no target.
    pub redraw_wake: Option<Wake>,
    /// Runs when a [`Driver::CallerThread`] session has work for
    /// [`DriverWork::service`](crate::DriverWork::service), and not again
    /// until a service starts.
    pub driver_work_wake: Option<Wake>,
    /// The kinds of sync that the host can wait for before it reads a
    /// borrowed ring's frame. The session picks the one of them that its
    /// target supports and that waits least: see [`SyncKinds::pick`].
    pub producer_sync_kinds: SyncKinds,
}

impl std::fmt::Debug for AttachOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AttachOptions")
            .field("driver", &self.driver)
            .field("frame_wake", &self.frame_wake.is_some())
            .field("redraw_wake", &self.redraw_wake.is_some())
            .field("driver_work_wake", &self.driver_work_wake.is_some())
            .field("producer_sync_kinds", &self.producer_sync_kinds)
            .finish()
    }
}

/// What a render session renders into. The session builds it into the
/// backend's objects on the thread that renders, on `device`, or on a device
/// that [`Device::new`] makes when that is `None`. The device's backend is
/// the session's.
#[derive(Debug)]
pub enum RenderTarget {
    /// An image of `size` logical pixels, rendered at the map's pixel ratio,
    /// whose pixels [`read_texture`](crate::RenderSession::read_texture)
    /// reads.
    Offscreen { size: Size, device: Option<Device> },
    /// A surface of `size` logical pixels, rendered at the map's pixel ratio,
    /// that shows each frame that the session renders. Without a device, a
    /// window renders on the platform's native backend where that takes
    /// windows, and on WebGPU otherwise (on Apple's platforms, whose Metal
    /// takes a layer); a Metal layer renders on Metal.
    ///
    /// A frame finishes as
    /// [`TargetNotReady`](crate::FrameDisposition::TargetNotReady) while the
    /// surface can't show it: while a window is minimized or has no client
    /// area, or the surface has an empty extent or gives no image for it.
    ///
    /// [`release_target`](crate::RenderSession::release_target) lets go of
    /// the surface, and [`set_surface`](crate::RenderSession::set_surface)
    /// gives the session another one.
    Surface { size: Size, surface: Surface, device: Option<Device> },
    /// A ring of the host's textures of `size` logical pixels, on `device`,
    /// whose backend is the ring's. Each frame goes to the host, which
    /// takes it with [`acquire_frame`](crate::RenderSession::acquire_frame)
    /// and gives it back once it has read it. The ring's textures set the
    /// size in pixels, and [`set_ring`](crate::RenderSession::set_ring)
    /// gives the session new ones.
    Borrowed { size: Size, ring: BorrowedRing, device: Device },
    /// A ring of images of `size` logical pixels, rendered at the map's
    /// pixel ratio, that the session allocates on `device` and shares with
    /// the host in a kind of `ring.kinds`: the first, by
    /// [`ExportKind::BY_PREFERENCE`], that the device exports. Without a
    /// device, the session renders on the first of [`Device::new`]'s
    /// backends whose device exports one. Each frame goes to the host, which
    /// takes it with [`acquire_frame`](crate::RenderSession::acquire_frame)
    /// and gives it back once it has read it.
    /// [`resize`](crate::RenderSession::resize) gives the ring images of a
    /// new size, and the frames that the host holds keep theirs.
    Exported { size: Size, ring: ExportedRing, device: Option<Device> },
}

/// A ring of the host's textures, of one backend.
#[derive(Clone, Debug, PartialEq)]
pub struct BorrowedRing {
    pub(crate) kind: RingKind,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum RingKind {
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    Vulkan(mln_vulkan::BorrowedImages),
    #[cfg(all(feature = "metal", target_vendor = "apple"))]
    Metal(mln_mtl::BorrowedTextures),
    #[cfg(all(feature = "gl", not(target_family = "wasm")))]
    Gl(mln_gl::BorrowedTextures),
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    WebGpu(mln_webgpu::BorrowedTextures),
}

/// The host's texture that an acquired frame was rendered into, of `size`
/// pixels.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrameTexture {
    pub size: Size,
    pub kind: FrameTextureKind,
}

/// A [`FrameTexture`]'s backend objects.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FrameTextureKind {
    /// A `VkImage` and its `VkImageView` of a `VkDevice`, in a `VkFormat`,
    /// left in a `VkImageLayout`.
    Vulkan { image: u64, view: u64, device: u64, format: i32, layout: i32 },
    /// An `id<MTLTexture>` of an `id<MTLDevice>`, in an `MTLPixelFormat`.
    Metal { texture: u64, device: u64, pixel_format: u64 },
    /// An OpenGL texture name, of a texture target.
    OpenGl { texture: u32, target: u32 },
    /// A WebGPU texture: the host's at the frame's slot of its ring.
    WebGpu,
    /// An image of an exported ring, and its shared memory.
    Exported(ExportedImage),
}

impl BorrowedRing {
    /// A ring of the host's Vulkan images.
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    #[must_use]
    pub fn vulkan(images: mln_vulkan::BorrowedImages) -> BorrowedRing {
        BorrowedRing { kind: RingKind::Vulkan(images) }
    }

    /// A ring of the host's Metal textures.
    #[cfg(all(feature = "metal", target_vendor = "apple"))]
    pub fn metal(textures: mln_mtl::BorrowedTextures) -> BorrowedRing {
        BorrowedRing { kind: RingKind::Metal(textures) }
    }

    /// A ring of the host's OpenGL textures, in the share group of a device
    /// that wraps the host's context.
    #[cfg(all(feature = "gl", not(target_family = "wasm")))]
    #[must_use]
    pub fn gl(textures: mln_gl::BorrowedTextures) -> BorrowedRing {
        BorrowedRing { kind: RingKind::Gl(textures) }
    }

    /// A ring of the host's WebGPU textures, of the WebGPU device that
    /// renders into them.
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    #[must_use]
    pub fn webgpu(textures: mln_webgpu::BorrowedTextures) -> BorrowedRing {
        BorrowedRing { kind: RingKind::WebGpu(textures) }
    }

    /// The backend whose device renders into the ring.
    #[must_use]
    pub fn backend(&self) -> BackendType {
        match self.kind {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            RingKind::Vulkan(_) => BackendType::Vulkan,
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            RingKind::Metal(_) => BackendType::Metal,
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            RingKind::Gl(_) => BackendType::OpenGL,
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            RingKind::WebGpu(_) => BackendType::WebGPU,
            // The browser build renders into no ring.
            #[allow(unreachable_patterns)]
            _ => BackendType::WebGPU,
        }
    }

    /// How many textures the ring has.
    #[must_use]
    pub fn depth(&self) -> usize {
        match &self.kind {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            RingKind::Vulkan(images) => images.images.len(),
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            RingKind::Metal(textures) => textures.textures.len(),
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            RingKind::Gl(textures) => textures.textures.len(),
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            RingKind::WebGpu(textures) => textures.textures.len(),
            #[allow(unreachable_patterns)]
            _ => 0,
        }
    }

    /// The textures' size in pixels.
    #[must_use]
    pub fn physical_size(&self) -> Size {
        match &self.kind {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            RingKind::Vulkan(images) => images.size,
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            RingKind::Metal(textures) => textures.size,
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            RingKind::Gl(textures) => textures.size,
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            RingKind::WebGpu(textures) => textures.size,
            #[allow(unreachable_patterns)]
            _ => Size::default(),
        }
    }

    /// Whether the texture at `slot` is the one at `other`'s `other_slot`.
    #[cfg_attr(target_family = "wasm", allow(unused_variables))]
    pub(crate) fn same_texture(&self, slot: usize, other: &BorrowedRing, other_slot: usize) -> bool {
        #[allow(unreachable_patterns)]
        match (&self.kind, &other.kind) {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            (RingKind::Vulkan(images), RingKind::Vulkan(others)) => images
                .images
                .get(slot)
                .zip(others.images.get(other_slot))
                .is_some_and(|(image, other)| image.image == other.image),
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            (RingKind::Metal(textures), RingKind::Metal(others)) => {
                textures.textures.get(slot).zip(others.textures.get(other_slot)).is_some_and(|(texture, other)| {
                    std::ptr::eq(
                        mln_mtl::objc2::rc::Retained::as_ptr(texture),
                        mln_mtl::objc2::rc::Retained::as_ptr(other),
                    )
                })
            }
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            (RingKind::Gl(textures), RingKind::Gl(others)) => {
                textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
            }
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            (RingKind::WebGpu(textures), RingKind::WebGpu(others)) => {
                textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
            }
            _ => false,
        }
    }

    /// The texture at `slot`, which sessions on `device` render into.
    #[cfg_attr(target_family = "wasm", allow(unused_variables))]
    #[must_use]
    pub fn texture(&self, slot: usize, device: &Device) -> Option<FrameTexture> {
        #[allow(unreachable_patterns)]
        match (&self.kind, device) {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            (RingKind::Vulkan(images), Device::Vulkan(device)) => {
                use mln_vulkan::ash::vk::Handle as _;
                images.images.get(slot).map(|image| FrameTexture {
                    size: images.size,
                    kind: FrameTextureKind::Vulkan {
                        image: image.image.as_raw(),
                        view: image.view.as_raw(),
                        device: device.raw().device.as_raw(),
                        format: images.format.as_raw(),
                        layout: images.final_layout.as_raw(),
                    },
                })
            }
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            (RingKind::Metal(textures), Device::Metal(device)) => {
                use mln_mtl::objc2::rc::Retained;
                textures.textures.get(slot).map(|texture| FrameTexture {
                    size: textures.size,
                    kind: FrameTextureKind::Metal {
                        texture: Retained::as_ptr(texture) as usize as u64,
                        device: Retained::as_ptr(device.device()) as usize as u64,
                        pixel_format: textures.pixel_format().0 as u64,
                    },
                })
            }
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            (RingKind::Gl(textures), Device::Gl(_)) => textures.textures.get(slot).map(|&texture| FrameTexture {
                size: textures.size,
                // GL_TEXTURE_2D.
                kind: FrameTextureKind::OpenGl { texture, target: 0x0DE1 },
            }),
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            (RingKind::WebGpu(textures), Device::WebGpu(_)) => textures
                .textures
                .get(slot)
                .map(|_| FrameTexture { size: textures.size, kind: FrameTextureKind::WebGpu }),
            _ => None,
        }
    }
}

/// What a surface session presents to.
pub struct Surface {
    pub(crate) kind: SurfaceKind,
}

pub(crate) enum SurfaceKind {
    Window(Arc<dyn Window>),
    #[cfg(all(feature = "metal", target_vendor = "apple"))]
    MetalLayer(mln_mtl::Layer),
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    Wgpu(wgpu::Surface<'static>),
    /// The host's `VkSurfaceKHR`, of the instance of the session's device.
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    Vulkan(mln_vulkan::HostSurface),
    /// The host's `EGLSurface`, of the display of the session's device.
    #[cfg(all(feature = "gl", not(target_family = "wasm")))]
    Gl(mln_gl::HostSurface),
}

impl Surface {
    /// A window, which the session makes its surface on. The window must
    /// outlive the session's use of it: until a
    /// [`set_surface`](crate::RenderSession::set_surface) or
    /// [`release_target`](crate::RenderSession::release_target) replaces it,
    /// or the session detaches or is abandoned.
    pub fn window(window: impl Window + 'static) -> Surface {
        Surface { kind: SurfaceKind::Window(Arc::new(window)) }
    }

    /// A `CAMetalLayer`, which a session on Metal presents to. It must
    /// outlive the session's use of it, as a window must.
    #[cfg(all(feature = "metal", target_vendor = "apple"))]
    #[must_use]
    pub fn metal_layer(layer: mln_mtl::Layer) -> Surface {
        Surface { kind: SurfaceKind::MetalLayer(layer) }
    }

    /// A surface that the host made with the instance of the WebGPU
    /// [`Device`] that the session renders on.
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    pub fn wgpu(surface: wgpu::Surface<'static>) -> Surface {
        Surface { kind: SurfaceKind::Wgpu(surface) }
    }

    /// The host's `VkSurfaceKHR`, which a session on the Vulkan device
    /// whose instance made it presents to. The surface stays the host's.
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    #[must_use]
    pub fn vulkan(surface: mln_vulkan::HostSurface) -> Surface {
        Surface { kind: SurfaceKind::Vulkan(surface) }
    }

    /// The host's `EGLSurface`, which a session on the OpenGL device that
    /// wraps the host's context presents to. The surface stays the host's.
    #[cfg(all(feature = "gl", not(target_family = "wasm")))]
    #[must_use]
    pub fn gl(surface: mln_gl::HostSurface) -> Surface {
        Surface { kind: SurfaceKind::Gl(surface) }
    }

    /// The backend of the device whose instance or share group a host's
    /// surface is of, or `None` for a window or layer.
    #[must_use]
    pub fn host_backend(&self) -> Option<BackendType> {
        match self.kind {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            SurfaceKind::Vulkan(_) => Some(BackendType::Vulkan),
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            SurfaceKind::Gl(_) => Some(BackendType::OpenGL),
            #[allow(unreachable_patterns)]
            _ => None,
        }
    }

    /// The surface again, if it is a window, or a surface that a new
    /// backend presents to.
    pub(crate) fn clone_window(&self) -> Option<Surface> {
        match &self.kind {
            SurfaceKind::Window(window) => Some(Surface { kind: SurfaceKind::Window(window.clone()) }),
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            SurfaceKind::Gl(surface) => Some(Surface { kind: SurfaceKind::Gl(*surface) }),
            #[allow(unreachable_patterns)]
            _ => None,
        }
    }

    /// Fails with [`ErrorKind::InvalidState`](mln_util::ErrorKind) for a
    /// window that has no area to present to: a minimized Win32 window, or
    /// one whose client area is empty ([`mln_gfx::window::has_area`]).
    pub(crate) fn check_area(&self) -> Result<(), Error> {
        if let SurfaceKind::Window(window) = &self.kind
            && !mln_gfx::window::has_area(&**window)
        {
            return Err(Error::invalid_state("the window is minimized or has no client area"));
        }
        Ok(())
    }

    /// The window, for a surface of one.
    pub(crate) fn host_window(&self) -> Option<Arc<dyn Window>> {
        match &self.kind {
            SurfaceKind::Window(window) => Some(window.clone()),
            #[allow(unreachable_patterns)]
            _ => None,
        }
    }

    /// Whether the session can make the surface without the host's device.
    pub(crate) fn needs_no_device(&self) -> bool {
        match self.kind {
            SurfaceKind::Window(_) => true,
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            SurfaceKind::MetalLayer(_) => true,
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            SurfaceKind::Wgpu(_) => false,
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            SurfaceKind::Vulkan(_) => false,
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            SurfaceKind::Gl(_) => false,
        }
    }
}

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

/// A GPU that sessions on many maps and threads share: one backend's
/// device. Sessions on it keep working after the last handle drops, until
/// they detach.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Device {
    /// A WebGPU device: the library's, through wgpu, or the host's
    /// ([`Device::webgpu`]).
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    WebGpu(mln_webgpu::Device),
    /// A Metal device and command queue.
    #[cfg(all(feature = "metal", target_vendor = "apple"))]
    Metal(mln_mtl::Device),
    /// A Vulkan GPU.
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    Vulkan(mln_vulkan::Device),
    /// An OpenGL share group, or the one context that its sessions take
    /// turns in.
    #[cfg(all(feature = "gl", not(target_family = "wasm")))]
    Gl(mln_gl::Device),
}

/// How to create a [`Device`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DeviceOptions {
    /// The GPU to use. `None` picks the default one.
    pub adapter: Option<Adapter>,
    /// The API that an OpenGL device renders with. Only Linux's and
    /// FreeBSD's EGL renders desktop OpenGL. Other backends ignore it.
    pub gl_api: GlApi,
    /// Whether the sessions of an OpenGL device take turns in one context
    /// instead of having one each. Other backends ignore it.
    pub gl_single_context: bool,
}

/// The OpenGL API that an OpenGL device renders with.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum GlApi {
    /// OpenGL ES 3.0 or later.
    #[default]
    Gles,
    /// Desktop OpenGL 3.3 or later.
    Gl,
}

/// A GPU, by what names it to a backend.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Adapter {
    /// An index into the backend's list of GPUs.
    Index(usize),
    /// The GPU's 16-byte UUID, as Vulkan's
    /// `VkPhysicalDeviceIDProperties.deviceUUID` reports it.
    Uuid([u8; 16]),
    /// The GPU's 8-byte LUID, as Vulkan's
    /// `VkPhysicalDeviceIDProperties.deviceLUID` reports it: on Windows, its
    /// DXGI adapter's.
    Luid([u8; 8]),
}

/// A kind of window or layer that a session presents to.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SurfaceType {
    /// A `CAMetalLayer`.
    MetalLayer,
    /// A Win32 window.
    Win32,
    /// An X11 window, through Xlib.
    Xlib,
    /// An X11 window, through XCB.
    Xcb,
    /// A Wayland surface.
    Wayland,
    /// An Android `ANativeWindow`.
    AndroidNdk,
    /// An `OpenHarmony` `OHNativeWindow`.
    OhosNdk,
}

impl SurfaceType {
    /// Whether a device of `backend` presents to the surfaces of this type
    /// on this platform, in this build.
    #[must_use]
    pub fn presents_with(self, backend: BackendType) -> bool {
        let windowing = match self {
            SurfaceType::MetalLayer => false,
            SurfaceType::Win32 => cfg!(windows),
            SurfaceType::Xlib | SurfaceType::Xcb | SurfaceType::Wayland => {
                cfg!(all(unix, not(target_vendor = "apple"), not(target_os = "android"), not(target_env = "ohos")))
            }
            SurfaceType::AndroidNdk => cfg!(target_os = "android"),
            SurfaceType::OhosNdk => cfg!(target_env = "ohos"),
        };
        match backend {
            BackendType::Metal => {
                cfg!(all(feature = "metal", target_vendor = "apple")) && self == SurfaceType::MetalLayer
            }
            BackendType::Vulkan => {
                cfg!(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm")))) && windowing
            }
            BackendType::OpenGL => {
                cfg!(all(feature = "gl", unix, not(target_vendor = "apple")))
                    && windowing
                    && matches!(
                        self,
                        SurfaceType::Xlib | SurfaceType::Wayland | SurfaceType::AndroidNdk | SurfaceType::OhosNdk
                    )
            }
            // wgpu's Vulkan backend makes no OpenHarmony surface, and this
            // build has no wgpu GLES backend.
            BackendType::WebGPU => {
                cfg!(all(feature = "webgpu", not(target_family = "wasm"))) && windowing && self != SurfaceType::OhosNdk
            }
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// The Vulkan instance extension that makes the surfaces of this type.
    #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
    fn vulkan_extension(self) -> Option<&'static std::ffi::CStr> {
        use mln_vulkan::ash::khr;
        match self {
            SurfaceType::MetalLayer => None,
            SurfaceType::Win32 => Some(khr::win32_surface::NAME),
            SurfaceType::Xlib => Some(khr::xlib_surface::NAME),
            SurfaceType::Xcb => Some(khr::xcb_surface::NAME),
            SurfaceType::Wayland => Some(khr::wayland_surface::NAME),
            SurfaceType::AndroidNdk => Some(khr::android_surface::NAME),
            SurfaceType::OhosNdk => Some(mln_vulkan::device::OHOS_SURFACE),
        }
    }

    /// Whether a device of any backend of this build presents to the
    /// surfaces of this type.
    #[must_use]
    pub fn presents_with_any(self) -> bool {
        [BackendType::Metal, BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
            .into_iter()
            .any(|backend| self.presents_with(backend))
    }

    /// Whether a session of this build presents to the surfaces of this
    /// type without the host's device: on the device that a surface session
    /// makes when it gets none.
    #[must_use]
    pub fn presents_without_device(self) -> bool {
        match self {
            SurfaceType::MetalLayer => self.presents_with(BackendType::Metal),
            _ if cfg!(target_vendor = "apple") => self.presents_with(BackendType::WebGPU),
            _ => [BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
                .into_iter()
                .any(|backend| self.presents_with(backend)),
        }
    }
}

/// Whether a backend that failed with `error` is missing, so the next one
/// may take its place.
#[cfg(not(target_family = "wasm"))]
fn missing(error: &Error) -> bool {
    error.kind == mln_util::ErrorKind::Unsupported
}

impl Device {
    /// The library's device, on the platform's native backend: Metal on
    /// Apple's platforms, and Vulkan elsewhere, or OpenGL where there is no
    /// Vulkan. WebGPU (wgpu) takes the place of a native backend that isn't
    /// there. A backend that can't name the GPU that `options` names is
    /// skipped: OpenGL names none, and only Vulkan names one by UUID or LUID.
    /// So is OpenGL where it can't render with `options.gl_api`.
    ///
    /// Fails with [`ErrorKind::NotFound`](mln_util::ErrorKind) when
    /// the options name no GPU, with
    /// [`ErrorKind::Unsupported`](mln_util::ErrorKind) when no backend has a
    /// GPU or in the browser, where a session renders on the page's device,
    /// and with [`ErrorKind::Native`](mln_util::ErrorKind) when the GPU
    /// can't create the device.
    pub fn new(options: DeviceOptions) -> Result<Device, Error> {
        #[cfg(target_family = "wasm")]
        {
            let _ = options;
            Err(Error::unsupported("the browser has no library devices"))
        }
        #[cfg(not(target_family = "wasm"))]
        {
            let mut error = Error::unsupported("this build has no backend");
            for backend in Self::native_order() {
                match Self::with_backend(*backend, options) {
                    Ok(device) => return Ok(device),
                    Err(e) if missing(&e) => error = e,
                    Err(e) => return Err(e),
                }
            }
            Err(error)
        }
    }

    /// The library's device on `backend`.
    ///
    /// Fails as [`new`](Self::new) does, and with
    /// [`ErrorKind::Unsupported`](mln_util::ErrorKind) for a backend that
    /// isn't in this build or on this platform.
    pub fn with_backend(backend: BackendType, options: DeviceOptions) -> Result<Device, Error> {
        let DeviceOptions { adapter, gl_api, gl_single_context } = options;
        // Only Vulkan names its GPUs by ID here.
        let index = |adapter: Option<Adapter>| match adapter {
            None => Ok(None),
            Some(Adapter::Index(index)) => Ok(Some(index)),
            Some(_) => Err(Error::unsupported(format!("{backend:?} names no GPU by UUID or LUID"))),
        };
        match backend {
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            BackendType::WebGPU => mln_webgpu::Device::new(index(adapter)?).map(Device::WebGpu),
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            BackendType::Metal => mln_mtl::Device::new(index(adapter)?).map(Device::Metal),
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            BackendType::Vulkan => match adapter {
                Some(Adapter::Uuid(uuid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Uuid(uuid)),
                Some(Adapter::Luid(luid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Luid(luid)),
                _ => mln_vulkan::Device::new(index(adapter)?),
            }
            .map(Device::Vulkan),
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            BackendType::OpenGL => {
                if adapter.is_some() {
                    return Err(Error::unsupported("OpenGL picks no GPU"));
                }
                let api = match gl_api {
                    GlApi::Gles => mln_gl::Api::Gles,
                    GlApi::Gl => mln_gl::Api::Gl,
                };
                let options = mln_gl::DeviceOptions { api, single_context: gl_single_context };
                mln_gl::Device::with_options(options).map(Device::Gl)
            }
            #[allow(unreachable_patterns)]
            _ => {
                let _ = (index, adapter, gl_api, gl_single_context);
                Err(Error::unsupported(format!("this build has no {backend:?} backend")))
            }
        }
    }

    /// The kinds of sync with which the host can wait for a frame of `ring`
    /// on the device (the producer's), and with which a session waits for
    /// the host's reads (the consumer's).
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) when the
    /// device can't render into the ring: one of another backend, or one the
    /// device lacks a feature for.
    pub fn borrowed_sync_kinds(&self, ring: &BorrowedRing) -> Result<(SyncKinds, SyncKinds), Error> {
        #[allow(unreachable_patterns)]
        match (self, &ring.kind) {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            (Device::Vulkan(device), RingKind::Vulkan(images)) => {
                images.check(device)?;
                Ok(device.sync_kinds())
            }
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            (Device::Metal(device), RingKind::Metal(textures)) => {
                textures.check(device)?;
                Ok(mln_mtl::BorrowedTextures::sync_kinds(device))
            }
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            (Device::Gl(device), RingKind::Gl(_)) => {
                if !device.is_host() {
                    return Err(Error::unsupported(
                        "an OpenGL ring renders on a device in the share group of the host's context",
                    ));
                }
                Ok(mln_gl::BorrowedTextures::sync_kinds())
            }
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            (Device::WebGpu(_), RingKind::WebGpu(textures)) => {
                textures.check()?;
                Ok(mln_webgpu::BorrowedTextures::sync_kinds())
            }
            _ => Err(Error::unsupported(format!(
                "a {:?} device renders into no ring of {:?} textures",
                self.backend(),
                ring.backend()
            ))),
        }
    }

    /// The kinds of exported ring that sessions on the device share their
    /// images in: those that the device has exported an image of.
    #[must_use]
    pub fn export_kinds(&self) -> ExportKinds {
        match self {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(device) => device.export_kinds(),
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            Device::Metal(device) => mln_mtl::exported_target::export_kinds(device),
            #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
            Device::Gl(device) => mln_gl::exported_target::export_kinds(device),
            #[allow(unreachable_patterns)]
            _ => ExportKinds::NONE,
        }
    }

    /// The kind that a session on the device shares `ring`'s images in, and
    /// the kinds of sync with which the host can wait for a frame (the
    /// producer's) and a session for the host's reads (the consumer's).
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) when the
    /// device exports none of the ring's kinds.
    pub fn export_plan(&self, ring: &ExportedRing) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
        #[allow(unreachable_patterns)]
        match self {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(device) => {
                let kind = mln_vulkan::exported_target::plan(device, ring)?;
                let (producer, consumer) = mln_vulkan::exported_target::sync_kinds(device);
                Ok((kind, producer, consumer))
            }
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            Device::Metal(device) => {
                let kind = mln_mtl::exported_target::plan(device, ring)?;
                let (producer, consumer) = mln_mtl::exported_target::sync_kinds(device);
                Ok((kind, producer, consumer))
            }
            #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
            Device::Gl(device) => {
                let kind = mln_gl::exported_target::plan(device, ring)?;
                let (producer, consumer) = mln_gl::exported_target::sync_kinds();
                Ok((kind, producer, consumer))
            }
            _ => {
                let _ = ring;
                Err(Error::unsupported(format!("a {:?} device exports no ring", self.backend())))
            }
        }
    }

    /// The kinds of sync with which the host can wait for an exported
    /// ring's frame (the producer's) and a session for the host's reads (the
    /// consumer's), on a device that exports a ring.
    #[must_use]
    pub fn export_sync_kinds(&self) -> (SyncKinds, SyncKinds) {
        if self.export_kinds().is_empty() {
            return (SyncKinds::NONE, SyncKinds::NONE);
        }
        #[allow(unreachable_patterns)]
        match self {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(device) => mln_vulkan::exported_target::sync_kinds(device),
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            Device::Metal(device) => mln_mtl::exported_target::sync_kinds(device),
            #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
            Device::Gl(_) => mln_gl::exported_target::sync_kinds(),
            _ => (SyncKinds::NONE, SyncKinds::NONE),
        }
    }

    /// The library's device for `ring`: on the first of [`new`](Self::new)'s
    /// backends whose device exports one of the ring's kinds.
    ///
    /// Fails with [`ErrorKind::Unsupported`](mln_util::ErrorKind) when no
    /// backend's device does, and as [`new`](Self::new) does.
    pub fn for_exported_ring(ring: &ExportedRing) -> Result<Device, Error> {
        #[cfg(target_family = "wasm")]
        {
            let _ = ring;
            Err(Error::unsupported("the browser has no library devices"))
        }
        #[cfg(not(target_family = "wasm"))]
        {
            let mut error = Error::unsupported("no backend of this build exports the ring's kinds of image");
            for backend in Self::native_order() {
                match Self::with_backend(*backend, DeviceOptions::default()) {
                    Ok(device) => match device.export_plan(ring) {
                        Ok(_) => return Ok(device),
                        Err(e) => error = e,
                    },
                    Err(e) if missing(&e) => {}
                    Err(e) => return Err(e),
                }
            }
            Err(error)
        }
    }

    /// Whether sessions on the device present to the host's own surfaces:
    /// a Vulkan device's `VkSurfaceKHR`s, or the `EGLSurface`s of an OpenGL
    /// device that wraps the host's context.
    #[must_use]
    pub fn presents_to_host_surfaces(&self) -> bool {
        match self {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(device) => device.presents(),
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            Device::Gl(device) => device.is_host(),
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// Whether sessions on the device present to the surfaces of
    /// `surface`'s type: a Vulkan device's instance needs the type's surface
    /// extension, which the loader may lack.
    #[must_use]
    pub fn presents_to(&self, surface: SurfaceType) -> bool {
        if !surface.presents_with(self.backend()) {
            return false;
        }
        match self {
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(device) => surface.vulkan_extension().is_some_and(|name| device.presents_through(name)),
            #[allow(unreachable_patterns)]
            _ => true,
        }
    }

    /// The backends [`new`](Self::new) tries, in order.
    #[cfg(not(target_family = "wasm"))]
    fn native_order() -> &'static [BackendType] {
        if cfg!(target_vendor = "apple") {
            &[BackendType::Metal, BackendType::WebGPU]
        } else {
            &[BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
        }
    }

    /// The host's WebGPU device and queue, which `instance` made. The
    /// session configures its surfaces with `format`, which each surface
    /// must support.
    #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
    #[must_use]
    pub fn webgpu(
        instance: wgpu::Instance,
        device: wgpu::Device,
        queue: wgpu::Queue,
        format: wgpu::TextureFormat,
    ) -> Device {
        Device::WebGpu(mln_webgpu::Device::from_wgpu(Some(instance), device, queue, Some(format)))
    }

    /// The backend the device is of.
    #[must_use]
    pub fn backend(&self) -> BackendType {
        match *self {
            #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
            Device::WebGpu(_) => BackendType::WebGPU,
            #[cfg(all(feature = "metal", target_vendor = "apple"))]
            Device::Metal(_) => BackendType::Metal,
            #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
            Device::Vulkan(_) => BackendType::Vulkan,
            #[cfg(all(feature = "gl", not(target_family = "wasm")))]
            Device::Gl(_) => BackendType::OpenGL,
        }
    }
}