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