Take advantage of RHI

Use Metal with Qt 6.4+ and macOS 10.14+, OpenGL for Wayland whenever enabled
This commit is contained in:
Ilya Fedin 2022-07-18 02:38:08 +04:00 committed by John Preston
parent 80505c1216
commit f5fb567052
4 changed files with 62 additions and 3 deletions

View file

@ -23,6 +23,16 @@ namespace Ui::GL {
namespace {
constexpr auto kUseNativeChild = false;// ::Platform::IsWindows();
class RpWindowNoRhi : public RpWindow {
protected:
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
std::optional<QPlatformBackingStoreRhiConfig> rhiConfig() const override {
return QPlatformBackingStoreRhiConfig();
}
#endif // Qt >= 6.4.0
};
[[nodiscard]] Fn<Backend(Capabilities)> ChooseBackendWrap(
Fn<Backend(Capabilities)> chooseBackend) {
return [=](Capabilities capabilities) {
@ -60,7 +70,7 @@ not_null<RpWidget*> Window::widget() const {
std::unique_ptr<RpWindow> Window::createWindow(
const Fn<Backend(Capabilities)> &chooseBackend) {
auto result = std::make_unique<RpWindow>();
std::unique_ptr<RpWindow> result = std::make_unique<RpWindowNoRhi>();
if constexpr (!kUseNativeChild) {
_backend = chooseBackend(CheckCapabilities(result.get()));
if (_backend != Backend::OpenGL) {

View file

@ -270,7 +270,9 @@ void WindowHelper::Private::enforceStyle() {
}
void WindowHelper::Private::initOpenGL() {
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
auto forceOpenGL = std::make_unique<QOpenGLWidget>(_owner->window());
#endif // Qt < 6.4.0
}
void WindowHelper::Private::resolveWeakPointers() {

View file

@ -6,10 +6,45 @@
//
#include "ui/rp_widget.h"
#include "base/platform/base_platform_info.h"
#include "base/qt_signal_producer.h"
#include "ui/gl/gl_detection.h"
#include <QtGui/QWindow>
#include <QtGui/QtEvents>
#include <private/qwidget_p.h>
class TWidgetPrivate : public QWidgetPrivate {
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
QPlatformBackingStoreRhiConfig rhiConfig() const override {
const auto q = static_cast<TWidget*>(q_ptr);
if (!q->testAttribute(Qt::WA_WState_Created)) {
return QWidgetPrivate::rhiConfig();
}
if (const auto config = q->rhiConfig()) {
return *config;
}
if (::Platform::IsMac10_14OrGreater()) {
return { QPlatformBackingStoreRhiConfig::Metal };
}
// We can't specify the widget here as q_evaluateRhiConfig is called
// in QWidgetWindow constructor, while windowHandle is set right after
// the constructor is completed
if (::Platform::IsWayland() // old versions of mutter produce flicker without OpenGL
&& Ui::GL::ChooseBackendDefault(
Ui::GL::CheckCapabilities(nullptr))
== Ui::GL::Backend::OpenGL) {
return { QPlatformBackingStoreRhiConfig::OpenGL };
}
return QWidgetPrivate::rhiConfig();
}
#endif // Qt >= 6.4.0
};
TWidget::TWidget(QWidget *parent)
: TWidgetHelper<QWidget>(*(new TWidgetPrivate), parent, {}) {
}
namespace Ui {
namespace {

View file

@ -18,6 +18,10 @@
#include <QtCore/QPointer>
#include <QtGui/QtEvents>
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#include <qpa/qplatformbackingstore.h>
#endif // Qt >= 6.4.0
namespace Ui {
void ToggleChildrenVisibility(not_null<QWidget*> widget, bool visible);
@ -25,6 +29,7 @@ void ToggleChildrenVisibility(not_null<QWidget*> widget, bool visible);
} // namespace Ui
class TWidget;
class TWidgetPrivate;
template <typename Base>
class TWidgetHelper : public Base {
@ -146,6 +151,12 @@ protected:
virtual void enterFromChildEvent(QEvent *e, QWidget *child) {
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
virtual std::optional<QPlatformBackingStoreRhiConfig> rhiConfig() const {
return std::nullopt;
}
#endif // Qt >= 6.4.0
private:
TWidget *tparent() {
return qobject_cast<TWidget*>(Base::parentWidget());
@ -157,6 +168,8 @@ private:
template <typename OtherBase>
friend class TWidgetHelper;
friend class TWidgetPrivate;
};
class TWidget : public TWidgetHelper<QWidget> {
@ -164,8 +177,7 @@ class TWidget : public TWidgetHelper<QWidget> {
Q_OBJECT
public:
TWidget(QWidget *parent = nullptr) : TWidgetHelper<QWidget>(parent) {
}
TWidget(QWidget *parent = nullptr);
// Get the size of the widget as it should be.
// Negative return value means no default width.