Refactor away wlr_renderer_state

This commit is contained in:
Drew DeVault 2017-08-14 08:37:50 -04:00
parent 0de5eed048
commit de6f32c84e
4 changed files with 50 additions and 46 deletions

View file

@ -10,6 +10,7 @@
#include <wlr/egl.h> #include <wlr/egl.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/render.h> #include <wlr/render.h>
#include <wlr/render/interface.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
@ -21,8 +22,9 @@ struct pixel_format {
GLuint *shader; GLuint *shader;
}; };
struct wlr_renderer_state { struct wlr_gles2_renderer {
struct wlr_renderer *renderer; struct wlr_renderer wlr_renderer;
struct wlr_egl *egl; struct wlr_egl *egl;
}; };

View file

@ -6,31 +6,29 @@
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
struct wlr_renderer_impl; struct wlr_renderer_impl;
struct wlr_renderer_state;
struct wlr_renderer { struct wlr_renderer {
struct wlr_renderer_impl *impl; struct wlr_renderer_impl *impl;
struct wlr_renderer_state *state;
}; };
struct wlr_renderer_impl { struct wlr_renderer_impl {
void (*begin)(struct wlr_renderer_state *state, struct wlr_output *output); void (*begin)(struct wlr_renderer *renderer, struct wlr_output *output);
void (*end)(struct wlr_renderer_state *state); void (*end)(struct wlr_renderer *renderer);
struct wlr_texture *(*texture_init)(struct wlr_renderer_state *state); struct wlr_texture *(*texture_init)(struct wlr_renderer *renderer);
bool (*render_with_matrix)(struct wlr_renderer_state *state, bool (*render_with_matrix)(struct wlr_renderer *renderer,
struct wlr_texture *texture, const float (*matrix)[16]); struct wlr_texture *texture, const float (*matrix)[16]);
void (*render_quad)(struct wlr_renderer_state *state, void (*render_quad)(struct wlr_renderer *renderer,
const float (*color)[4], const float (*matrix)[16]); const float (*color)[4], const float (*matrix)[16]);
void (*render_ellipse)(struct wlr_renderer_state *state, void (*render_ellipse)(struct wlr_renderer *renderer,
const float (*color)[4], const float (*matrix)[16]); const float (*color)[4], const float (*matrix)[16]);
const enum wl_shm_format *(*formats)( const enum wl_shm_format *(*formats)(
struct wlr_renderer_state *state, size_t *len); struct wlr_renderer *renderer, size_t *len);
bool (*buffer_is_drm)(struct wlr_renderer_state *state, bool (*buffer_is_drm)(struct wlr_renderer *renderer,
struct wl_resource *buffer); struct wl_resource *buffer);
void (*destroy)(struct wlr_renderer_state *state); void (*destroy)(struct wlr_renderer *renderer);
}; };
struct wlr_renderer *wlr_renderer_init(struct wlr_renderer_state *state, void wlr_renderer_init(struct wlr_renderer *renderer,
struct wlr_renderer_impl *impl); struct wlr_renderer_impl *impl);
struct wlr_texture_impl { struct wlr_texture_impl {

View file

@ -121,7 +121,7 @@ static void init_globals() {
init_default_shaders(); init_default_shaders();
} }
static void wlr_gles2_begin(struct wlr_renderer_state *state, static void wlr_gles2_begin(struct wlr_renderer *_renderer,
struct wlr_output *output) { struct wlr_output *output) {
// TODO: let users customize the clear color? // TODO: let users customize the clear color?
GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1)); GL_CALL(glClearColor(0.25f, 0.25f, 0.25f, 1));
@ -138,12 +138,15 @@ static void wlr_gles2_begin(struct wlr_renderer_state *state,
// for users to sling matricies themselves // for users to sling matricies themselves
} }
static void wlr_gles2_end(struct wlr_renderer_state *state) { static void wlr_gles2_end(struct wlr_renderer *renderer) {
// no-op // no-op
} }
static struct wlr_texture *wlr_gles2_texture_init(struct wlr_renderer_state *state) { static struct wlr_texture *wlr_gles2_texture_init(
return gles2_texture_init(state->egl); struct wlr_renderer *_renderer) {
struct wlr_gles2_renderer *renderer =
(struct wlr_gles2_renderer *)_renderer;
return gles2_texture_init(renderer->egl);
} }
static void draw_quad() { static void draw_quad() {
@ -172,7 +175,7 @@ static void draw_quad() {
GL_CALL(glDisableVertexAttribArray(1)); GL_CALL(glDisableVertexAttribArray(1));
} }
static bool wlr_gles2_render_texture(struct wlr_renderer_state *state, static bool wlr_gles2_render_texture(struct wlr_renderer *_renderer,
struct wlr_texture *texture, const float (*matrix)[16]) { struct wlr_texture *texture, const float (*matrix)[16]) {
if(!texture || !texture->valid) { if(!texture || !texture->valid) {
wlr_log(L_ERROR, "attempt to render invalid texture"); wlr_log(L_ERROR, "attempt to render invalid texture");
@ -187,7 +190,7 @@ static bool wlr_gles2_render_texture(struct wlr_renderer_state *state,
return true; return true;
} }
static void wlr_gles2_render_quad(struct wlr_renderer_state *state, static void wlr_gles2_render_quad(struct wlr_renderer *renderer,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
GL_CALL(glUseProgram(shaders.quad)); GL_CALL(glUseProgram(shaders.quad));
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
@ -195,7 +198,7 @@ static void wlr_gles2_render_quad(struct wlr_renderer_state *state,
draw_quad(); draw_quad();
} }
static void wlr_gles2_render_ellipse(struct wlr_renderer_state *state, static void wlr_gles2_render_ellipse(struct wlr_renderer *renderer,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
GL_CALL(glUseProgram(shaders.ellipse)); GL_CALL(glUseProgram(shaders.ellipse));
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
@ -204,7 +207,7 @@ static void wlr_gles2_render_ellipse(struct wlr_renderer_state *state,
} }
static const enum wl_shm_format *wlr_gles2_formats( static const enum wl_shm_format *wlr_gles2_formats(
struct wlr_renderer_state *state, size_t *len) { struct wlr_renderer *renderer, size_t *len) {
static enum wl_shm_format formats[] = { static enum wl_shm_format formats[] = {
WL_SHM_FORMAT_ARGB8888, WL_SHM_FORMAT_ARGB8888,
WL_SHM_FORMAT_XRGB8888, WL_SHM_FORMAT_XRGB8888,
@ -215,14 +218,17 @@ static const enum wl_shm_format *wlr_gles2_formats(
return formats; return formats;
} }
static bool wlr_gles2_buffer_is_drm(struct wlr_renderer_state *state, static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer,
struct wl_resource *buffer) { struct wl_resource *buffer) {
struct wlr_gles2_renderer *renderer =
(struct wlr_gles2_renderer *)_renderer;
EGLint format; EGLint format;
return wlr_egl_query_buffer(state->egl, buffer, EGL_TEXTURE_FORMAT, &format); return wlr_egl_query_buffer(renderer->egl, buffer,
EGL_TEXTURE_FORMAT, &format);
} }
static void wlr_gles2_destroy(struct wlr_renderer_state *state) { static void wlr_gles2_destroy(struct wlr_renderer *renderer) {
free(state); free(renderer);
} }
static struct wlr_renderer_impl wlr_renderer_impl = { static struct wlr_renderer_impl wlr_renderer_impl = {
@ -240,9 +246,9 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend) { struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend) {
init_globals(); init_globals();
struct wlr_egl *egl = wlr_backend_get_egl(backend); struct wlr_egl *egl = wlr_backend_get_egl(backend);
struct wlr_renderer_state *state = calloc(1, sizeof(struct wlr_renderer_state)); struct wlr_gles2_renderer *renderer =
struct wlr_renderer *renderer = wlr_renderer_init(state, &wlr_renderer_impl); calloc(1, sizeof(struct wlr_gles2_renderer));
state->renderer = renderer; wlr_renderer_init(&renderer->wlr_renderer, &wlr_renderer_impl);
state->egl = egl; renderer->egl = egl;
return renderer; return &renderer->wlr_renderer;
} }

View file

@ -2,52 +2,50 @@
#include <stdbool.h> #include <stdbool.h>
#include <wlr/render/interface.h> #include <wlr/render/interface.h>
struct wlr_renderer *wlr_renderer_init(struct wlr_renderer_state *state, void wlr_renderer_init(struct wlr_renderer *renderer,
struct wlr_renderer_impl *impl) { struct wlr_renderer_impl *impl) {
struct wlr_renderer *r = calloc(sizeof(struct wlr_renderer), 1); renderer->impl = impl;
r->state = state;
r->impl = impl;
return r;
} }
void wlr_renderer_destroy(struct wlr_renderer *r) { void wlr_renderer_destroy(struct wlr_renderer *r) {
r->impl->destroy(r->state); if (r->impl->destroy) {
free(r); r->impl->destroy(r);
}
} }
void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *o) { void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *o) {
r->impl->begin(r->state, o); r->impl->begin(r, o);
} }
void wlr_renderer_end(struct wlr_renderer *r) { void wlr_renderer_end(struct wlr_renderer *r) {
r->impl->end(r->state); r->impl->end(r);
} }
struct wlr_texture *wlr_render_texture_init(struct wlr_renderer *r) { struct wlr_texture *wlr_render_texture_init(struct wlr_renderer *r) {
return r->impl->texture_init(r->state); return r->impl->texture_init(r);
} }
bool wlr_render_with_matrix(struct wlr_renderer *r, bool wlr_render_with_matrix(struct wlr_renderer *r,
struct wlr_texture *texture, const float (*matrix)[16]) { struct wlr_texture *texture, const float (*matrix)[16]) {
return r->impl->render_with_matrix(r->state, texture, matrix); return r->impl->render_with_matrix(r, texture, matrix);
} }
void wlr_render_colored_quad(struct wlr_renderer *r, void wlr_render_colored_quad(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
r->impl->render_quad(r->state, color, matrix); r->impl->render_quad(r, color, matrix);
} }
void wlr_render_colored_ellipse(struct wlr_renderer *r, void wlr_render_colored_ellipse(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]) { const float (*color)[4], const float (*matrix)[16]) {
r->impl->render_ellipse(r->state, color, matrix); r->impl->render_ellipse(r, color, matrix);
} }
const enum wl_shm_format *wlr_renderer_get_formats( const enum wl_shm_format *wlr_renderer_get_formats(
struct wlr_renderer *r, size_t *len) { struct wlr_renderer *r, size_t *len) {
return r->impl->formats(r->state, len); return r->impl->formats(r, len);
} }
bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r, bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r,
struct wl_resource *buffer) { struct wl_resource *buffer) {
return r->impl->buffer_is_drm(r->state, buffer); return r->impl->buffer_is_drm(r, buffer);
} }