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
21class BBitmap;
22class BMessage;
23class BMessenger;
24struct QueueEntry;
25
26
27enum {
28	kMsgImageCacheImageLoaded		= 'icIL',
29	kMsgImageCacheProgressUpdate	= 'icPU'
30};
31
32
33class BitmapOwner : public BReferenceable {
34public:
35								BitmapOwner(BBitmap* bitmap);
36	virtual						~BitmapOwner();
37
38private:
39			BBitmap*			fBitmap;
40};
41
42
43struct 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
54class ImageCache {
55public:
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
63private:
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
76private:
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