aboutsummaryrefslogtreecommitdiff
path: root/src/fb.c
blob: f59c70005e31255aa561943a74fd9d2b0d47867f (plain)
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
#include "fb.h"

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) {
    SDL_InvalidParamError("fb");
    return false;
  }

  return pw_scale(fb->mode, fb->texture, &fb->target);
}

static uint8_t project_component(float r, float start, float size,
                                 uint8_t dim) {
  if (SDL_fabsf(size) < 1.) {
    return 0;
  }

  float dimf = (float)dim;
  float df = SDL_floorf(((r - start) * dimf) / size);
  return (uint8_t)SDL_clamp(df, 0.0, (float)(dim - 1));
}

bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
                       struct pw_coordinates *coords) {
  if (!fb) {
    SDL_InvalidParamError("fb");
    return false;
  }

  SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
  if (!renderer) {
    return false;
  }

  float rx, ry;
  if (!SDL_RenderCoordinatesFromWindow(renderer, wx, wy, &rx, &ry)) {
    return false;
  }

  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;
}

bool pw_fb_blit(struct pw_framebuffer *fb, uint8_t const pixels[PW_FB_SIZE]) {
  if (!fb) {
    SDL_InvalidParamError("fb");
    return false;
  }
  if (!pixels) {
    SDL_InvalidParamError("pixels");
    return false;
  }

  SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
  if (!renderer) {
    return false;
  }

  void *buffer;
  int pitch;
  if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch)) {
    return false;
  }

  bool ok = pitch == PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL;
  if (ok) {
    SDL_memcpy(buffer, pixels, PW_FB_SIZE);
  }

  SDL_UnlockTexture(fb->texture);
  return ok;
}

bool pw_fb_render(struct pw_framebuffer *fb) {
  if (!fb) {
    SDL_InvalidParamError("fb");
    return false;
  }

  SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
  if (!renderer) {
    return false;
  }

  return SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target);
}