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