Path: blob/main_old/src/tests/egl_tests/EGLX11VisualTest.cpp
1693 views
//1// Copyright 2015 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// EGLX11VisualTest.cpp: tests for EGL_ANGLE_x11_visual extension78#include <gtest/gtest.h>910#include <EGL/egl.h>11#include <EGL/eglext.h>12#include <X11/Xlib.h>1314#include "test_utils/ANGLETest.h"15#include "util/OSWindow.h"16#include "util/x11/X11Window.h"1718using namespace angle;1920namespace21{2223const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};24}2526class EGLX11VisualHintTest : public ANGLETest27{28public:29void testSetUp() override { mDisplay = XOpenDisplay(nullptr); }3031std::vector<EGLint> getDisplayAttributes(int visualId) const32{33std::vector<EGLint> attribs;3435attribs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE);36attribs.push_back(GetParam().getRenderer());37attribs.push_back(EGL_X11_VISUAL_ID_ANGLE);38attribs.push_back(visualId);39attribs.push_back(EGL_NONE);4041return attribs;42}4344unsigned int chooseDifferentVisual(unsigned int visualId)45{46int numVisuals;47XVisualInfo visualTemplate;48visualTemplate.screen = DefaultScreen(mDisplay);4950XVisualInfo *visuals =51XGetVisualInfo(mDisplay, VisualScreenMask, &visualTemplate, &numVisuals);52EXPECT_TRUE(numVisuals >= 2);5354for (int i = 0; i < numVisuals; ++i)55{56if (visuals[i].visualid != visualId)57{58int result = visuals[i].visualid;59XFree(visuals);60return result;61}62}6364EXPECT_TRUE(false);65return -1;66}6768protected:69Display *mDisplay;70};7172// Test that display creation fails if the visual ID passed in invalid.73TEST_P(EGLX11VisualHintTest, InvalidVisualID)74{75static const int gInvalidVisualId = -1;76auto attributes = getDisplayAttributes(gInvalidVisualId);7778EGLDisplay display = eglGetPlatformDisplayEXT(79EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),80attributes.data());81ASSERT_TRUE(display != EGL_NO_DISPLAY);8283ASSERT_TRUE(EGL_FALSE == eglInitialize(display, nullptr, nullptr));84ASSERT_EGL_ERROR(EGL_NOT_INITIALIZED);85}8687// Test that context creation with a visual ID succeeds, that the context exposes88// only one config, and that a clear on a surface with this config works.89TEST_P(EGLX11VisualHintTest, ValidVisualIDAndClear)90{91// We'll test the extension with one visual ID but we don't care which one. This means we92// can use OSWindow to create a window and just grab its visual.93OSWindow *osWindow = OSWindow::New();94osWindow->initialize("EGLX11VisualHintTest", 500, 500);95setWindowVisible(osWindow, true);9697Window xWindow = osWindow->getNativeWindow();9899XWindowAttributes windowAttributes;100ASSERT_NE(0, XGetWindowAttributes(mDisplay, xWindow, &windowAttributes));101int visualId = windowAttributes.visual->visualid;102103auto attributes = getDisplayAttributes(visualId);104EGLDisplay display = eglGetPlatformDisplayEXT(105EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),106attributes.data());107ASSERT_NE(EGL_NO_DISPLAY, display);108109ASSERT_TRUE(EGL_TRUE == eglInitialize(display, nullptr, nullptr));110111int nConfigs = 0;112ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, nullptr, 0, &nConfigs));113ASSERT_GE(nConfigs, 1);114115int nReturnedConfigs = 0;116std::vector<EGLConfig> configs(nConfigs);117ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, configs.data(), nConfigs, &nReturnedConfigs));118ASSERT_EQ(nConfigs, nReturnedConfigs);119120for (EGLConfig config : configs)121{122EGLint eglNativeId;123ASSERT_TRUE(EGL_TRUE ==124eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &eglNativeId));125ASSERT_EQ(visualId, eglNativeId);126127// Finally, try to do a clear on the window.128EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);129ASSERT_NE(EGL_NO_CONTEXT, context);130131EGLSurface window = eglCreateWindowSurface(display, config, xWindow, nullptr);132ASSERT_EGL_SUCCESS();133134eglMakeCurrent(display, window, window, context);135ASSERT_EGL_SUCCESS();136137glViewport(0, 0, 500, 500);138glClearColor(0.0f, 0.0f, 1.0f, 1.0f);139glClear(GL_COLOR_BUFFER_BIT);140ASSERT_GL_NO_ERROR();141EXPECT_PIXEL_EQ(250, 250, 0, 0, 255, 255);142143// Teardown144eglDestroySurface(display, window);145ASSERT_EGL_SUCCESS();146147eglDestroyContext(display, context);148ASSERT_EGL_SUCCESS();149150eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);151ASSERT_EGL_SUCCESS();152}153154OSWindow::Delete(&osWindow);155156eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);157eglTerminate(display);158}159160// Test that a child window is created when trying to create an EGL window from161// an X11 window whose visual ID doesn't match the visual ID passed at display creation.162TEST_P(EGLX11VisualHintTest, InvalidWindowVisualID)163{164// Get the default visual ID, as a good guess of a visual id for which display165// creation will succeed.166int visualId;167{168OSWindow *osWindow = OSWindow::New();169osWindow->initialize("EGLX11VisualHintTest", 500, 500);170setWindowVisible(osWindow, true);171172Window xWindow = osWindow->getNativeWindow();173174XWindowAttributes windowAttributes;175ASSERT_NE(0, XGetWindowAttributes(mDisplay, xWindow, &windowAttributes));176visualId = windowAttributes.visual->visualid;177178OSWindow::Delete(&osWindow);179}180181auto attributes = getDisplayAttributes(visualId);182EGLDisplay display = eglGetPlatformDisplayEXT(183EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<_XDisplay *>(EGL_DEFAULT_DISPLAY),184attributes.data());185ASSERT_NE(EGL_NO_DISPLAY, display);186187ASSERT_TRUE(EGL_TRUE == eglInitialize(display, nullptr, nullptr));188189// Initialize the window with a visual id different from the display's visual id190int otherVisualId = chooseDifferentVisual(visualId);191ASSERT_NE(visualId, otherVisualId);192193OSWindow *osWindow = new X11Window(otherVisualId);194osWindow->initialize("EGLX11VisualHintTest", 500, 500);195setWindowVisible(osWindow, true);196197Window xWindow = osWindow->getNativeWindow();198199// Creating the EGL window should succeed200int nReturnedConfigs = 0;201EGLConfig config;202ASSERT_TRUE(EGL_TRUE == eglGetConfigs(display, &config, 1, &nReturnedConfigs));203ASSERT_EQ(1, nReturnedConfigs);204205EGLSurface window = eglCreateWindowSurface(display, config, xWindow, nullptr);206ASSERT_TRUE(window);207ASSERT_EGL_SUCCESS();208209// When trying to create a window with a visual other than the one specified210// with EGL_X11_VISUAL_ID_ANGLE, ANGLE should fallback to using a child window.211Window root;212Window parent;213Window *children;214unsigned int nchildren;215XQueryTree(mDisplay, xWindow, &root, &parent, &children, &nchildren);216EXPECT_EQ(nchildren, 1U);217XFree(children);218219OSWindow::Delete(&osWindow);220}221222ANGLE_INSTANTIATE_TEST(EGLX11VisualHintTest, WithNoFixture(ES2_OPENGL()));223224225