euv-core 0.48.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
use super::*;

/// Probed physical GPU adapter and native shader compilation telemetry.
#[derive(Clone, Debug, Eq, Getter, New, PartialEq)]
pub struct GpuAdapterInfo {
    /// Active graphics backend identifier.
    #[get(type(copy))]
    pub(crate) backend: GraphicsBackend,
    /// Detected physical GPU adapter name (for example `"Apple M3 Max"`).
    pub(crate) device_name: String,
    /// True when a real physical OS GPU device/runtime was successfully initialized.
    #[get(type(copy))]
    pub(crate) hardware_verified: bool,
    /// True when the native UI shader (`MSL`/`HLSL`/`GLSL`/`WGSL`) compiled on the host GPU.
    #[get(type(copy))]
    pub(crate) shader_compiled: bool,
}

/// One hardware-fenced staging buffer slot inside `NativeGpuDriver`'s triple-buffered GPU ring.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct NativeGpuRingSlot {
    /// Native OS shared-memory buffer handle (`id<MTLBuffer>` on Apple, `0` when unallocated).
    #[get(type(copy))]
    pub(crate) buffer_handle: usize,
    /// CPU-writable mapped memory address (`[MTLBuffer contents]` on Apple, `0` when unallocated).
    #[get(type(copy))]
    pub(crate) contents_ptr: usize,
    /// Allocated byte capacity of this ring slot buffer.
    #[get(type(copy))]
    pub(crate) capacity_bytes: usize,
    /// Retained in-flight command buffer handle guarding reuse (`id<MTLCommandBuffer>` on Apple).
    #[get(type(copy))]
    pub(crate) in_flight_cmd_buffer: usize,
}

/// Physical OS GPU driver wrapping Metal, D3D12, Vulkan, or WebGL2 with real fenced ring submission and automatic fallback.
#[derive(Debug, Getter)]
pub struct NativeGpuDriver {
    /// Probed physical adapter and shader status.
    pub(crate) adapter: GpuAdapterInfo,
    /// Opaque OS GPU device handle (`id<MTLDevice>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) device_handle: usize,
    /// Opaque OS GPU command queue handle (`id<MTLCommandQueue>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) queue_handle: usize,
    /// Opaque compiled shader library handle (`id<MTLLibrary>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) library_handle: usize,
    /// Opaque compiled uber-shader render pipeline handle (`id<MTLRenderPipelineState>` on Apple).
    #[get(type(copy))]
    pub(crate) pipeline_handle: usize,
    /// Opaque compiled Dual-Kawase `BackdropBlur` pipeline handle (`id<MTLRenderPipelineState>` on Apple).
    #[get(type(copy))]
    pub(crate) blur_pipeline_handle: usize,
    /// Attached native presentation layer handle (`CAMetalLayer*` on Apple, `0` when headless).
    #[get(type(copy))]
    pub(crate) surface_layer_handle: usize,
    /// Resident R8Unorm monochrome glyph atlas `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) glyph_r8_texture_handle: usize,
    /// Resident RGBA8Unorm color glyph atlas `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) glyph_rgba8_texture_handle: usize,
    /// Resident RGBA8Unorm UI image/sprite `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) image_texture_handle: usize,
    /// Bound external GPU texture handle (3D game render target or hardware video surface).
    #[get(type(copy))]
    pub(crate) external_texture_handle: usize,
    /// Triple-buffered fenced GPU staging ring slots eliminating CPU/GPU buffer hazards.
    pub(crate) ring_slots: [NativeGpuRingSlot; GPU_DRIVER_RING_SLOTS],
    /// Active ring slot cursor cycling `0 -> 1 -> 2 -> 0` across frames.
    #[get(type(copy))]
    pub(crate) ring_cursor: usize,
    /// Persistent grow-only staging handle mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_buffer_handle: usize,
    /// Cached CPU-writable contents pointer mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_contents_ptr: usize,
    /// Byte capacity mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_buffer_capacity: usize,
    /// Total GPU frames submitted through this driver.
    #[get(type(copy))]
    pub(crate) submitted_frames: u64,
    /// Total instanced draw primitives uploaded to the GPU.
    #[get(type(copy))]
    pub(crate) uploaded_instances: u64,
    /// Cumulative instance payload bytes uploaded to the GPU across all frames.
    #[get(type(copy))]
    pub(crate) uploaded_bytes: u64,
    /// Instance payload bytes uploaded during the most recent submitted frame.
    #[get(type(copy))]
    pub(crate) last_upload_bytes: usize,
    /// Number of draw batches submitted during the most recent frame.
    #[get(type(copy))]
    pub(crate) last_submitted_batches: usize,
    /// Number of staging ring slot allocations or grows across the driver's lifetime.
    #[get(type(copy))]
    pub(crate) staging_reallocations: u64,
    /// Most recent hardware presentation timestamp in nanoseconds reported by Metal `addPresentedHandler:`.
    #[get(type(copy))]
    pub(crate) last_presented_time_ns: u64,
    /// Most recent GPU execution duration in nanoseconds (`GPUEndTime - GPUStartTime`).
    #[get(type(copy))]
    pub(crate) last_gpu_exec_ns: u64,
    /// Scratch buffer packing 64-byte `GpuPrimitiveInstance` records when legacy batches lack pre-packed primitives.
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    #[get(skip)]
    pub(crate) scratch_primitives: Vec<GpuPrimitiveInstance>,
    /// Fallback command recorder active on headless hosts.
    pub(crate) fallback: RecordingGpuDriver,
}

/// Retained native GPU device, queue, library, compiled pipelines, and dummy texture handles returned by `probe_native_gpu_full`.
#[derive(Clone, Debug)]
pub(crate) struct NativeGpuProbeOutput {
    /// Probed adapter capabilities and hardware backend descriptor.
    pub(crate) adapter: GpuAdapterInfo,
    /// Retained native GPU device handle (`0` when headless).
    pub(crate) device_handle: usize,
    /// Retained native GPU command queue handle (`0` when headless).
    pub(crate) queue_handle: usize,
    /// Retained compiled native shader library handle (`0` when headless).
    pub(crate) library_handle: usize,
    /// Retained cached uber-shader render pipeline handle (`0` when headless).
    pub(crate) pipeline_handle: usize,
    /// Retained cached Dual-Kawase blur render pipeline handle (`0` when headless).
    pub(crate) blur_pipeline_handle: usize,
    /// Retained 1x1 fallback R8Unorm texture handle (`0` when headless).
    pub(crate) dummy_r8_tex: usize,
    /// Retained 1x1 fallback RGBA8Unorm texture handle (`0` when headless).
    pub(crate) dummy_rgba8_tex: usize,
}

/// Process-wide cached Objective-C selector pointers for Apple Metal hot-path submission and encoding.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct MetalSelectors {
    /// Cached `retain` selector address.
    pub(crate) retain: usize,
    /// Cached `newBufferWithBytes:length:options:` selector address.
    pub(crate) new_buffer_with_bytes: usize,
    /// Cached `newBufferWithLength:options:` selector address.
    pub(crate) new_buffer_with_length: usize,
    /// Cached `contents` selector address.
    pub(crate) contents: usize,
    /// Cached `commandBuffer` selector address.
    pub(crate) command_buffer: usize,
    /// Cached `renderCommandEncoderWithDescriptor:` selector address.
    pub(crate) render_encoder: usize,
    /// Cached `setRenderPipelineState:` selector address.
    pub(crate) set_pipeline_state: usize,
    /// Cached `setVertexBuffer:offset:atIndex:` selector address.
    pub(crate) set_vertex_buffer: usize,
    /// Cached `setVertexBytes:length:atIndex:` selector address.
    pub(crate) set_vertex_bytes: usize,
    /// Cached `setFragmentTexture:atIndex:` selector address.
    pub(crate) set_fragment_texture: usize,
    /// Cached `setScissorRect:` selector address.
    pub(crate) set_scissor_rect: usize,
    /// Cached `drawPrimitives:vertexStart:vertexCount:instanceCount:baseInstance:` selector address.
    pub(crate) draw_primitives_instanced: usize,
    /// Cached `endEncoding` selector address.
    pub(crate) end_encoding: usize,
    /// Cached `presentDrawable:` selector address.
    pub(crate) present_drawable: usize,
    /// Cached `addPresentedHandler:` selector address (on `MTLDrawable`).
    pub(crate) add_presented_handler: usize,
    /// Cached `addCompletedHandler:` selector address (on `MTLCommandBuffer`).
    pub(crate) add_completed_handler: usize,
    /// Cached `presentedTime` selector address (on `MTLDrawable`).
    pub(crate) presented_time: usize,
    /// Cached `respondsToSelector:` selector address.
    pub(crate) responds_to_sel: usize,
    /// Cached `status` selector address.
    pub(crate) status: usize,
    /// Cached `waitUntilCompleted` selector address.
    pub(crate) wait_until_completed: usize,
    /// Cached `GPUStartTime` selector address.
    pub(crate) gpu_start_time: usize,
    /// Cached `GPUEndTime` selector address.
    pub(crate) gpu_end_time: usize,
    /// Cached `nextDrawable` selector address.
    pub(crate) next_drawable: usize,
    /// Cached `texture` selector address.
    pub(crate) texture: usize,
    /// Cached `width` selector address.
    pub(crate) width: usize,
    /// Cached `height` selector address.
    pub(crate) height: usize,
    /// Cached `renderPassDescriptor` selector address.
    pub(crate) render_pass_desc: usize,
    /// Cached `colorAttachments` selector address.
    pub(crate) color_attachments: usize,
    /// Cached `objectAtIndexedSubscript:` selector address.
    pub(crate) object_at_index: usize,
    /// Cached `setTexture:` selector address.
    pub(crate) set_texture: usize,
    /// Cached `setLoadAction:` selector address.
    pub(crate) set_load_action: usize,
    /// Cached `setStoreAction:` selector address.
    pub(crate) set_store_action: usize,
    /// Cached `setClearColor:` selector address.
    pub(crate) set_clear_color: usize,
    /// Cached `replaceRegion:mipmapLevel:withBytes:bytesPerRow:` selector address.
    pub(crate) replace_region: usize,
    /// Cached `commit` selector address.
    pub(crate) commit: usize,
    /// Cached `release` selector address.
    pub(crate) release: usize,
}

/// Process-wide cached Apple Metal runtime pipeline state eliminating redundant 40-180ms shader compiles.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Debug)]
pub(crate) struct MetalSharedPipelineCache {
    /// Probed physical adapter description and shader compile status.
    pub(crate) adapter: GpuAdapterInfo,
    /// Retained `id<MTLDevice>` handle.
    pub(crate) device: usize,
    /// Retained `id<MTLCommandQueue>` handle.
    pub(crate) queue: usize,
    /// Retained `id<MTLLibrary>` handle.
    pub(crate) library: usize,
    /// Retained `id<MTLRenderPipelineState>` handle for the 64-byte uber-shader.
    pub(crate) pipeline: usize,
    /// Retained `id<MTLRenderPipelineState>` handle for the Dual-Kawase blur shader.
    pub(crate) blur_pipeline: usize,
    /// Retained 1x1 fallback R8Unorm `MTLTexture` bound when glyph atlas is empty.
    pub(crate) dummy_r8_tex: usize,
    /// Retained 1x1 fallback RGBA8Unorm `MTLTexture` bound when color/image textures are unbound.
    pub(crate) dummy_rgba8_tex: usize,
}

/// Metal `MTLOrigin` 3D texel origin ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlOrigin {
    /// Horizontal texel origin.
    pub(crate) x: usize,
    /// Vertical texel origin.
    pub(crate) y: usize,
    /// Slice depth origin (`0`).
    pub(crate) z: usize,
}

/// Metal `MTLSize` 3D texel extent ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlSize {
    /// Extent width in texels.
    pub(crate) width: usize,
    /// Extent height in texels.
    pub(crate) height: usize,
    /// Extent depth in slices (`1`).
    pub(crate) depth: usize,
}

/// Metal `MTLRegion` 3D sub-texture upload rectangle ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlRegion {
    /// Sub-region origin.
    pub(crate) origin: MtlOrigin,
    /// Sub-region extent.
    pub(crate) size: MtlSize,
}

/// Metal `MTLScissorRect` physical scissor ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlScissorRect {
    /// Left physical pixel edge.
    pub(crate) x: usize,
    /// Top physical pixel edge.
    pub(crate) y: usize,
    /// Scissor width in physical pixels.
    pub(crate) width: usize,
    /// Scissor height in physical pixels.
    pub(crate) height: usize,
}

/// Metal `MTLClearColor` RGBA64F clear color ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlClearColor {
    /// Red channel in `[0.0..=1.0]`.
    pub(crate) red: f64,
    /// Green channel in `[0.0..=1.0]`.
    pub(crate) green: f64,
    /// Blue channel in `[0.0..=1.0]`.
    pub(crate) blue: f64,
    /// Alpha channel in `[0.0..=1.0]`.
    pub(crate) alpha: f64,
}

/// Objective-C runtime global block descriptor ABI layout.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
pub(crate) struct ObjcBlockDescriptor {
    /// Reserved word (`0`).
    pub(crate) reserved: usize,
    /// Byte size of the block struct.
    pub(crate) size: usize,
}

/// Objective-C runtime zero-allocation global block ABI struct for Metal `addPresentedHandler:`.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
pub(crate) struct ObjcGlobalBlock {
    /// Pointer to `_NSConcreteGlobalBlock`.
    pub(crate) isa: *const c_void,
    /// Block flags (`OBJC_BLOCK_IS_GLOBAL`).
    pub(crate) flags: i32,
    /// Reserved flags (`0`).
    pub(crate) reserved: i32,
    /// Block invoke C callback `(block, cmd_buffer)`.
    pub(crate) invoke: unsafe extern "C" fn(*mut c_void, *mut c_void),
    /// Descriptor pointer.
    pub(crate) descriptor: *const ObjcBlockDescriptor,
}

/// Process-wide cached Windows Direct3D 12 probe status eliminating repeated `LoadLibraryA`/`FreeLibrary` thrashing.
#[cfg(target_os = "windows")]
#[derive(Clone, Copy, Debug)]
pub(crate) struct WindowsD3d12ProbeCache {
    /// True when `d3d12.dll` exports `D3D12CreateDevice`.
    pub(crate) has_d3d12: bool,
    /// True when `d3dcompiler_47.dll` exports `D3DCompile`.
    pub(crate) has_compiler: bool,
}

/// Process-wide cached Vulkan loader probe status eliminating repeated `dlopen`/`dlclose` thrashing.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct VulkanProbeCache {
    /// True when Vulkan loader exports `vkCreateInstance` and `vkEnumeratePhysicalDevices`.
    pub(crate) available: bool,
    /// Cached `dlopen` handle kept resident for process lifetime.
    pub(crate) lib_handle: usize,
}

/// Physical pixel dirty sub-rectangle used by adaptive GLES 3.0 and Metal texture upload planners.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, Eq, New, PartialEq)]
pub struct PixelDirtyRect {
    /// Left inclusive pixel coordinate.
    #[get(type(copy))]
    pub(crate) x: u32,
    /// Top inclusive pixel coordinate.
    #[get(type(copy))]
    pub(crate) y: u32,
    /// Sub-rectangle width in pixels.
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Sub-rectangle height in pixels.
    #[get(type(copy))]
    pub(crate) height: u32,
}

/// Selected adaptive texture-upload plan choosing `MultiRect`, `MergedBbox`, `FullFrame`, or `None` by measured byte and driver cost.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct AdaptiveUploadPlan {
    /// Selected upload strategy for this frame.
    #[get(type(copy))]
    pub(crate) strategy: MobileUploadStrategy,
    /// Ordered physical rectangles to upload under `strategy`.
    pub(crate) rects: Vec<PixelDirtyRect>,
    /// Total RGBA8 pixel payload bytes transferred under `strategy`.
    #[get(type(copy))]
    pub(crate) total_upload_bytes: usize,
    /// Equivalent RGBA8 payload bytes of the single merged bounding box.
    #[get(type(copy))]
    pub(crate) merged_bbox_bytes: usize,
    /// Equivalent RGBA8 payload bytes of a full-frame texture upload.
    #[get(type(copy))]
    pub(crate) full_frame_bytes: usize,
    /// Estimated total upload cost score (pixel bytes plus per-call driver overhead).
    #[get(type(copy))]
    pub(crate) estimated_cost_score: f32,
}

/// One scissor-bounded instanced draw range inside a compiled `DirectGpuFrameBatch`.
#[repr(C)]
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct DirectGpuBatchRange {
    /// First primitive index inside `DirectGpuFrameBatch::primitives`.
    #[get(type(copy))]
    pub(crate) base_instance: u32,
    /// Number of consecutive 64-byte `GpuPrimitiveInstance` records drawn in this call.
    #[get(type(copy))]
    pub(crate) instance_count: u32,
    /// Physical scissor left coordinate in pixels (top-left origin).
    #[get(type(copy))]
    pub(crate) scissor_x: u32,
    /// Physical scissor top coordinate in pixels (top-left origin).
    #[get(type(copy))]
    pub(crate) scissor_y: u32,
    /// Physical scissor width in pixels.
    #[get(type(copy))]
    pub(crate) scissor_w: u32,
    /// Physical scissor height in pixels.
    #[get(type(copy))]
    pub(crate) scissor_h: u32,
    /// Horizontal retained-layer scroll translation in physical pixels.
    #[get(type(copy))]
    pub(crate) scroll_x: f32,
    /// Vertical retained-layer scroll translation in physical pixels.
    #[get(type(copy))]
    pub(crate) scroll_y: f32,
    /// Bound texture slot discriminant (`0` = default glyph/image textures).
    #[get(type(copy))]
    pub(crate) texture_slot: u8,
    /// Owning retained layer identifier (`0` = root).
    #[get(type(copy))]
    pub(crate) layer_id: u8,
    /// Explicit C-ABI alignment padding bytes.
    #[get(type(copy))]
    pub(crate) pad: [u8; 2],
}

/// Borrowed RGBA8 image or sprite-sheet sub-region queued for frame-level direct-GPU image atlas packing.
#[derive(Clone, Copy, Debug)]
pub(crate) struct DirectGpuImageSlice<'a> {
    /// Full source image width in pixels.
    pub(crate) src_w: u32,
    /// Full source image height in pixels.
    pub(crate) src_h: u32,
    /// Borrowed RGBA8 pixel bytes (`src_w * src_h * 4`).
    pub(crate) pixels: &'a [u8],
    /// Source crop rectangle `(x, y, width, height)` in pixels.
    pub(crate) crop: PixelDirtyRect,
}

/// Packed R8 monochrome glyph atlas or RGBA8 image texture sheet ready for direct GLES 3.0 or Metal upload.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct DirectGpuAtlasUpload {
    /// Backing texture width in texels (`0` when no upload is required).
    #[get(type(copy))]
    pub(crate) width: u32,
    /// Backing texture height in texels (`0` when no upload is required).
    #[get(type(copy))]
    pub(crate) height: u32,
    /// Monotonic revision tag avoiding redundant uploads when unchanged.
    #[get(type(copy))]
    pub(crate) revision: u64,
    /// Tightly packed texel payload bytes (`width * height` for R8, `width * height * 4` for RGBA8).
    pub(crate) pixels: Vec<u8>,
}

/// Complete direct-GPU frame payload targeting GLES 3.0 `EGLSurface` or Metal `CAMetalDrawable` with zero CPU framebuffer mirror.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct DirectGpuFrameBatch {
    /// Physical surface width in pixels.
    #[get(type(copy))]
    pub(crate) viewport_width: u32,
    /// Physical surface height in pixels.
    #[get(type(copy))]
    pub(crate) viewport_height: u32,
    /// Normalized sRGB clear color `[r, g, b, a]` applied to the native surface.
    #[get(type(copy))]
    pub(crate) clear_rgba: [f32; 4],
    /// Flat tightly packed 64-byte `GpuPrimitiveInstance` array uploaded once per frame.
    pub(crate) primitives: Vec<GpuPrimitiveInstance>,
    /// Ordered scissor-bounded draw calls indexing into `primitives`.
    pub(crate) draw_ranges: Vec<DirectGpuBatchRange>,
    /// Ordered Dual-Kawase `BackdropBlur` GPU passes for this frame.
    pub(crate) backdrop_blurs: Vec<GpuBackdropBlurPass>,
    /// Optional R8 monochrome glyph atlas update when new glyphs were rasterized.
    pub(crate) glyph_r8_atlas: Option<DirectGpuAtlasUpload>,
    /// Optional RGBA8 color/image texture sheet update for `Image` / `SharedImage` / `SpriteRegion`.
    pub(crate) image_rgba8_atlas: Option<DirectGpuAtlasUpload>,
    /// Total instance + atlas bytes uploaded to GPU memory for this frame.
    #[get(type(copy))]
    pub(crate) instance_upload_bytes: usize,
    /// Always `false` on the direct GPU draw path (zero full-frame CPU RGBA mirror).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Upload strategy tag (`MobileUploadStrategy::DirectGpu`).
    #[get(type(copy))]
    pub(crate) upload_strategy: MobileUploadStrategy,
}

/// GLES 3.0 instanced vertex attribute binding specification matching `GLSL_ES3_UI_VERTEX_SHADER` byte-for-byte.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Getter, New, PartialEq)]
pub struct Gles3InstanceAttribSpec {
    /// Vertex attribute layout location (`1..=5`).
    #[get(type(copy))]
    pub(crate) location: u32,
    /// Number of scalar components (`2` or `4`).
    #[get(type(copy))]
    pub(crate) components: i32,
    /// True when bound with `glVertexAttribIPointer` (`GL_UNSIGNED_INT`), false for `glVertexAttribPointer` (`GL_FLOAT`).
    #[get(type(copy))]
    pub(crate) is_integer: bool,
    /// Byte offset inside the 64-byte `GpuPrimitiveInstance` struct.
    #[get(type(copy))]
    pub(crate) byte_offset: usize,
    /// Stride in bytes (`64`).
    #[get(type(copy))]
    pub(crate) stride: i32,
}

/// Honest cross-platform mobile presentation telemetry ready to feed into `AdaptiveRenderRouter` and `report-v1` JSON.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct MobilePresentTelemetry {
    /// Currently active presentation path (`GpuDirect`, `GpuUpload`, or `Software`).
    #[get(type(copy))]
    pub(crate) active_path: MobilePresentPath,
    /// Upload strategy executed on the most recent frame.
    #[get(type(copy))]
    pub(crate) upload_strategy: MobileUploadStrategy,
    /// True only when a CPU framebuffer mirror is active (`false` on `GpuDirect`, `true` on `GpuUpload`/`Software`).
    #[get(type(copy))]
    pub(crate) cpu_mirror_active: bool,
    /// Provenance of presentation timestamps (`CompositorMeasured`, `SubmissionProxy`, or `Unobservable`).
    #[get(type(copy))]
    pub(crate) timing_source: MobilePresentTimingSource,
    /// Explicit reason when compositor presentation timing is unobservable (never masked as `0.0`).
    #[get(type(copy))]
    pub(crate) unobservable_reason: Option<&'static str>,
    /// True when executing inside an iOS/Android software simulator rather than physical GPU hardware.
    #[get(type(copy))]
    pub(crate) is_simulator: bool,
    /// Active display refresh rate in Hertz (`60.0`, `90.0`, `120.0`, `144.0`, etc.).
    #[get(type(copy))]
    pub(crate) display_mode_hz: f32,
    /// Total VSync frame callbacks received (`Choreographer.doFrame` or `CADisplayLink`).
    #[get(type(copy))]
    pub(crate) frame_callback_count: u64,
    /// Most recent VSync frame callback timestamp in nanoseconds.
    #[get(type(copy))]
    pub(crate) last_callback_timestamp_ns: u64,
    /// Measured interval between consecutive VSync callbacks in milliseconds (`None` before 2nd callback).
    #[get(type(copy))]
    pub(crate) last_callback_interval_ms: Option<f64>,
    /// Target presentation timestamp in nanoseconds (`CADisplayLink.targetTimestamp` or Choreographer deadline).
    #[get(type(copy))]
    pub(crate) last_target_timestamp_ns: Option<u64>,
    /// Measured compositor presentation timestamp in nanoseconds (`None` when unobservable on simulator).
    #[get(type(copy))]
    pub(crate) last_presented_timestamp_ns: Option<u64>,
    /// Cumulative count of frames actually submitted and presented.
    #[get(type(copy))]
    pub(crate) actual_presented_frames: u64,
    /// Payload bytes uploaded on the most recent frame (instance+atlas on `GpuDirect`; pixel rects on `GpuUpload`/`Software`).
    #[get(type(copy))]
    pub(crate) upload_bytes_last_frame: u64,
    /// Cumulative payload bytes uploaded across the session.
    #[get(type(copy))]
    pub(crate) upload_bytes_total: u64,
    /// Number of 64-byte `GpuPrimitiveInstance` records drawn on the most recent `GpuDirect` frame.
    #[get(type(copy))]
    pub(crate) primitive_count_last_frame: u32,
    /// Number of GPU draw calls emitted on the most recent frame.
    #[get(type(copy))]
    pub(crate) draw_calls_last_frame: u32,
    /// Cumulative count of submission errors (lock/encode/commit failures).
    #[get(type(copy))]
    pub(crate) submit_failures: u64,
    /// Cumulative count of compositor presentation failures (`eglSwapBuffers` / drawable errors).
    #[get(type(copy))]
    pub(crate) present_failures: u64,
    /// Cumulative count of GPU context-loss events (`EGL_CONTEXT_LOST` / Metal device reset).
    #[get(type(copy))]
    pub(crate) context_loss_count: u64,
    /// Count of blank frames actively prevented by retaining the last valid surface frame during failover.
    #[get(type(copy))]
    pub(crate) blank_frames_prevented: u64,
    /// True when the native surface holds a valid previously presented frame.
    #[get(type(copy))]
    pub(crate) last_valid_frame_retained: bool,
}

/// Immediate zero-blank-frame failover state machine for Android EGL/GLES3 and iOS Metal presentation.
#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
pub struct MobileFailoverController {
    /// Currently active presentation path.
    #[get(type(copy))]
    pub(crate) active_path: MobilePresentPath,
    /// Preferred startup presentation path before faults.
    #[get(type(copy))]
    pub(crate) preferred_path: MobilePresentPath,
    /// True once at least one valid frame has been presented to the current window surface.
    #[get(type(copy))]
    pub(crate) has_valid_presented_frame: bool,
    /// True while the OS native window / Metal layer remains attached and valid.
    #[get(type(copy))]
    pub(crate) window_valid: bool,
    /// True when direct GPU uber-shader pipeline is available on this surface.
    #[get(type(copy))]
    pub(crate) direct_gpu_available: bool,
    /// True when texture-upload GPU pipeline is available on this surface.
    #[get(type(copy))]
    pub(crate) gpu_upload_available: bool,
    /// Cumulative context-loss faults observed.
    #[get(type(copy))]
    pub(crate) context_loss_count: u64,
    /// Cumulative blank frames prevented by retaining the last valid frame during fault switchover.
    #[get(type(copy))]
    pub(crate) blank_frames_prevented: u64,
    /// Cumulative immediate fallback path transitions executed without waiting for perf comparisons.
    #[get(type(copy))]
    pub(crate) fallback_transitions: u64,
}