Qt 4.8
qvgimagepool.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtOpenVG module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qvgimagepool_p.h"
43 #include "qpixmapdata_vg_p.h"
44 
46 
48 
50 {
51 public:
53 
56 };
57 
59  : d_ptr(new QVGImagePoolPrivate())
60 {
61 }
62 
64 {
65 }
66 
68 {
69  if (!qt_vg_image_pool)
70  qt_vg_image_pool = new QVGImagePool();
71  return qt_vg_image_pool;
72 }
73 
75 {
76  if (qt_vg_image_pool != pool)
77  delete qt_vg_image_pool;
78  qt_vg_image_pool = pool;
79 }
80 
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 
98  VGint width, VGint height,
99  VGbitfield allowedQuality,
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 
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 
132 {
133  // Very simple strategy at the moment: just destroy the image.
134  if (data)
135  removeFromLRU(data);
136  vgDestroyImage(image);
137 }
138 
140 {
141  moveToHeadOfLRU(data);
142 }
143 
145 {
146  removeFromLRU(data);
147 }
148 
150  VGint width, VGint height,
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  bool succeeded = false;
158  bool wasInLRU = false;
159  if (data) {
160  wasInLRU = data->inLRU;
161  moveToHeadOfLRU(data);
162  }
163 
164  QVGPixmapData *lrudata = pixmapLRU();
165  if (lrudata && lrudata != data) {
166  lrudata->reclaimImages();
167  succeeded = true;
168  }
169 
170  if (data && !wasInLRU)
171  removeFromLRU(data);
172 
173  return succeeded;
174 }
175 
177 {
178  Q_D(QVGImagePool);
179  QVGPixmapData *pd = d->lruLast;
180  while (pd) {
181  QVGPixmapData *prevLRU = pd->prevLRU;
182  pd->inImagePool = false;
183  pd->inLRU = false;
184  pd->nextLRU = 0;
185  pd->prevLRU = 0;
186  pd->hibernate();
187  pd = prevLRU;
188  }
189  d->lruFirst = 0;
190  d->lruLast = 0;
191 }
192 
194 {
195  Q_D(QVGImagePool);
196  if (data->inLRU) {
197  if (!data->prevLRU)
198  return; // Already at the head of the list.
199  removeFromLRU(data);
200  }
201  data->inLRU = true;
202  data->nextLRU = d->lruFirst;
203  data->prevLRU = 0;
204  if (d->lruFirst)
205  d->lruFirst->prevLRU = data;
206  else
207  d->lruLast = data;
208  d->lruFirst = data;
209 }
210 
212 {
213  Q_D(QVGImagePool);
214  if (!data->inLRU)
215  return;
216  if (data->nextLRU)
217  data->nextLRU->prevLRU = data->prevLRU;
218  else
219  d->lruLast = data->prevLRU;
220  if (data->prevLRU)
221  data->prevLRU->nextLRU = data->nextLRU;
222  else
223  d->lruFirst = data->nextLRU;
224  data->inLRU = false;
225 }
226 
228 {
229  Q_D(QVGImagePool);
230  return d->lruLast;
231 }
232 
virtual void useImage(QVGPixmapData *data)
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual VGImage createPermanentImage(VGImageFormat format, VGint width, VGint height, VGbitfield allowedQuality)
QVGPixmapData * lruLast
virtual ~QVGImagePool()
QVGPixmapData * lruFirst
#define Q_D(Class)
Definition: qglobal.h:2482
virtual VGImage createImageForPixmap(VGImageFormat format, VGint width, VGint height, VGbitfield allowedQuality, QVGPixmapData *data)
QVGPixmapData * prevLRU
void moveToHeadOfLRU(QVGPixmapData *data)
virtual VGImage createTemporaryImage(VGImageFormat format, VGint width, VGint height, VGbitfield allowedQuality, QVGPixmapData *keepData=0)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
static void setImagePool(QVGImagePool *pool)
static QVGImagePool * qt_vg_image_pool
void removeFromLRU(QVGPixmapData *data)
static QVGImagePool * instance()
QVGPixmapData * nextLRU
QVGPixmapData * pixmapLRU()
virtual bool reclaimSpace(VGImageFormat format, VGint width, VGint height, QVGPixmapData *data)
virtual void hibernate()
virtual void detachImage(QVGPixmapData *data)
virtual void hibernate()
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
virtual void releaseImage(QVGPixmapData *data, VGImage image)
virtual void reclaimImages()