merge in oc-release history after reset to master
diff --git a/Android.mk b/Android.mk
index 566e737..9061c9b 100644
--- a/Android.mk
+++ b/Android.mk
@@ -73,6 +73,7 @@
 include $(EMUGL_PATH)/system/GLESv2/Android.mk
 
 include $(EMUGL_PATH)/system/gralloc/Android.mk
+include $(EMUGL_PATH)/system/surfaceInterface/Android.mk
 include $(EMUGL_PATH)/system/egl/Android.mk
 
 endif # BUILD_EMULATOR_OPENGL == true
diff --git a/system/OpenglSystemCommon/Android.mk b/system/OpenglSystemCommon/Android.mk
index 9853f29..b585971 100644
--- a/system/OpenglSystemCommon/Android.mk
+++ b/system/OpenglSystemCommon/Android.mk
@@ -5,6 +5,7 @@
 
 LOCAL_SRC_FILES := \
     goldfish_dma.cpp \
+    goldfishHwc2.cpp \
     FormatConversions.cpp \
     HostConnection.cpp \
     ProcessPipe.cpp    \
diff --git a/system/OpenglSystemCommon/goldfishHwc2.cpp b/system/OpenglSystemCommon/goldfishHwc2.cpp
new file mode 100644
index 0000000..888ba89
--- /dev/null
+++ b/system/OpenglSystemCommon/goldfishHwc2.cpp
@@ -0,0 +1,22 @@
+/*
+* Copyright (C) 2017 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+#include "goldfishHwc2.h"
+
+// Stub functions if not using HWC2.
+#ifndef USE_HWC2
+extern "C" void surfaceInterface_init() { }
+extern "C" void surfaceInterface_setAsyncModeForWindow(void* window) { }
+#endif
diff --git a/system/OpenglSystemCommon/goldfishHwc2.h b/system/OpenglSystemCommon/goldfishHwc2.h
new file mode 100644
index 0000000..cb1a396
--- /dev/null
+++ b/system/OpenglSystemCommon/goldfishHwc2.h
@@ -0,0 +1,25 @@
+/*
+* Copyright (C) 2017 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+#pragma once
+
+// Set of functions that help support HWC2 in the emulator.
+
+#ifdef USE_HWC2
+#include "../surfaceInterface/surfaceInterface.h"
+#else
+extern "C" void surfaceInterface_init();
+extern "C" void surfaceInterface_setAsyncModeForWindow(void* window);
+#endif
diff --git a/system/egl/Android.mk b/system/egl/Android.mk
index f7dcc70..37aadd7 100644
--- a/system/egl/Android.mk
+++ b/system/egl/Android.mk
@@ -13,8 +13,12 @@
     egl.cpp \
     ClientAPIExts.cpp
 
-LOCAL_SHARED_LIBRARIES += libdl
+ifeq ($(TARGET_USES_HWC2), true)
+    LOCAL_CFLAGS += -DUSE_HWC2
+    LOCAL_STATIC_LIBRARIES += libsurfaceInterface
+endif
 
+LOCAL_SHARED_LIBRARIES += libdl
 # Used to access the Bionic private OpenGL TLS slot
 LOCAL_C_INCLUDES += bionic/libc/private
 
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index 8273cb5..c00d380 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -29,6 +29,7 @@
 #include "eglContext.h"
 #include "ClientAPIExts.h"
 #include "EGLImage.h"
+#include "goldfishHwc2.h"
 #include "ProcessPipe.h"
 
 #include "GLEncoder.h"
@@ -367,6 +368,8 @@
     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface,
             ((cb_handle_t*)(buffer->handle))->hostHandle);
 
+    surfaceInterface_setAsyncModeForWindow((void*)nativeWindow);
+
     return EGL_TRUE;
 }
 
diff --git a/system/egl/eglDisplay.cpp b/system/egl/eglDisplay.cpp
index 68ead6b..2239e99 100644
--- a/system/egl/eglDisplay.cpp
+++ b/system/egl/eglDisplay.cpp
@@ -15,6 +15,8 @@
 */
 #include "eglDisplay.h"
 #include "HostConnection.h"
+#include "goldfishHwc2.h"
+
 #include <dlfcn.h>
 
 #include <string>
@@ -70,6 +72,7 @@
     pthread_mutex_init(&m_lock, NULL);
     pthread_mutex_init(&m_ctxLock, NULL);
     pthread_mutex_init(&m_surfaceLock, NULL);
+    surfaceInterface_init();
 }
 
 eglDisplay::~eglDisplay()
diff --git a/system/surfaceInterface/Android.mk b/system/surfaceInterface/Android.mk
new file mode 100644
index 0000000..db62d41
--- /dev/null
+++ b/system/surfaceInterface/Android.mk
@@ -0,0 +1,17 @@
+ifneq (false,$(BUILD_EMULATOR_OPENGL_DRIVER))
+
+ifeq ($(TARGET_USES_HWC2), true)
+
+LOCAL_PATH := $(call my-dir)
+
+$(call emugl-begin-static-library,libsurfaceInterface)
+$(call emugl-import,libOpenglSystemCommon)
+
+LOCAL_SRC_FILES := surfaceInterface.cpp
+LOCAL_SHARED_LIBRARIES := libgui
+
+$(call emugl-end-module)
+
+endif
+
+endif # BUILD_EMULATOR_OPENGL_DRIVER != false
diff --git a/system/surfaceInterface/surfaceInterface.cpp b/system/surfaceInterface/surfaceInterface.cpp
new file mode 100644
index 0000000..68f9110
--- /dev/null
+++ b/system/surfaceInterface/surfaceInterface.cpp
@@ -0,0 +1,36 @@
+#include "surfaceInterface.h"
+
+#include <cutils/log.h>
+#include <gui/Surface.h>
+
+class SurfaceInterface : public android::ANativeObjectBase<
+                                    ANativeWindow,
+                                    android::Surface,
+                                    android::RefBase> {
+public:
+    static SurfaceInterface* get();
+    void setAsyncMode(ANativeWindow* anw, bool async) {
+        ALOGD("SurfaceInterface::%s: set async mode %d", __func__, async);
+        window = anw;
+        android::Surface* s = android::Surface::getSelf(window);
+        s->setAsyncMode(async);
+        window = NULL;
+    }
+    ANativeWindow* window;
+};
+
+static SurfaceInterface* sSurfaceInterface = NULL;
+
+SurfaceInterface* SurfaceInterface::get() {
+    if (!sSurfaceInterface)
+        sSurfaceInterface = new SurfaceInterface;
+    return sSurfaceInterface;
+}
+
+extern "C" void surfaceInterface_init() {
+    SurfaceInterface::get();
+}
+
+extern "C" void surfaceInterface_setAsyncModeForWindow(void* window) {
+    SurfaceInterface::get()->setAsyncMode((ANativeWindow*)window, true);
+}
diff --git a/system/surfaceInterface/surfaceInterface.h b/system/surfaceInterface/surfaceInterface.h
new file mode 100644
index 0000000..6890847
--- /dev/null
+++ b/system/surfaceInterface/surfaceInterface.h
@@ -0,0 +1,25 @@
+/*
+* Copyright (C) 2017 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+#pragma once
+
+#if PLATFORM_SDK_VERSION >= 16
+#include <system/window.h>
+#else // PLATFORM_SDK_VERSION >= 16
+#include <private/ui/android_natives_priv.h>
+#endif // PLATFORM_SDK_VERSION >= 16
+
+extern "C" void surfaceInterface_init();
+extern "C" void surfaceInterface_setAsyncModeForWindow(void* window);