Revert "Revert "Make mock GrContext unit testable.""

This reverts commit c867a89b012c07e7e5cb719a31ed90e61f4a4901.

Reason for revert: test

Original change's description:
> Revert "Make mock GrContext unit testable."
> 
> This reverts commit 993e7e25217df05d63c3354c817e8bd18ea3738b.
> 
> Reason for revert: Seeing if this fixes the NexusPlayer bots
> 
> Original change's description:
> > Make mock GrContext unit testable.
> > 
> > Bug: skia:
> > Change-Id: I959122f1f2c390832ab1033bcdbdd2ca6cfc0419
> > Reviewed-on: https://skia-review.googlesource.com/20699
> > Reviewed-by: Greg Daniel <egdaniel@google.com>
> > Commit-Queue: Brian Salomon <bsalomon@google.com>
> 
> TBR=egdaniel@google.com,bsalomon@google.com
> 
> Change-Id: I25ed9329962d930fe38108f779ff7083e0e4847e
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:
> Reviewed-on: https://skia-review.googlesource.com/21731
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>

TBR=egdaniel@google.com,bsalomon@google.com

Change-Id: I62c579e087db1ff9891cf6c41b3eb40f47561887
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:
Reviewed-on: https://skia-review.googlesource.com/21733
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/mock/GrMockBuffer.h b/src/gpu/mock/GrMockBuffer.h
new file mode 100644
index 0000000..fdb812d
--- /dev/null
+++ b/src/gpu/mock/GrMockBuffer.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrMockBuffer_DEFINED
+#define GrMockBuffer_DEFINED
+
+#include "GrBuffer.h"
+#include "GrMockGpu.h"
+
+class GrMockBuffer : public GrBuffer {
+public:
+    GrMockBuffer(GrMockGpu* gpu, size_t sizeInBytes, GrBufferType type,
+                 GrAccessPattern accessPattern)
+            : INHERITED(gpu, sizeInBytes, type, accessPattern) {
+        this->registerWithCache(SkBudgeted::kYes);
+    }
+
+private:
+    void onMap() override {}
+    void onUnmap() override {}
+    bool onUpdateData(const void* src, size_t srcSizeInBytes) override { return true; }
+
+    typedef GrBuffer INHERITED;
+};
+
+#endif
diff --git a/src/gpu/mock/GrMockCaps.h b/src/gpu/mock/GrMockCaps.h
new file mode 100644
index 0000000..7190ad7
--- /dev/null
+++ b/src/gpu/mock/GrMockCaps.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrMockCaps_DEFINED
+#define GrMockCaps_DEFINED
+
+#include "GrCaps.h"
+#include "mock/GrMockOptions.h"
+
+class GrMockCaps : public GrCaps {
+public:
+    GrMockCaps(const GrContextOptions& contextOptions, const GrMockOptions& options)
+            : INHERITED(contextOptions), fOptions(options) {
+        fBufferMapThreshold = SK_MaxS32;
+        fMaxTextureSize = options.fMaxTextureSize;
+        fMaxRenderTargetSize = SkTMin(options.fMaxRenderTargetSize, fMaxTextureSize);
+        fMaxVertexAttributes = options.fMaxVertexAttributes;
+        fShaderCaps.reset(new GrShaderCaps(contextOptions));
+    }
+    bool isConfigTexturable(GrPixelConfig config) const override {
+        return fOptions.fConfigOptions[config].fTexturable;
+    }
+    bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const override {
+        return fOptions.fConfigOptions[config].fRenderable[withMSAA];
+    }
+    bool canConfigBeImageStorage(GrPixelConfig) const override { return false; }
+    bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
+                            bool* rectsMustMatch, bool* disallowSubrect) const override {
+        return false;
+    }
+
+private:
+    GrMockOptions fOptions;
+    typedef GrCaps INHERITED;
+};
+
+#endif
diff --git a/src/gpu/mock/GrMockGpu.cpp b/src/gpu/mock/GrMockGpu.cpp
new file mode 100644
index 0000000..70568e6
--- /dev/null
+++ b/src/gpu/mock/GrMockGpu.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrMockGpu.h"
+#include "GrMockBuffer.h"
+#include "GrMockCaps.h"
+#include "GrMockGpuCommandBuffer.h"
+#include "GrMockStencilAttachment.h"
+#include "GrMockTexture.h"
+
+GrGpu* GrMockGpu::Create(GrBackendContext backendContext, const GrContextOptions& contextOptions,
+                         GrContext* context) {
+    static const GrMockOptions kDefaultOptions = GrMockOptions();
+    const GrMockOptions* options = reinterpret_cast<const GrMockOptions*>(backendContext);
+    if (!options) {
+        options = &kDefaultOptions;
+    }
+    return new GrMockGpu(context, *options, contextOptions);
+}
+
+GrGpuCommandBuffer* GrMockGpu::createCommandBuffer(const GrGpuCommandBuffer::LoadAndStoreInfo&,
+                                                   const GrGpuCommandBuffer::LoadAndStoreInfo&) {
+    return new GrMockGpuCommandBuffer(this);
+}
+
+void GrMockGpu::submitCommandBuffer(const GrMockGpuCommandBuffer* cmdBuffer) {
+    for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
+        fStats.incNumDraws();
+    }
+}
+
+GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
+                     const GrContextOptions& contextOptions)
+        : INHERITED(context) {
+    fCaps.reset(new GrMockCaps(contextOptions, options));
+}
+
+sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
+                                            const SkTArray<GrMipLevel>& texels) {
+    bool hasMipLevels = texels.count() > 1;
+    if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
+        return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc, hasMipLevels));
+    }
+    return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, hasMipLevels));
+}
+
+GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
+                                    GrAccessPattern accessPattern, const void*) {
+    return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
+}
+
+GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
+                                                                       int width,
+                                                                       int height) {
+    static constexpr int kBits = 8;
+    fStats.incStencilAttachmentCreates();
+    return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples());
+}
diff --git a/src/gpu/mock/GrMockGpu.h b/src/gpu/mock/GrMockGpu.h
index e5d97ce..d54158b 100644
--- a/src/gpu/mock/GrMockGpu.h
+++ b/src/gpu/mock/GrMockGpu.h
@@ -4,55 +4,36 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
+
 #ifndef GrMockGpu_DEFINED
 #define GrMockGpu_DEFINED
 
-#include "GrCaps.h"
 #include "GrGpu.h"
 #include "GrSemaphore.h"
 #include "GrTexture.h"
 
+class GrMockGpuCommandBuffer;
+struct GrMockOptions;
 class GrPipeline;
 
-class GrMockCaps : public GrCaps {
-public:
-    explicit GrMockCaps(const GrContextOptions& options) : INHERITED(options) {
-        fBufferMapThreshold = SK_MaxS32;
-    }
-    bool isConfigTexturable(GrPixelConfig) const override { return false; }
-    bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const override { return false; }
-    bool canConfigBeImageStorage(GrPixelConfig) const override { return false; }
-    bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
-                            bool* rectsMustMatch, bool* disallowSubrect) const override {
-        return false;
-    }
-
-private:
-    typedef GrCaps INHERITED;
-};
-
 class GrMockGpu : public GrGpu {
 public:
-    static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options,
-                         GrContext* context) {
-        SkASSERT((void*)backendContext == nullptr);
-        return new GrMockGpu(context, options);
-    }
+    static GrGpu* Create(GrBackendContext, const GrContextOptions&, GrContext*);
 
     ~GrMockGpu() override {}
 
     bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
                              GrPixelConfig readConfig, DrawPreference*,
-                             ReadPixelTempDrawInfo*) override { return false; }
+                             ReadPixelTempDrawInfo*) override { return true; }
 
     bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
                               GrPixelConfig srcConfig, DrawPreference*,
-                              WritePixelTempDrawInfo*) override { return false; }
+                              WritePixelTempDrawInfo*) override { return true; }
 
     bool onCopySurface(GrSurface* dst,
                        GrSurface* src,
                        const SkIRect& srcRect,
-                       const SkIPoint& dstPoint) override { return false; }
+                       const SkIPoint& dstPoint) override { return true; }
 
     void onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&,
                                  int* effectiveSampleCnt, SamplePattern*) override {
@@ -60,9 +41,7 @@
     }
 
     GrGpuCommandBuffer* createCommandBuffer(const GrGpuCommandBuffer::LoadAndStoreInfo&,
-                                            const GrGpuCommandBuffer::LoadAndStoreInfo&) override {
-        return nullptr;
-    }
+                                            const GrGpuCommandBuffer::LoadAndStoreInfo&) override;
 
     GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; }
     bool waitFence(GrFence, uint64_t) override { return true; }
@@ -77,19 +56,17 @@
     void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {}
     sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
 
+    void submitCommandBuffer(const GrMockGpuCommandBuffer*);
+
 private:
-    GrMockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
-        fCaps.reset(new GrMockCaps(options));
-    }
+    GrMockGpu(GrContext* context, const GrMockOptions&, const GrContextOptions&);
 
     void onResetContext(uint32_t resetBits) override {}
 
     void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
 
-    sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
-                                     const SkTArray<GrMipLevel>& texels) override {
-        return nullptr;
-    }
+    sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, SkBudgeted,
+                                     const SkTArray<GrMipLevel>&) override;
 
     sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
                                           GrSurfaceOrigin,
@@ -110,9 +87,8 @@
         return nullptr;
     }
 
-    GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override {
-        return nullptr;
-    }
+    GrBuffer* onCreateBuffer(size_t sizeInBytes, GrBufferType, GrAccessPattern,
+                             const void*) override;
 
     gr_instanced::InstancedRendering* onCreateInstancedRendering() override { return nullptr; }
 
@@ -121,30 +97,27 @@
                       GrPixelConfig,
                       void* buffer,
                       size_t rowBytes) override {
-        return false;
+        return true;
     }
 
     bool onWritePixels(GrSurface* surface,
                        int left, int top, int width, int height,
                        GrPixelConfig config, const SkTArray<GrMipLevel>& texels) override {
-        return false;
+        return true;
     }
 
     bool onTransferPixels(GrTexture* texture,
                           int left, int top, int width, int height,
                           GrPixelConfig config, GrBuffer* transferBuffer,
                           size_t offset, size_t rowBytes) override {
-        return false;
+        return true;
     }
 
     void onResolveRenderTarget(GrRenderTarget* target) override { return; }
 
     GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
                                                                 int width,
-                                                                int height) override {
-        return nullptr;
-    }
-
+                                                                int height) override;
     void clearStencil(GrRenderTarget* target) override  {}
 
     GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
@@ -156,4 +129,5 @@
 
     typedef GrGpu INHERITED;
 };
-#endif  // GrMockGpu_DEFINED
+
+#endif
diff --git a/src/gpu/mock/GrMockGpuCommandBuffer.h b/src/gpu/mock/GrMockGpuCommandBuffer.h
new file mode 100644
index 0000000..c5719a5
--- /dev/null
+++ b/src/gpu/mock/GrMockGpuCommandBuffer.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrMockGpuCommandBuffer_DEFINED
+#define GrMockGpuCommandBuffer_DEFINED
+
+#include "GrGpuCommandBuffer.h"
+#include "GrMockGpu.h"
+
+class GrMockGpuCommandBuffer : public GrGpuCommandBuffer {
+public:
+    GrMockGpuCommandBuffer(GrMockGpu* gpu) : fGpu(gpu) {}
+
+    GrGpu* gpu() override { return fGpu; }
+    void inlineUpload(GrOpFlushState*, GrDrawOp::DeferredUploadFn&, GrRenderTarget*) override {}
+    void discard(GrRenderTarget*) override {}
+    void end() override {}
+
+    int numDraws() const { return fNumDraws; }
+
+private:
+    void onSubmit() override { fGpu->submitCommandBuffer(this); }
+    void onDraw(const GrPipeline&, const GrPrimitiveProcessor&, const GrMesh[],
+                const GrPipeline::DynamicState[], int meshCount, const SkRect& bounds) override {
+        ++fNumDraws;
+    }
+    void onClear(GrRenderTarget*, const GrFixedClip&, GrColor) override {}
+    void onClearStencilClip(GrRenderTarget*, const GrFixedClip&, bool insideStencilMask) override {}
+    GrRenderTarget* renderTarget() override { return nullptr; }
+
+    GrMockGpu* fGpu;
+    int fNumDraws = 0;
+
+    typedef GrGpuCommandBuffer INHERITED;
+};
+
+#endif
diff --git a/src/gpu/mock/GrMockStencilAttachment.h b/src/gpu/mock/GrMockStencilAttachment.h
new file mode 100644
index 0000000..697e444
--- /dev/null
+++ b/src/gpu/mock/GrMockStencilAttachment.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrMockStencilAttachment_DEFINED
+#define GrMockStencilAttachment_DEFINED
+
+#include "GrMockGpu.h"
+#include "GrStencilAttachment.h"
+
+class GrMockStencilAttachment : public GrStencilAttachment {
+public:
+    GrMockStencilAttachment(GrMockGpu* gpu, int width, int height, int bits, int sampleCnt)
+            : INHERITED(gpu, width, height, bits, sampleCnt) {
+        this->registerWithCache(SkBudgeted::kYes);
+    }
+
+private:
+    size_t onGpuMemorySize() const override {
+        return SkTMax(1, (int)(this->bits() / sizeof(char))) * this->width() * this->height();
+    }
+
+    typedef GrStencilAttachment INHERITED;
+};
+
+#endif
diff --git a/src/gpu/mock/GrMockTexture.h b/src/gpu/mock/GrMockTexture.h
new file mode 100644
index 0000000..41474c2
--- /dev/null
+++ b/src/gpu/mock/GrMockTexture.h
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#ifndef GrMockTexture_DEFINED
+#define GrMockTexture_DEFINED
+
+#include "GrMockGpu.h"
+#include "GrTexture.h"
+#include "GrTexturePriv.h"
+
+class GrMockTexture : public GrTexture {
+public:
+    GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, bool hasMipLevels)
+            : GrMockTexture(gpu, desc, hasMipLevels) {
+        this->registerWithCache(budgeted);
+    }
+    ~GrMockTexture() override {
+        if (fReleaseProc) {
+            fReleaseProc(fReleaseCtx);
+        }
+    }
+    GrBackendObject getTextureHandle() const override { return 0; }
+    void textureParamsModified() override {}
+    void setRelease(ReleaseProc proc, ReleaseCtx ctx) override {
+        fReleaseProc = proc;
+        fReleaseCtx = ctx;
+    }
+
+protected:
+    // constructor for subclasses
+    GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, bool hasMipLevels)
+            : GrSurface(gpu, desc)
+            , INHERITED(gpu, desc, kITexture2DSampler_GrSLType, GrSamplerParams::kMipMap_FilterMode,
+                        hasMipLevels)
+            , fReleaseProc(nullptr)
+            , fReleaseCtx(nullptr) {}
+
+private:
+    ReleaseProc fReleaseProc;
+    ReleaseCtx fReleaseCtx;
+
+    typedef GrTexture INHERITED;
+};
+
+class GrMockTextureRenderTarget : public GrMockTexture, public GrRenderTarget {
+public:
+    GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
+                              bool hasMipLevels)
+            : GrSurface(gpu, desc)
+            , GrMockTexture(gpu, desc, hasMipLevels)
+            , GrRenderTarget(gpu, desc) {
+        this->registerWithCache(budgeted);
+    }
+    ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
+    GrBackendObject getRenderTargetHandle() const override { return 0; }
+    bool canAttemptStencilAttachment() const override { return true; }
+    bool completeStencilAttachment() override { return true; }
+    GrTexture* asTexture() override { return this; }
+    GrRenderTarget* asRenderTarget() override { return this; }
+    const GrTexture* asTexture() const override { return this; }
+    const GrRenderTarget* asRenderTarget() const override { return this; }
+
+private:
+    void onAbandon() override {
+        GrRenderTarget::onAbandon();
+        GrMockTexture::onAbandon();
+    }
+
+    void onRelease() override {
+        GrRenderTarget::onRelease();
+        GrMockTexture::onRelease();
+    }
+
+    size_t onGpuMemorySize() const override {
+        return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
+                                      this->numStencilSamples(),
+                                      this->texturePriv().hasMipMaps());
+    }
+
+    void computeScratchKey(GrScratchKey* key) const override {
+        GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
+                                         this->origin(), true, this->numStencilSamples(),
+                                         this->texturePriv().hasMipMaps(), key);
+    }
+};
+
+#endif