From 8e6811aef0fde0f950a7a588130874cb1bbd74d4 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 10 Feb 2023 20:10:42 +0400 Subject: [PATCH] Allow custom ChooseBackend hook in RpWindow. --- ui/gl/gl_window.cpp | 49 ++++++++++++++++++++++----------------------- ui/gl/gl_window.h | 8 ++++++-- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ui/gl/gl_window.cpp b/ui/gl/gl_window.cpp index f2cc816..b866d4e 100644 --- a/ui/gl/gl_window.cpp +++ b/ui/gl/gl_window.cpp @@ -24,11 +24,24 @@ namespace { constexpr auto kUseNativeChild = false;// ::Platform::IsWindows(); +[[nodiscard]] Backend DefaultChooseBackend(Capabilities capabilities) { + const auto use = ::Platform::IsMac() + ? true + : ::Platform::IsWindows() + ? capabilities.supported + : capabilities.transparency; + LOG(("OpenGL: %1 (Window)").arg(use ? "[TRUE]" : "[FALSE]")); + return use ? Backend::OpenGL : Backend::Raster; +} + } // namespace -Window::Window() -: _window(createWindow()) -, _bodyNativeWrap(createNativeBodyWrap()) +Window::Window() : Window(DefaultChooseBackend) { +} + +Window::Window(Fn chooseBackend) +: _window(createWindow(chooseBackend)) +, _bodyNativeWrap(createNativeBodyWrap(chooseBackend)) , _body(_bodyNativeWrap ? _bodyNativeWrap.get() : _window->body().get()) { } @@ -46,19 +59,12 @@ not_null Window::widget() const { return _body.get(); } -std::unique_ptr Window::createWindow() { +std::unique_ptr Window::createWindow( + const Fn &chooseBackend) { auto result = std::make_unique(); if constexpr (!kUseNativeChild) { - const auto capabilities = CheckCapabilities(result.get()); - const auto use = ::Platform::IsMac() - ? true - : ::Platform::IsWindows() - ? capabilities.supported - : capabilities.transparency; - LOG(("OpenGL: %1 (Window)").arg(use ? "[TRUE]" : "[FALSE]")); - _backend = use ? Backend::OpenGL : Backend::Raster; - - if (!use) { + _backend = chooseBackend(CheckCapabilities(result.get())); + if (_backend != Backend::OpenGL) { // We have to create a new window, if OpenGL initialization failed. result = std::make_unique(); } @@ -66,7 +72,8 @@ std::unique_ptr Window::createWindow() { return result; } -std::unique_ptr Window::createNativeBodyWrap() { +std::unique_ptr Window::createNativeBodyWrap( + const Fn &chooseBackend) { if constexpr (!kUseNativeChild) { return nullptr; } @@ -81,16 +88,8 @@ std::unique_ptr Window::createNativeBodyWrap() { }; auto result = create(); - const auto capabilities = CheckCapabilities(result.get()); - const auto use = ::Platform::IsMac() - ? true - : ::Platform::IsWindows() - ? capabilities.supported - : capabilities.transparency; - LOG(("OpenGL: %1 (WindowBody)").arg(use ? "[TRUE]" : "[FALSE]")); - _backend = use ? Backend::OpenGL : Backend::Raster; - - if (!use) { + _backend = chooseBackend(CheckCapabilities(result.get())); + if (_backend != Backend::OpenGL) { // We have to create a new window, if OpenGL initialization failed. result = create(); } diff --git a/ui/gl/gl_window.h b/ui/gl/gl_window.h index 797fcdf..65bbc72 100644 --- a/ui/gl/gl_window.h +++ b/ui/gl/gl_window.h @@ -15,10 +15,12 @@ class RpWidget; namespace Ui::GL { enum class Backend; +struct Capabilities; class Window final { public: Window(); + explicit Window(Fn chooseBackend); ~Window(); [[nodiscard]] Backend backend() const; @@ -26,8 +28,10 @@ public: [[nodiscard]] not_null widget() const; private: - [[nodiscard]] std::unique_ptr createWindow(); - [[nodiscard]] std::unique_ptr createNativeBodyWrap(); + [[nodiscard]] std::unique_ptr createWindow( + const Fn &chooseBackend); + [[nodiscard]] std::unique_ptr createNativeBodyWrap( + const Fn &chooseBackend); Backend _backend = Backend(); const std::unique_ptr _window;