Remove GrFlushToGpuDrawTarget and move functionality up to GrDrawTarget.

Review URL: https://codereview.chromium.org/1117433002
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 4547aad..f9af6eb 100755
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -1913,7 +1913,7 @@
                                    DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
                                    DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
 
-    fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
+    fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this,
                                                    fDrawBufferVBAllocPool,
                                                    fDrawBufferIBAllocPool));
 }
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index d6fbbc2..00f6a8a 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -9,6 +9,7 @@
 #include "GrDrawTarget.h"
 
 #include "GrBatch.h"
+#include "GrBufferAllocPool.h"
 #include "GrContext.h"
 #include "GrDrawTargetCaps.h"
 #include "GrPath.h"
@@ -55,9 +56,15 @@
 #define DEBUG_INVAL_BUFFER 0xdeadcafe
 #define DEBUG_INVAL_START_IDX -1
 
-GrDrawTarget::GrDrawTarget(GrContext* context)
+GrDrawTarget::GrDrawTarget(GrContext* context,
+                           GrVertexBufferAllocPool* vpool,
+                           GrIndexBufferAllocPool* ipool)
     : fContext(context)
-    , fGpuTraceMarkerCount(0) {
+    , fCaps(SkRef(context->getGpu()->caps()))
+    , fGpuTraceMarkerCount(0)
+    , fVertexPool(vpool)
+    , fIndexPool(ipool)
+    , fFlushing(false) {
     SkASSERT(context);
 }
 
@@ -93,7 +100,13 @@
     // MSAA consideration: When there is support for reading MSAA samples in the shader we could
     // have per-sample dst values by making the copy multisampled.
     GrSurfaceDesc desc;
-    this->initCopySurfaceDstDesc(rt, &desc);
+    if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) {
+        desc.fOrigin = kDefault_GrSurfaceOrigin;
+        desc.fFlags = kRenderTarget_GrSurfaceFlag;
+        desc.fConfig = rt->config();
+    }
+
+
     desc.fWidth = copyRect.width();
     desc.fHeight = copyRect.height();
 
@@ -114,6 +127,29 @@
     }
 }
 
+void GrDrawTarget::reset() {
+    fVertexPool->reset();
+    fIndexPool->reset();
+
+    this->onReset();
+}
+
+void GrDrawTarget::flush() {
+    if (fFlushing) {
+        return;
+    }
+    fFlushing = true;
+
+    this->getGpu()->saveActiveTraceMarkers();
+
+    this->onFlush();
+
+    this->getGpu()->restoreActiveTraceMarkers();
+
+    fFlushing = false;
+    this->reset();
+}
+
 void GrDrawTarget::drawBatch(GrPipelineBuilder* pipelineBuilder,
                              GrBatch* batch,
                              const SkRect* devBounds) {
@@ -413,7 +449,8 @@
         return true;
     }
 
-    if (this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
+    if (this->getGpu()->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
+        this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
         return true;
     }
 
@@ -457,23 +494,8 @@
                                    &clippedDstPoint)) {
         return true;
     }
-    return this->internalCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
-}
-
-bool GrDrawTarget::internalCanCopySurface(const GrSurface* dst,
-                                          const GrSurface* src,
-                                          const SkIRect& clippedSrcRect,
-                                          const SkIPoint& clippedDstPoint) {
-    // Check that the read/write rects are contained within the src/dst bounds.
-    SkASSERT(!clippedSrcRect.isEmpty());
-    SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(clippedSrcRect));
-    SkASSERT(clippedDstPoint.fX >= 0 && clippedDstPoint.fY >= 0);
-    SkASSERT(clippedDstPoint.fX + clippedSrcRect.width() <= dst->width() &&
-             clippedDstPoint.fY + clippedSrcRect.height() <= dst->height());
-
-    // The base class can do it as a draw or the subclass may be able to handle it.
     return ((dst != src) && dst->asRenderTarget() && src->asTexture()) ||
-           this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
+           this->getGpu()->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
 }
 
 void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 8706702..cd2f436 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -31,9 +31,11 @@
 class GrBatch;
 class GrClip;
 class GrDrawTargetCaps;
+class GrIndexBufferAllocPool;
 class GrPath;
 class GrPathRange;
 class GrPipeline;
+class GrVertexBufferAllocPool;
 
 class GrDrawTarget : public SkRefCnt {
 public:
@@ -46,10 +48,22 @@
 
     // The context may not be fully constructed and should not be used during GrDrawTarget
     // construction.
-    GrDrawTarget(GrContext* context);
+    GrDrawTarget(GrContext* context, GrVertexBufferAllocPool*, GrIndexBufferAllocPool*);
+
     virtual ~GrDrawTarget() {}
 
     /**
+     * Empties the draw buffer of any queued up draws.
+     */
+    void reset();
+
+    /**
+     * This plays any queued up draws to its GrGpu target. It also resets this object (i.e. flushing
+     * is destructive).
+     */
+    void flush();
+
+    /**
      * Gets the capabilities of the draw target.
      */
     const GrDrawTargetCaps* caps() const { return fCaps.get(); }
@@ -301,8 +315,17 @@
     GrContext* getContext() { return fContext; }
     const GrContext* getContext() const { return fContext; }
 
-    // Subclass must initialize this in its constructor.
-    SkAutoTUnref<const GrDrawTargetCaps> fCaps;
+    GrGpu* getGpu() {
+        SkASSERT(fContext && fContext->getGpu());
+        return fContext->getGpu();
+    }
+    const GrGpu* getGpu() const {
+        SkASSERT(fContext && fContext->getGpu());
+        return fContext->getGpu();
+    }
+
+    GrVertexBufferAllocPool* getVertexAllocPool() { return fVertexPool; }
+    GrIndexBufferAllocPool* getIndexAllocPool() { return fIndexPool; }
 
     const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
 
@@ -342,24 +365,9 @@
     void setupPipeline(const PipelineInfo& pipelineInfo, GrPipeline* pipeline);
 
 private:
-    /**
-     * This will be called before allocating a texture as a dst for copySurface. This function
-     * populates the dstDesc's config, flags, and origin so as to maximize efficiency and guarantee
-     * success of the copySurface call.
-     */
-    void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* dstDesc) {
-        if (!this->onInitCopySurfaceDstDesc(src, dstDesc)) {
-            dstDesc->fOrigin = kDefault_GrSurfaceOrigin;
-            dstDesc->fFlags = kRenderTarget_GrSurfaceFlag;
-            dstDesc->fConfig = src->config();
-        }
-    }
+    virtual void onReset() = 0;
 
-    /** Internal implementation of canCopySurface. */
-    bool internalCanCopySurface(const GrSurface* dst,
-                                const GrSurface* src,
-                                const SkIRect& clippedSrcRect,
-                                const SkIPoint& clippedDstRect);
+    virtual void onFlush() = 0;
 
     virtual void onDrawBatch(GrBatch*, const PipelineInfo&) = 0;
     // TODO copy in order drawbuffer onDrawRect to here
@@ -392,30 +400,13 @@
     virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
                          GrRenderTarget* renderTarget) = 0;
 
-    /** The subclass will get a chance to copy the surface for falling back to the default
-        implementation, which simply draws a rectangle (and fails if dst isn't a render target). It
-        should assume that any clipping has already been performed on the rect and point. It won't
-        be called if the copy can be skipped. */
-    virtual bool onCopySurface(GrSurface* dst,
+    /** The subclass's copy surface implementation. It should assume that any clipping has already
+        been performed on the rect and point and that the GrGpu supports the copy. */
+    virtual void onCopySurface(GrSurface* dst,
                                GrSurface* src,
                                const SkIRect& srcRect,
                                const SkIPoint& dstPoint) = 0;
 
-    /** Indicates whether onCopySurface would succeed. It should assume that any clipping has
-        already been performed on the rect and point. It won't be called if the copy can be
-        skipped. */
-    virtual bool onCanCopySurface(const GrSurface* dst,
-                                  const GrSurface* src,
-                                  const SkIRect& srcRect,
-                                  const SkIPoint& dstPoint) = 0;
-    /**
-     * This will be called before allocating a texture to be a dst for onCopySurface. Only the
-     * dstDesc's config, flags, and origin need be set by the function. If the subclass cannot
-     * create a surface that would succeed its implementation of onCopySurface, it should return
-     * false. The base class will fall back to creating a render target to draw into using the src.
-     */
-    virtual bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* dstDesc) = 0;
-
     // Check to see if this set of draw commands has been sent out
     virtual bool       isIssued(uint32_t drawID) { return true; }
     void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
@@ -430,10 +421,14 @@
 
     // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
     GrContext*                                                      fContext;
+    SkAutoTUnref<const GrDrawTargetCaps>                            fCaps;
     // To keep track that we always have at least as many debug marker adds as removes
     int                                                             fGpuTraceMarkerCount;
     GrTraceMarkerSet                                                fActiveTraceMarkers;
     GrTraceMarkerSet                                                fStoredTraceMarkers;
+    GrVertexBufferAllocPool*                                        fVertexPool;
+    GrIndexBufferAllocPool*                                         fIndexPool;
+    bool                                                            fFlushing;
 
     typedef SkRefCnt INHERITED;
 };
@@ -443,7 +438,10 @@
  */
 class GrClipTarget : public GrDrawTarget {
 public:
-    GrClipTarget(GrContext* context) : INHERITED(context) {
+    GrClipTarget(GrContext* context,
+                 GrVertexBufferAllocPool* vpool,
+                 GrIndexBufferAllocPool* ipool)
+        : INHERITED(context, vpool, ipool) {
         fClipMaskManager.setClipTarget(this);
     }
 
diff --git a/src/gpu/GrFlushToGpuDrawTarget.cpp b/src/gpu/GrFlushToGpuDrawTarget.cpp
deleted file mode 100644
index 416051d..0000000
--- a/src/gpu/GrFlushToGpuDrawTarget.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "GrFlushToGpuDrawTarget.h"
-#include "GrContext.h"
-#include "GrGpu.h"
-#include "GrBufferAllocPool.h"
-
-GrFlushToGpuDrawTarget::GrFlushToGpuDrawTarget(GrGpu* gpu,
-                                               GrVertexBufferAllocPool* vertexPool,
-                                               GrIndexBufferAllocPool* indexPool)
-    : INHERITED(gpu->getContext())
-    , fGpu(SkRef(gpu))
-    , fVertexPool(vertexPool)
-    , fIndexPool(indexPool)
-    , fFlushing(false) {
-
-    fCaps.reset(SkRef(fGpu->caps()));
-
-    SkASSERT(vertexPool);
-    SkASSERT(indexPool);
-
-}
-
-void GrFlushToGpuDrawTarget::reset() {
-    fVertexPool->reset();
-    fIndexPool->reset();
-
-    this->onReset();
-}
-
-void GrFlushToGpuDrawTarget::flush() {
-    if (fFlushing) {
-        return;
-    }
-    fFlushing = true;
-
-    fGpu->saveActiveTraceMarkers();
-
-    this->onFlush();
-
-    fGpu->restoreActiveTraceMarkers();
-
-    fFlushing = false;
-    this->reset();
-}
-
-bool GrFlushToGpuDrawTarget::onCanCopySurface(const GrSurface* dst,
-                                              const GrSurface* src,
-                                              const SkIRect& srcRect,
-                                              const SkIPoint& dstPoint) {
-    return getGpu()->canCopySurface(dst, src, srcRect, dstPoint);
-}
-
-bool GrFlushToGpuDrawTarget::onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
-    return getGpu()->initCopySurfaceDstDesc(src, desc);
-}
diff --git a/src/gpu/GrFlushToGpuDrawTarget.h b/src/gpu/GrFlushToGpuDrawTarget.h
deleted file mode 100644
index b379f2e..0000000
--- a/src/gpu/GrFlushToGpuDrawTarget.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef GrFlushToGpuDrawTarget_DEFINED
-#define GrFlushToGpuDrawTarget_DEFINED
-
-#include "GrDrawTarget.h"
-
-class GrIndexBufferAllocPool;
-class GrVertexBufferAllocPool;
-class GrGpu;
-
-/**
- * Base class for draw targets that accumulate index and vertex data in buffers for deferred.
- * When draw target clients reserve geometry this subclass will place that geometry into
- * preallocated vertex/index buffers in the order the requests are made (assuming the requests fit
- * in the preallocated buffers).
- */
-class GrFlushToGpuDrawTarget : public GrClipTarget {
-public:
-    GrFlushToGpuDrawTarget(GrGpu*, GrVertexBufferAllocPool*,GrIndexBufferAllocPool*);
-
-    /**
-     * Empties the draw buffer of any queued up draws. This must not be called while inside an
-     * unbalanced pushGeometrySource().
-     */
-    void reset();
-
-    /**
-     * This plays any queued up draws to its GrGpu target. It also resets this object (i.e. flushing
-     * is destructive). This buffer must not have an active reserved vertex or index source. Any
-     * reserved geometry on the target will be finalized because it's geometry source will be pushed
-     * before flushing and popped afterwards.
-     */
-    void flush();
-
-protected:
-    GrGpu* getGpu() { return fGpu; }
-    const GrGpu* getGpu() const{ return fGpu; }
-
-    GrVertexBufferAllocPool* getVertexAllocPool() { return fVertexPool; }
-    GrIndexBufferAllocPool* getIndexAllocPool() { return fIndexPool; }
-
-private:
-    virtual void onReset() = 0;
-
-    virtual void onFlush() = 0;
-
-    bool onCanCopySurface(const GrSurface* dst,
-                          const GrSurface* src,
-                          const SkIRect& srcRect,
-                          const SkIPoint& dstPoint) override;
-    bool onInitCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override;
-
-    SkAutoTUnref<GrGpu>                 fGpu;
-    GrVertexBufferAllocPool*            fVertexPool;
-    GrIndexBufferAllocPool*             fIndexPool;
-    bool                                fFlushing;
-
-    typedef GrClipTarget INHERITED;
-};
-
-#endif
diff --git a/src/gpu/GrInOrderDrawBuffer.cpp b/src/gpu/GrInOrderDrawBuffer.cpp
index 82c8840..cb1e741 100644
--- a/src/gpu/GrInOrderDrawBuffer.cpp
+++ b/src/gpu/GrInOrderDrawBuffer.cpp
@@ -10,11 +10,11 @@
 #include "GrDefaultGeoProcFactory.h"
 #include "GrTemplates.h"
 
-GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrGpu* gpu,
+GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrContext* context,
                                          GrVertexBufferAllocPool* vertexPool,
                                          GrIndexBufferAllocPool* indexPool)
-    : INHERITED(gpu, vertexPool, indexPool)
-    , fCommands(gpu, vertexPool, indexPool)
+    : INHERITED(context, vertexPool, indexPool)
+    , fCommands(context->getGpu(), vertexPool, indexPool)
     , fPathIndexBuffer(kPathIdxBufferMinReserve * sizeof(char)/4)
     , fPathTransformBuffer(kPathXformBufferMinReserve * sizeof(float)/4)
     , fDrawID(0) {
@@ -376,14 +376,13 @@
     ++fDrawID;
 }
 
-bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
+void GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
                                         GrSurface* src,
                                         const SkIRect& srcRect,
                                         const SkIPoint& dstPoint) {
-    GrTargetCommands::Cmd* cmd = fCommands.recordCopySurface(this, dst, src,
-                                                             srcRect, dstPoint);
+    SkASSERT(this->getGpu()->canCopySurface(dst, src, srcRect, dstPoint));
+    GrTargetCommands::Cmd* cmd = fCommands.recordCopySurface(dst, src, srcRect, dstPoint);
     this->recordTraceMarkersIfNecessary(cmd);
-    return SkToBool(cmd);
 }
 
 void GrInOrderDrawBuffer::recordTraceMarkersIfNecessary(GrTargetCommands::Cmd* cmd) {
diff --git a/src/gpu/GrInOrderDrawBuffer.h b/src/gpu/GrInOrderDrawBuffer.h
index 11558f8..ce82c88 100644
--- a/src/gpu/GrInOrderDrawBuffer.h
+++ b/src/gpu/GrInOrderDrawBuffer.h
@@ -8,7 +8,7 @@
 #ifndef GrInOrderDrawBuffer_DEFINED
 #define GrInOrderDrawBuffer_DEFINED
 
-#include "GrFlushToGpuDrawTarget.h"
+#include "GrDrawTarget.h"
 #include "GrTargetCommands.h"
 #include "SkChunkAlloc.h"
 
@@ -22,19 +22,19 @@
  * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
  * store geometry.
  */
-class GrInOrderDrawBuffer : public GrFlushToGpuDrawTarget {
+class GrInOrderDrawBuffer : public GrClipTarget {
 public:
 
     /**
      * Creates a GrInOrderDrawBuffer
      *
-     * @param gpu        the gpu object that this draw buffer flushes to.
+     * @param context    the context object that owns this draw buffer.
      * @param vertexPool pool where vertices for queued draws will be saved when
      *                   the vertex source is either reserved or array.
      * @param indexPool  pool where indices for queued draws will be saved when
      *                   the index source is either reserved or array.
      */
-    GrInOrderDrawBuffer(GrGpu* gpu,
+    GrInOrderDrawBuffer(GrContext* context,
                         GrVertexBufferAllocPool* vertexPool,
                         GrIndexBufferAllocPool* indexPool);
 
@@ -107,7 +107,7 @@
                  GrColor color,
                  bool canIgnoreRect,
                  GrRenderTarget* renderTarget) override;
-    bool onCopySurface(GrSurface* dst,
+    void onCopySurface(GrSurface* dst,
                        GrSurface* src,
                        const SkIRect& srcRect,
                        const SkIPoint& dstPoint) override;
@@ -134,7 +134,7 @@
     SkChunkAlloc                        fPathTransformBuffer;
     uint32_t                            fDrawID;
 
-    typedef GrFlushToGpuDrawTarget INHERITED;
+    typedef GrClipTarget INHERITED;
 };
 
 #endif
diff --git a/src/gpu/GrTargetCommands.cpp b/src/gpu/GrTargetCommands.cpp
index 69ef53b..c448db9 100644
--- a/src/gpu/GrTargetCommands.cpp
+++ b/src/gpu/GrTargetCommands.cpp
@@ -341,18 +341,14 @@
     gpu->copySurface(this->dst(), this->src(), fSrcRect, fDstPoint);
 }
 
-GrTargetCommands::Cmd* GrTargetCommands::recordCopySurface(GrInOrderDrawBuffer* iodb,
-                                                           GrSurface* dst,
+GrTargetCommands::Cmd* GrTargetCommands::recordCopySurface(GrSurface* dst,
                                                            GrSurface* src,
                                                            const SkIRect& srcRect,
                                                            const SkIPoint& dstPoint) {
-    if (iodb->getGpu()->canCopySurface(dst, src, srcRect, dstPoint)) {
-        CopySurface* cs = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, CopySurface, (dst, src));
-        cs->fSrcRect = srcRect;
-        cs->fDstPoint = dstPoint;
-        return cs;
-    }
-    return NULL;
+    CopySurface* cs = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, CopySurface, (dst, src));
+    cs->fSrcRect = srcRect;
+    cs->fDstPoint = dstPoint;
+    return cs;
 }
 
 bool GrTargetCommands::setupPipelineAndShouldDraw(GrInOrderDrawBuffer* iodb,
diff --git a/src/gpu/GrTargetCommands.h b/src/gpu/GrTargetCommands.h
index 696fcec..cff9898 100644
--- a/src/gpu/GrTargetCommands.h
+++ b/src/gpu/GrTargetCommands.h
@@ -125,8 +125,7 @@
                      GrColor,
                      bool canIgnoreRect,
                      GrRenderTarget*);
-    Cmd* recordCopySurface(GrInOrderDrawBuffer*,
-                           GrSurface* dst,
+    Cmd* recordCopySurface(GrSurface* dst,
                            GrSurface* src,
                            const SkIRect& srcRect,
                            const SkIPoint& dstPoint);