diff options
author | Samuel Rødal <samuel.rodal@nokia.com> | 2011-07-07 08:51:52 (GMT) |
---|---|---|
committer | Samuel Rødal <samuel.rodal@nokia.com> | 2011-07-26 08:57:11 (GMT) |
commit | 5842d19cf3dff37a85cbdb31ab5a170ceaf5c7fb (patch) | |
tree | b87af2ff273085a80c491847db05aa50ca979f92 | |
parent | d852e6164a4aaf8e235e97cba8f0e8e9b933987c (diff) | |
download | Qt-5842d19cf3dff37a85cbdb31ab5a170ceaf5c7fb.zip Qt-5842d19cf3dff37a85cbdb31ab5a170ceaf5c7fb.tar.gz Qt-5842d19cf3dff37a85cbdb31ab5a170ceaf5c7fb.tar.bz2 |
Fixed holes in border image drawing by introducing new API.
When rasterizing two adjacent QRectFs it's important that the shared x
or y-edges have the exact same coordinates, or there might be a hole or
an overlapping pixel when they are rasterized. Since the
drawPixmapFragments API was based on a center position and a scale, it
can not be used for this purpose, as the in the situation of two
horizontally adjacent rectangles the right edge of the left-most rect
and the left edge of the right-most edge are computed differently. Thus
rounding errors can cause them to not be equal, especially when there's
also a scaling / translating painter transform involved.
Thus, to not sacrifice performance, we need to introduce a new
drawPixmapFragments API that's simply takes an array of target
rectangles and an array of source rectangles. It should give a slight
performance boost for the border pixmap use case as well, since there
are less floating point multiplications / divisions involved.
Task-number: QTBUG-19079
Reviewed-by: Kim
-rw-r--r-- | src/gui/painting/qdrawutil.cpp | 179 | ||||
-rw-r--r-- | src/gui/painting/qemulationpaintengine.cpp | 41 | ||||
-rw-r--r-- | src/gui/painting/qemulationpaintengine_p.h | 5 | ||||
-rw-r--r-- | src/gui/painting/qpaintengine_raster.cpp | 11 | ||||
-rw-r--r-- | src/gui/painting/qpaintengineex.cpp | 16 | ||||
-rw-r--r-- | src/gui/painting/qpaintengineex_p.h | 4 | ||||
-rw-r--r-- | src/gui/painting/qpainter.cpp | 46 | ||||
-rw-r--r-- | src/gui/painting/qpainter.h | 2 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp | 128 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h | 10 |
10 files changed, 294 insertions, 148 deletions
diff --git a/src/gui/painting/qdrawutil.cpp b/src/gui/painting/qdrawutil.cpp index 98294cb..1e98b05 100644 --- a/src/gui/painting/qdrawutil.cpp +++ b/src/gui/painting/qdrawutil.cpp @@ -1084,7 +1084,7 @@ void qDrawItem(QPainter *p, Qt::GUIStyle gs, according to the \a margins structure. */ -typedef QVarLengthArray<QPainter::PixmapFragment, 16> QPixmapFragmentsArray; +typedef QVarLengthArray<QRectF, 16> QRectFArray; /*! \since 4.6 @@ -1105,12 +1105,8 @@ void qDrawBorderPixmap(QPainter *painter, const QRect &targetRect, const QMargin const QPixmap &pixmap, const QRect &sourceRect,const QMargins &sourceMargins, const QTileRules &rules, QDrawBorderPixmap::DrawingHints hints) { - QPainter::PixmapFragment d; - d.opacity = 1.0; - d.rotation = 0.0; - - QPixmapFragmentsArray opaqueData; - QPixmapFragmentsArray translucentData; + QRectFArray sourceData[2]; + QRectFArray targetData[2]; // source center const int sourceCenterTop = sourceRect.top() + sourceMargins.top(); @@ -1192,166 +1188,95 @@ void qDrawBorderPixmap(QPainter *painter, const QRect &targetRect, const QMargin // corners if (targetMargins.top() > 0 && targetMargins.left() > 0 && sourceMargins.top() > 0 && sourceMargins.left() > 0) { // top left - d.x = (0.5 * (xTarget[1] + xTarget[0])); - d.y = (0.5 * (yTarget[1] + yTarget[0])); - d.sourceLeft = sourceRect.left(); - d.sourceTop = sourceRect.top(); - d.width = sourceMargins.left(); - d.height = sourceMargins.top(); - d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width; - d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height; - if (hints & QDrawBorderPixmap::OpaqueTopLeft) - opaqueData.append(d); - else - translucentData.append(d); + int index = bool(hints & QDrawBorderPixmap::OpaqueTopLeft); + sourceData[index].append(QRectF(sourceRect.topLeft(), QSizeF(sourceMargins.left(), sourceMargins.top()))); + targetData[index].append(QRectF(QPointF(xTarget[0], yTarget[0]), QPointF(xTarget[1], yTarget[1]))); } if (targetMargins.top() > 0 && targetMargins.right() > 0 && sourceMargins.top() > 0 && sourceMargins.right() > 0) { // top right - d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1])); - d.y = (0.5 * (yTarget[1] + yTarget[0])); - d.sourceLeft = sourceCenterRight; - d.sourceTop = sourceRect.top(); - d.width = sourceMargins.right(); - d.height = sourceMargins.top(); - d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width; - d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height; - if (hints & QDrawBorderPixmap::OpaqueTopRight) - opaqueData.append(d); - else - translucentData.append(d); + int index = bool(hints & QDrawBorderPixmap::OpaqueTopRight); + sourceData[index].append(QRectF(QPointF(sourceCenterRight, sourceRect.top()), QSizeF(sourceMargins.right(), sourceMargins.top()))); + targetData[index].append(QRectF(QPointF(xTarget[columns-1], yTarget[0]), QPointF(xTarget[columns], yTarget[1]))); } if (targetMargins.bottom() > 0 && targetMargins.left() > 0 && sourceMargins.bottom() > 0 && sourceMargins.left() > 0) { // bottom left - d.x = (0.5 * (xTarget[1] + xTarget[0])); - d.y =(0.5 * (yTarget[rows] + yTarget[rows - 1])); - d.sourceLeft = sourceRect.left(); - d.sourceTop = sourceCenterBottom; - d.width = sourceMargins.left(); - d.height = sourceMargins.bottom(); - d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width; - d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height; - if (hints & QDrawBorderPixmap::OpaqueBottomLeft) - opaqueData.append(d); - else - translucentData.append(d); + int index = bool(hints & QDrawBorderPixmap::OpaqueBottomLeft); + sourceData[index].append(QRectF(QPointF(sourceRect.left(), sourceCenterBottom), QSizeF(sourceMargins.left(), sourceMargins.bottom()))); + targetData[index].append(QRectF(QPointF(xTarget[0], yTarget[rows - 1]), QPointF(xTarget[1], yTarget[rows]))); } if (targetMargins.bottom() > 0 && targetMargins.right() > 0 && sourceMargins.bottom() > 0 && sourceMargins.right() > 0) { // bottom right - d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1])); - d.y = (0.5 * (yTarget[rows] + yTarget[rows - 1])); - d.sourceLeft = sourceCenterRight; - d.sourceTop = sourceCenterBottom; - d.width = sourceMargins.right(); - d.height = sourceMargins.bottom(); - d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width; - d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height; - if (hints & QDrawBorderPixmap::OpaqueBottomRight) - opaqueData.append(d); - else - translucentData.append(d); + int index = bool(hints & QDrawBorderPixmap::OpaqueBottomRight); + sourceData[index].append(QRectF(QPointF(sourceCenterRight, sourceCenterBottom), QSizeF(sourceMargins.right(), sourceMargins.bottom()))); + targetData[index].append(QRectF(QPointF(xTarget[columns - 1], yTarget[rows - 1]), QPointF(xTarget[columns], yTarget[rows]))); } // horizontal edges if (targetCenterWidth > 0 && sourceCenterWidth > 0) { if (targetMargins.top() > 0 && sourceMargins.top() > 0) { // top - QPixmapFragmentsArray &data = hints & QDrawBorderPixmap::OpaqueTop ? opaqueData : translucentData; - d.sourceLeft = sourceCenterLeft; - d.sourceTop = sourceRect.top(); - d.width = sourceCenterWidth; - d.height = sourceMargins.top(); - d.y = (0.5 * (yTarget[1] + yTarget[0])); - d.scaleX = dx / d.width; - d.scaleY = qreal(yTarget[1] - yTarget[0]) / d.height; + int index = bool(hints & QDrawBorderPixmap::OpaqueTop); for (int i = 1; i < columns - 1; ++i) { - d.x = (0.5 * (xTarget[i + 1] + xTarget[i])); - data.append(d); + if (rules.horizontal == Qt::RepeatTile && i == columns - 2) { + sourceData[index].append(QRectF(QPointF(sourceCenterLeft, sourceRect.top()), QSizeF(xTarget[i + 1] - xTarget[i], sourceMargins.top()))); + } else { + sourceData[index].append(QRectF(QPointF(sourceCenterLeft, sourceRect.top()), QSizeF(sourceCenterWidth, sourceMargins.top()))); + } + targetData[index].append(QRectF(QPointF(xTarget[i], yTarget[0]), QPointF(xTarget[i + 1], yTarget[1]))); } - if (rules.horizontal == Qt::RepeatTile) - data[data.size() - 1].width = ((xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX); } if (targetMargins.bottom() > 0 && sourceMargins.bottom() > 0) { // bottom - QPixmapFragmentsArray &data = hints & QDrawBorderPixmap::OpaqueBottom ? opaqueData : translucentData; - d.sourceLeft = sourceCenterLeft; - d.sourceTop = sourceCenterBottom; - d.width = sourceCenterWidth; - d.height = sourceMargins.bottom(); - d.y = (0.5 * (yTarget[rows] + yTarget[rows - 1])); - d.scaleX = dx / d.width; - d.scaleY = qreal(yTarget[rows] - yTarget[rows - 1]) / d.height; + int index = bool(hints & QDrawBorderPixmap::OpaqueBottom); for (int i = 1; i < columns - 1; ++i) { - d.x = (0.5 * (xTarget[i + 1] + xTarget[i])); - data.append(d); + if (rules.horizontal == Qt::RepeatTile && i == columns - 2) { + sourceData[index].append(QRectF(QPointF(sourceCenterLeft, sourceCenterBottom), QSizeF(xTarget[i + 1] - xTarget[i], sourceMargins.bottom()))); + } else { + sourceData[index].append(QRectF(QPointF(sourceCenterLeft, sourceCenterBottom), QSizeF(sourceCenterWidth, sourceMargins.bottom()))); + } + targetData[index].append(QRectF(QPointF(xTarget[i], yTarget[rows - 1]), QPointF(xTarget[i + 1], yTarget[rows]))); } - if (rules.horizontal == Qt::RepeatTile) - data[data.size() - 1].width = ((xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX); } } // vertical edges if (targetCenterHeight > 0 && sourceCenterHeight > 0) { if (targetMargins.left() > 0 && sourceMargins.left() > 0) { // left - QPixmapFragmentsArray &data = hints & QDrawBorderPixmap::OpaqueLeft ? opaqueData : translucentData; - d.sourceLeft = sourceRect.left(); - d.sourceTop = sourceCenterTop; - d.width = sourceMargins.left(); - d.height = sourceCenterHeight; - d.x = (0.5 * (xTarget[1] + xTarget[0])); - d.scaleX = qreal(xTarget[1] - xTarget[0]) / d.width; - d.scaleY = dy / d.height; + int index = bool(hints & QDrawBorderPixmap::OpaqueLeft); for (int i = 1; i < rows - 1; ++i) { - d.y = (0.5 * (yTarget[i + 1] + yTarget[i])); - data.append(d); + if (rules.vertical == Qt::RepeatTile && i == rows - 2) { + sourceData[index].append(QRectF(QPointF(sourceRect.left(), sourceCenterTop), QSizeF(sourceMargins.left(), yTarget[i + 1] - yTarget[i]))); + } else { + sourceData[index].append(QRectF(QPointF(sourceRect.left(), sourceCenterTop), QSizeF(sourceMargins.left(), sourceCenterHeight))); + } + targetData[index].append(QRectF(QPointF(xTarget[0], yTarget[i]), QPointF(xTarget[1], yTarget[i + 1]))); } - if (rules.vertical == Qt::RepeatTile) - data[data.size() - 1].height = ((yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY); } if (targetMargins.right() > 0 && sourceMargins.right() > 0) { // right - QPixmapFragmentsArray &data = hints & QDrawBorderPixmap::OpaqueRight ? opaqueData : translucentData; - d.sourceLeft = sourceCenterRight; - d.sourceTop = sourceCenterTop; - d.width = sourceMargins.right(); - d.height = sourceCenterHeight; - d.x = (0.5 * (xTarget[columns] + xTarget[columns - 1])); - d.scaleX = qreal(xTarget[columns] - xTarget[columns - 1]) / d.width; - d.scaleY = dy / d.height; + int index = bool(hints & QDrawBorderPixmap::OpaqueRight); for (int i = 1; i < rows - 1; ++i) { - d.y = (0.5 * (yTarget[i + 1] + yTarget[i])); - data.append(d); + if (rules.vertical == Qt::RepeatTile && i == rows - 2) { + sourceData[index].append(QRectF(QPointF(sourceCenterRight, sourceCenterTop), QSizeF(sourceMargins.right(), yTarget[i + 1] - yTarget[i]))); + } else { + sourceData[index].append(QRectF(QPointF(sourceCenterRight, sourceCenterTop), QSizeF(sourceMargins.right(), sourceCenterHeight))); + } + targetData[index].append(QRectF(QPointF(xTarget[columns - 1], yTarget[i]), QPointF(xTarget[columns], yTarget[i + 1]))); } - if (rules.vertical == Qt::RepeatTile) - data[data.size() - 1].height = ((yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY); } } // center if (targetCenterWidth > 0 && targetCenterHeight > 0 && sourceCenterWidth > 0 && sourceCenterHeight > 0) { - QPixmapFragmentsArray &data = hints & QDrawBorderPixmap::OpaqueCenter ? opaqueData : translucentData; - d.sourceLeft = sourceCenterLeft; - d.sourceTop = sourceCenterTop; - d.width = sourceCenterWidth; - d.height = sourceCenterHeight; - d.scaleX = dx / d.width; - d.scaleY = dy / d.height; - - qreal repeatWidth = (xTarget[columns - 1] - xTarget[columns - 2]) / d.scaleX; - qreal repeatHeight = (yTarget[rows - 1] - yTarget[rows - 2]) / d.scaleY; - + int index = bool(hints & QDrawBorderPixmap::OpaqueCenter); for (int j = 1; j < rows - 1; ++j) { - d.y = (0.5 * (yTarget[j + 1] + yTarget[j])); + qreal sourceHeight = (rules.vertical == Qt::RepeatTile && j == rows - 2) ? yTarget[j + 1] - yTarget[j] : sourceCenterHeight; for (int i = 1; i < columns - 1; ++i) { - d.x = (0.5 * (xTarget[i + 1] + xTarget[i])); - data.append(d); + qreal sourceWidth = (rules.horizontal == Qt::RepeatTile && i == columns - 2) ? xTarget[i + 1] - xTarget[i] : sourceCenterWidth; + sourceData[index].append(QRectF(QPointF(sourceCenterLeft, sourceCenterTop), QSizeF(sourceWidth, sourceHeight))); + targetData[index].append(QRectF(QPointF(xTarget[i], yTarget[j]), QPointF(xTarget[i + 1], yTarget[j + 1]))); } - if (rules.horizontal == Qt::RepeatTile) - data[data.size() - 1].width = repeatWidth; - } - if (rules.vertical == Qt::RepeatTile) { - for (int i = 1; i < columns - 1; ++i) - data[data.size() - i].height = repeatHeight; } } - if (opaqueData.size()) - painter->drawPixmapFragments(opaqueData.data(), opaqueData.size(), pixmap, QPainter::OpaqueHint); - if (translucentData.size()) - painter->drawPixmapFragments(translucentData.data(), translucentData.size(), pixmap); + for (int i = 0; i < 2; ++i) { + if (sourceData[i].size()) + painter->drawPixmapFragments(targetData[i].data(), sourceData[i].data(), sourceData[i].size(), pixmap, i == 1 ? QPainter::OpaqueHint : QPainter::PixmapFragmentHint(0)); + } if (oldAA) painter->setRenderHint(QPainter::Antialiasing, true); diff --git a/src/gui/painting/qemulationpaintengine.cpp b/src/gui/painting/qemulationpaintengine.cpp index 903ab1f..d7ad0f6 100644 --- a/src/gui/painting/qemulationpaintengine.cpp +++ b/src/gui/painting/qemulationpaintengine.cpp @@ -222,6 +222,47 @@ void QEmulationPaintEngine::drawImage(const QRectF &r, const QImage &pm, const Q real_engine->drawImage(r, pm, sr, flags); } +void QEmulationPaintEngine::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) +{ + if (state()->bgMode == Qt::OpaqueMode && pixmap.isQBitmap()) { + qreal oldOpacity = real_engine->state()->opacity; + QTransform oldTransform = real_engine->state()->matrix; + + for (int i = 0; i < fragmentCount; ++i) { + QTransform transform = oldTransform; + transform.translate(fragments[i].x, fragments[i].y); + transform.rotate(fragments[i].rotation); + real_engine->state()->opacity = oldOpacity * fragments[i].opacity; + real_engine->state()->matrix = transform; + real_engine->opacityChanged(); + real_engine->transformChanged(); + + qreal w = fragments[i].scaleX * fragments[i].width; + qreal h = fragments[i].scaleY * fragments[i].height; + fillBGRect(QRectF(-0.5 * w, -0.5 * h, w, h)); + } + + real_engine->state()->opacity = oldOpacity; + real_engine->state()->matrix = oldTransform; + real_engine->opacityChanged(); + real_engine->transformChanged(); + } + + real_engine->drawPixmapFragments(fragments, fragmentCount, pixmap, hints); +} + +void QEmulationPaintEngine::drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) +{ + if (state()->bgMode == Qt::OpaqueMode && pixmap.isQBitmap()) { + for (int i = 0; i < fragmentCount; ++i) + fillBGRect(targetRects[i]); + } + + real_engine->drawPixmapFragments(targetRects, sourceRects, fragmentCount, pixmap, hints); +} + void QEmulationPaintEngine::clipEnabledChanged() { real_engine->clipEnabledChanged(); diff --git a/src/gui/painting/qemulationpaintengine_p.h b/src/gui/painting/qemulationpaintengine_p.h index fdc3688..b4ed7e7 100644 --- a/src/gui/painting/qemulationpaintengine_p.h +++ b/src/gui/painting/qemulationpaintengine_p.h @@ -81,6 +81,11 @@ public: virtual void drawStaticTextItem(QStaticTextItem *item); virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s); virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags); + + virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints); + virtual void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints); virtual void clipEnabledChanged(); virtual void penChanged(); diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index 626100a..d77ef82 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -2396,12 +2396,13 @@ void QRasterPaintEngine::drawImage(const QRectF &r, const QImage &img, const QRe d->image_filler_xform.setupMatrix(copy, s->flags.bilinear); if (!s->flags.antialiased && s->matrix.type() == QTransform::TxScale) { - QRectF rr = s->matrix.mapRect(r); + QPointF rr_tl = s->matrix.map(r.topLeft()); + QPointF rr_br = s->matrix.map(r.bottomRight()); - const int x1 = qRound(rr.x()); - const int y1 = qRound(rr.y()); - const int x2 = qRound(rr.right()); - const int y2 = qRound(rr.bottom()); + const int x1 = qRound(rr_tl.x()); + const int y1 = qRound(rr_tl.y()); + const int x2 = qRound(rr_br.x()); + const int y2 = qRound(rr_br.y()); fillRect_normalized(QRect(x1, y1, x2-x1, y2-y1), &d->image_filler_xform, d); return; diff --git a/src/gui/painting/qpaintengineex.cpp b/src/gui/painting/qpaintengineex.cpp index 5105d9a..6df410b 100644 --- a/src/gui/painting/qpaintengineex.cpp +++ b/src/gui/painting/qpaintengineex.cpp @@ -1003,6 +1003,22 @@ void QPaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragmen transformChanged(); } +void QPaintEngineEx::drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, + const QPixmap &pixmap, QPainter::PixmapFragmentHints /*hints*/) +{ + if (pixmap.isNull()) + return; + + if (sourceRects) { + for (int i = 0; i < fragmentCount; ++i) + drawPixmap(targetRects[i], pixmap, sourceRects[i]); + } else { + QRectF sourceRect = pixmap.rect(); + for (int i = 0; i < fragmentCount; ++i) + drawPixmap(targetRects[i], pixmap, sourceRect); + } +} + void QPaintEngineEx::setState(QPainterState *s) { QPaintEngine::state = s; diff --git a/src/gui/painting/qpaintengineex_p.h b/src/gui/painting/qpaintengineex_p.h index c605685..275a6e0 100644 --- a/src/gui/painting/qpaintengineex_p.h +++ b/src/gui/painting/qpaintengineex_p.h @@ -198,7 +198,9 @@ public: virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s); virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, - QFlags<QPainter::PixmapFragmentHint> hints); + QPainter::PixmapFragmentHints hints); + virtual void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints); virtual void updateState(const QPaintEngineState &state); diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp index 1d10d75..8e64f3b 100644 --- a/src/gui/painting/qpainter.cpp +++ b/src/gui/painting/qpainter.cpp @@ -9248,6 +9248,52 @@ void QPainter::drawPixmapFragments(const PixmapFragment *fragments, int fragment } /*! + \since 4.8 + + This function is used to draw the same \a pixmap with multiple target + and source rectangles. If \a sourceRects is 0, the whole pixmap will be + rendered at each of the target rectangles. The \a hints parameter can be + used to pass in drawing hints. + + This function is potentially faster than multiple calls to drawPixmap(), + since the backend can optimize state changes. + + \sa QPainter::PixmapFragmentHint +*/ + +void QPainter::drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, + const QPixmap &pixmap, PixmapFragmentHints hints) +{ + Q_D(QPainter); + + if (!d->engine || pixmap.isNull()) + return; + +#ifndef QT_NO_DEBUG + if (sourceRects) { + for (int i = 0; i < fragmentCount; ++i) { + QRectF sourceRect = sourceRects[i]; + if (!(QRectF(pixmap.rect()).contains(sourceRect))) + qWarning("QPainter::drawPixmapFragments - the source rect is not contained by the pixmap's rectangle"); + } + } +#endif + + if (d->engine->isExtended()) { + d->extended->drawPixmapFragments(targetRects, sourceRects, fragmentCount, pixmap, hints); + } else { + if (sourceRects) { + for (int i = 0; i < fragmentCount; ++i) + drawPixmap(targetRects[i], pixmap, sourceRects[i]); + } else { + QRectF sourceRect = pixmap.rect(); + for (int i = 0; i < fragmentCount; ++i) + drawPixmap(targetRects[i], pixmap, sourceRect); + } + } +} + +/*! \since 4.7 \class QPainter::PixmapFragment diff --git a/src/gui/painting/qpainter.h b/src/gui/painting/qpainter.h index 30f8da0..12a14a2 100644 --- a/src/gui/painting/qpainter.h +++ b/src/gui/painting/qpainter.h @@ -380,6 +380,8 @@ public: void drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, PixmapFragmentHints hints = 0); + void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, + const QPixmap &pixmap, PixmapFragmentHints hints = 0); void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags = Qt::AutoColor); diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp index 0d2f2a2..a961366 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp @@ -651,7 +651,7 @@ void QGL2PaintEngineExPrivate::transferMode(EngineMode newMode) if (newMode == mode) return; - if (mode == TextDrawingMode || mode == ImageDrawingMode || mode == ImageArrayDrawingMode) { + if (mode == TextDrawingMode || imageDrawingMode) { lastTextureUsed = GLuint(-1); } @@ -661,14 +661,21 @@ void QGL2PaintEngineExPrivate::transferMode(EngineMode newMode) shaderManager->setHasComplexGeometry(false); } + imageDrawingMode = false; + if (newMode == ImageDrawingMode) { setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray); setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, staticTextureCoordinateArray); + imageDrawingMode = true; } - if (newMode == ImageArrayDrawingMode) { + if (newMode == ImageArrayDrawingMode || newMode == ImageArrayWithOpacityDrawingMode) { setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data()); setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data()); + imageDrawingMode = true; + } + + if (newMode == ImageArrayWithOpacityDrawingMode) { setVertexAttributePointer(QT_OPACITY_ATTR, (GLfloat*)opacityArray.data()); } @@ -1096,7 +1103,7 @@ void QGL2PaintEngineExPrivate::resetClipIfNeeded() bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) { - if (brushTextureDirty && mode != ImageDrawingMode && mode != ImageArrayDrawingMode) + if (brushTextureDirty && !imageDrawingMode) updateBrushTexture(); if (compositionModeDirty) @@ -1116,12 +1123,12 @@ bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) } QGLEngineShaderManager::OpacityMode opacityMode; - if (mode == ImageArrayDrawingMode) { + if (mode == ImageArrayWithOpacityDrawingMode) { opacityMode = QGLEngineShaderManager::AttributeOpacity; } else { opacityMode = stateHasOpacity ? QGLEngineShaderManager::UniformOpacity : QGLEngineShaderManager::NoOpacity; - if (stateHasOpacity && (mode != ImageDrawingMode)) { + if (stateHasOpacity && !imageDrawingMode) { // Using a brush bool brushIsPattern = (currentBrush.style() >= Qt::Dense1Pattern) && (currentBrush.style() <= Qt::DiagCrossPattern); @@ -1141,7 +1148,7 @@ bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) matrixUniformDirty = true; } - if (brushUniformsDirty && mode != ImageDrawingMode && mode != ImageArrayDrawingMode) + if (brushUniformsDirty && !imageDrawingMode) updateBrushUniforms(); if (opacityMode == QGLEngineShaderManager::UniformOpacity && opacityUniformDirty) { @@ -1881,23 +1888,46 @@ void QGL2PaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *frag return; } + QSize size = pixmap.size(); + ensureActive(); int max_texture_size = d->ctx->d_func()->maxTextureSize(); - if (pixmap.width() > max_texture_size || pixmap.height() > max_texture_size) { + if (size.width() > max_texture_size || size.height() > max_texture_size) { QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio); - d->drawPixmapFragments(fragments, fragmentCount, scaled, hints); + d->drawPixmapFragments(fragments, fragmentCount, scaled, size, hints); } else { - d->drawPixmapFragments(fragments, fragmentCount, pixmap, hints); + d->drawPixmapFragments(fragments, fragmentCount, pixmap, size, hints); } } +void QGL2PaintEngineEx::drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) +{ + Q_D(QGL2PaintEngineEx); + // Use fallback for extended composition modes. + if (state()->composition_mode > QPainter::CompositionMode_Plus) { + QPaintEngineEx::drawPixmapFragments(targetRects, sourceRects, fragmentCount, pixmap, hints); + return; + } + + QSize size = pixmap.size(); + + ensureActive(); + int max_texture_size = d->ctx->d_func()->maxTextureSize(); + if (size.width() > max_texture_size || size.height() > max_texture_size) { + QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio); + d->drawPixmapFragments(targetRects, sourceRects, fragmentCount, scaled, size, hints); + } else { + d->drawPixmapFragments(targetRects, sourceRects, fragmentCount, pixmap, size, hints); + } +} void QGL2PaintEngineExPrivate::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, - QPainter::PixmapFragmentHints hints) + const QSize &size, QPainter::PixmapFragmentHints hints) { - GLfloat dx = 1.0f / pixmap.size().width(); - GLfloat dy = 1.0f / pixmap.size().height(); + GLfloat dx = 1.0f / size.width(); + GLfloat dy = 1.0f / size.height(); vertexCoordinateArray.clear(); textureCoordinateArray.clear(); @@ -1958,7 +1988,7 @@ void QGL2PaintEngineExPrivate::drawPixmapFragments(const QPainter::PixmapFragmen data[i].y = 1 - data[i].y; } - transferMode(ImageArrayDrawingMode); + transferMode(allOpaque ? ImageArrayDrawingMode : ImageArrayWithOpacityDrawingMode); bool isBitmap = pixmap.isQBitmap(); bool isOpaque = !isBitmap && (!pixmap.hasAlpha() || (hints & QPainter::OpaqueHint)) && allOpaque; @@ -1981,6 +2011,77 @@ void QGL2PaintEngineExPrivate::drawPixmapFragments(const QPainter::PixmapFragmen glDrawArrays(GL_TRIANGLES, 0, 6 * fragmentCount); } +void QGL2PaintEngineExPrivate::drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, + const QPixmap &pixmap, const QSize &size, + QPainter::PixmapFragmentHints hints) +{ + GLfloat dx = 1.0f / size.width(); + GLfloat dy = 1.0f / size.height(); + + vertexCoordinateArray.clear(); + textureCoordinateArray.clear(); + + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + for (int i = 0; i < fragmentCount; ++i) { + vertexCoordinateArray.addVertex(targetRects[i].right(), targetRects[i].bottom()); + vertexCoordinateArray.addVertex(targetRects[i].right(), targetRects[i].top()); + vertexCoordinateArray.addVertex(targetRects[i].left(), targetRects[i].top()); + vertexCoordinateArray.addVertex(targetRects[i].left(), targetRects[i].top()); + vertexCoordinateArray.addVertex(targetRects[i].left(), targetRects[i].bottom()); + vertexCoordinateArray.addVertex(targetRects[i].right(), targetRects[i].bottom()); + + QRectF sourceRect = sourceRects ? sourceRects[i] : QRectF(0, 0, size.width(), size.height()); + + QGLRect src(sourceRect.left() * dx, sourceRect.top() * dy, + sourceRect.right() * dx, sourceRect.bottom() * dy); + + textureCoordinateArray.addVertex(src.right, src.bottom); + textureCoordinateArray.addVertex(src.right, src.top); + textureCoordinateArray.addVertex(src.left, src.top); + textureCoordinateArray.addVertex(src.left, src.top); + textureCoordinateArray.addVertex(src.left, src.bottom); + textureCoordinateArray.addVertex(src.right, src.bottom); + } + + glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT); + QGLTexture *texture = ctx->d_func()->bindTexture(pixmap, GL_TEXTURE_2D, GL_RGBA, + QGLContext::InternalBindOption + | QGLContext::CanFlipNativePixmapBindOption); + + if (texture->options & QGLContext::InvertedYBindOption) { + // Flip texture y-coordinate. + QGLPoint *data = textureCoordinateArray.data(); + for (int i = 0; i < 6 * fragmentCount; ++i) + data[i].y = 1 - data[i].y; + } + + transferMode(ImageArrayDrawingMode); + + bool isBitmap = pixmap.isQBitmap(); + bool isOpaque = !isBitmap && (!pixmap.hasAlpha() || (hints & QPainter::OpaqueHint)); + + updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE, + q->state()->renderHints & QPainter::SmoothPixmapTransform, texture->id); + + // Setup for texture drawing + currentBrush = noBrush; + shaderManager->setSrcPixelType(isBitmap ? QGLEngineShaderManager::PatternSrc + : QGLEngineShaderManager::ImageSrc); + if (prepareForDraw(isOpaque)) + shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT); + + if (isBitmap) { + QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity); + shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col); + } + + glDrawArrays(GL_TRIANGLES, 0, 6 * fragmentCount); +} + bool QGL2PaintEngineEx::begin(QPaintDevice *pdev) { Q_D(QGL2PaintEngineEx); @@ -2001,6 +2102,7 @@ bool QGL2PaintEngineEx::begin(QPaintDevice *pdev) d->width = sz.width(); d->height = sz.height(); d->mode = BrushDrawingMode; + d->imageDrawingMode = false; d->brushTextureDirty = true; d->brushUniformsDirty = true; d->matrixUniformDirty = true; diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h index 2895d5a..80daf63 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h @@ -68,7 +68,8 @@ enum EngineMode { ImageDrawingMode, TextDrawingMode, BrushDrawingMode, - ImageArrayDrawingMode + ImageArrayDrawingMode, + ImageArrayWithOpacityDrawingMode }; QT_BEGIN_NAMESPACE @@ -126,6 +127,8 @@ public: virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr); virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, QPainter::PixmapFragmentHints hints); + virtual void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints); virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags = Qt::AutoColor); virtual void drawTextItem(const QPointF &p, const QTextItem &textItem); @@ -202,7 +205,9 @@ public: void fill(const QVectorPath &path); void stroke(const QVectorPath &path, const QPen &pen); void drawTexture(const QGLRect& dest, const QGLRect& src, const QSize &textureSize, bool opaque, bool pattern = false); - void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, const QSize &size, + QPainter::PixmapFragmentHints hints); + void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount, const QPixmap &pixmap, const QSize &size, QPainter::PixmapFragmentHints hints); void drawCachedGlyphs(QFontEngineGlyphCache::Type glyphType, QStaticTextItem *staticTextItem); @@ -255,6 +260,7 @@ public: int width, height; QGLContext *ctx; EngineMode mode; + bool imageDrawingMode; QFontEngineGlyphCache::Type glyphCacheType; // Dirty flags |