From d797a04adc5a76146ecff36e17b2a8658b0a5022 Mon Sep 17 00:00:00 2001
From: Jan Arve Saether <jan-arve.saether@theqtcompany.com>
Date: Tue, 22 Sep 2015 13:36:22 +0200
Subject: [PATCH] Speed up printing when drawing opaque primitives.

We only need to merge the rectangles into a region if we are painting
a non-opaque a primitive with alpha.

Performance measurements QT_print_speed_bug.zip:
Excluding patch: 244686 ms
Including patch:   5070 ms

This is an improvement of 48x (for debug build)

Task-number: QTBUG-48334
Change-Id: I03684c6e7d8a5fb039ea6477ed1a860f09e1b08c
Reviewed-by: Andy Shaw <andy.shaw@theqtcompany.com>
---
 src/printsupport/kernel/qpaintengine_alpha.cpp | 10 +++++++++-
 src/printsupport/kernel/qpaintengine_alpha_p.h |  6 ++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/printsupport/kernel/qpaintengine_alpha.cpp b/src/printsupport/kernel/qpaintengine_alpha.cpp
index 710691453ab..0ae9464b691 100644
--- a/src/printsupport/kernel/qpaintengine_alpha.cpp
+++ b/src/printsupport/kernel/qpaintengine_alpha.cpp
@@ -376,6 +376,7 @@ QAlphaPaintEnginePrivate::QAlphaPaintEnginePrivate()
         m_pic(0),
         m_picengine(0),
         m_picpainter(0),
+        m_numberOfCachedRects(0),
         m_hasalpha(false),
         m_alphaPen(false),
         m_alphaBrush(false),
@@ -426,7 +427,14 @@ void QAlphaPaintEnginePrivate::addAlphaRect(const QRectF &rect)
 
 bool QAlphaPaintEnginePrivate::canSeeTroughBackground(bool somethingInRectHasAlpha, const QRectF &rect) const
 {
-    return somethingInRectHasAlpha && m_dirtyrgn.intersects(rect.toAlignedRect());
+    if (somethingInRectHasAlpha) {
+        if (m_dirtyRects.count() != m_numberOfCachedRects) {
+            m_cachedDirtyRgn.setRects(m_dirtyRects.constData(), m_dirtyRects.count());
+            m_numberOfCachedRects = m_dirtyRects.count();
+        }
+        return m_cachedDirtyRgn.intersects(rect.toAlignedRect());
+    }
+    return false;
 }
 
 void QAlphaPaintEnginePrivate::drawAlphaImage(const QRectF &rect)
diff --git a/src/printsupport/kernel/qpaintengine_alpha_p.h b/src/printsupport/kernel/qpaintengine_alpha_p.h
index 2becad73793..49a9938e56c 100644
--- a/src/printsupport/kernel/qpaintengine_alpha_p.h
+++ b/src/printsupport/kernel/qpaintengine_alpha_p.h
@@ -99,7 +99,9 @@ public:
 
     QRegion m_alphargn;
     QRegion m_cliprgn;
-    QRegion m_dirtyrgn;
+    mutable QRegion m_cachedDirtyRgn;
+    mutable int m_numberOfCachedRects;
+    QVector<QRect> m_dirtyRects;
 
     bool m_hasalpha;
     bool m_alphaPen;
@@ -115,7 +117,7 @@ public:
     QPen m_pen;
 
     void addAlphaRect(const QRectF &rect);
-    void addDirtyRect(const QRectF &rect) { m_dirtyrgn |= rect.toAlignedRect(); }
+    void addDirtyRect(const QRectF &rect) { m_dirtyRects.append(rect.toAlignedRect()); }
     bool canSeeTroughBackground(bool somethingInRectHasAlpha, const QRectF &rect) const;
 
     QRectF addPenWidth(const QPainterPath &path);
-- 
GitLab