Don't purge resources for trivial GrContext flushes
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2298003003

Review-Url: https://codereview.chromium.org/2298003003
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 5f4e663..6221fea 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -228,13 +228,15 @@
 void GrContext::flush(int flagsBitfield) {
     ASSERT_SINGLE_OWNER
     RETURN_IF_ABANDONED
-
+    bool flushed = false;
     if (kDiscard_FlushBit & flagsBitfield) {
         fDrawingManager->reset();
     } else {
-        fDrawingManager->flush();
+        flushed = fDrawingManager->flush();
     }
-    fResourceCache->notifyFlushOccurred();
+    if (flushed) {
+        fResourceCache->notifyFlushOccurred();
+    }
     fFlushToReduceCacheSize = false;
 }
 
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index d8b44b8..45ec9ac 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -221,9 +221,9 @@
     }
 }
 
-void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
+bool GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
     if (0 == fRecordedBatches.count()) {
-        return;
+        return false;
     }
     // Draw all the generated geometry.
     SkRandom random;
@@ -286,6 +286,7 @@
     }
 
     fGpu->finishDrawTarget();
+    return true;
 }
 
 void GrDrawTarget::reset() {
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 682dd2d..dfc1489 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -96,10 +96,11 @@
     void reset();
 
     /**
-     * Together these two functions flush all queued up draws to the Gpu.
+     * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
+     * of drawBatches() indicates whether any commands were actually issued to the GPU.
      */
     void prepareBatches(GrBatchFlushState* flushState);
-    void drawBatches(GrBatchFlushState* flushState);
+    bool drawBatches(GrBatchFlushState* flushState);
 
     /**
      * Gets the capabilities of the draw target.
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index df6bffd..6c75c0d 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -74,12 +74,12 @@
     fFlushState.reset();
 }
 
-void GrDrawingManager::flush() {
+bool GrDrawingManager::flush() {
     if (fFlushing || this->wasAbandoned()) {
-        return;
+        return false;
     }
     fFlushing = true;
-
+    bool flushed = false;
     SkDEBUGCODE(bool result =)
                         SkTTopoSort<GrDrawTarget, GrDrawTarget::TopoSortTraits>(&fDrawTargets);
     SkASSERT(result);
@@ -99,7 +99,9 @@
     fFlushState.preIssueDraws();
 
     for (int i = 0; i < fDrawTargets.count(); ++i) {
-        fDrawTargets[i]->drawBatches(&fFlushState);
+        if (fDrawTargets[i]->drawBatches(&fFlushState)) {
+            flushed = true;
+        }
     }
 
     SkASSERT(fFlushState.nextDrawToken() == fFlushState.nextTokenToFlush());
@@ -125,6 +127,7 @@
 
     fFlushState.reset();
     fFlushing = false;
+    return flushed;
 }
 
 GrDrawTarget* GrDrawingManager::newDrawTarget(GrRenderTarget* rt) {
diff --git a/src/gpu/GrDrawingManager.h b/src/gpu/GrDrawingManager.h
index 3e9f956..745820f 100644
--- a/src/gpu/GrDrawingManager.h
+++ b/src/gpu/GrDrawingManager.h
@@ -68,7 +68,8 @@
     void abandon();
     void cleanup();
     void reset();
-    void flush();
+    /** Returns true if there was anything to flush and false otherwise */
+    bool flush();
 
     friend class GrContext;  // for access to: ctor, abandon, reset & flush
 
diff --git a/tests/ResourceCacheTest.cpp b/tests/ResourceCacheTest.cpp
index 2af2d69..14705cb 100644
--- a/tests/ResourceCacheTest.cpp
+++ b/tests/ResourceCacheTest.cpp
@@ -1195,6 +1195,22 @@
     }
 
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
+
+    // Verify that calling flush() on a GrContext with nothing to do will not trigger resource
+    // eviction.
+    context->flush();
+    for (int i = 0; i < 10; ++i) {
+        TestResource* r = new TestResource(context->getGpu());
+        GrUniqueKey k;
+        make_unique_key<1>(&k, i);
+        r->resourcePriv().setUniqueKey(k);
+        r->unref();
+    }
+    REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
+    for (int i = 0; i < 10 * kFlushCount; ++i) {
+        context->flush();
+    }
+    REPORTER_ASSERT(reporter, 10 == cache->getResourceCount());
 }
 
 static void test_large_resource_count(skiatest::Reporter* reporter) {