xref: /aosp_15_r20/external/angle/src/tests/egl_tests/EGLDeviceTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 #ifndef ANGLE_ENABLE_D3D9
8 #    define ANGLE_ENABLE_D3D9
9 #endif
10 
11 #ifndef ANGLE_ENABLE_D3D11
12 #    define ANGLE_ENABLE_D3D11
13 #endif
14 
15 #include <d3d11.h>
16 
17 #include "test_utils/ANGLETest.h"
18 #include "util/EGLWindow.h"
19 #include "util/OSWindow.h"
20 #include "util/com_utils.h"
21 #include "util/gles_loader_autogen.h"
22 
23 using namespace angle;
24 
25 class EGLDeviceCreationTest : public ANGLETest<>
26 {
27   protected:
EGLDeviceCreationTest()28     EGLDeviceCreationTest()
29         : mD3D11Module(nullptr),
30           mD3D11CreateDevice(nullptr),
31           mDevice(nullptr),
32           mDeviceContext(nullptr),
33           mDeviceCreationD3D11ExtAvailable(false),
34           mOSWindow(nullptr),
35           mDisplay(EGL_NO_DISPLAY),
36           mSurface(EGL_NO_SURFACE),
37           mContext(EGL_NO_CONTEXT),
38           mConfig(0)
39     {}
40 
testSetUp()41     void testSetUp() override
42     {
43         ASSERT_TRUE(isD3D11Renderer());
44 
45         mD3D11Module = LoadLibrary(TEXT("d3d11.dll"));
46         if (mD3D11Module == nullptr)
47         {
48             std::cout << "Unable to LoadLibrary D3D11" << std::endl;
49             return;
50         }
51 
52         mD3D11CreateDevice = reinterpret_cast<PFN_D3D11_CREATE_DEVICE>(
53             GetProcAddress(mD3D11Module, "D3D11CreateDevice"));
54         if (mD3D11CreateDevice == nullptr)
55         {
56             std::cout << "Could not retrieve D3D11CreateDevice from d3d11.dll" << std::endl;
57             return;
58         }
59 
60         const char *extensionString =
61             static_cast<const char *>(eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS));
62         if (strstr(extensionString, "EGL_ANGLE_device_creation"))
63         {
64             if (strstr(extensionString, "EGL_ANGLE_device_creation_d3d11"))
65             {
66                 mDeviceCreationD3D11ExtAvailable = true;
67             }
68         }
69     }
70 
testTearDown()71     void testTearDown() override
72     {
73         SafeRelease(mDevice);
74         SafeRelease(mDeviceContext);
75 
76         OSWindow::Delete(&mOSWindow);
77 
78         if (mSurface != EGL_NO_SURFACE)
79         {
80             eglDestroySurface(mDisplay, mSurface);
81             mSurface = EGL_NO_SURFACE;
82         }
83 
84         if (mContext != EGL_NO_CONTEXT)
85         {
86             eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
87             eglDestroyContext(mDisplay, mContext);
88             mContext = EGL_NO_CONTEXT;
89         }
90 
91         if (mDisplay != EGL_NO_DISPLAY)
92         {
93             eglTerminate(mDisplay);
94             mDisplay = EGL_NO_DISPLAY;
95         }
96     }
97 
CreateD3D11Device()98     void CreateD3D11Device()
99     {
100         ASSERT_EQ(nullptr, mDevice);  // The device shouldn't be created twice
101 
102         HRESULT hr =
103             mD3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, 0, nullptr, 0,
104                                D3D11_SDK_VERSION, &mDevice, &mFeatureLevel, &mDeviceContext);
105 
106         ASSERT_TRUE(SUCCEEDED(hr));
107         ASSERT_GE(mFeatureLevel, D3D_FEATURE_LEVEL_9_3);
108     }
109 
CreateWindowSurface()110     void CreateWindowSurface()
111     {
112         EGLint majorVersion, minorVersion;
113         ASSERT_EGL_TRUE(eglInitialize(mDisplay, &majorVersion, &minorVersion));
114 
115         eglBindAPI(EGL_OPENGL_ES_API);
116         ASSERT_EGL_SUCCESS();
117 
118         // Choose a config
119         const EGLint configAttributes[] = {EGL_NONE};
120         EGLint configCount              = 0;
121         ASSERT_EGL_TRUE(eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount));
122 
123         // Create an OS Window
124         mOSWindow = OSWindow::New();
125         mOSWindow->initialize("EGLSurfaceTest", 64, 64);
126 
127         // Create window surface
128         mSurface = eglCreateWindowSurface(mDisplay, mConfig, mOSWindow->getNativeWindow(), nullptr);
129         ASSERT_EGL_SUCCESS();
130 
131         // Create EGL context
132         EGLint contextAttibutes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
133         mContext                  = eglCreateContext(mDisplay, mConfig, nullptr, contextAttibutes);
134         ASSERT_EGL_SUCCESS();
135 
136         // Make the surface current
137         eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
138         ASSERT_EGL_SUCCESS();
139     }
140 
141     // This triggers a D3D device lost on current Windows systems
142     // This behavior could potentially change in the future
trigger9_3DeviceLost()143     void trigger9_3DeviceLost()
144     {
145         ID3D11Buffer *gsBuffer       = nullptr;
146         D3D11_BUFFER_DESC bufferDesc = {0};
147         bufferDesc.ByteWidth         = 64;
148         bufferDesc.Usage             = D3D11_USAGE_DEFAULT;
149         bufferDesc.BindFlags         = D3D11_BIND_CONSTANT_BUFFER;
150 
151         HRESULT result = mDevice->CreateBuffer(&bufferDesc, nullptr, &gsBuffer);
152         ASSERT_TRUE(SUCCEEDED(result));
153 
154         mDeviceContext->GSSetConstantBuffers(0, 1, &gsBuffer);
155         SafeRelease(gsBuffer);
156         gsBuffer = nullptr;
157 
158         result = mDevice->GetDeviceRemovedReason();
159         ASSERT_TRUE(FAILED(result));
160     }
161 
162     HMODULE mD3D11Module;
163     PFN_D3D11_CREATE_DEVICE mD3D11CreateDevice;
164 
165     ID3D11Device *mDevice;
166     ID3D11DeviceContext *mDeviceContext;
167     D3D_FEATURE_LEVEL mFeatureLevel;
168 
169     bool mDeviceCreationD3D11ExtAvailable;
170 
171     OSWindow *mOSWindow;
172 
173     EGLDisplay mDisplay;
174     EGLSurface mSurface;
175     EGLContext mContext;
176     EGLConfig mConfig;
177 };
178 
179 // Test that creating a EGLDeviceEXT from D3D11 device works, and it can be queried to retrieve
180 // D3D11 device
TEST_P(EGLDeviceCreationTest,BasicD3D11Device)181 TEST_P(EGLDeviceCreationTest, BasicD3D11Device)
182 {
183     ANGLE_SKIP_TEST_IF(!mDeviceCreationD3D11ExtAvailable);
184 
185     CreateD3D11Device();
186 
187     EGLDeviceEXT eglDevice =
188         eglCreateDeviceANGLE(EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDevice), nullptr);
189     ASSERT_NE(EGL_NO_DEVICE_EXT, eglDevice);
190     ASSERT_EGL_SUCCESS();
191 
192     EGLAttrib deviceAttrib;
193     eglQueryDeviceAttribEXT(eglDevice, EGL_D3D11_DEVICE_ANGLE, &deviceAttrib);
194     ASSERT_EGL_SUCCESS();
195 
196     ID3D11Device *queriedDevice = reinterpret_cast<ID3D11Device *>(deviceAttrib);
197     ASSERT_EQ(mFeatureLevel, queriedDevice->GetFeatureLevel());
198 
199     eglReleaseDeviceANGLE(eglDevice);
200 }
201 
202 // Test that creating a EGLDeviceEXT from D3D11 device works, and it can be queried to retrieve
203 // D3D11 device
TEST_P(EGLDeviceCreationTest,BasicD3D11DeviceViaFuncPointer)204 TEST_P(EGLDeviceCreationTest, BasicD3D11DeviceViaFuncPointer)
205 {
206     ANGLE_SKIP_TEST_IF(!mDeviceCreationD3D11ExtAvailable);
207 
208     CreateD3D11Device();
209 
210     EGLDeviceEXT eglDevice =
211         eglCreateDeviceANGLE(EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDevice), nullptr);
212     ASSERT_NE(EGL_NO_DEVICE_EXT, eglDevice);
213     ASSERT_EGL_SUCCESS();
214 
215     EGLAttrib deviceAttrib;
216     eglQueryDeviceAttribEXT(eglDevice, EGL_D3D11_DEVICE_ANGLE, &deviceAttrib);
217     ASSERT_EGL_SUCCESS();
218 
219     ID3D11Device *queriedDevice = reinterpret_cast<ID3D11Device *>(deviceAttrib);
220     ASSERT_EQ(mFeatureLevel, queriedDevice->GetFeatureLevel());
221 
222     eglReleaseDeviceANGLE(eglDevice);
223 }
224 
225 // Test that creating a EGLDeviceEXT from D3D11 device works, and can be used for rendering
TEST_P(EGLDeviceCreationTest,RenderingUsingD3D11Device)226 TEST_P(EGLDeviceCreationTest, RenderingUsingD3D11Device)
227 {
228     CreateD3D11Device();
229 
230     EGLDeviceEXT eglDevice =
231         eglCreateDeviceANGLE(EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDevice), nullptr);
232     ASSERT_EGL_SUCCESS();
233 
234     // Create an EGLDisplay using the EGLDevice
235     mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, eglDevice, nullptr);
236     ASSERT_NE(EGL_NO_DISPLAY, mDisplay);
237 
238     // Create a surface using the display
239     CreateWindowSurface();
240 
241     // Perform some very basic rendering
242     glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
243     glClear(GL_COLOR_BUFFER_BIT);
244     EXPECT_PIXEL_EQ(32, 32, 255, 0, 255, 255);
245 
246     // Note that we must call TearDown() before we release the EGL device, since the display
247     // depends on the device
248     ASSERT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
249     testTearDown();
250 
251     eglReleaseDeviceANGLE(eglDevice);
252 }
253 
254 // Test that calling eglGetPlatformDisplayEXT with the same device returns the same display
TEST_P(EGLDeviceCreationTest,GetPlatformDisplayTwice)255 TEST_P(EGLDeviceCreationTest, GetPlatformDisplayTwice)
256 {
257     CreateD3D11Device();
258 
259     EGLDeviceEXT eglDevice =
260         eglCreateDeviceANGLE(EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDevice), nullptr);
261     ASSERT_EGL_SUCCESS();
262 
263     // Create an EGLDisplay using the EGLDevice
264     EGLDisplay display1 = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, eglDevice, nullptr);
265     ASSERT_NE(EGL_NO_DISPLAY, display1);
266 
267     EGLDisplay display2 = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, eglDevice, nullptr);
268     ASSERT_NE(EGL_NO_DISPLAY, display2);
269 
270     ASSERT_EQ(display1, display2);
271 
272     eglTerminate(display1);
273     eglReleaseDeviceANGLE(eglDevice);
274 }
275 
276 // Test that creating a EGLDeviceEXT from an invalid D3D11 device fails
TEST_P(EGLDeviceCreationTest,InvalidD3D11Device)277 TEST_P(EGLDeviceCreationTest, InvalidD3D11Device)
278 {
279     ANGLE_SKIP_TEST_IF(!mDeviceCreationD3D11ExtAvailable);
280 
281     CreateD3D11Device();
282 
283     // Use mDeviceContext instead of mDevice
284     EGLDeviceEXT eglDevice = eglCreateDeviceANGLE(
285         EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDeviceContext), nullptr);
286     EXPECT_EQ(EGL_NO_DEVICE_EXT, eglDevice);
287     EXPECT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
288 }
289 
290 // Test that EGLDeviceEXT holds a ref to the D3D11 device
TEST_P(EGLDeviceCreationTest,D3D11DeviceReferenceCounting)291 TEST_P(EGLDeviceCreationTest, D3D11DeviceReferenceCounting)
292 {
293     ANGLE_SKIP_TEST_IF(!mDeviceCreationD3D11ExtAvailable);
294 
295     CreateD3D11Device();
296 
297     EGLDeviceEXT eglDevice =
298         eglCreateDeviceANGLE(EGL_D3D11_DEVICE_ANGLE, reinterpret_cast<void *>(mDevice), nullptr);
299     ASSERT_NE(EGL_NO_DEVICE_EXT, eglDevice);
300     ASSERT_EGL_SUCCESS();
301 
302     // Now release our D3D11 device/context
303     SafeRelease(mDevice);
304     SafeRelease(mDeviceContext);
305 
306     EGLAttrib deviceAttrib;
307     eglQueryDeviceAttribEXT(eglDevice, EGL_D3D11_DEVICE_ANGLE, &deviceAttrib);
308     ASSERT_EGL_SUCCESS();
309 
310     ID3D11Device *queriedDevice = reinterpret_cast<ID3D11Device *>(deviceAttrib);
311     ASSERT_EQ(mFeatureLevel, queriedDevice->GetFeatureLevel());
312 
313     eglReleaseDeviceANGLE(eglDevice);
314 }
315 
316 // Test that creating a EGLDeviceEXT from a D3D9 device fails
TEST_P(EGLDeviceCreationTest,AnyD3D9Device)317 TEST_P(EGLDeviceCreationTest, AnyD3D9Device)
318 {
319     ANGLE_SKIP_TEST_IF(!mDeviceCreationD3D11ExtAvailable);
320 
321     std::string fakeD3DDevice = "This is a string, not a D3D device";
322 
323     EGLDeviceEXT eglDevice = eglCreateDeviceANGLE(
324         EGL_D3D9_DEVICE_ANGLE, reinterpret_cast<void *>(&fakeD3DDevice), nullptr);
325     EXPECT_EQ(EGL_NO_DEVICE_EXT, eglDevice);
326     EXPECT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
327 }
328 
329 class EGLDeviceQueryTest : public ANGLETest<>
330 {
331   protected:
EGLDeviceQueryTest()332     EGLDeviceQueryTest() {}
333 
testSetUp()334     void testSetUp() override
335     {
336         if (!eglQueryDeviceStringEXT)
337         {
338             FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDeviceStringEXT was not "
339                       "found";
340         }
341 
342         if (!eglQueryDisplayAttribEXT)
343         {
344             FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDisplayAttribEXT was "
345                       "not found";
346         }
347 
348         if (!eglQueryDeviceAttribEXT)
349         {
350             FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDeviceAttribEXT was not "
351                       "found";
352         }
353 
354         EGLAttrib angleDevice = 0;
355         EXPECT_EGL_TRUE(
356             eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
357         if (!IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
358                                          "EGL_ANGLE_device_d3d9") &&
359             !IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
360                                          "EGL_ANGLE_device_d3d11"))
361         {
362             FAIL() << "ANGLE extensions EGL_ANGLE_device_d3d9 or EGL_ANGLE_device_d3d11 were not "
363                       "found";
364         }
365     }
366 };
367 
368 // This test attempts to obtain a D3D11 device and a D3D9 device using the eglQueryDeviceAttribEXT
369 // function.
370 // If the test is configured to use D3D11 then it should succeed to obtain a D3D11 device.
371 // If the test is confitured to use D3D9, then it should succeed to obtain a D3D9 device.
TEST_P(EGLDeviceQueryTest,QueryDevice)372 TEST_P(EGLDeviceQueryTest, QueryDevice)
373 {
374     EGLAttrib angleDevice = 0;
375     EXPECT_EGL_TRUE(
376         eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
377 
378     if (IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
379                                     "EGL_ANGLE_device_d3d11"))
380     {
381         EGLAttrib device11 = 0;
382         EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
383                                                 EGL_D3D11_DEVICE_ANGLE, &device11));
384         ID3D11Device *d3d11Device = reinterpret_cast<ID3D11Device *>(device11);
385         IDXGIDevice *dxgiDevice   = DynamicCastComObject<IDXGIDevice>(d3d11Device);
386         EXPECT_TRUE(dxgiDevice != nullptr);
387         SafeRelease(dxgiDevice);
388     }
389 
390     if (IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
391                                     "EGL_ANGLE_device_d3d9"))
392     {
393         EGLAttrib device9 = 0;
394         EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
395                                                 EGL_D3D9_DEVICE_ANGLE, &device9));
396         IDirect3DDevice9 *d3d9Device = reinterpret_cast<IDirect3DDevice9 *>(device9);
397         IDirect3D9 *d3d9             = nullptr;
398         EXPECT_EQ(S_OK, d3d9Device->GetDirect3D(&d3d9));
399         EXPECT_TRUE(d3d9 != nullptr);
400         SafeRelease(d3d9);
401     }
402 }
403 
404 // This test attempts to obtain a D3D11 device from a D3D9 configured system and a D3D9 device from
405 // a D3D11 configured system using the eglQueryDeviceAttribEXT function.
406 // If the test is configured to use D3D11 then it should fail to obtain a D3D11 device.
407 // If the test is confitured to use D3D9, then it should fail to obtain a D3D9 device.
TEST_P(EGLDeviceQueryTest,QueryDeviceBadAttribute)408 TEST_P(EGLDeviceQueryTest, QueryDeviceBadAttribute)
409 {
410     EGLAttrib angleDevice = 0;
411     EXPECT_EGL_TRUE(
412         eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
413 
414     if (!IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
415                                      "EGL_ANGLE_device_d3d11"))
416     {
417         EGLAttrib device11 = 0;
418         EXPECT_EGL_FALSE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
419                                                  EGL_D3D11_DEVICE_ANGLE, &device11));
420     }
421 
422     if (!IsEGLDeviceExtensionEnabled(reinterpret_cast<EGLDeviceEXT>(angleDevice),
423                                      "EGL_ANGLE_device_d3d9"))
424     {
425         EGLAttrib device9 = 0;
426         EXPECT_EGL_FALSE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
427                                                  EGL_D3D9_DEVICE_ANGLE, &device9));
428     }
429 }
430 
431 // Ensure that:
432 //    - calling getPlatformDisplayEXT using ANGLE_Platform with some parameters
433 //    - extracting the EGLDeviceEXT from the EGLDisplay
434 //    - calling getPlatformDisplayEXT with this EGLDeviceEXT
435 // results in the same EGLDisplay being returned from getPlatformDisplayEXT both times
TEST_P(EGLDeviceQueryTest,GetPlatformDisplayDeviceReuse)436 TEST_P(EGLDeviceQueryTest, GetPlatformDisplayDeviceReuse)
437 {
438     EGLAttrib eglDevice = 0;
439     EXPECT_EGL_TRUE(
440         eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &eglDevice));
441 
442     EGLDisplay display2 = eglGetPlatformDisplayEXT(
443         EGL_PLATFORM_DEVICE_EXT, reinterpret_cast<EGLDeviceEXT>(eglDevice), nullptr);
444     EXPECT_EQ(getEGLWindow()->getDisplay(), display2);
445 }
446 
447 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
448 // tests should be run against.
449 ANGLE_INSTANTIATE_TEST(EGLDeviceCreationTest, WithNoFixture(ES2_D3D11()));
450 ANGLE_INSTANTIATE_TEST(EGLDeviceQueryTest, ES2_D3D9(), ES2_D3D11());
451