check that the NativeWindowType is valid in eglCreateWindowSurface

This will avoid certain crash (null dereference),
and return an error instead. This is useful for
app developers as java language apps are not
expected to crash when passed bad arguments

This doesn’t fix bug 36065206, but was prompted
by it.

Test: compile / run
Bug: 36065206
Change-Id: I90d25730b3dd292d756b4c38b51320a466fa6d48
diff --git a/opengl/libs/EGL/eglApi.cpp b/opengl/libs/EGL/eglApi.cpp
index e52713f..aed27c8 100644
--- a/opengl/libs/EGL/eglApi.cpp
+++ b/opengl/libs/EGL/eglApi.cpp
@@ -478,6 +478,16 @@
     if (dp) {
         EGLDisplay iDpy = dp->disp.dpy;
 
+        if (!window) {
+            return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
+        }
+
+        int value = 0;
+        window->query(window, NATIVE_WINDOW_IS_VALID, &value);
+        if (!value) {
+            return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
+        }
+
         int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
         if (result != OK) {
             ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "