Use nested template expansions to implement Config8888 conversions.
Review URL: https://codereview.appspot.com/5690068

git-svn-id: http://skia.googlecode.com/svn/trunk@3230 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp
new file mode 100644
index 0000000..67a9e41
--- /dev/null
+++ b/src/core/SkConfig8888.cpp
@@ -0,0 +1,257 @@
+#include "SkConfig8888.h"
+
+namespace {
+
+template <int A_IDX, int R_IDX, int G_IDX, int B_IDX>
+inline uint32_t pack_config8888(uint32_t a, uint32_t r,
+                                uint32_t g, uint32_t b) {
+#ifdef SK_CPU_LENDIAN
+    return (a << (A_IDX * 8)) | (r << (R_IDX * 8)) |
+           (g << (G_IDX * 8)) | (b << (B_IDX * 8));
+#else
+    return (a << ((3-A_IDX) * 8)) | (r << ((3-R_IDX) * 8)) |
+           (g << ((3-G_IDX) * 8)) | (b << ((3-B_IDX) * 8));
+#endif
+}
+
+template <int A_IDX, int R_IDX, int G_IDX, int B_IDX>
+inline void unpack_config8888(uint32_t color,
+                              uint32_t* a, uint32_t* r,
+                              uint32_t* g, uint32_t* b) {
+#ifdef SK_CPU_LENDIAN
+    *a = (color >> (A_IDX * 8)) & 0xff;
+    *r = (color >> (R_IDX * 8)) & 0xff;
+    *g = (color >> (G_IDX * 8)) & 0xff;
+    *b = (color >> (B_IDX * 8)) & 0xff;
+#else
+    *a = (color >> ((3 - A_IDX) * 8)) & 0xff;
+    *r = (color >> ((3 - R_IDX) * 8)) & 0xff;
+    *g = (color >> ((3 - G_IDX) * 8)) & 0xff;
+    *b = (color >> ((3 - B_IDX) * 8)) & 0xff;
+#endif
+}
+
+#ifdef SK_CPU_LENDIAN
+    static const int SK_NATIVE_A_IDX = SK_A32_SHIFT / 8;
+    static const int SK_NATIVE_R_IDX = SK_R32_SHIFT / 8;
+    static const int SK_NATIVE_G_IDX = SK_G32_SHIFT / 8;
+    static const int SK_NATIVE_B_IDX = SK_B32_SHIFT / 8;
+#else
+    static const int SK_NATIVE_A_IDX = 3 - (SK_A32_SHIFT / 8);
+    static const int SK_NATIVE_R_IDX = 3 - (SK_R32_SHIFT / 8);
+    static const int SK_NATIVE_G_IDX = 3 - (SK_G32_SHIFT / 8);
+    static const int SK_NATIVE_B_IDX = 3 - (SK_B32_SHIFT / 8);
+#endif
+
+/**
+ * convert_pixel<OUT_CFG, IN_CFG converts a pixel value from one Config8888 to
+ * another. It is implemented by first expanding OUT_CFG to r, g, b, a indices
+ * and an is_premul bool as params to another template function. Then IN_CFG is
+ * expanded via another function call.
+ */
+
+template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX,
+          bool IN_PM,  int IN_A_IDX,  int IN_R_IDX,  int IN_G_IDX,  int IN_B_IDX>
+inline uint32_t convert_pixel(uint32_t pixel) {
+    uint32_t a, r, g, b;
+    unpack_config8888<IN_A_IDX, IN_R_IDX, IN_G_IDX, IN_B_IDX>(pixel, &a, &r, &g, &b);
+    if (IN_PM && !OUT_PM) {
+        // We're doing the explicit divide to match WebKit layout
+        // test expectations. We can modify and rebaseline if there
+        // it can be shown that there is a more performant way to
+        // unpremul.
+        if (a) {
+            r = r * 0xff / a;
+            g = g * 0xff / a;
+            b = b * 0xff / a;
+        } else {
+            return 0;
+        }
+    } else if (!IN_PM && OUT_PM) {
+        // This matches WebKit's conversion which we are replacing.
+        // We can consider alternative rounding rules for performance.
+        r = SkMulDiv255Ceiling(r, a);
+        g = SkMulDiv255Ceiling(g, a);
+        b = SkMulDiv255Ceiling(b, a);
+    }
+    return pack_config8888<OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX>(a, r, g, b);
+}
+
+template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX, SkCanvas::Config8888 IN_CFG>
+inline uint32_t convert_pixel(uint32_t pixel) {
+    switch(IN_CFG) {
+        case SkCanvas::kNative_Premul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX,       OUT_R_IDX,       OUT_G_IDX,       OUT_B_IDX,
+                                 true,  SK_NATIVE_A_IDX,  SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel);
+            break;
+        case SkCanvas::kNative_Unpremul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX,       OUT_R_IDX,       OUT_G_IDX,       OUT_B_IDX,
+                                 false,  SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel);
+            break;
+        case SkCanvas::kBGRA_Premul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX,
+                                 true,  3,         2,         1,         0>(pixel);
+            break;
+        case SkCanvas::kBGRA_Unpremul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX,
+                                 false,  3,         2,         1,         0>(pixel);
+            break;
+        case SkCanvas::kRGBA_Premul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX,
+                                 true,  3,         0,         1,         2>(pixel);
+            break;
+        case SkCanvas::kRGBA_Unpremul_Config8888:
+            return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX,
+                                 false,  3,         0,         1,         2>(pixel);
+            break;
+        default:
+            SkDEBUGFAIL("Unexpected config8888");
+            return 0;
+            break;
+    }
+}
+
+template <SkCanvas::Config8888 OUT_CFG, SkCanvas::Config8888 IN_CFG>
+inline uint32_t convert_pixel(uint32_t pixel) {
+    switch(OUT_CFG) {
+        case SkCanvas::kNative_Premul_Config8888:
+            return convert_pixel<true,  SK_NATIVE_A_IDX,  SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel);
+            break;
+        case SkCanvas::kNative_Unpremul_Config8888:
+            return convert_pixel<false,  SK_NATIVE_A_IDX,  SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel);
+            break;
+        case SkCanvas::kBGRA_Premul_Config8888:
+            return convert_pixel<true, 3, 2, 1, 0, IN_CFG>(pixel);
+            break;
+        case SkCanvas::kBGRA_Unpremul_Config8888:
+            return convert_pixel<false, 3, 2, 1, 0, IN_CFG>(pixel);
+            break;
+        case SkCanvas::kRGBA_Premul_Config8888:
+            return convert_pixel<true, 3, 0, 1, 2, IN_CFG>(pixel);
+            break;
+        case SkCanvas::kRGBA_Unpremul_Config8888:
+            return convert_pixel<false, 3, 0, 1, 2, IN_CFG>(pixel);
+            break;
+        default:
+            SkDEBUGFAIL("Unexpected config8888");
+            return 0;
+            break;
+    }
+}
+
+/**
+ * SkConvertConfig8888Pixels has 6 * 6 possible combinations of src and dst
+ * configs. Each is implemented as an instantiation templated function. Two
+ * levels of switch statements are used to select the correct instantiation, one
+ * for the src config and one for the dst config.
+ */
+
+template <SkCanvas::Config8888 DST_CFG, SkCanvas::Config8888 SRC_CFG>
+inline void convert_config8888(uint32_t* dstPixels,
+                               size_t dstRowBytes,
+                               const uint32_t* srcPixels,
+                               size_t srcRowBytes,
+                               int width,
+                               int height) {
+    intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels);
+    intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels);
+
+    for (int y = 0; y < height; ++y) {
+        srcPixels = reinterpret_cast<const uint32_t*>(srcPix);
+        dstPixels = reinterpret_cast<uint32_t*>(dstPix);
+        for (int x = 0; x < width; ++x) {
+            dstPixels[x] = convert_pixel<DST_CFG, SRC_CFG>(srcPixels[x]);
+        }
+        dstPix += dstRowBytes;
+        srcPix += srcRowBytes;
+    }
+}
+
+template <SkCanvas::Config8888 SRC_CFG>
+inline void convert_config8888(uint32_t* dstPixels,
+                               size_t dstRowBytes,
+                               SkCanvas::Config8888 dstConfig,
+                               const uint32_t* srcPixels,
+                               size_t srcRowBytes,
+                               int width,
+                               int height) {
+    switch(dstConfig) {
+        case SkCanvas::kNative_Premul_Config8888:
+            convert_config8888<SkCanvas::kNative_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kNative_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kNative_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kBGRA_Premul_Config8888:
+            convert_config8888<SkCanvas::kBGRA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kBGRA_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kRGBA_Premul_Config8888:
+            convert_config8888<SkCanvas::kRGBA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kRGBA_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height);
+            break;
+        default:
+            SkDEBUGFAIL("Unexpected config8888");
+            break;
+    }
+}
+
+}
+
+void SkConvertConfig8888Pixels(uint32_t* dstPixels,
+                               size_t dstRowBytes,
+                               SkCanvas::Config8888 dstConfig,
+                               const uint32_t* srcPixels,
+                               size_t srcRowBytes,
+                               SkCanvas::Config8888 srcConfig,
+                               int width,
+                               int height) {
+    if (srcConfig == dstConfig) {
+        if (srcPixels == dstPixels) {
+            return;
+        }
+        if (dstRowBytes == srcRowBytes &&
+            4U * width == srcRowBytes) {
+            memcpy(dstPixels, srcPixels, srcRowBytes * height);
+            return;
+        } else {
+            intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels);
+            intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels);
+            for (int y = 0; y < height; ++y) {
+                srcPixels = reinterpret_cast<const uint32_t*>(srcPix);
+                dstPixels = reinterpret_cast<uint32_t*>(dstPix);
+                memcpy(dstPixels, srcPixels, 4 * width);
+                srcPix += srcRowBytes;
+                dstPix += dstRowBytes;
+            }
+            return;
+        }
+    }
+    switch(srcConfig) {
+        case SkCanvas::kNative_Premul_Config8888:
+            convert_config8888<SkCanvas::kNative_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kNative_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kNative_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kBGRA_Premul_Config8888:
+            convert_config8888<SkCanvas::kBGRA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kBGRA_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kRGBA_Premul_Config8888:
+            convert_config8888<SkCanvas::kRGBA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        case SkCanvas::kRGBA_Unpremul_Config8888:
+            convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height);
+            break;
+        default:
+            SkDEBUGFAIL("Unexpected config8888");
+            break;
+    }
+}
diff --git a/src/core/SkConfig8888.h b/src/core/SkConfig8888.h
index fe2f2cc..d4ca764 100644
--- a/src/core/SkConfig8888.h
+++ b/src/core/SkConfig8888.h
@@ -10,6 +10,18 @@
 #include "SkCanvas.h"
 #include "SkColorPriv.h"
 
+/**
+ * Converts pixels from one Config8888 to another Config8888
+ */
+void SkConvertConfig8888Pixels(uint32_t* dstPixels,
+                               size_t dstRowBytes,
+                               SkCanvas::Config8888 dstConfig,
+                               const uint32_t* srcPixels,
+                               size_t srcRowBytes,
+                               SkCanvas::Config8888 srcConfig,
+                               int width,
+                               int height);
+
 namespace {
 
 /**
@@ -22,14 +34,6 @@
                                      const SkBitmap& srcBmp);
 
 /**
- * Copies all pixels in a bitmap to a dst ptr with row bytes. The src bitmap
- * is assumed to have pixels and be kARGB_8888_Config. No conversion is applied
- */
-inline void SkCopyARGB8888BitmapTo(uint32_t* dstPixels,
-                                   size_t dstRowBytes,
-                                   const SkBitmap& srcBmp);
-
-/**
   Copies over all pixels in a bitmap from a src ptr with a given rowBytes and
   Config8888. The bitmap must have pixels and be kARGB_8888_Config.
  */
@@ -45,243 +49,32 @@
 
 namespace {
 
-template <int A_IDX, int R_IDX, int G_IDX, int B_IDX>
-inline uint32_t pack_config8888(uint32_t a, uint32_t r,
-                                uint32_t g, uint32_t b) {
-#ifdef SK_CPU_LENDIAN
-    return (a << (A_IDX * 8)) | (r << (R_IDX * 8)) |
-           (g << (G_IDX * 8)) | (b << (B_IDX * 8));
-#else
-    return (a << ((3-A_IDX) * 8)) | (r << ((3-R_IDX) * 8)) |
-           (g << ((3-G_IDX) * 8)) | (b << ((3-B_IDX) * 8));
-#endif
-}
-
-template <int A_IDX, int R_IDX, int G_IDX, int B_IDX>
-inline void unpack_config8888(uint32_t color,
-                              uint32_t* a, uint32_t* r,
-                              uint32_t* g, uint32_t* b) {
-#ifdef SK_CPU_LENDIAN
-    *a = (color >> (A_IDX * 8)) & 0xff;
-    *r = (color >> (R_IDX * 8)) & 0xff;
-    *g = (color >> (G_IDX * 8)) & 0xff;
-    *b = (color >> (B_IDX * 8)) & 0xff;
-#else
-    *a = (color >> ((3 - A_IDX) * 8)) & 0xff;
-    *r = (color >> ((3 - R_IDX) * 8)) & 0xff;
-    *g = (color >> ((3 - G_IDX) * 8)) & 0xff;
-    *b = (color >> ((3 - B_IDX) * 8)) & 0xff;
-#endif
-}
-
-template <bool UNPM, int A_IDX, int R_IDX, int G_IDX, int B_IDX>
-inline void bitmap_copy_to_config8888(uint32_t* dstPixels,
-                                      size_t dstRowBytes,
-                                      const SkBitmap& srcBmp) {
+inline void SkCopyBitmapToConfig8888(uint32_t* dstPixels,
+                                     size_t dstRowBytes,
+                                     SkCanvas::Config8888 dstConfig8888,
+                                     const SkBitmap& srcBmp) {
     SkASSERT(SkBitmap::kARGB_8888_Config == srcBmp.config());
     SkAutoLockPixels alp(srcBmp);
     int w = srcBmp.width();
     int h = srcBmp.height();
     size_t srcRowBytes = srcBmp.rowBytes();
+    const uint32_t* srcPixels = reinterpret_cast<uint32_t*>(srcBmp.getPixels());
 
-    intptr_t src = reinterpret_cast<intptr_t>(srcBmp.getPixels());
-    intptr_t dst = reinterpret_cast<intptr_t>(dstPixels);
-
-    for (int y = 0; y < h; ++y) {
-        const SkPMColor* srcRow = reinterpret_cast<SkPMColor*>(src);
-        uint32_t* dstRow  = reinterpret_cast<uint32_t*>(dst);
-        for (int x = 0; x < w; ++x) {
-            SkPMColor pmcolor = srcRow[x];
-            if (UNPM) {
-                U8CPU a, r, g, b;
-                a = SkGetPackedA32(pmcolor);
-                if (a) {
-                    // We're doing the explicit divide to match WebKit layout
-                    // test expectations. We can modify and rebaseline if there
-                    // it can be shown that there is a more performant way to
-                    // unpremul.
-                    r = SkGetPackedR32(pmcolor) * 0xff / a;
-                    g = SkGetPackedG32(pmcolor) * 0xff / a;
-                    b = SkGetPackedB32(pmcolor) * 0xff / a;
-                    dstRow[x] = pack_config8888<A_IDX, R_IDX,
-                                                G_IDX, B_IDX>(a, r, g, b);
-                } else {
-                    dstRow[x] = 0;
-                }
-            } else {
-                dstRow[x] = pack_config8888<A_IDX, R_IDX,
-                                            G_IDX, B_IDX>(
-                                                   SkGetPackedA32(pmcolor),
-                                                   SkGetPackedR32(pmcolor),
-                                                   SkGetPackedG32(pmcolor),
-                                                   SkGetPackedB32(pmcolor));
-            }
-        }
-        dst += dstRowBytes;
-        src += srcRowBytes;
-    }
-}
-
-template <bool PM, int A_IDX, int R_IDX, int G_IDX, int B_IDX>
-inline void config8888_copy_to_bitmap(const SkBitmap& dstBmp,
-                                      const uint32_t* srcPixels,
-                                      size_t srcRowBytes) {
-    SkASSERT(SkBitmap::kARGB_8888_Config == dstBmp.config());
-    SkAutoLockPixels alp(dstBmp);
-    int w = dstBmp.width();
-    int h = dstBmp.height();
-    size_t dstRowBytes = dstBmp.rowBytes();
-
-    intptr_t src = reinterpret_cast<intptr_t>(srcPixels);
-    intptr_t dst = reinterpret_cast<intptr_t>(dstBmp.getPixels());
-
-    for (int y = 0; y < h; ++y) {
-        const uint32_t* srcRow  = reinterpret_cast<uint32_t*>(src);
-        SkPMColor* dstRow = reinterpret_cast<SkPMColor*>(dst);
-        for (int x = 0; x < w; ++x) {
-            uint32_t c8888 = srcRow[x];
-            uint32_t a, r, g, b;
-            unpack_config8888<A_IDX, R_IDX, G_IDX, B_IDX>(c8888, &a, &r,
-                                                                 &g, &b);
-            if (PM) {
-                // This matches WebKit's conversion which we are replacing.
-                // We can consider alternative rounding rules for performance.
-                r = SkMulDiv255Ceiling(r, a);
-                g = SkMulDiv255Ceiling(g, a);
-                b = SkMulDiv255Ceiling(b, a);
-            }
-            // NoCheck: https://bugs.webkit.org/show_bug.cgi?id=74025
-            dstRow[x] = SkPackARGB32NoCheck(a, r, g, b);
-        }
-        src += srcRowBytes;
-        dst += dstRowBytes;
-    }
-}
-
-#ifdef SK_CPU_LENDIAN
-    static const int SK_NATIVE_A_IDX = SK_A32_SHIFT / 8;
-    static const int SK_NATIVE_R_IDX = SK_R32_SHIFT / 8;
-    static const int SK_NATIVE_G_IDX = SK_G32_SHIFT / 8;
-    static const int SK_NATIVE_B_IDX = SK_B32_SHIFT / 8;
-#else
-    static const int SK_NATIVE_A_IDX = 3 - (SK_A32_SHIFT / 8);
-    static const int SK_NATIVE_R_IDX = 3 - (SK_R32_SHIFT / 8);
-    static const int SK_NATIVE_G_IDX = 3 - (SK_G32_SHIFT / 8);
-    static const int SK_NATIVE_B_IDX = 3 - (SK_B32_SHIFT / 8);
-#endif
-
-inline void SkCopyBitmapToConfig8888(uint32_t* dstPixels,
-                                     size_t dstRowBytes,
-                                     SkCanvas::Config8888 dstConfig8888,
-                                     const SkBitmap& srcBmp) {
-    switch (dstConfig8888) {
-        case SkCanvas::kNative_Premul_Config8888:
-            bitmap_copy_to_config8888<false,
-                                      SK_NATIVE_A_IDX, SK_NATIVE_R_IDX,
-                                      SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(
-                                            dstPixels,
-                                            dstRowBytes,
-                                            srcBmp);
-            break;
-        case SkCanvas::kNative_Unpremul_Config8888:
-            bitmap_copy_to_config8888<true,
-                                      SK_NATIVE_A_IDX, SK_NATIVE_R_IDX,
-                                      SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(
-                                            dstPixels,
-                                            dstRowBytes,
-                                            srcBmp);
-            break;
-        case SkCanvas::kBGRA_Premul_Config8888:
-            bitmap_copy_to_config8888<false, 3, 2, 1, 0> (
-                                    dstPixels, dstRowBytes, srcBmp);
-            break;
-        case SkCanvas::kBGRA_Unpremul_Config8888:
-            bitmap_copy_to_config8888<true, 3, 2, 1, 0> (
-                                    dstPixels, dstRowBytes, srcBmp);
-            break;
-        case SkCanvas::kRGBA_Premul_Config8888:
-            bitmap_copy_to_config8888<false, 3, 0, 1, 2> (
-                                    dstPixels, dstRowBytes, srcBmp);
-            break;
-        case SkCanvas::kRGBA_Unpremul_Config8888:
-            bitmap_copy_to_config8888<true, 3, 0, 1, 2> (
-                                    dstPixels, dstRowBytes, srcBmp);
-            break;
-        default:
-            SkDEBUGFAIL("unexpected Config8888");
-            break;
-    }
+    SkConvertConfig8888Pixels(dstPixels, dstRowBytes, dstConfig8888, srcPixels, srcRowBytes, SkCanvas::kNative_Premul_Config8888, w, h);
 }
 
 inline void SkCopyConfig8888ToBitmap(const SkBitmap& dstBmp,
                                      const uint32_t* srcPixels,
                                      size_t srcRowBytes,
                                      SkCanvas::Config8888 srcConfig8888) {
-    switch (srcConfig8888) {
-        case SkCanvas::kNative_Premul_Config8888:
-            config8888_copy_to_bitmap<false,
-                                      SK_NATIVE_A_IDX, SK_NATIVE_R_IDX,
-                                      SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(
-                                            dstBmp,
-                                            srcPixels,
-                                            srcRowBytes);
-            break;
-        case SkCanvas::kNative_Unpremul_Config8888:
-            config8888_copy_to_bitmap<true,
-                                      SK_NATIVE_A_IDX, SK_NATIVE_R_IDX,
-                                      SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(
-                                            dstBmp,
-                                            srcPixels,
-                                            srcRowBytes);
-            break;
-        case SkCanvas::kBGRA_Premul_Config8888:
-            config8888_copy_to_bitmap<false, 3, 2, 1, 0> (
-                                    dstBmp, srcPixels, srcRowBytes);
-            break;
-        case SkCanvas::kBGRA_Unpremul_Config8888:
-            config8888_copy_to_bitmap<true, 3, 2, 1, 0> (
-                                    dstBmp, srcPixels, srcRowBytes);
-            break;
-        case SkCanvas::kRGBA_Premul_Config8888:
-            config8888_copy_to_bitmap<false, 3, 0, 1, 2> (
-                                    dstBmp, srcPixels, srcRowBytes);
-            break;
-        case SkCanvas::kRGBA_Unpremul_Config8888:
-            config8888_copy_to_bitmap<true, 3, 0, 1, 2> (
-                                    dstBmp, srcPixels, srcRowBytes);
-            break;
-        default:
-            SkDEBUGFAIL("unexpected Config8888");
-            break;
-    }
-}
+    SkASSERT(SkBitmap::kARGB_8888_Config == dstBmp.config());
+    SkAutoLockPixels alp(dstBmp);
+    int w = dstBmp.width();
+    int h = dstBmp.height();
+    size_t dstRowBytes = dstBmp.rowBytes();
+    uint32_t* dstPixels = reinterpret_cast<uint32_t*>(dstBmp.getPixels());
 
-inline void SkCopyARGB8888BitmapTo(uint32_t* dstPixels,
-                                   size_t dstRowBytes,
-                                   const SkBitmap& srcBmp) {
-    SkASSERT(SkBitmap::kARGB_8888_Config == srcBmp.config());
-
-    SkAutoLockPixels alp(srcBmp);
-
-    int w = srcBmp.width();
-    int h = srcBmp.height();
-    size_t srcRowBytes = srcBmp.rowBytes();
-
-    size_t tightRowBytes = w * 4;
-
-    char* src = reinterpret_cast<char*>(srcBmp.getPixels());
-    char* dst = reinterpret_cast<char*>(dstPixels);
-
-    if (tightRowBytes == srcRowBytes &&
-        tightRowBytes == dstRowBytes) {
-        memcpy(dst, src, tightRowBytes * h);
-    } else {
-        for (int y = 0; y < h; ++y) {
-            memcpy(dst, src, tightRowBytes);
-            dst += dstRowBytes;
-            src += srcRowBytes;
-        }
-    }
+    SkConvertConfig8888Pixels(dstPixels, dstRowBytes, SkCanvas::kNative_Premul_Config8888, srcPixels, srcRowBytes, srcConfig8888, w, h);
 }
 
 }
diff --git a/src/core/SkDevice.cpp b/src/core/SkDevice.cpp
index 34b5c4f..f1da2ef 100644
--- a/src/core/SkDevice.cpp
+++ b/src/core/SkDevice.cpp
@@ -208,15 +208,7 @@
     }
     SkAutoLockPixels alp(bitmap);
     uint32_t* bmpPixels = reinterpret_cast<uint32_t*>(bitmap.getPixels());
-    if ((SkCanvas::kNative_Premul_Config8888 == config8888 ||
-         kPMColorAlias == config8888)) {
-        SkCopyARGB8888BitmapTo(bmpPixels, bitmap.rowBytes(), subset);
-    } else {
-        SkCopyBitmapToConfig8888(bmpPixels,
-                                 bitmap.rowBytes(),
-                                 config8888,
-                                 subset);
-    }
+    SkCopyBitmapToConfig8888(bmpPixels, bitmap.rowBytes(), config8888, subset);
     return true;
 }