blob: badbc9cd8476471181506312bf6bae2b65808788 [file] [log] [blame]
djsollen@google.com21830d92012-08-07 19:49:41 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#ifndef SkBitmapHeap_DEFINED
9#define SkBitmapHeap_DEFINED
10
11#include "SkBitmap.h"
12#include "SkFlattenable.h"
13#include "SkRefCnt.h"
14#include "SkTDArray.h"
15#include "SkThread.h"
16#include "SkTRefArray.h"
17
18/**
19 * SkBitmapHeapEntry provides users of SkBitmapHeap (using internal storage) with a means to...
20 * (1) get access a bitmap in the heap
21 * (2) indicate they are done with bitmap by releasing their reference (if they were an owner).
22 */
23class SkBitmapHeapEntry : SkNoncopyable {
24public:
25 ~SkBitmapHeapEntry();
26
27 int32_t getSlot() { return fSlot; }
28
29 SkBitmap* getBitmap() { return &fBitmap; }
30
31 void releaseRef() {
32 sk_atomic_dec(&fRefCount);
33 }
34
35private:
36 SkBitmapHeapEntry();
37
38 void addReferences(int count);
39
40 int32_t fSlot;
41 int32_t fRefCount;
42
43 SkBitmap fBitmap;
44 // Keep track of the bytes allocated for this bitmap. When replacing the
45 // bitmap or removing this HeapEntry we know how much memory has been
46 // reclaimed.
47 size_t fBytesAllocated;
djsollen@google.com21830d92012-08-07 19:49:41 +000048
49 friend class SkBitmapHeap;
50};
51
52
53class SkBitmapHeapReader : public SkRefCnt {
54public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000055 SK_DECLARE_INST_COUNT(SkBitmapHeapReader)
56
djsollen@google.com21830d92012-08-07 19:49:41 +000057 SkBitmapHeapReader() : INHERITED() {}
58 virtual SkBitmap* getBitmap(int32_t slot) const = 0;
59 virtual void releaseRef(int32_t slot) = 0;
60private:
61 typedef SkRefCnt INHERITED;
62};
63
64
65/**
66 * TODO: stores immutable bitmaps into a heap
67 */
68class SkBitmapHeap : public SkBitmapHeapReader {
69public:
70 class ExternalStorage : public SkRefCnt {
71 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000072 SK_DECLARE_INST_COUNT(ExternalStorage)
73
djsollen@google.com21830d92012-08-07 19:49:41 +000074 virtual bool insert(const SkBitmap& bitmap, int32_t slot) = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000075
76 private:
77 typedef SkRefCnt INHERITED;
djsollen@google.com21830d92012-08-07 19:49:41 +000078 };
79
80 static const int32_t UNLIMITED_SIZE = -1;
81 static const int32_t IGNORE_OWNERS = -1;
82 static const int32_t INVALID_SLOT = -1;
83
84 /**
85 * Constructs a heap that is responsible for allocating and managing its own storage. In the
86 * case where we choose to allow the heap to grow indefinitely (i.e. UNLIMITED_SIZE) we
87 * guarantee that once allocated in the heap a bitmap's index in the heap is immutable.
88 * Otherwise we guarantee the bitmaps placement in the heap until its owner count goes to zero.
89 *
90 * @param preferredSize Specifies the preferred maximum number of bitmaps to store. This is
91 * not a hard limit as it can grow larger if the number of bitmaps in the heap with active
92 * owners exceeds this limit.
93 * @param ownerCount The number of owners to assign to each inserted bitmap. NOTE: while a
94 * bitmap in the heap has a least one owner it can't be removed.
95 */
96 SkBitmapHeap(int32_t preferredSize = UNLIMITED_SIZE, int32_t ownerCount = IGNORE_OWNERS);
97
98 /**
99 * Constructs a heap that defers the responsibility of storing the bitmaps to an external
100 * function. This is especially useful if the bitmaps will be used in a separate process as the
101 * external storage can ensure the data is properly shuttled to the appropriate processes.
102 *
103 * Our LRU implementation assumes that inserts into the external storage are consumed in the
104 * order that they are inserted (i.e. SkPipe). This ensures that we don't need to query the
105 * external storage to see if a slot in the heap is eligible to be overwritten.
106 *
107 * @param externalStorage The class responsible for storing the bitmaps inserted into the heap
108 * @param heapSize The maximum size of the heap. Because of the sequential limitation imposed
109 * by our LRU implementation we can guarantee that the heap will never grow beyond this size.
110 */
111 SkBitmapHeap(ExternalStorage* externalStorage, int32_t heapSize = UNLIMITED_SIZE);
112
113 ~SkBitmapHeap();
114
115 /**
116 * Makes a shallow copy of all bitmaps currently in the heap and returns them as an array. The
117 * array indices match their position in the heap.
118 *
119 * @return a ptr to an array of bitmaps or NULL if external storage is being used.
120 */
121 SkTRefArray<SkBitmap>* extractBitmaps() const;
122
123 /**
124 * Retrieves the bitmap from the specified slot in the heap
125 *
126 * @return The bitmap located at that slot or NULL if external storage is being used.
127 */
128 virtual SkBitmap* getBitmap(int32_t slot) const SK_OVERRIDE {
129 SkASSERT(fExternalStorage == NULL);
130 SkBitmapHeapEntry* entry = getEntry(slot);
131 if (entry) {
132 return &entry->fBitmap;
133 }
134 return NULL;
135 }
136
137 /**
138 * Retrieves the bitmap from the specified slot in the heap
139 *
140 * @return The bitmap located at that slot or NULL if external storage is being used.
141 */
142 virtual void releaseRef(int32_t slot) SK_OVERRIDE {
143 SkASSERT(fExternalStorage == NULL);
144 if (fOwnerCount != IGNORE_OWNERS) {
145 SkBitmapHeapEntry* entry = getEntry(slot);
146 if (entry) {
147 entry->releaseRef();
148 }
149 }
150 }
151
152 /**
153 * Inserts a bitmap into the heap. The stored version of bitmap is guaranteed to be immutable
154 * and is not dependent on the lifecycle of the provided bitmap.
155 *
156 * @param bitmap the bitmap to be inserted into the heap
157 * @return the slot in the heap where the bitmap is stored or INVALID_SLOT if the bitmap could
158 * not be added to the heap. If it was added the slot will remain valid...
159 * (1) indefinitely if no owner count has been specified.
160 * (2) until all owners have called releaseRef on the appropriate SkBitmapHeapEntry*
161 */
162 int32_t insert(const SkBitmap& bitmap);
163
164 /**
165 * Retrieves an entry from the heap at a given slot.
166 *
167 * @param slot the slot in the heap where a bitmap was stored.
168 * @return a SkBitmapHeapEntry that wraps the bitmap or NULL if external storage is used.
169 */
170 SkBitmapHeapEntry* getEntry(int32_t slot) const {
171 SkASSERT(slot <= fStorage.count());
172 if (fExternalStorage != NULL) {
173 return NULL;
174 }
175 return fStorage[slot];
176 }
177
178 /**
179 * Returns a count of the number of items currently in the heap
180 */
181 int count() const {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000182 SkASSERT(fExternalStorage != NULL ||
183 fStorage.count() - fUnusedSlots.count() == fLookupTable.count());
djsollen@google.com21830d92012-08-07 19:49:41 +0000184 return fLookupTable.count();
185 }
186
187 /**
188 * Returns the total number of bytes allocated by the bitmaps in the heap
189 */
190 size_t bytesAllocated() const {
191 return fBytesAllocated;
192 }
193
scroggo@google.com10dccde2012-08-08 20:43:22 +0000194 /**
195 * Attempt to reduce the storage allocated.
196 * @param bytesToFree minimum number of bytes that should be attempted to
197 * be freed.
198 * @return number of bytes actually freed.
199 */
200 size_t freeMemoryIfPossible(size_t bytesToFree);
201
scroggo@google.com7ca24432012-08-14 15:48:43 +0000202 /**
203 * Defer any increments of owner counts until endAddingOwnersDeferral is called. So if an
204 * existing SkBitmap is inserted into the SkBitmapHeap, its corresponding SkBitmapHeapEntry will
205 * not have addReferences called on it, and the client does not need to make a corresponding
206 * call to releaseRef. Only meaningful if this SkBitmapHeap was created with an owner count not
207 * equal to IGNORE_OWNERS.
208 */
209 void deferAddingOwners();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
scroggo@google.com7ca24432012-08-14 15:48:43 +0000211 /**
212 * Resume adding references when duplicate SkBitmaps are inserted.
213 * @param add If true, add references to the SkBitmapHeapEntrys whose SkBitmaps were re-inserted
214 * while deferring.
215 */
216 void endAddingOwnersDeferral(bool add);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217
djsollen@google.com21830d92012-08-07 19:49:41 +0000218private:
219 struct LookupEntry {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000220 LookupEntry(const SkBitmap& bm)
221 : fGenerationId(bm.getGenerationID())
222 , fPixelOffset(bm.pixelRefOffset())
223 , fWidth(bm.width())
224 , fHeight(bm.height())
225 , fMoreRecentlyUsed(NULL)
226 , fLessRecentlyUsed(NULL){}
227
228 const uint32_t fGenerationId; // SkPixelRef GenerationID.
229 const size_t fPixelOffset;
230 const uint32_t fWidth;
231 const uint32_t fHeight;
232
233 // TODO: Generalize the LRU caching mechanism
234 LookupEntry* fMoreRecentlyUsed;
235 LookupEntry* fLessRecentlyUsed;
djsollen@google.com21830d92012-08-07 19:49:41 +0000236
237 uint32_t fStorageSlot; // slot of corresponding bitmap in fStorage.
238
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000239 /**
240 * Compare two LookupEntry pointers, returning -1, 0, 1 for sorting.
241 */
242 static int Compare(const LookupEntry* a, const LookupEntry* b);
djsollen@google.com21830d92012-08-07 19:49:41 +0000243 };
244
245 /**
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000246 * Remove the entry from the lookup table. Also deletes the entry pointed
247 * to by the table. Therefore, if a pointer to that one was passed in, the
248 * pointer should no longer be used, since the object to which it points has
249 * been deleted.
scroggo@google.com10dccde2012-08-08 20:43:22 +0000250 * @return The index in the lookup table of the entry before removal.
251 */
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000252 int removeEntryFromLookupTable(LookupEntry*);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000253
254 /**
djsollen@google.com21830d92012-08-07 19:49:41 +0000255 * Searches for the bitmap in the lookup table and returns the bitmaps index within the table.
256 * If the bitmap was not already in the table it is added.
257 *
scroggo@google.com10dccde2012-08-08 20:43:22 +0000258 * @param key The key to search the lookup table, created from a bitmap.
djsollen@google.com21830d92012-08-07 19:49:41 +0000259 * @param entry A pointer to a SkBitmapHeapEntry* that if non-null AND the bitmap is found
260 * in the lookup table is populated with the entry from the heap storage.
261 */
scroggo@google.com10dccde2012-08-08 20:43:22 +0000262 int findInLookupTable(const LookupEntry& key, SkBitmapHeapEntry** entry);
djsollen@google.com21830d92012-08-07 19:49:41 +0000263
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000264 LookupEntry* findEntryToReplace(const SkBitmap& replacement);
djsollen@google.com21830d92012-08-07 19:49:41 +0000265 bool copyBitmap(const SkBitmap& originalBitmap, SkBitmap& copiedBitmap);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000266
267 /**
268 * Remove a LookupEntry from the LRU, in preparation for either deleting or appending as most
269 * recent. Points the LookupEntry's old neighbors at each other, and sets fLeastRecentlyUsed
270 * (if there is still an entry left). Sets LookupEntry's fMoreRecentlyUsed to NULL and leaves
271 * its fLessRecentlyUsed unmodified.
272 */
273 void removeFromLRU(LookupEntry* entry);
274
275 /**
276 * Append a LookupEntry to the end of the LRU cache, marking it as the most
277 * recently used. Assumes that the LookupEntry is already in fLookupTable,
278 * but is not in the LRU cache. If it is in the cache, removeFromLRU should
279 * be called first.
280 */
281 void appendToLRU(LookupEntry*);
djsollen@google.com21830d92012-08-07 19:49:41 +0000282
283 // searchable index that maps to entries in the heap
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000284 SkTDArray<LookupEntry*> fLookupTable;
djsollen@google.com21830d92012-08-07 19:49:41 +0000285
286 // heap storage
287 SkTDArray<SkBitmapHeapEntry*> fStorage;
scroggo@google.com10dccde2012-08-08 20:43:22 +0000288 // Used to mark slots in fStorage as deleted without actually deleting
289 // the slot so as not to mess up the numbering.
290 SkTDArray<int> fUnusedSlots;
djsollen@google.com21830d92012-08-07 19:49:41 +0000291 ExternalStorage* fExternalStorage;
292
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000293 LookupEntry* fMostRecentlyUsed;
294 LookupEntry* fLeastRecentlyUsed;
djsollen@google.com21830d92012-08-07 19:49:41 +0000295
296 const int32_t fPreferredCount;
297 const int32_t fOwnerCount;
298 size_t fBytesAllocated;
299
scroggo@google.com7ca24432012-08-14 15:48:43 +0000300 bool fDeferAddingOwners;
301 SkTDArray<int> fDeferredEntries;
302
djsollen@google.com21830d92012-08-07 19:49:41 +0000303 typedef SkBitmapHeapReader INHERITED;
304};
305
306#endif // SkBitmapHeap_DEFINED