Create client object EGL images.

Call the host-side eglCreateImageKHR() implementation to create EGL
images from client objects (e.g. textures).

Bug 24517776

Change-Id: If7df1c8716aa45f8de49d667790d923945ee91df
diff --git a/opengl/system/egl/egl.cpp b/opengl/system/egl/egl.cpp
index 476a9ed..52c39a2 100644
--- a/opengl/system/egl/egl.cpp
+++ b/opengl/system/egl/egl.cpp
@@ -1225,14 +1225,18 @@
         return (EGLImageKHR)image;
     }
     else if (target == EGL_GL_TEXTURE_2D_KHR) {
-        setErrorReturn(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);   // TODO
-
         VALIDATE_CONTEXT_RETURN(ctx, EGL_NO_IMAGE_KHR);
 
+        EGLContext_t *context = static_cast<EGLContext_t*>(ctx);
+        DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_IMAGE_KHR);
+
+        uint32_t ctxHandle = (context) ? context->rcContext : 0;
+        GLuint texture = (GLuint)reinterpret_cast<uintptr_t>(buffer);
+        uint32_t img = rcEnc->rcCreateClientImage(rcEnc, ctxHandle, target, texture);
         EGLImage_t *image = new EGLImage_t();
         image->dpy = dpy;
         image->target = target;
-        image->texture_2d = 0;   // TODO
+        image->host_egl_image = img;
 
         return (EGLImageKHR)image;
     }
@@ -1264,11 +1268,10 @@
         return EGL_TRUE;
     }
     else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
-        // TODO
-
+        uint32_t host_egl_image = image->host_egl_image;
         delete image;
-
-        return EGL_TRUE;
+        DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
+        return rcEnc->rcDestroyClientImage(rcEnc, host_egl_image);
     }
 
     setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
diff --git a/opengl/system/renderControl_enc/README b/opengl/system/renderControl_enc/README
index 2ee1a57..349b6db 100644
--- a/opengl/system/renderControl_enc/README
+++ b/opengl/system/renderControl_enc/README
@@ -134,3 +134,9 @@
                          GLenum type, void* pixels);
        Updates the content of a subregion of a colorBuffer object.
        pixels are always unpacked with alignment of 1.
+
+uint32_t rcCreateClientImage(uint32_t context, EGLenum target, GLuint buffer)
+       Create an EGLImage from a client object.
+
+int rcDestroyClientImage(uint32_t image)
+       Destroy an EGLImage object.
diff --git a/opengl/system/renderControl_enc/renderControl.in b/opengl/system/renderControl_enc/renderControl.in
index 8281fd9..ed68599 100644
--- a/opengl/system/renderControl_enc/renderControl.in
+++ b/opengl/system/renderControl_enc/renderControl.in
@@ -23,3 +23,6 @@
 GL_ENTRY(EGLint, rcColorBufferCacheFlush, uint32_t colorbuffer, EGLint postCount,int forRead)
 GL_ENTRY(void, rcReadColorBuffer, uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void *pixels)
 GL_ENTRY(int, rcUpdateColorBuffer, uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void *pixels)
+GL_ENTRY(int, rcOpenColorBuffer2, uint32_t colorbuffer)
+GL_ENTRY(uint32_t, rcCreateClientImage, uint32_t context, EGLenum target, GLuint buffer)
+GL_ENTRY(int, rcDestroyClientImage, uint32_t image)
diff --git a/opengl/system/renderControl_enc/renderControl_client_base.h b/opengl/system/renderControl_enc/renderControl_client_base.h
new file mode 100644
index 0000000..4fb5d76
--- /dev/null
+++ b/opengl/system/renderControl_enc/renderControl_client_base.h
@@ -0,0 +1,41 @@
+// Generated Code - DO NOT EDIT !!
+// generated by 'emugen'
+#ifndef __renderControl_client_base_t_h
+#define __renderControl_client_base_t_h
+
+#include "renderControl_client_proc.h"
+
+
+struct renderControl_client_base_t {
+
+	rcGetRendererVersion_client_proc_t rcGetRendererVersion;
+	rcGetEGLVersion_client_proc_t rcGetEGLVersion;
+	rcQueryEGLString_client_proc_t rcQueryEGLString;
+	rcGetGLString_client_proc_t rcGetGLString;
+	rcGetNumConfigs_client_proc_t rcGetNumConfigs;
+	rcGetConfigs_client_proc_t rcGetConfigs;
+	rcChooseConfig_client_proc_t rcChooseConfig;
+	rcGetFBParam_client_proc_t rcGetFBParam;
+	rcCreateContext_client_proc_t rcCreateContext;
+	rcDestroyContext_client_proc_t rcDestroyContext;
+	rcCreateWindowSurface_client_proc_t rcCreateWindowSurface;
+	rcDestroyWindowSurface_client_proc_t rcDestroyWindowSurface;
+	rcCreateColorBuffer_client_proc_t rcCreateColorBuffer;
+	rcOpenColorBuffer_client_proc_t rcOpenColorBuffer;
+	rcCloseColorBuffer_client_proc_t rcCloseColorBuffer;
+	rcSetWindowColorBuffer_client_proc_t rcSetWindowColorBuffer;
+	rcFlushWindowColorBuffer_client_proc_t rcFlushWindowColorBuffer;
+	rcMakeCurrent_client_proc_t rcMakeCurrent;
+	rcFBPost_client_proc_t rcFBPost;
+	rcFBSetSwapInterval_client_proc_t rcFBSetSwapInterval;
+	rcBindTexture_client_proc_t rcBindTexture;
+	rcBindRenderbuffer_client_proc_t rcBindRenderbuffer;
+	rcColorBufferCacheFlush_client_proc_t rcColorBufferCacheFlush;
+	rcReadColorBuffer_client_proc_t rcReadColorBuffer;
+	rcUpdateColorBuffer_client_proc_t rcUpdateColorBuffer;
+	rcOpenColorBuffer2_client_proc_t rcOpenColorBuffer2;
+	rcCreateClientImage_client_proc_t rcCreateClientImage;
+	rcDestroyClientImage_client_proc_t rcDestroyClientImage;
+};
+
+#endif
diff --git a/opengl/system/renderControl_enc/renderControl_client_context.cpp b/opengl/system/renderControl_enc/renderControl_client_context.cpp
index f58f549..22db054 100644
--- a/opengl/system/renderControl_enc/renderControl_client_context.cpp
+++ b/opengl/system/renderControl_enc/renderControl_client_context.cpp
@@ -36,6 +36,8 @@
 	rcReadColorBuffer = (rcReadColorBuffer_client_proc_t) getProc("rcReadColorBuffer", userData);
 	rcUpdateColorBuffer = (rcUpdateColorBuffer_client_proc_t) getProc("rcUpdateColorBuffer", userData);
 	rcOpenColorBuffer2 = (rcOpenColorBuffer2_client_proc_t) getProc("rcOpenColorBuffer2", userData);
+	rcCreateClientImage = (rcCreateClientImage_client_proc_t) getProc("rcCreateClientImage", userData);
+	rcDestroyClientImage = (rcDestroyClientImage_client_proc_t) getProc("rcDestroyClientImage", userData);
 	return 0;
 }
 
diff --git a/opengl/system/renderControl_enc/renderControl_client_context.h b/opengl/system/renderControl_enc/renderControl_client_context.h
index 4c71d89..18e274e 100644
--- a/opengl/system/renderControl_enc/renderControl_client_context.h
+++ b/opengl/system/renderControl_enc/renderControl_client_context.h
@@ -3,37 +3,11 @@
 #ifndef __renderControl_client_context_t_h
 #define __renderControl_client_context_t_h
 
-#include "renderControl_client_proc.h"
+#include "renderControl_client_base.h"
 
 
-struct renderControl_client_context_t {
+struct renderControl_client_context_t : renderControl_client_base_t {
 
-	rcGetRendererVersion_client_proc_t rcGetRendererVersion;
-	rcGetEGLVersion_client_proc_t rcGetEGLVersion;
-	rcQueryEGLString_client_proc_t rcQueryEGLString;
-	rcGetGLString_client_proc_t rcGetGLString;
-	rcGetNumConfigs_client_proc_t rcGetNumConfigs;
-	rcGetConfigs_client_proc_t rcGetConfigs;
-	rcChooseConfig_client_proc_t rcChooseConfig;
-	rcGetFBParam_client_proc_t rcGetFBParam;
-	rcCreateContext_client_proc_t rcCreateContext;
-	rcDestroyContext_client_proc_t rcDestroyContext;
-	rcCreateWindowSurface_client_proc_t rcCreateWindowSurface;
-	rcDestroyWindowSurface_client_proc_t rcDestroyWindowSurface;
-	rcCreateColorBuffer_client_proc_t rcCreateColorBuffer;
-	rcOpenColorBuffer_client_proc_t rcOpenColorBuffer;
-	rcCloseColorBuffer_client_proc_t rcCloseColorBuffer;
-	rcSetWindowColorBuffer_client_proc_t rcSetWindowColorBuffer;
-	rcFlushWindowColorBuffer_client_proc_t rcFlushWindowColorBuffer;
-	rcMakeCurrent_client_proc_t rcMakeCurrent;
-	rcFBPost_client_proc_t rcFBPost;
-	rcFBSetSwapInterval_client_proc_t rcFBSetSwapInterval;
-	rcBindTexture_client_proc_t rcBindTexture;
-	rcBindRenderbuffer_client_proc_t rcBindRenderbuffer;
-	rcColorBufferCacheFlush_client_proc_t rcColorBufferCacheFlush;
-	rcReadColorBuffer_client_proc_t rcReadColorBuffer;
-	rcUpdateColorBuffer_client_proc_t rcUpdateColorBuffer;
-	rcOpenColorBuffer2_client_proc_t rcOpenColorBuffer2;
 	 virtual ~renderControl_client_context_t() {}
 
 	typedef renderControl_client_context_t *CONTEXT_ACCESSOR_TYPE(void);
diff --git a/opengl/system/renderControl_enc/renderControl_client_proc.h b/opengl/system/renderControl_enc/renderControl_client_proc.h
index 85200cf..5465e8f 100644
--- a/opengl/system/renderControl_enc/renderControl_client_proc.h
+++ b/opengl/system/renderControl_enc/renderControl_client_proc.h
@@ -35,6 +35,8 @@
 typedef void (renderControl_APIENTRY *rcReadColorBuffer_client_proc_t) (void * ctx, uint32_t, GLint, GLint, GLint, GLint, GLenum, GLenum, void*);
 typedef int (renderControl_APIENTRY *rcUpdateColorBuffer_client_proc_t) (void * ctx, uint32_t, GLint, GLint, GLint, GLint, GLenum, GLenum, void*);
 typedef int (renderControl_APIENTRY *rcOpenColorBuffer2_client_proc_t) (void * ctx, uint32_t);
+typedef uint32_t (renderControl_APIENTRY *rcCreateClientImage_client_proc_t) (void * ctx, uint32_t, EGLenum, GLuint);
+typedef int (renderControl_APIENTRY *rcDestroyClientImage_client_proc_t) (void * ctx, uint32_t);
 
 
 #endif
diff --git a/opengl/system/renderControl_enc/renderControl_enc.cpp b/opengl/system/renderControl_enc/renderControl_enc.cpp
index 34113e1..7ae9e05 100644
--- a/opengl/system/renderControl_enc/renderControl_enc.cpp
+++ b/opengl/system/renderControl_enc/renderControl_enc.cpp
@@ -526,6 +526,46 @@
 	return retval;
 }
 
+uint32_t rcCreateClientImage_enc(void *self , uint32_t context, EGLenum target, GLuint buffer)
+{
+
+	renderControl_encoder_context_t *ctx = (renderControl_encoder_context_t *)self;
+	IOStream *stream = ctx->m_stream;
+
+	 unsigned char *ptr;
+	 const size_t packetSize = 8 + 4 + 4 + 4;
+	ptr = stream->alloc(packetSize);
+	int tmp = OP_rcCreateClientImage;memcpy(ptr, &tmp, 4); ptr += 4;
+	memcpy(ptr, &packetSize, 4);  ptr += 4;
+
+		memcpy(ptr, &context, 4); ptr += 4;
+		memcpy(ptr, &target, 4); ptr += 4;
+		memcpy(ptr, &buffer, 4); ptr += 4;
+
+	uint32_t retval;
+	stream->readback(&retval, 4);
+	return retval;
+}
+
+int rcDestroyClientImage_enc(void *self , uint32_t image)
+{
+
+	renderControl_encoder_context_t *ctx = (renderControl_encoder_context_t *)self;
+	IOStream *stream = ctx->m_stream;
+
+	 unsigned char *ptr;
+	 const size_t packetSize = 8 + 4;
+	ptr = stream->alloc(packetSize);
+	int tmp = OP_rcDestroyClientImage;memcpy(ptr, &tmp, 4); ptr += 4;
+	memcpy(ptr, &packetSize, 4);  ptr += 4;
+
+		memcpy(ptr, &image, 4); ptr += 4;
+
+	int retval;
+	stream->readback(&retval, 4);
+	return retval;
+}
+
 }  // namespace
 
 renderControl_encoder_context_t::renderControl_encoder_context_t(IOStream *stream)
@@ -558,5 +598,7 @@
 	this->rcReadColorBuffer = &rcReadColorBuffer_enc;
 	this->rcUpdateColorBuffer = &rcUpdateColorBuffer_enc;
 	this->rcOpenColorBuffer2 = &rcOpenColorBuffer2_enc;
+	this->rcCreateClientImage = &rcCreateClientImage_enc;
+	this->rcDestroyClientImage = &rcDestroyClientImage_enc;
 }
 
diff --git a/opengl/system/renderControl_enc/renderControl_entry.cpp b/opengl/system/renderControl_enc/renderControl_entry.cpp
index 71edb83..a4dc525 100644
--- a/opengl/system/renderControl_enc/renderControl_entry.cpp
+++ b/opengl/system/renderControl_enc/renderControl_entry.cpp
@@ -32,6 +32,8 @@
 	void rcReadColorBuffer(uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void* pixels);
 	int rcUpdateColorBuffer(uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void* pixels);
 	int rcOpenColorBuffer2(uint32_t colorbuffer);
+	uint32_t rcCreateClientImage(uint32_t context, EGLenum target, GLuint buffer);
+	int rcDestroyClientImage(uint32_t image);
 };
 
 #endif
@@ -197,3 +199,15 @@
 	return ctx->rcOpenColorBuffer2(ctx, colorbuffer);
 }
 
+uint32_t rcCreateClientImage(uint32_t context, EGLenum target, GLuint buffer)
+{
+	GET_CONTEXT;
+	return ctx->rcCreateClientImage(ctx, context, target, buffer);
+}
+
+int rcDestroyClientImage(uint32_t image)
+{
+	GET_CONTEXT;
+	return ctx->rcDestroyClientImage(ctx, image);
+}
+
diff --git a/opengl/system/renderControl_enc/renderControl_ftable.h b/opengl/system/renderControl_enc/renderControl_ftable.h
index e15fb0c..0be47b8 100644
--- a/opengl/system/renderControl_enc/renderControl_ftable.h
+++ b/opengl/system/renderControl_enc/renderControl_ftable.h
@@ -34,6 +34,8 @@
 	{"rcReadColorBuffer", (void*)rcReadColorBuffer},
 	{"rcUpdateColorBuffer", (void*)rcUpdateColorBuffer},
 	{"rcOpenColorBuffer2", (void*)rcOpenColorBuffer2},
+	{"rcCreateClientImage", (void*)rcCreateClientImage},
+	{"rcDestroyClientImage", (void*)rcDestroyClientImage},
 };
 static const int renderControl_num_funcs = sizeof(renderControl_funcs_by_name) / sizeof(struct _renderControl_funcs_by_name);
 
diff --git a/opengl/system/renderControl_enc/renderControl_opcodes.h b/opengl/system/renderControl_enc/renderControl_opcodes.h
index a00dc77..2ed08e3 100644
--- a/opengl/system/renderControl_enc/renderControl_opcodes.h
+++ b/opengl/system/renderControl_enc/renderControl_opcodes.h
@@ -29,7 +29,9 @@
 #define OP_rcReadColorBuffer 					10023
 #define OP_rcUpdateColorBuffer 					10024
 #define OP_rcOpenColorBuffer2 					10025
-#define OP_last 					10026
+#define OP_rcCreateClientImage 					10026
+#define OP_rcDestroyClientImage 					10027
+#define OP_last 					10028
 
 
 #endif