sk_sp for Ganesh.

Convert use of GrFragmentProcessor, GrGeometryProcessor, and
GrXPFactory to sk_sp. This clarifies ownership and should
reduce reference count churn by moving ownership.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2041113004

Review-Url: https://codereview.chromium.org/2041113004
diff --git a/include/gpu/GrClip.h b/include/gpu/GrClip.h
index e684dfa..ce0f155 100644
--- a/include/gpu/GrClip.h
+++ b/include/gpu/GrClip.h
@@ -22,7 +22,7 @@
 class GrAppliedClip : public SkNoncopyable {
 public:
     GrAppliedClip() : fHasStencilClip(false) {}
-    const GrFragmentProcessor* clipCoverageFragmentProcessor() const {
+    GrFragmentProcessor* getClipCoverageFragmentProcessor() const {
         return fClipCoverageFP.get();
     }
     const GrScissorState& scissorState() const { return fScissorState; }
@@ -40,22 +40,22 @@
         fHasStencilClip = hasStencil;
     }
 
-    void makeFPBased(sk_sp<const GrFragmentProcessor> fp) {
+    void makeFPBased(sk_sp<GrFragmentProcessor> fp) {
         fClipCoverageFP = fp;
         fScissorState.setDisabled();
         fHasStencilClip = false;
     }
 
-    void makeScissoredFPBased(sk_sp<const GrFragmentProcessor> fp, SkIRect& scissor) {
+    void makeScissoredFPBased(sk_sp<GrFragmentProcessor> fp, SkIRect& scissor) {
         fClipCoverageFP = fp;
         fScissorState.set(scissor);
         fHasStencilClip = false;
     }
 
 private:
-    sk_sp<const GrFragmentProcessor> fClipCoverageFP;
-    GrScissorState                   fScissorState;
-    bool                             fHasStencilClip;
+    sk_sp<GrFragmentProcessor> fClipCoverageFP;
+    GrScissorState             fScissorState;
+    bool                       fHasStencilClip;
 
     typedef SkNoncopyable INHERITED;
 };
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 4e2546b..f9d06f0 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -457,9 +457,9 @@
      * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
      * return NULL. They also can perform a swizzle as part of the draw.
      */
-    const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, const GrSwizzle&,
+    sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&,
                                                    const SkMatrix&) const;
-    const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, const GrSwizzle&,
+    sk_sp<GrFragmentProcessor> createUPMToPMEffect(GrTexture*, const GrSwizzle&,
                                                    const SkMatrix&) const;
     /** Called before either of the above two functions to determine the appropriate fragment
         processors for conversions. This must be called by readSurfacePixels before a mutex is
diff --git a/include/gpu/GrFragmentProcessor.h b/include/gpu/GrFragmentProcessor.h
index c3f291c..b8ebeca 100644
--- a/include/gpu/GrFragmentProcessor.h
+++ b/include/gpu/GrFragmentProcessor.h
@@ -31,29 +31,31 @@
     *  does so by returning a parent FP that multiplies the passed in FPs output by the parent's
     *  input alpha. The passed in FP will not receive an input color.
     */
-    static const GrFragmentProcessor* MulOutputByInputAlpha(const GrFragmentProcessor*);
+    static sk_sp<GrFragmentProcessor> MulOutputByInputAlpha(sk_sp<GrFragmentProcessor>);
 
     /**
      *  Similar to the above but it modulates the output r,g,b of the child processor by the input
      *  rgb and then multiplies all the components by the input alpha. This effectively modulates
      *  the child processor's premul color by a unpremul'ed input and produces a premul output
      */
-    static const GrFragmentProcessor* MulOutputByInputUnpremulColor(const GrFragmentProcessor*);
+    static sk_sp<GrFragmentProcessor> MulOutputByInputUnpremulColor(sk_sp<GrFragmentProcessor>);
 
     /**
      *  Returns a parent fragment processor that adopts the passed fragment processor as a child.
      *  The parent will ignore its input color and instead feed the passed in color as input to the
      *  child.
      */
-    static const GrFragmentProcessor* OverrideInput(const GrFragmentProcessor*, GrColor);
+    static sk_sp<GrFragmentProcessor> OverrideInput(sk_sp<GrFragmentProcessor>, GrColor);
 
     /**
      * Returns a fragment processor that runs the passed in array of fragment processors in a
      * series. The original input is passed to the first, the first's output is passed to the
      * second, etc. The output of the returned processor is the output of the last processor of the
      * series.
+     *
+     * The array elements with be moved.
      */
-    static const GrFragmentProcessor* RunInSeries(const GrFragmentProcessor*[], int cnt);
+    static sk_sp<GrFragmentProcessor> RunInSeries(sk_sp<GrFragmentProcessor>*, int cnt);
 
     GrFragmentProcessor()
         : INHERITED()
@@ -155,7 +157,7 @@
      * processors will allow the ProgramBuilder to automatically handle their transformed coords and
      * texture accesses and mangle their uniform and output color names.
      */
-    int registerChildProcessor(const GrFragmentProcessor* child);
+    int registerChildProcessor(sk_sp<GrFragmentProcessor> child);
 
     /**
      * Subclass implements this to support getConstantColorComponents(...).
@@ -187,7 +189,7 @@
 
     bool hasSameTransforms(const GrFragmentProcessor&) const;
 
-    bool                                            fUsesLocalCoords;
+    bool                                       fUsesLocalCoords;
 
     /**
      * fCoordTransforms stores the transforms of this proc, followed by all the transforms of this
@@ -212,11 +214,16 @@
      *
      * The same goes for fTextureAccesses with textures.
      */
-    SkSTArray<4, const GrCoordTransform*, true>     fCoordTransforms;
-    int                                             fNumTexturesExclChildren;
-    int                                             fNumBuffersExclChildren;
-    int                                             fNumTransformsExclChildren;
-    SkSTArray<1, const GrFragmentProcessor*, true>  fChildProcessors;
+    SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
+    int                                         fNumTexturesExclChildren;
+    int                                         fNumBuffersExclChildren;
+    int                                         fNumTransformsExclChildren;
+
+    /**
+     * This is not SkSTArray<1, sk_sp<GrFragmentProcessor>> because this class holds strong
+     * references until notifyRefCntIsZero and then it holds pending executions.
+     */
+    SkSTArray<1, GrFragmentProcessor*, true>    fChildProcessors;
 
     typedef GrProcessor INHERITED;
 };
diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h
index 7de559c..af40848 100644
--- a/include/gpu/GrPaint.h
+++ b/include/gpu/GrPaint.h
@@ -15,6 +15,7 @@
 #include "effects/GrPorterDuffXferProcessor.h"
 #include "GrFragmentProcessor.h"
 
+#include "SkRefCnt.h"
 #include "SkRegion.h"
 #include "SkXfermode.h"
 
@@ -42,7 +43,7 @@
 
     GrPaint(const GrPaint& paint) { *this = paint; }
 
-    ~GrPaint() { this->resetFragmentProcessors();  }
+    ~GrPaint() { }
 
     /**
      * The initial color of the drawn primitive. Defaults to solid white.
@@ -79,13 +80,12 @@
         setAllowSRGBInputs(gammaCorrect);
     }
 
-    const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
-        fXPFactory.reset(SkSafeRef(xpFactory));
-        return xpFactory;
+    void setXPFactory(sk_sp<GrXPFactory> xpFactory) {
+        fXPFactory = std::move(xpFactory);
     }
 
     void setPorterDuffXPFactory(SkXfermode::Mode mode) {
-        fXPFactory.reset(GrPorterDuffXPFactory::Create(mode));
+        fXPFactory = GrPorterDuffXPFactory::Make(mode);
     }
 
     void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage = false); 
@@ -93,19 +93,17 @@
     /**
      * Appends an additional color processor to the color computation.
      */
-    const GrFragmentProcessor* addColorFragmentProcessor(const GrFragmentProcessor* fp) {
+    void addColorFragmentProcessor(sk_sp<GrFragmentProcessor> fp) {
         SkASSERT(fp);
-        fColorFragmentProcessors.push_back(SkRef(fp));
-        return fp;
+        fColorFragmentProcessors.push_back(std::move(fp));
     }
 
     /**
      * Appends an additional coverage processor to the coverage computation.
      */
-    const GrFragmentProcessor* addCoverageFragmentProcessor(const GrFragmentProcessor* fp) {
+    void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> fp) {
         SkASSERT(fp);
-        fCoverageFragmentProcessors.push_back(SkRef(fp));
-        return fp;
+        fCoverageFragmentProcessors.push_back(std::move(fp));
     }
 
     /**
@@ -122,15 +120,15 @@
     int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
                                               this->numCoverageFragmentProcessors(); }
 
-    const GrXPFactory* getXPFactory() const {
-        return fXPFactory;
+    GrXPFactory* getXPFactory() const {
+        return fXPFactory.get();
     }
 
-    const GrFragmentProcessor* getColorFragmentProcessor(int i) const {
-        return fColorFragmentProcessors[i];
+    GrFragmentProcessor* getColorFragmentProcessor(int i) const {
+        return fColorFragmentProcessors[i].get();
     }
-    const GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
-        return fCoverageFragmentProcessors[i];
+    GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
+        return fCoverageFragmentProcessors[i].get();
     }
 
     GrPaint& operator=(const GrPaint& paint) {
@@ -139,17 +137,10 @@
         fAllowSRGBInputs = paint.fAllowSRGBInputs;
 
         fColor = paint.fColor;
-        this->resetFragmentProcessors();
         fColorFragmentProcessors = paint.fColorFragmentProcessors;
         fCoverageFragmentProcessors = paint.fCoverageFragmentProcessors;
-        for (int i = 0; i < fColorFragmentProcessors.count(); ++i) {
-            fColorFragmentProcessors[i]->ref();
-        }
-        for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) {
-            fCoverageFragmentProcessors[i]->ref();
-        }
 
-        fXPFactory.reset(SkSafeRef(paint.getXPFactory()));
+        fXPFactory = paint.fXPFactory;
 
         return *this;
     }
@@ -163,26 +154,15 @@
     bool isConstantBlendedColor(GrColor* constantColor) const;
 
 private:
-    void resetFragmentProcessors() {
-        for (int i = 0; i < fColorFragmentProcessors.count(); ++i) {
-            fColorFragmentProcessors[i]->unref();
-        }
-        for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) {
-            fCoverageFragmentProcessors[i]->unref();
-        }
-        fColorFragmentProcessors.reset();
-        fCoverageFragmentProcessors.reset();
-    }
+    mutable sk_sp<GrXPFactory>                fXPFactory;
+    SkSTArray<4, sk_sp<GrFragmentProcessor>>  fColorFragmentProcessors;
+    SkSTArray<2, sk_sp<GrFragmentProcessor>>  fCoverageFragmentProcessors;
 
-    mutable SkAutoTUnref<const GrXPFactory>         fXPFactory;
-    SkSTArray<4, const GrFragmentProcessor*, true>  fColorFragmentProcessors;
-    SkSTArray<2, const GrFragmentProcessor*, true>  fCoverageFragmentProcessors;
+    bool                                      fAntiAlias;
+    bool                                      fDisableOutputConversionToSRGB;
+    bool                                      fAllowSRGBInputs;
 
-    bool                                            fAntiAlias;
-    bool                                            fDisableOutputConversionToSRGB;
-    bool                                            fAllowSRGBInputs;
-
-    GrColor                                         fColor;
+    GrColor                                   fColor;
 };
 
 #endif
diff --git a/include/gpu/GrProcessorUnitTest.h b/include/gpu/GrProcessorUnitTest.h
index bc90204..4f26665 100644
--- a/include/gpu/GrProcessorUnitTest.h
+++ b/include/gpu/GrProcessorUnitTest.h
@@ -28,7 +28,7 @@
 
 /** This allows parent FPs to implement a test create with known leaf children in order to avoid
 creating an unbounded FP tree which may overflow various shader limits. */
-const GrFragmentProcessor* CreateChildFP(GrProcessorTestData*);
+sk_sp<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
 
 }
 
@@ -66,28 +66,28 @@
 
 template <class Processor> class GrProcessorTestFactory : SkNoncopyable {
 public:
-    typedef const Processor* (*CreateProc)(GrProcessorTestData*);
+    typedef sk_sp<Processor> (*MakeProc)(GrProcessorTestData*);
 
-    GrProcessorTestFactory(CreateProc createProc) {
-        fCreateProc = createProc;
+    GrProcessorTestFactory(MakeProc makeProc) {
+        fMakeProc = makeProc;
         GetFactories()->push_back(this);
     }
 
     /** Pick a random factory function and create a processor.  */
-    static const Processor* Create(GrProcessorTestData* data) {
+    static sk_sp<Processor> Make(GrProcessorTestData* data) {
         VerifyFactoryCount();
         SkASSERT(GetFactories()->count());
         uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
-        return CreateIdx(idx, data);
+        return MakeIdx(idx, data);
     }
 
     /** Number of registered factory functions */
     static int Count() { return GetFactories()->count(); }
 
     /** Use factory function at Index idx to create a processor. */
-    static const Processor* CreateIdx(int idx, GrProcessorTestData* data) {
+    static sk_sp<Processor> MakeIdx(int idx, GrProcessorTestData* data) {
         GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
-        return factory->fCreateProc(data);
+        return factory->fMakeProc(data);
     }
 
     /*
@@ -96,7 +96,7 @@
     static void VerifyFactoryCount();
 
 private:
-    CreateProc fCreateProc;
+    MakeProc fMakeProc;
 
     static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
 };
@@ -106,15 +106,15 @@
  */
 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST                                                         \
     static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED;                     \
-    static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
 
 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST                                                         \
     static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED;                     \
-    static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
 
 #define GR_DECLARE_XP_FACTORY_TEST                                                                 \
     static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED;                             \
-    static const GrXPFactory* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrXPFactory> TestCreate(GrProcessorTestData*)
 
 /** GrProcessor subclasses should insert this macro in their implementation file. They must then
  *  also implement this static function:
@@ -134,19 +134,19 @@
 // The unit test relies on static initializers. Just declare the TestCreate function so that
 // its definitions will compile.
 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST                                                         \
-    static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
 
 // The unit test relies on static initializers. Just declare the TestCreate function so that
 // its definitions will compile.
 #define GR_DECLARE_XP_FACTORY_TEST                                                                 \
-    static const GrXPFactory* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrXPFactory> TestCreate(GrProcessorTestData*)
 #define GR_DEFINE_XP_FACTORY_TEST(X)
 
 // The unit test relies on static initializers. Just declare the TestCreate function so that
 // its definitions will compile.
 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST                                                         \
-    static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
+    static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
 
 #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
diff --git a/include/gpu/GrTypesPriv.h b/include/gpu/GrTypesPriv.h
index 6a6fd54..8c7d6bd 100644
--- a/include/gpu/GrTypesPriv.h
+++ b/include/gpu/GrTypesPriv.h
@@ -10,6 +10,7 @@
 
 #include "GrTypes.h"
 #include "SkRect.h"
+#include "SkRefCnt.h"
 
  /**
   * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
@@ -476,4 +477,9 @@
     kOwned = true
 };
 
+template <typename T> T * const * sk_sp_address_as_pointer_address(sk_sp<T> const * sp) {
+    static_assert(sizeof(T*) == sizeof(sk_sp<T>), "sk_sp not expected size.");
+    return reinterpret_cast<T * const *>(sp);
+}
+
 #endif
diff --git a/include/gpu/effects/GrConstColorProcessor.h b/include/gpu/effects/GrConstColorProcessor.h
index 679533e..e9781bb 100644
--- a/include/gpu/effects/GrConstColorProcessor.h
+++ b/include/gpu/effects/GrConstColorProcessor.h
@@ -26,8 +26,8 @@
     };
     static const int kInputModeCnt = kLastInputMode + 1;
 
-    static GrFragmentProcessor* Create(GrColor color, InputMode mode) {
-        return new GrConstColorProcessor(color, mode);
+    static sk_sp<GrFragmentProcessor> Make(GrColor color, InputMode mode) {
+        return sk_sp<GrFragmentProcessor>(new GrConstColorProcessor(color, mode));
     }
 
     const char* name() const override { return "Color"; }
diff --git a/include/gpu/effects/GrCoverageSetOpXP.h b/include/gpu/effects/GrCoverageSetOpXP.h
index 06cc759..e5d197f 100644
--- a/include/gpu/effects/GrCoverageSetOpXP.h
+++ b/include/gpu/effects/GrCoverageSetOpXP.h
@@ -21,7 +21,7 @@
  */
 class GrCoverageSetOpXPFactory : public GrXPFactory {
 public:
-    static GrXPFactory* Create(SkRegion::Op regionOp, bool invertCoverage = false);
+    static sk_sp<GrXPFactory> Make(SkRegion::Op regionOp, bool invertCoverage = false);
 
     void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
                                   GrXPFactory::InvariantBlendedColor*) const override;
diff --git a/include/gpu/effects/GrCustomXfermode.h b/include/gpu/effects/GrCustomXfermode.h
index bcbd583..3bd3214 100644
--- a/include/gpu/effects/GrCustomXfermode.h
+++ b/include/gpu/effects/GrCustomXfermode.h
@@ -18,7 +18,7 @@
  */
 namespace GrCustomXfermode {
     bool IsSupportedMode(SkXfermode::Mode mode);
-    GrXPFactory* CreateXPFactory(SkXfermode::Mode mode);
+    sk_sp<GrXPFactory> MakeXPFactory(SkXfermode::Mode mode);
 };
 
 #endif
diff --git a/include/gpu/effects/GrPorterDuffXferProcessor.h b/include/gpu/effects/GrPorterDuffXferProcessor.h
index 476a039..8399d58 100644
--- a/include/gpu/effects/GrPorterDuffXferProcessor.h
+++ b/include/gpu/effects/GrPorterDuffXferProcessor.h
@@ -16,7 +16,7 @@
 
 class GrPorterDuffXPFactory : public GrXPFactory {
 public:
-    static GrXPFactory* Create(SkXfermode::Mode mode); 
+    static sk_sp<GrXPFactory> Make(SkXfermode::Mode mode);
 
     void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
                                   GrXPFactory::InvariantBlendedColor*) const override;
diff --git a/include/gpu/effects/GrXfermodeFragmentProcessor.h b/include/gpu/effects/GrXfermodeFragmentProcessor.h
index fb07d00..0e2435e 100644
--- a/include/gpu/effects/GrXfermodeFragmentProcessor.h
+++ b/include/gpu/effects/GrXfermodeFragmentProcessor.h
@@ -15,20 +15,20 @@
 namespace GrXfermodeFragmentProcessor {
     /** The color input to the returned processor is treated as the src and the passed in processor
         is the dst. */
-    const GrFragmentProcessor* CreateFromDstProcessor(const GrFragmentProcessor* dst,
-                                                      SkXfermode::Mode mode);
+    sk_sp<GrFragmentProcessor> MakeFromDstProcessor(sk_sp<GrFragmentProcessor> dst,
+                                                    SkXfermode::Mode mode);
 
     /** The color input to the returned processor is treated as the dst and the passed in processor
         is the src. */
-    const GrFragmentProcessor* CreateFromSrcProcessor(const GrFragmentProcessor* src,
-                                                      SkXfermode::Mode mode);
+    sk_sp<GrFragmentProcessor> MakeFromSrcProcessor(sk_sp<GrFragmentProcessor> src,
+                                                    SkXfermode::Mode mode);
 
     /** Takes the input color, which is assumed to be unpremultiplied, passes it as an opaque color
         to both src and dst. The outputs of a src and dst are blended using mode and the original
         input's alpha is applied to the blended color to produce a premul output. */
-    const GrFragmentProcessor* CreateFromTwoProcessors(const GrFragmentProcessor* src,
-                                                       const GrFragmentProcessor* dst,
-                                                       SkXfermode::Mode mode);
+    sk_sp<GrFragmentProcessor> MakeFromTwoProcessors(sk_sp<GrFragmentProcessor> src,
+                                                     sk_sp<GrFragmentProcessor> dst,
+                                                     SkXfermode::Mode mode);
 };
 
 #endif