sk-tree 0.4.0

Skathi's retained node tree and the renderer's host functions
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
//! The Metal half of a surface on macOS: frames are copied on a command queue of the surface's own
//! on the renderer's device, after a GPU wait on the producer's shared event, into one of a few
//! surface textures. The element's tick hands a finished one to the atlas
//! (`write_image_tile_metal`), so the renderer's queue never waits on a producer. A half-float
//! texture is first mapped into an 8-bit one of its own, on the same queue, when the tick asks.

use std::any::Any;
use std::ffi::c_void;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{mpsc, Arc, Mutex, MutexGuard, OnceLock};
use std::time::Duration;

use block2::RcBlock;
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::Message;
use objc2_core_foundation::{CFRetained, CFString};
use objc2_core_video::{
    kCVPixelFormatType_32BGRA, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
    kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_64RGBAHalf,
    kCVReturnSuccess, CVPixelBuffer, CVPixelBufferGetBaseAddress, CVPixelBufferGetBytesPerRow,
    CVPixelBufferGetHeight, CVPixelBufferGetIOSurface, CVPixelBufferGetPixelFormatType,
    CVPixelBufferGetWidth, CVPixelBufferLockBaseAddress, CVPixelBufferLockFlags,
    CVPixelBufferUnlockBaseAddress,
};
use objc2_foundation::NSString;
use objc2_io_surface::IOSurfaceRef;
use objc2_metal::{
    MTLBlitCommandEncoder, MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue,
    MTLComputeCommandEncoder, MTLComputePipelineState, MTLDevice, MTLLibrary, MTLOrigin,
    MTLPixelFormat, MTLResource, MTLSharedEvent, MTLSharedEventHandle, MTLSharedEventListener,
    MTLSize, MTLStorageMode, MTLTexture, MTLTextureDescriptor, MTLTextureType, MTLTextureUsage,
};

use super::{check_format, check_size, lock, Frame, State, SurfaceError, SurfaceFormat};

/// More in flight than this means the atlas is not keeping up; the present is refused.
const MAX_SLOTS: usize = 4;

const BGRA: u32 = kCVPixelFormatType_32BGRA;
const NV12_VIDEO: u32 = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
const NV12_FULL: u32 = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
const RGHA: u32 = kCVPixelFormatType_64RGBAHalf;

type Texture = ProtocolObject<dyn MTLTexture>;
type SharedEvent = ProtocolObject<dyn MTLSharedEvent>;

struct Gpu {
    device: Retained<ProtocolObject<dyn MTLDevice>>,
    renderer_queue: Retained<ProtocolObject<dyn MTLCommandQueue>>,
    queue: Retained<ProtocolObject<dyn MTLCommandQueue>>,
    /// Signalled after each copy: the release event the producer waits on.
    event: Retained<SharedEvent>,
    generation: u64,
    /// The last value signalled on `event`; held across each commit so values rise in queue order.
    next: Mutex<u64>,
    nv12: OnceLock<Result<Pipeline, String>>,
    rgba16f: OnceLock<Result<Pipeline, String>>,
    _lease: Arc<dyn Any + Send + Sync>,
}

// SAFETY: Metal devices, queues, events and pipeline states are thread-safe; command buffers are
// encoded under `next`'s mutex.
unsafe impl Send for Gpu {}
unsafe impl Sync for Gpu {}

static GPU: Mutex<Option<Arc<Gpu>>> = Mutex::new(None);

fn platform(what: &str) -> SurfaceError {
    SurfaceError::Platform(format!("{what} failed"))
}

fn gpu() -> Result<Arc<Gpu>, SurfaceError> {
    let lease = gpui::renderer_metal_device()
        .ok_or_else(|| SurfaceError::NoDevice("no window has drawn yet".into()))?;
    let mut cached = lock(&GPU);
    if let Some(gpu) = cached.as_ref().filter(|g| g.generation == lease.generation) {
        return Ok(gpu.clone());
    }
    // SAFETY: the lease keeps both alive.
    let device = unsafe { Retained::retain(lease.device.cast::<ProtocolObject<dyn MTLDevice>>()) }
        .ok_or_else(|| SurfaceError::NoDevice("the renderer returned a null device".into()))?;
    // SAFETY: as above.
    let renderer_queue =
        unsafe { Retained::retain(lease.queue.cast::<ProtocolObject<dyn MTLCommandQueue>>()) }
            .ok_or_else(|| SurfaceError::NoDevice("the renderer returned a null queue".into()))?;
    let queue = device
        .newCommandQueue()
        .ok_or_else(|| platform("creating the surface queue"))?;
    let event = device
        .newSharedEvent()
        .ok_or_else(|| platform("creating the release event"))?;
    let gpu = Arc::new(Gpu {
        device,
        renderer_queue,
        queue,
        event,
        generation: lease.generation,
        next: Mutex::new(0),
        nv12: OnceLock::new(),
        rgba16f: OnceLock::new(),
        _lease: lease.keep_alive,
    });
    *cached = Some(gpu.clone());
    Ok(gpu)
}

pub(super) fn forget_device() {
    lock(&GPU).take();
}

/// Called each frame: a renderer on another device drops every surface's GPU frame.
pub(super) fn watch_device() {
    static SEEN: AtomicU64 = AtomicU64::new(0);
    let Some(lease) = gpui::renderer_metal_device() else {
        return;
    };
    let before = SEEN.swap(lease.generation, Ordering::Relaxed);
    if before != 0 && before != lease.generation {
        super::device_changed();
    }
}

pub(crate) struct Device(Arc<Gpu>);

impl Device {
    pub(crate) fn as_raw(&self) -> *mut c_void {
        Retained::as_ptr(&self.0.device).cast_mut().cast()
    }

    pub(crate) fn queue(&self) -> *mut c_void {
        Retained::as_ptr(&self.0.renderer_queue).cast_mut().cast()
    }

    pub(crate) fn generation(&self) -> u64 {
        self.0.generation
    }
}

pub(super) fn device(state: &Mutex<State>) -> Result<Device, SurfaceError> {
    let gpu = gpu()?;
    lock(state).metal.handed = gpu.generation;
    Ok(Device(gpu))
}

pub(crate) struct Event(Arc<Gpu>);

impl Event {
    pub(crate) fn as_raw(&self) -> *mut c_void {
        Retained::as_ptr(&self.0.event).cast_mut().cast()
    }

    pub(crate) fn generation(&self) -> u64 {
        self.0.generation
    }

    pub(crate) fn signaled(&self) -> u64 {
        self.0.event.signaledValue()
    }

    pub(crate) fn wait(&self, value: u64, timeout: Duration) -> bool {
        if self.signaled() >= value {
            return true;
        }
        let (tx, rx) = mpsc::channel::<()>();
        let tx = Mutex::new(Some(tx));
        let listener = MTLSharedEventListener::new();
        let block = RcBlock::new(move |_: NonNull<SharedEvent>, _: u64| {
            if let Some(tx) = lock(&tx).take() {
                let _ = tx.send(());
            }
        });
        // SAFETY: Metal retains the listener and the block until it runs.
        unsafe {
            self.0
                .event
                .notifyListener_atValue_block(&listener, value, RcBlock::as_ptr(&block));
        }
        rx.recv_timeout(timeout).is_ok() || self.signaled() >= value
    }
}

pub(super) fn release_event() -> Result<Event, SurfaceError> {
    gpu().map(Event)
}

pub(crate) struct SlotTexture {
    texture: Retained<Texture>,
    /// A half-float slot's 8-bit picture and the release value its last mapping signals (0 until
    /// mapped for the frame in the slot).
    mapped: Option<(Retained<Texture>, Mutex<u64>)>,
}

// SAFETY: a Metal texture is thread-safe; the value is a mutex.
unsafe impl Send for SlotTexture {}
unsafe impl Sync for SlotTexture {}

impl SlotTexture {
    fn new(gpu: &Gpu, size: (u32, u32), format: SurfaceFormat) -> Result<Self, SurfaceError> {
        let texture = create_texture(gpu, size, pixel_format(format))?;
        let mapped = match format {
            SurfaceFormat::Bgra8 => None,
            SurfaceFormat::Rgba16Float => {
                rgba16f_pipeline(gpu)?;
                Some((
                    create_texture(gpu, size, MTLPixelFormat::BGRA8Unorm)?,
                    Mutex::new(0),
                ))
            }
        };
        Ok(Self { texture, mapped })
    }

    fn mapped(&self) -> Option<MutexGuard<'_, u64>> {
        self.mapped.as_ref().map(|(_, value)| lock(value))
    }
}

struct Slot {
    texture: Arc<SlotTexture>,
    size: (u32, u32),
    format: SurfaceFormat,
    generation: u64,
    written: u64,
}

#[derive(Default)]
pub(crate) struct SurfaceGpu {
    slots: Vec<Slot>,
    current: Option<usize>,
    gpu: Option<Arc<Gpu>>,
    /// The generation `metal_device` last handed out: a texture from another device after that one
    /// changed means the device changed, not that it is wrong.
    handed: u64,
}

// SAFETY: see `Gpu`.
unsafe impl Send for SurfaceGpu {}

/// A frame for the atlas: the texture, and the release value its copy (or its mapping, once
/// [`Ready::for_atlas`] made it) signals.
pub(crate) struct Ready {
    texture: Arc<SlotTexture>,
    gpu: Arc<Gpu>,
    value: u64,
    size: (u32, u32),
    mapped: bool,
}

// SAFETY: see `Gpu`.
unsafe impl Send for Ready {}
unsafe impl Sync for Ready {}

impl Ready {
    /// Whether the copy into the texture has run; until then the atlas must not read it.
    pub(crate) fn done(&self) -> bool {
        self.gpu.event.signaledValue() >= self.value
    }

    /// What the atlas copies: the texture, or for a half-float frame its 8-bit mapping, encoded on
    /// the surface's queue the first time it is asked for. The atlas copy waits on [`Self::ready`].
    pub(crate) fn for_atlas(self) -> Result<Ready, SurfaceError> {
        let Some(mut mapped) = self.texture.mapped() else {
            return Ok(self);
        };
        if *mapped == 0 {
            *mapped = map(&self.gpu, &self.texture, self.size)?;
        }
        let value = *mapped;
        drop(mapped);
        Ok(Ready {
            value,
            mapped: true,
            ..self
        })
    }

    /// A half-float frame, whose own texture an HDR window copies instead of its mapping.
    pub(crate) fn is_half_float(&self) -> bool {
        self.texture.mapped.is_some()
    }

    pub(crate) fn handle(&self) -> gpui::GpuTextureHandle {
        let texture = match (&self.texture.mapped, self.mapped) {
            (Some((mapped, _)), true) => mapped,
            _ => &self.texture.texture,
        };
        gpui::GpuTextureHandle(Retained::as_ptr(texture).cast_mut().cast())
    }

    /// The release event and the value the atlas copy waits for.
    pub(crate) fn ready(&self) -> (*mut c_void, u64) {
        (
            Retained::as_ptr(&self.gpu.event).cast_mut().cast(),
            self.value,
        )
    }
}

impl SurfaceGpu {
    pub(crate) fn ready(&self) -> Option<Ready> {
        let slot = &self.slots[self.current?];
        Some(Ready {
            texture: slot.texture.clone(),
            gpu: self.gpu.clone()?,
            value: slot.written,
            size: slot.size,
            mapped: false,
        })
    }

    pub(crate) fn forget(&mut self) {
        self.slots.clear();
        self.current = None;
        self.gpu = None;
    }

    /// A slot neither shown as the frame nor still copied into or read by the atlas (both hold
    /// its `Arc` until their command buffer completes).
    fn free_slot(
        &mut self,
        gpu: &Gpu,
        size: (u32, u32),
        format: SurfaceFormat,
    ) -> Result<usize, SurfaceError> {
        if self.slots.iter().any(|s| s.generation != gpu.generation) {
            self.forget();
        }
        let free = |(i, s): &(usize, &Slot)| {
            Some(*i) != self.current && Arc::strong_count(&s.texture) == 1
        };
        let fits = |s: &Slot| s.size == size && s.format == format;
        let pick = self
            .slots
            .iter()
            .enumerate()
            .filter(free)
            .min_by_key(|(_, s)| !fits(s))
            .map(|(i, _)| i);
        let slot = || -> Result<Slot, SurfaceError> {
            Ok(Slot {
                texture: Arc::new(SlotTexture::new(gpu, size, format)?),
                size,
                format,
                generation: gpu.generation,
                written: 0,
            })
        };
        match pick {
            Some(i) if fits(&self.slots[i]) => Ok(i),
            Some(i) => {
                self.slots[i] = slot()?;
                Ok(i)
            }
            None if self.slots.len() < MAX_SLOTS => {
                self.slots.push(slot()?);
                Ok(self.slots.len() - 1)
            }
            None => Err(SurfaceError::Busy),
        }
    }
}

fn pixel_format(format: SurfaceFormat) -> MTLPixelFormat {
    match format {
        SurfaceFormat::Bgra8 => MTLPixelFormat::BGRA8Unorm,
        SurfaceFormat::Rgba16Float => MTLPixelFormat::RGBA16Float,
    }
}

fn descriptor(
    format: MTLPixelFormat,
    (width, height): (u32, u32),
) -> Retained<MTLTextureDescriptor> {
    let desc = MTLTextureDescriptor::new();
    desc.setTextureType(MTLTextureType::Type2D);
    desc.setPixelFormat(format);
    // SAFETY: both sides were checked against the renderer's texture limit.
    unsafe {
        desc.setWidth(width as usize);
        desc.setHeight(height as usize);
    }
    desc.setUsage(MTLTextureUsage::ShaderRead);
    desc
}

fn create_texture(
    gpu: &Gpu,
    size: (u32, u32),
    format: MTLPixelFormat,
) -> Result<Retained<Texture>, SurfaceError> {
    let desc = descriptor(format, size);
    desc.setUsage(MTLTextureUsage::ShaderRead | MTLTextureUsage::ShaderWrite);
    desc.setStorageMode(MTLStorageMode::Private);
    gpu.device
        .newTextureWithDescriptor(&desc)
        .ok_or_else(|| platform("creating the surface texture"))
}

/// The texture's size and format, and the texture itself or a `BGRA8Unorm` view of an `_sRGB` one
/// (a copy keeps the format).
fn describe(
    texture: &Texture,
) -> Result<(Retained<Texture>, (u32, u32), SurfaceFormat), SurfaceError> {
    let format = texture.pixelFormat();
    let surface_format = match format {
        MTLPixelFormat::BGRA8Unorm | MTLPixelFormat::BGRA8Unorm_sRGB => SurfaceFormat::Bgra8,
        MTLPixelFormat::RGBA16Float => SurfaceFormat::Rgba16Float,
        _ => {
            return Err(SurfaceError::Format(format!(
                "a texture in MTLPixelFormat {}: a surface takes a 2D BGRA8Unorm (or \
                 BGRA8Unorm_sRGB) texture, or RGBA16Float on a surface set to Rgba16Float",
                format.0
            )))
        }
    };
    if texture.textureType() != MTLTextureType::Type2D {
        return Err(SurfaceError::Format(
            "a texture that is not 2D: a surface takes a 2D texture".into(),
        ));
    }
    if texture.sampleCount() != 1 {
        return Err(SurfaceError::Format(
            "a multisampled texture: resolve it to a single-sampled texture first".into(),
        ));
    }
    let size = (
        u32::try_from(texture.width()).unwrap_or(u32::MAX),
        u32::try_from(texture.height()).unwrap_or(u32::MAX),
    );
    check_size(size.0, size.1)?;
    let texture = if format != MTLPixelFormat::BGRA8Unorm_sRGB {
        texture.retain()
    } else {
        texture
            .newTextureViewWithPixelFormat(MTLPixelFormat::BGRA8Unorm)
            .ok_or_else(|| {
                SurfaceError::Format(
                    "a BGRA8Unorm_sRGB texture Metal gives no BGRA8Unorm view of: create it with \
                     MTLTextureUsagePixelFormatView"
                        .into(),
                )
            })?
    };
    Ok((texture, size, surface_format))
}

const RGBA16F_KERNEL: &str = r#"
#include <metal_stdlib>
using namespace metal;

kernel void rgba16f_to_bgra(texture2d<float, access::read> frame [[texture(0)]],
                            texture2d<float, access::write> out [[texture(1)]],
                            uint2 at [[thread_position_in_grid]]) {
    if (at.x >= out.get_width() || at.y >= out.get_height()) return;
    float4 texel = frame.read(at);
    float3 c = saturate(texel.rgb);
    float3 srgb = select(1.055 * precise::pow(c, float3(1.0 / 2.4)) - 0.055, c * 12.92,
                         c < 0.0031308);
    out.write(float4(srgb, saturate(texel.a)), at);
}
"#;

fn rgba16f_pipeline(gpu: &Gpu) -> Result<Pipeline, SurfaceError> {
    kernel(&gpu.rgba16f, &gpu.device, RGBA16F_KERNEL, "rgba16f_to_bgra")
}

/// Maps a half-float slot into its 8-bit texture on the surface's queue, after the copy into it.
/// Returns the release value the mapping signals.
fn map(gpu: &Arc<Gpu>, slot: &Arc<SlotTexture>, size: (u32, u32)) -> Result<u64, SurfaceError> {
    let (mapped, _) = slot.mapped.as_ref().expect("a half-float slot");
    let pipeline = rgba16f_pipeline(gpu)?;
    let mut next = lock(&gpu.next);
    let command = gpu
        .queue
        .commandBuffer()
        .ok_or_else(|| platform("creating a surface command buffer"))?;
    let encoder = command
        .computeCommandEncoder()
        .ok_or_else(|| platform("creating a compute encoder"))?;
    encoder.setComputePipelineState(&pipeline);
    let width = pipeline.threadExecutionWidth().max(1);
    let height = (pipeline.maxTotalThreadsPerThreadgroup() / width).clamp(1, 16);
    // SAFETY: the kernel reads texture 0 and writes texture 1, both `size`; the grid is `size`.
    unsafe {
        encoder.setTexture_atIndex(Some(&slot.texture), 0);
        encoder.setTexture_atIndex(Some(mapped), 1);
        encoder.dispatchThreads_threadsPerThreadgroup(
            MTLSize {
                width: size.0 as usize,
                height: size.1 as usize,
                depth: 1,
            },
            MTLSize {
                width,
                height,
                depth: 1,
            },
        );
    }
    encoder.endEncoding();
    let value = *next + 1;
    command.encodeSignalEvent_value(ProtocolObject::from_ref(&*gpu.event), value);
    let held = Mutex::new(Some(slot.clone()));
    let done = RcBlock::new(move |_: NonNull<ProtocolObject<dyn MTLCommandBuffer>>| {
        lock(&held).take();
    });
    // SAFETY: Metal retains the block until it has run.
    unsafe { command.addCompletedHandler(RcBlock::as_ptr(&done)) };
    command.commit();
    *next = value;
    Ok(value)
}
/// What a present copies into a surface texture.
enum Source<'a> {
    /// Copied as it is: BGRA8 or half-float.
    Texture(&'a Texture, SurfaceFormat),
    /// Bi-planar 4:2:0 luma and chroma planes, converted with `[y offset, y scale, c offset,
    /// c scale, r←cr, g←cb, g←cr, b←cb]`.
    Nv12 {
        luma: &'a Texture,
        chroma: &'a Texture,
        coefficients: [f32; 8],
    },
}

/// Keeps what a copy reads until its command buffer completes.
struct Held(#[allow(dead_code)] Vec<Box<dyn Any>>);

/// Copies `source` into a free surface texture after `wait`, signals the release event (and
/// `signal`), and makes the texture the frame. Returns the release value.
fn copy_in(
    gpu: &Arc<Gpu>,
    state: &mut State,
    source: Source<'_>,
    size: (u32, u32),
    wait: Option<(&SharedEvent, u64)>,
    signal: Option<(&SharedEvent, u64)>,
    mut held: Vec<Box<dyn Any>>,
) -> Result<(u64, u64), SurfaceError> {
    let pipeline = match &source {
        Source::Nv12 { .. } => Some(nv12_pipeline(gpu)?),
        Source::Texture(..) => None,
    };
    let format = match &source {
        Source::Texture(_, format) => *format,
        Source::Nv12 { .. } => SurfaceFormat::Bgra8,
    };
    check_format(state.format, format)?;
    let index = state.metal.free_slot(gpu, size, format)?;
    let target = state.metal.slots[index].texture.clone();
    if let Some(mut mapped) = target.mapped() {
        *mapped = 0;
    }
    let mut next = lock(&gpu.next);
    let command = gpu
        .queue
        .commandBuffer()
        .ok_or_else(|| platform("creating a surface command buffer"))?;
    if let Some((event, value)) = wait {
        command.encodeWaitForEvent_value(ProtocolObject::from_ref(event), value);
    }
    let whole = MTLSize {
        width: size.0 as usize,
        height: size.1 as usize,
        depth: 1,
    };
    match source {
        Source::Texture(texture, _) => {
            let encoder = command
                .blitCommandEncoder()
                .ok_or_else(|| platform("creating a blit encoder"))?;
            let origin = MTLOrigin { x: 0, y: 0, z: 0 };
            // SAFETY: both textures have the same pixel format, are 2D and at least `size`.
            unsafe {
                encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin(
                    texture, 0, 0, origin, whole, &target.texture, 0, 0, origin,
                );
            }
            encoder.endEncoding();
        }
        Source::Nv12 {
            luma,
            chroma,
            coefficients,
        } => {
            let pipeline = pipeline.expect("made above");
            let encoder = command
                .computeCommandEncoder()
                .ok_or_else(|| platform("creating a compute encoder"))?;
            encoder.setComputePipelineState(&pipeline);
            let width = pipeline.threadExecutionWidth().max(1);
            let height = (pipeline.maxTotalThreadsPerThreadgroup() / width).clamp(1, 16);
            // SAFETY: the kernel reads textures 0 and 1 and the 32 bytes at buffer 0, and writes
            // texture 2, which is `size`; the grid is `size`.
            unsafe {
                encoder.setTexture_atIndex(Some(luma), 0);
                encoder.setTexture_atIndex(Some(chroma), 1);
                encoder.setTexture_atIndex(Some(&target.texture), 2);
                encoder.setBytes_length_atIndex(
                    NonNull::from(&coefficients).cast(),
                    std::mem::size_of_val(&coefficients),
                    0,
                );
                encoder.dispatchThreads_threadsPerThreadgroup(
                    whole,
                    MTLSize {
                        width,
                        height,
                        depth: 1,
                    },
                );
            }
            encoder.endEncoding();
        }
    }
    let value = *next + 1;
    command.encodeSignalEvent_value(ProtocolObject::from_ref(&*gpu.event), value);
    if let Some((event, value)) = signal {
        command.encodeSignalEvent_value(ProtocolObject::from_ref(event), value);
    }
    for (event, _) in wait.into_iter().chain(signal) {
        held.push(Box::new(event.retain()));
    }
    held.push(Box::new(target.clone()));
    let held = Mutex::new(Some(Held(held)));
    let done = RcBlock::new(move |_: NonNull<ProtocolObject<dyn MTLCommandBuffer>>| {
        lock(&held).take();
    });
    // SAFETY: Metal retains the block until it has run.
    unsafe { command.addCompletedHandler(RcBlock::as_ptr(&done)) };
    command.commit();
    *next = value;
    drop(next);
    state.metal.slots[index].written = value;
    state.metal.current = Some(index);
    state.metal.gpu = Some(gpu.clone());
    let frame = state.put(Frame::Metal {
        width: size.0,
        height: size.1,
    });
    Ok((frame, value))
}

const NV12_KERNEL: &str = r#"
#include <metal_stdlib>
using namespace metal;

kernel void nv12_to_bgra(texture2d<float, access::read> luma [[texture(0)]],
                         texture2d<float, access::sample> chroma [[texture(1)]],
                         texture2d<float, access::write> out [[texture(2)]],
                         constant float4 *k [[buffer(0)]],
                         uint2 at [[thread_position_in_grid]]) {
    if (at.x >= out.get_width() || at.y >= out.get_height()) return;
    constexpr sampler linear(filter::linear, address::clamp_to_edge, coord::normalized);
    float2 uv = (float2(at) + 0.5) / float2(out.get_width(), out.get_height());
    float y = (luma.read(at).r - k[0].x) * k[0].y;
    float2 c = (chroma.sample(linear, uv).rg - k[0].z) * k[0].w;
    float3 rgb = float3(y + k[1].x * c.y, y - k[1].y * c.x - k[1].z * c.y, y + k[1].w * c.x);
    out.write(float4(saturate(rgb), 1.0), at);
}
"#;

type Pipeline = Retained<ProtocolObject<dyn MTLComputePipelineState>>;

/// Compiles a kernel on first use; a failure is kept and returned each time.
fn kernel(
    cell: &OnceLock<Result<Pipeline, String>>,
    device: &ProtocolObject<dyn MTLDevice>,
    source: &str,
    entry: &str,
) -> Result<Pipeline, SurfaceError> {
    cell.get_or_init(|| {
        let library = device
            .newLibraryWithSource_options_error(&NSString::from_str(source), None)
            .map_err(|e| format!("compiling the {entry} kernel: {e}"))?;
        let function = library
            .newFunctionWithName(&NSString::from_str(entry))
            .ok_or_else(|| format!("the {entry} kernel has no entry point"))?;
        device
            .newComputePipelineStateWithFunction_error(&function)
            .map_err(|e| format!("building the {entry} pipeline: {e}"))
    })
    .clone()
    .map_err(SurfaceError::Platform)
}

fn nv12_pipeline(gpu: &Gpu) -> Result<Pipeline, SurfaceError> {
    kernel(&gpu.nv12, &gpu.device, NV12_KERNEL, "nv12_to_bgra")
}

/// The device a texture was made on is the renderer's, or the producer has a stale one.
fn check_device(state: &Mutex<State>, gpu: &Gpu, texture: &Texture) -> Result<(), SurfaceError> {
    if std::ptr::eq(
        Retained::as_ptr(&texture.device()),
        Retained::as_ptr(&gpu.device),
    ) {
        return Ok(());
    }
    let handed = lock(state).metal.handed;
    Err(if handed != 0 && handed != gpu.generation {
        SurfaceError::DeviceChanged
    } else {
        SurfaceError::WrongDevice
    })
}

/// # Safety
/// `raw` is null or a live `id<MTLTexture>`; `ready` is a live `id<MTLSharedEvent>` and a value.
pub(super) unsafe fn present(
    state: &Mutex<State>,
    raw: *mut c_void,
    ready: Option<(*mut c_void, u64)>,
) -> Result<(u64, u64), SurfaceError> {
    // SAFETY: the caller's.
    let texture = unsafe { Retained::retain(raw.cast::<Texture>()) }
        .ok_or_else(|| SurfaceError::Size("a null texture".into()))?;
    let wait = match ready {
        Some((event, value)) => Some((
            // SAFETY: the caller's.
            unsafe { Retained::retain(event.cast::<SharedEvent>()) }
                .ok_or_else(|| SurfaceError::Size("a null shared event".into()))?,
            value,
        )),
        None => None,
    };
    let gpu = gpu()?;
    check_device(state, &gpu, &texture)?;
    let (view, size, format) = describe(&texture)?;
    let mut state = lock(state);
    copy_in(
        &gpu,
        &mut state,
        Source::Texture(&view, format),
        size,
        wait.as_ref().map(|(e, v)| (&**e, *v)),
        None,
        vec![Box::new(view.clone())],
    )
}

fn iosurface_texture(
    gpu: &Gpu,
    surface: &IOSurfaceRef,
    format: MTLPixelFormat,
    plane: usize,
    size: (u32, u32),
) -> Result<Retained<Texture>, SurfaceError> {
    let desc = descriptor(format, size);
    gpu.device
        .newTextureWithDescriptor_iosurface_plane(&desc, surface, plane)
        .ok_or_else(|| platform("making a Metal texture of the IOSurface"))
}

/// An IOSurface's size and format: 'BGRA' or 'RGhA'.
fn iosurface_format(surface: &IOSurfaceRef) -> Result<((u32, u32), SurfaceFormat), SurfaceError> {
    let format = match surface.pixel_format() {
        BGRA => SurfaceFormat::Bgra8,
        RGHA => SurfaceFormat::Rgba16Float,
        other => {
            return Err(SurfaceError::Format(format!(
                "an IOSurface of pixel format {}: a surface takes 'BGRA' \
                 (kCVPixelFormatType_32BGRA), or 'RGhA' (kCVPixelFormatType_64RGBAHalf) on a \
                 surface set to Rgba16Float; present 420v/420f video as a CVPixelBuffer",
                fourcc(other)
            )))
        }
    };
    let size = (
        u32::try_from(surface.width()).unwrap_or(u32::MAX),
        u32::try_from(surface.height()).unwrap_or(u32::MAX),
    );
    check_size(size.0, size.1)?;
    Ok((size, format))
}

fn fourcc(code: u32) -> String {
    let bytes = code.to_be_bytes();
    if bytes.iter().all(|b| b.is_ascii_graphic() || *b == b' ') {
        format!("'{}'", String::from_utf8_lossy(&bytes))
    } else {
        format!("{code:#x}")
    }
}

pub(crate) struct Opened {
    surface: CFRetained<IOSurfaceRef>,
    texture: Retained<Texture>,
    event: Option<Retained<SharedEvent>>,
    generation: u64,
    pub(super) size: (u32, u32),
    pub(super) format: SurfaceFormat,
}

// SAFETY: IOSurfaces and Metal objects are thread-safe.
unsafe impl Send for Opened {}

impl Opened {
    pub(super) fn fenced(&self) -> bool {
        self.event.is_some()
    }

    /// # Safety
    /// `event` is null or a live `id<MTLSharedEvent>`.
    pub(super) unsafe fn set_event(&mut self, event: *mut c_void) -> Result<(), SurfaceError> {
        // SAFETY: the caller's.
        self.event = Some(
            unsafe { Retained::retain(event.cast::<SharedEvent>()) }
                .ok_or_else(|| SurfaceError::Size("a null shared event".into()))?,
        );
        Ok(())
    }

    /// # Safety
    /// `handle` is null or a live `MTLSharedEventHandle`.
    pub(super) unsafe fn set_event_handle(
        &mut self,
        handle: *mut c_void,
    ) -> Result<(), SurfaceError> {
        // SAFETY: the caller's.
        let handle = unsafe { Retained::retain(handle.cast::<MTLSharedEventHandle>()) }
            .ok_or_else(|| SurfaceError::Size("a null MTLSharedEventHandle".into()))?;
        let gpu = gpu()?;
        self.event = Some(
            gpu.device
                .newSharedEventWithHandle(&handle)
                .ok_or_else(|| platform("opening the MTLSharedEventHandle"))?,
        );
        Ok(())
    }
}

pub(super) fn open(surface: CFRetained<IOSurfaceRef>) -> Result<Opened, SurfaceError> {
    let (size, format) = iosurface_format(&surface)?;
    let gpu = gpu()?;
    let texture = iosurface_texture(&gpu, &surface, pixel_format(format), 0, size)?;
    Ok(Opened {
        surface,
        texture,
        event: None,
        generation: gpu.generation,
        size,
        format,
    })
}

/// # Safety
/// `surface` is null or a live `IOSurfaceRef`.
pub(super) unsafe fn open_raw(surface: *mut c_void) -> Result<Opened, SurfaceError> {
    let surface = NonNull::new(surface.cast::<IOSurfaceRef>())
        .ok_or_else(|| SurfaceError::Size("a null IOSurfaceRef".into()))?;
    // SAFETY: the caller's; retaining keeps it alive for the texture's life.
    open(unsafe { CFRetained::retain(surface) })
}

pub(super) fn open_mach_port(port: u32) -> Result<Opened, SurfaceError> {
    let surface = IOSurfaceRef::lookup_from_mach_port(port).ok_or_else(|| {
        SurfaceError::Platform(format!(
            "mach port {port} names no IOSurface in this process (IOSurfaceCreateMachPort makes \
             one; send it with its send right)"
        ))
    })?;
    open(surface)
}

pub(super) fn present_shared(
    state: &Mutex<State>,
    opened: &Opened,
    values: Option<(u64, u64)>,
) -> Result<u64, SurfaceError> {
    let gpu = gpu()?;
    if gpu.generation != opened.generation {
        return Err(SurfaceError::DeviceChanged);
    }
    let (wait, signal) = match (values, opened.event.as_deref()) {
        (Some((wait, release)), Some(event)) => (Some((event, wait)), Some((event, release))),
        _ => (None, None),
    };
    let mut state = lock(state);
    copy_in(
        &gpu,
        &mut state,
        Source::Texture(&opened.texture, opened.format),
        opened.size,
        wait,
        signal,
        vec![Box::new(opened.surface.clone())],
    )
    .map(|(frame, _)| frame)
}

/// `[y offset, y scale, c offset, c scale]` for 8-bit video or full range, then the matrix.
fn coefficients(full_range: bool, matrix: Option<String>) -> [f32; 8] {
    let range = if full_range {
        [0.0, 1.0, 128.0 / 255.0, 1.0]
    } else {
        [16.0 / 255.0, 255.0 / 219.0, 128.0 / 255.0, 255.0 / 224.0]
    };
    let (kr, kb): (f32, f32) = match matrix.as_deref() {
        Some("ITU_R_601_4" | "SMPTE_240M_1995") => (0.299, 0.114),
        Some("ITU_R_2020") => (0.2627, 0.0593),
        _ => (0.2126, 0.0722),
    };
    let kg = 1.0 - kr - kb;
    let r_cr = 2.0 * (1.0 - kr);
    let b_cb = 2.0 * (1.0 - kb);
    [
        range[0],
        range[1],
        range[2],
        range[3],
        r_cr,
        b_cb * kb / kg,
        r_cr * kr / kg,
        b_cb,
    ]
}

fn ycbcr_matrix(buffer: &CVPixelBuffer) -> Option<String> {
    let key = CFString::from_static_str("CVImageBufferYCbCrMatrix");
    // SAFETY: a live buffer and key; the mode out-pointer may be null.
    let value = unsafe { buffer.attachment(&key, std::ptr::null_mut()) }?;
    Some(value.downcast_ref::<CFString>()?.to_string())
}

/// A CPU-only buffer, for the caller to present as a CPU frame: BGRA bytes, or for
/// [`SurfaceFormat::Rgba16Float`] native-endian halves.
pub(super) struct Cpu<'a> {
    pub(super) pixels: &'a [u8],
    pub(super) format: SurfaceFormat,
    pub(super) width: u32,
    pub(super) height: u32,
    pub(super) stride: usize,
}

/// # Safety
/// `raw` is null or a live `CVPixelBufferRef`.
pub(super) unsafe fn present_pixel_buffer(
    state: &Mutex<State>,
    raw: *mut c_void,
    cpu: impl FnOnce(Cpu<'_>) -> Result<u64, SurfaceError>,
) -> Result<u64, SurfaceError> {
    let buffer = NonNull::new(raw.cast::<CVPixelBuffer>())
        .ok_or_else(|| SurfaceError::Size("a null CVPixelBufferRef".into()))?;
    // SAFETY: the caller's; retained until the copy completes.
    let buffer: CFRetained<CVPixelBuffer> = unsafe { CFRetained::retain(buffer) };
    let format = CVPixelBufferGetPixelFormatType(&buffer);
    let full_range = match format {
        BGRA | RGHA | NV12_VIDEO => false,
        NV12_FULL => true,
        _ => {
            return Err(SurfaceError::Format(format!(
                "a CVPixelBuffer of pixel format {}: a surface takes BGRA (32BGRA), 4:2:0 \
                 bi-planar (420v, 420f), or RGhA (64RGBAHalf) on a surface set to Rgba16Float",
                fourcc(format)
            )))
        }
    };
    let surface_format = if format == RGHA {
        SurfaceFormat::Rgba16Float
    } else {
        SurfaceFormat::Bgra8
    };
    let size = (
        u32::try_from(CVPixelBufferGetWidth(&buffer)).unwrap_or(u32::MAX),
        u32::try_from(CVPixelBufferGetHeight(&buffer)).unwrap_or(u32::MAX),
    );
    check_size(size.0, size.1)?;
    let Some(surface) = CVPixelBufferGetIOSurface(Some(&buffer)) else {
        if format != BGRA && format != RGHA {
            return Err(SurfaceError::Format(
                "a 4:2:0 CVPixelBuffer without an IOSurface: create it with \
                 kCVPixelBufferIOSurfacePropertiesKey (VideoToolbox and AVFoundation do)"
                    .into(),
            ));
        }
        // SAFETY: locked read-only around the read, the base address covers `stride * height`.
        unsafe {
            if CVPixelBufferLockBaseAddress(&buffer, CVPixelBufferLockFlags::ReadOnly)
                != kCVReturnSuccess
            {
                return Err(platform("locking the CVPixelBuffer"));
            }
            let base = CVPixelBufferGetBaseAddress(&buffer);
            let stride = CVPixelBufferGetBytesPerRow(&buffer);
            let result = if base.is_null() {
                Err(platform("reading the CVPixelBuffer"))
            } else {
                cpu(Cpu {
                    pixels: std::slice::from_raw_parts(base.cast::<u8>(), stride * size.1 as usize),
                    format: surface_format,
                    width: size.0,
                    height: size.1,
                    stride,
                })
            };
            CVPixelBufferUnlockBaseAddress(&buffer, CVPixelBufferLockFlags::ReadOnly);
            return result;
        }
    };
    let gpu = gpu()?;
    let mut held: Vec<Box<dyn Any>> = Vec::new();
    let value = if format == BGRA || format == RGHA {
        let pixels = pixel_format(surface_format);
        let texture = iosurface_texture(&gpu, &surface, pixels, 0, size)?;
        held.push(Box::new(buffer.clone()));
        let mut state = lock(state);
        copy_in(
            &gpu,
            &mut state,
            Source::Texture(&texture, surface_format),
            size,
            None,
            None,
            held,
        )
    } else {
        let chroma_size = (size.0.div_ceil(2), size.1.div_ceil(2));
        let luma = iosurface_texture(&gpu, &surface, MTLPixelFormat::R8Unorm, 0, size)?;
        let chroma = iosurface_texture(&gpu, &surface, MTLPixelFormat::RG8Unorm, 1, chroma_size)?;
        let coefficients = coefficients(full_range, ycbcr_matrix(&buffer));
        held.push(Box::new(buffer.clone()));
        let mut state = lock(state);
        copy_in(
            &gpu,
            &mut state,
            Source::Nv12 {
                luma: &luma,
                chroma: &chroma,
                coefficients,
            },
            size,
            None,
            None,
            held,
        )
    };
    value.map(|(frame, _)| frame)
}

#[cfg(test)]
mod tests {
    use std::time::Instant;

    use objc2_core_foundation::{CFDictionary, CFType};
    use objc2_core_video::{
        kCVPixelBufferIOSurfacePropertiesKey, CVPixelBufferCreate,
        CVPixelBufferGetBaseAddressOfPlane, CVPixelBufferGetBytesPerRowOfPlane,
        CVPixelBufferGetHeightOfPlane, CVPixelBufferGetWidthOfPlane,
    };
    use objc2_metal::{
        MTLBuffer, MTLCreateSystemDefaultDevice, MTLRegion, MTLResourceOptions, MTLStorageMode,
    };

    use super::*;
    use crate::surface::color::tests::{f32_to_half, ramp};
    use crate::surface::{surface, SharedTexture, SurfaceHandle};

    const W: u32 = 64;
    const H: u32 = 8;

    /// The system device, published as the renderer's the way a window does; `None` without a GPU.
    fn renderer() -> Option<Retained<ProtocolObject<dyn MTLDevice>>> {
        static ONCE: OnceLock<()> = OnceLock::new();
        let device = MTLCreateSystemDefaultDevice()?;
        ONCE.get_or_init(|| {
            struct Kept(
                #[allow(dead_code)] Retained<ProtocolObject<dyn MTLDevice>>,
                #[allow(dead_code)] Retained<ProtocolObject<dyn MTLCommandQueue>>,
            );
            unsafe impl Send for Kept {}
            unsafe impl Sync for Kept {}
            let queue = device.newCommandQueue().unwrap();
            gpui::publish_renderer_metal_device(
                raw(&*device),
                raw(&*queue),
                Arc::new(Kept(device.clone(), queue)),
            );
        });
        Some(device)
    }

    fn raw<T: ?Sized>(object: &T) -> *mut c_void {
        (object as *const T).cast_mut().cast()
    }

    fn filled(device: &ProtocolObject<dyn MTLDevice>, bgra: [u8; 4]) -> Retained<Texture> {
        let bytes = bgra.repeat((W * H) as usize);
        written(device, MTLPixelFormat::BGRA8Unorm, (W, H), &bytes)
    }

    /// A texture holding `bytes`, rows packed.
    fn written(
        device: &ProtocolObject<dyn MTLDevice>,
        format: MTLPixelFormat,
        (width, height): (u32, u32),
        bytes: &[u8],
    ) -> Retained<Texture> {
        let desc = descriptor(format, (width, height));
        desc.setStorageMode(if device.hasUnifiedMemory() {
            MTLStorageMode::Shared
        } else {
            MTLStorageMode::Managed
        });
        let texture = device.newTextureWithDescriptor(&desc).unwrap();
        let region = MTLRegion {
            origin: MTLOrigin { x: 0, y: 0, z: 0 },
            size: MTLSize {
                width: width as usize,
                height: height as usize,
                depth: 1,
            },
        };
        unsafe {
            texture.replaceRegion_mipmapLevel_withBytes_bytesPerRow(
                region,
                0,
                NonNull::from(bytes).cast(),
                bytes.len() / height as usize,
            );
        }
        texture
    }

    fn halves(values: &[u16]) -> Vec<u8> {
        values.iter().flat_map(|v| v.to_ne_bytes()).collect()
    }

    fn pixel_buffer(format: u32, iosurface: bool) -> CFRetained<CVPixelBuffer> {
        let key = unsafe { kCVPixelBufferIOSurfacePropertiesKey };
        let empty = CFDictionary::<CFType, CFType>::empty();
        let attributes = CFDictionary::from_slices(&[key], &[&*empty]);
        let mut buffer = std::ptr::null_mut();
        let status = unsafe {
            CVPixelBufferCreate(
                None,
                W as usize,
                H as usize,
                format,
                iosurface.then(|| attributes.as_opaque()),
                NonNull::from(&mut buffer),
            )
        };
        assert_eq!(status, kCVReturnSuccess);
        unsafe { CFRetained::from_raw(NonNull::new(buffer).unwrap()) }
    }

    fn fill(buffer: &CVPixelBuffer, plane: usize, texel: &[u8]) {
        unsafe {
            CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags::empty());
            let base = CVPixelBufferGetBaseAddressOfPlane(buffer, plane).cast::<u8>();
            let stride = CVPixelBufferGetBytesPerRowOfPlane(buffer, plane);
            let width = CVPixelBufferGetWidthOfPlane(buffer, plane);
            for y in 0..CVPixelBufferGetHeightOfPlane(buffer, plane) {
                let row = std::slice::from_raw_parts_mut(base.add(y * stride), width * texel.len());
                for px in row.chunks_exact_mut(texel.len()) {
                    px.copy_from_slice(texel);
                }
            }
            CVPixelBufferUnlockBaseAddress(buffer, CVPixelBufferLockFlags::empty());
        }
    }

    fn eventually(what: &str, mut done: impl FnMut() -> bool) {
        let until = Instant::now() + Duration::from_secs(5);
        while !done() {
            assert!(Instant::now() < until, "{what} never happened");
            std::thread::sleep(Duration::from_millis(1));
        }
    }

    /// What the atlas would copy for the surface's frame, read back once it is written.
    fn shown(surface: &SurfaceHandle) -> Vec<[u8; 4]> {
        let ready = surface.lock_state().metal.ready().expect("a Metal frame");
        eventually("the copy", || ready.done());
        let ready = ready.for_atlas().unwrap();
        eventually("the mapping", || ready.done());
        let texture = match (&ready.texture.mapped, ready.mapped) {
            (Some((mapped, _)), true) => mapped,
            _ => &ready.texture.texture,
        };
        assert_eq!(texture.pixelFormat(), MTLPixelFormat::BGRA8Unorm);
        let (w, h) = (texture.width(), texture.height());
        let device = &ready.gpu.device;
        let buffer = device
            .newBufferWithLength_options(w * h * 4, MTLResourceOptions::StorageModeShared)
            .unwrap();
        let command = device.newCommandQueue().unwrap().commandBuffer().unwrap();
        let encoder = command.blitCommandEncoder().unwrap();
        unsafe {
            encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toBuffer_destinationOffset_destinationBytesPerRow_destinationBytesPerImage(
                texture,
                0,
                0,
                MTLOrigin { x: 0, y: 0, z: 0 },
                MTLSize { width: w, height: h, depth: 1 },
                &buffer,
                0,
                w * 4,
                w * h * 4,
            );
        }
        encoder.endEncoding();
        command.commit();
        command.waitUntilCompleted();
        let bytes = unsafe {
            std::slice::from_raw_parts(buffer.contents().cast::<u8>().as_ptr(), w * h * 4)
        };
        bytes
            .chunks_exact(4)
            .map(|p| [p[0], p[1], p[2], p[3]])
            .collect()
    }

    fn all_near(pixels: &[[u8; 4]], want: [u8; 4], by: u8) -> bool {
        pixels
            .iter()
            .all(|p| p.iter().zip(want).all(|(g, w)| g.abs_diff(w) <= by))
    }

    #[test]
    fn a_texture_is_copied_once_its_event_fires_and_then_released() {
        let Some(device) = renderer() else { return };
        let s = surface("tests:metal-wait");
        let source = filled(&device, [10, 20, 30, 255]);
        let event = device.newSharedEvent().unwrap();
        let released = unsafe { s.present_metal_texture(raw(&*source), Some((raw(&*event), 1))) }
            .unwrap()
            .release;
        let ready = s.lock_state().metal.ready().unwrap();
        std::thread::sleep(Duration::from_millis(50));
        assert!(
            !ready.done(),
            "the copy waits for the producer's event on the GPU"
        );
        event.setSignaledValue(1);
        assert!(s
            .release_event()
            .unwrap()
            .wait(released, Duration::from_secs(5)));
        assert!(all_near(&shown(&s), [10, 20, 30, 255], 0));
    }

    #[test]
    fn a_fifth_frame_in_flight_is_refused_until_one_is_released() {
        let Some(device) = renderer() else { return };
        let s = surface("tests:metal-busy");
        let source = filled(&device, [1, 2, 3, 255]);
        let event = device.newSharedEvent().unwrap();
        let present = || unsafe { s.present_metal_texture(raw(&*source), Some((raw(&*event), 1))) };
        for _ in 0..MAX_SLOTS {
            present().unwrap();
        }
        assert_eq!(present(), Err(SurfaceError::Busy));
        event.setSignaledValue(1);
        eventually("a free slot", || present().is_ok());
        assert!(all_near(&shown(&s), [1, 2, 3, 255], 0));
    }

    #[test]
    fn a_shared_iosurface_waits_for_its_event_and_signals_the_release_value() {
        let Some(device) = renderer() else { return };
        let s = surface("tests:metal-iosurface");
        let buffer = pixel_buffer(BGRA, true);
        fill(&buffer, 0, &[200, 100, 50, 255]);
        let iosurface = CVPixelBufferGetIOSurface(Some(&buffer)).unwrap();
        let event = device.newSharedEvent().unwrap();
        let shared = unsafe { SharedTexture::from_iosurface(raw(&*iosurface)) }
            .and_then(|t| unsafe { t.with_shared_event(raw(&*event)) })
            .unwrap();
        assert_eq!(shared.size(), (W, H));
        assert!(matches!(
            s.present_shared(&shared),
            Err(SurfaceError::Format(_))
        ));
        s.present_shared_fenced(&shared, 1, 2).unwrap();
        std::thread::sleep(Duration::from_millis(50));
        assert_eq!(event.signaledValue(), 0);
        event.setSignaledValue(1);
        eventually("the release value", || event.signaledValue() == 2);
        assert!(all_near(&shown(&s), [200, 100, 50, 255], 0));
    }

    #[test]
    fn pixel_buffers_convert_420v_and_copy_bgra() {
        if renderer().is_none() {
            return;
        }
        let s = surface("tests:metal-cv");
        // BT.709 video-range red.
        let nv12 = pixel_buffer(NV12_VIDEO, true);
        fill(&nv12, 0, &[63]);
        fill(&nv12, 1, &[102, 240]);
        unsafe { s.present_cv_pixel_buffer(raw(&*nv12)) }.unwrap();
        assert!(all_near(&shown(&s), [0, 0, 255, 255], 2));

        let bgra = pixel_buffer(BGRA, true);
        fill(&bgra, 0, &[9, 8, 7, 255]);
        unsafe { s.present_cv_pixel_buffer(raw(&*bgra)) }.unwrap();
        assert!(all_near(&shown(&s), [9, 8, 7, 255], 0));

        let cpu_only = pixel_buffer(BGRA, false);
        fill(&cpu_only, 0, &[4, 5, 6, 128]);
        unsafe { s.present_cv_pixel_buffer(raw(&*cpu_only)) }.unwrap();
        let frame = s.lock_state().frame.clone();
        match frame {
            Some(Frame::Cpu { bgra, .. }) => assert!(bgra.chunks(4).all(|p| p == [4, 5, 6, 128])),
            _ => panic!("a buffer with no IOSurface is a CPU frame"),
        }
    }

    #[test]
    fn a_half_float_texture_shows_every_srgb_code_unchanged_and_clamps_above_one() {
        let Some(device) = renderer() else { return };
        let s = surface("tests:metal-rgba16f");
        s.set_format(SurfaceFormat::Rgba16Float);
        let h = f32_to_half;
        let mut texels = ramp();
        texels.extend([h(8.0), h(1.5), h(-0.25), h(2.0)]);
        let source = written(
            &device,
            MTLPixelFormat::RGBA16Float,
            (257, 1),
            &halves(&texels),
        );
        unsafe { s.present_metal_texture(raw(&*source), None) }.unwrap();
        let pixels = shown(&s);
        for (code, px) in pixels[..256].iter().enumerate() {
            assert_eq!(px, &[code as u8; 4], "code {code}");
        }
        assert_eq!(pixels[256], [0, 255, 255, 255]);
    }

    #[test]
    fn rgha_pixel_buffers_and_iosurfaces_are_mapped() {
        if renderer().is_none() {
            return;
        }
        let s = surface("tests:metal-rgha");
        s.set_format(SurfaceFormat::Rgba16Float);
        let h = f32_to_half;
        let texel = halves(&[h(0.2159), h(1.0), h(4.0), h(1.0)]);
        let want = [255, 255, 128, 255];

        let buffer = pixel_buffer(RGHA, true);
        fill(&buffer, 0, &texel);
        unsafe { s.present_cv_pixel_buffer(raw(&*buffer)) }.unwrap();
        assert!(all_near(&shown(&s), want, 0));

        let iosurface = CVPixelBufferGetIOSurface(Some(&buffer)).unwrap();
        let shared = unsafe { SharedTexture::from_iosurface(raw(&*iosurface)) }.unwrap();
        assert_eq!(shared.format(), SurfaceFormat::Rgba16Float);
        s.present_shared(&shared).unwrap();
        assert!(all_near(&shown(&s), want, 0));

        let cpu_only = pixel_buffer(RGHA, false);
        fill(&cpu_only, 0, &texel);
        unsafe { s.present_cv_pixel_buffer(raw(&*cpu_only)) }.unwrap();
        let frame = s.lock_state().frame.clone();
        match frame {
            Some(Frame::Cpu { bgra, .. }) => assert!(bgra.chunks(4).all(|p| p == want)),
            _ => panic!("an RGhA buffer with no IOSurface is a CPU frame"),
        }
    }

    #[test]
    fn a_texture_of_the_other_format_is_refused() {
        let Some(device) = renderer() else { return };
        let half = surface("tests:metal-mismatch");
        half.set_format(SurfaceFormat::Rgba16Float);
        let bgra = filled(&device, [1, 2, 3, 255]);
        assert_eq!(
            unsafe { half.present_metal_texture(raw(&*bgra), None) },
            Err(SurfaceError::FormatMismatch {
                surface: SurfaceFormat::Rgba16Float,
                frame: SurfaceFormat::Bgra8
            })
        );
        let buffer = pixel_buffer(BGRA, true);
        assert!(matches!(
            unsafe { half.present_cv_pixel_buffer(raw(&*buffer)) },
            Err(SurfaceError::FormatMismatch { .. })
        ));
        assert_eq!(half.stats().presented, 0);
    }
}