From c2dd8271e33c9a93e8c520c4060ece6b6ec4e514 Mon Sep 17 00:00:00 2001 From: wuyize Date: Thu, 9 Mar 2023 16:21:55 +0800 Subject: [PATCH] =?UTF-8?q?ElementRender=E6=94=B9=E4=B8=BA=E5=8D=95?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp | 2 +- .../src/Renderer/Preview/ElementRenderer.cpp | 9 +++++++-- .../src/Renderer/Preview/ElementRenderer.h | 5 +++-- UnitTest/ElementRendererTest.h | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index 082ac27..0db3c48 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -2,7 +2,7 @@ PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent) { - this->renderer = new Renderer::ElementRenderer(this); + this->renderer = Renderer::ElementRenderer::instance(); QSurfaceFormat surfaceFormat; surfaceFormat.setSamples(16); setFormat(surfaceFormat); diff --git a/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.cpp b/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.cpp index 46bbb40..60a3b81 100644 --- a/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.cpp @@ -114,8 +114,7 @@ QRectF calcBoundingRect(const QPainterPath& path, const std::vector& return QRectF(QPointF(leftTop.x, leftTop.y), QPointF(rightBottom.x, rightBottom.y)); } -Renderer::ElementRenderer::ElementRenderer(QOpenGLWidget* glWidget) - : glWidget(glWidget) +Renderer::ElementRenderer::ElementRenderer() { surface.create(); thread = std::jthread([&] { @@ -181,6 +180,12 @@ Renderer::ElementRenderer::ElementRenderer(QOpenGLWidget* glWidget) std::this_thread::yield(); } +ElementRenderer* Renderer::ElementRenderer::instance() +{ + static ElementRenderer renderer; + return &renderer; +} + std::pair Renderer::ElementRenderer::drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio) { std::unique_lock lock(drawMutex); diff --git a/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.h b/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.h index c2f10b9..9c59db5 100644 --- a/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.h +++ b/ArchitectureColoredPainting/src/Renderer/Preview/ElementRenderer.h @@ -10,7 +10,7 @@ namespace Renderer class ElementRenderer : protected QOpenGLFunctions_4_5_Core { public: - ElementRenderer(QOpenGLWidget* glWidget); + static ElementRenderer* instance(); /** * @brief ½«Í¼Ôª»æÖƵ½QImage @@ -22,7 +22,6 @@ namespace Renderer */ std::pair drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio); protected: - QOpenGLWidget* glWidget; std::unique_ptr shader; std::jthread thread; QOffscreenSurface surface; @@ -36,6 +35,8 @@ namespace Renderer const ElementStyle* style; float pixelRatio; std::pair result; + + ElementRenderer(); }; } diff --git a/UnitTest/ElementRendererTest.h b/UnitTest/ElementRendererTest.h index 5c1e12c..9271821 100644 --- a/UnitTest/ElementRendererTest.h +++ b/UnitTest/ElementRendererTest.h @@ -14,11 +14,11 @@ namespace UnitTest { Q_OBJECT private: - Renderer::ElementRenderer renderer; + Renderer::ElementRenderer& renderer; Renderer::ElementStyle& style; public: TestGLWidget(Renderer::ElementStyle& style, QWidget* parent = nullptr) - : QOpenGLWidget(parent), renderer(this), style(style) {}; + : QOpenGLWidget(parent), renderer(*Renderer::ElementRenderer::instance()), style(style) {}; void initializeGL() override { initializeOpenGLFunctions();