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)
{
this->renderer = new Renderer::ElementRenderer(this);
this->renderer = Renderer::ElementRenderer::instance();
QSurfaceFormat surfaceFormat;
surfaceFormat.setSamples(16);
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));
}
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<QImage, QPointF> Renderer::ElementRenderer::drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio)
{
std::unique_lock<std::mutex> lock(drawMutex);

View File

@ -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<QImage, QPointF> drawElement(const QPainterPath& path, const ElementStyle& style, float pixelRatio);
protected:
QOpenGLWidget* glWidget;
std::unique_ptr<QOpenGLShaderProgram> shader;
std::jthread thread;
QOffscreenSurface surface;
@ -36,6 +35,8 @@ namespace Renderer
const ElementStyle* style;
float pixelRatio;
std::pair<QImage, QPointF> result;
ElementRenderer();
};
}

View File

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