add kGray_8_SkColorType

patch from issue 1014783003 at patchset 60001 (http://crrev.com/1014783003#ps60001)

BUG=skia:
TBR=

Review URL: https://codereview.chromium.org/1010343002
diff --git a/gm/filterbitmap.cpp b/gm/filterbitmap.cpp
index 7028012..c76a70a 100644
--- a/gm/filterbitmap.cpp
+++ b/gm/filterbitmap.cpp
@@ -138,12 +138,13 @@
 };
 
 class FilterBitmapCheckerboardGM: public FilterBitmapGM {
-  public:
-      FilterBitmapCheckerboardGM(int size, int num_checks)
-      : fSize(size), fNumChecks(num_checks)
-        {
-            fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks);
-        }
+public:
+    FilterBitmapCheckerboardGM(int size, int num_checks, bool convertToG8 = false)
+        : fSize(size), fNumChecks(num_checks), fConvertToG8(convertToG8)
+    {
+        fName.printf("filterbitmap_checkerboard_%d_%d%s",
+                     fSize, fNumChecks, convertToG8 ? "_g8" : "");
+    }
 
   protected:
       int fSize;
@@ -167,20 +168,26 @@
                   }
               }
           }
+          if (fConvertToG8) {
+              SkBitmap tmp;
+              fBM.copyTo(&tmp, kGray_8_SkColorType);
+              fBM = tmp;
+          }
       }
-  private:
-      typedef FilterBitmapGM INHERITED;
+private:
+    const bool fConvertToG8;
+    typedef FilterBitmapGM INHERITED;
 };
 
 class FilterBitmapImageGM: public FilterBitmapGM {
-  public:
-      FilterBitmapImageGM(const char filename[])
-      : fFilename(filename)
-        {
-            fName.printf("filterbitmap_image_%s", filename);
-        }
+public:
+    FilterBitmapImageGM(const char filename[], bool convertToG8 = false)
+        : fFilename(filename), fConvertToG8(convertToG8)
+    {
+        fName.printf("filterbitmap_image_%s%s", filename, convertToG8 ? "_g8" : "");
+    }
 
-  protected:
+protected:
       SkString fFilename;
       int fSize;
 
@@ -204,9 +211,15 @@
               *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
           }
           fSize = fBM.height();
+          if (fConvertToG8) {
+              SkBitmap tmp;
+              fBM.copyTo(&tmp, kGray_8_SkColorType);
+              fBM = tmp;
+          }
       }
-  private:
-      typedef FilterBitmapGM INHERITED;
+private:
+    const bool fConvertToG8;
+    typedef FilterBitmapGM INHERITED;
 };
 
 //////////////////////////////////////////////////////////////////////////////
@@ -216,12 +229,14 @@
 DEF_GM( return new FilterBitmapTextGM(10); )
 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
+DEF_GM( return new FilterBitmapCheckerboardGM(32,32, true); )
 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
+DEF_GM( return new FilterBitmapImageGM("mandrill_64.png", true); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
diff --git a/include/core/SkImageInfo.h b/include/core/SkImageInfo.h
index d915c09..01318fd 100644
--- a/include/core/SkImageInfo.h
+++ b/include/core/SkImageInfo.h
@@ -72,8 +72,9 @@
     kRGBA_8888_SkColorType,
     kBGRA_8888_SkColorType,
     kIndex_8_SkColorType,
+    kGray_8_SkColorType,
 
-    kLastEnum_SkColorType = kIndex_8_SkColorType,
+    kLastEnum_SkColorType = kGray_8_SkColorType,
 
 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
     kN32_SkColorType = kBGRA_8888_SkColorType,
@@ -93,6 +94,7 @@
         4,  // RGBA_8888
         4,  // BGRA_8888
         1,  // kIndex_8
+        1,  // kGray_8
     };
     SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
                       size_mismatch_with_SkColorType_enum);
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 0b586cb..877cfaa 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -516,6 +516,7 @@
                 break;
             case kAlpha_8_SkColorType:
             case kIndex_8_SkColorType:
+            case kGray_8_SkColorType:
                 base += x;
                 break;
             default:
@@ -532,6 +533,10 @@
     SkASSERT((unsigned)y < (unsigned)this->height());
 
     switch (this->colorType()) {
+        case kGray_8_SkColorType: {
+            uint8_t* addr = this->getAddr8(x, y);
+            return SkColorSetRGB(*addr, *addr, *addr);
+        }
         case kAlpha_8_SkColorType: {
             uint8_t* addr = this->getAddr8(x, y);
             return SkColorSetA(0, addr[0]);
@@ -597,6 +602,7 @@
             return 0xFF == SkGetPackedA32(c);
         } break;
         case kRGB_565_SkColorType:
+        case kGray_8_SkColorType:
             return true;
             break;
         case kARGB_4444_SkColorType: {
@@ -674,6 +680,20 @@
     const int rowBytes = fRowBytes;
 
     switch (this->colorType()) {
+        case kGray_8_SkColorType: {
+            if (255 != a) {
+                r = SkMulDiv255Round(r, a);
+                g = SkMulDiv255Round(g, a);
+                b = SkMulDiv255Round(b, a);
+            }
+            int gray = SkComputeLuminance(r, g, b);
+            uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
+            while (--height >= 0) {
+                memset(p, gray, width);
+                p += rowBytes;
+            }
+            break;
+        }
         case kAlpha_8_SkColorType: {
             uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
             while (--height >= 0) {
@@ -828,6 +848,16 @@
             break;
         case kARGB_4444_SkColorType:
             return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT;
+        case kGray_8_SkColorType:
+            switch (srcCT) {
+                case kGray_8_SkColorType:
+                case kRGBA_8888_SkColorType:
+                case kBGRA_8888_SkColorType:
+                    return true;
+                default:
+                    break;
+            }
+            return false;
         default:
             return false;
     }
diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp
index 42d9c10..69c2ea1 100644
--- a/src/core/SkBitmapProcState.cpp
+++ b/src/core/SkBitmapProcState.cpp
@@ -431,6 +431,10 @@
                 index |= 32;
                 fPaintPMColor = SkPreMultiplyColor(paint.getColor());
                 break;
+            case kGray_8_SkColorType:
+                index |= 40;
+                fPaintPMColor = SkPreMultiplyColor(paint.getColor());
+                break;
             default:
                 // TODO(dominikg): Should we ever get here? SkASSERT(false) instead?
                 return false;
@@ -473,7 +477,7 @@
             S4444_alpha_D32_filter_DXDY,
             S4444_opaque_D32_filter_DX,
             S4444_alpha_D32_filter_DX,
-
+            
             // A8 treats alpha/opaque the same (equally efficient)
             SA8_alpha_D32_nofilter_DXDY,
             SA8_alpha_D32_nofilter_DXDY,
@@ -482,7 +486,17 @@
             SA8_alpha_D32_filter_DXDY,
             SA8_alpha_D32_filter_DXDY,
             SA8_alpha_D32_filter_DX,
-            SA8_alpha_D32_filter_DX
+            SA8_alpha_D32_filter_DX,
+            
+            // todo: possibly specialize on opaqueness
+            SG8_alpha_D32_nofilter_DXDY,
+            SG8_alpha_D32_nofilter_DXDY,
+            SG8_alpha_D32_nofilter_DX,
+            SG8_alpha_D32_nofilter_DX,
+            SG8_alpha_D32_filter_DXDY,
+            SG8_alpha_D32_filter_DXDY,
+            SG8_alpha_D32_filter_DX,
+            SG8_alpha_D32_filter_DX
         };
 
         static const SampleProc16 gSkBitmapProcStateSample16[] = {
@@ -504,6 +518,8 @@
             // Don't support 4444 -> 565
             NULL, NULL, NULL, NULL,
             // Don't support A8 -> 565
+            NULL, NULL, NULL, NULL,
+            // Don't support G8 -> 565 (but we could)
             NULL, NULL, NULL, NULL
         };
 #endif
diff --git a/src/core/SkBitmapProcState_procs.h b/src/core/SkBitmapProcState_procs.h
index 3b4cef3..a9b8750 100644
--- a/src/core/SkBitmapProcState_procs.h
+++ b/src/core/SkBitmapProcState_procs.h
@@ -224,6 +224,25 @@
 #define SRC_TO_FILTER(src)      src
 #include "SkBitmapProcState_sample.h"
 
+// SRC == Gray8
+
+#undef FILTER_PROC
+#define FILTER_PROC(x, y, a, b, c, d, dst) \
+    do {                                                        \
+        unsigned tmp = Filter_8(x, y, a, b, c, d);              \
+        SkPMColor color = SkPackARGB32(0xFF, tmp, tmp, tmp);    \
+        *(dst) = SkAlphaMulQ(color, alphaScale);                \
+    } while (0)
+
+#define MAKENAME(suffix)        NAME_WRAP(SG8_alpha_D32 ## suffix)
+#define DSTSIZE                 32
+#define SRCTYPE                 uint8_t
+#define CHECKSTATE(state)       SkASSERT(kGray_8_SkColorType == state.fBitmap->colorType());
+#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale
+#define RETURNDST(src)          SkAlphaMulQ(SkPackARGB32(0xFF, src, src, src), alphaScale)
+#define SRC_TO_FILTER(src)      src
+#include "SkBitmapProcState_sample.h"
+
 /*****************************************************************************
  *
  *  D16 functions
diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp
index 85e208f..d28941c 100644
--- a/src/core/SkConfig8888.cpp
+++ b/src/core/SkConfig8888.cpp
@@ -137,6 +137,47 @@
     }
 }
 
+static void copy_g8_to_32(void* dst, size_t dstRB, const void* src, size_t srcRB, int w, int h) {
+    uint32_t* dst32 = (uint32_t*)dst;
+    const uint8_t* src8 = (const uint8_t*)src;
+    
+    for (int y = 0; y < h; ++y) {
+        for (int x = 0; x < w; ++x) {
+            dst32[x] = SkPackARGB32(0xFF, src8[x], src8[x], src8[x]);
+        }
+        dst32 = (uint32_t*)((char*)dst32 + dstRB);
+        src8 += srcRB;
+    }
+}
+
+static void copy_32_to_g8(void* dst, size_t dstRB, const void* src, size_t srcRB,
+                          const SkImageInfo& srcInfo) {
+    uint8_t* dst8 = (uint8_t*)dst;
+    const uint32_t* src32 = (const uint32_t*)src;
+
+    const int w = srcInfo.width();
+    const int h = srcInfo.height();
+    const bool isBGRA = (kBGRA_8888_SkColorType == srcInfo.colorType());
+
+    for (int y = 0; y < h; ++y) {
+        if (isBGRA) {
+            // BGRA
+            for (int x = 0; x < w; ++x) {
+                uint32_t s = src32[x];
+                dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
+            }
+        } else {
+            // RGBA
+            for (int x = 0; x < w; ++x) {
+                uint32_t s = src32[x];
+                dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
+            }
+        }
+        src32 = (const uint32_t*)((const char*)src32 + srcRB);
+        dst8 += dstRB;
+    }
+}
+
 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
                              const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
                              SkColorTable* ctable) {
@@ -170,6 +211,7 @@
         switch (srcInfo.colorType()) {
             case kRGB_565_SkColorType:
             case kAlpha_8_SkColorType:
+            case kGray_8_SkColorType:
                 break;
             case kIndex_8_SkColorType:
             case kARGB_4444_SkColorType:
@@ -189,6 +231,15 @@
      *  are supported.
      */
 
+    if (kGray_8_SkColorType == srcInfo.colorType() && 4 == dstInfo.bytesPerPixel()) {
+        copy_g8_to_32(dstPixels, dstRB, srcPixels, srcRB, width, height);
+        return true;
+    }
+    if (kGray_8_SkColorType == dstInfo.colorType() && 4 == srcInfo.bytesPerPixel()) {
+        copy_32_to_g8(dstPixels, dstRB, srcPixels, srcRB, srcInfo);
+        return true;
+    }
+
     // Can no longer draw directly into 4444, but we can manually whack it for a few combinations
     if (kARGB_4444_SkColorType == dstInfo.colorType() &&
         (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) {
diff --git a/src/core/SkImageInfo.cpp b/src/core/SkImageInfo.cpp
index 0e8b0b1..8429ef2 100644
--- a/src/core/SkImageInfo.cpp
+++ b/src/core/SkImageInfo.cpp
@@ -66,6 +66,7 @@
             }
             break;
         case kRGB_565_SkColorType:
+        case kGray_8_SkColorType:
             alphaType = kOpaque_SkAlphaType;
             break;
         default:
diff --git a/src/core/SkMipMap.cpp b/src/core/SkMipMap.cpp
index 9e0e5e1..1b41085 100644
--- a/src/core/SkMipMap.cpp
+++ b/src/core/SkMipMap.cpp
@@ -135,6 +135,39 @@
    *((uint16_t*)dst) = (uint16_t)collaps4444(c >> 2);
 }
 
+static void downsample8_nocheck(void* dst, int, int, const void* srcPtr, const SkBitmap& srcBM) {
+    const size_t rb = srcBM.rowBytes();
+    const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
+    *(uint8_t*)dst = (p[0] + p[1] + p[rb] + p[rb + 1]) >> 2;
+}
+
+static void downsample8_check(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) {
+    const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
+    const uint8_t* baseP = p;
+
+    x <<= 1;
+    y <<= 1;
+    SkASSERT(srcBM.getAddr8(x, y) == p);
+
+    unsigned c = *p;
+    if (x < srcBM.width() - 1) {
+        p += 1;
+    }
+    c += *p;
+
+    p = baseP;
+    if (y < srcBM.height() - 1) {
+        p += srcBM.rowBytes();
+    }
+    c += *p;
+    if (x < srcBM.width() - 1) {
+        p += 1;
+    }
+    c += *p;
+
+    *(uint8_t*)dst = c >> 2;
+}
+
 size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
     if (levelCount < 0) {
         return 0;
@@ -167,6 +200,11 @@
             proc_check = downsample4444;
             proc_nocheck = proc_check;
             break;
+        case kAlpha_8_SkColorType:
+        case kGray_8_SkColorType:
+            proc_check = downsample8_check;
+            proc_nocheck = downsample8_nocheck;
+            break;
         default:
             return NULL; // don't build mipmaps for any other colortypes (yet)
     }
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index d105418..32c9158 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -594,6 +594,8 @@
             return kBGRA_8888_GrPixelConfig;
         case kIndex_8_SkColorType:
             return kIndex_8_GrPixelConfig;
+        case kGray_8_SkColorType:
+            return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
     }
     SkASSERT(0);    // shouldn't get here
     return kUnknown_GrPixelConfig;
diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp
index 666a008..e3971aa 100644
--- a/src/pdf/SkPDFImage.cpp
+++ b/src/pdf/SkPDFImage.cpp
@@ -31,6 +31,7 @@
             return srcRect.width() * 3 * srcRect.height();
         case kRGBA_8888_SkColorType:
         case kBGRA_8888_SkColorType:
+        case kGray_8_SkColorType:
             return srcRect.width() * 3 * srcRect.height();
         case kAlpha_8_SkColorType:
             return 1;
@@ -113,7 +114,7 @@
                                       const SkIRect& srcRect) {
     SkStream* stream = SkNEW_ARGS(SkMemoryStream,
                                   (get_uncompressed_size(bitmap,
-                                                     srcRect)));
+                                                         srcRect)));
     uint8_t* dst = (uint8_t*)stream->getMemoryBase();
     for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
         uint16_t* src = bitmap.getAddr16(0, y);
@@ -127,6 +128,20 @@
     return stream;
 }
 
+static SkStream* extract_gray8_image(const SkBitmap& bitmap, const SkIRect& srcRect) {
+    SkStream* stream = SkNEW_ARGS(SkMemoryStream,
+                                  (get_uncompressed_size(bitmap, srcRect)));
+    uint8_t* dst = (uint8_t*)stream->getMemoryBase();
+    for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
+        uint8_t* src = bitmap.getAddr8(0, y);
+        for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
+            dst[0] = dst[1] = dst[2] = src[x];
+            dst += 3;
+        }
+    }
+    return stream;
+}
+
 static uint32_t get_argb8888_neighbor_avg_color(const SkBitmap& bitmap,
                                                 int xOrig,
                                                 int yOrig);
@@ -227,7 +242,8 @@
                                     bool extractAlpha, bool* isTransparent) {
     SkColorType colorType = bitmap.colorType();
     if (extractAlpha && (kIndex_8_SkColorType == colorType ||
-                         kRGB_565_SkColorType == colorType)) {
+                         kRGB_565_SkColorType == colorType ||
+                         kGray_8_SkColorType  == colorType)) {
         if (isTransparent != NULL) {
             *isTransparent = false;
         }
@@ -258,6 +274,11 @@
                 stream.reset(extract_rgb565_image(bitmap, srcRect));
             }
             break;
+        case kGray_8_SkColorType:
+            if (!extractAlpha) {
+                stream.reset(extract_gray8_image(bitmap, srcRect));
+            }
+            break;
         case kN32_SkColorType:
             stream.reset(extract_argb8888_data(bitmap, srcRect, extractAlpha,
                                                &isOpaque, &transparent));