| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "GrTextureStripAtlas.h" | 9 #include "GrTextureStripAtlas.h" |
| 10 #include "SkPixelRef.h" | 10 #include "SkPixelRef.h" |
| 11 #include "SkTSearch.h" | 11 #include "SkTSearch.h" |
| 12 #include "GrTexture.h" | 12 #include "GrTexture.h" |
| 13 | 13 |
| 14 #ifdef SK_DEBUG | 14 #ifdef SK_DEBUG |
| 15 #define VALIDATE this->validate() | 15 #define VALIDATE this->validate() |
| 16 #else | 16 #else |
| 17 #define VALIDATE | 17 #define VALIDATE |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::Atl
asEntry, | 20 class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::Atl
asEntry, |
| 21 GrTextureStripAtlas::Des
c> {}; | 21 GrTextureStripAtlas::Des
c> {}; |
| 22 | 22 |
| 23 int32_t GrTextureStripAtlas::gCacheCount = 0; | 23 int32_t GrTextureStripAtlas::gCacheCount = 0; |
| 24 | 24 |
| 25 GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = NULL; | 25 GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = nullptr; |
| 26 | 26 |
| 27 GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() { | 27 GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() { |
| 28 | 28 |
| 29 if (NULL == gAtlasCache) { | 29 if (nullptr == gAtlasCache) { |
| 30 gAtlasCache = new Hash; | 30 gAtlasCache = new Hash; |
| 31 } | 31 } |
| 32 | 32 |
| 33 return gAtlasCache; | 33 return gAtlasCache; |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Remove the specified atlas from the cache | 36 // Remove the specified atlas from the cache |
| 37 void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) { | 37 void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) { |
| 38 SkASSERT(info); | 38 SkASSERT(info); |
| 39 | 39 |
| 40 AtlasEntry* entry = static_cast<AtlasEntry*>(info); | 40 AtlasEntry* entry = static_cast<AtlasEntry*>(info); |
| 41 | 41 |
| 42 // remove the cache entry | 42 // remove the cache entry |
| 43 GetCache()->remove(entry->fDesc); | 43 GetCache()->remove(entry->fDesc); |
| 44 | 44 |
| 45 // remove the actual entry | 45 // remove the actual entry |
| 46 delete entry; | 46 delete entry; |
| 47 | 47 |
| 48 if (0 == GetCache()->count()) { | 48 if (0 == GetCache()->count()) { |
| 49 delete gAtlasCache; | 49 delete gAtlasCache; |
| 50 gAtlasCache = NULL; | 50 gAtlasCache = nullptr; |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::De
sc& desc) { | 54 GrTextureStripAtlas* GrTextureStripAtlas::GetAtlas(const GrTextureStripAtlas::De
sc& desc) { |
| 55 AtlasEntry* entry = GetCache()->find(desc); | 55 AtlasEntry* entry = GetCache()->find(desc); |
| 56 if (NULL == entry) { | 56 if (nullptr == entry) { |
| 57 entry = new AtlasEntry; | 57 entry = new AtlasEntry; |
| 58 | 58 |
| 59 entry->fAtlas = new GrTextureStripAtlas(desc); | 59 entry->fAtlas = new GrTextureStripAtlas(desc); |
| 60 entry->fDesc = desc; | 60 entry->fDesc = desc; |
| 61 | 61 |
| 62 desc.fContext->addCleanUp(CleanUp, entry); | 62 desc.fContext->addCleanUp(CleanUp, entry); |
| 63 | 63 |
| 64 GetCache()->add(entry); | 64 GetCache()->add(entry); |
| 65 } | 65 } |
| 66 | 66 |
| 67 return entry->fAtlas; | 67 return entry->fAtlas; |
| 68 } | 68 } |
| 69 | 69 |
| 70 GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc) | 70 GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc) |
| 71 : fCacheKey(sk_atomic_inc(&gCacheCount)) | 71 : fCacheKey(sk_atomic_inc(&gCacheCount)) |
| 72 , fLockedRows(0) | 72 , fLockedRows(0) |
| 73 , fDesc(desc) | 73 , fDesc(desc) |
| 74 , fNumRows(desc.fHeight / desc.fRowHeight) | 74 , fNumRows(desc.fHeight / desc.fRowHeight) |
| 75 , fTexture(NULL) | 75 , fTexture(nullptr) |
| 76 , fRows(new AtlasRow[fNumRows]) | 76 , fRows(new AtlasRow[fNumRows]) |
| 77 , fLRUFront(NULL) | 77 , fLRUFront(nullptr) |
| 78 , fLRUBack(NULL) { | 78 , fLRUBack(nullptr) { |
| 79 SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight); | 79 SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight); |
| 80 this->initLRU(); | 80 this->initLRU(); |
| 81 fNormalizedYHeight = SK_Scalar1 / fDesc.fHeight; | 81 fNormalizedYHeight = SK_Scalar1 / fDesc.fHeight; |
| 82 VALIDATE; | 82 VALIDATE; |
| 83 } | 83 } |
| 84 | 84 |
| 85 GrTextureStripAtlas::~GrTextureStripAtlas() { delete[] fRows; } | 85 GrTextureStripAtlas::~GrTextureStripAtlas() { delete[] fRows; } |
| 86 | 86 |
| 87 int GrTextureStripAtlas::lockRow(const SkBitmap& data) { | 87 int GrTextureStripAtlas::lockRow(const SkBitmap& data) { |
| 88 VALIDATE; | 88 VALIDATE; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 111 rowNumber = static_cast<int>(row - fRows); | 111 rowNumber = static_cast<int>(row - fRows); |
| 112 } else { | 112 } else { |
| 113 // ~index is the index where we will insert the new key to keep things s
orted | 113 // ~index is the index where we will insert the new key to keep things s
orted |
| 114 index = ~index; | 114 index = ~index; |
| 115 | 115 |
| 116 // We don't have this data cached, so pick the least recently used row t
o copy into | 116 // We don't have this data cached, so pick the least recently used row t
o copy into |
| 117 AtlasRow* row = this->getLRU(); | 117 AtlasRow* row = this->getLRU(); |
| 118 | 118 |
| 119 ++fLockedRows; | 119 ++fLockedRows; |
| 120 | 120 |
| 121 if (NULL == row) { | 121 if (nullptr == row) { |
| 122 // force a flush, which should unlock all the rows; then try again | 122 // force a flush, which should unlock all the rows; then try again |
| 123 fDesc.fContext->flush(); | 123 fDesc.fContext->flush(); |
| 124 row = this->getLRU(); | 124 row = this->getLRU(); |
| 125 if (NULL == row) { | 125 if (nullptr == row) { |
| 126 --fLockedRows; | 126 --fLockedRows; |
| 127 return -1; | 127 return -1; |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 this->removeFromLRU(row); | 131 this->removeFromLRU(row); |
| 132 | 132 |
| 133 uint32_t oldKey = row->fKey; | 133 uint32_t oldKey = row->fKey; |
| 134 | 134 |
| 135 // If we are writing into a row that already held bitmap data, we need t
o remove the | 135 // If we are writing into a row that already held bitmap data, we need t
o remove the |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 texDesc.fHeight = fDesc.fHeight; | 194 texDesc.fHeight = fDesc.fHeight; |
| 195 texDesc.fConfig = fDesc.fConfig; | 195 texDesc.fConfig = fDesc.fConfig; |
| 196 | 196 |
| 197 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); | 197 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 198 GrUniqueKey key; | 198 GrUniqueKey key; |
| 199 GrUniqueKey::Builder builder(&key, kDomain, 1); | 199 GrUniqueKey::Builder builder(&key, kDomain, 1); |
| 200 builder[0] = static_cast<uint32_t>(fCacheKey); | 200 builder[0] = static_cast<uint32_t>(fCacheKey); |
| 201 builder.finish(); | 201 builder.finish(); |
| 202 | 202 |
| 203 fTexture = fDesc.fContext->textureProvider()->findAndRefTextureByUniqueKey(k
ey); | 203 fTexture = fDesc.fContext->textureProvider()->findAndRefTextureByUniqueKey(k
ey); |
| 204 if (NULL == fTexture) { | 204 if (nullptr == fTexture) { |
| 205 fTexture = fDesc.fContext->textureProvider()->createTexture(texDesc, tru
e, NULL, 0); | 205 fTexture = fDesc.fContext->textureProvider()->createTexture(texDesc, tru
e, nullptr, 0); |
| 206 if (!fTexture) { | 206 if (!fTexture) { |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 fDesc.fContext->textureProvider()->assignUniqueKeyToTexture(key, fTextur
e); | 209 fDesc.fContext->textureProvider()->assignUniqueKeyToTexture(key, fTextur
e); |
| 210 // This is a new texture, so all of our cache info is now invalid | 210 // This is a new texture, so all of our cache info is now invalid |
| 211 this->initLRU(); | 211 this->initLRU(); |
| 212 fKeyTable.rewind(); | 212 fKeyTable.rewind(); |
| 213 } | 213 } |
| 214 SkASSERT(fTexture); | 214 SkASSERT(fTexture); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void GrTextureStripAtlas::unlockTexture() { | 217 void GrTextureStripAtlas::unlockTexture() { |
| 218 SkASSERT(fTexture && 0 == fLockedRows); | 218 SkASSERT(fTexture && 0 == fLockedRows); |
| 219 fTexture->unref(); | 219 fTexture->unref(); |
| 220 fTexture = NULL; | 220 fTexture = nullptr; |
| 221 } | 221 } |
| 222 | 222 |
| 223 void GrTextureStripAtlas::initLRU() { | 223 void GrTextureStripAtlas::initLRU() { |
| 224 fLRUFront = NULL; | 224 fLRUFront = nullptr; |
| 225 fLRUBack = NULL; | 225 fLRUBack = nullptr; |
| 226 // Initially all the rows are in the LRU list | 226 // Initially all the rows are in the LRU list |
| 227 for (int i = 0; i < fNumRows; ++i) { | 227 for (int i = 0; i < fNumRows; ++i) { |
| 228 fRows[i].fKey = kEmptyAtlasRowKey; | 228 fRows[i].fKey = kEmptyAtlasRowKey; |
| 229 fRows[i].fNext = NULL; | 229 fRows[i].fNext = nullptr; |
| 230 fRows[i].fPrev = NULL; | 230 fRows[i].fPrev = nullptr; |
| 231 this->appendLRU(fRows + i); | 231 this->appendLRU(fRows + i); |
| 232 } | 232 } |
| 233 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev); | 233 SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 234 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext); | 234 SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void GrTextureStripAtlas::appendLRU(AtlasRow* row) { | 237 void GrTextureStripAtlas::appendLRU(AtlasRow* row) { |
| 238 SkASSERT(NULL == row->fPrev && NULL == row->fNext); | 238 SkASSERT(nullptr == row->fPrev && nullptr == row->fNext); |
| 239 if (NULL == fLRUFront && NULL == fLRUBack) { | 239 if (nullptr == fLRUFront && nullptr == fLRUBack) { |
| 240 fLRUFront = row; | 240 fLRUFront = row; |
| 241 fLRUBack = row; | 241 fLRUBack = row; |
| 242 } else { | 242 } else { |
| 243 row->fPrev = fLRUBack; | 243 row->fPrev = fLRUBack; |
| 244 fLRUBack->fNext = row; | 244 fLRUBack->fNext = row; |
| 245 fLRUBack = row; | 245 fLRUBack = row; |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 | 248 |
| 249 void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) { | 249 void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) { |
| 250 SkASSERT(row); | 250 SkASSERT(row); |
| 251 if (row->fNext && row->fPrev) { | 251 if (row->fNext && row->fPrev) { |
| 252 row->fPrev->fNext = row->fNext; | 252 row->fPrev->fNext = row->fNext; |
| 253 row->fNext->fPrev = row->fPrev; | 253 row->fNext->fPrev = row->fPrev; |
| 254 } else { | 254 } else { |
| 255 if (NULL == row->fNext) { | 255 if (nullptr == row->fNext) { |
| 256 SkASSERT(row == fLRUBack); | 256 SkASSERT(row == fLRUBack); |
| 257 fLRUBack = row->fPrev; | 257 fLRUBack = row->fPrev; |
| 258 if (fLRUBack) { | 258 if (fLRUBack) { |
| 259 fLRUBack->fNext = NULL; | 259 fLRUBack->fNext = nullptr; |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 if (NULL == row->fPrev) { | 262 if (nullptr == row->fPrev) { |
| 263 SkASSERT(row == fLRUFront); | 263 SkASSERT(row == fLRUFront); |
| 264 fLRUFront = row->fNext; | 264 fLRUFront = row->fNext; |
| 265 if (fLRUFront) { | 265 if (fLRUFront) { |
| 266 fLRUFront->fPrev = NULL; | 266 fLRUFront->fPrev = nullptr; |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 row->fNext = NULL; | 270 row->fNext = nullptr; |
| 271 row->fPrev = NULL; | 271 row->fPrev = nullptr; |
| 272 } | 272 } |
| 273 | 273 |
| 274 int GrTextureStripAtlas::searchByKey(uint32_t key) { | 274 int GrTextureStripAtlas::searchByKey(uint32_t key) { |
| 275 AtlasRow target; | 275 AtlasRow target; |
| 276 target.fKey = key; | 276 target.fKey = key; |
| 277 return SkTSearch<const AtlasRow, | 277 return SkTSearch<const AtlasRow, |
| 278 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.b
egin(), | 278 GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.b
egin(), |
| 279 fKeyTable.count(), | 279 fKeyTable.count(), |
| 280 &target, | 280 &target, |
| 281 sizeof(AtlasRow*)); | 281 sizeof(AtlasRow*)); |
| 282 } | 282 } |
| 283 | 283 |
| 284 #ifdef SK_DEBUG | 284 #ifdef SK_DEBUG |
| 285 void GrTextureStripAtlas::validate() { | 285 void GrTextureStripAtlas::validate() { |
| 286 | 286 |
| 287 // Our key table should be sorted | 287 // Our key table should be sorted |
| 288 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey; | 288 uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey; |
| 289 for (int i = 1; i < fKeyTable.count(); ++i) { | 289 for (int i = 1; i < fKeyTable.count(); ++i) { |
| 290 SkASSERT(prev < fKeyTable[i]->fKey); | 290 SkASSERT(prev < fKeyTable[i]->fKey); |
| 291 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey); | 291 SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey); |
| 292 prev = fKeyTable[i]->fKey; | 292 prev = fKeyTable[i]->fKey; |
| 293 } | 293 } |
| 294 | 294 |
| 295 int lruCount = 0; | 295 int lruCount = 0; |
| 296 // Validate LRU pointers, and count LRU entries | 296 // Validate LRU pointers, and count LRU entries |
| 297 SkASSERT(NULL == fLRUFront || NULL == fLRUFront->fPrev); | 297 SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 298 SkASSERT(NULL == fLRUBack || NULL == fLRUBack->fNext); | 298 SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
| 299 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) { | 299 for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
| 300 if (NULL == r->fNext) { | 300 if (nullptr == r->fNext) { |
| 301 SkASSERT(r == fLRUBack); | 301 SkASSERT(r == fLRUBack); |
| 302 } else { | 302 } else { |
| 303 SkASSERT(r->fNext->fPrev == r); | 303 SkASSERT(r->fNext->fPrev == r); |
| 304 } | 304 } |
| 305 ++lruCount; | 305 ++lruCount; |
| 306 } | 306 } |
| 307 | 307 |
| 308 int rowLocks = 0; | 308 int rowLocks = 0; |
| 309 int freeRows = 0; | 309 int freeRows = 0; |
| 310 | 310 |
| 311 for (int i = 0; i < fNumRows; ++i) { | 311 for (int i = 0; i < fNumRows; ++i) { |
| 312 rowLocks += fRows[i].fLocks; | 312 rowLocks += fRows[i].fLocks; |
| 313 if (0 == fRows[i].fLocks) { | 313 if (0 == fRows[i].fLocks) { |
| 314 ++freeRows; | 314 ++freeRows; |
| 315 bool inLRU = false; | 315 bool inLRU = false; |
| 316 // Step through the LRU and make sure it's present | 316 // Step through the LRU and make sure it's present |
| 317 for (AtlasRow* r = fLRUFront; r != NULL; r = r->fNext) { | 317 for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
| 318 if (r == &fRows[i]) { | 318 if (r == &fRows[i]) { |
| 319 inLRU = true; | 319 inLRU = true; |
| 320 break; | 320 break; |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 SkASSERT(inLRU); | 323 SkASSERT(inLRU); |
| 324 } else { | 324 } else { |
| 325 // If we are locked, we should have a key | 325 // If we are locked, we should have a key |
| 326 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey); | 326 SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // If we have a key != kEmptyAtlasRowKey, it should be in the key table | 329 // If we have a key != kEmptyAtlasRowKey, it should be in the key table |
| 330 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i
].fKey) >= 0); | 330 SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i
].fKey) >= 0); |
| 331 } | 331 } |
| 332 | 332 |
| 333 // Our count of locks should equal the sum of row locks, unless we ran out o
f rows and flushed, | 333 // Our count of locks should equal the sum of row locks, unless we ran out o
f rows and flushed, |
| 334 // in which case we'll have one more lock than recorded in the rows (to repr
esent the pending | 334 // in which case we'll have one more lock than recorded in the rows (to repr
esent the pending |
| 335 // lock of a row; which ensures we don't unlock the texture prematurely). | 335 // lock of a row; which ensures we don't unlock the texture prematurely). |
| 336 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows); | 336 SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows); |
| 337 | 337 |
| 338 // We should have one lru entry for each free row | 338 // We should have one lru entry for each free row |
| 339 SkASSERT(freeRows == lruCount); | 339 SkASSERT(freeRows == lruCount); |
| 340 | 340 |
| 341 // If we have locked rows, we should have a locked texture, otherwise | 341 // If we have locked rows, we should have a locked texture, otherwise |
| 342 // it should be unlocked | 342 // it should be unlocked |
| 343 if (fLockedRows == 0) { | 343 if (fLockedRows == 0) { |
| 344 SkASSERT(NULL == fTexture); | 344 SkASSERT(nullptr == fTexture); |
| 345 } else { | 345 } else { |
| 346 SkASSERT(fTexture); | 346 SkASSERT(fTexture); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 #endif | 349 #endif |
| OLD | NEW |