• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // EGLX11VisualTest.cpp: tests for EGL_ANGLE_x11_visual extension
8 
9 #include <gtest/gtest.h>
10 
11 #include <EGL/egl.h>
12 #include <EGL/eglext.h>
13 #include <X11/Xlib.h>
14 
15 #include "test_utils/ANGLETest.h"
16 #include "util/OSWindow.h"
17 #include "util/linux/x11/X11Window.h"
18 
19 using namespace angle;
20 
21 namespace
22 {
23 
24 const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
25 }
26 
27 class EGLX11VisualHintTest : public ANGLETest<>
28 {
29   public:
testSetUp()30     void testSetUp() override { mDisplay = XOpenDisplay(nullptr); }
31 
getDisplayAttributes(int visualId) const32     std::vector<EGLint> getDisplayAttributes(int visualId) const
33     {
34         std::vector<EGLint> attribs;
35 
36         attribs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);
37         attribs.push_back(GetParam().getRenderer());
38         attribs.push_back(EGL_X11_VISUAL_ID_ANGLE);
39         attribs.push_back(visualId);
40         attribs.push_back(EGL_NONE);
41 
42         return attribs;
43     }
44 
chooseDifferentVisual(unsigned int visualId)45     unsigned int chooseDifferentVisual(unsigned int visualId)
46     {
47         int numVisuals;
48         XVisualInfo visualTemplate;
49         visualTemplate.screen = DefaultScreen(mDisplay);
50 
51         XVisualInfo *visuals =
52             XGetVisualInfo(mDisplay, VisualScreenMask, &visualTemplate, &numVisuals);
53         EXPECT_TRUE(numVisuals >= 2);
54 
55         for (int i = 0; i < numVisuals; ++i)
56         {
57             if (visuals[i].visualid != visualId)
58             {
59                 int result = visuals[i].visualid;
60                 XFree(visuals);
61                 return result;
62             }
63         }
64 
65         EXPECT_TRUE(false);
66         return -1;
67     }
68 
69   protected:
70     Display *mDisplay;
71 };
72 
73 // Test that display creation fails if the visual ID passed in invalid.
TEST_P(EGLX11VisualHintTest,InvalidVisualID)74 TEST_P(EGLX11VisualHintTest, InvalidVisualID)
75 {
76     static const int gInvalidVisualId = -1;
77     auto attributes                   = getDisplayAttributes(gInvalidVisualId);
78 
79     EGLDisplay display = eglGetPlatformDisplayEXT(
80         EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),
81         attributes.data());
82     ASSERT_TRUE(display != EGL_NO_DISPLAY);
83 
84     ASSERT_TRUE(EGL_FALSE == eglInitialize(display, nullptr, nullptr));
85     ASSERT_EGL_ERROR(EGL_NOT_INITIALIZED);
86 }
87 
88 // Test that context creation with a visual ID succeeds, that the context exposes
89 // only one config, and that a clear on a surface with this config works.
TEST_P(EGLX11VisualHintTest,ValidVisualIDAndClear)90 TEST_P(EGLX11VisualHintTest, ValidVisualIDAndClear)
91 {
92     // We'll test the extension with one visual ID but we don't care which one. This means we
93     // can use OSWindow to create a window and just grab its visual.
94     OSWindow *osWindow = OSWindow::New();
95     osWindow->initialize("EGLX11VisualHintTest", 500, 500);
96     setWindowVisible(osWindow, true);
97 
98     Window xWindow = osWindow->getNativeWindow();
99 
100     XWindowAttributes windowAttributes;
101     ASSERT_NE(0, XGetWindowAttributes(mDisplay, xWindow, &windowAttributes));
102     int visualId = windowAttributes.visual->visualid;
103 
104     auto attributes    = getDisplayAttributes(visualId);
105     EGLDisplay display = eglGetPlatformDisplayEXT(
106         EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),
107         attributes.data());
108     ASSERT_NE(EGL_NO_DISPLAY, display);
109 
110     ASSERT_TRUE(EGL_TRUE == eglInitialize(display, nullptr, nullptr));
111 
112     int nConfigs = 0;
113     ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, nullptr, 0, &nConfigs));
114     ASSERT_GE(nConfigs, 1);
115 
116     int nReturnedConfigs = 0;
117     std::vector<EGLConfig> configs(nConfigs);
118     ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, configs.data(), nConfigs, &nReturnedConfigs));
119     ASSERT_EQ(nConfigs, nReturnedConfigs);
120 
121     for (EGLConfig config : configs)
122     {
123         EGLint eglNativeId;
124         ASSERT_TRUE(EGL_TRUE ==
125                     eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &eglNativeId));
126         ASSERT_EQ(visualId, eglNativeId);
127 
128         // Finally, try to do a clear on the window.
129         EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
130         ASSERT_NE(EGL_NO_CONTEXT, context);
131 
132         EGLSurface window = eglCreateWindowSurface(display, config, xWindow, nullptr);
133         ASSERT_EGL_SUCCESS();
134 
135         eglMakeCurrent(display, window, window, context);
136         ASSERT_EGL_SUCCESS();
137 
138         glViewport(0, 0, 500, 500);
139         glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
140         glClear(GL_COLOR_BUFFER_BIT);
141         ASSERT_GL_NO_ERROR();
142         EXPECT_PIXEL_EQ(250, 250, 0, 0, 255, 255);
143 
144         // Teardown
145         eglDestroySurface(display, window);
146         ASSERT_EGL_SUCCESS();
147 
148         eglDestroyContext(display, context);
149         ASSERT_EGL_SUCCESS();
150 
151         eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
152         ASSERT_EGL_SUCCESS();
153     }
154 
155     OSWindow::Delete(&osWindow);
156 
157     eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
158     eglTerminate(display);
159 }
160 
161 // Test that a child window is created when trying to create an EGL window from
162 // an X11 window whose visual ID doesn't match the visual ID passed at display creation.
TEST_P(EGLX11VisualHintTest,InvalidWindowVisualID)163 TEST_P(EGLX11VisualHintTest, InvalidWindowVisualID)
164 {
165     // Get the default visual ID, as a good guess of a visual id for which display
166     // creation will succeed.
167     int visualId;
168     {
169         OSWindow *osWindow = OSWindow::New();
170         osWindow->initialize("EGLX11VisualHintTest", 500, 500);
171         setWindowVisible(osWindow, true);
172 
173         Window xWindow = osWindow->getNativeWindow();
174 
175         XWindowAttributes windowAttributes;
176         ASSERT_NE(0, XGetWindowAttributes(mDisplay, xWindow, &windowAttributes));
177         visualId = windowAttributes.visual->visualid;
178 
179         OSWindow::Delete(&osWindow);
180     }
181 
182     auto attributes    = getDisplayAttributes(visualId);
183     EGLDisplay display = eglGetPlatformDisplayEXT(
184         EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),
185         attributes.data());
186     ASSERT_NE(EGL_NO_DISPLAY, display);
187 
188     ASSERT_TRUE(EGL_TRUE == eglInitialize(display, nullptr, nullptr));
189 
190     // Initialize the window with a visual id different from the display's visual id
191     int otherVisualId = chooseDifferentVisual(visualId);
192     ASSERT_NE(visualId, otherVisualId);
193 
194     OSWindow *osWindow = new X11Window(otherVisualId);
195     osWindow->initialize("EGLX11VisualHintTest", 500, 500);
196     setWindowVisible(osWindow, true);
197 
198     Window xWindow = osWindow->getNativeWindow();
199 
200     // Creating the EGL window should succeed
201     int nReturnedConfigs = 0;
202     EGLConfig config;
203     ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, &config, 1, &nReturnedConfigs));
204     ASSERT_EQ(1, nReturnedConfigs);
205 
206     EGLSurface window = eglCreateWindowSurface(display, config, xWindow, nullptr);
207     ASSERT_TRUE(window);
208     ASSERT_EGL_SUCCESS();
209 
210     // When trying to create a window with a visual other than the one specified
211     // with EGL_X11_VISUAL_ID_ANGLE, ANGLE should fallback to using a child window.
212     Window root;
213     Window parent;
214     Window *children;
215     unsigned int nchildren;
216     XQueryTree(mDisplay, xWindow, &root, &parent, &children, &nchildren);
217     EXPECT_EQ(nchildren, 1U);
218     XFree(children);
219 
220     OSWindow::Delete(&osWindow);
221 }
222 
223 ANGLE_INSTANTIATE_TEST(EGLX11VisualHintTest, WithNoFixture(ES2_OPENGL()));
224