Skip to content

Commit

Permalink
[qtdeclarative] Load images in an OpenGL texture friendly format. Con…
Browse files Browse the repository at this point in the history
…tributes to JB#49193

Pre-emptively convert images in the pixmap cache to RGBX8888 or
RGBA8888_Premultipled to avoid potentially doing the conversion in the
render thread where it will directly impact the frame rate.
  • Loading branch information
denexter authored and rainemak committed Mar 24, 2020
1 parent 7bd6917 commit 4a9b06b
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/quick/util/qquickpixmapcache.cpp
Expand Up @@ -347,26 +347,30 @@ QNetworkAccessManager *QQuickPixmapReader::networkAccessManager()
return accessManager;
}

static void maybeRemoveAlpha(QImage *image)
{
// If the image
if (image->hasAlphaChannel() && image->data_ptr()
&& !image->data_ptr()->checkForAlphaPixels()) {
switch (image->format()) {
case QImage::Format_RGBA8888:
case QImage::Format_RGBA8888_Premultiplied:
*image = image->convertToFormat(QImage::Format_RGBX8888);
break;
case QImage::Format_A2BGR30_Premultiplied:
*image = image->convertToFormat(QImage::Format_BGR30);
break;
case QImage::Format_A2RGB30_Premultiplied:
*image = image->convertToFormat(QImage::Format_RGB30);
break;
default:
*image = image->convertToFormat(QImage::Format_RGB32);
break;
}
namespace {

class ConversionImage : public QImage
{
public:
using QImage::convertToFormat_inplace;
};

}

static void convertImageToFormat(QImage *image, QImage::Format format)
{
if (!static_cast<ConversionImage *>(image)->convertToFormat_inplace(format, Qt::AutoColor)) {
*image = image->convertToFormat(format);
}
}

static void optimizeImageForTexture(QImage *image)
{
if (!image->hasAlphaChannel()
|| (image->data_ptr() && !image->data_ptr()->checkForAlphaPixels())) {
convertImageToFormat(image, QImage::Format_RGBX8888);
} else {
convertImageToFormat(image, QImage::Format_RGBA8888_Premultiplied);
}
}

Expand Down Expand Up @@ -403,7 +407,7 @@ static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *e
*impsize = imgio.size();

if (imgio.read(image)) {
maybeRemoveAlpha(image);
optimizeImageForTexture(image);
if (impsize && impsize->width() < 0)
*impsize = image->size();
return true;
Expand Down

0 comments on commit 4a9b06b

Please sign in to comment.