ElementRender改为单例
parent
6412d9c865
commit
c2dd8271e3
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue