| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | #![allow (clippy::identity_op)] // We use x + 0 a lot here for symmetry |
| 5 | |
| 6 | //! This is the module for the functions that are drawing the pixels |
| 7 | //! on the line buffer |
| 8 | |
| 9 | use super::{Fixed, PhysicalLength, PhysicalRect}; |
| 10 | use crate::graphics::{Rgb8Pixel, TexturePixelFormat}; |
| 11 | use crate::lengths::{PointLengths, SizeLengths}; |
| 12 | use crate::Color; |
| 13 | use derive_more::{Add, Mul, Sub}; |
| 14 | use integer_sqrt::IntegerSquareRoot; |
| 15 | |
| 16 | /// Draw one line of the texture in the line buffer |
| 17 | /// |
| 18 | pub(super) fn draw_texture_line( |
| 19 | span: &PhysicalRect, |
| 20 | line: PhysicalLength, |
| 21 | texture: &super::SceneTexture, |
| 22 | line_buffer: &mut [impl TargetPixel], |
| 23 | extra_clip_begin: i16, |
| 24 | extra_clip_end: i16, |
| 25 | ) { |
| 26 | let super::SceneTexture { |
| 27 | data, |
| 28 | format, |
| 29 | pixel_stride, |
| 30 | extra: super::SceneTextureExtra { colorize, alpha, rotation, dx, dy, off_x, off_y }, |
| 31 | } = *texture; |
| 32 | |
| 33 | let source_size = texture.source_size().cast::<i32>(); |
| 34 | let len = line_buffer.len(); |
| 35 | let y = line - span.origin.y_length(); |
| 36 | let y = if rotation.mirror_width() { span.size.height - y.get() - 1 } else { y.get() } as i32; |
| 37 | |
| 38 | let off_y = Fixed::<i32, 8>::from_fixed(off_y); |
| 39 | let dx = Fixed::<i32, 8>::from_fixed(dx); |
| 40 | let dy = Fixed::<i32, 8>::from_fixed(dy); |
| 41 | let off_x = Fixed::<i32, 8>::from_fixed(off_x); |
| 42 | |
| 43 | if !rotation.is_transpose() { |
| 44 | let mut delta = dx; |
| 45 | let row = off_y + dy * y; |
| 46 | // The position where to start in the image array for a this row |
| 47 | let mut init = |
| 48 | Fixed::from_integer(row.truncate() % source_size.height) * pixel_stride as i32; |
| 49 | |
| 50 | // the size of the tile in physical pixels in the target |
| 51 | let tile_len = (Fixed::from_integer(source_size.width) / delta) as usize; |
| 52 | // the amount of missing image pixel on one tile |
| 53 | let mut remainder = Fixed::from_integer(source_size.width) % delta; |
| 54 | // The position in image pixel where to get the image |
| 55 | let mut pos; |
| 56 | // the end index in the target buffer |
| 57 | let mut end; |
| 58 | // the accumulated error in image pixels |
| 59 | let mut acc_err; |
| 60 | if rotation.mirror_height() { |
| 61 | let o = (off_x + (delta * (extra_clip_end as i32 + len as i32 - 1))) |
| 62 | % Fixed::from_integer(source_size.width); |
| 63 | pos = init + o; |
| 64 | init += Fixed::from_integer(source_size.width); |
| 65 | end = (o / delta) as usize + 1; |
| 66 | acc_err = -delta + o % delta; |
| 67 | delta = -delta; |
| 68 | remainder = -remainder; |
| 69 | } else { |
| 70 | let o = |
| 71 | (off_x + delta * extra_clip_begin as i32) % Fixed::from_integer(source_size.width); |
| 72 | pos = init + o; |
| 73 | end = ((Fixed::from_integer(source_size.width) - o) / delta) as usize; |
| 74 | acc_err = (Fixed::from_integer(source_size.width) - o) % delta; |
| 75 | if acc_err != Fixed::default() { |
| 76 | acc_err = delta - acc_err; |
| 77 | end += 1; |
| 78 | } |
| 79 | } |
| 80 | end = end.min(len); |
| 81 | let mut begin = 0; |
| 82 | let row_fract = row.fract(); |
| 83 | while begin < len { |
| 84 | fetch_blend_pixel( |
| 85 | &mut line_buffer[begin..end], |
| 86 | format, |
| 87 | data, |
| 88 | alpha, |
| 89 | colorize, |
| 90 | (pixel_stride as usize, dy), |
| 91 | #[inline (always)] |
| 92 | |bpp| { |
| 93 | let p = (pos.truncate() as usize * bpp, pos.fract(), row_fract); |
| 94 | pos += delta; |
| 95 | p |
| 96 | }, |
| 97 | ); |
| 98 | begin = end; |
| 99 | end += tile_len; |
| 100 | pos = init; |
| 101 | pos += acc_err; |
| 102 | if remainder != Fixed::from_integer(0) { |
| 103 | acc_err -= remainder; |
| 104 | let wrap = if rotation.mirror_height() { |
| 105 | acc_err >= Fixed::from_integer(0) |
| 106 | } else { |
| 107 | acc_err < Fixed::from_integer(0) |
| 108 | }; |
| 109 | if wrap { |
| 110 | acc_err += delta; |
| 111 | end += 1; |
| 112 | } |
| 113 | }; |
| 114 | end = end.min(len); |
| 115 | } |
| 116 | } else { |
| 117 | let bpp = format.bpp(); |
| 118 | let col = off_x + dx * y; |
| 119 | let col_fract = col.fract(); |
| 120 | let col = (col.truncate() % source_size.width) as usize * bpp; |
| 121 | let stride = pixel_stride as usize * bpp; |
| 122 | let mut row_delta = dy; |
| 123 | let tile_len = (Fixed::from_integer(source_size.height) / row_delta) as usize; |
| 124 | let mut remainder = Fixed::from_integer(source_size.height) % row_delta; |
| 125 | let mut end; |
| 126 | let mut row_init = Fixed::default(); |
| 127 | let mut row; |
| 128 | let mut acc_err; |
| 129 | if rotation.mirror_height() { |
| 130 | row_init = Fixed::from_integer(source_size.height); |
| 131 | row = (off_y + (row_delta * (extra_clip_end as i32 + len as i32 - 1))) |
| 132 | % Fixed::from_integer(source_size.height); |
| 133 | end = (row / row_delta) as usize + 1; |
| 134 | acc_err = -row_delta + row % row_delta; |
| 135 | row_delta = -row_delta; |
| 136 | remainder = -remainder; |
| 137 | } else { |
| 138 | row = (off_y + row_delta * extra_clip_begin as i32) |
| 139 | % Fixed::from_integer(source_size.height); |
| 140 | end = ((Fixed::from_integer(source_size.height) - row) / row_delta) as usize; |
| 141 | acc_err = (Fixed::from_integer(source_size.height) - row) % row_delta; |
| 142 | if acc_err != Fixed::default() { |
| 143 | acc_err = row_delta - acc_err; |
| 144 | end += 1; |
| 145 | } |
| 146 | }; |
| 147 | end = end.min(len); |
| 148 | let mut begin = 0; |
| 149 | while begin < len { |
| 150 | fetch_blend_pixel( |
| 151 | &mut line_buffer[begin..end], |
| 152 | format, |
| 153 | data, |
| 154 | alpha, |
| 155 | colorize, |
| 156 | (stride, dy), |
| 157 | #[inline (always)] |
| 158 | |_| { |
| 159 | let pos = (row.truncate() as usize * stride + col, col_fract, row.fract()); |
| 160 | row += row_delta; |
| 161 | pos |
| 162 | }, |
| 163 | ); |
| 164 | begin = end; |
| 165 | end += tile_len; |
| 166 | row = row_init; |
| 167 | row += acc_err; |
| 168 | if remainder != Fixed::from_integer(0) { |
| 169 | acc_err -= remainder; |
| 170 | let wrap = if rotation.mirror_height() { |
| 171 | acc_err >= Fixed::from_integer(0) |
| 172 | } else { |
| 173 | acc_err < Fixed::from_integer(0) |
| 174 | }; |
| 175 | if wrap { |
| 176 | acc_err += row_delta; |
| 177 | end += 1; |
| 178 | } |
| 179 | }; |
| 180 | end = end.min(len); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | fn fetch_blend_pixel( |
| 185 | line_buffer: &mut [impl TargetPixel], |
| 186 | format: TexturePixelFormat, |
| 187 | data: &[u8], |
| 188 | alpha: u8, |
| 189 | color: Color, |
| 190 | (stride, delta): (usize, Fixed<i32, 8>), |
| 191 | mut pos: impl FnMut(usize) -> (usize, u8, u8), |
| 192 | ) { |
| 193 | match format { |
| 194 | TexturePixelFormat::Rgb => { |
| 195 | for pix in line_buffer { |
| 196 | let pos = pos(3).0; |
| 197 | let p: &[u8] = &data[pos..pos + 3]; |
| 198 | if alpha == 0xff { |
| 199 | *pix = TargetPixel::from_rgb(p[0], p[1], p[2]); |
| 200 | } else { |
| 201 | pix.blend(PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 202 | alpha, p[0], p[1], p[2], |
| 203 | ))) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | TexturePixelFormat::Rgba => { |
| 208 | if color.alpha() == 0 { |
| 209 | for pix in line_buffer { |
| 210 | let pos = pos(4).0; |
| 211 | let alpha = ((data[pos + 3] as u16 * alpha as u16) / 255) as u8; |
| 212 | let c = PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 213 | alpha, |
| 214 | data[pos + 0], |
| 215 | data[pos + 1], |
| 216 | data[pos + 2], |
| 217 | )); |
| 218 | pix.blend(c); |
| 219 | } |
| 220 | } else { |
| 221 | for pix in line_buffer { |
| 222 | let pos = pos(4).0; |
| 223 | let alpha = ((data[pos + 3] as u16 * alpha as u16) / 255) as u8; |
| 224 | let c = PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 225 | alpha, |
| 226 | color.red(), |
| 227 | color.green(), |
| 228 | color.blue(), |
| 229 | )); |
| 230 | pix.blend(c); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | TexturePixelFormat::RgbaPremultiplied => { |
| 235 | if color.alpha() > 0 { |
| 236 | for pix in line_buffer { |
| 237 | let pos = pos(4).0; |
| 238 | let c = PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 239 | ((data[pos + 3] as u16 * alpha as u16) / 255) as u8, |
| 240 | color.red(), |
| 241 | color.green(), |
| 242 | color.blue(), |
| 243 | )); |
| 244 | pix.blend(c); |
| 245 | } |
| 246 | } else if alpha == 0xff { |
| 247 | for pix in line_buffer { |
| 248 | let pos = pos(4).0; |
| 249 | let c = PremultipliedRgbaColor { |
| 250 | alpha: data[pos + 3], |
| 251 | red: data[pos + 0], |
| 252 | green: data[pos + 1], |
| 253 | blue: data[pos + 2], |
| 254 | }; |
| 255 | pix.blend(c); |
| 256 | } |
| 257 | } else { |
| 258 | for pix in line_buffer { |
| 259 | let pos = pos(4).0; |
| 260 | let c = PremultipliedRgbaColor { |
| 261 | alpha: (data[pos + 3] as u16 * alpha as u16 / 255) as u8, |
| 262 | red: (data[pos + 0] as u16 * alpha as u16 / 255) as u8, |
| 263 | green: (data[pos + 1] as u16 * alpha as u16 / 255) as u8, |
| 264 | blue: (data[pos + 2] as u16 * alpha as u16 / 255) as u8, |
| 265 | }; |
| 266 | pix.blend(c); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | TexturePixelFormat::AlphaMap => { |
| 271 | for pix in line_buffer { |
| 272 | let pos = pos(1).0; |
| 273 | let c = PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 274 | ((data[pos] as u16 * alpha as u16) / 255) as u8, |
| 275 | color.red(), |
| 276 | color.green(), |
| 277 | color.blue(), |
| 278 | )); |
| 279 | pix.blend(c); |
| 280 | } |
| 281 | } |
| 282 | TexturePixelFormat::SignedDistanceField => { |
| 283 | const RANGE: i32 = 6; |
| 284 | let factor = (362 * 256 / delta.0) * RANGE; // 362 ≃ 255 * sqrt(2) |
| 285 | for pix in line_buffer { |
| 286 | let (pos, col_f, row_f) = pos(1); |
| 287 | let (col_f, row_f) = (col_f as i32, row_f as i32); |
| 288 | let mut dist = ((data[pos] as i8 as i32) * (256 - col_f) |
| 289 | + (data[pos + 1] as i8 as i32) * col_f) |
| 290 | * (256 - row_f); |
| 291 | if pos + stride + 1 < data.len() { |
| 292 | dist += ((data[pos + stride] as i8 as i32) * (256 - col_f) |
| 293 | + (data[pos + stride + 1] as i8 as i32) * col_f) |
| 294 | * row_f |
| 295 | } else { |
| 296 | debug_assert_eq!(row_f, 0); |
| 297 | } |
| 298 | let a = ((((dist >> 8) * factor) >> 16) + 128).clamp(0, 255) * alpha as i32; |
| 299 | let c = PremultipliedRgbaColor::premultiply(Color::from_argb_u8( |
| 300 | (a / 255) as u8, |
| 301 | color.red(), |
| 302 | color.green(), |
| 303 | color.blue(), |
| 304 | )); |
| 305 | pix.blend(c); |
| 306 | } |
| 307 | } |
| 308 | }; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /// draw one line of the rounded rectangle in the line buffer |
| 313 | #[allow (clippy::unnecessary_cast)] // Coord |
| 314 | pub(super) fn draw_rounded_rectangle_line( |
| 315 | span: &PhysicalRect, |
| 316 | line: PhysicalLength, |
| 317 | rr: &super::RoundedRectangle, |
| 318 | line_buffer: &mut [impl TargetPixel], |
| 319 | extra_left_clip: i16, |
| 320 | extra_right_clip: i16, |
| 321 | ) { |
| 322 | /// This is an integer shifted by 4 bits. |
| 323 | /// Note: this is not a "fixed point" because multiplication and sqrt operation operate to |
| 324 | /// the shifted integer |
| 325 | #[derive (Clone, Copy, PartialEq, Ord, PartialOrd, Eq, Add, Sub, Mul)] |
| 326 | struct Shifted(u32); |
| 327 | impl Shifted { |
| 328 | const ONE: Self = Shifted(1 << 4); |
| 329 | #[track_caller ] |
| 330 | #[inline ] |
| 331 | pub fn new(value: impl TryInto<u32> + core::fmt::Debug + Copy) -> Self { |
| 332 | Self(value.try_into().unwrap_or_else(|_| panic!("Overflow {value:?}" )) << 4) |
| 333 | } |
| 334 | #[inline (always)] |
| 335 | pub fn floor(self) -> u32 { |
| 336 | self.0 >> 4 |
| 337 | } |
| 338 | #[inline (always)] |
| 339 | pub fn ceil(self) -> u32 { |
| 340 | (self.0 + Self::ONE.0 - 1) >> 4 |
| 341 | } |
| 342 | #[inline (always)] |
| 343 | pub fn saturating_sub(self, other: Self) -> Self { |
| 344 | Self(self.0.saturating_sub(other.0)) |
| 345 | } |
| 346 | #[inline (always)] |
| 347 | pub fn sqrt(self) -> Self { |
| 348 | Self(self.0.integer_sqrt()) |
| 349 | } |
| 350 | } |
| 351 | impl core::ops::Mul for Shifted { |
| 352 | type Output = Shifted; |
| 353 | #[inline (always)] |
| 354 | fn mul(self, rhs: Self) -> Self::Output { |
| 355 | Self(self.0 * rhs.0) |
| 356 | } |
| 357 | } |
| 358 | let width = line_buffer.len(); |
| 359 | let y1 = (line - span.origin.y_length()) + rr.top_clip; |
| 360 | let y2 = (span.origin.y_length() + span.size.height_length() - line) + rr.bottom_clip |
| 361 | - PhysicalLength::new(1); |
| 362 | let y = y1.min(y2); |
| 363 | debug_assert!(y.get() >= 0,); |
| 364 | let border = Shifted::new(rr.width.get()); |
| 365 | const ONE: Shifted = Shifted::ONE; |
| 366 | const ZERO: Shifted = Shifted(0); |
| 367 | let anti_alias = |x1: Shifted, x2: Shifted, process_pixel: &mut dyn FnMut(usize, u32)| { |
| 368 | // x1 and x2 are the coordinate on the top and bottom of the intersection of the pixel |
| 369 | // line and the curve. |
| 370 | // `process_pixel` be called for the coordinate in the array and a coverage between 0..255 |
| 371 | // This algorithm just go linearly which is not perfect, but good enough. |
| 372 | for x in x1.floor()..x2.ceil() { |
| 373 | // the coverage is basically how much of the pixel should be used |
| 374 | let cov = ((ONE + Shifted::new(x) - x1).0 << 8) / (ONE + x2 - x1).0; |
| 375 | process_pixel(x as usize, cov); |
| 376 | } |
| 377 | }; |
| 378 | let rev = |x: Shifted| { |
| 379 | (Shifted::new(width) + Shifted::new(rr.right_clip.get() + extra_right_clip)) |
| 380 | .saturating_sub(x) |
| 381 | }; |
| 382 | let calculate_xxxx = |r: i16, y: i16| { |
| 383 | let r = Shifted::new(r); |
| 384 | // `y` is how far away from the center of the circle the current line is. |
| 385 | let y = r - Shifted::new(y); |
| 386 | // Circle equation: x = √(r² - y²) |
| 387 | // Coordinate from the left edge: x' = r - x |
| 388 | let x2 = r - (r * r).saturating_sub(y * y).sqrt(); |
| 389 | let x1 = r - (r * r).saturating_sub((y - ONE) * (y - ONE)).sqrt(); |
| 390 | let r2 = r.saturating_sub(border); |
| 391 | let x4 = r - (r2 * r2).saturating_sub(y * y).sqrt(); |
| 392 | let x3 = r - (r2 * r2).saturating_sub((y - ONE) * (y - ONE)).sqrt(); |
| 393 | (x1, x2, x3, x4) |
| 394 | }; |
| 395 | |
| 396 | let (x1, x2, x3, x4, x5, x6, x7, x8) = if let Some(r) = rr.radius.as_uniform() { |
| 397 | let (x1, x2, x3, x4) = |
| 398 | if y.get() < r { calculate_xxxx(r, y.get()) } else { (ZERO, ZERO, border, border) }; |
| 399 | (x1, x2, x3, x4, rev(x4), rev(x3), rev(x2), rev(x1)) |
| 400 | } else { |
| 401 | let (x1, x2, x3, x4) = if y1 < PhysicalLength::new(rr.radius.top_left) { |
| 402 | calculate_xxxx(rr.radius.top_left, y.get()) |
| 403 | } else if y2 < PhysicalLength::new(rr.radius.bottom_left) { |
| 404 | calculate_xxxx(rr.radius.bottom_left, y.get()) |
| 405 | } else { |
| 406 | (ZERO, ZERO, border, border) |
| 407 | }; |
| 408 | let (x5, x6, x7, x8) = if y1 < PhysicalLength::new(rr.radius.top_right) { |
| 409 | let x = calculate_xxxx(rr.radius.top_right, y.get()); |
| 410 | (x.3, x.2, x.1, x.0) |
| 411 | } else if y2 < PhysicalLength::new(rr.radius.bottom_right) { |
| 412 | let x = calculate_xxxx(rr.radius.bottom_right, y.get()); |
| 413 | (x.3, x.2, x.1, x.0) |
| 414 | } else { |
| 415 | (border, border, ZERO, ZERO) |
| 416 | }; |
| 417 | (x1, x2, x3, x4, rev(x5), rev(x6), rev(x7), rev(x8)) |
| 418 | }; |
| 419 | anti_alias( |
| 420 | x1.saturating_sub(Shifted::new(rr.left_clip.get() + extra_left_clip)), |
| 421 | x2.saturating_sub(Shifted::new(rr.left_clip.get() + extra_left_clip)), |
| 422 | &mut |x, cov| { |
| 423 | if x >= width { |
| 424 | return; |
| 425 | } |
| 426 | let c = if border == ZERO { rr.inner_color } else { rr.border_color }; |
| 427 | let col = PremultipliedRgbaColor { |
| 428 | alpha: (((c.alpha as u32) * cov as u32) / 255) as u8, |
| 429 | red: (((c.red as u32) * cov as u32) / 255) as u8, |
| 430 | green: (((c.green as u32) * cov as u32) / 255) as u8, |
| 431 | blue: (((c.blue as u32) * cov as u32) / 255) as u8, |
| 432 | }; |
| 433 | line_buffer[x].blend(col); |
| 434 | }, |
| 435 | ); |
| 436 | if y < rr.width { |
| 437 | // up or down border (x2 .. x7) |
| 438 | let l = x2 |
| 439 | .ceil() |
| 440 | .saturating_sub((rr.left_clip.get() + extra_left_clip) as u32) |
| 441 | .min(width as u32) as usize; |
| 442 | let r = x7.floor().min(width as u32) as usize; |
| 443 | if l < r { |
| 444 | TargetPixel::blend_slice(&mut line_buffer[l..r], rr.border_color) |
| 445 | } |
| 446 | } else { |
| 447 | if border > ZERO { |
| 448 | // 3. draw the border (between x2 and x3) |
| 449 | if ONE + x2 <= x3 { |
| 450 | TargetPixel::blend_slice( |
| 451 | &mut line_buffer[x2 |
| 452 | .ceil() |
| 453 | .saturating_sub((rr.left_clip.get() + extra_left_clip) as u32) |
| 454 | .min(width as u32) as usize |
| 455 | ..x3.floor() |
| 456 | .saturating_sub((rr.left_clip.get() + extra_left_clip) as u32) |
| 457 | .min(width as u32) as usize], |
| 458 | rr.border_color, |
| 459 | ) |
| 460 | } |
| 461 | // 4. anti-aliasing for the contents (x3 .. x4) |
| 462 | anti_alias( |
| 463 | x3.saturating_sub(Shifted::new(rr.left_clip.get() + extra_left_clip)), |
| 464 | x4.saturating_sub(Shifted::new(rr.left_clip.get() + extra_left_clip)), |
| 465 | &mut |x, cov| { |
| 466 | if x >= width { |
| 467 | return; |
| 468 | } |
| 469 | let col = interpolate_color(cov, rr.border_color, rr.inner_color); |
| 470 | line_buffer[x].blend(col); |
| 471 | }, |
| 472 | ); |
| 473 | } |
| 474 | if rr.inner_color.alpha > 0 { |
| 475 | // 5. inside (x4 .. x5) |
| 476 | let begin = x4 |
| 477 | .ceil() |
| 478 | .saturating_sub((rr.left_clip.get() + extra_left_clip) as u32) |
| 479 | .min(width as u32); |
| 480 | let end = x5.floor().min(width as u32); |
| 481 | if begin < end { |
| 482 | TargetPixel::blend_slice( |
| 483 | &mut line_buffer[begin as usize..end as usize], |
| 484 | rr.inner_color, |
| 485 | ) |
| 486 | } |
| 487 | } |
| 488 | if border > ZERO { |
| 489 | // 6. border anti-aliasing: x5..x6 |
| 490 | anti_alias(x5, x6, &mut |x, cov| { |
| 491 | if x >= width { |
| 492 | return; |
| 493 | } |
| 494 | let col = interpolate_color(cov, rr.inner_color, rr.border_color); |
| 495 | line_buffer[x].blend(col) |
| 496 | }); |
| 497 | // 7. border x6 .. x7 |
| 498 | if ONE + x6 <= x7 { |
| 499 | TargetPixel::blend_slice( |
| 500 | &mut line_buffer[x6.ceil().min(width as u32) as usize |
| 501 | ..x7.floor().min(width as u32) as usize], |
| 502 | rr.border_color, |
| 503 | ) |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | anti_alias(x7, x8, &mut |x, cov| { |
| 508 | if x >= width { |
| 509 | return; |
| 510 | } |
| 511 | let c = if border == ZERO { rr.inner_color } else { rr.border_color }; |
| 512 | let col = PremultipliedRgbaColor { |
| 513 | alpha: (((c.alpha as u32) * (255 - cov) as u32) / 255) as u8, |
| 514 | red: (((c.red as u32) * (255 - cov) as u32) / 255) as u8, |
| 515 | green: (((c.green as u32) * (255 - cov) as u32) / 255) as u8, |
| 516 | blue: (((c.blue as u32) * (255 - cov) as u32) / 255) as u8, |
| 517 | }; |
| 518 | line_buffer[x].blend(col); |
| 519 | }); |
| 520 | } |
| 521 | |
| 522 | // a is between 0 and 255. When 0, we get color1, when 255 we get color2 |
| 523 | fn interpolate_color( |
| 524 | a: u32, |
| 525 | color1: PremultipliedRgbaColor, |
| 526 | color2: PremultipliedRgbaColor, |
| 527 | ) -> PremultipliedRgbaColor { |
| 528 | let b: u32 = 255 - a; |
| 529 | |
| 530 | let al1: u32 = color1.alpha as u32; |
| 531 | let al2: u32 = color2.alpha as u32; |
| 532 | |
| 533 | let a_: u32 = a * al2; |
| 534 | let b_: u32 = b * al1; |
| 535 | let m: u32 = a_ + b_; |
| 536 | |
| 537 | if m == 0 { |
| 538 | return PremultipliedRgbaColor::default(); |
| 539 | } |
| 540 | |
| 541 | PremultipliedRgbaColor { |
| 542 | alpha: (m / 255) as u8, |
| 543 | red: ((b * color1.red as u32 + a * color2.red as u32) / 255) as u8, |
| 544 | green: ((b * color1.green as u32 + a * color2.green as u32) / 255) as u8, |
| 545 | blue: ((b * color1.blue as u32 + a * color2.blue as u32) / 255) as u8, |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | pub(super) fn draw_gradient_line( |
| 550 | rect: &PhysicalRect, |
| 551 | line: PhysicalLength, |
| 552 | g: &super::GradientCommand, |
| 553 | mut buffer: &mut [impl TargetPixel], |
| 554 | extra_left_clip: i16, |
| 555 | ) { |
| 556 | let fill_col1 = g.flags & 0b010 != 0; |
| 557 | let fill_col2 = g.flags & 0b100 != 0; |
| 558 | let invert_slope = g.flags & 0b1 != 0; |
| 559 | |
| 560 | let y = (line.get() - rect.min_y() + g.top_clip.get()) as i32; |
| 561 | let size_y = (rect.height() + g.top_clip.get() + g.bottom_clip.get()) as i32; |
| 562 | let start = g.start as i32; |
| 563 | |
| 564 | let (mut color1, mut color2) = (g.color1, g.color2); |
| 565 | |
| 566 | if g.start == 0 { |
| 567 | let p = if invert_slope { |
| 568 | (255 - start) * y / size_y |
| 569 | } else { |
| 570 | start + (255 - start) * y / size_y |
| 571 | }; |
| 572 | if (fill_col1 || p >= 0) && (fill_col2 || p < 255) { |
| 573 | let col = interpolate_color(p.clamp(0, 255) as u32, color1, color2); |
| 574 | TargetPixel::blend_slice(buffer, col); |
| 575 | } |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | let size_x = (rect.width() + g.left_clip.get() + g.right_clip.get()) as i32; |
| 580 | |
| 581 | let mut x = if invert_slope { |
| 582 | (y * size_x * (255 - start)) / (size_y * start) |
| 583 | } else { |
| 584 | (size_y - y) * size_x * (255 - start) / (size_y * start) |
| 585 | } + g.left_clip.get() as i32 |
| 586 | + extra_left_clip as i32; |
| 587 | |
| 588 | let len = ((255 * size_x) / start) as usize; |
| 589 | |
| 590 | if x < 0 { |
| 591 | let l = (-x as usize).min(buffer.len()); |
| 592 | if invert_slope { |
| 593 | if fill_col1 { |
| 594 | TargetPixel::blend_slice(&mut buffer[..l], g.color1); |
| 595 | } |
| 596 | } else if fill_col2 { |
| 597 | TargetPixel::blend_slice(&mut buffer[..l], g.color2); |
| 598 | } |
| 599 | buffer = &mut buffer[l..]; |
| 600 | x = 0; |
| 601 | } |
| 602 | |
| 603 | if buffer.len() + x as usize > len { |
| 604 | let l = len.saturating_sub(x as usize); |
| 605 | if invert_slope { |
| 606 | if fill_col2 { |
| 607 | TargetPixel::blend_slice(&mut buffer[l..], g.color2); |
| 608 | } |
| 609 | } else if fill_col1 { |
| 610 | TargetPixel::blend_slice(&mut buffer[l..], g.color1); |
| 611 | } |
| 612 | buffer = &mut buffer[..l]; |
| 613 | } |
| 614 | |
| 615 | if buffer.is_empty() { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | if !invert_slope { |
| 620 | core::mem::swap(&mut color1, &mut color2); |
| 621 | } |
| 622 | |
| 623 | let dr = (((color2.red as i32 - color1.red as i32) * start) << 15) / (255 * size_x); |
| 624 | let dg = (((color2.green as i32 - color1.green as i32) * start) << 15) / (255 * size_x); |
| 625 | let db = (((color2.blue as i32 - color1.blue as i32) * start) << 15) / (255 * size_x); |
| 626 | let da = (((color2.alpha as i32 - color1.alpha as i32) * start) << 15) / (255 * size_x); |
| 627 | |
| 628 | let mut r = ((color1.red as u32) << 15).wrapping_add((x * dr) as _); |
| 629 | let mut g = ((color1.green as u32) << 15).wrapping_add((x * dg) as _); |
| 630 | let mut b = ((color1.blue as u32) << 15).wrapping_add((x * db) as _); |
| 631 | let mut a = ((color1.alpha as u32) << 15).wrapping_add((x * da) as _); |
| 632 | |
| 633 | if color1.alpha == 255 && color2.alpha == 255 { |
| 634 | buffer.fill_with(|| { |
| 635 | let pix = TargetPixel::from_rgb((r >> 15) as u8, (g >> 15) as u8, (b >> 15) as u8); |
| 636 | r = r.wrapping_add(dr as _); |
| 637 | g = g.wrapping_add(dg as _); |
| 638 | b = b.wrapping_add(db as _); |
| 639 | pix |
| 640 | }) |
| 641 | } else { |
| 642 | for pix in buffer { |
| 643 | pix.blend(PremultipliedRgbaColor { |
| 644 | red: (r >> 15) as u8, |
| 645 | green: (g >> 15) as u8, |
| 646 | blue: (b >> 15) as u8, |
| 647 | alpha: (a >> 15) as u8, |
| 648 | }); |
| 649 | r = r.wrapping_add(dr as _); |
| 650 | g = g.wrapping_add(dg as _); |
| 651 | b = b.wrapping_add(db as _); |
| 652 | a = a.wrapping_add(da as _); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /// A color whose component have been pre-multiplied by alpha |
| 658 | /// |
| 659 | /// The renderer operates faster on pre-multiplied color since it |
| 660 | /// caches the multiplication of its component |
| 661 | /// |
| 662 | /// PremultipliedRgbaColor can be constructed from a [`Color`] with |
| 663 | /// the [`From`] trait. This conversion will pre-multiply the color |
| 664 | /// components |
| 665 | #[allow (missing_docs)] |
| 666 | #[derive (Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)] |
| 667 | #[repr (C)] |
| 668 | pub struct PremultipliedRgbaColor { |
| 669 | pub red: u8, |
| 670 | pub green: u8, |
| 671 | pub blue: u8, |
| 672 | pub alpha: u8, |
| 673 | } |
| 674 | |
| 675 | /// Convert a non-premultiplied color to a premultiplied one |
| 676 | impl From<Color> for PremultipliedRgbaColor { |
| 677 | fn from(col: Color) -> Self { |
| 678 | Self::premultiply(col) |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | impl PremultipliedRgbaColor { |
| 683 | /// Convert a non premultiplied color to a premultiplied one |
| 684 | fn premultiply(col: Color) -> Self { |
| 685 | let a: u16 = col.alpha() as u16; |
| 686 | Self { |
| 687 | alpha: col.alpha(), |
| 688 | red: (col.red() as u16 * a / 255) as u8, |
| 689 | green: (col.green() as u16 * a / 255) as u8, |
| 690 | blue: (col.blue() as u16 * a / 255) as u8, |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /// Trait for the pixels in the buffer |
| 696 | pub trait TargetPixel: Sized + Copy { |
| 697 | /// Blend a single pixel with a color |
| 698 | fn blend(&mut self, color: PremultipliedRgbaColor); |
| 699 | /// Blend a color to all the pixel in the slice. |
| 700 | fn blend_slice(slice: &mut [Self], color: PremultipliedRgbaColor) { |
| 701 | if color.alpha == u8::MAX { |
| 702 | slice.fill(Self::from_rgb(color.red, color.green, color.blue)) |
| 703 | } else { |
| 704 | for x: &mut Self in slice { |
| 705 | Self::blend(self:x, color); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | /// Create a pixel from the red, gree, blue component in the range 0..=255 |
| 710 | fn from_rgb(red: u8, green: u8, blue: u8) -> Self; |
| 711 | |
| 712 | /// Pixel which will be filled as the background in case the slint view has transparency |
| 713 | fn background() -> Self { |
| 714 | Self::from_rgb(red:0, green:0, blue:0) |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | impl TargetPixel for crate::graphics::image::Rgb8Pixel { |
| 719 | fn blend(&mut self, color: PremultipliedRgbaColor) { |
| 720 | let a: u16 = (u8::MAX - color.alpha) as u16; |
| 721 | self.r = (self.r as u16 * a / 255) as u8 + color.red; |
| 722 | self.g = (self.g as u16 * a / 255) as u8 + color.green; |
| 723 | self.b = (self.b as u16 * a / 255) as u8 + color.blue; |
| 724 | } |
| 725 | |
| 726 | fn from_rgb(r: u8, g: u8, b: u8) -> Self { |
| 727 | Self::new(red:r, green:g, blue:b) |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | impl TargetPixel for PremultipliedRgbaColor { |
| 732 | fn blend(&mut self, color: PremultipliedRgbaColor) { |
| 733 | let a: u16 = (u8::MAX - color.alpha) as u16; |
| 734 | self.red = (self.red as u16 * a / 255) as u8 + color.red; |
| 735 | self.green = (self.green as u16 * a / 255) as u8 + color.green; |
| 736 | self.blue = (self.blue as u16 * a / 255) as u8 + color.blue; |
| 737 | self.alpha = (self.alpha as u16 + color.alpha as u16 |
| 738 | - (self.alpha as u16 * color.alpha as u16) / 255) as u8; |
| 739 | } |
| 740 | |
| 741 | fn from_rgb(r: u8, g: u8, b: u8) -> Self { |
| 742 | Self { red: r, green: g, blue: b, alpha: 255 } |
| 743 | } |
| 744 | |
| 745 | fn background() -> Self { |
| 746 | Self { red: 0, green: 0, blue: 0, alpha: 0 } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /// A 16bit pixel that has 5 red bits, 6 green bits and 5 blue bits |
| 751 | #[repr (transparent)] |
| 752 | #[derive (Copy, Clone, Debug, PartialEq, Eq, Default, bytemuck::Pod, bytemuck::Zeroable)] |
| 753 | pub struct Rgb565Pixel(pub u16); |
| 754 | |
| 755 | impl Rgb565Pixel { |
| 756 | const R_MASK: u16 = 0b1111_1000_0000_0000; |
| 757 | const G_MASK: u16 = 0b0000_0111_1110_0000; |
| 758 | const B_MASK: u16 = 0b0000_0000_0001_1111; |
| 759 | |
| 760 | /// Return the red component as a u8. |
| 761 | /// |
| 762 | /// The bits are shifted so that the result is between 0 and 255 |
| 763 | fn red(self) -> u8 { |
| 764 | ((self.0 & Self::R_MASK) >> 8) as u8 |
| 765 | } |
| 766 | /// Return the green component as a u8. |
| 767 | /// |
| 768 | /// The bits are shifted so that the result is between 0 and 255 |
| 769 | fn green(self) -> u8 { |
| 770 | ((self.0 & Self::G_MASK) >> 3) as u8 |
| 771 | } |
| 772 | /// Return the blue component as a u8. |
| 773 | /// |
| 774 | /// The bits are shifted so that the result is between 0 and 255 |
| 775 | fn blue(self) -> u8 { |
| 776 | ((self.0 & Self::B_MASK) << 3) as u8 |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | impl TargetPixel for Rgb565Pixel { |
| 781 | fn blend(&mut self, color: PremultipliedRgbaColor) { |
| 782 | let a = (u8::MAX - color.alpha) as u32; |
| 783 | // convert to 5 bits |
| 784 | let a = (a + 4) >> 3; |
| 785 | |
| 786 | // 00000ggg_ggg00000_rrrrr000_000bbbbb |
| 787 | let expanded = (self.0 & (Self::R_MASK | Self::B_MASK)) as u32 |
| 788 | | (((self.0 & Self::G_MASK) as u32) << 16); |
| 789 | |
| 790 | // gggggggg_000rrrrr_rrr000bb_bbbbbb00 |
| 791 | let c = |
| 792 | ((color.red as u32) << 13) | ((color.green as u32) << 24) | ((color.blue as u32) << 2); |
| 793 | // gggggg00_000rrrrr_000000bb_bbb00000 |
| 794 | let c = c & 0b11111100_00011111_00000011_11100000; |
| 795 | |
| 796 | let res = expanded * a + c; |
| 797 | |
| 798 | self.0 = ((res >> 21) as u16 & Self::G_MASK) |
| 799 | | ((res >> 5) as u16 & (Self::R_MASK | Self::B_MASK)); |
| 800 | } |
| 801 | |
| 802 | fn from_rgb(r: u8, g: u8, b: u8) -> Self { |
| 803 | Self(((r as u16 & 0b11111000) << 8) | ((g as u16 & 0b11111100) << 3) | (b as u16 >> 3)) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | impl From<Rgb8Pixel> for Rgb565Pixel { |
| 808 | fn from(p: Rgb8Pixel) -> Self { |
| 809 | Self::from_rgb(red:p.r, green:p.g, blue:p.b) |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | impl From<Rgb565Pixel> for Rgb8Pixel { |
| 814 | fn from(p: Rgb565Pixel) -> Self { |
| 815 | Rgb8Pixel { r: p.red(), g: p.green(), b: p.blue() } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | #[test ] |
| 820 | fn rgb565() { |
| 821 | let pix565 = Rgb565Pixel::from_rgb(0xff, 0x25, 0); |
| 822 | let pix888: Rgb8Pixel = pix565.into(); |
| 823 | assert_eq!(pix565, pix888.into()); |
| 824 | |
| 825 | let pix565 = Rgb565Pixel::from_rgb(0x56, 0x42, 0xe3); |
| 826 | let pix888: Rgb8Pixel = pix565.into(); |
| 827 | assert_eq!(pix565, pix888.into()); |
| 828 | } |
| 829 | |