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 | * Copyright 2010 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "GrAtlas.h" |
michael@0 | 9 | #include "GrGpu.h" |
michael@0 | 10 | #include "GrRectanizer.h" |
michael@0 | 11 | #include "GrTextStrike.h" |
michael@0 | 12 | #include "GrTextStrike_impl.h" |
michael@0 | 13 | #include "SkString.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "SkDistanceFieldGen.h" |
michael@0 | 16 | |
michael@0 | 17 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 18 | |
michael@0 | 19 | #define FONT_CACHE_STATS 0 |
michael@0 | 20 | #if FONT_CACHE_STATS |
michael@0 | 21 | static int g_PurgeCount = 0; |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
michael@0 | 25 | gpu->ref(); |
michael@0 | 26 | for (int i = 0; i < kAtlasCount; ++i) { |
michael@0 | 27 | fAtlasMgr[i] = NULL; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | fHead = fTail = NULL; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | GrFontCache::~GrFontCache() { |
michael@0 | 34 | fCache.deleteAll(); |
michael@0 | 35 | for (int i = 0; i < kAtlasCount; ++i) { |
michael@0 | 36 | delete fAtlasMgr[i]; |
michael@0 | 37 | } |
michael@0 | 38 | fGpu->unref(); |
michael@0 | 39 | #if FONT_CACHE_STATS |
michael@0 | 40 | GrPrintf("Num purges: %d\n", g_PurgeCount); |
michael@0 | 41 | #endif |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
michael@0 | 45 | static const GrPixelConfig sPixelConfigs[] = { |
michael@0 | 46 | kAlpha_8_GrPixelConfig, |
michael@0 | 47 | kRGB_565_GrPixelConfig, |
michael@0 | 48 | kSkia8888_GrPixelConfig, |
michael@0 | 49 | kSkia8888_GrPixelConfig |
michael@0 | 50 | }; |
michael@0 | 51 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch); |
michael@0 | 52 | |
michael@0 | 53 | return sPixelConfigs[format]; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | static int mask_format_to_atlas_index(GrMaskFormat format) { |
michael@0 | 57 | static const int sAtlasIndices[] = { |
michael@0 | 58 | GrFontCache::kA8_AtlasType, |
michael@0 | 59 | GrFontCache::k565_AtlasType, |
michael@0 | 60 | GrFontCache::k8888_AtlasType, |
michael@0 | 61 | GrFontCache::k8888_AtlasType |
michael@0 | 62 | }; |
michael@0 | 63 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch); |
michael@0 | 64 | |
michael@0 | 65 | SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount); |
michael@0 | 66 | return sAtlasIndices[format]; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, |
michael@0 | 70 | const Key& key) { |
michael@0 | 71 | GrMaskFormat format = scaler->getMaskFormat(); |
michael@0 | 72 | GrPixelConfig config = mask_format_to_pixel_config(format); |
michael@0 | 73 | int atlasIndex = mask_format_to_atlas_index(format); |
michael@0 | 74 | if (NULL == fAtlasMgr[atlasIndex]) { |
michael@0 | 75 | fAtlasMgr[atlasIndex] = SkNEW_ARGS(GrAtlasMgr, (fGpu, config)); |
michael@0 | 76 | } |
michael@0 | 77 | GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
michael@0 | 78 | (this, scaler->getKey(), format, fAtlasMgr[atlasIndex])); |
michael@0 | 79 | fCache.insert(key, strike); |
michael@0 | 80 | |
michael@0 | 81 | if (fHead) { |
michael@0 | 82 | fHead->fPrev = strike; |
michael@0 | 83 | } else { |
michael@0 | 84 | SkASSERT(NULL == fTail); |
michael@0 | 85 | fTail = strike; |
michael@0 | 86 | } |
michael@0 | 87 | strike->fPrev = NULL; |
michael@0 | 88 | strike->fNext = fHead; |
michael@0 | 89 | fHead = strike; |
michael@0 | 90 | |
michael@0 | 91 | return strike; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void GrFontCache::freeAll() { |
michael@0 | 95 | fCache.deleteAll(); |
michael@0 | 96 | for (int i = 0; i < kAtlasCount; ++i) { |
michael@0 | 97 | delete fAtlasMgr[i]; |
michael@0 | 98 | fAtlasMgr[i] = NULL; |
michael@0 | 99 | } |
michael@0 | 100 | fHead = NULL; |
michael@0 | 101 | fTail = NULL; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void GrFontCache::purgeStrike(GrTextStrike* strike) { |
michael@0 | 105 | const GrFontCache::Key key(strike->fFontScalerKey); |
michael@0 | 106 | fCache.remove(key, strike); |
michael@0 | 107 | this->detachStrikeFromList(strike); |
michael@0 | 108 | delete strike; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { |
michael@0 | 112 | SkASSERT(NULL != preserveStrike); |
michael@0 | 113 | |
michael@0 | 114 | GrAtlasMgr* atlasMgr = preserveStrike->fAtlasMgr; |
michael@0 | 115 | GrPlot* plot = atlasMgr->getUnusedPlot(); |
michael@0 | 116 | if (NULL == plot) { |
michael@0 | 117 | return false; |
michael@0 | 118 | } |
michael@0 | 119 | plot->resetRects(); |
michael@0 | 120 | |
michael@0 | 121 | GrTextStrike* strike = fHead; |
michael@0 | 122 | GrMaskFormat maskFormat = preserveStrike->fMaskFormat; |
michael@0 | 123 | while (strike) { |
michael@0 | 124 | if (maskFormat != strike->fMaskFormat) { |
michael@0 | 125 | strike = strike->fNext; |
michael@0 | 126 | continue; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | GrTextStrike* strikeToPurge = strike; |
michael@0 | 130 | strike = strikeToPurge->fNext; |
michael@0 | 131 | strikeToPurge->removePlot(plot); |
michael@0 | 132 | |
michael@0 | 133 | // clear out any empty strikes (except this one) |
michael@0 | 134 | if (strikeToPurge != preserveStrike && strikeToPurge->fAtlas.isEmpty()) { |
michael@0 | 135 | this->purgeStrike(strikeToPurge); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | #if FONT_CACHE_STATS |
michael@0 | 140 | ++g_PurgeCount; |
michael@0 | 141 | #endif |
michael@0 | 142 | |
michael@0 | 143 | return true; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | #ifdef SK_DEBUG |
michael@0 | 147 | void GrFontCache::validate() const { |
michael@0 | 148 | int count = fCache.count(); |
michael@0 | 149 | if (0 == count) { |
michael@0 | 150 | SkASSERT(!fHead); |
michael@0 | 151 | SkASSERT(!fTail); |
michael@0 | 152 | } else if (1 == count) { |
michael@0 | 153 | SkASSERT(fHead == fTail); |
michael@0 | 154 | } else { |
michael@0 | 155 | SkASSERT(fHead != fTail); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | int count2 = 0; |
michael@0 | 159 | const GrTextStrike* strike = fHead; |
michael@0 | 160 | while (strike) { |
michael@0 | 161 | count2 += 1; |
michael@0 | 162 | strike = strike->fNext; |
michael@0 | 163 | } |
michael@0 | 164 | SkASSERT(count == count2); |
michael@0 | 165 | |
michael@0 | 166 | count2 = 0; |
michael@0 | 167 | strike = fTail; |
michael@0 | 168 | while (strike) { |
michael@0 | 169 | count2 += 1; |
michael@0 | 170 | strike = strike->fPrev; |
michael@0 | 171 | } |
michael@0 | 172 | SkASSERT(count == count2); |
michael@0 | 173 | } |
michael@0 | 174 | #endif |
michael@0 | 175 | |
michael@0 | 176 | #ifdef SK_DEVELOPER |
michael@0 | 177 | void GrFontCache::dump() const { |
michael@0 | 178 | static int gDumpCount = 0; |
michael@0 | 179 | for (int i = 0; i < kAtlasCount; ++i) { |
michael@0 | 180 | if (NULL != fAtlasMgr[i]) { |
michael@0 | 181 | GrTexture* texture = fAtlasMgr[i]->getTexture(); |
michael@0 | 182 | if (NULL != texture) { |
michael@0 | 183 | SkString filename; |
michael@0 | 184 | filename.printf("fontcache_%d%d.png", gDumpCount, i); |
michael@0 | 185 | texture->savePixels(filename.c_str()); |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | } |
michael@0 | 189 | ++gDumpCount; |
michael@0 | 190 | } |
michael@0 | 191 | #endif |
michael@0 | 192 | |
michael@0 | 193 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 194 | |
michael@0 | 195 | #ifdef SK_DEBUG |
michael@0 | 196 | static int gCounter; |
michael@0 | 197 | #endif |
michael@0 | 198 | |
michael@0 | 199 | // this acts as the max magnitude for the distance field, |
michael@0 | 200 | // as well as the pad we need around the glyph |
michael@0 | 201 | #define DISTANCE_FIELD_RANGE 4 |
michael@0 | 202 | |
michael@0 | 203 | /* |
michael@0 | 204 | The text strike is specific to a given font/style/matrix setup, which is |
michael@0 | 205 | represented by the GrHostFontScaler object we are given in getGlyph(). |
michael@0 | 206 | |
michael@0 | 207 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
michael@0 | 208 | atlas and a position within that texture. |
michael@0 | 209 | */ |
michael@0 | 210 | |
michael@0 | 211 | GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, |
michael@0 | 212 | GrMaskFormat format, |
michael@0 | 213 | GrAtlasMgr* atlasMgr) : fPool(64) { |
michael@0 | 214 | fFontScalerKey = key; |
michael@0 | 215 | fFontScalerKey->ref(); |
michael@0 | 216 | |
michael@0 | 217 | fFontCache = cache; // no need to ref, it won't go away before we do |
michael@0 | 218 | fAtlasMgr = atlasMgr; // no need to ref, it won't go away before we do |
michael@0 | 219 | |
michael@0 | 220 | fMaskFormat = format; |
michael@0 | 221 | |
michael@0 | 222 | #ifdef SK_DEBUG |
michael@0 | 223 | // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
michael@0 | 224 | gCounter += 1; |
michael@0 | 225 | #endif |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | // this signature is needed because it's used with |
michael@0 | 229 | // SkTDArray::visitAll() (see destructor) |
michael@0 | 230 | static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
michael@0 | 231 | |
michael@0 | 232 | GrTextStrike::~GrTextStrike() { |
michael@0 | 233 | fFontScalerKey->unref(); |
michael@0 | 234 | fCache.getArray().visitAll(free_glyph); |
michael@0 | 235 | |
michael@0 | 236 | #ifdef SK_DEBUG |
michael@0 | 237 | gCounter -= 1; |
michael@0 | 238 | // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
michael@0 | 239 | #endif |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
michael@0 | 243 | GrFontScaler* scaler) { |
michael@0 | 244 | SkIRect bounds; |
michael@0 | 245 | if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
michael@0 | 246 | return NULL; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | GrGlyph* glyph = fPool.alloc(); |
michael@0 | 250 | // expand bounds to hold full distance field data |
michael@0 | 251 | if (fUseDistanceField) { |
michael@0 | 252 | bounds.fLeft -= DISTANCE_FIELD_RANGE; |
michael@0 | 253 | bounds.fRight += DISTANCE_FIELD_RANGE; |
michael@0 | 254 | bounds.fTop -= DISTANCE_FIELD_RANGE; |
michael@0 | 255 | bounds.fBottom += DISTANCE_FIELD_RANGE; |
michael@0 | 256 | } |
michael@0 | 257 | glyph->init(packed, bounds); |
michael@0 | 258 | fCache.insert(packed, glyph); |
michael@0 | 259 | return glyph; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | void GrTextStrike::removePlot(const GrPlot* plot) { |
michael@0 | 263 | SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); |
michael@0 | 264 | for (int i = 0; i < glyphArray.count(); ++i) { |
michael@0 | 265 | if (plot == glyphArray[i]->fPlot) { |
michael@0 | 266 | glyphArray[i]->fPlot = NULL; |
michael@0 | 267 | } |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | fAtlasMgr->removePlot(&fAtlas, plot); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
michael@0 | 275 | #if 0 // testing hack to force us to flush our cache often |
michael@0 | 276 | static int gCounter; |
michael@0 | 277 | if ((++gCounter % 10) == 0) return false; |
michael@0 | 278 | #endif |
michael@0 | 279 | |
michael@0 | 280 | SkASSERT(glyph); |
michael@0 | 281 | SkASSERT(scaler); |
michael@0 | 282 | SkASSERT(fCache.contains(glyph)); |
michael@0 | 283 | SkASSERT(NULL == glyph->fPlot); |
michael@0 | 284 | |
michael@0 | 285 | SkAutoRef ar(scaler); |
michael@0 | 286 | |
michael@0 | 287 | int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
michael@0 | 288 | |
michael@0 | 289 | GrPlot* plot; |
michael@0 | 290 | if (fUseDistanceField) { |
michael@0 | 291 | // we've already expanded the glyph dimensions to match the final size |
michael@0 | 292 | // but must shrink back down to get the packed glyph data |
michael@0 | 293 | int dfWidth = glyph->width(); |
michael@0 | 294 | int dfHeight = glyph->height(); |
michael@0 | 295 | int width = dfWidth - 2*DISTANCE_FIELD_RANGE; |
michael@0 | 296 | int height = dfHeight - 2*DISTANCE_FIELD_RANGE; |
michael@0 | 297 | int stride = width*bytesPerPixel; |
michael@0 | 298 | |
michael@0 | 299 | size_t size = width * height * bytesPerPixel; |
michael@0 | 300 | SkAutoSMalloc<1024> storage(size); |
michael@0 | 301 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, width, height, stride, storage.get())) { |
michael@0 | 302 | return false; |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | // alloc storage for distance field glyph |
michael@0 | 306 | size_t dfSize = dfWidth * dfHeight * bytesPerPixel; |
michael@0 | 307 | SkAutoSMalloc<1024> dfStorage(dfSize); |
michael@0 | 308 | |
michael@0 | 309 | if (1 == bytesPerPixel) { |
michael@0 | 310 | (void) SkGenerateDistanceFieldFromImage((unsigned char*)dfStorage.get(), |
michael@0 | 311 | (unsigned char*)storage.get(), |
michael@0 | 312 | width, height, DISTANCE_FIELD_RANGE); |
michael@0 | 313 | } else { |
michael@0 | 314 | // TODO: Fix color emoji |
michael@0 | 315 | // for now, copy glyph into distance field storage |
michael@0 | 316 | // this is not correct, but it won't crash |
michael@0 | 317 | sk_bzero(dfStorage.get(), dfSize); |
michael@0 | 318 | unsigned char* ptr = (unsigned char*) storage.get(); |
michael@0 | 319 | unsigned char* dfPtr = (unsigned char*) dfStorage.get(); |
michael@0 | 320 | size_t dfStride = dfWidth*bytesPerPixel; |
michael@0 | 321 | dfPtr += DISTANCE_FIELD_RANGE*dfStride; |
michael@0 | 322 | dfPtr += DISTANCE_FIELD_RANGE*bytesPerPixel; |
michael@0 | 323 | |
michael@0 | 324 | for (int i = 0; i < height; ++i) { |
michael@0 | 325 | memcpy(dfPtr, ptr, stride); |
michael@0 | 326 | |
michael@0 | 327 | dfPtr += dfStride; |
michael@0 | 328 | ptr += stride; |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | // copy to atlas |
michael@0 | 333 | plot = fAtlasMgr->addToAtlas(&fAtlas, dfWidth, dfHeight, dfStorage.get(), |
michael@0 | 334 | &glyph->fAtlasLocation); |
michael@0 | 335 | |
michael@0 | 336 | } else { |
michael@0 | 337 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
michael@0 | 338 | SkAutoSMalloc<1024> storage(size); |
michael@0 | 339 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
michael@0 | 340 | glyph->height(), |
michael@0 | 341 | glyph->width() * bytesPerPixel, |
michael@0 | 342 | storage.get())) { |
michael@0 | 343 | return false; |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | plot = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
michael@0 | 347 | glyph->height(), storage.get(), |
michael@0 | 348 | &glyph->fAtlasLocation); |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | if (NULL == plot) { |
michael@0 | 352 | return false; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | glyph->fPlot = plot; |
michael@0 | 356 | return true; |
michael@0 | 357 | } |