Path: blob/21.2-virgl/src/gallium/targets/libgl-gdi/tests/wgl_tests.cpp
4573 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include <gtest/gtest.h>2425#include <windows.h>26#include <GL/gl.h>2728#undef GetMessage2930class window31{32public:33window(UINT width = 64, UINT height = 64);34~window();3536HWND get_hwnd() const { return _window; };37HDC get_hdc() const { return _hdc; };38bool valid() const { return _window && _hdc && _hglrc; }39void show() {40ShowWindow(_window, SW_SHOW);41}4243private:44HWND _window = nullptr;45HDC _hdc = nullptr;46HGLRC _hglrc = nullptr;47};4849window::window(uint32_t width, uint32_t height)50{51_window = CreateWindowW(52L"STATIC",53L"OpenGLTestWindow",54WS_OVERLAPPEDWINDOW,550,560,57width,58height,59NULL,60NULL,61NULL,62NULL63);6465if (_window == nullptr)66return;6768_hdc = ::GetDC(_window);6970PIXELFORMATDESCRIPTOR pfd = {71sizeof(PIXELFORMATDESCRIPTOR), /* size */721, /* version */73PFD_SUPPORT_OPENGL |74PFD_DRAW_TO_WINDOW |75PFD_DOUBLEBUFFER, /* support double-buffering */76PFD_TYPE_RGBA, /* color type */778, /* prefered color depth */780, 0, 0, 0, 0, 0, /* color bits (ignored) */790, /* no alpha buffer */800, /* alpha bits (ignored) */810, /* no accumulation buffer */820, 0, 0, 0, /* accum bits (ignored) */8332, /* depth buffer */840, /* no stencil buffer */850, /* no auxiliary buffers */86PFD_MAIN_PLANE, /* main layer */870, /* reserved */880, 0, 0, /* no layer, visible, damage masks */89};90int pixel_format = ChoosePixelFormat(_hdc, &pfd);91if (pixel_format == 0)92return;93if (!SetPixelFormat(_hdc, pixel_format, &pfd))94return;9596_hglrc = wglCreateContext(_hdc);97if (!_hglrc)98return;99100wglMakeCurrent(_hdc, _hglrc);101}102103window::~window()104{105if (_hglrc) {106wglMakeCurrent(NULL, NULL);107wglDeleteContext(_hglrc);108}109if (_hdc)110ReleaseDC(_window, _hdc);111if (_window)112DestroyWindow(_window);113}114115TEST(wgl, basic_create)116{117window wnd;118ASSERT_TRUE(wnd.valid());119120const char *version = (const char *)glGetString(GL_VERSION);121ASSERT_NE(strstr(version, "Mesa"), nullptr);122}123124#ifdef GALLIUM_D3D12125/* Fixture for tests for the d3d12 backend. Will be skipped if126* the environment isn't set up to run them.127*/128#include <directx/d3d12.h>129#include <wrl/client.h>130#include <memory>131using Microsoft::WRL::ComPtr;132133class d3d12 : public ::testing::Test134{135void SetUp() override;136};137138void d3d12::SetUp()139{140window wnd;141ASSERT_TRUE(wnd.valid());142143const char *renderer = (const char *)glGetString(GL_RENDERER);144if (!strstr(renderer, "D3D12"))145GTEST_SKIP();146}147148static bool149info_queue_has_swapchain(ID3D12DebugDevice *debug_device, ID3D12InfoQueue *info_queue)150{151info_queue->PushEmptyStorageFilter();152153debug_device->ReportLiveDeviceObjects(D3D12_RLDO_DETAIL);154155uint32_t num_messages = info_queue->GetNumStoredMessages();156for (uint32_t i = 0; i < num_messages; ++i) {157SIZE_T message_size = 0;158info_queue->GetMessage(i, nullptr, &message_size);159EXPECT_GT(message_size, 0);160161std::unique_ptr<byte[]> message_bytes(new byte[message_size]);162D3D12_MESSAGE *message = (D3D12_MESSAGE *)message_bytes.get();163info_queue->GetMessage(i, message, &message_size);164165if (strstr(message->pDescription, "SwapChain")) {166info_queue->ClearStoredMessages();167info_queue->PopStorageFilter();168return true;169}170}171info_queue->ClearStoredMessages();172info_queue->PopStorageFilter();173return false;174}175176TEST_F(d3d12, swapchain_cleanup)177{178ComPtr<ID3D12InfoQueue> info_queue;179ComPtr<ID3D12DebugDevice> debug_device;180if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&info_queue))) ||181FAILED(info_queue.As(&debug_device)))182GTEST_SKIP();183184ASSERT_FALSE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get()));185186{187window wnd;188wnd.show();189glClearColor(1.0f, 0.0f, 0.0f, 1.0f);190glClear(GL_COLOR_BUFFER_BIT);191SwapBuffers(wnd.get_hdc());192193ASSERT_TRUE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get()));194}195196ASSERT_FALSE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get()));197}198#endif199200201