source: trunk/src/opengl/gl2paintengineex/qglgradientcache.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 7.3 KB
RevLine 
[2]1/****************************************************************************
2**
[651]3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
[561]24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
[561]47QT_BEGIN_NAMESPACE
48
49static void QGL2GradientCache_free(void *ptr)
50{
51 delete reinterpret_cast<QGL2GradientCache *>(ptr);
52}
53
54Q_GLOBAL_STATIC_WITH_ARGS(QGLContextResource, qt_gradient_caches, (QGL2GradientCache_free))
55
56QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context)
57{
58 QGL2GradientCache *p = reinterpret_cast<QGL2GradientCache *>(qt_gradient_caches()->value(context));
59 if (!p) {
60 QGLShareContextScope scope(context);
61 p = new QGL2GradientCache;
62 qt_gradient_caches()->insert(context, p);
63 }
64 return p;
65}
66
67void QGL2GradientCache::cleanCache() {
[2]68 QGLGradientColorTableHash::const_iterator it = cache.constBegin();
69 for (; it != cache.constEnd(); ++it) {
70 const CacheInfo &cache_info = it.value();
71 glDeleteTextures(1, &cache_info.texId);
72 }
73 cache.clear();
74}
75
[561]76GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity)
[2]77{
78 quint64 hash_val = 0;
79
80 QGradientStops stops = gradient.stops();
81 for (int i = 0; i < stops.size() && i <= 2; i++)
82 hash_val += stops[i].second.rgba();
83
84 QGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val);
85
86 if (it == cache.constEnd())
87 return addCacheElement(hash_val, gradient, opacity);
88 else {
89 do {
90 const CacheInfo &cache_info = it.value();
91 if (cache_info.stops == stops && cache_info.opacity == opacity && cache_info.interpolationMode == gradient.interpolationMode()) {
92 return cache_info.texId;
93 }
94 ++it;
95 } while (it != cache.constEnd() && it.key() == hash_val);
96 // an exact match for these stops and opacity was not found, create new cache
97 return addCacheElement(hash_val, gradient, opacity);
98 }