xref: /aosp_15_r20/external/swiftshader/tests/VulkanWrapper/Window.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Window.hpp"
16 
17 #if USE_HEADLESS_SURFACE
18 
Window(vk::Instance instance,vk::Extent2D windowSize)19 Window::Window(vk::Instance instance, vk::Extent2D windowSize)
20     : instance(instance)
21 {
22 	vk::HeadlessSurfaceCreateInfoEXT surfaceCreateInfo;
23 	surface = instance.createHeadlessSurfaceEXT(surfaceCreateInfo);
24 	assert(surface);
25 }
26 
~Window()27 Window::~Window()
28 {
29 	instance.destroySurfaceKHR(surface, nullptr);
30 }
31 
getSurface()32 vk::SurfaceKHR Window::getSurface()
33 {
34 	return surface;
35 }
36 
show()37 void Window::show()
38 {
39 }
40 
41 #elif defined(_WIN32)
42 
Window(vk::Instance instance,vk::Extent2D windowSize)43 Window::Window(vk::Instance instance, vk::Extent2D windowSize)
44     : instance(instance)
45 {
46 	windowClass.cbSize = sizeof(WNDCLASSEX);
47 	windowClass.style = CS_HREDRAW | CS_VREDRAW;
48 	windowClass.lpfnWndProc = DefWindowProc;
49 	windowClass.cbClsExtra = 0;
50 	windowClass.cbWndExtra = 0;
51 	windowClass.hInstance = moduleInstance;
52 	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
53 	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
54 	windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
55 	windowClass.lpszMenuName = NULL;
56 	windowClass.lpszClassName = "Window";
57 	windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
58 
59 	RegisterClassEx(&windowClass);
60 
61 	DWORD style = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
62 	DWORD extendedStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
63 
64 	RECT windowRect;
65 	windowRect.left = 0L;
66 	windowRect.top = 0L;
67 	windowRect.right = (long)windowSize.width;
68 	windowRect.bottom = (long)windowSize.height;
69 
70 	AdjustWindowRectEx(&windowRect, style, FALSE, extendedStyle);
71 	uint32_t x = (GetSystemMetrics(SM_CXSCREEN) - windowRect.right) / 2;
72 	uint32_t y = (GetSystemMetrics(SM_CYSCREEN) - windowRect.bottom) / 2;
73 
74 	window = CreateWindowEx(extendedStyle, "Window", "Hello",
75 	                        style | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
76 	                        x, y,
77 	                        windowRect.right - windowRect.left,
78 	                        windowRect.bottom - windowRect.top,
79 	                        NULL, NULL, moduleInstance, NULL);
80 
81 	SetForegroundWindow(window);
82 	SetFocus(window);
83 
84 	// Create the Vulkan surface
85 	vk::Win32SurfaceCreateInfoKHR surfaceCreateInfo;
86 	surfaceCreateInfo.hinstance = moduleInstance;
87 	surfaceCreateInfo.hwnd = window;
88 	surface = instance.createWin32SurfaceKHR(surfaceCreateInfo);
89 	assert(surface);
90 }
91 
~Window()92 Window::~Window()
93 {
94 	instance.destroySurfaceKHR(surface, nullptr);
95 	DestroyWindow(window);
96 	UnregisterClass("Window", moduleInstance);
97 }
98 
getSurface()99 vk::SurfaceKHR Window::getSurface()
100 {
101 	return surface;
102 }
103 
show()104 void Window::show()
105 {
106 	ShowWindow(window, SW_SHOW);
107 }
108 
109 #else
110 #	error Window class unimplemented for this platform
111 #endif
112