Qt 4.8
Classes | Public Slots | Public Functions | Protected Functions | Protected Variables | Private Types | List of all members
QGLGradientCache Class Reference
Inheritance diagram for QGLGradientCache:
QObject

Classes

struct  CacheInfo
 

Public Slots

void cleanupGLContextRefs (const QGLContext *context)
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Public Functions

GLuint getBuffer (const QGradient &gradient, qreal opacity, QGLContext *ctx)
 
int paletteSize () const
 
 QGLGradientCache ()
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Functions

GLuint addCacheElement (quint64 hash_val, const QGradient &gradient, qreal opacity)
 
void cleanCache ()
 
void generateGradientColorTable (const QGradient &g, uint *colorTable, int size, qreal opacity) const
 
int maxCacheSize () const
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Protected Variables

QGLContextbuffer_ctx
 
QGLGradientColorTableHash cache
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Private Types

typedef QMultiHash< quint64, CacheInfoQGLGradientColorTableHash
 

Additional Inherited Members

- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 994 of file qpaintengine_opengl.cpp.

Typedefs

◆ QGLGradientColorTableHash

Definition at line 1008 of file qpaintengine_opengl.cpp.

Constructors and Destructors

◆ QGLGradientCache()

QGLGradientCache::QGLGradientCache ( )
inline

Definition at line 1011 of file qpaintengine_opengl.cpp.

1011  : QObject(), buffer_ctx(0)
1012  {
1014  SIGNAL(aboutToDestroyContext(const QGLContext*)),
1016  }
#define SLOT(a)
Definition: qobjectdefs.h:226
static QGLSignalProxy * instance()
Definition: qgl.cpp:148
void cleanupGLContextRefs(const QGLContext *context)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

Functions

◆ addCacheElement()

GLuint QGLGradientCache::addCacheElement ( quint64  hash_val,
const QGradient gradient,
qreal  opacity 
)
inlineprotected

Definition at line 1054 of file qpaintengine_opengl.cpp.

1054  {
1055  if (cache.size() == maxCacheSize()) {
1056  int elem_to_remove = qrand() % maxCacheSize();
1057  quint64 key = cache.keys()[elem_to_remove];
1058 
1059  // need to call glDeleteTextures on each removed cache entry:
1061  do {
1062  glDeleteTextures(1, &it.value().texId);
1063  } while (++it != cache.constEnd() && it.key() == key);
1064  cache.remove(key); // may remove more than 1, but OK
1065  }
1066  CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode());
1067  uint buffer[1024];
1068  generateGradientColorTable(gradient, buffer, paletteSize(), opacity);
1069  glGenTextures(1, &cache_entry.texId);
1070 #ifndef QT_OPENGL_ES
1071  glBindTexture(GL_TEXTURE_1D, cache_entry.texId);
1072  glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, paletteSize(),
1073  0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
1074 #else
1075  // create 2D one-line texture instead. This requires an impl of manual GL_TEXGEN for all primitives
1076 #endif
1077  return cache.insert(hash_val, cache_entry).value().texId;
1078  }
#define GL_BGRA
Definition: glfunctions.h:63
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()
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
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
void generateGradientColorTable(const QGradient &g, uint *colorTable, int size, qreal opacity) 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
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
QGLGradientColorTableHash cache
friend class const_iterator
Definition: qhash.h:461

◆ cleanCache()

void QGLGradientCache::cleanCache ( )
inlineprotected

Definition at line 1080 of file qpaintengine_opengl.cpp.

1080  {
1083  for (; it != cache.constEnd(); ++it) {
1084  const CacheInfo &cache_info = it.value();
1085  glDeleteTextures(1, &cache_info.texId);
1086  }
1087  cache.clear();
1088  }
void clear()
Removes all items from the hash.
Definition: qhash.h:574
#define it(className, varName)
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
QGLGradientColorTableHash cache
friend class const_iterator
Definition: qhash.h:461

◆ cleanupGLContextRefs

void QGLGradientCache::cleanupGLContextRefs ( const QGLContext context)
inlineslot

Definition at line 1095 of file qpaintengine_opengl.cpp.

1095  {
1096  if (context == buffer_ctx) {
1097  cleanCache();
1098  buffer_ctx = 0;
1099  }
1100  }

◆ generateGradientColorTable()

void QGLGradientCache::generateGradientColorTable ( const QGradient g,
uint colorTable,
int  size,
qreal  opacity 
) const
inlineprotected

Definition at line 1115 of file qpaintengine_opengl.cpp.

1116 {
1117  int pos = 0;
1118  QGradientStops s = gradient.stops();
1119  QVector<uint> colors(s.size());
1120 
1121  for (int i = 0; i < s.size(); ++i)
1122  colors[i] = s[i].second.rgba();
1123 
1124  bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation);
1125 
1126  uint alpha = qRound(opacity * 256);
1127  uint current_color = ARGB_COMBINE_ALPHA(colors[0], alpha);
1128  qreal incr = 1.0 / qreal(size);
1129  qreal fpos = 1.5 * incr;
1130  colorTable[pos++] = endianColor(PREMUL(current_color));
1131 
1132  while (fpos <= s.first().first) {
1133  colorTable[pos] = colorTable[pos - 1];
1134  pos++;
1135  fpos += incr;
1136  }
1137 
1138  if (colorInterpolation)
1139  current_color = PREMUL(current_color);
1140 
1141  for (int i = 0; i < s.size() - 1; ++i) {
1142  qreal delta = 1/(s[i+1].first - s[i].first);
1143  uint next_color = ARGB_COMBINE_ALPHA(colors[i+1], alpha);
1144  if (colorInterpolation)
1145  next_color = PREMUL(next_color);
1146 
1147  while (fpos < s[i+1].first && pos < size) {
1148  int dist = int(256 * ((fpos - s[i].first) * delta));
1149  int idist = 256 - dist;
1150  if (colorInterpolation)
1151  colorTable[pos] = endianColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist));
1152  else
1153  colorTable[pos] = endianColor(PREMUL(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)));
1154  ++pos;
1155  fpos += incr;
1156  }
1157  current_color = next_color;
1158  }
1159 
1160  Q_ASSERT(s.size() > 0);
1161 
1162  uint last_color = endianColor(PREMUL(ARGB_COMBINE_ALPHA(colors[s.size() - 1], alpha)));
1163  for (;pos < size; ++pos)
1164  colorTable[pos] = last_color;
1165 
1166  // Make sure the last color stop is represented at the end of the table
1167  colorTable[size-1] = last_color;
1168 }
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
#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
static uint endianColor(uint c)
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 QGLGradientCache::getBuffer ( const QGradient gradient,
qreal  opacity,
QGLContext ctx 
)
inline

Definition at line 1018 of file qpaintengine_opengl.cpp.

1018  {
1020  cleanCache();
1021 
1022  buffer_ctx = ctx;
1023 
1024  quint64 hash_val = 0;
1025 
1026  QGradientStops stops = gradient.stops();
1027  for (int i = 0; i < stops.size() && i <= 2; i++)
1028  hash_val += stops[i].second.rgba();
1029 
1031 
1032  if (it == cache.constEnd())
1033  return addCacheElement(hash_val, gradient, opacity);
1034  else {
1035  do {
1036  const CacheInfo &cache_info = it.value();
1037  if (cache_info.stops == stops && cache_info.opacity == opacity && cache_info.interpolationMode == gradient.interpolationMode()) {
1038  return cache_info.texId;
1039  }
1040  ++it;
1041  } while (it != cache.constEnd() && it.key() == hash_val);
1042  // an exact match for these stops and opacity was not found, create new cache
1043  return addCacheElement(hash_val, gradient, opacity);
1044  }
1045  }
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)
GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity)
InterpolationMode interpolationMode() const
Returns the interpolation mode of this gradient.
Definition: qbrush.cpp:1607
static bool areSharing(const QGLContext *context1, const QGLContext *context2)
Returns true if context1 and context2 are sharing their GL resources such as textures, shader programs, etc; otherwise returns false.
Definition: qgl.cpp:3319
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
#define ctx
Definition: qgl.cpp:6094
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QGLGradientColorTableHash cache
friend class const_iterator
Definition: qhash.h:461

◆ maxCacheSize()

int QGLGradientCache::maxCacheSize ( ) const
inlineprotected

Definition at line 1050 of file qpaintengine_opengl.cpp.

1050 { return 60; }

◆ paletteSize()

int QGLGradientCache::paletteSize ( ) const
inline

Definition at line 1047 of file qpaintengine_opengl.cpp.

1047 { return 1024; }

Properties

◆ buffer_ctx

QGLContext* QGLGradientCache::buffer_ctx
protected

Definition at line 1092 of file qpaintengine_opengl.cpp.

◆ cache

QGLGradientColorTableHash QGLGradientCache::cache
protected

Definition at line 1090 of file qpaintengine_opengl.cpp.


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