Free unused scratch space from idle Pools.

GrBlockAllocators will hang onto an extra scratch node unless you ask
them to reset. When a Pool is detached from its thread, we know that
we're done with whatever work we needed to do, so it's a good time to
reset any scratch space and reclaim that memory.

Change-Id: I0ff2c4fad9f51a2f3dc911730abad77c06af8d57
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/330276
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLPool.cpp b/src/sksl/SkSLPool.cpp
index 31f32ec..ee8eeb5 100644
--- a/src/sksl/SkSLPool.cpp
+++ b/src/sksl/SkSLPool.cpp
@@ -89,6 +89,10 @@
 
 void Pool::Recycle(std::unique_ptr<Pool> pool) {
     if (pool) {
+        if (get_thread_local_memory_pool() == pool->fMemPool.get()) {
+            SkDEBUGFAIL("SkSL pool is being recycled while it is still attached to the thread");
+        }
+
         pool->fMemPool->reportLeaks();
         SkASSERT(pool->fMemPool->isEmpty());
     }
@@ -109,8 +113,10 @@
 }
 
 void Pool::detachFromThread() {
-    VLOG("DETACH Pool:0x%016llX\n", (uint64_t)get_thread_local_memory_pool());
-    SkASSERT(get_thread_local_memory_pool() != nullptr);
+    MemoryPool* memPool = get_thread_local_memory_pool();
+    VLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool);
+    SkASSERT(memPool == fMemPool.get());
+    memPool->resetScratchSpace();
     set_thread_local_memory_pool(nullptr);
 }