From f3db2963ce8f84a12cebb6057d4ac593080da24a Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Wed, 31 Mar 2021 13:49:31 -0700 Subject: [PATCH] wgl: Add unit test infrastructure for OpenGL32.dll on Windows Reviewed-By: Bill Kristiansen Reviewed-by: Jose Fonseca Part-of: --- meson.build | 1 + src/gallium/targets/libgl-gdi/meson.build | 18 +++ .../targets/libgl-gdi/tests/wgl_tests.cpp | 121 ++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/gallium/targets/libgl-gdi/tests/wgl_tests.cpp diff --git a/meson.build b/meson.build index ddc08096296..c69659afc2c 100644 --- a/meson.build +++ b/meson.build @@ -570,6 +570,7 @@ if with_gallium_zink dep_vulkan = dependency('vulkan') endif +dep_dxheaders = null_dep if with_gallium_d3d12 or with_microsoft_clc dep_dxheaders = dependency('DirectX-Headers', fallback : ['DirectX-Headers', 'dep_dxheaders'], required : with_gallium_d3d12 diff --git a/src/gallium/targets/libgl-gdi/meson.build b/src/gallium/targets/libgl-gdi/meson.build index 7ed09f543da..022714b8d81 100644 --- a/src/gallium/targets/libgl-gdi/meson.build +++ b/src/gallium/targets/libgl-gdi/meson.build @@ -44,3 +44,21 @@ libopengl32 = shared_library( name_prefix : '', # otherwise mingw will create libopengl32.dll install : true, ) + +# The CI pipeline for MinGW doesn't support creating a window, so don't run these tests there +if with_tests and cc.get_id() != 'gcc' + extra_test_deps = [] + test( + 'wgl', + executable( + 'test_wgl', + files('tests/wgl_tests.cpp'), + cpp_args : [cpp_msvc_compat_args], + dependencies : [idep_gtest, dep_dxheaders, extra_test_deps, + driver_swrast, driver_swr, driver_d3d12, driver_zink + ], + link_with : [libopengl32], + ), + suite : ['wgl'], + ) +endif diff --git a/src/gallium/targets/libgl-gdi/tests/wgl_tests.cpp b/src/gallium/targets/libgl-gdi/tests/wgl_tests.cpp new file mode 100644 index 00000000000..d2feddc4242 --- /dev/null +++ b/src/gallium/targets/libgl-gdi/tests/wgl_tests.cpp @@ -0,0 +1,121 @@ +/* + * Copyright © Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include + +#include +#include + +class window +{ +public: + window(UINT width = 64, UINT height = 64); + ~window(); + + HWND get_hwnd() const { return _window; }; + HDC get_hdc() const { return _hdc; }; + bool valid() const { return _window && _hdc && _hglrc; } + void show() { + ShowWindow(_window, SW_SHOW); + } + +private: + HWND _window = nullptr; + HDC _hdc = nullptr; + HGLRC _hglrc = nullptr; +}; + +window::window(uint32_t width, uint32_t height) +{ + _window = CreateWindowW( + L"STATIC", + L"OpenGLTestWindow", + WS_OVERLAPPEDWINDOW, + 0, + 0, + width, + height, + NULL, + NULL, + NULL, + NULL + ); + + if (_window == nullptr) + return; + + _hdc = ::GetDC(_window); + + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), /* size */ + 1, /* version */ + PFD_SUPPORT_OPENGL | + PFD_DRAW_TO_WINDOW | + PFD_DOUBLEBUFFER, /* support double-buffering */ + PFD_TYPE_RGBA, /* color type */ + 8, /* prefered color depth */ + 0, 0, 0, 0, 0, 0, /* color bits (ignored) */ + 0, /* no alpha buffer */ + 0, /* alpha bits (ignored) */ + 0, /* no accumulation buffer */ + 0, 0, 0, 0, /* accum bits (ignored) */ + 32, /* depth buffer */ + 0, /* no stencil buffer */ + 0, /* no auxiliary buffers */ + PFD_MAIN_PLANE, /* main layer */ + 0, /* reserved */ + 0, 0, 0, /* no layer, visible, damage masks */ + }; + int pixel_format = ChoosePixelFormat(_hdc, &pfd); + if (pixel_format == 0) + return; + if (!SetPixelFormat(_hdc, pixel_format, &pfd)) + return; + + _hglrc = wglCreateContext(_hdc); + if (!_hglrc) + return; + + wglMakeCurrent(_hdc, _hglrc); +} + +window::~window() +{ + if (_hglrc) { + wglMakeCurrent(NULL, NULL); + wglDeleteContext(_hglrc); + } + if (_hdc) + ReleaseDC(_window, _hdc); + if (_window) + DestroyWindow(_window); +} + +TEST(wgl, basic_create) +{ + window wnd; + ASSERT_TRUE(wnd.valid()); + + const char *version = (const char *)glGetString(GL_VERSION); + ASSERT_NE(strstr(version, "Mesa"), nullptr); +}