Files
kryon/tests/pixmap_surface_test.zi
T

299 lines
15 KiB
Ziran

#import "pixmap"
#import "pixmap_text"
#import "drawing_props"
#import "geometry"
#import "paint_queue"
pixels: [35]u32;
portrait: [48]u32;
image: [4]u32;
rgb: [4320]u32;
argb: [4320]u32;
filled: [480]u32;
reference: [480]u32;
mask_pixels: [512]u32;
mask_reference: [512]u32;
mask_coverage: [256]u8;
Shape :: struct { x0: s32; y0: s32; x1: s32; y1: s32; radius: s32 }
// Keep the original 4x4 geometric definition independent of the optimized
// sampler. Wide squares cover every supported 16384-pixel surface.
ReferenceSamples :: (x: s32, y: s32, x0: s32, y0: s32, x1: s32, y1: s32, r: s32) -> s32 {
count: s32
for row: cast(s32)0..3 {
for column: cast(s32)0..3 {
px := cast(s64)x * 8 + 1 + column * 2
py := cast(s64)y * 8 + 1 + row * 2
left := cast(s64)x0 * 8; right := cast(s64)x1 * 8
top := cast(s64)y0 * 8; bottom := cast(s64)y1 * 8
if px < left || px >= right || py < top || py >= bottom { continue }
reach := cast(s64)r * 8; dx: s64; dy: s64
if px < left + reach { dx = left + reach - px }
else if px > right - reach { dx = px - (right - reach) }
if py < top + reach { dy = top + reach - py }
else if py > bottom - reach { dy = py - (bottom - reach) }
if dx * dx + dy * dy <= reach * reach { count += 1 }
}
}
return count
}
CheckSamples :: () -> bool {
// Exhaust every pixel around odd/even, empty and thin small rectangles.
for width: cast(s32)0..8 {
for height: cast(s32)0..8 {
for radius: cast(s32)0..4 {
r := PixmapRadius(-3, -5, width - 3, height - 5, radius)
for y: cast(s32)-6..height - 4 {
for x: cast(s32)-4..width - 2 {
if PixmapSamples(x, y, -3, -5, width - 3, height - 5, r) !=
ReferenceSamples(x, y, -3, -5, width - 3, height - 5, r) { return false }
}
}
}
}
}
// Probe all four corners of large radii without allocating a surface.
radii: [7]s32 = .[8, 17, 63, 511, 4096, 5793, 8192]
for r: radii {
for i: cast(s32)0..63 {
x := (i * 163 + 7) % r; y := (i * 281 + 13) % r
for corner: cast(s32)0..3 {
px := ifx corner % 2 == 0 then x else 2 * r - 1 - x
py := ifx corner < 2 then y else 2 * r - 1 - y
if PixmapSamples(px, py, 0, 0, 2 * r, 2 * r, r) !=
ReferenceSamples(px, py, 0, 0, 2 * r, 2 * r, r) { return false }
}
}
}
return true
}
// Compare clipped mask rows with the independent single-pixel primitive.
// Every coverage value appears, with RGB extremes, partial source alpha,
// negative origins, empty clips and guard pixels past the borrowed surface.
CheckMask :: () -> bool {
masks: [4]PixmapGlyphMask = .[
.{4, 2, 16, 16}, .{-6, -9, 16, 16}, .{21, 18, 16, 16}, .{4, 2, 0, 0}]
clips: [4]Rectangle = .[.{0.0, 0.0, 24.0, 20.0}, .{5.0, 4.0, 13.0, 10.0},
.{10.0, 7.0, 1.0, 1.0}, .{30.0, 0.0, 2.0, 20.0}]
colors: [4]Color = .[.{0, 0, 0, 255}, .{255, 255, 255, 255},
.{207, 53, 129, 255}, .{0, 255, 1, 255}]
alphas: [6]u8 = .[0, 1, 63, 127, 254, 255]
for at: cast(s32)0..255 { mask_coverage[at] = cast(u8)at }
for mask: masks {
for clip: clips {
for color: colors {
for alpha: alphas {
color.a = alpha
for format: cast(s32)0..1 {
for at: cast(s32)0..511 {
value := cast(u32)at * cast(u32)39417 + cast(u32)0x90051a32
mask_pixels[at] = value; mask_reference[at] = value
}
opaque := format == 1
unused PixmapResumeInto(24, 20, mask_pixels[:], opaque); PixmapClipPixels(clip)
PixmapBlendMask(mask, mask_coverage[:], color); PixmapEndInto()
unused PixmapResumeInto(24, 20, mask_reference[:], opaque); PixmapClipPixels(clip)
for y: cast(s32)0..mask.height - 1 {
for x: cast(s32)0..mask.width - 1 {
PixmapBlend(mask.left + x, mask.top + y, color,
cast(s32)mask_coverage[y * mask.width + x])
}
}
PixmapEndInto()
for at: cast(s32)0..511 {
if mask_pixels[at] != mask_reference[at] {
print("mask mismatch at % alpha % format %\n", at, cast(s32)alpha, format)
return false
}
}
}
}
}
}
}
return true
}
// The per-pixel raster is the oracle for contiguous interior spans. Rounded
// coverage still comes from the existing 4x4 sampler; blending goes through
// the ordinary single-pixel primitive rather than the span implementation.
CheckFill :: () -> bool {
shapes: [8]Shape = .[
.{0, 0, 24, 20, 0}, .{2, 3, 20, 18, 5}, .{-8, -7, 17, 15, 8},
.{10, 8, 32, 26, 20}, .{7, 4, 8, 17, 8}, .{4, 5, 20, 6, 6},
.{5, 6, 16, 14, -2}, .{8, 5, 8, 14, 4}]
clips: [4]Rectangle = .[.{0.0, 0.0, 24.0, 20.0}, .{5.0, 4.0, 13.0, 10.0},
.{10.0, 7.0, 1.0, 1.0}, .{30.0, 0.0, 2.0, 20.0}]
alphas: [6]u8 = .[0, 1, 63, 127, 254, 255]
for shape: shapes {
for clip: clips {
for alpha: alphas {
for format: cast(s32)0..1 {
opaque := format == 1
for at: cast(s32)0..479 {
// Vary all destination channels, including alpha.
value := cast(u32)at * cast(u32)39417 + cast(u32)0x90051a32
filled[at] = value; reference[at] = value
}
color: Color = .{207, 53, 129, alpha}
unused PixmapResumeInto(24, 20, filled[:], opaque); PixmapClipPixels(clip)
PixmapFillRounded(shape.x0, shape.y0, shape.x1, shape.y1, shape.radius, color)
PixmapEndInto()
unused PixmapResumeInto(24, 20, reference[:], opaque); PixmapClipPixels(clip)
radius := PixmapRadius(shape.x0, shape.y0, shape.x1, shape.y1, shape.radius)
for y: cast(s32)0..19 {
for x: cast(s32)0..23 {
samples := ReferenceSamples(x, y, shape.x0, shape.y0, shape.x1, shape.y1, radius)
if samples > 0 { PixmapBlend(x, y, color, samples * 255 / 16) }
}
}
PixmapEndInto()
for at: cast(s32)0..479 {
if filled[at] != reference[at] {
print("fill mismatch at % alpha % format %\n", at, cast(s32)alpha, format)
return false
}
}
}
}
}
}
return true
}
// Strokes subtract the inner shape's sampled coverage. Probe outside inner
// bounds, over-wide strokes, degenerate geometry and each rounded corner.
CheckStroke :: () -> bool {
shapes: [4]Shape = .[.{2, 3, 20, 18, 5}, .{-8, -7, 17, 15, 8},
.{7, 4, 8, 17, 8}, .{4, 5, 20, 6, 6}]
widths: [4]s32 = .[0, 1, 3, 20]
clips: [2]Rectangle = .[.{0.0, 0.0, 24.0, 20.0}, .{5.0, 4.0, 13.0, 10.0}]
for shape: shapes {
for width: widths {
for clip: clips {
for format: cast(s32)0..1 {
for at: cast(s32)0..479 {
value := cast(u32)at * cast(u32)39417 + cast(u32)0x90051a32
filled[at] = value; reference[at] = value
}
color: Color = .{207, 53, 129, 127}
unused PixmapResumeInto(24, 20, filled[:], format == 1); PixmapClipPixels(clip)
PixmapStrokeRounded(shape.x0, shape.y0, shape.x1, shape.y1, shape.radius, width, color)
PixmapEndInto()
unused PixmapResumeInto(24, 20, reference[:], format == 1); PixmapClipPixels(clip)
radius := PixmapRadius(shape.x0, shape.y0, shape.x1, shape.y1, shape.radius)
w := ifx width < 1 then 1 else width
ir := ifx radius < w then 0 else radius - w
for y: cast(s32)0..19 {
for x: cast(s32)0..23 {
samples := ReferenceSamples(x, y, shape.x0, shape.y0, shape.x1, shape.y1, radius)
if shape.x1 - w > shape.x0 + w && shape.y1 - w > shape.y0 + w {
samples -= ReferenceSamples(x, y, shape.x0 + w, shape.y0 + w,
shape.x1 - w, shape.y1 - w, ir)
}
if samples > 0 { PixmapBlend(x, y, color, samples * 255 / 16) }
}
}
PixmapEndInto()
for at: cast(s32)0..479 { if filled[at] != reference[at] { return false } }
}
}
}
}
return true
}
DrawOpaque :: (surface: []u32, opaque: bool) -> bool {
if !PixmapBeginInto(cast(u32)0x123456, 72, 56, surface, opaque) { return false }
unused PixmapScale(1.25, 1.25)
RasterRoundedRectangle(.{2.0, 2.0, 48.0, 32.0}, 6.0, 8, .{200, 90, 30, 255})
RasterRoundedRectangle(.{4.0, 8.0, 30.0, 24.0}, 3.0, 8, .{10, 70, 220, 127})
RasterRoundedRectangleOutline(.{1.0, 3.0, 48.0, 35.0}, 5.0, 8, 1.5, .{200, 200, 220, 180})
RasterLine(.{2.0, 1.0, 38.0, 28.0}, .{10, 40, 200, 127})
for row: cast(s32)0..4 {
RasterRoundedRectangle(.{10.0, 35.0 + cast(float32)row, 35.0, 1.0}, 0.0, 0,
GradientColorAt(.{80, 30, 150, 120}, .{30, 180, 80, 220}, (cast(float32)row + 0.5) / 5.0))
}
RasterTextClipped("café Ω 🚀", 5, 9, 12, .{240, 245, 255, 210}, .{2.0, 4.0, 48.0, 20.0})
RasterImage("opaque-fixture", cast(u32)0, .{0.0, 0.0, 2.0, 2.0},
.{34.0, 20.0, 18.0, 18.0}, .{34.0, 20.0, 18.0, 18.0}, .{}, 0.0, 3.0, .{255, 255, 255, 190})
if opaque {
// Rejected storage must preserve both the borrowed surface and its
// format. A subsequent pixel still needs its opaque host alpha.
if PixmapResumeInto(73, 60, surface, false) { return false }
RasterLine(.{0.0, 0.0, 0.0, 0.0}, .{1, 2, 3, 255})
} else { RasterLine(.{0.0, 0.0, 0.0, 0.0}, .{1, 2, 3, 255}) }
PixmapEndInto(); return true
}
CheckOpaque :: () -> bool {
image[0] = cast(u32)0xffff0000; image[1] = cast(u32)0xff00ff00
image[2] = cast(u32)0x800000ff; image[3] = cast(u32)0x00ffffff
if !PixmapSetImage(5, "opaque-fixture", 2, 2, image[:]) { return false }
for at: 0..cast(s32)4319 { rgb[at] = cast(u32)0xabcdef; argb[at] = cast(u32)0xabcdef }
if !DrawOpaque(rgb[:], false) || !DrawOpaque(argb[:], true) { return false }
for at: 0..cast(s32)4031 {
if argb[at] != (rgb[at] | cast(u32)0xff000000) { return false }
}
for at: 4032..cast(s32)4319 {
if rgb[at] != cast(u32)0xabcdef || argb[at] != cast(u32)0xabcdef { return false }
}
// EndInto returns to the default RGB backing surface.
RasterLine(.{959.0, 599.0, 0.0, 0.0}, .{1, 2, 3, 255})
if pixmap_pixels[575999] != cast(u32)0x010203 { return false }
PixmapClearImage(5); return true
}
#program_export
main :: () -> s32 {
if !CheckSamples() { return 26 }
if !CheckMask() { return 25 }
if !CheckFill() { return 24 }
if !CheckStroke() { return 27 }
if !CheckOpaque() { return 23 }
image[0] = cast(u32)0xffff0000; image[1] = cast(u32)0xff00ff00
image[2] = cast(u32)0x800000ff; image[3] = cast(u32)0x00ffffff
for i: 0..cast(s32)34 { pixels[i] = cast(u32)0xabcdef }
if !PixmapBeginInto(cast(u32)0x102030, 7, 4, pixels[:]) { return 1 }
for i: 0..cast(s32)27 { if pixels[i] != cast(u32)0x102030 { return 2 } }
for i: 28..cast(s32)34 { if pixels[i] != cast(u32)0xabcdef { return 3 } }
RasterLine(.{6.0, 0.0, 0.0, 3.0}, .{255, 0, 0, 255})
if pixels[6] != cast(u32)0xff0000 || pixels[27] != cast(u32)0xff0000 || pixels[26] != cast(u32)0x102030 { return 4 }
RasterRoundedRectangle(.{-2.0, 2.0, 4.0, 5.0}, 0.0, 1, .{0, 255, 0, 255})
if pixels[14] != cast(u32)0x00ff00 || pixels[21] != cast(u32)0x00ff00 || pixels[23] != cast(u32)0x102030 { return 5 }
if PixmapBeginInto(cast(u32)0, 8, 5, pixels[:]) || PixmapBeginInto(cast(u32)0, 0, 1, pixels[:]) || PixmapBeginInto(cast(u32)0, 16385, 1, pixels[:]) { return 6 }
if pixmap_width != 7 || pixmap_height != 4 || pixels[14] != cast(u32)0x00ff00 { return 7 }
if !PixmapBeginInto(cast(u32)0xffffff, 4, 12, portrait[:]) { return 8 }
RasterLine(.{3.0, 0.0, 0.0, 11.0}, .{0, 0, 255, 255})
if portrait[3] != cast(u32)0x0000ff || portrait[47] != cast(u32)0x0000ff || portrait[46] != cast(u32)0xffffff { return 9 }
PixmapEndInto()
PixmapBegin(cast(u32)0x123456)
RasterLine(.{959.0, 599.0, 0.0, 0.0}, .{1, 2, 3, 255})
if pixmap_pixels[575999] != cast(u32)0x010203 || portrait[47] != cast(u32)0x0000ff || pixels[28] != cast(u32)0xabcdef { return 10 }
version := PixmapImageRevision()
if !PixmapSetImage(0, "fixture", 2, 2, image[:]) || ImageWidth("fixture") != 2 || ImageHeight("fixture") != 2 { return 11 }
if PixmapSetImage(1, "fixture", 2, 2, image[:]) || PixmapSetImage(0, "bad", 3, 2, image[:]) || ImageWidth("fixture") != 2 { return 12 }
if PixmapImageRevision() != version + cast(u64)1 { return 16 }
if !PixmapSetImage(0, "fixture", 2, 2, image[:]) || PixmapImageRevision() != version + cast(u64)2 { return 17 }
unused PixmapBeginInto(cast(u32)0, 4, 4, portrait[:])
RasterImage("fixture", cast(u32)0, .{0.0, 0.0, 2.0, 2.0}, .{0.0, 0.0, 4.0, 4.0}, .{0.0, 0.0, 4.0, 4.0}, .{}, 0.0, 0.0, .{255, 255, 255, 255})
if portrait[0] != cast(u32)0xff0000 || portrait[3] != cast(u32)0x00ff00 || portrait[12] != cast(u32)0x000080 || portrait[15] != cast(u32)0 { return 13 }
unused PixmapBeginInto(cast(u32)0, 4, 4, portrait[:])
RasterImage("fixture", cast(u32)0, .{0.0, 0.0, 2.0, 2.0}, .{0.0, 0.0, 4.0, 4.0}, .{0.0, 0.0, 4.0, 4.0}, .{}, 0.0, 2.0, .{255, 255, 255, 255})
if portrait[0] != cast(u32)0 || portrait[1] != cast(u32)0xff0000 || portrait[2] != cast(u32)0x00ff00 { return 14 }
PixmapClearImage(0)
if ImageWidth("fixture") != 0 || ImageHeight("fixture") != 0 || pixmap_image_pixels[0].value.count != 0 { return 15 }
if PixmapImageRevision() != version + cast(u64)3 { return 18 }
PixmapClearImage(0); PixmapClearImage(-1)
if PixmapImageRevision() != version + cast(u64)3 { return 19 }
if !PixmapSetImage(768, "late-fixture", 2, 2, image[:]) || ImageWidth("late-fixture") != 2 { return 20 }
if PixmapSetImage(PixmapImageLimit, "outside-fixture", 2, 2, image[:]) { return 21 }
PixmapClearImage(768)
if ImageWidth("late-fixture") != 0 { return 22 }
PixmapEndInto()
print("Pixmap caller-owned surfaces passed\n"); return 0
}