skia-rs-gpu 0.2.3

GPU backends for skia-rs
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
//! Image tiling for GPU rendering.
//!
//! This module provides utilities for tiling images across surfaces,
//! handling different tile modes and transformations.

use skia_rs_core::{Matrix, Point, Rect, Scalar};

/// Tile mode for image edges.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TileMode {
    /// Clamp to edge pixels.
    #[default]
    Clamp,
    /// Repeat the image.
    Repeat,
    /// Mirror the image.
    Mirror,
    /// Transparent outside bounds.
    Decal,
}

/// Image tiling configuration.
#[derive(Debug, Clone)]
pub struct TileConfig {
    /// Horizontal tile mode.
    pub tile_x: TileMode,
    /// Vertical tile mode.
    pub tile_y: TileMode,
    /// Source rect within the image (normalized 0-1).
    pub source_rect: Rect,
    /// Destination rect.
    pub dest_rect: Rect,
    /// Transform to apply.
    pub transform: Matrix,
}

impl Default for TileConfig {
    fn default() -> Self {
        Self {
            tile_x: TileMode::Clamp,
            tile_y: TileMode::Clamp,
            source_rect: Rect::new(0.0, 0.0, 1.0, 1.0),
            dest_rect: Rect::EMPTY,
            transform: Matrix::IDENTITY,
        }
    }
}

/// A single tile instance for batched rendering.
#[derive(Debug, Clone, Copy)]
pub struct TileInstance {
    /// Position of the tile.
    pub position: Point,
    /// UV coordinates [u0, v0, u1, v1].
    pub uv: [f32; 4],
    /// Size of the tile.
    pub size: [f32; 2],
    /// Tile index (for debugging).
    pub tile_index: [i32; 2],
}

/// Generate tile instances for a given area.
pub fn generate_tiles(
    image_width: u32,
    image_height: u32,
    config: &TileConfig,
) -> Vec<TileInstance> {
    let mut tiles = Vec::new();

    if config.dest_rect.is_empty() || image_width == 0 || image_height == 0 {
        return tiles;
    }

    let src_width = config.source_rect.width() * image_width as f32;
    let src_height = config.source_rect.height() * image_height as f32;

    if src_width <= 0.0 || src_height <= 0.0 {
        return tiles;
    }

    match (config.tile_x, config.tile_y) {
        (TileMode::Clamp, TileMode::Clamp) | (TileMode::Decal, TileMode::Decal) => {
            // Single tile
            tiles.push(TileInstance {
                position: Point::new(config.dest_rect.left, config.dest_rect.top),
                uv: [
                    config.source_rect.left,
                    config.source_rect.top,
                    config.source_rect.right,
                    config.source_rect.bottom,
                ],
                size: [config.dest_rect.width(), config.dest_rect.height()],
                tile_index: [0, 0],
            });
        }
        _ => {
            // Calculate number of tiles needed
            let dest_width = config.dest_rect.width();
            let dest_height = config.dest_rect.height();

            let tile_count_x = ((dest_width / src_width).ceil() as i32 + 2).max(1);
            let tile_count_y = ((dest_height / src_height).ceil() as i32 + 2).max(1);

            // Generate tiles
            for ty in -1..tile_count_y {
                for tx in -1..tile_count_x {
                    let (uv, flip_x, flip_y) =
                        get_tile_uv(tx, ty, config.tile_x, config.tile_y, &config.source_rect);

                    // Skip decal tiles outside bounds
                    if config.tile_x == TileMode::Decal && (tx < 0 || tx >= 1) {
                        continue;
                    }
                    if config.tile_y == TileMode::Decal && (ty < 0 || ty >= 1) {
                        continue;
                    }

                    let x = config.dest_rect.left + tx as f32 * src_width;
                    let y = config.dest_rect.top + ty as f32 * src_height;

                    // Apply UV flipping for mirror mode
                    let final_uv = if flip_x || flip_y {
                        [
                            if flip_x { uv[2] } else { uv[0] },
                            if flip_y { uv[3] } else { uv[1] },
                            if flip_x { uv[0] } else { uv[2] },
                            if flip_y { uv[1] } else { uv[3] },
                        ]
                    } else {
                        uv
                    };

                    tiles.push(TileInstance {
                        position: Point::new(x, y),
                        uv: final_uv,
                        size: [src_width, src_height],
                        tile_index: [tx, ty],
                    });
                }
            }
        }
    }

    tiles
}

/// Get UV coordinates for a tile at given index.
fn get_tile_uv(
    tx: i32,
    ty: i32,
    tile_x: TileMode,
    tile_y: TileMode,
    source_rect: &Rect,
) -> ([f32; 4], bool, bool) {
    let (u_base, flip_x) = get_tile_coord(tx, tile_x, source_rect.left, source_rect.right);
    let (v_base, flip_y) = get_tile_coord(ty, tile_y, source_rect.top, source_rect.bottom);

    let u_size = source_rect.width();
    let v_size = source_rect.height();

    (
        [u_base, v_base, u_base + u_size, v_base + v_size],
        flip_x,
        flip_y,
    )
}

/// Get coordinate for a single axis tile.
fn get_tile_coord(index: i32, mode: TileMode, min: f32, _max: f32) -> (f32, bool) {
    match mode {
        TileMode::Clamp | TileMode::Decal => (min, false),
        TileMode::Repeat => (min, false),
        TileMode::Mirror => {
            let flip = index.rem_euclid(2) != 0;
            (min, flip)
        }
    }
}

/// Calculate UV transform matrix for tiled rendering.
pub fn calculate_uv_transform(image_width: u32, image_height: u32, config: &TileConfig) -> Matrix {
    let scale_x = config.dest_rect.width() / (config.source_rect.width() * image_width as f32);
    let scale_y = config.dest_rect.height() / (config.source_rect.height() * image_height as f32);

    let offset_x = config.source_rect.left;
    let offset_y = config.source_rect.top;

    Matrix::scale(1.0 / scale_x, 1.0 / scale_y).concat(&Matrix::translate(offset_x, offset_y))
}

/// Nine-patch image configuration.
#[derive(Debug, Clone)]
pub struct NinePatch {
    /// Left inset.
    pub left: f32,
    /// Top inset.
    pub top: f32,
    /// Right inset.
    pub right: f32,
    /// Bottom inset.
    pub bottom: f32,
}

impl NinePatch {
    /// Create a new nine-patch configuration.
    pub fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
        Self {
            left,
            top,
            right,
            bottom,
        }
    }

    /// Create a uniform nine-patch (same inset on all sides).
    pub fn uniform(inset: f32) -> Self {
        Self::new(inset, inset, inset, inset)
    }
}

/// Generate nine-patch tile instances.
pub fn generate_nine_patch(
    image_width: u32,
    image_height: u32,
    patch: &NinePatch,
    dest_rect: &Rect,
) -> Vec<TileInstance> {
    let mut tiles = Vec::with_capacity(9);

    let img_w = image_width as f32;
    let img_h = image_height as f32;

    // Source regions (in pixels)
    let src_left = patch.left;
    let src_top = patch.top;
    let src_right = img_w - patch.right;
    let src_bottom = img_h - patch.bottom;

    // Destination regions
    let dst_left = dest_rect.left;
    let dst_top = dest_rect.top;
    let dst_right = dest_rect.right;
    let dst_bottom = dest_rect.bottom;

    let dst_inner_left = dst_left + patch.left;
    let dst_inner_top = dst_top + patch.top;
    let dst_inner_right = dst_right - patch.right;
    let dst_inner_bottom = dst_bottom - patch.bottom;

    // UV conversion
    let to_uv_x = |x: f32| x / img_w;
    let to_uv_y = |y: f32| y / img_h;

    // Generate 9 patches
    let patches = [
        // Top row
        (
            Rect::new(dst_left, dst_top, dst_inner_left, dst_inner_top),
            [0.0, 0.0, to_uv_x(src_left), to_uv_y(src_top)],
            [-1, -1],
        ),
        (
            Rect::new(dst_inner_left, dst_top, dst_inner_right, dst_inner_top),
            [to_uv_x(src_left), 0.0, to_uv_x(src_right), to_uv_y(src_top)],
            [0, -1],
        ),
        (
            Rect::new(dst_inner_right, dst_top, dst_right, dst_inner_top),
            [to_uv_x(src_right), 0.0, 1.0, to_uv_y(src_top)],
            [1, -1],
        ),
        // Middle row
        (
            Rect::new(dst_left, dst_inner_top, dst_inner_left, dst_inner_bottom),
            [
                0.0,
                to_uv_y(src_top),
                to_uv_x(src_left),
                to_uv_y(src_bottom),
            ],
            [-1, 0],
        ),
        (
            Rect::new(
                dst_inner_left,
                dst_inner_top,
                dst_inner_right,
                dst_inner_bottom,
            ),
            [
                to_uv_x(src_left),
                to_uv_y(src_top),
                to_uv_x(src_right),
                to_uv_y(src_bottom),
            ],
            [0, 0],
        ),
        (
            Rect::new(dst_inner_right, dst_inner_top, dst_right, dst_inner_bottom),
            [
                to_uv_x(src_right),
                to_uv_y(src_top),
                1.0,
                to_uv_y(src_bottom),
            ],
            [1, 0],
        ),
        // Bottom row
        (
            Rect::new(dst_left, dst_inner_bottom, dst_inner_left, dst_bottom),
            [0.0, to_uv_y(src_bottom), to_uv_x(src_left), 1.0],
            [-1, 1],
        ),
        (
            Rect::new(
                dst_inner_left,
                dst_inner_bottom,
                dst_inner_right,
                dst_bottom,
            ),
            [
                to_uv_x(src_left),
                to_uv_y(src_bottom),
                to_uv_x(src_right),
                1.0,
            ],
            [0, 1],
        ),
        (
            Rect::new(dst_inner_right, dst_inner_bottom, dst_right, dst_bottom),
            [to_uv_x(src_right), to_uv_y(src_bottom), 1.0, 1.0],
            [1, 1],
        ),
    ];

    for (rect, uv, idx) in patches {
        if rect.width() > 0.0 && rect.height() > 0.0 {
            tiles.push(TileInstance {
                position: Point::new(rect.left, rect.top),
                uv,
                size: [rect.width(), rect.height()],
                tile_index: idx,
            });
        }
    }

    tiles
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tile_mode() {
        assert_eq!(TileMode::default(), TileMode::Clamp);
    }

    #[test]
    fn test_generate_tiles_clamp() {
        let config = TileConfig {
            tile_x: TileMode::Clamp,
            tile_y: TileMode::Clamp,
            dest_rect: Rect::from_xywh(0.0, 0.0, 100.0, 100.0),
            ..Default::default()
        };

        let tiles = generate_tiles(64, 64, &config);
        assert_eq!(tiles.len(), 1);
    }

    #[test]
    fn test_generate_tiles_repeat() {
        let config = TileConfig {
            tile_x: TileMode::Repeat,
            tile_y: TileMode::Repeat,
            dest_rect: Rect::from_xywh(0.0, 0.0, 200.0, 200.0),
            source_rect: Rect::new(0.0, 0.0, 1.0, 1.0),
            ..Default::default()
        };

        let tiles = generate_tiles(64, 64, &config);
        assert!(tiles.len() > 1);
    }

    #[test]
    fn test_nine_patch() {
        let patch = NinePatch::uniform(10.0);
        let dest = Rect::from_xywh(0.0, 0.0, 100.0, 100.0);

        let tiles = generate_nine_patch(64, 64, &patch, &dest);
        assert_eq!(tiles.len(), 9);
    }

    #[test]
    fn test_tile_instance() {
        let tile = TileInstance {
            position: Point::new(10.0, 20.0),
            uv: [0.0, 0.0, 1.0, 1.0],
            size: [64.0, 64.0],
            tile_index: [0, 0],
        };

        assert_eq!(tile.position.x, 10.0);
        assert_eq!(tile.size[0], 64.0);
    }
}