dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / painting / tests_paint_recording.lucb
#==============================================================================================
#
# tests_paint_recording - regions r51b and r52: recording paintables into a display list and
# rasterizing it, pinned to the reference build
#
# DESCRIPTION:
# The cases are tests_paint_recording_cases (the local oracle
# luce-browser-tools/oracles/luce-browser-engine/paint_recording over the reference build):
#
# - values: DevicePixelConverter's roundings, DisplayListRecordingContext's conversions
# back to CSS pixels and its CSS viewport rect, and ChromeMetrics, at five scales;
# - display lists: Document::dump_display_list of small documents (backgrounds, borders
# of every style, border radii, box and text shadows, gradients, outlines, background
# clips, text, opacity layers; regions r49+r53: SVG shapes, strokes, viewBoxes, list
# markers, fieldsets with legends), after the real Document::update_layout: the commands
# the painters record must be Ladybird's exactly;
# - pixels: the same documents rendered as Ladybird screenshots them (the display list a
# screenshot records, played by DisplayListPlayerCpu into an 800x600 bitmap) against
# the pixels Ladybird's DisplayListPlayerSkia renders (identical to its headless
# screenshots), under Ladybird's fuzzy rule: the maximum channel difference and the
# number of differing pixels each case allows (test_paint_recording_tolerance). Glyphs
# are FreeType's masks in both and match; the differences left are anti-aliasing (the
# box shadow blur, gradients, a dashed stroke's corner).
#
#==============================================================================================
# mark: Helpers ===================================================================================
## `appendff("{}", value)` of a double.
func test_paint_recording_put_f64(builder: ak.StringBuilder*, value: f64):
ak.format_f64(builder, value)
## Gfx::IntRect's formatter: "[x,y wxh]".
func test_paint_recording_put_device_rect(builder: ak.StringBuilder*, rect: gfx.DevicePixelRect):
dump_appendff(builder, "[{},{} {}x{}]", [ak.arg_i64((i64)rect.m_location.m_x), ak.arg_i64((i64)rect.m_location.m_y), ak.arg_i64((i64)rect.m_size.m_width), ak.arg_i64((i64)rect.m_size.m_height)])
## Gfx::IntPoint's formatter: "[x,y]".
func test_paint_recording_put_point(builder: ak.StringBuilder*, point: gfx.IntPoint):
dump_appendff(builder, "[{},{}]", [ak.arg_i64((i64)point.m_x), ak.arg_i64((i64)point.m_y)])
## Gfx::IntSize's formatter: "[wxh]".
func test_paint_recording_put_size(builder: ak.StringBuilder*, size: gfx.IntSize):
dump_appendff(builder, "[{}x{}]", [ak.arg_i64((i64)size.m_width), ak.arg_i64((i64)size.m_height)])
## A CSSPixelRect of raw (1/64 px) values.
func test_paint_recording_raw_rect(x: i32, y: i32, width: i32, height: i32) -> gfx.CssPixelRect:
return gfx.CssPixelRect(m_location = gfx.CssPixelPoint(m_x = gfx.CssPixels.from_raw(x), m_y = gfx.CssPixels.from_raw(y)),
m_size = gfx.CssPixelSize(m_width = gfx.CssPixels.from_raw(width), m_height = gfx.CssPixels.from_raw(height)))
## Whether the right-trimmed `text`, each line indented by two spaces, is `expected`.
func test_paint_recording_matches(text: ak.StringView, expected: str) -> bool:
var out = ak.string_builder_new()
test_layout_tree_put_lines(&out, text)
let actual = ak.string_builder_to_string_without_validation(&out)
let actual_view = ak.string_bytes_as_string_view(&actual)
if ak.string_view_equals(&actual_view, expected):
return true
print(f"expected:\n{expected}actual:\n{ak.string_view_text(actual_view)}")
return false
# mark: The oracle's values =======================================================================
## The oracle's `values` mode.
func test_paint_recording_values(heap: gc.Heap*) -> ak.StringBuilder:
var b = ak.string_builder_new()
let scales: f64[5] = [1.0, 1.5, 2.0, 0.75, 1.25]
let values: gfx.CssPixels[9] = [gfx.CssPixels.from_int(0), gfx.CssPixels.from_int(1), gfx.CssPixels.from_raw(33), gfx.CssPixels.from_raw(-33), gfx.CssPixels.from_raw(96),
gfx.CssPixels.from_raw(-96), gfx.CssPixels.from_raw(1023), gfx.CssPixels.from_int(-7), gfx.css_pixels_nearest_value_for_f64(10.3)]
let rects: gfx.CssPixelRect[3] = [test_paint_recording_raw_rect(33, 65, 640, 97), test_paint_recording_raw_rect(-3 * 64, -161, 1000, 12 * 64), test_paint_recording_raw_rect(8 * 64, 8 * 64, 784 * 64, 18 * 64)]
for scale in scales:
var converter: PaintDevicePixelConverter = ---
paint_device_pixel_converter_construct(&converter, scale)
ak.string_builder_append(&b, "scale ")
test_paint_recording_put_f64(&b, scale)
ak.string_builder_append(&b, "\n")
for v in values:
dump_appendff(&b, " px {} round {} enclose {} floor {}\n", [ak.arg_i64((i64)v.raw_value()), ak.arg_i64((i64)paint_device_pixel_converter_rounded_device_pixels(&converter, v)),
ak.arg_i64((i64)paint_device_pixel_converter_enclosing_device_pixels(&converter, v)), ak.arg_i64((i64)paint_device_pixel_converter_floored_device_pixels(&converter, v))])
for r in rects:
ak.string_builder_append(&b, " rect ")
dump_append_css_pixel_rect(&b, r)
ak.string_builder_append(&b, " rounded ")
test_paint_recording_put_device_rect(&b, paint_device_pixel_converter_rounded_device_rect(&converter, r))
ak.string_builder_append(&b, " enclosing ")
test_paint_recording_put_device_rect(&b, paint_device_pixel_converter_enclosing_device_rect(&converter, r))
ak.string_builder_append(&b, " point ")
test_paint_recording_put_point(&b, paint_device_pixel_converter_rounded_device_point(&converter, r.m_location))
ak.string_builder_append(&b, " floored_point ")
test_paint_recording_put_point(&b, paint_device_pixel_converter_floored_device_point(&converter, r.m_location))
ak.string_builder_append(&b, " size ")
test_paint_recording_put_size(&b, paint_device_pixel_converter_rounded_device_size(&converter, r.m_size))
ak.string_builder_append(&b, " enclosing_size ")
test_paint_recording_put_size(&b, paint_device_pixel_converter_enclosing_device_size(&converter, r.m_size))
ak.string_builder_append(&b, "\n")
let list = display_list.display_list_create(display_list.accumulated_visual_context_tree_create())
var recorder = display_list.display_list_recorder_new(list)
var context = display_list_recording_context_new(&recorder, test_palette(), scale, chrome_metrics_new(scale))
var viewport: gfx.DevicePixelRect = ---
gfx.device_pixel_rect_construct_i32(&viewport, 3, 5, 801, 599)
display_list_recording_context_set_device_viewport_rect(&context, viewport)
ak.string_builder_append(&b, " css_viewport_rect ")
dump_append_css_pixel_rect(&b, display_list_recording_context_css_viewport_rect(&context))
ak.string_builder_append(&b, "\n")
let devices: i32[6] = [0, 1, 3, -5, 17, 799]
for d in devices:
dump_appendff(&b, " device {} css {}\n", [ak.arg_i64((i64)d), ak.arg_i64((i64)display_list_recording_context_scale_to_css_pixels(&context, d).raw_value())])
var device_rect: gfx.DevicePixelRect = ---
gfx.device_pixel_rect_construct_i32(&device_rect, 3, 7, 11, 13)
ak.string_builder_append(&b, " css_rect ")
dump_append_css_pixel_rect(&b, display_list_recording_context_scale_to_css_rect(&context, device_rect))
ak.string_builder_append(&b, "\n")
let metrics = chrome_metrics_new(scale)
dump_appendff(&b, " metrics {} {} {} {} {} {} {}\n", [ak.arg_i64((i64)metrics.scroll_thumb_min_length.raw_value()), ak.arg_i64((i64)metrics.scroll_thumb_padding_thin.raw_value()),
ak.arg_i64((i64)metrics.scroll_thumb_thickness_thin.raw_value()), ak.arg_i64((i64)metrics.scroll_thumb_thickness.raw_value()),
ak.arg_i64((i64)metrics.scroll_gutter_thickness.raw_value()), ak.arg_i64((i64)metrics.resize_gripper_size.raw_value()),
ak.arg_i64((i64)metrics.resize_gripper_padding.raw_value())])
_ = heap
return b
# mark: Rendering a document ======================================================================
## The screenshot of `document` (TraversableNavigable::take_screenshot of the viewport): the display list recorded
## with the overlay and the canvas fill of the 800x600 rect, played into a BGRA8888 bitmap by DisplayListPlayerCpu.
func test_paint_recording_render(document: DomDocument*) -> gfx.Bitmap*:
dom_document_update_layout(document, DomUpdateLayoutReason.process_screenshot)
var rect: gfx.DevicePixelRect = ---
gfx.device_pixel_rect_construct_i32(&rect, 0, 0, 800, 600)
let recorded = dom_document_record_display_list(document, HtmlPaintConfig(paint_overlay = true, canvas_fill_rect = rect)) else trap("no display list")
let viewport = dom_document_paintable(document) else trap("no viewport paintable")
paint_viewport_paintable_refresh_scroll_state(viewport)
let snapshot = paint_viewport_paintable_scroll_state_snapshot(viewport)
let bitmap = gfx.bitmap_create_int_size(gfx.BitmapFormat.bgra8888, gfx.IntSize(m_width = 800, m_height = 600)) else trap("no bitmap")
let surface = gfx.painting_surface_wrap_bitmap(bitmap)
let player = display_list.display_list_player_cpu_create()
display_list.display_list_player_execute((display_list.DisplayListPlayer*)player, recorded, &snapshot, surface)
return bitmap
## The fuzzy comparison of a rendered page with the expected crop: (the maximum channel difference, the number of
## differing pixels, the number of pixels outside the crop that are not opaque white).
func test_paint_recording_compare(bitmap: gfx.Bitmap*, test_case: const TestPaintRecordingCase*) -> (i32, i32, i32):
var max_difference: i32 = 0
var differing: i32 = 0
var index: i32 = 0
let bytes = test_case.expected.bytes
var at: usize = 0
while at < bytes.length:
# A token: COUNT "x" WORD (eight hex digits), then a space.
var count: i32 = 0
while bytes[at] != 'x':
count = count * 10 + (i32)(bytes[at] - '0')
at += 1
at += 1
var word: u32 = 0
while at < bytes.length and bytes[at] != ' ':
let c = bytes[at]
word = word * 16 + (u32)(c - '0' if c <= '9' else c - 'a' + 10)
at += 1
at += 1
for i in 0..<count:
_ = i
let actual = bitmap_scanline_word(bitmap, index % test_case.width, index // test_case.width)
var channel_difference: i32 = 0
for shift in [0, 8, 16, 24]:
let a = (i32)((actual >> (u32)shift) & 0xff)
let e = (i32)((word >> (u32)shift) & 0xff)
channel_difference = ak.max(channel_difference, a - e if a > e else e - a)
if channel_difference > 0:
differing += 1
max_difference = ak.max(max_difference, channel_difference)
index += 1
assert(index == test_case.width * test_case.height)
var outside: i32 = 0
for y in 0..<600:
for x in 0..<800:
if (x >= test_case.width or y >= test_case.height) and bitmap_scanline_word(bitmap, (i32)x, (i32)y) != 0xffffffff:
outside += 1
return (max_difference, differing, outside)
## The pixel (0xAARRGGBB) at x, y of a BGRA8888 bitmap.
func bitmap_scanline_word(bitmap: gfx.Bitmap*, x: i32, y: i32) -> u32:
let row = gfx.bitmap_scanline(bitmap, y) else trap("no scanline")
return row[x]
## Ladybird's fuzzy rule per case: the maximum channel difference and the number of differing pixels allowed.
func test_paint_recording_tolerance(name: str) -> (i32, i32):
# Measured on macOS arm64 (with a little room for x86-64's float rounding): glyphs are FreeType's masks at Skia's
# quarter-pixel phases in both (render's INTEGRATION.md, Glyph masks) and match exactly; off macOS a mask pre-blend
# table entry may be a level away (powf). The box shadow blur is three box blurs where Skia uses SkMaskBlurFilter
# (2944 pixels within 3 levels); gradients differ by 1 level on 14 pixels.
# Regions r49+r53: the markers' and the legends' text is glyphs (exact, as the text cases); the dashed SVG stroke's
# first dash starts at the rectangle's corner, where Skia's dasher leaves 3 pixels of the miter white that the CPU
# player's (tiny-skia's) paints (render follow-up); the SVG fills are exact.
# Region r56: SVG patterns, masks, clip paths, use, filters and text (paths) are exact; the SVG gradients (linear
# and focal radial, pad/reflect/repeat, a half-transparent stop) differ by at most 2 levels on 3107 pixels, the
# CPU player's gradient evaluation against Skia's (render follow-up); the tolerance leaves room for x86-64.
match name:
"text", "text-shadow", "markers", "fieldset": return (1, 40)
"shadows": return (4, 3200)
"gradients": return (2, 40)
"svg-gradients": return (3, 3600)
"svg-strokes": return (255, 8)
_: return (0, 0)
# mark: Tests =====================================================================================
## Runs `body` for each case with a VM, the test fonts, the light color scheme and the user agent sheets.
test "DevicePixelConverter, DisplayListRecordingContext and ChromeMetrics match the reference build":
web_initialize_strings()
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
_ = realm
let heap = vm_heap(vm)
let allocator: memory.Allocator = heap
with allocator:
let b = test_paint_recording_values(heap)
assert(test_paint_recording_matches(ak.string_builder_string_view(&b), test_paint_recording_values_expected))
test "MouseAction's flag operators, BoxModelMetrics and the mix-blend-mode operators":
let both = paint_bit_or(PaintMouseAction.capture_input, PaintMouseAction.swallow_event)
assert(paint_has_flag(both, PaintMouseAction.capture_input) and paint_has_flag(both, PaintMouseAction.swallow_event))
assert(not paint_has_any_flag(PaintMouseAction.capture_input, PaintMouseAction.swallow_event))
var action = PaintMouseAction.none_
_ = paint_or_assign(&action, PaintMouseAction.swallow_event)
assert(action == PaintMouseAction.swallow_event)
_ = paint_and_assign(&action, paint_bit_not(PaintMouseAction.swallow_event))
assert(action == PaintMouseAction.none_)
assert(paint_bit_xor(both, PaintMouseAction.capture_input) == PaintMouseAction.swallow_event)
let px = gfx.CssPixels.from_int
let metrics = PaintBoxModelMetrics(margin = PaintPixelBox(top = px(1), right = px(2), bottom = px(3), left = px(4)),
padding = PaintPixelBox(top = px(10), right = px(20), bottom = px(30), left = px(40)),
border = PaintPixelBox(top = px(100), right = px(200), bottom = px(300), left = px(400)),
inset = PaintPixelBox(top = px(0), right = px(0), bottom = px(0), left = px(0)))
let margin_box = paint_box_model_metrics_margin_box(&metrics)
assert(margin_box.top == px(111) and margin_box.right == px(222) and margin_box.bottom == px(333) and margin_box.left == px(444))
let border_box = paint_box_model_metrics_border_box(&metrics)
assert(border_box.top == px(110) and border_box.left == px(440))
let padding_box = paint_box_model_metrics_padding_box(&metrics)
assert(padding_box.bottom == px(30))
assert(paint_mix_blend_mode_to_compositing_and_blending_operator(css_data.MixBlendMode.normal) == gfx.CompositingAndBlendingOperator.normal)
assert(paint_mix_blend_mode_to_compositing_and_blending_operator(css_data.MixBlendMode.color_dodge) == gfx.CompositingAndBlendingOperator.color_dodge)
assert(paint_mix_blend_mode_to_compositing_and_blending_operator(css_data.MixBlendMode.plus_lighter) == gfx.CompositingAndBlendingOperator.plus_lighter)
test "painting small documents records the reference build's display lists":
web_initialize_strings()
test_fonts_setup()
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
var (realm, realm_root) = test_layout_tree_window_realm(vm)
defer gc.root_release(&realm_root)
let heap = vm_heap(vm)
var passed: usize = 0
var failed: usize = 0
let allocator: memory.Allocator = heap
with allocator:
test_page_client_color_scheme = CssPreferredColorScheme.light
defer test_page_client_color_scheme = CssPreferredColorScheme.auto
test_media_invalidation_install_user_agent_sheets(realm)
defer test_style_computer_release_user_agent_sheets()
for test_case in &test_paint_recording_display_list_cases:
var (document, navigable) = test_layout_tree_parse_and_style(heap, realm, test_case.html, test_case.css)
let dump = dom_document_dump_display_list(document)
let dump_view = ak.string_bytes_as_string_view(&dump)
if test_paint_recording_matches(ak.string_view_trim_whitespace(&dump_view, ak.TrimMode.right), test_case.expected):
passed += 1
else:
failed += 1
print(f"display list mismatch: {test_case.name}")
gc.root_release(&navigable)
gc.heap_collect_garbage(heap)
print(f"paint recording display lists: {passed} passed, {failed} failed")
assert(failed == 0)
assert(passed > 0)
test "rendering small documents with the CPU player matches Ladybird's screenshots":
web_initialize_strings()
test_fonts_setup()
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
var (realm, realm_root) = test_layout_tree_window_realm(vm)
defer gc.root_release(&realm_root)
let heap = vm_heap(vm)
var passed: usize = 0
var failed: usize = 0
let allocator: memory.Allocator = heap
with allocator:
test_page_client_color_scheme = CssPreferredColorScheme.light
defer test_page_client_color_scheme = CssPreferredColorScheme.auto
test_media_invalidation_install_user_agent_sheets(realm)
defer test_style_computer_release_user_agent_sheets()
for test_case in &test_paint_recording_pixel_cases:
var (document, navigable) = test_layout_tree_parse_and_style(heap, realm, test_case.html, test_case.css)
let bitmap = test_paint_recording_render(document)
let (max_difference, differing, outside) = test_paint_recording_compare(bitmap, test_case)
let (allowed_difference, allowed_pixels) = test_paint_recording_tolerance(test_case.name)
print(f"pixels {test_case.name}: {differing} differing pixels, maximum channel difference {max_difference}, {outside} painted outside")
if max_difference <= allowed_difference and differing <= allowed_pixels and outside == 0:
passed += 1
else:
failed += 1
gc.root_release(&navigable)
gc.heap_collect_garbage(heap)
print(f"paint recording pixels: {passed} passed, {failed} failed")
assert(failed == 0)
assert(passed > 0)