Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / webview / tests_webview_gpu.lucb

#==============================================================================================
#
#   tests_webview_gpu - the GPU player draws what the CPU player draws, before and after a scroll
#
#   DESCRIPTION:
#       A page loaded with the GPU player on: the frame it draws into an offscreen texture is
#       compared with the CPU player's frame of the same display list and scroll state, at the
#       top and after a wheel scroll (when the view's tiles are moved, not drawn again). A
#       machine without a GPU device skips it.
#
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## The GPU frame of `view` into `texture`, read back into `gpu_pixels`, and the CPU player's
## into `cpu_pixels`; the number of pixels further apart than 2 in a channel.
func test_webview_gpu_differences(view: WebView*, texture: gpu.Texture, gpu_pixels: u8[], cpu_pixels: u8[]) -> usize:
    with memory.heap:
        let frame = texture.frame() catch failure: trap(f"webview tests: {failure.message}")
        defer frame.release()
        let target = frame.value().target() catch failure: trap(f"webview tests: {failure.message}")
        defer target.release()
        webview_gpu_draw(view, target.value()) catch failure: trap(f"webview tests: {failure.message}")
        _ = frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0)) catch failure: trap(f"webview tests: {failure.message}")
        texture.read(gpu_pixels) catch failure: trap(f"webview tests: {failure.message}")
    webview_cpu_reference_frame(view, cpu_pixels, 800, 600)
    var differing: usize = 0
    var at: usize = 0
    while at < gpu_pixels.length:
        for channel in 0..<(usize)4:
            let d = (i32)gpu_pixels[at + channel] - (i32)cpu_pixels[at + channel]
            if d > 2 or d < -2:
                differing += 1
                break
        at += 4
    return differing

test "the GPU player draws the page as the CPU player does, before and after a scroll":
    test_webview_platform()
    var device = gpu.Device.open() catch failure: return
    defer device.destroy()
    var texture = gpu.Texture.create(device, 800, 600, gpu.Format.rgba8) catch failure: trap(f"webview tests: {failure.message}")
    defer texture.destroy()
    let gpu_pixels = (new u8[800 * 600 * 4] --- in memory.heap) else trap("out of memory")
    defer free(gpu_pixels) in memory.heap
    let cpu_pixels = (new u8[800 * 600 * 4] --- in memory.heap) else trap("out of memory")
    defer free(cpu_pixels) in memory.heap
    let allocator: memory.Allocator = webview_heap()
    with allocator:
        let view = test_webview_open()
        defer webview_destroy(view)
        var log = TestWebViewLog()
        webview_set_gpu_player(view, true)
        webview_load(view, test_webview_file_url("fragment.html"))
        test_webview_wait_for_load(view, &log, "fragment.html")
        test_webview_sync_rendering(view, &log)
        let top = test_webview_gpu_differences(view, texture, gpu_pixels, cpu_pixels)
        assert(top * 1000 <= 800 * 600, "the GPU player's frame is the CPU player's at the top")
        webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_wheel, 400.0, 300.0, 0, 0, 0, 0, 240, 0)))
        test_webview_sync_rendering(view, &log)
        let scrolled = test_webview_gpu_differences(view, texture, gpu_pixels, cpu_pixels)
        assert(scrolled * 1000 <= 800 * 600, "the GPU player's frame is the CPU player's after a scroll")

## The width of the view's back store (the top-level navigable's), 0 before one exists.
func test_webview_back_store_width(view: WebView*) -> i32:
    let stores = &webview_rendering_thread_data(view).m_backing_stores
    let store = stores.back_store else return 0
    return gfx.bitmap_width(gfx.painting_surface_bitmap(store))

test "with the GPU player the back stores are a pixel each, and full frames again without it":
    test_webview_platform()
    let allocator: memory.Allocator = webview_heap()
    with allocator:
        let view = test_webview_open()
        defer webview_destroy(view)
        var log = TestWebViewLog()
        webview_load(view, test_webview_file_url("fragment.html"))
        test_webview_wait_for_load(view, &log, "fragment.html")
        test_webview_sync_rendering(view, &log)
        assert(test_webview_back_store_width(view) >= 800, "the CPU player's back store is a frame")
        webview_set_gpu_player(view, true)
        let serial = webview_frame_serial(view)
        test_webview_sync_rendering(view, &log)
        assert(test_webview_back_store_width(view) == 1, "the GPU player's back store is a pixel")
        assert(webview_frame_serial(view) > serial, "the switch presents a frame")
        webview_set_gpu_player(view, false)
        test_webview_sync_rendering(view, &log)
        assert(test_webview_back_store_width(view) >= 800, "the CPU player's back store is a frame again")