| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/opengl/gl2paintengineex/qglgradientcache.cpp | 
| Switch to Source code | Preprocessed file | 
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||
| 2 | - | |||||||||||||||||||
| 3 | - | |||||||||||||||||||
| 4 | class QGL2GradientCacheWrapper | - | ||||||||||||||||||
| 5 | { | - | ||||||||||||||||||
| 6 | public: | - | ||||||||||||||||||
| 7 | QGL2GradientCache *cacheForContext(const QGLContext *context) { | - | ||||||||||||||||||
| 8 | QMutexLocker lock(&m_mutex); | - | ||||||||||||||||||
| 9 |         return never executed:   m_resource.value<QGL2GradientCache>(context->contextHandle());return m_resource.value<QGL2GradientCache>(context->contextHandle());never executed:  return m_resource.value<QGL2GradientCache>(context->contextHandle()); | 0 | ||||||||||||||||||
| 10 | } | - | ||||||||||||||||||
| 11 | - | |||||||||||||||||||
| 12 | private: | - | ||||||||||||||||||
| 13 | QOpenGLMultiGroupSharedResource m_resource; | - | ||||||||||||||||||
| 14 | QMutex m_mutex; | - | ||||||||||||||||||
| 15 | }; | - | ||||||||||||||||||
| 16 | - | |||||||||||||||||||
| 17 | namespace { namespace Q_QGS_qt_gradient_caches { typedef QGL2GradientCacheWrapper Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized
 never executed:   }guard.store(QtGlobalStatic::Destroyed);never executed:   }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ())) : value () { guard.store(QtGlobalStatic::Initialized); } } holder; returnend of blocknever executed:   &holder.value;return &holder.value;never executed:   } } } static QGlobalStatic<QGL2GradientCacheWrapper, Q_QGS_qt_gradient_caches::innerFunction, Q_QGS_qt_gradient_caches::guard> qt_gradient_caches;return &holder.value; | 0 | ||||||||||||||||||
| 18 | - | |||||||||||||||||||
| 19 | QGL2GradientCache::QGL2GradientCache(QOpenGLContext *ctx) | - | ||||||||||||||||||
| 20 | : QOpenGLSharedResource(ctx->shareGroup()) | - | ||||||||||||||||||
| 21 | { | - | ||||||||||||||||||
| 22 | } never executed:  end of block | 0 | ||||||||||||||||||
| 23 | - | |||||||||||||||||||
| 24 | QGL2GradientCache::~QGL2GradientCache() | - | ||||||||||||||||||
| 25 | { | - | ||||||||||||||||||
| 26 | cache.clear(); | - | ||||||||||||||||||
| 27 | } never executed:  end of block | 0 | ||||||||||||||||||
| 28 | - | |||||||||||||||||||
| 29 | QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) | - | ||||||||||||||||||
| 30 | { | - | ||||||||||||||||||
| 31 |     return never executed:   qt_gradient_caches()->cacheForContext(context);return qt_gradient_caches()->cacheForContext(context);never executed:  return qt_gradient_caches()->cacheForContext(context); | 0 | ||||||||||||||||||
| 32 | } | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | void QGL2GradientCache::invalidateResource() | - | ||||||||||||||||||
| 35 | { | - | ||||||||||||||||||
| 36 | QMutexLocker lock(&m_mutex); | - | ||||||||||||||||||
| 37 | cache.clear(); | - | ||||||||||||||||||
| 38 | } never executed:  end of block | 0 | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | void QGL2GradientCache::freeResource(QOpenGLContext *) | - | ||||||||||||||||||
| 41 | { | - | ||||||||||||||||||
| 42 | cleanCache(); | - | ||||||||||||||||||
| 43 | } never executed:  end of block | 0 | ||||||||||||||||||
| 44 | - | |||||||||||||||||||
| 45 | void QGL2GradientCache::cleanCache() | - | ||||||||||||||||||
| 46 | { | - | ||||||||||||||||||
| 47 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - | ||||||||||||||||||
| 48 | QMutexLocker lock(&m_mutex); | - | ||||||||||||||||||
| 49 | QGLGradientColorTableHash::const_iterator it = cache.constBegin(); | - | ||||||||||||||||||
| 50 |     for (; it != cache.constEnd()
  | 0 | ||||||||||||||||||
| 51 | const CacheInfo &cache_info = it.value(); | - | ||||||||||||||||||
| 52 | funcs->glDeleteTextures(1, &cache_info.texId); | - | ||||||||||||||||||
| 53 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 54 | cache.clear(); | - | ||||||||||||||||||
| 55 | } never executed:  end of block | 0 | ||||||||||||||||||
| 56 | - | |||||||||||||||||||
| 57 | GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) | - | ||||||||||||||||||
| 58 | { | - | ||||||||||||||||||
| 59 | QMutexLocker lock(&m_mutex); | - | ||||||||||||||||||
| 60 | quint64 hash_val = 0; | - | ||||||||||||||||||
| 61 | - | |||||||||||||||||||
| 62 | QGradientStops stops = gradient.stops(); | - | ||||||||||||||||||
| 63 |     for (int i = 0; i < stops.size()
 
  | 0 | ||||||||||||||||||
| 64 |         hash_val += stops[i].second.rgba(); never executed:  hash_val += stops[i].second.rgba(); | 0 | ||||||||||||||||||
| 65 | - | |||||||||||||||||||
| 66 | QGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val); | - | ||||||||||||||||||
| 67 | - | |||||||||||||||||||
| 68 |     if (it == cache.constEnd()
  | 0 | ||||||||||||||||||
| 69 |         return never executed:   addCacheElement(hash_val, gradient, opacity);return addCacheElement(hash_val, gradient, opacity);never executed:  return addCacheElement(hash_val, gradient, opacity); | 0 | ||||||||||||||||||
| 70 | else { | - | ||||||||||||||||||
| 71 | do { | - | ||||||||||||||||||
| 72 | const CacheInfo &cache_info = it.value(); | - | ||||||||||||||||||
| 73 |             if (cache_info.stops == stops
 
  | 0 | ||||||||||||||||||
| 74 |                 && cache_info.interpolationMode == gradient.interpolationMode()
  | 0 | ||||||||||||||||||
| 75 | { | - | ||||||||||||||||||
| 76 |                 return never executed:   cache_info.texId;return cache_info.texId;never executed:  return cache_info.texId; | 0 | ||||||||||||||||||
| 77 | } | - | ||||||||||||||||||
| 78 | ++it; | - | ||||||||||||||||||
| 79 |         } never executed:   while (it != cache.constEnd()end of block
 
  | 0 | ||||||||||||||||||
| 80 | - | |||||||||||||||||||
| 81 |         return never executed:   addCacheElement(hash_val, gradient, opacity);return addCacheElement(hash_val, gradient, opacity);never executed:  return addCacheElement(hash_val, gradient, opacity); | 0 | ||||||||||||||||||
| 82 | } | - | ||||||||||||||||||
| 83 | } | - | ||||||||||||||||||
| 84 | - | |||||||||||||||||||
| 85 | - | |||||||||||||||||||
| 86 | GLuint QGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity) | - | ||||||||||||||||||
| 87 | { | - | ||||||||||||||||||
| 88 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - | ||||||||||||||||||
| 89 |     if (cache.size() == maxCacheSize()
  | 0 | ||||||||||||||||||
| 90 | int elem_to_remove = qrand() % maxCacheSize(); | - | ||||||||||||||||||
| 91 | quint64 key = cache.keys()[elem_to_remove]; | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | - | |||||||||||||||||||
| 94 | QGLGradientColorTableHash::const_iterator it = cache.constFind(key); | - | ||||||||||||||||||
| 95 | do { | - | ||||||||||||||||||
| 96 | funcs->glDeleteTextures(1, &it.value().texId); | - | ||||||||||||||||||
| 97 |         } never executed:   while (++end of block
 
 
  | 0 | ||||||||||||||||||
| 98 | cache.remove(key); | - | ||||||||||||||||||
| 99 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 100 | - | |||||||||||||||||||
| 101 | CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode()); | - | ||||||||||||||||||
| 102 | uint buffer[1024]; | - | ||||||||||||||||||
| 103 | generateGradientColorTable(gradient, buffer, paletteSize(), opacity); | - | ||||||||||||||||||
| 104 | funcs->glGenTextures(1, &cache_entry.texId); | - | ||||||||||||||||||
| 105 | funcs->glBindTexture(0x0DE1, cache_entry.texId); | - | ||||||||||||||||||
| 106 | funcs->glTexImage2D(0x0DE1, 0, 0x1908, paletteSize(), 1, | - | ||||||||||||||||||
| 107 | 0, 0x1908, 0x1401, buffer); | - | ||||||||||||||||||
| 108 |     return never executed:   cache.insert(hash_val, cache_entry).value().texId;return cache.insert(hash_val, cache_entry).value().texId;never executed:  return cache.insert(hash_val, cache_entry).value().texId; | 0 | ||||||||||||||||||
| 109 | } | - | ||||||||||||||||||
| 110 | - | |||||||||||||||||||
| 111 | - | |||||||||||||||||||
| 112 | - | |||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | static inline uint qtToGlColor(uint c) | - | ||||||||||||||||||
| 115 | { | - | ||||||||||||||||||
| 116 | uint o; | - | ||||||||||||||||||
| 117 | - | |||||||||||||||||||
| 118 | o = (c & 0xff00ff00) | - | ||||||||||||||||||
| 119 | | ((c >> 16) & 0x000000ff) | - | ||||||||||||||||||
| 120 | | ((c << 16) & 0x00ff0000); | - | ||||||||||||||||||
| 121 | - | |||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | - | |||||||||||||||||||
| 124 | - | |||||||||||||||||||
| 125 |     return never executed:   o;return o;never executed:  return o; | 0 | ||||||||||||||||||
| 126 | } | - | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 | void QGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const | - | ||||||||||||||||||
| 130 | { | - | ||||||||||||||||||
| 131 | int pos = 0; | - | ||||||||||||||||||
| 132 | QGradientStops s = gradient.stops(); | - | ||||||||||||||||||
| 133 | QVector<uint> colors(s.size()); | - | ||||||||||||||||||
| 134 | - | |||||||||||||||||||
| 135 |     for (int i = 0; i < s.size()
  | 0 | ||||||||||||||||||
| 136 |         colors[i] = s[i].second.rgba(); never executed:  colors[i] = s[i].second.rgba(); | 0 | ||||||||||||||||||
| 137 | - | |||||||||||||||||||
| 138 | bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); | - | ||||||||||||||||||
| 139 | - | |||||||||||||||||||
| 140 | uint alpha = qRound(opacity * 256); | - | ||||||||||||||||||
| 141 | uint current_color = ((((colors[0] >> 24) * alpha) >> 8) << 24) | (colors[0] & 0x00ffffff); | - | ||||||||||||||||||
| 142 | qreal incr = 1.0 / qreal(size); | - | ||||||||||||||||||
| 143 | qreal fpos = 1.5 * incr; | - | ||||||||||||||||||
| 144 | colorTable[pos++] = qtToGlColor(qPremultiply(current_color)); | - | ||||||||||||||||||
| 145 | - | |||||||||||||||||||
| 146 |     while (fpos <= s.first().first
  | 0 | ||||||||||||||||||
| 147 | colorTable[pos] = colorTable[pos - 1]; | - | ||||||||||||||||||
| 148 | pos++; | - | ||||||||||||||||||
| 149 | fpos += incr; | - | ||||||||||||||||||
| 150 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 |     if (colorInterpolation
  | 0 | ||||||||||||||||||
| 153 |         current_color = qPremultiply(current_color); never executed:  current_color = qPremultiply(current_color); | 0 | ||||||||||||||||||
| 154 | - | |||||||||||||||||||
| 155 |     for (int i = 0; i < s.size() - 1
  | 0 | ||||||||||||||||||
| 156 | qreal delta = 1/(s[i+1].first - s[i].first); | - | ||||||||||||||||||
| 157 | uint next_color = ((((colors[i+1] >> 24) * alpha) >> 8) << 24) | (colors[i+1] & 0x00ffffff); | - | ||||||||||||||||||
| 158 |         if (colorInterpolation
  | 0 | ||||||||||||||||||
| 159 |             next_color = qPremultiply(next_color); never executed:  next_color = qPremultiply(next_color); | 0 | ||||||||||||||||||
| 160 | - | |||||||||||||||||||
| 161 |         while (fpos < s[i+1].first
 
  | 0 | ||||||||||||||||||
| 162 | int dist = int(256 * ((fpos - s[i].first) * delta)); | - | ||||||||||||||||||
| 163 | int idist = 256 - dist; | - | ||||||||||||||||||
| 164 |             if (colorInterpolation
  | 0 | ||||||||||||||||||
| 165 |                 colorTable[pos] = qtToGlColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); never executed:  colorTable[pos] = qtToGlColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); | 0 | ||||||||||||||||||
| 166 | else | - | ||||||||||||||||||
| 167 |                 colorTable[pos] = qtToGlColor(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); never executed:  colorTable[pos] = qtToGlColor(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); | 0 | ||||||||||||||||||
| 168 | ++pos; | - | ||||||||||||||||||
| 169 | fpos += incr; | - | ||||||||||||||||||
| 170 |         } never executed:  end of block | 0 | ||||||||||||||||||
| 171 | current_color = next_color; | - | ||||||||||||||||||
| 172 |     } never executed:  end of block | 0 | ||||||||||||||||||
| 173 | - | |||||||||||||||||||
| 174 | ((!(s.size() > 0)) ? qt_assert("s.size() > 0",__FILE__,211) : qt_noop()); | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | uint last_color = qtToGlColor(qPremultiply(((((colors[s.size() - 1] >> 24) * alpha) >> 8) << 24) | (colors[s.size() - 1] & 0x00ffffff))); | - | ||||||||||||||||||
| 177 |     for (;pos < size
  | 0 | ||||||||||||||||||
| 178 |         colorTable[pos] = last_color; never executed:  colorTable[pos] = last_color; | 0 | ||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | - | |||||||||||||||||||
| 181 | colorTable[size-1] = last_color; | - | ||||||||||||||||||
| 182 | } never executed:  end of block | 0 | ||||||||||||||||||
| 183 | - | |||||||||||||||||||
| 184 | - | |||||||||||||||||||
| Switch to Source code | Preprocessed file |