src/openvg/qvgimagepool.cpp
changeset 3 41300fa6a67c
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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     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 
       
   169 void 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 
       
   175 void 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 
       
   193 void 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 
       
   209 QVGPixmapData *QVGImagePool::pixmapLRU()
       
   210 {
       
   211     Q_D(QVGImagePool);
       
   212     return d->lruLast;
       
   213 }
       
   214 
       
   215 QT_END_NAMESPACE