Use QImage::reinterpretAsFormat in QPixmap

Use the new QImage method instead of operating on private API.

At the same time the code is improved to ensure the QImage is detached.

Change-Id: Ia015c0bb18d7bc62da38397594730254843e5a0d
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2016-11-11 13:33:20 +01:00
parent ffecbfc980
commit 17e672a67e
3 changed files with 37 additions and 16 deletions

View File

@ -51,7 +51,6 @@
#include <QImageReader>
#include <QGuiApplication>
#include <QScreen>
#include <private/qimage_p.h>
#include <private/qsimd_p.h>
#include <private/qdrawhelper_p.h>
#include <qpa/qplatformscreen.h>
@ -135,7 +134,7 @@ bool QRasterPlatformPixmap::fromData(const uchar *buffer, uint len, const char *
if (image.isNull())
return false;
createPixmapForImage(image, flags, /* inplace = */true);
createPixmapForImage(std::move(image), flags);
return !isNull();
}
@ -143,13 +142,13 @@ void QRasterPlatformPixmap::fromImage(const QImage &sourceImage,
Qt::ImageConversionFlags flags)
{
QImage image = sourceImage;
createPixmapForImage(image, flags, /* inplace = */false);
createPixmapForImage(std::move(image), flags);
}
void QRasterPlatformPixmap::fromImageInPlace(QImage &sourceImage,
Qt::ImageConversionFlags flags)
{
createPixmapForImage(sourceImage, flags, /* inplace = */true);
createPixmapForImage(std::move(sourceImage), flags);
}
void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,
@ -160,7 +159,7 @@ void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,
if (image.isNull())
return;
createPixmapForImage(image, flags, /* inplace = */true);
createPixmapForImage(std::move(image), flags);
}
// from qbackingstore.cpp
@ -301,7 +300,7 @@ int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric) const
return 0;
}
void QRasterPlatformPixmap::createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace)
void QRasterPlatformPixmap::createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
{
QImage::Format format;
if (flags & Qt::NoFormatConversion)
@ -335,16 +334,10 @@ void QRasterPlatformPixmap::createPixmapForImage(QImage &sourceImage, Qt::ImageC
if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
|| sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
{
inPlace = inPlace && sourceImage.isDetached();
image = sourceImage;
if (!inPlace)
image.detach();
if (image.d)
image.d->format = QImage::Format_RGB32;
} else if (inPlace && sourceImage.d->convertInPlace(format, flags)) {
image = sourceImage;
image = std::move(sourceImage);
image.reinterpretAsFormat(QImage::Format_RGB32);
} else {
image = sourceImage.convertToFormat(format, flags);
image = std::move(sourceImage).convertToFormat(format, flags);
}
if (image.d) {

View File

@ -85,7 +85,7 @@ public:
protected:
int metric(QPaintDevice::PaintDeviceMetric metric) const Q_DECL_OVERRIDE;
void createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace);
void createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags);
void setImage(const QImage &image);
QImage image;
static QImage::Format systemOpaqueFormat();

View File

@ -99,6 +99,7 @@ private slots:
void task_51271();
void convertFromImageNoDetach();
void convertFromImageNoDetach2();
void convertFromImageDetach();
void convertFromImageCacheKey();
@ -766,6 +767,33 @@ void tst_QPixmap::convertFromImageNoDetach()
QCOMPARE(constOrig.bits(), constCopy.bits());
}
void tst_QPixmap::convertFromImageNoDetach2()
{
QPixmap randomPixmap(10, 10);
if (randomPixmap.handle()->classId() != QPlatformPixmap::RasterClass)
QSKIP("Test only valid for raster pixmaps");
//first get the screen format
QImage::Format screenFormat = randomPixmap.toImage().format();
QVERIFY(screenFormat != QImage::Format_Invalid);
if (screenFormat != QImage::Format_RGB32 &&
screenFormat != QImage::Format_ARGB32_Premultiplied)
QSKIP("Test only valid for platforms with RGB32 pixmaps");
QImage orig(100,100, QImage::Format_ARGB32_Premultiplied);
orig.fill(Qt::white);
const uchar *origBits = orig.constBits();
QPixmap pix = QPixmap::fromImage(std::move(orig));
QImage copy = pix.toImage();
QVERIFY(!copy.hasAlphaChannel());
QCOMPARE(copy.format(), QImage::Format_RGB32);
QCOMPARE(origBits, copy.constBits());
}
void tst_QPixmap::convertFromImageDetach()
{
QImage img(10,10, QImage::Format_RGB32);