mirx 0.44.0

MIRX binary image format — flat & chunk-based container for ahead-of-time UI assets
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
use super::{GroupPlanes, SurfaceDescriptor, TileGrid, UnitGroup};

/// Remaining bounded operations for allocation-free surface coverage validation.
///
/// One operation is a group/pair visit, selected-unit visit, or spatial query.
/// Indexed queries have the bounded lookup costs of UnitSelection. No budget
/// exhaustion is treated as valid coverage; callers can retry with more work.
/// Constant-space encoded group validation also charges its documented
/// conservative record/index re-resolution costs through this budget.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CoverageBudget {
    remaining: u64,
}

impl CoverageBudget {
    pub const fn new(operations: u64) -> Self {
        Self {
            remaining: operations,
        }
    }
    pub const fn remaining(self) -> u64 {
        self.remaining
    }
    fn spend(&mut self) -> Result<(), CoverageError> {
        self.spend_many(1)
    }
    pub(crate) fn spend_many(&mut self, operations: u64) -> Result<(), CoverageError> {
        self.remaining = self
            .remaining
            .checked_sub(operations)
            .ok_or(CoverageError::BudgetExceeded)?;
        Ok(())
    }
}

impl SurfaceDescriptor {
    /// Checks exact plane coverage with O(1) memory and explicit work limits.
    /// This does not validate checksums, coding support or reference availability.
    pub fn validate_coverage(
        self,
        groups: &[UnitGroup<'_>],
        budget: &mut CoverageBudget,
    ) -> Result<(), CoverageError> {
        self.validate_coverage_by(groups.len(), |index, _| Ok(groups[index]), budget)
    }

    /// Checks that every group targets this surface and no selected units overlap.
    ///
    /// Unlike [`Self::validate_coverage`], holes are permitted. This is the
    /// coverage rule for a frame that retains unchanged samples from a previous
    /// canvas.
    pub fn validate_disjoint_coverage(
        self,
        groups: &[UnitGroup<'_>],
        budget: &mut CoverageBudget,
    ) -> Result<(), CoverageError> {
        self.validate_disjoint_coverage_by(groups.len(), |index, _| Ok(groups[index]), budget)
    }

    pub(crate) fn validate_coverage_by<'a>(
        self,
        count: usize,
        group_at: impl Fn(usize, &mut CoverageBudget) -> Result<UnitGroup<'a>, CoverageError>,
        budget: &mut CoverageBudget,
    ) -> Result<(), CoverageError> {
        for index in 0..count {
            budget.spend()?;
            let group = group_at(index, budget)?;
            if group.surface() != self {
                return Err(CoverageError::SurfaceMismatch);
            }
        }
        for plane in 0..self.plane_count() {
            let geometry = self.plane(plane).unwrap();
            let expected = u64::from(geometry.width()) * u64::from(geometry.height());
            let mut actual = 0u64;
            for index in 0..count {
                budget.spend()?;
                let group = group_at(index, budget)?;
                actual = actual
                    .checked_add(group.covered_area(plane, budget)?)
                    .ok_or(CoverageError::AreaOverflow)?;
            }
            if actual != expected {
                return Err(CoverageError::AreaMismatch {
                    plane,
                    expected,
                    actual,
                });
            }
            for index in 0..count {
                let group = group_at(index, budget)?;
                for other in index + 1..count {
                    if group.overlaps(group_at(other, budget)?, plane, budget)? {
                        return Err(CoverageError::Overlap { plane });
                    }
                }
            }
        }
        Ok(())
    }

    pub(crate) fn validate_disjoint_coverage_by<'a>(
        self,
        count: usize,
        group_at: impl Fn(usize, &mut CoverageBudget) -> Result<UnitGroup<'a>, CoverageError>,
        budget: &mut CoverageBudget,
    ) -> Result<(), CoverageError> {
        for index in 0..count {
            budget.spend()?;
            let group = group_at(index, budget)?;
            if group.surface() != self {
                return Err(CoverageError::SurfaceMismatch);
            }
        }
        for plane in 0..self.plane_count() {
            for index in 0..count {
                let group = group_at(index, budget)?;
                for other in index + 1..count {
                    if group.overlaps(group_at(other, budget)?, plane, budget)? {
                        return Err(CoverageError::Overlap { plane });
                    }
                }
            }
        }
        Ok(())
    }
}

impl UnitGroup<'_> {
    /// Reports overlap in one plane, including groups using different tile sizes.
    pub fn overlaps(
        self,
        other: Self,
        plane: u8,
        budget: &mut CoverageBudget,
    ) -> Result<bool, CoverageError> {
        budget.spend()?;
        if self.surface() != other.surface() {
            return Err(CoverageError::SurfaceMismatch);
        }
        if plane >= self.surface().plane_count() {
            return Err(CoverageError::InvalidPlane(plane));
        }
        if self.is_empty() || other.is_empty() {
            return Ok(false);
        }
        let (Some(grid), Some(other_grid)) = (self.plane_grid(plane), other.plane_grid(plane))
        else {
            return Ok(false);
        };
        if self.len() == grid.len() || other.len() == other_grid.len() {
            return Ok(true);
        }
        let (small, small_grid, large, large_grid) = if self.len() <= other.len() {
            (self, grid, other, other_grid)
        } else {
            (other, other_grid, self, grid)
        };
        if small_grid == large_grid {
            for cell in small.selection().iter() {
                budget.spend()?;
                if large.selection().contains(cell) {
                    return Ok(true);
                }
            }
            return Ok(false);
        }
        for cell in small.selection().iter() {
            budget.spend()?;
            let region = small_grid.get(cell as usize).unwrap();
            let first_row = region.y() / large_grid.tile_height();
            let end_row = region.bottom().div_ceil(large_grid.tile_height());
            if (end_row - first_row) as usize > large.len() {
                for other_cell in large.selection().iter() {
                    budget.spend()?;
                    if region.intersects(large_grid.get(other_cell as usize).unwrap()) {
                        return Ok(true);
                    }
                }
            } else {
                let first_column = region.x() / large_grid.tile_width();
                let end_column = region.right().div_ceil(large_grid.tile_width());
                for row in first_row..end_row {
                    budget.spend()?;
                    let start = row * large_grid.columns() + first_column;
                    let end = row * large_grid.columns() + end_column;
                    if large.selection().range(start..end).unwrap().len() != 0 {
                        return Ok(true);
                    }
                }
            }
        }
        Ok(false)
    }

    fn plane_grid(self, plane: u8) -> Option<TileGrid> {
        if !self.planes().contains(plane) {
            return None;
        }
        let geometry = self.surface().plane(plane)?;
        let first = self.grid().get(0)?;
        let first = match self.planes() {
            GroupPlanes::Joint(_) => first.for_plane(self.surface(), plane).ok()?,
            GroupPlanes::Plane(_) => first,
        };
        TileGrid::new(
            geometry.width(),
            geometry.height(),
            first.width(),
            first.height(),
        )
        .ok()
    }

    fn covered_area(self, plane: u8, budget: &mut CoverageBudget) -> Result<u64, CoverageError> {
        let Some(grid) = self.plane_grid(plane) else {
            return Ok(0);
        };
        if self.len() == grid.len() {
            return Ok(u64::from(grid.width()) * u64::from(grid.height()));
        }
        let mut area = 0u64;
        for cell in self.selection().iter() {
            budget.spend()?;
            let region = grid.get(cell as usize).unwrap();
            area += u64::from(region.width()) * u64::from(region.height());
        }
        Ok(area)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CoverageError {
    BudgetExceeded,
    SurfaceMismatch,
    InvalidPlane(u8),
    AreaOverflow,
    AreaMismatch {
        plane: u8,
        expected: u64,
        actual: u64,
    },
    Overlap {
        plane: u8,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        coding::CodingId,
        image::{ColorDescription, Region, SampleLayout},
        media::{CodingRecord, UnitSelection, UnitSelectionEncoding},
    };

    fn coding() -> CodingRecord<'static> {
        CodingRecord::new(CodingId::new(19), 1, &[])
    }

    #[test]
    fn matching_area_cannot_hide_an_overlap_and_hole() {
        let surface =
            SurfaceDescriptor::new(4, 1, SampleLayout::A8, ColorDescription::NONE).unwrap();
        let left = [0, 0, 0, 0, 1, 0, 0, 0];
        let wrong = [1, 0, 0, 0, 3, 0, 0, 0];
        let right = [2, 0, 0, 0, 3, 0, 0, 0];
        let make = |bytes| {
            UnitGroup::builder(surface, coding(), &[1; 2])
                .with_tiles(1, 1)
                .with_selection(UnitSelection::list(4, bytes).unwrap())
                .build()
                .unwrap()
        };
        assert_eq!(
            surface.validate_coverage(&[make(&left), make(&wrong)], &mut CoverageBudget::new(100)),
            Err(CoverageError::Overlap { plane: 0 })
        );
        assert_eq!(
            surface.validate_coverage(&[make(&left), make(&right)], &mut CoverageBudget::new(100)),
            Ok(())
        );
        assert_eq!(
            surface.validate_coverage(&[make(&left)], &mut CoverageBudget::new(100)),
            Err(CoverageError::AreaMismatch {
                plane: 0,
                expected: 4,
                actual: 2
            })
        );
        assert_eq!(
            surface.validate_disjoint_coverage(&[make(&left)], &mut CoverageBudget::new(100)),
            Ok(())
        );
        assert!(
            !Region::new(0, 0, 1, 1)
                .unwrap()
                .intersects(Region::new(1, 0, 1, 1).unwrap())
        );
        assert!(
            !Region::new(0, 0, 0, 1)
                .unwrap()
                .intersects(Region::new(0, 0, 1, 1).unwrap())
        );
    }

    #[test]
    fn different_grids_and_joint_planar_groups_partition_exactly() {
        let surface =
            SurfaceDescriptor::new(6, 4, SampleLayout::A8, ColorDescription::NONE).unwrap();
        let left = [0, 0, 0, 0, 2, 0, 0, 0];
        let mut right = [0; 48];
        UnitSelectionEncoding::List
            .encode_into(
                24,
                &[3, 4, 5, 9, 10, 11, 15, 16, 17, 21, 22, 23],
                &mut right,
            )
            .unwrap();
        let groups = [
            UnitGroup::builder(surface, coding(), &[1; 2])
                .with_tiles(3, 2)
                .with_selection(UnitSelection::list(4, &left).unwrap())
                .build()
                .unwrap(),
            UnitGroup::builder(surface, coding(), &[1; 12])
                .with_tiles(1, 1)
                .with_selection(UnitSelection::list(24, &right).unwrap())
                .build()
                .unwrap(),
        ];
        assert_eq!(
            surface.validate_coverage(&groups, &mut CoverageBudget::new(100)),
            Ok(())
        );
        let yuv = SurfaceDescriptor::new(
            5,
            3,
            SampleLayout::NV12,
            ColorDescription::BT709_YUV_LIMITED,
        )
        .unwrap();
        let planar = [
            UnitGroup::builder(yuv, coding(), &[1])
                .with_planes(GroupPlanes::Joint(1))
                .build()
                .unwrap(),
            UnitGroup::builder(yuv, coding(), &[1; 6])
                .with_tiles(1, 1)
                .with_planes(GroupPlanes::Plane(1))
                .build()
                .unwrap(),
        ];
        assert_eq!(
            yuv.validate_coverage(&planar, &mut CoverageBudget::new(100)),
            Ok(())
        );
        assert_eq!(
            planar[0].overlaps(planar[1], 0, &mut CoverageBudget::new(10)),
            Ok(false)
        );
        assert_eq!(
            planar[0].overlaps(planar[1], 2, &mut CoverageBudget::new(10)),
            Err(CoverageError::InvalidPlane(2))
        );
    }

    #[test]
    fn huge_sparse_grid_uses_selected_units_instead_of_billions_of_rows() {
        let surface =
            SurfaceDescriptor::new(1, u32::MAX, SampleLayout::A8, ColorDescription::NONE).unwrap();
        let first = [0; 4];
        let last = (u32::MAX - 1).to_le_bytes();
        let upper = UnitGroup::builder(surface, coding(), &[1])
            .with_tiles(1, 1 << 31)
            .with_selection(UnitSelection::list(2, &first).unwrap())
            .build()
            .unwrap();
        let lower = UnitGroup::builder(surface, coding(), &[1])
            .with_tiles(1, 1)
            .with_selection(UnitSelection::list(u32::MAX, &last).unwrap())
            .build()
            .unwrap();
        let mut budget = CoverageBudget::new(3);
        assert_eq!(upper.overlaps(lower, 0, &mut budget), Ok(false));
        assert_eq!(budget.remaining(), 0);
        assert_eq!(
            upper.overlaps(lower, 0, &mut budget),
            Err(CoverageError::BudgetExceeded)
        );
        let whole = UnitGroup::builder(surface, coding(), &[1]).build().unwrap();
        assert_eq!(
            surface.validate_coverage(&[whole], &mut CoverageBudget::new(2)),
            Ok(())
        );
    }

    #[test]
    fn budget_exhaustion_and_surface_mismatch_never_pass_validation() {
        let surface =
            SurfaceDescriptor::new(1, 1, SampleLayout::A8, ColorDescription::NONE).unwrap();
        let group = UnitGroup::builder(surface, coding(), &[1]).build().unwrap();
        let mut budget = CoverageBudget::new(1);
        assert_eq!(
            surface.validate_coverage(&[group], &mut budget),
            Err(CoverageError::BudgetExceeded)
        );
        assert_eq!(budget.remaining(), 0);
        assert_eq!(
            surface.validate_coverage(&[group], &mut CoverageBudget::new(2)),
            Ok(())
        );
        let empty = SurfaceDescriptor::new(0, 1, SampleLayout::A8, ColorDescription::NONE).unwrap();
        assert_eq!(
            empty.validate_coverage(&[], &mut CoverageBudget::new(0)),
            Ok(())
        );
        assert_eq!(
            empty.validate_coverage(&[group], &mut CoverageBudget::new(10)),
            Err(CoverageError::SurfaceMismatch)
        );
        let huge =
            SurfaceDescriptor::new(u32::MAX, u32::MAX, SampleLayout::A8, ColorDescription::NONE)
                .unwrap();
        let huge_group = UnitGroup::builder(huge, coding(), &[1]).build().unwrap();
        assert_eq!(
            huge.validate_coverage(&[huge_group, huge_group], &mut CoverageBudget::new(10)),
            Err(CoverageError::AreaOverflow)
        );
    }
}