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