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. How to pause rendering?
Forum Updated to NodeBB v4.3 + New Features

How to pause rendering?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 671 Views 1 Watching
  • 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.
  • Attila SantoA Offline
    Attila SantoA Offline
    Attila Santo
    wrote on last edited by
    #1

    I'm trying to prevent rendering while the application state is ApplicationSuspended but am stuck atm with my attempt.

    QScopedPointer<QQuickWindow> window ... 
    
    QSemaphore renderSemaphore(1);
    bool releaseOnRender {};
    
    QObject::connect(window.data(), &QQuickWindow::beforeRendering,
        qApp, [&] {
            releaseOnRender = renderSemaphore.tryAcquire(1, -1);
        }, Qt::DirectConnection);
    
    QObject::connect(window.data(), &QQuickWindow::afterRendering,
        qApp, [&] {
            if (releaseOnRender) {
                renderSemaphore.release(1);
            }
        }, Qt::DirectConnection);
    
    Qt::ApplicationState prevApplicationState = Qt::ApplicationState::ApplicationActive;
    
    QObject::connect(qApp, &QGuiApplication::applicationStateChanged,
        qApp, [&] (Qt::ApplicationState state) {
            qDebug() << "Changed state to" << state;
            if (state != prevApplicationState) {
                if (state == Qt::ApplicationState::ApplicationSuspended) {
                    renderSemaphore.acquire(1);
                    qDebug() << "Freezing render";
                } else {
                    renderSemaphore.release(1);
                    qDebug() << "Releasing render";
                }
                prevApplicationState = state;
            }
        }, Qt::DirectConnection);
    

    after initially being blocked in renderSemaphore.tryAcquire(1, -1);, QGuiApplication::applicationStateChanged isn't responding either (though it should be a different thread (shouldn't it?)

    Am happy about any advice / idea on how to block, pause or stop rendering while being ApplicationSuspended

    kshegunovK 1 Reply Last reply
    0
    • Attila SantoA Attila Santo

      I'm trying to prevent rendering while the application state is ApplicationSuspended but am stuck atm with my attempt.

      QScopedPointer<QQuickWindow> window ... 
      
      QSemaphore renderSemaphore(1);
      bool releaseOnRender {};
      
      QObject::connect(window.data(), &QQuickWindow::beforeRendering,
          qApp, [&] {
              releaseOnRender = renderSemaphore.tryAcquire(1, -1);
          }, Qt::DirectConnection);
      
      QObject::connect(window.data(), &QQuickWindow::afterRendering,
          qApp, [&] {
              if (releaseOnRender) {
                  renderSemaphore.release(1);
              }
          }, Qt::DirectConnection);
      
      Qt::ApplicationState prevApplicationState = Qt::ApplicationState::ApplicationActive;
      
      QObject::connect(qApp, &QGuiApplication::applicationStateChanged,
          qApp, [&] (Qt::ApplicationState state) {
              qDebug() << "Changed state to" << state;
              if (state != prevApplicationState) {
                  if (state == Qt::ApplicationState::ApplicationSuspended) {
                      renderSemaphore.acquire(1);
                      qDebug() << "Freezing render";
                  } else {
                      renderSemaphore.release(1);
                      qDebug() << "Releasing render";
                  }
                  prevApplicationState = state;
              }
          }, Qt::DirectConnection);
      

      after initially being blocked in renderSemaphore.tryAcquire(1, -1);, QGuiApplication::applicationStateChanged isn't responding either (though it should be a different thread (shouldn't it?)

      Am happy about any advice / idea on how to block, pause or stop rendering while being ApplicationSuspended

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      I can't tell you why it's not working exactly, but I'd suggest something along these lines:

      QAtomicInteger<bool> pauseFlag(false);
      QSemaphore pauseSemaphore;
      
      QObject::connect(window.data(), &QQuickWindow::beforeRendering, window.data(), [] () {
          if (pauseFlag.loadAcquire())
              pauseSemaphore.acquire();
      });
      
      QObject::connect(qApp, &QGuiApplication::applicationStateChanged, qApp, [] (Qt::ApplicationState state) {
          switch (state)
          {
          case Qt::ApplicationState::ApplicationSuspended:
              pauseFlag.storeRelease(true);
              break;
          case Qt::ApplicationState::ApplicationActive:
              if (pauseFlag.testAndSetRelease(true, false))
                  pauseSemaphore.release();
              break;
          }
      });
      

      PS. The pauseFlag.storeRelease(true); can probably safely be replaced by a relaxed store.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Something came to mind. You should check that the thread the signal is emitted from isn't the main thread, which'd explain why your code (and mine) deadlocks.

        Read and abide by the Qt Code of Conduct

        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