Advertisement
jjxtra

Terrible painting performance on iPhone, why?

Jan 9th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include "drdrawview.h"
  2.  
  3. DRDrawView::DRDrawView(QWidget *parent) : QGLWidget(parent), image(1024, 1536, QImage::Format_ARGB32_Premultiplied)
  4. {
  5.     pen.setColor(Qt::black);
  6.     pen.setWidth(20 * devicePixelRatio());
  7.     pen.setCapStyle(Qt::RoundCap);
  8.     pen.setJoinStyle(Qt::RoundJoin);
  9.     setAutoFillBackground(false);
  10.     setAttribute(Qt::WA_OpaquePaintEvent, true);
  11.     setAttribute(Qt::WA_NoSystemBackground, true);
  12.     clearScreen();
  13. }
  14.  
  15. DRDrawView::~DRDrawView()
  16. {
  17.  
  18. }
  19.  
  20. void DRDrawView::save( const QString &filename, const QString &format )
  21. {
  22.  
  23. }
  24.  
  25. void DRDrawView::clearScreen()
  26. {
  27.     QMutexLocker l(&bufferMutex);
  28.     memset(image.bits(), 0xFF, image.byteCount());
  29.     update();
  30. }
  31.  
  32. void DRDrawView::mousePressEvent( QMouseEvent *e )
  33. {
  34.     mousePressed = true;
  35. }
  36.  
  37. void DRDrawView::mouseReleaseEvent( QMouseEvent * )
  38. {
  39.     QMutexLocker l(&bufferMutex);
  40.     mousePressed = false;
  41.     polyline.clear();
  42. }
  43.  
  44. void DRDrawView::mouseMoveEvent( QMouseEvent *e )
  45. {
  46.     if ( mousePressed )
  47.     {
  48.         QMutexLocker l(&bufferMutex);
  49.         QPoint p = e->pos();
  50.         p.setX(p.x() * devicePixelRatio());
  51.         p.setY(p.y() * devicePixelRatio());
  52.  
  53.         polyline.push_front(p);
  54.         if (polyline.count() == 4)
  55.         {
  56.             polyline.pop_back();
  57.             QPainter painter(&image);
  58.             painter.setRenderHint(QPainter::Antialiasing);
  59.             painter.setPen(pen);
  60.             painter.drawPolyline(polyline);
  61.             QRect r = polyline.boundingRect();
  62.  
  63.             r.setWidth(r.width() / devicePixelRatio());
  64.             r.setHeight(r.height() / devicePixelRatio());
  65.             r.setX(r.x() / devicePixelRatio());
  66.             r.setY(r.y() / devicePixelRatio());
  67.  
  68.             r.setLeft( r.left() - penWidth() );
  69.             r.setTop( r.top() - penWidth() );
  70.             r.setRight( r.right() + penWidth() );
  71.             r.setBottom( r.bottom() + penWidth() );
  72.  
  73.             r = r.normalized();
  74.             qDebug() << "DRAW RECT: " << devicePixelRatio() << r.x() << " " << r.y() << " " << r.width() << " " << r.height();
  75.  
  76.             update(r);
  77.         }
  78.     }
  79. }
  80.  
  81. void DRDrawView::resizeEvent( QResizeEvent *e )
  82. {
  83.     QWidget::resizeEvent( e );
  84.  
  85.     //image = QImage(width() * devicePixelRatio(), height() * devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
  86.     //clearScreen();
  87. }
  88.  
  89. void DRDrawView::paintEvent( QPaintEvent *e )
  90. {
  91.     //QGLWidget::paintEvent( e );
  92.  
  93.     QMutexLocker l(&bufferMutex);
  94.     QVector<QRect> rects = e->region().rects();
  95.     QPainter painter(this);
  96.     painter.setRenderHint(QPainter::Antialiasing);
  97.  
  98.     for (int i = 0; i < rects.count(); i++)
  99.     {
  100.         QRect r = rects[i];
  101.         QRect r2 = r;
  102.         r2.setWidth(r2.width() * devicePixelRatio());
  103.         r2.setHeight(r2.height() * devicePixelRatio());
  104.         r2.setX(r2.x() * devicePixelRatio());
  105.         r2.setY(r2.y() * devicePixelRatio());
  106.  
  107.         //painter.drawImage(r.x(), r.y(), &image, r.x(), r.y(), r.width(), r.height(), Qt::AutoColor);
  108.         painter.drawImage(r, image, r2);
  109.  
  110.         qDebug() << "UPDATE RECT: " << devicePixelRatio() << r.x() << " " << r.y() << " " << r.width() << " " << r.height();
  111.     }
  112. }
  113.  
  114. /*
  115. Logging:
  116. UPDATE RECT:  2 0   0   320   568
  117. UPDATE RECT:  2 0   0   320   568
  118. DRAW RECT:  2 128   159   266   302
  119. UPDATE RECT:  2 0   0   320   568
  120. DRAW RECT:  2 87   171   255   398
  121. UPDATE RECT:  2 0   0   320   568
  122. DRAW RECT:  2 75   182   248   431
  123. DRAW RECT:  2 73   278   207   434
  124. UPDATE RECT:  2 0   0   320   568
  125. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement