tests/benchmarks/qgraphicsview/tst_qgraphicsview.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    60 
    60 
    61 class QEventWaiter : public QEventLoop
    61 class QEventWaiter : public QEventLoop
    62 {
    62 {
    63 public:
    63 public:
    64     QEventWaiter(QObject *receiver, QEvent::Type type)
    64     QEventWaiter(QObject *receiver, QEvent::Type type)
    65 	: waiting(false), t(type)
    65         : waiting(false), t(type)
    66     {
    66     {
    67 	receiver->installEventFilter(this);
    67         receiver->installEventFilter(this);
    68     }
    68     }
    69 
    69 
    70     void wait()
    70     void wait()
    71     {
    71     {
    72 	waiting = true;
    72         waiting = true;
    73 	exec();
    73         exec();
    74     }
    74     }
    75 
    75 
    76     bool eventFilter(QObject *receiver, QEvent *event)
    76     bool eventFilter(QObject *receiver, QEvent *event)
    77     {
    77     {
    78 	Q_UNUSED(receiver);
    78         Q_UNUSED(receiver);
    79 	if (waiting && event->type() == t) {
    79         if (waiting && event->type() == t) {
    80 	    waiting = false;
    80             waiting = false;
    81 	    exit();
    81             exit();
    82 	}
    82         }
    83 	return false;
    83         return false;
    84     }
    84     }
    85 
    85 
    86 private:
    86 private:
    87     bool waiting;
    87     bool waiting;
    88     QEvent::Type t;
    88     QEvent::Type t;
   164 #endif
   164 #endif
   165 
   165 
   166     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   166     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   167     QPainter painter(&image);
   167     QPainter painter(&image);
   168     QBENCHMARK {
   168     QBENCHMARK {
   169 	view.viewport()->render(&painter);
   169         view.viewport()->render(&painter);
   170     }
   170     }
   171 }
   171 }
       
   172 
       
   173 #ifdef Q_OS_SYMBIAN
       
   174 #  define DEEP_STACKING_COUNT 200
       
   175 #else
       
   176 #  define DEEP_STACKING_COUNT 1000
       
   177 #endif
   172 
   178 
   173 void tst_QGraphicsView::paintDeepStackingItems()
   179 void tst_QGraphicsView::paintDeepStackingItems()
   174 {
   180 {
   175     QGraphicsScene scene(0, 0, 100, 100);
   181     QGraphicsScene scene(0, 0, 100, 100);
   176     QGraphicsRectItem *item = scene.addRect(0, 0, 10, 10);
   182     QGraphicsRectItem *item = scene.addRect(0, 0, 10, 10);
   177     QGraphicsRectItem *lastRect = item;
   183     QGraphicsRectItem *lastRect = item;
   178     for (int i = 0; i < 1000; ++i) {
   184     for (int i = 0; i < DEEP_STACKING_COUNT; ++i) {
   179 	QGraphicsRectItem *rect = scene.addRect(0, 0, 10, 10);
   185         QGraphicsRectItem *rect = scene.addRect(0, 0, 10, 10);
   180 	rect->setPos(1, 1);
   186         rect->setPos(1, 1);
   181 	rect->setParentItem(lastRect);	
   187         rect->setParentItem(lastRect);
   182 	lastRect = rect;
   188         lastRect = rect;
   183     }
   189     }
   184 
   190 
   185     QGraphicsView view(&scene);
   191     QGraphicsView view(&scene);
   186     view.show();
   192     view.show();
   187     view.resize(100, 100);
   193     view.resize(100, 100);
   190 #endif
   196 #endif
   191 
   197 
   192     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   198     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   193     QPainter painter(&image);
   199     QPainter painter(&image);
   194     QBENCHMARK {
   200     QBENCHMARK {
   195 	view.viewport()->render(&painter);
   201         view.viewport()->render(&painter);
   196     }
   202     }
   197 }
   203 }
   198 
   204 
   199 void tst_QGraphicsView::paintDeepStackingItems_clipped()
   205 void tst_QGraphicsView::paintDeepStackingItems_clipped()
   200 {
   206 {
   201     QGraphicsScene scene(0, 0, 100, 100);
   207     QGraphicsScene scene(0, 0, 100, 100);
   202     QGraphicsRectItem *item = scene.addRect(0, 0, 10, 10);
   208     QGraphicsRectItem *item = scene.addRect(0, 0, 10, 10);
   203     item->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
   209     item->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
   204     QGraphicsRectItem *lastRect = item;
   210     QGraphicsRectItem *lastRect = item;
   205     for (int i = 0; i < 1000; ++i) {
   211     for (int i = 0; i < DEEP_STACKING_COUNT; ++i) {
   206 	QGraphicsRectItem *rect = scene.addRect(0, 0, 10, 10);
   212         QGraphicsRectItem *rect = scene.addRect(0, 0, 10, 10);
   207 	rect->setPos(1, 1);
   213         rect->setPos(1, 1);
   208 	rect->setParentItem(lastRect);	
   214         rect->setParentItem(lastRect);
   209 	lastRect = rect;
   215         lastRect = rect;
   210     }
   216     }
   211 
   217 
   212     QGraphicsView view(&scene);
   218     QGraphicsView view(&scene);
   213     view.show();
   219     view.show();
   214     view.resize(100, 100);
   220     view.resize(100, 100);
   217 #endif
   223 #endif
   218 
   224 
   219     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   225     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   220     QPainter painter(&image);
   226     QPainter painter(&image);
   221     QBENCHMARK {
   227     QBENCHMARK {
   222 	view.viewport()->render(&painter);
   228         view.viewport()->render(&painter);
   223     }
   229     }
   224 }
   230 }
   225 
   231 
   226 void tst_QGraphicsView::moveSingleItem()
   232 void tst_QGraphicsView::moveSingleItem()
   227 {
   233 {
   237 
   243 
   238     QEventWaiter waiter(view.viewport(), QEvent::Paint);
   244     QEventWaiter waiter(view.viewport(), QEvent::Paint);
   239     int n = 1;
   245     int n = 1;
   240     QBENCHMARK {
   246     QBENCHMARK {
   241         item->setPos(25 * n, 25 * n);
   247         item->setPos(25 * n, 25 * n);
   242 	waiter.wait();
   248         waiter.wait();
   243 	n = n ? 0 : 1;
   249         n = n ? 0 : 1;
   244     }
   250     }
   245 }
   251 }
   246 
   252 
   247 void tst_QGraphicsView::mapPointToScene_data()
   253 void tst_QGraphicsView::mapPointToScene_data()
   248 {
   254 {
   380 #endif
   386 #endif
   381     tester.setAntialias(antialias);
   387     tester.setAntialias(antialias);
   382     tester.setOpenGL(opengl);
   388     tester.setOpenGL(opengl);
   383     tester.setOperation(ChipTester::Operation(operation));
   389     tester.setOperation(ChipTester::Operation(operation));
   384     QBENCHMARK {
   390     QBENCHMARK {
   385 	tester.runBenchmark();
   391         tester.runBenchmark();
   386     }
   392     }
   387 }
   393 }
   388 
   394 
   389 static void addChildHelper(QGraphicsItem *parent, int n, bool rotate)
   395 static void addChildHelper(QGraphicsItem *parent, int n, bool rotate)
   390 {
   396 {
   391     if (!n)
   397     if (!n)
   392 	return;
   398         return;
   393     QGraphicsRectItem *item = new QGraphicsRectItem(QRectF(0, 0, 50, 50), parent);
   399     QGraphicsRectItem *item = new QGraphicsRectItem(QRectF(0, 0, 50, 50), parent);
   394     item->setPos(10, 10);
   400     item->setPos(10, 10);
   395     if (rotate)
   401     if (rotate)
   396 	item->rotate(10);
   402         item->rotate(10);
   397     addChildHelper(item, n - 1, rotate);
   403     addChildHelper(item, n - 1, rotate);
   398 }
   404 }
   399 
   405 
   400 void tst_QGraphicsView::deepNesting_data()
   406 void tst_QGraphicsView::deepNesting_data()
   401 {
   407 {
   419     QFETCH(bool, sortCache);
   425     QFETCH(bool, sortCache);
   420     QFETCH(bool, bsp);
   426     QFETCH(bool, bsp);
   421 
   427 
   422     QGraphicsScene scene;
   428     QGraphicsScene scene;
   423     for (int y = 0; y < 15; ++y) {
   429     for (int y = 0; y < 15; ++y) {
   424 	for (int x = 0; x < 15; ++x) {
   430         for (int x = 0; x < 15; ++x) {
   425 	    QGraphicsItem *item1 = scene.addRect(QRectF(0, 0, 50, 50));
   431             QGraphicsItem *item1 = scene.addRect(QRectF(0, 0, 50, 50));
   426 	    if (rotate) item1->rotate(10);
   432             if (rotate) item1->rotate(10);
   427 	    item1->setPos(x * 25, y * 25);
   433             item1->setPos(x * 25, y * 25);
   428 	    addChildHelper(item1, 30, rotate);
   434             addChildHelper(item1, 30, rotate);
   429 	}
   435         }
   430     }
   436     }
   431     scene.setItemIndexMethod(bsp ? QGraphicsScene::BspTreeIndex : QGraphicsScene::NoIndex);
   437     scene.setItemIndexMethod(bsp ? QGraphicsScene::BspTreeIndex : QGraphicsScene::NoIndex);
   432     scene.setSortCacheEnabled(sortCache);
   438     scene.setSortCacheEnabled(sortCache);
   433 
   439 
   434     QGraphicsView view(&scene);
   440     QGraphicsView view(&scene);
   439 #endif
   445 #endif
   440     QTest::qWait(250);
   446     QTest::qWait(250);
   441 
   447 
   442     QBENCHMARK {
   448     QBENCHMARK {
   443 #ifdef CALLGRIND_DEBUG
   449 #ifdef CALLGRIND_DEBUG
   444 	CALLGRIND_START_INSTRUMENTATION
   450         CALLGRIND_START_INSTRUMENTATION
   445 #endif
   451 #endif
   446 	view.viewport()->repaint();
   452         view.viewport()->repaint();
   447 #ifdef CALLGRIND_DEBUG
   453 #ifdef CALLGRIND_DEBUG
   448 	CALLGRIND_STOP_INSTRUMENTATION
   454         CALLGRIND_STOP_INSTRUMENTATION
   449 #endif
   455 #endif
   450     }
   456     }
   451 }
   457 }
   452 
   458 
   453 class AnimatedPixmapItem : public QGraphicsPixmapItem
   459 class AnimatedPixmapItem : public QGraphicsPixmapItem
   454 {
   460 {
   455 public:
   461 public:
   456     AnimatedPixmapItem(int x, int y, bool rot, bool scal, QGraphicsItem *parent = 0)
   462     AnimatedPixmapItem(int x, int y, bool rot, bool scal, QGraphicsItem *parent = 0)
   457         : QGraphicsPixmapItem(parent), rotateFactor(0), scaleFactor(0)
   463         : QGraphicsPixmapItem(parent), rotateFactor(0), scaleFactor(0)
   458     {
   464     {
   459 	rotate = rot;
   465         rotate = rot;
   460 	scale = scal;
   466         scale = scal;
   461 	xspeed = x;
   467         xspeed = x;
   462 	yspeed = y;
   468         yspeed = y;
   463     }
   469     }
   464 
   470 
   465 protected:
   471 protected:
   466     void advance(int i)
   472     void advance(int i)
   467     { 
   473     {
   468 	if (!i)
   474         if (!i)
   469 	    return;
   475             return;
   470 	int x = int(pos().x()) + pixmap().width();
   476         int x = int(pos().x()) + pixmap().width();
   471 	x += xspeed;
   477         x += xspeed;
   472 	x = (x % (300 + pixmap().width() * 2)) - pixmap().width();
   478         x = (x % (300 + pixmap().width() * 2)) - pixmap().width();
   473 	int y = int(pos().y()) + pixmap().width();
   479         int y = int(pos().y()) + pixmap().width();
   474 	y += yspeed;
   480         y += yspeed;
   475 	y = (y % (300 + pixmap().width() * 2)) - pixmap().width();
   481         y = (y % (300 + pixmap().width() * 2)) - pixmap().width();
   476 	setPos(x, y);
   482         setPos(x, y);
   477 
   483 
   478 	int rot = rotateFactor;
   484         int rot = rotateFactor;
   479 	int sca = scaleFactor;
   485         int sca = scaleFactor;
   480         if (rotate)
   486         if (rotate)
   481 	    rotateFactor = 1 + (rot + xspeed) % 360;
   487             rotateFactor = 1 + (rot + xspeed) % 360;
   482         if (scale)
   488         if (scale)
   483 	    scaleFactor = 1 + (sca + yspeed) % 50;
   489             scaleFactor = 1 + (sca + yspeed) % 50;
   484 
   490 
   485 	if (rotate || scale) {
   491         if (rotate || scale) {
   486 	    qreal s = 0.5 + scaleFactor / 50.0;
   492             qreal s = 0.5 + scaleFactor / 50.0;
   487 	    setTransform(QTransform().rotate(rotateFactor).scale(s, s));
   493             setTransform(QTransform().rotate(rotateFactor).scale(s, s));
   488 	}
   494         }
   489     }
   495     }
   490 
   496 
   491 private:
   497 private:
   492     int xspeed;
   498     int xspeed;
   493     int yspeed;
   499     int yspeed;
   541     view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   547     view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   542     view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   548     view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   543     view.show();
   549     view.show();
   544 
   550 
   545     QPixmap pix(":/images/designer.png");
   551     QPixmap pix(":/images/designer.png");
   546     QVERIFY(!pix.isNull());   
   552     QVERIFY(!pix.isNull());
   547 
   553 
   548     QList<QGraphicsItem *> items;
   554     QList<QGraphicsItem *> items;
   549     QFile file(":/random.data");
   555     QFile file(":/random.data");
   550     QVERIFY(file.open(QIODevice::ReadOnly));
   556     QVERIFY(file.open(QIODevice::ReadOnly));
   551     QDataStream str(&file);
   557     QDataStream str(&file);
   552     for (int i = 0; i < 100; ++i) {
   558     for (int i = 0; i < 100; ++i) {
   553 	AnimatedPixmapItem *item;
   559         AnimatedPixmapItem *item;
   554 	if (direction == 0) item = new AnimatedPixmapItem((i % 4) + 1, 0, rotation, scale);
   560         if (direction == 0) item = new AnimatedPixmapItem((i % 4) + 1, 0, rotation, scale);
   555 	if (direction == 1) item = new AnimatedPixmapItem(0, (i % 4) + 1, rotation, scale);
   561         if (direction == 1) item = new AnimatedPixmapItem(0, (i % 4) + 1, rotation, scale);
   556 	if (direction == 2) item = new AnimatedPixmapItem((i % 4) + 1, (i % 4) + 1, rotation, scale);
   562         if (direction == 2) item = new AnimatedPixmapItem((i % 4) + 1, (i % 4) + 1, rotation, scale);
   557 	item->setPixmap(pix);
   563         item->setPixmap(pix);
   558         int rnd1, rnd2;
   564         int rnd1, rnd2;
   559         str >> rnd1 >> rnd2;
   565         str >> rnd1 >> rnd2;
   560 	item->setPos(-pix.width() + rnd1 % (view.width() + pix.width()),
   566         item->setPos(-pix.width() + rnd1 % (view.width() + pix.width()),
   561 		     -pix.height() + rnd2 % (view.height() + pix.height()));
   567                      -pix.height() + rnd2 % (view.height() + pix.height()));
   562 	scene.addItem(item);
   568         scene.addItem(item);
   563     }
   569     }
   564 
   570 
   565     view.count = 0;
   571     view.count = 0;
   566 
   572 
   567     QBENCHMARK {
   573     QBENCHMARK {
   568 #ifdef CALLGRIND_DEBUG
   574 #ifdef CALLGRIND_DEBUG
   569 	CALLGRIND_START_INSTRUMENTATION
   575         CALLGRIND_START_INSTRUMENTATION
   570 #endif
   576 #endif
   571 	for (int i = 0; i < 100; ++i) {
   577         for (int i = 0; i < 100; ++i) {
   572 	    scene.advance();
   578             scene.advance();
   573 	    while (view.count < (i+1))
   579             while (view.count < (i+1))
   574 		qApp->processEvents();
   580                 qApp->processEvents();
   575 	}
   581         }
   576 #ifdef CALLGRIND_DEBUG
   582 #ifdef CALLGRIND_DEBUG
   577 	CALLGRIND_STOP_INSTRUMENTATION
   583         CALLGRIND_STOP_INSTRUMENTATION
   578 #endif
   584 #endif
   579     }
   585     }
   580 }
   586 }
   581 
   587 
   582 class AnimatedTextItem : public QGraphicsSimpleTextItem
   588 class AnimatedTextItem : public QGraphicsSimpleTextItem
   583 {
   589 {
   584 public:
   590 public:
   585     AnimatedTextItem(int x, int y, bool rot, bool scal, QGraphicsItem *parent = 0)
   591     AnimatedTextItem(int x, int y, bool rot, bool scal, QGraphicsItem *parent = 0)
   586         : QGraphicsSimpleTextItem(parent), rotateFactor(0), scaleFactor(25)
   592         : QGraphicsSimpleTextItem(parent), rotateFactor(0), scaleFactor(25)
   587     {
   593     {
   588 	setText("River of text");
   594         setText("River of text");
   589 	rotate = rot;
   595         rotate = rot;
   590 	scale = scal;
   596         scale = scal;
   591 	xspeed = x;
   597         xspeed = x;
   592 	yspeed = y;
   598         yspeed = y;
   593     }
   599     }
   594 
   600 
   595 protected:
   601 protected:
   596     void advance(int i)
   602     void advance(int i)
   597     { 
   603     {
   598 	if (!i)
   604         if (!i)
   599 	    return;
   605             return;
   600 	QRect r = boundingRect().toRect();
   606         QRect r = boundingRect().toRect();
   601 	int x = int(pos().x()) + r.width();
   607         int x = int(pos().x()) + r.width();
   602 	x += xspeed;
   608         x += xspeed;
   603 	x = (x % (300 + r.width() * 2)) - r.width();
   609         x = (x % (300 + r.width() * 2)) - r.width();
   604 	int y = int(pos().y()) + r.width();
   610         int y = int(pos().y()) + r.width();
   605 	y += yspeed;
   611         y += yspeed;
   606 	y = (y % (300 + r.width() * 2)) - r.width();
   612         y = (y % (300 + r.width() * 2)) - r.width();
   607 	setPos(x, y);
   613         setPos(x, y);
   608 
   614 
   609 	int rot = rotateFactor;
   615         int rot = rotateFactor;
   610 	int sca = scaleFactor;
   616         int sca = scaleFactor;
   611         if (rotate)
   617         if (rotate)
   612 	    rotateFactor = 1 + (rot + xspeed) % 360;
   618             rotateFactor = 1 + (rot + xspeed) % 360;
   613         if (scale)
   619         if (scale)
   614 	    scaleFactor = 1 + (sca + yspeed) % 50;
   620             scaleFactor = 1 + (sca + yspeed) % 50;
   615 
   621 
   616 	if (rotate || scale) {
   622         if (rotate || scale) {
   617 	    qreal s = 0.5 + scaleFactor / 50.0;
   623             qreal s = 0.5 + scaleFactor / 50.0;
   618 	    setTransform(QTransform().rotate(rotateFactor).scale(s, s));
   624             setTransform(QTransform().rotate(rotateFactor).scale(s, s));
   619 	}
   625         }
   620     }
   626     }
   621 
   627 
   622 private:
   628 private:
   623     int xspeed;
   629     int xspeed;
   624     int yspeed;
   630     int yspeed;
   655     view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   661     view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   656     view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   662     view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   657     view.show();
   663     view.show();
   658 
   664 
   659     QPixmap pix(":/images/designer.png");
   665     QPixmap pix(":/images/designer.png");
   660     QVERIFY(!pix.isNull());   
   666     QVERIFY(!pix.isNull());
   661 
   667 
   662     QList<QGraphicsItem *> items;
   668     QList<QGraphicsItem *> items;
   663     QFile file(":/random.data");
   669     QFile file(":/random.data");
   664     QVERIFY(file.open(QIODevice::ReadOnly));
   670     QVERIFY(file.open(QIODevice::ReadOnly));
   665     QDataStream str(&file);
   671     QDataStream str(&file);
   666     for (int i = 0; i < 100; ++i) {
   672     for (int i = 0; i < 100; ++i) {
   667 	AnimatedTextItem *item;
   673         AnimatedTextItem *item;
   668 	if (direction == 0) item = new AnimatedTextItem((i % 4) + 1, 0, rotation, scale);
   674         if (direction == 0) item = new AnimatedTextItem((i % 4) + 1, 0, rotation, scale);
   669 	if (direction == 1) item = new AnimatedTextItem(0, (i % 4) + 1, rotation, scale);
   675         if (direction == 1) item = new AnimatedTextItem(0, (i % 4) + 1, rotation, scale);
   670 	if (direction == 2) item = new AnimatedTextItem((i % 4) + 1, (i % 4) + 1, rotation, scale);
   676         if (direction == 2) item = new AnimatedTextItem((i % 4) + 1, (i % 4) + 1, rotation, scale);
   671         int rnd1, rnd2;
   677         int rnd1, rnd2;
   672         str >> rnd1 >> rnd2;
   678         str >> rnd1 >> rnd2;
   673 	item->setPos(-pix.width() + rnd1 % (view.width() + pix.width()),
   679         item->setPos(-pix.width() + rnd1 % (view.width() + pix.width()),
   674 		     -pix.height() + rnd2 % (view.height() + pix.height()));
   680                      -pix.height() + rnd2 % (view.height() + pix.height()));
   675 	scene.addItem(item);
   681         scene.addItem(item);
   676     }
   682     }
   677 
   683 
   678     view.count = 0;
   684     view.count = 0;
   679 
   685 
   680     QBENCHMARK {
   686     QBENCHMARK {
   681 #ifdef CALLGRIND_DEBUG
   687 #ifdef CALLGRIND_DEBUG
   682 	CALLGRIND_START_INSTRUMENTATION
   688         CALLGRIND_START_INSTRUMENTATION
   683 #endif
   689 #endif
   684 	for (int i = 0; i < 100; ++i) {
   690         for (int i = 0; i < 100; ++i) {
   685 	    scene.advance();
   691             scene.advance();
   686 	    while (view.count < (i+1))
   692             while (view.count < (i+1))
   687 		qApp->processEvents();
   693                 qApp->processEvents();
   688 	}
   694         }
   689 #ifdef CALLGRIND_DEBUG
   695 #ifdef CALLGRIND_DEBUG
   690 	CALLGRIND_STOP_INSTRUMENTATION
   696         CALLGRIND_STOP_INSTRUMENTATION
   691 #endif
   697 #endif
   692     }
   698     }
   693 }
   699 }
   694 
   700 
   695 class AnimatedPixmapCacheItem : public QGraphicsPixmapItem
   701 class AnimatedPixmapCacheItem : public QGraphicsPixmapItem