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