Let web pages carry only what they draw
The Canvas host registered nothing on first use: every frame pumped Lanczos copies, the window reset meshes and copies, an evicted asset forgot its copies, and the host polled the test driver directly, so even a box carried the Lanczos cache, the resampler, page meshes and the test driver. Now the first smooth copy registers the pump, close and forget (canvas_window), the first warped picture registers its mesh batches, smooth copies are reachable only once a page asks a picture's size (as on the desktop), and test input comes through the frame driver. canvas_raster and page_mesh drop their unused image imports. Paint queues warped pictures through their own emitter, so no host links its mesh rasterizer for a program that warps none. Web profile (C files, C bytes, page bytes), before -> after: box 74, 990 402, 1 388 955 -> 70, 828 685, 1 346 817 button 85, 1 238 728, 1 598 206 -> 79, 969 324, 1 487 512 hello checked modules 84 -> 80; an empty window stays at 10. make pay-per-import-test checks the web window, hello, box, button and image next to the desktop cases. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
83c24cdba6
commit
9b551b591d
9 files changed
+187
-38
No files matched your search
@@ -20,6 +20,8 @@
|
||||
- Pictures, text input and scroll devices come only with the widgets that
|
||||
use them; a button without an icon carries no decoders or asset paths.
|
||||
Its file pictures come with `kryon/image`, its icons with `kryon/icon`.
|
||||
- Web pages carry smooth picture copies, warped-picture meshes and the test
|
||||
driver only when they use them.
|
||||
- Empty desktop windows omit image decoding, smooth scaling, video and threads.
|
||||
Import `kryon/cursor` for picture cursors in custom widget hosts. Native
|
||||
desktop project builds link X11 correctly.
|
||||
|
||||
@@ -205,6 +205,29 @@ buttons and the widget host fill the lookup where they draw, as they reached
|
||||
pictures before. Image's executable grew by 744 bytes (the lookup, its
|
||||
initializer and `image_files`) while its C shrank.
|
||||
|
||||
### The web
|
||||
|
||||
The web profile (`ziran tool kryon build --profile web`, C files and bytes
|
||||
and the page), and `ziran ir --target=c --target-os web --target-cpu wasm32`
|
||||
for the program entries (checked modules). Before is Kryon 389bf25e:
|
||||
|
||||
| Example | Before | After |
|
||||
| --- | --- | --- |
|
||||
| window (checked modules) | 10 | 10 |
|
||||
| hello (checked modules) | 84 | 80 |
|
||||
| box | 74 files, 990 402 bytes, 1 388 955-byte page | 70, 828 685, 1 346 817 |
|
||||
| button | 85, 1 238 728, 1 598 206 | 79, 969 324, 1 487 512 |
|
||||
| image | 78, 1 153 816, 1 508 177 | 79, 1 158 182, 1 514 171 |
|
||||
|
||||
An empty window on the web is the window, kryon/app, its types, the page
|
||||
window and the page (`page_core`, `page_geometry`) with four std modules.
|
||||
The Canvas host registers its Lanczos pump, close and forget with the first
|
||||
smooth copy, its mesh batches with the first warped picture, and smooth
|
||||
copies only once a page asks a picture's size, as the desktop does; it takes
|
||||
test-driver input through the frame driver. Paint queues warped pictures
|
||||
through their own emitter, so no host links its mesh rasterizer for a
|
||||
program that warps none.
|
||||
|
||||
### Modules outside an example's imports
|
||||
|
||||
All 88 frame examples carry these although their `.zi` imports do not name
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#import "canvas_events"
|
||||
#import "input_delivery"
|
||||
#import "input_take"
|
||||
#import "test_driver"
|
||||
#import "frame_driver"
|
||||
#import "canvas_frame"
|
||||
#import "page_core"
|
||||
#import "program_title"
|
||||
@@ -25,7 +25,6 @@
|
||||
#import "number_text"
|
||||
#import "byte_text_linux"
|
||||
Metrics :: #import "font_metrics";
|
||||
Lanczos :: #import "lanczos_cache";
|
||||
|
||||
// Bitmap cursors keep their CSS by request and density, so switching between
|
||||
// an application hand and a tool never re-encodes an image.
|
||||
@@ -378,7 +377,7 @@ PollCanvas :: () -> bool {
|
||||
}
|
||||
CanvasGamepadPoll()
|
||||
// A test driver types through the same ordered delivery.
|
||||
TestDriverInput(*canvas_input)
|
||||
if frame_test_input != null { frame_test_input(*canvas_input) }
|
||||
canvas_pointer.x = cast(float32)GetMouseX()
|
||||
canvas_pointer.y = cast(float32)GetMouseY()
|
||||
canvas_pointer.pressed = IsMouseButtonPressed(0)
|
||||
@@ -458,7 +457,7 @@ BeginCanvasFrame :: () {
|
||||
canvas_image_error = false
|
||||
// The page makes Lanczos copies a slice per frame; a finished one is
|
||||
// drawn on this frame.
|
||||
if Lanczos.LanczosPump() { PaintResourcesChanged() }
|
||||
if canvas_smooth_pump != null && canvas_smooth_pump() { PaintResourcesChanged() }
|
||||
CanvasFrameBegin(Color.{248, 250, 252, 255})
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,13 @@ Bitmaps :: #import "bitmap_store";
|
||||
Lanczos :: #import "lanczos_cache";
|
||||
#import "canvas_text"
|
||||
ImageSchema :: #import "image_props";
|
||||
Image :: #import "image";
|
||||
#import "page_font"
|
||||
#import "page_paint"
|
||||
#import "page_texture"
|
||||
#import "page_mesh"
|
||||
#import "raylib_clip"
|
||||
#import "page_core"
|
||||
#import "canvas_window"
|
||||
#import "web"
|
||||
|
||||
libm :: #system_library "m";
|
||||
@@ -191,6 +191,7 @@ CanvasLanczosRelease :: (argument: *void) {
|
||||
// filter.
|
||||
CanvasLanczosImage :: (index: s32, source: Rectangle, destination: Rectangle,
|
||||
clip: Rectangle, radius: float32, tint: Color) -> bool {
|
||||
CanvasUseSmooth()
|
||||
asset := *canvas_assets[index]
|
||||
texture := asset.texture
|
||||
slot := PageTextureFind(cast(s32)texture.id)
|
||||
@@ -246,6 +247,21 @@ CanvasLanczosImage :: (index: s32, source: Rectangle, destination: Rectangle,
|
||||
return true
|
||||
}
|
||||
|
||||
// The first smooth picture brings the Lanczos cache and its frame pump.
|
||||
CanvasUseSmooth :: () {
|
||||
canvas_smooth_pump = Lanczos.LanczosPump
|
||||
canvas_smooth_close = Lanczos.LanczosClose
|
||||
canvas_smooth_forget = Lanczos.LanczosForget
|
||||
}
|
||||
|
||||
// Smooth copies of file pictures arrive with the first picture a page asks
|
||||
// the size of: every widget that draws one lays it out from its size first
|
||||
// (see image_raster), so a page that draws only managed pixels, such as
|
||||
// vector icons, carries no Lanczos copies.
|
||||
CanvasPictureSmooth :: #type (index: s32, source: Rectangle, destination: Rectangle,
|
||||
clip: Rectangle, radius: float32, tint: Color) -> bool;
|
||||
canvas_picture_smooth: CanvasPictureSmooth;
|
||||
|
||||
// The browser decodes images itself and draws each when it is ready.
|
||||
#program_export
|
||||
ImagesPending :: () -> s32 {
|
||||
@@ -254,6 +270,7 @@ ImagesPending :: () -> s32 {
|
||||
|
||||
#program_export
|
||||
ImageWidth :: (asset_path: string) -> s32 {
|
||||
canvas_picture_smooth = CanvasLanczosImage
|
||||
index: s32 = CanvasAssetIndex(asset_path)
|
||||
if index < 0 { return 0 }
|
||||
return canvas_assets[index].texture.width
|
||||
@@ -261,6 +278,7 @@ ImageWidth :: (asset_path: string) -> s32 {
|
||||
|
||||
#program_export
|
||||
ImageHeight :: (asset_path: string) -> s32 {
|
||||
canvas_picture_smooth = CanvasLanczosImage
|
||||
index: s32 = CanvasAssetIndex(asset_path)
|
||||
if index < 0 { return 0 }
|
||||
return canvas_assets[index].texture.height
|
||||
@@ -309,7 +327,7 @@ RasterImage :: (asset_path: string, texture_id: u32,
|
||||
scope := WebScope()
|
||||
context := page_ctx
|
||||
if polygon.count >= 3 {
|
||||
PageMeshFlush()
|
||||
if canvas_mesh_flush != null { canvas_mesh_flush() }
|
||||
unused WebCall0(context, WebName("save"))
|
||||
unused WebCall0(context, WebName("beginPath"))
|
||||
unused WebCall2(context, WebName("moveTo"),
|
||||
@@ -323,8 +341,8 @@ RasterImage :: (asset_path: string, texture_id: u32,
|
||||
unused WebCall0(context, WebName("closePath"))
|
||||
unused WebCall0(context, WebName("clip"))
|
||||
}
|
||||
if asset < 0 || !CurrentPaintSmooth() || rotation != 0.0 ||
|
||||
!CanvasLanczosImage(asset, source, destination, clip, radius, tint) {
|
||||
if asset < 0 || !CurrentPaintSmooth() || rotation != 0.0 || canvas_picture_smooth == null ||
|
||||
!canvas_picture_smooth(asset, source, destination, clip, radius, tint) {
|
||||
CanvasDrawImageClipped(texture, source, destination, clip, origin,
|
||||
rotation, radius, tint)
|
||||
}
|
||||
@@ -368,6 +386,9 @@ RasterImageMesh :: (asset_path: string, texture_id: u32, source: Rectangle,
|
||||
if Bitmaps.BitmapTagged(texture_id) { SetTextureFilter(texture, ifx CurrentPaintSmooth() then 1 else 0) }
|
||||
id: s32 = cast(s32)texture.id
|
||||
filter: s32 = PageTextureFilterFor(id)
|
||||
// The first warped picture brings the page's mesh batches.
|
||||
canvas_mesh_reset = PageMeshReset
|
||||
canvas_mesh_flush = PageMeshFlush
|
||||
if PageMeshDraw(id, source, points[:count], columns, rows, clip, tint, filter) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#import "canvas_os"
|
||||
#import "text_typeface"
|
||||
#import "text_buffer"
|
||||
Lanczos :: #import "lanczos_cache";
|
||||
#import "canvas_window"
|
||||
#import "page_core"
|
||||
#import "paint_queue"
|
||||
// Text layout stays cached while these fonts stay loaded; see font_metrics.
|
||||
@@ -276,7 +276,7 @@ CanvasForgetAsset :: (index: s32) {
|
||||
// Queued page paint can still refer to the texture being released.
|
||||
PageFlushPaint()
|
||||
UnloadTexture(canvas_assets[index].texture)
|
||||
Lanczos.LanczosForget(canvas_assets[index].hash)
|
||||
if canvas_smooth_forget != null { canvas_smooth_forget(canvas_assets[index].hash) }
|
||||
PaintResourcesChanged()
|
||||
}
|
||||
canvas_assets[index] = CanvasAsset.{}
|
||||
|
||||
@@ -9,11 +9,27 @@
|
||||
#import "page_input"
|
||||
#import "page_paint"
|
||||
#import "page_texture"
|
||||
#import "page_mesh"
|
||||
#import "c_string"
|
||||
#import "geometry"
|
||||
#import "drawing_props"
|
||||
Lanczos :: #import "lanczos_cache";
|
||||
|
||||
// Smooth picture copies (see lanczos_cache) and warped pictures (page_mesh)
|
||||
// register here the first time canvas_raster draws one, so a page that never
|
||||
// draws one carries neither: the frame pumps the copies, the window resets
|
||||
// both, and an evicted picture forgets its copies.
|
||||
CanvasSmoothPump :: #type () -> bool;
|
||||
CanvasRelease :: #type () -> void;
|
||||
CanvasSmoothForget :: #type (content: u64) -> void;
|
||||
canvas_smooth_pump: CanvasSmoothPump;
|
||||
canvas_smooth_close: CanvasRelease;
|
||||
canvas_smooth_forget: CanvasSmoothForget;
|
||||
canvas_mesh_reset: CanvasRelease;
|
||||
canvas_mesh_flush: CanvasRelease;
|
||||
CanvasPicturesReset :: () {
|
||||
if canvas_mesh_reset != null { canvas_mesh_reset() }
|
||||
// Lanczos copies are page textures.
|
||||
if canvas_smooth_close != null { canvas_smooth_close() }
|
||||
}
|
||||
#if OS == .WEB {
|
||||
canvas_monitor_name: [32]u8;
|
||||
canvas_empty: [1]u8;
|
||||
@@ -37,10 +53,8 @@ Lanczos :: #import "lanczos_cache";
|
||||
canvas_width = width
|
||||
canvas_height = height
|
||||
canvas_external_frame_pacing = false
|
||||
PageMeshReset()
|
||||
CanvasBitmapClose()
|
||||
// Lanczos copies are page textures.
|
||||
Lanczos.LanczosClose()
|
||||
CanvasPicturesReset()
|
||||
PageTexturesReset()
|
||||
PageBoot(width, height, TextFromCString(title))
|
||||
PageEventsBind()
|
||||
@@ -52,10 +66,9 @@ Lanczos :: #import "lanczos_cache";
|
||||
|
||||
#program_export
|
||||
CloseWindow :: () {
|
||||
Lanczos.LanczosClose()
|
||||
CanvasPicturesReset()
|
||||
CanvasBitmapClose()
|
||||
CloseCanvasText()
|
||||
PageMeshReset()
|
||||
PageClose()
|
||||
PageTexturesReset()
|
||||
canvas_ready = false
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#import "geometry"
|
||||
#import "drawing_props"
|
||||
ImageSchema :: #import "image_props";
|
||||
Image :: #import "image";
|
||||
#import "page_core"
|
||||
#import "page_texture"
|
||||
#import "paint_queue"
|
||||
|
||||
+32
-19
@@ -669,25 +669,38 @@ EmitLabel :: (command: PaintCommand) {
|
||||
}
|
||||
}
|
||||
|
||||
// The texture, smoothing and clip polygon an image's raster call reads.
|
||||
EmitImageBegin :: (command: PaintCommand) -> bool {
|
||||
if command.clip.width <= 0.0 || command.clip.height <= 0.0 { return false }
|
||||
emitting_texture = command.texture
|
||||
emitting_smooth = command.smooth
|
||||
if command.image_clip_count > 0 { emitting_image_polygon = command.mesh_points }
|
||||
return true
|
||||
}
|
||||
|
||||
EmitImageEnd :: () {
|
||||
emitting_texture.id = cast(u32)0
|
||||
emitting_smooth = false
|
||||
emitting_image_polygon = emitting_image_polygon[:0]
|
||||
}
|
||||
|
||||
EmitImage :: (command: PaintCommand) {
|
||||
if command.clip.width > 0.0 && command.clip.height > 0.0 {
|
||||
emitting_texture = command.texture
|
||||
emitting_smooth = command.smooth
|
||||
if command.image_clip_count > 0 { emitting_image_polygon = command.mesh_points }
|
||||
if PaintMeshPointCount(command.mesh_columns, command.mesh_rows) > 0 {
|
||||
RasterImageMesh(command.asset_path, command.texture_id,
|
||||
command.source, command.mesh_points, command.mesh_columns,
|
||||
command.mesh_rows, command.clip, command.color)
|
||||
} else {
|
||||
RasterImage(command.asset_path, command.texture_id,
|
||||
command.source, command.bounds, command.clip,
|
||||
command.origin, command.rotation, command.radius,
|
||||
command.color)
|
||||
}
|
||||
emitting_texture.id = cast(u32)0
|
||||
emitting_smooth = false
|
||||
emitting_image_polygon = emitting_image_polygon[:0]
|
||||
}
|
||||
if !EmitImageBegin(command) { return }
|
||||
RasterImage(command.asset_path, command.texture_id,
|
||||
command.source, command.bounds, command.clip,
|
||||
command.origin, command.rotation, command.radius,
|
||||
command.color)
|
||||
EmitImageEnd()
|
||||
}
|
||||
|
||||
// A warped image has its own emitter, so a program that warps none carries
|
||||
// no mesh rasterizer.
|
||||
EmitImageMesh :: (command: PaintCommand) {
|
||||
if !EmitImageBegin(command) { return }
|
||||
RasterImageMesh(command.asset_path, command.texture_id,
|
||||
command.source, command.mesh_points, command.mesh_columns,
|
||||
command.mesh_rows, command.clip, command.color)
|
||||
EmitImageEnd()
|
||||
}
|
||||
|
||||
ClipLabel :: (command: PaintCommand, geometry: NodeGeometry) -> PaintCommand {
|
||||
@@ -1054,7 +1067,7 @@ PaintImageMesh :: (session: Session, node: s32, asset_path: string,
|
||||
command.data.mesh_columns = columns
|
||||
command.data.mesh_rows = rows
|
||||
command.data.mesh_points = points
|
||||
emit: PaintEmitter = EmitImage
|
||||
emit: PaintEmitter = EmitImageMesh
|
||||
command.emit = emit
|
||||
clip_for: PaintClipper = ClipImage
|
||||
command.clip_for = clip_for
|
||||
|
||||
@@ -36,6 +36,19 @@ not_in_a_window: [48]string = .["frames_desktop", "desktop_loop", "desktop_runti
|
||||
window_checked: [7]string = .["window", "app", "app_types", "desktop_window",
|
||||
"sdl_event", "pixmap_sdl", "std_c_string"];
|
||||
|
||||
// The checked program of an empty window on the web: the window, kryon/app,
|
||||
// its types, the page window and the page it opens.
|
||||
web_window_checked: [10]string = .["window", "app", "app_types", "page_window", "page_core",
|
||||
"page_geometry", "std_web", "std_text_buffer", "std_number_text", "std_byte_text_linux"];
|
||||
|
||||
// What a web program that warps or smooths no picture carries none of.
|
||||
not_in_a_plain_page: [5]string = .["image", "lanczos_cache", "std_resample", "page_mesh",
|
||||
"test_driver"];
|
||||
|
||||
// What a web frame example with no picture, text field or scroll carries none of.
|
||||
not_in_a_plain_web_frame: [8]string = .["image", "lanczos_cache", "std_resample", "page_mesh",
|
||||
"test_driver", "text_request", "text_input_platform", "scroll_device"];
|
||||
|
||||
// What no example that shows no picture or video carries.
|
||||
not_without_pictures: [10]string = .["image_decode_linux", "avif_decode", "lanczos_cache",
|
||||
"ziran_image_decode", "riff", "bmff", "video_linux", "std_thread_linux",
|
||||
@@ -140,6 +153,31 @@ CheckProgram :: (ziran: string, entry: string, source: string, output: string) -
|
||||
return Run(arguments[:], "")
|
||||
}
|
||||
|
||||
// The same checked program for the web page, as the web profile builds it.
|
||||
CheckWebProgram :: (ziran: string, entry: string, source: string, output: string) -> bool {
|
||||
arguments: [15]string = .[ziran, "ir", "--target=c", "--target-os", "web", "--target-cpu",
|
||||
"wasm32", "--project", "--root", "examples", "--entry", entry, "-o", output, source]
|
||||
return Run(arguments[:], "")
|
||||
}
|
||||
|
||||
// The checked program in DIRECTORY holds each of PRESENT and none of ABSENT.
|
||||
ExpectCheckedModules :: (example: string, directory: string, present: []string, absent: []string) {
|
||||
files: Vec(string)
|
||||
if !ListDirectory(directory, *files) {
|
||||
Fail(example, "has no checked program in", directory)
|
||||
return
|
||||
}
|
||||
for module: present {
|
||||
found := false
|
||||
for file: files { found = found || SavedFor(file, module) }
|
||||
if !found { Fail(example, "does not check", module) }
|
||||
}
|
||||
for module: absent {
|
||||
for file: files { if SavedFor(file, module) { Fail(example, "checks", module) } }
|
||||
}
|
||||
FreeDirectoryNames(*files)
|
||||
}
|
||||
|
||||
// FILE is MODULE's saved checked IR.
|
||||
SavedFor :: (file: string, module: string) -> bool {
|
||||
return EndsWith(file, ".zir") && file[:file.count - 4] == module
|
||||
@@ -172,6 +210,12 @@ BuildFrame :: (ziran: string, directory: string) -> bool {
|
||||
return Run(arguments[:], directory)
|
||||
}
|
||||
|
||||
// The same frame example as a web page, into .../generated/web/c.
|
||||
BuildWebFrame :: (ziran: string, directory: string) -> bool {
|
||||
arguments: [6]string = .[ziran, "tool", "kryon", "build", "--profile", "web"]
|
||||
return Run(arguments[:], directory)
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
ziran := EnvironmentText("ZIRAN_BIN")
|
||||
if ziran == "" { ziran = "ziran" }
|
||||
@@ -233,7 +277,42 @@ main :: () -> s32 {
|
||||
textfield_absent[:])
|
||||
}
|
||||
|
||||
// The web: an empty window is the window and its page, and frame
|
||||
// examples carry only what they import, as on Linux.
|
||||
if !CheckWebProgram(ziran, "window:main", "examples/window.zi", "build/test/pay-per-import/window-web-ir") {
|
||||
Fail("window (web)", "does not check", "examples/window.zi")
|
||||
} else {
|
||||
ExpectChecked("window (web)", "build/test/pay-per-import/window-web-ir", web_window_checked[:])
|
||||
}
|
||||
web_hello_present: [5]string = .["app", "frames_web", "tree", "text", "paint_queue"]
|
||||
if !CheckWebProgram(ziran, "hello:main", "examples/hello.zi", "build/test/pay-per-import/hello-web-ir") {
|
||||
Fail("hello (web)", "does not check", "examples/hello.zi")
|
||||
} else {
|
||||
ExpectCheckedModules("hello (web)", "build/test/pay-per-import/hello-web-ir", web_hello_present[:],
|
||||
not_in_a_plain_page[:])
|
||||
}
|
||||
web_box_present: [2]string = .["box", "tree"]
|
||||
if !BuildWebFrame(ziran, "examples/box") {
|
||||
Fail("box", "does not build for the web", "examples/box")
|
||||
} else {
|
||||
Expect("box", "build/examples/box/generated/web/c", web_box_present[:], not_in_a_plain_web_frame[:])
|
||||
}
|
||||
web_button_present: [2]string = .["button", "tree"]
|
||||
if !BuildWebFrame(ziran, "examples/button") {
|
||||
Fail("button", "does not build for the web", "examples/button")
|
||||
} else {
|
||||
Expect("button", "build/examples/button/generated/web/c", web_button_present[:],
|
||||
not_in_a_plain_web_frame[:])
|
||||
}
|
||||
web_image_present: [3]string = .["image", "lanczos_cache", "page_mesh"]
|
||||
web_image_absent: [3]string = .["test_driver", "text_request", "text_input_platform"]
|
||||
if !BuildWebFrame(ziran, "examples/image") {
|
||||
Fail("image", "does not build for the web", "examples/image")
|
||||
} else {
|
||||
Expect("image", "build/examples/image/generated/web/c", web_image_present[:], web_image_absent[:])
|
||||
}
|
||||
|
||||
if failures > 0 { return 1 }
|
||||
print("pay per import: window, hello, box, button, image and textfield carry only what they import\n")
|
||||
print("pay per import: window, hello, box, button, image and textfield carry only what they import, on Linux and the web\n")
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user