1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrTextureStripAtlas.h"
#include "SkPixelRef.h"
#include "SkTSearch.h"
#include "GrTexture.h"
#ifdef SK_DEBUG
#define VALIDATE this->validate()
#else
#define VALIDATE
#endif
int32_t GrTextureStripAtlas::gCacheCount = 0;
GrTHashTable<GrTextureStripAtlas::AtlasEntry,
GrTextureStripAtlas::AtlasHashKey, 8>*
GrTextureStripAtlas::gAtlasCache = NULL;
GrTHashTable<GrTextureStripAtlas::AtlasEntry, GrTextureStripAtlas::AtlasHashKey, 8>*
GrTextureStripAtlas::GetCache() {
if (NULL == gAtlasCache) {
gAtlasCache = SkNEW((GrTHashTable<AtlasEntry, AtlasHashKey, 8>));
}
return gAtlasCache;
}
// Remove the specified atlas from the cache
void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) {
SkASSERT(NULL != info);
AtlasEntry* entry = static_cast<AtlasEntry*>(info);
// remove the cache entry
GetCache()->remove(entry->fKey, entry);
// remove the actual entry
SkDELETE(entry);
if (0 == GetCache()->count()) {
SkDELETE(gAtlasCache);
gAtlasCache = NULL;
}
}
GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::Desc& desc) {
AtlasHashKey key;
key.setKeyData(desc.asKey());
AtlasEntry* entry = GetCache()->find(key);
if (NULL == entry) {
entry = SkNEW(AtlasEntry);
entry->fAtlas = SkNEW_ARGS(GrTextureStripAtlas, (desc));
entry->fKey = key;
desc.fContext->addCleanUp(CleanUp, entry);
GetCache()->insert(key, entry);
}
return entry->fAtlas;
}
GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc)
: fCacheKey(sk_atomic_inc(&gCacheCount))
, fLockedRows(0)
, fDesc(desc)
, fNumRows(desc.fHeight / desc.fRowHeight)
, fTexture(NULL)
, fRows(SkNEW_ARRAY(AtlasRow, fNumRows))
, fLRUFront(NULL)
, fLRUBack(NULL) {
SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight);
this->initLRU();
VALIDATE;
}
GrTextureStripAtlas::~GrTextureStripAtlas() {
SkDELETE_ARRAY(fRows);
}
int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
VALIDATE;
if (0 == fLockedRows) {
this->lockTexture();
}
int key = data.getGenerationID();
int rowNumber = -1;
int index = this->searchByKey(key);
if (index >= 0) {
// We already have the data in a row, so we can just return that row
AtlasRow* row = fKeyTable[index];
if (0 == row->fLocks) {
this->removeFromLRU(row);
}
++row->fLocks;
++fLockedRows;
// Since all the rows are always stored in a contiguous array, we can save the memory
// required for storing row numbers and just compute it with some pointer arithmetic
rowNumber = static_cast<int>(row - fRows);
} else {
// ~index is the index where we will insert the new key to keep things sorted
index = ~index;
// We don't have this data cached, so pick the least recently used row to copy into
AtlasRow* row = this->getLRU();
++fLockedRows;
if (NULL == row) {
// force a flush, which should unlock all the rows; then try again
fDesc.fContext->flush();
row = this->getLRU();
if (NULL == row) {
--fLockedRows;
return -1;
}
}
this->removeFromLRU(row);
uint32_t oldKey = row->fKey;
// If we are writing into a row that already held bitmap data, we need to remove the
// reference to that genID which is stored in our sorted table of key values.
if (oldKey != kEmptyAtlasRowKey) {
// Find the entry in the list; if it's before the index where we plan on adding the new
// entry, we decrement since it will shift elements ahead of it back by one.
int oldIndex = this->searchByKey(oldKey);
if (oldIndex < index) {
--index;
}
fKeyTable.remove(oldIndex);
}
row->fKey = key;
row->fLocks = 1;
fKeyTable.insert(index, 1, &row);
rowNumber = static_cast<int>(row - fRows);
SkAutoLockPixels lock(data);
// Pass in the kDontFlush flag, since we know we're writing to a part of this texture
// that is not currently in use
fDesc.fContext->writeTexturePixels(fTexture,
0, rowNumber * fDesc.fRowHeight,
fDesc.fWidth, fDesc.fRowHeight,
SkBitmapConfig2GrPixelConfig(data.config()),
data.getPixels(),
data.rowBytes(),
GrContext::kDontFlush_PixelOpsFlag);
}
SkASSERT(rowNumber >= 0);
VALIDATE;
return rowNumber;
}
void GrTextureStripAtlas::unlockRow(int row) {
VALIDATE;
--fRows[row].fLocks;
--fLockedRows;
SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0);
if (0 == fRows[row].fLocks) {
this->appendLRU(fRows + row);
}
if (0 == fLockedRows) {
this->unlockTexture();
}
VALIDATE;
}
GrTextureStripAtlas::AtlasRow* GrTextureStripAtlas::getLRU() {
// Front is least-recently-used
AtlasRow* row = fLRUFront;
return row;
}
void GrTextureStripAtlas::lockTexture() {
GrTextureParams params;
GrTextureDesc texDesc;
texDesc.fWidth = fDesc.fWidth;
texDesc.fHeight = fDesc.fHeight;
texDesc.fConfig = fDesc.fConfig;
static const GrCacheID::Domain gTextureStripAtlasDomain = GrCacheID::GenerateDomain();
GrCacheID::Key key;
*key.fData32 = fCacheKey;
memset(key.fData32 + 1, 0, sizeof(key) - sizeof(uint32_t));
GrCacheID cacheID(gTextureStripAtlasDomain, key);
fTexture = fDesc.fContext->findAndRefTexture(texDesc, cacheID, ¶ms);
if (NULL == fTexture) {
fTexture = fDesc.fContext->createTexture(¶ms, texDesc, cacheID, NULL, 0);
// This is a new texture, so all of our cache info is now invalid
this->initLRU();
fKeyTable.rewind();
}
SkASSERT(NULL != fTexture);
}
void GrTextureStripAtlas::unlockTexture() {
SkASSERT(NULL != fTexture && 0 == fLockedRows);
fTexture->unref();
fTexture = NULL;
fDesc.fContext->purgeCache();
}
void GrTextureStripAtlas::initLRU() {
fLRUFront = NULL;
fLRUBack = NULL;
// Initially all the rows are in the LRU list
for (int i = 0; i < fNumRows; ++i) {
fRows[i].fKey = kEmptyAtlasRowKey;
fRows[i].fNext = NULL;
fRows[i].fPrev = NULL;
this->appendLRU(fRows + i);
}
SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
}
void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
SkASSERT(NULL == row->fPrev && NULL == row->fNext);
if (NULL == fLRUFront && NULL == fLRUBack) {
fLRUFront = row;
fLRUBack = row;
} else {
row->fPrev = fLRUBack;
fLRUBack->fNext = row;
fLRUBack = row;
}
}
void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
SkASSERT(NULL != row);
if (NULL != row->fNext && NULL != row->fPrev) {
row->fPrev->fNext = row->fNext;
row->fNext->fPrev = row->fPrev;
} else {
if (NULL == row->fNext) {
SkASSERT(row == fLRUBack);
fLRUBack = row->fPrev;
if (fLRUBack) {
fLRUBack->fNext = NULL;
}
}
if (NULL == row->fPrev) {
SkASSERT(row == fLRUFront);
fLRUFront = row->fNext;
if (fLRUFront) {
fLRUFront->fPrev = NULL;
}
}
}
row->fNext = NULL;
row->fPrev = NULL;
}
int GrTextureStripAtlas::searchByKey(uint32_t key) {
AtlasRow target;
target.fKey = key;
return SkTSearch<const AtlasRow,
GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(),
fKeyTable.count(),
&target,
sizeof(AtlasRow*));
}
#ifdef SK_DEBUG
void GrTextureStripAtlas::validate() {
// Our key table should be sorted
uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
for (int i = 1; i < fKeyTable.count(); ++i) {
SkASSERT(prev < fKeyTable[i]->fKey);
SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
prev = fKeyTable[i]->fKey;
}
int lruCount = 0;
// Validate LRU pointers, and count LRU entries
SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
if (NULL == r->fNext) {
SkASSERT(r == fLRUBack);
} else {
SkASSERT(r->fNext->fPrev == r);
}
++lruCount;
}
int rowLocks = 0;
int freeRows = 0;
for (int i = 0; i < fNumRows; ++i) {
rowLocks += fRows[i].fLocks;
if (0 == fRows[i].fLocks) {
++freeRows;
bool inLRU = false;
// Step through the LRU and make sure it's present
for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
if (r == &fRows[i]) {
inLRU = true;
break;
}
}
SkASSERT(inLRU);
} else {
// If we are locked, we should have a key
SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey);
}
// If we have a key != kEmptyAtlasRowKey, it should be in the key table
SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
}
// Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
// in which case we'll have one more lock than recorded in the rows (to represent the pending
// lock of a row; which ensures we don't unlock the texture prematurely).
SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
// We should have one lru entry for each free row
SkASSERT(freeRows == lruCount);
// If we have locked rows, we should have a locked texture, otherwise
// it should be unlocked
if (fLockedRows == 0) {
SkASSERT(NULL == fTexture);
} else {
SkASSERT(NULL != fTexture);
}
}
#endif
|