1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/*
** Copyright (c) 2020 LunarG, Inc.
**
** 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 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 "application/xlib_window.h"
#include "application/application.h"
#include "util/logging.h"
#include "X11/Xatom.h"
#include <cassert>
#include <cstdlib>
#include <limits>
#include <unistd.h>
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(application)
XlibWindow::XlibWindow(XlibContext* xlib_context) :
xlib_context_(xlib_context), display_(nullptr), window_(0), width_(0), height_(0),
screen_width_(std::numeric_limits<uint32_t>::max()), screen_height_(std::numeric_limits<uint32_t>::max()),
visible_(false), fullscreen_(false)
{
assert(xlib_context_ != nullptr);
}
XlibWindow::~XlibWindow() {}
bool XlibWindow::Create(
const std::string& title, const int32_t xpos, const int32_t ypos, const uint32_t width, const uint32_t height)
{
display_ = xlib_context_->OpenDisplay();
const auto xlib = xlib_context_->GetXlibFunctionTable();
const auto screen = DefaultScreen(display_);
const auto root = RootWindow(display_, screen);
xlib_context_->RegisterXlibWindow(this);
// Get screen size
XWindowAttributes root_attributes;
xlib.GetWindowAttributes(display_, root, &root_attributes);
screen_width_ = root_attributes.width;
screen_height_ = root_attributes.height;
// Determine if fullscreen mode is required.
int32_t x = xpos;
int32_t y = ypos;
bool go_fullscreen = false;
if ((root_attributes.height <= height) || (root_attributes.width <= width))
{
if ((screen_height_ == height) || (screen_width_ == width))
{
go_fullscreen = true;
// Place fullscreen window at 0, 0.
x = 0;
y = 0;
}
else
{
GFXRECON_LOG_WARNING(
"Requested window size (%ux%u) exceeds current screen size (%ux%u); replay may fail due to "
"inability to create a window of the appropriate size.",
width,
height,
screen_width_,
screen_height_);
}
}
// Set window event mask to receive keyboard events
XSetWindowAttributes attributes = {};
attributes.event_mask = KeyPressMask | KeyReleaseMask;
unsigned long attribute_mask = CWEventMask;
// Create window
window_ = xlib.CreateWindow(display_,
RootWindow(display_, screen),
x,
y,
width,
height,
0,
CopyFromParent,
InputOutput,
CopyFromParent,
attribute_mask,
&attributes);
// Set window title
SetTitle(title);
// Display the window
SetVisibility(true);
// Enable fullscreen if necessary.
if (go_fullscreen)
{
SetFullscreen(true);
}
return true;
}
bool XlibWindow::Destroy()
{
if (window_ != 0)
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
SetFullscreen(false);
SetVisibility(false);
xlib.DestroyWindow(display_, window_);
xlib.Sync(display_, true);
xlib_context_->CloseDisplay(display_);
display_ = nullptr;
xlib_context_->UnregisterXlibWindow(this);
window_ = 0;
return true;
}
return false;
}
void XlibWindow::SetTitle(const std::string& title)
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
xlib.StoreName(display_, window_, title.c_str());
}
void XlibWindow::SetPosition(const int32_t x, const int32_t y)
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
xlib.MoveWindow(display_, window_, x, y);
}
void XlibWindow::SetSize(const uint32_t width, const uint32_t height)
{
if ((width != width_) || (height != height_))
{
if ((screen_width_ == width) || (screen_height_ == height))
{
SetFullscreen(true);
}
else
{
if ((screen_height_ < height) || (screen_width_ < width))
{
GFXRECON_LOG_WARNING(
"Requested window size (%ux%u) exceeds current screen size (%ux%u); replay may fail due to "
"inability to create a window of the appropriate size.",
width,
height,
screen_width_,
screen_height_);
}
SetFullscreen(false);
const auto xlib = xlib_context_->GetXlibFunctionTable();
xlib.ResizeWindow(display_, window_, width, height);
xlib.Sync(display_, true);
}
// Sleep to ensure window resize has completed.
usleep(50000); // 0.05 seconds (same as vktrace)
}
}
void XlibWindow::SetSizePreTransform(const uint32_t width, const uint32_t height, const uint32_t pre_transform)
{
GFXRECON_UNREFERENCED_PARAMETER(pre_transform);
SetSize(width, height);
}
void XlibWindow::SetFullscreen(bool fullscreen)
{
if (fullscreen != fullscreen_)
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
const auto screen = DefaultScreen(display_);
const auto root = RootWindow(display_, screen);
XEvent event;
event.type = ClientMessage;
event.xclient.window = window_;
event.xclient.format = 32;
event.xclient.message_type = xlib.InternAtom(display_, "_NET_WM_STATE", false);
event.xclient.data.l[0] = fullscreen ? 1 : 0;
event.xclient.data.l[1] = xlib.InternAtom(display_, "_NET_WM_STATE_FULLSCREEN", false);
event.xclient.data.l[2] = 0;
event.xclient.data.l[3] = 0;
event.xclient.data.l[4] = 0;
xlib.SendEvent(display_, root, false, (SubstructureNotifyMask | SubstructureRedirectMask), &event);
// Use same compositor workaround as XcbWindow for GNOME/NVIDIA VK_ERROR_OUT_OF_DATE_KHR issue
int32_t bypass = fullscreen ? 2 : 0;
xlib.ChangeProperty(display_,
window_,
xlib.InternAtom(display_, "_NET_WM_BYPASS_COMPOSITOR", false),
XA_CARDINAL,
32,
PropModeReplace,
reinterpret_cast<unsigned char*>(&bypass),
1);
xlib.Sync(display_, true);
// Sleep to ensure window resize has completed.
usleep(50000); // 0.05 seconds (same as vktrace)
fullscreen_ = fullscreen;
}
}
void XlibWindow::SetVisibility(bool show)
{
if (show != visible_)
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
if (show)
{
xlib.MapWindow(display_, window_);
}
else
{
xlib.UnmapWindow(display_, window_);
}
xlib.Sync(display_, true);
visible_ = show;
}
}
void XlibWindow::SetForeground()
{
const auto xlib = xlib_context_->GetXlibFunctionTable();
const auto screen = DefaultScreen(display_);
const auto root = RootWindow(display_, screen);
XEvent event;
event.type = ClientMessage;
event.xclient.window = window_;
event.xclient.format = 32;
event.xclient.message_type = xlib.InternAtom(display_, "_NET_ACTIVE_WINDOW", false);
event.xclient.data.l[0] = 1;
event.xclient.data.l[1] = 0;
event.xclient.data.l[2] = 0;
event.xclient.data.l[3] = 0;
event.xclient.data.l[4] = 0;
xlib.SendEvent(display_, root, false, (SubstructureNotifyMask | SubstructureRedirectMask), &event);
}
bool XlibWindow::GetNativeHandle(HandleType type, void** handle)
{
assert(handle != nullptr);
switch (type)
{
case Window::kXlibDisplay:
*handle = reinterpret_cast<void*>(display_);
return true;
case Window::kXlibWindow:
*handle = reinterpret_cast<void*>(window_);
return true;
default:
return false;
}
}
std::string XlibWindow::GetWsiExtension() const
{
return VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
}
VkResult XlibWindow::CreateSurface(const encode::InstanceTable* table,
VkInstance instance,
VkFlags flags,
VkSurfaceKHR* pSurface)
{
if (table != nullptr)
{
VkXlibSurfaceCreateInfoKHR create_info{
VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, nullptr, flags, display_, window_
};
return table->CreateXlibSurfaceKHR(instance, &create_info, nullptr, pSurface);
}
return VK_ERROR_INITIALIZATION_FAILED;
}
void XlibWindow::DestroySurface(const encode::InstanceTable* table, VkInstance instance, VkSurfaceKHR surface)
{
if (table != nullptr)
{
table->DestroySurfaceKHR(instance, surface, nullptr);
}
}
XlibWindowFactory::XlibWindowFactory(XlibContext* context) : xlib_context_(context)
{
assert(xlib_context_ != nullptr);
}
decode::Window* XlibWindowFactory::Create(const int32_t x, const int32_t y, const uint32_t width, const uint32_t height)
{
assert(xlib_context_);
decode::Window* window = new XlibWindow(xlib_context_);
auto application = xlib_context_->GetApplication();
assert(application);
window->Create(application->GetName(), x, y, width, height);
return window;
}
void XlibWindowFactory::Destroy(decode::Window* window)
{
if (window != nullptr)
{
window->Destroy();
delete window;
}
}
VkBool32 XlibWindowFactory::GetPhysicalDevicePresentationSupport(const encode::InstanceTable* table,
VkPhysicalDevice physical_device,
uint32_t queue_family_index)
{
const auto display = xlib_context_->OpenDisplay();
const auto xlib = xlib_context_->GetXlibFunctionTable();
// Get visual ID which will be inherited from parent at window creation
XWindowAttributes root_attributes;
xlib.GetWindowAttributes(display, RootWindow(display, DefaultScreen(display)), &root_attributes);
const auto visual_id = xlib.VisualIDFromVisual(root_attributes.visual);
const auto result =
table->GetPhysicalDeviceXlibPresentationSupportKHR(physical_device, queue_family_index, display, visual_id);
xlib_context_->CloseDisplay(display);
return result;
}
GFXRECON_END_NAMESPACE(application)
GFXRECON_END_NAMESPACE(gfxrecon)
|