dymokomi/luce-browser-render
Source of release 0.6.2
0.6.2 / src / gpu_player / recorder_draws.lucb
#==============================================================================================
#
# recorder_draws - DisplayListPlayerGpu's glyph runs, paths and images
#
# DESCRIPTION:
# Glyph runs place each glyph as the CPU player does (display_list's cpu_glyph_run: the
# same strike, rounding spec and quarter-pixel field, so the same mask lands on the same
# pixel) and draw its mask from the atlas, rastered the first time the atlas meets the
# glyph. A path filled with a color is rastered to coverage by the CPU rasterizer (the
# pixels the CPU player's fill would cover, limited to the tile), placed in the atlas
# for this tile, and drawn as a mask: Ganesh's software path renderer does the same for
# paths it has no GPU method for. Images are textures kept per bitmap (images.lucb),
# sampled nearest or bilinear in the shader. Shadows are in recorder_shadows.lucb.
#
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Masks ===================================================================================
## A `mask` draw of the atlas texels of `entry` at `dst` (whole pixels) in `color`; a
## nine-patch entry stretches over `dst` (see tile.frag's kind 2). The atlas's texture now:
## making room may have replaced it.
func gpu_mask_draw(this: DisplayListPlayerGpu*, entry: GpuAtlasEntry, dst: GpuRect, color: f32[4]):
if not dst.intersects(this.state.clip):
return
let texture = this.atlas.texture else return
var draw = gpu_shader_draw(this, GpuDrawKind.mask, gpu_kind_mask, dst)
draw.image = texture
draw.data[0] = (f32)dst.x
draw.data[1] = (f32)dst.y
draw.data[2] = (f32)entry.x
draw.data[3] = (f32)entry.y
for i in 0..<(usize)4:
draw.data[4 + i] = color[i]
if let nine = entry.nine:
draw.data[8] = (f32)nine.center_x
draw.data[9] = (f32)nine.center_y
draw.data[10] = (f32)(dst.width + 1 - entry.width)
draw.data[11] = (f32)(dst.height + 1 - entry.height) * (1.0 if nine.fill_center else -1.0)
gpu_append(this, draw)
# mark: Glyph runs ==============================================================================
func gpu_draw_glyph_run_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawGlyphRun):
let this = gpu_player_of(base)
if not gpu_recording(this):
return
let blob = web_fonts.glyph_run_cached_skia_text_blob(command.glyph_run) else return
if command.orientation != gfx.Orientation.horizontal:
gpu_give_up(this, GpuFallback.text)
return
# cpu_canvas_draw_text_blob: the canvas's matrix pre-translated by the run's origin; the
# strike is made with its 2x2 part (glyphs rotated or scaled by it).
let position = display_list.cpu_pre_translate(gpu_ctm(this), command.translation.m_x, command.translation.m_y)
if display_list.cpu_should_draw_as_path(&blob.font, position):
gpu_give_up(this, GpuFallback.text)
return
if (command.color.m_value >> 24) == 0:
return
let r = gfx.color_red(&command.color)
let g = gfx.color_green(&command.color)
let b = gfx.color_blue(&command.color)
let m22 = web_fonts.SkMatrix22(scale_x = position.sx, skew_x = position.kx, skew_y = position.ky, scale_y = position.sy)
let strike = web_fonts.sk_strike_make(&blob.font, m22, r, g, b) else return
let spec = display_list.cpu_rounding_spec(&strike)
let device = this.device else return
# What makes the strike: the typeface, the size, subpixel positioning, the color's gamma
# and the matrix's 2x2 part.
var strike_key = gpu_hash(gpu_hash_start, (u64)(void*)(blob.font.typeface else return))
strike_key = gpu_hash(strike_key, ((u64)(i64)(blob.font.size * 65536.0) << 1) | (1 if blob.font.subpixel else 0))
strike_key = gpu_hash(strike_key, ((u64)r << 16) | ((u64)g << 8) | (u64)b)
if not (position.sx == 1.0 and position.sy == 1.0 and position.kx == 0.0 and position.ky == 0.0):
strike_key = gpu_hash(gpu_hash(strike_key, ((u64)position.sx.bits() << 32) | (u64)position.kx.bits()), ((u64)position.ky.bits() << 32) | (u64)position.sy.bits())
let color = gpu_premultiplied(command.color)
# The mapping to the device with the rounding constant added, so flooring places a glyph.
var rounding = position
rounding.tx = position.tx + spec.half_x
rounding.ty = position.ty + spec.half_y
for i in 0..<ak.vector_size(&blob.glyphs):
let glyph = *ak.vector_at_const(&blob.glyphs, i)
let origin = *ak.vector_at_const(&blob.positions, i)
if not (math32.is_finite(origin.m_x) and math32.is_finite(origin.m_y)):
continue
let (mapped_x, mapped_y) = display_list.cpu_map_point(rounding, origin.m_x, origin.m_y)
let sub_x = display_list.cpu_subpixel_field(mapped_x, spec.keep_x)
let sub_y = display_list.cpu_subpixel_field(mapped_y, spec.keep_y)
let key = gpu_hash(gpu_hash(strike_key, (u64)glyph), ((u64)sub_x << 2) | (u64)sub_y)
let entry = gpu_glyph_entry(this, device, &strike, key, glyph, sub_x, sub_y) else continue
if not gpu_recording(this):
return
let dst = GpuRect(x = display_list.cpu_floor_to_int(mapped_x) + entry.left, y = display_list.cpu_floor_to_int(mapped_y) + entry.top, width = entry.width, height = entry.height)
gpu_mask_draw(this, entry, dst, color)
## The atlas entry of a glyph, rastered and placed the first time; none for an empty glyph.
func gpu_glyph_entry(this: DisplayListPlayerGpu*, device: gpu.Device, strike: const web_fonts.SkStrike*, key: u64, glyph: u32, sub_x: u32, sub_y: u32) -> GpuAtlasEntry?:
if let entry = this.atlas.find(key):
return entry if entry.width > 0 else none
var mask = web_fonts.sk_strike_glyph_mask(strike, glyph, sub_x, sub_y)
defer mask.release()
if mask.is_empty():
this.atlas.remember_empty(key)
return none
return gpu_place(this, device, key, mask.image, mask.width, mask.height, mask.left, mask.top)
# mark: Paths ===================================================================================
func gpu_fill_path_impl(base: display_list.DisplayListPlayer*, command: display_list.FillPath):
let this = gpu_player_of(base)
if not gpu_recording(this):
return
var color = gfx.Color()
match command.paint_style_or_color:
.color(value): color = value
.svg_paint_server_paint_style(paint_style):
gpu_painted_path(this, &command, paint_style)
return
if (color.m_value >> 24) == 0:
return
if gpu_under_matrix(this):
var under = display_list.to_raster_path(&command.path) else return
defer under.path.release()
let fill = GpuCoverageShape.fill(path = &under.path, fill_rule = display_list.cpu_fill_rule(command.winding_rule), anti_alias = command.should_anti_alias == display_list.ShouldAntiAlias.yes)
gpu_coverage_draw(this, fill, under.path.bounds().transform(gpu_ctm(this)), color)
return
# The path's bounds, one pixel wider for anti-aliasing, in its own coordinates.
let local = gpu_rect_of(command.path_bounding_rect)
let bounds = GpuRect(x = local.x - 1, y = local.y - 1, width = local.width + 2, height = local.height + 2)
if not bounds.translated(this.state.dx, this.state.dy).intersects(this.state.clip):
return
let device = this.device else return
let anti_alias = command.should_anti_alias == display_list.ShouldAntiAlias.yes
# A small path is rastered whole, once: its coverage is the same wherever a whole-pixel
# translation puts it, so every tile it crosses shares it. A larger one (a box's border)
# is rastered for the part this tile shows, so the atlas is not filled with its interior.
var area = bounds
var key: u64? = none
if bounds.width * bounds.height <= 512 * 512 and bounds.width <= 2048 and bounds.height <= 2048:
var hash = gpu_hash(gpu_hash_start, (u64)(void*)(command.path.m_impl else return))
hash = gpu_hash(hash, ((u64)(u32)bounds.x << 32) | (u64)(u32)bounds.y)
hash = gpu_hash(hash, ((u64)(u32)bounds.width << 32) | (u64)(u32)bounds.height)
key = gpu_hash(hash, ((u64)command.winding_rule << 1) | (1 if anti_alias else 0))
else:
area = bounds.translated(this.state.dx, this.state.dy).intersected(gpu_grown_clip(this)).translated(-this.state.dx, -this.state.dy)
var entry = GpuAtlasEntry()
if let k = key:
if let found = this.atlas.find(k):
entry = found
if entry.width == 0:
let coverage = gpu_path_coverage(&command, area, anti_alias) else return
defer free(coverage) in memory.heap
let placed = gpu_place(this, device, key, coverage, area.width, area.height, 0, 0)
if placed == none:
gpu_give_up(this, GpuFallback.path)
return
entry = placed else return
if not gpu_recording(this):
return
gpu_mask_draw(this, entry, area.translated(this.state.dx, this.state.dy), gpu_premultiplied(color))
## The coverage of `command`'s path over `area` (its own coordinates), one byte a pixel: the
## alpha of a white fill on a transparent pixmap, which is the CPU player's draw coverage
## exactly (a clip mask's scan conversion differs). Lives in memory.heap; none for no path.
func gpu_path_coverage(command: const display_list.FillPath*, area: GpuRect, anti_alias: bool) -> u8[]?:
var path = display_list.to_raster_path(&command.path) else return none
defer path.path.release()
var pixmap = gpu_coverage_pixmap(area) else return none
defer pixmap.release()
var paint = gpu_white_paint(anti_alias)
pixmap.fill_path(&path.path, &paint, display_list.cpu_fill_rule(command.winding_rule), raster.Transform.from_translate((f32)-area.x, (f32)-area.y), none)
return gpu_alpha_of(&pixmap)
## A transparent pixmap the size of `area`.
func gpu_coverage_pixmap(area: GpuRect) -> raster.Pixmap?:
return raster.Pixmap.new_((u32)area.width, (u32)area.height)
## Opaque white, anti-aliased or not: what coverage is drawn with.
func gpu_white_paint(anti_alias: bool) -> raster.Paint:
var paint = raster.Paint()
paint.set_color(raster.Color.from_rgba8(255, 255, 255, 255))
paint.anti_alias = anti_alias
return paint
## The alpha channel of `pixmap`, one byte a pixel (in memory.heap).
func gpu_alpha_of(pixmap: const raster.Pixmap*) -> u8[]:
let coverage = (new u8[(usize)pixmap.width() * (usize)pixmap.height()] --- in memory.heap) else trap("gpu_player: out of memory")
let pixels = pixmap.pixels()
for index in 0..<coverage.length:
coverage[index] = pixels[index].alpha()
return coverage
## fill_path with a paint server: its coverage under the matrix, painted with the gradient's
## stages (a pattern paint server gives the tile up).
func gpu_painted_path(this: DisplayListPlayerGpu*, command: const display_list.FillPath*, paint_style: display_list.SvgPaintServerPaintStyle*?):
let style = paint_style else trap("VERIFY: a null paint style")
let anti_alias = command.should_anti_alias == display_list.ShouldAntiAlias.yes
let bounding = display_list.int_rect_to_float_rect(display_list.fill_path_bounding_rect(command))
let made = display_list.cpu_gradient_style_paint(style, bounding, anti_alias, command.opacity)
if made == none:
gpu_give_up(this, GpuFallback.path)
return
var paint = made else return
defer paint.release()
var under = display_list.to_raster_path(&command.path) else return
defer under.path.release()
let fill = GpuCoverageShape.fill(path = &under.path, fill_rule = display_list.cpu_fill_rule(command.winding_rule), anti_alias = anti_alias)
gpu_coverage_draw(this, fill, under.path.bounds().transform(gpu_ctm(this)), gfx.Color(), &paint)
func gpu_stroke_path_impl(base: display_list.DisplayListPlayer*, command: display_list.StrokePath):
# DisplayListPlayerCpu::stroke_path: the path stroked (width, caps, joins, miter, dashes)
# under the matrix, as the CPU rasterizer's coverage, in a color or a gradient paint
# server's stages.
let this = gpu_player_of(base)
if not gpu_recording(this):
return
var under = display_list.to_raster_path(&command.path) else return
defer under.path.release()
var stroke = display_list.cpu_stroke_of(&command)
defer display_list.cpu_stroke_release(&stroke)
let anti_alias = command.should_anti_alias == display_list.ShouldAntiAlias.yes
# How far a stroke reaches past the path: half its width, times the miter limit at joins.
let reach = command.thickness * math32.max(command.miter_limit, 2.0) * 0.5 + 1.0
let bounds = gpu_mapped_bounds(this, under.path.bounds(), reach)
let shape = GpuCoverageShape.stroke(path = &under.path, stroke = &stroke, anti_alias = anti_alias)
match command.paint_style_or_color:
.color(color):
gpu_coverage_draw(this, shape, bounds, color)
.svg_paint_server_paint_style(paint_style):
let style = paint_style else trap("VERIFY: a null paint style")
let bounding = display_list.int_rect_to_float_rect(display_list.stroke_path_bounding_rect(&command))
let made = display_list.cpu_gradient_style_paint(style, bounding, anti_alias, command.opacity)
if made == none:
gpu_give_up(this, GpuFallback.stroke)
return
var paint = made else return
defer paint.release()
gpu_coverage_draw(this, shape, bounds, gfx.Color(), &paint)
# mark: Lines ===================================================================================
func gpu_draw_line_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawLine):
# DisplayListPlayerCpu::draw_line's stroke (cpu_line_of) as coverage: a short line once per
# shape (whole-pixel endpoints make it the same wherever it is), a long one for the part
# this tile shows.
let this = gpu_player_of(base)
if not gpu_recording(this):
return
if (command.color.m_value >> 24) == 0:
return
if gpu_under_matrix(this):
var under = display_list.cpu_line_of(&command) else return
defer under.path.release()
defer display_list.cpu_stroke_release(&under.stroke)
let bounds = under.path.bounds()
gpu_coverage_draw(this, .stroke(path = &under.path, stroke = &under.stroke, anti_alias = true), gpu_mapped_bounds(this, bounds, (f32)command.thickness + 1.0), command.color)
return
let local = gpu_rect_of(display_list.draw_line_bounding_rect(&command))
let bounds = GpuRect(x = local.x - 1, y = local.y - 1, width = local.width + 2, height = local.height + 2)
if not bounds.translated(this.state.dx, this.state.dy).intersects(this.state.clip):
return
let device = this.device else return
var area = bounds
var key: u64? = none
if bounds.width * bounds.height <= 512 * 512 and bounds.width <= 2048 and bounds.height <= 2048:
var hash = gpu_hash(gpu_hash_start, ((u64)(u32)(command.to.m_x - command.from_.m_x) << 32) | (u64)(u32)(command.to.m_y - command.from_.m_y))
hash = gpu_hash(hash, ((u64)(u32)(command.from_.m_x - bounds.x) << 32) | (u64)(u32)(command.from_.m_y - bounds.y))
key = gpu_hash(hash, ((u64)(u32)command.thickness << 8) | (u64)command.style | 0x10000000000)
else:
area = bounds.translated(this.state.dx, this.state.dy).intersected(gpu_grown_clip(this)).translated(-this.state.dx, -this.state.dy)
var entry = GpuAtlasEntry()
if let k = key:
if let found = this.atlas.find(k):
entry = found
if entry.width == 0:
var line = display_list.cpu_line_of(&command) else return
defer line.path.release()
defer display_list.cpu_stroke_release(&line.stroke)
var pixmap = gpu_coverage_pixmap(area) else return
defer pixmap.release()
var paint = gpu_white_paint(true)
pixmap.stroke_path(&line.path, &paint, &line.stroke, raster.Transform.from_translate((f32)-area.x, (f32)-area.y), none)
let coverage = gpu_alpha_of(&pixmap)
defer free(coverage) in memory.heap
entry = gpu_place(this, device, key, coverage, area.width, area.height, 0, 0) else return
if not gpu_recording(this):
return
gpu_mask_draw(this, entry, area.translated(this.state.dx, this.state.dy), gpu_premultiplied(command.color))
# mark: Images ==================================================================================
func gpu_scaled_bitmap_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawScaledImmutableBitmap):
let this = gpu_player_of(base)
if not gpu_recording(this):
return
if gpu_under_matrix(this):
gpu_give_up(this, GpuFallback.transform)
return
let bitmap = gfx.immutable_bitmap_bitmap(command.bitmap) else return
let width = gfx.bitmap_width(bitmap)
let height = gfx.bitmap_height(bitmap)
let dst = gpu_rect_of(command.dst_rect).translated(this.state.dx, this.state.dy)
let clip = gpu_rect_of(command.clip_rect).translated(this.state.dx, this.state.dy).intersected(this.state.clip)
let area = dst.intersected(clip)
if area.is_empty() or width <= 0 or height <= 0:
return
# cpu_image_shader's sampling: mipmaps only matter when the image is made smaller, and
# then the texture holds Skia's levels and the shader blends the two the pattern picks.
var filter: f32 = 0.0
match command.scaling_mode:
.bilinear: filter = 1.0
.bilinear_mipmap:
filter = 2.0 if dst.width < width or dst.height < height else 1.0
_: filter = 0.0
var levels: f32[8] = [0.0, 0.0, (f32)width, (f32)height, 0.0, 0.0, (f32)width, (f32)height]
if filter > 1.5:
let image_to_device = raster.Transform.from_row((f32)dst.width / (f32)width, 0.0, 0.0, (f32)dst.height / (f32)height, (f32)dst.x, (f32)dst.y)
let inverse = image_to_device.invert() else return
let choice = raster.mipmap_choice(inverse, raster.mipmap_levels_below((u32)width, (u32)height))
let upper = raster.mipmap_level_rect((u32)width, (u32)height, choice.upper)
let lower = raster.mipmap_level_rect((u32)width, (u32)height, choice.lower else choice.upper)
levels = [(f32)upper[0], (f32)upper[1], (f32)upper[2], (f32)upper[3], (f32)lower[0], (f32)lower[1], (f32)lower[2], (f32)lower[3]]
filter = 2.0 + (choice.weight if choice.lower != none else 0.0)
if gfx.immutable_bitmap_color_xform(command.bitmap) != none:
gpu_give_up(this, GpuFallback.color_managed_image)
return
let device = this.device else return
let found = this.images.texture_of(device, bitmap, filter > 1.5) catch failure:
gpu_give_up(this, GpuFallback.image)
return
let texture = found else return
var draw = gpu_shader_draw(this, GpuDrawKind.image, gpu_kind_image, area)
draw.scissor = clip
draw.image = texture
let k = gpu_uniform_header
draw.uniforms[k] = (f32)dst.x
draw.uniforms[k + 1] = (f32)dst.y
draw.uniforms[k + 2] = (f32)dst.width
draw.uniforms[k + 3] = (f32)dst.height
draw.uniforms[k + 4] = (f32)width
draw.uniforms[k + 5] = (f32)height
draw.uniforms[k + 6] = filter
draw.uniforms[k + 7] = 1.0
if filter > 1.5:
for i in 0..<(usize)8:
draw.uniforms[k + 8 + i] = levels[i]
gpu_append(this, draw)
func gpu_repeated_bitmap_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawRepeatedImmutableBitmap):
# DisplayListPlayerCpu::draw_repeated_immutable_bitmap: an image shader repeating on both
# axes, filled over the whole clip along a repeating axis and over the image's own span
# along the other.
let this = gpu_player_of(base)
if not gpu_recording(this):
return
if gpu_under_matrix(this):
gpu_give_up(this, GpuFallback.transform)
return
let bitmap = gfx.immutable_bitmap_bitmap(command.bitmap) else return
let width = gfx.bitmap_width(bitmap)
let height = gfx.bitmap_height(bitmap)
let dst = gpu_rect_of(command.dst_rect).translated(this.state.dx, this.state.dy)
if dst.width <= 0 or dst.height <= 0 or width <= 0 or height <= 0:
return
if gfx.immutable_bitmap_color_xform(command.bitmap) != none:
gpu_give_up(this, GpuFallback.color_managed_image)
return
var filter: f32 = 0.0
match command.scaling_mode:
.bilinear: filter = 1.0
.bilinear_mipmap:
# Mipmaps of a repeating image are not drawn on the GPU yet.
if dst.width < width or dst.height < height:
gpu_give_up(this, GpuFallback.repeated_image)
return
filter = 1.0
_: filter = 0.0
let clip = this.state.clip
var area = dst
if command.repeat.x:
area.x = clip.x
area.width = clip.width
if command.repeat.y:
area.y = clip.y
area.height = clip.height
area = area.intersected(clip)
if area.is_empty():
return
let device = this.device else return
let found = this.images.texture_of(device, bitmap, false) catch failure:
gpu_give_up(this, GpuFallback.image)
return
let texture = found else return
var draw = gpu_shader_draw(this, GpuDrawKind.image, gpu_kind_image, area)
draw.image = texture
let k = gpu_uniform_header
draw.uniforms[k] = (f32)dst.x
draw.uniforms[k + 1] = (f32)dst.y
draw.uniforms[k + 2] = (f32)dst.width
draw.uniforms[k + 3] = (f32)dst.height
draw.uniforms[k + 4] = (f32)width
draw.uniforms[k + 5] = (f32)height
draw.uniforms[k + 6] = filter
draw.uniforms[k + 7] = 1.0
draw.uniforms[k + 15] = 1.0
gpu_append(this, draw)