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();