66#include < qobject.h>
77#include < qqmlcontext.h>
88#include < qqmlengine.h>
9+ #include < qqmlinfo.h>
910#include < qqmllist.h>
1011#include < qquickitem.h>
1112#include < qquickwindow.h>
1213#include < qregion.h>
14+ #include < qsurfaceformat.h>
15+ #include < qtenvironmentvariables.h>
1316#include < qtmetamacros.h>
1417#include < qtypes.h>
1518#include < qvariant.h>
@@ -50,7 +53,7 @@ ProxyWindowBase::~ProxyWindowBase() { this->deleteWindow(true); }
5053void ProxyWindowBase::onReload (QObject* oldInstance) {
5154 this ->window = this ->retrieveWindow (oldInstance);
5255 auto wasVisible = this ->window != nullptr && this ->window ->isVisible ();
53- if ( this ->window == nullptr ) this -> window = this -> createQQuickWindow ();
56+ this ->ensureQWindow ();
5457
5558 // The qml engine will leave the WindowInterface as owner of everything
5659 // nested in an item, so we have to make sure the interface's children
@@ -85,10 +88,55 @@ void ProxyWindowBase::postCompleteWindow() { this->setVisible(this->mVisible); }
8588
8689ProxiedWindow* ProxyWindowBase::createQQuickWindow () { return new ProxiedWindow (this ); }
8790
88- void ProxyWindowBase::createWindow () {
89- if (this ->window != nullptr ) return ;
91+ void ProxyWindowBase::ensureQWindow () {
92+ auto format = QSurfaceFormat::defaultFormat ();
93+
94+ {
95+ // match QtQuick's default format, including env var controls
96+ static const auto useDepth = qEnvironmentVariableIsEmpty (" QSG_NO_DEPTH_BUFFER" );
97+ static const auto useStencil = qEnvironmentVariableIsEmpty (" QSG_NO_STENCIL_BUFFER" );
98+ static const auto enableDebug = qEnvironmentVariableIsSet (" QSG_OPENGL_DEBUG" );
99+ static const auto disableVSync = qEnvironmentVariableIsSet (" QSG_NO_VSYNC" );
100+
101+ if (useDepth && format.depthBufferSize () == -1 ) format.setDepthBufferSize (24 );
102+ else if (!useDepth) format.setDepthBufferSize (0 );
103+
104+ if (useStencil && format.stencilBufferSize () == -1 ) format.setStencilBufferSize (8 );
105+ else if (!useStencil) format.setStencilBufferSize (0 );
106+
107+ auto opaque = this ->qsSurfaceFormat .opaqueModified ? this ->qsSurfaceFormat .opaque
108+ : this ->mColor .alpha () >= 255 ;
109+
110+ if (opaque) format.setAlphaBufferSize (0 );
111+ else format.setAlphaBufferSize (8 );
112+
113+ if (enableDebug) format.setOption (QSurfaceFormat::DebugContext);
114+ if (disableVSync) format.setSwapInterval (0 );
115+
116+ format.setSwapBehavior (QSurfaceFormat::DoubleBuffer);
117+ format.setRedBufferSize (8 );
118+ format.setGreenBufferSize (8 );
119+ format.setBlueBufferSize (8 );
120+ }
121+
122+ this ->mSurfaceFormat = format;
123+
124+ auto useOldWindow = this ->window != nullptr ;
125+
126+ if (useOldWindow) {
127+ if (this ->window ->requestedFormat () != format) {
128+ useOldWindow = false ;
129+ }
130+ }
131+
132+ if (useOldWindow) return ;
133+ delete this ->window ;
90134 this ->window = this ->createQQuickWindow ();
135+ this ->window ->setFormat (format);
136+ }
91137
138+ void ProxyWindowBase::createWindow () {
139+ this ->ensureQWindow ();
92140 this ->connectWindow ();
93141 this ->completeWindow ();
94142 emit this ->windowConnected ();
@@ -320,6 +368,8 @@ void ProxyWindowBase::setColor(QColor color) {
320368 );
321369
322370 this ->window ->setColor (premultiplied);
371+ // setColor also modifies the alpha buffer size of the surface format
372+ this ->window ->setFormat (this ->mSurfaceFormat );
323373 }
324374}
325375
@@ -343,6 +393,17 @@ void ProxyWindowBase::setMask(PendingRegion* mask) {
343393 emit this ->maskChanged ();
344394}
345395
396+ void ProxyWindowBase::setSurfaceFormat (QsSurfaceFormat format) {
397+ if (format == this ->qsSurfaceFormat ) return ;
398+ if (this ->window != nullptr ) {
399+ qmlWarning (this ) << " Cannot set window surface format." ;
400+ return ;
401+ }
402+
403+ this ->qsSurfaceFormat = format;
404+ emit this ->surfaceFormatChanged ();
405+ }
406+
346407void ProxyWindowBase::onMaskChanged () {
347408 if (this ->window != nullptr ) this ->updateMask ();
348409}
0 commit comments