| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 |  | 
|---|
| 45 | QT_BEGIN_NAMESPACE | 
|---|
| 46 |  | 
|---|
| 47 | static QVGImagePool *qt_vg_image_pool = 0; | 
|---|
| 48 |  | 
|---|
| 49 | class QVGImagePoolPrivate | 
|---|
| 50 | { | 
|---|
| 51 | public: | 
|---|
| 52 | QVGImagePoolPrivate() : lruFirst(0), lruLast(0) {} | 
|---|
| 53 |  | 
|---|
| 54 | QVGPixmapData *lruFirst; | 
|---|
| 55 | QVGPixmapData *lruLast; | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | QVGImagePool::QVGImagePool() | 
|---|
| 59 | : d_ptr(new QVGImagePoolPrivate()) | 
|---|
| 60 | { | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | QVGImagePool::~QVGImagePool() | 
|---|
| 64 | { | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | QVGImagePool *QVGImagePool::instance() | 
|---|
| 68 | { | 
|---|
| 69 | if (!qt_vg_image_pool) | 
|---|
| 70 | qt_vg_image_pool = new QVGImagePool(); | 
|---|
| 71 | return qt_vg_image_pool; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void 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 |  | 
|---|
| 81 | VGImage 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 |  | 
|---|
| 97 | VGImage 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 |  | 
|---|
| 116 | VGImage 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 |  | 
|---|
| 131 | void 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 |  | 
|---|
| 139 | void QVGImagePool::useImage(QVGPixmapData *data) | 
|---|
| 140 | { | 
|---|
| 141 | moveToHeadOfLRU(data); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | void QVGImagePool::detachImage(QVGPixmapData *data) | 
|---|
| 145 | { | 
|---|
| 146 | removeFromLRU(data); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | bool 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 | 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 |  | 
|---|
| 176 | void QVGImagePool::hibernate() | 
|---|
| 177 | { | 
|---|
| 178 | // Nothing to do here at the moment since the pool does not | 
|---|
| 179 | // retain VGImage's after they have been released. | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | void QVGImagePool::moveToHeadOfLRU(QVGPixmapData *data) | 
|---|
| 183 | { | 
|---|
| 184 | Q_D(QVGImagePool); | 
|---|
| 185 | if (data->inLRU) { | 
|---|
| 186 | if (!data->prevLRU) | 
|---|
| 187 | return;     // Already at the head of the list. | 
|---|
| 188 | removeFromLRU(data); | 
|---|
| 189 | } | 
|---|
| 190 | data->inLRU = true; | 
|---|
| 191 | data->nextLRU = d->lruFirst; | 
|---|
| 192 | data->prevLRU = 0; | 
|---|
| 193 | if (d->lruFirst) | 
|---|
| 194 | d->lruFirst->prevLRU = data; | 
|---|
| 195 | else | 
|---|
| 196 | d->lruLast = data; | 
|---|
| 197 | d->lruFirst = data; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | void QVGImagePool::removeFromLRU(QVGPixmapData *data) | 
|---|
| 201 | { | 
|---|
| 202 | Q_D(QVGImagePool); | 
|---|
| 203 | if (!data->inLRU) | 
|---|
| 204 | return; | 
|---|
| 205 | if (data->nextLRU) | 
|---|
| 206 | data->nextLRU->prevLRU = data->prevLRU; | 
|---|
| 207 | else | 
|---|
| 208 | d->lruLast = data->prevLRU; | 
|---|
| 209 | if (data->prevLRU) | 
|---|
| 210 | data->prevLRU->nextLRU = data->nextLRU; | 
|---|
| 211 | else | 
|---|
| 212 | d->lruFirst = data->nextLRU; | 
|---|
| 213 | data->inLRU = false; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | QVGPixmapData *QVGImagePool::pixmapLRU() | 
|---|
| 217 | { | 
|---|
| 218 | Q_D(QVGImagePool); | 
|---|
| 219 | return d->lruLast; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | QT_END_NAMESPACE | 
|---|