Skip to content

Commit

Permalink
Removed redundant invoke-in-main-thread code in opengl-grabber.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdyk committed Jun 6, 2014
1 parent 2f490cc commit c5f6f71
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 148 deletions.
11 changes: 4 additions & 7 deletions include/tinia/qtcontroller/impl/ServerThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ namespace impl {
class ServerThread : public QRunnable
{
public:
explicit ServerThread(OpenGLServerGrabber& grabber,
explicit ServerThread(OpenGLServerGrabber* grabber,
Invoker* mainthread_invoker,
tinia::jobcontroller::Job* job,
int socket,
QObject *parent = 0);
int socket );

void run();

private:


bool isLongPoll(const QString& request);
void getSnapshotTxt(QTextStream& os, const QString& request);

/** Handles non-static content, if applicable.
* @returns true if the file is non-static, false otherwise.
Expand All @@ -44,9 +42,8 @@ class ServerThread : public QRunnable
int m_socket;

tinia::model::impl::xml::XMLHandler m_xmlHandler;
tinia::jobcontroller::Job* m_job;

OpenGLServerGrabber& m_grabber;
tinia::jobcontroller::Job* m_job;
OpenGLServerGrabber* m_grabber;
Invoker* m_mainthread_invoker;
};

Expand Down
44 changes: 11 additions & 33 deletions include/tinia/qtcontroller/moc/OpenGLServerGrabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ namespace tinia {
namespace qtcontroller {
namespace impl {

class OpenGLServerGrabber : public QObject, public ImageSource
class OpenGLServerGrabber : public QObject
{
Q_OBJECT
public:
explicit OpenGLServerGrabber(tinia::jobcontroller::Job* job,
QObject *parent = 0);
explicit OpenGLServerGrabber( QObject *parent );

~OpenGLServerGrabber();

void getImageAsText(QTextStream& os, unsigned int width, unsigned int height, QString key);

/** Mutex that governs exclusive access to this object. */
QMutex*
exclusiveAccessMutex()
Expand All @@ -42,44 +39,25 @@ class OpenGLServerGrabber : public QObject, public ImageSource
* \note \ref exclusiveAccessMutex must be held before invocation.
*/
void
grab(tinia::jobcontroller::OpenGLJob* job,
grab( tinia::jobcontroller::OpenGLJob* job,
unsigned int width,
unsigned int height,
const std::string &key );


signals:
void glImageReady();
void getGLImage(unsigned int width, unsigned int height, QString key);

private slots:
void getImage(unsigned int width, unsigned int height, QString key);
void wakeListeners();

private:
void setupOpenGL();
void resize(unsigned int width, unsigned int height);
bool m_glImageIsReady;

// We only want to grab one image at the time
QMutex m_mainMutex;

// This is for waiting. We let the event-loop take the image, and wait
// for it to finish.
QMutex m_waitMutex;
QWaitCondition m_waitCondition;

tinia::jobcontroller::Job* m_job;
unsigned char* m_buffer;
QMutex m_mainMutex;
unsigned char* m_buffer;
size_t m_buffer_size;
QString m_renderlist_update_xml;
bool m_openglIsReady;
unsigned int m_fbo;
unsigned int m_renderbufferRGBA;
unsigned int m_renderbufferDepth;

unsigned int m_width;
unsigned int m_height;
bool m_openglIsReady;
unsigned int m_fbo;
unsigned int m_renderbufferRGBA;
unsigned int m_renderbufferDepth;
unsigned int m_width;
unsigned int m_height;
};

} // namespace impl
Expand Down
7 changes: 5 additions & 2 deletions src/qtcontroller/HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ HTTPServer::HTTPServer( tinia::jobcontroller::Job* job,
QObject *parent)
: QTcpServer(parent),
m_job(job),
m_serverGrabber( new OpenGLServerGrabber(m_job, this ) ),
m_serverGrabber( new OpenGLServerGrabber( this ) ),
m_mainthread_invoker( new Invoker( this ) )
{
listen(QHostAddress::Any, 8080);
}

void HTTPServer::incomingConnection(int socket)
{
ServerThread* thread = new ServerThread(*m_serverGrabber, m_mainthread_invoker, m_job, socket);
ServerThread* thread = new ServerThread( m_serverGrabber,
m_mainthread_invoker,
m_job,
socket );

//connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

Expand Down
99 changes: 10 additions & 89 deletions src/qtcontroller/OpenGLServerGrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ namespace tinia {
namespace qtcontroller {
namespace impl {

OpenGLServerGrabber::OpenGLServerGrabber(tinia::jobcontroller::Job* job,
QObject *parent) :
QObject(parent), m_glImageIsReady(false), m_job(job), m_buffer( NULL ), m_buffer_size(0), m_openglIsReady(false),
m_width(500), m_height(500)
OpenGLServerGrabber::OpenGLServerGrabber( QObject *parent )
: QObject(parent),
m_buffer( NULL ),
m_buffer_size(0),
m_openglIsReady(false),
m_width(500),
m_height(500)
{
connect(this, SIGNAL(glImageReady()), this,
SLOT(wakeListeners()));

connect(this, SIGNAL(getGLImage(uint,uint,QString)), this,
SLOT(getImage(uint,uint,QString)));
}

OpenGLServerGrabber::~OpenGLServerGrabber()
Expand All @@ -30,43 +28,15 @@ OpenGLServerGrabber::~OpenGLServerGrabber()
}
}

void OpenGLServerGrabber::getImageAsText(QTextStream &os, unsigned int width, unsigned int height, QString key)
{
m_mainMutex.lock();
emit getGLImage(width, height, key);
m_waitMutex.lock();
while(!m_glImageIsReady) {
m_waitCondition.wait(&m_waitMutex);
}

QImage img(m_buffer, width, height, QImage::Format_RGB888);

// This is a temporary fix. The image is reflected through the horizontal
// line y=height ((x, y) |--> (x, h-y) ).
QTransform flipTransformation(1, 0,
0, -1,
0, height);
img = img.transformed(flipTransformation);


QBuffer qBuffer;

img.save(&qBuffer, "png");
os << httpHeader(getMimeType("file.txt"));
QString str(QByteArray(qBuffer.data(), int(qBuffer.size())).toBase64());
os << "\r\n"<<str;

m_glImageIsReady = false;
m_waitMutex.unlock();
m_mainMutex.unlock();
}

void
OpenGLServerGrabber::grab( jobcontroller::OpenGLJob *job,
unsigned int width,
unsigned int height,
const std::string& key )
{
if( !m_openglIsReady ) {
setupOpenGL();
}
if(m_width != width || m_height != height) {
resize(width, height);
}
Expand Down Expand Up @@ -95,57 +65,8 @@ OpenGLServerGrabber::grab( jobcontroller::OpenGLJob *job,
}


void OpenGLServerGrabber::getImage(unsigned int width, unsigned int height, QString key)
{
tinia::jobcontroller::OpenGLJob* openGLJob = static_cast<tinia::jobcontroller::OpenGLJob*>(m_job);
if(!openGLJob) {
throw std::invalid_argument("This is not an OpenGL job!");
}
if(!m_openglIsReady) {
setupOpenGL();
}

glBindFramebuffer( GL_FRAMEBUFFER, m_fbo );

if(m_width != width || m_height != height) {
resize(width, height);
}

glViewport( 0, 0, width, height );



openGLJob->renderFrame( "session", key.toStdString(), m_fbo, width, height );

// QImage requires scanline size to be a multiple of 32 bits.
size_t scanline_size = 4*((3*width+3)/4);
glPixelStorei( GL_PACK_ALIGNMENT, 4 );

// make sure that buffer is large enough to hold raw image
size_t req_buffer_size = scanline_size*height*3;
if( (m_buffer == NULL) || (m_buffer_size < req_buffer_size) ) {
if( m_buffer != NULL ) {
delete m_buffer;
}
m_buffer_size = req_buffer_size;
m_buffer = new unsigned char[m_buffer_size];
}

glBindFramebuffer( GL_FRAMEBUFFER, m_fbo );
glReadPixels( 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, m_buffer );
glBindFramebuffer(GL_FRAMEBUFFER, 0);

emit glImageReady();
}

void OpenGLServerGrabber::wakeListeners()
{
m_waitMutex.lock();
m_glImageIsReady = true;
m_waitCondition.wakeAll();
m_waitMutex.unlock();
}

void OpenGLServerGrabber::setupOpenGL()
{
glewInit();
Expand Down
20 changes: 3 additions & 17 deletions src/qtcontroller/ServerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ namespace tinia {
namespace qtcontroller {
namespace impl {

ServerThread::ServerThread(OpenGLServerGrabber &grabber,
ServerThread::ServerThread(OpenGLServerGrabber* grabber,
Invoker* mainthread_invoker,
tinia::jobcontroller::Job* job,
int socket,
QObject *parent) :
int socket ) :
m_socket(socket),
m_xmlHandler(job->getExposedModel()),
m_job(job),
Expand Down Expand Up @@ -212,28 +211,15 @@ bool ServerThread::isLongPoll(const QString &request)
}


void ServerThread::getSnapshotTxt(QTextStream &os, const QString &request)
{
boost::tuple<unsigned int, unsigned int,
std::string> arguments =
parseGet<boost::tuple<unsigned int, unsigned int,
std::string> >(decodeGetParameters(request), "width height key");

unsigned int width = arguments.get<0>();
unsigned int height = arguments.get<1>();
std::string key = arguments.get<2>();

m_grabber.getImageAsText(os, width, height, QString(key.c_str()));
}
bool ServerThread::handleNonStatic(QTextStream &os, const QString& file,
const QString& request)
{
try {
if(file == "/snapshot.txt") {
updateState(os, request);
SnapshotAsTextFetcher f( os, request, m_job, &m_grabber );
SnapshotAsTextFetcher f( os, request, m_job, m_grabber );
m_mainthread_invoker->invokeInMainThread( &f, true );
//getSnapshotTxt(os, request);
return true;
}
else if(file == "/getRenderList.xml") {
Expand Down

0 comments on commit c5f6f71

Please sign in to comment.