gfx/skia/trunk/src/core/SkMipMap.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 2013 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 "SkMipMap.h"
michael@0 9 #include "SkBitmap.h"
michael@0 10 #include "SkColorPriv.h"
michael@0 11
michael@0 12 static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
michael@0 13 const SkBitmap& src) {
michael@0 14 x <<= 1;
michael@0 15 y <<= 1;
michael@0 16 const SkPMColor* p = src.getAddr32(x, y);
michael@0 17 const SkPMColor* baseP = p;
michael@0 18 SkPMColor c, ag, rb;
michael@0 19
michael@0 20 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
michael@0 21 if (x < src.width() - 1) {
michael@0 22 p += 1;
michael@0 23 }
michael@0 24 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
michael@0 25
michael@0 26 p = baseP;
michael@0 27 if (y < src.height() - 1) {
michael@0 28 p += src.rowBytes() >> 2;
michael@0 29 }
michael@0 30 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
michael@0 31 if (x < src.width() - 1) {
michael@0 32 p += 1;
michael@0 33 }
michael@0 34 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
michael@0 35
michael@0 36 *dst->getAddr32(x >> 1, y >> 1) =
michael@0 37 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
michael@0 38 }
michael@0 39
michael@0 40 static inline uint32_t expand16(U16CPU c) {
michael@0 41 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
michael@0 42 }
michael@0 43
michael@0 44 // returns dirt in the top 16bits, but we don't care, since we only
michael@0 45 // store the low 16bits.
michael@0 46 static inline U16CPU pack16(uint32_t c) {
michael@0 47 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
michael@0 48 }
michael@0 49
michael@0 50 static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
michael@0 51 const SkBitmap& src) {
michael@0 52 x <<= 1;
michael@0 53 y <<= 1;
michael@0 54 const uint16_t* p = src.getAddr16(x, y);
michael@0 55 const uint16_t* baseP = p;
michael@0 56 SkPMColor c;
michael@0 57
michael@0 58 c = expand16(*p);
michael@0 59 if (x < src.width() - 1) {
michael@0 60 p += 1;
michael@0 61 }
michael@0 62 c += expand16(*p);
michael@0 63
michael@0 64 p = baseP;
michael@0 65 if (y < src.height() - 1) {
michael@0 66 p += src.rowBytes() >> 1;
michael@0 67 }
michael@0 68 c += expand16(*p);
michael@0 69 if (x < src.width() - 1) {
michael@0 70 p += 1;
michael@0 71 }
michael@0 72 c += expand16(*p);
michael@0 73
michael@0 74 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
michael@0 75 }
michael@0 76
michael@0 77 static uint32_t expand4444(U16CPU c) {
michael@0 78 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
michael@0 79 }
michael@0 80
michael@0 81 static U16CPU collaps4444(uint32_t c) {
michael@0 82 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
michael@0 83 }
michael@0 84
michael@0 85 static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
michael@0 86 const SkBitmap& src) {
michael@0 87 x <<= 1;
michael@0 88 y <<= 1;
michael@0 89 const uint16_t* p = src.getAddr16(x, y);
michael@0 90 const uint16_t* baseP = p;
michael@0 91 uint32_t c;
michael@0 92
michael@0 93 c = expand4444(*p);
michael@0 94 if (x < src.width() - 1) {
michael@0 95 p += 1;
michael@0 96 }
michael@0 97 c += expand4444(*p);
michael@0 98
michael@0 99 p = baseP;
michael@0 100 if (y < src.height() - 1) {
michael@0 101 p += src.rowBytes() >> 1;
michael@0 102 }
michael@0 103 c += expand4444(*p);
michael@0 104 if (x < src.width() - 1) {
michael@0 105 p += 1;
michael@0 106 }
michael@0 107 c += expand4444(*p);
michael@0 108
michael@0 109 *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
michael@0 110 }
michael@0 111
michael@0 112 SkMipMap::Level* SkMipMap::AllocLevels(int levelCount, size_t pixelSize) {
michael@0 113 if (levelCount < 0) {
michael@0 114 return NULL;
michael@0 115 }
michael@0 116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
michael@0 117 if (!sk_64_isS32(size)) {
michael@0 118 return NULL;
michael@0 119 }
michael@0 120 return (Level*)sk_malloc_throw(sk_64_asS32(size));
michael@0 121 }
michael@0 122
michael@0 123 SkMipMap* SkMipMap::Build(const SkBitmap& src) {
michael@0 124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
michael@0 125
michael@0 126 const SkBitmap::Config config = src.config();
michael@0 127 switch (config) {
michael@0 128 case SkBitmap::kARGB_8888_Config:
michael@0 129 proc = downsampleby2_proc32;
michael@0 130 break;
michael@0 131 case SkBitmap::kRGB_565_Config:
michael@0 132 proc = downsampleby2_proc16;
michael@0 133 break;
michael@0 134 case SkBitmap::kARGB_4444_Config:
michael@0 135 proc = downsampleby2_proc4444;
michael@0 136 break;
michael@0 137 case SkBitmap::kIndex8_Config:
michael@0 138 case SkBitmap::kA8_Config:
michael@0 139 default:
michael@0 140 return NULL; // don't build mipmaps for these configs
michael@0 141 }
michael@0 142
michael@0 143 SkAutoLockPixels alp(src);
michael@0 144 if (!src.readyToDraw()) {
michael@0 145 return NULL;
michael@0 146 }
michael@0 147
michael@0 148 // whip through our loop to compute the exact size needed
michael@0 149 size_t size = 0;
michael@0 150 int countLevels = 0;
michael@0 151 {
michael@0 152 int width = src.width();
michael@0 153 int height = src.height();
michael@0 154 for (;;) {
michael@0 155 width >>= 1;
michael@0 156 height >>= 1;
michael@0 157 if (0 == width || 0 == height) {
michael@0 158 break;
michael@0 159 }
michael@0 160 size += SkBitmap::ComputeRowBytes(config, width) * height;
michael@0 161 countLevels += 1;
michael@0 162 }
michael@0 163 }
michael@0 164 if (0 == countLevels) {
michael@0 165 return NULL;
michael@0 166 }
michael@0 167
michael@0 168 Level* levels = SkMipMap::AllocLevels(countLevels, size);
michael@0 169 if (NULL == levels) {
michael@0 170 return NULL;
michael@0 171 }
michael@0 172
michael@0 173 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
michael@0 174 uint8_t* addr = baseAddr;
michael@0 175 int width = src.width();
michael@0 176 int height = src.height();
michael@0 177 uint32_t rowBytes;
michael@0 178 SkBitmap srcBM(src);
michael@0 179
michael@0 180 for (int i = 0; i < countLevels; ++i) {
michael@0 181 width >>= 1;
michael@0 182 height >>= 1;
michael@0 183 rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width));
michael@0 184
michael@0 185 levels[i].fPixels = addr;
michael@0 186 levels[i].fWidth = width;
michael@0 187 levels[i].fHeight = height;
michael@0 188 levels[i].fRowBytes = rowBytes;
michael@0 189 levels[i].fScale = (float)width / src.width();
michael@0 190
michael@0 191 SkBitmap dstBM;
michael@0 192 dstBM.setConfig(config, width, height, rowBytes);
michael@0 193 dstBM.setPixels(addr);
michael@0 194
michael@0 195 srcBM.lockPixels();
michael@0 196 for (int y = 0; y < height; y++) {
michael@0 197 for (int x = 0; x < width; x++) {
michael@0 198 proc(&dstBM, x, y, srcBM);
michael@0 199 }
michael@0 200 }
michael@0 201 srcBM.unlockPixels();
michael@0 202
michael@0 203 srcBM = dstBM;
michael@0 204 addr += height * rowBytes;
michael@0 205 }
michael@0 206 SkASSERT(addr == baseAddr + size);
michael@0 207
michael@0 208 return SkNEW_ARGS(SkMipMap, (levels, countLevels, size));
michael@0 209 }
michael@0 210
michael@0 211 ///////////////////////////////////////////////////////////////////////////////
michael@0 212
michael@0 213 //static int gCounter;
michael@0 214
michael@0 215 SkMipMap::SkMipMap(Level* levels, int count, size_t size)
michael@0 216 : fSize(size), fLevels(levels), fCount(count) {
michael@0 217 SkASSERT(levels);
michael@0 218 SkASSERT(count > 0);
michael@0 219 // SkDebugf("mips %d\n", ++gCounter);
michael@0 220 }
michael@0 221
michael@0 222 SkMipMap::~SkMipMap() {
michael@0 223 sk_free(fLevels);
michael@0 224 // SkDebugf("mips %d\n", --gCounter);
michael@0 225 }
michael@0 226
michael@0 227 static SkFixed compute_level(SkScalar scale) {
michael@0 228 SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale)));
michael@0 229
michael@0 230 if (s < SK_Fixed1) {
michael@0 231 return 0;
michael@0 232 }
michael@0 233 int clz = SkCLZ(s);
michael@0 234 SkASSERT(clz >= 1 && clz <= 15);
michael@0 235 return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16);
michael@0 236 }
michael@0 237
michael@0 238 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const {
michael@0 239 if (scale >= SK_Scalar1) {
michael@0 240 return false;
michael@0 241 }
michael@0 242
michael@0 243 int level = compute_level(scale) >> 16;
michael@0 244 SkASSERT(level >= 0);
michael@0 245 if (level <= 0) {
michael@0 246 return false;
michael@0 247 }
michael@0 248
michael@0 249 if (level > fCount) {
michael@0 250 level = fCount;
michael@0 251 }
michael@0 252 if (levelPtr) {
michael@0 253 *levelPtr = fLevels[level - 1];
michael@0 254 }
michael@0 255 return true;
michael@0 256 }

mercurial