| 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 = NULL; |
| 26 | 26 |
| 27 GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() { | 27 GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() { |
| 28 | 28 |
| 29 if (NULL == gAtlasCache) { | 29 if (NULL == gAtlasCache) { |
| 30 gAtlasCache = SkNEW(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 SkDELETE(entry); | 46 delete entry; |
| 47 | 47 |
| 48 if (0 == GetCache()->count()) { | 48 if (0 == GetCache()->count()) { |
| 49 SkDELETE(gAtlasCache); | 49 delete gAtlasCache; |
| 50 gAtlasCache = NULL; | 50 gAtlasCache = NULL; |
| 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 (NULL == entry) { |
| 57 entry = SkNEW(AtlasEntry); | 57 entry = new AtlasEntry; |
| 58 | 58 |
| 59 entry->fAtlas = SkNEW_ARGS(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(NULL) |
| 76 , fRows(SkNEW_ARRAY(AtlasRow, fNumRows)) | 76 , fRows(new AtlasRow[fNumRows]) |
| 77 , fLRUFront(NULL) | 77 , fLRUFront(NULL) |
| 78 , fLRUBack(NULL) { | 78 , fLRUBack(NULL) { |
| 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() { | 85 GrTextureStripAtlas::~GrTextureStripAtlas() { delete[] fRows; } |
| 86 SkDELETE_ARRAY(fRows); | |
| 87 } | |
| 88 | 86 |
| 89 int GrTextureStripAtlas::lockRow(const SkBitmap& data) { | 87 int GrTextureStripAtlas::lockRow(const SkBitmap& data) { |
| 90 VALIDATE; | 88 VALIDATE; |
| 91 if (0 == fLockedRows) { | 89 if (0 == fLockedRows) { |
| 92 this->lockTexture(); | 90 this->lockTexture(); |
| 93 if (!fTexture) { | 91 if (!fTexture) { |
| 94 return -1; | 92 return -1; |
| 95 } | 93 } |
| 96 } | 94 } |
| 97 | 95 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 340 |
| 343 // 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 |
| 344 // it should be unlocked | 342 // it should be unlocked |
| 345 if (fLockedRows == 0) { | 343 if (fLockedRows == 0) { |
| 346 SkASSERT(NULL == fTexture); | 344 SkASSERT(NULL == fTexture); |
| 347 } else { | 345 } else { |
| 348 SkASSERT(fTexture); | 346 SkASSERT(fTexture); |
| 349 } | 347 } |
| 350 } | 348 } |
| 351 #endif | 349 #endif |
| OLD | NEW |