use crate::display::{PixelFormat, SpriteFormat};
use crate::element::SpriteSource;
use crate::motion::{DrawOp, DrawPayload};
use crate::sdf::isqrt_i32;
use crate::solver::Region;
use crate::style::Color;
#[allow(dead_code)]
#[inline(always)]
pub(super) fn compute_sdf_dist_16_r16(px: i16, py: i16, box_reg: Region, r_16: i32) -> i32 {
let max_r_16 = (box_reg.w.min(box_reg.h).max(0) as i32) * 8;
let r_16 = r_16.clamp(0, max_r_16);
let px_16 = (px as i32) * 16 + 8;
let py_16 = (py as i32) * 16 + 8;
let half_w_16 = (box_reg.w as i32) * 8;
let half_h_16 = (box_reg.h as i32) * 8;
let cx_16 = (box_reg.x as i32) * 16 + half_w_16;
let cy_16 = (box_reg.y as i32) * 16 + half_h_16;
let qx = (px_16 - cx_16).abs() - (half_w_16 - r_16);
let qy = (py_16 - cy_16).abs() - (half_h_16 - r_16);
let out_x = qx.max(0);
let out_y = qy.max(0);
let outside_dist = if out_x == 0 {
out_y
} else if out_y == 0 {
out_x
} else {
isqrt_i32(out_x * out_x + out_y * out_y)
};
let inside_dist = qx.max(qy).min(0);
outside_dist + inside_dist - r_16
}
#[inline(always)]
pub(super) fn compute_sdf_coverage_r16(px: i16, py: i16, box_reg: Region, r_16: i32) -> u8 {
if box_reg.w <= 0 || box_reg.h <= 0 {
return 0;
}
if px < box_reg.x || px >= box_reg.right() || py < box_reg.y || py >= box_reg.bottom() {
return 0;
}
if r_16 <= 0 {
return 255;
}
{
let half_w = (box_reg.w as f32) * 0.5;
let half_h = (box_reg.h as f32) * 0.5;
let cx = (box_reg.x as f32) + half_w;
let cy = (box_reg.y as f32) + half_h;
let r = ((r_16 as f32) * (1.0 / 16.0)).clamp(0.0, half_w.min(half_h));
let qx = ((px as f32 + 0.5) - cx).abs() - (half_w - r);
let qy = ((py as f32 + 0.5) - cy).abs() - (half_h - r);
let ox = qx.max(0.0);
let oy = qy.max(0.0);
let sd = (ox * ox + oy * oy).sqrt() + qx.max(qy).min(0.0) - r;
return ((0.5 - sd).clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
}
}
#[inline(always)]
pub fn compute_sdf_coverage(px: i16, py: i16, box_reg: Region, r: i16) -> u8 {
compute_sdf_coverage_r16(px, py, box_reg, (r.max(0) as i32) * 16)
}
#[inline]
pub fn opaque_core_rect(op: &DrawOp<'_>) -> Region {
let core = match op.payload {
DrawPayload::Quad {
bg,
bg_grad,
border_color,
rounded,
border_width,
} if bg.a() == 255
&& bg_grad.a() == 255
&& !(border_color.a() == 0 && (border_width & 0x7F) > 0) =>
{
let bw = (border_width & 0x7F) as i16;
let inset = if bw > 0 && border_color.a() < 255 {
(rounded as i16).max(bw)
} else {
rounded as i16
};
let inner_w = op.bounds.w.saturating_sub(inset.saturating_mul(2));
let inner_h = op.bounds.h.saturating_sub(inset.saturating_mul(2));
if inner_w <= 0 || inner_h <= 0 {
Region::EMPTY
} else {
Region::new(
op.bounds.x.saturating_add(inset),
op.bounds.y.saturating_add(inset),
inner_w,
inner_h,
)
.intersect(op.clip)
}
}
DrawPayload::Sprite(SpriteSource::Bitmap {
format: SpriteFormat::Rgb565,
bytes,
tint,
}) if tint.a() == 255
&& bytes.len() >= 2
&& {
let px_n = bytes.len() / 2;
let area = (op.bounds.w.max(0) as usize).saturating_mul(op.bounds.h.max(0) as usize);
area > 0 && (px_n >= area || area % px_n == 0)
} =>
{
op.bounds.intersect(op.clip)
}
DrawPayload::StyledQuad(sq)
if sq.blend_mode.is_normal()
&& sq.transform.is_none()
&& sq.opacity == 255
&& sq.paint.is_fully_opaque() =>
{
let max_r = sq.radii.max_radius() as i16;
let bw = sq.border_width as i16;
let inset = if bw > 0 && sq.border_color.a() < 255 {
max_r.max(bw)
} else {
max_r
};
let inner_w = op.bounds.w.saturating_sub(inset.saturating_mul(2));
let inner_h = op.bounds.h.saturating_sub(inset.saturating_mul(2));
if inner_w <= 0 || inner_h <= 0 {
Region::EMPTY
} else {
Region::new(
op.bounds.x.saturating_add(inset),
op.bounds.y.saturating_add(inset),
inner_w,
inner_h,
)
.intersect(op.clip)
}
}
_ => Region::EMPTY,
};
if core.is_empty() || op.clip_radius == 0 {
return core;
}
let inset = (op.clip_radius as i16).min(op.clip.w / 2).min(op.clip.h / 2);
core.intersect(Region::new(
op.clip.x.saturating_add(inset),
op.clip.y.saturating_add(inset),
op.clip.w - 2 * inset,
op.clip.h - 2 * inset,
))
}
#[inline(always)]
fn apply_single_occluder(mut clip: Region, occ: Region) -> Region {
let spans_x = occ.x <= clip.x && occ.right() >= clip.right();
let spans_y = occ.y <= clip.y && occ.bottom() >= clip.bottom();
if spans_x && spans_y {
return Region::EMPTY;
}
if spans_x {
if occ.y <= clip.y && occ.bottom() > clip.y {
let new_y = occ.bottom();
clip.h = clip.bottom().saturating_sub(new_y);
clip.y = new_y;
} else if occ.bottom() >= clip.bottom() && occ.y < clip.bottom() {
clip.h = occ.y.saturating_sub(clip.y);
}
}
if spans_y {
if occ.x <= clip.x && occ.right() > clip.x {
let new_x = occ.right();
clip.w = clip.right().saturating_sub(new_x);
clip.x = new_x;
} else if occ.right() >= clip.right() && occ.x < clip.right() {
clip.w = occ.x.saturating_sub(clip.x);
}
}
clip
}
mod spatial_occlusion {
use core::cell::RefCell;
use std::vec::Vec;
use super::{apply_single_occluder, clip_against_opaque_rects, opaque_core_rect};
use crate::motion::DrawOp;
use crate::solver::Region;
const SMALL_OCCLUDER_THRESHOLD: usize = 16;
const LARGE_OCCLUDER_MAX_CELLS: usize = 16;
const MAX_GRID_CELLS: usize = 16_384;
pub(crate) struct SpatialOcclusionGrid {
occluders: Vec<(usize, Region)>,
large_occluders: Vec<u32>,
bucket_counts: Vec<u32>,
bucket_offsets: Vec<u32>,
bucket_cursors: Vec<u32>,
bucket_entries: Vec<u32>,
origin_x: i32,
origin_y: i32,
cols: usize,
rows: usize,
cell_shift: u32,
}
impl SpatialOcclusionGrid {
fn new() -> Self {
Self {
occluders: Vec::with_capacity(256),
large_occluders: Vec::with_capacity(16),
bucket_counts: Vec::new(),
bucket_offsets: Vec::new(),
bucket_cursors: Vec::new(),
bucket_entries: Vec::new(),
origin_x: 0,
origin_y: 0,
cols: 0,
rows: 0,
cell_shift: 6,
}
}
fn build(&mut self, ops: &[DrawOp<'_>]) {
self.occluders.clear();
self.large_occluders.clear();
let mut min_x = i32::MAX;
let mut min_y = i32::MAX;
let mut max_x = i32::MIN;
let mut max_y = i32::MIN;
for (idx, op) in ops.iter().enumerate() {
let occ = opaque_core_rect(op);
if !occ.is_empty() {
let x0 = occ.x as i32;
let y0 = occ.y as i32;
let x1 = occ.right() as i32;
let y1 = occ.bottom() as i32;
min_x = min_x.min(x0);
min_y = min_y.min(y0);
max_x = max_x.max(x1);
max_y = max_y.max(y1);
self.occluders.push((idx, occ));
}
}
if self.occluders.len() <= SMALL_OCCLUDER_THRESHOLD {
self.cols = 0;
self.rows = 0;
return;
}
let mut shift: u32 = 6;
let span_x = (max_x - min_x).max(1) as usize;
let span_y = (max_y - min_y).max(1) as usize;
let (cols, rows) = loop {
let step = 1usize << shift;
let c = span_x.div_ceil(step).max(1);
let r = span_y.div_ceil(step).max(1);
if c.saturating_mul(r) <= MAX_GRID_CELLS || shift >= 15 {
break (c, r);
}
shift += 1;
};
self.origin_x = min_x;
self.origin_y = min_y;
self.cols = cols;
self.rows = rows;
self.cell_shift = shift;
let total_cells = cols * rows;
self.bucket_counts.clear();
self.bucket_counts.resize(total_cells, 0);
let mut total_entries = 0usize;
for (occ_id, &(_, occ)) in self.occluders.iter().enumerate() {
let (cx0, cy0, cx1, cy1) = self.rect_cell_range(occ);
let cell_count = (cx1 - cx0 + 1) * (cy1 - cy0 + 1);
if cell_count > LARGE_OCCLUDER_MAX_CELLS {
self.large_occluders.push(occ_id as u32);
} else {
for cy in cy0..=cy1 {
let row_base = cy * cols;
for cx in cx0..=cx1 {
self.bucket_counts[row_base + cx] += 1;
}
}
total_entries += cell_count;
}
}
self.bucket_offsets.clear();
self.bucket_offsets.reserve(total_cells + 1);
let mut acc = 0u32;
for &cnt in &self.bucket_counts {
self.bucket_offsets.push(acc);
acc += cnt;
}
self.bucket_offsets.push(acc);
self.bucket_cursors.clear();
self.bucket_cursors
.extend_from_slice(&self.bucket_offsets[..total_cells]);
self.bucket_entries.clear();
self.bucket_entries.resize(total_entries, 0);
for (occ_id, &(_, occ)) in self.occluders.iter().enumerate() {
let (cx0, cy0, cx1, cy1) = self.rect_cell_range(occ);
let cell_count = (cx1 - cx0 + 1) * (cy1 - cy0 + 1);
if cell_count <= LARGE_OCCLUDER_MAX_CELLS {
for cy in cy0..=cy1 {
let row_base = cy * cols;
for cx in cx0..=cx1 {
let b = row_base + cx;
let pos = self.bucket_cursors[b] as usize;
self.bucket_entries[pos] = occ_id as u32;
self.bucket_cursors[b] = (pos + 1) as u32;
}
}
}
}
}
#[inline(always)]
fn rect_cell_range(&self, r: Region) -> (usize, usize, usize, usize) {
let max_cx = self.cols - 1;
let max_cy = self.rows - 1;
let cx0 = (((r.x as i32 - self.origin_x).max(0) as usize) >> self.cell_shift).min(max_cx);
let cy0 = (((r.y as i32 - self.origin_y).max(0) as usize) >> self.cell_shift).min(max_cy);
let rx1 = (r.right() as i32 - 1 - self.origin_x).max(0) as usize;
let ry1 = (r.bottom() as i32 - 1 - self.origin_y).max(0) as usize;
let cx1 = (rx1 >> self.cell_shift).min(max_cx);
let cy1 = (ry1 >> self.cell_shift).min(max_cy);
(cx0, cy0, cx1.max(cx0), cy1.max(cy0))
}
#[inline(always)]
fn point_bucket(&self, px: i32, py: i32) -> Option<usize> {
let dx = px - self.origin_x;
let dy = py - self.origin_y;
if dx < 0 || dy < 0 {
return None;
}
let cx = (dx as usize) >> self.cell_shift;
let cy = (dy as usize) >> self.cell_shift;
if cx >= self.cols || cy >= self.rows {
return None;
}
Some(cy * self.cols + cx)
}
#[inline(always)]
fn bucket_slice(&self, b: usize) -> &[u32] {
let s = self.bucket_offsets[b] as usize;
let e = self.bucket_offsets[b + 1] as usize;
&self.bucket_entries[s..e]
}
#[inline(always)]
fn scan_sorted_candidates(
&self,
list: &[u32],
clip: Region,
min_idx: usize,
best_op_idx: &mut usize,
best_clip: &mut Region,
) {
if list.is_empty() {
return;
}
let last_op_idx = self.occluders[*list.last().unwrap() as usize].0;
if last_op_idx <= min_idx {
return;
}
let start = if list.len() <= 8 {
0
} else {
list.partition_point(|&id| self.occluders[id as usize].0 <= min_idx)
};
for &id in &list[start..] {
let (op_idx, occ) = self.occluders[id as usize];
if op_idx <= min_idx {
continue;
}
if op_idx >= *best_op_idx {
break;
}
let next = apply_single_occluder(clip, occ);
if next != clip {
*best_op_idx = op_idx;
*best_clip = next;
break;
}
}
}
#[inline]
pub(crate) fn clip_region(&self, mut clip: Region, mut min_idx: usize) -> Region {
if self.occluders.is_empty() || clip.is_empty() {
return clip;
}
if self.cols == 0 {
return clip_against_opaque_rects(clip, &self.occluders, min_idx);
}
loop {
if clip.is_empty() {
return Region::EMPTY;
}
let tl_b = self.point_bucket(clip.x as i32, clip.y as i32);
let br_b = self.point_bucket(clip.right() as i32 - 1, clip.bottom() as i32 - 1);
let mut best_op_idx = usize::MAX;
let mut best_clip = clip;
if let Some(b0) = tl_b {
self.scan_sorted_candidates(
self.bucket_slice(b0),
clip,
min_idx,
&mut best_op_idx,
&mut best_clip,
);
}
if let Some(b1) = br_b {
if Some(b1) != tl_b {
self.scan_sorted_candidates(
self.bucket_slice(b1),
clip,
min_idx,
&mut best_op_idx,
&mut best_clip,
);
}
}
if !self.large_occluders.is_empty() {
self.scan_sorted_candidates(
&self.large_occluders,
clip,
min_idx,
&mut best_op_idx,
&mut best_clip,
);
}
if best_op_idx == usize::MAX {
return clip;
}
clip = best_clip;
min_idx = best_op_idx;
}
}
}
std::thread_local! {
static TL_OCCLUSION_GRID: RefCell<SpatialOcclusionGrid> =
RefCell::new(SpatialOcclusionGrid::new());
}
#[cfg(any(feature = "gpu", not(target_arch = "wasm32")))]
pub(crate) fn with_spatial_occlusion<R>(
ops: &[DrawOp<'_>],
f: impl FnOnce(Option<&SpatialOcclusionGrid>) -> R,
) -> R {
if ops.len() <= SMALL_OCCLUDER_THRESHOLD {
return f(None);
}
let mut f = Some(f);
let result = TL_OCCLUSION_GRID.try_with(|cell| {
let Ok(mut grid) = cell.try_borrow_mut() else {
return None;
};
grid.build(ops);
f.take().map(|f| f(Some(&grid)))
});
match result {
Ok(Some(result)) => result,
_ => f.expect("occlusion callback has not run")(None),
}
}
pub(super) fn precompute_spatial(ops: &mut [DrawOp<'_>]) {
if ops.len() <= SMALL_OCCLUDER_THRESHOLD {
super::precompute_direct(ops);
return;
}
let used_grid = TL_OCCLUSION_GRID.try_with(|cell| {
let Ok(mut grid) = cell.try_borrow_mut() else {
return false;
};
grid.build(ops);
for (idx, op) in ops.iter_mut().enumerate() {
op.clip = super::occlusion_for_op(op, |clip| grid.clip_region(clip, idx));
}
true
});
if !matches!(used_grid, Ok(true)) {
super::precompute_direct(ops);
}
}
}
#[cfg(any(feature = "gpu", not(target_arch = "wasm32")))]
pub(crate) use spatial_occlusion::{SpatialOcclusionGrid, with_spatial_occlusion};
#[inline]
fn occlusion_for_op(op: &DrawOp<'_>, clip_region: impl Fn(Region) -> Region) -> Region {
let transformed =
matches!(op.payload, DrawPayload::StyledQuad(sq) if sq.transform.is_some());
let bounds_clip = if transformed {
op.clip
} else {
op.clip.intersect(op.bounds)
};
if bounds_clip.is_empty() {
return Region::EMPTY;
}
let visible = clip_region(bounds_clip);
if visible.is_empty() {
return Region::EMPTY;
}
if op.clip_radius > 0 || transformed {
return op.clip;
}
if matches!(op.payload, DrawPayload::TextRun { .. }) {
return clip_region(op.clip);
}
visible
}
fn precompute_direct(ops: &mut [DrawOp<'_>]) {
for idx in 0..ops.len() {
ops[idx].clip = occlusion_for_op(&ops[idx], |clip| {
compute_unoccluded_clip(clip, &ops[(idx + 1)..])
});
}
}
pub fn precompute_occlusion_clips(ops: &mut [DrawOp<'_>]) {
if super::effects::ops_have_layers_or_blend(ops) {
return;
}
{
spatial_occlusion::precompute_spatial(ops);
return;
}
}
#[inline]
pub(super) fn clip_against_opaque_rects(mut clip: Region, occluders: &[(usize, Region)], min_idx: usize) -> Region {
for &(occ_idx, occ) in occluders {
if occ_idx <= min_idx {
continue;
}
if clip.is_empty() {
return Region::EMPTY;
}
clip = apply_single_occluder(clip, occ);
}
clip
}
#[inline]
pub fn compute_unoccluded_clip(mut clip: Region, ops_above: &[DrawOp<'_>]) -> Region {
for above in ops_above {
if clip.is_empty() {
return Region::EMPTY;
}
let occ = opaque_core_rect(above);
if occ.is_empty() {
continue;
}
clip = apply_single_occluder(clip, occ);
}
clip
}
#[inline(always)]
fn erf_approx(x: f32) -> f32 {
let s = if x < 0.0 { -1.0 } else if x > 0.0 { 1.0 } else { 0.0 };
let a = x.abs();
let t = 1.0 + (0.278_393 + (0.230_389 + 0.078_108 * (a * a)) * a) * a;
let t2 = t * t;
s - s / (t2 * t2)
}
#[inline(always)]
fn gaussian_1d(x: f32, sigma: f32) -> f32 {
let inv = x / sigma;
(-0.5 * inv * inv).exp() / (2.506_628_3 * sigma)
}
#[inline(always)]
fn rounded_box_shadow_x(x: f32, y: f32, sigma: f32, corner: f32, half_w: f32, half_h: f32) -> f32 {
let delta = (half_h - corner - y.abs()).min(0.0);
let curved = half_w - corner + (corner * corner - delta * delta).max(0.0).sqrt();
let k = 0.707_106_77 / sigma;
let i0 = 0.5 + 0.5 * erf_approx((x - curved) * k);
let i1 = 0.5 + 0.5 * erf_approx((x + curved) * k);
i1 - i0
}
#[inline]
fn eval_gaussian_rounded_box_shadow(
center_x: f32,
center_y: f32,
half_w: f32,
half_h: f32,
px: f32,
py: f32,
sigma: f32,
corner: f32,
) -> f32 {
let ptx = px - center_x;
let pty = py - center_y;
let low = pty - half_h;
let high = pty + half_h;
let start = (-3.0 * sigma).clamp(low, high);
let end = (3.0 * sigma).clamp(low, high);
let step = (end - start) * 0.25;
if step <= 0.0001 {
return 0.0;
}
let mut y = start + step * 0.5;
let mut val = 0.0f32;
for _ in 0..4 {
val += rounded_box_shadow_x(ptx, pty - y, sigma, corner, half_w, half_h)
* gaussian_1d(y, sigma)
* step;
y += step;
}
val.clamp(0.0, 1.0)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn rasterize_quad<P: PixelFormat>(
bounds: Region,
clip: Region,
bg: Color,
bg_grad: Color,
border_color: Color,
rounded: i16,
encoded_border_width: u8,
origin_x: i16,
origin_y: i16,
stride: usize,
target: &mut [P],
) {
let r_16 = (rounded.max(0) as i32) * 16;
let raw_bw = encoded_border_width & 0x7F;
rasterize_quad_precise(
bounds,
clip,
bg,
bg_grad,
border_color,
r_16,
raw_bw as i16,
raw_bw as f32,
(encoded_border_width & 0x80) != 0,
origin_x,
origin_y,
stride,
target,
);
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn rasterize_quad_precise<P: PixelFormat>(
bounds: Region,
clip: Region,
bg: Color,
bg_grad: Color,
border_color: Color,
rounded_16: i32,
border_width: i16,
#[allow(unused_variables)] blur_px_f32: f32,
grad_horiz: bool,
origin_x: i16,
origin_y: i16,
stride: usize,
target: &mut [P],
) {
let is_soft_shadow = border_color.a() == 0 && border_width > 0;
if is_soft_shadow {
if bg.a() == 0 {
return;
}
{
let half_w = (bounds.w as f32) * 0.5;
let half_h = (bounds.h as f32) * 0.5;
let center_x = (bounds.x as f32) + half_w;
let center_y = (bounds.y as f32) + half_h;
let blur = blur_px_f32.max(1.0);
let inner_half_w = (half_w - blur).max(0.5);
let inner_half_h = (half_h - blur).max(0.5);
let sigma = (blur * 0.45).max(0.5);
let radius = (rounded_16.max(0) as f32) * (1.0 / 16.0);
let corner = radius.min(inner_half_w.min(inner_half_h));
let safe_margin = corner + 3.5 * sigma;
let core_hw = (inner_half_w - safe_margin).max(0.0);
let core_hh = (inner_half_h - safe_margin).max(0.0);
for py in clip.y..clip.bottom() {
let row_offset = ((py - origin_y) as usize) * stride;
let py_f = (py as f32) + 0.5;
let in_core_y = core_hh > 0.0 && (py_f - center_y).abs() < core_hh;
for px in clip.x..clip.right() {
let px_f = (px as f32) + 0.5;
let cov_u8 = if in_core_y && core_hw > 0.0 && (px_f - center_x).abs() < core_hw
{
255u8
} else {
let cov = eval_gaussian_rounded_box_shadow(
center_x,
center_y,
inner_half_w,
inner_half_h,
px_f,
py_f,
sigma,
corner,
);
if cov <= 0.001 {
continue;
}
(cov * 255.0 + 0.5) as u8
};
if cov_u8 > 0 {
let idx = row_offset + ((px - origin_x) as usize);
if let Some(dst) = target.get_mut(idx) {
*dst = dst.blend(bg, cov_u8);
}
}
}
}
return;
}
}
let has_grad = bg != bg_grad;
let has_border = border_width > 0 && border_color.a() > 0;
if rounded_16 <= 0 && !has_border && !has_grad && bg.a() == 255 {
let px_val = P::from_color(bg);
for py in clip.y..clip.bottom() {
let row_idx = ((py - origin_y) as usize) * stride;
let start_col = (clip.x - origin_x) as usize;
let end_col = (clip.right() - origin_x) as usize;
if let Some(row_slice) = target.get_mut((row_idx + start_col)..(row_idx + end_col)) {
row_slice.fill(px_val);
}
}
return;
}
let h_denom = bounds.h.max(1) as i32;
let w_denom = bounds.w.max(1) as i32;
let tx_step_q16: i64 = if has_grad && grad_horiz {
(256i64 << 16) / (w_denom as i64)
} else {
0
};
let tx_base_q16: i64 = if has_grad && grad_horiz {
let start_dx = (clip.x - bounds.x).max(0) as i64;
((start_dx * 2 + 1) * (128i64 << 16)) / (w_denom as i64)
} else {
0
};
let (cx_f, cy_f, r_f, hx_f, hy_f) = {
let hw = (bounds.w as f32) * 0.5;
let hh = (bounds.h as f32) * 0.5;
let cx = (bounds.x as f32) + hw;
let cy = (bounds.y as f32) + hh;
let r = ((rounded_16.max(0) as f32) * (1.0 / 16.0)).clamp(0.0, hw.min(hh));
(cx, cy, r, hw - r, hh - r)
};
let rounded_ceil = ((rounded_16.max(0) + 15) >> 4) as i16;
let bw_inset = border_width.saturating_add(1);
let corner_inset = rounded_ceil.saturating_add(bw_inset);
let inner_y0 = bounds.y.saturating_add(bw_inset);
let inner_y1 = bounds.bottom().saturating_sub(bw_inset);
let mid_y0 = bounds.y.saturating_add(corner_inset);
let mid_y1 = bounds.bottom().saturating_sub(corner_inset);
let corner_flat_x0 = bounds.x.saturating_add(corner_inset).max(clip.x);
let corner_flat_x1 = bounds.right().saturating_sub(corner_inset).min(clip.right());
let mid_flat_x0 = bounds.x.saturating_add(bw_inset).max(clip.x);
let mid_flat_x1 = bounds.right().saturating_sub(bw_inset).min(clip.right());
for py in clip.y..clip.bottom() {
let row_offset = ((py - origin_y) as usize) * stride;
let row_bg = if has_grad && !grad_horiz {
let ty_256 =
((((py - bounds.y).max(0) as i32 * 2 + 1) * 128) / h_denom).clamp(0, 256) as u16;
bg.lerp(bg_grad, ty_256)
} else {
bg
};
let (flat_x0, flat_x1) = if py >= mid_y0 && py < mid_y1 {
(mid_flat_x0, mid_flat_x1)
} else if py >= inner_y0 && py < inner_y1 {
(corner_flat_x0, corner_flat_x1)
} else {
(0, 0)
};
let use_flat_span = flat_x0 < flat_x1 && !(has_grad && grad_horiz);
let (qy_f, oy_f, oy2_f) = {
let qy = ((py as f32 + 0.5) - cy_f).abs() - hy_f;
let oy = qy.max(0.0);
(qy, oy, oy * oy)
};
let mut tx_q16 = tx_base_q16;
let mut px = clip.x;
let px_end = clip.right();
while px < px_end {
if use_flat_span && px == flat_x0 {
if row_bg.a() == 255 {
let s_col = (flat_x0 - origin_x) as usize;
let e_col = (flat_x1 - origin_x) as usize;
if let Some(slice) = target.get_mut((row_offset + s_col)..(row_offset + e_col))
{
if P::NEEDS_DITHER && has_grad {
for (i, dst_px) in slice.iter_mut().enumerate() {
*dst_px = P::from_color_dithered(row_bg, flat_x0 + i as i16, py);
}
} else {
slice.fill(P::from_color(row_bg));
}
}
} else if row_bg.a() > 0 {
let s_col = (flat_x0 - origin_x) as usize;
let e_col = (flat_x1 - origin_x) as usize;
if let Some(slice) = target.get_mut((row_offset + s_col)..(row_offset + e_col))
{
for dst in slice.iter_mut() {
*dst = dst.blend(row_bg, 255);
}
}
}
px = flat_x1;
continue;
}
let (outer_cov, sd_outer_f32) = {
let qx = ((px as f32 + 0.5) - cx_f).abs() - hx_f;
let ox = qx.max(0.0);
let out_d = if ox == 0.0 {
oy_f
} else if oy_f == 0.0 {
ox
} else {
(ox * ox + oy2_f).sqrt()
};
let sd = out_d + qx.max(qy_f).min(0.0) - r_f;
let cov = ((0.5 - sd).clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
(cov, sd)
};
if outer_cov == 0 {
px += 1;
tx_q16 += tx_step_q16;
continue;
}
let idx = row_offset + ((px - origin_x) as usize);
let Some(dst) = target.get_mut(idx) else {
px += 1;
tx_q16 += tx_step_q16;
continue;
};
let cur_bg = if has_grad && grad_horiz {
let tx_256 = (tx_q16 >> 16).clamp(0, 256) as u16;
bg.lerp(bg_grad, tx_256)
} else {
row_bg
};
if has_border {
let inner_cov = {
let sd_in = sd_outer_f32 + (border_width as f32);
((0.5 - sd_in).clamp(0.0, 1.0) * 255.0 + 0.5) as u32
};
if inner_cov == 255 {
if cur_bg.a() > 0 {
*dst = dst.blend(cur_bg, outer_cov);
}
} else if inner_cov == 0 {
*dst = dst.blend(border_color, outer_cov);
} else if cur_bg.a() == 0 {
let ring_cov = (((outer_cov as u32) * (255 - inner_cov) + 127) / 255) as u8;
if ring_cov > 0 {
*dst = dst.blend(border_color, ring_cov);
}
} else {
let ba = border_color.a() as u32;
let fa = cur_bg.a() as u32;
let inv_ic = 255 - inner_cov;
let pm_a = (ba * inv_ic + fa * inner_cov + 127) / 255;
if pm_a > 0 {
let pm_r = ((border_color.r() as u32) * ba * inv_ic
+ (cur_bg.r() as u32) * fa * inner_cov
+ 127 * 255)
/ (255 * 255);
let pm_g = ((border_color.g() as u32) * ba * inv_ic
+ (cur_bg.g() as u32) * fa * inner_cov
+ 127 * 255)
/ (255 * 255);
let pm_b = ((border_color.b() as u32) * ba * inv_ic
+ (cur_bg.b() as u32) * fa * inner_cov
+ 127 * 255)
/ (255 * 255);
let out_a = ((pm_a * (outer_cov as u32) + 127) / 255).min(255);
if out_a > 0 {
let sr = ((pm_r * (outer_cov as u32) + 127) / 255).min(255);
let sg = ((pm_g * (outer_cov as u32) + 127) / 255).min(255);
let sb = ((pm_b * (outer_cov as u32) + 127) / 255).min(255);
let bg_px = dst.to_color();
let inv_a = 255 - out_a;
let r = (sr + ((bg_px.r() as u32) * inv_a + 127) / 255).min(255) as u8;
let g = (sg + ((bg_px.g() as u32) * inv_a + 127) / 255).min(255) as u8;
let b = (sb + ((bg_px.b() as u32) * inv_a + 127) / 255).min(255) as u8;
let da = bg_px.a() as u32;
let final_a = (out_a + ((da * inv_a + 127) / 255)).min(255) as u8;
*dst = P::from_color(Color::rgba(r, g, b, final_a));
}
}
}
} else if cur_bg.a() > 0 {
if P::NEEDS_DITHER && has_grad && outer_cov == 255 && cur_bg.a() == 255 {
*dst = P::from_color_dithered(cur_bg, px, py);
} else {
*dst = dst.blend(cur_bg, outer_cov);
}
}
px += 1;
tx_q16 += tx_step_q16;
}
}
}