am 11ed894a: am f55719bc: am 7d3073a4: Merge "Crash instead of leaking layers/textures between GL contexts" into lmp-dev

* commit '11ed894a043a193baa5ec004e0cf376eab9f21eb':
  Crash instead of leaking layers/textures between GL contexts
diff --git a/libs/hwui/Layer.cpp b/libs/hwui/Layer.cpp
index 8639ae1..e6bc4db 100644
--- a/libs/hwui/Layer.cpp
+++ b/libs/hwui/Layer.cpp
@@ -54,9 +54,11 @@
     convexMask = NULL;
     caches.resourceCache.incrementRefcount(this);
     rendererLightPosDirty = true;
+    renderState.registerLayer(this);
 }
 
 Layer::~Layer() {
+    renderState.unregisterLayer(this);
     SkSafeUnref(colorFilter);
     removeFbo();
     deleteTexture();
diff --git a/libs/hwui/RenderState.cpp b/libs/hwui/RenderState.cpp
index 97f379d..50b8f39 100644
--- a/libs/hwui/RenderState.cpp
+++ b/libs/hwui/RenderState.cpp
@@ -34,6 +34,10 @@
     mCaches->init();
 }
 
+void RenderState::onGLContextDestroyed() {
+    LOG_ALWAYS_FATAL_IF(!mActiveLayers.empty(), "layers have survived gl context destruction");
+}
+
 void RenderState::setViewport(GLsizei width, GLsizei height) {
     mViewportWidth = width;
     mViewportHeight = height;
diff --git a/libs/hwui/RenderState.h b/libs/hwui/RenderState.h
index f7116e2..cd71c73 100644
--- a/libs/hwui/RenderState.h
+++ b/libs/hwui/RenderState.h
@@ -16,6 +16,7 @@
 #ifndef RENDERSTATE_H
 #define RENDERSTATE_H
 
+#include <set>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 
@@ -37,6 +38,7 @@
     PREVENT_COPY_AND_ASSIGN(RenderState);
 public:
     void onGLContextCreated();
+    void onGLContextDestroyed();
 
     void setViewport(GLsizei width, GLsizei height);
     void getViewport(GLsizei* outWidth, GLsizei* outHeight);
@@ -48,6 +50,13 @@
 
     void debugOverdraw(bool enable, bool clear);
 
+    void registerLayer(const Layer* layer) {
+        mActiveLayers.insert(layer);
+    }
+    void unregisterLayer(const Layer* layer) {
+        mActiveLayers.erase(layer);
+    }
+
 private:
     friend class renderthread::RenderThread;
 
@@ -58,6 +67,7 @@
     ~RenderState();
 
     Caches* mCaches;
+    std::set<const Layer*> mActiveLayers;
 
     GLsizei mViewportWidth;
     GLsizei mViewportHeight;
diff --git a/libs/hwui/renderthread/EglManager.cpp b/libs/hwui/renderthread/EglManager.cpp
index 05ca34d..37f8e60 100644
--- a/libs/hwui/renderthread/EglManager.cpp
+++ b/libs/hwui/renderthread/EglManager.cpp
@@ -214,6 +214,7 @@
         Caches::getInstance().terminate();
     }
 
+    mRenderThread.renderState().onGLContextDestroyed();
     eglDestroyContext(mEglDisplay, mEglContext);
     eglDestroySurface(mEglDisplay, mPBufferSurface);
     eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);