opengl: rcOpenColorBuffer must be synchronous

The gralloc register_buffer() function, which calls rcOpenColorBuffer,
must actually increment the reference count before returning.
Otherwise the buffer allocator may release its reference before the
client has obtained one, and the buffer will be freed prematurely.
Since rcOpenColorBuffer was just sending a message to the host without
waiting for it to be received/processed, this guarantee was not met.
Adding a return value makes the call synchronous.

Bug: 12988668
Change-Id: I8b2399cfb0f600f99b3387f630343291b59bc9a6
diff --git a/opengl/system/gralloc/gralloc.cpp b/opengl/system/gralloc/gralloc.cpp
index 6250a2f..c13a98d 100644
--- a/opengl/system/gralloc/gralloc.cpp
+++ b/opengl/system/gralloc/gralloc.cpp
@@ -535,7 +535,7 @@
     if (cb->hostHandle != 0) {
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
-        rcEnc->rcOpenColorBuffer(rcEnc, cb->hostHandle);
+        rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
     }
 
     //
diff --git a/opengl/system/renderControl_enc/renderControl_client_context.cpp b/opengl/system/renderControl_enc/renderControl_client_context.cpp
index 9488248..a4bdb25 100644
--- a/opengl/system/renderControl_enc/renderControl_client_context.cpp
+++ b/opengl/system/renderControl_enc/renderControl_client_context.cpp
@@ -37,6 +37,7 @@
 	ptr = getProc("rcColorBufferCacheFlush", userData); set_rcColorBufferCacheFlush((rcColorBufferCacheFlush_client_proc_t)ptr);
 	ptr = getProc("rcReadColorBuffer", userData); set_rcReadColorBuffer((rcReadColorBuffer_client_proc_t)ptr);
 	ptr = getProc("rcUpdateColorBuffer", userData); set_rcUpdateColorBuffer((rcUpdateColorBuffer_client_proc_t)ptr);
+	ptr = getProc("rcOpenColorBuffer2", userData); set_rcOpenColorBuffer2((rcOpenColorBuffer2_client_proc_t)ptr);
 	return 0;
 }
 
diff --git a/opengl/system/renderControl_enc/renderControl_client_context.h b/opengl/system/renderControl_enc/renderControl_client_context.h
index ed2b1fb..46a007f 100644
--- a/opengl/system/renderControl_enc/renderControl_client_context.h
+++ b/opengl/system/renderControl_enc/renderControl_client_context.h
@@ -33,6 +33,7 @@
 	rcColorBufferCacheFlush_client_proc_t rcColorBufferCacheFlush;
 	rcReadColorBuffer_client_proc_t rcReadColorBuffer;
 	rcUpdateColorBuffer_client_proc_t rcUpdateColorBuffer;
+	rcOpenColorBuffer2_client_proc_t rcOpenColorBuffer2;
 	//Accessors 
 	virtual rcGetRendererVersion_client_proc_t set_rcGetRendererVersion(rcGetRendererVersion_client_proc_t f) { rcGetRendererVersion_client_proc_t retval = rcGetRendererVersion; rcGetRendererVersion = f; return retval;}
 	virtual rcGetEGLVersion_client_proc_t set_rcGetEGLVersion(rcGetEGLVersion_client_proc_t f) { rcGetEGLVersion_client_proc_t retval = rcGetEGLVersion; rcGetEGLVersion = f; return retval;}
@@ -59,6 +60,7 @@
 	virtual rcColorBufferCacheFlush_client_proc_t set_rcColorBufferCacheFlush(rcColorBufferCacheFlush_client_proc_t f) { rcColorBufferCacheFlush_client_proc_t retval = rcColorBufferCacheFlush; rcColorBufferCacheFlush = f; return retval;}
 	virtual rcReadColorBuffer_client_proc_t set_rcReadColorBuffer(rcReadColorBuffer_client_proc_t f) { rcReadColorBuffer_client_proc_t retval = rcReadColorBuffer; rcReadColorBuffer = f; return retval;}
 	virtual rcUpdateColorBuffer_client_proc_t set_rcUpdateColorBuffer(rcUpdateColorBuffer_client_proc_t f) { rcUpdateColorBuffer_client_proc_t retval = rcUpdateColorBuffer; rcUpdateColorBuffer = f; return retval;}
+	virtual rcOpenColorBuffer2_client_proc_t set_rcOpenColorBuffer2(rcOpenColorBuffer2_client_proc_t f) { rcOpenColorBuffer2_client_proc_t retval = rcOpenColorBuffer2; rcOpenColorBuffer2 = f; return retval;}
 	 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 3e00290..85200cf 100644
--- a/opengl/system/renderControl_enc/renderControl_client_proc.h
+++ b/opengl/system/renderControl_enc/renderControl_client_proc.h
@@ -34,6 +34,7 @@
 typedef EGLint (renderControl_APIENTRY *rcColorBufferCacheFlush_client_proc_t) (void * ctx, uint32_t, EGLint, int);
 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);
 
 
 #endif
diff --git a/opengl/system/renderControl_enc/renderControl_enc.cpp b/opengl/system/renderControl_enc/renderControl_enc.cpp
index 41f59d0..a40a501 100644
--- a/opengl/system/renderControl_enc/renderControl_enc.cpp
+++ b/opengl/system/renderControl_enc/renderControl_enc.cpp
@@ -505,6 +505,25 @@
 	return retval;
 }
 
+int rcOpenColorBuffer2_enc(void *self , uint32_t colorbuffer)
+{
+
+	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_rcOpenColorBuffer2;memcpy(ptr, &tmp, 4); ptr += 4;
+	memcpy(ptr, &packetSize, 4);  ptr += 4;
+
+		memcpy(ptr, &colorbuffer, 4); ptr += 4;
+
+	int retval;
+	stream->readback(&retval, 4);
+	return retval;
+}
+
 renderControl_encoder_context_t::renderControl_encoder_context_t(IOStream *stream)
 {
 	m_stream = stream;
@@ -534,5 +553,6 @@
 	set_rcColorBufferCacheFlush(rcColorBufferCacheFlush_enc);
 	set_rcReadColorBuffer(rcReadColorBuffer_enc);
 	set_rcUpdateColorBuffer(rcUpdateColorBuffer_enc);
+	set_rcOpenColorBuffer2(rcOpenColorBuffer2_enc);
 }
 
diff --git a/opengl/system/renderControl_enc/renderControl_enc.h b/opengl/system/renderControl_enc/renderControl_enc.h
index 712eeb9..92ef76e 100644
--- a/opengl/system/renderControl_enc/renderControl_enc.h
+++ b/opengl/system/renderControl_enc/renderControl_enc.h
@@ -47,5 +47,6 @@
 	EGLint rcColorBufferCacheFlush_enc(void *self , uint32_t colorbuffer, EGLint postCount, int forRead);
 	void rcReadColorBuffer_enc(void *self , uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void* pixels);
 	int rcUpdateColorBuffer_enc(void *self , uint32_t colorbuffer, GLint x, GLint y, GLint width, GLint height, GLenum format, GLenum type, void* pixels);
+	int rcOpenColorBuffer2_enc(void *self , uint32_t colorbuffer);
 };
 #endif
\ No newline at end of file
diff --git a/opengl/system/renderControl_enc/renderControl_entry.cpp b/opengl/system/renderControl_enc/renderControl_entry.cpp
index 9a0bba9..a15fd14 100644
--- a/opengl/system/renderControl_enc/renderControl_entry.cpp
+++ b/opengl/system/renderControl_enc/renderControl_entry.cpp
@@ -31,6 +31,7 @@
 	EGLint rcColorBufferCacheFlush(uint32_t colorbuffer, EGLint postCount, int forRead);
 	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);
 };
 
 #endif
@@ -190,3 +191,9 @@
 	 return ctx->rcUpdateColorBuffer(ctx, colorbuffer, x, y, width, height, format, type, pixels);
 }
 
+int rcOpenColorBuffer2(uint32_t colorbuffer)
+{
+	GET_CONTEXT; 
+	 return ctx->rcOpenColorBuffer2(ctx, colorbuffer);
+}
+
diff --git a/opengl/system/renderControl_enc/renderControl_ftable.h b/opengl/system/renderControl_enc/renderControl_ftable.h
index 1e9e2f9..a43fe5b 100644
--- a/opengl/system/renderControl_enc/renderControl_ftable.h
+++ b/opengl/system/renderControl_enc/renderControl_ftable.h
@@ -33,6 +33,7 @@
 	{"rcColorBufferCacheFlush", (void*)rcColorBufferCacheFlush},
 	{"rcReadColorBuffer", (void*)rcReadColorBuffer},
 	{"rcUpdateColorBuffer", (void*)rcUpdateColorBuffer},
+	{"rcOpenColorBuffer2", (void*)rcOpenColorBuffer2},
 };
 static 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 b44f5d0..a00dc77 100644
--- a/opengl/system/renderControl_enc/renderControl_opcodes.h
+++ b/opengl/system/renderControl_enc/renderControl_opcodes.h
@@ -28,7 +28,8 @@
 #define OP_rcColorBufferCacheFlush 					10022
 #define OP_rcReadColorBuffer 					10023
 #define OP_rcUpdateColorBuffer 					10024
-#define OP_last 					10025
+#define OP_rcOpenColorBuffer2 					10025
+#define OP_last 					10026
 
 
 #endif