Implement CacheManager for the Skia pipelines.

The core of the implementation is complete and provides heuristic
cache sizing based on the size of the surface being used. This CL
will also be used to add the following features in the future...

1) Support Vulkan pipeline reporting on the size of the surface.
2) Complete the VectorDrawableAtlas stub code
3) Automatic purging of stale resources for low memory devices.

Test: hwui_unit_tests (new test added) and CtsUiRendering
Bug: 62260637
Change-Id: Ib85159cca28b646fe249f2190b07f1b7e0f50d8f
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 0554583..13af2c4 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -202,6 +202,45 @@
     mRenderState = new RenderState(*this);
     mJankTracker = new JankTracker(mDisplayInfo);
     mVkManager = new VulkanManager(*this);
+    mCacheManager = new CacheManager(mDisplayInfo);
+}
+
+void RenderThread::dumpGraphicsMemory(int fd) {
+    jankTracker().dump(fd);
+
+    String8 cachesOutput;
+    String8 pipeline;
+    auto renderType = Properties::getRenderPipelineType();
+    switch (renderType) {
+        case RenderPipelineType::OpenGL: {
+            if (Caches::hasInstance()) {
+                cachesOutput.appendFormat("Caches:\n");
+                Caches::getInstance().dumpMemoryUsage(cachesOutput);
+            } else {
+                cachesOutput.appendFormat("No caches instance.");
+            }
+            pipeline.appendFormat("FrameBuilder");
+            break;
+        }
+        case RenderPipelineType::SkiaGL: {
+            mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
+            pipeline.appendFormat("Skia (OpenGL)");
+            break;
+        }
+        case RenderPipelineType::SkiaVulkan: {
+            mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
+            pipeline.appendFormat("Skia (Vulkan)");
+            break;
+        }
+        default:
+            LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
+            break;
+    }
+
+    FILE *file = fdopen(fd, "a");
+    fprintf(file, "\n%s\n", cachesOutput.string());
+    fprintf(file, "\nPipeline=%s\n", pipeline.string());
+    fflush(file);
 }
 
 Readback& RenderThread::readback() {
@@ -228,6 +267,14 @@
     return *mReadback;
 }
 
+void RenderThread::setGrContext(GrContext* context) {
+    mCacheManager->reset(context);
+    if (mGrContext.get()) {
+        mGrContext->releaseResourcesAndAbandonContext();
+    }
+    mGrContext.reset(context);
+}
+
 int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
     if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
         ALOGE("Display event receiver pipe was closed or an error occurred.  "