Make "alpha only" be a property of GrTextureProducer

Review URL: https://codereview.chromium.org/1507973005
diff --git a/src/gpu/GrImageIDTextureAdjuster.cpp b/src/gpu/GrImageIDTextureAdjuster.cpp
index a9189f6..2edff60 100644
--- a/src/gpu/GrImageIDTextureAdjuster.cpp
+++ b/src/gpu/GrImageIDTextureAdjuster.cpp
@@ -15,8 +15,12 @@
 #include "SkImageCacherator.h"
 #include "SkPixelRef.h"
 
+static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
+
 GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp)
-    : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height()))
+    : INHERITED(bmp->getTexture(),
+                SkIRect::MakeWH(bmp->width(), bmp->height()),
+                bmp_is_alpha_only(*bmp))
     , fBmp(bmp) {}
 
 void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
@@ -39,8 +43,15 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
+// SkImage's don't have a way of communicating whether they're alpha-only. So we fallback to
+// inspecting the texture.
+static bool tex_image_is_alpha_only(const SkImage_Base& img) {
+    return GrPixelConfigIsAlphaOnly(img.peekTexture()->config());
+}
+
 GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img)
-    : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()))
+    : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()),
+                tex_image_is_alpha_only(*img))
     , fImageBase(img) {}
 
 void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
@@ -59,7 +70,7 @@
 //////////////////////////////////////////////////////////////////////////////
 
 GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
-    : INHERITED(context, bitmap.width(), bitmap.height())
+    : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
     , fBitmap(bitmap) {
     SkASSERT(!bitmap.getTexture());
     if (!bitmap.isVolatile()) {
@@ -99,10 +110,13 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////
-
+static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
+    return kAlpha_8_SkColorType == cacher.info().colorType();
+}
 GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
                                          const SkImage* client, SkImage::CachingHint chint)
-    : INHERITED(context, cacher->info().width(), cacher->info().height())
+    : INHERITED(context, cacher->info().width(), cacher->info().height(),
+                cacher_is_alpha_only(*cacher))
     , fCacher(cacher)
     , fClient(client)
     , fCachingHint(chint) {
diff --git a/src/gpu/GrTextureParamsAdjuster.cpp b/src/gpu/GrTextureParamsAdjuster.cpp
index 2aedad5..336ab6b 100644
--- a/src/gpu/GrTextureParamsAdjuster.cpp
+++ b/src/gpu/GrTextureParamsAdjuster.cpp
@@ -121,8 +121,10 @@
     return copy.detach();
 }
 
-GrTextureAdjuster::GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea)
-    : INHERITED(contentArea.width(), contentArea.height())
+GrTextureAdjuster::GrTextureAdjuster(GrTexture* original,
+                                     const SkIRect& contentArea,
+                                     bool isAlphaOnly)
+    : INHERITED(contentArea.width(), contentArea.height(), isAlphaOnly)
     , fOriginal(original) {
     SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(contentArea));
     if (contentArea.fLeft > 0 || contentArea.fTop > 0 ||
diff --git a/src/gpu/GrTextureParamsAdjuster.h b/src/gpu/GrTextureParamsAdjuster.h
index ed9f142..103bcbf 100644
--- a/src/gpu/GrTextureParamsAdjuster.h
+++ b/src/gpu/GrTextureParamsAdjuster.h
@@ -70,9 +70,13 @@
 
     int width() const { return fWidth; }
     int height() const { return fHeight; }
+    bool isAlphaOnly() const { return fIsAlphaOnly; }
 
 protected:
-    GrTextureProducer(int width, int height) : fWidth(width), fHeight(height) {}
+    GrTextureProducer(int width, int height, bool isAlphaOnly)
+        : fWidth(width)
+        , fHeight(height)
+        , fIsAlphaOnly(isAlphaOnly) {}
 
     /** Helper for creating a key for a copy from an original key. */
     static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey,
@@ -104,8 +108,9 @@
     virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0;
 
 private:
-    const int fWidth;
-    const int fHeight;
+    const int   fWidth;
+    const int   fHeight;
+    const bool  fIsAlphaOnly;
 
     typedef SkNoncopyable INHERITED;
 };
@@ -133,11 +138,11 @@
 
 protected:
     /** The whole texture is content. */
-    explicit GrTextureAdjuster(GrTexture* original)
-        : INHERITED(original->width(), original->height())
+    explicit GrTextureAdjuster(GrTexture* original, bool isAlphaOnly)
+        : INHERITED(original->width(), original->height(), isAlphaOnly)
         , fOriginal(original) {}
 
-    GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea);
+    GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea, bool isAlphaOnly);
 
     GrTexture* originalTexture() const { return fOriginal; }
 
@@ -170,8 +175,8 @@
                                 const GrTextureParams::FilterMode* filterOrNullForBicubic) override;
 
 protected:
-    GrTextureMaker(GrContext* context, int width, int height)
-        : INHERITED(width, height)
+    GrTextureMaker(GrContext* context, int width, int height, bool isAlphaOnly)
+        : INHERITED(width, height, isAlphaOnly)
         , fContext(context) {}
 
     /**
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 9527870..fcfe292 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -830,14 +830,13 @@
                              const SkMatrix& m,
                              const SkPaint& paint) {
     CHECK_SHOULD_DRAW(origDraw);
-    bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
     SkMatrix viewMatrix;
     viewMatrix.setConcat(*origDraw.fMatrix, m);
     if (bitmap.getTexture()) {
         GrBitmapTextureAdjuster adjuster(&bitmap);
         // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
-        this->drawTextureProducer(&adjuster, alphaOnly, nullptr, nullptr,
-                                  SkCanvas::kFast_SrcRectConstraint, viewMatrix, fClip, paint);
+        this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
+                                  viewMatrix, fClip, paint);
         return;
     }
     int maxTileSize = fContext->caps()->maxTileSize();
@@ -882,8 +881,8 @@
         }
     }
     GrBitmapTextureMaker maker(fContext, bitmap);
-    this->drawTextureProducer(&maker, alphaOnly, nullptr, nullptr,
-                              SkCanvas::kStrict_SrcRectConstraint, viewMatrix, fClip, paint);
+    this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
+                              viewMatrix, fClip, paint);
 }
 
 // This method outsets 'iRect' by 'outset' all around and then clamps its extents to
@@ -1215,12 +1214,11 @@
 void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
                                  const SkRect* src, const SkRect& origDst,
                                  const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
-    bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
     if (bitmap.getTexture()) {
         CHECK_SHOULD_DRAW(draw);
         GrBitmapTextureAdjuster adjuster(&bitmap);
-        this->drawTextureProducer(&adjuster, alphaOnly, src, &origDst, constraint, *draw.fMatrix,
-                                  fClip, paint);
+        this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
+                                  paint);
         return;
     }
     // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
@@ -1297,7 +1295,7 @@
         }
     }
     GrBitmapTextureMaker maker(fContext, bitmap);
-    this->drawTextureProducer(&maker, alphaOnly, src, dst, constraint, *draw.fMatrix, fClip, paint);
+    this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
 }
 
 void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
@@ -1412,12 +1410,11 @@
                             const SkPaint& paint) {
     SkMatrix viewMatrix = *draw.fMatrix;
     viewMatrix.preTranslate(x, y);
-    if (GrTexture* tex = as_IB(image)->peekTexture()) {
+    if (as_IB(image)->peekTexture()) {
         CHECK_SHOULD_DRAW(draw);
-        bool alphaOnly = GrPixelConfigIsAlphaOnly(tex->config());
         GrImageTextureAdjuster adjuster(as_IB(image));
-        this->drawTextureProducer(&adjuster, alphaOnly, nullptr, nullptr,
-                                  SkCanvas::kFast_SrcRectConstraint, viewMatrix, fClip, paint);
+        this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
+                                  viewMatrix, fClip, paint);
         return;
     } else {
         SkBitmap bm;
@@ -1431,9 +1428,8 @@
         } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
             CHECK_SHOULD_DRAW(draw);
             GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
-            bool alphaOnly = kAlpha_8_SkColorType == cacher->info().colorType();
-            this->drawTextureProducer(&maker, alphaOnly, nullptr, nullptr,
-                                      SkCanvas::kFast_SrcRectConstraint, viewMatrix, fClip, paint);
+            this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
+                                      viewMatrix, fClip, paint);
         } else if (as_IB(image)->getROPixels(&bm)) {
             this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
         }
@@ -1443,12 +1439,10 @@
 void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
                                 const SkRect& dst, const SkPaint& paint,
                                 SkCanvas::SrcRectConstraint constraint) {
-    if (GrTexture* tex = as_IB(image)->peekTexture()) {
+    if (as_IB(image)->peekTexture()) {
         CHECK_SHOULD_DRAW(draw);
         GrImageTextureAdjuster adjuster(as_IB(image));
-        bool alphaOnly = GrPixelConfigIsAlphaOnly(tex->config());
-        this->drawTextureProducer(&adjuster, alphaOnly, src, &dst, constraint, *draw.fMatrix,
-                                  fClip, paint);
+        this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
         return;
     }
     SkBitmap bm;
@@ -1464,15 +1458,13 @@
     } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
         CHECK_SHOULD_DRAW(draw);
         GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
-        bool alphaOnly = kAlpha_8_SkColorType == cacher->info().colorType();
-        this->drawTextureProducer(&maker, alphaOnly, src, &dst, constraint, *draw.fMatrix,
-                                  fClip, paint);
+        this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
     } else if (as_IB(image)->getROPixels(&bm)) {
         this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
     }
 }
 
-void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer, bool alphaOnly,
+void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
                                    const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
     GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawProducerNine", fContext);
 
@@ -1490,9 +1482,8 @@
 
         SkRect srcR, dstR;
         while (iter.next(&srcR, &dstR)) {
-            this->drawTextureProducer(producer, alphaOnly, &srcR, &dstR,
-                                      SkCanvas::kStrict_SrcRectConstraint,  *draw.fMatrix, fClip,
-                                      paint);
+            this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint, 
+                                      *draw.fMatrix, fClip, paint);
         }
         return;
     }
@@ -1505,7 +1496,7 @@
                                           &kMode));
     GrPaint grPaint;
     if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
-                                     alphaOnly, &grPaint)) {
+                                     producer->isAlphaOnly(), &grPaint)) {
         return;
     }
 
@@ -1515,16 +1506,14 @@
 
 void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
                                 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
-    if (GrTexture* tex = as_IB(image)->peekTexture()) {
-        bool alphaOnly = GrPixelConfigIsAlphaOnly(tex->config());
+    if (as_IB(image)->peekTexture()) {
         GrImageTextureAdjuster adjuster(as_IB(image));
-        this->drawProducerNine(draw, &adjuster, alphaOnly, center, dst, paint);
+        this->drawProducerNine(draw, &adjuster, center, dst, paint);
     } else {
         SkBitmap bm;
         if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
             GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
-            bool alphaOnly = kAlpha_8_SkColorType == cacher->info().colorType();
-            this->drawProducerNine(draw, &maker, alphaOnly, center, dst, paint);
+            this->drawProducerNine(draw, &maker, center, dst, paint);
         } else if (as_IB(image)->getROPixels(&bm)) {
             this->drawBitmapNine(draw, bm, center, dst, paint);
         }
@@ -1533,13 +1522,12 @@
 
 void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
                                  const SkRect& dst, const SkPaint& paint) {
-    bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
     if (bitmap.getTexture()) {
         GrBitmapTextureAdjuster adjuster(&bitmap);
-        this->drawProducerNine(draw, &adjuster, alphaOnly, center, dst, paint);
+        this->drawProducerNine(draw, &adjuster, center, dst, paint);
     } else {
         GrBitmapTextureMaker maker(fContext, bitmap);
-        this->drawProducerNine(draw, &maker, alphaOnly, center, dst, paint);
+        this->drawProducerNine(draw, &maker, center, dst, paint);
     }
 }
 
diff --git a/src/gpu/SkGpuDevice.h b/src/gpu/SkGpuDevice.h
index ec5bc9e..a35e92b 100644
--- a/src/gpu/SkGpuDevice.h
+++ b/src/gpu/SkGpuDevice.h
@@ -231,7 +231,6 @@
                          bool bicubic);
 
     void drawTextureProducer(GrTextureProducer*,
-                             bool alphaOnly,
                              const SkRect* srcRect,
                              const SkRect* dstRect,
                              SkCanvas::SrcRectConstraint,
@@ -240,7 +239,6 @@
                              const SkPaint&);
 
     void drawTextureProducerImpl(GrTextureProducer*,
-                                 bool alphaOnly,
                                  const SkRect& clippedSrcRect,
                                  const SkRect& clippedDstRect,
                                  SkCanvas::SrcRectConstraint,
@@ -249,8 +247,8 @@
                                  const GrClip&,
                                  const SkPaint&);
 
-    void drawProducerNine(const SkDraw&, GrTextureProducer*, bool alphaOnly,
-                          const SkIRect& center, const SkRect& dst, const SkPaint&);
+    void drawProducerNine(const SkDraw&, GrTextureProducer*, const SkIRect& center,
+                          const SkRect& dst, const SkPaint&);
 
     bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
 
diff --git a/src/gpu/SkGpuDevice_drawTexture.cpp b/src/gpu/SkGpuDevice_drawTexture.cpp
index 0b1d3f1..efbc6c9 100644
--- a/src/gpu/SkGpuDevice_drawTexture.cpp
+++ b/src/gpu/SkGpuDevice_drawTexture.cpp
@@ -88,7 +88,6 @@
 //////////////////////////////////////////////////////////////////////////////
 
 void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer,
-                                      bool alphaOnly,
                                       const SkRect* srcRect,
                                       const SkRect* dstRect,
                                       SkCanvas::SrcRectConstraint constraint,
@@ -135,12 +134,11 @@
         }
     }
 
-    this->drawTextureProducerImpl(producer, alphaOnly, clippedSrcRect, clippedDstRect, constraint,
-                                  viewMatrix, srcToDstMatrix, clip, paint);
+    this->drawTextureProducerImpl(producer, clippedSrcRect, clippedDstRect, constraint, viewMatrix,
+                                  srcToDstMatrix, clip, paint);
 }
 
 void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer,
-                                          bool alphaTexture,
                                           const SkRect& clippedSrcRect,
                                           const SkRect& clippedDstRect,
                                           SkCanvas::SrcRectConstraint constraint,
@@ -156,7 +154,7 @@
     // The shader expects proper local coords, so we can't replace local coords with texture coords
     // if the shader will be used. If we have a mask filter we will change the underlying geometry
     // that is rendered.
-    bool canUseTextureCoordsAsLocalCoords = !use_shader(alphaTexture, paint) && !mf;
+    bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;
 
     bool doBicubic;
     GrTextureParams::FilterMode fm =
@@ -204,7 +202,8 @@
     }
 
     GrPaint grPaint;
-    if (!SkPaintToGrPaintWithTexture(fContext, paint, viewMatrix, fp, alphaTexture, &grPaint)) {
+    if (!SkPaintToGrPaintWithTexture(fContext, paint, viewMatrix, fp, producer->isAlphaOnly(),
+                                     &grPaint)) {
         return;
     }
 
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index 55a82bb..e773aed 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -8,7 +8,7 @@
 #include "GrCaps.h"
 #include "GrContext.h"
 #include "GrDrawContext.h"
-#include "GrTextureParamsAdjuster.h"
+#include "GrImageIDTextureAdjuster.h"
 #include "effects/GrYUVtoRGBEffect.h"
 #include "SkCanvas.h"
 #include "SkBitmapCache.h"
@@ -78,31 +78,8 @@
     return true;
 }
 
-class GpuImage_GrTextureAdjuster : public GrTextureAdjuster {
-public:
-    GpuImage_GrTextureAdjuster(const SkImage_Gpu* image)
-        : INHERITED(image->peekTexture())
-        , fImage(image)
-    {}
-
-protected:
-    void makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) override {
-        GrUniqueKey baseKey;
-        GrMakeKeyFromImageID(&baseKey, fImage->uniqueID(),
-                             SkIRect::MakeWH(fImage->width(), fImage->height()));
-        MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
-    }
-
-    void didCacheCopy(const GrUniqueKey& copyKey) override { as_IB(fImage)->notifyAddedToCache(); }
-
-private:
-    const SkImage*  fImage;
-
-    typedef GrTextureAdjuster INHERITED;
-};
-
 GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const {
-    return GpuImage_GrTextureAdjuster(this).refTextureSafeForParams(params, nullptr);
+    return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, nullptr);
 }
 
 bool SkImage_Gpu::isOpaque() const {