source: trunk/src/openvg/qvgimagepool.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.

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtOpenVG 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**
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.
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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvgimagepool_p.h"
43#include "qpixmapdata_vg_p.h"
44
45QT_BEGIN_NAMESPACE
46
47static QVGImagePool *qt_vg_image_pool = 0;
48
49class QVGImagePoolPrivate
50{
51public:
52 QVGImagePoolPrivate() : lruFirst(0), lruLast(0) {}
53
54 QVGPixmapData *lruFirst;
55 QVGPixmapData *lruLast;
56};
57
58QVGImagePool::QVGImagePool()
59 : d_ptr(new QVGImagePoolPrivate())
60{
61}
62
63QVGImagePool::~QVGImagePool()
64{
65}
66
67QVGImagePool *QVGImagePool::instance()
68{
69 if (!qt_vg_image_pool)
70 qt_vg_image_pool = new QVGImagePool();
71 return qt_vg_image_pool;
72}
73
74void QVGImagePool::setImagePool(QVGImagePool *pool)
75{
76 if (qt_vg_image_pool != pool)
77 delete qt_vg_image_pool;
78 qt_vg_image_pool = pool;
79}
80
81VGImage QVGImagePool::createTemporaryImage(VGImageFormat format,
82 VGint width, VGint height,
83 VGbitfield allowedQuality,
84 QVGPixmapData *keepData)
85{
86 VGImage image;
87 do {
88 image = vgCreateImage(format, width, height, allowedQuality);
89 if (image != VG_INVALID_HANDLE)
90 return image;
91 } while (reclaimSpace(format, width, height, keepData));
92 qWarning("QVGImagePool: cannot reclaim sufficient space for a %dx%d temporary image",
93 width, height);
94 return VG_INVALID_HANDLE;
95}
96
97VGImage QVGImagePool::createImageForPixmap(VGImageFormat format,
98 VGint width, VGint height,
99 VGbitfield allowedQuality,
100 QVGPixmapData *data)
101{
102 VGImage image;
103 do {
104 image = vgCreateImage(format, width, height, allowedQuality);
105 if (image != VG_INVALID_HANDLE) {
106 if (data)
107 moveToHeadOfLRU(data);
108 return image;
109 }
110 } while (reclaimSpace(format, width, height, data));
111 qWarning("QVGImagePool: cannot reclaim sufficient space for a %dx%d pixmap",
112 width, height);
113 return VG_INVALID_HANDLE;
114}
115
116VGImage QVGImagePool::createPermanentImage(VGImageFormat format,
117 VGint width, VGint height,
118 VGbitfield allowedQuality)
119{
120 VGImage image;
121 do {
122 image = vgCreateImage(format, width, height, allowedQuality);
123 if (image != VG_INVALID_HANDLE)
124 return image;
125 } while (reclaimSpace(format, width, height, 0));
126 qWarning("QVGImagePool: cannot reclaim sufficient space for a %dx%d image",
127 width, height);
128 return VG_INVALID_HANDLE;
129}
130
131void QVGImagePool::releaseImage(QVGPixmapData *data, VGImage image)
132{
133 // Very simple strategy at the moment: just destroy the image.
134 if (data)
135 removeFromLRU(data);
136 vgDestroyImage(image);
137}
138
139void QVGImagePool::useImage(QVGPixmapData *data)
140{
141 moveToHeadOfLRU(data);
142}
143
144void QVGImagePool::detachImage(QVGPixmapData *data)
145{
146 removeFromLRU(data);
147}
148
149bool QVGImagePool::reclaimSpace(VGImageFormat format,
150 VGint width, VGint height,
151 QVGPixmapData *data)
152{
153 Q_UNUSED(format); // For future use in picking the best image to eject.
154 Q_UNUSED(width);
155 Q_UNUSED(height);
156
157 if (data)
158 moveToHeadOfLRU(data);
159
160 QVGPixmapData *lrudata = pixmapLRU();
161 if (lrudata && lrudata != data) {
162 lrudata->reclaimImages();
163 return true;
164 }
165
166 return false;
167}
168
169void QVGImagePool::hibernate()
170{
171 // Nothing to do here at the moment since the pool does not
172 // retain VGImage's after they have been released.
173}
174
175void QVGImagePool::moveToHeadOfLRU(QVGPixmapData *data)
176{
177 Q_D(QVGImagePool);
178 if (data->inLRU) {
179 if (!data->prevLRU)
180 return; // Already at the head of the list.
181 removeFromLRU(data);
182 }
183 data->inLRU = true;
184 data->nextLRU = d->lruFirst;
185 data->prevLRU = 0;
186 if (d->lruFirst)
187 d->lruFirst->prevLRU = data;
188 else
189 d->lruLast = data;
190 d->lruFirst = data;
191}
192
193void QVGImagePool::removeFromLRU(QVGPixmapData *data)
194{
195 Q_D(QVGImagePool);
196 if (!data->inLRU)
197 return;
198 if (data->nextLRU)
199 data->nextLRU->prevLRU = data->prevLRU;
200 else
201 d->lruLast = data->prevLRU;
202 if (data->prevLRU)
203 data->prevLRU->nextLRU = data->nextLRU;
204 else
205 d->lruFirst = data->nextLRU;
206 data->inLRU = false;
207}
208
209QVGPixmapData *QVGImagePool::pixmapLRU()
210{
211 Q_D(QVGImagePool);
212 return d->lruLast;
213}
214
215QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.