gr-fosphor: Split block into base function and UI specific part

It's split into:

 - A generic "base" sink that handles pretty much everything except
   managing a GL context and getting the UI interaction for it
 - The GLFW specific part

This is preparation for for WX and QT widget implementations of a
fosphor sink.

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
This commit is contained in:
Sylvain Munaut 2013-10-26 21:04:12 +02:00
parent 062040ae70
commit b056fcc1a8
10 changed files with 470 additions and 199 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0"?>
<block>
<name>fosphor sink</name>
<name>fosphor sink (GLFW)</name>
<key>fosphor_glfw_sink_c</key>
<category>Instrumentation</category>
<import>from gnuradio import fosphor</import>

View File

@ -22,6 +22,7 @@
########################################################################
install(FILES
api.h
base_sink_c.h
glfw_sink_c.h
DESTINATION include/gnuradio/fosphor
)

View File

@ -0,0 +1,57 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Sylvain Munaut <tnt@246tNt.com>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDED_GR_FOSPHOR_BASE_SINK_C_H
#define INCLUDED_GR_FOSPHOR_BASE_SINK_C_H
#include <gnuradio/fosphor/api.h>
#include <gnuradio/sync_block.h>
namespace gr {
namespace fosphor {
/*!
* \brief Base fosphor sink API interface
* \ingroup fosphor
*/
class GR_FOSPHOR_API base_sink_c : public gr::sync_block
{
protected:
base_sink_c(const char *name = NULL);
public:
enum ui_action_t {
DB_PER_DIV_UP,
DB_PER_DIV_DOWN,
REF_UP,
REF_DOWN,
};
virtual void execute_ui_action(enum ui_action_t action) = 0;
};
} // namespace fosphor
} // namespace gr
#endif /* INCLUDED_GR_FOSPHOR_BASE_SINK_C_H */

View File

@ -23,6 +23,7 @@
#define INCLUDED_GR_FOSPHOR_GLFW_SINK_C_H
#include <gnuradio/fosphor/api.h>
#include <gnuradio/fosphor/base_sink_c.h>
#include <gnuradio/sync_block.h>
@ -30,10 +31,10 @@ namespace gr {
namespace fosphor {
/*!
* \brief Main fosphor sink for GL spectrum display (GLFW version)
* \brief GLFW version of fosphor sink
* \ingroup fosphor
*/
class GR_FOSPHOR_API glfw_sink_c : virtual public gr::sync_block
class GR_FOSPHOR_API glfw_sink_c : virtual public base_sink_c
{
public:
typedef boost::shared_ptr<glfw_sink_c> sptr;

View File

@ -58,6 +58,7 @@ list(APPEND fosphor_sources
fosphor/resource.c
fosphor/resource_data.c
fifo.cc
base_sink_c_impl.cc
glfw_sink_c_impl.cc
)

232
lib/base_sink_c_impl.cc Normal file
View File

@ -0,0 +1,232 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Sylvain Munaut <tnt@246tNt.com>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <gnuradio/io_signature.h>
#include <gnuradio/thread/thread.h>
#include "fifo.h"
#include "base_sink_c_impl.h"
extern "C" {
#include "fosphor/fosphor.h"
#include "fosphor/gl_platform.h"
}
namespace gr {
namespace fosphor {
base_sink_c::base_sink_c(const char *name)
: gr::sync_block(name,
gr::io_signature::make(1, 1, sizeof(gr_complex)),
gr::io_signature::make(0, 0, 0))
{
/* Nothing to do but super call */
}
const int base_sink_c_impl::k_db_per_div[] = {1, 2, 5, 10, 20};
base_sink_c_impl::base_sink_c_impl()
: d_db_ref(0), d_db_per_div_idx(3), d_active(false)
{
/* Init FIFO */
this->d_fifo = new fifo(1024 * 1024);
}
base_sink_c_impl::~base_sink_c_impl()
{
delete this->d_fifo;
}
void base_sink_c_impl::worker()
{
/* Init GL context */
this->glctx_init();
/* Init fosphor */
this->d_fosphor = fosphor_init();
if (!this->d_fosphor)
return;
fosphor_set_range(this->d_fosphor,
this->d_db_ref,
this->k_db_per_div[this->d_db_per_div_idx]
);
/* Main loop */
while (this->d_active)
{
this->render();
this->glctx_poll();
}
/* Cleanup fosphor */
fosphor_release(this->d_fosphor);
/* And GL context */
this->glctx_fini();
}
void base_sink_c_impl::_worker(base_sink_c_impl *obj)
{
obj->worker();
}
void
base_sink_c_impl::render(void)
{
/* Handle pending reshape */
if (this->d_reshaped)
{
this->d_reshaped = false;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (double)this->d_width, 0.0, (double)this->d_height, -1.0, 1.0);
glViewport(0, 0, this->d_width, this->d_height);
}
/* Clear everything */
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear(GL_COLOR_BUFFER_BIT);
/* Flush fifo */
while (this->d_fifo->used() > 128*1024) {
gr_complex *data = this->d_fifo->read_peek(128*1024, false);
fosphor_process(this->d_fosphor, data, 128*1024);
this->d_fifo->read_discard(128*1024);
}
/* Draw */
fosphor_draw(this->d_fosphor, this->d_width, this->d_height);
/* Done, swap buffer */
this->glctx_swap();
}
void
base_sink_c_impl::cb_reshape(int width, int height)
{
this->d_width = width;
this->d_height = height;
this->d_reshaped = true;
}
void
base_sink_c_impl::execute_ui_action(enum ui_action_t action)
{
switch (action) {
case DB_PER_DIV_UP:
if (this->d_db_per_div_idx < 4)
this->d_db_per_div_idx++;
break;
case DB_PER_DIV_DOWN:
if (this->d_db_per_div_idx > 0)
this->d_db_per_div_idx--;
break;
case REF_UP:
this->d_db_ref += k_db_per_div[this->d_db_per_div_idx];
break;
case REF_DOWN:
this->d_db_ref -= k_db_per_div[this->d_db_per_div_idx];
break;
}
fosphor_set_range(this->d_fosphor,
this->d_db_ref,
this->k_db_per_div[this->d_db_per_div_idx]
);
}
int
base_sink_c_impl::work(
int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const gr_complex *in = (const gr_complex *) input_items[0];
gr_complex *dst;
int l, mw;
/* How much can we hope to write */
l = noutput_items;
mw = this->d_fifo->write_max_size();
if (l > mw)
l = mw;
if (!l)
return 0;
/* Get a pointer */
dst = this->d_fifo->write_prepare(l, true);
if (!dst)
return 0;
/* Do the copy */
memcpy(dst, in, sizeof(gr_complex) * l);
this->d_fifo->write_commit(l);
/* Report what we took */
return l;
}
bool base_sink_c_impl::start()
{
bool rv = base_sink_c::start();
if (!this->d_active) {
this->d_active = true;
this->d_worker = gr::thread::thread(_worker, this);
}
return rv;
}
bool base_sink_c_impl::stop()
{
bool rv = base_sink_c::stop();
if (this->d_active) {
this->d_active = false;
this->d_worker.join();
}
return rv;
}
} /* namespace fosphor */
} /* namespace gr */

96
lib/base_sink_c_impl.h Normal file
View File

@ -0,0 +1,96 @@
/* -*- c++ -*- */
/*
* Copyright 2013 Sylvain Munaut <tnt@246tNt.com>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDED_GR_FOSPHOR_BASE_SINK_C_IMPL_H
#define INCLUDED_GR_FOSPHOR_BASE_SINK_C_IMPL_H
#include <gnuradio/thread/thread.h>
#include <gnuradio/fosphor/base_sink_c.h>
struct fosphor;
namespace gr {
namespace fosphor {
class fifo;
/*!
* \brief Base class for fosphor sink implementation
* \ingroup fosphor
*/
class base_sink_c_impl : virtual public base_sink_c
{
private:
/* Worker thread */
gr::thread::thread d_worker;
bool d_active;
void worker();
static void _worker(base_sink_c_impl *obj);
/* fosphor core */
fifo *d_fifo;
struct fosphor *d_fosphor;
void render();
int d_width;
int d_height;
bool d_reshaped;
static const int k_db_per_div[];
int d_db_ref;
int d_db_per_div_idx;
protected:
base_sink_c_impl();
/* Delegated implementation of GL context management */
virtual void glctx_init() = 0;
virtual void glctx_poll() = 0;
virtual void glctx_swap() = 0;
virtual void glctx_fini() = 0;
/* Callbacks from GL window */
void cb_reshape(int width, int height);
public:
virtual ~base_sink_c_impl();
/* gr::fosphor::base_sink_c implementation */
void execute_ui_action(enum ui_action_t action);
/* gr::sync_block implementation */
int work (int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
bool start();
bool stop();
};
} // namespace fosphor
} // namespace gr
#endif /* INCLUDED_GR_FOSPHOR_BASE_SINK_C_IMPL_H */

View File

@ -23,21 +23,10 @@
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <gnuradio/io_signature.h>
#include <gnuradio/thread/thread.h>
#include <GLFW/glfw3.h>
#include "fifo.h"
#include "glfw_sink_c_impl.h"
extern "C" {
#include "fosphor/fosphor.h"
}
namespace gr {
namespace fosphor {
@ -48,64 +37,69 @@ glfw_sink_c::make()
return gnuradio::get_initial_sptr(new glfw_sink_c_impl());
}
const int glfw_sink_c_impl::k_db_per_div[] = {1, 2, 5, 10, 20};
glfw_sink_c_impl::glfw_sink_c_impl()
: gr::sync_block("glfw_sink_c",
gr::io_signature::make(1, 1, sizeof(gr_complex)),
gr::io_signature::make(0, 0, 0)),
d_db_ref(0), d_db_per_div_idx(3)
: base_sink_c("glfw_sink_c")
{
/* Init FIFO */
this->d_fifo = new fifo(1024 * 1024);
/* Start worker */
this->d_active = true;
this->d_worker = gr::thread::thread(_worker, this);
}
glfw_sink_c_impl::~glfw_sink_c_impl()
{
this->d_active = false;
this->d_worker.join();
delete this->d_fifo;
}
int
glfw_sink_c_impl::work(
int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const gr_complex *in = (const gr_complex *) input_items[0];
gr_complex *dst;
int l, mw;
/* How much can we hope to write */
l = noutput_items;
mw = this->d_fifo->write_max_size();
if (l > mw)
l = mw;
if (!l)
return 0;
/* Get a pointer */
dst = this->d_fifo->write_prepare(l, true);
if (!dst)
return 0;
/* Do the copy */
memcpy(dst, in, sizeof(gr_complex) * l);
this->d_fifo->write_commit(l);
/* Report what we took */
return l;
/* Nothing to do but super call */
}
void glfw_sink_c_impl::worker()
void
glfw_sink_c_impl::glfw_cb_reshape(int w, int h)
{
if (w < 0 || h < 0)
glfwGetFramebufferSize(this->d_window, &w, &h);
this->cb_reshape(w, h);
}
void
glfw_sink_c_impl::glfw_cb_key(int key, int scancode, int action, int mods)
{
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_ESCAPE:
exit(0);
break;
case GLFW_KEY_UP:
this->base_sink_c_impl::execute_ui_action(REF_DOWN);
break;
case GLFW_KEY_DOWN:
this->base_sink_c_impl::execute_ui_action(REF_UP);
break;
case GLFW_KEY_LEFT:
this->base_sink_c_impl::execute_ui_action(DB_PER_DIV_DOWN);
break;
case GLFW_KEY_RIGHT:
this->base_sink_c_impl::execute_ui_action(DB_PER_DIV_UP);
break;
}
}
void
glfw_sink_c_impl::_glfw_cb_reshape(GLFWwindow *wnd, int w, int h)
{
glfw_sink_c_impl *sink = (glfw_sink_c_impl *) glfwGetWindowUserPointer(wnd);
sink->glfw_cb_reshape(w, h);
}
void
glfw_sink_c_impl::_glfw_cb_key(GLFWwindow *wnd, int key, int scancode, int action, int mods)
{
glfw_sink_c_impl *sink = (glfw_sink_c_impl *) glfwGetWindowUserPointer(wnd);
sink->glfw_cb_key(key, scancode, action, mods);
}
void
glfw_sink_c_impl::glctx_init()
{
GLFWwindow *wnd;
@ -128,128 +122,27 @@ void glfw_sink_c_impl::worker()
/* Force first reshape */
this->glfw_cb_reshape(-1, -1);
/* Init fosphor */
this->d_fosphor = fosphor_init();
if (!this->d_fosphor)
return;
fosphor_set_range(this->d_fosphor,
this->d_db_ref,
this->k_db_per_div[this->d_db_per_div_idx]
);
/* Main loop */
while (!glfwWindowShouldClose(wnd) && this->d_active)
{
this->glfw_render();
glfwPollEvents();
}
/* Cleanup fosphor */
fosphor_release(this->d_fosphor);
/* And GLFW */
glfwDestroyWindow(wnd);
glfwTerminate();
}
void glfw_sink_c_impl::_worker(glfw_sink_c_impl *obj)
{
obj->worker();
}
void
glfw_sink_c_impl::glfw_render(void)
glfw_sink_c_impl::glctx_swap()
{
/* Clear everything */
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear(GL_COLOR_BUFFER_BIT);
/* Flush fifo */
while (this->d_fifo->used() > 128*1024) {
gr_complex *data = this->d_fifo->read_peek(128*1024, false);
fosphor_process(this->d_fosphor, data, 128*1024);
this->d_fifo->read_discard(128*1024);
}
/* Draw */
fosphor_draw(this->d_fosphor, this->d_width, this->d_height);
/* Done, swap buffer */
glfwSwapBuffers(this->d_window);
}
void
glfw_sink_c_impl::glfw_cb_reshape(int w, int h)
glfw_sink_c_impl::glctx_poll()
{
if (w < 0 || h < 0)
glfwGetFramebufferSize(this->d_window, &w, &h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (double)w, 0.0, (double)h, -1.0, 1.0);
glViewport(0, 0, w, h);
this->d_width = w;
this->d_height = h;
glfwPollEvents();
}
void
glfw_sink_c_impl::glfw_cb_key(int key, int scancode, int action, int mods)
glfw_sink_c_impl::glctx_fini()
{
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_ESCAPE:
exit(0);
break;
case GLFW_KEY_UP:
this->d_db_ref -= k_db_per_div[this->d_db_per_div_idx];
break;
case GLFW_KEY_DOWN:
this->d_db_ref += k_db_per_div[this->d_db_per_div_idx];
break;
case GLFW_KEY_LEFT:
if (this->d_db_per_div_idx > 0)
this->d_db_per_div_idx--;
break;
case GLFW_KEY_RIGHT:
if (this->d_db_per_div_idx < 4)
this->d_db_per_div_idx++;
break;
}
fosphor_set_range(this->d_fosphor,
this->d_db_ref,
this->k_db_per_div[this->d_db_per_div_idx]
);
glfwDestroyWindow(this->d_window);
glfwTerminate();
}
void
glfw_sink_c_impl::_glfw_cb_reshape(GLFWwindow *wnd, int w, int h)
{
glfw_sink_c_impl *sink = (glfw_sink_c_impl *) glfwGetWindowUserPointer(wnd);
sink->glfw_cb_reshape(w, h);
}
void
glfw_sink_c_impl::_glfw_cb_key(GLFWwindow *wnd, int key, int scancode, int action, int mods)
{
glfw_sink_c_impl *sink = (glfw_sink_c_impl *) glfwGetWindowUserPointer(wnd);
sink->glfw_cb_key(key, scancode, action, mods);
}
} /* namespace fosphor */
} /* namespace gr */

View File

@ -22,27 +22,24 @@
#ifndef INCLUDED_GR_FOSPHOR_GLFW_SINK_C_IMPL_H
#define INCLUDED_GR_FOSPHOR_GLFW_SINK_C_IMPL_H
#include <gnuradio/thread/thread.h>
#include <gnuradio/fosphor/glfw_sink_c.h>
struct fosphor;
#include "base_sink_c_impl.h"
struct GLFWwindow;
namespace gr {
namespace fosphor {
class fifo;
/*!
* \brief Main fosphor sink for GL spectrum display
* \brief GLFW version of fosphor sink (implementation)
* \ingroup fosphor
*/
class glfw_sink_c_impl : public glfw_sink_c
class glfw_sink_c_impl : public glfw_sink_c, public base_sink_c_impl
{
private:
void worker();
static void _worker(glfw_sink_c_impl *obj);
/* GLFW stuff */
GLFWwindow *d_window;
void glfw_render(void);
void glfw_cb_reshape(int w, int h);
@ -51,25 +48,15 @@ namespace gr {
static void _glfw_cb_reshape(GLFWwindow *wnd, int w, int h);
static void _glfw_cb_key(GLFWwindow *wnd, int key, int scancode, int action, int mods);
gr::thread::thread d_worker;
bool d_active;
GLFWwindow *d_window;
fifo *d_fifo;
struct fosphor *d_fosphor;
int d_width, d_height;
int d_db_ref, d_db_per_div_idx;
static const int k_db_per_div[];
protected:
/* Delegated implementation of GL context management */
void glctx_init();
void glctx_swap();
void glctx_poll();
void glctx_fini();
public:
glfw_sink_c_impl();
virtual ~glfw_sink_c_impl();
int work (int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};
} // namespace fosphor

View File

@ -12,5 +12,8 @@
%}
%include "gnuradio/fosphor/base_sink_c.h"
%nodefaultctor gr::fosphor::glfw_sink_c; // bug workaround
%include "gnuradio/fosphor/glfw_sink_c.h"
GR_SWIG_BLOCK_MAGIC2(fosphor, glfw_sink_c);