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