Move Frame Uploader to Thread

This commit is contained in:
Florian Märkl 2019-08-05 14:18:20 +02:00
parent 3358759aab
commit 679af22f6d
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
4 changed files with 37 additions and 7 deletions

View file

@ -23,6 +23,7 @@
class AVOpenGLWidget; class AVOpenGLWidget;
class VideoDecoder; class VideoDecoder;
class QSurface;
class AVOpenGLFrameUploader: public QObject class AVOpenGLFrameUploader: public QObject
{ {
@ -31,12 +32,14 @@ class AVOpenGLFrameUploader: public QObject
private: private:
VideoDecoder *decoder; VideoDecoder *decoder;
AVOpenGLWidget *widget; AVOpenGLWidget *widget;
QOpenGLContext *context;
QSurface *surface;
private slots: private slots:
void UpdateFrame(); void UpdateFrame();
public: public:
AVOpenGLFrameUploader(VideoDecoder *decoder, AVOpenGLWidget *widget); AVOpenGLFrameUploader(VideoDecoder *decoder, AVOpenGLWidget *widget, QOpenGLContext *context, QSurface *surface);
}; };
#endif // CHIAKI_AVOPENGLFRAMEUPLOADER_H #endif // CHIAKI_AVOPENGLFRAMEUPLOADER_H

View file

@ -52,7 +52,9 @@ class AVOpenGLWidget: public QOpenGLWidget
AVOpenGLFrame frames[2]; AVOpenGLFrame frames[2];
int frame_fg; int frame_fg;
QMutex frames_mutex; QMutex frames_mutex;
QOpenGLContext *frame_uploader_context;
AVOpenGLFrameUploader *frame_uploader; AVOpenGLFrameUploader *frame_uploader;
QThread *frame_uploader_thread;
public: public:
explicit AVOpenGLWidget(VideoDecoder *decoder, QWidget *parent = nullptr); explicit AVOpenGLWidget(VideoDecoder *decoder, QWidget *parent = nullptr);

View file

@ -22,17 +22,21 @@
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
AVOpenGLFrameUploader::AVOpenGLFrameUploader(VideoDecoder *decoder, AVOpenGLWidget *widget) AVOpenGLFrameUploader::AVOpenGLFrameUploader(VideoDecoder *decoder, AVOpenGLWidget *widget, QOpenGLContext *context, QSurface *surface)
: QObject(nullptr), : QObject(nullptr),
decoder(decoder), decoder(decoder),
widget(widget) widget(widget),
context(context),
surface(surface)
{ {
connect(decoder, SIGNAL(FramesAvailable()), this, SLOT(UpdateFrame())); connect(decoder, SIGNAL(FramesAvailable()), this, SLOT(UpdateFrame()));
} }
void AVOpenGLFrameUploader::UpdateFrame() void AVOpenGLFrameUploader::UpdateFrame()
{ {
if(QOpenGLContext::currentContext() != context)
context->makeCurrent(surface);
AVFrame *next_frame = decoder->PullFrame(); AVFrame *next_frame = decoder->PullFrame();
if(!next_frame) if(!next_frame)
return; return;

View file

@ -22,6 +22,7 @@
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QOpenGLExtraFunctions> #include <QOpenGLExtraFunctions>
#include <QOpenGLDebugLogger> #include <QOpenGLDebugLogger>
#include <QThread>
//#define DEBUG_OPENGL //#define DEBUG_OPENGL
@ -85,12 +86,18 @@ AVOpenGLWidget::AVOpenGLWidget(VideoDecoder *decoder, QWidget *parent)
#endif #endif
setFormat(format); setFormat(format);
frame_uploader_context = nullptr;
frame_uploader = nullptr; frame_uploader = nullptr;
frame_uploader_thread = nullptr;
} }
AVOpenGLWidget::~AVOpenGLWidget() AVOpenGLWidget::~AVOpenGLWidget()
{ {
frame_uploader_thread->quit();
frame_uploader_thread->wait();
delete frame_uploader_thread;
delete frame_uploader; delete frame_uploader;
delete frame_uploader_context;
} }
void AVOpenGLWidget::SwapFrames() void AVOpenGLWidget::SwapFrames()
@ -127,7 +134,7 @@ bool AVOpenGLFrame::Update(AVFrame *frame)
f->glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[i]); f->glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, frame->data[i]);
} }
f->glFinish(); // TODO: fence f->glFinish();
return true; return true;
} }
@ -210,8 +217,22 @@ void AVOpenGLWidget::initializeGL()
f->glEnable(GL_CULL_FACE); f->glEnable(GL_CULL_FACE);
f->glClearColor(0.0, 0.0, 0.0, 1.0); f->glClearColor(0.0, 0.0, 0.0, 1.0);
frame_uploader = new AVOpenGLFrameUploader(decoder, this); frame_uploader_context = new QOpenGLContext(nullptr);
frame_uploader_context->setFormat(context()->format());
frame_uploader_context->setShareContext(context());
if(!frame_uploader_context->create())
{
printf("Failed to create upload context!\n"); // TODO: log to somewhere else
return;
}
frame_uploader = new AVOpenGLFrameUploader(decoder, this, frame_uploader_context, context()->surface());
frame_fg = 0; frame_fg = 0;
frame_uploader_thread = new QThread(this);
frame_uploader_context->moveToThread(frame_uploader_thread);
frame_uploader->moveToThread(frame_uploader_thread);
frame_uploader_thread->start();
} }
void AVOpenGLWidget::resizeGL(int w, int h) void AVOpenGLWidget::resizeGL(int w, int h)
@ -258,5 +279,5 @@ void AVOpenGLWidget::paintGL()
f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
f->glFinish(); // TODO: fence f->glFinish();
} }