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