Added GrSurfaceContext and GrTextureContext

This lets copy-to-texture to be treated like copy-to-rt.
To match current behavior, though, copies to texture are
still executed immediately (forcing a flush).

Once MDB is enabled, copies to texture will be deferred.

BUG=skia:

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

Change-Id: Icc0ce5435507a5f0a237c22eedef879824952367
Reviewed-on: https://skia-review.googlesource.com/5093
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h
index dcbe8ea..2d4eaf7 100644
--- a/include/gpu/GrRenderTarget.h
+++ b/include/gpu/GrRenderTarget.h
@@ -115,10 +115,6 @@
     GrRenderTargetPriv renderTargetPriv();
     const GrRenderTargetPriv renderTargetPriv() const;
 
-    GrRenderTargetOpList* getLastRenderTargetOpList() {
-        return (GrRenderTargetOpList*) this->getLastOpList();
-    }
-
 protected:
     enum class Flags {
         kNone                = 0,
diff --git a/include/gpu/GrRenderTargetContext.h b/include/gpu/GrRenderTargetContext.h
index 6ed321c..20ca59f 100644
--- a/include/gpu/GrRenderTargetContext.h
+++ b/include/gpu/GrRenderTargetContext.h
@@ -11,25 +11,19 @@
 #include "GrColor.h"
 #include "GrContext.h"
 #include "GrPaint.h"
-#include "GrRenderTarget.h"
+#include "GrSurfaceContext.h"
 #include "SkRefCnt.h"
-#include "SkRegion.h"
 #include "SkSurfaceProps.h"
 #include "../private/GrInstancedPipelineInfo.h"
 #include "../private/GrRenderTargetProxy.h"
-#include "../private/GrSingleOwner.h"
 
-class GrAuditTrail;
 class GrClip;
 class GrDrawBatch;
-class GrRenderTargetContextPriv;
-class GrDrawPathBatchBase;
 class GrDrawingManager;
 class GrFixedClip;
-class GrPaint;
-class GrPathProcessor;
 class GrPipelineBuilder;
 class GrRenderTarget;
+class GrRenderTargetContextPriv;
 class GrRenderTargetOpList;
 class GrStyle;
 class GrSurface;
@@ -44,18 +38,19 @@
 class SkPath;
 struct SkPoint;
 struct SkRect;
+class SkRegion;
 class SkRRect;
 struct SkRSXform;
 class SkTextBlob;
 
-/*
- * A helper object to orchestrate draws
+/**
+ * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
  */
-class SK_API GrRenderTargetContext : public SkRefCnt {
+class SK_API GrRenderTargetContext : public GrSurfaceContext {
 public:
     ~GrRenderTargetContext() override;
 
-    bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
+    bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
 
     // TODO: it is odd that we need both the SkPaint in the following 3 methods.
     // We should extract the text parameters from SkPaint and pass them separately
@@ -385,8 +380,6 @@
     GrRenderTargetContextPriv priv();
     const GrRenderTargetContextPriv priv() const;
 
-    GrAuditTrail* auditTrail() { return fAuditTrail; }
-
     bool isWrapped_ForTesting() const;
 
 protected:
@@ -396,7 +389,6 @@
 
     GrDrawingManager* drawingManager() { return fDrawingManager; }
 
-    SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
     SkDEBUGCODE(void validate() const;)
 
 private:
@@ -462,16 +454,11 @@
     // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
     // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
     GrRenderTargetOpList*             fOpList;
-    GrContext*                        fContext;
     GrInstancedPipelineInfo           fInstancedPipelineInfo;
 
     sk_sp<SkColorSpace>               fColorSpace;
     sk_sp<GrColorSpaceXform>          fColorXformFromSRGB;
     SkSurfaceProps                    fSurfaceProps;
-    GrAuditTrail*                     fAuditTrail;
-
-    // In debug builds we guard against improper thread handling
-    SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
 };
 
 #endif
diff --git a/include/gpu/GrSurfaceContext.h b/include/gpu/GrSurfaceContext.h
new file mode 100644
index 0000000..a05d37f
--- /dev/null
+++ b/include/gpu/GrSurfaceContext.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrSurfaceContext_DEFINED
+#define GrSurfaceContext_DEFINED
+
+#include "SkRefCnt.h"
+
+class GrAuditTrail;
+class GrContext;
+class GrSingleOwner;
+class GrSurface;
+struct SkIPoint;
+struct SkIRect;
+
+/**
+ * A helper object to orchestrate commands for a particular surface
+ */
+class SK_API GrSurfaceContext : public SkRefCnt {
+public:
+    ~GrSurfaceContext() override {}
+
+    virtual bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) = 0;
+
+    GrAuditTrail* auditTrail() { return fAuditTrail; }
+
+protected:
+    GrSurfaceContext(GrContext*, GrAuditTrail*, GrSingleOwner*);
+
+    SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
+
+    GrContext*            fContext;
+    GrAuditTrail*         fAuditTrail;
+
+    // In debug builds we guard against improper thread handling
+    SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
+};
+
+#endif
diff --git a/include/gpu/GrTextureContext.h b/include/gpu/GrTextureContext.h
new file mode 100644
index 0000000..da71c07
--- /dev/null
+++ b/include/gpu/GrTextureContext.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrTextureContext_DEFINED
+#define GrTextureContext_DEFINED
+
+#include "GrSurfaceContext.h"
+#include "../private/GrTextureProxy.h"
+
+class GrContext;
+class GrDrawingManager;
+class GrSurface;
+class GrTextureOpList;
+class GrTextureProxy;
+struct SkIPoint;
+struct SkIRect;
+
+/**
+ * A helper object to orchestrate commands (currently just copies) for GrSurfaces that are
+ * GrTextures and not GrRenderTargets.
+ */
+class SK_API GrTextureContext : public GrSurfaceContext {
+public:
+    ~GrTextureContext() override;
+
+    bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
+
+protected:
+    GrTextureContext(GrContext*, GrDrawingManager*, sk_sp<GrTextureProxy>, GrAuditTrail*,
+                     GrSingleOwner*);
+
+    GrDrawingManager* drawingManager() { return fDrawingManager; }
+
+    SkDEBUGCODE(void validate() const;)
+
+private:
+    friend class GrDrawingManager; // for ctor
+
+    GrTextureOpList* getOpList();
+
+    GrDrawingManager*            fDrawingManager;
+    sk_sp<GrTextureProxy>        fTextureProxy;
+
+    // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
+    // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
+    GrTextureOpList*             fOpList;
+};
+
+#endif
diff --git a/include/private/GrRenderTargetProxy.h b/include/private/GrRenderTargetProxy.h
index bfc6e05..83107da 100644
--- a/include/private/GrRenderTargetProxy.h
+++ b/include/private/GrRenderTargetProxy.h
@@ -53,12 +53,6 @@
 
     GrRenderTarget::Flags testingOnly_getFlags() const;
 
-    GrRenderTargetOpList* getLastRenderTargetOpList() {
-        return (GrRenderTargetOpList*) this->getLastOpList();
-    }
-
-    SkDEBUGCODE(void validate(GrContext*) const;)
-
 protected:
     friend class GrSurfaceProxy;  // for ctors
 
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index 1ff6df8..61dc9e1 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -13,11 +13,11 @@
 #include "SkRect.h"
 
 class GrCaps;
-class GrOpList;
+class GrRenderTargetOpList;
+class GrRenderTargetProxy;
+class GrTextureOpList;
 class GrTextureProvider;
 class GrTextureProxy;
-class GrRenderTargetProxy;
-class GrTextureProvider;
 
 // This class replicates the functionality GrIORef<GrSurface> but tracks the
 // utilitization for later resource allocation (for the deferred case) and
@@ -244,6 +244,9 @@
     void setLastOpList(GrOpList* opList);
     GrOpList* getLastOpList() { return fLastOpList; }
 
+    GrRenderTargetOpList* getLastRenderTargetOpList();
+    GrTextureOpList* getLastTextureOpList();
+
     /**
      * Retrieves the amount of GPU memory that will be or currently is used by this resource 
      * in bytes. It is approximate since we aren't aware of additional padding or copies made
@@ -261,6 +264,8 @@
 
     bool isWrapped_ForTesting() const;
 
+    SkDEBUGCODE(void validate(GrContext*) const;)
+
 protected:
     // Deferred version
     GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted)
diff --git a/include/private/GrTextureProxy.h b/include/private/GrTextureProxy.h
index a206e76..5eb3066 100644
--- a/include/private/GrTextureProxy.h
+++ b/include/private/GrTextureProxy.h
@@ -12,6 +12,7 @@
 #include "GrTexture.h"
 
 class GrCaps;
+class GrTextureOpList;
 class GrTextureProvider;
 
 // This class delays the acquisition of textures until they are actually required