source: trunk/src/gui/image/qpixmapcache.cpp@ 147

Last change on this file since 147 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpixmapcache.h"
43#include "qcache.h"
44#include "qobject.h"
45#include "qdebug.h"
46
47#include "qpaintengine.h"
48#include <private/qimage_p.h>
49#include <private/qpixmap_raster_p.h>
50
51QT_BEGIN_NAMESPACE
52
53/*!
54 \class QPixmapCache
55
56 \brief The QPixmapCache class provides an application-wide cache for pixmaps.
57
58 \ingroup environment
59 \ingroup multimedia
60
61 This class is a tool for optimized drawing with QPixmap. You can
62 use it to store temporary pixmaps that are expensive to generate
63 without using more storage space than cacheLimit(). Use insert()
64 to insert pixmaps, find() to find them, and clear() to empty the
65 cache.
66
67 QPixmapCache contains no member data, only static functions to
68 access the global pixmap cache. It creates an internal QCache
69 object for caching the pixmaps.
70
71 The cache associates a pixmap with a string (key). If two pixmaps
72 are inserted into the cache using equal keys, then the last pixmap
73 will hide the first pixmap. The QHash and QCache classes do
74 exactly the same.
75
76 The cache becomes full when the total size of all pixmaps in the
77 cache exceeds cacheLimit(). The initial cache limit is 1024 KB (1
78 MB); it is changed with setCacheLimit(). A pixmap takes roughly
79 (\e{width} * \e{height} * \e{depth})/8 bytes of memory.
80
81 The \e{Qt Quarterly} article
82 \l{http://doc.trolltech.com/qq/qq12-qpixmapcache.html}{Optimizing
83 with QPixmapCache} explains how to use QPixmapCache to speed up
84 applications by caching the results of painting.
85
86 \sa QCache, QPixmap
87*/
88
89#if defined(Q_WS_QWS) || defined(Q_OS_WINCE)
90static int cache_limit = 2048; // 2048 KB cache limit for embedded
91#else
92static int cache_limit = 10240; // 10 MB cache limit for desktop
93#endif
94
95// XXX: hw: is this a general concept we need to abstract?
96class QDetachedPixmap : public QPixmap
97{
98public:
99 QDetachedPixmap(const QPixmap &pix) : QPixmap(pix)
100 {
101 if (data && data->classId() == QPixmapData::RasterClass) {
102 QRasterPixmapData *d = static_cast<QRasterPixmapData*>(data);
103 if (!d->image.isNull() && d->image.d->paintEngine
104 && !d->image.d->paintEngine->isActive())
105 {
106 delete d->image.d->paintEngine;
107 d->image.d->paintEngine = 0;
108 }
109 }
110 }
111};
112
113class QPMCache : public QObject, public QCache<qint64, QDetachedPixmap>
114{
115 Q_OBJECT
116public:
117 QPMCache()
118 : QObject(0),
119 QCache<qint64, QDetachedPixmap>(cache_limit * 1024),
120 theid(0), ps(0), t(false) { }
121 ~QPMCache() { }
122
123 void timerEvent(QTimerEvent *);
124 bool insert(const QString& key, const QPixmap &pixmap, int cost);
125 bool remove(const QString &key);
126
127 QPixmap *object(const QString &key) const;