gpu_tonemapper: Clear EGLImage mappings in Tonemapper

Tonemapper does not clear the eglImage/fd mappings in the destructor,
which leads to incorrect usage of those fds, when a tone map session
gets deleted and a new session gets created, leading to artifcats.

CRs-Fixed: 1104823
Change-Id: I9697eff93f9e5f150796a582f471246bca3b2816
diff --git a/gpu_tonemapper/EGLImageWrapper.cpp b/gpu_tonemapper/EGLImageWrapper.cpp
index eb0a2ca..298475f 100644
--- a/gpu_tonemapper/EGLImageWrapper.cpp
+++ b/gpu_tonemapper/EGLImageWrapper.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -22,8 +22,6 @@
 #include <gralloc_priv.h>
 #include <ui/GraphicBuffer.h>
 
-std::map<int, EGLImageBuffer *> EGLImageWrapper::eglImageBufferMap;
-
 //-----------------------------------------------------------------------------
 EGLImageBuffer *EGLImageWrapper::wrap(const void *pvt_handle)
 //-----------------------------------------------------------------------------
@@ -65,4 +63,4 @@
     delete it->second;
   }
   eglImageBufferMap.clear();
-}
\ No newline at end of file
+}
diff --git a/gpu_tonemapper/EGLImageWrapper.h b/gpu_tonemapper/EGLImageWrapper.h
index d7fc84b..90aaf58 100644
--- a/gpu_tonemapper/EGLImageWrapper.h
+++ b/gpu_tonemapper/EGLImageWrapper.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -24,11 +24,11 @@
 #include "EGLImageBuffer.h"
 
 class EGLImageWrapper {
-  static std::map<int, EGLImageBuffer *> eglImageBufferMap;
+  std::map<int, EGLImageBuffer *> eglImageBufferMap;
 
  public:
-  static EGLImageBuffer *wrap(const void *pvt_handle);
-  static void destroy();
+  EGLImageBuffer *wrap(const void *pvt_handle);
+  void destroy();
 };
 
-#endif  //__TONEMAPPER_EGLIMAGEWRAPPER_H__
\ No newline at end of file
+#endif  //__TONEMAPPER_EGLIMAGEWRAPPER_H__
diff --git a/gpu_tonemapper/TonemapFactory.cpp b/gpu_tonemapper/TonemapFactory.cpp
index 81e9f7f..cffc33a 100644
--- a/gpu_tonemapper/TonemapFactory.cpp
+++ b/gpu_tonemapper/TonemapFactory.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -19,7 +19,6 @@
 
 #include "TonemapFactory.h"
 #include <utils/Log.h>
-#include "EGLImageWrapper.h"
 #include "Tonemapper.h"
 #include "engine.h"
 
@@ -41,8 +40,6 @@
 void TonemapperFactory_Destroy()
 //------------------------------------------
 {
-  // clear EGLImage mappings
-  EGLImageWrapper::destroy();
   // shutdown the engine
   engine_shutdown();
 }
diff --git a/gpu_tonemapper/Tonemapper.cpp b/gpu_tonemapper/Tonemapper.cpp
index 957c133..5f2f974 100644
--- a/gpu_tonemapper/Tonemapper.cpp
+++ b/gpu_tonemapper/Tonemapper.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -32,6 +32,7 @@
   tonemapTexture = 0;
   lutXformTexture = 0;
   programID = 0;
+  eglImageWrapper = new EGLImageWrapper();
 }
 
 //-----------------------------------------------------------------------------
@@ -41,6 +42,13 @@
   engine_deleteInputBuffer(tonemapTexture);
   engine_deleteInputBuffer(lutXformTexture);
   engine_deleteProgram(programID);
+
+  // clear EGLImage mappings
+  if (eglImageWrapper != 0) {
+    eglImageWrapper->destroy();
+    delete eglImageWrapper;
+    eglImageWrapper = 0;
+  }
 }
 
 //-----------------------------------------------------------------------------
@@ -95,8 +103,8 @@
   engine_bind();
 
   // create eglimages if required
-  EGLImageBuffer *dst_buffer = EGLImageWrapper::wrap(dst);
-  EGLImageBuffer *src_buffer = EGLImageWrapper::wrap(src);
+  EGLImageBuffer *dst_buffer = eglImageWrapper->wrap(dst);
+  EGLImageBuffer *src_buffer = eglImageWrapper->wrap(src);
 
   // bind the program
   engine_setProgram(programID);
diff --git a/gpu_tonemapper/Tonemapper.h b/gpu_tonemapper/Tonemapper.h
index 9c41670..0f9bd51 100644
--- a/gpu_tonemapper/Tonemapper.h
+++ b/gpu_tonemapper/Tonemapper.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -23,11 +23,14 @@
 #define TONEMAP_FORWARD 0
 #define TONEMAP_INVERSE 1
 
+#include "EGLImageWrapper.h"
+
 class Tonemapper {
  private:
   unsigned int tonemapTexture;
   unsigned int lutXformTexture;
   unsigned int programID;
+  EGLImageWrapper* eglImageWrapper;
   Tonemapper();
 
  public: