dymokomi/luce-browser-render
Source of release 0.6.1
0.6.1 / src / raster / lowp.lucb
#==============================================================================================
#
# lowp - the low precision (u16) raster pipeline: driver, stage table, memory stages
#
# DESCRIPTION:
# Port of tiny-skia pipeline/lowp.rs, part 1, brought to Skia m144's lowp stages
# (namespace lowp of src/opts/SkRasterPipeline_opts.h) as its NEON build computes
# them. A lowp pipeline uses u16 instead of f32 for colors; the pipeline compiler
# picks it when every stage has a lowp implementation, and the table below has one
# exactly for the stages m144's has. Skia's NEON lowp is 8 lanes wide; here the
# registers hold 16 lanes (`U16x16`), which computes the same per-pixel values.
# Coordinates travel through the u16 lanes as the bits of f32x16 (`lowp_join` /
# `lowp_split`). The blend, gradient and image stages are in lowp_stages.lucb and
# lowp_image.lucb.
#
# Copyright (c) 2018 Google Inc. All rights reserved.
# Copyright (c) 2020 Yevhenii Reizner All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause (see LICENSE-skia, LICENSE-tiny-skia)
#
#==============================================================================================
let lowp_stage_width: usize = 16
type LowpStageFn = func(LowpPipeline*) -> unit
struct LowpPipeline:
var index: usize
var functions: const LowpStageFn[]
var pixmap_src: PixmapRef
var pixmap: SubPixmapMut*
var mask_ctx: MaskCtx
var aa_mask_ctx: AAMaskCtx
var ctx: Context*
var r: U16x16
var g: U16x16
var b: U16x16
var a: U16x16
var dr: U16x16
var dg: U16x16
var db: U16x16
var da: U16x16
var tail: usize
var dx: usize
var dy: usize
func lowp_next_stage(p: LowpPipeline*):
let next = p.functions[p.index]
p.index += 1
next(p)
# Must be in the same order as Stage; lowp_null_fn marks a stage m144 has no lowp version of.
let lowp_stages: LowpStageFn[94] = [
lowp_move_source_to_destination,
lowp_move_destination_to_source,
lowp_clamp_01,
lowp_clamp_gamut,
lowp_premultiply,
lowp_uniform_color,
lowp_seed_shader,
lowp_load_dst,
lowp_store,
lowp_load_dst_u8,
lowp_store_u8,
lowp_gather,
lowp_load_mask_u8,
lowp_mask_u8,
lowp_scale_u8,
lowp_lerp_u8,
lowp_scale_1_float,
lowp_lerp_1_float,
lowp_scale_paint_alpha,
lowp_null_fn, # Dither
lowp_destination_atop,
lowp_destination_in,
lowp_destination_out,
lowp_destination_over,
lowp_source_atop,
lowp_source_in,
lowp_source_out,
lowp_source_over,
lowp_clear,
lowp_modulate,
lowp_multiply,
lowp_plus,
lowp_screen,
lowp_xor,
lowp_null_fn, # ColorBurn
lowp_null_fn, # ColorDodge
lowp_darken,
lowp_difference,
lowp_exclusion,
lowp_hard_light,
lowp_lighten,
lowp_overlay,
lowp_null_fn, # SoftLight
lowp_null_fn, # Hue
lowp_null_fn, # Saturation
lowp_null_fn, # Color
lowp_null_fn, # Luminosity
lowp_transform,
lowp_scale_translate_x,
lowp_null_fn, # Repeat
lowp_null_fn, # Mirror
lowp_null_fn, # Bilinear
lowp_null_fn, # Bicubic
lowp_bilerp_clamp_8888,
lowp_null_fn, # BicubicClamp8888
lowp_clamp_x_1,
lowp_mirror_x_1,
lowp_repeat_x_1,
lowp_gradient,
lowp_evenly_spaced_gradient,
lowp_evenly_spaced_2_stop_gradient,
lowp_xy_to_unit_angle,
lowp_xy_to_radius,
lowp_null_fn, # XYTo2PtConicalFocalOnCircle
lowp_null_fn, # XYTo2PtConicalWellBehaved
lowp_null_fn, # XYTo2PtConicalSmaller
lowp_null_fn, # XYTo2PtConicalGreater
lowp_null_fn, # XYTo2PtConicalStrip
lowp_null_fn, # Mask2PtConicalNan
lowp_null_fn, # Mask2PtConicalDegenerates
lowp_null_fn, # ApplyVectorMask
lowp_null_fn, # Alter2PtConicalCompensateFocal
lowp_null_fn, # Alter2PtConicalUnswap
lowp_null_fn, # NegateX
lowp_null_fn, # GammaExpand2
lowp_null_fn, # GammaExpandDestination2
lowp_null_fn, # GammaCompress2
lowp_null_fn, # GammaExpand22
lowp_null_fn, # GammaExpandDestination22
lowp_null_fn, # GammaCompress22
lowp_null_fn, # GammaExpandSrgb
lowp_null_fn, # GammaExpandDestinationSrgb
lowp_null_fn, # GammaCompressSrgb
lowp_transform_perspective,
lowp_null_fn, # unpremul
lowp_null_fn, # unpremul_polar
lowp_null_fn, # css_lab_to_xyz
lowp_null_fn, # css_oklab_to_linear_srgb
lowp_null_fn, # css_hcl_to_lab
lowp_null_fn, # css_hsl_to_srgb
lowp_null_fn, # css_hwb_to_srgb
lowp_null_fn, # bilinear_mipmap
lowp_null_fn, # ColorXform
lowp_null_fn, # ColorFilterXform
]
func lowp_start(functions: const LowpStageFn[], functions_tail: const LowpStageFn[], rect: ScreenIntRect, aa_mask_ctx: AAMaskCtx, mask_ctx: MaskCtx, ctx: Context*, pixmap_src: PixmapRef, pixmap: SubPixmapMut*):
var p = LowpPipeline(
index = 0,
functions = functions,
pixmap_src = pixmap_src,
pixmap = pixmap,
mask_ctx = mask_ctx,
aa_mask_ctx = aa_mask_ctx,
ctx = ctx,
tail = 0,
dx = 0,
dy = 0,
)
for y in rect.y()..<rect.bottom():
var x = (usize)rect.x()
let end = (usize)rect.right()
p.functions = functions
while x + lowp_stage_width <= end:
p.index = 0
p.dx = x
p.dy = (usize)y
p.tail = lowp_stage_width
lowp_next_stage(&p)
x += lowp_stage_width
if x != end:
p.index = 0
p.functions = functions_tail
p.dx = x
p.dy = (usize)y
p.tail = end - x
lowp_next_stage(&p)
func lowp_just_return(_p: LowpPipeline*):
return # Ends the loop.
func lowp_null_fn(_p: LowpPipeline*):
return # Just for unsupported functions in STAGES.
# mark: Basic stages ==========================================================================
func lowp_move_source_to_destination(p: LowpPipeline*):
p.dr = p.r
p.dg = p.g
p.db = p.b
p.da = p.a
lowp_next_stage(p)
func lowp_move_destination_to_source(p: LowpPipeline*):
p.r = p.dr
p.g = p.dg
p.b = p.db
p.a = p.da
lowp_next_stage(p)
# No need to clamp against 0 here (values are unsigned).
func lowp_clamp_01(p: LowpPipeline*):
let max = u16[8](255)
for h: usize in 0..<2:
p.r[h] = u16x8_min(p.r[h], max)
p.g[h] = u16x8_min(p.g[h], max)
p.b[h] = u16x8_min(p.b[h], max)
p.a[h] = u16x8_min(p.a[h], max)
lowp_next_stage(p)
func lowp_clamp_gamut(p: LowpPipeline*):
for h: usize in 0..<2:
p.a[h] = u16x8_min(p.a[h], u16[8](255))
p.r[h] = u16x8_min(p.r[h], p.a[h])
p.g[h] = u16x8_min(p.g[h], p.a[h])
p.b[h] = u16x8_min(p.b[h], p.a[h])
lowp_next_stage(p)
func lowp_premultiply(p: LowpPipeline*):
for h: usize in 0..<2:
p.r[h] = div255(p.r[h] *% p.a[h])
p.g[h] = div255(p.g[h] *% p.a[h])
p.b[h] = div255(p.b[h] *% p.a[h])
lowp_next_stage(p)
func lowp_uniform_color(p: LowpPipeline*):
let rgba = p.ctx.uniform_color.rgba
p.r = u16_splat(rgba[0])
p.g = u16_splat(rgba[1])
p.b = u16_splat(rgba[2])
p.a = u16_splat(rgba[3])
lowp_next_stage(p)
func lowp_seed_shader(p: LowpPipeline*):
let iota: F16 = [[0.5, 1.5, 2.5, 3.5], [4.5, 5.5, 6.5, 7.5], [8.5, 9.5, 10.5, 11.5], [12.5, 13.5, 14.5, 15.5]]
let dx = (f32)p.dx
let x: F16 = [f32[4](dx) + iota[0], f32[4](dx) + iota[1], f32[4](dx) + iota[2], f32[4](dx) + iota[3]]
let y = f32[4]((f32)p.dy + 0.5)
lowp_split(x, &p.r, &p.g)
lowp_split([y, y, y, y], &p.b, &p.a)
lowp_next_stage(p)
# mark: Memory ================================================================================
func lowp_dst_pixels(p: LowpPipeline*) -> PremultipliedColorU8[]:
let offset = p.pixmap.offset(p.dx, p.dy)
return pixels_of(p.pixmap.data)[offset..]
func lowp_dst_mask(p: LowpPipeline*) -> u8[]:
let offset = p.pixmap.offset(p.dx, p.dy)
return p.pixmap.data[offset..]
func lowp_load_dst(p: LowpPipeline*):
lowp_load_8888(lowp_dst_pixels(p), &p.dr, &p.dg, &p.db, &p.da)
lowp_next_stage(p)
func lowp_load_dst_tail(p: LowpPipeline*):
lowp_load_8888_tail(p.tail, lowp_dst_pixels(p), &p.dr, &p.dg, &p.db, &p.da)
lowp_next_stage(p)
func lowp_store(p: LowpPipeline*):
lowp_store_8888(&p.r, &p.g, &p.b, &p.a, lowp_dst_pixels(p))
lowp_next_stage(p)
func lowp_store_tail(p: LowpPipeline*):
lowp_store_8888_tail(&p.r, &p.g, &p.b, &p.a, p.tail, lowp_dst_pixels(p))
lowp_next_stage(p)
func lowp_load_dst_u8(p: LowpPipeline*):
lowp_load_8(lowp_dst_mask(p), &p.da)
lowp_next_stage(p)
func lowp_load_dst_u8_tail(p: LowpPipeline*):
# Fill a dummy array with `tail` values. `tail` is always in a 1..STAGE_WIDTH-1 range.
let data = lowp_dst_mask(p)
var tmp: u8[16]
for i in 0..<p.tail:
tmp[i] = data[i]
lowp_load_8(tmp, &p.da)
lowp_next_stage(p)
# store_a8: min(a, 255), then the low byte.
func lowp_store_u8(p: LowpPipeline*):
let data = lowp_dst_mask(p)
if data.length < 16:
trap("index out of bounds")
let dst = (u8*)data.data
for i: usize in 0..<16:
dst[i] = (u8)u16_min(p.a[i >> 3][i & 7], 255)
lowp_next_stage(p)
func lowp_store_u8_tail(p: LowpPipeline*):
let data = lowp_dst_mask(p)
for i in 0..<p.tail:
data[i] = (u8)u16_min(p.a[i >> 3][i & 7], 255)
lowp_next_stage(p)
# Similar to mask_u8, but only loads the mask values without actually masking the pipeline.
func lowp_load_mask_u8(p: LowpPipeline*):
let offset = p.mask_ctx.offset(p.dx, p.dy)
var c = u16_splat(0)
for i in 0..<p.tail:
c[i >> 3][i & 7] = (u16)p.mask_ctx.data[offset + i]
p.r = u16_splat(0)
p.g = u16_splat(0)
p.b = u16_splat(0)
p.a = c
lowp_next_stage(p)
# tiny-skia's clip mask stage (Skia clips with a clip blitter instead).
func lowp_mask_u8(p: LowpPipeline*):
let offset = p.mask_ctx.offset(p.dx, p.dy)
var c = u16_splat(0)
for i in 0..<p.tail:
c[i >> 3][i & 7] = (u16)p.mask_ctx.data[offset + i]
let any = c[0] | c[1]
var all_zero = true
for i: usize in 0..<8:
if any[i] != 0:
all_zero = false
if all_zero:
return
for h: usize in 0..<2:
p.r[h] = div255(p.r[h] *% c[h])
p.g[h] = div255(p.g[h] *% c[h])
p.b[h] = div255(p.b[h] *% c[h])
p.a[h] = div255(p.a[h] *% c[h])
lowp_next_stage(p)
# mark: Coverage ==============================================================================
## The blitted mask's coverage of the stage's lanes (zero past `tail`).
func lowp_aa_coverage(p: LowpPipeline*) -> U16x16:
var bytes: u8[16]
p.aa_mask_ctx.load(p.dx, p.dy, p.tail, bytes)
var c: U16x16 = ---
for i: usize in 0..<16:
c[i >> 3][i & 7] = (u16)bytes[i]
return c
func lowp_scale_u8(p: LowpPipeline*):
let c = lowp_aa_coverage(p)
for h: usize in 0..<2:
p.r[h] = div255(p.r[h] *% c[h])
p.g[h] = div255(p.g[h] *% c[h])
p.b[h] = div255(p.b[h] *% c[h])
p.a[h] = div255(p.a[h] *% c[h])
lowp_next_stage(p)
func lowp_lerp_u8(p: LowpPipeline*):
let c = lowp_aa_coverage(p)
for h: usize in 0..<2:
p.r[h] = lowp_lerp(p.dr[h], p.r[h], c[h])
p.g[h] = lowp_lerp(p.dg[h], p.g[h], c[h])
p.b[h] = lowp_lerp(p.db[h], p.b[h], c[h])
p.a[h] = lowp_lerp(p.da[h], p.a[h], c[h])
lowp_next_stage(p)
func lowp_scale_by(p: LowpPipeline*, f: f32):
let c = lowp_from_float(f)
for h: usize in 0..<2:
p.r[h] = div255(p.r[h] *% c)
p.g[h] = div255(p.g[h] *% c)
p.b[h] = div255(p.b[h] *% c)
p.a[h] = div255(p.a[h] *% c)
lowp_next_stage(p)
func lowp_scale_1_float(p: LowpPipeline*):
lowp_scale_by(p, p.ctx.current_coverage)
func lowp_scale_paint_alpha(p: LowpPipeline*):
lowp_scale_by(p, p.ctx.paint_alpha)
func lowp_lerp_1_float(p: LowpPipeline*):
let c = lowp_from_float(p.ctx.current_coverage)
for h: usize in 0..<2:
p.r[h] = lowp_lerp(p.dr[h], p.r[h], c)
p.g[h] = lowp_lerp(p.dg[h], p.g[h], c)
p.b[h] = lowp_lerp(p.db[h], p.b[h], c)
p.a[h] = lowp_lerp(p.da[h], p.a[h], c)
lowp_next_stage(p)
## load_8888_dst, srcover and store_8888 fused (an optimization: the same math, one stage).
func lowp_source_over_rgba(p: LowpPipeline*):
let pixels = lowp_dst_pixels(p)
lowp_load_8888(pixels, &p.dr, &p.dg, &p.db, &p.da)
lowp_source_over_math(p)
lowp_store_8888(&p.r, &p.g, &p.b, &p.a, pixels)
lowp_next_stage(p)
func lowp_source_over_rgba_tail(p: LowpPipeline*):
let pixels = lowp_dst_pixels(p)
lowp_load_8888_tail(p.tail, pixels, &p.dr, &p.dg, &p.db, &p.da)
lowp_source_over_math(p)
lowp_store_8888_tail(&p.r, &p.g, &p.b, &p.a, p.tail, pixels)
lowp_next_stage(p)
# mark: Load and store ========================================================================
# The 16 pixels are checked to be in bounds once (Rust's `first_chunk_mut().unwrap()`), then
# read and written through a pointer.
func lowp_load_8888(data: const PremultipliedColorU8[], r: U16x16*, g: U16x16*, b: U16x16*, a: U16x16*):
if data.length < 16:
trap("index out of bounds")
let src = (const u8*)data.data
for h: usize in 0..<2:
var rr: U16x8 = ---
var gg: U16x8 = ---
var bb: U16x8 = ---
var aa: U16x8 = ---
for l: usize in 0..<8:
let q = (h * 8 + l) * 4
rr[l] = (u16)src[q]
gg[l] = (u16)src[q + 1]
bb[l] = (u16)src[q + 2]
aa[l] = (u16)src[q + 3]
(*r)[h] = rr
(*g)[h] = gg
(*b)[h] = bb
(*a)[h] = aa
func lowp_load_8888_tail(tail: usize, data: const PremultipliedColorU8[], r: U16x16*, g: U16x16*, b: U16x16*, a: U16x16*):
# Fill a dummy array with `tail` values. `tail` is always in a 1..STAGE_WIDTH-1 range.
var tmp: PremultipliedColorU8[16]
for i in 0..<tail:
tmp[i] = data[i]
lowp_load_8888(tmp, r, g, b, a)
# store_8888_: min(v, 255), then the low byte.
func lowp_store_8888(r: const U16x16*, g: const U16x16*, b: const U16x16*, a: const U16x16*, data: PremultipliedColorU8[]):
if data.length < 16:
trap("index out of bounds")
let dst = (u8*)data.data
let max = u16[8](255)
for h: usize in 0..<2:
let rr = u16x8_min((*r)[h], max)
let gg = u16x8_min((*g)[h], max)
let bb = u16x8_min((*b)[h], max)
let aa = u16x8_min((*a)[h], max)
for l: usize in 0..<8:
let q = (h * 8 + l) * 4
dst[q] = (u8)rr[l]
dst[q + 1] = (u8)gg[l]
dst[q + 2] = (u8)bb[l]
dst[q + 3] = (u8)aa[l]
func lowp_store_8888_tail(r: const U16x16*, g: const U16x16*, b: const U16x16*, a: const U16x16*, tail: usize, data: PremultipliedColorU8[]):
for i in 0..<tail:
let h = i >> 3
let l = i & 7
data[i] = PremultipliedColorU8(rgba = [(u8)u16_min((*r)[h][l], 255), (u8)u16_min((*g)[h][l], 255), (u8)u16_min((*b)[h][l], 255), (u8)u16_min((*a)[h][l], 255)])
func lowp_load_8(data: const u8[], a: U16x16*):
if data.length < 16:
trap("index out of bounds")
let src = (const u8*)data.data
for h: usize in 0..<2:
var aa: U16x8 = ---
for l: usize in 0..<8:
aa[l] = (u16)src[h * 8 + l]
(*a)[h] = aa
# mark: Helpers ===============================================================================
func u16_min(a: u16, b: u16) -> u16:
return a if a < b else b
## div255 of NEON, `vrshrq_n_u16(vrsraq_n_u16(v, v, 8), 8)`: (v + 128 + ((v + 128) >> 8)) >> 8,
## the correctly rounded v / 255 (Skia's div255_accurate is the same on NEON; x86 builds
## compute (v + 255) >> 8 instead). Computed in u16 lanes, which is exact for v <= 65152: every
## product of two u8 values, and every sum the stages form from premultiplied colors (at most
## 255 * 255). Past that, NEON's rounding shifts keep a 17th bit this does not.
func div255(v: U16x8) -> U16x8:
let q = v +% u16[8](128)
return (q +% (q >> 8)) >> 8
func lowp_inv(v: U16x8) -> U16x8:
return u16[8](255) -% v
func lowp_from_float(f: f32) -> U16x8:
return u16[8]((u16)(f * 255.0 + 0.5))
func lowp_lerp(from_: U16x8, to: U16x8, t: U16x8) -> U16x8:
return div255(from_ *% lowp_inv(t) +% to *% t)
union LaneBits:
u: U16x16
f: F8
# We're splitting f32x16 (512bit) into two u16x16 (256 bit).
func lowp_split(v: F16, lo: U16x16*, hi: U16x16*):
var bits: LaneBits
bits.f = [v[0], v[1]]
*lo = bits.u
bits.f = [v[2], v[3]]
*hi = bits.u
# We're joining two u16x16 (256 bit) into f32x16 (512bit).
func lowp_join(lo: U16x16, hi: U16x16) -> F16:
var a: LaneBits
a.u = lo
var b: LaneBits
b.u = hi
return [a.f[0], a.f[1], b.f[0], b.f[1]]
## lowp's mad: a + f * m, two roundings (lowp does not fuse).
func lowp_mad(f: F4, m: F4, a: F4) -> F4:
return a + f * m
## lowp's max(x, y): if_then_else(x < y, y, x).
func lowp_fmax(x: F4, y: F4) -> F4:
return [y[0] if x[0] < y[0] else x[0], y[1] if x[1] < y[1] else x[1], y[2] if x[2] < y[2] else x[2], y[3] if x[3] < y[3] else x[3]]
## lowp's min(x, y): if_then_else(x < y, x, y).
func lowp_fmin(x: F4, y: F4) -> F4:
return [x[0] if x[0] < y[0] else y[0], x[1] if x[1] < y[1] else y[1], x[2] if x[2] < y[2] else y[2], x[3] if x[3] < y[3] else y[3]]
## lowp's clamp_01_: min(max(0, v), 1).
func lowp_clamp_01_(v: F4) -> F4:
return lowp_fmin(lowp_fmax(f32[4](0.0), v), f32[4](1.0))