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