From bae716bc153962c3ac79660bf32e5c50f0de343d Mon Sep 17 00:00:00 2001 From: Jamie Gennis Date: Mon, 14 Mar 2011 15:34:04 -0700 Subject: [PATCH] SurfaceTexture: disallow unsupported uses. This change makes the ANativeWindow_lock NDK function error out if it is passed an ANativeWindow with a concrete type that is not Surface. It also makes eglCreateWindowSurface fail if it is passed a SurfaceTextureClient as its 'window' argument. Bug: 4087277 Change-Id: Ie68c50c52d88f72d8a387f6c094908044c83a88c --- libs/gui/tests/SurfaceTextureClient_test.cpp | 45 +++++++++++++++++++- native/android/native_window.cpp | 6 +++ opengl/libs/EGL/egl.cpp | 10 +++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libs/gui/tests/SurfaceTextureClient_test.cpp b/libs/gui/tests/SurfaceTextureClient_test.cpp index 25109f6cbf2f3..94b05bc2d3ca2 100644 --- a/libs/gui/tests/SurfaceTextureClient_test.cpp +++ b/libs/gui/tests/SurfaceTextureClient_test.cpp @@ -14,8 +14,9 @@ * limitations under the License. */ -#include +#include #include +#include namespace android { @@ -57,4 +58,46 @@ TEST_F(SurfaceTextureClientTest, ConcreteTypeIsSurfaceTextureClient) { EXPECT_EQ(NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT, result); } +TEST_F(SurfaceTextureClientTest, ANativeWindowLockFails) { + sp anw(mSTC); + ANativeWindow_Buffer buf; + ASSERT_EQ(BAD_VALUE, ANativeWindow_lock(anw.get(), &buf, NULL)); +} + +TEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceFails) { + sp anw(mSTC); + + EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + ASSERT_EQ(EGL_SUCCESS, eglGetError()); + ASSERT_NE(EGL_NO_DISPLAY, dpy); + + EGLint majorVersion; + EGLint minorVersion; + EXPECT_TRUE(eglInitialize(dpy, &majorVersion, &minorVersion)); + ASSERT_EQ(EGL_SUCCESS, eglGetError()); + + EGLConfig myConfig = {0}; + EGLint numConfigs = 0; + EGLint configAttribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_ALPHA_SIZE, 8, + EGL_DEPTH_SIZE, 16, + EGL_STENCIL_SIZE, 8, + EGL_NONE }; + EXPECT_TRUE(eglChooseConfig(dpy, configAttribs, &myConfig, 1, + &numConfigs)); + ASSERT_EQ(EGL_SUCCESS, eglGetError()); + + EGLSurface eglSurface = eglCreateWindowSurface(dpy, myConfig, anw.get(), + NULL); + ASSERT_EQ(EGL_NO_SURFACE, eglSurface); + ASSERT_EQ(EGL_BAD_NATIVE_WINDOW, eglGetError()); + + eglTerminate(dpy); +} + } diff --git a/native/android/native_window.cpp b/native/android/native_window.cpp index 219cd196daf23..ae1993deaea84 100644 --- a/native/android/native_window.cpp +++ b/native/android/native_window.cpp @@ -74,6 +74,12 @@ int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width, int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) { + int type = -1; + if (window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0 || + type != NATIVE_WINDOW_SURFACE) { + return BAD_VALUE; + } + Region dirtyRegion; Region* dirtyParam = NULL; if (inOutDirtyBounds != NULL) { diff --git a/opengl/libs/EGL/egl.cpp b/opengl/libs/EGL/egl.cpp index 3d5a4d16f6e19..f4a16509ed288 100644 --- a/opengl/libs/EGL/egl.cpp +++ b/opengl/libs/EGL/egl.cpp @@ -1094,6 +1094,16 @@ EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, EGLConfig iConfig = dp->configs[intptr_t(config)].config; EGLint format; + // for now fail if the window is not a Surface. + int type = -1; + ANativeWindow* anw = reinterpret_cast(window); + if ((anw->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0) || + (type == NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT)) { + LOGE("native window is a SurfaceTextureClient (currently " + "unsupported)"); + return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); + } + // set the native window's buffers format to match this config if (cnx->egl.eglGetConfigAttrib(iDpy, iConfig, EGL_NATIVE_VISUAL_ID, &format)) {