src/gui/painting/qpaintbuffer.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    46 #include <private/qemulationpaintengine_p.h>
    46 #include <private/qemulationpaintengine_p.h>
    47 #include <private/qimage_p.h>
    47 #include <private/qimage_p.h>
    48 
    48 
    49 #include <QDebug>
    49 #include <QDebug>
    50 
    50 
    51 //#define QPAINTBUFFER_DEBUG_DRAW
    51 // #define QPAINTBUFFER_DEBUG_DRAW
    52 
    52 
    53 QT_BEGIN_NAMESPACE
    53 QT_BEGIN_NAMESPACE
    54 
    54 
    55 extern int qt_defaultDpiX();
    55 extern int qt_defaultDpiX();
    56 extern int qt_defaultDpiY();
    56 extern int qt_defaultDpiY();
   245 void QPaintBuffer::draw(QPainter *painter, int frame) const
   245 void QPaintBuffer::draw(QPainter *painter, int frame) const
   246 {
   246 {
   247 #ifdef QPAINTBUFFER_DEBUG_DRAW
   247 #ifdef QPAINTBUFFER_DEBUG_DRAW
   248     qDebug() << "QPaintBuffer::draw() --------------------------------";
   248     qDebug() << "QPaintBuffer::draw() --------------------------------";
   249 
   249 
   250 //     printf("Float buffer:");
   250     Q_D(const QPaintBuffer);
   251 //     for (int i=0; i<d->floats.size(); i++) {
   251     printf("Float buffer:");
   252 //         if ((i % 10) == 0) {
   252     for (int i=0; i<d->floats.size(); i++) {
   253 //             printf("\n%4d-%4d: ", i, i+9);
   253         if ((i % 10) == 0) {
   254 //         }
   254             printf("\n%4d-%4d: ", i, i+9);
   255 //         printf("%4.2f  ", d->floats[i]);
   255         }
   256 //     }
   256         printf("%4.2f  ", d->floats[i]);
   257 //     printf("\n");
   257     }
   258 
   258     printf("\n");
   259 //     printf("Int Buffer:");
   259 
   260 //     for (int i=0; i<d->ints.size(); i++) {
   260     printf("Int Buffer:");
   261 //         if ((i % 10) == 0) {
   261     for (int i=0; i<d->ints.size(); i++) {
   262 //             printf("\n%4d-%4d: ", i, i+10);
   262         if ((i % 10) == 0) {
   263 //         }
   263             printf("\n%4d-%4d: ", i, i+10);
   264 //         printf("%5d", d->ints[i]);
   264         }
   265 //     }
   265         printf("%5d", d->ints[i]);
   266 //     printf("\n");
   266     }
       
   267     printf("\n");
   267 #endif
   268 #endif
   268 
   269 
   269     if (painter && !painter->isActive())
   270     if (painter && !painter->isActive())
   270         return;
   271         return;
   271 
   272 
   404     buffer->addCommand(QPaintBufferPrivate::Cmd_SetClipEnabled, state()->clipEnabled);
   405     buffer->addCommand(QPaintBufferPrivate::Cmd_SetClipEnabled, state()->clipEnabled);
   405 }
   406 }
   406 
   407 
   407 void QPaintBufferEngine::penChanged()
   408 void QPaintBufferEngine::penChanged()
   408 {
   409 {
   409 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   410     qDebug() << "QPaintBufferEngine:" << state()->pen;
       
   411 #endif
       
   412     const QPen &pen = state()->pen;
   410     const QPen &pen = state()->pen;
   413 
   411 
   414     if (!buffer->commands.isEmpty()
   412     if (!buffer->commands.isEmpty()
   415         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetPen) {
   413         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetPen) {
       
   414 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   415     qDebug() << "QPaintBufferEngine: penChanged (compressed)" << state()->pen;
       
   416 #endif
   416         buffer->variants[buffer->commands.last().offset] = pen;
   417         buffer->variants[buffer->commands.last().offset] = pen;
   417         return;
   418         return;
   418     }
   419     }
       
   420 
   419     if (buffer->calculateBoundingRect) {
   421     if (buffer->calculateBoundingRect) {
   420         if (pen.style() == Qt::NoPen) {
   422         if (pen.style() == Qt::NoPen) {
   421             buffer->penWidthAdjustment = 0;
   423             buffer->penWidthAdjustment = 0;
   422         } else {
   424         } else {
   423             qreal penWidth = (pen.widthF() == 0) ? 1 : pen.widthF();
   425             qreal penWidth = (pen.widthF() == 0) ? 1 : pen.widthF();
   425             if (!pen.isCosmetic())
   427             if (!pen.isCosmetic())
   426                 transformedWidth = painter()->transform().map(transformedWidth);
   428                 transformedWidth = painter()->transform().map(transformedWidth);
   427             buffer->penWidthAdjustment = transformedWidth.x() / 2.0;
   429             buffer->penWidthAdjustment = transformedWidth.x() / 2.0;
   428         }
   430         }
   429     }
   431     }
       
   432 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   433     qDebug() << "QPaintBufferEngine: penChanged" << state()->pen;
       
   434 #endif
   430     buffer->addCommand(QPaintBufferPrivate::Cmd_SetPen, pen);
   435     buffer->addCommand(QPaintBufferPrivate::Cmd_SetPen, pen);
   431 }
   436 }
   432 
   437 
   433 void QPaintBufferEngine::brushChanged()
   438 void QPaintBufferEngine::brushChanged()
   434 {
   439 {
   435 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   436     qDebug() << "QPaintBufferEngine:" << state()->brush;
       
   437 #endif
       
   438     const QBrush &brush = state()->brush;
   440     const QBrush &brush = state()->brush;
   439 
   441 
   440     if (!buffer->commands.isEmpty()
   442     if (!buffer->commands.isEmpty()
   441         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetBrush) {
   443         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetBrush) {
       
   444 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   445         qDebug() << "QPaintBufferEngine: brushChanged (compressed)" << state()->brush;
       
   446 #endif
   442         buffer->variants[buffer->commands.last().offset] = brush;
   447         buffer->variants[buffer->commands.last().offset] = brush;
   443         return;
   448         return;
   444     }
   449     }
   445 
   450 
       
   451 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   452     qDebug() << "QPaintBufferEngine: brushChanged" << state()->brush;
       
   453 #endif
   446     buffer->addCommand(QPaintBufferPrivate::Cmd_SetBrush, brush);
   454     buffer->addCommand(QPaintBufferPrivate::Cmd_SetBrush, brush);
   447 }
   455 }
   448 
   456 
   449 void QPaintBufferEngine::brushOriginChanged()
   457 void QPaintBufferEngine::brushOriginChanged()
   450 {
   458 {
   486 {
   494 {
   487     // ### accumulate, like in QBrush case...
   495     // ### accumulate, like in QBrush case...
   488     if (!buffer->commands.isEmpty()
   496     if (!buffer->commands.isEmpty()
   489         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetTransform) {
   497         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetTransform) {
   490 #ifdef QPAINTBUFFER_DEBUG_DRAW
   498 #ifdef QPAINTBUFFER_DEBUG_DRAW
   491         qDebug() << "QPaintBufferEngine: compressing " << state()->matrix;
   499         qDebug() << "QPaintBufferEngine: transformChanged (compressing) " << state()->matrix;
   492 #endif
   500 #endif
   493         buffer->variants[buffer->commands.last().offset] = state()->matrix;
   501         buffer->variants[buffer->commands.last().offset] = state()->matrix;
   494         return;
   502         return;
   495     }
   503     }
   496 
   504 
   497 #ifdef QPAINTBUFFER_DEBUG_DRAW
   505 #ifdef QPAINTBUFFER_DEBUG_DRAW
   498         qDebug() << "QPaintBufferEngine: " << state()->matrix;
   506         qDebug() << "QPaintBufferEngine: transformChanged:" << state()->matrix;
   499 #endif
   507 #endif
   500     buffer->addCommand(QPaintBufferPrivate::Cmd_SetTransform, state()->matrix);
   508     buffer->addCommand(QPaintBufferPrivate::Cmd_SetTransform, state()->matrix);
   501 }
   509 }
   502 
   510 
   503 void QPaintBufferEngine::backgroundModeChanged()
   511 void QPaintBufferEngine::backgroundModeChanged()
   512 void QPaintBufferEngine::draw(const QVectorPath &path)
   520 void QPaintBufferEngine::draw(const QVectorPath &path)
   513 {
   521 {
   514 #ifdef QPAINTBUFFER_DEBUG_DRAW
   522 #ifdef QPAINTBUFFER_DEBUG_DRAW
   515     qDebug() << "QPaintBufferEngine: draw vpath:" << path.elementCount();
   523     qDebug() << "QPaintBufferEngine: draw vpath:" << path.elementCount();
   516 #endif
   524 #endif
   517     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawVectorPath, path);
   525 
       
   526     bool hasBrush = qbrush_style(state()->brush) != Qt::NoBrush;
       
   527     bool hasPen = qpen_style(state()->pen) != Qt::NoPen
       
   528                   && qbrush_style(qpen_brush(state()->pen)) != Qt::NoBrush;
       
   529 
       
   530     if (hasPen || hasBrush)
       
   531         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawVectorPath, path);
       
   532 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
   533     else
       
   534         qDebug() << " - no pen or brush active, discarded...\n";
       
   535 #endif
       
   536 
   518 //     if (buffer->calculateBoundingRect) {
   537 //     if (buffer->calculateBoundingRect) {
   519 //         QRealRect r = path.controlPointRect();
   538 //         QRealRect r = path.controlPointRect();
   520 //         buffer->updateBoundingRect(QRectF(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1));
   539 //         buffer->updateBoundingRect(QRectF(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1));
   521 //     }
   540 //     }
   522 }
   541 }
   743         buffer->updateBoundingRect(r);
   762         buffer->updateBoundingRect(r);
   744 }
   763 }
   745 
   764 
   746 void QPaintBufferEngine::drawPath(const QPainterPath &path)
   765 void QPaintBufferEngine::drawPath(const QPainterPath &path)
   747 {
   766 {
   748 #ifdef QPAINTBUFFER_DEBUG_DRAW
   767 // #ifdef QPAINTBUFFER_DEBUG_DRAW
   749     qDebug() << "QPaintBufferEngine: drawPath: element count:" << path.elementCount();
   768 //     qDebug() << "QPaintBufferEngine: drawPath: element count:" << path.elementCount();
   750 #endif
   769 // #endif
   751     // ### Path -> QVariant
   770 //     // ### Path -> QVariant
   752     // buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPath, QVariant(path));
   771 //     // buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPath, QVariant(path));
   753     QPaintEngineEx::drawPath(path);
   772     QPaintEngineEx::drawPath(path);
   754 
   773 
   755     if (buffer->calculateBoundingRect)
   774 //     if (buffer->calculateBoundingRect)
   756         buffer->updateBoundingRect(path.boundingRect());
   775 //         buffer->updateBoundingRect(path.boundingRect());
   757 }
   776 }
   758 
   777 
   759 void QPaintBufferEngine::drawPoints(const QPoint *points, int pointCount)
   778 void QPaintBufferEngine::drawPoints(const QPoint *points, int pointCount)
   760 {
   779 {
   761 #ifdef QPAINTBUFFER_DEBUG_DRAW
   780 #ifdef QPAINTBUFFER_DEBUG_DRAW
  1422         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
  1441         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
  1423         QTextItemIntCopy *tiCopy = reinterpret_cast<QTextItemIntCopy *>(qVariantValue<void *>(d->variants.at(cmd.offset)));
  1442         QTextItemIntCopy *tiCopy = reinterpret_cast<QTextItemIntCopy *>(qVariantValue<void *>(d->variants.at(cmd.offset)));
  1424         QTextItemInt &ti = (*tiCopy)();
  1443         QTextItemInt &ti = (*tiCopy)();
  1425         QString text(ti.text());
  1444         QString text(ti.text());
  1426 
  1445 
  1427 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
  1428         qDebug() << " -> Cmd_DrawTextItem:" << pos << " " << text << " " << scaleFactor;
       
  1429 #endif
       
  1430 
       
  1431         QFont font(ti.font());
  1446         QFont font(ti.font());
  1432         font.setUnderline(false);
  1447         font.setUnderline(false);
  1433         font.setStrikeOut(false);
  1448         font.setStrikeOut(false);
  1434         font.setOverline(false);
  1449         font.setOverline(false);
  1435 
  1450 
  1436         const QTextItemInt &si = static_cast<const QTextItemInt &>(ti);
  1451         const QTextItemInt &si = static_cast<const QTextItemInt &>(ti);
  1437         qreal justificationWidth = 0;
  1452         qreal justificationWidth = 0;
  1438         if (si.justified)
  1453         if (si.justified)
  1439             justificationWidth = si.width.toReal();
  1454             justificationWidth = si.width.toReal();
  1440         qreal scaleFactor = font.d->dpi/qreal(qt_defaultDpiY());
  1455         qreal scaleFactor = font.d->dpi/qreal(qt_defaultDpiY());
       
  1456 
       
  1457 #ifdef QPAINTBUFFER_DEBUG_DRAW
       
  1458         qDebug() << " -> Cmd_DrawTextItem:" << pos << " " << text << " " << scaleFactor;
       
  1459 #endif
  1441 
  1460 
  1442         if (scaleFactor != 1.0) {
  1461         if (scaleFactor != 1.0) {
  1443             QFont fnt(font);
  1462             QFont fnt(font);
  1444             QFakeDevice fake;
  1463             QFakeDevice fake;
  1445             fake.setDpiX(qRound(scaleFactor*qt_defaultDpiX()));
  1464             fake.setDpiX(qRound(scaleFactor*qt_defaultDpiX()));
  1687     }
  1706     }
  1688 }
  1707 }
  1689 
  1708 
  1690 QPaintBufferResource::QPaintBufferResource(FreeFunc f, QObject *parent) : QObject(parent), free(f)
  1709 QPaintBufferResource::QPaintBufferResource(FreeFunc f, QObject *parent) : QObject(parent), free(f)
  1691 {
  1710 {
  1692     connect(QPaintBufferSignalProxy::instance(), SIGNAL(aboutToDestroy(const QPaintBufferPrivate *)), this, SLOT(remove(const QPaintBufferPrivate *)));
  1711     connect(QPaintBufferSignalProxy::instance(), SIGNAL(aboutToDestroy(const QPaintBufferPrivate*)), this, SLOT(remove(const QPaintBufferPrivate*)));
  1693 }
  1712 }
  1694 
  1713 
  1695 QPaintBufferResource::~QPaintBufferResource()
  1714 QPaintBufferResource::~QPaintBufferResource()
  1696 {
  1715 {
  1697     for (Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)
  1716     for (Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)