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 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 89 | void GrTextureStripAtlas::lockRow(int row) { |
| 90 | // This should only be called on a row that is already locked. |
| 91 | SkASSERT(fRows[row].fLocks); |
| 92 | fRows[row].fLocks++; |
| 93 | ++fLockedRows; |
| 94 | } |
| 95 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 96 | int GrTextureStripAtlas::lockRow(const SkBitmap& bitmap) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 97 | VALIDATE; |
| 98 | if (0 == fLockedRows) { |
| 99 | this->lockTexture(); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 100 | if (!fTexContext) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 101 | return -1; |
| 102 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 105 | int key = bitmap.getGenerationID(); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 106 | int rowNumber = -1; |
| 107 | int index = this->searchByKey(key); |
| 108 | |
| 109 | if (index >= 0) { |
| 110 | // We already have the data in a row, so we can just return that row |
| 111 | AtlasRow* row = fKeyTable[index]; |
| 112 | if (0 == row->fLocks) { |
| 113 | this->removeFromLRU(row); |
| 114 | } |
| 115 | ++row->fLocks; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 116 | ++fLockedRows; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 117 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 118 | // 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] | 119 | // 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] | 120 | rowNumber = static_cast<int>(row - fRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 121 | } else { |
| 122 | // ~index is the index where we will insert the new key to keep things sorted |
| 123 | index = ~index; |
| 124 | |
| 125 | // We don't have this data cached, so pick the least recently used row to copy into |
| 126 | AtlasRow* row = this->getLRU(); |
| 127 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 128 | ++fLockedRows; |
| 129 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | if (nullptr == row) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 131 | // force a flush, which should unlock all the rows; then try again |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 132 | fDesc.fContext->contextPriv().flush(nullptr); // tighten this up? |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 133 | row = this->getLRU(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | if (nullptr == row) { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 135 | --fLockedRows; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | this->removeFromLRU(row); |
| 141 | |
| 142 | uint32_t oldKey = row->fKey; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 143 | |
| 144 | // If we are writing into a row that already held bitmap data, we need to remove the |
| 145 | // reference to that genID which is stored in our sorted table of key values. |
| 146 | if (oldKey != kEmptyAtlasRowKey) { |
| 147 | |
| 148 | // Find the entry in the list; if it's before the index where we plan on adding the new |
| 149 | // entry, we decrement since it will shift elements ahead of it back by one. |
| 150 | int oldIndex = this->searchByKey(oldKey); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 151 | if (oldIndex < index) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 152 | --index; |
| 153 | } |
| 154 | |
| 155 | fKeyTable.remove(oldIndex); |
| 156 | } |
| 157 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 158 | row->fKey = key; |
| 159 | row->fLocks = 1; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 160 | fKeyTable.insert(index, 1, &row); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 161 | rowNumber = static_cast<int>(row - fRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 162 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 163 | SkASSERT(bitmap.width() == fDesc.fWidth); |
| 164 | SkASSERT(bitmap.height() == fDesc.fRowHeight); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 165 | |
| 166 | // Pass in the kDontFlush flag, since we know we're writing to a part of this texture |
| 167 | // that is not currently in use |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 168 | fTexContext->writePixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), |
| 169 | 0, rowNumber * fDesc.fRowHeight, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 170 | GrContextPriv::kDontFlush_PixelOpsFlag); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 171 | } |
| 172 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 173 | SkASSERT(rowNumber >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 174 | VALIDATE; |
| 175 | return rowNumber; |
| 176 | } |
| 177 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 178 | sk_sp<GrTextureProxy> GrTextureStripAtlas::asTextureProxyRef() const { |
| 179 | return fTexContext->asTextureProxyRef(); |
| 180 | } |
| 181 | |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 182 | void GrTextureStripAtlas::unlockRow(int row) { |
| 183 | VALIDATE; |
| 184 | --fRows[row].fLocks; |
| 185 | --fLockedRows; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 186 | SkASSERT(fRows[row].fLocks >= 0 && fLockedRows >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 187 | if (0 == fRows[row].fLocks) { |
| 188 | this->appendLRU(fRows + row); |
| 189 | } |
| 190 | if (0 == fLockedRows) { |
| 191 | this->unlockTexture(); |
| 192 | } |
| 193 | VALIDATE; |
| 194 | } |
| 195 | |
| 196 | GrTextureStripAtlas::AtlasRow* GrTextureStripAtlas::getLRU() { |
| 197 | // Front is least-recently-used |
| 198 | AtlasRow* row = fLRUFront; |
| 199 | return row; |
| 200 | } |
| 201 | |
| 202 | void GrTextureStripAtlas::lockTexture() { |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 203 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 204 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 205 | GrUniqueKey key; |
| 206 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 207 | builder[0] = static_cast<uint32_t>(fCacheKey); |
| 208 | builder.finish(); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 209 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 210 | sk_sp<GrTextureProxy> proxy = fDesc.fContext->resourceProvider()->findProxyByUniqueKey( |
| 211 | key, kTopLeft_GrSurfaceOrigin); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 212 | if (!proxy) { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 213 | GrSurfaceDesc texDesc; |
| 214 | texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 215 | texDesc.fWidth = fDesc.fWidth; |
| 216 | texDesc.fHeight = fDesc.fHeight; |
| 217 | texDesc.fConfig = fDesc.fConfig; |
| 218 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 219 | proxy = GrSurfaceProxy::MakeDeferred(fDesc.fContext->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 220 | texDesc, SkBackingFit::kExact, |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 221 | SkBudgeted::kYes, |
| 222 | GrResourceProvider::kNoPendingIO_Flag); |
| 223 | if (!proxy) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 226 | |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 227 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 228 | fDesc.fContext->resourceProvider()->assignUniqueKeyToProxy(key, proxy.get()); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 229 | // This is a new texture, so all of our cache info is now invalid |
| 230 | this->initLRU(); |
| 231 | fKeyTable.rewind(); |
| 232 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 233 | SkASSERT(proxy); |
| 234 | fTexContext = fDesc.fContext->contextPriv().makeWrappedSurfaceContext(std::move(proxy), |
| 235 | nullptr); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void GrTextureStripAtlas::unlockTexture() { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 239 | SkASSERT(fTexContext && 0 == fLockedRows); |
| 240 | fTexContext.reset(); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void GrTextureStripAtlas::initLRU() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 244 | fLRUFront = nullptr; |
| 245 | fLRUBack = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 246 | // Initially all the rows are in the LRU list |
| 247 | for (int i = 0; i < fNumRows; ++i) { |
| 248 | fRows[i].fKey = kEmptyAtlasRowKey; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 249 | fRows[i].fNext = nullptr; |
| 250 | fRows[i].fPrev = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 251 | this->appendLRU(fRows + i); |
| 252 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 253 | SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 254 | SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void GrTextureStripAtlas::appendLRU(AtlasRow* row) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | SkASSERT(nullptr == row->fPrev && nullptr == row->fNext); |
| 259 | if (nullptr == fLRUFront && nullptr == fLRUBack) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 260 | fLRUFront = row; |
| 261 | fLRUBack = row; |
| 262 | } else { |
| 263 | row->fPrev = fLRUBack; |
| 264 | fLRUBack->fNext = row; |
| 265 | fLRUBack = row; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void GrTextureStripAtlas::removeFromLRU(AtlasRow* row) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 270 | SkASSERT(row); |
| 271 | if (row->fNext && row->fPrev) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 272 | row->fPrev->fNext = row->fNext; |
| 273 | row->fNext->fPrev = row->fPrev; |
| 274 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | if (nullptr == row->fNext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 276 | SkASSERT(row == fLRUBack); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 277 | fLRUBack = row->fPrev; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 278 | if (fLRUBack) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 279 | fLRUBack->fNext = nullptr; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +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 | if (nullptr == row->fPrev) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 283 | SkASSERT(row == fLRUFront); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 284 | fLRUFront = row->fNext; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 285 | if (fLRUFront) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | fLRUFront->fPrev = nullptr; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 287 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 288 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 289 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 290 | row->fNext = nullptr; |
| 291 | row->fPrev = nullptr; |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | int GrTextureStripAtlas::searchByKey(uint32_t key) { |
| 295 | AtlasRow target; |
| 296 | target.fKey = key; |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 297 | return SkTSearch<const AtlasRow, |
| 298 | GrTextureStripAtlas::KeyLess>((const AtlasRow**)fKeyTable.begin(), |
| 299 | fKeyTable.count(), |
| 300 | &target, |
| 301 | sizeof(AtlasRow*)); |
skia.committer@gmail.com | 845220b | 2013-05-20 11:51:35 +0000 | [diff] [blame] | 302 | } |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 303 | |
| 304 | #ifdef SK_DEBUG |
| 305 | void GrTextureStripAtlas::validate() { |
| 306 | |
| 307 | // Our key table should be sorted |
| 308 | uint32_t prev = 1 > fKeyTable.count() ? 0 : fKeyTable[0]->fKey; |
| 309 | for (int i = 1; i < fKeyTable.count(); ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 310 | SkASSERT(prev < fKeyTable[i]->fKey); |
| 311 | SkASSERT(fKeyTable[i]->fKey != kEmptyAtlasRowKey); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 312 | prev = fKeyTable[i]->fKey; |
| 313 | } |
| 314 | |
| 315 | int lruCount = 0; |
| 316 | // Validate LRU pointers, and count LRU entries |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 317 | SkASSERT(nullptr == fLRUFront || nullptr == fLRUFront->fPrev); |
| 318 | SkASSERT(nullptr == fLRUBack || nullptr == fLRUBack->fNext); |
| 319 | for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
| 320 | if (nullptr == r->fNext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 321 | SkASSERT(r == fLRUBack); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 322 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 323 | SkASSERT(r->fNext->fPrev == r); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 324 | } |
| 325 | ++lruCount; |
| 326 | } |
| 327 | |
| 328 | int rowLocks = 0; |
| 329 | int freeRows = 0; |
| 330 | |
| 331 | for (int i = 0; i < fNumRows; ++i) { |
| 332 | rowLocks += fRows[i].fLocks; |
| 333 | if (0 == fRows[i].fLocks) { |
| 334 | ++freeRows; |
| 335 | bool inLRU = false; |
| 336 | // Step through the LRU and make sure it's present |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 337 | for (AtlasRow* r = fLRUFront; r != nullptr; r = r->fNext) { |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 338 | if (r == &fRows[i]) { |
| 339 | inLRU = true; |
| 340 | break; |
| 341 | } |
| 342 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 343 | SkASSERT(inLRU); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 344 | } else { |
| 345 | // If we are locked, we should have a key |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 346 | SkASSERT(kEmptyAtlasRowKey != fRows[i].fKey); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // 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] | 350 | SkASSERT(fRows[i].fKey == kEmptyAtlasRowKey || this->searchByKey(fRows[i].fKey) >= 0); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 351 | } |
| 352 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 353 | // Our count of locks should equal the sum of row locks, unless we ran out of rows and flushed, |
| 354 | // in which case we'll have one more lock than recorded in the rows (to represent the pending |
| 355 | // 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] | 356 | SkASSERT(rowLocks == fLockedRows || rowLocks + 1 == fLockedRows); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 357 | |
| 358 | // We should have one lru entry for each free row |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 359 | SkASSERT(freeRows == lruCount); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 360 | |
| 361 | // If we have locked rows, we should have a locked texture, otherwise |
| 362 | // it should be unlocked |
| 363 | if (fLockedRows == 0) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 364 | SkASSERT(!fTexContext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 365 | } else { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 366 | SkASSERT(fTexContext); |
rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | #endif |