Remove GrContext from GrDrawTarget

Still exists on clip target (for now).

Review URL: https://codereview.chromium.org/1166553002
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index 5adc68a..71b5674 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -11,12 +11,13 @@
 #include "GrAARectRenderer.h"
 #include "GrBatch.h"
 #include "GrCaps.h"
-#include "GrContext.h"
+#include "GrGpu.h"
 #include "GrPath.h"
 #include "GrPipeline.h"
 #include "GrMemoryPool.h"
 #include "GrRectBatch.h"
 #include "GrRenderTarget.h"
+#include "GrResourceProvider.h"
 #include "GrRenderTargetPriv.h"
 #include "GrSurfacePriv.h"
 #include "GrTemplates.h"
@@ -27,15 +28,17 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#define DEBUG_INVAL_BUFFER 0xdeadcafe
-#define DEBUG_INVAL_START_IDX -1
-
-GrDrawTarget::GrDrawTarget(GrContext* context)
-    : fContext(context)
-    , fCaps(SkRef(context->caps()))
+GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider)
+    : fGpu(SkRef(gpu))
+    , fCaps(SkRef(gpu->caps()))
+    , fResourceProvider(resourceProvider)
     , fGpuTraceMarkerCount(0)
     , fFlushing(false) {
-    SkASSERT(context);
+}
+
+GrDrawTarget::~GrDrawTarget() {
+    fGpu->unref();
+    fCaps->unref();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -93,8 +96,8 @@
     desc.fWidth = copyRect.width();
     desc.fHeight = copyRect.height();
 
-    SkAutoTUnref<GrTexture> copy(fContext->textureProvider()->refScratchTexture(desc,
-        GrTextureProvider::kApprox_ScratchTexMatch));
+    SkAutoTUnref<GrTexture> copy(
+        fResourceProvider->refScratchTexture(desc, GrTextureProvider::kApprox_ScratchTexMatch));
 
     if (!copy) {
         SkDebugf("Failed to create temporary copy of destination texture.\n");
@@ -540,7 +543,9 @@
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-GrClipTarget::GrClipTarget(GrContext* context) : INHERITED(context) {
+GrClipTarget::GrClipTarget(GrContext* context)
+    : INHERITED(context->getGpu(), context->resourceProvider())
+    , fContext(context) {
     fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
 }
 
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 8b9c0ee..a740202 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -46,9 +46,9 @@
 
     // The context may not be fully constructed and should not be used during GrDrawTarget
     // construction.
-    GrDrawTarget(GrContext* context);
+    GrDrawTarget(GrGpu* gpu, GrResourceProvider*);
 
-    virtual ~GrDrawTarget() {}
+    virtual ~GrDrawTarget();
 
     /**
      * Empties the draw buffer of any queued up draws.
@@ -64,7 +64,7 @@
     /**
      * Gets the capabilities of the draw target.
      */
-    const GrCaps* caps() const { return fCaps.get(); }
+    const GrCaps* caps() const { return fCaps; }
 
     void drawBatch(GrPipelineBuilder*, GrBatch*);
 
@@ -208,7 +208,7 @@
      */
     virtual void purgeResources() {};
 
-    bool programUnitTest(int maxStages);
+    bool programUnitTest(GrContext* owner, int maxStages);
 
 protected:
     friend class GrCommandBuilder; // for PipelineInfo
@@ -216,17 +216,8 @@
     friend class GrReorderCommandBuilder; // for PipelineInfo
     friend class GrTargetCommands; // for PipelineInfo
 
-    GrContext* getContext() { return fContext; }
-    const GrContext* getContext() const { return fContext; }
-
-    GrGpu* getGpu() {
-        SkASSERT(fContext && fContext->getGpu());
-        return fContext->getGpu();
-    }
-    const GrGpu* getGpu() const {
-        SkASSERT(fContext && fContext->getGpu());
-        return fContext->getGpu();
-    }
+    GrGpu* getGpu() { return fGpu; }
+    const GrGpu* getGpu() const { return fGpu; }
 
     const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
 
@@ -312,14 +303,14 @@
                            GrScissorState*,
                            const SkRect* devBounds) = 0;
 
-    // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget.
-    GrContext*                                                      fContext;
-    SkAutoTUnref<const GrCaps>                            fCaps;
+    GrGpu*                  fGpu;
+    const GrCaps*           fCaps;
+    GrResourceProvider*     fResourceProvider;
     // To keep track that we always have at least as many debug marker adds as removes
-    int                                                             fGpuTraceMarkerCount;
-    GrTraceMarkerSet                                                fActiveTraceMarkers;
-    GrTraceMarkerSet                                                fStoredTraceMarkers;
-    bool                                                            fFlushing;
+    int                     fGpuTraceMarkerCount;
+    GrTraceMarkerSet        fActiveTraceMarkers;
+    GrTraceMarkerSet        fStoredTraceMarkers;
+    bool                    fFlushing;
 
     typedef SkRefCnt INHERITED;
 };
@@ -334,8 +325,8 @@
     /* Clip mask manager needs access to the context.
      * TODO we only need a very small subset of context in the CMM.
      */
-    GrContext* getContext() { return INHERITED::getContext(); }
-    const GrContext* getContext() const { return INHERITED::getContext(); }
+    GrContext* getContext() { return fContext; }
+    const GrContext* getContext() const { return fContext; }
 
     /**
      * Clip Mask Manager(and no one else) needs to clear private stencil bits.
@@ -353,6 +344,7 @@
 
 protected:
     SkAutoTDelete<GrClipMaskManager> fClipMaskManager;
+    GrContext*                       fContext;
 
 private:
     GrClipMaskManager* clipMaskManager() override { return fClipMaskManager; }
diff --git a/tests/GLProgramsTest.cpp b/tests/GLProgramsTest.cpp
index ca801f9..d329124 100644
--- a/tests/GLProgramsTest.cpp
+++ b/tests/GLProgramsTest.cpp
@@ -18,6 +18,7 @@
 #include "GrContextFactory.h"
 #include "GrInvariantOutput.h"
 #include "GrPipeline.h"
+#include "GrResourceProvider.h"
 #include "GrTest.h"
 #include "GrXferProcessor.h"
 #include "SkChecksum.h"
@@ -105,7 +106,7 @@
 static const int kRenderTargetHeight = 1;
 static const int kRenderTargetWidth = 1;
 
-static GrRenderTarget* random_render_target(GrContext* context, SkRandom* random,
+static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
                                             const GrCaps* caps) {
     // setup render target
     GrTextureParams params;
@@ -125,11 +126,11 @@
     builder[1] = texDesc.fSampleCnt;
     builder.finish();
 
-    GrTexture* texture = context->textureProvider()->findAndRefTextureByUniqueKey(key);
+    GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
     if (!texture) {
-        texture = context->textureProvider()->createTexture(texDesc, true);
+        texture = textureProvider->createTexture(texDesc, true);
         if (texture) {
-            context->textureProvider()->assignUniqueKeyToTexture(key, texture);
+            textureProvider->assignUniqueKeyToTexture(key, texture);
         }
     }
     return texture ? texture->asRenderTarget() : NULL;
@@ -144,7 +145,7 @@
     pipelineBuilder->setXPFactory(xpf.get());
 }
 
-static void set_random_color_coverage_stages(GrGLGpu* gpu,
+static void set_random_color_coverage_stages(GrGpu* gpu,
                                              GrPipelineBuilder* pipelineBuilder,
                                              int maxStages,
                                              SkRandom* random,
@@ -208,20 +209,21 @@
     }
 }
 
-bool GrDrawTarget::programUnitTest(int maxStages) {
-    GrGLGpu* gpu = static_cast<GrGLGpu*>(fContext->getGpu());
+bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) {
     // setup dummy textures
     GrSurfaceDesc dummyDesc;
     dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
     dummyDesc.fConfig = kSkia8888_GrPixelConfig;
     dummyDesc.fWidth = 34;
     dummyDesc.fHeight = 18;
-    SkAutoTUnref<GrTexture> dummyTexture1(gpu->createTexture(dummyDesc, false, NULL, 0));
+    SkAutoTUnref<GrTexture> dummyTexture1(
+        fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
     dummyDesc.fFlags = kNone_GrSurfaceFlags;
     dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
     dummyDesc.fWidth = 16;
     dummyDesc.fHeight = 22;
-    SkAutoTUnref<GrTexture> dummyTexture2(gpu->createTexture(dummyDesc, false, NULL, 0));
+    SkAutoTUnref<GrTexture> dummyTexture2(
+        fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
 
     if (!dummyTexture1 || ! dummyTexture2) {
         SkDebugf("Could not allocate dummy textures");
@@ -240,7 +242,8 @@
     static const int NUM_TESTS = 2048;
     for (int t = 0; t < NUM_TESTS; t++) {
         // setup random render target(can fail)
-        SkAutoTUnref<GrRenderTarget> rt(random_render_target(fContext, &random, this->caps()));
+        SkAutoTUnref<GrRenderTarget> rt(random_render_target(
+            fResourceProvider, &random, this->caps()));
         if (!rt.get()) {
             SkDebugf("Could not allocate render target");
             return false;
@@ -250,17 +253,17 @@
         pipelineBuilder.setRenderTarget(rt.get());
         pipelineBuilder.setClip(clip);
 
-        SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, fContext));
+        SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, context));
         SkASSERT(batch);
 
-        set_random_color_coverage_stages(gpu,
+        set_random_color_coverage_stages(fGpu,
                                          &pipelineBuilder,
                                          maxStages,
                                          &random,
                                          dummyTextures);
 
         // creates a random xfer processor factory on the draw state 
-        set_random_xpf(fContext, gpu->glCaps(), &pipelineBuilder, &random, dummyTextures);
+        set_random_xpf(context, *fGpu->caps(), &pipelineBuilder, &random, dummyTextures);
 
         set_random_state(&pipelineBuilder, &random);
         set_random_stencil(&pipelineBuilder, &random);
@@ -314,7 +317,7 @@
 #endif
             GrTestTarget target;
             context->getTestTarget(&target);
-            REPORTER_ASSERT(reporter, target.target()->programUnitTest(maxStages));
+            REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages));
         }
     }
 }