Qt 4.8
qglgradientcache.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
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 
48 
50 {
51 public:
54  return m_resource.value(context);
55  }
56 
57 private:
60 };
61 
62 Q_GLOBAL_STATIC(QGL2GradientCacheWrapper, qt_gradient_caches)
63 
65 {
66  return qt_gradient_caches()->cacheForContext(context);
67 }
68 
70 {
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 
80 GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity)
81 {
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 
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.
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
151 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 
GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity)
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
T * value(const QGLContext *context)
Definition: qgl_p.h:775
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
#define it(className, varName)
T & first()
Returns a reference to the first item in the vector.
Definition: qvector.h:260
T1 first
Definition: qpair.h:65
QGL2GradientCache * cacheForContext(const QGLContext *context)
Q_CORE_EXPORT int qrand()
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define GL_TEXTURE_2D
InterpolationMode interpolationMode() const
Returns the interpolation mode of this gradient.
Definition: qbrush.cpp:1607
static uint qtToGlColor(uint c)
QGradientStops stops() const
Returns the stop points for this gradient.
Definition: qbrush.cpp:1520
#define ARGB_COMBINE_ALPHA(argb, alpha)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
unsigned __int64 quint64
Definition: qglobal.h:943
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
QGradient::InterpolationMode interpolationMode
Q_STATIC_INLINE_FUNCTION uint PREMUL(uint x)
Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b)
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
unsigned int uint
Definition: qglobal.h:996
GLuint getBuffer(const QGradient &gradient, qreal opacity)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QGL2GradientCache * cacheForContext(const QGLContext *context)
The QGradient class is used in combination with QBrush to specify gradient fills. ...
Definition: qbrush.h:201
#define GL_UNSIGNED_BYTE
void generateGradientColorTable(const QGradient &gradient, uint *colorTable, int size, qreal opacity) const
QGLContextGroupResource< QGL2GradientCache > m_resource
int key
static QReadWriteLock lock
Definition: proxyconf.cpp:399
#define GL_RGBA
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203