display: Store and restore the current eglContext

Tone mapper engine setting the current eglContext and never
restored it.  With multiple eglContexts used in same thread
by SurfaceFlinger and GPU Tonemapper, it lead to incorrect
eglContexts being used, hence always store the restore the
eglContext in each GPUTonemapper call.

CRs-Fixed: 2009259
Change-Id: Ic9fe73818ddfe3881e5fa82f7853dce130bba24e
diff --git a/gpu_tonemapper/Tonemapper.cpp b/gpu_tonemapper/Tonemapper.cpp
index 38b4fe2..c6646f6 100644
--- a/gpu_tonemapper/Tonemapper.cpp
+++ b/gpu_tonemapper/Tonemapper.cpp
@@ -45,6 +45,7 @@
 Tonemapper::~Tonemapper()
 //-----------------------------------------------------------------------------
 {
+  void* caller_context = engine_backup();
   engine_bind(engineContext);
   engine_deleteInputBuffer(tonemapTexture);
   engine_deleteInputBuffer(lutXformTexture);
@@ -57,6 +58,9 @@
   }
 
   engine_shutdown(engineContext);
+  // restore the caller context
+  engine_bind(caller_context);
+  engine_free_backup(caller_context);
 }
 
 //-----------------------------------------------------------------------------
@@ -74,6 +78,7 @@
 
   tonemapper->engineContext = engine_initialize();
 
+  void* caller_context = engine_backup();
   engine_bind(tonemapper->engineContext);
 
   // load the 3d lut
@@ -112,6 +117,10 @@
   tonemapper->programID =
       engine_loadProgram(1, &fullscreen_vertex_shader, fragmentShaderCount, fragmentShaders);
 
+  // restore the caller context
+  engine_bind(caller_context);
+  engine_free_backup(caller_context);
+
   return tonemapper;
 }
 
@@ -119,6 +128,7 @@
 int Tonemapper::blit(const void *dst, const void *src, int srcFenceFd)
 //-----------------------------------------------------------------------------
 {
+  void* caller_context = engine_backup();
   // make current
   engine_bind(engineContext);
 
@@ -149,5 +159,10 @@
   // perform
   int fenceFD = engine_blit(srcFenceFd);
 
+  // restore the caller context
+  engine_bind(caller_context);
+  engine_free_backup(caller_context);
+
+
   return fenceFD;
 }
diff --git a/gpu_tonemapper/engine.h b/gpu_tonemapper/engine.h
index c07f13e..84c7dc6 100644
--- a/gpu_tonemapper/engine.h
+++ b/gpu_tonemapper/engine.h
@@ -22,6 +22,8 @@
 
 void* engine_initialize();
 void engine_bind(void*);
+void* engine_backup();
+void engine_free_backup(void*);
 void engine_shutdown(void*);
 
 unsigned int engine_loadProgram(int, const char **, int, const char **);
diff --git a/gpu_tonemapper/glengine.cpp b/gpu_tonemapper/glengine.cpp
index 7a970c5..90fe502 100644
--- a/gpu_tonemapper/glengine.cpp
+++ b/gpu_tonemapper/glengine.cpp
@@ -47,6 +47,27 @@
 }
 
 //-----------------------------------------------------------------------------
+// store the current context(caller)
+void* engine_backup()
+{
+  EngineContext* callerContext = new EngineContext();
+  // store the previous display/context
+  callerContext->eglDisplay = eglGetCurrentDisplay();
+  callerContext->eglContext = eglGetCurrentContext();
+  callerContext->eglSurface = eglGetCurrentSurface(EGL_DRAW);
+
+  return (void*)callerContext;
+}
+//-----------------------------------------------------------------------------
+// frees the backed up caller context
+void engine_free_backup(void* context)
+{
+  EngineContext* callerContext = (EngineContext*)(context);
+
+  delete callerContext;
+}
+
+//-----------------------------------------------------------------------------
 // initialize GL
 //
 void* engine_initialize()