Files
nt-lite/code/user/tests/surface_smoke/surface_smoke.c
T

140 lines
5.0 KiB
C

// Native surface smoke test.
// Verifies shared surface storage, attenuated mappings, commit notification, and cleanup.
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <ntlite/syscall.h>
#define SURFACE_W 64u
#define SURFACE_H 48u
static void print(const char* s) {
SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) };
SysWriteConsole(&req);
}
static void print_u64(uint64_t v) {
char buf[32];
int i = 0;
if (v == 0) { print("0"); return; }
while (v && i < (int)sizeof(buf)) {
buf[i++] = (char)('0' + (v % 10));
v /= 10;
}
while (i--) {
char c = buf[i];
SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) };
SysWriteConsole(&req);
}
}
static void exit_now(int code) {
while (true) SysExitProcess(code);
}
static void fail(const char* msg) {
print("surface_smoke: ");
print(msg);
print("\n");
exit_now(1);
}
static void fail_status(const char* what, KSTATUS status) {
print("surface_smoke: ");
print(what);
print(" failed status=");
print_u64(status);
print("\n");
exit_now(1);
}
int main(void) {
print("surface_smoke: start\n");
uint32_t surface = 0;
SYS_CREATE_SURFACE_REQUEST req;
memset(&req, 0, sizeof(req));
req.Size = sizeof(req);
req.Width = SURFACE_W;
req.Height = SURFACE_H;
req.Format = SYS_SURFACE_FORMAT_XRGB8888;
req.Flags = SYS_SURFACE_FLAG_NONE;
req.HandleOut = &surface;
KSTATUS s = SysCreateSurface(&req);
if (s != NTLSTATUS_SUCCESS) fail_status("create surface", s);
if (!surface) fail("surface handle is zero");
void* base = 0;
uint32_t pitch = 0;
s = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch);
if (s != NTLSTATUS_SUCCESS) fail_status("map surface", s);
if (!base || pitch < SURFACE_W * sizeof(uint32_t)) fail("invalid mapping");
SYS_HANDLE readHandle = SYS_HANDLE_INVALID;
SYS_DERIVE_HANDLE_REQUEST derive = {
sizeof(derive),
surface,
SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_SURFACE_RIGHT_MAP_READ,
&readHandle,
};
s = SysDeriveHandle(&derive);
if (s != NTLSTATUS_SUCCESS) fail_status("derive read-only surface", s);
void* readBase = 0;
uint32_t readPitch = 0;
s = SysMapSurface(readHandle, SYS_SURFACE_MAP_READ, &readBase, &readPitch);
if (s != NTLSTATUS_SUCCESS) fail_status("map read-only surface", s);
if (!readBase || readPitch != pitch || readBase == base) fail("invalid read-only mapping");
SYS_SURFACE_DESC desc;
memset(&desc, 0, sizeof(desc));
s = SysQuerySurface(surface, &desc);
if (s != NTLSTATUS_SUCCESS) fail_status("query surface", s);
if (desc.Width != SURFACE_W || desc.Height != SURFACE_H) fail("query dimensions mismatch");
if (desc.PitchBytes != pitch || desc.Format != SYS_SURFACE_FORMAT_XRGB8888) fail("query metadata mismatch");
if (desc.MapCount != 2) fail("surface map count mismatch");
if (desc.ContentGeneration != 0 || desc.CommitCount != 0) fail("fresh surface generation mismatch");
uint8_t* rows = (uint8_t*)base;
for (uint32_t y = 0; y < SURFACE_H; ++y) {
uint32_t* row = (uint32_t*)(rows + (size_t)y * pitch);
for (uint32_t x = 0; x < SURFACE_W; ++x)
row[x] = 0x00202020u + ((x & 0xffu) << 16) + ((y & 0xffu) << 8);
}
const uint32_t* readPixel = (const uint32_t*)((const uint8_t*)readBase + (size_t)17u * readPitch) + 23u;
uint32_t expectedPixel = 0x00202020u + (23u << 16) + (17u << 8);
if (*readPixel != expectedPixel) fail("shared read-only mapping does not see written pixels");
DISPLAY_RECT rect = { 0, 0, (int32_t)SURFACE_W, (int32_t)SURFACE_H };
uint64_t generation = 0;
s = SysCommitSurface(surface, &rect, 1, &generation);
if (s != NTLSTATUS_SUCCESS) fail_status("commit surface", s);
if (generation != 1) fail("commit generation mismatch");
s = SysWaitSingle(surface, 0, 0);
if (s != NTLSTATUS_SUCCESS) fail_status("wait committed surface", s);
s = SysWaitSingle(surface, 0, 0);
if (s != NTLSTATUS_TIMEOUT) fail_status("surface commit event did not auto-reset", s);
memset(&desc, 0, sizeof(desc));
s = SysQuerySurface(surface, &desc);
if (s != NTLSTATUS_SUCCESS) fail_status("query committed surface", s);
if (desc.ContentGeneration != 1 || desc.CommitCount != 1 || !desc.LastDamageValid)
fail("committed surface metadata mismatch");
if (desc.LastDamageBounds.X != 0 || desc.LastDamageBounds.Y != 0 ||
desc.LastDamageBounds.W != SURFACE_W || desc.LastDamageBounds.H != SURFACE_H)
fail("commit damage bounds mismatch");
s = SysUnmapSurface(readBase);
if (s != NTLSTATUS_SUCCESS) fail_status("unmap read-only surface", s);
s = SysUnmapSurface(base);
if (s != NTLSTATUS_SUCCESS) fail_status("unmap surface", s);
SysCloseHandle(readHandle);
SysCloseHandle(surface);
print("surface_smoke: PASS\n");
exit_now(0);
return 0;
}