forked from Tulon/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThumbnailPixmapCache.cpp
1018 lines (825 loc) · 25.4 KB
/
ThumbnailPixmapCache.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) Joseph Artsimovich <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ThumbnailPixmapCache.h"
#include "ImageId.h"
#include "ImageLoader.h"
#include "AtomicFileOverwriter.h"
#include "RelinkablePath.h"
#include "OutOfMemoryHandler.h"
#include "imageproc/Scale.h"
#include "imageproc/GrayImage.h"
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QString>
#include <QChar>
#include <QImage>
#include <QPixmap>
#include <QEvent>
#include <QSize>
#include <QDebug>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/foreach.hpp>
#include <algorithm>
#include <vector>
#include <new>
using namespace ::boost;
using namespace ::boost::multi_index;
using namespace imageproc;
class ThumbnailPixmapCache::Item
{
public:
enum Status {
/**
* The background threaed hasn't touched it yet.
*/
QUEUED,
/**
* The image is currently being loaded by a background
* thread, or it has been loaded, but the main thread
* hasn't yet received the loaded image, or it's currently
* converting it to a pixmap.
*/
IN_PROGRESS,
/**
* The image was loaded and then converted to a pixmap
* by the main thread.
*/
LOADED,
/**
* The image could not be loaded.
*/
LOAD_FAILED
};
ImageId imageId;
mutable QPixmap pixmap; /**< Guaranteed to be set if status is LOADED */
mutable std::vector<
boost::weak_ptr<CompletionHandler>
> completionHandlers;
/**
* The total image loading attempts (of any images) by
* ThumbnailPixmapCache at the time of the creation of this item.
* This information is used for request expiration.
* \see ThumbnailLoadResult::REQUEST_EXPIRED
*/
int precedingLoadAttempts;
mutable Status status;
Item(ImageId const& image_id, int preceding_load_attepmts, Status status);
Item(Item const& other);
private:
Item& operator=(Item const& other); // Assignment is forbidden.
};
class ThumbnailPixmapCache::Impl : public QThread
{
public:
Impl(QString const& thumb_dir, QSize const& max_thumb_size,
int max_cached_pixmaps, int expiration_threshold);
~Impl();
void setThumbDir(QString const& thumb_dir);
Status request(
ImageId const& image_id, QPixmap& pixmap, bool load_now = false,
boost::weak_ptr<CompletionHandler> const* completion_handler = 0);
void ensureThumbnailExists(ImageId const& image_id, QImage const& image);
void recreateThumbnail(ImageId const& image_id, QImage const& image);
protected:
virtual void run();
virtual void customEvent(QEvent* e);
private:
class LoadResultEvent;
class ItemsByKeyTag;
class LoadQueueTag;
class RemoveQueueTag;
typedef multi_index_container<
Item,
indexed_by<
ordered_unique<tag<ItemsByKeyTag>, member<Item, ImageId, &Item::imageId> >,
sequenced<tag<LoadQueueTag> >,
sequenced<tag<RemoveQueueTag> >
>
> Container;
typedef Container::index<ItemsByKeyTag>::type ItemsByKey;
typedef Container::index<LoadQueueTag>::type LoadQueue;
typedef Container::index<RemoveQueueTag>::type RemoveQueue;
class BackgroundLoader : public QObject
{
public:
BackgroundLoader(Impl& owner);
protected:
virtual void customEvent(QEvent* e);
private:
Impl& m_rOwner;
};
void backgroundProcessing();
static QImage loadSaveThumbnail(
ImageId const& image_id, QString const& thumb_dir,
QSize const& max_thumb_size);
static QString getThumbFilePath(
ImageId const& image_id, QString const& thumb_dir);
static QImage makeThumbnail(
QImage const& image, QSize const& max_thumb_size);
void queuedToInProgress(LoadQueue::iterator const& lq_it);
void postLoadResult(
LoadQueue::iterator const& lq_it, QImage const& image,
ThumbnailLoadResult::Status status);
void processLoadResult(LoadResultEvent* result);
void removeExcessLocked();
void removeItemLocked(RemoveQueue::iterator const& it);
void cachePixmapUnlocked(ImageId const& image_id, QPixmap const& pixmap);
void cachePixmapLocked(ImageId const& image_id, QPixmap const& pixmap);
mutable QMutex m_mutex;
BackgroundLoader m_backgroundLoader;
Container m_items;
ItemsByKey& m_itemsByKey; /**< ImageId => Item mapping */
/**
* An "std::list"-like view of QUEUED items in the order they are
* going to be loaded. Actually the list contains all kinds of items,
* but all QUEUED ones precede any others. New QUEUED items are added
* to the front of this list for purposes of request expiration.
* \see ThumbnailLoadResult::REQUEST_EXPIRED
*/
LoadQueue& m_loadQueue;
/**
* An "std::list"-like view of LOADED items in the order they are
* going to be removed. Actually the list contains all kinds of items,
* but all LOADED ones precede any others. Note that we don't bother
* removing items without a pixmap, which would be all except LOADED
* items. New LOADED items are added after the last LOADED item
* already present in the list.
*/
RemoveQueue& m_removeQueue;
/**
* An iterator of m_removeQueue that marks the end of LOADED items.
*/
RemoveQueue::iterator m_endOfLoadedItems;
QString m_thumbDir;
QSize m_maxThumbSize;
int m_maxCachedPixmaps;
/**
* \see ThumbnailPixmapCache::ThumbnailPixmapCache()
*/
int m_expirationThreshold;
int m_numQueuedItems;
int m_numLoadedItems;
/**
* Total image loading attempts so far. Used for request expiration.
* \see ThumbnailLoadResult::REQUEST_EXPIRED
*/
int m_totalLoadAttempts;
bool m_threadStarted;
bool m_shuttingDown;
};
class ThumbnailPixmapCache::Impl::LoadResultEvent : public QEvent
{
public:
LoadResultEvent(Impl::LoadQueue::iterator const& lq_t,
QImage const& image, ThumbnailLoadResult::Status status);
virtual ~LoadResultEvent();
Impl::LoadQueue::iterator lqIter() const { return m_lqIter; }
QImage const& image() const { return m_image; }
void releaseImage() { m_image = QImage(); }
ThumbnailLoadResult::Status status() const { return m_status; }
private:
Impl::LoadQueue::iterator m_lqIter;
QImage m_image;
ThumbnailLoadResult::Status m_status;
};
/*========================== ThumbnailPixmapCache ===========================*/
ThumbnailPixmapCache::ThumbnailPixmapCache(
QString const& thumb_dir, QSize const& max_thumb_size,
int const max_cached_pixmaps, int const expiration_threshold)
: m_ptrImpl(
new Impl(
RelinkablePath::normalize(thumb_dir), max_thumb_size,
max_cached_pixmaps, expiration_threshold
)
)
{
}
ThumbnailPixmapCache::~ThumbnailPixmapCache()
{
}
void
ThumbnailPixmapCache::setThumbDir(QString const& thumb_dir)
{
m_ptrImpl->setThumbDir(RelinkablePath::normalize(thumb_dir));
}
ThumbnailPixmapCache::Status
ThumbnailPixmapCache::loadFromCache(ImageId const& image_id, QPixmap& pixmap)
{
return m_ptrImpl->request(image_id, pixmap);
}
ThumbnailPixmapCache::Status
ThumbnailPixmapCache::loadNow(ImageId const& image_id, QPixmap& pixmap)
{
return m_ptrImpl->request(image_id, pixmap, true);
}
ThumbnailPixmapCache::Status
ThumbnailPixmapCache::loadRequest(
ImageId const& image_id, QPixmap& pixmap,
boost::weak_ptr<CompletionHandler> const& completion_handler)
{
return m_ptrImpl->request(image_id, pixmap, false, &completion_handler);
}
void
ThumbnailPixmapCache::ensureThumbnailExists(
ImageId const& image_id, QImage const& image)
{
m_ptrImpl->ensureThumbnailExists(image_id, image);
}
void
ThumbnailPixmapCache::recreateThumbnail(
ImageId const& image_id, QImage const& image)
{
m_ptrImpl->recreateThumbnail(image_id, image);
}
/*======================= ThumbnailPixmapCache::Impl ========================*/
ThumbnailPixmapCache::Impl::Impl(
QString const& thumb_dir, QSize const& max_thumb_size,
int const max_cached_pixmaps, int const expiration_threshold)
: m_backgroundLoader(*this),
m_items(),
m_itemsByKey(m_items.get<ItemsByKeyTag>()),
m_loadQueue(m_items.get<LoadQueueTag>()),
m_removeQueue(m_items.get<RemoveQueueTag>()),
m_endOfLoadedItems(m_removeQueue.end()),
m_thumbDir(thumb_dir),
m_maxThumbSize(max_thumb_size),
m_maxCachedPixmaps(max_cached_pixmaps),
m_expirationThreshold(expiration_threshold),
m_numQueuedItems(0),
m_numLoadedItems(0),
m_totalLoadAttempts(0),
m_threadStarted(false),
m_shuttingDown(false)
{
// Note that QDir::mkdir() will fail if the parent directory,
// that is $OUT/cache doesn't exist. We want that behaviour,
// as otherwise when loading a project from a different machine,
// a whole bunch of bogus directories would be created.
QDir().mkdir(m_thumbDir);
m_backgroundLoader.moveToThread(this);
}
ThumbnailPixmapCache::Impl::~Impl()
{
{
QMutexLocker const locker(&m_mutex);
if (!m_threadStarted) {
return;
}
m_shuttingDown = true;
}
quit();
wait();
}
void
ThumbnailPixmapCache::Impl::setThumbDir(QString const& thumb_dir)
{
QMutexLocker locker(&m_mutex);
if (thumb_dir == m_thumbDir) {
return;
}
m_thumbDir = thumb_dir;
BOOST_FOREACH(Item const& item, m_loadQueue) {
// This trick will make all queued tasks to expire.
m_totalLoadAttempts = std::max(
m_totalLoadAttempts,
item.precedingLoadAttempts + m_expirationThreshold + 1
);
}
}
ThumbnailPixmapCache::Status
ThumbnailPixmapCache::Impl::request(
ImageId const& image_id, QPixmap& pixmap, bool const load_now,
boost::weak_ptr<CompletionHandler> const* completion_handler)
{
assert(QCoreApplication::instance()->thread() == QThread::currentThread());
QMutexLocker locker(&m_mutex);
if (m_shuttingDown) {
return LOAD_FAILED;
}
ItemsByKey::iterator const k_it(m_itemsByKey.find(image_id));
if (k_it != m_itemsByKey.end()) {
if (k_it->status == Item::LOADED) {
pixmap = k_it->pixmap;
// Move it after all other candidates for removal.
RemoveQueue::iterator const rq_it(
m_items.project<RemoveQueueTag>(k_it)
);
m_removeQueue.relocate(m_endOfLoadedItems, rq_it);
return LOADED;
} else if (k_it->status == Item::LOAD_FAILED) {
pixmap = k_it->pixmap;
return LOAD_FAILED;
}
}
if (load_now) {
QString const thumb_dir(m_thumbDir);
QSize const max_thumb_size(m_maxThumbSize);
locker.unlock();
pixmap = QPixmap::fromImage(
loadSaveThumbnail(image_id, thumb_dir, max_thumb_size)
);
if (pixmap.isNull()) {
return LOAD_FAILED;
}
cachePixmapUnlocked(image_id, pixmap);
return LOADED;
}
if (!completion_handler) {
return LOAD_FAILED;
}
if (k_it != m_itemsByKey.end()) {
assert(k_it->status == Item::QUEUED || k_it->status == Item::IN_PROGRESS);
k_it->completionHandlers.push_back(*completion_handler);
if (k_it->status == Item::QUEUED) {
// Because we've got a new request for this item,
// we move it to the beginning of the load queue.
// Note that we don't do it for IN_PROGRESS items,
// because all QUEUED items must precede any other
// items in the load queue.
LoadQueue::iterator const lq_it(
m_items.project<LoadQueueTag>(k_it)
);
m_loadQueue.relocate(m_loadQueue.begin(), lq_it);
}
return QUEUED;
}
// Create a new item.
LoadQueue::iterator const lq_it(
m_loadQueue.push_front(
Item(image_id, m_totalLoadAttempts, Item::QUEUED)
).first
);
// Now our new item is at the beginning of the load queue and at the
// end of the remove queue.
assert(lq_it->status == Item::QUEUED);
assert(lq_it->completionHandlers.empty());
if (m_endOfLoadedItems == m_removeQueue.end()) {
m_endOfLoadedItems = m_items.project<RemoveQueueTag>(lq_it);
}
lq_it->completionHandlers.push_back(*completion_handler);
if (m_numQueuedItems++ == 0) {
if (m_threadStarted) {
// Wake the background thread up.
QCoreApplication::postEvent(
&m_backgroundLoader, new QEvent(QEvent::User)
);
} else {
// Start the background thread.
start();
m_threadStarted = true;
}
}
return QUEUED;
}
void
ThumbnailPixmapCache::Impl::ensureThumbnailExists(
ImageId const& image_id, QImage const& image)
{
if (m_shuttingDown) {
return;
}
if (image.isNull()) {
return;
}
QMutexLocker locker(&m_mutex);
QString const thumb_dir(m_thumbDir);
QSize const max_thumb_size(m_maxThumbSize);
locker.unlock();
QString const thumb_file_path(getThumbFilePath(image_id, thumb_dir));
if (QFile::exists(thumb_file_path)) {
return;
}
QImage const thumbnail(makeThumbnail(image, max_thumb_size));
AtomicFileOverwriter overwriter;
QIODevice* iodev = overwriter.startWriting(thumb_file_path);
if (iodev && thumbnail.save(iodev, "PNG")) {
overwriter.commit();
}
}
void
ThumbnailPixmapCache::Impl::recreateThumbnail(
ImageId const& image_id, QImage const& image)
{
if (m_shuttingDown) {
return;
}
if (image.isNull()) {
return;
}
QMutexLocker locker(&m_mutex);
QString const thumb_dir(m_thumbDir);
QSize const max_thumb_size(m_maxThumbSize);
locker.unlock();
QString const thumb_file_path(getThumbFilePath(image_id, thumb_dir));
QImage const thumbnail(makeThumbnail(image, max_thumb_size));
bool thumb_written = false;
// Note that we may be called from multiple threads at the same time.
AtomicFileOverwriter overwriter;
QIODevice* iodev = overwriter.startWriting(thumb_file_path);
if (iodev && thumbnail.save(iodev, "PNG")) {
thumb_written = overwriter.commit();
} else {
overwriter.abort();
}
if (!thumb_written) {
return;
}
QMutexLocker const locker2(&m_mutex);
ItemsByKey::iterator const k_it(m_itemsByKey.find(image_id));
if (k_it == m_itemsByKey.end()) {
return;
}
switch (k_it->status) {
case Item::LOADED:
case Item::LOAD_FAILED:
removeItemLocked(m_items.project<RemoveQueueTag>(k_it));
break;
case Item::QUEUED:
break;
case Item::IN_PROGRESS:
// We have a small race condition in this case.
// We don't know if the other thread has already loaded
// the thumbnail or not. In case it did, again we
// don't know if it loaded the old or new version.
// Well, let's just pretend the thumnail was loaded
// (or failed to load) before we wrote the new version.
break;
}
}
void
ThumbnailPixmapCache::Impl::run()
{
backgroundProcessing();
exec(); // Wait for further processing requests (via custom events).
}
void
ThumbnailPixmapCache::Impl::customEvent(QEvent* e)
{
processLoadResult(dynamic_cast<LoadResultEvent*>(e));
}
void
ThumbnailPixmapCache::Impl::backgroundProcessing()
{
// This method is called from a background thread.
assert(QCoreApplication::instance()->thread() != QThread::currentThread());
for (;;) {
try {
// We are going to initialize these while holding the mutex.
LoadQueue::iterator lq_it;
ImageId image_id;
QString thumb_dir;
QSize max_thumb_size;
{
QMutexLocker const locker(&m_mutex);
if (m_shuttingDown || m_items.empty()) {
break;
}
lq_it = m_loadQueue.begin();
image_id = lq_it->imageId;
if (lq_it->status != Item::QUEUED) {
// All QUEUED items precede any other items
// in the load queue, so it means there are no
// QUEUED items at all.
assert(m_numQueuedItems == 0);
break;
}
// By marking the item as IN_PROGRESS, we prevent it
// from being processed again before the GUI thread
// receives our LoadResultEvent.
queuedToInProgress(lq_it);
if (m_totalLoadAttempts - lq_it->precedingLoadAttempts
> m_expirationThreshold) {
// Expire this request. The reasoning behind
// request expiration is described in
// ThumbnailLoadResult::REQUEST_EXPIRED
// documentation.
postLoadResult(
lq_it, QImage(),
ThumbnailLoadResult::REQUEST_EXPIRED
);
continue;
}
// Expired requests don't count as load attempts.
++m_totalLoadAttempts;
// Copy those while holding the mutex.
thumb_dir = m_thumbDir;
max_thumb_size = m_maxThumbSize;
} // mutex scope
QImage const image(
loadSaveThumbnail(image_id, thumb_dir, max_thumb_size)
);
ThumbnailLoadResult::Status const status = image.isNull()
? ThumbnailLoadResult::LOAD_FAILED
: ThumbnailLoadResult::LOADED;
postLoadResult(lq_it, image, status);
} catch (std::bad_alloc const&) {
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
}
}
}
QImage
ThumbnailPixmapCache::Impl::loadSaveThumbnail(
ImageId const& image_id, QString const& thumb_dir,
QSize const& max_thumb_size)
{
QString const thumb_file_path(getThumbFilePath(image_id, thumb_dir));
QImage image(ImageLoader::load(thumb_file_path, 0));
if (!image.isNull()) {
return image;
}
image = ImageLoader::load(image_id);
if (image.isNull()) {
return QImage();
}
QImage const thumbnail(makeThumbnail(image, max_thumb_size));
thumbnail.save(thumb_file_path, "PNG");
return thumbnail;
}
QString
ThumbnailPixmapCache::Impl::getThumbFilePath(
ImageId const& image_id, QString const& thumb_dir)
{
// Because a project may have several files with the same name (from
// different directories), we add a hash of the original image path
// to the thumbnail file name.
QByteArray const orig_path_hash(
QCryptographicHash::hash(
image_id.filePath().toUtf8(), QCryptographicHash::Md5
).toHex()
);
QString const orig_path_hash_str(
QString::fromAscii(orig_path_hash.data(), orig_path_hash.size())
);
QFileInfo const orig_img_path(image_id.filePath());
QString thumb_file_path(thumb_dir);
thumb_file_path += QChar('/');
thumb_file_path += orig_img_path.baseName();
thumb_file_path += QChar('_');
thumb_file_path += QString::number(image_id.zeroBasedPage());
thumb_file_path += QChar('_');
thumb_file_path += orig_path_hash_str;
thumb_file_path += QString::fromAscii(".png");
return thumb_file_path;
}
QImage
ThumbnailPixmapCache::Impl::makeThumbnail(
QImage const& image, QSize const& max_thumb_size)
{
if (image.width() < max_thumb_size.width() &&
image.height() < max_thumb_size.height()) {
return image;
}
QSize to_size(image.size());
to_size.scale(max_thumb_size, Qt::KeepAspectRatio);
if (image.format() == QImage::Format_Indexed8 && image.isGrayscale()) {
// This will be faster than QImage::scale().
return scaleToGray(GrayImage(image), to_size);
}
return image.scaled(
to_size,
Qt::KeepAspectRatio, Qt::SmoothTransformation
);
}
void
ThumbnailPixmapCache::Impl::queuedToInProgress(LoadQueue::iterator const& lq_it)
{
assert(lq_it->status == Item::QUEUED);
lq_it->status = Item::IN_PROGRESS;
assert(m_numQueuedItems > 0);
--m_numQueuedItems;
// Move it item to the end of load queue.
// The point is to keep QUEUED items before any others.
m_loadQueue.relocate(m_loadQueue.end(), lq_it);
// Going from QUEUED to IN_PROGRESS doesn't require
// moving it in the remove queue, as we only remove
// LOADED items.
}
void
ThumbnailPixmapCache::Impl::postLoadResult(
LoadQueue::iterator const& lq_it, QImage const& image,
ThumbnailLoadResult::Status const status)
{
LoadResultEvent* e = new LoadResultEvent(lq_it, image, status);
QCoreApplication::postEvent(this, e);
}
void
ThumbnailPixmapCache::Impl::processLoadResult(LoadResultEvent* result)
{
assert(QCoreApplication::instance()->thread() == QThread::currentThread());
QPixmap pixmap(QPixmap::fromImage(result->image()));
result->releaseImage();
std::vector<boost::weak_ptr<CompletionHandler> > completion_handlers;
{
QMutexLocker const locker(&m_mutex);
if (m_shuttingDown) {
return;
}
LoadQueue::iterator const lq_it(result->lqIter());
RemoveQueue::iterator const rq_it(
m_items.project<RemoveQueueTag>(lq_it)
);
Item const& item = *lq_it;
if (result->status() == ThumbnailLoadResult::LOADED
&& pixmap.isNull()) {
// That's a special case caused by cachePixmapLocked().
assert(!item.pixmap.isNull());
} else {
item.pixmap = pixmap;
}
item.completionHandlers.swap(completion_handlers);
if (result->status() == ThumbnailLoadResult::LOADED) {
// Maybe remove an older item.
removeExcessLocked();
item.status = Item::LOADED;
++m_numLoadedItems;
// Move this item after all other LOADED items in
// the remove queue.
m_removeQueue.relocate(m_endOfLoadedItems, rq_it);
// Move to the end of load queue.
m_loadQueue.relocate(m_loadQueue.end(), lq_it);
} else if (result->status() == ThumbnailLoadResult::LOAD_FAILED) {
// We keep items that failed to load, as they are cheap
// to keep and helps us avoid trying to load them
// again and again.
item.status = Item::LOAD_FAILED;
// Move to the end of load queue.
m_loadQueue.relocate(m_loadQueue.end(), lq_it);
} else {
assert(result->status() == ThumbnailLoadResult::REQUEST_EXPIRED);
// Just remove it.
removeItemLocked(rq_it);
}
} // mutex scope
// Notify listeners.
ThumbnailLoadResult const load_result(result->status(), pixmap);
typedef boost::weak_ptr<CompletionHandler> WeakHandler;
BOOST_FOREACH (WeakHandler const& wh, completion_handlers) {
boost::shared_ptr<CompletionHandler> const sh(wh.lock());
if (sh.get()) {
(*sh)(load_result);
}
}
}
void
ThumbnailPixmapCache::Impl::removeExcessLocked()
{
if (m_numLoadedItems >= m_maxCachedPixmaps) {
assert(m_numLoadedItems > 0);
assert(!m_removeQueue.empty());
assert(m_removeQueue.front().status == Item::LOADED);
removeItemLocked(m_removeQueue.begin());
}
}
void
ThumbnailPixmapCache::Impl::removeItemLocked(
RemoveQueue::iterator const& it)
{
switch (it->status) {
case Item::QUEUED:
assert(m_numQueuedItems > 0);
--m_numQueuedItems;
break;
case Item::LOADED:
assert(m_numLoadedItems > 0);
--m_numLoadedItems;
break;
default:;
}
if (m_endOfLoadedItems == it) {
++m_endOfLoadedItems;
}
m_removeQueue.erase(it);
}
void
ThumbnailPixmapCache::Impl::cachePixmapUnlocked(
ImageId const& image_id, QPixmap const& pixmap)
{
QMutexLocker const locker(&m_mutex);
cachePixmapLocked(image_id, pixmap);
}
void
ThumbnailPixmapCache::Impl::cachePixmapLocked(
ImageId const& image_id, QPixmap const& pixmap)
{
if (m_shuttingDown) {
return;
}
Item::Status const new_status = pixmap.isNull()
? Item::LOAD_FAILED : Item::LOADED;
// Check if such item already exists.
ItemsByKey::iterator const k_it(m_itemsByKey.find(image_id));
if (k_it == m_itemsByKey.end()) {
// Existing item not found.
// Maybe remove an older item.
removeExcessLocked();
// Insert our new item.
RemoveQueue::iterator const rq_it(
m_removeQueue.insert(
m_endOfLoadedItems,
Item(image_id, m_totalLoadAttempts, new_status)
).first
);
// Our new item is now after all LOADED items in the
// remove queue and at the end of the load queue.
if (new_status == Item::LOAD_FAILED) {
--m_endOfLoadedItems;
}
rq_it->pixmap = pixmap;
assert(rq_it->completionHandlers.empty());
return;
}
switch (k_it->status) {
case Item::LOADED:
// There is no point in replacing LOADED items.
case Item::IN_PROGRESS:
// It's unsafe to touch IN_PROGRESS items.
return;
default:
break;
}
if (new_status == Item::LOADED && k_it->status == Item::QUEUED) {
// Not so fast. We can't go from QUEUED to LOADED directly.
// Well, maybe we can, but we'd have to invoke the completion
// handlers right now. We'd rather do it asynchronously,
// so let's transition it to IN_PROGRESS and send
// a LoadResultEvent asynchronously.
assert(!k_it->completionHandlers.empty());
LoadQueue::iterator const lq_it(
m_items.project<LoadQueueTag>(k_it)
);
lq_it->pixmap = pixmap;
queuedToInProgress(lq_it);
postLoadResult(lq_it, QImage(), ThumbnailLoadResult::LOADED);
return;
}
assert(k_it->status == Item::LOAD_FAILED);
k_it->status = new_status;
k_it->pixmap = pixmap;
if (new_status == Item::LOADED) {
RemoveQueue::iterator const rq_it(
m_items.project<RemoveQueueTag>(k_it)
);
m_removeQueue.relocate(m_endOfLoadedItems, rq_it);
++m_numLoadedItems;
}
}
/*====================== ThumbnailPixmapCache::Item =========================*/
ThumbnailPixmapCache::Item::Item(ImageId const& image_id,
int const preceding_load_attempts, Status const st)
: imageId(image_id),
precedingLoadAttempts(preceding_load_attempts),
status(st)
{
}
ThumbnailPixmapCache::Item::Item(Item const& other)
: imageId(other.imageId),
pixmap(other.pixmap),
completionHandlers(other.completionHandlers),
precedingLoadAttempts(other.precedingLoadAttempts),
status(other.status)
{
}
/*=============== ThumbnailPixmapCache::Impl::LoadResultEvent ===============*/
ThumbnailPixmapCache::Impl::LoadResultEvent::LoadResultEvent(
Impl::LoadQueue::iterator const& lq_it, QImage const& image,
ThumbnailLoadResult::Status const status)
: QEvent(QEvent::User),
m_lqIter(lq_it),
m_image(image),
m_status(status)
{
}