goldfish-opengl: update to 4153dd9ab6a14

commit 4153dd9ab6a14a263e97c40faeab237b66c9f67b
Author: bohu <bohu@google.com>
Date:   Tue Aug 1 17:05:26 2017 -0700

get emulator iamge on oc-mr1-dev work with treble

Need to remove libui dependency
otherwise, libEGL wont load because it is looking for it;
but it does not seem to need it

BUG: 64401460

Change-Id: I05bfb5dc921f50264a01522d32f3d3dcc0199ed5
Merged-In: I05bfb5dc921f50264a01522d32f3d3dcc0199ed5
Merged-Id: I81a77c85307524c2b075d430a84c73556f64524e
Merged-In: I6b435afce04164308767daef3586358b04021cbd
diff --git a/system/GLESv2_enc/GL2Encoder.cpp b/system/GLESv2_enc/GL2Encoder.cpp
index 3c37a82..39e4cda 100755
--- a/system/GLESv2_enc/GL2Encoder.cpp
+++ b/system/GLESv2_enc/GL2Encoder.cpp
@@ -285,6 +285,8 @@
 
     OVERRIDE(glGetIntegeri_v);
     OVERRIDE(glGetInteger64i_v);
+    OVERRIDE(glGetInteger64v);
+    OVERRIDE(glGetBooleani_v);
 
     OVERRIDE(glGetShaderiv);
 
@@ -356,12 +358,112 @@
     GL2Encoder *ctx = (GL2Encoder *)self;
     GLenum err = ctx->getError();
     if(err != GL_NO_ERROR) {
+        ctx->m_glGetError_enc(ctx); // also clear host error
         ctx->setError(GL_NO_ERROR);
         return err;
     }
 
     return ctx->m_glGetError_enc(self);
+}
 
+class GL2Encoder::ErrorUpdater {
+public:
+    ErrorUpdater(GL2Encoder* ctx) :
+        mCtx(ctx),
+        guest_error(ctx->getError()),
+        host_error(ctx->m_glGetError_enc(ctx)) {
+            // Preserve any existing GL error in the guest:
+            // OpenGL ES 3.0.5 spec:
+            // The command enum GetError( void ); is used to obtain error information.
+            // Each detectable error is assigned a numeric code. When an error is
+            // detected, a flag is set and the code is recorded. Further errors, if
+            // they occur, do not affect this recorded code. When GetError is called,
+            // the code is returned and the flag is cleared, so that a further error
+            // will again record its code. If a call to GetError returns NO_ERROR, then
+            // there has been no detectable error since the last call to GetError (or
+            // since the GL was initialized).
+            if (guest_error == GL_NO_ERROR) {
+                guest_error = host_error;
+            }
+        }
+
+    GLenum getHostErrorAndUpdate() {
+        host_error = mCtx->m_glGetError_enc(mCtx);
+        if (guest_error == GL_NO_ERROR) {
+            guest_error = host_error;
+        }
+        return host_error;
+    }
+
+    void updateGuestErrorState() {
+        mCtx->setError(guest_error);
+    }
+
+private:
+    GL2Encoder* mCtx;
+    GLenum guest_error;
+    GLenum host_error;
+};
+
+template<class T>
+class GL2Encoder::ScopedQueryUpdate {
+public:
+    ScopedQueryUpdate(GL2Encoder* ctx, uint32_t bytes, T* target) :
+        mCtx(ctx),
+        mBuf(bytes, 0),
+        mTarget(target),
+        mErrorUpdater(ctx) {
+    }
+    T* hostStagingBuffer() {
+        return (T*)&mBuf[0];
+    }
+    ~ScopedQueryUpdate() {
+        GLint hostError = mErrorUpdater.getHostErrorAndUpdate();
+        if (hostError == GL_NO_ERROR) {
+            memcpy(mTarget, &mBuf[0], mBuf.size());
+        }
+        mErrorUpdater.updateGuestErrorState();
+    }
+private:
+    GL2Encoder* mCtx;
+    std::vector<char> mBuf;
+    T* mTarget;
+    ErrorUpdater mErrorUpdater;
+};
+
+void GL2Encoder::safe_glGetBooleanv(GLenum param, GLboolean* val) {
+    ScopedQueryUpdate<GLboolean> query(this, glUtilsParamSize(param) * sizeof(GLboolean), val);
+    m_glGetBooleanv_enc(this, param, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetFloatv(GLenum param, GLfloat* val) {
+    ScopedQueryUpdate<GLfloat> query(this, glUtilsParamSize(param) * sizeof(GLfloat), val);
+    m_glGetFloatv_enc(this, param, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetIntegerv(GLenum param, GLint* val) {
+    ScopedQueryUpdate<GLint> query(this, glUtilsParamSize(param) * sizeof(GLint), val);
+    m_glGetIntegerv_enc(this, param, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetInteger64v(GLenum param, GLint64* val) {
+    ScopedQueryUpdate<GLint64> query(this, glUtilsParamSize(param) * sizeof(GLint64), val);
+    m_glGetInteger64v_enc(this, param, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetIntegeri_v(GLenum param, GLuint index, GLint* val) {
+    ScopedQueryUpdate<GLint> query(this, sizeof(GLint), val);
+    m_glGetIntegeri_v_enc(this, param, index, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetInteger64i_v(GLenum param, GLuint index, GLint64* val) {
+    ScopedQueryUpdate<GLint64> query(this, sizeof(GLint64), val);
+    m_glGetInteger64i_v_enc(this, param, index, query.hostStagingBuffer());
+}
+
+void GL2Encoder::safe_glGetBooleani_v(GLenum param, GLuint index, GLboolean* val) {
+    ScopedQueryUpdate<GLboolean> query(this, sizeof(GLboolean), val);
+    m_glGetBooleani_v_enc(this, param, index, query.hostStagingBuffer());
 }
 
 void GL2Encoder::s_glFlush(void *self)
@@ -553,7 +655,7 @@
     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
     case GL_MAX_TEXTURE_IMAGE_UNITS:
-        ctx->m_glGetIntegerv_enc(self, param, ptr);
+        ctx->safe_glGetIntegerv(param, ptr);
         *ptr = MIN(*ptr, GLClientState::MAX_TEXTURE_UNITS);
         break;
 
@@ -566,7 +668,7 @@
 
     case GL_MAX_VERTEX_ATTRIBS:
         if (!ctx->m_state->getClientStateParameter<GLint>(param, ptr)) {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_state->setMaxVertexAttribs(*ptr);
         }
         break;
@@ -574,7 +676,7 @@
         if (ctx->m_max_vertexAttribStride != 0) {
             *ptr = ctx->m_max_vertexAttribStride;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_max_vertexAttribStride = *ptr;
         }
         break;
@@ -582,7 +684,7 @@
         if (ctx->m_max_cubeMapTextureSize != 0) {
             *ptr = ctx->m_max_cubeMapTextureSize;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_max_cubeMapTextureSize = *ptr;
         }
         break;
@@ -590,7 +692,7 @@
         if (ctx->m_max_renderBufferSize != 0) {
             *ptr = ctx->m_max_renderBufferSize;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_max_renderBufferSize = *ptr;
         }
         break;
@@ -598,7 +700,7 @@
         if (ctx->m_max_textureSize != 0) {
             *ptr = ctx->m_max_textureSize;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_max_textureSize = *ptr;
         }
         break;
@@ -606,7 +708,7 @@
         if (ctx->m_max_3d_textureSize != 0) {
             *ptr = ctx->m_max_3d_textureSize;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_max_3d_textureSize = *ptr;
         }
         break;
@@ -614,7 +716,7 @@
         if (ctx->m_ssbo_offset_align != 0) {
             *ptr = ctx->m_ssbo_offset_align;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_ssbo_offset_align = *ptr;
         }
         break;
@@ -622,7 +724,7 @@
         if (ctx->m_ubo_offset_align != 0) {
             *ptr = ctx->m_ubo_offset_align;
         } else {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
             ctx->m_ubo_offset_align = *ptr;
         }
         break;
@@ -634,9 +736,31 @@
     case GL_MAX_DEPTH_TEXTURE_SAMPLES:
         *ptr = 4;
         break;
+    // Checks for version-incompatible enums.
+    // Not allowed in vanilla ES 2.0.
+    case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
+    case GL_MAX_UNIFORM_BUFFER_BINDINGS:
+        SET_ERROR_IF(ctx->majorVersion() < 3, GL_INVALID_ENUM);
+        ctx->safe_glGetIntegerv(param, ptr);
+        break;
+    case GL_MAX_COLOR_ATTACHMENTS:
+    case GL_MAX_DRAW_BUFFERS:
+        SET_ERROR_IF(ctx->majorVersion() < 3 &&
+                     !ctx->hasExtension("GL_EXT_draw_buffers"), GL_INVALID_ENUM);
+        ctx->safe_glGetIntegerv(param, ptr);
+        break;
+    // Not allowed in ES 3.0.
+    case GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS:
+    case GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS:
+    case GL_MAX_VERTEX_ATTRIB_BINDINGS:
+        SET_ERROR_IF(ctx->majorVersion() < 3 ||
+                     (ctx->majorVersion() == 3 &&
+                      ctx->minorVersion() == 0), GL_INVALID_ENUM);
+        ctx->safe_glGetIntegerv(param, ptr);
+        break;
     default:
         if (!ctx->m_state->getClientStateParameter<GLint>(param, ptr)) {
-            ctx->m_glGetIntegerv_enc(self, param, ptr);
+            ctx->safe_glGetIntegerv(param, ptr);
         }
         break;
     }
@@ -671,7 +795,7 @@
     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
     case GL_MAX_TEXTURE_IMAGE_UNITS:
-        ctx->m_glGetFloatv_enc(self, param, ptr);
+        ctx->safe_glGetFloatv(param, ptr);
         *ptr = MIN(*ptr, (GLfloat)GLClientState::MAX_TEXTURE_UNITS);
         break;
 
@@ -684,7 +808,7 @@
 
     default:
         if (!ctx->m_state->getClientStateParameter<GLfloat>(param, ptr)) {
-            ctx->m_glGetFloatv_enc(self, param, ptr);
+            ctx->safe_glGetFloatv(param, ptr);
         }
         break;
     }
@@ -726,7 +850,7 @@
 
     default:
         if (!ctx->m_state->getClientStateParameter<GLboolean>(param, ptr)) {
-            ctx->m_glGetBooleanv_enc(self, param, ptr);
+            ctx->safe_glGetBooleanv(param, ptr);
         }
         *ptr = (*ptr != 0) ? GL_TRUE : GL_FALSE;
         break;
@@ -1313,7 +1437,7 @@
     ctx->m_glLinkProgram_enc(self, program);
 
     GLint linkStatus = 0;
-    ctx->glGetProgramiv(self,program,GL_LINK_STATUS,&linkStatus);
+    ctx->glGetProgramiv(self, program, GL_LINK_STATUS, &linkStatus);
     if (!linkStatus) {
         return;
     }
@@ -4140,7 +4264,7 @@
         break;
     }
 
-    ctx->m_glGetIntegeri_v_enc(self, target, index, params);
+    ctx->safe_glGetIntegeri_v(target, index, params);
 }
 
 void GL2Encoder::s_glGetInteger64i_v(void* self, GLenum target, GLuint index, GLint64* params) {
@@ -4189,7 +4313,17 @@
         break;
     }
 
-    ctx->m_glGetInteger64i_v_enc(self, target, index, params);
+    ctx->safe_glGetInteger64i_v(target, index, params);
+}
+
+void GL2Encoder::s_glGetInteger64v(void* self, GLenum param, GLint64* val) {
+    GL2Encoder *ctx = (GL2Encoder *)self;
+    ctx->safe_glGetInteger64v(param, val);
+}
+
+void GL2Encoder::s_glGetBooleani_v(void* self, GLenum param, GLuint index, GLboolean* val) {
+    GL2Encoder *ctx = (GL2Encoder *)self;
+    ctx->safe_glGetBooleani_v(param, index, val);
 }
 
 void GL2Encoder::s_glGetShaderiv(void* self, GLuint shader, GLenum pname, GLint* params) {
@@ -4260,7 +4394,7 @@
     ctx->m_shared->associateGLShaderProgram(res, spDataId);
 
     GLint numUniforms = 0;
-    ctx->glGetProgramiv(ctx, res, GL_ACTIVE_UNIFORMS, &numUniforms);
+    ctx->glGetProgramiv(self, res, GL_ACTIVE_UNIFORMS, &numUniforms);
     ctx->m_shared->initShaderProgramData(res, numUniforms);
 
     GLint maxLength=0;
diff --git a/system/GLESv2_enc/GL2Encoder.h b/system/GLESv2_enc/GL2Encoder.h
index 11162ee..3084713 100644
--- a/system/GLESv2_enc/GL2Encoder.h
+++ b/system/GLESv2_enc/GL2Encoder.h
@@ -30,6 +30,13 @@
     void setClientState(GLClientState *state) {
         m_state = state;
     }
+    void setVersion(int major, int minor,
+                    int deviceMajor, int deviceMinor) {
+        m_currMajorVersion = major;
+        m_currMinorVersion = minor;
+        m_deviceMajorVersion = deviceMajor;
+        m_deviceMinorVersion = deviceMinor;
+    }
     void setClientStateMakeCurrent(GLClientState *state,
                                    int majorVersion,
                                    int minorVersion,
@@ -128,6 +135,21 @@
     bool isCompleteFbo(GLenum target, const GLClientState* state, GLenum attachment) const;
     bool checkFramebufferCompleteness(GLenum target, const GLClientState* state) const;
 
+    // Utility classes for safe queries that
+    // need access to private class members
+    class ErrorUpdater;
+    template<class T> class ScopedQueryUpdate;
+    
+    // General queries
+    void safe_glGetBooleanv(GLenum param, GLboolean *val);
+    void safe_glGetFloatv(GLenum param, GLfloat *val);
+    void safe_glGetIntegerv(GLenum param, GLint *val);
+    void safe_glGetInteger64v(GLenum param, GLint64 *val);
+    void safe_glGetIntegeri_v(GLenum param, GLuint index, GLint *val);
+    void safe_glGetInteger64i_v(GLenum param, GLuint index, GLint64 *val);
+    void safe_glGetBooleani_v(GLenum param, GLuint index, GLboolean *val);
+
+    // API implementation
     glGetError_client_proc_t    m_glGetError_enc;
     static GLenum s_glGetError(void * self);
 
@@ -161,7 +183,6 @@
     glDrawElements_client_proc_t m_glDrawElements_enc;
     static void s_glDrawElements(void *self, GLenum mode, GLsizei count, GLenum type, const void *indices);
 
-
     glGetIntegerv_client_proc_t m_glGetIntegerv_enc;
     static void s_glGetIntegerv(void *self, GLenum pname, GLint *ptr);
 
@@ -171,6 +192,12 @@
     glGetBooleanv_client_proc_t m_glGetBooleanv_enc;
     static void s_glGetBooleanv(void *self, GLenum pname, GLboolean *ptr);
 
+    glGetInteger64v_client_proc_t m_glGetInteger64v_enc;
+    static void s_glGetInteger64v(void* self, GLenum param, GLint64* val);
+
+    glGetBooleani_v_client_proc_t m_glGetBooleani_v_enc;
+    static void s_glGetBooleani_v(void* self, GLenum param, GLuint index, GLboolean* val);
+
     glVertexAttribPointer_client_proc_t m_glVertexAttribPointer_enc;
     static void s_glVertexAttribPointer(void *self, GLuint indx, GLint size, GLenum type,
                                         GLboolean normalized, GLsizei stride, const GLvoid * ptr);
diff --git a/system/egl/Android.mk b/system/egl/Android.mk
index 2d7a559..bbab4f3 100644
--- a/system/egl/Android.mk
+++ b/system/egl/Android.mk
@@ -20,7 +20,6 @@
 endif
 
 ifdef IS_AT_LEAST_OPD1
-LOCAL_SHARED_LIBRARIES += libui
 LOCAL_HEADER_LIBRARIES += libnativebase_headers
 endif
 
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index 66a8a45..185fbc9 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -1561,6 +1561,13 @@
             ClientAPIExts::initClientFuncs(s_display.gles2_iface(), 1);
         }
         if (contextState->needsInitFromCaps()) {
+            // Need to set the version first if
+            // querying caps, or validation will trip incorrectly.
+            hostCon->gl2Encoder()->setVersion(
+                context->majorVersion,
+                context->minorVersion,
+                context->deviceMajorVersion,
+                context->deviceMinorVersion);
             // Get caps for indexed buffers from host.
             // Some need a current context.
             int max_transform_feedback_separate_attribs = 0;
@@ -1598,7 +1605,7 @@
                     max_draw_buffers);
         }
 
-        // set the client state and share group
+        // update the client state, share group, and version
         if (context->majorVersion > 1) {
             hostCon->gl2Encoder()->setClientStateMakeCurrent(
                     contextState,
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index 0b6785e..7c51667 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -27,6 +27,7 @@
 #include "HostConnection.h"
 #include "ProcessPipe.h"
 #include "glUtils.h"
+#include <utils/CallStack.h>
 #include <cutils/log.h>
 #include <cutils/properties.h>
 
@@ -604,7 +605,11 @@
         }
     }
 
-    if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
+    // API26 always expect at least one file descriptor is associated with
+    // one color buffer
+    // BUG: 37719038
+    if (PLATFORM_SDK_VERSION >= 26 ||
+        sw_read || sw_write || hw_cam_write || hw_vid_enc_read) {
         // keep space for image on guest memory if SW access is needed
         // or if the camera is doing writing
         if (yuv_format) {
@@ -1481,6 +1486,15 @@
  *
  * If not, then load gralloc.default instead as a fallback.
  */
+
+#if __LP64__
+static const char kGrallocDefaultSystemPath[] = "/system/lib64/hw/gralloc.default.so";
+static const char kGrallocDefaultVendorPath[] = "/vendor/lib64/hw/gralloc.default.so";
+#else
+static const char kGrallocDefaultSystemPath[] = "/system/lib/hw/gralloc.default.so";
+static const char kGrallocDefaultVendorPath[] = "/vendor/lib/hw/gralloc.default.so";
+#endif
+
 static void
 fallback_init(void)
 {
@@ -1494,12 +1508,17 @@
     if (atoi(prop) == 1) {
         return;
     }
-    ALOGD("Emulator without host-side GPU emulation detected.");
-#if __LP64__
-    module = dlopen("/vendor/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
-#else
-    module = dlopen("/vendor/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL);
-#endif
+    ALOGD("Emulator without host-side GPU emulation detected. "
+          "Loading gralloc.default.so from %s...",
+          kGrallocDefaultVendorPath);
+    module = dlopen(kGrallocDefaultVendorPath, RTLD_LAZY | RTLD_LOCAL);
+    if (!module) {
+        // vendor folder didn't work. try system
+        ALOGD("gralloc.default.so not found in /vendor. Trying %s...",
+              kGrallocDefaultSystemPath);
+        module = dlopen(kGrallocDefaultSystemPath, RTLD_LAZY | RTLD_LOCAL);
+    }
+
     if (module != NULL) {
         sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR));
         if (sFallback == NULL) {
@@ -1507,6 +1526,6 @@
         }
     }
     if (sFallback == NULL) {
-        ALOGE("Could not find software fallback module!?");
+        ALOGE("FATAL: Could not find gralloc.default.so!");
     }
 }