ElementRender改为单例

TaoZhang-Branch
wuyize 2023-03-09 16:21:55 +08:00
parent 6412d9c865
commit c2dd8271e3
4 changed files with 13 additions and 7 deletions

View File

@ -2,7 +2,7 @@
PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent) PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent)
{ {
this->renderer = new Renderer::ElementRenderer(this); this->renderer = Renderer::ElementRenderer::instance();
QSurfaceFormat surfaceFormat; QSurfaceFormat surfaceFormat;
surfaceFormat.setSamples(16); surfaceFormat.setSamples(16);
setFormat(surfaceFormat); setFormat(surfaceFormat);

View File

@ -114,8 +114,7 @@ QRectF calcBoundingRect(const QPainterPath& path, const std::vector<BaseStyle>&
return QRectF(QPointF(leftTop.x, leftTop.y), QPointF(rightBottom.x, rightBottom.y)); return QRectF(QPointF(leftTop.x, leftTop.y), QPointF(rightBottom.x, rightBottom.y));
} }
Renderer::ElementRenderer::ElementRenderer(QOpenGLWidget* glWidget) Renderer::ElementRenderer::ElementRenderer()
: glWidget(glWidget)
{ {
surface.create(); surface.create();
thread = std::jthread([&] { thread = std::jthread([&] {
@ -181,6 +180,12 @@ Renderer::ElementRenderer::ElementRenderer(QOpenGLWidget* glWidget)
std::this_thread::yield(); std::this_thread::yield();
} }
ElementRenderer* Renderer::ElementRenderer::instance()
{
static ElementRenderer renderer;
return &renderer;
}
std::pair<QImage, QPointF> Renderer::ElementRenderer::drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio) std::pair<QImage, QPointF> Renderer::ElementRenderer::drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio)
{ {
std::unique_lock<std::mutex> lock(drawMutex); std::unique_lock<std::mutex> lock(drawMutex);

View File

@ -10,7 +10,7 @@ namespace Renderer
class ElementRenderer : protected QOpenGLFunctions_4_5_Core class ElementRenderer : protected QOpenGLFunctions_4_5_Core
{ {
public: public:
ElementRenderer(QOpenGLWidget* glWidget); static ElementRenderer* instance();
/** /**
* @brief QImage * @brief QImage
@ -22,7 +22,6 @@ namespace Renderer
*/ */
std::pair<QImage, QPointF> drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio); std::pair<QImage, QPointF> drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio);
protected: protected:
QOpenGLWidget* glWidget;
std::unique_ptr<QOpenGLShaderProgram> shader; std::unique_ptr<QOpenGLShaderProgram> shader;
std::jthread thread; std::jthread thread;
QOffscreenSurface surface; QOffscreenSurface surface;
@ -36,6 +35,8 @@ namespace Renderer
const ElementStyle* style; const ElementStyle* style;
float pixelRatio; float pixelRatio;
std::pair<QImage, QPointF> result; std::pair<QImage, QPointF> result;
ElementRenderer();
}; };
} }

View File

@ -14,11 +14,11 @@ namespace UnitTest
{ {
Q_OBJECT Q_OBJECT
private: private:
Renderer::ElementRenderer renderer; Renderer::ElementRenderer& renderer;
Renderer::ElementStyle& style; Renderer::ElementStyle& style;
public: public:
TestGLWidget(Renderer::ElementStyle& style, QWidget* parent = nullptr) TestGLWidget(Renderer::ElementStyle& style, QWidget* parent = nullptr)
: QOpenGLWidget(parent), renderer(this), style(style) {}; : QOpenGLWidget(parent), renderer(*Renderer::ElementRenderer::instance()), style(style) {};
void initializeGL() override void initializeGL() override
{ {
initializeOpenGLFunctions(); initializeOpenGLFunctions();