Fix onTrimMemory for HardwareRenderer

 Also fixes detachFunctor possibly drawing after return

 Bug: 15189843
 Bug: 15990672

Change-Id: I64c48cb674c461a8eeaba407b697e09f72c98ce3
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index e1ee620..6cd3d0b 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -312,18 +312,30 @@
     postAndWait(task);
 }
 
-CREATE_BRIDGE2(flushCaches, CanvasContext* context, Caches::FlushMode flushMode) {
-    args->context->flushCaches(args->flushMode);
+CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
+    args->context->destroyHardwareResources();
     return NULL;
 }
 
-void RenderProxy::flushCaches(Caches::FlushMode flushMode) {
-    SETUP_TASK(flushCaches);
+void RenderProxy::destroyHardwareResources() {
+    SETUP_TASK(destroyHardwareResources);
     args->context = mContext;
-    args->flushMode = flushMode;
     post(task);
 }
 
+CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) {
+    CanvasContext::trimMemory(*args->thread, args->level);
+    return NULL;
+}
+
+void RenderProxy::trimMemory(int level) {
+    RenderThread& thread = RenderThread::getInstance();
+    SETUP_TASK(timMemory);
+    args->thread = &thread;
+    args->level = level;
+    thread.queue(task);
+}
+
 CREATE_BRIDGE0(fence) {
     // Intentionally empty
     return NULL;
@@ -334,6 +346,17 @@
     postAndWait(task);
 }
 
+CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
+    args->context->stopDrawing();
+    return NULL;
+}
+
+void RenderProxy::stopDrawing() {
+    SETUP_TASK(stopDrawing);
+    args->context = mContext;
+    postAndWait(task);
+}
+
 CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
     args->context->notifyFramePending();
     return NULL;