Refcount RenderNode

Change-Id: I7a86db8acc2b78ef33d987a43a119f5933d7d752
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index cd711b0..43182d5 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -75,6 +75,9 @@
 
 void RenderProxy::destroyContext() {
     if (mContext) {
+        // Flush any pending changes to ensure all garbage is destroyed
+        mDrawFrameTask.flushStateChanges(&mRenderThread);
+
         SETUP_TASK(destroyContext);
         args->context = mContext;
         mContext = 0;
@@ -138,6 +141,10 @@
 }
 
 void RenderProxy::destroyCanvas() {
+    // If the canvas is being destroyed we won't be drawing again anytime soon
+    // So flush any pending state changes to allow for resource cleanup.
+    mDrawFrameTask.flushStateChanges(&mRenderThread);
+
     SETUP_TASK(destroyCanvas);
     args->context = mContext;
     post(task);