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/dm/DM.cpp b/dm/DM.cpp
index faee2cc..3efc5c1 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -49,15 +49,21 @@
 
 static int32_t gPending = 0;  // Atomic.
 
-static void done(double ms, ImplicitString config, ImplicitString src, ImplicitString name) {
+static void done(double ms,
+                 ImplicitString config, ImplicitString src, ImplicitString name,
+                 ImplicitString log) {
+    if (!log.isEmpty()) {
+        log.prepend("\n");
+    }
     int32_t pending = sk_atomic_dec(&gPending)-1;
-    SkDebugf("%s(%4dMB %5d) %s\t%s %s %s  ", FLAGS_verbose ? "\n" : kSkOverwriteLine
+    SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
                                            , sk_tools::getMaxResidentSetSizeMB()
                                            , pending
                                            , HumanizeMs(ms).c_str()
                                            , config.c_str()
                                            , src.c_str()
-                                           , name.c_str());
+                                           , name.c_str()
+                                           , log.c_str());
     // We write our dm.json file every once in a while in case we crash.
     // Notice this also handles the final dm.json when pending == 0.
     if (pending % 500 == 0) {
@@ -177,7 +183,8 @@
 
     SkBitmap bitmap;
     SkDynamicMemoryWStream stream;
-    Error err = sink->draw(noop, &bitmap, &stream);
+    SkString log;
+    Error err = sink->draw(noop, &bitmap, &stream, &log);
     if (!err.isEmpty()) {
         SkDebugf("Skipping %s: %s\n", tag, err.c_str());
         return;
@@ -297,12 +304,13 @@
     static void Run(Task* task) {
         SkString name = task->src->name();
         SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
+        SkString log;
         WallTimer timer;
         timer.start();
         if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
             SkBitmap bitmap;
             SkDynamicMemoryWStream stream;
-            Error err = task->sink->draw(*task->src, &bitmap, &stream);
+            Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
             if (!err.isEmpty()) {
                 fail(SkStringPrintf("%s %s %s: %s",
                                     task->sink.tag,
@@ -352,7 +360,7 @@
         if (!whyBlacklisted.isEmpty()) {
             name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
         }
-        done(timer.fWall, task->sink.tag, task->src.tag, name);
+        done(timer.fWall, task->sink.tag, task->src.tag, name, log);
     }
 
     static void WriteToDisk(const Task& task,
@@ -473,7 +481,7 @@
         test->proc(&reporter, &factory);
     }
     timer.end();
-    done(timer.fWall, "unit", "test", test->name);
+    done(timer.fWall, "unit", "test", test->name, "");
 }
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
diff --git a/dm/DMGpuSupport.h b/dm/DMGpuSupport.h
index 032151f..1ced019 100644
--- a/dm/DMGpuSupport.h
+++ b/dm/DMGpuSupport.h
@@ -42,6 +42,12 @@
 };
 static const int kGrGLStandardCnt = 3;
 
+class GrContext {
+public:
+    void dumpCacheStats(SkString*) const {}
+    void dumpGpuStats(SkString*) const {}
+};
+
 class GrContextFactory {
 public:
     typedef int GLContextType;
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index baf1871..8370573 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -131,6 +131,8 @@
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
+DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
+
 GPUSink::GPUSink(GrContextFactory::GLContextType ct,
                  GrGLStandard api,
                  int samples,
@@ -146,7 +148,7 @@
     return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
 }
 
-Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*) const {
+Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
     GrContextFactory factory;
     const SkISize size = src.size();
     const SkImageInfo info =
@@ -162,6 +164,10 @@
         return err;
     }
     canvas->flush();
+    if (FLAGS_gpuStats) {
+        canvas->getGrContext()->dumpCacheStats(log);
+        canvas->getGrContext()->dumpGpuStats(log);
+    }
     dst->allocPixels(info);
     canvas->readPixels(dst, 0,0);
     if (FLAGS_abandonGpuContext) {
@@ -174,7 +180,7 @@
 
 PDFSink::PDFSink() {}
 
-Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
+Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
     // Print the given DM:Src to a PDF, breaking on 8.5x11 pages.
     SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
 
@@ -213,7 +219,7 @@
 
 SKPSink::SKPSink() {}
 
-Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
+Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
     SkSize size;
     size = src.size();
     SkPictureRecorder recorder;
@@ -230,7 +236,7 @@
 
 SVGSink::SVGSink() {}
 
-Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst) const {
+Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
     SkAutoTUnref<SkBaseDevice> device(SkSVGDevice::Create(src.size(), dst));
     SkCanvas canvas(device);
     return src.draw(&canvas);
@@ -240,7 +246,7 @@
 
 RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
 
-Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*) const {
+Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
     const SkISize size = src.size();
     // If there's an appropriate alpha type for this color type, use it, otherwise use premul.
     SkAlphaType alphaType = kPremul_SkAlphaType;
@@ -256,7 +262,7 @@
 
 ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
 
-Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
+Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
     // We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
     struct ProxySrc : public Src {
         const Src& fSrc;
@@ -270,14 +276,14 @@
         SkISize size() const SK_OVERRIDE { return fSrc.size(); }
         Name name() const SK_OVERRIDE { sk_throw(); return ""; }  // No one should be calling this.
     } proxy(src, fMatrix);
-    return fSink->draw(proxy, bitmap, stream);
+    return fSink->draw(proxy, bitmap, stream, log);
 }
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
 
-Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
+Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
     // We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
     struct ProxySrc : public Src {
         const Src& fSrc;
@@ -293,14 +299,15 @@
         SkISize size() const SK_OVERRIDE { return fSrc.size(); }
         Name name() const SK_OVERRIDE { sk_throw(); return ""; }  // No one should be calling this.
     } proxy(src);
-    return fSink->draw(proxy, bitmap, stream);
+    return fSink->draw(proxy, bitmap, stream, log);
 }
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
 
-Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
+Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log)
+    const {
     // Record our Src into a picture.
     SkSize size;
     size = src.size();
@@ -330,7 +337,7 @@
         SkISize size() const SK_OVERRIDE { return fSize; }
         Name name() const SK_OVERRIDE { sk_throw(); return ""; }  // No one should be calling this.
     } proxy(deserialized, src.size());
-    return fSink->draw(proxy, bitmap, stream);
+    return fSink->draw(proxy, bitmap, stream, log);
 }
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@@ -341,7 +348,7 @@
     , fFactory(factory)
     , fSink(sink) {}
 
-Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream) const {
+Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
     // Record our Src into a picture.
     SkSize size;
     size = src.size();
@@ -396,7 +403,7 @@
         SkISize size() const SK_OVERRIDE { return fSize; }
         Name name() const SK_OVERRIDE { sk_throw(); return ""; }  // No one should be calling this.
     } proxy(fW, fH, pic, src.size());
-    return fSink->draw(proxy, bitmap, stream);
+    return fSink->draw(proxy, bitmap, stream, log);
 }
 
 }  // namespace DM
diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h
index 1fdc9ed..48c572b 100644
--- a/dm/DMSrcSink.h
+++ b/dm/DMSrcSink.h
@@ -33,9 +33,9 @@
 
 struct Sink {
     virtual ~Sink() {}
-    // You may write to either the bitmap or stream.
-    virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*) const
-        = 0;
+    // You may write to either the bitmap or stream.  If you write to log, we'll print that out.
+    virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
+        const = 0;
     // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
     virtual int enclave() const = 0;
 
@@ -90,7 +90,7 @@
 public:
     GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE;
     const char* fileExtension() const SK_OVERRIDE { return "png"; }
 private:
@@ -105,7 +105,7 @@
 public:
     PDFSink();
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return kPDFSink_Enclave; }
     const char* fileExtension() const SK_OVERRIDE { return "pdf"; }
 };
@@ -114,7 +114,7 @@
 public:
     explicit RasterSink(SkColorType);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
     const char* fileExtension() const SK_OVERRIDE { return "png"; }
 private:
@@ -125,7 +125,7 @@
 public:
     SKPSink();
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
     const char* fileExtension() const SK_OVERRIDE { return "skp"; }
 };
@@ -134,7 +134,7 @@
 public:
     SVGSink();
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
     const char* fileExtension() const SK_OVERRIDE { return "svg"; }
 };
@@ -146,7 +146,7 @@
 public:
     ViaMatrix(SkMatrix, Sink*);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return fSink->enclave(); }
     const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
 private:
@@ -158,7 +158,7 @@
 public:
     explicit ViaPipe(Sink*);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return fSink->enclave(); }
     const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
 private:
@@ -169,7 +169,7 @@
 public:
     explicit ViaSerialization(Sink*);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return fSink->enclave(); }
     const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
 private:
@@ -180,7 +180,7 @@
 public:
     ViaTiles(int w, int h, SkBBHFactory*, Sink*);
 
-    Error draw(const Src&, SkBitmap*, SkWStream*) const SK_OVERRIDE;
+    Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
     int enclave() const SK_OVERRIDE { return fSink->enclave(); }
     const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
 private:
@@ -191,7 +191,7 @@
 
 class NullSink : public Sink {
 public:
-    Error draw(const Src& src, SkBitmap*, SkWStream* out) const SK_OVERRIDE {
+    Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE {
         return src.draw(SkCreateNullCanvas());
     }
     int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 88a3148..e47ad15 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -768,33 +768,13 @@
      */
     const Options& getOptions() const { return fOptions; }
 
-#if GR_CACHE_STATS
+    /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
+    void dumpCacheStats(SkString*) const;
     void printCacheStats() const;
-#endif
 
-    class GPUStats {
-    public:
-#if GR_GPU_STATS
-        GPUStats() { this->reset(); }
-
-        void reset() { fRenderTargetBinds = 0; fShaderCompilations = 0; }
-
-        int renderTargetBinds() const { return fRenderTargetBinds; }
-        void incRenderTargetBinds() { fRenderTargetBinds++; }
-        int shaderCompilations() const { return fShaderCompilations; }
-        void incShaderCompilations() { fShaderCompilations++; }
-    private:
-        int fRenderTargetBinds;
-        int fShaderCompilations;
-#else
-        void incRenderTargetBinds() {}
-        void incShaderCompilations() {}
-#endif
-    };
-
-#if GR_GPU_STATS
-    const GPUStats* gpuStats() const;
-#endif
+    /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
+    void dumpGpuStats(SkString*) const;
+    void printGpuStats() const;
 
 private:
     GrGpu*                          fGpu;
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;
     }
diff --git a/tools/bench_pictures_main.cpp b/tools/bench_pictures_main.cpp
index e8c4063..f919812 100644
--- a/tools/bench_pictures_main.cpp
+++ b/tools/bench_pictures_main.cpp
@@ -473,9 +473,7 @@
 
 #if GR_GPU_STATS && SK_SUPPORT_GPU
     if (FLAGS_gpuStats && benchmark.renderer()->isUsingGpuDevice()) {
-        GrContext* ctx = benchmark.renderer()->getGrContext();
-        SkDebugf("RenderTarget Binds: %d\n", ctx->gpuStats()->renderTargetBinds());
-        SkDebugf("Shader Compilations: %d\n", ctx->gpuStats()->shaderCompilations());
+        benchmark.renderer()->getGrContext()->printGpuStats();
     }
 #endif
 
diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp
index 91b6481..11c640f 100644
--- a/tools/render_pictures_main.cpp
+++ b/tools/render_pictures_main.cpp
@@ -486,9 +486,7 @@
 
 #if GR_GPU_STATS && SK_SUPPORT_GPU
     if (FLAGS_gpuStats && renderer->isUsingGpuDevice()) {
-        GrContext* ctx = renderer->getGrContext();
-        SkDebugf("RenderTarget Binds: %d\n", ctx->gpuStats()->renderTargetBinds());
-        SkDebugf("Shader Compilations: %d\n", ctx->gpuStats()->shaderCompilations());
+        renderer->getGrContext()->printGpuStats();
     }
 #endif