patch from issue 886233004 at patchset 40001 (http://crrev.com/886233004#ps40001)

... with changes proposed in the review.

BUG=skia:

Review URL: https://codereview.chromium.org/894013002
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 07073a5..bcd84dd 100755
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -1578,16 +1578,3 @@
     }
 }
 
-///////////////////////////////////////////////////////////////////////////////
-#if GR_CACHE_STATS
-void GrContext::printCacheStats() const {
-    fResourceCache2->printStats();
-}
-#endif
-
-#if GR_GPU_STATS
-const GrContext::GPUStats* GrContext::gpuStats() const {
-    return fGpu->gpuStats();
-}
-#endif
-
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index 5680d5c..c989589 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -26,7 +26,6 @@
 
 class GrGpu : public SkRefCnt {
 public:
-
     /**
      * Additional blend coefficients for dual source blending, not exposed
      * through GrPaint/GrContext.
@@ -62,9 +61,7 @@
      */
     const GrDrawTargetCaps* caps() const { return fCaps.get(); }
 
-    GrPathRendering* pathRendering() {
-        return fPathRendering.get();
-    }
+    GrPathRendering* pathRendering() { return fPathRendering.get(); }
 
     // Called by GrContext when the underlying backend context has been destroyed.
     // GrGpu should use this to ensure that no backend API calls will be made from
@@ -78,11 +75,7 @@
      * the GrGpu that the state was modified and it shouldn't make assumptions
      * about the state.
      */
-    void markContextDirty(uint32_t state = kAll_GrBackendState) {
-        fResetBits |= state;
-    }
-
-    void unimpl(const char[]);
+    void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
 
     /**
      * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
@@ -302,33 +295,12 @@
     // is dirty.
     ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
 
-    GrContext::GPUStats* gpuStats() { return &fGPUStats; }
-
     virtual void buildProgramDesc(GrProgramDesc*,
                                   const GrPrimitiveProcessor&,
                                   const GrPipeline&,
                                   const GrProgramDesc::DescInfo&,
                                   const GrBatchTracker&) const = 0;
 
-    /**
-     * Called at start and end of gpu trace marking
-     * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
-     * and end of a code block respectively
-     */
-    void addGpuTraceMarker(const GrGpuTraceMarker* marker);
-    void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
-
-    /**
-     * Takes the current active set of markers and stores them for later use. Any current marker
-     * in the active set is removed from the active set and the targets remove function is called.
-     * These functions do not work as a stack so you cannot call save a second time before calling
-     * restore. Also, it is assumed that when restore is called the current active set of markers
-     * is empty. When the stored markers are added back into the active set, the targets add marker
-     * is called.
-     */
-    void saveActiveTraceMarkers();
-    void restoreActiveTraceMarkers();
-
     // Called to determine whether a copySurface call would succeed or not. Derived
     // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks
     // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function
@@ -389,6 +361,53 @@
                    int count,
                    const GrStencilSettings&);
 
+    ///////////////////////////////////////////////////////////////////////////
+    // Debugging and Stats
+
+    class Stats {
+    public:
+#if GR_GPU_STATS
+        Stats() { this->reset(); }
+
+        void reset() { fRenderTargetBinds = 0; fShaderCompilations = 0; }
+
+        int renderTargetBinds() const { return fRenderTargetBinds; }
+        void incRenderTargetBinds() { fRenderTargetBinds++; }
+        int shaderCompilations() const { return fShaderCompilations; }
+        void incShaderCompilations() { fShaderCompilations++; }
+        void dump(SkString*);
+
+    private:
+        int fRenderTargetBinds;
+        int fShaderCompilations;
+#else
+        void dump(SkString*) {};
+        void incRenderTargetBinds() {}
+        void incShaderCompilations() {}
+#endif
+    };
+
+    Stats* stats() { return &fStats; }
+
+    /**
+     * Called at start and end of gpu trace marking
+     * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
+     * and end of a code block respectively
+     */
+    void addGpuTraceMarker(const GrGpuTraceMarker* marker);
+    void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
+
+    /**
+     * Takes the current active set of markers and stores them for later use. Any current marker
+     * in the active set is removed from the active set and the targets remove function is called.
+     * These functions do not work as a stack so you cannot call save a second time before calling
+     * restore. Also, it is assumed that when restore is called the current active set of markers
+     * is empty. When the stored markers are added back into the active set, the targets add marker
+     * is called.
+     */
+    void saveActiveTraceMarkers();
+    void restoreActiveTraceMarkers();
+
 protected:
     // Functions used to map clip-respecting stencil tests into normal
     // stencil funcs supported by GPUs.
@@ -401,14 +420,12 @@
                                           unsigned int* ref,
                                           unsigned int* mask);
 
-    const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
+    const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; }
 
-    GrContext::GPUStats         fGPUStats;
-
-    SkAutoTDelete<GrPathRendering> fPathRendering;
-
+    Stats                                   fStats;
+    SkAutoTDelete<GrPathRendering>          fPathRendering;
     // Subclass must initialize this in its constructor.
-    SkAutoTUnref<const GrDrawTargetCaps> fCaps;
+    SkAutoTUnref<const GrDrawTargetCaps>    fCaps;
 
 private:
     // called when the 3D context state is unknown. Subclass should emit any
@@ -432,9 +449,7 @@
 
     // Overridden by backend specific classes to perform a clear of the stencil clip bits.  This is
     // ONLY used by the the clip target
-    virtual void onClearStencilClip(GrRenderTarget*,
-                                    const SkIRect& rect,
-                                    bool insideClip) = 0;
+    virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
 
     // overridden by backend-specific derived class to perform the draw call.
     virtual void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) = 0;
diff --git a/src/gpu/GrResourceCache2.cpp b/src/gpu/GrResourceCache2.cpp
index 99c4270..84a8d48 100644
--- a/src/gpu/GrResourceCache2.cpp
+++ b/src/gpu/GrResourceCache2.cpp
@@ -483,43 +483,3 @@
     // SkASSERT(!overBudget || locked == count || fPurging);
 }
 #endif
-
-#if GR_CACHE_STATS
-void GrResourceCache2::printStats() const {
-    this->validate();
-
-    int locked = 0;
-    int scratch = 0;
-    int wrapped = 0;
-    size_t unbudgetedSize = 0;
-
-    ResourceList::Iter iter;
-    GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
-
-    for ( ; resource; resource = iter.next()) {
-        if (!resource->isPurgeable()) {
-            ++locked;
-        }
-        if (resource->cacheAccess().isScratch()) {
-            ++scratch;
-        }
-        if (resource->cacheAccess().isWrapped()) {
-            ++wrapped;
-        }
-        if (!resource->cacheAccess().isBudgeted()) {
-            unbudgetedSize += resource->gpuMemorySize();
-        }
-    }
-
-    float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
-    float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
-
-    SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
-    SkDebugf("\t\tEntry Count: current %d"
-             " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
-        fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization, fHighWaterCount);
-    SkDebugf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
-                fBytes, fBudgetedBytes, byteUtilization, unbudgetedSize, fHighWaterBytes);
-}
-
-#endif
diff --git a/src/gpu/GrResourceCache2.h b/src/gpu/GrResourceCache2.h
index 9c9900a..4f21db9 100644
--- a/src/gpu/GrResourceCache2.h
+++ b/src/gpu/GrResourceCache2.h
@@ -16,6 +16,8 @@
 #include "SkTInternalLList.h"
 #include "SkTMultiMap.h"
 
+class SkString;
+
 /**
  * Manages the lifetime of all GrGpuResource instances.
  *
@@ -146,7 +148,7 @@
     }
 
 #if GR_GPU_STATS
-    void printStats() const;
+    void dumpStats(SkString*) const;
 #endif
 
 private:
diff --git a/src/gpu/GrTest.cpp b/src/gpu/GrTest.cpp
index e4c7a83..1689070 100644
--- a/src/gpu/GrTest.cpp
+++ b/src/gpu/GrTest.cpp
@@ -10,6 +10,7 @@
 
 #include "GrInOrderDrawBuffer.h"
 #include "GrResourceCache2.h"
+#include "SkString.h"
 
 void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
     SkASSERT(!fContext);
@@ -40,6 +41,80 @@
     fResourceCache2->purgeAllUnlocked();
 }
 
+void GrContext::dumpCacheStats(SkString* out) const {
+#if GR_CACHE_STATS
+    fResourceCache2->dumpStats(out);
+#endif
+}
+
+void GrContext::printCacheStats() const {
+    SkString out;
+    this->dumpCacheStats(&out);
+    SkDebugf(out.c_str());
+}
+
+void GrContext::dumpGpuStats(SkString* out) const {
+#if GR_GPU_STATS
+    return fGpu->stats()->dump(out);
+#endif
+}
+
+void GrContext::printGpuStats() const {
+    SkString out;
+    this->dumpGpuStats(&out);
+    SkDebugf(out.c_str());
+}
+
+#if GR_GPU_STATS
+void GrGpu::Stats::dump(SkString* out) {
+    out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
+    out->appendf("Shader Compilations: %d\n", fShaderCompilations);
+}
+#endif
+
+#if GR_CACHE_STATS
+void GrResourceCache2::dumpStats(SkString* out) const {
+    this->validate();
+
+    int locked = 0;
+    int scratch = 0;
+    int wrapped = 0;
+    size_t unbudgetedSize = 0;
+
+    ResourceList::Iter iter;
+    GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
+
+    for ( ; resource; resource = iter.next()) {
+        if (!resource->isPurgeable()) {
+            ++locked;
+        }
+        if (resource->cacheAccess().isScratch()) {
+            ++scratch;
+        }
+        if (resource->cacheAccess().isWrapped()) {
+            ++wrapped;
+        }
+        if (!resource->cacheAccess().isBudgeted()) {
+            unbudgetedSize += resource->gpuMemorySize();
+        }
+    }
+
+    float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
+    float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
+
+    out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
+    out->appendf("\t\tEntry Count: current %d"
+                 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
+                 fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization,
+                 fHighWaterCount);
+    out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
+                 (int)fBytes, (int)fBudgetedBytes, byteUtilization,
+                 (int)unbudgetedSize, (int)fHighWaterBytes);
+}
+
+#endif
+
+
 ///////////////////////////////////////////////////////////////////////////////
 // Code for the mock context. It's built on a mock GrGpu class that does nothing.
 ////
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 6d14e7e..e32b2e6 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -850,7 +850,7 @@
                                        desc.fWidth, desc.fHeight)) {
             goto FAILED;
         }
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
         GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
                                       GR_GL_COLOR_ATTACHMENT0,
@@ -865,7 +865,7 @@
             fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
         }
     }
-    fGPUStats.incRenderTargetBinds();
+    fStats.incRenderTargetBinds();
     GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
 
     if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
@@ -1209,7 +1209,7 @@
         GrGLuint rb = glsb->renderbufferID();
 
         fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
         GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
                                         GR_GL_STENCIL_ATTACHMENT,
@@ -1509,7 +1509,7 @@
     GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
     if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
         fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
     }
     switch (this->glCaps().invalidateFBType()) {
@@ -1670,7 +1670,7 @@
         case GrGLRenderTarget::kCanResolve_ResolveType:
             this->onResolveRenderTarget(tgt);
             // we don't track the state of the READ FBO ID.
-            fGPUStats.incRenderTargetBinds();
+            fStats.incRenderTargetBinds();
             GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
                                     tgt->textureFBOID()));
             break;
@@ -1768,7 +1768,7 @@
 
     uint32_t rtID = target->getUniqueID();
     if (fHWBoundRenderTargetUniqueID != rtID) {
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
 #ifdef SK_DEBUG
         // don't do this check in Chromium -- this is causing
@@ -1912,8 +1912,8 @@
         // Some extensions automatically resolves the texture when it is read.
         if (this->glCaps().usesMSAARenderBuffers()) {
             SkASSERT(rt->textureFBOID() != rt->renderFBOID());
-            fGPUStats.incRenderTargetBinds();
-            fGPUStats.incRenderTargetBinds();
+            fStats.incRenderTargetBinds();
+            fStats.incRenderTargetBinds();
             GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
             GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
             // make sure we go through flushRenderTarget() since we've modified
@@ -2519,7 +2519,7 @@
         SkASSERT(surface->asTexture());
         GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
         GR_GL_CALL(this->glInterface(), GenFramebuffers(1, &tempFBOID));
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, tempFBOID));
         GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
                                                              GR_GL_COLOR_ATTACHMENT0,
@@ -2532,7 +2532,7 @@
         viewport->fHeight = surface->height();
     } else {
         tempFBOID = 0;
-        fGPUStats.incRenderTargetBinds();
+        fStats.incRenderTargetBinds();
         GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
         *viewport = rt->getViewport();
     }
diff --git a/src/gpu/gl/builders/GrGLFragmentShaderBuilder.cpp b/src/gpu/gl/builders/GrGLFragmentShaderBuilder.cpp
index 46433f0..61932f3 100644
--- a/src/gpu/gl/builders/GrGLFragmentShaderBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLFragmentShaderBuilder.cpp
@@ -276,7 +276,7 @@
 
     GrGLuint fragShaderId = GrGLCompileAndAttachShader(gpu->glContext(), programId,
                                                        GR_GL_FRAGMENT_SHADER, fragShaderSrc,
-                                                       gpu->gpuStats());
+                                                       gpu->stats());
     if (!fragShaderId) {
         return false;
     }
diff --git a/src/gpu/gl/builders/GrGLGeometryShaderBuilder.cpp b/src/gpu/gl/builders/GrGLGeometryShaderBuilder.cpp
index 573c553..8be2531 100644
--- a/src/gpu/gl/builders/GrGLGeometryShaderBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLGeometryShaderBuilder.cpp
@@ -62,7 +62,7 @@
     GrGLuint geomShaderId =
         GrGLCompileAndAttachShader(glCtx, programId,
                                    GR_GL_GEOMETRY_SHADER, geomShaderSrc,
-                                   fProgramBuilder->gpu()->gpuStats());
+                                   fProgramBuilder->gpu()->stats());
     if (!geomShaderId) {
         return false;
     }
diff --git a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
index 4a0fee5..5265d20 100644
--- a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
@@ -21,7 +21,7 @@
                                     GrGLuint programId,
                                     GrGLenum type,
                                     const SkString& shaderSrc,
-                                    GrContext::GPUStats* gpuStats) {
+                                    GrGpu::Stats* stats) {
     const GrGLInterface* gli = glCtx.interface();
 
     GrGLuint shaderId;
@@ -39,7 +39,7 @@
     const GrGLchar* sourceStr = shaderSrc.c_str();
 #endif
     GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength));
-    gpuStats->incShaderCompilations();
+    stats->incShaderCompilations();
     GR_GL_CALL(gli, CompileShader(shaderId));
 
     // Calling GetShaderiv in Chromium is quite expensive. Assume success in release builds.
diff --git a/src/gpu/gl/builders/GrGLShaderStringBuilder.h b/src/gpu/gl/builders/GrGLShaderStringBuilder.h
index ab8734f..cf54253 100644
--- a/src/gpu/gl/builders/GrGLShaderStringBuilder.h
+++ b/src/gpu/gl/builders/GrGLShaderStringBuilder.h
@@ -9,7 +9,7 @@
 #define GrGLShaderStringBuilder_DEFINED
 
 #include "GrAllocator.h"
-#include "GrContext.h"
+#include "GrGpu.h"
 #include "gl/GrGLContext.h"
 #include "SkTypes.h"
 
@@ -17,6 +17,6 @@
                                     GrGLuint programId,
                                     GrGLenum type,
                                     const SkString& shaderSrc,
-                                    GrContext::GPUStats* gpuStats);
+                                    GrGpu::Stats*);
 
 #endif
diff --git a/src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp b/src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp
index f17e741..f1671af 100644
--- a/src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp
@@ -85,9 +85,8 @@
     vertShaderSrc.append("void main() {");
     vertShaderSrc.append(fCode);
     vertShaderSrc.append("}\n");
-    GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId,
-                                                       GR_GL_VERTEX_SHADER, vertShaderSrc,
-                                                       gpu->gpuStats());
+    GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, GR_GL_VERTEX_SHADER,
+                                                       vertShaderSrc, gpu->stats());
     if (!vertShaderId) {
         return false;
     }