blob: 39bc3899b392b38160cf3cec27458bbc0228dfeb [file] [log] [blame]
rileya@google.com2e2aedc2012-08-13 20:28:48 +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 */
7
8#include "GrTextureStripAtlas.h"
bsalomonf276ac52015-10-09 13:36:42 -07009#include "GrContext.h"
10#include "GrTexture.h"
11#include "SkGr.h"
rileya@google.com2e2aedc2012-08-13 20:28:48 +000012#include "SkPixelRef.h"
13#include "SkTSearch.h"
rileya@google.com2e2aedc2012-08-13 20:28:48 +000014
15#ifdef SK_DEBUG
16 #define VALIDATE this->validate()
17#else
18 #define VALIDATE
19#endif
20
cblume61214052016-01-26 09:10:48 -080021class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::AtlasEntry,
joshualitt690fc752015-07-13 12:49:13 -070022 GrTextureStripAtlas::Desc> {};
robertphillips3d533ac2014-07-20 09:40:00 -070023
rileya@google.com2e2aedc2012-08-13 20:28:48 +000024int32_t GrTextureStripAtlas::gCacheCount = 0;
25
halcanary96fcdcc2015-08-27 07:41:13 -070026GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = nullptr;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000027
robertphillips3d533ac2014-07-20 09:40:00 -070028GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000029
halcanary96fcdcc2015-08-27 07:41:13 -070030 if (nullptr == gAtlasCache) {
halcanary385fe4d2015-08-26 13:07:48 -070031 gAtlasCache = new Hash;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000032 }
33
34 return gAtlasCache;
35}
36
37// Remove the specified atlas from the cache
sugoi@google.come0e385c2013-03-11 18:50:03 +000038void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) {
bsalomon49f085d2014-09-05 13:34:00 -070039 SkASSERT(info);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000040
41 AtlasEntry* entry = static_cast<AtlasEntry*>(info);
42
43 // remove the cache entry
joshualitt690fc752015-07-13 12:49:13 -070044 GetCache()->remove(entry->fDesc);
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000045
46 // remove the actual entry
halcanary385fe4d2015-08-26 13:07:48 -070047 delete entry;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000048
49 if (0 == GetCache()->count()) {
halcanary385fe4d2015-08-26 13:07:48 -070050 delete gAtlasCache;
halcanary96fcdcc2015-08-27 07:41:13 -070051 gAtlasCache = nullptr;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000052 }
53}
rileya@google.com2e2aedc2012-08-13 20:28:48 +000054
rileya@google.com2e2aedc2012-08-13 20:28:48 +000055GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::Desc& desc) {
joshualitt690fc752015-07-13 12:49:13 -070056 AtlasEntry* entry = GetCache()->find(desc);
halcanary96fcdcc2015-08-27 07:41:13 -070057 if (nullptr == entry) {
halcanary385fe4d2015-08-26 13:07:48 -070058 entry = new AtlasEntry;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000059
halcanary385fe4d2015-08-26 13:07:48 -070060 entry->fAtlas = new GrTextureStripAtlas(desc);
joshualitt690fc752015-07-13 12:49:13 -070061 entry->fDesc = desc;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000062
63 desc.fContext->addCleanUp(CleanUp, entry);
64
robertphillips3d533ac2014-07-20 09:40:00 -070065 GetCache()->add(entry);
rileya@google.com2e2aedc2012-08-13 20:28:48 +000066 }
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000067
68 return entry->fAtlas;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000069}
70
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc)
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000072 : fCacheKey(sk_atomic_inc(&gCacheCount))
rileya@google.com2e2aedc2012-08-13 20:28:48 +000073 , fLockedRows(0)
74 , fDesc(desc)
75 , fNumRows(desc.fHeight / desc.fRowHeight)
halcanary96fcdcc2015-08-27 07:41:13 -070076 , fTexture(nullptr)
halcanary385fe4d2015-08-26 13:07:48 -070077 , fRows(new AtlasRow[fNumRows])
halcanary96fcdcc2015-08-27 07:41:13 -070078 , fLRUFront(nullptr)
79 , fLRUBack(nullptr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000080 SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight);
rileya@google.com2e2aedc2012-08-13 20:28:48 +000081 this->initLRU();
bsalomonc6327a82014-10-27 12:53:08 -070082 fNormalizedYHeight = SK_Scalar1 / fDesc.fHeight;
rileya@google.com2e2aedc2012-08-13 20:28:48 +000083 VALIDATE;
84}
85
halcanary385fe4d2015-08-26 13:07:48 -070086GrTextureStripAtlas::~GrTextureStripAtlas() { delete[] fRows; }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000087
88int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
89 VALIDATE;
90 if (0 == fLockedRows) {
91 this->lockTexture();
joshualitt5f5a8d72015-02-25 14:09:45 -080092 if (!fTexture) {
93 return -1;
94 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +000095 }
96
97 int key = data.getGenerationID();
98 int rowNumber = -1;
99 int index = this->searchByKey(key);
100
101 if (index >= 0) {
102 // We already have the data in a row, so we can just return that row
103 AtlasRow* row = fKeyTable[index];
104 if (0 == row->fLocks) {
105 this->removeFromLRU(row);
106 }
107 ++row->fLocks;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000108 ++fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000109
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000110 // Since all the rows are always stored in a contiguous array, we can save the memory
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000111 // required for storing row numbers and just compute it with some pointer arithmetic
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000112 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000113 } else {
114 // ~index is the index where we will insert the new key to keep things sorted
115 index = ~index;
116
117 // We don't have this data cached, so pick the least recently used row to copy into
118 AtlasRow* row = this->getLRU();
119
rileya@google.comb3e50f22012-08-20 17:43:08 +0000120 ++fLockedRows;
121
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == row) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000123 // force a flush, which should unlock all the rows; then try again
124 fDesc.fContext->flush();
125 row = this->getLRU();
halcanary96fcdcc2015-08-27 07:41:13 -0700126 if (nullptr == row) {
rileya@google.comb3e50f22012-08-20 17:43:08 +0000127 --fLockedRows;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000128 return -1;
129 }
130 }
131
132 this->removeFromLRU(row);
133
134 uint32_t oldKey = row->fKey;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000135
136 // If we are writing into a row that already held bitmap data, we need to remove the
137 // reference to that genID which is stored in our sorted table of key values.
138 if (oldKey != kEmptyAtlasRowKey) {
139
140 // Find the entry in the list; if it's before the index where we plan on adding the new
141 // entry, we decrement since it will shift elements ahead of it back by one.
142 int oldIndex = this->searchByKey(oldKey);
rileya@google.comb3e50f22012-08-20 17:43:08 +0000143 if (oldIndex < index) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000144 --index;
145 }
146
147 fKeyTable.remove(oldIndex);
148 }
149
rileya@google.comb3e50f22012-08-20 17:43:08 +0000150 row->fKey = key;
151 row->fLocks = 1;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000152 fKeyTable.insert(index, 1, &row);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000153 rowNumber = static_cast<int>(row - fRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000154
155 SkAutoLockPixels lock(data);
156
157 // Pass in the kDontFlush flag, since we know we're writing to a part of this texture
158 // that is not currently in use
bsalomon81beccc2014-10-13 12:32:55 -0700159 fTexture->writePixels(0, rowNumber * fDesc.fRowHeight,
160 fDesc.fWidth, fDesc.fRowHeight,
161 SkImageInfo2GrPixelConfig(data.info()),
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() {
bsalomonf2703d82014-10-28 14:33:06 -0700193 GrSurfaceDesc texDesc;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000194 texDesc.fWidth = fDesc.fWidth;
195 texDesc.fHeight = fDesc.fHeight;
196 texDesc.fConfig = fDesc.fConfig;
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +0000197
bsalomon8718aaf2015-02-19 07:24:21 -0800198 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
199 GrUniqueKey key;
200 GrUniqueKey::Builder builder(&key, kDomain, 1);
bsalomon24db3b12015-01-23 04:24:04 -0800201 builder[0] = static_cast<uint32_t>(fCacheKey);
202 builder.finish();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000203
bsalomond309e7a2015-04-30 14:18:54 -0700204 fTexture = fDesc.fContext->textureProvider()->findAndRefTextureByUniqueKey(key);
halcanary96fcdcc2015-08-27 07:41:13 -0700205 if (nullptr == fTexture) {
206 fTexture = fDesc.fContext->textureProvider()->createTexture(texDesc, true, nullptr, 0);
joshualitt5f5a8d72015-02-25 14:09:45 -0800207 if (!fTexture) {
208 return;
209 }
bsalomond309e7a2015-04-30 14:18:54 -0700210 fDesc.fContext->textureProvider()->assignUniqueKeyToTexture(key, fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000211 // This is a new texture, so all of our cache info is now invalid
212 this->initLRU();
213 fKeyTable.rewind();
214 }
bsalomon49f085d2014-09-05 13:34:00 -0700215 SkASSERT(fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000216}
217
218void GrTextureStripAtlas::unlockTexture() {
bsalomon49f085d2014-09-05 13:34:00 -0700219 SkASSERT(fTexture && 0 == fLockedRows);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000220 fTexture->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700221 fTexture = nullptr;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000222}
223
224void GrTextureStripAtlas::initLRU() {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 fLRUFront = nullptr;
226 fLRUBack = nullptr;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000227 // Initially all the rows are in the LRU list
228 for (int i = 0; i < fNumRows; ++i) {
229 fRows[i].fKey = kEmptyAtlasRowKey;
halcanary96fcdcc2015-08-27 07:41:13 -0700230 fRows[i].fNext = nullptr;
231 fRows[i].fPrev = nullptr;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000232 this->appendLRU(fRows + i);
233 }
halcanary96fcdcc2015-08-27 07:41:13 -0700234 SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev);
235 SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000236}
237
238void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
halcanary96fcdcc2015-08-27 07:41:13 -0700239 SkASSERT(nullptr == row->fPrev && nullptr == row->fNext);
240 if (nullptr == fLRUFront && nullptr == fLRUBack) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000241 fLRUFront = row;
242 fLRUBack = row;
243 } else {
244 row->fPrev = fLRUBack;
245 fLRUBack->fNext = row;
246 fLRUBack = row;
247 }
248}
249
250void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
bsalomon49f085d2014-09-05 13:34:00 -0700251 SkASSERT(row);
252 if (row->fNext && row->fPrev) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000253 row->fPrev->fNext = row->fNext;
254 row->fNext->fPrev = row->fPrev;
255 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700256 if (nullptr == row->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000257 SkASSERT(row == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000258 fLRUBack = row->fPrev;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000259 if (fLRUBack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700260 fLRUBack->fNext = nullptr;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000261 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000262 }
halcanary96fcdcc2015-08-27 07:41:13 -0700263 if (nullptr == row->fPrev) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000264 SkASSERT(row == fLRUFront);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000265 fLRUFront = row->fNext;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000266 if (fLRUFront) {
halcanary96fcdcc2015-08-27 07:41:13 -0700267 fLRUFront->fPrev = nullptr;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000268 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000269 }
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000270 }
halcanary96fcdcc2015-08-27 07:41:13 -0700271 row->fNext = nullptr;
272 row->fPrev = nullptr;
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000273}
274
275int GrTextureStripAtlas::searchByKey(uint32_t key) {
276 AtlasRow target;
277 target.fKey = key;
bsalomon@google.com20f7f172013-05-17 19:05:03 +0000278 return SkTSearch<const AtlasRow,
279 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(),
280 fKeyTable.count(),
281 &target,
282 sizeof(AtlasRow*));
skia.committer@gmail.com845220b2013-05-20 11:51:35 +0000283}
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000284
285#ifdef SK_DEBUG
286void GrTextureStripAtlas::validate() {
287
288 // Our key table should be sorted
289 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
290 for (int i = 1; i < fKeyTable.count(); ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000291 SkASSERT(prev < fKeyTable[i]->fKey);
292 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000293 prev = fKeyTable[i]->fKey;
294 }
295
296 int lruCount = 0;
297 // Validate LRU pointers, and count LRU entries
halcanary96fcdcc2015-08-27 07:41:13 -0700298 SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev);
299 SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext);
300 for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) {
301 if (nullptr == r->fNext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000302 SkASSERT(r == fLRUBack);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000303 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000304 SkASSERT(r->fNext->fPrev == r);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000305 }
306 ++lruCount;
307 }
308
309 int rowLocks = 0;
310 int freeRows = 0;
311
312 for (int i = 0; i < fNumRows; ++i) {
313 rowLocks += fRows[i].fLocks;
314 if (0 == fRows[i].fLocks) {
315 ++freeRows;
316 bool inLRU = false;
317 // Step through the LRU and make sure it's present
halcanary96fcdcc2015-08-27 07:41:13 -0700318 for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) {
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000319 if (r == &fRows[i]) {
320 inLRU = true;
321 break;
322 }
323 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000324 SkASSERT(inLRU);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000325 } else {
326 // If we are locked, we should have a key
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000327 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000328 }
329
330 // If we have a key != kEmptyAtlasRowKey, it should be in the key table
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000331 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000332 }
333
rileya@google.comb3e50f22012-08-20 17:43:08 +0000334 // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
335 // in which case we'll have one more lock than recorded in the rows (to represent the pending
336 // lock of a row; which ensures we don't unlock the texture prematurely).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000337 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000338
339 // We should have one lru entry for each free row
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(freeRows == lruCount);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000341
342 // If we have locked rows, we should have a locked texture, otherwise
343 // it should be unlocked
344 if (fLockedRows == 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700345 SkASSERT(nullptr == fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000346 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700347 SkASSERT(fTexture);
rileya@google.com2e2aedc2012-08-13 20:28:48 +0000348 }
349}
350#endif