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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
#include "std/target_os.hpp"
#if !defined(OMIM_OS_LINUX)
#error Unsupported OS
#endif
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3native.h>
#include <X11/X.h>
#include <dlfcn.h>
#include <vulkan_wrapper.h>
// Workaround for TestFunction::Always compilation issue:
// /usr/include/X11/X.h:441:33: note: expanded from macro 'Always'
#undef Always
// Workaround for storage::Status compilation issue:
// /usr/include/X11/Xlib.h:83:16: note: expanded from macro 'Status'
#undef Status
#include "drape/vulkan/vulkan_context_factory.hpp"
#include "drape/gl_functions.hpp"
#include "drape/gl_includes.hpp"
#include "drape/oglcontext.hpp"
#include <array>
#include <atomic>
#include <memory>
class LinuxVulkanContextFactory : public dp::vulkan::VulkanContextFactory
{
public:
LinuxVulkanContextFactory() : dp::vulkan::VulkanContextFactory(1, 33, false) {}
void SetSurface(Display * display, Window window)
{
VkXlibSurfaceCreateInfoKHR const createInfo = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.pNext = nullptr,
.flags = 0,
.dpy = display,
.window = window,
};
VkResult statusCode;
CHECK(vkCreateXlibSurfaceKHR, ());
statusCode = vkCreateXlibSurfaceKHR(m_vulkanInstance, &createInfo, nullptr, &m_surface);
if (statusCode != VK_SUCCESS)
{
LOG_ERROR_VK_CALL(vkCreateXlibSurfaceKHR, statusCode);
return;
}
uint32_t const renderingQueueIndex = m_drawContext->GetRenderingQueueFamilyIndex();
VkBool32 supportsPresent;
statusCode = vkGetPhysicalDeviceSurfaceSupportKHR(m_gpu, renderingQueueIndex, m_surface, &supportsPresent);
if (statusCode != VK_SUCCESS)
{
LOG_ERROR_VK_CALL(vkGetPhysicalDeviceSurfaceSupportKHR, statusCode);
return;
}
CHECK_EQUAL(supportsPresent, VK_TRUE, ());
CHECK(QuerySurfaceSize(), ());
if (m_drawContext)
m_drawContext->SetSurface(m_surface, m_surfaceFormat, m_surfaceCapabilities);
}
void ResetSurface()
{
if (m_drawContext)
m_drawContext->ResetSurface(false);
vkDestroySurfaceKHR(m_vulkanInstance, m_surface, nullptr);
}
};
// Based on: https://github.com/glfw/glfw/blob/master/src/glx_context.c
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#define GLX_PBUFFER_HEIGHT 0x8040
#define GLX_PBUFFER_WIDTH 0x8041
#define GLX_DOUBLEBUFFER 5
#define GLX_DRAWABLE_TYPE 0x8010
#define GLX_RENDER_TYPE 0x8011
#define GLX_WINDOW_BIT 0x00000001
#define GLX_PBUFFER_BIT 0x00000004
#define GLX_RGBA_BIT 0x00000001
#define GLX_RED_SIZE 8
#define GLX_GREEN_SIZE 9
#define GLX_BLUE_SIZE 10
#define GLX_ALPHA_SIZE 11
#define GLX_DEPTH_SIZE 12
#define GLX_STENCIL_SIZE 13
typedef XID GLXDrawable;
typedef struct __GLXcontext * GLXContext;
typedef XID GLXPbuffer;
typedef struct __GLXFBConfig * GLXFBConfig;
typedef void (*__GLXextproc)(void);
typedef __GLXextproc (*PFNGLXGETPROCADDRESSPROC)(GLubyte const * procName);
typedef int (*PFNXFREE)(void *);
typedef GLXFBConfig * (*PFNGLXCHOOSEFBCONFIGPROC)(Display *, int, int const *, int *);
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSARB)(Display *, GLXFBConfig, GLXContext, Bool, int const *);
typedef void (*PFNGLXDESTROYCONTEXT)(Display *, GLXContext);
typedef GLXPbuffer (*PFNGLXCREATEPBUFFERPROC)(Display *, GLXFBConfig, int const *);
typedef void (*PFNGLXDESTROYPBUFFER)(Display *, GLXPbuffer);
typedef Bool (*PFNGLXMAKECURRENTPROC)(Display *, GLXDrawable, GLXContext);
typedef void (*PFNGLXSWAPBUFFERSPROC)(Display *, GLXDrawable);
struct GLXFunctions
{
GLXFunctions()
{
std::array<char const *, 3> libs = {
"libGLX.so.0",
"libGL.so.1",
"libGL.so",
};
for (char const * lib : libs)
{
m_module = dlopen(lib, RTLD_LAZY | RTLD_LOCAL);
if (m_module)
break;
}
CHECK(m_module != nullptr, ("Failed to initialize GLX"));
XFree = loadFunction<PFNXFREE>("XFree");
glXGetProcAddress = loadFunction<PFNGLXGETPROCADDRESSPROC>("glXGetProcAddress");
glXGetProcAddressARB = loadFunction<PFNGLXGETPROCADDRESSPROC>("glXGetProcAddressARB");
glXChooseFBConfig = loadGlxFunction<PFNGLXCHOOSEFBCONFIGPROC>("glXChooseFBConfig");
glXCreateContextAttribsARB = loadGlxFunction<PFNGLXCREATECONTEXTATTRIBSARB>("glXCreateContextAttribsARB");
glXDestroyContext = loadGlxFunction<PFNGLXDESTROYCONTEXT>("glXDestroyContext");
glXCreatePbuffer = loadGlxFunction<PFNGLXCREATEPBUFFERPROC>("glXCreatePbuffer");
glXDestroyPbuffer = loadGlxFunction<PFNGLXDESTROYPBUFFER>("glXDestroyPbuffer");
glXMakeCurrent = loadGlxFunction<PFNGLXMAKECURRENTPROC>("glXMakeCurrent");
glXSwapBuffers = loadGlxFunction<PFNGLXSWAPBUFFERSPROC>("glXSwapBuffers");
}
~GLXFunctions()
{
if (m_module)
dlclose(m_module);
}
PFNXFREE XFree = nullptr;
PFNGLXGETPROCADDRESSPROC glXGetProcAddress = nullptr;
PFNGLXGETPROCADDRESSPROC glXGetProcAddressARB = nullptr;
PFNGLXCHOOSEFBCONFIGPROC glXChooseFBConfig = nullptr;
PFNGLXCREATECONTEXTATTRIBSARB glXCreateContextAttribsARB = nullptr;
PFNGLXDESTROYCONTEXT glXDestroyContext = nullptr;
PFNGLXCREATEPBUFFERPROC glXCreatePbuffer = nullptr;
PFNGLXDESTROYPBUFFER glXDestroyPbuffer = nullptr;
PFNGLXMAKECURRENTPROC glXMakeCurrent = nullptr;
PFNGLXSWAPBUFFERSPROC glXSwapBuffers = nullptr;
private:
template <typename T>
T loadFunction(char const * func)
{
auto f = reinterpret_cast<T>(dlsym(m_module, func));
ASSERT(f, ("Failed to initialize GLX:", func, "is not found"));
return f;
}
template <typename T>
T loadGlxFunction(char const * func)
{
if (auto f = reinterpret_cast<T>(glXGetProcAddress(reinterpret_cast<GLubyte const *>(func))))
return f;
if (auto f = reinterpret_cast<T>(glXGetProcAddressARB(reinterpret_cast<GLubyte const *>(func))))
return f;
return loadFunction<T>(func);
}
void * m_module = nullptr;
};
class LinuxGLContext : public dp::OGLContext
{
public:
LinuxGLContext(GLXFunctions const & glx, Display * display, Window window, LinuxGLContext * contextToShareWith,
bool usePixelBuffer)
: m_glx(glx)
, m_display(display)
, m_window(window)
{
int visualAttribs[] = {GLX_DOUBLEBUFFER,
True,
GLX_RENDER_TYPE,
GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE,
(usePixelBuffer ? GLX_PBUFFER_BIT : GLX_WINDOW_BIT),
GLX_RED_SIZE,
8,
GLX_GREEN_SIZE,
8,
GLX_BLUE_SIZE,
8,
GLX_ALPHA_SIZE,
8,
GLX_DEPTH_SIZE,
24,
GLX_STENCIL_SIZE,
8,
None};
int contextAttribs[] = {GLX_CONTEXT_PROFILE_MASK_ARB,
GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_MAJOR_VERSION_ARB,
4,
GLX_CONTEXT_MINOR_VERSION_ARB,
1,
None};
int fbcount = 0;
if (GLXFBConfig * config = m_glx.glXChooseFBConfig(display, DefaultScreen(display), visualAttribs, &fbcount))
{
m_context = m_glx.glXCreateContextAttribsARB(
display, config[0], contextToShareWith ? contextToShareWith->m_context : 0, True, contextAttribs);
CHECK(m_context != nullptr, ("Failed to create GLX context"));
if (usePixelBuffer)
{
int pbufferAttribs[] = {GLX_PBUFFER_WIDTH, 1, GLX_PBUFFER_HEIGHT, 1, None};
m_pixelBufferHandle = m_glx.glXCreatePbuffer(display, config[0], pbufferAttribs);
CHECK(m_pixelBufferHandle != 0, ("Failed to create GLX pbuffer"));
}
m_glx.XFree(config);
}
}
~LinuxGLContext() override
{
if (m_pixelBufferHandle)
{
m_glx.glXDestroyPbuffer(m_display, m_pixelBufferHandle);
m_pixelBufferHandle = 0;
}
if (m_context)
{
m_glx.glXDestroyContext(m_display, m_context);
m_context = nullptr;
}
}
void Present() override
{
if (!m_pixelBufferHandle)
m_glx.glXSwapBuffers(m_display, m_window);
}
void MakeCurrent() override
{
if (!m_glx.glXMakeCurrent(m_display, m_pixelBufferHandle ? m_pixelBufferHandle : m_window, m_context))
LOG(LERROR, ("MakeCurrent(): glXMakeCurrent failed"));
}
void DoneCurrent() override
{
if (!m_glx.glXMakeCurrent(m_display, None, nullptr))
LOG(LERROR, ("DoneCurrent(): glXMakeCurrent failed"));
}
void SetFramebuffer(ref_ptr<dp::BaseFramebuffer> framebuffer) override
{
if (framebuffer)
framebuffer->Bind();
else
GLFunctions::glBindFramebuffer(0);
}
private:
GLXFunctions const & m_glx;
Display * m_display = nullptr;
Window m_window = 0;
GLXDrawable m_pixelBufferHandle = 0;
GLXContext m_context = nullptr;
};
class LinuxContextFactory : public dp::GraphicsContextFactory
{
public:
LinuxContextFactory(Display * display, Window window) : m_display(display), m_window(window) {}
dp::GraphicsContext * GetDrawContext() override
{
std::lock_guard<std::mutex> lock(m_contextAccess);
if (m_drawContext == nullptr)
m_drawContext = std::make_unique<LinuxGLContext>(m_glx, m_display, m_window, m_uploadContext.get(), false);
return m_drawContext.get();
}
dp::GraphicsContext * GetResourcesUploadContext() override
{
std::lock_guard<std::mutex> lock(m_contextAccess);
if (m_uploadContext == nullptr)
m_uploadContext = std::make_unique<LinuxGLContext>(m_glx, m_display, 0, m_drawContext.get(), true);
return m_uploadContext.get();
}
void WaitForInitialization(dp::GraphicsContext *) override
{
std::unique_lock<std::mutex> lock(m_initializationMutex);
if (m_isInitialized)
return;
m_initializationCounter++;
if (m_initializationCounter >= kGLThreadsCount)
{
m_isInitialized = true;
m_initializationCondition.notify_all();
}
else
{
m_initializationCondition.wait(lock, [this] { return m_isInitialized; });
}
}
bool IsDrawContextCreated() const override
{
std::lock_guard<std::mutex> lock(m_contextAccess);
return m_drawContext != nullptr;
}
bool IsUploadContextCreated() const override
{
std::lock_guard<std::mutex> lock(m_contextAccess);
return m_uploadContext != nullptr;
}
private:
static size_t constexpr kGLThreadsCount = 2;
GLXFunctions m_glx;
Display * m_display = nullptr;
Window m_window = 0;
std::unique_ptr<LinuxGLContext> m_drawContext;
std::unique_ptr<LinuxGLContext> m_uploadContext;
mutable std::mutex m_contextAccess;
bool m_isInitialized = false;
size_t m_initializationCounter = 0;
std::condition_variable m_initializationCondition;
std::mutex m_initializationMutex;
};
drape_ptr<dp::GraphicsContextFactory> CreateContextFactory(GLFWwindow * window, dp::ApiVersion api, m2::PointU size)
{
if (api == dp::ApiVersion::Vulkan)
{
auto contextFactory = make_unique_dp<LinuxVulkanContextFactory>();
contextFactory->SetSurface(glfwGetX11Display(), glfwGetX11Window(window));
return contextFactory;
}
if (api == dp::ApiVersion::OpenGLES3)
return make_unique_dp<LinuxContextFactory>(glfwGetX11Display(), glfwGetX11Window(window));
ASSERT(false, ("API is not available yet"));
return nullptr;
}
void OnCreateDrapeEngine(GLFWwindow * window, dp::ApiVersion api, ref_ptr<dp::GraphicsContextFactory> contextFactory)
{
// Do nothing
}
void PrepareDestroyContextFactory(ref_ptr<dp::GraphicsContextFactory> contextFactory)
{
auto const api = contextFactory->GetDrawContext()->GetApiVersion();
if (api == dp::ApiVersion::OpenGLES3)
{
// Do nothing
}
else if (api == dp::ApiVersion::Vulkan)
{
ref_ptr<LinuxVulkanContextFactory> linuxContextFactory = contextFactory;
linuxContextFactory->ResetSurface();
}
else
{
ASSERT(false, ("API is not available yet"));
}
}
void UpdateContentScale(GLFWwindow * window, float scale)
{
// Do nothing
}
void UpdateSize(ref_ptr<dp::GraphicsContextFactory> contextFactory, int w, int h)
{
// Do nothing
}
|