Debian Codegraph @

libs/bluegl/tests/OpenGLSupport.cpp

raw
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "OpenGLSupport.hpp"
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <iostream>
23 #include <mutex>
24
25 #ifdef __APPLE__
26 #include <dlfcn.h>
27 #include <OpenGL/OpenGL.h>
28
29 #define LIBRARY_CGL "/System/Library/Frameworks/OpenGL.framework/OpenGL"
30 #elif defined(__CYGWIN__) || defined(WIN32)
31 #include <windows.h>
32 #else
33 #include <dlfcn.h>
34 #include <X11/Xlib.h>
35 #include <GL/glx.h>
36 #include <GL/glxext.h>
37
38 #define LIBRARY_GLX "libGL.so.1"
39 #define LIBRARY_X11 "libX11.so.6"
40 #endif
41
42 namespace bluegl {
43 namespace gl {
44
45 // This mutex protect g_library_refcount below.
46 static std::mutex g_library_mutex;
47
48 #ifdef __APPLE__
49 static uint32_t g_library_refcount = 0;
50
51 // Function pointer types for CGL functions
52 typedef CGLError (*CGL_CHOOSE_PIXEL_FORMAT_PROC)(const CGLPixelFormatAttribute*, CGLPixelFormatObj*, GLint*);
53 typedef CGLError (*CGL_DESTROY_PIXEL_FORMAT_PROC)(CGLPixelFormatObj);
54 typedef CGLError (*CGL_CREATE_CONTEXT_PROC)(CGLPixelFormatObj, CGLContextObj, CGLContextObj*);
55 typedef CGLError (*CGL_SET_CURRENT_CONTEXT_PROC)(CGLContextObj);
56 typedef CGLError (*CGL_DESTROY_CONTEXT_PROC)(CGLContextObj);
57
58 // Stores CGL function pointers and a handle to the system's OpenGL library
59 struct CGLFunctions {
60 CGL_CHOOSE_PIXEL_FORMAT_PROC choosePixelFormat;
61 CGL_DESTROY_PIXEL_FORMAT_PROC destroyPixelFormat;
62 CGL_CREATE_CONTEXT_PROC createContext;
63 CGL_SET_CURRENT_CONTEXT_PROC setCurrentContext;
64 CGL_DESTROY_CONTEXT_PROC destroyContext;
65 void* library;
66 } g_cgl;
67
68 bool loadLibraries() {
69 std::lock_guard<std::mutex> lock(g_library_mutex);
70 g_library_refcount++;
71
72 if (g_library_refcount == 1) {
73 g_cgl.library = dlopen(LIBRARY_CGL, RTLD_GLOBAL);
74 if (!g_cgl.library) {
75 std::cerr << "Could not find library " << LIBRARY_CGL << std::endl;
76 return false;
77 }
78
79 g_cgl.choosePixelFormat = (CGL_CHOOSE_PIXEL_FORMAT_PROC)
80 dlsym(g_cgl.library, "CGLChoosePixelFormat");
81 g_cgl.destroyPixelFormat = (CGL_DESTROY_PIXEL_FORMAT_PROC)
82 dlsym(g_cgl.library, "CGLDestroyPixelFormat");
83 g_cgl.createContext = (CGL_CREATE_CONTEXT_PROC)
84 dlsym(g_cgl.library, "CGLCreateContext");
85 g_cgl.setCurrentContext = (CGL_SET_CURRENT_CONTEXT_PROC)
86 dlsym(g_cgl.library, "CGLSetCurrentContext");
87 g_cgl.destroyContext = (CGL_DESTROY_CONTEXT_PROC)
88 dlsym(g_cgl.library, "CGLDestroyContext");
89 }
90
91 return true;
92 }
93
94 void unloadLibraries() {
95 std::lock_guard<std::mutex> lock(g_library_mutex);
96 uint32_t refcount = g_library_refcount;
97 if (refcount > 0) {
98 g_library_refcount--;
99 }
100
101 if (refcount == 1) {
102 dlclose(g_cgl.library);
103 memset(&g_cgl, 0, sizeof(g_cgl));
104 }
105 }
106
107 OpenGLContext createOpenGLContext() {
108 if (!loadLibraries()) return nullptr;
109
110 // The attributes don't really matter here but we choose a configuration
111 // that would make sense if we were to perform actual rendering
112 CGLPixelFormatAttribute attributes[] = {
113 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_GL4_Core,
114 kCGLPFAColorSize, (CGLPixelFormatAttribute) 24,
115 kCGLPFAAlphaSize, (CGLPixelFormatAttribute) 8,
116 kCGLPFAAccelerated, (CGLPixelFormatAttribute) 0
117 };
118
119 GLint pixelFormatCount;
120 CGLPixelFormatObj pixelFormat;
121 (*g_cgl.choosePixelFormat)(attributes, &pixelFormat, &pixelFormatCount);
122
123 OpenGLContext context;
124 (*g_cgl.createContext)(pixelFormat, nullptr, (CGLContextObj*) &context);
125
126 (*g_cgl.destroyPixelFormat)(pixelFormat);
127
128 return context;
129 }
130
131 void setCurrentOpenGLContext(OpenGLContext context) {
132 (*g_cgl.setCurrentContext)((CGLContextObj) context);
133 }
134
135 void destroyOpenGLContext(OpenGLContext context) {
136 (*g_cgl.setCurrentContext)(nullptr);
137 (*g_cgl.destroyContext)((CGLContextObj) context);
138
139 unloadLibraries();
140 }
141
142 #elif defined(__CYGWIN__) || defined(WIN32)
143
144 #include "../include/GL/glcorearb.h"
145 #include "GL/wglext.h"
146
147 struct wglLocalContext {
148 wglLocalContext(HWND ohwnd, HDC owhdc, HGLRC ocontext) :
149 hwnd(ohwnd), whdc(owhdc), context(ocontext) {
150 };
151 HWND hwnd;
152 HDC whdc;
153 HGLRC context;
154 };
155
156 bool loadLibraries() {
157 return true;
158 }
159
160 bool unloadLibraries() {
161 return true;
162 }
163
164 OpenGLContext createOpenGLContext() {
165 if (!loadLibraries()) return nullptr;
166
167 PIXELFORMATDESCRIPTOR pfd = {
168 sizeof(PIXELFORMATDESCRIPTOR),
169 1,
170 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags
171 PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
172 32, // Colordepth of the framebuffer.
173 0, 0, 0, 0, 0, 0,
174 0,
175 0,
176 0,
177 0, 0, 0, 0,
178 24, // Number of bits for the depthbuffer
179 0, // Number of bits for the stencilbuffer
180 0, // Number of Aux buffers in the framebuffer.
181 PFD_MAIN_PLANE,
182 0,
183 0, 0, 0
184 };
185
186 int attribs[] = {
187 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
188 WGL_CONTEXT_MINOR_VERSION_ARB, 1,
189 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_PROFILE_MASK_ARB,
190 0
191 };
192
193 HWND hwnd = CreateWindowA("STATIC", "dummy", 0, 0, 0, 1, 1, NULL, NULL, NULL, NULL);
194 HDC whdc = GetDC(hwnd);
195
196 int pixelFormat = ChoosePixelFormat(whdc, &pfd);
197 SetPixelFormat(whdc, pixelFormat, &pfd);
198
199 // We need a tmp context to retrieve and call wglCreateContextAttribsARB.
200 HGLRC tempContext = wglCreateContext(whdc);
201 wglMakeCurrent(whdc, tempContext);
202
203 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs =
204 (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
205 HGLRC context = wglCreateContextAttribs(whdc, nullptr, attribs);
206
207 wglMakeCurrent(NULL, NULL);
208 wglDeleteContext(tempContext);
209 wglMakeCurrent(whdc, context);
210
211 return new wglLocalContext(hwnd, whdc, context);
212 }
213
214 void setCurrentOpenGLContext(OpenGLContext context) {
215 wglLocalContext* wContext = static_cast<wglLocalContext*>(context);
216 HDC hdc = GetDC(wContext->hwnd);
217 wglMakeCurrent(hdc, wContext->context);
218 }
219
220 void destroyOpenGLContext(OpenGLContext context) {
221 wglLocalContext* wContext = static_cast<wglLocalContext*>(context);
222 wglMakeCurrent(NULL, NULL);
223 wglDeleteContext(wContext->context);
224 ReleaseDC(wContext->hwnd, wContext->whdc);
225 DestroyWindow(wContext->hwnd);
226 delete static_cast<wglLocalContext*>(context);
227 unloadLibraries();
228 }
229
230 #else
231
232 // Function pointer types for X11 functions
233 typedef Display* (*X11_OPEN_DISPLAY)(const char*);
234 typedef Display* (*X11_CLOSE_DISPLAY)(Display*);
235 typedef int (*X11_FREE)(void*);
236 typedef int (*X11_SYNC)(Display*, Bool);
237
238 // Function pointer types for GLX functions
239 typedef void (*GLX_DESTROY_CONTEXT)(Display*, GLXContext);
240
241 // Stores GLX function pointers and a handle to the system's GLX library
242 struct GLXFunctions {
243 PFNGLXCHOOSEFBCONFIGPROC chooseFbConfig;
244 PFNGLXCREATECONTEXTATTRIBSARBPROC createContext;
245 PFNGLXCREATEPBUFFERPROC createPbuffer;
246 PFNGLXDESTROYPBUFFERPROC destroyPbuffer;
247 PFNGLXMAKECONTEXTCURRENTPROC setCurrentContext;
248 GLX_DESTROY_CONTEXT destroyContext;
249 void* library;
250 } g_glx;
251
252 // Stores X11 function pointers and a handle to the system's X11 library
253 struct X11Functions {
254 X11_OPEN_DISPLAY openDisplay;
255 X11_CLOSE_DISPLAY closeDisplay;
256 X11_FREE free;
257 X11_SYNC sync;
258 void* library;
259 } g_x11;
260
261 /*
262 * A GLXLocalContext is returned by createOpenGLContext() and is
263 * expected by destroyOpenGLContext(). It contains system types
264 * required to properly make a context current and perform cleanup
265 * when we're done.
266 */
267 struct GLXLocalContext {
268 Display* display;
269 GLXContext context;
270 GLXPbuffer buffer;
271 };
272
273 static uint32_t g_library_refcount = 0;
274
275 bool loadLibraries() {
276 std::lock_guard<std::mutex> lock(g_library_mutex);
277 g_library_refcount++;
278
279 if (g_library_refcount == 1) {
280 g_glx.library = dlopen(LIBRARY_GLX, RTLD_LOCAL | RTLD_NOW);
281 if (!g_glx.library) {
282 std::cerr << "Could not find library " << LIBRARY_GLX << std::endl;
283 return false;
284 }
285
286 PFNGLXGETPROCADDRESSPROC getProcAddress =
287 (PFNGLXGETPROCADDRESSPROC) dlsym(g_glx.library, "glXGetProcAddressARB");
288
289 g_glx.chooseFbConfig = (PFNGLXCHOOSEFBCONFIGPROC)
290 getProcAddress((const GLubyte*) "glXChooseFBConfig");
291 g_glx.createContext = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
292 getProcAddress((const GLubyte*) "glXCreateContextAttribsARB");
293 g_glx.createPbuffer = (PFNGLXCREATEPBUFFERPROC)
294 getProcAddress((const GLubyte*) "glXCreatePbuffer");
295 g_glx.destroyPbuffer = (PFNGLXDESTROYPBUFFERPROC)
296 getProcAddress((const GLubyte*) "glXDestroyPbuffer");
297 g_glx.setCurrentContext = (PFNGLXMAKECONTEXTCURRENTPROC)
298 getProcAddress((const GLubyte*) "glXMakeContextCurrent");
299 g_glx.destroyContext = (GLX_DESTROY_CONTEXT)
300 getProcAddress((const GLubyte*) "glXDestroyContext");
301
302 g_x11.library = dlopen(LIBRARY_X11, RTLD_LOCAL | RTLD_NOW);
303 if (!g_x11.library) {
304 std::cerr << "Could not find library " << LIBRARY_X11 << std::endl;
305 return false;
306 }
307
308 g_x11.openDisplay = (X11_OPEN_DISPLAY) dlsym(g_x11.library, "XOpenDisplay");
309 g_x11.closeDisplay = (X11_CLOSE_DISPLAY) dlsym(g_x11.library, "XCloseDisplay");
310 g_x11.free = (X11_FREE) dlsym(g_x11.library, "XFree");
311 g_x11.sync = (X11_SYNC) dlsym(g_x11.library, "XSync");
312 }
313 return true;
314 }
315
316 void unloadLibraries() {
317 std::lock_guard<std::mutex> lock(g_library_mutex);
318 uint32_t refcount = g_library_refcount;
319 if (refcount > 0) {
320 g_library_refcount--;
321 }
322
323 if (refcount == 1) {
324 dlclose(g_glx.library);
325 memset(&g_glx, 0, sizeof(g_glx));
326
327 dlclose(g_x11.library);
328 memset(&g_x11, 0, sizeof(g_x11));
329 }
330 }
331
332 OpenGLContext createOpenGLContext() {
333 const char* gl_indirect = getenv("LIBGL_ALWAYS_INDIRECT");
334 if (gl_indirect != nullptr) {
335 std::cerr << "The environment variable LIBGL_ALWAYS_INDIRECT is set.\n"
336 << "This variable must be unset for this test to run properly."
337 << std::endl;
338 return nullptr;
339 }
340
341 if (!loadLibraries()) return nullptr;
342
343 // Open the default display, it doesn't matter which one since we
344 // are not creating a window but only a pbuffer
345 Display* display = (*g_x11.openDisplay)(nullptr);
346 if (display == nullptr) return nullptr;
347
348 // Any frame buffer configuration will do, we only care about the
349 // context, not the frame buffer itself
350 static int attribs[] = { None };
351 int config_count = 0;
352 GLXFBConfig* configs = (*g_glx.chooseFbConfig)(display, DefaultScreen(display),
353 attribs, &config_count);
354 if (configs == nullptr || config_count == 0) return nullptr;
355
356 // We'll only need OpenGL 2.1 for testing purposes. This ensures
357 // tests can be run in a virtual machine
358 int context_attribs[] = {
359 GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
360 GLX_CONTEXT_MINOR_VERSION_ARB, 1,
361 None
362 };
363 GLXContext context = (*g_glx.createContext)(display, configs[0],
364 nullptr, True, context_attribs);
365
366 // A 1x1 pbuffer is required to be able to do a make current later on
367 int pbuffer_attribs[] = {
368 GLX_PBUFFER_WIDTH, 1,
369 GLX_PBUFFER_HEIGHT, 1,
370 None
371 };
372 GLXPbuffer pbuffer = (*g_glx.createPbuffer)(display, configs[0], pbuffer_attribs);
373
374 (*g_x11.free)(configs);
375 (*g_x11.sync)(display, False);
376
377 GLXLocalContext* local_context = new GLXLocalContext();
378 local_context->display = display;
379 local_context->context = context;
380 local_context->buffer = pbuffer;
381
382 return local_context;
383 }
384
385 void setCurrentOpenGLContext(OpenGLContext context) {
386 GLXLocalContext* local_context = (GLXLocalContext*) context;
387 (*g_glx.setCurrentContext)(local_context->display,
388 local_context->buffer, local_context->buffer, local_context->context);
389 }
390
391 void destroyOpenGLContext(OpenGLContext context) {
392 GLXLocalContext* local_context = (GLXLocalContext*) context;
393 (*g_glx.setCurrentContext)(local_context->display, None, None, nullptr);
394
395 (*g_glx.destroyPbuffer)(local_context->display, local_context->buffer);
396 (*g_glx.destroyContext)(local_context->display, local_context->context);
397
398 (*g_x11.closeDisplay)(local_context->display);
399
400 delete local_context;
401 unloadLibraries();
402 }
403
404 #endif
405
406 }; // namespace gl
407 }; // namespace bluegl