dymokomi/luce-browser-render
Source of release 0.6.1
0.6.1 / src / display_list / cpu_gradient.lucb
#==============================================================================================
#
# cpu_gradient - the gradient shaders the Skia player makes, on the raster module
#
# DESCRIPTION:
# New code for DisplayListPlayerCpu (DESIGN.md ยง7.3): the SkGradientShader calls of
# DisplayListPlayerSkia (MakeLinear, MakeRadial, MakeTwoPointConical and MakeSweep with
# an SkGradientShader::Interpolation, a local matrix and a tile mode) as the raster
# module's gradient shaders, which compute what Skia m144's raster pipeline computes:
# SkGradientBaseShader's stops, the interpolation color space and hue method
# (SkColor4fXformer, AppendInterpolatedToDstStages), the shapes' t stages. The paint
# around them is the player's: its alpha (SkPaint::setAlphaf), dither (setDither) and
# SVG's SkColorFilters::LinearToSRGBGamma.
#
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Types ==================================================================================
## Four unpremultiplied color components (r, g, b, a), as SkColor4f.
type CpuColor4 = f32[4]
## A color stop: its position and its unpremultiplied sRGB color.
struct CpuGradientStop:
var position: f32
var color: CpuColor4
## SkTileMode (the player never asks for decal).
enum CpuTileMode:
clamp
repeat
mirror
## The gradient's geometry in its own coordinate space.
enum CpuGradientGeometry:
## SkGradientShader::MakeLinear: t runs from (x0, y0) to (x1, y1).
linear(x0: f32, y0: f32, x1: f32, y1: f32)
## SkGradientShader::MakeRadial: t is the distance from (cx, cy) over `radius`.
radial(cx: f32, cy: f32, radius: f32)
## SkGradientShader::MakeTwoPointConical.
two_point_conical(x0: f32, y0: f32, r0: f32, x1: f32, y1: f32, r1: f32)
## SkGradientShader::MakeSweep from 0 to 360 degrees around (cx, cy).
sweep(cx: f32, cy: f32)
## A gradient shader as the player asks Skia for it.
struct CpuGradient:
var geometry: CpuGradientGeometry
var stops: ak.Vector[CpuGradientStop]
var tile_mode: CpuTileMode
## SkGradientShader::Interpolation (CSS gradients: premultiplied, in their color space).
var interpolation: raster.GradientInterpolation
## The shader's local matrix: gradient space to the canvas's user space.
var local_matrix: raster.Transform
## Apply SkColorFilters::LinearToSRGBGamma to the result (SVG's linearRGB).
var linear_to_srgb: bool
# mark: Shaders ================================================================================
## The raster spread mode of a tile mode.
func cpu_spread_mode(mode: CpuTileMode) -> raster.SpreadMode:
match mode:
.clamp: return raster.SpreadMode.pad_
.repeat: return raster.SpreadMode.repeat
.mirror: return raster.SpreadMode.reflect
## The raster shader of `gradient`; none when Skia makes none (no stops, a negative radius, a
## matrix that cannot be inverted). Degenerate gradients are solid colors, as
## SkGradientBaseShader::MakeDegenerateGradient makes them.
func cpu_gradient_shader(gradient: const CpuGradient*) -> raster.Shader?:
let list = ak.vector_span_const(&gradient.stops)
if list.length == 0:
return none
var stops: ak.Vector[raster.GradientStop]
for stop in list:
let c = stop.color
let color = raster.Color.from_rgba(c[0], c[1], c[2], c[3]) else return none
ak.vector_append_t(&stops, raster.GradientStop.new_(stop.position, color))
let view = ak.vector_span_const(&stops)
let mode = cpu_spread_mode(gradient.tile_mode)
var shader: raster.Shader? = none
match gradient.geometry:
.linear(x0, y0, x1, y1):
shader = raster.LinearGradient.new_(raster.Point.from_xy(x0, y0), raster.Point.from_xy(x1, y1), view, mode, gradient.local_matrix)
.radial(cx, cy, radius):
shader = raster.RadialGradient.new_simple(raster.Point.from_xy(cx, cy), radius, view, mode, gradient.local_matrix)
.two_point_conical(x0, y0, r0, x1, y1, r1):
shader = raster.RadialGradient.new_(raster.Point.from_xy(x0, y0), r0, raster.Point.from_xy(x1, y1), r1, view, mode, gradient.local_matrix)
.sweep(cx, cy):
shader = raster.SweepGradient.new_(raster.Point.from_xy(cx, cy), 0.0, 360.0, view, mode, gradient.local_matrix)
var result = shader else return none
result.set_interpolation(gradient.interpolation)
return result
# mark: Filling ================================================================================
## Fills `path` (in the current transform) with `gradient` at `opacity` (SkPaint::setAlphaf),
## dithered when `dither` (SkPaint::setDither); in black when Skia makes no shader of it.
func cpu_canvas_fill_path_with_gradient(canvas: CpuCanvas*, path: const raster.Path*, fill_rule: raster.FillRule, anti_alias: bool, gradient: const CpuGradient*, opacity: f32, dither: bool = false):
if cpu_canvas_is_clip_empty(canvas):
return
var paint = cpu_gradient_paint(gradient, anti_alias, opacity, dither)
cpu_canvas_fill_path(canvas, path, &paint, fill_rule)
paint.release()
## The paint of a gradient at `opacity` (the caller releases it).
func cpu_gradient_paint(gradient: const CpuGradient*, anti_alias: bool, opacity: f32, dither: bool) -> raster.Paint:
var paint = raster.Paint()
paint.anti_alias = anti_alias
# Without a shader (Skia made none) the paint keeps its default color, opaque black.
if let shader = cpu_gradient_shader(gradient):
paint.shader = shader
paint.alpha = cpu_clamp_unit(opacity)
paint.dither = dither
paint.linear_to_srgb = gradient.linear_to_srgb
return paint
## A [0, 1] value as a byte, rounded.
func cpu_unit_to_u8(value: f32) -> u8:
return (u8)math32.round(cpu_clamp_unit(value) * 255.0)