~neon/kolourpaint/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
At 100% zoom: kpMainWindow::drawTransparentBackground() accounts for
about 75% of kpView::paintEvent()'s time.  Bottleneck is
QPainter::fillRect().  QPainter::drawPixmap() seems much faster.  For
800x600, renderer goes from 10ms to 1ms.

2007-10-12:
Have not reprofiled KolourPaint under Qt4 to determine whether this patch
is still worthwhile (I suspect it still is since QPainter/X11 could not
magically have gotten faster).  In any case, the patch needs to be updated
before being applied.

--- kpmainwindow.cpp	2004-08-05 02:10:38.000000000 +1000
+++ kpmainwindow.cpp	2004-09-29 11:24:45.000000000 +1000
@@ -838,12 +838,116 @@
 }
 
 
+#if 1
+// (indexed by [isPreview][parity])
+static QPixmap *checkerBoardCache [2][2] = {{0, 0}, {0, 0}};
+
+
+static int checkerBoardCellSize (bool isPreview)
+{
+    return !isPreview ? 16 : 10;
+}
+
+
+// public
+static QPixmap *createCheckerBoardCache (bool isPreview, bool parity)
+{
+    int cellSize = checkerBoardCellSize (isPreview);
+    const int rep = 2;  // must be multiple of 2
+
+    QPixmap *newPixmap = new QPixmap (cellSize * rep, cellSize * rep);
+    QPainter painter (newPixmap);
+
+    int parityAsInt = parity ? 1 : 0;
+    for (int y = 0; y < rep; y++)
+    {
+        for (int x = 0; x < rep; x++)
+        {
+            QColor col;
+
+            if ((parityAsInt + x + y) % 2)
+            {
+                if (!isPreview)
+                    col = QColor (213, 213, 213);
+                else
+                    col = QColor (224, 224, 224);
+            }
+            else
+                col = Qt::white;
+
+            painter.fillRect (x * cellSize, y * cellSize,
+                              cellSize, cellSize,
+                              col);
+        }
+    }
+
+    painter.end ();
+    return newPixmap;
+}
+
+void kpMainWindow::drawTransparentBackground (QPainter *painter,
+                                              int /*viewWidth*/, int /*viewHeight*/,
+                                              const QRect &rect,
+                                              bool isPreview)
+{
+#if DEBUG_KP_MAIN_WINDOW && 1 || 1
+    kDebug () << "\tkpMainWindow::drawTransparentBackground(rect="
+               << rect << ")" << endl;
+    QTime totalTimer; totalTimer.start ();
+#endif
+
+    int cellSize = checkerBoardCellSize (isPreview);
+
+
+    int starty = rect.y ();
+    if (starty % cellSize)
+        starty -= (starty % cellSize);
+
+    int startx = rect.x ();
+    if (startx % cellSize)
+        startx -= (startx % cellSize);
+
+
+    int parity = ((startx / cellSize + starty / cellSize) % 2) ? 1 : 0;
+
+    if (!checkerBoardCache [isPreview][parity])
+    {
+        checkerBoardCache [isPreview][parity] = createCheckerBoardCache (isPreview, parity);
+    }
+
+    QPixmap *tilePixmap = checkerBoardCache [isPreview][parity];
+    for (int y = starty; y <= rect.bottom (); y += tilePixmap->height ())
+    {
+        for (int x = startx; x <= rect.right (); x += tilePixmap->width ())
+        {
+            painter->drawPixmap (x - rect.x (), y - rect.y (), *tilePixmap);
+        }
+    }
+
+#if DEBUG_KP_MAIN_WINDOW && 1 || 1
+{
+    const int totalTimerElapsed = totalTimer.elapsed ();
+    kDebug () << "\t\ttotal=" << totalTimerElapsed << endl;
+}
+#endif
+}
+
+
+#else
+
 // public
 void kpMainWindow::drawTransparentBackground (QPainter *painter,
                                               int /*viewWidth*/, int /*viewHeight*/,
                                               const QRect &rect,
                                               bool isPreview)
 {
+#if DEBUG_KP_MAIN_WINDOW && 1
+    kDebug () << "\tkpMainWindow::drawTransparentBackground(rect="
+               << rect << ")" << endl;
+    QTime totalTimer; totalTimer.start ();
+#endif
+
+
     const int cellSize = !isPreview ? 16 : 10;
 
     int starty = rect.y ();
@@ -877,8 +982,15 @@
         }
     }
     painter->restore ();
-}
 
+#if DEBUG_KP_MAIN_WINDOW && 1 || 1
+{
+    const int totalTimerElapsed = totalTimer.elapsed ();
+    kDebug () << "\t\ttotal=" << totalTimerElapsed << endl;
+}
+#endif
+}
+#endif
 
 // private slot
 void kpMainWindow::slotUpdateCaption ()