blob: 877215ecea259a7682aa8461660bc791b5af3465 [file] [log] [blame]
rileya@google.com2e2aedc2012-08-13 20:28:48 +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
9#include "GrTextureStripAtlas.h"
10#include "SkPixelRef.h"
11#include "SkTSearch.h"
rileya@google.com2e2aedc2012-08-13 20:28:48 +000012#include "GrTexture.h"
13
14#ifdef SK_DEBUG
15 #define VALIDATE this->validate()
16#else
17 #define VALIDATE
18#endif
19
rileya@google.com2e2aedc2012-08-13 20:28:48 +000020int32_t GrTextureStripAtlas::gCacheCount = 0;
21
skia.committer@gmail.com63e0ffd2012-09-25 02:01:21 +000022GrTHashTable<GrTextureStripAtlas::AtlasEntry,
23 GrTextureStripAtlas::AtlasHashKey, 8>*
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000024 GrTextureStripAtlas::gAtlasCache = NULL;
25
26GrTHashTable<GrTextureStripAtlas::AtlasEntry, GrTextureStripAtlas::AtlasHashKey, 8>*
27GrTextureStripAtlas::GetCache() {
28
29 if (NULL == gAtlasCache) {
30 gAtlasCache = SkNEW((GrTHashTable<AtlasEntry, AtlasHashKey, 8>));
31 }
32
33 return gAtlasCache;
34}
35
36// Remove the specified atlas from the cache
sugoi@google.come0e385c2013-03-11 18:50:03 +000037void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000038 SkASSERT(NULL != info);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000039
40 AtlasEntry* entry = static_cast<AtlasEntry*>(info);
41
42 // remove the cache entry
43 GetCache()->remove(entry->fKey, entry);
44
45 // remove the actual entry
46 SkDELETE(entry);
47
48 if (0 == GetCache()->count()) {
49 SkDELETE(gAtlasCache);
50 gAtlasCache = NULL;
51 }
52}
rileya@google.com2e2aedc2012-08-13 20:28:48 +000053
rileya@google.com2e2aedc2012-08-13 20:28:48 +000054GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::Desc& desc) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +000055 AtlasHashKey key;
56 key.setKeyData(desc.asKey());
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000057 AtlasEntry* entry = GetCache()->find(key);
58 if (NULL == entry) {
59 entry = SkNEW(AtlasEntry);
60
rileya@google.com2e2aedc2012-08-13 20:28:48 +000061 entry->fAtlas = SkNEW_ARGS(GrTextureStripAtlas, (desc));
62 entry->fKey = key;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000063
64 desc.fContext->addCleanUp(CleanUp, entry);
65
66 GetCache()->insert(key, entry);
rileya@google.com2e2aedc2012-08-13 20:28:48 +000067 }
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000068
69 return entry->fAtlas;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000070}
71
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc)
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000073 : fCacheKey(sk_atomic_inc(&gCacheCount))
rileya@google.com2e2aedc2012-08-13 20:28:48 +000074 , fLockedRows(0)
75 , fDesc(desc)
76 , fNumRows(desc.fHeight / desc.fRowHeight)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000077 , fTexture(NULL)
rileya@google.com2e2aedc2012-08-13 20:28:48 +000078 , fRows(SkNEW_ARRAY(AtlasRow, fNumRows))
79 , fLRUFront(NULL)
80 , fLRUBack(NULL) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000081 SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight);
rileya@google.com2e2aedc2012-08-13 20:28:48 +000082 this->initLRU();
83 VALIDATE;
84}
85
86GrTextureStripAtlas::~GrTextureStripAtlas() {
87 SkDELETE_ARRAY(fRows);
88}
89
90int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
91 VALIDATE;
92 if (0 == fLockedRows) {
93 this->lockTexture();
94 }
95
96 int key = data.getGenerationID();
97 int rowNumber = -1;
98 int index = this->searchByKey(key);
99
100 if (index >= 0) {
101 // We already have the data in a row, so we can just return that row
102 AtlasRow* row = fKeyTable[index];
103 if (0 == row->fLocks) {
104 this->removeFromLRU(row);
105 }
106 ++row->fLocks;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000107 ++fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000108
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109 // Since all the rows are always stored in a contiguous array, we can save the memory
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000110 // required for storing row numbers and just compute it with some pointer arithmetic
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000111 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000112 } else {
113 // ~index is the index where we will insert the new key to keep things sorted
114 index = ~index;
115
116 // We don't have this data cached, so pick the least recently used row to copy into
117 AtlasRow* row = this->getLRU();
118
rileya@google.comb3e50f22012-08-20 17:43:08 +0000119 ++fLockedRows;
120
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000121 if (NULL == row) {
122 // force a flush, which should unlock all the rows; then try again
123 fDesc.fContext->flush();
124 row = this->getLRU();
125 if (NULL == row) {
rileya@google.comb3e50f22012-08-20 17:43:08 +0000126 --fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000127 return -1;
128 }
129 }
130
131 this->removeFromLRU(row);
132
133 uint32_t oldKey = row->fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000134
135 // If we are writing into a row that already held bitmap data, we need to remove the
136 // reference to that genID which is stored in our sorted table of key values.
137 if (oldKey != kEmptyAtlasRowKey) {
138
139 // Find the entry in the list; if it's before the index where we plan on adding the new
140 // entry, we decrement since it will shift elements ahead of it back by one.
141 int oldIndex = this->searchByKey(oldKey);
rileya@google.comb3e50f22012-08-20 17:43:08 +0000142 if (oldIndex < index) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000143 --index;
144 }
145
146 fKeyTable.remove(oldIndex);
147 }
148
rileya@google.comb3e50f22012-08-20 17:43:08 +0000149 row->fKey = key;
150 row->fLocks = 1;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000151 fKeyTable.insert(index, 1, &row);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000152 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000153
154 SkAutoLockPixels lock(data);
155
156 // Pass in the kDontFlush flag, since we know we're writing to a part of this texture
157 // that is not currently in use
bsalomon@google.com0342a852012-08-20 19:22:38 +0000158 fDesc.fContext->writeTexturePixels(fTexture,
159 0, rowNumber * fDesc.fRowHeight,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000160 fDesc.fWidth, fDesc.fRowHeight,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000161 SkBitmapConfig2GrPixelConfig(data.config()),
162 data.getPixels(),
163 data.rowBytes(),
164 GrContext::kDontFlush_PixelOpsFlag);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000165 }
166
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000167 SkASSERT(rowNumber >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000168 VALIDATE;
169 return rowNumber;
170}
171
172void GrTextureStripAtlas::unlockRow(int row) {
173 VALIDATE;
174 --fRows[row].fLocks;
175 --fLockedRows;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000176 SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000177 if (0 == fRows[row].fLocks) {
178 this->appendLRU(fRows + row);
179 }
180 if (0 == fLockedRows) {
181 this->unlockTexture();
182 }
183 VALIDATE;
184}
185
186GrTextureStripAtlas::AtlasRow* GrTextureStripAtlas::getLRU() {
187 // Front is least-recently-used
188 AtlasRow* row = fLRUFront;
189 return row;
190}
191
192void GrTextureStripAtlas::lockTexture() {
193 GrTextureParams params;
194 GrTextureDesc texDesc;
195 texDesc.fWidth = fDesc.fWidth;
196 texDesc.fHeight = fDesc.fHeight;
197 texDesc.fConfig = fDesc.fConfig;
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +0000198
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000199 static const GrCacheID::Domain gTextureStripAtlasDomain = GrCacheID::GenerateDomain();
200 GrCacheID::Key key;
201 *key.fData32 = fCacheKey;
202 memset(key.fData32 + 1, 0, sizeof(key) - sizeof(uint32_t));
203 GrCacheID cacheID(gTextureStripAtlasDomain, key);
204
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000205 fTexture = fDesc.fContext->findAndRefTexture(texDesc, cacheID, &params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000206 if (NULL == fTexture) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000207 fTexture = fDesc.fContext->createTexture(&params, texDesc, cacheID, NULL, 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000208 // This is a new texture, so all of our cache info is now invalid
209 this->initLRU();
210 fKeyTable.rewind();
211 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000212 SkASSERT(NULL != fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000213}
214
215void GrTextureStripAtlas::unlockTexture() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000216 SkASSERT(NULL != fTexture && 0 == fLockedRows);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000217 fTexture->unref();
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000218 fTexture = NULL;
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000219 fDesc.fContext->purgeCache();
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000220}
221
222void GrTextureStripAtlas::initLRU() {
223 fLRUFront = NULL;
224 fLRUBack = NULL;
225 // Initially all the rows are in the LRU list
226 for (int i = 0; i < fNumRows; ++i) {
227 fRows[i].fKey = kEmptyAtlasRowKey;
228 fRows[i].fNext = NULL;
229 fRows[i].fPrev = NULL;
230 this->appendLRU(fRows + i);
231 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000232 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
233 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000234}
235
236void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000237 SkASSERT(NULL == row->fPrev && NULL == row->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000238 if (NULL == fLRUFront && NULL == fLRUBack) {
239 fLRUFront = row;
240 fLRUBack = row;
241 } else {
242 row->fPrev = fLRUBack;
243 fLRUBack->fNext = row;
244 fLRUBack = row;
245 }
246}
247
248void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000249 SkASSERT(NULL != row);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000250 if (NULL != row->fNext && NULL != row->fPrev) {
251 row->fPrev->fNext = row->fNext;
252 row->fNext->fPrev = row->fPrev;
253 } else {
254 if (NULL == row->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000255 SkASSERT(row == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000256 fLRUBack = row->fPrev;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000257 if (fLRUBack) {
258 fLRUBack->fNext = NULL;
259 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000260 }
261 if (NULL == row->fPrev) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(row == fLRUFront);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000263 fLRUFront = row->fNext;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000264 if (fLRUFront) {
265 fLRUFront->fPrev = NULL;
266 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000267 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000268 }
269 row->fNext = NULL;
270 row->fPrev = NULL;
271}
272
273int GrTextureStripAtlas::searchByKey(uint32_t key) {
274 AtlasRow target;
275 target.fKey = key;
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000276 return SkTSearch<const AtlasRow,
277 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(),
278 fKeyTable.count(),
279 &target,
280 sizeof(AtlasRow*));
skia.committer@gmail.com845220b2013-05-20 11:51:35 +0000281}
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000282
283#ifdef SK_DEBUG
284void GrTextureStripAtlas::validate() {
285
286 // Our key table should be sorted
287 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
288 for (int i = 1; i < fKeyTable.count(); ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000289 SkASSERT(prev < fKeyTable[i]->fKey);
290 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000291 prev = fKeyTable[i]->fKey;
292 }
293
294 int lruCount = 0;
295 // Validate LRU pointers, and count LRU entries
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000296 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
297 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000298 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
299 if (NULL == r->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000300 SkASSERT(r == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000301 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000302 SkASSERT(r->fNext->fPrev == r);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000303 }
304 ++lruCount;
305 }
306
307 int rowLocks = 0;
308 int freeRows = 0;
309
310 for (int i = 0; i < fNumRows; ++i) {
311 rowLocks += fRows[i].fLocks;
312 if (0 == fRows[i].fLocks) {
313 ++freeRows;
314 bool inLRU = false;
315 // Step through the LRU and make sure it's present
316 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
317 if (r == &fRows[i]) {
318 inLRU = true;
319 break;
320 }
321 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000322 SkASSERT(inLRU);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000323 } else {
324 // If we are locked, we should have a key
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000325 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000326 }
327
328 // If we have a key != kEmptyAtlasRowKey, it should be in the key table
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000329 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000330 }
331
rileya@google.comb3e50f22012-08-20 17:43:08 +0000332 // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
333 // in which case we'll have one more lock than recorded in the rows (to represent the pending
334 // lock of a row; which ensures we don't unlock the texture prematurely).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000335 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000336
337 // We should have one lru entry for each free row
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000338 SkASSERT(freeRows == lruCount);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000339
340 // If we have locked rows, we should have a locked texture, otherwise
341 // it should be unlocked
342 if (fLockedRows == 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000343 SkASSERT(NULL == fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000344 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000345 SkASSERT(NULL != fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000346 }
347}
348#endif