Fix issue with bitmap uploading

 Bug: 13912749

Change-Id: Ic23fa1d280118dc93dc2716a4a24cc0bbbdca595
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index 3638184..a8980af 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -382,13 +382,18 @@
     mCanvas->setViewport(width, height);
 }
 
-void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters,
-        bool* hasFunctors) {
-    LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot process layer updates without a canvas!");
+void CanvasContext::makeCurrent() {
     mGlobalContext->makeCurrent(mEglSurface);
+}
+
+void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters,
+        TreeInfo& info) {
+    LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot process layer updates without a canvas!");
+    makeCurrent();
     for (size_t i = 0; i < layerUpdaters->size(); i++) {
         DeferredLayerUpdater* update = layerUpdaters->itemAt(i);
-        LOG_ALWAYS_FATAL_IF(!update->apply(hasFunctors), "Failed to update layer!");
+        bool success = update->apply(info);
+        LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
         if (update->backingLayer()->deferredUpdateScheduled) {
             mCanvas->pushLayerUpdate(update->backingLayer());
         }
@@ -444,8 +449,8 @@
 
 bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
     requireGlContext();
-    bool hasFunctors;
-    layer->apply(&hasFunctors);
+    TreeInfo info;
+    layer->apply(info);
     return LayerRenderer::copyLayer(layer->backingLayer(), bitmap);
 }
 
diff --git a/libs/hwui/renderthread/CanvasContext.h b/libs/hwui/renderthread/CanvasContext.h
index dcb5957..bbf1b9a 100644
--- a/libs/hwui/renderthread/CanvasContext.h
+++ b/libs/hwui/renderthread/CanvasContext.h
@@ -23,6 +23,7 @@
 #include <utils/Functor.h>
 #include <utils/Vector.h>
 
+#include "../RenderNode.h"
 #include "RenderTask.h"
 
 #define FUNCTOR_PROCESS_DELAY 4
@@ -31,8 +32,6 @@
 namespace uirenderer {
 
 class DeferredLayerUpdater;
-class RenderNode;
-class DisplayListData;
 class OpenGLRenderer;
 class Rect;
 class Layer;
@@ -54,7 +53,8 @@
     void updateSurface(EGLNativeWindowType window);
     void pauseSurface(EGLNativeWindowType window);
     void setup(int width, int height);
-    void processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters, bool* hasFunctors);
+    void makeCurrent();
+    void processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters, TreeInfo& info);
     void drawDisplayList(RenderNode* displayList, Rect* dirty);
     void destroyCanvas();
 
diff --git a/libs/hwui/renderthread/DrawFrameTask.cpp b/libs/hwui/renderthread/DrawFrameTask.cpp
index cf6c8db..f542d43 100644
--- a/libs/hwui/renderthread/DrawFrameTask.cpp
+++ b/libs/hwui/renderthread/DrawFrameTask.cpp
@@ -85,7 +85,6 @@
 void DrawFrameTask::run() {
     ATRACE_NAME("DrawFrame");
 
-    // canUnblockUiThread is temporary until WebView has a solution for syncing frame state
     bool canUnblockUiThread = syncFrameState();
 
     // Grab a copy of everything we need
@@ -105,17 +104,20 @@
     }
 }
 
+static void prepareTreeInfo(TreeInfo& info) {
+    info.prepareTextures = true;
+}
+
 bool DrawFrameTask::syncFrameState() {
     ATRACE_CALL();
-
-    bool hasFunctors = false;
-    mContext->processLayerUpdates(&mLayers, &hasFunctors);
-
-    TreeInfo info = {0};
+    mContext->makeCurrent();
+    Caches::getInstance().textureCache.resetMarkInUse();
+    TreeInfo info;
+    prepareTreeInfo(info);
+    mContext->processLayerUpdates(&mLayers, info);
     mRenderNode->prepareTree(info);
-    hasFunctors |= info.hasFunctors;
-
-    return !hasFunctors;
+    // If prepareTextures is false, we ran out of texture cache space
+    return !info.hasFunctors && info.prepareTextures;
 }
 
 void DrawFrameTask::unblockUiThread() {