123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- //========================================================================
- // GLFW 3.4 OSMesa - www.glfw.org
- //------------------------------------------------------------------------
- // Copyright (c) 2016 Google Inc.
- // Copyright (c) 2016-2017 Camilla Löwy <elmindreda@glfw.org>
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- // arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it
- // freely, subject to the following restrictions:
- //
- // 1. The origin of this software must not be misrepresented; you must not
- // claim that you wrote the original software. If you use this software
- // in a product, an acknowledgment in the product documentation would
- // be appreciated but is not required.
- //
- // 2. Altered source versions must be plainly marked as such, and must not
- // be misrepresented as being the original software.
- //
- // 3. This notice may not be removed or altered from any source
- // distribution.
- //
- //========================================================================
- // Please use C89 style variable declarations in this file because VS 2010
- //========================================================================
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include "internal.h"
- static void makeContextCurrentOSMesa(_GLFWwindow* window)
- {
- if (window)
- {
- int width, height;
- _glfwPlatformGetFramebufferSize(window, &width, &height);
- // Check to see if we need to allocate a new buffer
- if ((window->context.osmesa.buffer == NULL) ||
- (width != window->context.osmesa.width) ||
- (height != window->context.osmesa.height))
- {
- free(window->context.osmesa.buffer);
- // Allocate the new buffer (width * height * 8-bit RGBA)
- window->context.osmesa.buffer = calloc(4, (size_t) width * height);
- window->context.osmesa.width = width;
- window->context.osmesa.height = height;
- }
- if (!OSMesaMakeCurrent(window->context.osmesa.handle,
- window->context.osmesa.buffer,
- GL_UNSIGNED_BYTE,
- width, height))
- {
- _glfwInputError(GLFW_PLATFORM_ERROR,
- "OSMesa: Failed to make context current");
- return;
- }
- }
- _glfwPlatformSetTls(&_glfw.contextSlot, window);
- }
- static GLFWglproc getProcAddressOSMesa(const char* procname)
- {
- return (GLFWglproc) OSMesaGetProcAddress(procname);
- }
- static void destroyContextOSMesa(_GLFWwindow* window)
- {
- if (window->context.osmesa.handle)
- {
- OSMesaDestroyContext(window->context.osmesa.handle);
- window->context.osmesa.handle = NULL;
- }
- if (window->context.osmesa.buffer)
- {
- free(window->context.osmesa.buffer);
- window->context.osmesa.width = 0;
- window->context.osmesa.height = 0;
- }
- }
- static void swapBuffersOSMesa(_GLFWwindow* window UNUSED)
- {
- // No double buffering on OSMesa
- }
- static void swapIntervalOSMesa(int interval UNUSED)
- {
- // No swap interval on OSMesa
- }
- static int extensionSupportedOSMesa(const char* extension UNUSED)
- {
- // OSMesa does not have extensions
- return false;
- }
- //////////////////////////////////////////////////////////////////////////
- ////// GLFW internal API //////
- //////////////////////////////////////////////////////////////////////////
- bool _glfwInitOSMesa(void)
- {
- int i;
- const char* sonames[] =
- {
- #if defined(_GLFW_OSMESA_LIBRARY)
- _GLFW_OSMESA_LIBRARY,
- #elif defined(_WIN32)
- "libOSMesa.dll",
- "OSMesa.dll",
- #elif defined(__APPLE__)
- "libOSMesa.8.dylib",
- #elif defined(__CYGWIN__)
- "libOSMesa-8.so",
- #else
- "libOSMesa.so.8",
- "libOSMesa.so.6",
- #endif
- NULL
- };
- if (_glfw.osmesa.handle)
- return true;
- for (i = 0; sonames[i]; i++)
- {
- _glfw.osmesa.handle = _glfw_dlopen(sonames[i]);
- if (_glfw.osmesa.handle)
- break;
- }
- if (!_glfw.osmesa.handle)
- {
- _glfwInputError(GLFW_API_UNAVAILABLE, "OSMesa: Library not found");
- return false;
- }
- glfw_dlsym(_glfw.osmesa.CreateContextExt, _glfw.osmesa.handle, "OSMesaCreateContextExt");
- glfw_dlsym(_glfw.osmesa.CreateContextAttribs, _glfw.osmesa.handle, "OSMesaCreateContextAttribs");
- glfw_dlsym(_glfw.osmesa.DestroyContext, _glfw.osmesa.handle, "OSMesaDestroyContext");
- glfw_dlsym(_glfw.osmesa.MakeCurrent, _glfw.osmesa.handle, "OSMesaMakeCurrent");
- glfw_dlsym(_glfw.osmesa.GetColorBuffer, _glfw.osmesa.handle, "OSMesaGetColorBuffer");
- glfw_dlsym(_glfw.osmesa.GetDepthBuffer, _glfw.osmesa.handle, "OSMesaGetDepthBuffer");
- glfw_dlsym(_glfw.osmesa.GetProcAddress, _glfw.osmesa.handle, "OSMesaGetProcAddress");
- if (!_glfw.osmesa.CreateContextExt ||
- !_glfw.osmesa.DestroyContext ||
- !_glfw.osmesa.MakeCurrent ||
- !_glfw.osmesa.GetColorBuffer ||
- !_glfw.osmesa.GetDepthBuffer ||
- !_glfw.osmesa.GetProcAddress)
- {
- _glfwInputError(GLFW_PLATFORM_ERROR,
- "OSMesa: Failed to load required entry points");
- _glfwTerminateOSMesa();
- return false;
- }
- return true;
- }
- void _glfwTerminateOSMesa(void)
- {
- if (_glfw.osmesa.handle)
- {
- _glfw_dlclose(_glfw.osmesa.handle);
- _glfw.osmesa.handle = NULL;
- }
- }
- #define setAttrib(a, v) \
- { \
- assert(((size_t) index + 1) < sizeof(attribs) / sizeof(attribs[0])); \
- attribs[index++] = a; \
- attribs[index++] = v; \
- }
- bool _glfwCreateContextOSMesa(_GLFWwindow* window,
- const _GLFWctxconfig* ctxconfig,
- const _GLFWfbconfig* fbconfig)
- {
- OSMesaContext share = NULL;
- const int accumBits = fbconfig->accumRedBits +
- fbconfig->accumGreenBits +
- fbconfig->accumBlueBits +
- fbconfig->accumAlphaBits;
- if (ctxconfig->client == GLFW_OPENGL_ES_API)
- {
- _glfwInputError(GLFW_API_UNAVAILABLE,
- "OSMesa: OpenGL ES is not available on OSMesa");
- return false;
- }
- if (ctxconfig->share)
- share = ctxconfig->share->context.osmesa.handle;
- if (OSMesaCreateContextAttribs)
- {
- int index = 0, attribs[40];
- setAttrib(OSMESA_FORMAT, OSMESA_RGBA);
- setAttrib(OSMESA_DEPTH_BITS, fbconfig->depthBits);
- setAttrib(OSMESA_STENCIL_BITS, fbconfig->stencilBits);
- setAttrib(OSMESA_ACCUM_BITS, accumBits);
- if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
- {
- setAttrib(OSMESA_PROFILE, OSMESA_CORE_PROFILE);
- }
- else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
- {
- setAttrib(OSMESA_PROFILE, OSMESA_COMPAT_PROFILE);
- }
- if (ctxconfig->major != 1 || ctxconfig->minor != 0)
- {
- setAttrib(OSMESA_CONTEXT_MAJOR_VERSION, ctxconfig->major);
- setAttrib(OSMESA_CONTEXT_MINOR_VERSION, ctxconfig->minor);
- }
- if (ctxconfig->forward)
- {
- _glfwInputError(GLFW_VERSION_UNAVAILABLE,
- "OSMesa: Forward-compatible contexts not supported");
- return false;
- }
- setAttrib(0, 0);
- window->context.osmesa.handle =
- OSMesaCreateContextAttribs(attribs, share);
- }
- else
- {
- if (ctxconfig->profile)
- {
- _glfwInputError(GLFW_VERSION_UNAVAILABLE,
- "OSMesa: OpenGL profiles unavailable");
- return false;
- }
- window->context.osmesa.handle =
- OSMesaCreateContextExt(OSMESA_RGBA,
- fbconfig->depthBits,
- fbconfig->stencilBits,
- accumBits,
- share);
- }
- if (window->context.osmesa.handle == NULL)
- {
- _glfwInputError(GLFW_VERSION_UNAVAILABLE,
- "OSMesa: Failed to create context");
- return false;
- }
- window->context.makeCurrent = makeContextCurrentOSMesa;
- window->context.swapBuffers = swapBuffersOSMesa;
- window->context.swapInterval = swapIntervalOSMesa;
- window->context.extensionSupported = extensionSupportedOSMesa;
- window->context.getProcAddress = getProcAddressOSMesa;
- window->context.destroy = destroyContextOSMesa;
- return true;
- }
- #undef setAttrib
- //////////////////////////////////////////////////////////////////////////
- ////// GLFW native API //////
- //////////////////////////////////////////////////////////////////////////
- GLFWAPI int glfwGetOSMesaColorBuffer(GLFWwindow* handle, int* width,
- int* height, int* format, void** buffer)
- {
- void* mesaBuffer;
- GLint mesaWidth, mesaHeight, mesaFormat;
- _GLFWwindow* window = (_GLFWwindow*) handle;
- assert(window != NULL);
- _GLFW_REQUIRE_INIT_OR_RETURN(false);
- if (!OSMesaGetColorBuffer(window->context.osmesa.handle,
- &mesaWidth, &mesaHeight,
- &mesaFormat, &mesaBuffer))
- {
- _glfwInputError(GLFW_PLATFORM_ERROR,
- "OSMesa: Failed to retrieve color buffer");
- return false;
- }
- if (width)
- *width = mesaWidth;
- if (height)
- *height = mesaHeight;
- if (format)
- *format = mesaFormat;
- if (buffer)
- *buffer = mesaBuffer;
- return true;
- }
- GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow* handle,
- int* width, int* height,
- int* bytesPerValue,
- void** buffer)
- {
- void* mesaBuffer;
- GLint mesaWidth, mesaHeight, mesaBytes;
- _GLFWwindow* window = (_GLFWwindow*) handle;
- assert(window != NULL);
- _GLFW_REQUIRE_INIT_OR_RETURN(false);
- if (!OSMesaGetDepthBuffer(window->context.osmesa.handle,
- &mesaWidth, &mesaHeight,
- &mesaBytes, &mesaBuffer))
- {
- _glfwInputError(GLFW_PLATFORM_ERROR,
- "OSMesa: Failed to retrieve depth buffer");
- return false;
- }
- if (width)
- *width = mesaWidth;
- if (height)
- *height = mesaHeight;
- if (bytesPerValue)
- *bytesPerValue = mesaBytes;
- if (buffer)
- *buffer = mesaBuffer;
- return true;
- }
- GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* handle)
- {
- _GLFWwindow* window = (_GLFWwindow*) handle;
- _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
- if (window->context.client == GLFW_NO_API)
- {
- _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
- return NULL;
- }
- return window->context.osmesa.handle;
- }
|