From 7ee38fe125ef9eebf7023ffd6af3f7b9c98c62d0 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 15 Dec 2009 12:59:22 +1000 Subject: Add basic text benchmark. --- tests/benchmarks/declarative/text/text.pro | 8 ++ tests/benchmarks/declarative/text/tst_text.cpp | 120 +++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 tests/benchmarks/declarative/text/text.pro create mode 100644 tests/benchmarks/declarative/text/tst_text.cpp diff --git a/tests/benchmarks/declarative/text/text.pro b/tests/benchmarks/declarative/text/text.pro new file mode 100644 index 0000000..e4840b1 --- /dev/null +++ b/tests/benchmarks/declarative/text/text.pro @@ -0,0 +1,8 @@ +load(qttest_p4) +QT += script +TEMPLATE = app +TARGET = tst_text +macx:CONFIG -= app_bundle + +SOURCES += tst_text.cpp + diff --git a/tests/benchmarks/declarative/text/tst_text.cpp b/tests/benchmarks/declarative/text/tst_text.cpp new file mode 100644 index 0000000..4fd84e6 --- /dev/null +++ b/tests/benchmarks/declarative/text/tst_text.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include + +class tst_text : public QObject +{ + Q_OBJECT +public: + tst_text() {} + +private slots: + void layout(); + void paintToPixmap(); +}; + +QSize setupTextLayout(QTextLayout *layout) +{ + bool wrap = true; + int wrapWidth = 300; + layout->setCacheEnabled(true); + + int height = 0; + qreal widthUsed = 0; + qreal lineWidth = 0; + + //set manual width + if (wrap) + lineWidth = wrapWidth; + + layout->beginLayout(); + + while (1) { + QTextLine line = layout->createLine(); + if (!line.isValid()) + break; + + if (wrap) + line.setLineWidth(lineWidth); + } + layout->endLayout(); + + for (int i = 0; i < layout->lineCount(); ++i) { + QTextLine line = layout->lineAt(i); + widthUsed = qMax(widthUsed, line.naturalTextWidth()); + line.setPosition(QPointF(0, height)); + height += int(line.height()); + } + return QSize(qCeil(widthUsed), height); +} + +void tst_text::layout() +{ + //get rid of initialization effects + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + setupTextLayout(&layout); + + QBENCHMARK { + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + setupTextLayout(&layout); + } +} + +void tst_text::paintToPixmap() +{ + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + QSize size = setupTextLayout(&layout); + + QBENCHMARK { + QPixmap img(size); + img.fill(Qt::transparent); + QPainter p(&img); + layout.draw(&p, QPointF(0, 0)); + } +} + +QTEST_MAIN(tst_text) +#include "tst_text.moc" -- cgit v0.12 From 8cb4d7cf6a0aec02081898b1912a772b79e9dbfc Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 15 Dec 2009 13:21:37 +1000 Subject: Little smarter bufferCache --- .../graphicsitems/qmlgraphicslistview.cpp | 35 +++++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/declarative/graphicsitems/qmlgraphicslistview.cpp b/src/declarative/graphicsitems/qmlgraphicslistview.cpp index cdb3c49..16e9d6e 100644 --- a/src/declarative/graphicsitems/qmlgraphicslistview.cpp +++ b/src/declarative/graphicsitems/qmlgraphicslistview.cpp @@ -181,6 +181,7 @@ public: , highlightMoveSpeed(400), highlightResizeSpeed(400), highlightRange(QmlGraphicsListView::NoHighlightRange) , snapMode(QmlGraphicsListView::NoSnap), overshootDist(0.0) , footerComponent(0), footer(0), headerComponent(0), header(0) + , bufferMode(NoBuffer) , ownModel(false), wrap(false), autoHighlight(true), haveHighlightRange(false) , correctFlick(true), inFlickCorrection(false), lazyRelease(false) {} @@ -423,7 +424,7 @@ public: } } - void refill(qreal from, qreal to); + void refill(qreal from, qreal to, bool doBuffer = false); void layout(); void updateUnrequestedIndexes(); void updateUnrequestedPositions(); @@ -475,6 +476,8 @@ public: FxListItem *footer; QmlComponent *headerComponent; FxListItem *header; + enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 }; + BufferMode bufferMode; bool ownModel : 1; bool wrap : 1; @@ -574,13 +577,20 @@ void QmlGraphicsListViewPrivate::releaseItem(FxListItem *item) delete item; } -void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) +void QmlGraphicsListViewPrivate::refill(qreal from, qreal to, bool doBuffer) { Q_Q(QmlGraphicsListView); if (!isValid() || !q->isComponentComplete()) return; - from -= buffer; - to += buffer; + qreal bufferFrom = from - buffer; + qreal bufferTo = to + buffer; + qreal fillFrom = from; + qreal fillTo = to; + if (doBuffer && (bufferMode & BufferAfter)) + fillTo = bufferTo; + if (doBuffer && (bufferMode & BufferBefore)) + fillFrom = bufferFrom; + int modelIndex = visibleIndex; qreal itemEnd = visiblePos-1; if (!visibleItems.isEmpty()) { @@ -595,7 +605,7 @@ void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) bool changed = false; FxListItem *item = 0; int pos = itemEnd + 1; - while (modelIndex < model->count() && pos <= to) { + while (modelIndex < model->count() && pos <= fillTo) { //qDebug() << "refill: append item" << modelIndex; if (!(item = createItem(modelIndex))) break; @@ -604,8 +614,10 @@ void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) visibleItems.append(item); ++modelIndex; changed = true; + if (doBuffer) // never buffer more than one item per frame + break; } - while (visibleIndex > 0 && visibleIndex <= model->count() && visiblePos > from) { + while (visibleIndex > 0 && visibleIndex <= model->count() && visiblePos > fillFrom) { //qDebug() << "refill: prepend item" << visibleIndex-1 << "current top pos" << visiblePos; if (!(item = createItem(visibleIndex-1))) break; @@ -614,10 +626,12 @@ void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) item->setPosition(visiblePos); visibleItems.prepend(item); changed = true; + if (doBuffer) // never buffer more than one item per frame + break; } if (!lazyRelease || !changed) { // avoid destroying items in the same frame that we create - while (visibleItems.count() > 1 && (item = visibleItems.first()) && item->endPosition() < from) { + while (visibleItems.count() > 1 && (item = visibleItems.first()) && item->endPosition() < bufferFrom) { if (item->attached->delayRemove()) break; //qDebug() << "refill: remove first" << visibleIndex << "top end pos" << item->endPosition(); @@ -627,7 +641,7 @@ void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) releaseItem(item); changed = true; } - while (visibleItems.count() > 1 && (item = visibleItems.last()) && item->position() > to) { + while (visibleItems.count() > 1 && (item = visibleItems.last()) && item->position() > bufferTo) { if (item->attached->delayRemove()) break; //qDebug() << "refill: remove last" << visibleIndex+visibleItems.count()-1; @@ -647,6 +661,8 @@ void QmlGraphicsListViewPrivate::refill(qreal from, qreal to) if (footer) updateFooter(); updateViewport(); + } else if (!doBuffer && buffer && bufferMode != NoBuffer) { + refill(from, to, true); } lazyRelease = false; } @@ -1923,11 +1939,13 @@ void QmlGraphicsListView::viewportMoved() if ((minY - d->_moveY.value() < height()/2 || d->flickTargetY - d->_moveY.value() < height()/2) && minY != d->flickTargetY) d->flickY(-d->verticalVelocity.value()); + d->bufferMode = QmlGraphicsListViewPrivate::BufferBefore; } else if (d->velocityY < 0) { const qreal maxY = maxYExtent(); if ((d->_moveY.value() - maxY < height()/2 || d->_moveY.value() - d->flickTargetY < height()/2) && maxY != d->flickTargetY) d->flickY(-d->verticalVelocity.value()); + d->bufferMode = QmlGraphicsListViewPrivate::BufferAfter; } } @@ -2548,6 +2566,7 @@ void QmlGraphicsListView::animStopped() { Q_D(QmlGraphicsListView); d->moveReason = QmlGraphicsListViewPrivate::Other; + d->bufferMode = QmlGraphicsListViewPrivate::NoBuffer; } QmlGraphicsListViewAttached *QmlGraphicsListView::qmlAttachedProperties(QObject *obj) -- cgit v0.12 From 76f0ff2e192d18f8e9aed6d7f56c864a1678133c Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 15 Dec 2009 13:55:16 +1000 Subject: Expand text benchmark. --- src/declarative/graphicsitems/qmlgraphicstext.cpp | 2 - tests/benchmarks/declarative/text/text.pro | 1 - tests/benchmarks/declarative/text/tst_text.cpp | 47 ++++++++++++++++++++--- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/declarative/graphicsitems/qmlgraphicstext.cpp b/src/declarative/graphicsitems/qmlgraphicstext.cpp index 03baaae..6ec72ef 100644 --- a/src/declarative/graphicsitems/qmlgraphicstext.cpp +++ b/src/declarative/graphicsitems/qmlgraphicstext.cpp @@ -578,8 +578,6 @@ QSize QmlGraphicsTextPrivate::setupTextLayout(QTextLayout *layout) Q_Q(QmlGraphicsText); layout->setCacheEnabled(true); - QFontMetrics fm = QFontMetrics(font); - int height = 0; qreal widthUsed = 0; qreal lineWidth = 0; diff --git a/tests/benchmarks/declarative/text/text.pro b/tests/benchmarks/declarative/text/text.pro index e4840b1..3320f53 100644 --- a/tests/benchmarks/declarative/text/text.pro +++ b/tests/benchmarks/declarative/text/text.pro @@ -1,5 +1,4 @@ load(qttest_p4) -QT += script TEMPLATE = app TARGET = tst_text macx:CONFIG -= app_bundle diff --git a/tests/benchmarks/declarative/text/tst_text.cpp b/tests/benchmarks/declarative/text/tst_text.cpp index 4fd84e6..5c57633 100644 --- a/tests/benchmarks/declarative/text/tst_text.cpp +++ b/tests/benchmarks/declarative/text/tst_text.cpp @@ -44,16 +44,25 @@ #include #include #include +#include class tst_text : public QObject { Q_OBJECT public: - tst_text() {} + tst_text() + { + m_text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; + } private slots: void layout(); - void paintToPixmap(); + void paintLayoutToPixmap(); + void document(); + void paintDocToPixmap(); + +private: + QString m_text; }; QSize setupTextLayout(QTextLayout *layout) @@ -94,18 +103,18 @@ QSize setupTextLayout(QTextLayout *layout) void tst_text::layout() { //get rid of initialization effects - QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + QTextLayout layout(m_text); setupTextLayout(&layout); QBENCHMARK { - QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + QTextLayout layout(m_text); setupTextLayout(&layout); } } -void tst_text::paintToPixmap() +void tst_text::paintLayoutToPixmap() { - QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + QTextLayout layout(m_text); QSize size = setupTextLayout(&layout); QBENCHMARK { @@ -116,5 +125,31 @@ void tst_text::paintToPixmap() } } +void tst_text::document() +{ + QTextControl *control = new QTextControl(m_text); + + QBENCHMARK { + QTextControl *control = new QTextControl; + QTextDocument *doc = control->document(); + doc->setHtml(m_text); + } +} + +void tst_text::paintDocToPixmap() +{ + QTextControl *control = new QTextControl; + QTextDocument *doc = control->document(); + doc->setHtml(m_text); + QSize size = doc->size().toSize(); + + QBENCHMARK { + QPixmap img(size); + img.fill(Qt::transparent); + QPainter p(&img); + control->drawContents(&p, QRectF(QPointF(0, 0), QSizeF(size))); + } +} + QTEST_MAIN(tst_text) #include "tst_text.moc" -- cgit v0.12