fn draw_software()

in native/desktop-linux-sample/src/sample_linux.rs [243:298]


fn draw_software(data: &SoftwareDrawData, physical_size: PhysicalSize, scale: f64, window_state: &WindowState) {
    const BYTES_PER_PIXEL: u8 = 4;
    let drag_source_indicator_heigh = 100. * scale;
    let canvas = {
        let len = usize::try_from(physical_size.height.0 * data.stride).unwrap();
        unsafe { std::slice::from_raw_parts_mut(data.canvas, len) }
    };
    let w = f64::from(physical_size.width.0);
    let h = f64::from(physical_size.height.0);
    let line_thickness = 5.0 * scale;

    // Order of bytes in `pixel` is [b, g, r, a] (for the Argb8888 format)
    for (pixel, i) in canvas.chunks_exact_mut(BYTES_PER_PIXEL.into()).zip(1u32..) {
        let i = f64::from(i);
        let x = i % w;
        let y = (i / f64::from(data.stride)) * f64::from(BYTES_PER_PIXEL);
        if between(
            x,
            DRAG_AND_DROP_LEFT_OF * scale,
            DRAG_AND_DROP_LEFT_OF.mul_add(scale, line_thickness),
        ) {
            pixel[0] = 0;
            pixel[1] = 0;
            pixel[2] = 0;
        } else if between(x, line_thickness,  line_thickness * 2.0)  // left border
           || between(y, line_thickness,  line_thickness * 2.0)  // top border
           || between(x, line_thickness.mul_add(-2.0, w), w - line_thickness)  // right border
           || between(y, line_thickness.mul_add(-2.0, h), h - line_thickness)  // bottom border
           || between(x, (i / h) - (line_thickness / 2.0), (i / h) + (line_thickness / 2.0))
        {
            pixel[0] = 0;
            pixel[1] = 0;
            pixel[2] = 255;
        } else if x < DRAG_AND_DROP_LEFT_OF
            && window_state.drag_and_drop_source
            && between(y, drag_source_indicator_heigh, drag_source_indicator_heigh + line_thickness)
        {
            pixel[0] = 255;
            pixel[1] = 0;
            pixel[2] = 0;
        } else if x < DRAG_AND_DROP_LEFT_OF && window_state.drag_and_drop_target {
            pixel[0] = 128;
            pixel[1] = 0;
            pixel[2] = 0;
        } else if window_state.active {
            pixel[0] = 255;
            pixel[1] = 255;
            pixel[2] = 255;
        } else {
            pixel[0] = 128;
            pixel[1] = 128;
            pixel[2] = 128;
        }
        pixel[3] = 255;
    }
}