3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "GrTextureStripAtlas.h"
10 #include "SkPixelRef.h"
11 #include "SkTSearch.h"
12 #include "GrTexture.h"
15 #define VALIDATE this->validate()
20 class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::AtlasEntry,
21 GrTextureStripAtlas::AtlasEntry::Key> {};
23 int32_t GrTextureStripAtlas::gCacheCount = 0;
25 GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = NULL;
27 GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() {
29 if (NULL == gAtlasCache) {
30 gAtlasCache = SkNEW(Hash);
36 // Remove the specified atlas from the cache
37 void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) {
40 AtlasEntry* entry = static_cast<AtlasEntry*>(info);
42 // remove the cache entry
43 GetCache()->remove(entry->fKey);
45 // remove the actual entry
48 if (0 == GetCache()->count()) {
49 SkDELETE(gAtlasCache);
54 GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::Desc& desc) {
56 key.setKeyData(desc.asKey());
57 AtlasEntry* entry = GetCache()->find(key);
59 entry = SkNEW(AtlasEntry);
61 entry->fAtlas = SkNEW_ARGS(GrTextureStripAtlas, (desc));
64 desc.fContext->addCleanUp(CleanUp, entry);
66 GetCache()->add(entry);
72 GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc)
73 : fCacheKey(sk_atomic_inc(&gCacheCount))
76 , fNumRows(desc.fHeight / desc.fRowHeight)
78 , fRows(SkNEW_ARRAY(AtlasRow, fNumRows))
81 SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight);
86 GrTextureStripAtlas::~GrTextureStripAtlas() {
87 SkDELETE_ARRAY(fRows);
90 int GrTextureStripAtlas::lockRow(const SkBitmap& data) {
92 if (0 == fLockedRows) {
96 int key = data.getGenerationID();
98 int index = this->searchByKey(key);
101 // We already have the data in a row, so we can just return that row
102 AtlasRow* row = fKeyTable[index];
103 if (0 == row->fLocks) {
104 this->removeFromLRU(row);
109 // Since all the rows are always stored in a contiguous array, we can save the memory
110 // required for storing row numbers and just compute it with some pointer arithmetic
111 rowNumber = static_cast<int>(row - fRows);
113 // ~index is the index where we will insert the new key to keep things sorted
116 // We don't have this data cached, so pick the least recently used row to copy into
117 AtlasRow* row = this->getLRU();
122 // force a flush, which should unlock all the rows; then try again
123 fDesc.fContext->flush();
124 row = this->getLRU();
131 this->removeFromLRU(row);
133 uint32_t oldKey = row->fKey;
135 // If we are writing into a row that already held bitmap data, we need to remove the
136 // reference to that genID which is stored in our sorted table of key values.
137 if (oldKey != kEmptyAtlasRowKey) {
139 // Find the entry in the list; if it's before the index where we plan on adding the new
140 // entry, we decrement since it will shift elements ahead of it back by one.
141 int oldIndex = this->searchByKey(oldKey);
142 if (oldIndex < index) {
146 fKeyTable.remove(oldIndex);
151 fKeyTable.insert(index, 1, &row);
152 rowNumber = static_cast<int>(row - fRows);
154 SkAutoLockPixels lock(data);
156 // Pass in the kDontFlush flag, since we know we're writing to a part of this texture
157 // that is not currently in use
158 fDesc.fContext->writeTexturePixels(fTexture,
159 0, rowNumber * fDesc.fRowHeight,
160 fDesc.fWidth, fDesc.fRowHeight,
161 SkImageInfo2GrPixelConfig(data.info()),
164 GrContext::kDontFlush_PixelOpsFlag);
167 SkASSERT(rowNumber >= 0);
172 void GrTextureStripAtlas::unlockRow(int row) {
176 SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0);
177 if (0 == fRows[row].fLocks) {
178 this->appendLRU(fRows + row);
180 if (0 == fLockedRows) {
181 this->unlockTexture();
186 GrTextureStripAtlas::AtlasRow* GrTextureStripAtlas::getLRU() {
187 // Front is least-recently-used
188 AtlasRow* row = fLRUFront;
192 void GrTextureStripAtlas::lockTexture() {
193 GrTextureParams params;
194 GrTextureDesc texDesc;
195 texDesc.fWidth = fDesc.fWidth;
196 texDesc.fHeight = fDesc.fHeight;
197 texDesc.fConfig = fDesc.fConfig;
199 static const GrCacheID::Domain gTextureStripAtlasDomain = GrCacheID::GenerateDomain();
201 *key.fData32 = fCacheKey;
202 memset(key.fData32 + 1, 0, sizeof(key) - sizeof(uint32_t));
203 GrCacheID cacheID(gTextureStripAtlasDomain, key);
205 fTexture = fDesc.fContext->findAndRefTexture(texDesc, cacheID, ¶ms);
206 if (NULL == fTexture) {
207 fTexture = fDesc.fContext->createTexture(¶ms, texDesc, cacheID, NULL, 0);
208 // This is a new texture, so all of our cache info is now invalid
215 void GrTextureStripAtlas::unlockTexture() {
216 SkASSERT(fTexture && 0 == fLockedRows);
219 fDesc.fContext->purgeCache();
222 void GrTextureStripAtlas::initLRU() {
225 // Initially all the rows are in the LRU list
226 for (int i = 0; i < fNumRows; ++i) {
227 fRows[i].fKey = kEmptyAtlasRowKey;
228 fRows[i].fNext = NULL;
229 fRows[i].fPrev = NULL;
230 this->appendLRU(fRows + i);
232 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
233 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
236 void GrTextureStripAtlas::appendLRU(AtlasRow* row) {
237 SkASSERT(NULL == row->fPrev && NULL == row->fNext);
238 if (NULL == fLRUFront && NULL == fLRUBack) {
242 row->fPrev = fLRUBack;
243 fLRUBack->fNext = row;
248 void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) {
250 if (row->fNext && row->fPrev) {
251 row->fPrev->fNext = row->fNext;
252 row->fNext->fPrev = row->fPrev;
254 if (NULL == row->fNext) {
255 SkASSERT(row == fLRUBack);
256 fLRUBack = row->fPrev;
258 fLRUBack->fNext = NULL;
261 if (NULL == row->fPrev) {
262 SkASSERT(row == fLRUFront);
263 fLRUFront = row->fNext;
265 fLRUFront->fPrev = NULL;
273 int GrTextureStripAtlas::searchByKey(uint32_t key) {
276 return SkTSearch<const AtlasRow,
277 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(),
284 void GrTextureStripAtlas::validate() {
286 // Our key table should be sorted
287 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey;
288 for (int i = 1; i < fKeyTable.count(); ++i) {
289 SkASSERT(prev < fKeyTable[i]->fKey);
290 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey);
291 prev = fKeyTable[i]->fKey;
295 // Validate LRU pointers, and count LRU entries
296 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev);
297 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext);
298 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
299 if (NULL == r->fNext) {
300 SkASSERT(r == fLRUBack);
302 SkASSERT(r->fNext->fPrev == r);
310 for (int i = 0; i < fNumRows; ++i) {
311 rowLocks += fRows[i].fLocks;
312 if (0 == fRows[i].fLocks) {
315 // Step through the LRU and make sure it's present
316 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) {
317 if (r == &fRows[i]) {
324 // If we are locked, we should have a key
325 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey);
328 // If we have a key != kEmptyAtlasRowKey, it should be in the key table
329 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0);
332 // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed,
333 // in which case we'll have one more lock than recorded in the rows (to represent the pending
334 // lock of a row; which ensures we don't unlock the texture prematurely).
335 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows);
337 // We should have one lru entry for each free row
338 SkASSERT(freeRows == lruCount);
340 // If we have locked rows, we should have a locked texture, otherwise
341 // it should be unlocked
342 if (fLockedRows == 0) {
343 SkASSERT(NULL == fTexture);