GPU caching for better performance after many strokes
This commit is contained in:
@@ -3,13 +3,16 @@
|
|||||||
#include <QColor>
|
#include <QColor>
|
||||||
#include <QPointF>
|
#include <QPointF>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <QCanvasPath>
|
||||||
|
|
||||||
namespace ZShell::internal {
|
namespace ZShell::internal {
|
||||||
|
|
||||||
struct Stroke {
|
struct Stroke {
|
||||||
QVector<QPointF> points;
|
QVector<QPointF> points;
|
||||||
|
QCanvasPath path;
|
||||||
QColor color;
|
QColor color;
|
||||||
float width;
|
float width;
|
||||||
|
int groupId = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "strokecanvasitem.hpp"
|
#include "strokecanvasitem.hpp"
|
||||||
#include "strokecanvasrenderer.hpp"
|
#include "strokecanvasrenderer.hpp"
|
||||||
|
#include <qcanvaspainter.h>
|
||||||
#include <qnamespace.h>
|
#include <qnamespace.h>
|
||||||
|
|
||||||
namespace ZShell::internal {
|
namespace ZShell::internal {
|
||||||
@@ -27,6 +28,42 @@ static bool shouldAddPoint(
|
|||||||
>= minDistance * minDistance;
|
>= minDistance * minDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QCanvasPath buildStrokePath(const QVector<QPointF> &points) {
|
||||||
|
QCanvasPath path;
|
||||||
|
|
||||||
|
if (points.size() == 1) {
|
||||||
|
// Single point — store as a tiny circle so the group can still be cached
|
||||||
|
path.circle(points[0], 0); // radius 0; actual width applied at draw time
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto catmullToBezier = [](
|
||||||
|
const QPointF &p0, const QPointF &p1,
|
||||||
|
const QPointF &p2, const QPointF &p3,
|
||||||
|
float tension,
|
||||||
|
QPointF &cp1, QPointF &cp2)
|
||||||
|
{
|
||||||
|
cp1 = p1 + (p2 - p0) * tension / 3.0f;
|
||||||
|
cp2 = p2 - (p3 - p1) * tension / 3.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
const float tension = 0.5f;
|
||||||
|
path.moveTo(points[0]);
|
||||||
|
|
||||||
|
for (int i = 0; i < points.size() - 1; ++i) {
|
||||||
|
const QPointF &p0 = points[qMax(i - 1, 0)];
|
||||||
|
const QPointF &p1 = points[i];
|
||||||
|
const QPointF &p2 = points[i + 1];
|
||||||
|
const QPointF &p3 = points[qMin(i + 2, points.size() - 1)];
|
||||||
|
|
||||||
|
QPointF cp1, cp2;
|
||||||
|
catmullToBezier(p0, p1, p2, p3, tension, cp1, cp2);
|
||||||
|
path.bezierCurveTo(cp1, cp2, p2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
void StrokeCanvasItem::setPenColor(const QColor &color) {
|
void StrokeCanvasItem::setPenColor(const QColor &color) {
|
||||||
if (m_penColor == color)
|
if (m_penColor == color)
|
||||||
return;
|
return;
|
||||||
@@ -129,16 +166,19 @@ void StrokeCanvasItem::endStroke() {
|
|||||||
if (m_currentStroke.points.isEmpty())
|
if (m_currentStroke.points.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_strokes.append(m_currentStroke);
|
m_currentStroke.path = buildStrokePath(m_currentStroke.points);
|
||||||
|
m_currentStroke.groupId = m_nextGroupId++;
|
||||||
m_currentStroke.points.clear();
|
m_currentStroke.points.clear();
|
||||||
|
|
||||||
|
m_strokes.append(m_currentStroke);
|
||||||
|
m_currentStroke = {};
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StrokeCanvasItem::clear() {
|
void StrokeCanvasItem::clear() {
|
||||||
m_strokes.clear();
|
m_strokes.clear();
|
||||||
m_currentStroke.points.clear();
|
m_currentStroke = {};
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ QPointF m_hoverPoint;
|
|||||||
QColor m_penColor = Qt::white;
|
QColor m_penColor = Qt::white;
|
||||||
float m_penWidth = 4.f;
|
float m_penWidth = 4.f;
|
||||||
|
|
||||||
|
int m_nextGroupId = 0;
|
||||||
QVector<Stroke> m_strokes;
|
QVector<Stroke> m_strokes;
|
||||||
Stroke m_currentStroke;
|
Stroke m_currentStroke;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -73,7 +73,18 @@ void StrokeCanvasRenderer::synchronizeData(QCanvasPainterItem *item) {
|
|||||||
m_penColor = canvas->m_penColor;
|
m_penColor = canvas->m_penColor;
|
||||||
m_penWidth = canvas->m_penWidth;
|
m_penWidth = canvas->m_penWidth;
|
||||||
|
|
||||||
m_strokes = canvas->m_strokes;
|
// Only copy strokes the renderer hasn't seen yet
|
||||||
|
while (m_strokes.size() < canvas->m_strokes.size())
|
||||||
|
m_strokes.append(canvas->m_strokes[m_strokes.size()]);
|
||||||
|
|
||||||
|
// Handle clear()
|
||||||
|
if (canvas->m_strokes.isEmpty() && !m_strokes.isEmpty()) {
|
||||||
|
for (const auto &stroke : m_strokes)
|
||||||
|
if (stroke.groupId >= 0)
|
||||||
|
m_pendingGroupRemovals.append(stroke.groupId);
|
||||||
|
m_strokes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
m_currentStroke = canvas->m_currentStroke;
|
m_currentStroke = canvas->m_currentStroke;
|
||||||
|
|
||||||
m_hoverVisible = canvas->m_hoverVisible;
|
m_hoverVisible = canvas->m_hoverVisible;
|
||||||
@@ -81,26 +92,30 @@ void StrokeCanvasRenderer::synchronizeData(QCanvasPainterItem *item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void StrokeCanvasRenderer::paint(QCanvasPainter *painter) {
|
void StrokeCanvasRenderer::paint(QCanvasPainter *painter) {
|
||||||
|
for (int id : m_pendingGroupRemovals)
|
||||||
|
painter->removePathGroup(id);
|
||||||
|
m_pendingGroupRemovals.clear();
|
||||||
|
|
||||||
painter->clearRect(0, 0, width(), height());
|
painter->clearRect(0, 0, width(), height());
|
||||||
|
|
||||||
for (const auto &stroke : m_strokes)
|
for (const auto &stroke : m_strokes) {
|
||||||
drawStroke(painter, stroke.points, stroke.color, stroke.width);
|
painter->setStrokeStyle(stroke.color);
|
||||||
|
painter->setFillStyle(stroke.color);
|
||||||
|
painter->setLineWidth(stroke.width);
|
||||||
|
painter->setLineCap(QCanvasPainter::LineCap::Round);
|
||||||
|
painter->setLineJoin(QCanvasPainter::LineJoin::Round);
|
||||||
|
|
||||||
drawStroke(
|
if (stroke.path.commandsSize() == 1) {
|
||||||
painter,
|
painter->fill(stroke.path, stroke.groupId);
|
||||||
m_currentStroke.points,
|
} else {
|
||||||
m_currentStroke.color,
|
painter->stroke(stroke.path, stroke.groupId);
|
||||||
m_currentStroke.width
|
}
|
||||||
);
|
|
||||||
|
|
||||||
if (m_hoverVisible) {
|
|
||||||
drawDot(
|
|
||||||
painter,
|
|
||||||
m_hoverPoint,
|
|
||||||
m_penColor,
|
|
||||||
m_penWidth
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawStroke(painter, m_currentStroke.points, m_currentStroke.color, m_currentStroke.width);
|
||||||
|
|
||||||
|
if (m_hoverVisible)
|
||||||
|
drawDot(painter, m_hoverPoint, m_penColor, m_penWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QCanvasPainterItemRenderer>
|
#include <QCanvasPainterItemRenderer>
|
||||||
|
#include <qcontainerfwd.h>
|
||||||
#include "stroke.hpp"
|
#include "stroke.hpp"
|
||||||
|
|
||||||
namespace ZShell::internal {
|
namespace ZShell::internal {
|
||||||
@@ -19,6 +20,7 @@ QPointF m_hoverPoint;
|
|||||||
|
|
||||||
QVector<Stroke> m_strokes;
|
QVector<Stroke> m_strokes;
|
||||||
Stroke m_currentStroke;
|
Stroke m_currentStroke;
|
||||||
|
QVector<int> m_pendingGroupRemovals;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user