gfx/skia/trunk/src/gpu/SkGr.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 "SkGr.h"
michael@0 9 #include "SkConfig8888.h"
michael@0 10 #include "SkMessageBus.h"
michael@0 11 #include "SkPixelRef.h"
michael@0 12 #include "GrResourceCache.h"
michael@0 13
michael@0 14 /* Fill out buffer with the compressed format Ganesh expects from a colortable
michael@0 15 based bitmap. [palette (colortable) + indices].
michael@0 16
michael@0 17 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
michael@0 18 we could detect that the colortable.count is <= 16, and then repack the
michael@0 19 indices as nibbles to save RAM, but it would take more time (i.e. a lot
michael@0 20 slower than memcpy), so skipping that for now.
michael@0 21
michael@0 22 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
michael@0 23 as the colortable.count says it is.
michael@0 24 */
michael@0 25 static void build_compressed_data(void* buffer, const SkBitmap& bitmap) {
michael@0 26 SkASSERT(SkBitmap::kIndex8_Config == bitmap.config());
michael@0 27
michael@0 28 SkAutoLockPixels alp(bitmap);
michael@0 29 if (!bitmap.readyToDraw()) {
michael@0 30 SkDEBUGFAIL("bitmap not ready to draw!");
michael@0 31 return;
michael@0 32 }
michael@0 33
michael@0 34 SkColorTable* ctable = bitmap.getColorTable();
michael@0 35 char* dst = (char*)buffer;
michael@0 36
michael@0 37 uint32_t* colorTableDst = reinterpret_cast<uint32_t*>(dst);
michael@0 38 const uint32_t* colorTableSrc = reinterpret_cast<const uint32_t*>(ctable->lockColors());
michael@0 39 SkConvertConfig8888Pixels(colorTableDst, 0, SkCanvas::kRGBA_Premul_Config8888,
michael@0 40 colorTableSrc, 0, SkCanvas::kNative_Premul_Config8888,
michael@0 41 ctable->count(), 1);
michael@0 42 ctable->unlockColors();
michael@0 43
michael@0 44 // always skip a full 256 number of entries, even if we memcpy'd fewer
michael@0 45 dst += kGrColorTableSize;
michael@0 46
michael@0 47 if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
michael@0 48 memcpy(dst, bitmap.getPixels(), bitmap.getSize());
michael@0 49 } else {
michael@0 50 // need to trim off the extra bytes per row
michael@0 51 size_t width = bitmap.width();
michael@0 52 size_t rowBytes = bitmap.rowBytes();
michael@0 53 const char* src = (const char*)bitmap.getPixels();
michael@0 54 for (int y = 0; y < bitmap.height(); y++) {
michael@0 55 memcpy(dst, src, width);
michael@0 56 src += rowBytes;
michael@0 57 dst += width;
michael@0 58 }
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 ////////////////////////////////////////////////////////////////////////////////
michael@0 63
michael@0 64 static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) {
michael@0 65 // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
michael@0 66 // are unique.
michael@0 67 uint32_t genID = bitmap.getGenerationID();
michael@0 68 SkIPoint origin = bitmap.pixelRefOrigin();
michael@0 69 int16_t width = SkToS16(bitmap.width());
michael@0 70 int16_t height = SkToS16(bitmap.height());
michael@0 71
michael@0 72 GrCacheID::Key key;
michael@0 73 memcpy(key.fData8 + 0, &genID, 4);
michael@0 74 memcpy(key.fData8 + 4, &origin.fX, 4);
michael@0 75 memcpy(key.fData8 + 8, &origin.fY, 4);
michael@0 76 memcpy(key.fData8 + 12, &width, 2);
michael@0 77 memcpy(key.fData8 + 14, &height, 2);
michael@0 78 static const size_t kKeyDataSize = 16;
michael@0 79 memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize);
michael@0 80 GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize);
michael@0 81 static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
michael@0 82 id->reset(gBitmapTextureDomain, key);
michael@0 83 }
michael@0 84
michael@0 85 static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
michael@0 86 desc->fFlags = kNone_GrTextureFlags;
michael@0 87 desc->fWidth = bitmap.width();
michael@0 88 desc->fHeight = bitmap.height();
michael@0 89 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
michael@0 90 desc->fSampleCnt = 0;
michael@0 91 }
michael@0 92
michael@0 93 namespace {
michael@0 94
michael@0 95 // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
michael@0 96 class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
michael@0 97 public:
michael@0 98 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
michael@0 99 private:
michael@0 100 GrResourceKey fKey;
michael@0 101
michael@0 102 virtual void onChange() SK_OVERRIDE {
michael@0 103 const GrResourceInvalidatedMessage message = { fKey };
michael@0 104 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
michael@0 105 }
michael@0 106 };
michael@0 107
michael@0 108 } // namespace
michael@0 109
michael@0 110 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
michael@0 111 SkASSERT(NULL != pixelRef);
michael@0 112 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
michael@0 113 }
michael@0 114
michael@0 115 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
michael@0 116 bool cache,
michael@0 117 const GrTextureParams* params,
michael@0 118 const SkBitmap& origBitmap) {
michael@0 119 SkBitmap tmpBitmap;
michael@0 120
michael@0 121 const SkBitmap* bitmap = &origBitmap;
michael@0 122
michael@0 123 GrTextureDesc desc;
michael@0 124 generate_bitmap_texture_desc(*bitmap, &desc);
michael@0 125
michael@0 126 if (SkBitmap::kIndex8_Config == bitmap->config()) {
michael@0 127 // build_compressed_data doesn't do npot->pot expansion
michael@0 128 // and paletted textures can't be sub-updated
michael@0 129 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) {
michael@0 130 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize;
michael@0 131 SkAutoMalloc storage(imagesize);
michael@0 132
michael@0 133 build_compressed_data(storage.get(), origBitmap);
michael@0 134
michael@0 135 // our compressed data will be trimmed, so pass width() for its
michael@0 136 // "rowBytes", since they are the same now.
michael@0 137
michael@0 138 if (cache) {
michael@0 139 GrCacheID cacheID;
michael@0 140 generate_bitmap_cache_id(origBitmap, &cacheID);
michael@0 141
michael@0 142 GrResourceKey key;
michael@0 143 GrTexture* result = ctx->createTexture(params, desc, cacheID,
michael@0 144 storage.get(), bitmap->width(), &key);
michael@0 145 if (NULL != result) {
michael@0 146 add_genID_listener(key, origBitmap.pixelRef());
michael@0 147 }
michael@0 148 return result;
michael@0 149 } else {
michael@0 150 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
michael@0 151 GrContext::kExact_ScratchTexMatch);
michael@0 152 result->writePixels(0, 0, bitmap->width(),
michael@0 153 bitmap->height(), desc.fConfig,
michael@0 154 storage.get());
michael@0 155 return result;
michael@0 156 }
michael@0 157 } else {
michael@0 158 origBitmap.copyTo(&tmpBitmap, kPMColor_SkColorType);
michael@0 159 // now bitmap points to our temp, which has been promoted to 32bits
michael@0 160 bitmap = &tmpBitmap;
michael@0 161 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 SkAutoLockPixels alp(*bitmap);
michael@0 166 if (!bitmap->readyToDraw()) {
michael@0 167 return NULL;
michael@0 168 }
michael@0 169 if (cache) {
michael@0 170 // This texture is likely to be used again so leave it in the cache
michael@0 171 GrCacheID cacheID;
michael@0 172 generate_bitmap_cache_id(origBitmap, &cacheID);
michael@0 173
michael@0 174 GrResourceKey key;
michael@0 175 GrTexture* result = ctx->createTexture(params, desc, cacheID,
michael@0 176 bitmap->getPixels(), bitmap->rowBytes(), &key);
michael@0 177 if (NULL != result) {
michael@0 178 add_genID_listener(key, origBitmap.pixelRef());
michael@0 179 }
michael@0 180 return result;
michael@0 181 } else {
michael@0 182 // This texture is unlikely to be used again (in its present form) so
michael@0 183 // just use a scratch texture. This will remove the texture from the
michael@0 184 // cache so no one else can find it. Additionally, once unlocked, the
michael@0 185 // scratch texture will go to the end of the list for purging so will
michael@0 186 // likely be available for this volatile bitmap the next time around.
michael@0 187 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
michael@0 188 result->writePixels(0, 0,
michael@0 189 bitmap->width(), bitmap->height(),
michael@0 190 desc.fConfig,
michael@0 191 bitmap->getPixels(),
michael@0 192 bitmap->rowBytes());
michael@0 193 return result;
michael@0 194 }
michael@0 195 }
michael@0 196
michael@0 197 bool GrIsBitmapInCache(const GrContext* ctx,
michael@0 198 const SkBitmap& bitmap,
michael@0 199 const GrTextureParams* params) {
michael@0 200 GrCacheID cacheID;
michael@0 201 generate_bitmap_cache_id(bitmap, &cacheID);
michael@0 202
michael@0 203 GrTextureDesc desc;
michael@0 204 generate_bitmap_texture_desc(bitmap, &desc);
michael@0 205 return ctx->isTextureInCache(desc, cacheID, params);
michael@0 206 }
michael@0 207
michael@0 208 GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx,
michael@0 209 const SkBitmap& bitmap,
michael@0 210 const GrTextureParams* params) {
michael@0 211 GrTexture* result = NULL;
michael@0 212
michael@0 213 bool cache = !bitmap.isVolatile();
michael@0 214
michael@0 215 if (cache) {
michael@0 216 // If the bitmap isn't changing try to find a cached copy first.
michael@0 217
michael@0 218 GrCacheID cacheID;
michael@0 219 generate_bitmap_cache_id(bitmap, &cacheID);
michael@0 220
michael@0 221 GrTextureDesc desc;
michael@0 222 generate_bitmap_texture_desc(bitmap, &desc);
michael@0 223
michael@0 224 result = ctx->findAndRefTexture(desc, cacheID, params);
michael@0 225 }
michael@0 226 if (NULL == result) {
michael@0 227 result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
michael@0 228 }
michael@0 229 if (NULL == result) {
michael@0 230 GrPrintf("---- failed to create texture for cache [%d %d]\n",
michael@0 231 bitmap.width(), bitmap.height());
michael@0 232 }
michael@0 233 return result;
michael@0 234 }
michael@0 235
michael@0 236 void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) {
michael@0 237 SkASSERT(NULL != texture->getContext());
michael@0 238
michael@0 239 texture->getContext()->unlockScratchTexture(texture);
michael@0 240 texture->unref();
michael@0 241 }
michael@0 242
michael@0 243 ///////////////////////////////////////////////////////////////////////////////
michael@0 244
michael@0 245 GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) {
michael@0 246 switch (config) {
michael@0 247 case SkBitmap::kA8_Config:
michael@0 248 return kAlpha_8_GrPixelConfig;
michael@0 249 case SkBitmap::kIndex8_Config:
michael@0 250 return kIndex_8_GrPixelConfig;
michael@0 251 case SkBitmap::kRGB_565_Config:
michael@0 252 return kRGB_565_GrPixelConfig;
michael@0 253 case SkBitmap::kARGB_4444_Config:
michael@0 254 return kRGBA_4444_GrPixelConfig;
michael@0 255 case SkBitmap::kARGB_8888_Config:
michael@0 256 return kSkia8888_GrPixelConfig;
michael@0 257 default:
michael@0 258 // kNo_Config, kA1_Config missing
michael@0 259 return kUnknown_GrPixelConfig;
michael@0 260 }
michael@0 261 }
michael@0 262
michael@0 263 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
michael@0 264 // alpha info, that will be considered.
michael@0 265 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) {
michael@0 266 switch (ct) {
michael@0 267 case kUnknown_SkColorType:
michael@0 268 return kUnknown_GrPixelConfig;
michael@0 269 case kAlpha_8_SkColorType:
michael@0 270 return kAlpha_8_GrPixelConfig;
michael@0 271 case kRGB_565_SkColorType:
michael@0 272 return kRGB_565_GrPixelConfig;
michael@0 273 case kARGB_4444_SkColorType:
michael@0 274 return kRGBA_4444_GrPixelConfig;
michael@0 275 case kRGBA_8888_SkColorType:
michael@0 276 return kRGBA_8888_GrPixelConfig;
michael@0 277 case kBGRA_8888_SkColorType:
michael@0 278 return kBGRA_8888_GrPixelConfig;
michael@0 279 case kIndex_8_SkColorType:
michael@0 280 return kIndex_8_GrPixelConfig;
michael@0 281 }
michael@0 282 SkASSERT(0); // shouldn't get here
michael@0 283 return kUnknown_GrPixelConfig;
michael@0 284 }
michael@0 285
michael@0 286 bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) {
michael@0 287 SkColorType ct;
michael@0 288 switch (config) {
michael@0 289 case kAlpha_8_GrPixelConfig:
michael@0 290 ct = kAlpha_8_SkColorType;
michael@0 291 break;
michael@0 292 case kIndex_8_GrPixelConfig:
michael@0 293 ct = kIndex_8_SkColorType;
michael@0 294 break;
michael@0 295 case kRGB_565_GrPixelConfig:
michael@0 296 ct = kRGB_565_SkColorType;
michael@0 297 break;
michael@0 298 case kRGBA_4444_GrPixelConfig:
michael@0 299 ct = kARGB_4444_SkColorType;
michael@0 300 break;
michael@0 301 case kRGBA_8888_GrPixelConfig:
michael@0 302 ct = kRGBA_8888_SkColorType;
michael@0 303 break;
michael@0 304 case kBGRA_8888_GrPixelConfig:
michael@0 305 ct = kBGRA_8888_SkColorType;
michael@0 306 break;
michael@0 307 default:
michael@0 308 return false;
michael@0 309 }
michael@0 310 if (ctOut) {
michael@0 311 *ctOut = ct;
michael@0 312 }
michael@0 313 return true;
michael@0 314 }

mercurial