mirui 0.42.0

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
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
//! Compose a hybrid backend out of two Dummy backends and verify every
//! method routes to the intended side.
//!
//! Each Dummy counts method invocations. The `Hybrid` instance exposes both
//! dummies as struct fields, so we can read the counters directly.

use std::cell::Cell;

use mirui::render::canvas::{Canvas, Paint};
use mirui::render::command::CompositeMode;
use mirui::render::path::Path;
use mirui::render::texture::{ColorFormat, Texture};
use mirui::types::{Color, Fixed, Point, Rect, Transform};
use mirui_macros::compose_backend;

#[derive(Default)]
struct Counts {
    fill_path: Cell<u32>,
    stroke_path: Cell<u32>,
    blit: Cell<u32>,
    clear: Cell<u32>,
    draw_label: Cell<u32>,
    flush: Cell<u32>,
    fill_rect: Cell<u32>,
    stroke_rect: Cell<u32>,
    draw_line: Cell<u32>,
    draw_arc: Cell<u32>,
}

struct Dummy {
    counts: Counts,
}

impl Dummy {
    fn new() -> Self {
        Self {
            counts: Counts::default(),
        }
    }
}

impl Canvas for Dummy {
    fn fill_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::FillRule,
    ) {
        self.counts.fill_path.set(self.counts.fill_path.get() + 1);
    }
    fn stroke_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: Fixed,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::LineCap,
        _: ::mirui::render::raster::LineJoin,
        _: ::mirui::types::Fixed,
        _: &[Fixed],
    ) {
        self.counts
            .stroke_path
            .set(self.counts.stroke_path.get() + 1);
    }
    fn blit(
        &mut self,
        _: &Texture,
        _: &Rect,
        _: Point,
        _: Point,
        _: &Rect,
        _: u8,
        _: Fixed,
        _: CompositeMode,
    ) {
        self.counts.blit.set(self.counts.blit.get() + 1);
    }
    fn clear(&mut self, _: &Rect, _: &Color) {
        self.counts.clear.set(self.counts.clear.get() + 1);
    }
    fn draw_label(
        &mut self,
        _: &Point,
        _: &str,
        _: &mirui::render::font::Font,
        _: &Rect,
        _: &Color,
        _: u8,
    ) {
        self.counts.draw_label.set(self.counts.draw_label.get() + 1);
    }
    fn flush(&mut self) {
        self.counts.flush.set(self.counts.flush.get() + 1);
    }
    // Override default impls so the counter actually gets hit without going
    // through fill_path / stroke_path.
    fn fill_rect(&mut self, _: &Rect, _: &Rect, _: &Color, _: Fixed, _: u8) {
        self.counts.fill_rect.set(self.counts.fill_rect.get() + 1);
    }
    fn stroke_rect(&mut self, _: &Rect, _: &Rect, _: Fixed, _: &Color, _: Fixed, _: u8) {
        self.counts
            .stroke_rect
            .set(self.counts.stroke_rect.get() + 1);
    }
    fn draw_line(&mut self, _: Point, _: Point, _: &Rect, _: Fixed, _: &Color, _: u8) {
        self.counts.draw_line.set(self.counts.draw_line.get() + 1);
    }
    fn draw_arc(
        &mut self,
        _: Point,
        _: Fixed,
        _: Fixed,
        _: Fixed,
        _: &Rect,
        _: Fixed,
        _: &Color,
        _: u8,
    ) {
        self.counts.draw_arc.set(self.counts.draw_arc.get() + 1);
    }
}

compose_backend! {
    pub struct Hybrid {
        sw: Dummy,
        gpu: Dummy,
    }
    route {
        default => sw,
        blit => gpu,
        clear => gpu,
        fill_rect => gpu,
    }
}

fn fresh_hybrid() -> Hybrid<Dummy, Dummy> {
    Hybrid {
        sw: Dummy::new(),
        gpu: Dummy::new(),
    }
}

fn zero_rect() -> Rect {
    Rect::new(0, 0, 4, 4)
}

fn dummy_texture_buf() -> Vec<u8> {
    vec![0u8; 4 * 4 * 4]
}

#[test]
fn default_methods_route_to_sw() {
    let mut h = fresh_hybrid();
    let path = Path::new();
    let rect = zero_rect();
    let color = Color::rgb(0, 0, 0);
    let paint = Paint::Color(color.into());

    h.fill_path(
        &path,
        &rect,
        &paint,
        255,
        ::mirui::render::raster::FillRule::EvenOdd,
    );
    h.stroke_path(
        &path,
        &rect,
        Fixed::ONE,
        &paint,
        255,
        ::mirui::render::raster::LineCap::Butt,
        ::mirui::render::raster::LineJoin::Miter,
        ::mirui::types::Fixed::from_int(4),
        &[],
    );
    let font = mirui::render::font::Font::bitmap_8x8();
    h.draw_label(&Point::ZERO, "x", &font, &rect, &color, 255);
    h.flush();

    assert_eq!(h.sw.counts.fill_path.get(), 1);
    assert_eq!(h.sw.counts.stroke_path.get(), 1);
    assert_eq!(h.sw.counts.draw_label.get(), 1);
    assert_eq!(h.sw.counts.flush.get(), 1);
    assert_eq!(h.gpu.counts.fill_path.get(), 0);
}

#[test]
fn explicit_routes_go_to_gpu() {
    let mut h = fresh_hybrid();
    let mut buf = dummy_texture_buf();
    let tex = Texture::new(&mut buf, 4, 4, ColorFormat::RGBA8888);
    let rect = zero_rect();
    let color = Color::rgb(0, 0, 0);

    h.blit(
        &tex,
        &rect,
        Point::ZERO,
        Point::ZERO,
        &rect,
        255,
        Fixed::ZERO,
        CompositeMode::SourceOver,
    );
    h.clear(&rect, &color);
    h.fill_rect(&rect, &rect, &color, Fixed::ZERO, 255);

    assert_eq!(h.gpu.counts.blit.get(), 1);
    assert_eq!(h.gpu.counts.clear.get(), 1);
    assert_eq!(h.gpu.counts.fill_rect.get(), 1);
    assert_eq!(h.sw.counts.blit.get(), 0);
    assert_eq!(h.sw.counts.clear.get(), 0);
    assert_eq!(h.sw.counts.fill_rect.get(), 0);
}

#[test]
fn unrouted_default_impl_methods_fall_through_to_trait_default() {
    // stroke_rect, draw_line, draw_arc were not routed. They should go
    // through the Canvas trait default, which ultimately calls
    // stroke_path on the default backend (sw).
    let mut h = fresh_hybrid();
    let rect = zero_rect();
    let color = Color::rgb(0, 0, 0);

    h.stroke_rect(&rect, &rect, Fixed::ONE, &color, Fixed::ZERO, 255);
    h.draw_line(Point::ZERO, Point::ZERO, &rect, Fixed::ONE, &color, 255);
    h.draw_arc(
        Point::ZERO,
        Fixed::from_int(4),
        Fixed::ZERO,
        Fixed::from_int(90),
        &rect,
        Fixed::ONE,
        &color,
        255,
    );

    // sw counters: each default-impl call funnels into stroke_path.
    // Dummy overrides stroke_rect/draw_line/draw_arc so those are NOT called
    // on sw — instead the Hybrid's trait default path took over (since we
    // didn't route them) and invoked sw.stroke_path directly.
    assert_eq!(h.sw.counts.stroke_path.get(), 3);
    assert_eq!(h.sw.counts.stroke_rect.get(), 0);
    assert_eq!(h.sw.counts.draw_line.get(), 0);
    assert_eq!(h.sw.counts.draw_arc.get(), 0);
}

/// A backend that borrows a pixel buffer, giving it a real lifetime parameter
/// so we can prove the macro handles `BorrowedDummy<'fb>` as a generic type
/// argument without needing the Hybrid struct itself to declare `'fb`.
struct BorrowedDummy<'fb> {
    buf: &'fb mut [u8],
    fills: Cell<u32>,
}

impl<'fb> BorrowedDummy<'fb> {
    fn new(buf: &'fb mut [u8]) -> Self {
        Self {
            buf,
            fills: Cell::new(0),
        }
    }
}

impl<'fb> Canvas for BorrowedDummy<'fb> {
    fn fill_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::FillRule,
    ) {
        // Touch the borrowed buffer so the lifetime actually matters at the
        // call site — otherwise `'fb` could be optimised away and the test
        // would be vacuous.
        if !self.buf.is_empty() {
            self.buf[0] = self.buf[0].wrapping_add(1);
        }
        self.fills.set(self.fills.get() + 1);
    }
    fn stroke_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: Fixed,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::LineCap,
        _: ::mirui::render::raster::LineJoin,
        _: ::mirui::types::Fixed,
        _: &[Fixed],
    ) {
    }
    fn blit(
        &mut self,
        _: &Texture,
        _: &Rect,
        _: Point,
        _: Point,
        _: &Rect,
        _: u8,
        _: Fixed,
        _: CompositeMode,
    ) {
    }
    fn clear(&mut self, _: &Rect, _: &Color) {}
    fn draw_label(
        &mut self,
        _: &Point,
        _: &str,
        _: &mirui::render::font::Font,
        _: &Rect,
        _: &Color,
        _: u8,
    ) {
    }
    fn flush(&mut self) {}
}

struct PlainDummy;
impl Canvas for PlainDummy {
    fn fill_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::FillRule,
    ) {
    }
    fn stroke_path(
        &mut self,
        _: &Path,
        _: &Rect,
        _: Fixed,
        _: &Paint,
        _: u8,
        _: ::mirui::render::raster::LineCap,
        _: ::mirui::render::raster::LineJoin,
        _: ::mirui::types::Fixed,
        _: &[Fixed],
    ) {
    }
    fn blit(
        &mut self,
        _: &Texture,
        _: &Rect,
        _: Point,
        _: Point,
        _: &Rect,
        _: u8,
        _: Fixed,
        _: CompositeMode,
    ) {
    }
    fn clear(&mut self, _: &Rect, _: &Color) {}
    fn draw_label(
        &mut self,
        _: &Point,
        _: &str,
        _: &mirui::render::font::Font,
        _: &Rect,
        _: &Color,
        _: u8,
    ) {
    }
    fn flush(&mut self) {}
}

compose_backend! {
    pub struct HybridWithLifetime {
        borrowed: BorrowedDummy,
        plain: PlainDummy,
    }
    route {
        default => borrowed,
        blit => plain,
    }
}

#[test]
fn hybrid_is_a_renderer_and_dispatches_drawcommands() {
    // Verifies the Renderer impl the macro emits alongside Canvas.
    // Sending a DrawCommand::Blit should reach the field that owns `blit`
    // in the route table.
    use mirui::render::DrawCommand;
    use mirui::render::renderer::Renderer;

    let mut h = fresh_hybrid();
    let mut buf = dummy_texture_buf();
    let tex = Texture::new(&mut buf, 4, 4, ColorFormat::RGBA8888);
    let rect = zero_rect();

    Renderer::draw(
        &mut h,
        &DrawCommand::Blit {
            pos: Point::ZERO,
            size: Point::ZERO,
            transform: Transform::IDENTITY,
            quad: None,
            texture: &tex,
            opa: 255,
            radius: Fixed::ZERO,
            composite: mirui::render::CompositeMode::SourceOver,
        },
        &rect,
    );

    assert_eq!(h.gpu.counts.blit.get(), 1);
    assert_eq!(h.sw.counts.blit.get(), 0);
}

#[test]
fn hybrid_accepts_backend_with_lifetime_parameter() {
    let mut buf = [0u8; 8];
    let borrowed = BorrowedDummy::new(&mut buf);
    let plain = PlainDummy;

    // The hybrid type itself takes two generic parameters — no lifetime on
    // Hybrid, the borrow in BorrowedDummy<'_> gets threaded through the
    // generic slot.
    let mut h: HybridWithLifetime<BorrowedDummy<'_>, PlainDummy> =
        HybridWithLifetime { borrowed, plain };

    let rect = Rect::new(0, 0, 4, 4);
    let path = Path::new();
    let paint = Paint::Color(Color::rgb(0, 0, 0).into());
    h.fill_path(
        &path,
        &rect,
        &paint,
        255,
        ::mirui::render::raster::FillRule::EvenOdd,
    );

    assert_eq!(h.borrowed.fills.get(), 1);
    // Side effect through the borrowed slice proves the lifetime really is
    // being honoured end-to-end.
    assert_eq!(h.borrowed.buf[0], 1);
}