macOS: Release surfaces straight away when reconfiguring QCocoaGLContext

Calling setView or update on NSOpenGLContext results in recreating the
internal GL surfaces of the view. Unfortunately there seems to be a
fixed amount of these surfaces available, so if we spin a loop where
we for some reason end up recreating them, we'll easily run out, and
lock up the whole window system:

  thread #6, name = 'SwapThread'
    frame #0: 0x00007fff7b45220a libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x00007fff7b451724 libsystem_kernel.dylib`mach_msg + 60
    frame #2: 0x00007fff751c1675 SkyLight`SLSBindSurface + 247
    frame #3: 0x00007fff5d9c4328 OpenGL`___lldb_unnamed_symbol29$$OpenGL + 255
    frame #4: 0x00007fff6bf42c33 libGPUSupportMercury.dylib`gldAttachDrawable + 364
    frame #5: 0x00007fff5d9e61e7 GLEngine`gliAttachDrawableWithOptions + 257
    frame #6: 0x00007fff5d9c4bb0 OpenGL`___lldb_unnamed_symbol38$$OpenGL + 969
    frame #7: 0x00007fff5d9c8b0e OpenGL`___lldb_unnamed_symbol57$$OpenGL + 82
    frame #8: 0x00007fff5d9c8e55 OpenGL`CGLSetSurface + 330
    frame #9: 0x00007fff50d0eb2c AppKit`NSOpenGLContextAttachOffScreenViewSurface + 352

This can happen e.g. when resizing the application, where AppKit itself spins
a loop where we don't end up back in QCocoaEventDispatcher::processEvents()
for each pass (where we do have a local pool). Or it can happen in the
render-loop of a render-thread that doesn't use the event dispatcher.

Change-Id: Iaf2f879dd01e3d807d0f35705ccc978dbc89036b
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
bb10
Tor Arne Vestbø 2018-09-05 18:51:29 +02:00
parent 33b79ddc80
commit 1398f4f828
1 changed files with 10 additions and 0 deletions

View File

@ -363,6 +363,11 @@ bool QCocoaGLContext::makeCurrent(QPlatformSurface *surface)
*/
bool QCocoaGLContext::setDrawable(QPlatformSurface *surface)
{
// Make sure any surfaces released during this process are deallocated
// straight away, otherwise we may run out of surfaces when spinning a
// render-loop that doesn't return to one of the outer pools.
QMacAutoReleasePool pool;
if (!surface || surface->surface()->surfaceClass() == QSurface::Offscreen) {
// Clear the current drawable and reset the active window, so that GL
// commands that don't target a specific FBO will not end up stomping
@ -393,6 +398,11 @@ static QMutex s_contextMutex;
void QCocoaGLContext::update()
{
// Make sure any surfaces released during this process are deallocated
// straight away, otherwise we may run out of surfaces when spinning a
// render-loop that doesn't return to one of the outer pools.
QMacAutoReleasePool pool;
QMutexLocker locker(&s_contextMutex);
qCInfo(lcQpaOpenGLContext) << "Updating" << m_context << "for" << m_context.view;
[m_context update];