summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@nokia.com>2010-06-22 11:24:26 (GMT)
committerSamuel Rødal <samuel.rodal@nokia.com>2010-06-23 08:45:11 (GMT)
commitfb76a872e20bd0df8e7bbe9c039b7f20423c6f12 (patch)
treec01d5f47cad65a056eb6edc1cab11c628b11e2f8
parenteaf886318568bfbd9aa1722586264c8fdf90df12 (diff)
downloadQt-fb76a872e20bd0df8e7bbe9c039b7f20423c6f12.zip
Qt-fb76a872e20bd0df8e7bbe9c039b7f20423c6f12.tar.gz
Qt-fb76a872e20bd0df8e7bbe9c039b7f20423c6f12.tar.bz2
Optimized sub-rect copying / painting of QPixmaps.
Prevented downloading of the whole XImage by introducing new QPixmapData::toImage() overload taking a sub-rect. Also avoid an additional copy by simply taking ownership of the XImage data when the XImage format matches the QImage format. Reviewed-by: Trond
-rw-r--r--src/gui/image/qpixmap_raster.cpp14
-rw-r--r--src/gui/image/qpixmap_raster_p.h1
-rw-r--r--src/gui/image/qpixmap_x11.cpp172
-rw-r--r--src/gui/image/qpixmap_x11_p.h8
-rw-r--r--src/gui/image/qpixmapdata.cpp10
-rw-r--r--src/gui/image/qpixmapdata_p.h1
-rw-r--r--src/gui/painting/qpaintengine_raster.cpp8
7 files changed, 152 insertions, 62 deletions
diff --git a/src/gui/image/qpixmap_raster.cpp b/src/gui/image/qpixmap_raster.cpp
index 13c03a1..13e95c7 100644
--- a/src/gui/image/qpixmap_raster.cpp
+++ b/src/gui/image/qpixmap_raster.cpp
@@ -289,6 +289,20 @@ QImage QRasterPixmapData::toImage() const
return image;
}
+QImage QRasterPixmapData::toImage(const QRect &rect) const
+{
+ if (rect.isNull())
+ return image;
+
+ QRect clipped = rect.intersected(QRect(0, 0, w, h));
+ if (d % 8 == 0)
+ return QImage(image.scanLine(clipped.y()) + clipped.x() * (d / 8),
+ clipped.width(), clipped.height(),
+ image.bytesPerLine(), image.format());
+ else
+ return image.copy(clipped);
+}
+
void QRasterPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
{
image.setAlphaChannel(alphaChannel.toImage());
diff --git a/src/gui/image/qpixmap_raster_p.h b/src/gui/image/qpixmap_raster_p.h
index d7e3f85..42ceeca 100644
--- a/src/gui/image/qpixmap_raster_p.h
+++ b/src/gui/image/qpixmap_raster_p.h
@@ -81,6 +81,7 @@ public:
bool hasAlphaChannel() const;
void setAlphaChannel(const QPixmap &alphaChannel);
QImage toImage() const;
+ QImage toImage(const QRect &rect) const;
QPaintEngine* paintEngine() const;
QImage* buffer();
diff --git a/src/gui/image/qpixmap_x11.cpp b/src/gui/image/qpixmap_x11.cpp
index e8dc5ae..604dbf5 100644
--- a/src/gui/image/qpixmap_x11.cpp
+++ b/src/gui/image/qpixmap_x11.cpp
@@ -1458,6 +1458,95 @@ int QX11PixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const
}
}
+struct QXImageWrapper
+{
+ XImage *xi;
+};
+
+bool QX11PixmapData::canTakeQImageFromXImage(const QXImageWrapper &xiWrapper) const
+{
+ XImage *xi = xiWrapper.xi;
+
+ // ARGB32_Premultiplied
+ if (picture && depth() == 32)
+ return true;
+
+ Visual *visual = (Visual *)xinfo.visual();
+
+ // RGB32
+ if (depth() == 24 && xi->bits_per_pixel == 32 && visual->red_mask == 0xff0000
+ && visual->green_mask == 0xff00 && visual->blue_mask == 0xff)
+ return true;
+
+ // RGB16
+ if (depth() == 16 && xi->bits_per_pixel == 16 && visual->red_mask == 0xf800
+ && visual->green_mask == 0x7e0 && visual->blue_mask == 0x1f)
+ return true;
+
+ return false;
+}
+
+QImage QX11PixmapData::takeQImageFromXImage(const QXImageWrapper &xiWrapper) const
+{
+ XImage *xi = xiWrapper.xi;
+
+ QImage::Format format = QImage::Format_ARGB32_Premultiplied;
+ if (depth() == 24)
+ format = QImage::Format_RGB32;
+ else if (depth() == 16)
+ format = QImage::Format_RGB16;
+
+ QImage image((uchar *)xi->data, xi->width, xi->height, xi->bytes_per_line, format);
+ // take ownership
+ image.data_ptr()->own_data = true;
+ xi->data = 0;
+
+ // we may have to swap the byte order
+ if ((QSysInfo::ByteOrder == QSysInfo::LittleEndian && xi->byte_order == MSBFirst)
+ || (QSysInfo::ByteOrder == QSysInfo::BigEndian && xi->byte_order == LSBFirst))
+ {
+ for (int i=0; i < image.height(); i++) {
+ if (depth() == 16) {
+ ushort *p = (ushort*)image.scanLine(i);
+ ushort *end = p + image.width();
+ while (p < end) {
+ *p = ((*p << 8) & 0xff00) | ((*p >> 8) & 0x00ff);
+ p++;
+ }
+ } else {
+ uint *p = (uint*)image.scanLine(i);
+ uint *end = p + image.width();
+ while (p < end) {
+ *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
+ | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
+ p++;
+ }
+ }
+ }
+ }
+
+ XDestroyImage(xi);
+ return image;
+}
+
+QImage QX11PixmapData::toImage(const QRect &rect) const
+{
+ QXImageWrapper xiWrapper;
+ xiWrapper.xi = XGetImage(X11->display, hd, rect.x(), rect.y(), rect.width(), rect.height(),
+ AllPlanes, (depth() == 1) ? XYPixmap : ZPixmap);
+
+ Q_CHECK_PTR(xiWrapper.xi);
+ if (!xiWrapper.xi)
+ return QImage();
+
+ if (canTakeQImageFromXImage(xiWrapper))
+ return takeQImageFromXImage(xiWrapper);
+
+ QImage image = toImage(xiWrapper, rect);
+ qSafeXDestroyImage(xiWrapper.xi);
+ return image;
+}
+
/*!
Converts the pixmap to a QImage. Returns a null image if the
conversion fails.
@@ -1475,6 +1564,13 @@ int QX11PixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const
QImage QX11PixmapData::toImage() const
{
+ return toImage(QRect(0, 0, w, h));
+}
+
+QImage QX11PixmapData::toImage(const QXImageWrapper &xiWrapper, const QRect &rect) const
+{
+ XImage *xi = xiWrapper.xi;
+
int d = depth();
Visual *visual = (Visual *)xinfo.visual();
bool trucol = (visual->c_class >= TrueColor) && d > 1;
@@ -1492,59 +1588,21 @@ QImage QX11PixmapData::toImage() const
format = QImage::Format_RGB32;
}
- XImage *xi = XGetImage(X11->display, hd, 0, 0, w, h, AllPlanes,
- (d == 1) ? XYPixmap : ZPixmap);
-
- Q_CHECK_PTR(xi);
- if (!xi)
- return QImage();
-
- if (picture && depth() == 32) {
- QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
- memcpy(image.bits(), xi->data, xi->bytes_per_line * xi->height);
-
- // we may have to swap the byte order
- if ((QSysInfo::ByteOrder == QSysInfo::LittleEndian && xi->byte_order == MSBFirst)
- || (QSysInfo::ByteOrder == QSysInfo::BigEndian && xi->byte_order == LSBFirst))
- {
- for (int i=0; i < image.height(); i++) {
- uint *p = (uint*)image.scanLine(i);
- uint *end = p + image.width();
- if ((xi->byte_order == LSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian)
- || (xi->byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian)) {
- while (p < end) {
- *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
- | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
- p++;
- }
- } else if (xi->byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian) {
- while (p < end) {
- *p = ((*p << 16) & 0x00ff0000) | ((*p >> 16) & 0x000000ff)
- | ((*p ) & 0xff00ff00);
- p++;
- }
- }
- }
- }
-
- // throw away image data
- qSafeXDestroyImage(xi);
-
- return image;
- }
-
if (d == 1 && xi->bitmap_bit_order == LSBFirst)
format = QImage::Format_MonoLSB;
if (x11_mask && format == QImage::Format_RGB32)
format = QImage::Format_ARGB32;
- QImage image(w, h, format);
+ QImage image(xi->width, xi->height, format);
if (image.isNull()) // could not create image
return image;
QImage alpha;
if (x11_mask) {
- alpha = mask().toImage();
+ if (rect.contains(QRect(0, 0, w, h)))
+ alpha = mask().toImage();
+ else
+ alpha = mask().toImage().copy(rect);
}
bool ale = alpha.format() == QImage::Format_MonoLSB;
@@ -1587,11 +1645,11 @@ QImage QX11PixmapData::toImage() const
if (bppc > 8 && xi->byte_order == LSBFirst)
bppc++;
- for (int y = 0; y < h; ++y) {
+ for (int y = 0; y < xi->height; ++y) {
uchar* asrc = x11_mask ? alpha.scanLine(y) : 0;
dst = (QRgb *)image.scanLine(y);
src = (uchar *)xi->data + xi->bytes_per_line*y;
- for (int x = 0; x < w; x++) {
+ for (int x = 0; x < xi->width; x++) {
switch (bppc) {
case 8:
pixel = *src++;
@@ -1621,8 +1679,8 @@ QImage QX11PixmapData::toImage() const
src += 4;
break;
default: // should not really happen
- x = w; // leave loop
- y = h;
+ x = xi->width; // leave loop
+ y = xi->height;
pixel = 0; // eliminate compiler warning
qWarning("QPixmap::convertToImage: Invalid depth %d", bppc);
}
@@ -1660,7 +1718,7 @@ QImage QX11PixmapData::toImage() const
} else if (xi->bits_per_pixel == d) { // compatible depth
char *xidata = xi->data; // copy each scanline
int bpl = qMin(image.bytesPerLine(),xi->bytes_per_line);
- for (int y=0; y<h; y++) {
+ for (int y=0; y<xi->height; y++) {
memcpy(image.scanLine(y), xidata, bpl);
xidata += xi->bytes_per_line;
}
@@ -1686,17 +1744,17 @@ QImage QX11PixmapData::toImage() const
bpl = image.bytesPerLine();
if (x11_mask) { // which pixels are used?
- for (int i = 0; i < h; i++) {
+ for (int i = 0; i < xi->height; i++) {
uchar* asrc = alpha.scanLine(i);
p = image.scanLine(i);
if (ale) {
- for (int x = 0; x < w; x++) {
+ for (int x = 0; x < xi->width; x++) {
if (asrc[x >> 3] & (1 << (x & 7)))
use[*p] = 1;
++p;
}
} else {
- for (int x = 0; x < w; x++) {
+ for (int x = 0; x < xi->width; x++) {
if (asrc[x >> 3] & (0x80 >> (x & 7)))
use[*p] = 1;
++p;
@@ -1704,7 +1762,7 @@ QImage QX11PixmapData::toImage() const
}
}
} else {
- for (int i = 0; i < h; i++) {
+ for (int i = 0; i < xi->height; i++) {
p = image.scanLine(i);
end = p + bpl;
while (p < end)
@@ -1716,7 +1774,7 @@ QImage QX11PixmapData::toImage() const
if (use[i])
pix[i] = ncols++;
}
- for (int i = 0; i < h; i++) { // translate pixels
+ for (int i = 0; i < xi->height; i++) { // translate pixels
p = image.scanLine(i);
end = p + bpl;
while (p < end) {
@@ -1736,17 +1794,17 @@ QImage QX11PixmapData::toImage() const
// use first pixel in image (as good as any).
trans = image.scanLine(0)[0];
}
- for (int i = 0; i < h; i++) {
+ for (int i = 0; i < xi->height; i++) {
uchar* asrc = alpha.scanLine(i);
p = image.scanLine(i);
if (ale) {
- for (int x = 0; x < w; x++) {
+ for (int x = 0; x < xi->width; x++) {
if (!(asrc[x >> 3] & (1 << (x & 7))))
*p = trans;
++p;
}
} else {
- for (int x = 0; x < w; x++) {
+ for (int x = 0; x < xi->width; x++) {
if (!(asrc[x >> 3] & (1 << (7 -(x & 7)))))
*p = trans;
++p;
@@ -1764,8 +1822,6 @@ QImage QX11PixmapData::toImage() const
}
}
- qSafeXDestroyImage(xi);
-
return image;
}
diff --git a/src/gui/image/qpixmap_x11_p.h b/src/gui/image/qpixmap_x11_p.h
index 7575838..821fb69 100644
--- a/src/gui/image/qpixmap_x11_p.h
+++ b/src/gui/image/qpixmap_x11_p.h
@@ -62,6 +62,8 @@ QT_BEGIN_NAMESPACE
class QX11PaintEngine;
+struct QXImageWrapper;
+
class Q_GUI_EXPORT QX11PixmapData : public QPixmapData
{
public:
@@ -87,6 +89,7 @@ public:
QPixmap transformed(const QTransform &transform,
Qt::TransformationMode mode) const;
QImage toImage() const;
+ QImage toImage(const QRect &rect) const;
QPaintEngine* paintEngine() const;
Qt::HANDLE handle() const { return hd; }
@@ -116,10 +119,15 @@ private:
void release();
+ QImage toImage(const QXImageWrapper &xi, const QRect &rect) const;
+
QBitmap mask_to_bitmap(int screen) const;
static Qt::HANDLE bitmap_to_mask(const QBitmap &, int screen);
void bitmapFromImage(const QImage &image);
+ bool canTakeQImageFromXImage(const QXImageWrapper &xi) const;
+ QImage takeQImageFromXImage(const QXImageWrapper &xi) const;
+
Qt::HANDLE hd;
enum Flag {
diff --git a/src/gui/image/qpixmapdata.cpp b/src/gui/image/qpixmapdata.cpp
index 31ca909..345e3cf 100644
--- a/src/gui/image/qpixmapdata.cpp
+++ b/src/gui/image/qpixmapdata.cpp
@@ -146,7 +146,7 @@ bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::I
void QPixmapData::copy(const QPixmapData *data, const QRect &rect)
{
- fromImage(data->toImage().copy(rect), Qt::NoOpaqueDetection);
+ fromImage(data->toImage(rect), Qt::NoOpaqueDetection);
}
bool QPixmapData::scroll(int dx, int dy, const QRect &rect)
@@ -255,6 +255,14 @@ void QPixmapData::setSerialNumber(int serNo)
ser_no = serNo;
}
+QImage QPixmapData::toImage(const QRect &rect) const
+{
+ if (rect.contains(QRect(0, 0, w, h)))
+ return toImage();
+ else
+ return toImage().copy(rect);
+}
+
QImage* QPixmapData::buffer()
{
return 0;
diff --git a/src/gui/image/qpixmapdata_p.h b/src/gui/image/qpixmapdata_p.h
index 60ed26a..9a1505a 100644
--- a/src/gui/image/qpixmapdata_p.h
+++ b/src/gui/image/qpixmapdata_p.h
@@ -102,6 +102,7 @@ public:
virtual void setAlphaChannel(const QPixmap &alphaChannel);
virtual QPixmap alphaChannel() const;
virtual QImage toImage() const = 0;
+ virtual QImage toImage(const QRect &rect) const;
virtual QPaintEngine* paintEngine() const = 0;
inline int serialNumber() const { return ser_no; }
diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp
index 94e5cbc..84b36c7 100644
--- a/src/gui/painting/qpaintengine_raster.cpp
+++ b/src/gui/painting/qpaintengine_raster.cpp
@@ -2419,7 +2419,9 @@ void QRasterPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pixmap, cons
drawImage(r, image, sr);
}
} else {
- const QImage image = pixmap.toImage();
+ QRect clippedSource = sr.toAlignedRect().intersected(pixmap.rect());
+ const QImage image = pd->toImage(clippedSource);
+ QRectF translatedSource = sr.translated(-clippedSource.topLeft());
if (image.depth() == 1) {
Q_D(QRasterPaintEngine);
QRasterPaintEngineState *s = state();
@@ -2430,10 +2432,10 @@ void QRasterPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pixmap, cons
drawBitmap(r.topLeft() + QPointF(s->matrix.dx(), s->matrix.dy()), image, &s->penData);
return;
} else {
- drawImage(r, d->rasterBuffer->colorizeBitmap(image, s->pen.color()), sr);
+ drawImage(r, d->rasterBuffer->colorizeBitmap(image, s->pen.color()), translatedSource);
}
} else {
- drawImage(r, image, sr);
+ drawImage(r, image, translatedSource);
}
}
}