xref: /haiku/src/apps/showimage/ImageCache.h (revision 002f37b0cca92e4cf72857c72ac95db5a8b09615)
1 /*
2  * Copyright 2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef IMAGE_CACHE_H
6 #define IMAGE_CACHE_H
7 
8 
9 #include <deque>
10 #include <map>
11 #include <set>
12 
13 #include <Entry.h>
14 #include <Locker.h>
15 #include <String.h>
16 
17 #include <kernel/util/DoublyLinkedList.h>
18 #include <Referenceable.h>
19 
20 
21 class BBitmap;
22 class BMessage;
23 class BMessenger;
24 struct QueueEntry;
25 
26 
27 enum {
28 	kMsgImageCacheImageLoaded		= 'icIL',
29 	kMsgImageCacheProgressUpdate	= 'icPU'
30 };
31 
32 
33 class BitmapOwner : public BReferenceable {
34 public:
35 								BitmapOwner(BBitmap* bitmap);
36 	virtual						~BitmapOwner();
37 
38 private:
39 			BBitmap*			fBitmap;
40 };
41 
42 
43 struct CacheEntry : DoublyLinkedListLinkImpl<CacheEntry> {
44 	entry_ref				ref;
45 	int32					page;
46 	int32					pageCount;
47 	BBitmap*				bitmap;
48 	BitmapOwner*			bitmapOwner;
49 	BString					type;
50 	BString					mimeType;
51 };
52 
53 
54 class ImageCache {
55 public:
56 	static	ImageCache&			Default() { return sCache; }
57 
58 			status_t			RetrieveImage(const entry_ref& ref,
59 									int32 page = 1,
60 									const BMessenger* target = NULL);
61 
62 private:
63 								ImageCache();
64 	virtual						~ImageCache();
65 
66 	static	status_t			_QueueWorkerThread(void* self);
67 
68 			status_t			_RetrieveImage(QueueEntry* entry,
69 									CacheEntry** _entry);
70 			void				_NotifyListeners(CacheEntry* entry,
71 									QueueEntry* queueEntry);
72 			void				_NotifyTarget(CacheEntry* entry,
73 									const BMessenger* target);
74 			void				_BuildNotification(CacheEntry* entry,
75 									BMessage& message);
76 
77 private:
78 			typedef std::pair<entry_ref, int32> ImageSelector;
79 			typedef std::map<ImageSelector, CacheEntry*> CacheMap;
80 			typedef std::map<ImageSelector, QueueEntry*> QueueMap;
81 			typedef std::deque<QueueEntry*> QueueDeque;
82 			typedef DoublyLinkedList<CacheEntry> CacheList;
83 
84 			BLocker				fLocker;
85 			CacheMap			fCacheMap;
86 			CacheList			fCacheEntriesByAge;
87 			QueueMap			fQueueMap;
88 			QueueDeque			fQueue;
89 			int32				fThreadCount;
90 			int32				fMaxThreadCount;
91 			uint64				fBytes;
92 			uint64				fMaxBytes;
93 			size_t				fMaxEntries;
94 
95 	static	ImageCache			sCache;
96 };
97 
98 
99 #endif	// IMAGE_CACHE_H
100