Skip to content

Commit

Permalink
Fixed remaining lag after switching off hardware acceleration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bjorn committed Feb 23, 2023
1 parent 0587f3e commit 0d1b1b4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -96,6 +91,7 @@
#include <QStackedWidget>
#include <QToolBar>
#include <QUndoGroup>
#include <QWindow>

#include <memory>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down
1 change: 1 addition & 0 deletions src/tiled/mapeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/tiled/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0d1b1b4

Please sign in to comment.