Fix Discord streaming for OpenGL

Check if the OpenGL context is valid on every operation, and if not
re-initialize it. Also rebind the textures every time the frame is
drawn.

Also rename the SetCurrent method to SetContext, which is clearer.

Fix #643
Fix #767
Fix #840
Fix #843

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Squall-Leonhart 2023-06-29 09:25:37 +10:00 committed by Rafael Kitover
parent 24b92462f9
commit 7561ca97c1
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
2 changed files with 37 additions and 14 deletions

View File

@ -38,10 +38,11 @@ protected:
void DrawArea(wxWindowDC& dc);
void OnSize(wxSizeEvent& ev);
void AdjustViewport();
void RefreshGL();
#ifndef wxGL_IMPLICIT_CONTEXT
wxGLContext* ctx;
wxGLContext* ctx = nullptr;
#endif
bool SetCurrent();
bool SetContext();
void DrawingPanelInit();
GLuint texid, vlist;
int texsize;

View File

@ -2183,9 +2183,26 @@ static int glopts[] = {
WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0
};
bool GLDrawingPanel::SetCurrent()
bool GLDrawingPanel::SetContext()
{
#ifndef wxGL_IMPLICIT_CONTEXT
// Check if the current context is valid
if (!ctx
#if wxCHECK_VERSION(3, 1, 0)
|| !ctx->IsOK()
#endif
)
{
// Delete the old context
if (ctx) {
delete ctx;
ctx = nullptr;
}
// Create a new context
ctx = new wxGLContext(this);
DrawingPanelInit();
}
return wxGLCanvas::SetCurrent(*ctx);
#else
return wxGLContext::SetCurrent(*this);
@ -2198,11 +2215,7 @@ GLDrawingPanel::GLDrawingPanel(wxWindow* parent, int _width, int _height)
wxFULL_REPAINT_ON_RESIZE | wxWANTS_CHARS)
{
widgets::RequestHighResolutionOpenGlSurfaceForWindow(this);
#ifndef wxGL_IMPLICIT_CONTEXT
ctx = new wxGLContext(this);
#endif
SetCurrent();
if (!did_init) DrawingPanelInit();
SetContext();
}
GLDrawingPanel::~GLDrawingPanel()
@ -2211,7 +2224,7 @@ GLDrawingPanel::~GLDrawingPanel()
// it's also unsafe if panel no longer displayed
if (did_init)
{
SetCurrent();
SetContext();
glDeleteLists(vlist, 1);
glDeleteTextures(1, &texid);
}
@ -2223,7 +2236,7 @@ GLDrawingPanel::~GLDrawingPanel()
void GLDrawingPanel::DrawingPanelInit()
{
SetCurrent();
SetContext();
DrawingPanelBase::DrawingPanelInit();
@ -2361,7 +2374,6 @@ void GLDrawingPanel::DrawingPanelInit()
void GLDrawingPanel::OnSize(wxSizeEvent& ev)
{
SetCurrent();
AdjustViewport();
// Temporary hack to backport 800d6ed69b from wxWidgets until 3.2.2 is released.
@ -2373,7 +2385,7 @@ void GLDrawingPanel::OnSize(wxSizeEvent& ev)
void GLDrawingPanel::AdjustViewport()
{
SetCurrent();
SetContext();
int x, y;
widgets::GetRealPixelClientSize(this, &x, &y);
@ -2387,10 +2399,20 @@ void GLDrawingPanel::AdjustViewport()
#endif
}
void GLDrawingPanel::RefreshGL()
{
SetContext();
// Rebind any textures or other OpenGL resources here
glBindTexture(GL_TEXTURE_2D, texid);
}
void GLDrawingPanel::DrawArea(wxWindowDC& dc)
{
(void)dc; // unused params
SetCurrent();
SetContext();
RefreshGL();
if (!did_init)
DrawingPanelInit();