Use vndk/window.h instead of the system one

Bug: b/159763893
Change-Id: I9c662b726ccd77d8974cefb8bacfee8994f8c7b1
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/48829
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Trevor Black <vantablack@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Yiwei Zhang <zzyiwei@google.com>
diff --git a/include/Android/system/window.h b/include/Android/system/window.h
deleted file mode 100644
index f986274..0000000
--- a/include/Android/system/window.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2018 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
-
-#include <nativebase/nativebase.h>
-#include <system/graphics.h>
-
-#define ANDROID_NATIVE_WINDOW_MAGIC ANDROID_NATIVE_MAKE_CONSTANT('_', 'w', 'n', 'd')
-
-enum {
-    NATIVE_WINDOW_WIDTH = 0,
-    NATIVE_WINDOW_HEIGHT = 1,
-};
-
-struct ANativeWindow {
-    ANativeWindow() : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0) {
-        common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
-        common.version = sizeof(ANativeWindowBuffer);
-        memset(common.reserved, 0, sizeof(common.reserved));
-    }
-
-    android_native_base_t common;
-
-    const uint32_t flags;
-    const int minSwapInterval;
-    const int maxSwapInterval;
-    const float xdpi;
-    const float ydpi;
-    intptr_t oem[4];
-
-    int (*setSwapInterval)(ANativeWindow*, int);
-    int (*dequeueBuffer_DEPRECATED)(ANativeWindow*, ANativeWindowBuffer**);
-    int (*lockBuffer_DEPRECATED)(ANativeWindow*, ANativeWindowBuffer*);
-    int (*queueBuffer_DEPRECATED)(ANativeWindow*, ANativeWindowBuffer*);
-    int (*query)(const ANativeWindow*, int, int*);
-    int (*perform)(ANativeWindow*, int, ...);
-    int (*cancelBuffer_DEPRECATED)(ANativeWindow*, ANativeWindowBuffer*);
-    int (*dequeueBuffer)(ANativeWindow*, ANativeWindowBuffer**, int*);
-    int (*queueBuffer)(ANativeWindow*, ANativeWindowBuffer*, int);
-    int (*cancelBuffer)(ANativeWindow*, ANativeWindowBuffer*, int);
-};
-
-static inline int native_window_set_usage(ANativeWindow*, uint64_t) {
-    // No-op
-    return 0;
-}
-
-static inline int native_window_dequeue_buffer_and_wait(ANativeWindow* anw,
-                                                        ANativeWindowBuffer** anwb) {
-    return anw->dequeueBuffer_DEPRECATED(anw, anwb);
-}
diff --git a/include/Android/vndk/window.h b/include/Android/vndk/window.h
new file mode 100644
index 0000000..8b5965f
--- /dev/null
+++ b/include/Android/vndk/window.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2020 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
+
+#include <nativebase/nativebase.h>
+
+struct ANativeWindow;
+typedef struct ANativeWindow ANativeWindow;
+
+void ANativeWindow_acquire(ANativeWindow* window);
+void ANativeWindow_release(ANativeWindow* window);
+int32_t ANativeWindow_getWidth(ANativeWindow* window);
+int32_t ANativeWindow_getHeight(ANativeWindow* window);
+int ANativeWindow_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd);
+int ANativeWindow_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
+int ANativeWindow_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
+int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage);
diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp
index 345a0b6..5ff1b41 100644
--- a/src/Main/FrameBufferAndroid.cpp
+++ b/src/Main/FrameBufferAndroid.cpp
@@ -16,7 +16,9 @@
 
 #ifndef ANDROID_NDK_BUILD
 #include "Common/GrallocAndroid.hpp"
-#include <system/window.h>
+#include <sync/sync.h>
+#include <system/graphics.h>
+#include <vndk/window.h>
 #else
 #include <android/native_window.h>
 #endif
@@ -26,29 +28,22 @@
 #if !defined(ANDROID_NDK_BUILD)
 	inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
 	{
-		#if ANDROID_PLATFORM_SDK_VERSION > 16
-			return native_window_dequeue_buffer_and_wait(window, buffer);
-		#else
-			return window->dequeueBuffer(window, buffer);
-		#endif
+		int fenceFd = -1;
+		int ret = ANativeWindow_dequeueBuffer(window, buffer, &fenceFd);
+		if (ret || fenceFd < 0) return ret;
+		sync_wait(fenceFd, -1 /* forever */);
+		close(fenceFd);
+		return ret;
 	}
 
 	inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
 	{
-		#if ANDROID_PLATFORM_SDK_VERSION > 16
-			return window->queueBuffer(window, buffer, fenceFd);
-		#else
-			return window->queueBuffer(window, buffer);
-		#endif
+		return ANativeWindow_queueBuffer(window, buffer, fenceFd);
 	}
 
 	inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
 	{
-		#if ANDROID_PLATFORM_SDK_VERSION > 16
-			return window->cancelBuffer(window, buffer, fenceFd);
-		#else
-			return window->cancelBuffer(window, buffer);
-		#endif
+		return ANativeWindow_cancelBuffer(window, buffer, fenceFd);
 	}
 #endif // !defined(ANDROID_NDK_BUILD)
 
@@ -57,15 +52,15 @@
 			nativeWindow(window), buffer(nullptr)
 	{
 #ifndef ANDROID_NDK_BUILD
-		nativeWindow->common.incRef(&nativeWindow->common);
-		native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
+		ANativeWindow_acquire(nativeWindow);
+		ANativeWindow_setUsage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
 #endif
 	}
 
 	FrameBufferAndroid::~FrameBufferAndroid()
 	{
 #ifndef ANDROID_NDK_BUILD
-		nativeWindow->common.decRef(&nativeWindow->common);
+		ANativeWindow_release(nativeWindow);
 #endif
 	}
 
diff --git a/src/OpenGL/common/Image.hpp b/src/OpenGL/common/Image.hpp
index 7fb0817..d0cec58 100644
--- a/src/OpenGL/common/Image.hpp
+++ b/src/OpenGL/common/Image.hpp
@@ -22,7 +22,8 @@
 #include <GLES2/gl2ext.h>
 
 #if defined(__ANDROID__) && !defined(ANDROID_NDK_BUILD)
-#include <system/window.h>
+#include <system/graphics.h>
+#include <vndk/window.h>
 #include "../../Common/GrallocAndroid.hpp"
 #endif
 
diff --git a/src/OpenGL/libEGL/Display.cpp b/src/OpenGL/libEGL/Display.cpp
index 426769e..5138b60 100644
--- a/src/OpenGL/libEGL/Display.cpp
+++ b/src/OpenGL/libEGL/Display.cpp
@@ -26,7 +26,7 @@
 #include "Common/RecursiveLock.hpp"
 
 #if defined(__ANDROID__) && !defined(ANDROID_NDK_BUILD)
-#include <system/window.h>
+#include <vndk/window.h>
 #include <sys/ioctl.h>
 #include <linux/fb.h>
 #include <fcntl.h>
@@ -674,13 +674,6 @@
 			ERR("%s called with window==NULL %s:%d", __FUNCTION__, __FILE__, __LINE__);
 			return false;
 		}
-	#if !defined(ANDROID_NDK_BUILD)
-		if(static_cast<ANativeWindow*>(window)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC)
-		{
-			ERR("%s called with window==%p bad magic %s:%d", __FUNCTION__, window, __FILE__, __LINE__);
-			return false;
-		}
-	#endif // !defined(ANDROID_NDK_BUILD)
 		return true;
 	#elif defined(USE_X11)
 		if(nativeDisplay)
diff --git a/src/OpenGL/libEGL/Surface.cpp b/src/OpenGL/libEGL/Surface.cpp
index 17fc234..f93065a 100644
--- a/src/OpenGL/libEGL/Surface.cpp
+++ b/src/OpenGL/libEGL/Surface.cpp
@@ -344,13 +344,8 @@
 		int windowWidth = client.right - client.left;
 		int windowHeight = client.bottom - client.top;
 	#elif defined(__ANDROID__)
-	#ifdef ANDROID_NDK_BUILD
 		int windowWidth = ANativeWindow_getWidth(window);
 		int windowHeight = ANativeWindow_getHeight(window);
-	#else
-		int windowWidth;  window->query(window, NATIVE_WINDOW_WIDTH, &windowWidth);
-		int windowHeight; window->query(window, NATIVE_WINDOW_HEIGHT, &windowHeight);
-	#endif
 	#elif defined(USE_X11)
 		XWindowAttributes windowAttributes;
 		Status status = libX11->XGetWindowAttributes((::Display*)display->getNativeDisplay(), window, &windowAttributes);
diff --git a/src/OpenGL/libEGL/libEGL.cpp b/src/OpenGL/libEGL/libEGL.cpp
index b0eeddc..c38cb7e 100644
--- a/src/OpenGL/libEGL/libEGL.cpp
+++ b/src/OpenGL/libEGL/libEGL.cpp
@@ -24,7 +24,7 @@
 #include "Common/Version.h"
 
 #if defined(__ANDROID__) && !defined(ANDROID_NDK_BUILD)
-#include <system/window.h>
+#include <vndk/window.h>
 #elif defined(USE_X11)
 #include "Main/libX11.hpp"
 #endif