#include "fb.h" static bool is_normal(float f) { return !SDL_isinff(f) && !SDL_isnanf(f); } struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer, enum pw_scale_mode mode) { if (!renderer) { SDL_InvalidParamError("renderer"); return NULL; } SDL_Texture *texture = NULL; struct pw_framebuffer *fb = NULL; // Web Assembly guarantees little-endian semantics, so the pixel data we get // will _always_ be RGBA in **byte** sequence. Use the appropriate single bit // count format, which has a stable byte order regardless of our endianness. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT); if (!texture) { goto error; } fb = SDL_malloc(sizeof(*fb)); if (!fb) { goto error; } fb->mode = mode; fb->texture = texture; SDL_zero(fb->target); if (!pw_fb_iterate(fb)) { goto error; } return fb; error: if (texture) { SDL_DestroyTexture(texture); } if (fb) { SDL_free(fb); } return NULL; } void pw_fb_free(struct pw_framebuffer *fb) { if (!fb) { SDL_InvalidParamError("fb"); return; } SDL_DestroyTexture(fb->texture); SDL_free(fb); } bool pw_fb_iterate(struct pw_framebuffer *fb) { if (!fb) { return SDL_InvalidParamError("fb"); } return pw_scale(fb->mode, fb->texture, &fb->target); } static float project_component(float r, float start, float size, float dim) { if (!(int32_t)size) { return 0.; } float f = ((r - start) * dim) / size; return SDL_clamp(f, -PW_MAX_SAFE_INT, PW_MAX_SAFE_INT); } bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy, bool relative, struct pw_coordinates *coords) { if (!fb) { return SDL_InvalidParamError("fb"); } if (!is_normal(wx)) { return SDL_InvalidParamError("wx"); } if (!is_normal(wy)) { return SDL_InvalidParamError("wy"); } if (!coords) { return SDL_InvalidParamError("coords"); } SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture); if (!renderer) { return false; } float sx, sy; if (relative) { sx = sy = 0.; } else { sx = fb->target.x; sy = fb->target.y; } float rx, ry; if (!SDL_RenderCoordinatesFromWindow(renderer, wx, wy, &rx, &ry)) { return false; } if (!is_normal(rx) || !is_normal(ry)) { return SDL_SetError("unreasonable render coordinates"); } coords->x = project_component(rx, sx, fb->target.w, PW_FB_WIDTH); coords->y = project_component(ry, sy, fb->target.h, PW_FB_HEIGHT); return true; } uint8_t *pw_fb_lock(struct pw_framebuffer *fb, size_t *pitch) { if (!fb) { SDL_InvalidParamError("fb"); return NULL; } if (!pitch) { SDL_InvalidParamError("pitch"); return NULL; } void *buffer; int ipitch; if (!SDL_LockTexture(fb->texture, NULL, &buffer, &ipitch)) { return NULL; } // This cast is necessarily safe, as it reprensents a memory offset // from one row to the other, meaning it must fit in a `size_t`. *pitch = (size_t)ipitch; return buffer; } void pw_fb_unlock(struct pw_framebuffer *fb) { if (!fb) { SDL_InvalidParamError("fb"); return; } SDL_UnlockTexture(fb->texture); } bool pw_fb_render(struct pw_framebuffer *fb) { if (!fb) { return SDL_InvalidParamError("fb"); } SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture); if (!renderer) { return false; } return SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target); }