blob: 92f5ad5c6f257ecc9c0dcc57cd189737a663a338 [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
20GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrTextureStripAtlas, GetTextureStripAtlasDomain)
21
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000022
rileya@google.com2e2aedc2012-08-13 20:28:48 +000023int32_t GrTextureStripAtlas::gCacheCount = 0;
24
skia.committer@gmail.com63e0ffd2012-09-25 02:01:21 +000025GrTHashTable<GrTextureStripAtlas::AtlasEntry,
26 GrTextureStripAtlas::AtlasHashKey, 8>*
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000027 GrTextureStripAtlas::gAtlasCache = NULL;
28
29GrTHashTable<GrTextureStripAtlas::AtlasEntry, GrTextureStripAtlas::AtlasHashKey, 8>*
30GrTextureStripAtlas::GetCache() {
31
32 if (NULL == gAtlasCache) {
33 gAtlasCache = SkNEW((GrTHashTable<AtlasEntry, AtlasHashKey, 8>));
34 }
35
36 return gAtlasCache;
37}
38
39// Remove the specified atlas from the cache
40void GrTextureStripAtlas::CleanUp(const GrContext* context, void* info) {
41 GrAssert(NULL != info);
42
43 AtlasEntry* entry = static_cast<AtlasEntry*>(info);
44
45 // remove the cache entry
46 GetCache()->remove(entry->fKey, entry);
47
48 // remove the actual entry
49 SkDELETE(entry);
50
51 if (0 == GetCache()->count()) {
52 SkDELETE(gAtlasCache);
53 gAtlasCache = NULL;
54 }
55}
rileya@google.com2e2aedc2012-08-13 20:28:48 +000056
rileya@google.com2e2aedc2012-08-13 20:28:48 +000057GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::Desc& desc) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +000058 AtlasHashKey key;
59 key.setKeyData(desc.asKey());
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000060 AtlasEntry* entry = GetCache()->find(key);
61 if (NULL == entry) {
62 entry = SkNEW(AtlasEntry);
63
rileya@google.com2e2aedc2012-08-13 20:28:48 +000064 entry->fAtlas = SkNEW_ARGS(GrTextureStripAtlas, (desc));
65 entry->fKey = key;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000066
67 desc.fContext->addCleanUp(CleanUp, entry);
68
69 GetCache()->insert(key, entry);
rileya@google.com2e2aedc2012-08-13 20:28:48 +000070 }
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000071
72 return entry->fAtlas;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000073}
74
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc)
rileya@google.comf61c7462012-08-13 21:03:39 +000076 : fCacheID(sk_atomic_inc(&gCacheCount))
rileya@google.com2e2aedc2012-08-13 20:28:48 +000077 , fLockedRows(0)
78 , fDesc(desc)
79 , fNumRows(desc.fHeight / desc.fRowHeight)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000080 , fTexture(NULL)
rileya@google.com2e2aedc2012-08-13 20:28:48 +000081 , fRows(SkNEW_ARRAY(AtlasRow, fNumRows))
82 , fLRUFront(NULL)
83 , fLRUBack(NULL) {
84 GrAssert(fNumRows * fDesc.fRowHeight == fDesc.fHeight);
85 this->initLRU();
86 VALIDATE;
87}
88
89GrTextureStripAtlas::~GrTextureStripAtlas() {
90 SkDELETE_ARRAY(fRows);
91}
92
93int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
94 VALIDATE;
95 if (0 == fLockedRows) {
96 this->lockTexture();
97 }
98
99 int key = data.getGenerationID();
100 int rowNumber = -1;
101 int index = this->searchByKey(key);
102
103 if (index >= 0) {
104 // We already have the data in a row, so we can just return that row
105 AtlasRow* row = fKeyTable[index];
106 if (0 == row->fLocks) {
107 this->removeFromLRU(row);
108 }
109 ++row->fLocks;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000110 ++fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000111
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000112 // Since all the rows are always stored in a contiguous array, we can save the memory
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000113 // required for storing row numbers and just compute it with some pointer arithmetic
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000114 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000115 } else {
116 // ~index is the index where we will insert the new key to keep things sorted
117 index = ~index;
118
119 // We don't have this data cached, so pick the least recently used row to copy into
120 AtlasRow* row = this->getLRU();
121
rileya@google.comb3e50f22012-08-20 17:43:08 +0000122 ++fLockedRows;
123
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000124 if (NULL == row) {
125 // force a flush, which should unlock all the rows; then try again
126 fDesc.fContext->flush();
127 row = this->getLRU();
128 if (NULL == row) {
rileya@google.comb3e50f22012-08-20 17:43:08 +0000129 --fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000130 return -1;
131 }
132 }
133
134 this->removeFromLRU(row);
135
136 uint32_t oldKey = row->fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000137
138 // If we are writing into a row that already held bitmap data, we need to remove the
139 // reference to that genID which is stored in our sorted table of key values.
140 if (oldKey != kEmptyAtlasRowKey) {
141
142 // Find the entry in the list; if it's before the index where we plan on adding the new
143 // entry, we decrement since it will shift elements ahead of it back by one.
144 int oldIndex = this->searchByKey(oldKey);
rileya@google.comb3e50f22012-08-20 17:43:08 +0000145 if (oldIndex < index) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000146 --index;
147 }
148
149 fKeyTable.remove(oldIndex);
150 }
151
rileya@google.comb3e50f22012-08-20 17:43:08 +0000152 row->fKey = key;
153 row->fLocks = 1;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000154 fKeyTable.insert(index, 1, &row);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000156
157 SkAutoLockPixels lock(data);
158
159 // Pass in the kDontFlush flag, since we know we're writing to a part of this texture
160 // that is not currently in use
bsalomon@google.com0342a852012-08-20 19:22:38 +0000161 fDesc.fContext->writeTexturePixels(fTexture,
162 0, rowNumber * fDesc.fRowHeight,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163 fDesc.fWidth, fDesc.fRowHeight,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000164 SkBitmapConfig2GrPixelConfig(data.config()),
165 data.getPixels(),
166 data.rowBytes(),
167 GrContext::kDontFlush_PixelOpsFlag);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000168 }
169
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000170 GrAssert(rowNumber >= 0);
171 VALIDATE;
172 return rowNumber;
173}
174
175void GrTextureStripAtlas::unlockRow(int row) {
176 VALIDATE;
177 --fRows[row].fLocks;
178 --fLockedRows;
179 GrAssert(fRows[row].fLocks >= 0 && fLockedRows >= 0);
180 if (0 == fRows[row].fLocks) {
181 this->appendLRU(fRows + row);
182 }
183 if (0 == fLockedRows) {
184 this->unlockTexture();
185 }
186 VALIDATE;
187}
188
189GrTextureStripAtlas::AtlasRow* GrTextureStripAtlas::getLRU() {
190 // Front is least-recently-used
191 AtlasRow* row = fLRUFront;
192 return row;
193}
194
195void GrTextureStripAtlas::lockTexture() {
196 GrTextureParams params;
197 GrTextureDesc texDesc;
198 texDesc.fWidth = fDesc.fWidth;
199 texDesc.fHeight = fDesc.fHeight;
200 texDesc.fConfig = fDesc.fConfig;
201 GrCacheData cacheData(fCacheID);
202 cacheData.fResourceDomain = GetTextureStripAtlasDomain();
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000203 fTexture = fDesc.fContext->findTexture(texDesc, cacheData, &params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000204 if (NULL == fTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000205 fTexture = fDesc.fContext->createTexture(&params, texDesc, cacheData, NULL, 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000206 // This is a new texture, so all of our cache info is now invalid
207 this->initLRU();
208 fKeyTable.rewind();
209 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000210 GrAssert(NULL != fTexture);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000211 fTexture->ref();
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000212}
213
214void GrTextureStripAtlas::unlockTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000215 GrAssert(NULL != fTexture && 0 == fLockedRows);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000216 fTexture->unref();
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000217 fTexture = NULL;
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000218 fDesc.fContext->purgeCache();
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000219}
220
221void GrTextureStripAtlas::initLRU() {
222 fLRUFront = NULL;
223 fLRUBack = NULL;
224 // Initially all the rows are in the LRU list
225 for (int i = 0; i < fNumRows; ++i) {
226 fRows[i].fKey = kEmptyAtlasRowKey;
227 fRows[i].fNext = NULL;
228 fRows[i].fPrev = NULL;
229 this->appendLRU(fRows + i);
230 }
231 GrAssert(NULL == fLRUFront->fPrev && NULL == fLRUBack->fNext);
232}
233
234void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
235 GrAssert(NULL == row->fPrev && NULL == row->fNext);
236 if (NULL == fLRUFront && NULL == fLRUBack) {
237 fLRUFront = row;
238 fLRUBack = row;
239 } else {
240 row->fPrev = fLRUBack;
241 fLRUBack->fNext = row;
242 fLRUBack = row;
243 }
244}
245
246void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
247 GrAssert(NULL != row);
248 if (NULL != row->fNext && NULL != row->fPrev) {
249 row->fPrev->fNext = row->fNext;
250 row->fNext->fPrev = row->fPrev;
251 } else {
252 if (NULL == row->fNext) {
253 GrAssert(row == fLRUBack);
254 fLRUBack = row->fPrev;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000255 if (fLRUBack) {
256 fLRUBack->fNext = NULL;
257 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000258 }
259 if (NULL == row->fPrev) {
260 GrAssert(row == fLRUFront);
261 fLRUFront = row->fNext;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000262 if (fLRUFront) {
263 fLRUFront->fPrev = NULL;
264 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000266 }
267 row->fNext = NULL;
268 row->fPrev = NULL;
269}
270
271int GrTextureStripAtlas::searchByKey(uint32_t key) {
272 AtlasRow target;
273 target.fKey = key;
274 return SkTSearch<AtlasRow, GrTextureStripAtlas::compareKeys>((const AtlasRow**)fKeyTable.begin(),
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000275 fKeyTable.count(),
276 &target,
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000277 sizeof(AtlasRow*));
278}
279
280#ifdef SK_DEBUG
281void GrTextureStripAtlas::validate() {
282
283 // Our key table should be sorted
284 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
285 for (int i = 1; i < fKeyTable.count(); ++i) {
286 GrAssert(prev < fKeyTable[i]->fKey);
287 GrAssert(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
288 prev = fKeyTable[i]->fKey;
289 }
290
291 int lruCount = 0;
292 // Validate LRU pointers, and count LRU entries
293 GrAssert(NULL == fLRUFront || NULL == fLRUFront->fPrev);
294 GrAssert(NULL == fLRUBack || NULL == fLRUBack->fNext);
295 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
296 if (NULL == r->fNext) {
297 GrAssert(r == fLRUBack);
298 } else {
299 GrAssert(r->fNext->fPrev == r);
300 }
301 ++lruCount;
302 }
303
304 int rowLocks = 0;
305 int freeRows = 0;
306
307 for (int i = 0; i < fNumRows; ++i) {
308 rowLocks += fRows[i].fLocks;
309 if (0 == fRows[i].fLocks) {
310 ++freeRows;
311 bool inLRU = false;
312 // Step through the LRU and make sure it's present
313 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
314 if (r == &fRows[i]) {
315 inLRU = true;
316 break;
317 }
318 }
319 GrAssert(inLRU);
320 } else {
321 // If we are locked, we should have a key
322 GrAssert(kEmptyAtlasRowKey != fRows[i].fKey);
323 }
324
325 // If we have a key != kEmptyAtlasRowKey, it should be in the key table
326 GrAssert(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
327 }
328
rileya@google.comb3e50f22012-08-20 17:43:08 +0000329 // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
330 // in which case we'll have one more lock than recorded in the rows (to represent the pending
331 // lock of a row; which ensures we don't unlock the texture prematurely).
332 GrAssert(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000333
334 // We should have one lru entry for each free row
335 GrAssert(freeRows == lruCount);
336
337 // If we have locked rows, we should have a locked texture, otherwise
338 // it should be unlocked
339 if (fLockedRows == 0) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000340 GrAssert(NULL == fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000341 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000342 GrAssert(NULL != fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000343 }
344}
345#endif
346