Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QopenglWidget share context among thread

QopenglWidget share context among thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Delavega77
    wrote on last edited by
    #1

    I create a QOpenglWidget and I'm trying to share context in a WorkerThread to load textures:
    void VideoWidget::updateVideoFrame(QVideoFrame frame)
    {
    QOpenGLContext* ctx = new QOpenGLContext();
    QOpenGLContext* sharectx = QOpenGLContext::currentContext();
    ctx->setFormat(sharectx->format());
    ctx->setShareContext(sharectx);
    ctx->create();
    if (ctx->isValid()){

        qDebug()<<"valid";
    }
    WorkerThread *workerThread = new WorkerThread();
    ctx->moveToThread(workerThread);
    connect(workerThread,SIGNAL(resultReady(QOpenGLTexture*)), this, SLOT(updateTexture(QOpenGLTexture*)));
    connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
    workerThread->setFrame(frame);
    

    workerThread->start();
    }

    here it is workerthread.h

    class WorkerThread : public QThread
    {
    Q_OBJECT

    void run() override {
        //    Q_UNUSED(frame);
    
        // Handle the frame and do your processing
        QVideoFrame cloneFrame(frametexture);
        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
        QImage image(cloneFrame.bits(),
                     cloneFrame.width(),
                     cloneFrame.height(),
                     QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
    
    
        QOpenGLTexture* texture = new QOpenGLTexture(image);
    
        cloneFrame.unmap();
    
    }
    

    But when i try to create the texture there is no valid context.
    What I'm missing?
    thanks

    D 1 Reply Last reply
    0
    • D Delavega77

      I create a QOpenglWidget and I'm trying to share context in a WorkerThread to load textures:
      void VideoWidget::updateVideoFrame(QVideoFrame frame)
      {
      QOpenGLContext* ctx = new QOpenGLContext();
      QOpenGLContext* sharectx = QOpenGLContext::currentContext();
      ctx->setFormat(sharectx->format());
      ctx->setShareContext(sharectx);
      ctx->create();
      if (ctx->isValid()){

          qDebug()<<"valid";
      }
      WorkerThread *workerThread = new WorkerThread();
      ctx->moveToThread(workerThread);
      connect(workerThread,SIGNAL(resultReady(QOpenGLTexture*)), this, SLOT(updateTexture(QOpenGLTexture*)));
      connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
      workerThread->setFrame(frame);
      

      workerThread->start();
      }

      here it is workerthread.h

      class WorkerThread : public QThread
      {
      Q_OBJECT

      void run() override {
          //    Q_UNUSED(frame);
      
          // Handle the frame and do your processing
          QVideoFrame cloneFrame(frametexture);
          cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
          QImage image(cloneFrame.bits(),
                       cloneFrame.width(),
                       cloneFrame.height(),
                       QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
      
      
          QOpenGLTexture* texture = new QOpenGLTexture(image);
      
          cloneFrame.unmap();
      
      }
      

      But when i try to create the texture there is no valid context.
      What I'm missing?
      thanks

      D Offline
      D Offline
      Delavega77
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The thread is stopped (and deleted in your case since your signals/slot connection) as soon as run() finishes which is pretty fast in your case...
        The signal resultReady is also not emitted anywhere

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          The thread is stopped (and deleted in your case since your signals/slot connection) as soon as run() finishes which is pretty fast in your case...
          The signal resultReady is also not emitted anywhere

          D Offline
          D Offline
          Delavega77
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          thank you for the reply, the signal is received correvtly in the main thread. but when i cretae the opengltexture the context is not valid

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            The signal resultReady() is not sent in your example so I guess it's incomplete.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              The signal resultReady() is not sent in your example so I guess it's incomplete.

              D Offline
              D Offline
              Delavega77
              wrote on last edited by
              #6

              @Christian-Ehrlicher you are right copy/paste error but i sent the signal right the unmap of the frame.
              But it gives error when create the opengltexture because the context is not valid

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                And where do you use the QOpenGLContext created in updateVideoFrame()? Did you check that the generated QImage is valid?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  And where do you use the QOpenGLContext created in updateVideoFrame()? Did you check that the generated QImage is valid?

                  D Offline
                  D Offline
                  Delavega77
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher
                  the image is correct i also save it on hdd and it is fine.
                  in the updatevideoframe i just create the context share it and move the thread. Maybe do i need something else?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    I've no more idea but I think you don't need a context at all. From the QOpenGLTexture ctor description:

                    This does create the underlying OpenGL texture object. Therefore, construction using this constructor does require a valid current OpenGL context.
                    

                    Maybe also check for QOpenGLTexture::isCreated() in thread and main thread.
                    And I wonder if you need a thread for this small calculation at all - since not much is done the thread overhead looks much bigger than the benefit.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    D 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      I've no more idea but I think you don't need a context at all. From the QOpenGLTexture ctor description:

                      This does create the underlying OpenGL texture object. Therefore, construction using this constructor does require a valid current OpenGL context.
                      

                      Maybe also check for QOpenGLTexture::isCreated() in thread and main thread.
                      And I wonder if you need a thread for this small calculation at all - since not much is done the thread overhead looks much bigger than the benefit.

                      D Offline
                      D Offline
                      Delavega77
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher i know that the qopenglwidget creates the underlying context and it is shared among all the others widget and it works because i used it.
                      but for documentation to use context in a different thread i have to use share context but...

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        wrosecrans
                        wrote on last edited by
                        #11

                        I use

                            QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
                        

                        before I create my QApplication in main to make sure all contexts are sharing by default. I had some trouble getting sharing to work after porting some code from QGLWidget to QopenGLWidget a few years back, and that was the bit that got everything working for me.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved