|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtOpenGL module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <private/qdrawhelper_p.h> |
|
43 #include <private/qgl_p.h> |
|
44 |
|
45 #include "qglgradientcache_p.h" |
|
46 |
|
47 QT_BEGIN_NAMESPACE |
|
48 |
|
49 static void QGL2GradientCache_free(void *ptr) |
|
50 { |
|
51 delete reinterpret_cast<QGL2GradientCache *>(ptr); |
|
52 } |
|
53 |
|
54 Q_GLOBAL_STATIC_WITH_ARGS(QGLContextResource, qt_gradient_caches, (QGL2GradientCache_free)) |
|
55 |
|
56 QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) |
|
57 { |
|
58 QGL2GradientCache *p = reinterpret_cast<QGL2GradientCache *>(qt_gradient_caches()->value(context)); |
|
59 if (!p) { |
|
60 QGLShareContextScope scope(context); |
|
61 p = new QGL2GradientCache; |
|
62 qt_gradient_caches()->insert(context, p); |
|
63 } |
|
64 return p; |
|
65 } |
|
66 |
|
67 void QGL2GradientCache::cleanCache() { |
|
68 QGLGradientColorTableHash::const_iterator it = cache.constBegin(); |
|
69 for (; it != cache.constEnd(); ++it) { |
|
70 const CacheInfo &cache_info = it.value(); |
|
71 glDeleteTextures(1, &cache_info.texId); |
|
72 } |
|
73 cache.clear(); |
|
74 } |
|
75 |
|
76 GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) |
|
77 { |
|
78 quint64 hash_val = 0; |
|
79 |
|
80 QGradientStops stops = gradient.stops(); |
|
81 for (int i = 0; i < stops.size() && i <= 2; i++) |
|
82 hash_val += stops[i].second.rgba(); |
|
83 |
|
84 QGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val); |
|
85 |
|
86 if (it == cache.constEnd()) |
|
87 return addCacheElement(hash_val, gradient, opacity); |
|
88 else { |
|
89 do { |
|
90 const CacheInfo &cache_info = it.value(); |
|
91 if (cache_info.stops == stops && cache_info.opacity == opacity && cache_info.interpolationMode == gradient.interpolationMode()) { |
|
92 return cache_info.texId; |
|
93 } |
|
94 ++it; |
|
95 } while (it != cache.constEnd() && it.key() == hash_val); |
|
96 // an exact match for these stops and opacity was not found, create new cache |
|
97 return addCacheElement(hash_val, gradient, opacity); |
|
98 } |
|
99 } |
|
100 |
|
101 |
|
102 GLuint QGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity) |
|
103 { |
|
104 if (cache.size() == maxCacheSize()) { |
|
105 int elem_to_remove = qrand() % maxCacheSize(); |
|
106 quint64 key = cache.keys()[elem_to_remove]; |
|
107 |
|
108 // need to call glDeleteTextures on each removed cache entry: |
|
109 QGLGradientColorTableHash::const_iterator it = cache.constFind(key); |
|
110 do { |
|
111 glDeleteTextures(1, &it.value().texId); |
|
112 } while (++it != cache.constEnd() && it.key() == key); |
|
113 cache.remove(key); // may remove more than 1, but OK |
|
114 } |
|
115 |
|
116 CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode()); |
|
117 uint buffer[1024]; |
|
118 generateGradientColorTable(gradient, buffer, paletteSize(), opacity); |
|
119 glGenTextures(1, &cache_entry.texId); |
|
120 glBindTexture(GL_TEXTURE_2D, cache_entry.texId); |
|
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, paletteSize(), 1, |
|
122 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
|
123 return cache.insert(hash_val, cache_entry).value().texId; |
|
124 } |
|
125 |
|
126 |
|
127 // GL's expects pixels in RGBA (when using GL_RGBA), bin-endian (ABGR on x86). |
|
128 // Qt always stores in ARGB reguardless of the byte-order the mancine uses. |
|
129 static inline uint qtToGlColor(uint c) |
|
130 { |
|
131 uint o; |
|
132 #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
|
133 o = (c & 0xff00ff00) // alpha & green already in the right place |
|
134 | ((c >> 16) & 0x000000ff) // red |
|
135 | ((c << 16) & 0x00ff0000); // blue |
|
136 #else //Q_BIG_ENDIAN |
|
137 o = (c << 8) |
|
138 | ((c >> 24) & 0x000000ff); |
|
139 #endif // Q_BYTE_ORDER |
|
140 return o; |
|
141 } |
|
142 |
|
143 //TODO: Let GL generate the texture using an FBO |
|
144 void QGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const |
|
145 { |
|
146 int pos = 0; |
|
147 QGradientStops s = gradient.stops(); |
|
148 QVector<uint> colors(s.size()); |
|
149 |
|
150 for (int i = 0; i < s.size(); ++i) |
|
151 colors[i] = s[i].second.rgba(); // Qt LIES! It returns ARGB (on little-endian AND on big-endian) |
|
152 |
|
153 bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); |
|
154 |
|
155 uint alpha = qRound(opacity * 256); |
|
156 uint current_color = ARGB_COMBINE_ALPHA(colors[0], alpha); |
|
157 qreal incr = 1.0 / qreal(size); |
|
158 qreal fpos = 1.5 * incr; |
|
159 colorTable[pos++] = qtToGlColor(PREMUL(current_color)); |
|
160 |
|
161 while (fpos <= s.first().first) { |
|
162 colorTable[pos] = colorTable[pos - 1]; |
|
163 pos++; |
|
164 fpos += incr; |
|
165 } |
|
166 |
|
167 if (colorInterpolation) |
|
168 current_color = PREMUL(current_color); |
|
169 |
|
170 for (int i = 0; i < s.size() - 1; ++i) { |
|
171 qreal delta = 1/(s[i+1].first - s[i].first); |
|
172 uint next_color = ARGB_COMBINE_ALPHA(colors[i+1], alpha); |
|
173 if (colorInterpolation) |
|
174 next_color = PREMUL(next_color); |
|
175 |
|
176 while (fpos < s[i+1].first && pos < size) { |
|
177 int dist = int(256 * ((fpos - s[i].first) * delta)); |
|
178 int idist = 256 - dist; |
|
179 if (colorInterpolation) |
|
180 colorTable[pos] = qtToGlColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); |
|
181 else |
|
182 colorTable[pos] = qtToGlColor(PREMUL(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); |
|
183 ++pos; |
|
184 fpos += incr; |
|
185 } |
|
186 current_color = next_color; |
|
187 } |
|
188 |
|
189 Q_ASSERT(s.size() > 0); |
|
190 |
|
191 uint last_color = qtToGlColor(PREMUL(ARGB_COMBINE_ALPHA(colors[s.size() - 1], alpha))); |
|
192 for (;pos < size; ++pos) |
|
193 colorTable[pos] = last_color; |
|
194 |
|
195 // Make sure the last color stop is represented at the end of the table |
|
196 colorTable[size-1] = last_color; |
|
197 } |
|
198 |
|
199 QT_END_NAMESPACE |