Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.2

0.6.2 / src / gpu_player / recorder_state.lucb

#==============================================================================================
#
#   recorder_state - DisplayListPlayerGpu's virtuals: state, clips, rectangles, the vtable
#
#   DESCRIPTION:
#       The virtuals that change the drawing state (save, restore, translations, clips,
#       effects and transforms that change nothing), the rectangle commands, the commands the
#       GPU player gives to the CPU player, and the player's vtable and class. Glyph runs,
#       paths and images are in recorder_draws.lucb.
#
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: State ===================================================================================

func gpu_save_impl(base: display_list.DisplayListPlayer*, arg0: display_list.Save):
    let this = gpu_player_of(base)
    ak.vector_append_t(&this.saved, this.state)

func gpu_save_layer_impl(base: display_list.DisplayListPlayer*, arg0: display_list.SaveLayer):
    # An isolated group: what blends inside it blends with the group, not what lies below.
    gpu_save_impl(base, display_list.Save())
    gpu_begin_layer(gpu_player_of(base), 1.0, (u32)raster.BlendMode.source_over, GpuFallback.blend)

func gpu_restore_impl(base: display_list.DisplayListPlayer*, arg0: display_list.Restore):
    let this = gpu_player_of(base)
    if ak.vector_is_empty(&this.saved):
        return
    let previous = ak.vector_take_last(&this.saved)
    # The restore of the save that opened a layer ends it.
    if this.state.layer != previous.layer and this.state.layer != 0 and gpu_recording(this):
        gpu_end_layer(this)
    this.state = previous

func gpu_translate_impl(base: display_list.DisplayListPlayer*, command: display_list.Translate):
    let this = gpu_player_of(base)
    if let matrix = this.state.matrix:
        gpu_set_matrix(this, display_list.cpu_pre_translate(matrix, (f32)command.delta.m_x, (f32)command.delta.m_y))
        return
    this.state.dx += command.delta.m_x
    this.state.dy += command.delta.m_y

func gpu_add_clip_rect_impl(base: display_list.DisplayListPlayer*, command: display_list.AddClipRect):
    let this = gpu_player_of(base)
    if command.rect.m_size.m_width < 0 or command.rect.m_size.m_height < 0:
        return
    if gpu_under_matrix(this):
        _ = gpu_matrix_clip_rect(this, command.rect)
        return
    this.state.clip = this.state.clip.intersected(gpu_rect_of(command.rect).translated(this.state.dx, this.state.dy))

func gpu_add_rounded_rect_clip_impl(base: display_list.DisplayListPlayer*, command: display_list.AddRoundedRectClip):
    let this = gpu_player_of(base)
    let keep_outside = command.corner_clip == display_list.CornerClip.inside
    if gpu_under_matrix(this):
        gpu_matrix_rounded_clip(this, command.border_rect, &command.corner_radii, keep_outside)
        return
    let rect = gpu_rect_of(command.border_rect).translated(this.state.dx, this.state.dy)
    if rect.width < 0 or rect.height < 0:
        return
    if not keep_outside:
        this.state.clip = this.state.clip.intersected(rect)
    # A clip that no longer reaches the scissor changes nothing drawn there.
    if this.state.clip.is_empty():
        return
    # A clip inside another: the outer one joins the clip mask.
    if not gpu_push_mask(this):
        return
    let radii = gpu_normalized_radii(rect, &command.corner_radii)
    this.state.rounded = GpuRoundedClip(rect = [(f32)rect.x, (f32)rect.y, (f32)rect.width, (f32)rect.height], radii = radii, mode = 2.0 if keep_outside else 1.0)

func gpu_add_clip_path_impl(base: display_list.DisplayListPlayer*, path: gfx.Path):
    gpu_give_up(gpu_player_of(base), GpuFallback.clip_path)

func gpu_apply_transform_impl(base: display_list.DisplayListPlayer*, origin: gfx.FloatPoint, matrix: gfx.FloatMatrix4X4):
    # DisplayListPlayerCpu::apply_transform: the matrix about `origin`, concatenated (the loop
    # saved). The 2D part draws; perspective gives the tile up.
    let this = gpu_player_of(base)
    var about = gfx.translation_matrix(gfx.FloatVector3([origin.m_x, origin.m_y, 0.0]))
    about = gfx.float_matrix4_x4_mul(&about, matrix)
    about = gfx.float_matrix4_x4_mul(&about, gfx.translation_matrix(gfx.FloatVector3([-origin.m_x, -origin.m_y, 0.0])))
    let m = about.m_elements
    # Only a 2D matrix: a z row or column (perspective, a 3D rotation) changes how later
    # transforms compose, which the CPU player's 4x4 keeps.
    let flat = m[0][2] == 0.0 and m[1][2] == 0.0 and m[2][0] == 0.0 and m[2][1] == 0.0 and m[2][2] == 1.0 and m[2][3] == 0.0 and m[3][2] == 0.0
    if not flat or m[3][0] != 0.0 or m[3][1] != 0.0 or m[3][3] != 1.0:
        gpu_give_up(this, GpuFallback.perspective)
        return
    let local = raster.Transform.from_row(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3])
    gpu_set_matrix(this, gpu_ctm(this).pre_concat(local))

func gpu_apply_effects_impl(base: display_list.DisplayListPlayer*, command: display_list.ApplyEffects):
    # DisplayListPlayerCpu::apply_effects: a layer with the effects (the loop restores it); an
    # effect that changes nothing is a save.
    let this = gpu_player_of(base)
    gpu_save_impl(base, display_list.Save())
    let operator = command.compositing_and_blending_operator
    if command.opacity < 1.0 or operator != gfx.CompositingAndBlendingOperator.normal:
        var blend = (u32)display_list.cpu_blend_mode(operator)
        if operator == gfx.CompositingAndBlendingOperator.plus_darker:
            blend = 29
        gpu_begin_layer(this, command.opacity, blend, GpuFallback.opacity if command.opacity < 1.0 else GpuFallback.blend)
    if command.filter != none:
        gpu_give_up(this, GpuFallback.filter)
    if command.mask_kind != none:
        gpu_give_up(this, GpuFallback.mask)

func gpu_would_be_fully_clipped_impl(base: display_list.DisplayListPlayer*, rect: gfx.DevicePixelRect) -> bool:
    let this = gpu_player_of(base)
    if gpu_under_matrix(this):
        return gpu_matrix_fully_clipped(this, rect)
    return not gpu_rect_of(rect).translated(this.state.dx, this.state.dy).intersects(this.state.clip)

func gpu_flush_impl(base: display_list.DisplayListPlayer*):
    _ = base

# mark: Rectangles ==============================================================================

func gpu_fill_rect_impl(base: display_list.DisplayListPlayer*, command: display_list.FillRect):
    let this = gpu_player_of(base)
    if not gpu_recording(this):
        return
    if gpu_under_matrix(this):
        gpu_matrix_fill_rect(this, command.rect, command.color)
        return
    let rect = gpu_rect_of(command.rect).translated(this.state.dx, this.state.dy)
    if rect.is_empty() or not rect.intersects(this.state.clip):
        return
    if this.state.rounded != none:
        gpu_fill_shape(this, rect, gpu_no_radii(), command.color, 0.0, false)
        return
    gpu_solid_rect(this, rect, command.color)

func gpu_fill_rect_with_rounded_corners_impl(base: display_list.DisplayListPlayer*, command: display_list.FillRectWithRoundedCorners):
    let this = gpu_player_of(base)
    if not gpu_recording(this):
        return
    if gpu_under_matrix(this):
        gpu_matrix_fill_rrect(this, command.rect, &command.corner_radii, command.color)
        return
    let rect = gpu_rect_of(command.rect).translated(this.state.dx, this.state.dy)
    if rect.is_empty():
        return
    gpu_fill_shape(this, rect, gpu_normalized_radii(rect, &command.corner_radii), command.color, 0.0, false)

func gpu_paint_scrollbar_impl(base: display_list.DisplayListPlayer*, command: display_list.PaintScrollBar):
    # DisplayListPlayerCpu::paint_scrollbar: the gutter, then the thumb (a pill), both filled
    # without anti-aliasing, then the thumb's edge stroked one pixel wide in a lighter color.
    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 gutter = gpu_rect_of(command.gutter_rect).translated(this.state.dx, this.state.dy)
    if not gutter.is_empty():
        gpu_fill_shape(this, gutter, gpu_no_radii(), command.track_color, 0.0, true)
    let thumb = gpu_rect_of(command.thumb_rect).translated(this.state.dx, this.state.dy)
    if thumb.width < 0 or thumb.height < 0:
        return
    var color = command.thumb_color
    if gfx.device_pixel_rect_is_empty(&command.gutter_rect) and color == display_list.initial_values_scrollbar_color().thumb_color:
        color = gfx.color_with_alpha(&color, 128)
    let radii = gpu_normalized_uniform_radii(thumb, (f32)thumb.width / 2.0)
    gpu_fill_shape(this, thumb, radii, color, 0.0, true)
    gpu_fill_shape(this, thumb, radii, gfx.color_lightened(&color), 1.0, false)

func gpu_draw_rect_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawRect):
    # SkCanvas::drawRect with a one-pixel stroke: a frame half a pixel each side of the edge.
    let this = gpu_player_of(base)
    if not gpu_recording(this):
        return
    if gpu_under_matrix(this):
        gpu_matrix_draw_rect(this, command.rect, command.color)
        return
    let rect = gpu_rect_of(command.rect).translated(this.state.dx, this.state.dy)
    if rect.width < 0 or rect.height < 0:
        return
    gpu_fill_shape(this, rect, gpu_no_radii(), command.color, 1.0, false)

# mark: Commands for the CPU player ==============================================================

func gpu_text_shadow_impl(base: display_list.DisplayListPlayer*, command: display_list.PaintTextShadow):
    gpu_give_up(gpu_player_of(base), GpuFallback.text_shadow)

func gpu_draw_ellipse_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawEllipse):
    gpu_ellipse(gpu_player_of(base), command.rect, command.color, command.thickness)

func gpu_fill_ellipse_impl(base: display_list.DisplayListPlayer*, command: display_list.FillEllipse):
    gpu_ellipse(gpu_player_of(base), command.rect, command.color, none)

func gpu_backdrop_filter_impl(base: display_list.DisplayListPlayer*, command: display_list.ApplyBackdropFilter):
    gpu_give_up(gpu_player_of(base), GpuFallback.backdrop_filter)

func gpu_nested_display_list_impl(base: display_list.DisplayListPlayer*, command: display_list.PaintNestedDisplayList):
    # DisplayListPlayerCpu::paint_nested_display_list: translated to the rectangle, the nested
    # list (an iframe's) played by this player with no scroll offsets.
    let this = gpu_player_of(base)
    if not gpu_recording(this):
        return
    gpu_translate_impl(base, display_list.Translate(delta = command.rect.m_location))
    var empty_scroll_state: display_list.ScrollStateSnapshot
    let nested = command.display_list else trap("VERIFY: a null nested display list")
    display_list.display_list_player_execute_nested(base, nested, &empty_scroll_state)

func gpu_external_content_impl(base: display_list.DisplayListPlayer*, command: display_list.DrawExternalContent):
    gpu_give_up(gpu_player_of(base), GpuFallback.external_content)

# mark: Class ===================================================================================

## The vtable of Web::Painting::DisplayListPlayerGpu.
pub let display_list_player_gpu_vtable: display_list.DisplayListPlayerVTable = display_list.DisplayListPlayerVTable(
    class_info = &display_list_player_gpu_class, flush = gpu_flush_impl, draw_glyph_run = gpu_draw_glyph_run_impl, fill_rect = gpu_fill_rect_impl,
    draw_scaled_immutable_bitmap = gpu_scaled_bitmap_impl, draw_repeated_immutable_bitmap = gpu_repeated_bitmap_impl, draw_external_content = gpu_external_content_impl,
    save = gpu_save_impl, save_layer = gpu_save_layer_impl, restore = gpu_restore_impl, translate = gpu_translate_impl, add_clip_rect = gpu_add_clip_rect_impl,
    paint_linear_gradient = gpu_linear_gradient_impl, paint_radial_gradient = gpu_radial_gradient_impl, paint_conic_gradient = gpu_conic_gradient_impl,
    paint_outer_box_shadow = gpu_outer_box_shadow_impl, paint_inner_box_shadow = gpu_inner_box_shadow_impl, paint_text_shadow = gpu_text_shadow_impl,
    fill_rect_with_rounded_corners = gpu_fill_rect_with_rounded_corners_impl, fill_path = gpu_fill_path_impl, stroke_path = gpu_stroke_path_impl,
    draw_ellipse = gpu_draw_ellipse_impl, fill_ellipse = gpu_fill_ellipse_impl, draw_line = gpu_draw_line_impl, apply_backdrop_filter = gpu_backdrop_filter_impl,
    draw_rect = gpu_draw_rect_impl, add_rounded_rect_clip = gpu_add_rounded_rect_clip_impl, paint_nested_display_list = gpu_nested_display_list_impl,
    paint_scrollbar = gpu_paint_scrollbar_impl, apply_effects = gpu_apply_effects_impl, apply_transform = gpu_apply_transform_impl,
    would_be_fully_clipped_by_painter = gpu_would_be_fully_clipped_impl, add_clip_path = gpu_add_clip_path_impl)

## Class information of Web::Painting::DisplayListPlayerGpu (class id 2).
pub let display_list_player_gpu_class: ak.ClassInfo = ak.ClassInfo(name = "DisplayListPlayerGpu", first_id = 2, last_id = 2, size = memory.size_of(DisplayListPlayerGpu), vtable = (const void*)&display_list_player_gpu_vtable)