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

Qt fails to flush to Mac native sub window when tlw is RHI

    XMLWordPrintable

Details

    • macOS
    • f16f31ba3 (dev), d26be2a20 (6.7), 4ba4525b5 (6.6), db32b871a (tqtc/lts-6.5), cd21ae6cd (dev)
    • G&UI Finishing 2023

    Description

      Please use this code to reproduce:

      #include <QtWidgets/QApplication>
      #include <QtWidgets/QWidget>
      #include <QtOpenGLWidgets/QOpenGLWidget>
      #include <QtCore/QTimer>
      #include <QtCore/QPointer>
      #include <QtGui/QBackingStore>
      #include <QtGui/QPainter>
      
      // This can be any custom native WA_PaintOnScreen widget. e.g. a custom Metal canvas
      class SomePaintOnScreen : public QWidget
      {
      public:
      	SomePaintOnScreen(QWidget* parent)
      		: QWidget(parent)
      	{
      		setAttribute(Qt::WA_OpaquePaintEvent);
      		setAttribute(Qt::WA_PaintOnScreen);
      		winId();
      	}
      
      	QPaintEngine* paintEngine() const override
      	{
      		return nullptr;
      	}
      	
      	void paintEvent(QPaintEvent* pe) override
      	{
      		// This is only some convenient code to fill NSView with green pixels.
      		// Can be replaced by any code to manipulate NSView.
      		// e.g. set a custom CAMetalLayer and render some 3d contents to it.
      		QBackingStore store(windowHandle());
      		store.resize(size());
      		store.beginPaint(rect());
      
      		QPainter p(store.paintDevice());
      		p.setCompositionMode(QPainter::CompositionMode_Source);
      		p.fillRect(rect(), Qt::green);
      		p.end();
      
      		store.endPaint();
      		store.flush(rect(), windowHandle(), QPoint(0, 0));
      	}
      };
      
      int main(int argc, char** argv)
      {
      	QApplication app(argc, argv);
      
      	// tlw: red.
      	QWidget tlw;
      	tlw.resize(640, 480);
      	tlw.setStyleSheet("QWidget { background-color: red; }");
      
      	// gl: black.
      	QOpenGLWidget gl(&tlw);
      	gl.setGeometry(0, 0, 640, 240);
      	
      	// tlw's window's surface type will become OpenGL because there is a
      	// QOpenGLWidget child. If there is a quick child, it will become
      	// Metal by default.
      	// In both case, tlw's backing store is QRhiBackingStore.
      	
      	// paint-on-surface: green.
      	SomePaintOnScreen canvas(&tlw);
      	canvas.setGeometry(0, 240, 640, 240);
      
      	// native child of paint-on-surface: blue.
      	QWidget raster(&canvas);
      	raster.setGeometry(0, 0, 640, 240);
      	raster.setStyleSheet("QWidget { background-color: blue; }");
      	
      	// We expect:
      	//   [ black ]
      	//   [ blue ]
      
      	// Got:
      	//   [ black ]
      	//   [ green ]
      	
      	// QOpenGLWidget cause the tlw's backing store becomes QRhiBackingStore.
      	// QRhiBackingStore is not able to flush into a native raster child.
      	//
      
      	tlw.show();
      	return app.exec();
      }
      

      If there is a OpenGL/Metal RHI child, the top-level widget will create QRhiBackingStore. It's not able to flush into native child's NSView. Internally, QRhiBackingStore skips earily when flushing into a native child. The top-level window shows the content but the native child window has no content. WA_PaintOnScreen widget obscures the top-level window so we can see the content of the native child window without interfered by the top-level window.

      If we remove QOpenGLWidget, the native child draws correctly. This only happens when switching to QRhiBackingStore on Mac.

      When tlw is RHI, Qt fails to flush to all native sub window. It's not limited to having a WA_PaintOnScreen parent. NSView by default is transparent so it's difficult to observe the issue without WA_PaintOnScreen showing a different color.

      Can be reproduced on OSX 14

      Attachments

        For Gerrit Dashboard: QTBUG-119309
        # Subject Branch Project Status CR V

        Activity

          People

            vestbo Tor Arne Vestbø
            mingxiang Mingxiang Xu
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: