diff options
author | Laszlo Agocs <laszlo.p.agocs@nokia.com> | 2011-03-22 11:24:21 (GMT) |
---|---|---|
committer | Laszlo Agocs <laszlo.p.agocs@nokia.com> | 2011-03-22 11:27:30 (GMT) |
commit | 30dbb938701963c92fc911f59c72720741e556d1 (patch) | |
tree | 4d27f6756f52fd9bd0db235880b6a290ca569f35 | |
parent | 8ba79f904cc94ac40525cdb3dad534caa5cb9289 (diff) | |
download | Qt-30dbb938701963c92fc911f59c72720741e556d1.zip Qt-30dbb938701963c92fc911f59c72720741e556d1.tar.gz Qt-30dbb938701963c92fc911f59c72720741e556d1.tar.bz2 |
Implement tiled image and pixmap drawing in VG paint engine.
The vgWritePixel-based "fallback" in drawImage can only be used when
the transformation is simple and no opacity is set. The tiled drawing
introduced here will serve as an additional fallback for the more
complex cases and will allow drawing large (12 Mpix or more) pixmaps
and images with transformation and semi-transparency even when the
complete image data would not fit into the GPU memory.
Task-number: QTBUG-18251
Reviewed-by: Jani Hautakangas
-rw-r--r-- | src/openvg/qpaintengine_vg.cpp | 101 |
1 files changed, 98 insertions, 3 deletions
diff --git a/src/openvg/qpaintengine_vg.cpp b/src/openvg/qpaintengine_vg.cpp index 44dceea..570adfd 100644 --- a/src/openvg/qpaintengine_vg.cpp +++ b/src/openvg/qpaintengine_vg.cpp @@ -3071,6 +3071,95 @@ static void drawVGImage(QVGPaintEnginePrivate *d, vgDrawImage(vgImg); } +static void drawImageTiled(QVGPaintEnginePrivate *d, + const QRectF &r, + const QImage &image, + const QRectF &sr = QRectF()) +{ + const int minTileSize = 16; + int tileWidth = 512; + int tileHeight = tileWidth; + + VGImageFormat tileFormat = qt_vg_image_to_vg_format(image.format()); + VGImage tile = VG_INVALID_HANDLE; + QVGImagePool *pool = QVGImagePool::instance(); + while (tile == VG_INVALID_HANDLE && tileWidth >= minTileSize) { + tile = pool->createPermanentImage(tileFormat, tileWidth, tileHeight, + VG_IMAGE_QUALITY_FASTER); + if (tile == VG_INVALID_HANDLE) { + tileWidth /= 2; + tileHeight /= 2; + } + } + if (tile == VG_INVALID_HANDLE) { + qWarning("drawImageTiled: Failed to create %dx%d tile, giving up", tileWidth, tileHeight); + return; + } + + VGfloat opacityMatrix[20] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, d->opacity, + 0.0f, 0.0f, 0.0f, 0.0f + }; + VGImage tileWithOpacity = VG_INVALID_HANDLE; + if (d->opacity != 1) { + tileWithOpacity = pool->createPermanentImage(VG_sARGB_8888_PRE, + tileWidth, tileHeight, VG_IMAGE_QUALITY_FASTER); + if (tileWithOpacity == VG_INVALID_HANDLE) + qWarning("drawImageTiled: Failed to create extra tile, ignoring opacity"); + } + + QRect sourceRect = sr.toRect(); + if (sourceRect.isNull()) + sourceRect = QRect(0, 0, image.width(), image.height()); + + VGfloat scaleX = r.width() / sourceRect.width(); + VGfloat scaleY = r.height() / sourceRect.height(); + + d->setImageOptions(); + + for (int y = sourceRect.y(); y < sourceRect.height(); y += tileHeight) { + int h = qMin(tileHeight, sourceRect.height() - y); + if (h < 1) + break; + for (int x = sourceRect.x(); x < sourceRect.width(); x += tileWidth) { + int w = qMin(tileWidth, sourceRect.width() - x); + if (w < 1) + break; + + int bytesPerPixel = image.depth() / 8; + const uchar *sptr = image.constBits() + x * bytesPerPixel + y * image.bytesPerLine(); + vgImageSubData(tile, sptr, image.bytesPerLine(), tileFormat, 0, 0, w, h); + + QTransform transform(d->imageTransform); + transform.translate(r.x() + x, r.y() + y); + transform.scale(scaleX, scaleY); + d->setTransform(VG_MATRIX_IMAGE_USER_TO_SURFACE, transform); + + VGImage actualTile = tile; + if (tileWithOpacity != VG_INVALID_HANDLE) { + vgColorMatrix(tileWithOpacity, actualTile, opacityMatrix); + if (w < tileWidth || h < tileHeight) + actualTile = vgChildImage(tileWithOpacity, 0, 0, w, h); + else + actualTile = tileWithOpacity; + } else if (w < tileWidth || h < tileHeight) { + actualTile = vgChildImage(tile, 0, 0, w, h); + } + vgDrawImage(actualTile); + + if (actualTile != tile && actualTile != tileWithOpacity) + vgDestroyImage(actualTile); + } + } + + vgDestroyImage(tile); + if (tileWithOpacity != VG_INVALID_HANDLE) + vgDestroyImage(tileWithOpacity); +} + // Used by qpixmapfilter_vg.cpp to draw filtered VGImage's. void qt_vg_drawVGImage(QPainter *painter, const QPointF& pos, VGImage vgImg) { @@ -3170,7 +3259,7 @@ void QVGPaintEngine::drawPixmap(const QPointF &pos, const QPixmap &pm) vgpd->source.beginDataAccess(); drawImage(pos, vgpd->source.imageRef()); - vgpd->source.endDataAccess(); + vgpd->source.endDataAccess(true); } else { drawImage(pos, *(pd->buffer())); } @@ -3219,7 +3308,10 @@ void QVGPaintEngine::drawImage } else { // Monochrome images need to use the vgChildImage() path. vgImg = toVGImage(image, flags); - drawVGImage(d, r, vgImg, image.size(), sr); + if (vgImg == VG_INVALID_HANDLE) + drawImageTiled(d, r, image, sr); + else + drawVGImage(d, r, vgImg, image.size(), sr); } } vgDestroyImage(vgImg); @@ -3246,7 +3338,10 @@ void QVGPaintEngine::drawImage(const QPointF &pos, const QImage &image) } else { vgImg = toVGImageWithOpacity(image, d->opacity); } - drawVGImage(d, pos, vgImg); + if (vgImg == VG_INVALID_HANDLE) + drawImageTiled(d, QRectF(pos, image.size()), image); + else + drawVGImage(d, pos, vgImg); vgDestroyImage(vgImg); } |