Fix wrong threaded usage of OpenGL context.

This fixes crashes on all platform when rendering to the screen.
This commit is contained in:
Yuri Kunde Schlesner 2014-08-23 18:08:09 -03:00
parent fab2f28ea5
commit 3c150c4bdb
4 changed files with 11 additions and 10 deletions

View file

@ -79,15 +79,11 @@ class GGLWidgetInternal : public QGLWidget
public: public:
GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) : QGLWidget(parent) GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) : QGLWidget(parent)
{ {
doneCurrent();
parent_ = parent; parent_ = parent;
} }
void paintEvent(QPaintEvent* ev) void paintEvent(QPaintEvent* ev)
{ {
// Apparently, Windows doesn't display anything if we don't call this here.
// TODO: Breaks linux though because we aren't calling doneCurrent() ... -.-
// makeCurrent();
} }
void resizeEvent(QResizeEvent* ev) { void resizeEvent(QResizeEvent* ev) {
parent_->SetClientAreaWidth(size().width()); parent_->SetClientAreaWidth(size().width());
@ -97,6 +93,12 @@ private:
GRenderWindow* parent_; GRenderWindow* parent_;
}; };
void GRenderWindow::MoveContextToEmuThread() {
// This isn't required in Qt4 and moveToThread doesn't exist there
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
child->context()->moveToThread(&emu_thread);
#endif
}
EmuThread& GRenderWindow::GetEmuThread() EmuThread& GRenderWindow::GetEmuThread()
{ {
@ -129,7 +131,6 @@ GRenderWindow::~GRenderWindow()
void GRenderWindow::SwapBuffers() void GRenderWindow::SwapBuffers()
{ {
child->makeCurrent(); // TODO: Not necessary?
child->swapBuffers(); child->swapBuffers();
} }

View file

@ -98,6 +98,7 @@ public:
void restoreGeometry(const QByteArray& geometry); // overridden void restoreGeometry(const QByteArray& geometry); // overridden
QByteArray saveGeometry(); // overridden QByteArray saveGeometry(); // overridden
void MoveContextToEmuThread();
EmuThread& GetEmuThread(); EmuThread& GetEmuThread();
void keyPressEvent(QKeyEvent* event); void keyPressEvent(QKeyEvent* event);

View file

@ -142,7 +142,8 @@ void GMainWindow::BootGame(std::string filename)
registersWidget->OnCPUStepped(); registersWidget->OnCPUStepped();
callstackWidget->OnCPUStepped(); callstackWidget->OnCPUStepped();
render_window->DoneCurrent(); // make sure EmuThread can access GL context render_window->DoneCurrent();
render_window->MoveContextToEmuThread();
render_window->GetEmuThread().SetFilename(filename); render_window->GetEmuThread().SetFilename(filename);
render_window->GetEmuThread().start(); render_window->GetEmuThread().start();
@ -204,7 +205,6 @@ void GMainWindow::ToggleWindowMode()
ui.horizontalLayout->removeWidget(render_window); ui.horizontalLayout->removeWidget(render_window);
render_window->setParent(NULL); render_window->setParent(NULL);
render_window->setVisible(true); render_window->setVisible(true);
render_window->DoneCurrent();
render_window->RestoreGeometry(); render_window->RestoreGeometry();
} }
else if (!enable && render_window->parent() == NULL) else if (!enable && render_window->parent() == NULL)
@ -212,7 +212,6 @@ void GMainWindow::ToggleWindowMode()
render_window->BackupGeometry(); render_window->BackupGeometry();
ui.horizontalLayout->addWidget(render_window); ui.horizontalLayout->addWidget(render_window);
render_window->setVisible(true); render_window->setVisible(true);
render_window->DoneCurrent();
} }
} }

View file

@ -36,7 +36,7 @@ void Init(EmuWindow* emu_window) {
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
g_emu_window = emu_window; g_emu_window = emu_window;
g_emu_window->MakeCurrent();
g_renderer = new RendererOpenGL(); g_renderer = new RendererOpenGL();
g_renderer->SetWindow(g_emu_window); g_renderer->SetWindow(g_emu_window);
g_renderer->Init(); g_renderer->Init();