osmesa_context.c
Go to the documentation of this file.
1 //========================================================================
2 // GLFW 3.3 OSMesa - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2016 Google Inc.
5 // Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
6 //
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
10 //
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
14 //
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
25 //
26 //========================================================================
27 
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include "internal.h"
33 
34 
36 {
37  if (window)
38  {
39  int width, height;
40  _glfwPlatformGetFramebufferSize(window, &width, &height);
41 
42  // Check to see if we need to allocate a new buffer
43  if ((window->context.osmesa.buffer == NULL) ||
44  (width != window->context.osmesa.width) ||
45  (height != window->context.osmesa.height))
46  {
47  free(window->context.osmesa.buffer);
48 
49  // Allocate the new buffer (width * height * 8-bit RGBA)
50  window->context.osmesa.buffer = calloc(4, width * height);
51  window->context.osmesa.width = width;
52  window->context.osmesa.height = height;
53  }
54 
55  if (!OSMesaMakeCurrent(window->context.osmesa.handle,
56  window->context.osmesa.buffer,
58  width, height))
59  {
61  "OSMesa: Failed to make context current");
62  return;
63  }
64  }
65 
67 }
68 
69 static GLFWglproc getProcAddressOSMesa(const char* procname)
70 {
71  return (GLFWglproc) OSMesaGetProcAddress(procname);
72 }
73 
75 {
76  if (window->context.osmesa.handle)
77  {
78  OSMesaDestroyContext(window->context.osmesa.handle);
79  window->context.osmesa.handle = NULL;
80  }
81 
82  if (window->context.osmesa.buffer)
83  {
84  free(window->context.osmesa.buffer);
85  window->context.osmesa.width = 0;
86  window->context.osmesa.height = 0;
87  }
88 }
89 
91 {
92  // No double buffering on OSMesa
93 }
94 
95 static void swapIntervalOSMesa(int interval)
96 {
97  // No swap interval on OSMesa
98 }
99 
100 static int extensionSupportedOSMesa(const char* extension)
101 {
102  // OSMesa does not have extensions
103  return GLFW_FALSE;
104 }
105 
106 
110 
112 {
113  int i;
114  const char* sonames[] =
115  {
116 #if defined(_GLFW_OSMESA_LIBRARY)
117  _GLFW_OSMESA_LIBRARY,
118 #elif defined(_WIN32)
119  "libOSMesa.dll",
120  "OSMesa.dll",
121 #elif defined(__APPLE__)
122  "libOSMesa.8.dylib",
123 #elif defined(__CYGWIN__)
124  "libOSMesa-8.so",
125 #else
126  "libOSMesa.so.8",
127  "libOSMesa.so.6",
128 #endif
129  NULL
130  };
131 
132  if (_glfw.osmesa.handle)
133  return GLFW_TRUE;
134 
135  for (i = 0; sonames[i]; i++)
136  {
137  _glfw.osmesa.handle = _glfw_dlopen(sonames[i]);
138  if (_glfw.osmesa.handle)
139  break;
140  }
141 
142  if (!_glfw.osmesa.handle)
143  {
144  _glfwInputError(GLFW_API_UNAVAILABLE, "OSMesa: Library not found");
145  return GLFW_FALSE;
146  }
147 
148  _glfw.osmesa.CreateContextExt = (PFN_OSMesaCreateContextExt)
149  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaCreateContextExt");
150  _glfw.osmesa.CreateContextAttribs = (PFN_OSMesaCreateContextAttribs)
151  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaCreateContextAttribs");
152  _glfw.osmesa.DestroyContext = (PFN_OSMesaDestroyContext)
153  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaDestroyContext");
154  _glfw.osmesa.MakeCurrent = (PFN_OSMesaMakeCurrent)
155  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaMakeCurrent");
156  _glfw.osmesa.GetColorBuffer = (PFN_OSMesaGetColorBuffer)
157  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaGetColorBuffer");
158  _glfw.osmesa.GetDepthBuffer = (PFN_OSMesaGetDepthBuffer)
159  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaGetDepthBuffer");
160  _glfw.osmesa.GetProcAddress = (PFN_OSMesaGetProcAddress)
161  _glfw_dlsym(_glfw.osmesa.handle, "OSMesaGetProcAddress");
162 
163  if (!_glfw.osmesa.CreateContextExt ||
164  !_glfw.osmesa.DestroyContext ||
165  !_glfw.osmesa.MakeCurrent ||
166  !_glfw.osmesa.GetColorBuffer ||
167  !_glfw.osmesa.GetDepthBuffer ||
168  !_glfw.osmesa.GetProcAddress)
169  {
171  "OSMesa: Failed to load required entry points");
172 
174  return GLFW_FALSE;
175  }
176 
177  return GLFW_TRUE;
178 }
179 
181 {
182  if (_glfw.osmesa.handle)
183  {
184  _glfw_dlclose(_glfw.osmesa.handle);
185  _glfw.osmesa.handle = NULL;
186  }
187 }
188 
189 #define setAttrib(a, v) \
190 { \
191  assert((size_t) (index + 1) < sizeof(attribs) / sizeof(attribs[0])); \
192  attribs[index++] = a; \
193  attribs[index++] = v; \
194 }
195 
197  const _GLFWctxconfig* ctxconfig,
198  const _GLFWfbconfig* fbconfig)
199 {
200  OSMesaContext share = NULL;
201  const int accumBits = fbconfig->accumRedBits +
202  fbconfig->accumGreenBits +
203  fbconfig->accumBlueBits +
204  fbconfig->accumAlphaBits;
205 
206  if (ctxconfig->client == GLFW_OPENGL_ES_API)
207  {
209  "OSMesa: OpenGL ES is not available on OSMesa");
210  return GLFW_FALSE;
211  }
212 
213  if (ctxconfig->share)
214  share = ctxconfig->share->context.osmesa.handle;
215 
217  {
218  int index = 0, attribs[40];
219 
223  setAttrib(OSMESA_ACCUM_BITS, accumBits);
224 
225  if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
226  {
228  }
229  else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
230  {
232  }
233 
234  if (ctxconfig->major != 1 || ctxconfig->minor != 0)
235  {
238  }
239 
240  if (ctxconfig->forward)
241  {
243  "OSMesa: Foward-compatible contexts not supported");
244  return GLFW_FALSE;
245  }
246 
247  setAttrib(0, 0);
248 
249  window->context.osmesa.handle =
251  }
252  else
253  {
254  if (ctxconfig->profile)
255  {
257  "OSMesa: OpenGL profiles unavailable");
258  return GLFW_FALSE;
259  }
260 
261  window->context.osmesa.handle =
263  fbconfig->depthBits,
264  fbconfig->stencilBits,
265  accumBits,
266  share);
267  }
268 
269  if (window->context.osmesa.handle == NULL)
270  {
272  "OSMesa: Failed to create context");
273  return GLFW_FALSE;
274  }
275 
282 
283  return GLFW_TRUE;
284 }
285 
286 #undef setAttrib
287 
288 
292 
294  int* height, int* format, void** buffer)
295 {
296  void* mesaBuffer;
297  GLint mesaWidth, mesaHeight, mesaFormat;
298  _GLFWwindow* window = (_GLFWwindow*) handle;
299  assert(window != NULL);
300 
302 
303  if (!OSMesaGetColorBuffer(window->context.osmesa.handle,
304  &mesaWidth, &mesaHeight,
305  &mesaFormat, &mesaBuffer))
306  {
308  "OSMesa: Failed to retrieve color buffer");
309  return GLFW_FALSE;
310  }
311 
312  if (width)
313  *width = mesaWidth;
314  if (height)
315  *height = mesaHeight;
316  if (format)
317  *format = mesaFormat;
318  if (buffer)
319  *buffer = mesaBuffer;
320 
321  return GLFW_TRUE;
322 }
323 
325  int* width, int* height,
326  int* bytesPerValue,
327  void** buffer)
328 {
329  void* mesaBuffer;
330  GLint mesaWidth, mesaHeight, mesaBytes;
331  _GLFWwindow* window = (_GLFWwindow*) handle;
332  assert(window != NULL);
333 
335 
336  if (!OSMesaGetDepthBuffer(window->context.osmesa.handle,
337  &mesaWidth, &mesaHeight,
338  &mesaBytes, &mesaBuffer))
339  {
341  "OSMesa: Failed to retrieve depth buffer");
342  return GLFW_FALSE;
343  }
344 
345  if (width)
346  *width = mesaWidth;
347  if (height)
348  *height = mesaHeight;
349  if (bytesPerValue)
350  *bytesPerValue = mesaBytes;
351  if (buffer)
352  *buffer = mesaBuffer;
353 
354  return GLFW_TRUE;
355 }
356 
358 {
359  _GLFWwindow* window = (_GLFWwindow*) handle;
361 
362  if (window->context.client == GLFW_NO_API)
363  {
365  return NULL;
366  }
367 
368  return window->context.osmesa.handle;
369 }
370 
#define OSMESA_STENCIL_BITS
#define OSMesaMakeCurrent
#define OSMESA_ACCUM_BITS
_GLFWswapbuffersfun swapBuffers
Definition: internal.h:349
int(GLAPIENTRY * PFN_OSMesaGetColorBuffer)(OSMesaContext, int *, int *, int *, void **)
#define GLFW_OPENGL_ES_API
Definition: glfw3.h:991
void(GLAPIENTRY * PFN_OSMesaDestroyContext)(OSMesaContext)
_GLFWwindow * share
Definition: internal.h:297
int(GLAPIENTRY * PFN_OSMesaGetDepthBuffer)(OSMesaContext, int *, int *, int *, void **)
#define OSMESA_CONTEXT_MAJOR_VERSION
_GLFWgetprocaddressfun getProcAddress
Definition: internal.h:352
#define OSMesaGetDepthBuffer
#define GLFWAPI
Definition: glfw3.h:240
#define GLFW_OPENGL_COMPAT_PROFILE
Definition: glfw3.h:999
_GLFWtls contextSlot
Definition: internal.h:536
const GLint * attribs
Definition: glext.h:11230
GLFWbool _glfwInitOSMesa(void)
#define GL_UNSIGNED_BYTE
static int extensionSupportedOSMesa(const char *extension)
GLuint64 GLenum void * handle
Definition: glext.h:7785
_GLFWdestroycontextfun destroy
Definition: internal.h:353
void _glfwPlatformGetFramebufferSize(_GLFWwindow *window, int *width, int *height)
Definition: null_window.c:128
int accumGreenBits
Definition: internal.h:320
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
#define _glfw_dlopen(name)
#define GLFW_NO_WINDOW_CONTEXT
The specified window does not have an OpenGL or OpenGL ES context.
Definition: glfw3.h:753
GLFWbool forward
Definition: internal.h:291
OSMesaContext(GLAPIENTRY * PFN_OSMesaCreateContextAttribs)(const int *, OSMesaContext)
#define OSMESA_PROFILE
#define OSMESA_FORMAT
void * OSMesaContext
#define GLFW_API_UNAVAILABLE
GLFW could not find support for the requested API on the system.
Definition: glfw3.h:698
GLFWAPI int glfwGetOSMesaColorBuffer(GLFWwindow *handle, int *width, int *height, int *format, void **buffer)
int stencilBits
Definition: internal.h:318
_GLFWcontext context
Definition: internal.h:394
int GLFWbool
Definition: internal.h:61
GLenum GLfloat * buffer
void _glfwTerminateOSMesa(void)
void * handle
Definition: internal.h:547
GLuint index
void(* GLFWglproc)(void)
static void makeContextCurrentOSMesa(_GLFWwindow *window)
#define OSMESA_RGBA
int(GLAPIENTRY * PFN_OSMesaMakeCurrent)(OSMesaContext, void *, int, int, int)
#define _glfw_dlclose(handle)
#define _GLFW_REQUIRE_INIT_OR_RETURN(x)
Definition: internal.h:210
GLFWglproc(GLAPIENTRY * PFN_OSMesaGetProcAddress)(const char *)
#define setAttrib(a, v)
#define OSMESA_CONTEXT_MINOR_VERSION
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
Definition: glfw3.h:726
#define OSMESA_COMPAT_PROFILE
_GLFWlibrary _glfw
Definition: init.c:44
int GLint
static GLFWglproc getProcAddressOSMesa(const char *procname)
GLint GLsizei GLsizei height
GLint GLint GLsizei GLint GLenum format
int accumBlueBits
Definition: internal.h:321
_GLFWmakecontextcurrentfun makeCurrent
Definition: internal.h:348
#define OSMESA_DEPTH_BITS
void _glfwInputError(int code, const char *format,...)
Definition: init.c:129
_GLFWextensionsupportedfun extensionSupported
Definition: internal.h:351
OSMesaContext(GLAPIENTRY * PFN_OSMesaCreateContextExt)(GLenum, GLint, GLint, GLint, OSMesaContext)
#define GLFW_VERSION_UNAVAILABLE
The requested OpenGL or OpenGL ES version is not available.
Definition: glfw3.h:715
_GLFWswapintervalfun swapInterval
Definition: internal.h:350
#define GLFW_TRUE
One.
Definition: glfw3.h:279
void _glfwPlatformSetTls(_GLFWtls *tls, void *value)
Definition: posix_thread.c:66
#define OSMesaCreateContextExt
#define OSMesaGetColorBuffer
#define OSMesaGetProcAddress
#define OSMesaCreateContextAttribs
#define GLFW_OPENGL_CORE_PROFILE
Definition: glfw3.h:998
GLFWbool _glfwCreateContextOSMesa(_GLFWwindow *window, const _GLFWctxconfig *ctxconfig, const _GLFWfbconfig *fbconfig)
static void swapIntervalOSMesa(int interval)
GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow *handle, int *width, int *height, int *bytesPerValue, void **buffer)
int accumAlphaBits
Definition: internal.h:322
#define NULL
Definition: tinycthread.c:47
GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow *handle)
int i
#define OSMESA_CORE_PROFILE
static void swapBuffersOSMesa(_GLFWwindow *window)
#define _glfw_dlsym(handle, name)
static void destroyContextOSMesa(_GLFWwindow *window)
#define OSMesaDestroyContext
struct GLFWwindow GLFWwindow
int accumRedBits
Definition: internal.h:319
GLint GLsizei width
#define GLFW_NO_API
Definition: glfw3.h:989


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39