3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #ifndef SkBitmapHeap_DEFINED
9 #define SkBitmapHeap_DEFINED
12 #include "SkFlattenable.h"
14 #include "SkTDArray.h"
18 * SkBitmapHeapEntry provides users of SkBitmapHeap (using internal storage) with a means to...
19 * (1) get access a bitmap in the heap
20 * (2) indicate they are done with bitmap by releasing their reference (if they were an owner).
22 class SkBitmapHeapEntry : SkNoncopyable {
26 int32_t getSlot() { return fSlot; }
28 SkBitmap* getBitmap() { return &fBitmap; }
31 sk_atomic_dec(&fRefCount);
37 void addReferences(int count);
43 // Keep track of the bytes allocated for this bitmap. When replacing the
44 // bitmap or removing this HeapEntry we know how much memory has been
46 size_t fBytesAllocated;
48 friend class SkBitmapHeap;
49 friend class SkBitmapHeapTester;
53 class SkBitmapHeapReader : public SkRefCnt {
55 SK_DECLARE_INST_COUNT(SkBitmapHeapReader)
57 SkBitmapHeapReader() : INHERITED() {}
58 virtual SkBitmap* getBitmap(int32_t slot) const = 0;
59 virtual void releaseRef(int32_t slot) = 0;
61 typedef SkRefCnt INHERITED;
66 * TODO: stores immutable bitmaps into a heap
68 class SkBitmapHeap : public SkBitmapHeapReader {
70 class ExternalStorage : public SkRefCnt {
72 SK_DECLARE_INST_COUNT(ExternalStorage)
74 virtual bool insert(const SkBitmap& bitmap, int32_t slot) = 0;
77 typedef SkRefCnt INHERITED;
80 static const int32_t UNLIMITED_SIZE = -1;
81 static const int32_t IGNORE_OWNERS = -1;
82 static const int32_t INVALID_SLOT = -1;
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.
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.
96 SkBitmapHeap(int32_t preferredSize = UNLIMITED_SIZE, int32_t ownerCount = IGNORE_OWNERS);
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.
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.
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.
111 SkBitmapHeap(ExternalStorage* externalStorage, int32_t heapSize = UNLIMITED_SIZE);
113 virtual ~SkBitmapHeap();
116 * Retrieves the bitmap from the specified slot in the heap
118 * @return The bitmap located at that slot or NULL if external storage is being used.
120 SkBitmap* getBitmap(int32_t slot) const SK_OVERRIDE {
121 SkASSERT(fExternalStorage == NULL);
122 SkBitmapHeapEntry* entry = getEntry(slot);
124 return &entry->fBitmap;
130 * Retrieves the bitmap from the specified slot in the heap
132 * @return The bitmap located at that slot or NULL if external storage is being used.
134 void releaseRef(int32_t slot) SK_OVERRIDE {
135 SkASSERT(fExternalStorage == NULL);
136 if (fOwnerCount != IGNORE_OWNERS) {
137 SkBitmapHeapEntry* entry = getEntry(slot);
145 * Inserts a bitmap into the heap. The stored version of bitmap is guaranteed to be immutable
146 * and is not dependent on the lifecycle of the provided bitmap.
148 * @param bitmap the bitmap to be inserted into the heap
149 * @return the slot in the heap where the bitmap is stored or INVALID_SLOT if the bitmap could
150 * not be added to the heap. If it was added the slot will remain valid...
151 * (1) indefinitely if no owner count has been specified.
152 * (2) until all owners have called releaseRef on the appropriate SkBitmapHeapEntry*
154 int32_t insert(const SkBitmap& bitmap);
157 * Retrieves an entry from the heap at a given slot.
159 * @param slot the slot in the heap where a bitmap was stored.
160 * @return a SkBitmapHeapEntry that wraps the bitmap or NULL if external storage is used.
162 SkBitmapHeapEntry* getEntry(int32_t slot) const {
163 SkASSERT(slot <= fStorage.count());
164 if (fExternalStorage != NULL) {
167 return fStorage[slot];
171 * Returns a count of the number of items currently in the heap
174 SkASSERT(fExternalStorage != NULL ||
175 fStorage.count() - fUnusedSlots.count() == fLookupTable.count());
176 return fLookupTable.count();
180 * Returns the total number of bytes allocated by the bitmaps in the heap
182 size_t bytesAllocated() const {
183 return fBytesAllocated;
187 * Attempt to reduce the storage allocated.
188 * @param bytesToFree minimum number of bytes that should be attempted to
190 * @return number of bytes actually freed.
192 size_t freeMemoryIfPossible(size_t bytesToFree);
195 * Defer any increments of owner counts until endAddingOwnersDeferral is called. So if an
196 * existing SkBitmap is inserted into the SkBitmapHeap, its corresponding SkBitmapHeapEntry will
197 * not have addReferences called on it, and the client does not need to make a corresponding
198 * call to releaseRef. Only meaningful if this SkBitmapHeap was created with an owner count not
199 * equal to IGNORE_OWNERS.
201 void deferAddingOwners();
204 * Resume adding references when duplicate SkBitmaps are inserted.
205 * @param add If true, add references to the SkBitmapHeapEntrys whose SkBitmaps were re-inserted
208 void endAddingOwnersDeferral(bool add);
212 LookupEntry(const SkBitmap& bm)
213 : fGenerationId(bm.getGenerationID())
214 , fPixelOrigin(bm.pixelRefOrigin())
216 , fHeight(bm.height())
217 , fMoreRecentlyUsed(NULL)
218 , fLessRecentlyUsed(NULL){}
220 const uint32_t fGenerationId; // SkPixelRef GenerationID.
221 const SkIPoint fPixelOrigin;
222 const uint32_t fWidth;
223 const uint32_t fHeight;
225 // TODO: Generalize the LRU caching mechanism
226 LookupEntry* fMoreRecentlyUsed;
227 LookupEntry* fLessRecentlyUsed;
229 uint32_t fStorageSlot; // slot of corresponding bitmap in fStorage.
232 * Compare two LookupEntry pointers for sorting and searching.
234 static bool Less(const LookupEntry& a, const LookupEntry& b);
238 * Remove the entry from the lookup table. Also deletes the entry pointed
239 * to by the table. Therefore, if a pointer to that one was passed in, the
240 * pointer should no longer be used, since the object to which it points has
242 * @return The index in the lookup table of the entry before removal.
244 int removeEntryFromLookupTable(LookupEntry*);
247 * Searches for the bitmap in the lookup table and returns the bitmaps index within the table.
248 * If the bitmap was not already in the table it is added.
250 * @param key The key to search the lookup table, created from a bitmap.
251 * @param entry A pointer to a SkBitmapHeapEntry* that if non-null AND the bitmap is found
252 * in the lookup table is populated with the entry from the heap storage.
254 int findInLookupTable(const LookupEntry& key, SkBitmapHeapEntry** entry);
256 LookupEntry* findEntryToReplace(const SkBitmap& replacement);
257 bool copyBitmap(const SkBitmap& originalBitmap, SkBitmap& copiedBitmap);
260 * Remove a LookupEntry from the LRU, in preparation for either deleting or appending as most
261 * recent. Points the LookupEntry's old neighbors at each other, and sets fLeastRecentlyUsed
262 * (if there is still an entry left). Sets LookupEntry's fMoreRecentlyUsed to NULL and leaves
263 * its fLessRecentlyUsed unmodified.
265 void removeFromLRU(LookupEntry* entry);
268 * Append a LookupEntry to the end of the LRU cache, marking it as the most
269 * recently used. Assumes that the LookupEntry is already in fLookupTable,
270 * but is not in the LRU cache. If it is in the cache, removeFromLRU should
273 void appendToLRU(LookupEntry*);
275 // searchable index that maps to entries in the heap
276 SkTDArray<LookupEntry*> fLookupTable;
279 SkTDArray<SkBitmapHeapEntry*> fStorage;
280 // Used to mark slots in fStorage as deleted without actually deleting
281 // the slot so as not to mess up the numbering.
282 SkTDArray<int> fUnusedSlots;
283 ExternalStorage* fExternalStorage;
285 LookupEntry* fMostRecentlyUsed;
286 LookupEntry* fLeastRecentlyUsed;
288 const int32_t fPreferredCount;
289 const int32_t fOwnerCount;
290 size_t fBytesAllocated;
292 bool fDeferAddingOwners;
293 SkTDArray<int> fDeferredEntries;
295 typedef SkBitmapHeapReader INHERITED;
298 #endif // SkBitmapHeap_DEFINED