dymokomi/luce-browser-render
Source of release 0.6.2
0.6.2 / src / display_list / cpu_mask_blur.lucb
#==============================================================================================
#
# cpu_mask_blur - shapes drawn with SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma)
#
# DESCRIPTION:
# Port of how Skia m144's raster device draws a rectangle, a rounded rectangle or a path
# whose paint has a blur mask filter (skcpu::Draw::drawRect, drawRRect,
# drawRRectNinePatch and drawDevPath; SkMaskFilterBase::filterRects, filterRRect,
# filterPath and draw_nine; SkBlurMaskFilterImpl::filterRectsToNine and
# filterRRectToNine; skcpu::DrawToMask): a rectangle blurs analytically
# (SkBlurMask::BlurRect) into a small nine-patch, a rounded rectangle draws a small copy
# of itself into a mask and blurs that (SkMaskBlurFilter), and anything else (an oval,
# a shape too small for a nine-patch, a rotated one) is drawn into a device mask that is
# blurred whole. The nine-patch is expanded to the shape's blurred bounds here and drawn
# as one mask: draw_nine's pieces (masks, stretched rows and columns, the filled center)
# blit the same coverage. The GPU player takes the nine-patch itself (CpuNinePatch,
# cpu_blurred_rrect_nine) and stretches it in its shader. The sigma is SkBlurMaskFilterImpl::computeXformedSigma's
# (SkMatrix::mapRadius, at most 128). A sigma that blurs nothing draws the shape plainly.
#
# Copyright 2006 The Android Open Source Project
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia)
#
#==============================================================================================
## The kMaxMargin of compute_mask_bounds.
let cpu_mask_max_margin: i32 = 128
# mark: Sigma ==================================================================================
## SkMatrix::mapRadius: the geometric mean of the lengths of (radius, 0) and (0, radius)
## mapped as vectors.
func cpu_map_radius(transform: raster.Transform, radius: f32) -> f32:
var v0 = raster.Point.from_xy(radius, 0.0)
var v1 = raster.Point.from_xy(0.0, radius)
if transform.has_perspective():
var origin = raster.Point.from_xy(0.0, 0.0)
transform.map_point(&origin)
transform.map_point(&v0)
transform.map_point(&v1)
v0 = v0.sub(origin)
v1 = v1.sub(origin)
else:
var linear = transform
linear.tx = 0.0
linear.ty = 0.0
linear.map_point(&v0)
linear.map_point(&v1)
return math32.sqrt(v0.length() * v1.length())
## SkBlurMaskFilterImpl::computeXformedSigma.
func cpu_xformed_sigma(transform: raster.Transform, sigma: f32) -> f32:
return math32.min(cpu_map_radius(transform, sigma), 128.0)
# mark: Nine-patches ===========================================================================
## A blurred shape's coverage as draw_nine takes it: the small `mask` covers the whole of
## `left`, `top`, `width`, `height` (device pixels) with its corners, its row `center_y` and
## column `center_x` stretched along the edges, and the center 255 (`fill_center`) or left
## alone. Unless `stretched`, `mask` is the whole coverage at those bounds as it is.
pub struct CpuNinePatch:
pub var mask: CpuA8Mask
pub var left: i32
pub var top: i32
pub var width: i32
pub var height: i32
pub var center_x: i32
pub var center_y: i32
pub var fill_center: bool
pub var stretched: bool
## A nine-patch of the small `mask` over `outer` (see CpuNinePatch); it takes the mask.
func cpu_nine_patch(mask: CpuA8Mask, outer: CpuIRect, center_x: i32, center_y: i32, fill_center: bool) -> CpuNinePatch:
return CpuNinePatch(mask = mask, left = outer.left, top = outer.top, width = outer.right - outer.left, height = outer.bottom - outer.top,
center_x = center_x, center_y = center_y, fill_center = fill_center, stretched = true)
## The whole `mask` as a nine-patch that stretches nothing; it takes the mask.
func cpu_whole_patch(mask: CpuA8Mask) -> CpuNinePatch:
return CpuNinePatch(mask = mask, left = mask.left, top = mask.top, width = mask.width, height = mask.height)
## Frees the nine-patch's mask.
pub func cpu_nine_patch_release(nine: CpuNinePatch*):
cpu_a8_mask_release(&nine.mask)
## draw_nine's coverage over the nine-patch's bounds, as one mask; releases the small mask.
func cpu_expand_nine_patch(nine: CpuNinePatch*) -> CpuA8Mask:
if not nine.stretched:
return nine.mask
let mask = &nine.mask
defer cpu_a8_mask_release(&nine.mask)
let center_x = nine.center_x
let center_y = nine.center_y
let fill_center = nine.fill_center
let width = nine.width
let height = nine.height
var result = cpu_a8_mask_new(nine.left, nine.top, width, height)
# innerR, relative to `outer`.
let inner_left = center_x
let inner_top = center_y
let inner_right = width + (center_x + 1 - mask.width)
let inner_bottom = height + (center_y + 1 - mask.height)
for y in 0..<height:
let in_rows = y >= inner_top and y < inner_bottom
let my = y if y < inner_top else (center_y if in_rows else y - (height - mask.height))
for x in 0..<width:
let in_columns = x >= inner_left and x < inner_right
var value: u8 = 0
if in_rows and in_columns:
value = 255 if fill_center else 0
else:
let mx = x if x < inner_left else (center_x if in_columns else x - (width - mask.width))
value = mask.image[(usize)(my * mask.width + mx)]
result.image[(usize)(y * width + x)] = value
return result
## What a nine-patch filter answers (SkMaskFilterBase::FilterReturn): the expanded mask, no
## mask and no plain fill (kFalse), or the fall back to a mask of the whole shape
## (kUnimplemented).
enum CpuNineResult:
mask(value: CpuNinePatch)
nothing
unimplemented
## SkBlurMaskFilterImpl::filterRectsToNine for one device rectangle (left, top, right, bottom).
func cpu_filter_rect_to_nine(rect: f32[4], sigma: f32) -> CpuNineResult:
if cpu_rect_exceeds(rect, 32767.0):
return .unimplemented
let src_bounds = cpu_round_out(rect)
# The bounds only, to know the margin.
var bounds_only = cpu_blur_rect(sigma, rect, false) else return .nothing
let dst = bounds_only.0
cpu_a8_mask_release(&bounds_only.0)
# The smallest rectangle that blurs the same at every edge, plus one stretchable center
# row and column (one more pixel on each side for fractional edges).
var small_w = dst.width - (src_bounds.right - src_bounds.left) + 2
var small_h = dst.height - (src_bounds.bottom - src_bounds.top) + 2
let center_x = small_w
let center_y = small_h
small_w += 1
small_h += 1
let dx = (f32)((src_bounds.right - src_bounds.left) - small_w)
let dy = (f32)((src_bounds.bottom - src_bounds.top) - small_h)
if dx < 0.0 or dy < 0.0:
return .unimplemented
let small: f32[4] = [rect[0], rect[1], rect[2] - dx, rect[3] - dy]
if small[2] - small[0] < 2.0 or small[3] - small[1] < 2.0:
return .unimplemented
let filtered = cpu_blur_rect(sigma, small, true) else return .nothing
let outer = CpuIRect(left = dst.left, top = dst.top, right = dst.left + dst.width, bottom = dst.top + dst.height)
return .mask(value = cpu_nine_patch(filtered.0, outer, center_x, center_y, true))
## SkBlurMaskFilterImpl::filterRectsToNine for two nested device rectangles (the even-odd
## frame between them): the small frame is drawn into a mask and blurred; the center is left.
func cpu_filter_nested_rects_to_nine(outer_rect: f32[4], inner_rect: f32[4], sigma: f32) -> CpuNineResult:
if cpu_rect_exceeds(outer_rect, 32767.0):
return .unimplemented
let blur_sigma = cpu_mask_blur_sigma(sigma)
if cpu_mask_blur_has_no_blur(blur_sigma):
return .nothing
let margin = cpu_mask_blur_border(blur_sigma)
let src_bounds = cpu_round_out(outer_rect)
let outer = CpuIRect(left = src_bounds.left - margin, top = src_bounds.top - margin, right = src_bounds.right + margin, bottom = src_bounds.bottom + margin)
var small_w = 2 * margin + 2
var small_h = 2 * margin + 2
# SkRect::roundIn of the inner rectangle.
let inner_ir = CpuIRect(left = (i32)math32.ceil(inner_rect[0]), top = (i32)math32.ceil(inner_rect[1]), right = (i32)math32.floor(inner_rect[2]), bottom = (i32)math32.floor(inner_rect[3]))
let center_x = small_w + (inner_ir.left - src_bounds.left)
let center_y = small_h + (inner_ir.top - src_bounds.top)
small_w += 1
small_h += 1
let dx = (f32)((inner_ir.right - inner_ir.left) - small_w)
let dy = (f32)((inner_ir.bottom - inner_ir.top) - small_h)
if dx < 0.0 or dy < 0.0:
return .unimplemented
let small0: f32[4] = [outer_rect[0], outer_rect[1], outer_rect[2] - dx, outer_rect[3] - dy]
if small0[2] - small0[0] < 2.0 or small0[3] - small0[1] < 2.0:
return .unimplemented
let small1: f32[4] = [inner_rect[0], inner_rect[1], inner_rect[2] - dx, inner_rect[3] - dy]
# draw_rects_into_mask: both rectangles as one even-odd path, anti-aliased.
let r0 = raster.Rect.from_ltrb(small0[0], small0[1], small0[2], small0[3]) else return .nothing
let r1 = raster.Rect.from_ltrb(small1[0], small1[1], small1[2], small1[3]) else return .nothing
var builder = raster.PathBuilder.new_()
builder.push_rect(r0)
builder.push_rect(r1)
var frame = builder.finish() else return .nothing
defer frame.release()
var src = cpu_path_mask(&frame, raster.FillRule.even_odd, cpu_round_out(small0))
defer cpu_a8_mask_release(&src)
let filtered = cpu_box_blur(&src, sigma) else return .nothing
return .mask(value = cpu_nine_patch(filtered.0, outer, center_x, center_y, false))
## SkBlurMaskFilterImpl::filterRRectToNine for a device rounded rectangle that is neither a
## rectangle nor an oval.
func cpu_filter_rrect_to_nine(rect: raster.Rect, radii: CpuRadii, sigma: f32) -> CpuNineResult:
let bounds: f32[4] = [rect.left(), rect.top(), rect.right(), rect.bottom()]
if cpu_rect_exceeds(bounds, 32767.0):
return .unimplemented
let blur_sigma = cpu_mask_blur_sigma(sigma)
if cpu_mask_blur_has_no_blur(blur_sigma):
return .unimplemented
let margin = cpu_mask_blur_border(blur_sigma)
let src_bounds = cpu_round_out(bounds)
let outer = CpuIRect(left = src_bounds.left - margin, top = src_bounds.top - margin, right = src_bounds.right + margin, bottom = src_bounds.bottom + margin)
# The larger radius per side (rounded up) and the margin on each side of one stretchable
# pixel.
let left_unstretched = (i32)math32.ceil(math32.max(radii.x[0], radii.x[3])) + margin
let right_unstretched = (i32)math32.ceil(math32.max(radii.x[1], radii.x[2])) + margin
let total_small_width = left_unstretched + right_unstretched + 1
if (f32)total_small_width >= rect.width():
return .unimplemented
let top_unstretched = (i32)math32.ceil(math32.max(radii.y[0], radii.y[1])) + margin
let bottom_unstretched = (i32)math32.ceil(math32.max(radii.y[3], radii.y[2])) + margin
let total_small_height = top_unstretched + bottom_unstretched + 1
if (f32)total_small_height >= rect.height():
return .unimplemented
# The small rounded rectangle, drawn anti-aliased into a mask, then blurred.
let small_rect = raster.Rect.from_ltrb(0.0, 0.0, (f32)total_small_width, (f32)total_small_height) else return .nothing
var small_path = cpu_rrect_path(small_rect, radii)
defer small_path.release()
var src = cpu_path_mask(&small_path, raster.FillRule.winding, CpuIRect(left = 0, top = 0, right = total_small_width, bottom = total_small_height))
defer cpu_a8_mask_release(&src)
let filtered = cpu_box_blur(&src, sigma) else return .nothing
return .mask(value = cpu_nine_patch(filtered.0, outer, margin + left_unstretched, margin + top_unstretched, true))
## rect_exceeds.
func cpu_rect_exceeds(r: f32[4], v: f32) -> bool:
return r[0] < -v or r[1] < -v or r[2] > v or r[3] > v or r[2] - r[0] > v or r[3] - r[1] > v
## SkRect::roundOut.
func cpu_round_out(r: f32[4]) -> CpuIRect:
return CpuIRect(left = (i32)math32.floor(r[0]), top = (i32)math32.floor(r[1]), right = (i32)math32.ceil(r[2]), bottom = (i32)math32.ceil(r[3]))
# mark: Device masks ===========================================================================
## The anti-aliased coverage of the device `path` over `bounds` (SkScan::AntiFillPath into an
## A8 mask through a blitter, as draw_into_mask draws it: the scan converter may take its
## mask accumulator, which an SkAAClip's forced run-length path does not): an opaque white fill
## onto transparent pixels leaves the coverage in the alpha channel.
func cpu_path_mask(path: const raster.Path*, fill_rule: raster.FillRule, bounds: CpuIRect) -> CpuA8Mask:
let width = bounds.right - bounds.left
let height = bounds.bottom - bounds.top
var result = cpu_a8_mask_new(bounds.left, bounds.top, width, height)
var pixmap = raster.Pixmap.new_((u32)width, (u32)height) else trap("CpuCanvas: empty mask")
defer pixmap.release()
var white = raster.Paint()
white.set_color_rgba8(255, 255, 255, 255)
pixmap.fill_path(path, &white, fill_rule, raster.Transform.from_translate(-(f32)bounds.left, -(f32)bounds.top), none)
let data = pixmap.data()
for i in 0..<(usize)width * (usize)height:
result.image[i] = data[i * 4 + 3]
return result
## skcpu::DrawToMask then filterMask: the device `path`'s coverage over its bounds (outset by
## half a pixel, rounded out, kept within the clip bounds plus the blur's margin), blurred;
## none when the bounds are empty or nothing blurs.
func cpu_blurred_path_mask(path: const raster.Path*, fill_rule: raster.FillRule, clip: CpuIRect, sigma: f32) -> CpuA8Mask?:
let blur_sigma = cpu_mask_blur_sigma(sigma)
if cpu_mask_blur_has_no_blur(blur_sigma):
return none
let margin = math32.min((f32)cpu_mask_blur_border(blur_sigma), (f32)cpu_mask_max_margin)
let b = path.bounds()
var bounds = cpu_round_out([b.left() - 0.5, b.top() - 0.5, b.right() + 0.5, b.bottom() + 0.5])
let m = (i32)margin
bounds.left = cpu_max_i32(bounds.left, clip.left - m)
bounds.top = cpu_max_i32(bounds.top, clip.top - m)
bounds.right = cpu_min_i32(bounds.right, clip.right + m)
bounds.bottom = cpu_min_i32(bounds.bottom, clip.bottom + m)
if bounds.left >= bounds.right or bounds.top >= bounds.bottom:
return none
var src = cpu_path_mask(path, fill_rule, bounds)
defer cpu_a8_mask_release(&src)
let blurred = cpu_box_blur(&src, sigma) else return none
return blurred.0
# mark: Drawing ================================================================================
## Blits the nine-patch `nine` expanded, with `paint`, and releases it.
func cpu_canvas_blit_nine_patch(canvas: CpuCanvas*, nine: CpuNinePatch*, paint: const raster.Paint*):
var expanded = cpu_expand_nine_patch(nine)
cpu_canvas_blit_mask(canvas, &expanded, paint)
cpu_a8_mask_release(&expanded)
## Blits `mask` with `paint` through the canvas's clip (blitMask through the raster clip).
func cpu_canvas_blit_mask(canvas: CpuCanvas*, mask: const CpuA8Mask*, paint: const raster.Paint*):
if mask.width <= 0 or mask.height <= 0:
return
let placed: raster.DeviceMask[1] = [raster.DeviceMask(image = mask.image, left = mask.left, top = mask.top, width = mask.width, height = mask.height)]
cpu_canvas_target(canvas).fill_masks(placed, paint, cpu_canvas_draw_mask(canvas))
## skcpu::Draw::drawDevPath with the blur mask filter, for the device `path` (filterPath): a
## rectangle goes through filterRects, anything else through a whole blurred mask; when the
## filter declines, the path is filled plainly.
func cpu_canvas_fill_dev_path_blurred(canvas: CpuCanvas*, path: const raster.Path*, fill_rule: raster.FillRule, paint: const raster.Paint*, sigma: f32):
if let rect = cpu_path_as_rect(path):
match cpu_filter_rect_to_nine(rect, sigma):
.mask(value):
var nine = value
cpu_canvas_blit_nine_patch(canvas, &nine, paint)
return
.nothing:
cpu_canvas_target(canvas).fill_path(path, paint, fill_rule, raster.Transform.identity(), cpu_canvas_draw_mask(canvas))
return
.unimplemented:
_ = 0
let state = cpu_canvas_state(canvas)
let clip = CpuIRect(left = state.clip_left, top = state.clip_top, right = state.clip_right, bottom = state.clip_bottom)
if let blurred = cpu_blurred_path_mask(path, fill_rule, clip, sigma):
var mask = blurred
cpu_canvas_blit_mask(canvas, &mask, paint)
cpu_a8_mask_release(&mask)
return
cpu_canvas_target(canvas).fill_path(path, paint, fill_rule, raster.Transform.identity(), cpu_canvas_draw_mask(canvas))
## filterPath's mask route for a shape given as device `coverage` (a mask of the layer's size)
## with device `bounds`: the coverage over the bounds outset by half a pixel (within the clip
## plus the margin), blurred and blitted; the coverage itself when nothing blurs.
func cpu_canvas_fill_coverage_blurred(canvas: CpuCanvas*, coverage: raster.Mask*, bounds: raster.Rect, paint: const raster.Paint*, sigma: f32):
let blur_sigma = cpu_mask_blur_sigma(sigma)
if cpu_mask_blur_has_no_blur(blur_sigma):
cpu_canvas_fill_coverage(canvas, coverage, paint)
return
let state = cpu_canvas_state(canvas)
let m = (i32)math32.min((f32)cpu_mask_blur_border(blur_sigma), (f32)cpu_mask_max_margin)
var b = cpu_round_out([bounds.left() - 0.5, bounds.top() - 0.5, bounds.right() + 0.5, bounds.bottom() + 0.5])
b.left = cpu_max_i32(cpu_max_i32(b.left, state.clip_left - m), 0)
b.top = cpu_max_i32(cpu_max_i32(b.top, state.clip_top - m), 0)
b.right = cpu_min_i32(cpu_min_i32(b.right, state.clip_right + m), (i32)coverage.width())
b.bottom = cpu_min_i32(cpu_min_i32(b.bottom, state.clip_bottom + m), (i32)coverage.height())
if b.left >= b.right or b.top >= b.bottom:
return
var src = cpu_a8_mask_new(b.left, b.top, b.right - b.left, b.bottom - b.top)
defer cpu_a8_mask_release(&src)
let data = coverage.data()
let stride = (usize)coverage.width()
for y in 0..<(usize)src.height:
for x in 0..<(usize)src.width:
src.image[y * (usize)src.width + x] = data[((usize)b.top + y) * stride + (usize)b.left + x]
var blurred = cpu_box_blur(&src, sigma) else return
cpu_canvas_blit_mask(canvas, &blurred.0, paint)
cpu_a8_mask_release(&blurred.0)
## SkPathRaw::isRect of a device path: the rectangle (left, top, right, bottom) of a path made
## of one closed axis-aligned rectangle; none otherwise.
func cpu_path_as_rect(path: const raster.Path*) -> f32[4]?:
let verbs = path.verbs_view()
let points = path.points_view()
if verbs.length < 4 or points.length < 4:
return none
if verbs[0] != raster.PathVerb.move:
return none
var count: usize = 1
for i in 1..<verbs.length:
match verbs[i]:
.line: count += 1
.close: _ = 0
_: return none
if count != 4 and not (count == 5 and points[4].x == points[0].x and points[4].y == points[0].y):
return none
for i in 0..<4usize:
let a = points[i]
let b = points[(i + 1) % 4]
if a.x != b.x and a.y != b.y:
return none
let bounds = path.bounds()
if bounds.width() == 0.0 or bounds.height() == 0.0:
return none
return [bounds.left(), bounds.top(), bounds.right(), bounds.bottom()]
## SkCanvas::drawRRect of `rect` with `radii` and a blur mask filter of `sigma`, in the
## current transform: a rectangle as drawRect, an oval as drawOval, other rounded rectangles
## through filterRRect (SkRRect::transform, then drawRRectNinePatch) when the transform keeps
## rectangles rectangles (quarter turns and flips included); paths otherwise.
func cpu_canvas_fill_rrect_blurred(canvas: CpuCanvas*, rect: raster.Rect, radii0: CpuRadii, paint: const raster.Paint*, sigma0: f32):
if cpu_canvas_is_clip_empty(canvas):
return
let ts = cpu_canvas_transform(canvas)
let sigma = cpu_xformed_sigma(ts, sigma0)
let radii = cpu_normalize_radii(rect, radii0)
let kind = cpu_rrect_kind(rect, &radii)
if kind == CpuRRectKind.nine_patch and cpu_rect_stays_rect(ts):
let device = cpu_rrect_transform(rect, &radii, ts)
match cpu_filter_rrect_to_nine(device.0, device.1, sigma):
.mask(value):
var nine = value
cpu_canvas_blit_nine_patch(canvas, &nine, paint)
return
_:
_ = 0
# drawPath(SkPath::Rect / Oval / RRect): the device path, then drawDevPath.
var path = cpu_rrect_path(rect, radii)
defer path.release()
if not path.transform(ts):
return
cpu_canvas_fill_dev_path_blurred(canvas, &path, raster.FillRule.winding, paint, sigma)
## The coverage cpu_canvas_fill_rrect_blurred blits for `rect` with `radii0` and a blur of
## `sigma0` under the identity transform, as the nine-patch it is made from (a shape blurred
## whole is a nine-patch that stretches nothing): what the GPU player keeps in its atlas (a
## whole-pixel translation moves the mask as it moves the shape). The whole shape's mask is
## limited to `clip` plus the blur's margin, as the CPU player limits it to its clip; none
## clips nothing. None when the blur draws the shape plainly instead. The caller releases it.
pub func cpu_blurred_rrect_nine(rect: raster.Rect, radii0: CpuRadii, sigma0: f32, clip: raster.Rect? = none) -> CpuNinePatch?:
let sigma = cpu_xformed_sigma(raster.Transform.identity(), sigma0)
let radii = cpu_normalize_radii(rect, radii0)
if cpu_rrect_kind(rect, &radii) == CpuRRectKind.nine_patch:
match cpu_filter_rrect_to_nine(rect, radii, sigma):
.mask(value): return value
_: _ = 0
var path = cpu_rrect_path(rect, radii)
defer path.release()
if let bounds = cpu_path_as_rect(&path):
match cpu_filter_rect_to_nine(bounds, sigma):
.mask(value): return value
.nothing: return none
.unimplemented: _ = 0
let b = clip else path.bounds()
let limit = cpu_round_out([b.left(), b.top(), b.right(), b.bottom()])
let mask = cpu_blurred_path_mask(&path, raster.FillRule.winding, limit, sigma) else return none
return cpu_whole_patch(mask)
## SkBlurMaskFilterImpl::filterRectsToNine for two nested whole-pixel rectangles (left, top,
## right, bottom): the inner box shadow's frame, blurred by `sigma0` under the identity
## transform; none when Skia would draw it some other way. The caller releases it.
pub func cpu_blurred_frame_nine(outer: f32[4], inner: f32[4], sigma0: f32) -> CpuNinePatch?:
let sigma = cpu_xformed_sigma(raster.Transform.identity(), sigma0)
match cpu_filter_nested_rects_to_nine(outer, inner, sigma):
.mask(value): return value
_: return none
## What paint_inner_box_shadow blits (see cpu_inner_shadow_patch).
pub enum CpuInnerShadow:
patch(value: CpuNinePatch)
nothing
unsupported
## The coverage DisplayListPlayerCpu's paint_inner_box_shadow blits for the frame between
## `outer` (with the content's radii) and `inner` (with the shadow's), blurred by
## `blur_radius` / 2 under the identity transform, before its clip to the content: the
## frame's nine-patch for two nested rectangles, the blurred even-odd difference of the
## rounded rectangles (limited to `clip` plus the blur's margin), or the difference's plain
## coverage without a blur (limited to `clip`); for shapes that touch or cross, the outer
## coverage less the inner one, blurred. The caller releases the patch.
pub func cpu_inner_shadow_patch(outer: raster.Rect, outer_radii0: CpuRadii, inner: raster.Rect, inner_radii0: CpuRadii, blur_radius: i32, clip: raster.Rect) -> CpuInnerShadow:
let outer_radii = cpu_normalize_radii(outer, outer_radii0)
let inner_radii = cpu_normalize_radii(inner, inner_radii0)
let sigma = cpu_xformed_sigma(raster.Transform.identity(), (f32)(blur_radius // 2))
let both_rects = cpu_rrect_kind(outer, &outer_radii) == CpuRRectKind.rect and cpu_rrect_kind(inner, &inner_radii) == CpuRRectKind.rect
let inside = inner.left() >= outer.left() and inner.top() >= outer.top() and inner.right() <= outer.right() and inner.bottom() <= outer.bottom()
if both_rects and inside and blur_radius // 2 > 0:
match cpu_filter_nested_rects_to_nine([outer.left(), outer.top(), outer.right(), outer.bottom()], [inner.left(), inner.top(), inner.right(), inner.bottom()], sigma):
.mask(value): return .patch(value = value)
_: _ = 0
let limit = cpu_round_out([clip.left(), clip.top(), clip.right(), clip.bottom()])
var difference = cpu_inner_shadow_difference(outer, outer_radii, inner, inner_radii) else return cpu_crossing_shadow_patch(outer, outer_radii, inner, inner_radii, sigma, blur_radius // 2 > 0, limit)
defer difference.release()
if blur_radius // 2 > 0:
if let blurred = cpu_blurred_path_mask(&difference, raster.FillRule.even_odd, limit, sigma):
return .patch(value = cpu_whole_patch(blurred))
let b = difference.bounds()
var bounds = cpu_round_out([b.left(), b.top(), b.right(), b.bottom()])
bounds.left = cpu_max_i32(bounds.left, limit.left)
bounds.top = cpu_max_i32(bounds.top, limit.top)
bounds.right = cpu_min_i32(bounds.right, limit.right)
bounds.bottom = cpu_min_i32(bounds.bottom, limit.bottom)
if bounds.left >= bounds.right or bounds.top >= bounds.bottom:
return .nothing
return .patch(value = cpu_whole_patch(cpu_path_mask(&difference, raster.FillRule.even_odd, bounds)))
## paint_inner_box_shadow's shapes that touch or cross (SkPathOps' merge is not ported): the
## outer rounded rectangle's coverage less the inner's (as clip masks scan-convert them) over
## the outer one's bounds, blurred when `blur` (cpu_canvas_fill_coverage_blurred), within
## `limit` plus the blur's margin.
func cpu_crossing_shadow_patch(outer: raster.Rect, outer_radii: CpuRadii, inner: raster.Rect, inner_radii: CpuRadii, sigma: f32, blur: bool, limit: CpuIRect) -> CpuInnerShadow:
var outer_path = cpu_rrect_path(outer, outer_radii)
defer outer_path.release()
var inner_path = cpu_rrect_path(inner, inner_radii)
defer inner_path.release()
let blur_sigma = cpu_mask_blur_sigma(sigma)
let blurred = blur and not cpu_mask_blur_has_no_blur(blur_sigma)
let m = (i32)math32.min((f32)cpu_mask_blur_border(blur_sigma), (f32)cpu_mask_max_margin) if blurred else 0
let ob = outer_path.bounds()
let grow: f32 = 0.5 if blurred else 0.0
var b = cpu_round_out([ob.left() - grow, ob.top() - grow, ob.right() + grow, ob.bottom() + grow])
b.left = cpu_max_i32(b.left, limit.left - m)
b.top = cpu_max_i32(b.top, limit.top - m)
b.right = cpu_min_i32(b.right, limit.right + m)
b.bottom = cpu_min_i32(b.bottom, limit.bottom + m)
if b.left >= b.right or b.top >= b.bottom:
return .nothing
let width = b.right - b.left
let height = b.bottom - b.top
let shift = raster.Transform.from_translate(-(f32)b.left, -(f32)b.top)
var outer_mask = raster.Mask.new_((u32)width, (u32)height) else return .nothing
defer outer_mask.release()
outer_mask.fill_path(&outer_path, raster.FillRule.winding, true, shift)
var inner_mask = raster.Mask.new_((u32)width, (u32)height) else return .nothing
defer inner_mask.release()
inner_mask.fill_path(&inner_path, raster.FillRule.winding, true, shift)
var src = cpu_a8_mask_new(b.left, b.top, width, height)
let outer_data = outer_mask.data()
let inner_data = inner_mask.data()
for i in 0..<src.image.length:
src.image[i] = raster.premultiply_u8(outer_data[i], 255 - inner_data[i])
if not blurred:
return .patch(value = cpu_whole_patch(src))
defer cpu_a8_mask_release(&src)
let result = cpu_box_blur(&src, sigma) else return .nothing
return .patch(value = cpu_whole_patch(result.0))
## SkRRect's type, as far as the blurred draw cares.
enum CpuRRectKind:
rect
oval
nine_patch
## SkRRect::computeType: a rectangle when every corner is square, an oval when all radii are
## equal and reach half the sides; anything else takes the nine-patch path.
func cpu_rrect_kind(rect: raster.Rect, radii: const CpuRadii*) -> CpuRRectKind:
var all_square = radii.x[0] == 0.0 or radii.y[0] == 0.0
var all_equal = true
for i in 1..<4usize:
if radii.x[i] != 0.0 and radii.y[i] != 0.0:
all_square = false
if radii.x[i] != radii.x[i - 1] or radii.y[i] != radii.y[i - 1]:
all_equal = false
if all_square:
return CpuRRectKind.rect
if all_equal and radii.x[0] >= rect.width() * 0.5 and radii.y[0] >= rect.height() * 0.5:
return CpuRRectKind.oval
return CpuRRectKind.nine_patch
## SkMatrix::preservesAxisAlignment (rectStaysRect): no perspective, and either a scale
## (no skew) or a quarter turn (no scale terms), each nonzero.
func cpu_rect_stays_rect(ts: raster.Transform) -> bool:
if ts.has_perspective():
return false
if ts.kx == 0.0 and ts.ky == 0.0:
return ts.sx != 0.0 and ts.sy != 0.0
return ts.sx == 0.0 and ts.sy == 0.0 and ts.kx != 0.0 and ts.ky != 0.0
## SkRRect::transform by a matrix that keeps rectangles rectangles: the mapped rectangle, and
## the radii DeduceRRectFromContour reads back from the rounded rectangle's contour
## (SkPathRawShapes::RRect, clockwise from start index 6) mapped point by point: each conic's
## corner point names the corner it is at in the mapped rectangle, the two legs its radii;
## setRectRadii normalizes them again.
pub func cpu_rrect_transform(rect: raster.Rect, radii: const CpuRadii*, ts: raster.Transform) -> (raster.Rect, CpuRadii):
let mapped = rect.transform(ts) else return (rect, *radii)
let l = rect.left()
let t = rect.top()
let r = rect.right()
let b = rect.bottom()
# Each conic: its start, its corner and its end, in contour order (lower-left first).
var pts: raster.Point[12] = [
raster.Point.from_xy(l, t + radii.y[0]), raster.Point.from_xy(l, t), raster.Point.from_xy(l + radii.x[0], t),
raster.Point.from_xy(r - radii.x[1], t), raster.Point.from_xy(r, t), raster.Point.from_xy(r, t + radii.y[1]),
raster.Point.from_xy(r, b - radii.y[2]), raster.Point.from_xy(r, b), raster.Point.from_xy(r - radii.x[2], b),
raster.Point.from_xy(l + radii.x[3], b), raster.Point.from_xy(l, b), raster.Point.from_xy(l, b - radii.y[3]),
]
ts.map_points(pts)
var result = CpuRadii(x = [0.0, 0.0, 0.0, 0.0], y = [0.0, 0.0, 0.0, 0.0])
for i in 0..<4usize:
let start = pts[i * 3]
let corner = pts[i * 3 + 1]
let end = pts[i * 3 + 2]
let v10 = raster.Point.from_xy(corner.x - start.x, corner.y - start.y)
let v21 = raster.Point.from_xy(end.x - corner.x, end.y - corner.y)
var dx = math32.abs(v21.x)
var dy = math32.abs(v10.y)
if v10.x != 0.0:
dx = math32.abs(v10.x)
dy = math32.abs(v21.y)
elif v10.y == 0.0:
dx = math32.abs(v21.x)
dy = math32.abs(v21.y)
# kUpperLeft, kUpperRight, kLowerRight, kLowerLeft.
var index: usize = 2
if corner.x == mapped.left():
index = 0 if corner.y == mapped.top() else 3
elif corner.y == mapped.top():
index = 1
result.x[index] = dx
result.y[index] = dy
return (mapped, cpu_normalize_radii(mapped, result))