From 0d1b1b448c72012e3554d8572ff14fd00175d4a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 23 Feb 2023 12:03:54 +0100 Subject: [PATCH] Fixed remaining lag after switching off hardware acceleration The QOpenGLWidget causes the entire window to switch to an OpenGLSurface, but it doesn't automatically switch back to a RasterSurface when the last QOpenGLWidget is deleted again. Closes #3584 --- NEWS.md | 1 + src/tiled/mapeditor.cpp | 35 +++++++++++++++++++++++++++-------- src/tiled/mapeditor.h | 1 + src/tiled/mapview.cpp | 4 +--- src/tiled/mapview.h | 3 ++- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index 79d43a0808..e1aae643c3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,6 +23,7 @@ * Qt 6: Fixed behavior of "Class of" selection popup * Fixed positioning of point object name labels (by Logan Higinbotham, #3400) * Fixed slight drift when zooming the map view in/out +* Fixed remaining lag after switching off hardware acceleration (#3584) * Fixed compile against Qt 6.4 * snap: Added Wayland platform plugin and additional image format plugins * AppImage: Updated to Sentry 0.5.4 diff --git a/src/tiled/mapeditor.cpp b/src/tiled/mapeditor.cpp index 33878c2f92..530d6a774a 100644 --- a/src/tiled/mapeditor.cpp +++ b/src/tiled/mapeditor.cpp @@ -20,10 +20,7 @@ #include "mapeditor.h" -#include "actionmanager.h" -#include "addremovelayer.h" #include "addremovetileset.h" -#include "brokenlinks.h" #include "bucketfilltool.h" #include "changeselectedarea.h" #include "createellipseobjecttool.h" @@ -40,7 +37,6 @@ #include "editablewangset.h" #include "editpolygontool.h" #include "eraser.h" -#include "filechangedwarning.h" #include "layerdock.h" #include "layermodel.h" #include "layeroffsettool.h" @@ -56,7 +52,6 @@ #include "objectsdock.h" #include "objectselectiontool.h" #include "objecttemplate.h" -#include "painttilelayer.h" #include "preferences.h" #include "propertiesdock.h" #include "reversingproxymodel.h" @@ -96,6 +91,7 @@ #include #include #include +#include #include @@ -300,6 +296,7 @@ MapEditor::MapEditor(QObject *parent) retranslateUi(); Preferences *prefs = Preferences::instance(); + connect(prefs, &Preferences::useOpenGLChanged, this, &MapEditor::setUseOpenGL); connect(prefs, &Preferences::languageChanged, this, &MapEditor::retranslateUi); connect(prefs, &Preferences::showTileCollisionShapesChanged, this, &MapEditor::showTileCollisionShapesChanged); @@ -978,21 +975,43 @@ void MapEditor::setupQuickStamps() // Set up shortcut for selecting this quick stamp QShortcut *selectStamp = new QShortcut(key, mMainWindow); - connect(selectStamp, &QShortcut::activated, [=] { mTileStampManager->selectQuickStamp(i); }); + connect(selectStamp, &QShortcut::activated, this, [=] { mTileStampManager->selectQuickStamp(i); }); // Set up shortcut for creating this quick stamp QShortcut *createStamp = new QShortcut(Qt::CTRL + key, mMainWindow); - connect(createStamp, &QShortcut::activated, [=] { mTileStampManager->createQuickStamp(i); }); + connect(createStamp, &QShortcut::activated, this, [=] { mTileStampManager->createQuickStamp(i); }); // Set up shortcut for extending this quick stamp QShortcut *extendStamp = new QShortcut((Qt::CTRL | Qt::SHIFT) + key, mMainWindow); - connect(extendStamp, &QShortcut::activated, [=] { mTileStampManager->extendQuickStamp(i); }); + connect(extendStamp, &QShortcut::activated, this, [=] { mTileStampManager->extendQuickStamp(i); }); } connect(mTileStampManager, &TileStampManager::setStamp, this, &MapEditor::setStamp); } +void MapEditor::setUseOpenGL(bool useOpenGL) +{ + for (MapView *mapView : qAsConst(mWidgetForMap)) + mapView->setUseOpenGL(useOpenGL); + + if (useOpenGL) + return; + + // When turning off OpenGL, we may need to change the surface type back + // to RasterSurface, to avoid lag and improve performance. + if (auto w = mMainWindow->window()->windowHandle()) { + if (w->surfaceType() != QSurface::RasterSurface) { + w->setSurfaceType(QSurface::RasterSurface); + + if (w->handle()) { + w->destroy(); + w->show(); + } + } + } +} + void MapEditor::retranslateUi() { mToolsToolBar->setWindowTitle(tr("Tools")); diff --git a/src/tiled/mapeditor.h b/src/tiled/mapeditor.h index e0319297e7..5246fde877 100644 --- a/src/tiled/mapeditor.h +++ b/src/tiled/mapeditor.h @@ -159,6 +159,7 @@ class MapEditor final : public Editor void updateLayerComboIndex(); void setupQuickStamps(); + void setUseOpenGL(bool useOpenGL); void retranslateUi(); void showTileCollisionShapesChanged(bool enabled); void parallaxEnabledChanged(bool enabled); diff --git a/src/tiled/mapview.cpp b/src/tiled/mapview.cpp index 7184f204ab..d628e1e532 100644 --- a/src/tiled/mapview.cpp +++ b/src/tiled/mapview.cpp @@ -72,9 +72,7 @@ MapView::MapView(QWidget *parent, Mode mode) #endif #ifndef QT_NO_OPENGL - Preferences *prefs = Preferences::instance(); - setUseOpenGL(prefs->useOpenGL()); - connect(prefs, &Preferences::useOpenGLChanged, this, &MapView::setUseOpenGL); + setUseOpenGL(Preferences::instance()->useOpenGL()); #endif QWidget *v = viewport(); diff --git a/src/tiled/mapview.h b/src/tiled/mapview.h index 968f4e9319..77f90dc7ab 100644 --- a/src/tiled/mapview.h +++ b/src/tiled/mapview.h @@ -96,6 +96,8 @@ class MapView : public QGraphicsView void forceCenterOn(QPointF pos); void forceCenterOn(QPointF pos, const Layer &layer); + void setUseOpenGL(bool useOpenGL); + protected: bool event(QEvent *event) override; @@ -124,7 +126,6 @@ class MapView : public QGraphicsView private: void adjustScale(qreal scale); - void setUseOpenGL(bool useOpenGL); void updateSceneRect(const QRectF &sceneRect); void updateSceneRect(const QRectF &sceneRect, const QTransform &transform); void updateViewRect();