Project
Loading...
Searching...
No Matches
GPUDisplayFrontendGlfw.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
18#include "GPULogging.h"
19
20#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
21#define GLFW_INCLUDE_VULKAN
22#endif
23#include <GLFW/glfw3.h>
24
25#include <cstdio>
26#include <cstring>
27#include <unistd.h>
28
29using namespace o2::gpu;
30
36
37static GPUDisplayFrontendGlfw* me = nullptr;
38
39int32_t GPUDisplayFrontendGlfw::GetKey(int32_t key)
40{
41 if (key == GLFW_KEY_KP_SUBTRACT) {
42 return ('-');
43 }
44 if (key == GLFW_KEY_KP_ADD) {
45 return ('+');
46 }
47 if (key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT) {
48 return (KEY_SHIFT);
49 }
50 if (key == GLFW_KEY_LEFT_ALT) {
51 return (KEY_ALT);
52 }
53 if (key == GLFW_KEY_RIGHT_ALT) {
54 return (KEY_RALT);
55 }
56 if (key == GLFW_KEY_LEFT_CONTROL) {
57 return (KEY_CTRL);
58 }
59 if (key == GLFW_KEY_RIGHT_CONTROL) {
60 return (KEY_RCTRL);
61 }
62 if (key == GLFW_KEY_UP) {
63 return (KEY_UP);
64 }
65 if (key == GLFW_KEY_DOWN) {
66 return (KEY_DOWN);
67 }
68 if (key == GLFW_KEY_LEFT) {
69 return (KEY_LEFT);
70 }
71 if (key == GLFW_KEY_RIGHT) {
72 return (KEY_RIGHT);
73 }
74 if (key == GLFW_KEY_PAGE_UP) {
75 return (KEY_PAGEUP);
76 }
77 if (key == GLFW_KEY_PAGE_DOWN) {
78 return (KEY_PAGEDOWN);
79 }
80 if (key == GLFW_KEY_ESCAPE) {
81 return (KEY_ESCAPE);
82 }
83 if (key == GLFW_KEY_SPACE) {
84 return (KEY_SPACE);
85 }
86 if (key == GLFW_KEY_HOME) {
87 return (KEY_HOME);
88 }
89 if (key == GLFW_KEY_END) {
90 return (KEY_END);
91 }
92 if (key == GLFW_KEY_INSERT) {
93 return (KEY_INSERT);
94 }
95 if (key == GLFW_KEY_BACKSPACE) {
96 return (KEY_BACKSPACE);
97 }
98 if (key == GLFW_KEY_DELETE) {
99 return (KEY_DELETE);
100 }
101 if (key == GLFW_KEY_GRAVE_ACCENT) { // Key left of 1 (named after the US layout): console key (^), independent of the layout
102 return ('^');
103 }
104 if (key == GLFW_KEY_ENTER) {
105 return (KEY_ENTER);
106 }
107 if (key == GLFW_KEY_F1) {
108 return (KEY_F1);
109 }
110 if (key == GLFW_KEY_F2) {
111 return (KEY_F2);
112 }
113 if (key == GLFW_KEY_F3) {
114 return (KEY_F3);
115 }
116 if (key == GLFW_KEY_F4) {
117 return (KEY_F4);
118 }
119 if (key == GLFW_KEY_F5) {
120 return (KEY_F5);
121 }
122 if (key == GLFW_KEY_F6) {
123 return (KEY_F6);
124 }
125 if (key == GLFW_KEY_F7) {
126 return (KEY_F7);
127 }
128 if (key == GLFW_KEY_F8) {
129 return (KEY_F8);
130 }
131 if (key == GLFW_KEY_F9) {
132 return (KEY_F9);
133 }
134 if (key == GLFW_KEY_F10) {
135 return (KEY_F10);
136 }
137 if (key == GLFW_KEY_F11) {
138 return (KEY_F11);
139 }
140 if (key == GLFW_KEY_F12) {
141 return (KEY_F12);
142 }
143 return (0);
144}
145
146void GPUDisplayFrontendGlfw::GetKey(int32_t key, int32_t scancode, int32_t mods, int32_t& keyOut, int32_t& keyPressOut)
147{
148 int32_t specialKey = GetKey(key);
149 const char* str = glfwGetKeyName(key, scancode);
150 char localeKey = str ? str[0] : 0;
151 if ((mods & GLFW_MOD_SHIFT) && localeKey >= 'a' && localeKey <= 'z') {
152 localeKey += 'A' - 'a';
153 }
154 // GPUInfo("Key: key %d (%c) scancode %d -> %d (%c) special %d (%c)", key, (char)key, scancode, (int32_t)localeKey, localeKey, specialKey, (char)specialKey);
155
156 if (specialKey) {
157 keyOut = keyPressOut = specialKey;
158 } else {
159 keyOut = keyPressOut = (uint8_t)localeKey;
160 if (keyPressOut >= 'a' && keyPressOut <= 'z') {
161 keyPressOut += 'A' - 'a';
162 }
163 }
164}
165
166void GPUDisplayFrontendGlfw::error_callback(int32_t error, const char* description) { fprintf(stderr, "Error: %s\n", description); }
167
168void GPUDisplayFrontendGlfw::key_callback(GLFWwindow* window, int32_t key, int32_t scancode, int32_t action, int32_t mods)
169{
170 int32_t handleKey = 0, keyPress = 0;
171 GetKey(key, scancode, mods, handleKey, keyPress);
172 if (handleKey < 32) {
173 if (action == GLFW_PRESS || action == GLFW_REPEAT) { // Repeat as the other frontends (e.g. backspace in the console), characters are repeated by char_callback
174 me->mKeys[keyPress] = keyPress == KEY_SHIFT || !me->consoleShown(); // Held keys only without console, SHIFT also with it (scrolling)
175 me->HandleKey(handleKey);
176 } else if (action == GLFW_RELEASE) {
177 me->mKeys[keyPress] = false;
178 }
179 } else if (handleKey < 256) {
180 if (action == GLFW_PRESS) {
181 me->mLastKeyDown = handleKey;
182 if (key == GLFW_KEY_GRAVE_ACCENT) { // Console key, its character (if not a dead key) follows in the same glfwPollEvents
183 me->mConsoleKeyPressed = true;
184 me->HandleKey('^');
185 }
186 } else if (action == GLFW_RELEASE) {
187 keyPress = (uint8_t)me->mGLFWKeyDownMap[handleKey];
188 me->mKeys[keyPress] = false;
189 }
190 }
191}
192
193void GPUDisplayFrontendGlfw::char_callback(GLFWwindow* window, uint32_t codepoint)
194{
195 // GPUInfo("Key (char callback): %d %c - key: %d", codepoint, (char)codepoint, (int32_t)me->mLastKeyDown);
196 if (me->mConsoleKeyPressed) { // Character of the console key, handled in key_callback
197 me->mConsoleKeyPressed = false;
198 return;
199 }
200 if (codepoint < 256) {
201 uint8_t keyPress = codepoint;
202 if (keyPress >= 'a' && keyPress <= 'z') {
203 keyPress += 'A' - 'a';
204 }
205 me->mGLFWKeyDownMap[me->mLastKeyDown] = keyPress;
206 me->mKeys[keyPress] = keyPress == KEY_SHIFT || !me->consoleShown(); // Held keys only without console, SHIFT also with it (scrolling)
207 me->HandleKey(codepoint);
208 }
209}
210
211void GPUDisplayFrontendGlfw::mouseButton_callback(GLFWwindow* window, int32_t button, int32_t action, int32_t mods)
212{
213 if (action == GLFW_PRESS) {
214 if (button == 0) {
215 me->mMouseDn = true;
216 } else if (button == 1) {
217 me->mMouseDnR = true;
218 } else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
219 me->mMouseDnM = true;
220 }
221 me->mMouseDnX = me->mMouseMvX;
222 me->mMouseDnY = me->mMouseMvY;
223 } else if (action == GLFW_RELEASE) {
224 if (button == 0) {
225 me->mMouseDn = false;
226 } else if (button == 1) {
227 me->mMouseDnR = false;
228 } else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
229 me->mMouseDnM = false;
230 }
231 }
232}
233
234void GPUDisplayFrontendGlfw::scroll_callback(GLFWwindow* window, double x, double y) { me->mMouseWheel += y * 100; }
235
236void GPUDisplayFrontendGlfw::cursorPos_callback(GLFWwindow* window, double x, double y)
237{
238 me->mMouseMvX = x * me->mPixelRatio; // Window coordinates to framebuffer pixels
239 me->mMouseMvY = y * me->mPixelRatio;
240}
241
242void GPUDisplayFrontendGlfw::resize_callback(GLFWwindow* window, int32_t width, int32_t height) // Framebuffer size in pixels
243{
244 int32_t windowWidth, windowHeight;
245 glfwGetWindowSize(window, &windowWidth, &windowHeight);
246 me->mPixelRatio = windowWidth ? (double)width / windowWidth : 1.;
247 me->ResizeScene(width, height);
248}
249
250void GPUDisplayFrontendGlfw::focus_callback(GLFWwindow* window, int32_t focused)
251{
252 if (!focused) {
253 me->focusLost();
254 }
255}
256
257void GPUDisplayFrontendGlfw::contentScale_callback(GLFWwindow* window, float xscale, float yscale)
258{
259 me->mScale = xscale;
260}
261
262void GPUDisplayFrontendGlfw::maximize_callback(GLFWwindow* window, int32_t maximized)
263{
264 me->setWindowState(glfwGetWindowMonitor(window) != nullptr, maximized);
265 if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) { // Called before the resize. GLFW keeps the maximized size if the compositor leaves the size to the client when unmaximizing, so restore the last windowed size
266 if (maximized) {
267 glfwGetWindowSize(window, &me->mWindowedWidth, &me->mWindowedHeight);
268 } else {
269 me->mRestoreWindowedSize = true; // Must be applied after GLFW has processed the configure event, otherwise GLFW resizes to the pending maximized size again
270 }
271 }
272}
273
274int32_t GPUDisplayFrontendGlfw::FrontendMain()
275{
276 me = this;
277
278 if (!glfwInit()) {
279 fprintf(stderr, "Error initializing glfw\n");
280 return (-1);
281 }
282 glfwSetErrorCallback(error_callback);
283
284 if (backend()->backendType() == GPUDisplayBackend::TYPE_VULKAN) {
285 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
286 }
287 if (backend()->backendType() == GPUDisplayBackend::TYPE_OPENGL) {
288 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GL_MIN_VERSION_MAJOR);
289 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GL_MIN_VERSION_MINOR);
290 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 0);
291 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
292 }
293 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // Initial size scaled by the content scale on X11 / Win32
294 mWindow = glfwCreateWindow(INIT_WIDTH, INIT_HEIGHT, DISPLAY_WINDOW_NAME, nullptr, nullptr);
295 if (!mWindow) {
296 fprintf(stderr, "Error creating glfw window\n");
297 glfwTerminate();
298 return (-1);
299 }
300 if (backend()->backendType() == GPUDisplayBackend::TYPE_OPENGL) {
301 glfwMakeContextCurrent(mWindow);
302 }
303
304 glfwSetKeyCallback(mWindow, key_callback);
305 glfwSetCharCallback(mWindow, char_callback);
306 glfwSetMouseButtonCallback(mWindow, mouseButton_callback);
307 glfwSetScrollCallback(mWindow, scroll_callback);
308 glfwSetCursorPosCallback(mWindow, cursorPos_callback);
309 glfwSetFramebufferSizeCallback(mWindow, resize_callback);
310 glfwSetWindowMaximizeCallback(mWindow, maximize_callback);
311 glfwSetWindowFocusCallback(mWindow, focus_callback);
312 float xscale, yscale;
313 glfwGetWindowContentScale(mWindow, &xscale, &yscale);
314 contentScale_callback(mWindow, xscale, yscale);
315 glfwSetWindowContentScaleCallback(mWindow, contentScale_callback);
316 const float limitScale = (glfwGetPlatform() == GLFW_PLATFORM_X11 || glfwGetPlatform() == GLFW_PLATFORM_WIN32) ? mScale : 1.f; // Window coordinates are pixels on X11 / Win32, logical on Wayland / macOS
317 glfwSetWindowSizeLimits(mWindow, MIN_WIDTH * limitScale, MIN_HEIGHT * limitScale, GLFW_DONT_CARE, GLFW_DONT_CARE);
318 int32_t fbWidth, fbHeight, windowWidth, windowHeight;
319 glfwGetFramebufferSize(mWindow, &fbWidth, &fbHeight);
320 glfwGetWindowSize(mWindow, &windowWidth, &windowHeight);
321 mPixelRatio = windowWidth ? (double)fbWidth / windowWidth : 1.; // Updated in resize_callback, which must not be called before InitDisplay
324
325 if (mBackend->ExtInit()) {
326 fprintf(stderr, "Error initializing GL extension wrapper\n");
327 return (-1);
328 }
329
330 if (InitDisplay()) {
331 fprintf(stderr, "Error in GLFW display initialization\n");
332 return (1);
333 }
334
335 int32_t retVal = 0;
336 while (!glfwWindowShouldClose(mWindow)) {
338 fprintf(stderr, "Error drawing GL scene\n");
339 retVal = 1;
340 break; // Cleanup below
341 }
342 if (backend()->backendType() == GPUDisplayBackend::TYPE_OPENGL) {
343 glfwSwapBuffers(mWindow);
344 }
346 glfwPollEvents();
347 mConsoleKeyPressed = false; // No character followed (dead key)
348 if (mRestoreWindowedSize) {
349 mRestoreWindowedSize = false;
350 if (mWindowedWidth > 0 && mWindowedHeight > 0 && !glfwGetWindowAttrib(mWindow, GLFW_MAXIMIZED)) {
351 glfwSetWindowSize(mWindow, mWindowedWidth, mWindowedHeight);
352 }
353 }
354 }
355
356 ExitDisplay();
357 {
358 std::lock_guard lock(mExitStateMutex);
360 glfwDestroyWindow(mWindow);
361 glfwTerminate();
362 mDisplayRunning = false;
363 mDisplayStoppedCV.notify_all();
364 }
365
366 return retVal;
367}
368
370{
371 glfwSetWindowShouldClose(mWindow, true);
372}
373
375{
376 GPUInfo("Setting Full Screen %d", (int32_t)set);
377 if (set) {
378 glfwGetWindowPos(mWindow, &mWindowX, &mWindowY);
379 glfwGetWindowSize(mWindow, &mWindowWidth, &mWindowHeight);
380 GLFWmonitor* primary = glfwGetPrimaryMonitor();
381 const GLFWvidmode* mode = glfwGetVideoMode(primary);
382 glfwSetWindowMonitor(mWindow, primary, 0, 0, mode->width, mode->height, mode->refreshRate);
383 } else {
384 glfwSetWindowMonitor(mWindow, nullptr, mWindowX, mWindowY, mWindowWidth, mWindowHeight, GLFW_DONT_CARE);
385 }
386}
387
389{
390 if (set) {
391 glfwMaximizeWindow(mWindow);
392 } else {
393 glfwRestoreWindow(mWindow);
394 }
395}
396
398{
399 if (canSetVSync()) { // No GL context with Vulkan
400 glfwSwapInterval(enable);
401 }
402}
403
405
407{
408 GLFWmonitor* monitor = glfwGetWindowMonitor(mWindow) ? glfwGetWindowMonitor(mWindow) : glfwGetPrimaryMonitor(); // Monitor of a windowed window is not known, use the primary one
409 const GLFWvidmode* mode = monitor ? glfwGetVideoMode(monitor) : nullptr;
410 return mode ? mode->refreshRate : 0.f;
411}
412
414{
415 glfwGetFramebufferSize(mWindow, &width, &height);
416}
417
419{
420#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
421 return glfwCreateWindowSurface(*(VkInstance*)instance, mWindow, nullptr, (VkSurfaceKHR*)surface) != VK_SUCCESS;
422#else
423 return 1;
424#endif
425}
426
428{
429 uint32_t glfwExtensionCount = 0;
430#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
431 p = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
432#endif
433 return glfwExtensionCount;
434}
header::DataDescription description
unsigned int uint32_t
int32_t retVal
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder int32_t
char error[CURL_ERROR_SIZE]
const Acts::Surface * surface
StringRef key
backendTypes backendType() const
void getSize(int32_t &width, int32_t &height) override
uint32_t getReqVulkanExtensions(const char **&p) override
int32_t getVulkanSurface(void *instance, void *surface) override
void setDisplayControl(int32_t v) override
int32_t HandleUpdatesAndDrawGLScene(bool suspended=false)
std::condition_variable mDisplayStoppedCV
static constexpr const char * DISPLAY_WINDOW_NAME
static constexpr int32_t INIT_WIDTH
static constexpr int32_t GL_MIN_VERSION_MAJOR
static constexpr int32_t GL_MIN_VERSION_MINOR
int32_t InitDisplay(bool initFailure=false)
static constexpr int32_t MIN_WIDTH
static constexpr int32_t INIT_HEIGHT
static constexpr int32_t MIN_HEIGHT
std::atomic< bool > mDisplayRunning
GLint GLenum GLint x
Definition glcorearb.h:403
GLint GLsizei GLsizei height
Definition glcorearb.h:270
GLint GLsizei width
Definition glcorearb.h:270
GLint y
Definition glcorearb.h:270
constexpr value_T me
Definition TrackUtils.h:128
const std::string str