Skip to content

Commit 26bb7fd

Browse files
committed
feat: add generic window type
1 parent ca7596e commit 26bb7fd

5 files changed

Lines changed: 268 additions & 32 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ SET (SRC_LIST "${Anvil_SOURCE_DIR}/include/misc/memalloc_backends/backend_onesho
100100
"${Anvil_SOURCE_DIR}/include/misc/vulkan.h"
101101
"${Anvil_SOURCE_DIR}/include/misc/window.h"
102102
"${Anvil_SOURCE_DIR}/include/misc/window_factory.h"
103+
"${Anvil_SOURCE_DIR}/include/misc/window_generic.h"
103104
"${Anvil_SOURCE_DIR}/include/wrappers/buffer.h"
104105
"${Anvil_SOURCE_DIR}/include/wrappers/buffer_view.h"
105106
"${Anvil_SOURCE_DIR}/include/wrappers/command_buffer.h"
@@ -181,6 +182,7 @@ SET (SRC_LIST "${Anvil_SOURCE_DIR}/include/misc/memalloc_backends/backend_onesho
181182
"${Anvil_SOURCE_DIR}/src/misc/vulkan.cpp"
182183
"${Anvil_SOURCE_DIR}/src/misc/window.cpp"
183184
"${Anvil_SOURCE_DIR}/src/misc/window_factory.cpp"
185+
"${Anvil_SOURCE_DIR}/src/misc/window_generic.cpp"
184186
"${Anvil_SOURCE_DIR}/src/wrappers/buffer.cpp"
185187
"${Anvil_SOURCE_DIR}/src/wrappers/buffer_view.cpp"
186188
"${Anvil_SOURCE_DIR}/src/wrappers/command_buffer.cpp"

include/misc/window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ namespace Anvil
9090
WINDOW_PLATFORM_WAYLAND,
9191
#endif
9292

93+
WINDOW_PLATFORM_GENERIC,
94+
9395
/* Always last */
9496
WINDOW_PLATFORM_COUNT,
9597
WINDOW_PLATFORM_UNKNOWN = WINDOW_PLATFORM_COUNT

include/misc/window_generic.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Copyright (c) 2017-2018 Advanced Micro Devices, Inc. All rights reserved.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
//
22+
23+
/** Implements a simple window wrapper for Windows environments.
24+
*
25+
* NOTE: This wrapper does not support scaling (yet).
26+
**/
27+
#ifndef WINDOW_GENERIC_H
28+
#define WINDOW_GENERIC_H
29+
30+
#include "misc/window.h"
31+
32+
namespace Anvil
33+
{
34+
class WindowGeneric : public Window
35+
{
36+
public:
37+
enum class Type : uint8_t {
38+
Win32 = 0,
39+
Xcb,
40+
Wayland,
41+
42+
Count,
43+
};
44+
static Anvil::WindowUniquePtr create(Type type, void *windowHandle, uint32_t width, uint32_t height, bool visible);
45+
46+
virtual ~WindowGeneric() { /* Stub */ }
47+
48+
virtual void close();
49+
virtual void run();
50+
51+
/* Returns window's platform */
52+
WindowPlatform get_platform() const
53+
{
54+
return WINDOW_PLATFORM_GENERIC;
55+
}
56+
57+
/* This function should never be called under Windows */
58+
virtual void* get_connection() const
59+
{
60+
anvil_assert_fail();
61+
62+
return nullptr;
63+
}
64+
Type get_type() const { return m_type; }
65+
66+
/** Changes the window title.
67+
*
68+
* @param in_new_title Null-terminated string, holding the new title.
69+
*/
70+
void set_title(const std::string& in_new_title) override;
71+
72+
private:
73+
/* Private functions */
74+
75+
WindowGeneric(Type type,
76+
void *windowHandle,
77+
const std::string &in_title,
78+
unsigned int in_width,
79+
unsigned int in_height,
80+
PresentCallbackFunction in_present_callback_func);
81+
82+
/** Creates a new system window and prepares it for usage. */
83+
bool init(const bool& in_visible);
84+
85+
/* Private variables */
86+
Type m_type;
87+
};
88+
}; /* namespace Anvil */
89+
90+
#endif /* WINDOW_WIN3264_H */
91+

src/misc/window_generic.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Copyright (c) 2017-2018 Advanced Micro Devices, Inc. All rights reserved.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
//
22+
23+
#include "misc/window_generic.h"
24+
#include <sstream>
25+
26+
/* See create() for documentation */
27+
Anvil::WindowGeneric::WindowGeneric(Type type,
28+
void *windowHandle,
29+
const std::string &in_title,
30+
unsigned int in_width,
31+
unsigned int in_height,
32+
Anvil::PresentCallbackFunction in_present_callback_func)
33+
:Window(in_title,
34+
in_width,
35+
in_height,
36+
true, /* in_closable */
37+
in_present_callback_func)
38+
{
39+
m_window = reinterpret_cast<WindowHandle>(windowHandle);
40+
m_window_owned = false;
41+
m_type = type;
42+
}
43+
44+
/** Please see header for specification */
45+
Anvil::WindowUniquePtr Anvil::WindowGeneric::create(Type type, void *windowHandle, uint32_t width, uint32_t height, bool visible)
46+
{
47+
WindowUniquePtr result_ptr (nullptr,
48+
std::default_delete<Window>() );
49+
uint32_t window_size[2] = {0};
50+
51+
window_size[0] = width;
52+
window_size[1] = height;
53+
54+
/* Go ahead and create the window wrapper instance */
55+
result_ptr.reset(
56+
new Anvil::WindowGeneric(type,
57+
windowHandle,
58+
"Generic wrapper window instance",
59+
window_size[0],
60+
window_size[1],
61+
nullptr) /* present_callback_func_ptr */
62+
);
63+
64+
if (result_ptr)
65+
{
66+
if (!dynamic_cast<WindowGeneric*>(result_ptr.get() )->init(visible))
67+
{
68+
result_ptr.reset();
69+
}
70+
}
71+
72+
end:
73+
return result_ptr;
74+
}
75+
76+
void Anvil::WindowGeneric::close() {}
77+
78+
/** Creates a new system window and prepares it for usage. */
79+
bool Anvil::WindowGeneric::init(const bool& in_visible)
80+
{
81+
bool result = false;
82+
const char* window_class_name = (m_closable) ? "Anvil window (closable)"
83+
: "Anvil window (non-closable)";
84+
85+
result = true;
86+
return result;
87+
}
88+
89+
void Anvil::WindowGeneric::run() {}
90+
91+
void Anvil::WindowGeneric::set_title(const std::string& in_new_title) {}

src/wrappers/rendering_surface.cpp

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "misc/object_tracker.h"
2525
#include "misc/rendering_surface_create_info.h"
2626
#include "misc/window.h"
27+
#include "misc/window_generic.h"
2728
#include "wrappers/instance.h"
2829
#include "wrappers/physical_device.h"
2930
#include "wrappers/pipeline_layout.h"
@@ -388,38 +389,87 @@ bool Anvil::RenderingSurface::init()
388389
{
389390
auto window_ptr = m_create_info_ptr->get_window_ptr();
390391

391-
#if defined(ANVIL_INCLUDE_WIN3264_WINDOW_SYSTEM_SUPPORT) && defined(_WIN32)
392-
{
393-
VkWin32SurfaceCreateInfoKHR surface_create_info;
394-
395-
surface_create_info.flags = 0;
396-
surface_create_info.hinstance = GetModuleHandle(nullptr);
397-
surface_create_info.hwnd = window_ptr->get_handle();
398-
surface_create_info.pNext = nullptr;
399-
surface_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
400-
401-
result = instance_ptr->get_extension_khr_win32_surface_entrypoints().vkCreateWin32SurfaceKHR(instance_ptr->get_instance_vk(),
402-
&surface_create_info,
403-
nullptr, /* pAllocator */
404-
&m_surface);
405-
}
406-
#endif
407-
#if defined(ANVIL_INCLUDE_XCB_WINDOW_SYSTEM_SUPPORT) && !defined(_WIN32)
408-
{
409-
VkXcbSurfaceCreateInfoKHR surface_create_info;
410-
411-
surface_create_info.flags = 0;
412-
surface_create_info.window = window_ptr->get_handle();
413-
surface_create_info.connection = static_cast<xcb_connection_t*>(window_ptr->get_connection());
414-
surface_create_info.pNext = nullptr;
415-
surface_create_info.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
416-
417-
result = instance_ptr->get_extension_khr_xcb_surface_entrypoints().vkCreateXcbSurfaceKHR(instance_ptr->get_instance_vk(),
418-
&surface_create_info,
419-
nullptr, /* pAllocator */
420-
&m_surface);
421-
}
422-
#endif
392+
auto *generic_ptr = dynamic_cast<const Anvil::WindowGeneric *>(window_ptr);
393+
if(generic_ptr != nullptr)
394+
{
395+
auto type = generic_ptr->get_type();
396+
#ifdef _WIN32
397+
if(type != Anvil::WindowGeneric::Type::Win32)
398+
result = VK_ERROR_INITIALIZATION_FAILED;
399+
else {
400+
VkWin32SurfaceCreateInfoKHR surface_create_info;
401+
402+
surface_create_info.flags = 0;
403+
surface_create_info.hinstance = GetModuleHandle(nullptr);
404+
surface_create_info.hwnd = window_ptr->get_handle();
405+
surface_create_info.pNext = nullptr;
406+
surface_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
407+
408+
result = instance_ptr->get_extension_khr_win32_surface_entrypoints().vkCreateWin32SurfaceKHR(instance_ptr->get_instance_vk(), &surface_create_info, nullptr, /* pAllocator */
409+
&m_surface);
410+
}
411+
#else
412+
if(type == Anvil::WindowGeneric::Type::Xcb) {
413+
VkXcbSurfaceCreateInfoKHR surface_create_info;
414+
415+
surface_create_info.flags = 0;
416+
surface_create_info.window = window_ptr->get_handle();
417+
surface_create_info.connection = static_cast<xcb_connection_t *>(window_ptr->get_connection());
418+
surface_create_info.pNext = nullptr;
419+
surface_create_info.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
420+
421+
result = instance_ptr->get_extension_khr_xcb_surface_entrypoints().vkCreateXcbSurfaceKHR(instance_ptr->get_instance_vk(), &surface_create_info, nullptr, /* pAllocator */
422+
&m_surface);
423+
}
424+
else if(type == Anvil::WindowGeneric::Type::Wayland) {
425+
VkWaylandSurfaceCreateInfoKHR surface_create_info;
426+
surface_create_info.flags = 0;
427+
surface_create_info.display = static_cast<wl_display *>(window_ptr->get_connection());
428+
surface_create_info.surface = window_ptr->get_handle();
429+
surface_create_info.pNext = nullptr;
430+
surface_create_info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
431+
result = instance_ptr->get_extension_khr_wayland_surface_entrypoints().vkCreateWaylandSurfaceKHR(instance_ptr->get_instance_vk(), &surface_create_info, nullptr, /* pAllocator */
432+
&m_surface);
433+
}
434+
else
435+
result = VK_ERROR_INITIALIZATION_FAILED;
436+
#endif
437+
}
438+
else
439+
{
440+
#if defined(ANVIL_INCLUDE_WIN3264_WINDOW_SYSTEM_SUPPORT) && defined(_WIN32)
441+
{
442+
VkWin32SurfaceCreateInfoKHR surface_create_info;
443+
444+
surface_create_info.flags = 0;
445+
surface_create_info.hinstance = GetModuleHandle(nullptr);
446+
surface_create_info.hwnd = window_ptr->get_handle();
447+
surface_create_info.pNext = nullptr;
448+
surface_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
449+
450+
result = instance_ptr->get_extension_khr_win32_surface_entrypoints().vkCreateWin32SurfaceKHR(instance_ptr->get_instance_vk(),
451+
&surface_create_info,
452+
nullptr, /* pAllocator */
453+
&m_surface);
454+
}
455+
#endif
456+
#if defined(ANVIL_INCLUDE_XCB_WINDOW_SYSTEM_SUPPORT) && !defined(_WIN32)
457+
{
458+
VkXcbSurfaceCreateInfoKHR surface_create_info;
459+
460+
surface_create_info.flags = 0;
461+
surface_create_info.window = window_ptr->get_handle();
462+
surface_create_info.connection = static_cast<xcb_connection_t*>(window_ptr->get_connection());
463+
surface_create_info.pNext = nullptr;
464+
surface_create_info.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
465+
466+
result = instance_ptr->get_extension_khr_xcb_surface_entrypoints().vkCreateXcbSurfaceKHR(instance_ptr->get_instance_vk(),
467+
&surface_create_info,
468+
nullptr, /* pAllocator */
469+
&m_surface);
470+
}
471+
#endif
472+
}
423473

424474
anvil_assert_vk_call_succeeded(result);
425475
if (is_vk_call_successful(result) )

0 commit comments

Comments
 (0)