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