Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-141171

Shouldn't QOpenGLContext::makeCurrent allow no thread affinity ?

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P1: Critical P1: Critical
    • None
    • 6.10.0
    • GUI: OpenGL
    • None
    • Windows

      Using a custom created QOpenGLContext with QQuickWindow triggers a fatal error inside QSGRenderThread on QQuickWindow::exposeEvent here https://github.com/qt/qtbase/blob/f9b83106255aa97d0a3ffc105fb17676d2a72b78/src/gui/kernel/qopenglcontext.cpp#L665 (writing this explicitely because this link seems to behave oddly here). Ignoring this causes no issue and I could just set AA_DontCheckOpenGLContextThreadAffinity but I don't think I should.

      Here is the basic code to set the custom OpenGL context when creating the QQuickWindow

      Window::Window()
          : _context(new QOpenGLContext())
      {
          setGraphicsApi(QSGRendererInterface::OpenGL);
          Q_ASSERT(_context->create());
          setSurfaceType(QWindow::OpenGLSurface);
          setGraphicsDevice(QQuickGraphicsDevice::fromOpenGLContext(_context.get()));
          // I think this should fix the issue but it doesn't
          //_context->moveToThread(nullptr);
      }
      
      void Window::exposeEvent(QExposeEvent* a_Event)
      {
          // this is done to avoid a fatal error during QSGRenderThread initialization
          QGuiApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity, true);
          QQuickView::exposeEvent(a_Event);
          QGuiApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity, false);
      }
      

      The obvious solution would be to be able to move the context to QSGRenderThread before it attempts to make it current but AFAIK it emits no signal before sceneGraphInitialized
      The only solution I found is to set AA_DontCheckOpenGLContextThreadAffinity to true inside QQuickWindow::exposeEvent before passing it to the parent class

      Maybe makeCurrent should be something like this :

      if (Q_UNLIKELY(!qApp->testAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity)
                     && thread() != nullptr
                     && thread() != QThread::currentThread())) {
          qFatal("Cannot make QOpenGLContext current in a different thread");
      }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            lagocs Laszlo Agocs
            gpinchon Guillaume Pïnchon
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes