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 const char *extensionString =
337 static_cast<const char *>(eglQueryString(getEGLWindow()->getDisplay(), EGL_EXTENSIONS));
338
339 if (!eglQueryDeviceStringEXT)
340 {
341 FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDeviceStringEXT was not "
342 "found";
343 }
344
345 if (!eglQueryDisplayAttribEXT)
346 {
347 FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDisplayAttribEXT was "
348 "not found";
349 }
350
351 if (!eglQueryDeviceAttribEXT)
352 {
353 FAIL() << "ANGLE extension EGL_EXT_device_query export eglQueryDeviceAttribEXT was not "
354 "found";
355 }
356
357 EGLAttrib angleDevice = 0;
358 EXPECT_EGL_TRUE(
359 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
360 extensionString = static_cast<const char *>(
361 eglQueryDeviceStringEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice), EGL_EXTENSIONS));
362 if (strstr(extensionString, "EGL_ANGLE_device_d3d") == nullptr)
363 {
364 FAIL() << "ANGLE extension EGL_ANGLE_device_d3d was not found";
365 }
366 }
367 };
368
369 // This test attempts to obtain a D3D11 device and a D3D9 device using the eglQueryDeviceAttribEXT
370 // function.
371 // If the test is configured to use D3D11 then it should succeed to obtain a D3D11 device.
372 // If the test is confitured to use D3D9, then it should succeed to obtain a D3D9 device.
TEST_P(EGLDeviceQueryTest,QueryDevice)373 TEST_P(EGLDeviceQueryTest, QueryDevice)
374 {
375 EGLAttrib device = 0;
376 EGLAttrib angleDevice = 0;
377 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
378 {
379 EXPECT_EGL_TRUE(
380 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
381 EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
382 EGL_D3D11_DEVICE_ANGLE, &device));
383 ID3D11Device *d3d11Device = reinterpret_cast<ID3D11Device *>(device);
384 IDXGIDevice *dxgiDevice = DynamicCastComObject<IDXGIDevice>(d3d11Device);
385 EXPECT_TRUE(dxgiDevice != nullptr);
386 SafeRelease(dxgiDevice);
387 }
388
389 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE)
390 {
391 EXPECT_EGL_TRUE(
392 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
393 EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
394 EGL_D3D9_DEVICE_ANGLE, &device));
395 IDirect3DDevice9 *d3d9Device = reinterpret_cast<IDirect3DDevice9 *>(device);
396 IDirect3D9 *d3d9 = nullptr;
397 EXPECT_EQ(S_OK, d3d9Device->GetDirect3D(&d3d9));
398 EXPECT_TRUE(d3d9 != nullptr);
399 SafeRelease(d3d9);
400 }
401 }
402
403 // This test attempts to obtain a D3D11 device from a D3D9 configured system and a D3D9 device from
404 // a D3D11 configured system using the eglQueryDeviceAttribEXT function.
405 // If the test is configured to use D3D11 then it should fail to obtain a D3D11 device.
406 // If the test is confitured to use D3D9, then it should fail to obtain a D3D9 device.
TEST_P(EGLDeviceQueryTest,QueryDeviceBadAttribute)407 TEST_P(EGLDeviceQueryTest, QueryDeviceBadAttribute)
408 {
409 EGLAttrib device = 0;
410 EGLAttrib angleDevice = 0;
411 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
412 {
413 EXPECT_EGL_TRUE(
414 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
415 EXPECT_EGL_FALSE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
416 EGL_D3D9_DEVICE_ANGLE, &device));
417 }
418
419 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE)
420 {
421 EXPECT_EGL_TRUE(
422 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
423 EXPECT_EGL_FALSE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
424 EGL_D3D11_DEVICE_ANGLE, &device));
425 }
426 }
427
428 // Ensure that:
429 // - calling getPlatformDisplayEXT using ANGLE_Platform with some parameters
430 // - extracting the EGLDeviceEXT from the EGLDisplay
431 // - calling getPlatformDisplayEXT with this EGLDeviceEXT
432 // results in the same EGLDisplay being returned from getPlatformDisplayEXT both times
TEST_P(EGLDeviceQueryTest,GetPlatformDisplayDeviceReuse)433 TEST_P(EGLDeviceQueryTest, GetPlatformDisplayDeviceReuse)
434 {
435 EGLAttrib eglDevice = 0;
436 EXPECT_EGL_TRUE(
437 eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &eglDevice));
438
439 EGLDisplay display2 = eglGetPlatformDisplayEXT(
440 EGL_PLATFORM_DEVICE_EXT, reinterpret_cast<EGLDeviceEXT>(eglDevice), nullptr);
441 EXPECT_EQ(getEGLWindow()->getDisplay(), display2);
442 }
443
444 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
445 // tests should be run against.
446 ANGLE_INSTANTIATE_TEST(EGLDeviceCreationTest, WithNoFixture(ES2_D3D11()));
447 ANGLE_INSTANTIATE_TEST(EGLDeviceQueryTest, ES2_D3D9(), ES2_D3D11());
448