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 | #include "SkConfig8888.h" |
michael@0 | 2 | #include "SkMathPriv.h" |
michael@0 | 3 | #include "SkUnPreMultiply.h" |
michael@0 | 4 | |
michael@0 | 5 | namespace { |
michael@0 | 6 | |
michael@0 | 7 | template <int A_IDX, int R_IDX, int G_IDX, int B_IDX> |
michael@0 | 8 | inline uint32_t pack_config8888(uint32_t a, uint32_t r, |
michael@0 | 9 | uint32_t g, uint32_t b) { |
michael@0 | 10 | #ifdef SK_CPU_LENDIAN |
michael@0 | 11 | return (a << (A_IDX * 8)) | (r << (R_IDX * 8)) | |
michael@0 | 12 | (g << (G_IDX * 8)) | (b << (B_IDX * 8)); |
michael@0 | 13 | #else |
michael@0 | 14 | return (a << ((3-A_IDX) * 8)) | (r << ((3-R_IDX) * 8)) | |
michael@0 | 15 | (g << ((3-G_IDX) * 8)) | (b << ((3-B_IDX) * 8)); |
michael@0 | 16 | #endif |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | template <int A_IDX, int R_IDX, int G_IDX, int B_IDX> |
michael@0 | 20 | inline void unpack_config8888(uint32_t color, |
michael@0 | 21 | uint32_t* a, uint32_t* r, |
michael@0 | 22 | uint32_t* g, uint32_t* b) { |
michael@0 | 23 | #ifdef SK_CPU_LENDIAN |
michael@0 | 24 | *a = (color >> (A_IDX * 8)) & 0xff; |
michael@0 | 25 | *r = (color >> (R_IDX * 8)) & 0xff; |
michael@0 | 26 | *g = (color >> (G_IDX * 8)) & 0xff; |
michael@0 | 27 | *b = (color >> (B_IDX * 8)) & 0xff; |
michael@0 | 28 | #else |
michael@0 | 29 | *a = (color >> ((3 - A_IDX) * 8)) & 0xff; |
michael@0 | 30 | *r = (color >> ((3 - R_IDX) * 8)) & 0xff; |
michael@0 | 31 | *g = (color >> ((3 - G_IDX) * 8)) & 0xff; |
michael@0 | 32 | *b = (color >> ((3 - B_IDX) * 8)) & 0xff; |
michael@0 | 33 | #endif |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | #ifdef SK_CPU_LENDIAN |
michael@0 | 37 | static const int SK_NATIVE_A_IDX = SK_A32_SHIFT / 8; |
michael@0 | 38 | static const int SK_NATIVE_R_IDX = SK_R32_SHIFT / 8; |
michael@0 | 39 | static const int SK_NATIVE_G_IDX = SK_G32_SHIFT / 8; |
michael@0 | 40 | static const int SK_NATIVE_B_IDX = SK_B32_SHIFT / 8; |
michael@0 | 41 | #else |
michael@0 | 42 | static const int SK_NATIVE_A_IDX = 3 - (SK_A32_SHIFT / 8); |
michael@0 | 43 | static const int SK_NATIVE_R_IDX = 3 - (SK_R32_SHIFT / 8); |
michael@0 | 44 | static const int SK_NATIVE_G_IDX = 3 - (SK_G32_SHIFT / 8); |
michael@0 | 45 | static const int SK_NATIVE_B_IDX = 3 - (SK_B32_SHIFT / 8); |
michael@0 | 46 | #endif |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * convert_pixel<OUT_CFG, IN_CFG converts a pixel value from one Config8888 to |
michael@0 | 50 | * another. It is implemented by first expanding OUT_CFG to r, g, b, a indices |
michael@0 | 51 | * and an is_premul bool as params to another template function. Then IN_CFG is |
michael@0 | 52 | * expanded via another function call. |
michael@0 | 53 | */ |
michael@0 | 54 | |
michael@0 | 55 | template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX, |
michael@0 | 56 | bool IN_PM, int IN_A_IDX, int IN_R_IDX, int IN_G_IDX, int IN_B_IDX> |
michael@0 | 57 | inline uint32_t convert_pixel(uint32_t pixel) { |
michael@0 | 58 | uint32_t a, r, g, b; |
michael@0 | 59 | unpack_config8888<IN_A_IDX, IN_R_IDX, IN_G_IDX, IN_B_IDX>(pixel, &a, &r, &g, &b); |
michael@0 | 60 | if (IN_PM && !OUT_PM) { |
michael@0 | 61 | // Using SkUnPreMultiply::ApplyScale is faster than (value * 0xff) / a. |
michael@0 | 62 | if (a) { |
michael@0 | 63 | SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(a); |
michael@0 | 64 | r = SkUnPreMultiply::ApplyScale(scale, r); |
michael@0 | 65 | g = SkUnPreMultiply::ApplyScale(scale, g); |
michael@0 | 66 | b = SkUnPreMultiply::ApplyScale(scale, b); |
michael@0 | 67 | } else { |
michael@0 | 68 | return 0; |
michael@0 | 69 | } |
michael@0 | 70 | } else if (!IN_PM && OUT_PM) { |
michael@0 | 71 | // This matches SkUnPreMultiply conversion which we are replacing. |
michael@0 | 72 | r = SkMulDiv255Round(r, a); |
michael@0 | 73 | g = SkMulDiv255Round(g, a); |
michael@0 | 74 | b = SkMulDiv255Round(b, a); |
michael@0 | 75 | } |
michael@0 | 76 | return pack_config8888<OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX>(a, r, g, b); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX, SkCanvas::Config8888 IN_CFG> |
michael@0 | 80 | inline uint32_t convert_pixel(uint32_t pixel) { |
michael@0 | 81 | switch(IN_CFG) { |
michael@0 | 82 | case SkCanvas::kNative_Premul_Config8888: |
michael@0 | 83 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 84 | true, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel); |
michael@0 | 85 | break; |
michael@0 | 86 | case SkCanvas::kNative_Unpremul_Config8888: |
michael@0 | 87 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 88 | false, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel); |
michael@0 | 89 | break; |
michael@0 | 90 | case SkCanvas::kBGRA_Premul_Config8888: |
michael@0 | 91 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 92 | true, 3, 2, 1, 0>(pixel); |
michael@0 | 93 | break; |
michael@0 | 94 | case SkCanvas::kBGRA_Unpremul_Config8888: |
michael@0 | 95 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 96 | false, 3, 2, 1, 0>(pixel); |
michael@0 | 97 | break; |
michael@0 | 98 | case SkCanvas::kRGBA_Premul_Config8888: |
michael@0 | 99 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 100 | true, 3, 0, 1, 2>(pixel); |
michael@0 | 101 | break; |
michael@0 | 102 | case SkCanvas::kRGBA_Unpremul_Config8888: |
michael@0 | 103 | return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, |
michael@0 | 104 | false, 3, 0, 1, 2>(pixel); |
michael@0 | 105 | break; |
michael@0 | 106 | default: |
michael@0 | 107 | SkDEBUGFAIL("Unexpected config8888"); |
michael@0 | 108 | return 0; |
michael@0 | 109 | break; |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | template <SkCanvas::Config8888 OUT_CFG, SkCanvas::Config8888 IN_CFG> |
michael@0 | 114 | inline uint32_t convert_pixel(uint32_t pixel) { |
michael@0 | 115 | switch(OUT_CFG) { |
michael@0 | 116 | case SkCanvas::kNative_Premul_Config8888: |
michael@0 | 117 | return convert_pixel<true, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel); |
michael@0 | 118 | break; |
michael@0 | 119 | case SkCanvas::kNative_Unpremul_Config8888: |
michael@0 | 120 | return convert_pixel<false, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel); |
michael@0 | 121 | break; |
michael@0 | 122 | case SkCanvas::kBGRA_Premul_Config8888: |
michael@0 | 123 | return convert_pixel<true, 3, 2, 1, 0, IN_CFG>(pixel); |
michael@0 | 124 | break; |
michael@0 | 125 | case SkCanvas::kBGRA_Unpremul_Config8888: |
michael@0 | 126 | return convert_pixel<false, 3, 2, 1, 0, IN_CFG>(pixel); |
michael@0 | 127 | break; |
michael@0 | 128 | case SkCanvas::kRGBA_Premul_Config8888: |
michael@0 | 129 | return convert_pixel<true, 3, 0, 1, 2, IN_CFG>(pixel); |
michael@0 | 130 | break; |
michael@0 | 131 | case SkCanvas::kRGBA_Unpremul_Config8888: |
michael@0 | 132 | return convert_pixel<false, 3, 0, 1, 2, IN_CFG>(pixel); |
michael@0 | 133 | break; |
michael@0 | 134 | default: |
michael@0 | 135 | SkDEBUGFAIL("Unexpected config8888"); |
michael@0 | 136 | return 0; |
michael@0 | 137 | break; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * SkConvertConfig8888Pixels has 6 * 6 possible combinations of src and dst |
michael@0 | 143 | * configs. Each is implemented as an instantiation templated function. Two |
michael@0 | 144 | * levels of switch statements are used to select the correct instantiation, one |
michael@0 | 145 | * for the src config and one for the dst config. |
michael@0 | 146 | */ |
michael@0 | 147 | |
michael@0 | 148 | template <SkCanvas::Config8888 DST_CFG, SkCanvas::Config8888 SRC_CFG> |
michael@0 | 149 | inline void convert_config8888(uint32_t* dstPixels, |
michael@0 | 150 | size_t dstRowBytes, |
michael@0 | 151 | const uint32_t* srcPixels, |
michael@0 | 152 | size_t srcRowBytes, |
michael@0 | 153 | int width, |
michael@0 | 154 | int height) { |
michael@0 | 155 | intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels); |
michael@0 | 156 | intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels); |
michael@0 | 157 | |
michael@0 | 158 | for (int y = 0; y < height; ++y) { |
michael@0 | 159 | srcPixels = reinterpret_cast<const uint32_t*>(srcPix); |
michael@0 | 160 | dstPixels = reinterpret_cast<uint32_t*>(dstPix); |
michael@0 | 161 | for (int x = 0; x < width; ++x) { |
michael@0 | 162 | dstPixels[x] = convert_pixel<DST_CFG, SRC_CFG>(srcPixels[x]); |
michael@0 | 163 | } |
michael@0 | 164 | dstPix += dstRowBytes; |
michael@0 | 165 | srcPix += srcRowBytes; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | template <SkCanvas::Config8888 SRC_CFG> |
michael@0 | 170 | inline void convert_config8888(uint32_t* dstPixels, |
michael@0 | 171 | size_t dstRowBytes, |
michael@0 | 172 | SkCanvas::Config8888 dstConfig, |
michael@0 | 173 | const uint32_t* srcPixels, |
michael@0 | 174 | size_t srcRowBytes, |
michael@0 | 175 | int width, |
michael@0 | 176 | int height) { |
michael@0 | 177 | switch(dstConfig) { |
michael@0 | 178 | case SkCanvas::kNative_Premul_Config8888: |
michael@0 | 179 | convert_config8888<SkCanvas::kNative_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 180 | break; |
michael@0 | 181 | case SkCanvas::kNative_Unpremul_Config8888: |
michael@0 | 182 | convert_config8888<SkCanvas::kNative_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 183 | break; |
michael@0 | 184 | case SkCanvas::kBGRA_Premul_Config8888: |
michael@0 | 185 | convert_config8888<SkCanvas::kBGRA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 186 | break; |
michael@0 | 187 | case SkCanvas::kBGRA_Unpremul_Config8888: |
michael@0 | 188 | convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 189 | break; |
michael@0 | 190 | case SkCanvas::kRGBA_Premul_Config8888: |
michael@0 | 191 | convert_config8888<SkCanvas::kRGBA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 192 | break; |
michael@0 | 193 | case SkCanvas::kRGBA_Unpremul_Config8888: |
michael@0 | 194 | convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); |
michael@0 | 195 | break; |
michael@0 | 196 | default: |
michael@0 | 197 | SkDEBUGFAIL("Unexpected config8888"); |
michael@0 | 198 | break; |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | void SkConvertConfig8888Pixels(uint32_t* dstPixels, |
michael@0 | 205 | size_t dstRowBytes, |
michael@0 | 206 | SkCanvas::Config8888 dstConfig, |
michael@0 | 207 | const uint32_t* srcPixels, |
michael@0 | 208 | size_t srcRowBytes, |
michael@0 | 209 | SkCanvas::Config8888 srcConfig, |
michael@0 | 210 | int width, |
michael@0 | 211 | int height) { |
michael@0 | 212 | if (srcConfig == dstConfig) { |
michael@0 | 213 | if (srcPixels == dstPixels) { |
michael@0 | 214 | return; |
michael@0 | 215 | } |
michael@0 | 216 | if (dstRowBytes == srcRowBytes && |
michael@0 | 217 | 4U * width == srcRowBytes) { |
michael@0 | 218 | memcpy(dstPixels, srcPixels, srcRowBytes * height); |
michael@0 | 219 | return; |
michael@0 | 220 | } else { |
michael@0 | 221 | intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels); |
michael@0 | 222 | intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels); |
michael@0 | 223 | for (int y = 0; y < height; ++y) { |
michael@0 | 224 | srcPixels = reinterpret_cast<const uint32_t*>(srcPix); |
michael@0 | 225 | dstPixels = reinterpret_cast<uint32_t*>(dstPix); |
michael@0 | 226 | memcpy(dstPixels, srcPixels, 4 * width); |
michael@0 | 227 | srcPix += srcRowBytes; |
michael@0 | 228 | dstPix += dstRowBytes; |
michael@0 | 229 | } |
michael@0 | 230 | return; |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | switch(srcConfig) { |
michael@0 | 234 | case SkCanvas::kNative_Premul_Config8888: |
michael@0 | 235 | convert_config8888<SkCanvas::kNative_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 236 | break; |
michael@0 | 237 | case SkCanvas::kNative_Unpremul_Config8888: |
michael@0 | 238 | convert_config8888<SkCanvas::kNative_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 239 | break; |
michael@0 | 240 | case SkCanvas::kBGRA_Premul_Config8888: |
michael@0 | 241 | convert_config8888<SkCanvas::kBGRA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 242 | break; |
michael@0 | 243 | case SkCanvas::kBGRA_Unpremul_Config8888: |
michael@0 | 244 | convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 245 | break; |
michael@0 | 246 | case SkCanvas::kRGBA_Premul_Config8888: |
michael@0 | 247 | convert_config8888<SkCanvas::kRGBA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 248 | break; |
michael@0 | 249 | case SkCanvas::kRGBA_Unpremul_Config8888: |
michael@0 | 250 | convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); |
michael@0 | 251 | break; |
michael@0 | 252 | default: |
michael@0 | 253 | SkDEBUGFAIL("Unexpected config8888"); |
michael@0 | 254 | break; |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | uint32_t SkPackConfig8888(SkCanvas::Config8888 config, |
michael@0 | 259 | uint32_t a, |
michael@0 | 260 | uint32_t r, |
michael@0 | 261 | uint32_t g, |
michael@0 | 262 | uint32_t b) { |
michael@0 | 263 | switch (config) { |
michael@0 | 264 | case SkCanvas::kNative_Premul_Config8888: |
michael@0 | 265 | case SkCanvas::kNative_Unpremul_Config8888: |
michael@0 | 266 | return pack_config8888<SK_NATIVE_A_IDX, |
michael@0 | 267 | SK_NATIVE_R_IDX, |
michael@0 | 268 | SK_NATIVE_G_IDX, |
michael@0 | 269 | SK_NATIVE_B_IDX>(a, r, g, b); |
michael@0 | 270 | case SkCanvas::kBGRA_Premul_Config8888: |
michael@0 | 271 | case SkCanvas::kBGRA_Unpremul_Config8888: |
michael@0 | 272 | return pack_config8888<3, 2, 1, 0>(a, r, g, b); |
michael@0 | 273 | case SkCanvas::kRGBA_Premul_Config8888: |
michael@0 | 274 | case SkCanvas::kRGBA_Unpremul_Config8888: |
michael@0 | 275 | return pack_config8888<3, 0, 1, 2>(a, r, g, b); |
michael@0 | 276 | default: |
michael@0 | 277 | SkDEBUGFAIL("Unexpected config8888"); |
michael@0 | 278 | return 0; |
michael@0 | 279 | } |
michael@0 | 280 | } |