Mirror signed image sources and isolate invalid image draws

This commit is contained in:
waozi committed 2026-11-17 01:24:17 -03:00
1 parent 156cea179d
commit 2d608291a9
7 files changed
+204 -14

No files matched your search

+6
View File
@@ -1087,3 +1087,9 @@ test: android-present-damage-test
image-dimensions-host-test: project-toolchain
@sh tests/image_dimensions_host_test.sh
test: image-dimensions-host-test
.PHONY: mirrored-image-test
mirrored-image-test: project-toolchain
@sh tests/ziran_cairo_mirrored_image_test.sh
@sh tests/pixmap_mirrored_image_test.sh
test: mirrored-image-test
+25 -12
View File
@@ -243,7 +243,6 @@ ShapedCaret :: (value: string, byte: s32, font: s32, typeface: string) -> s32 {
surface: *void;
paint: *void;
image_error: bool;
frame_pending: bool;
raster_presented_surface: *void;
RasterFrameImage :: #type (id: u32) -> *void;
@@ -562,7 +561,6 @@ CloseRaster :: () {
BeginRasterFrame :: (retain: bool = false) {
if raster_smooth_pump != null { unused raster_smooth_pump() }
frame_pending = true
image_error = false
text_error = false
if !retain { PrepareRasterPaint() }
}
@@ -600,7 +598,7 @@ ClearRasterFrame :: (color: Color) {
FinishRasterFrame :: (retain: bool = false) -> bool {
// A changed frame with no commands still replaces the previous picture.
if !retain { PrepareRasterPaint() }
if image_error || text_error || Status(paint) != 0 { return false }
if text_error || Status(paint) != 0 { return false }
SurfaceFlush(surface)
if SurfaceStatus(surface) != 0 { return false }
return true
@@ -1197,20 +1195,28 @@ RasterImage :: (asset_path: string, texture_id: u32,
origin: Vector2, rotation: float32, radius: float32, tint: Color) {
PrepareRasterPaint()
if (texture_id == cast(u32)0 && asset_path.count == 0) ||
source.width <= 0.0 || source.height <= 0.0 ||
!Finite32(source.x) || !Finite32(source.y) || !Finite32(source.width) || !Finite32(source.height) ||
!Finite32(destination.x) || !Finite32(destination.y) || !Finite32(destination.width) || !Finite32(destination.height) ||
!Finite32(clip.x) || !Finite32(clip.y) || !Finite32(clip.width) || !Finite32(clip.height) ||
!Finite32(origin.x) || !Finite32(origin.y) || !Finite32(rotation) || !Finite32(radius) ||
source.width == 0.0 || source.height == 0.0 ||
destination.width <= 0.0 || destination.height <= 0.0 ||
clip.width <= 0.0 || clip.height <= 0.0 {
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
image := CairoPictureFor(asset_path, texture_id)
if image == null {
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
mirror_x := source.width < 0.0; mirror_y := source.height < 0.0
if mirror_x { source.width = -source.width }
if mirror_y { source.height = -source.height }
sampled: *Lanczos.LanczosEntry
snapped: Rectangle
if texture_id == cast(u32)0 && raster_picture_smooth != null && rotation == 0.0 &&
// Mirrored sources keep Cairo's filter; cached copies have positive axes.
if !mirror_x && !mirror_y && texture_id == cast(u32)0 && raster_picture_smooth != null && rotation == 0.0 &&
origin.x == 0.0 && origin.y == 0.0 && CurrentPaintSmooth() {
sampled = raster_picture_smooth(image, source, destination, *snapped)
}
@@ -1276,6 +1282,8 @@ RasterImage :: (asset_path: string, texture_id: u32,
destination.height)
Clip(paint)
}
if mirror_x { Translate(paint, cast(float64)destination.width, 0.0); Scale(paint, -1.0, 1.0) }
if mirror_y { Translate(paint, 0.0, cast(float64)destination.height); Scale(paint, 1.0, -1.0) }
Scale(paint, cast(float64)destination.width /
cast(float64)source.width,
cast(float64)destination.height / cast(float64)source.height)
@@ -1286,7 +1294,7 @@ RasterImage :: (asset_path: string, texture_id: u32,
if pattern == null {
Restore(paint)
CairoReleaseManaged(texture_id, image)
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
PatternFilter(pattern, ifx CurrentPaintSmooth() then 1 else 3)
@@ -1306,16 +1314,21 @@ RasterImage :: (asset_path: string, texture_id: u32,
RasterImageMesh :: (asset_path: string, texture_id: u32, source: Rectangle,
points: []Vector2, columns: s32, rows: s32, clip: Rectangle, tint: Color) {
PrepareRasterPaint()
if columns < 2 || rows < 2 || clip.width <= 0.0 || clip.height <= 0.0 { return }
if columns < 2 || rows < 2 || !Finite32(clip.x) || !Finite32(clip.y) ||
!Finite32(clip.width) || !Finite32(clip.height) || clip.width <= 0.0 || clip.height <= 0.0 ||
!Finite32(source.x) || !Finite32(source.y) || !Finite32(source.width) || !Finite32(source.height) ||
source.width == 0.0 || source.height == 0.0 {
print("Kryon: skipped image mesh: invalid geometry\n"); return
}
count: s64 = cast(s64)columns * cast(s64)rows
if count > cast(s64)ImageSchema.IMAGE_MESH_POINT_LIMIT || points.count < count { return }
if texture_id == cast(u32)0 && asset_path.count == 0 {
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
image := CairoPictureFor(asset_path, texture_id)
if image == null {
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
Save(paint)
@@ -1393,7 +1406,7 @@ CairoMeshTriangle :: (image: *void, a: Vector2, a_source: Vector2,
pattern: *void = PatternForSurface(image)
if pattern == null {
Restore(paint)
image_error = true
print("Kryon: skipped image: invalid or unavailable picture\n")
return
}
PatternFilter(pattern, ifx CurrentPaintSmooth() then 4 else 3)
+10 -2
View File
@@ -1305,11 +1305,14 @@ RasterImageUnmasked :: (asset_path: string, texture_id: u32, source: Rectangle,
!Finite32(destination.x) || !Finite32(destination.y) || !Finite32(destination.width) || !Finite32(destination.height) ||
!Finite32(clip.x) || !Finite32(clip.y) || !Finite32(clip.width) || !Finite32(clip.height) ||
!Finite32(origin.x) || !Finite32(origin.y) || !Finite32(rotation) || !Finite32(radius) ||
destination.width <= 0.0 || destination.height <= 0.0 || clip.width <= 0.0 || clip.height <= 0.0 { return }
source.width == 0.0 || source.height == 0.0 ||
destination.width <= 0.0 || destination.height <= 0.0 || clip.width <= 0.0 || clip.height <= 0.0 {
print("Kryon: skipped image: invalid geometry\n"); return
}
smooth := CurrentPaintSmooth()
#if PIXMAP_LANCZOS {
// A smooth unrotated image draws its Lanczos copy pixel for pixel.
if smooth && rotation == 0.0 {
if smooth && rotation == 0.0 && source.width > 0.0 && source.height > 0.0 {
shown: Rectangle = .{destination.x - origin.x, destination.y - origin.y, destination.width, destination.height}
copy_source: Rectangle; snapped: Rectangle; stretched: bool
copy := PixmapLanczosSlot(slot, source, shown, *copy_source, *snapped, *stretched)
@@ -1319,6 +1322,11 @@ RasterImageUnmasked :: (asset_path: string, texture_id: u32, source: Rectangle,
}
}
}
// Negative source sizes mirror the positive source rectangle, as
// in ImageCanvas and meshes. Both CPU sampling and GPU UVs then walk
// from its far edge back toward its unchanged source origin.
if source.width < 0.0 { source.x -= source.width }
if source.height < 0.0 { source.y -= source.height }
// Clamp floats before converting them to pixel indexes.
left := clip.x; top := clip.y; right := clip.x + clip.width; bottom := clip.y + clip.height
if !Finite32(right) || !Finite32(bottom) { return }
+70
View File
@@ -0,0 +1,70 @@
Raster :: #import "cairo_raster";
Pictures :: #import "raster_picture";
Texture :: #import "paint_texture";
#import "geometry"
#import "drawing_props"
pixels: [6]u32 = .{0xffd01020, 0xff20b030, 0xff3040e0, 0xffd0a020, 0xff20a0c0, 0xffa030c0};
white :: Color.{255, 255, 255, 255};
clip :: Rectangle.{0.0, 0.0, 8.0, 8.0};
Picture :: (path: string, picture: *Pictures.RasterPicture) -> bool {
if path != "mirror" { return false }
picture.* = .{cast(*u8)*pixels[0], 3, 2, 1}
return true
}
Playing :: () -> bool { return false }
Stop :: () {}
Pixel :: (x: s32, y: s32) -> u32 {
data := Raster.RasterSurfaceData()
pixel := cast(*u32)*data[y * Raster.RasterSurfaceStride() + x * 4]
return pixel.*
}
#program_export
main :: () -> s32 {
source: Pictures.RasterPictureSource = Picture
playing: Pictures.RasterSourcePlaying = Playing
stop: Pictures.RasterSourceStop = Stop
unused Pictures.RasterUsePictureSource(source, playing, stop)
if !Raster.OpenRaster(8, 8) || Raster.ImageWidth("mirror") != 3 { return 1 }
defer { Raster.CloseRaster() }
for smooth: cast(s32)0..1 {
Texture.emitting_smooth = smooth == 1
for crop: cast(s32)0..1 {
width := 3 - crop
for flip: cast(s32)0..3 {
Raster.BeginRasterFrame()
box := Rectangle.{cast(float32)crop, 0.0,
cast(float32)(ifx (flip & 1) == 0 then width else -width),
ifx (flip & 2) == 0 then 2.0 else -2.0}
Raster.RasterImage("mirror", 0, box, .{1.0, 1.0, cast(float32)width, 2.0}, clip, .{}, 0.0, 0.0, white)
if !Raster.FinishRasterFrame() { return 2 }
for y: cast(s32)0..1 {
for x: cast(s32)0..width - 1 {
sx := crop + (ifx (flip & 1) == 0 then x else width - 1 - x)
sy := ifx (flip & 2) == 0 then y else 1 - y
if Pixel(x + 1, y + 1) != pixels[sy * 3 + sx] {
print("Cairo mirror mismatch: smooth % crop % flip % at %, %\n", smooth, crop, flip, x, y)
return 3
}
}
}
}
}
}
// Missing pictures and invalid geometry must not poison the context or
// discard the valid drawing that follows them in the same frame.
Raster.BeginRasterFrame()
Raster.RasterImage("missing", 0, .{0.0, 0.0, 3.0, 2.0}, .{0.0, 0.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
Raster.RasterImage("mirror", 0, .{}, .{0.0, 0.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
invalid_bits: u32 = 0x7fc00000
invalid_pointer := cast(*float32)*invalid_bits
invalid := invalid_pointer.*
Raster.RasterImage("mirror", 0, .{invalid, 0.0, 3.0, 2.0}, .{0.0, 0.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
points: [4]Vector2 = .{.{0.0, 0.0}, .{3.0, 0.0}, .{0.0, 2.0}, .{3.0, 2.0}}
Raster.RasterImageMesh("missing", 0, .{0.0, 0.0, 3.0, 2.0}, points[:], 2, 2, clip, white)
Raster.RasterImage("mirror", 0, .{0.0, 0.0, -3.0, -2.0}, .{1.0, 1.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
if !Raster.FinishRasterFrame() || Pixel(1, 1) != pixels[5] { return 4 }
print("Cairo mirrored images: both axes, crops, smooth/sharp and invalid draw isolation passed\n")
return 0
}
+28
View File
@@ -0,0 +1,28 @@
#!/bin/sh
set -eu
repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
. "$repo/tests/toolchain.sh"
work="$repo/build/scratch/pixmap-mirrored-image"
mkdir -p "$work"
source="$repo/tests/pixmap_mirrored_image_test.zi"
"$ziran" ir --project --root "$repo/tests" -o "$work/ir" "$source"
for target in c go; do
for input in source saved; do
executable=--exe
package=; if test "$target" = go; then package='--pkg main'; fi
if test "$input" = source; then
"$ziran" build --project --root "$repo/tests" --target="$target" --entry pixmap_mirrored_image_test:main $package $executable -o "$work/$target-$input" "$source"
else
"$ziran" build --root "$work/ir" --module-path "$work/ir" --target="$target" --entry pixmap_mirrored_image_test:main $package $executable -o "$work/$target-$input" "$work/ir/pixmap_mirrored_image_test.zir"
fi
if test "$target" = go; then
env -u DISPLAY -u WAYLAND_DISPLAY GO111MODULE=off go run "$work/$target-$input"/*.go
else
env -u DISPLAY -u WAYLAND_DISPLAY "$work/$target-$input/pixmap_mirrored_image_test"
fi
done
done
"$ziran" bundle --project --root "$repo/tests" --entry pixmap_mirrored_image_test:main -o "$work/source.zx" "$source"
"$ziran" bundle --root "$work/ir" --module-path "$work/ir" --entry pixmap_mirrored_image_test:main -o "$work/saved.zx" "$work/ir/pixmap_mirrored_image_test.zir"
test "$(env -u DISPLAY -u WAYLAND_DISPLAY "$ziran_root/build/bin/zi2zx" run --backend engine "$work/source.zx" | tail -1)" = 0
test "$(env -u DISPLAY -u WAYLAND_DISPLAY "$ziran_root/build/bin/zi2zx" run --backend engine "$work/saved.zx" | tail -1)" = 0
+44
View File
@@ -0,0 +1,44 @@
using Pixels :: #import "pixmap";
Texture :: #import "paint_texture";
#import "geometry"
#import "drawing_props"
image: [6]u32 = .{0xffd01020, 0xff20b030, 0xff3040e0, 0xffd0a020, 0xff20a0c0, 0xffa030c0};
frame: [64]u32;
white :: Color.{255, 255, 255, 255};
clip :: Rectangle.{0.0, 0.0, 8.0, 8.0};
#program_export
main :: () -> s32 {
if !PixmapSetImage(0, "mirror", 3, 2, image[:]) { return 1 }
for smooth: cast(s32)0..1 {
Texture.emitting_smooth = smooth == 1
for crop: cast(s32)0..1 {
width := 3 - crop
for flip: cast(s32)0..3 {
if !PixmapBeginInto(0, 8, 8, frame[:], true) { return 2 }
box := Rectangle.{cast(float32)crop, 0.0,
cast(float32)(ifx (flip & 1) == 0 then width else -width),
ifx (flip & 2) == 0 then 2.0 else -2.0}
RasterImage("mirror", 0, box, .{1.0, 1.0, cast(float32)width, 2.0}, clip, .{}, 0.0, 0.0, white)
PixmapEndInto()
for y: cast(s32)0..1 {
for x: cast(s32)0..width - 1 {
sx := crop + (ifx (flip & 1) == 0 then x else width - 1 - x)
sy := ifx (flip & 2) == 0 then y else 1 - y
if frame[(y + 1) * 8 + x + 1] != image[sy * 3 + sx] {
print("Pixmap mirror mismatch: smooth % crop % flip % at %, %\n", smooth, crop, flip, x, y)
return 3
}
}
}
}
}
}
if !PixmapBeginInto(0, 8, 8, frame[:], true) { return 4 }
RasterImage("mirror", 0, .{}, .{1.0, 1.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
RasterImage("mirror", 0, .{0.0, 0.0, -3.0, -2.0}, .{1.0, 1.0, 3.0, 2.0}, clip, .{}, 0.0, 0.0, white)
PixmapEndInto()
if frame[9] != image[5] { return 5 }
print("Pixmap mirrored images: both axes, crops, smooth/sharp and invalid draw isolation passed\n")
return 0
}
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
set -eu
repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
. "$repo/tests/toolchain.sh"
work="$repo/build/scratch/cairo-mirrored-image"
mkdir -p "$work"
unset DISPLAY WAYLAND_DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS SESSION_MANAGER
export HARMONY_DESKTOP_RECOVERY=0 YUE_DESKTOP_RECOVERY=0
"$ziran" ir $kryon_project_args --root "$repo/tests" --define POSIX_THREADS -o "$work/ir" "$repo/tests/cairo_mirrored_image_test.zi"
for form in source saved; do
input="$repo/tests/cairo_mirrored_image_test.zi"; root="$repo/tests"
if test "$form" = saved; then input="$work/ir/cairo_mirrored_image_test.zir"; root="$work/ir"; fi
output="$work/$form"
"$ziran" build $kryon_project_args --target=c --root "$root" --define POSIX_THREADS --prune-stale -o "$output" "$input"
includes=$(find "$output" -type d -printf ' -iquote %p')
# shellcheck disable=SC2046,SC2086
"${CC:-cc}" -std=c11 -O1 -ffunction-sections -fdata-sections -I"$ziran_root/include" $includes \
$(find "$output" -name '*.c') "$ziran_root/build/libziran.a" -Wl,--gc-sections \
$(pkg-config --libs cairo freetype2 fontconfig pangocairo) -ldl -lm -lpthread -o "$output/test"
"$output/test"
done