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 // EGLBackwardsCompatibleContextTest.cpp.cpp:
8 // Coverage of the EGL_ANGLE_create_context_backwards_compatible extension
9
10 #include <vector>
11
12 #include "test_utils/ANGLETest.h"
13 #include "test_utils/angle_test_configs.h"
14 #include "test_utils/angle_test_instantiate.h"
15
16 namespace angle
17 {
18
19 namespace
20 {
GetCurrentContextVersion()21 std::pair<EGLint, EGLint> GetCurrentContextVersion()
22 {
23 const char *versionString = reinterpret_cast<const char *>(glGetString(GL_VERSION));
24 EXPECT_TRUE(strstr(versionString, "OpenGL ES") != nullptr);
25 return {versionString[10] - '0', versionString[12] - '0'};
26 }
27 } // anonymous namespace
28
29 class EGLBackwardsCompatibleContextTest : public ANGLETest<>
30 {
31 public:
EGLBackwardsCompatibleContextTest()32 EGLBackwardsCompatibleContextTest() : mDisplay(0) {}
33
testSetUp()34 void testSetUp() override
35 {
36 EGLint dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE};
37 mDisplay = eglGetPlatformDisplayEXT(
38 EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
39 ASSERT_TRUE(mDisplay != EGL_NO_DISPLAY);
40
41 ASSERT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
42
43 int configsCount = 0;
44 ASSERT_EGL_TRUE(eglGetConfigs(mDisplay, nullptr, 0, &configsCount));
45 ASSERT_TRUE(configsCount != 0);
46
47 std::vector<EGLConfig> configs(configsCount);
48 ASSERT_EGL_TRUE(eglGetConfigs(mDisplay, configs.data(), configsCount, &configsCount));
49
50 for (auto config : configs)
51 {
52 EGLint surfaceType;
53 eglGetConfigAttrib(mDisplay, config, EGL_SURFACE_TYPE, &surfaceType);
54 if (surfaceType & EGL_PBUFFER_BIT)
55 {
56 mConfig = config;
57 break;
58 }
59 }
60 if (!mConfig)
61 {
62 mConfig = configs[0];
63 }
64 ASSERT_NE(nullptr, mConfig);
65
66 EGLint surfaceType = EGL_NONE;
67 eglGetConfigAttrib(mDisplay, mConfig, EGL_SURFACE_TYPE, &surfaceType);
68 if (surfaceType & EGL_PBUFFER_BIT)
69 {
70 const EGLint pbufferAttribs[] = {
71 EGL_WIDTH, 500, EGL_HEIGHT, 500, EGL_NONE,
72 };
73 mPbuffer = eglCreatePbufferSurface(mDisplay, mConfig, pbufferAttribs);
74 EXPECT_TRUE(mPbuffer != EGL_NO_SURFACE);
75 }
76 }
77
testTearDown()78 void testTearDown() override
79 {
80 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
81
82 if (mPbuffer != EGL_NO_SURFACE)
83 {
84 eglDestroySurface(mDisplay, mPbuffer);
85 }
86
87 eglTerminate(mDisplay);
88 }
89
90 EGLDisplay mDisplay = EGL_NO_DISPLAY;
91 EGLSurface mPbuffer = EGL_NO_SURFACE;
92 EGLConfig mConfig = 0;
93 };
94
95 // Test extension presence. All backends should expose this extension
TEST_P(EGLBackwardsCompatibleContextTest,PbufferDifferentConfig)96 TEST_P(EGLBackwardsCompatibleContextTest, PbufferDifferentConfig)
97 {
98 EXPECT_TRUE(
99 IsEGLDisplayExtensionEnabled(mDisplay, "EGL_ANGLE_create_context_backwards_compatible"));
100 }
101
102 // Test that disabling backwards compatibility will always return the expected context version
TEST_P(EGLBackwardsCompatibleContextTest,BackwardsCompatibleDisbled)103 TEST_P(EGLBackwardsCompatibleContextTest, BackwardsCompatibleDisbled)
104 {
105 ANGLE_SKIP_TEST_IF(
106 !IsEGLDisplayExtensionEnabled(mDisplay, "EGL_ANGLE_create_context_backwards_compatible"));
107 ANGLE_SKIP_TEST_IF(!mPbuffer);
108
109 std::pair<EGLint, EGLint> testVersions[] = {
110 {1, 0}, {1, 1}, {2, 0}, {3, 0}, {3, 1}, {3, 2},
111 };
112
113 for (const auto &version : testVersions)
114 {
115 EGLint attribs[] = {EGL_CONTEXT_MAJOR_VERSION,
116 version.first,
117 EGL_CONTEXT_MINOR_VERSION,
118 version.second,
119 EGL_CONTEXT_OPENGL_BACKWARDS_COMPATIBLE_ANGLE,
120 EGL_FALSE,
121 EGL_NONE,
122 EGL_NONE};
123
124 EGLContext context = eglCreateContext(mDisplay, mConfig, nullptr, attribs);
125 if (context == EGL_NO_CONTEXT)
126 {
127 // Context version not supported
128 continue;
129 }
130
131 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, mPbuffer, mPbuffer, context));
132
133 auto contextVersion = GetCurrentContextVersion();
134 EXPECT_EQ(version, contextVersion);
135
136 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
137 eglDestroyContext(mDisplay, context);
138 }
139 }
140
141 // Test that if it's possible to create an ES3 context, requesting an ES2 context should return an
142 // ES3 context as well
TEST_P(EGLBackwardsCompatibleContextTest,BackwardsCompatibleEnabledES3)143 TEST_P(EGLBackwardsCompatibleContextTest, BackwardsCompatibleEnabledES3)
144 {
145 ANGLE_SKIP_TEST_IF(
146 !IsEGLDisplayExtensionEnabled(mDisplay, "EGL_ANGLE_create_context_backwards_compatible"));
147 ANGLE_SKIP_TEST_IF(!mPbuffer);
148
149 EGLint es3ContextAttribs[] = {
150 EGL_CONTEXT_MAJOR_VERSION, 3, EGL_CONTEXT_MINOR_VERSION, 0, EGL_NONE, EGL_NONE};
151
152 EGLContext es3Context = eglCreateContext(mDisplay, mConfig, nullptr, es3ContextAttribs);
153 ANGLE_SKIP_TEST_IF(es3Context == EGL_NO_CONTEXT);
154
155 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, mPbuffer, mPbuffer, es3Context));
156 auto es3ContextVersion = GetCurrentContextVersion();
157 eglDestroyContext(mDisplay, es3Context);
158
159 EGLint es2ContextAttribs[] = {
160 EGL_CONTEXT_MAJOR_VERSION, 2, EGL_CONTEXT_MINOR_VERSION, 0, EGL_NONE, EGL_NONE};
161
162 EGLContext es2Context = eglCreateContext(mDisplay, mConfig, nullptr, es2ContextAttribs);
163 EXPECT_NE(es2Context, EGL_NO_CONTEXT);
164
165 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, mPbuffer, mPbuffer, es2Context));
166 auto es2ContextVersion = GetCurrentContextVersion();
167 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
168 eglDestroyContext(mDisplay, es2Context);
169
170 EXPECT_EQ(es3ContextVersion, es2ContextVersion);
171 }
172
173 // Test that if ES1.1 is supported and a 1.0 context is requested, an ES 1.1 context is returned
TEST_P(EGLBackwardsCompatibleContextTest,BackwardsCompatibleEnabledES1)174 TEST_P(EGLBackwardsCompatibleContextTest, BackwardsCompatibleEnabledES1)
175 {
176 ANGLE_SKIP_TEST_IF(
177 !IsEGLDisplayExtensionEnabled(mDisplay, "EGL_ANGLE_create_context_backwards_compatible"));
178 ANGLE_SKIP_TEST_IF(!mPbuffer);
179
180 EGLint es11ContextAttribs[] = {
181 EGL_CONTEXT_MAJOR_VERSION, 1, EGL_CONTEXT_MINOR_VERSION, 1, EGL_NONE, EGL_NONE};
182
183 EGLContext es11Context = eglCreateContext(mDisplay, mConfig, nullptr, es11ContextAttribs);
184 ANGLE_SKIP_TEST_IF(es11Context == EGL_NO_CONTEXT);
185
186 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, mPbuffer, mPbuffer, es11Context));
187 auto es11ContextVersion = GetCurrentContextVersion();
188 ASSERT_EQ(std::make_pair(1, 1), es11ContextVersion);
189 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
190 eglDestroyContext(mDisplay, es11Context);
191
192 EGLint es10ContextAttribs[] = {
193 EGL_CONTEXT_MAJOR_VERSION, 1, EGL_CONTEXT_MINOR_VERSION, 0, EGL_NONE, EGL_NONE};
194
195 EGLContext es10Context = eglCreateContext(mDisplay, mConfig, nullptr, es10ContextAttribs);
196 EXPECT_NE(es10Context, EGL_NO_CONTEXT);
197
198 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, mPbuffer, mPbuffer, es10Context));
199 auto es10ContextVersion = GetCurrentContextVersion();
200 ASSERT_EQ(std::make_pair(1, 1), es10ContextVersion);
201 ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
202 eglDestroyContext(mDisplay, es10Context);
203 }
204
205 ANGLE_INSTANTIATE_TEST(EGLBackwardsCompatibleContextTest,
206 WithNoFixture(ES2_D3D9()),
207 WithNoFixture(ES2_D3D11()),
208 WithNoFixture(ES2_OPENGL()),
209 WithNoFixture(ES2_OPENGLES()),
210 WithNoFixture(ES2_VULKAN()));
211
212 } // namespace angle
213