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