-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcommonpixmaps.cpp
346 lines (289 loc) · 11.4 KB
/
mcommonpixmaps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "mcommonpixmaps.h"
#include "mthemedaemon.h"
#include "mpixmaphandle.h"
#include <QFile>
#include <QDir>
using namespace M::MThemeDaemonProtocol;
namespace {
// before loading one item from the queue the cpu usage must
// be below the given percentage
#ifdef UNIT_TEST
const int maximumCpuUsageBeforeLoadingOneItem = 100;
#else
const int maximumCpuUsageBeforeLoadingOneItem = 30;
#endif
// when the cpu has been busy wait that many milliseconds before
// doing the next check
const int delayBeforeLoadingNextItemWhenBusy = 1000;
// when the cpu was idle (we loaded an item) wait that many milliseconds
// before doing the next check
const int delayBeforeLoadingNextItemWhenIdle = 100;
// this one is a tradeof between the time needed to load all pixmaps and
// increased cpu usage
const int numberOfPixmapsToLoadAtOnce = 5;
// before sending the most used pixmaps the device should be pretty
// idle as this wakes up all processes connected to the themedaemon
#ifdef UNIT_TEST
const int maximumCpuUsageBeforeSendingMostUsed = 100;
#else
const int maximumCpuUsageBeforeSendingMostUsed = 10;
#endif
// check the cpu usage for that many seconds before deciding if
// a most used package can be sent
#ifdef UNIT_TEST
const int delayBeforeSendingMostUsed = 100;
#else
const int delayBeforeSendingMostUsed = 5000;
#endif
}
#define VERSION(major, minor) ((major << 16) | minor)
const unsigned int PRELOAD_FILE_VERSION = VERSION(0, 1);
MCommonPixmaps::MCommonPixmaps(MThemeDaemon *daemon, bool loadMostUsed) :
minRequestsForCache(0),
daemon(daemon),
timerSinceLastSave(QElapsedTimer())
{
if (loadMostUsed) {
connect(&cpuMonitor, SIGNAL(newCpuFrameAvailable()), SLOT(loadOne()));
connect(this, SIGNAL(mostUsedPixmapsChanged(M::MThemeDaemonProtocol::MostUsedPixmaps)), SLOT(considerSaving()));
}
timerSinceLastSave.invalidate();
}
MCommonPixmaps::~MCommonPixmaps()
{
// please call clear before destroying this object
Q_ASSERT(mostUsedPixmaps.count() == 0);
}
void MCommonPixmaps::clear()
{
// release all most used pixmaps
foreach(const PixmapIdentifier & id, mostUsedPixmaps) {
if (toLoadList.contains(id))
continue;
ImageResource *resource = daemon->findImageResource(id.imageId);
resource->releasePixmap(id.size);
}
cpuMonitor.stop();
mostUsedPixmaps.clear();
toLoadList.clear();
requestCounts.clear();
minRequestsForCache = 0;
}
void MCommonPixmaps::load()
{
clear();
if (!QFile::exists(cacheFilename())) {
return;
}
QFile file(cacheFilename());
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << Q_FUNC_INFO << "Could not load most used pixmaps from" << cacheFilename();
return;
}
QDataStream stream(&file);
unsigned int version;
stream >> version;
if (version != PRELOAD_FILE_VERSION)
return;
while (file.bytesAvailable()) {
QString imageId;
QSize size;
quint32 requestCount;
bool isMostUsed;
stream >> imageId >> size >> requestCount >> isMostUsed;
PixmapIdentifier id(imageId, size);
requestCounts.insert(id, requestCount);
if (isMostUsed) {
toLoadList.insert(PixmapIdentifier(imageId, size));
mostUsedPixmaps.insert(PixmapIdentifier(imageId, size));
}
}
if (!toLoadList.isEmpty()) {
cpuMonitor.start(delayBeforeLoadingNextItemWhenBusy);
}
file.close();
}
void MCommonPixmaps::save() const
{
QFile file(cacheFilename());
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << Q_FUNC_INFO << "Could not save most used pixmaps to" << cacheFilename();
return;
}
QDataStream stream(&file);
stream << PRELOAD_FILE_VERSION;
QHash<PixmapIdentifier, quint32>::const_iterator i = requestCounts.begin();
for (; i != requestCounts.end(); ++i) {
const PixmapIdentifier& id = i.key();
bool isMostUsed = mostUsedPixmaps.contains(id);
stream << id.imageId << id.size << i.value() << isMostUsed;
}
file.close();
}
void MCommonPixmaps::loadOne()
{
// stop the timer, so we can adjust the frequency depending on the usage
cpuMonitor.stop();
if ((cpuMonitor.usage() != -1) && (cpuMonitor.usage() <= maximumCpuUsageBeforeLoadingOneItem)) {
for (int i = 0; i < numberOfPixmapsToLoadAtOnce; ++i) {
if (!toLoadList.isEmpty()) {
PixmapIdentifier id = *toLoadList.begin();
toLoadList.erase(toLoadList.begin());
ImageResource *resource = daemon->findImageResource(id.imageId);
if (resource) {
resource->fetchPixmap(id.size);
} else {
qWarning() << Q_FUNC_INFO << QString("Themedaemon could not find resource %1 while loading most used pixmaps. Removing from list.").arg(id.imageId);
requestCounts.remove(id);
mostUsedPixmaps.remove(id);
}
} else {
break;
}
}
if (!toLoadList.isEmpty()) {
// there's still items in the list, so start the timer with small delay
cpuMonitor.start(delayBeforeLoadingNextItemWhenIdle);
} else {
if (toLoadList.isEmpty()) {
disconnect(&cpuMonitor, SIGNAL(newCpuFrameAvailable()), this, SLOT(loadOne()));
connect(&cpuMonitor, SIGNAL(newCpuFrameAvailable()), SLOT(updateClientsAboutMostUsed()));
cpuMonitor.start(delayBeforeSendingMostUsed);
}
}
} else {
// the cpu usage was too high, so start start the timer with longer delay
cpuMonitor.start(delayBeforeLoadingNextItemWhenBusy);
}
}
void MCommonPixmaps::updateClientsAboutMostUsed()
{
cpuMonitor.stop();
// only wakeup all clients if the device is idle
if ((cpuMonitor.usage() != -1) && (cpuMonitor.usage() <= maximumCpuUsageBeforeSendingMostUsed)) {
MostUsedPixmaps mostUsed;
mostUsed.addedHandles = mostUsedPixmapHandles();
emit mostUsedPixmapsChanged(mostUsed);
disconnect(&cpuMonitor, SIGNAL(newCpuFrameAvailable()), this, SLOT(updateClientsAboutMostUsed()));
} else {
cpuMonitor.start(delayBeforeSendingMostUsed);
}
}
void MCommonPixmaps::increaseRequestCount(const M::MThemeDaemonProtocol::PixmapIdentifier &id, ImageResource *resource)
{
QHash<PixmapIdentifier, quint32>::iterator requestCount = requestCounts.find(id);
if (requestCount == requestCounts.end()) {
requestCount = requestCounts.insert(id, 0);
}
++requestCount.value();
if (toLoadList.contains(id)) {
toLoadList.remove(id);
resource->fetchPixmap(id.size);
}
// pixmap has higher request count value than the current minimum for cache?
if (requestCount.value() > minRequestsForCache && !mostUsedPixmaps.contains(id)) {
// this pixmap might end up in mostUsedPixmaps list
// check if there's still room for this pixmap
if (mostUsedPixmaps.count() < MCommonPixmaps::CacheSize) {
// yep, just add this pixmap and return
MPixmapHandle handle = resource->fetchPixmap(id.size);
mostUsedPixmaps.insert(id);
return;
}
// there was no room, so we'll check if we can make it
QSet<PixmapIdentifier>::iterator i = mostUsedPixmaps.begin();
QSet<PixmapIdentifier>::iterator leastUsed = i;
quint32 leastUsedRequests = requestCounts[*leastUsed];
quint32 secondlyLeastUsedRequests = leastUsedRequests;
++i;
// find the least used pixmap from most used list
for (; i != mostUsedPixmaps.end(); ++i) {
const PixmapIdentifier &curId = *i;
quint32 count = requestCounts[curId];
if (count < leastUsedRequests) {
secondlyLeastUsedRequests = leastUsedRequests;
leastUsedRequests = count;
leastUsed = i;
}
}
// if the least used is still above the current, we'll just update the limit
if (leastUsedRequests >= requestCount.value()) {
minRequestsForCache = leastUsedRequests;
return;
}
// otherwise we have a new pixmap for the list
// update the limit, there may be duplicate request counts in the most used list
minRequestsForCache = (secondlyLeastUsedRequests > requestCount.value()) ? requestCount.value() : secondlyLeastUsedRequests;
// allocate one pixmap for the list
MPixmapHandle handle = resource->fetchPixmap(id.size);
MostUsedPixmaps packet;
packet.addedHandles.append(PixmapHandle(id, handle));
// release the old one from the list
PixmapIdentifier leastUsedId = *leastUsed;
mostUsedPixmaps.remove(leastUsedId);
if (!toLoadList.remove(leastUsedId)) {
packet.removedIdentifiers.append(id);
// OBS: if it's also in the toLoadList it means that it has not been
// fetched yet and therefore there's nothing to be released
ImageResource* old = daemon->findImageResource(leastUsedId.imageId);
old->releasePixmap(leastUsedId.size);
}
mostUsedPixmaps.insert(id);
if (toLoadList.isEmpty()) {
emit mostUsedPixmapsChanged(packet);
}
}
}
void MCommonPixmaps::reload(const PixmapIdentifier &id, ImageResource *oldResource)
{
if (toLoadList.contains(id) || !mostUsedPixmaps.contains(id)) {
// no need to do anything
return;
}
oldResource->releasePixmap(id.size);
toLoadList.insert(id);
}
QList<M::MThemeDaemonProtocol::PixmapHandle> MCommonPixmaps::mostUsedPixmapHandles()
{
QSet<M::MThemeDaemonProtocol::PixmapIdentifier> validMostUsedPixmaps(mostUsedPixmaps);
validMostUsedPixmaps.subtract(toLoadList);
// we could also save the handles earlier but it is cheap to do the query
QList<PixmapHandle> pixmapHandles;
foreach(const M::MThemeDaemonProtocol::PixmapIdentifier& id, validMostUsedPixmaps) {
MPixmapHandle handle = daemon->findImageResource(id.imageId)->pixmapHandle(id.size);
pixmapHandles.append(M::MThemeDaemonProtocol::PixmapHandle(id, handle));
}
return pixmapHandles;
}
QString MCommonPixmaps::cacheFilename() const
{
if (!requiredCacheFilename.isEmpty()) {
return requiredCacheFilename;
} else {
return daemon->systemThemeCacheDirectory() + QDir::separator() + daemon->currentTheme() + QDir::separator() + QLatin1String("preload.list");
}
}
void MCommonPixmaps::setCacheFilename(const QString &filename)
{
requiredCacheFilename = filename;
}
void MCommonPixmaps::considerSaving()
{
// we do not allow more than one update in 10 seconds
const int delay = 10000;
if (!timerSinceLastSave.isValid() || timerSinceLastSave.hasExpired(delay)) {
save();
timerSinceLastSave.start();
}
}