Merge pi-qpr1-release PQ1A.181105.017.A1 to pi-platform-release
am: 4178a73f0d

Change-Id: I3aff5ea4bcfc5da7eb56a89fea74691773760bc3
diff --git a/shared/OpenglCodecCommon/GLClientState.h b/shared/OpenglCodecCommon/GLClientState.h
index 675cea4..491fb73 100644
--- a/shared/OpenglCodecCommon/GLClientState.h
+++ b/shared/OpenglCodecCommon/GLClientState.h
@@ -146,9 +146,9 @@
 
     struct VAOState {
         VAOState(GLuint ibo, int nLoc, int nBindings) :
-            element_array_buffer_binding(ibo),
             attribState(nLoc),
-            bindingState(nBindings) { }
+            bindingState(nBindings),
+            element_array_buffer_binding(ibo) { }
         VertexAttribStateVector attribState;
         VertexAttribBindingVector bindingState;
         GLuint element_array_buffer_binding;
diff --git a/shared/OpenglCodecCommon/IndexRangeCache.h b/shared/OpenglCodecCommon/IndexRangeCache.h
index f19bd24..5f77958 100644
--- a/shared/OpenglCodecCommon/IndexRangeCache.h
+++ b/shared/OpenglCodecCommon/IndexRangeCache.h
@@ -75,10 +75,8 @@
             primitiveRestartEnabled(_primitiveRestart) { }
 
         bool operator<(const IndexRangeKey& rhs) const {
-            size_t start = offset;
             size_t start_other = rhs.offset;
             size_t end = offset + count * glSizeof(type);
-            size_t end_other = rhs.offset + rhs.count * glSizeof(rhs.type);
 
             if (end <= start_other) {
                 return true;
diff --git a/shared/OpenglCodecCommon/SocketStream.cpp b/shared/OpenglCodecCommon/SocketStream.cpp
index f7a2314..e813142 100644
--- a/shared/OpenglCodecCommon/SocketStream.cpp
+++ b/shared/OpenglCodecCommon/SocketStream.cpp
@@ -112,7 +112,6 @@
 
 const unsigned char *SocketStream::readFully(void *buf, size_t len)
 {
-    const unsigned char* ret = NULL;
     if (!valid()) return NULL;
     if (!buf) {
       return NULL;  // do not allow NULL buf in that implementation
diff --git a/system/GLESv1_enc/GLEncoder.cpp b/system/GLESv1_enc/GLEncoder.cpp
index b698fae..8db973f 100644
--- a/system/GLESv1_enc/GLEncoder.cpp
+++ b/system/GLESv1_enc/GLEncoder.cpp
@@ -752,8 +752,6 @@
         GLenum target, GLenum pname, GLfloat* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
-
     if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
         ctx->override2DTextureTarget(target);
         ctx->m_glGetTexParameterfv_enc(ctx, GL_TEXTURE_2D, pname, params);
@@ -767,7 +765,6 @@
         GLenum target, GLenum pname, GLint* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     switch (pname) {
     case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
@@ -790,7 +787,6 @@
         GLenum target, GLenum pname, GLfixed* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
         ctx->override2DTextureTarget(target);
@@ -824,7 +820,6 @@
         GLenum target, GLenum pname, GLfloat param)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)param)),
@@ -843,7 +838,6 @@
         GLenum target, GLenum pname, const GLfloat* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)params[0])),
@@ -862,7 +856,6 @@
         GLenum target, GLenum pname, GLint param)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)param)),
@@ -881,7 +874,6 @@
         GLenum target, GLenum pname, GLfixed param)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)param)),
@@ -900,7 +892,6 @@
         GLenum target, GLenum pname, const GLint* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)params[0])),
@@ -919,7 +910,6 @@
         GLenum target, GLenum pname, const GLfixed* params)
 {
     GLEncoder* ctx = (GLEncoder*)self;
-    const GLClientState* state = ctx->m_state;
 
     SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
             !isValidTextureExternalParam(pname, (GLenum)params[0])),
diff --git a/system/GLESv2/gl2.cpp b/system/GLESv2/gl2.cpp
index 331da78..0a2bf8e 100644
--- a/system/GLESv2/gl2.cpp
+++ b/system/GLESv2/gl2.cpp
@@ -83,7 +83,7 @@
         GET_CONTEXT;
         ctx->override2DTextureTarget(target);
         ctx->associateEGLImage(target, hostImage);
-        ctx->m_glEGLImageTargetTexture2DOES_enc(self, target, hostImage);
+        ctx->m_glEGLImageTargetTexture2DOES_enc(self, GL_TEXTURE_2D, hostImage);
         ctx->restore2DTextureTarget(target);
     }
 }
diff --git a/system/GLESv2_enc/GL2Encoder.cpp b/system/GLESv2_enc/GL2Encoder.cpp
index cbc0c32..8b07e66 100755
--- a/system/GLESv2_enc/GL2Encoder.cpp
+++ b/system/GLESv2_enc/GL2Encoder.cpp
@@ -426,7 +426,7 @@
     }
     ~ScopedQueryUpdate() {
         GLint hostError = mErrorUpdater.getHostErrorAndUpdate();
-        if (hostError == GL_NO_ERROR) {
+        if (hostError == GL_NO_ERROR && mTarget) {
             memcpy(mTarget, &mBuf[0], mBuf.size());
         }
         mErrorUpdater.updateGuestErrorState();
diff --git a/system/GLESv2_enc/GLESv2Validation.cpp b/system/GLESv2_enc/GLESv2Validation.cpp
index c703325..f736e23 100644
--- a/system/GLESv2_enc/GLESv2Validation.cpp
+++ b/system/GLESv2_enc/GLESv2Validation.cpp
@@ -55,7 +55,6 @@
 
 bool bufferParam(GL2Encoder* ctx, GLenum pname) {
     int glesMajorVersion = ctx->majorVersion();
-    int glesMinorVersion = ctx->minorVersion();
     switch (pname) {
     case GL_BUFFER_SIZE:
     case GL_BUFFER_USAGE:
@@ -289,7 +288,7 @@
     return retval;
 }
 
-bool readPixelsFboFormatMatch(GLenum format, GLenum type, GLenum fboTexType) {
+bool readPixelsFboFormatMatch(GLenum, GLenum type, GLenum fboTexType) {
 #define INVALID_TYPE_MATCH(x, y) \
     if (type == x && fboTexType == y) return false; \
     if (type == y && fboTexType == x) return false; \
@@ -429,6 +428,11 @@
 bool supportedCompressedFormat(GL2Encoder* ctx, GLenum internalformat) {
     int glesMajorVersion = ctx->majorVersion();
     int glesMinorVersion = ctx->minorVersion();
+
+#define COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(internal) \
+    case internal: \
+        return glesMajorVersion > 1 && ctx->hasExtension("GL_KHR_texture_compression_astc_ldr"); \
+
 #define COMPRESSED_TEX_IMAGE_SUPPORT_CASE(internal, maj, min) \
     case internal: \
         if (maj < 3) return true; \
@@ -451,34 +455,34 @@
     COMPRESSED_TEX_IMAGE_SUPPORT_CASE(GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, 2, 0)
     COMPRESSED_TEX_IMAGE_SUPPORT_CASE(GL_COMPRESSED_RGBA8_ETC2_EAC, 2, 0)
     COMPRESSED_TEX_IMAGE_SUPPORT_CASE(GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, 2, 0)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_4x4_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_5x4_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_5x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_6x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_6x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_8x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_8x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_8x8_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_10x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_10x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_10x8_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_10x10_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_12x10_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_RGBA_ASTC_12x12_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR)
-    COMPRESSED_TEX_IMAGE_NOTSUPPORTED(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_4x4_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_5x4_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_5x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_6x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_6x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_8x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_8x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_8x8_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_10x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_10x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_10x8_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_10x10_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_12x10_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_RGBA_ASTC_12x12_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR)
+    COMPRESSED_TEX_IMAGE_SUPPORT_CASE_ASTC(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR)
     default:
         break;
     }
@@ -586,7 +590,7 @@
     return false;
 }
 
-bool depthRenderableFormat(GL2Encoder* ctx, GLenum internalformat) {
+bool depthRenderableFormat(GL2Encoder*, GLenum internalformat) {
     switch (internalformat) {
     case GL_DEPTH_COMPONENT:
     case GL_DEPTH_STENCIL:
@@ -600,7 +604,7 @@
     return false;
 }
 
-bool stencilRenderableFormat(GL2Encoder* ctx, GLenum internalformat) {
+bool stencilRenderableFormat(GL2Encoder*, GLenum internalformat) {
     switch (internalformat) {
     case GL_DEPTH_STENCIL:
     case GL_STENCIL_INDEX8:
diff --git a/system/OpenglSystemCommon/ThreadInfo.cpp b/system/OpenglSystemCommon/ThreadInfo.cpp
index 9e11624..425904e 100644
--- a/system/OpenglSystemCommon/ThreadInfo.cpp
+++ b/system/OpenglSystemCommon/ThreadInfo.cpp
@@ -18,7 +18,9 @@
 
 thread_store_t s_tls = THREAD_STORE_INITIALIZER;
 
-static bool sDefaultTlsDestructorCallback(void* ptr) { return true; }
+static bool sDefaultTlsDestructorCallback(__attribute__((__unused__)) void* ptr) {
+  return true;
+}
 static bool (*sTlsDestructorCallback)(void*) = sDefaultTlsDestructorCallback;
 
 static void tlsDestruct(void *ptr)
diff --git a/system/OpenglSystemCommon/ThreadInfo.h b/system/OpenglSystemCommon/ThreadInfo.h
index 06d9267..57fd5ef 100644
--- a/system/OpenglSystemCommon/ThreadInfo.h
+++ b/system/OpenglSystemCommon/ThreadInfo.h
@@ -21,7 +21,6 @@
 
 #include <bionic_tls.h>
 struct EGLContext_t;
-struct HostConnection;
 
 struct EGLThreadInfo
 {
diff --git a/system/OpenglSystemCommon/goldfish_dma.cpp b/system/OpenglSystemCommon/goldfish_dma.cpp
index ccb8fda..046367d 100644
--- a/system/OpenglSystemCommon/goldfish_dma.cpp
+++ b/system/OpenglSystemCommon/goldfish_dma.cpp
@@ -35,8 +35,8 @@
 int goldfish_dma_create_region(uint32_t sz, struct goldfish_dma_context* res) {
 
     res->fd = qemu_pipe_open("opengles");
-    res->mapped = NULL;
-    res->sz = 0;
+    res->mapped_addr = 0;
+    res->size = 0;
 
     if (res->fd > 0) {
         // now alloc
@@ -52,9 +52,9 @@
             return alloc_res;
         }
 
-        res->sz = sz;
-        ALOGV("%s: successfully allocated goldfish DMA region with size %lu cxt=%p",
-              __FUNCTION__, sz, res);
+        res->size = sz;
+        ALOGV("%s: successfully allocated goldfish DMA region with size %u cxt=%p fd=%d",
+              __FUNCTION__, sz, res, res->fd);
         return 0;
     } else {
         ALOGE("%s: could not obtain fd to device! fd %d errno=%d\n",
@@ -65,31 +65,35 @@
 
 void* goldfish_dma_map(struct goldfish_dma_context* cxt) {
     ALOGV("%s: on fd %d errno=%d", __FUNCTION__, cxt->fd, errno);
-    cxt->mapped = mmap(0, cxt->sz, PROT_WRITE, MAP_SHARED, cxt->fd, 0);
-    ALOGV("%s: mapped addr=%p errno=%d", __FUNCTION__, cxt->mapped, errno);
+    void *mapped = mmap(0, cxt->size, PROT_WRITE, MAP_SHARED, cxt->fd, 0);
+    ALOGV("%s: cxt=%p mapped=%p size=%u errno=%d",
+        __FUNCTION__, cxt, mapped, cxt->size, errno);
 
-    if (cxt->mapped == MAP_FAILED) {
-        cxt->mapped = NULL;
+    if (mapped == MAP_FAILED) {
+        mapped = NULL;
     }
-    return cxt->mapped;
+
+    cxt->mapped_addr = reinterpret_cast<uint64_t>(mapped);
+    return mapped;
 }
 
 int goldfish_dma_unmap(struct goldfish_dma_context* cxt) {
-    munmap(cxt->mapped, cxt->sz);
-    cxt->mapped = NULL;
-    cxt->sz = 0;
+    ALOGV("%s: cxt=%p mapped=0x%08llx", __FUNCTION__, cxt, cxt->mapped_addr);
+    munmap(reinterpret_cast<void *>(cxt->mapped_addr), cxt->size);
+    cxt->mapped_addr = 0;
+    cxt->size = 0;
     return 0;
 }
 
 void goldfish_dma_write(struct goldfish_dma_context* cxt,
-                               void* to_write,
+                               const void* to_write,
                                uint32_t sz) {
-    ALOGV("%s: mapped addr=%p", __FUNCTION__, cxt->mapped);
-    memcpy(cxt->mapped, to_write, sz);
+    ALOGV("%s: cxt=%p mapped=0x%08llx to_write=%p size=%u",
+        __FUNCTION__, cxt, cxt->mapped_addr, to_write, sz);
+    memcpy(reinterpret_cast<void *>(cxt->mapped_addr), to_write, sz);
 }
 
 void goldfish_dma_free(goldfish_dma_context* cxt) {
-    struct goldfish_dma_ioctl_info info;
     close(cxt->fd);
 }
 
diff --git a/system/OpenglSystemCommon/goldfish_dma.h b/system/OpenglSystemCommon/goldfish_dma.h
index 8a09c0e..6c1b09a 100644
--- a/system/OpenglSystemCommon/goldfish_dma.h
+++ b/system/OpenglSystemCommon/goldfish_dma.h
@@ -42,13 +42,9 @@
 
 // userspace interface
 struct goldfish_dma_context {
-    void* mapped;
-#if !defined(__LP64__)
-    int mapped_padding;
-#endif
-    uint64_t sz; // size of reservation
-    int fd;
-    int fd_padding;
+	uint64_t mapped_addr;
+	uint32_t size;
+	int32_t fd;
 };
 
 int goldfish_dma_lock(struct goldfish_dma_context* cxt);
@@ -59,7 +55,7 @@
 int goldfish_dma_unmap(struct goldfish_dma_context* cxt);
 
 void goldfish_dma_write(struct goldfish_dma_context* cxt,
-                        void* to_write,
+                        const void* to_write,
                         uint32_t sz);
 
 void goldfish_dma_free(goldfish_dma_context* cxt);
diff --git a/system/OpenglSystemCommon/qemu_pipe.h b/system/OpenglSystemCommon/qemu_pipe.h
index 47387ab..52f4ee7 100644
--- a/system/OpenglSystemCommon/qemu_pipe.h
+++ b/system/OpenglSystemCommon/qemu_pipe.h
@@ -69,7 +69,7 @@
 {
     char  buff[256];
     int   buffLen;
-    int   fd, ret;
+    int   fd;
 
     if (pipeName == NULL || pipeName[0] == '\0') {
         errno = EINVAL;
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index 16285f9..e52c5bd 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -282,7 +282,7 @@
     void        setTextureTarget(EGLint _texTarget) { texTarget = _texTarget; }
     EGLint      getTextureTarget() { return texTarget; }
 
-    virtual     void setCollectingTimestamps(EGLint collect) { }
+    virtual     void setCollectingTimestamps(EGLint) { }
     virtual     EGLint isCollectingTimestamps() const { return EGL_FALSE; }
     EGLint      deletePending;
     void        setIsCurrent(bool isCurrent) { mIsCurrent = isCurrent; }
@@ -312,8 +312,8 @@
 };
 
 egl_surface_t::egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfaceType)
-    : dpy(dpy), config(config), surfaceType(surfaceType), rcSurface(0),
-      deletePending(0), mIsCurrent(false)
+    : dpy(dpy), config(config), deletePending(0), mIsCurrent(false),
+      surfaceType(surfaceType), rcSurface(0)
 {
     width = 0;
     height = 0;
@@ -712,14 +712,14 @@
     int extStart = 0;
     int extEnd = 0;
     int currentExtIndex = 0;
-    int numExts = 0;
 
     if (sWantES30OrAbove(hostStr) &&
         !strstr(hostStr, kOESEGLImageExternalEssl3)) {
         res.push_back(kOESEGLImageExternalEssl3);
     }
 
-    while (extEnd < strlen(hostStr)) {
+    const int hostStrLen = strlen(hostStr);
+    while (extEnd < hostStrLen) {
         if (hostStr[extEnd] == ' ') {
             int extSz = extEnd - extStart;
             res.push_back(std::string(hostStr + extStart, extSz));
@@ -777,7 +777,7 @@
         std::vector<std::string> exts = getExtStringArray();
 
         int totalSz = 1; // null terminator
-        for (int i = 0; i < exts.size(); i++) {
+        for (unsigned int i = 0; i < exts.size(); i++) {
             totalSz += exts[i].size() + 1; // for space
         }
 
@@ -787,7 +787,7 @@
         memset(hostStr, 0, totalSz);
 
         char* current = hostStr;
-        for (int i = 0; i < exts.size(); i++) {
+        for (unsigned int i = 0; i < exts.size(); i++) {
             memcpy(current, exts[i].c_str(), exts[i].size());
             current += exts[i].size();
             *current = ' ';
@@ -818,6 +818,7 @@
 
 // ----------------------------------------------------------------------------
 
+// Note: C99 syntax was tried here but does not work for all compilers.
 static EGLClient_eglInterface s_eglIface = {
     getThreadInfo: getEGLThreadInfo,
     getGLString: getGLString,
@@ -1103,7 +1104,6 @@
     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
 
-    EGLThreadInfo* tInfo = getEGLThreadInfo();
     egl_surface_t* surface(static_cast<egl_surface_t*>(eglSurface));
     if (surface->isCurrent()) {
         surface->deletePending = 1;
@@ -1436,7 +1436,6 @@
     EGLint minorVersion = 0;
     EGLint context_flags = 0;
     EGLint profile_mask = 0;
-    EGLint reset_notification_strategy = 0;
 
     bool wantedMajorVersion = false;
     bool wantedMinorVersion = false;
@@ -1642,9 +1641,9 @@
         return EGL_TRUE;
     }
 
+    // Destroy surfaces while the previous context is still current.
+    EGLContext_t* prevCtx = tInfo->currentContext;
     if (tInfo->currentContext) {
-        EGLContext_t* prevCtx = tInfo->currentContext;
-
         if (prevCtx->draw) {
             static_cast<egl_surface_t *>(prevCtx->draw)->setIsCurrent(false);
         }
@@ -1652,11 +1651,6 @@
             static_cast<egl_surface_t *>(prevCtx->read)->setIsCurrent(false);
         }
         s_destroyPendingSurfacesInContext(tInfo->currentContext);
-
-        if (prevCtx->deletePending && prevCtx != context) {
-            tInfo->currentContext = 0;
-            eglDestroyContext(dpy, prevCtx);
-        }
     }
 
     if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
@@ -1768,11 +1762,15 @@
 
     }
 
+    // Delete the previous context here
     if (tInfo->currentContext && (tInfo->currentContext != context)) {
         tInfo->currentContext->flags &= ~EGLContext_t::IS_CURRENT;
+        if (tInfo->currentContext->deletePending && tInfo->currentContext != context) {
+            eglDestroyContext(dpy, tInfo->currentContext);
+        }
     }
 
-    //Now make current
+    // Now the new context is current in tInfo
     tInfo->currentContext = context;
 
     //Check maybe we need to init the encoder, if it's first eglMakeCurrent
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index cabc27b..fc68f4e 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -52,6 +52,8 @@
 
 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
 
+#define GOLDFISH_OFFSET_UNIT 8
+
 #ifdef GOLDFISH_HIDL_GRALLOC
 static bool isHidlGralloc = true;
 #else
@@ -64,8 +66,8 @@
 
 uint32_t getAshmemColorOffset(cb_handle_t* cb) {
     uint32_t res = 0;
-    if (cb->canBePosted()) res = sizeof(intptr_t);
-    if (isHidlGralloc) res = sizeof(intptr_t) * 2;
+    if (cb->canBePosted()) res = GOLDFISH_OFFSET_UNIT;
+    if (isHidlGralloc) res = GOLDFISH_OFFSET_UNIT * 2;
     return res;
 }
 
@@ -165,7 +167,7 @@
 
 static void resize_gralloc_dmaregion_locked(uint32_t new_sz) {
     if (!s_grdma) return;
-    if (s_grdma->goldfish_dma.mapped) {
+    if (s_grdma->goldfish_dma.mapped_addr) {
         goldfish_dma_unmap(&s_grdma->goldfish_dma);
     }
     close(s_grdma->goldfish_dma.fd);
@@ -211,7 +213,7 @@
             resize_gralloc_dmaregion_locked(new_sz);
         }
     }
-    if (!s_grdma->goldfish_dma.mapped) {
+    if (!s_grdma->goldfish_dma.mapped_addr) {
         goldfish_dma_map(&s_grdma->goldfish_dma);
     }
     pthread_mutex_unlock(&s_grdma->lock);
@@ -633,12 +635,12 @@
         if (needHostCb || (usage & GRALLOC_USAGE_HW_FB)) {
             // keep space for postCounter
             // AND openCounter for all host cb
-            ashmem_size += sizeof(uint32_t) * 2;
+            ashmem_size += GOLDFISH_OFFSET_UNIT * 2;
         }
     } else {
         if (usage & GRALLOC_USAGE_HW_FB) {
             // keep space for postCounter
-            ashmem_size += sizeof(uint32_t) * 1;
+            ashmem_size += GOLDFISH_OFFSET_UNIT * 1;
         }
     }