Draw code optimization

This commit is contained in:
Martin Tůma 2018-11-13 01:01:36 +01:00
parent 61868b9544
commit 03fbf4643d
6 changed files with 28 additions and 26 deletions

View File

@ -113,7 +113,7 @@ static inline QPoint parameters(quint32 v1, quint32 v2)
return QPoint(zigzag32decode(v1), zigzag32decode(v2)); return QPoint(zigzag32decode(v1), zigzag32decode(v2));
} }
static void drawFeature(const Feature &feature, Style *style, int styleLayer, static void processFeature(const Feature &feature, Style *style, int styleLayer,
const QSizeF &factor, Tile &tile) const QSizeF &factor, Tile &tile)
{ {
if (!style->match(styleLayer, feature.tags())) if (!style->match(styleLayer, feature.tags()))
@ -151,7 +151,7 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
} }
} }
style->drawFeature(styleLayer, path, feature.tags(), tile); style->processFeature(styleLayer, path, feature.tags(), tile);
} }
static void drawLayer(const Layer &layer, Style *style, int styleLayer, static void drawLayer(const Layer &layer, Style *style, int styleLayer,
@ -163,8 +163,9 @@ static void drawLayer(const Layer &layer, Style *style, int styleLayer,
QSizeF factor(tile.size().width() / (qreal)layer.data()->extent(), QSizeF factor(tile.size().width() / (qreal)layer.data()->extent(),
tile.size().height() / (qreal)layer.data()->extent()); tile.size().height() / (qreal)layer.data()->extent());
style->setPainter(styleLayer, tile);
for (int i = 0; i < layer.features().size(); i++) for (int i = 0; i < layer.features().size(); i++)
drawFeature(layer.features().at(i), style, styleLayer, factor, tile); processFeature(layer.features().at(i), style, styleLayer, factor, tile);
} }
bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale, bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale,
@ -200,7 +201,7 @@ bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale,
drawLayer(*it, style, i, t); drawLayer(*it, style, i, t);
} }
t.render(); t.text().render(&t.painter());
return true; return true;
} }

View File

@ -366,30 +366,22 @@ bool Style::Layer::match(int zoom, const QVariantHash &tags) const
return _filter.match(tags); return _filter.match(tags);
} }
void Style::Layer::drawPath(int zoom, const QPainterPath &path, void Style::Layer::setPainter(int zoom, Tile &tile) const
Tile &tile) const
{ {
QPen pen(_paint.pen(_type, zoom)); QPen pen(_paint.pen(_type, zoom));
if (_type == Line && pen.style() == Qt::NoPen)
return;
QBrush brush(_paint.brush(_type, zoom)); QBrush brush(_paint.brush(_type, zoom));
if (_type == Fill && brush.style() == Qt::NoBrush)
return;
pen.setJoinStyle(_layout.lineJoin()); pen.setJoinStyle(_layout.lineJoin());
pen.setCapStyle(_layout.lineCap()); pen.setCapStyle(_layout.lineCap());
QPainter &p = tile.painter(); QPainter &p = tile.painter();
p.save();
p.setRenderHint(QPainter::Antialiasing, _paint.antialias(_type, zoom)); p.setRenderHint(QPainter::Antialiasing, _paint.antialias(_type, zoom));
p.setPen(pen); p.setPen(pen);
p.setBrush(brush); p.setBrush(brush);
p.setOpacity(_paint.opacity(_type, zoom)); p.setOpacity(_paint.opacity(_type, zoom));
p.drawPath(path);
p.restore();
} }
void Style::Layer::drawSymbol(int zoom, const QPainterPath &path, void Style::Layer::addSymbol(int zoom, const QPainterPath &path,
const QVariantHash &tags, Tile &tile) const const QVariantHash &tags, Tile &tile) const
{ {
if (_layout.keys().isEmpty()) if (_layout.keys().isEmpty())
@ -453,15 +445,23 @@ bool Style::match(int layer, const QVariantHash &tags)
return _styles.at(layer).match(_zoom, tags); return _styles.at(layer).match(_zoom, tags);
} }
void Style::drawFeature(int layer, const QPainterPath &path, void Style::setPainter(int layer, Tile &tile)
{
const Layer &sl = _styles.at(layer);
if (sl.isPath())
sl.setPainter(_zoom, tile);
}
void Style::processFeature(int layer, const QPainterPath &path,
const QVariantHash &tags, Tile &tile) const QVariantHash &tags, Tile &tile)
{ {
const Layer &sl = _styles.at(layer); const Layer &sl = _styles.at(layer);
if (sl.isPath()) if (sl.isPath())
sl.drawPath(_zoom, path, tile); tile.painter().drawPath(path);
else if (sl.isSymbol()) else if (sl.isSymbol())
sl.drawSymbol(_zoom, path, tags, tile); sl.addSymbol(_zoom, path, tags, tile);
} }
void Style::drawBackground(Tile &tile) void Style::drawBackground(Tile &tile)
@ -474,6 +474,8 @@ void Style::drawBackground(Tile &tile)
tile.painter().setBrush(Qt::lightGray); tile.painter().setBrush(Qt::lightGray);
tile.painter().setPen(Qt::NoPen); tile.painter().setPen(Qt::NoPen);
tile.painter().drawRect(rect); tile.painter().drawRect(rect);
} else if (_styles.first().isBackground()) } else if (_styles.first().isBackground()) {
_styles.first().drawPath(_zoom, path, tile); _styles.first().setPainter(_zoom, tile);
tile.painter().drawPath(path);
}
} }

View File

@ -29,7 +29,8 @@ public:
bool match(int layer, const QVariantHash &tags); bool match(int layer, const QVariantHash &tags);
void drawBackground(Tile &tile); void drawBackground(Tile &tile);
void drawFeature(int layer, const QPainterPath &path, void setPainter(int layer, Tile &tile);
void processFeature(int layer, const QPainterPath &path,
const QVariantHash &tags, Tile &tile); const QVariantHash &tags, Tile &tile);
private: private:
@ -43,8 +44,8 @@ private:
bool isSymbol() const {return (_type == Symbol);} bool isSymbol() const {return (_type == Symbol);}
bool match(int zoom, const QVariantHash &tags) const; bool match(int zoom, const QVariantHash &tags) const;
void drawPath(int zoom, const QPainterPath &path, Tile &tile) const; void setPainter(int zoom, Tile &tile) const;
void drawSymbol(int zoom, const QPainterPath &path, void addSymbol(int zoom, const QPainterPath &path,
const QVariantHash &tags, Tile &tile) const; const QVariantHash &tags, Tile &tile) const;
private: private:

View File

@ -140,7 +140,7 @@ Text::~Text()
delete _items[i]; delete _items[i];
} }
void Text::render(QPainter *painter) void Text::render(QPainter *painter) const
{ {
for (int i = 0; i < _items.size(); i++) { for (int i = 0; i < _items.size(); i++) {
const TextItem *ti = _items.at(i); const TextItem *ti = _items.at(i);

View File

@ -10,7 +10,7 @@ public:
: _sceneRect(QRectF(QPointF(0, 0), size)), _fontScale(scale) {} : _sceneRect(QRectF(QPointF(0, 0), size)), _fontScale(scale) {}
~Text(); ~Text();
void render(QPainter *painter); void render(QPainter *painter) const;
void addLabel(const QString &text, const QPointF &pos, const QFont &font, void addLabel(const QString &text, const QPointF &pos, const QFont &font,
const QPen &pen, qreal maxTextWidth); const QPen &pen, qreal maxTextWidth);

View File

@ -14,8 +14,6 @@ public:
Text &text() {return _text;} Text &text() {return _text;}
QPainter &painter() {return _painter;} QPainter &painter() {return _painter;}
void render() {_text.render(&_painter);}
private: private:
QSize _size; QSize _size;
Text _text; Text _text;