blob: 8610691a43f04fa9f24facf28fe3bd036c1cc001 [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
robertphillips3d533ac2014-07-20 09:40:00 -070020class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::AtlasEntry,
21 GrTextureStripAtlas::AtlasEntry::Key> {};
22
rileya@google.com2e2aedc2012-08-13 20:28:48 +000023int32_t GrTextureStripAtlas::gCacheCount = 0;
24
robertphillips3d533ac2014-07-20 09:40:00 -070025GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = NULL;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000026
robertphillips3d533ac2014-07-20 09:40:00 -070027GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000028
29 if (NULL == gAtlasCache) {
robertphillips3d533ac2014-07-20 09:40:00 -070030 gAtlasCache = SkNEW(Hash);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000031 }
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) {
bsalomon49f085d2014-09-05 13:34:00 -070038 SkASSERT(info);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000039
40 AtlasEntry* entry = static_cast<AtlasEntry*>(info);
41
42 // remove the cache entry
robertphillips3d533ac2014-07-20 09:40:00 -070043 GetCache()->remove(entry->fKey);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000044
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) {
robertphillips3d533ac2014-07-20 09:40:00 -070055 AtlasEntry::Key key;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000056 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
robertphillips3d533ac2014-07-20 09:40:00 -070066 GetCache()->add(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();
bsalomonc6327a82014-10-27 12:53:08 -070083 fNormalizedYHeight = SK_Scalar1 / fDesc.fHeight;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000084 VALIDATE;
85}
86
87GrTextureStripAtlas::~GrTextureStripAtlas() {
88 SkDELETE_ARRAY(fRows);
89}
90
91int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
92 VALIDATE;
93 if (0 == fLockedRows) {
94 this->lockTexture();
joshualitt5f5a8d72015-02-25 14:09:45 -080095 if (!fTexture) {
96 return -1;
97 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000098 }
99
100 int key = data.getGenerationID();
101 int rowNumber = -1;
102 int index = this->searchByKey(key);
103
104 if (index >= 0) {
105 // We already have the data in a row, so we can just return that row
106 AtlasRow* row = fKeyTable[index];
107 if (0 == row->fLocks) {
108 this->removeFromLRU(row);
109 }
110 ++row->fLocks;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000111 ++fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000112
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113 // Since all the rows are always stored in a contiguous array, we can save the memory
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000114 // required for storing row numbers and just compute it with some pointer arithmetic
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000116 } else {
117 // ~index is the index where we will insert the new key to keep things sorted
118 index = ~index;
119
120 // We don't have this data cached, so pick the least recently used row to copy into
121 AtlasRow* row = this->getLRU();
122
rileya@google.comb3e50f22012-08-20 17:43:08 +0000123 ++fLockedRows;
124
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000125 if (NULL == row) {
126 // force a flush, which should unlock all the rows; then try again
127 fDesc.fContext->flush();
128 row = this->getLRU();
129 if (NULL == row) {
rileya@google.comb3e50f22012-08-20 17:43:08 +0000130 --fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000131 return -1;
132 }
133 }
134
135 this->removeFromLRU(row);
136
137 uint32_t oldKey = row->fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000138
139 // If we are writing into a row that already held bitmap data, we need to remove the
140 // reference to that genID which is stored in our sorted table of key values.
141 if (oldKey != kEmptyAtlasRowKey) {
142
143 // Find the entry in the list; if it's before the index where we plan on adding the new
144 // entry, we decrement since it will shift elements ahead of it back by one.
145 int oldIndex = this->searchByKey(oldKey);
rileya@google.comb3e50f22012-08-20 17:43:08 +0000146 if (oldIndex < index) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000147 --index;
148 }
149
150 fKeyTable.remove(oldIndex);
151 }
152
rileya@google.comb3e50f22012-08-20 17:43:08 +0000153 row->fKey = key;
154 row->fLocks = 1;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000155 fKeyTable.insert(index, 1, &row);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000156 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000157
158 SkAutoLockPixels lock(data);
159
160 // Pass in the kDontFlush flag, since we know we're writing to a part of this texture
161 // that is not currently in use
bsalomon81beccc2014-10-13 12:32:55 -0700162 fTexture->writePixels(0, rowNumber * fDesc.fRowHeight,
163 fDesc.fWidth, fDesc.fRowHeight,
164 SkImageInfo2GrPixelConfig(data.info()),
165 data.getPixels(),
166 data.rowBytes(),
167 GrContext::kDontFlush_PixelOpsFlag);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000168 }
169
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000170 SkASSERT(rowNumber >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000171 VALIDATE;
172 return rowNumber;
173}
174
175void GrTextureStripAtlas::unlockRow(int row) {
176 VALIDATE;
177 --fRows[row].fLocks;
178 --fLockedRows;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000179 SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000180 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() {
bsalomonf2703d82014-10-28 14:33:06 -0700196 GrSurfaceDesc texDesc;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000197 texDesc.fWidth = fDesc.fWidth;
198 texDesc.fHeight = fDesc.fHeight;
199 texDesc.fConfig = fDesc.fConfig;
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +0000200
bsalomon8718aaf2015-02-19 07:24:21 -0800201 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
202 GrUniqueKey key;
203 GrUniqueKey::Builder builder(&key, kDomain, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800204 builder[0] = static_cast<uint32_t>(fCacheKey);
205 builder.finish();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000206
bsalomond309e7a2015-04-30 14:18:54 -0700207 fTexture = fDesc.fContext->textureProvider()->findAndRefTextureByUniqueKey(key);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000208 if (NULL == fTexture) {
bsalomond309e7a2015-04-30 14:18:54 -0700209 fTexture = fDesc.fContext->textureProvider()->createTexture(texDesc, true, NULL, 0);
joshualitt5f5a8d72015-02-25 14:09:45 -0800210 if (!fTexture) {
211 return;
212 }
bsalomond309e7a2015-04-30 14:18:54 -0700213 fDesc.fContext->textureProvider()->assignUniqueKeyToTexture(key, fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000214 // This is a new texture, so all of our cache info is now invalid
215 this->initLRU();
216 fKeyTable.rewind();
217 }
bsalomon49f085d2014-09-05 13:34:00 -0700218 SkASSERT(fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000219}
220
221void GrTextureStripAtlas::unlockTexture() {
bsalomon49f085d2014-09-05 13:34:00 -0700222 SkASSERT(fTexture && 0 == fLockedRows);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000223 fTexture->unref();
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000224 fTexture = NULL;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000225}
226
227void GrTextureStripAtlas::initLRU() {
228 fLRUFront = NULL;
229 fLRUBack = NULL;
230 // Initially all the rows are in the LRU list
231 for (int i = 0; i < fNumRows; ++i) {
232 fRows[i].fKey = kEmptyAtlasRowKey;
233 fRows[i].fNext = NULL;
234 fRows[i].fPrev = NULL;
235 this->appendLRU(fRows + i);
236 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000237 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
238 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000239}
240
241void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000242 SkASSERT(NULL == row->fPrev && NULL == row->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000243 if (NULL == fLRUFront && NULL == fLRUBack) {
244 fLRUFront = row;
245 fLRUBack = row;
246 } else {
247 row->fPrev = fLRUBack;
248 fLRUBack->fNext = row;
249 fLRUBack = row;
250 }
251}
252
253void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
bsalomon49f085d2014-09-05 13:34:00 -0700254 SkASSERT(row);
255 if (row->fNext && row->fPrev) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000256 row->fPrev->fNext = row->fNext;
257 row->fNext->fPrev = row->fPrev;
258 } else {
259 if (NULL == row->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000260 SkASSERT(row == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000261 fLRUBack = row->fPrev;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000262 if (fLRUBack) {
263 fLRUBack->fNext = NULL;
264 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000265 }
266 if (NULL == row->fPrev) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000267 SkASSERT(row == fLRUFront);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000268 fLRUFront = row->fNext;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000269 if (fLRUFront) {
270 fLRUFront->fPrev = NULL;
271 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000273 }
274 row->fNext = NULL;
275 row->fPrev = NULL;
276}
277
278int GrTextureStripAtlas::searchByKey(uint32_t key) {
279 AtlasRow target;
280 target.fKey = key;
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000281 return SkTSearch<const AtlasRow,
282 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(),
283 fKeyTable.count(),
284 &target,
285 sizeof(AtlasRow*));
skia.committer@gmail.com845220b2013-05-20 11:51:35 +0000286}
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000287
288#ifdef SK_DEBUG
289void GrTextureStripAtlas::validate() {
290
291 // Our key table should be sorted
292 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
293 for (int i = 1; i < fKeyTable.count(); ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000294 SkASSERT(prev < fKeyTable[i]->fKey);
295 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000296 prev = fKeyTable[i]->fKey;
297 }
298
299 int lruCount = 0;
300 // Validate LRU pointers, and count LRU entries
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000301 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
302 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000303 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
304 if (NULL == r->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000305 SkASSERT(r == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000306 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000307 SkASSERT(r->fNext->fPrev == r);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000308 }
309 ++lruCount;
310 }
311
312 int rowLocks = 0;
313 int freeRows = 0;
314
315 for (int i = 0; i < fNumRows; ++i) {
316 rowLocks += fRows[i].fLocks;
317 if (0 == fRows[i].fLocks) {
318 ++freeRows;
319 bool inLRU = false;
320 // Step through the LRU and make sure it's present
321 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
322 if (r == &fRows[i]) {
323 inLRU = true;
324 break;
325 }
326 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000327 SkASSERT(inLRU);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000328 } else {
329 // If we are locked, we should have a key
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000330 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000331 }
332
333 // If we have a key != kEmptyAtlasRowKey, it should be in the key table
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000334 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000335 }
336
rileya@google.comb3e50f22012-08-20 17:43:08 +0000337 // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
338 // in which case we'll have one more lock than recorded in the rows (to represent the pending
339 // lock of a row; which ensures we don't unlock the texture prematurely).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000341
342 // We should have one lru entry for each free row
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000343 SkASSERT(freeRows == lruCount);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000344
345 // If we have locked rows, we should have a locked texture, otherwise
346 // it should be unlocked
347 if (fLockedRows == 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000348 SkASSERT(NULL == fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000349 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700350 SkASSERT(fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000351 }
352}
353#endif