rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrTextureStripAtlas.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 9 | #include "GrContext.h" |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 11 | #include "GrResourceProvider.h" |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 12 | #include "GrSurfaceContext.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 13 | #include "SkGr.h" |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 14 | #include "SkPixelRef.h" |
| 15 | #include "SkTSearch.h" |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 16 | |
| 17 | #ifdef SK_DEBUG |
| 18 | #define VALIDATE this->validate() |
| 19 | #else |
| 20 | #define VALIDATE |
| 21 | #endif |
| 22 | |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 23 | class GrTextureStripAtlas::Hash : public SkTDynamicHash<GrTextureStripAtlas::AtlasEntry, |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 24 | GrTextureStripAtlas::Desc> {}; |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 25 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 26 | int32_t GrTextureStripAtlas::gCacheCount = 0; |
| 27 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | GrTextureStripAtlas::Hash* GrTextureStripAtlas::gAtlasCache = nullptr; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 29 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 30 | GrTextureStripAtlas::Hash* GrTextureStripAtlas::GetCache() { |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 31 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | if (nullptr == gAtlasCache) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 33 | gAtlasCache = new Hash; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return gAtlasCache; |
| 37 | } |
| 38 | |
| 39 | // Remove the specified atlas from the cache |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 40 | void GrTextureStripAtlas::CleanUp(const GrContext*, void* info) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 41 | SkASSERT(info); |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 42 | |
| 43 | AtlasEntry* entry = static_cast<AtlasEntry*>(info); |
| 44 | |
| 45 | // remove the cache entry |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 46 | GetCache()->remove(entry->fDesc); |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 47 | |
| 48 | // remove the actual entry |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 49 | delete entry; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 50 | |
| 51 | if (0 == GetCache()->count()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 52 | delete gAtlasCache; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | gAtlasCache = nullptr; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 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) { |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 58 | AtlasEntry* entry = GetCache()->find(desc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | if (nullptr == entry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 60 | entry = new AtlasEntry; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 61 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 62 | entry->fAtlas = new GrTextureStripAtlas(desc); |
joshualitt | 690fc75 | 2015-07-13 12:49:13 -0700 | [diff] [blame] | 63 | entry->fDesc = desc; |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 64 | |
| 65 | desc.fContext->addCleanUp(CleanUp, entry); |
| 66 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 67 | GetCache()->add(entry); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 68 | } |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 69 | |
| 70 | return entry->fAtlas; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 71 | } |
| 72 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | GrTextureStripAtlas::GrTextureStripAtlas(GrTextureStripAtlas::Desc desc) |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 74 | : fCacheKey(sk_atomic_inc(&gCacheCount)) |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 75 | , fLockedRows(0) |
| 76 | , fDesc(desc) |
| 77 | , fNumRows(desc.fHeight / desc.fRowHeight) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 78 | , fRows(new AtlasRow[fNumRows]) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | , fLRUFront(nullptr) |
| 80 | , fLRUBack(nullptr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 81 | SkASSERT(fNumRows * fDesc.fRowHeight == fDesc.fHeight); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 82 | this->initLRU(); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 83 | fNormalizedYHeight = SK_Scalar1 / fDesc.fHeight; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 84 | VALIDATE; |
| 85 | } |
| 86 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 87 | GrTextureStripAtlas::~GrTextureStripAtlas() { delete[] fRows; } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 88 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 89 | int GrTextureStripAtlas::lockRow(const SkBitmap& bitmap) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 90 | VALIDATE; |
| 91 | if (0 == fLockedRows) { |
| 92 | this->lockTexture(); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 93 | if (!fTexContext) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 94 | return -1; |
| 95 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 98 | int key = bitmap.getGenerationID(); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 99 | int rowNumber = -1; |
| 100 | int index = this->searchByKey(key); |
| 101 | |
| 102 | if (index >= 0) { |
| 103 | // We already have the data in a row, so we can just return that row |
| 104 | AtlasRow* row = fKeyTable[index]; |
| 105 | if (0 == row->fLocks) { |
| 106 | this->removeFromLRU(row); |
| 107 | } |
| 108 | ++row->fLocks; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 109 | ++fLockedRows; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 110 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 111 | // 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] | 112 | // 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] | 113 | rowNumber = static_cast<int>(row - fRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 114 | } else { |
| 115 | // ~index is the index where we will insert the new key to keep things sorted |
| 116 | index = ~index; |
| 117 | |
| 118 | // We don't have this data cached, so pick the least recently used row to copy into |
| 119 | AtlasRow* row = this->getLRU(); |
| 120 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 121 | ++fLockedRows; |
| 122 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | if (nullptr == row) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 124 | // force a flush, which should unlock all the rows; then try again |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 125 | fDesc.fContext->contextPriv().flush(nullptr); // tighten this up? |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 126 | row = this->getLRU(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | if (nullptr == row) { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 128 | --fLockedRows; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | this->removeFromLRU(row); |
| 134 | |
| 135 | uint32_t oldKey = row->fKey; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 136 | |
| 137 | // If we are writing into a row that already held bitmap data, we need to remove the |
| 138 | // reference to that genID which is stored in our sorted table of key values. |
| 139 | if (oldKey != kEmptyAtlasRowKey) { |
| 140 | |
| 141 | // Find the entry in the list; if it's before the index where we plan on adding the new |
| 142 | // entry, we decrement since it will shift elements ahead of it back by one. |
| 143 | int oldIndex = this->searchByKey(oldKey); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 144 | if (oldIndex < index) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 145 | --index; |
| 146 | } |
| 147 | |
| 148 | fKeyTable.remove(oldIndex); |
| 149 | } |
| 150 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 151 | row->fKey = key; |
| 152 | row->fLocks = 1; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 153 | fKeyTable.insert(index, 1, &row); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 154 | rowNumber = static_cast<int>(row - fRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 155 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 156 | SkASSERT(bitmap.width() == fDesc.fWidth); |
| 157 | SkASSERT(bitmap.height() == fDesc.fRowHeight); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 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 |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 161 | fTexContext->writePixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), |
| 162 | 0, rowNumber * fDesc.fRowHeight, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 163 | GrContextPriv::kDontFlush_PixelOpsFlag); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 166 | SkASSERT(rowNumber >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 167 | VALIDATE; |
| 168 | return rowNumber; |
| 169 | } |
| 170 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 171 | sk_sp<GrTextureProxy> GrTextureStripAtlas::asTextureProxyRef() const { |
| 172 | return fTexContext->asTextureProxyRef(); |
| 173 | } |
| 174 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 175 | void GrTextureStripAtlas::unlockRow(int row) { |
| 176 | VALIDATE; |
| 177 | --fRows[row].fLocks; |
| 178 | --fLockedRows; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 179 | SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 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() { |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 196 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 197 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 198 | GrUniqueKey key; |
| 199 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 200 | builder[0] = static_cast<uint32_t>(fCacheKey); |
| 201 | builder.finish(); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 202 | |
Robert Phillips | 96be9df | 2017-07-21 14:17:45 +0000 | [diff] [blame] | 203 | sk_sp<GrTextureProxy> proxy = fDesc.fContext->resourceProvider()->findProxyByUniqueKey(key); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 204 | if (!proxy) { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame^] | 205 | GrSurfaceDesc texDesc; |
| 206 | texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 207 | texDesc.fWidth = fDesc.fWidth; |
| 208 | texDesc.fHeight = fDesc.fHeight; |
| 209 | texDesc.fConfig = fDesc.fConfig; |
| 210 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 211 | proxy = GrSurfaceProxy::MakeDeferred(fDesc.fContext->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 212 | texDesc, SkBackingFit::kExact, |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 213 | SkBudgeted::kYes, |
| 214 | GrResourceProvider::kNoPendingIO_Flag); |
| 215 | if (!proxy) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 218 | |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame^] | 219 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 220 | fDesc.fContext->resourceProvider()->assignUniqueKeyToProxy(key, proxy.get()); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 221 | // This is a new texture, so all of our cache info is now invalid |
| 222 | this->initLRU(); |
| 223 | fKeyTable.rewind(); |
| 224 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 225 | SkASSERT(proxy); |
| 226 | fTexContext = fDesc.fContext->contextPriv().makeWrappedSurfaceContext(std::move(proxy), |
| 227 | nullptr); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void GrTextureStripAtlas::unlockTexture() { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 231 | SkASSERT(fTexContext && 0 == fLockedRows); |
| 232 | fTexContext.reset(); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void GrTextureStripAtlas::initLRU() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 236 | fLRUFront = nullptr; |
| 237 | fLRUBack = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 238 | // Initially all the rows are in the LRU list |
| 239 | for (int i = 0; i < fNumRows; ++i) { |
| 240 | fRows[i].fKey = kEmptyAtlasRowKey; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 241 | fRows[i].fNext = nullptr; |
| 242 | fRows[i].fPrev = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 243 | this->appendLRU(fRows + i); |
| 244 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 245 | SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 246 | SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void GrTextureStripAtlas::appendLRU(AtlasRow* row) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 250 | SkASSERT(nullptr == row->fPrev && nullptr == row->fNext); |
| 251 | if (nullptr == fLRUFront && nullptr == fLRUBack) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 252 | fLRUFront = row; |
| 253 | fLRUBack = row; |
| 254 | } else { |
| 255 | row->fPrev = fLRUBack; |
| 256 | fLRUBack->fNext = row; |
| 257 | fLRUBack = row; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 262 | SkASSERT(row); |
| 263 | if (row->fNext && row->fPrev) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 264 | row->fPrev->fNext = row->fNext; |
| 265 | row->fNext->fPrev = row->fPrev; |
| 266 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 267 | if (nullptr == row->fNext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 268 | SkASSERT(row == fLRUBack); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 269 | fLRUBack = row->fPrev; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 270 | if (fLRUBack) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 271 | fLRUBack->fNext = nullptr; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 272 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 273 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 274 | if (nullptr == row->fPrev) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 275 | SkASSERT(row == fLRUFront); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 276 | fLRUFront = row->fNext; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 277 | if (fLRUFront) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 278 | fLRUFront->fPrev = nullptr; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 279 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 280 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 281 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 282 | row->fNext = nullptr; |
| 283 | row->fPrev = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | int GrTextureStripAtlas::searchByKey(uint32_t key) { |
| 287 | AtlasRow target; |
| 288 | target.fKey = key; |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 289 | return SkTSearch<const AtlasRow, |
| 290 | GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(), |
| 291 | fKeyTable.count(), |
| 292 | &target, |
| 293 | sizeof(AtlasRow*)); |
skia.committer@gmail.com | 845220b | 2013-05-20 11:51:35 +0000 | [diff] [blame] | 294 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 295 | |
| 296 | #ifdef SK_DEBUG |
| 297 | void GrTextureStripAtlas::validate() { |
| 298 | |
| 299 | // Our key table should be sorted |
| 300 | uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey; |
| 301 | for (int i = 1; i < fKeyTable.count(); ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 302 | SkASSERT(prev < fKeyTable[i]->fKey); |
| 303 | SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 304 | prev = fKeyTable[i]->fKey; |
| 305 | } |
| 306 | |
| 307 | int lruCount = 0; |
| 308 | // Validate LRU pointers, and count LRU entries |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 310 | SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
| 311 | for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
| 312 | if (nullptr == r->fNext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 313 | SkASSERT(r == fLRUBack); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 314 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 315 | SkASSERT(r->fNext->fPrev == r); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 316 | } |
| 317 | ++lruCount; |
| 318 | } |
| 319 | |
| 320 | int rowLocks = 0; |
| 321 | int freeRows = 0; |
| 322 | |
| 323 | for (int i = 0; i < fNumRows; ++i) { |
| 324 | rowLocks += fRows[i].fLocks; |
| 325 | if (0 == fRows[i].fLocks) { |
| 326 | ++freeRows; |
| 327 | bool inLRU = false; |
| 328 | // Step through the LRU and make sure it's present |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 329 | for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 330 | if (r == &fRows[i]) { |
| 331 | inLRU = true; |
| 332 | break; |
| 333 | } |
| 334 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 335 | SkASSERT(inLRU); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 336 | } else { |
| 337 | // If we are locked, we should have a key |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 338 | SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // If we have a key != kEmptyAtlasRowKey, it should be in the key table |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 342 | SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 343 | } |
| 344 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 345 | // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed, |
| 346 | // in which case we'll have one more lock than recorded in the rows (to represent the pending |
| 347 | // lock of a row; which ensures we don't unlock the texture prematurely). |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 348 | SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 349 | |
| 350 | // We should have one lru entry for each free row |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 351 | SkASSERT(freeRows == lruCount); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 352 | |
| 353 | // If we have locked rows, we should have a locked texture, otherwise |
| 354 | // it should be unlocked |
| 355 | if (fLockedRows == 0) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 356 | SkASSERT(!fTexContext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 357 | } else { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 358 | SkASSERT(fTexContext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | #endif |