blob: 1e1979fbf7d85f3fbdd405e12e754371dc1766f8 [file] [log] [blame]
djsollen@google.com21830d92012-08-07 19:49:41 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bungemand3ebb482015-08-05 13:57:49 -07007
djsollen@google.com21830d92012-08-07 19:49:41 +00008#ifndef SkBitmapHeap_DEFINED
9#define SkBitmapHeap_DEFINED
10
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkAtomics.h"
djsollen@google.com21830d92012-08-07 19:49:41 +000012#include "SkBitmap.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPoint.h"
djsollen@google.com21830d92012-08-07 19:49:41 +000014#include "SkRefCnt.h"
15#include "SkTDArray.h"
bungemand3ebb482015-08-05 13:57:49 -070016#include "SkTypes.h"
djsollen@google.com21830d92012-08-07 19:49:41 +000017
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;
scroggo@google.com013c5d92012-11-16 20:34:37 +000050 friend class SkBitmapHeapTester;
djsollen@google.com21830d92012-08-07 19:49:41 +000051};
52
53
54class SkBitmapHeapReader : public SkRefCnt {
55public:
mtklein1b249332015-07-07 12:21:21 -070056
robertphillips@google.coma22e2112012-08-16 14:58:06 +000057
djsollen@google.com21830d92012-08-07 19:49:41 +000058 SkBitmapHeapReader() : INHERITED() {}
59 virtual SkBitmap* getBitmap(int32_t slot) const = 0;
60 virtual void releaseRef(int32_t slot) = 0;
61private:
62 typedef SkRefCnt INHERITED;
63};
64
65
66/**
67 * TODO: stores immutable bitmaps into a heap
68 */
69class SkBitmapHeap : public SkBitmapHeapReader {
70public:
71 class ExternalStorage : public SkRefCnt {
72 public:
mtklein1b249332015-07-07 12:21:21 -070073
robertphillips@google.coma22e2112012-08-16 14:58:06 +000074
djsollen@google.com21830d92012-08-07 19:49:41 +000075 virtual bool insert(const SkBitmap& bitmap, int32_t slot) = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000076
77 private:
78 typedef SkRefCnt INHERITED;
djsollen@google.com21830d92012-08-07 19:49:41 +000079 };
80
81 static const int32_t UNLIMITED_SIZE = -1;
82 static const int32_t IGNORE_OWNERS = -1;
83 static const int32_t INVALID_SLOT = -1;
84
85 /**
86 * Constructs a heap that is responsible for allocating and managing its own storage. In the
87 * case where we choose to allow the heap to grow indefinitely (i.e. UNLIMITED_SIZE) we
88 * guarantee that once allocated in the heap a bitmap's index in the heap is immutable.
89 * Otherwise we guarantee the bitmaps placement in the heap until its owner count goes to zero.
90 *
91 * @param preferredSize Specifies the preferred maximum number of bitmaps to store. This is
92 * not a hard limit as it can grow larger if the number of bitmaps in the heap with active
93 * owners exceeds this limit.
94 * @param ownerCount The number of owners to assign to each inserted bitmap. NOTE: while a
95 * bitmap in the heap has a least one owner it can't be removed.
96 */
97 SkBitmapHeap(int32_t preferredSize = UNLIMITED_SIZE, int32_t ownerCount = IGNORE_OWNERS);
98
99 /**
100 * Constructs a heap that defers the responsibility of storing the bitmaps to an external
101 * function. This is especially useful if the bitmaps will be used in a separate process as the
102 * external storage can ensure the data is properly shuttled to the appropriate processes.
103 *
104 * Our LRU implementation assumes that inserts into the external storage are consumed in the
105 * order that they are inserted (i.e. SkPipe). This ensures that we don't need to query the
106 * external storage to see if a slot in the heap is eligible to be overwritten.
107 *
108 * @param externalStorage The class responsible for storing the bitmaps inserted into the heap
109 * @param heapSize The maximum size of the heap. Because of the sequential limitation imposed
110 * by our LRU implementation we can guarantee that the heap will never grow beyond this size.
111 */
112 SkBitmapHeap(ExternalStorage* externalStorage, int32_t heapSize = UNLIMITED_SIZE);
113
commit-bot@chromium.orgd7e0fbe2014-03-10 16:41:00 +0000114 virtual ~SkBitmapHeap();
djsollen@google.com21830d92012-08-07 19:49:41 +0000115
116 /**
djsollen@google.com21830d92012-08-07 19:49:41 +0000117 * Retrieves the bitmap from the specified slot in the heap
118 *
119 * @return The bitmap located at that slot or NULL if external storage is being used.
120 */
mtklein36352bf2015-03-25 18:17:31 -0700121 SkBitmap* getBitmap(int32_t slot) const override {
djsollen@google.com21830d92012-08-07 19:49:41 +0000122 SkASSERT(fExternalStorage == NULL);
123 SkBitmapHeapEntry* entry = getEntry(slot);
124 if (entry) {
125 return &entry->fBitmap;
126 }
127 return NULL;
128 }
129
130 /**
131 * Retrieves the bitmap from the specified slot in the heap
132 *
133 * @return The bitmap located at that slot or NULL if external storage is being used.
134 */
mtklein36352bf2015-03-25 18:17:31 -0700135 void releaseRef(int32_t slot) override {
djsollen@google.com21830d92012-08-07 19:49:41 +0000136 SkASSERT(fExternalStorage == NULL);
137 if (fOwnerCount != IGNORE_OWNERS) {
138 SkBitmapHeapEntry* entry = getEntry(slot);
139 if (entry) {
140 entry->releaseRef();
141 }
142 }
143 }
144
145 /**
146 * Inserts a bitmap into the heap. The stored version of bitmap is guaranteed to be immutable
147 * and is not dependent on the lifecycle of the provided bitmap.
148 *
149 * @param bitmap the bitmap to be inserted into the heap
150 * @return the slot in the heap where the bitmap is stored or INVALID_SLOT if the bitmap could
151 * not be added to the heap. If it was added the slot will remain valid...
152 * (1) indefinitely if no owner count has been specified.
153 * (2) until all owners have called releaseRef on the appropriate SkBitmapHeapEntry*
154 */
155 int32_t insert(const SkBitmap& bitmap);
156
157 /**
158 * Retrieves an entry from the heap at a given slot.
159 *
160 * @param slot the slot in the heap where a bitmap was stored.
161 * @return a SkBitmapHeapEntry that wraps the bitmap or NULL if external storage is used.
162 */
163 SkBitmapHeapEntry* getEntry(int32_t slot) const {
164 SkASSERT(slot <= fStorage.count());
165 if (fExternalStorage != NULL) {
166 return NULL;
167 }
168 return fStorage[slot];
169 }
170
171 /**
172 * Returns a count of the number of items currently in the heap
173 */
174 int count() const {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000175 SkASSERT(fExternalStorage != NULL ||
176 fStorage.count() - fUnusedSlots.count() == fLookupTable.count());
djsollen@google.com21830d92012-08-07 19:49:41 +0000177 return fLookupTable.count();
178 }
179
180 /**
181 * Returns the total number of bytes allocated by the bitmaps in the heap
182 */
183 size_t bytesAllocated() const {
184 return fBytesAllocated;
185 }
186
scroggo@google.com10dccde2012-08-08 20:43:22 +0000187 /**
188 * Attempt to reduce the storage allocated.
189 * @param bytesToFree minimum number of bytes that should be attempted to
190 * be freed.
191 * @return number of bytes actually freed.
192 */
193 size_t freeMemoryIfPossible(size_t bytesToFree);
194
scroggo@google.com7ca24432012-08-14 15:48:43 +0000195 /**
196 * Defer any increments of owner counts until endAddingOwnersDeferral is called. So if an
197 * existing SkBitmap is inserted into the SkBitmapHeap, its corresponding SkBitmapHeapEntry will
198 * not have addReferences called on it, and the client does not need to make a corresponding
199 * call to releaseRef. Only meaningful if this SkBitmapHeap was created with an owner count not
200 * equal to IGNORE_OWNERS.
201 */
202 void deferAddingOwners();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000203
scroggo@google.com7ca24432012-08-14 15:48:43 +0000204 /**
205 * Resume adding references when duplicate SkBitmaps are inserted.
206 * @param add If true, add references to the SkBitmapHeapEntrys whose SkBitmaps were re-inserted
207 * while deferring.
208 */
209 void endAddingOwnersDeferral(bool add);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
djsollen@google.com21830d92012-08-07 19:49:41 +0000211private:
212 struct LookupEntry {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000213 LookupEntry(const SkBitmap& bm)
214 : fGenerationId(bm.getGenerationID())
reed@google.com672588b2014-01-08 15:42:01 +0000215 , fPixelOrigin(bm.pixelRefOrigin())
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000216 , fWidth(bm.width())
217 , fHeight(bm.height())
218 , fMoreRecentlyUsed(NULL)
219 , fLessRecentlyUsed(NULL){}
220
221 const uint32_t fGenerationId; // SkPixelRef GenerationID.
reed@google.com672588b2014-01-08 15:42:01 +0000222 const SkIPoint fPixelOrigin;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000223 const uint32_t fWidth;
224 const uint32_t fHeight;
225
226 // TODO: Generalize the LRU caching mechanism
227 LookupEntry* fMoreRecentlyUsed;
228 LookupEntry* fLessRecentlyUsed;
djsollen@google.com21830d92012-08-07 19:49:41 +0000229
230 uint32_t fStorageSlot; // slot of corresponding bitmap in fStorage.
231
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000232 /**
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000233 * Compare two LookupEntry pointers for sorting and searching.
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000234 */
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000235 static bool Less(const LookupEntry& a, const LookupEntry& b);
djsollen@google.com21830d92012-08-07 19:49:41 +0000236 };
237
238 /**
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000239 * Remove the entry from the lookup table. Also deletes the entry pointed
240 * to by the table. Therefore, if a pointer to that one was passed in, the
241 * pointer should no longer be used, since the object to which it points has
242 * been deleted.
scroggo@google.com10dccde2012-08-08 20:43:22 +0000243 * @return The index in the lookup table of the entry before removal.
244 */
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000245 int removeEntryFromLookupTable(LookupEntry*);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000246
247 /**
djsollen@google.com21830d92012-08-07 19:49:41 +0000248 * Searches for the bitmap in the lookup table and returns the bitmaps index within the table.
249 * If the bitmap was not already in the table it is added.
250 *
scroggo@google.com10dccde2012-08-08 20:43:22 +0000251 * @param key The key to search the lookup table, created from a bitmap.
djsollen@google.com21830d92012-08-07 19:49:41 +0000252 * @param entry A pointer to a SkBitmapHeapEntry* that if non-null AND the bitmap is found
253 * in the lookup table is populated with the entry from the heap storage.
254 */
scroggo@google.com10dccde2012-08-08 20:43:22 +0000255 int findInLookupTable(const LookupEntry& key, SkBitmapHeapEntry** entry);
djsollen@google.com21830d92012-08-07 19:49:41 +0000256
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000257 LookupEntry* findEntryToReplace(const SkBitmap& replacement);
djsollen@google.com21830d92012-08-07 19:49:41 +0000258 bool copyBitmap(const SkBitmap& originalBitmap, SkBitmap& copiedBitmap);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000259
260 /**
261 * Remove a LookupEntry from the LRU, in preparation for either deleting or appending as most
262 * recent. Points the LookupEntry's old neighbors at each other, and sets fLeastRecentlyUsed
263 * (if there is still an entry left). Sets LookupEntry's fMoreRecentlyUsed to NULL and leaves
264 * its fLessRecentlyUsed unmodified.
265 */
266 void removeFromLRU(LookupEntry* entry);
267
268 /**
269 * Append a LookupEntry to the end of the LRU cache, marking it as the most
270 * recently used. Assumes that the LookupEntry is already in fLookupTable,
271 * but is not in the LRU cache. If it is in the cache, removeFromLRU should
272 * be called first.
273 */
274 void appendToLRU(LookupEntry*);
djsollen@google.com21830d92012-08-07 19:49:41 +0000275
276 // searchable index that maps to entries in the heap
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000277 SkTDArray<LookupEntry*> fLookupTable;
djsollen@google.com21830d92012-08-07 19:49:41 +0000278
279 // heap storage
280 SkTDArray<SkBitmapHeapEntry*> fStorage;
scroggo@google.com10dccde2012-08-08 20:43:22 +0000281 // Used to mark slots in fStorage as deleted without actually deleting
282 // the slot so as not to mess up the numbering.
283 SkTDArray<int> fUnusedSlots;
djsollen@google.com21830d92012-08-07 19:49:41 +0000284 ExternalStorage* fExternalStorage;
285
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000286 LookupEntry* fMostRecentlyUsed;
287 LookupEntry* fLeastRecentlyUsed;
djsollen@google.com21830d92012-08-07 19:49:41 +0000288
289 const int32_t fPreferredCount;
290 const int32_t fOwnerCount;
291 size_t fBytesAllocated;
292
scroggo@google.com7ca24432012-08-14 15:48:43 +0000293 bool fDeferAddingOwners;
294 SkTDArray<int> fDeferredEntries;
295
djsollen@google.com21830d92012-08-07 19:49:41 +0000296 typedef SkBitmapHeapReader INHERITED;
297};
298
299#endif // SkBitmapHeap_DEFINED