1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#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,
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 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, fb->target.x, fb->target.w, PW_FB_WIDTH);
coords->y = project_component(ry, fb->target.y, fb->target.h, PW_FB_HEIGHT);
return true;
}
uint8_t *pw_fb_lock(struct pw_framebuffer *fb) {
if (!fb) {
SDL_InvalidParamError("fb");
return NULL;
}
void *buffer;
int pitch;
if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch)) {
return NULL;
}
if (pitch != PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL) {
SDL_UnlockTexture(fb->texture);
SDL_SetError("unexpected framebuffer pitch");
return NULL;
}
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);
}
|