From 3ccf1aa52dead3b91f7d767f5f4ab4fc6ec316d2 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Thu, 7 Nov 2013 23:46:29 +0100 Subject: [PATCH] fosphor: Refactor to introduce more shared state between global/cl/gl Originally I wanted the CL/GL part to be as separate as possible and have no shared data. But it's too inconvenient since a beside the CL / GL objects there is also configuration data that should be shared and distributed between the two. So instead we still have separate gl/cl state that are restricted but the function act on a shared 'struct fosphor' object that contains everything that's shared and exposed to both cl.c and gl.c Signed-off-by: Sylvain Munaut --- lib/fosphor/cl.c | 43 +++++++++++++++++---------- lib/fosphor/cl.h | 13 ++++---- lib/fosphor/fosphor.c | 30 ++++++++----------- lib/fosphor/gl.c | 46 +++++++++++++++++++---------- lib/fosphor/gl.h | 12 ++++---- lib/fosphor/main.c | 2 +- lib/fosphor/{config.h => private.h} | 25 +++++++++++----- 7 files changed, 101 insertions(+), 70 deletions(-) rename lib/fosphor/{config.h => private.h} (71%) diff --git a/lib/fosphor/cl.c b/lib/fosphor/cl.c index 7df8653..3a09fc8 100644 --- a/lib/fosphor/cl.c +++ b/lib/fosphor/cl.c @@ -44,9 +44,9 @@ # include #endif -#include "config.h" #include "cl.h" #include "gl.h" +#include "private.h" #include "resource.h" @@ -349,8 +349,9 @@ error: } static int -cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl) +cl_do_init(struct fosphor *self) { + struct fosphor_cl_state *cl = self->cl; cl_context_properties ctx_props[7]; const char *disp_opts; cl_int err; @@ -429,7 +430,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl) /* Waterfall texture */ cl->mem_waterfall = clCreateFromGLTexture2D(cl->ctx, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, - fosphor_gl_get_shared_id(gl, GL_ID_TEX_WATERFALL), + fosphor_gl_get_shared_id(self, GL_ID_TEX_WATERFALL), &err ); CL_ERR_CHECK(err, "Unable to share waterfall texture into OpenCL context"); @@ -437,7 +438,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl) /* Histogram texture */ cl->mem_histogram = clCreateFromGLTexture2D(cl->ctx, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, - fosphor_gl_get_shared_id(gl, GL_ID_TEX_HISTOGRAM), + fosphor_gl_get_shared_id(self, GL_ID_TEX_HISTOGRAM), &err ); CL_ERR_CHECK(err, "Unable to share histogram texture into OpenCL context"); @@ -445,7 +446,7 @@ cl_do_init(struct fosphor_cl_state *cl, struct fosphor_gl_state *gl) /* Spectrum VBO */ cl->mem_spectrum = clCreateFromGLBuffer(cl->ctx, CL_MEM_WRITE_ONLY, - fosphor_gl_get_shared_id(gl, GL_ID_VBO_SPECTRUM), + fosphor_gl_get_shared_id(self, GL_ID_VBO_SPECTRUM), &err ); CL_ERR_CHECK(err, "Unable to share spectrum VBO into OpenCL context"); @@ -535,8 +536,8 @@ cl_do_release(struct fosphor_cl_state *cl) /* Exposed API */ /* -------------------------------------------------------------------------- */ -struct fosphor_cl_state * -fosphor_cl_init(struct fosphor_gl_state *gl) +int +fosphor_cl_init(struct fosphor *self) { struct fosphor_cl_state *cl; char dev_name[128]; @@ -545,7 +546,9 @@ fosphor_cl_init(struct fosphor_gl_state *gl) /* Allocate structure */ cl = malloc(sizeof(struct fosphor_cl_state)); if (!cl) - return NULL; + return -ENOMEM; + + self->cl = cl; memset(cl, 0, sizeof(struct fosphor_cl_state)); @@ -562,23 +565,25 @@ fosphor_cl_init(struct fosphor_gl_state *gl) fprintf(stderr, "[+] Selected device: %s\n", dev_name); /* Initialize selected platform / device */ - err = cl_do_init(cl, gl); + err = cl_do_init(self); if (err) goto error; /* Done */ - return cl; + return 0; /* Error path */ error: - fosphor_cl_release(cl); + fosphor_cl_release(self); - return NULL; + return -EIO; } void -fosphor_cl_release(struct fosphor_cl_state *cl) +fosphor_cl_release(struct fosphor *self) { + struct fosphor_cl_state *cl = self->cl; + /* Safety */ if (!cl) return; @@ -591,9 +596,11 @@ fosphor_cl_release(struct fosphor_cl_state *cl) } int -fosphor_cl_process(struct fosphor_cl_state *cl, +fosphor_cl_process(struct fosphor *self, void *samples, int len) { + struct fosphor_cl_state *cl = self->cl; + cl_int err; size_t local[2], global[2]; int n_spectra = len / FOSPHOR_FFT_LEN; @@ -669,15 +676,19 @@ error: } int -fosphor_cl_get_waterfall_position(struct fosphor_cl_state *cl) +fosphor_cl_get_waterfall_position(struct fosphor *self) { + struct fosphor_cl_state *cl = self->cl; + return cl->waterfall_pos; } void -fosphor_cl_set_histogram_range(struct fosphor_cl_state *cl, +fosphor_cl_set_histogram_range(struct fosphor *self, float scale, float offset) { + struct fosphor_cl_state *cl = self->cl; + cl->histo_scale = scale * 128.0f; cl->histo_offset = offset; } diff --git a/lib/fosphor/cl.h b/lib/fosphor/cl.h index 6c51e47..b3e8b8c 100644 --- a/lib/fosphor/cl.h +++ b/lib/fosphor/cl.h @@ -30,17 +30,16 @@ * \brief OpenCL base routines */ -struct fosphor_gl_state; -struct fosphor_cl_state; +struct fosphor; -struct fosphor_cl_state *fosphor_cl_init(struct fosphor_gl_state *gl); -void fosphor_cl_release(struct fosphor_cl_state *cl); +int fosphor_cl_init(struct fosphor *self); +void fosphor_cl_release(struct fosphor *self); -int fosphor_cl_process(struct fosphor_cl_state *cl, +int fosphor_cl_process(struct fosphor *self, void *samples, int len); -int fosphor_cl_get_waterfall_position(struct fosphor_cl_state *cl); -void fosphor_cl_set_histogram_range(struct fosphor_cl_state *cl, +int fosphor_cl_get_waterfall_position(struct fosphor *self); +void fosphor_cl_set_histogram_range(struct fosphor *self, float scale, float offset); /*! @} */ diff --git a/lib/fosphor/fosphor.c b/lib/fosphor/fosphor.c index 4457c96..d1c03e3 100644 --- a/lib/fosphor/fosphor.c +++ b/lib/fosphor/fosphor.c @@ -32,21 +32,17 @@ #include #include -#include "config.h" #include "cl.h" #include "gl.h" #include "fosphor.h" +#include "private.h" -struct fosphor -{ - struct fosphor_cl_state *cl; - struct fosphor_gl_state *gl; -}; struct fosphor * fosphor_init(void) { struct fosphor *self; + int rv; /* Allocate structure */ self = malloc(sizeof(struct fosphor)); @@ -56,12 +52,12 @@ fosphor_init(void) memset(self, 0, sizeof(struct fosphor)); /* Init GL/CL sub-states */ - self->gl = fosphor_gl_init(); - if (!self->gl) + rv = fosphor_gl_init(self); + if (rv) goto error; - self->cl = fosphor_cl_init(self->gl); - if (!self->cl) + rv = fosphor_cl_init(self); + if (rv) goto error; /* Initial state */ @@ -81,8 +77,8 @@ fosphor_release(struct fosphor *self) if (!self) return; - fosphor_cl_release(self->cl); - fosphor_gl_release(self->gl); + fosphor_cl_release(self); + fosphor_gl_release(self); free(self); } @@ -90,14 +86,14 @@ fosphor_release(struct fosphor *self) int fosphor_process(struct fosphor *self, void *samples, int len) { - return fosphor_cl_process(self->cl, samples, len); + return fosphor_cl_process(self, samples, len); } void fosphor_draw(struct fosphor *self, int w, int h) { - int wf_pos = fosphor_cl_get_waterfall_position(self->cl); - fosphor_gl_draw(self->gl, w, h, wf_pos); + int wf_pos = fosphor_cl_get_waterfall_position(self); + fosphor_gl_draw(self, w, h, wf_pos); } @@ -116,8 +112,8 @@ fosphor_set_range(struct fosphor *self, int db_ref, int db_per_div) offset = - ( k + ((float)db0 / 20.0f) ); scale = 20.0f / (float)(db1 - db0); - fosphor_cl_set_histogram_range(self->cl, scale, offset); - fosphor_gl_set_range(self->gl, scale, offset, db_ref, db_per_div); + fosphor_cl_set_histogram_range(self, scale, offset); + fosphor_gl_set_range(self, scale, offset, db_ref, db_per_div); } /*! @} */ diff --git a/lib/fosphor/gl.c b/lib/fosphor/gl.c index 60ff138..6066500 100644 --- a/lib/fosphor/gl.c +++ b/lib/fosphor/gl.c @@ -27,17 +27,18 @@ * \brief OpenGL part of fosphor */ +#include #include #include #include #include "gl_platform.h" -#include "config.h" #include "gl.h" #include "gl_cmap.h" #include "gl_cmap_gen.h" #include "gl_font.h" +#include "private.h" #include "resource.h" @@ -122,8 +123,9 @@ gl_vbo_read(GLuint vbo_id, int size, void *dst) #endif static void -gl_deferred_init(struct fosphor_gl_state *gl) +gl_deferred_init(struct fosphor *self) { + struct fosphor_gl_state *gl = self->gl; int len; /* Prevent double init */ @@ -176,8 +178,8 @@ gl_deferred_init(struct fosphor_gl_state *gl) /* Exposed API */ /* -------------------------------------------------------------------------- */ -struct fosphor_gl_state * -fosphor_gl_init(void) +int +fosphor_gl_init(struct fosphor *self) { struct fosphor_gl_state *gl; const void *font_data; @@ -186,18 +188,24 @@ fosphor_gl_init(void) /* Allocate structure */ gl = malloc(sizeof(struct fosphor_gl_state)); if (!gl) - return NULL; + return -ENOMEM; + + self->gl = gl; memset(gl, 0, sizeof(struct fosphor_gl_state)); /* Font */ gl->font = glf_alloc(8, GLF_FLG_LCD); - if (!gl->font) + if (!gl->font) { + rv = -ENOMEM; goto error; + } font_data = resource_get("DroidSansMonoDotted.ttf", &len); - if (!font_data) + if (!font_data) { + rv = -ENOENT; goto error; + } rv = glf_load_face_mem(gl->font, font_data, len); if (rv) @@ -217,16 +225,19 @@ fosphor_gl_init(void) goto error; /* Done */ - return gl; + return 0; error: - fosphor_gl_release(gl); - return NULL; + fosphor_gl_release(self); + + return rv; } void -fosphor_gl_release(struct fosphor_gl_state *gl) +fosphor_gl_release(struct fosphor *self) { + struct fosphor_gl_state *gl = self->gl; + /* Safety */ if (!gl) return; @@ -249,12 +260,14 @@ fosphor_gl_release(struct fosphor_gl_state *gl) GLuint -fosphor_gl_get_shared_id(struct fosphor_gl_state *gl, +fosphor_gl_get_shared_id(struct fosphor *self, enum fosphor_gl_id id) { + struct fosphor_gl_state *gl = self->gl; + /* CL is not sufficiently booted to complete the GL init * in a CL context */ - gl_deferred_init(gl); + gl_deferred_init(self); /* Select ID to return */ switch (id) { @@ -273,8 +286,9 @@ fosphor_gl_get_shared_id(struct fosphor_gl_state *gl, void -fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos) +fosphor_gl_draw(struct fosphor *self, int w, int h, int wf_pos) { + struct fosphor_gl_state *gl = self->gl; float x_div_width, y_div_width; float x[2], y[4]; int i; @@ -406,10 +420,12 @@ fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos) void -fosphor_gl_set_range(struct fosphor_gl_state *gl, +fosphor_gl_set_range(struct fosphor *self, float scale, float offset, int db_ref, int db_per_div) { + struct fosphor_gl_state *gl = self->gl; + gl->scale = scale; gl->offset = offset; gl->db_ref = db_ref; diff --git a/lib/fosphor/gl.h b/lib/fosphor/gl.h index f879614..780ea2f 100644 --- a/lib/fosphor/gl.h +++ b/lib/fosphor/gl.h @@ -32,10 +32,10 @@ #include "gl_platform.h" -struct fosphor_gl_state; +struct fosphor; -struct fosphor_gl_state *fosphor_gl_init(void); -void fosphor_gl_release(struct fosphor_gl_state *gl); +int fosphor_gl_init(struct fosphor *self); +void fosphor_gl_release(struct fosphor *self); enum fosphor_gl_id { @@ -44,12 +44,12 @@ enum fosphor_gl_id { GL_ID_VBO_SPECTRUM, }; -GLuint fosphor_gl_get_shared_id(struct fosphor_gl_state *gl, +GLuint fosphor_gl_get_shared_id(struct fosphor *self, enum fosphor_gl_id id); -void fosphor_gl_draw(struct fosphor_gl_state *gl, int w, int h, int wf_pos); +void fosphor_gl_draw(struct fosphor *self, int w, int h, int wf_pos); -void fosphor_gl_set_range(struct fosphor_gl_state *gl, +void fosphor_gl_set_range(struct fosphor *self, float scale, float offset, int db_ref, int db_per_div); diff --git a/lib/fosphor/main.c b/lib/fosphor/main.c index 54061f7..40047ce 100644 --- a/lib/fosphor/main.c +++ b/lib/fosphor/main.c @@ -16,8 +16,8 @@ #endif #include -#include "config.h" #include "fosphor.h" +#include "private.h" struct app_state { diff --git a/lib/fosphor/config.h b/lib/fosphor/private.h similarity index 71% rename from lib/fosphor/config.h rename to lib/fosphor/private.h index 9a492d7..46e553e 100644 --- a/lib/fosphor/config.h +++ b/lib/fosphor/private.h @@ -1,7 +1,7 @@ /* - * config.h + * private.h * - * Global fosphor configuration constants + * Private fosphor definitions * * Copyright (C) 2013 Sylvain Munaut * @@ -19,15 +19,15 @@ * along with this program. If not, see . */ -#ifndef __FOSPHOR_CONFIG_H__ -#define __FOSPHOR_CONFIG_H__ +#ifndef __FOSPHOR_PRIVATE_H__ +#define __FOSPHOR_PRIVATE_H__ -/*! \defgroup config +/*! \defgroup private * @{ */ -/*! \file config.h - * \brief Global fosphor configuration constants +/*! \file private.h + * \brief Private fosphor definitions */ @@ -37,7 +37,16 @@ #define FOSPHOR_FFT_MULT_BATCH 16 #define FOSPHOR_FFT_MAX_BATCH 1024 +struct fosphor_cl_state; +struct fosphor_gl_state; + +struct fosphor +{ + struct fosphor_cl_state *cl; + struct fosphor_gl_state *gl; +}; + /*! @} */ -#endif /* __FOSPHOR_CONFIG_H__ */ +#endif /* __FOSPHOR_PRIVATE_H__ */