Qt 4.8
Classes | Public Functions | Static Public Functions | Private Types | Private Functions | Properties | List of all members
QGL2GradientCache Class Reference

#include <qglgradientcache_p.h>

Classes

struct  CacheInfo
 

Public Functions

GLuint getBuffer (const QGradient &gradient, qreal opacity)
 
int paletteSize () const
 
 QGL2GradientCache (const QGLContext *)
 
 ~QGL2GradientCache ()
 

Static Public Functions

static QGL2GradientCachecacheForContext (const QGLContext *context)
 

Private Types

typedef QMultiHash< quint64, CacheInfoQGLGradientColorTableHash
 

Private Functions

GLuint addCacheElement (quint64 hash_val, const QGradient &gradient, qreal opacity)
 
void cleanCache ()
 
void generateGradientColorTable (const QGradient &gradient, uint *colorTable, int size, qreal opacity) const
 
int maxCacheSize () const
 

Properties

QGLGradientColorTableHash cache
 
QMutex m_mutex
 

Detailed Description

Definition at line 61 of file qglgradientcache_p.h.

Typedefs

◆ QGLGradientColorTableHash

Definition at line 74 of file qglgradientcache_p.h.

Constructors and Destructors

◆ QGL2GradientCache()

QGL2GradientCache::QGL2GradientCache ( const QGLContext )
inline

Definition at line 79 of file qglgradientcache_p.h.

79 {}

◆ ~QGL2GradientCache()

QGL2GradientCache::~QGL2GradientCache ( )
inline

Definition at line 80 of file qglgradientcache_p.h.

80 { cleanCache(); }

Functions

◆ addCacheElement()

GLuint QGL2GradientCache::addCacheElement ( quint64  hash_val,
const QGradient gradient,
qreal  opacity 
)
private

Definition at line 109 of file qglgradientcache.cpp.

Referenced by maxCacheSize().

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:
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 }
QHash< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with the key and the value in the hash.
Definition: qhash.h:992
#define it(className, varName)
Q_CORE_EXPORT int qrand()
#define GL_TEXTURE_2D
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
InterpolationMode interpolationMode() const
Returns the interpolation mode of this gradient.
Definition: qbrush.cpp:1607
QGLGradientColorTableHash cache
QGradientStops stops() const
Returns the stop points for this gradient.
Definition: qbrush.cpp:1520
unsigned __int64 quint64
Definition: qglobal.h:943
unsigned int uint
Definition: qglobal.h:996
int maxCacheSize() const
QHash< Key, T >::iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:934
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
int remove(const Key &key, const T &value)
Removes all the items that have the key and the value value from the hash.
Definition: qhash.h:1006
int size() const
Returns the number of items in the hash.
Definition: qhash.h:295
#define GL_UNSIGNED_BYTE
void generateGradientColorTable(const QGradient &gradient, uint *colorTable, int size, qreal opacity) const
int key
#define GL_RGBA
QList< Key > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition: qhash.h:648
friend class const_iterator
Definition: qhash.h:461

◆ cacheForContext()

QGL2GradientCache * QGL2GradientCache::cacheForContext ( const QGLContext context)
static

Definition at line 64 of file qglgradientcache.cpp.

Referenced by cacheForContext(), and QGL2PaintEngineExPrivate::updateBrushTexture().

65 {
66  return qt_gradient_caches()->cacheForContext(context);
67 }

◆ cleanCache()

void QGL2GradientCache::cleanCache ( )
private

Definition at line 69 of file qglgradientcache.cpp.

Referenced by maxCacheSize(), and ~QGL2GradientCache().

70 {
73  for (; it != cache.constEnd(); ++it) {
74  const CacheInfo &cache_info = it.value();
75  glDeleteTextures(1, &cache_info.texId);
76  }
77  cache.clear();
78 }
void clear()
Removes all items from the hash.
Definition: qhash.h:574
#define it(className, varName)
QGLGradientColorTableHash cache
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399
friend class const_iterator
Definition: qhash.h:461

◆ generateGradientColorTable()

void QGL2GradientCache::generateGradientColorTable ( const QGradient gradient,
uint colorTable,
int  size,
qreal  opacity 
) const
inlineprivate

Definition at line 151 of file qglgradientcache.cpp.

Referenced by maxCacheSize().

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 }
double qreal
Definition: qglobal.h:1193
T & first()
Returns a reference to the first item in the vector.
Definition: qvector.h:260
T1 first
Definition: qpair.h:65
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
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)
Q_STATIC_INLINE_FUNCTION uint PREMUL(uint x)
Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b)
unsigned int uint
Definition: qglobal.h:996
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

◆ getBuffer()

GLuint QGL2GradientCache::getBuffer ( const QGradient gradient,
qreal  opacity 
)

Definition at line 80 of file qglgradientcache.cpp.

Referenced by QGL2PaintEngineExPrivate::updateBrushTexture(), and ~QGL2GradientCache().

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 
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 }
GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity)
QHash< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with the key and the value in the hash.
Definition: qhash.h:992
#define it(className, varName)
InterpolationMode interpolationMode() const
Returns the interpolation mode of this gradient.
Definition: qbrush.cpp:1607
QGLGradientColorTableHash cache
QGradientStops stops() const
Returns the stop points for this gradient.
Definition: qbrush.cpp:1520
unsigned __int64 quint64
Definition: qglobal.h:943
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
friend class const_iterator
Definition: qhash.h:461

◆ maxCacheSize()

int QGL2GradientCache::maxCacheSize ( ) const
inlineprivate

Definition at line 86 of file qglgradientcache_p.h.

86 { return 60; }

◆ paletteSize()

int QGL2GradientCache::paletteSize ( ) const
inline

Definition at line 83 of file qglgradientcache_p.h.

83 { return 1024; }

Properties

◆ cache

QGLGradientColorTableHash QGL2GradientCache::cache
private

Definition at line 93 of file qglgradientcache_p.h.

◆ m_mutex

QMutex QGL2GradientCache::m_mutex
private

Definition at line 94 of file qglgradientcache_p.h.


The documentation for this class was generated from the following files: