Files
kryon/tests/kry_sw_test.c
T
waozi 3319143452 kry_sw: track the dirty rectangle per call
KrySwDirty previously reported the full frame. Drawing ops now union
their clip-intersected bounds into an accumulator (fill_rect covers
rects, circles, rings, blits and the bitmap font; text marks its
string bbox so the atlas path is included), and KrySwDirty became
one-shot: it reports everything drawn since the previous read and
resets, so hosts can present partial frames.
2026-08-18 16:17:29 -03:00

253 lines
9.7 KiB
C

#include "kry_backend_rec.h"
#include "kry_sw.h"
#include "../src/backend/kry_sw_png.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
fail(const char *msg)
{
fprintf(stderr, "kry_sw_test: %s\n", msg);
return 1;
}
static unsigned
px(const KrySw *sw, int x, int y)
{
const unsigned char *p = sw->pixels + (size_t)y * sw->stride + x * 4;
return ((unsigned)p[0] << 24) | ((unsigned)p[1] << 16) |
((unsigned)p[2] << 8) | (unsigned)p[3];
}
int
main(void)
{
KrySw sw;
const KryBackend *b;
if(KrySwInit(&sw, NULL, 32, 32) != 0)
return fail("init");
b = KrySwBackend(&sw);
/* clear paints the whole target */
b->clear(0xff0000ffu);
if(px(&sw, 0, 0) != 0xff0000ffu || px(&sw, 31, 31) != 0xff0000ffu)
return fail("clear did not cover the target");
/* rect fill, including clamping outside the target */
b->rect(4, 4, 4, 4, 0x00ff00ffu);
if(px(&sw, 5, 5) != 0x00ff00ffu || px(&sw, 3, 5) != 0xff0000ffu ||
px(&sw, 8, 5) != 0xff0000ffu)
return fail("rect fill wrong");
b->rect(30, 30, 8, 8, 0x0000ffffu);
if(px(&sw, 31, 31) != 0x0000ffffu)
return fail("rect did not clamp to target");
/* clipping confines drawing */
b->clear(0xff0000ffu);
b->clip_push(0, 0, 16, 32);
b->rect(0, 0, 32, 32, 0x00ff00ffu);
b->clip_pop();
if(px(&sw, 15, 15) != 0x00ff00ffu || px(&sw, 16, 15) != 0xff0000ffu)
return fail("clip not respected");
/* text writes pixels inside its cell and measure matches advance */
b->clear(0x00000000u);
b->text("A", 2, 2, 16, 0xffffffffu);
{
int x;
int y;
int lit = 0;
for(y = 2; y < 18; y++)
for(x = 2; x < 10; x++)
if(px(&sw, x, y) == 0xffffffffu)
lit++;
if(lit == 0)
return fail("text drew nothing");
if(lit == 8 * 16)
return fail("text drew a solid block");
}
if(b->measure_text("AB", 16) != 16)
return fail("measure_text mismatch");
/* rgb565 conversion: pure red -> 0xf800 */
{
unsigned short dst[32 * 32];
KrySwToRGB565(&sw, dst);
b->clear(0xff0000ffu);
KrySwToRGB565(&sw, dst);
if(dst[0] != 0xf800u)
return fail("rgb565 red conversion");
}
/* recording wraps sw and counts draw calls */
{
KryBackendRec rec;
const KryBackend *rb = KryBackendRecBackend(&rec, NULL, b);
KryBackendSelect(rb);
KryBackendCurrent()->clear(0xff0000ffu);
KryBackendCurrent()->rect(1, 1, 2, 2, 0x00ff00ffu);
if(px(&sw, 1, 1) != 0x00ff00ffu)
return fail("recorder did not delegate to inner engine");
KryBackendCurrent()->text("hi", 0, 0, 8, 0xffffffffu);
if(KryBackendRecCalls(&rec) != 3)
return fail("recorder call count");
}
/* PNG decoding: 4x4 RGBA (dynamic-huffman deflate) */
{
static const unsigned char png_rgba[] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,
0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,
0x08,0x06,0x00,0x00,0x00,0xa9,0xf1,0x9e,0x7e,0x00,0x00,0x00,
0x31,0x49,0x44,0x41,0x54,0x78,0xda,0x15,0xca,0x21,0x01,0x00,
0x30,0x0c,0x03,0xc1,0x08,0x0b,0xa8,0xac,0xc0,0x8a,0x8a,0xbf,
0xee,0x87,0x8e,0x9c,0xa4,0x3d,0x6b,0x27,0x58,0x94,0xbc,0x63,
0xef,0x05,0x8b,0x52,0x18,0x61,0x60,0xf3,0x47,0x19,0x65,0x60,
0xf1,0x01,0x77,0xbd,0x1d,0x39,0x54,0xc4,0x65,0x55,0x00,0x00,
0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
};
unsigned char *rgba;
int w = 0, h = 0;
rgba = kry_sw_png_rgba(png_rgba, sizeof(png_rgba), &w, &h);
if(rgba == NULL || w != 4 || h != 4)
return fail("rgba png did not decode");
/* pixels are (x*60, y*60, 0x80, alternating alpha) */
if(rgba[0] != 0x00 || rgba[1] != 0x00 || rgba[2] != 0x80 ||
rgba[3] != 0xff)
return fail("rgba pixel (0,0)");
if(rgba[4] != 60 || rgba[5] != 0 || rgba[7] != 0x40)
return fail("rgba pixel (1,0)");
if(rgba[((size_t)3 * 4 + 3) * 4 + 0] != 180 ||
rgba[((size_t)3 * 4 + 3) * 4 + 1] != 180 ||
rgba[((size_t)3 * 4 + 3) * 4 + 3] != 0xff)
return fail("rgba pixel (3,3)");
free(rgba);
}
/* PNG decoding: 3x2 palette with tRNS alpha */
{
static const unsigned char png_pal[] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,
0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x02,
0x08,0x03,0x00,0x00,0x00,0xaa,0xaa,0x96,0x28,0x00,0x00,0x00,
0x09,0x50,0x4c,0x54,0x45,0xff,0x00,0x00,0x00,0xff,0x00,0x00,
0x00,0xff,0x2d,0x4a,0xcd,0x8a,0x00,0x00,0x00,0x02,0x74,0x52,
0x4e,0x53,0xff,0x80,0x08,0x0f,0xb3,0x6a,0x00,0x00,0x00,0x10,
0x49,0x44,0x41,0x54,0x78,0xda,0x63,0x60,0x60,0x64,0x62,0x60,
0x62,0x64,0x00,0x00,0x00,0x20,0x00,0x07,0xf5,0x2a,0xdf,0x2f,
0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
};
unsigned char *rgba;
int w = 0, h = 0;
rgba = kry_sw_png_rgba(png_pal, sizeof(png_pal), &w, &h);
if(rgba == NULL || w != 3 || h != 2)
return fail("palette png did not decode");
/* row 0 is indices 0,1,2; index 1 carries tRNS alpha 0x80 */
if(rgba[0] != 0xff || rgba[1] != 0 || rgba[3] != 0xff)
return fail("palette pixel (0,0)");
if(rgba[4] != 0 || rgba[5] != 0xff || rgba[7] != 0x80)
return fail("palette pixel (1,0) alpha");
if(rgba[8] != 0 || rgba[9] != 0 || rgba[10] != 0xff)
return fail("palette pixel (2,0)");
free(rgba);
}
/* malformed input must fail cleanly, never crash */
{
static const unsigned char png_rgba[] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,
0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,
0x08,0x06,0x00,0x00,0x00,0xa9,0xf1,0x9e,0x7e,0x00,0x00,0x00,
0x31,0x49,0x44,0x41,0x54,0x78,0xda,0x15,0xca,0x21,0x01,0x00,
0x30,0x0c,0x03,0xc1,0x08,0x0b,0xa8,0xac,0xc0,0x8a,0x8a,0xbf,
0xee,0x87,0x8e,0x9c,0xa4,0x3d,0x6b,0x27,0x58,0x94,0xbc,0x63,
0xef,0x05,0x8b,0x52,0x18,0x61,0x60,0xf3,0x47,0x19,0x65,0x60,
0xf1,0x01,0x77,0xbd,0x1d,0x39,0x54,0xc4,0x65,0x55,0x00,0x00,
0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
};
int w = 0, h = 0;
if(kry_sw_png_rgba(png_rgba, 20, &w, &h) != NULL)
return fail("truncated png decoded");
if(kry_sw_png_rgba(png_rgba + 1, sizeof(png_rgba) - 1, &w, &h) != NULL)
return fail("bad signature decoded");
{
unsigned char patched[sizeof(png_rgba)];
memcpy(patched, png_rgba, sizeof(patched));
patched[24] = 16; /* IHDR bit depth 16 */
if(kry_sw_png_rgba(patched, sizeof(patched), &w, &h) != NULL)
return fail("16-bit depth decoded");
}
}
/* the texture entry point blits decoded files with caching */
{
static const unsigned char png_rgba[] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,
0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,
0x08,0x06,0x00,0x00,0x00,0xa9,0xf1,0x9e,0x7e,0x00,0x00,0x00,
0x31,0x49,0x44,0x41,0x54,0x78,0xda,0x15,0xca,0x21,0x01,0x00,
0x30,0x0c,0x03,0xc1,0x08,0x0b,0xa8,0xac,0xc0,0x8a,0x8a,0xbf,
0xee,0x87,0x8e,0x9c,0xa4,0x3d,0x6b,0x27,0x58,0x94,0xbc,0x63,
0xef,0x05,0x8b,0x52,0x18,0x61,0x60,0xf3,0x47,0x19,0x65,0x60,
0xf1,0x01,0x77,0xbd,0x1d,0x39,0x54,0xc4,0x65,0x55,0x00,0x00,
0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
};
char path[256];
FILE *f;
sprintf(path, "%s/kry_sw_test_texture.png",
getenv("TMPDIR") != NULL ? getenv("TMPDIR") : "/tmp");
f = fopen(path, "wb");
if(f == NULL)
return fail("texture test setup");
fwrite(png_rgba, 1, sizeof(png_rgba), f);
fclose(f);
b->clear(0xff0000ffu);
b->texture(path, 0, 0, 8, 8, 0xffffffffu, 0);
/* pixel (1,1) of an 8x8 blit samples source (0,0): opaque 0x0080 */
if(px(&sw, 1, 1) != 0x000080ffu)
return fail("texture blit did not decode the png");
/* pixel (7,7) samples source (3,3): (180,180,0x80,255) */
if(px(&sw, 7, 7) != 0xb4b480ffu)
return fail("texture blit corner sample");
b->texture(path, 0, 0, 8, 8, 0xffffffffu, 0); /* cached path */
remove(path);
}
/* dirty rect: unions per-call, honors nothing else needed, one-shot */
{
int dx, dy, dw, dh;
b->clear(0xff0000ffu);
KrySwDirty(&sw, &dx, &dy, &dw, &dh);
if(dx != 0 || dy != 0 || dw != sw.w || dh != sw.h)
return fail("clear did not mark the full frame");
b->rect(4, 4, 4, 4, 0x00ff00ffu);
b->rect(12, 8, 4, 2, 0x00ff00ffu);
KrySwDirty(&sw, &dx, &dy, &dw, &dh);
if(dx != 4 || dy != 4 || dw != 12 || dh != 6)
return fail("dirty rect did not union two fills");
KrySwDirty(&sw, &dx, &dy, &dw, &dh);
if(dw != 0 || dh != 0)
return fail("dirty rect did not reset after read");
b->text("hi", 10, 4, 16, 0xffffffffu);
KrySwDirty(&sw, &dx, &dy, &dw, &dh);
if(dx != 10 || dy != 4 || dw != 16 || dh != 16) {
fprintf(stderr, "got x=%d y=%d w=%d h=%d\n", dx, dy, dw, dh);
return fail("text did not mark its string bbox");
}
}
KrySwFree(&sw);
printf("ok\n");
return 0;
}