livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
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
//! Provides a software renderer that can be used without a GPU. The renderer is
//! surprisingly fast and can be considered the default rendering backend.

use crate::platform::prelude::*;
use alloc::rc::Rc;
use core::{mem, ops::Deref};

use super::{
    entity::Entity,
    path_based_text_engine::{Font, Label, TextEngine},
    resource::{self, ResourceAllocator},
    FillShader, FontKind, Scene, SceneManager, SharedOwnership, Transform,
};
use crate::{layout::LayoutState, settings};
#[cfg(feature = "image")]
use image::ImageBuffer;
use tiny_skia::{
    BlendMode, Color, FillRule, FilterQuality, GradientStop, LinearGradient, Paint, Path,
    PathBuilder, Pattern, Pixmap, PixmapMut, Point, Rect, Shader, SpreadMode, Stroke,
};

#[cfg(feature = "image")]
pub use image::{self, RgbaImage};

struct SkiaBuilder(PathBuilder);

type SkiaPath = Option<UnsafeRc<Path>>;
type SkiaImage = UnsafeRc<Pixmap>;
type SkiaFont = Font<SkiaPath>;
type SkiaLabel = Label<SkiaPath>;

impl resource::PathBuilder for SkiaBuilder {
    type Path = SkiaPath;

    fn move_to(&mut self, x: f32, y: f32) {
        self.0.move_to(x, y)
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.0.line_to(x, y)
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.0.quad_to(x1, y1, x, y)
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.0.cubic_to(x1, y1, x2, y2, x, y)
    }

    fn close(&mut self) {
        self.0.close()
    }

    fn finish(self) -> Self::Path {
        self.0.finish().map(UnsafeRc::new)
    }
}

fn convert_color(&[r, g, b, a]: &[f32; 4]) -> Color {
    Color::from_rgba(r, g, b, a).unwrap()
}

fn convert_transform(transform: &Transform) -> tiny_skia::Transform {
    tiny_skia::Transform::from_row(
        transform.scale_x,
        0.0,
        0.0,
        transform.scale_y,
        transform.x,
        transform.y,
    )
}

struct SkiaAllocator {
    text_engine: TextEngine,
}

impl ResourceAllocator for SkiaAllocator {
    type PathBuilder = SkiaBuilder;
    type Path = SkiaPath;
    type Image = SkiaImage;
    type Font = SkiaFont;
    type Label = SkiaLabel;

    fn path_builder(&mut self) -> Self::PathBuilder {
        path_builder()
    }

    fn create_image(&mut self, _data: &[u8]) -> Option<(Self::Image, f32)> {
        #[cfg(feature = "image")]
        {
            let mut buf = image::load_from_memory(_data).ok()?.to_rgba8();

            // Premultiplication
            for [r, g, b, a] in bytemuck::cast_slice_mut::<u8, [u8; 4]>(&mut buf) {
                // If it's opaque we can skip the entire pixel. However this
                // hurts vectorization, so we want to avoid it if the compiler
                // can vectorize the loop. WASM, PowerPC, and MIPS are
                // unaffected at the moment.
                #[cfg(not(any(target_feature = "avx2", target_feature = "neon")))]
                if *a == 0xFF {
                    continue;
                }
                let a = *a as u16;
                *r = ((*r as u16 * a) / 255) as u8;
                *g = ((*g as u16 * a) / 255) as u8;
                *b = ((*b as u16 * a) / 255) as u8;
            }

            let (width, height) = (buf.width(), buf.height());

            let pixmap = Pixmap::from_vec(
                buf.into_raw(),
                tiny_skia_path::IntSize::from_wh(width, height)?,
            )?;

            Some((UnsafeRc::new(pixmap), width as f32 / height as f32))
        }
        #[cfg(not(feature = "image"))]
        {
            None
        }
    }

    fn create_font(&mut self, font: Option<&settings::Font>, kind: FontKind) -> Self::Font {
        self.text_engine.create_font(font, kind)
    }

    fn create_label(
        &mut self,
        text: &str,
        font: &mut Self::Font,
        max_width: Option<f32>,
    ) -> Self::Label {
        self.text_engine
            .create_label(path_builder, text, font, max_width)
    }

    fn update_label(
        &mut self,
        label: &mut Self::Label,
        text: &str,
        font: &mut Self::Font,
        max_width: Option<f32>,
    ) {
        self.text_engine
            .update_label(path_builder, label, text, font, max_width)
    }
}

fn path_builder() -> SkiaBuilder {
    SkiaBuilder(PathBuilder::new())
}

/// The software renderer allows rendering layouts entirely on the CPU. This is
/// surprisingly fast and can be considered the default renderer. There are two
/// versions of the software renderer. This version of the software renderer
/// does not own the image to render into. This allows the caller to manage
/// their own image buffer.
pub struct BorrowedRenderer {
    allocator: SkiaAllocator,
    scene_manager: SceneManager<SkiaPath, SkiaImage, SkiaFont, SkiaLabel>,
    background: Pixmap,
    min_y: f32,
    max_y: f32,
}

struct UnsafeRc<T>(Rc<T>);

impl<T: Send + Sync> Deref for UnsafeRc<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> UnsafeRc<T> {
    fn new(val: T) -> Self {
        Self(Rc::new(val))
    }
}

impl<T: Send + Sync> SharedOwnership for UnsafeRc<T> {
    fn share(&self) -> Self {
        Self(self.0.share())
    }
}

// Safety: This is safe because the BorrowedSoftwareRenderer and the
// SceneManager never share any of their resources with anyone. For the
// BorrowedSoftwareRenderer this is trivially true as it doesn't share any its
// fields with anyone, you provide the image to render into yourself. For the
// SceneManager it's harder to prove. However as long as the trait bounds for
// the ResourceAllocator's Image and Path types do not require Sync or Send,
// then the SceneManager simply can't share any of the allocated resources
// across any threads at all.
unsafe impl<T: Send + Sync> Send for UnsafeRc<T> {}

// Safety: The BorrowedSoftwareRenderer only has a render method which requires
// exclusive access. The SceneManager could still mess it up. But as long as the
// ResourceAllocator's Image and Path types do not require Sync or Send, it
// can't make use of the Sync bound in any dangerous way anyway.
unsafe impl<T: Send + Sync> Sync for UnsafeRc<T> {}

impl Default for BorrowedRenderer {
    fn default() -> Self {
        Self::new()
    }
}

impl BorrowedRenderer {
    /// Creates a new software renderer.
    pub fn new() -> Self {
        let mut allocator = SkiaAllocator {
            text_engine: TextEngine::new(),
        };
        let scene_manager = SceneManager::new(&mut allocator);
        Self {
            allocator,
            scene_manager,
            background: Pixmap::new(1, 1).unwrap(),
            min_y: f32::INFINITY,
            max_y: f32::NEG_INFINITY,
        }
    }

    /// Renders the layout state provided into the image buffer provided. The
    /// image has to be an array of `RGBA8` encoded pixels (red, green, blue,
    /// alpha with each channel being an u8). Some frameworks may over allocate
    /// an image's dimensions. So an image with dimensions `100x50` may be over
    /// allocated as `128x64`. In that case you provide the real dimensions of
    /// `100x50` as the width and height, but a stride of `128` pixels as that
    /// correlates with the real width of the underlying buffer. It may detect
    /// that the layout got resized. In that case it returns the new ideal size.
    /// This is just a hint and can be ignored entirely. The image is always
    /// rendered with the resolution provided. By default the renderer will try
    /// not to redraw parts of the image that haven't changed. You can force a
    /// redraw in case the image provided or its contents have changed.
    pub fn render(
        &mut self,
        state: &LayoutState,
        image: &mut [u8],
        [width, height]: [u32; 2],
        stride: u32,
        force_redraw: bool,
    ) -> Option<(f32, f32)> {
        let mut frame_buffer = PixmapMut::from_bytes(image, stride, height).unwrap();

        if stride != self.background.width() || height != self.background.height() {
            self.background = Pixmap::new(stride, height).unwrap();
        }

        let new_resolution =
            self.scene_manager
                .update_scene(&mut self.allocator, (width as _, height as _), state);

        let scene = self.scene_manager.scene();
        let rectangle = scene.rectangle();
        let rectangle = rectangle.as_deref().unwrap();

        let bottom_layer_changed = scene.bottom_layer_changed();

        let mut background = self.background.as_mut();

        if bottom_layer_changed {
            fill_background(scene, &mut background, width, height);
            render_layer(&mut background, scene.bottom_layer(), rectangle);
        }

        let top_layer = scene.top_layer();

        let (min_y, max_y) = calculate_bounds(top_layer);
        let min_y = mem::replace(&mut self.min_y, min_y).min(min_y);
        let max_y = mem::replace(&mut self.max_y, max_y).max(max_y);

        if force_redraw || bottom_layer_changed {
            frame_buffer
                .data_mut()
                .copy_from_slice(background.data_mut());
        } else if min_y <= max_y {
            let stride = 4 * stride as usize;
            let min_y = stride * (min_y - 1.0) as usize;
            let max_y = stride * ((max_y + 2.0) as usize).min(height as usize);

            frame_buffer.data_mut()[min_y..max_y]
                .copy_from_slice(&background.data_mut()[min_y..max_y]);
        }

        render_layer(&mut frame_buffer, top_layer, rectangle);

        new_resolution
    }
}

/// The software renderer allows rendering layouts entirely on the CPU. This is
/// surprisingly fast and can be considered the default renderer. There are two
/// versions of the software renderer. This version of the software renderer
/// owns the image it renders into.
pub struct Renderer {
    renderer: BorrowedRenderer,
    frame_buffer: Pixmap,
}

impl Default for Renderer {
    fn default() -> Self {
        Self::new()
    }
}

impl Renderer {
    /// Creates a new software renderer.
    pub fn new() -> Self {
        Self {
            renderer: BorrowedRenderer::new(),
            frame_buffer: Pixmap::new(1, 1).unwrap(),
        }
    }

    /// Renders the layout state provided with the chosen resolution. It may
    /// detect that the layout got resized. In that case it returns the new
    /// ideal size. This is just a hint and can be ignored entirely. The image
    /// is always rendered with the resolution provided.
    pub fn render(&mut self, state: &LayoutState, [width, height]: [u32; 2]) -> Option<(f32, f32)> {
        if width != self.frame_buffer.width() || height != self.frame_buffer.height() {
            self.frame_buffer = Pixmap::new(width, height).unwrap();
        }

        self.renderer.render(
            state,
            self.frame_buffer.data_mut(),
            [width, height],
            width,
            false,
        )
    }

    /// Accesses the image as a byte slice of RGBA8 encoded pixels (red, green,
    /// blue, alpha with each channel being an u8).
    pub fn image_data(&self) -> &[u8] {
        self.frame_buffer.data()
    }

    /// Turns the whole renderer into the underlying image buffer of RGBA8
    /// encoded pixels (red, green, blue, alpha with each channel being an u8).
    pub fn into_image_data(self) -> Vec<u8> {
        self.frame_buffer.take()
    }

    /// Accesses the image.
    #[cfg(feature = "image")]
    pub fn image(&self) -> ImageBuffer<image::Rgba<u8>, &[u8]> {
        ImageBuffer::from_raw(
            self.frame_buffer.width(),
            self.frame_buffer.height(),
            self.frame_buffer.data(),
        )
        .unwrap()
    }

    /// Turns the whole renderer into the underlying image.
    #[cfg(feature = "image")]
    pub fn into_image(self) -> RgbaImage {
        RgbaImage::from_raw(
            self.frame_buffer.width(),
            self.frame_buffer.height(),
            self.frame_buffer.take(),
        )
        .unwrap()
    }
}

fn render_layer(
    canvas: &mut PixmapMut<'_>,
    layer: &[Entity<SkiaPath, SkiaImage, SkiaLabel>],
    rectangle: &Path,
) {
    for entity in layer {
        match entity {
            Entity::FillPath(path, shader, transform) => {
                if let Some(path) = path.as_deref() {
                    let paint = convert_shader(
                        shader,
                        path,
                        |path| {
                            let bounds = path.bounds();
                            (bounds.top(), bounds.bottom())
                        },
                        |path| {
                            let bounds = path.bounds();
                            (bounds.left(), bounds.right())
                        },
                    );

                    canvas.fill_path(
                        path,
                        &paint,
                        FillRule::Winding,
                        convert_transform(transform),
                        None,
                    );
                }
            }
            Entity::StrokePath(path, stroke_width, color, transform) => {
                if let Some(path) = path.as_deref() {
                    canvas.stroke_path(
                        path,
                        &Paint {
                            shader: Shader::SolidColor(convert_color(color)),
                            anti_alias: true,
                            ..Default::default()
                        },
                        &Stroke {
                            width: *stroke_width,
                            ..Default::default()
                        },
                        convert_transform(transform),
                        None,
                    );
                }
            }
            Entity::Image(image, transform) => {
                canvas.fill_path(
                    rectangle,
                    &Paint {
                        shader: Pattern::new(
                            image.as_ref(),
                            SpreadMode::Pad,
                            FilterQuality::Bilinear,
                            1.0,
                            tiny_skia::Transform::from_scale(
                                1.0 / image.width() as f32,
                                1.0 / image.height() as f32,
                            ),
                        ),
                        anti_alias: true,
                        ..Default::default()
                    },
                    FillRule::Winding,
                    convert_transform(transform),
                    None,
                );
            }
            Entity::Label(label, shader, transform) => {
                let label = label.read().unwrap();
                let label = &*label;

                let paint = convert_shader(
                    shader,
                    label,
                    |label| {
                        let (mut top, mut bottom) = (f32::INFINITY, f32::NEG_INFINITY);
                        for glyph in label.glyphs() {
                            if let Some(path) = &glyph.path {
                                let bounds = path.bounds();
                                top = top.min(bounds.top());
                                bottom = bottom.max(bounds.bottom());
                            }
                        }
                        if bottom < top {
                            (0.0, 0.0)
                        } else {
                            (top, bottom)
                        }
                    },
                    |label| {
                        let (mut left, mut right) = (f32::INFINITY, f32::NEG_INFINITY);
                        for glyph in label.glyphs() {
                            if let Some(path) = &glyph.path {
                                let bounds = path.bounds();
                                left = left.min(bounds.left());
                                right = right.max(bounds.right());
                            }
                        }
                        if right < left {
                            (0.0, 0.0)
                        } else {
                            (left, right)
                        }
                    },
                );

                let transform = transform.pre_scale(label.scale(), label.scale());

                for glyph in label.glyphs() {
                    if let Some(path) = &glyph.path {
                        let transform = transform.pre_translate(glyph.x, glyph.y);

                        let glyph_paint;
                        let paint = if let Some(color) = &glyph.color {
                            glyph_paint = Paint {
                                shader: Shader::SolidColor(convert_color(color)),
                                ..paint
                            };
                            &glyph_paint
                        } else {
                            &paint
                        };

                        canvas.fill_path(
                            path,
                            paint,
                            FillRule::Winding,
                            convert_transform(&transform),
                            None,
                        );
                    }
                }
            }
        }
    }
}

fn convert_shader<T>(
    shader: &FillShader,
    has_bounds: &T,
    calculate_top_bottom: impl FnOnce(&T) -> (f32, f32),
    calculate_left_right: impl FnOnce(&T) -> (f32, f32),
) -> Paint<'static> {
    let shader = match shader {
        FillShader::SolidColor(col) => Shader::SolidColor(convert_color(col)),
        FillShader::VerticalGradient(top, bottom) => {
            let (bound_top, bound_bottom) = calculate_top_bottom(has_bounds);
            LinearGradient::new(
                Point::from_xy(0.0, bound_top),
                Point::from_xy(0.0, bound_bottom),
                vec![
                    GradientStop::new(0.0, convert_color(top)),
                    GradientStop::new(1.0, convert_color(bottom)),
                ],
                SpreadMode::Pad,
                tiny_skia::Transform::identity(),
            )
            .unwrap()
        }
        FillShader::HorizontalGradient(left, right) => {
            let (bound_left, bound_right) = calculate_left_right(has_bounds);
            LinearGradient::new(
                Point::from_xy(bound_left, 0.0),
                Point::from_xy(bound_right, 0.0),
                vec![
                    GradientStop::new(0.0, convert_color(left)),
                    GradientStop::new(1.0, convert_color(right)),
                ],
                SpreadMode::Pad,
                tiny_skia::Transform::identity(),
            )
            .unwrap()
        }
    };

    Paint {
        shader,
        anti_alias: true,
        ..Default::default()
    }
}

fn fill_background(
    scene: &Scene<SkiaPath, SkiaImage, SkiaLabel>,
    background: &mut PixmapMut<'_>,
    width: u32,
    height: u32,
) {
    match scene.background() {
        Some(shader) => match shader {
            FillShader::SolidColor(color) => {
                background
                    .pixels_mut()
                    .fill(convert_color(color).premultiply().to_color_u8());
            }
            FillShader::VerticalGradient(top, bottom) => {
                background.fill_rect(
                    Rect::from_xywh(0.0, 0.0, width as _, height as _).unwrap(),
                    &Paint {
                        shader: LinearGradient::new(
                            Point::from_xy(0.0, 0.0),
                            Point::from_xy(0.0, height as _),
                            vec![
                                GradientStop::new(0.0, convert_color(top)),
                                GradientStop::new(1.0, convert_color(bottom)),
                            ],
                            SpreadMode::Pad,
                            tiny_skia::Transform::identity(),
                        )
                        .unwrap(),
                        blend_mode: BlendMode::Source,
                        ..Default::default()
                    },
                    tiny_skia::Transform::identity(),
                    None,
                );
            }
            FillShader::HorizontalGradient(left, right) => {
                background.fill_rect(
                    Rect::from_xywh(0.0, 0.0, width as _, height as _).unwrap(),
                    &Paint {
                        shader: LinearGradient::new(
                            Point::from_xy(0.0, 0.0),
                            Point::from_xy(width as _, 0.0),
                            vec![
                                GradientStop::new(0.0, convert_color(left)),
                                GradientStop::new(1.0, convert_color(right)),
                            ],
                            SpreadMode::Pad,
                            tiny_skia::Transform::identity(),
                        )
                        .unwrap(),
                        blend_mode: BlendMode::Source,
                        ..Default::default()
                    },
                    tiny_skia::Transform::identity(),
                    None,
                );
            }
        },
        None => background.data_mut().fill(0),
    }
}

fn calculate_bounds(layer: &[Entity<SkiaPath, SkiaImage, SkiaLabel>]) -> (f32, f32) {
    let (mut min_y, mut max_y) = (f32::INFINITY, f32::NEG_INFINITY);
    for entity in layer.iter() {
        match entity {
            Entity::FillPath(path, _, transform) => {
                if let Some(path) = &**path {
                    let bounds = path.bounds();
                    for y in [bounds.top(), bounds.bottom()] {
                        let transformed_y = transform.transform_y(y);
                        min_y = min_y.min(transformed_y);
                        max_y = max_y.max(transformed_y);
                    }
                }
            }
            Entity::StrokePath(path, radius, _, transform) => {
                if let Some(path) = &**path {
                    let radius = transform.scale_y * radius;
                    let bounds = path.bounds();
                    for y in [bounds.top(), bounds.bottom()] {
                        let transformed_y = transform.transform_y(y);
                        min_y = min_y.min(transformed_y - radius);
                        max_y = max_y.max(transformed_y + radius);
                    }
                }
            }
            Entity::Image(_, transform) => {
                for y in [0.0, 1.0] {
                    let transformed_y = transform.transform_y(y);
                    min_y = min_y.min(transformed_y);
                    max_y = max_y.max(transformed_y);
                }
            }
            Entity::Label(label, _, transform) => {
                for glyph in label.read().unwrap().glyphs() {
                    if let Some(path) = &glyph.path {
                        let transform = transform.pre_translate(glyph.x, glyph.y);
                        let bounds = path.bounds();
                        for y in [bounds.top(), bounds.bottom()] {
                            let transformed_y = transform.transform_y(y);
                            min_y = min_y.min(transformed_y);
                            max_y = max_y.max(transformed_y);
                        }
                    }
                }
            }
        }
    }
    (min_y, max_y)
}