xref: /haiku/src/apps/showimage/ImageCache.h (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 								ImageCache();
57 	virtual						~ImageCache();
58 			status_t			RetrieveImage(const entry_ref& ref,
59 									int32 page = 1,
60 									const BMessenger* target = NULL);
61 			void				Stop();
62 
63 private:
64 
65 	static	status_t			_QueueWorkerThread(void* self);
66 
67 			status_t			_RetrieveImage(QueueEntry* entry,
68 									CacheEntry** _entry);
69 			void				_NotifyListeners(CacheEntry* entry,
70 									QueueEntry* queueEntry);
71 			void				_NotifyTarget(CacheEntry* entry,
72 									const BMessenger* target);
73 			void				_BuildNotification(CacheEntry* entry,
74 									BMessage& message);
75 
76 private:
77 			typedef std::pair<entry_ref, int32> ImageSelector;
78 			typedef std::map<ImageSelector, CacheEntry*> CacheMap;
79 			typedef std::map<ImageSelector, QueueEntry*> QueueMap;
80 			typedef std::deque<QueueEntry*> QueueDeque;
81 			typedef DoublyLinkedList<CacheEntry> CacheList;
82 
83 			BLocker				fLocker;
84 			CacheMap			fCacheMap;
85 			CacheList			fCacheEntriesByAge;
86 			QueueMap			fQueueMap;
87 			QueueDeque			fQueue;
88 			int32				fThreadCount;
89 			int32				fMaxThreadCount;
90 			uint64				fBytes;
91 			uint64				fMaxBytes;
92 			size_t				fMaxEntries;
93 };
94 
95 
96 #endif	// IMAGE_CACHE_H
97