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
|
/*
* Copyright (C) 2011, 2012 Igalia, S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "GLContext.h"
#if USE(OPENGL)
#if USE(EGL)
#include "GLContextEGL.h"
#endif
#if USE(GLX)
#include "GLContextGLX.h"
#endif
#include <wtf/ThreadSpecific.h>
#if PLATFORM(X11)
#include <X11/Xlib.h>
#endif
#if PLATFORM(GTK)
#include <gdk/gdk.h>
#ifndef GTK_API_VERSION_2
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
#endif
#endif
using WTF::ThreadSpecific;
namespace WebCore {
class ThreadGlobalGLContext {
public:
static ThreadSpecific<ThreadGlobalGLContext>* staticGLContext;
void setContext(GLContext* context) { m_context = context; }
GLContext* context() { return m_context; }
private:
GLContext* m_context;
};
ThreadSpecific<ThreadGlobalGLContext>* ThreadGlobalGLContext::staticGLContext;
inline ThreadGlobalGLContext* currentContext()
{
if (!ThreadGlobalGLContext::staticGLContext)
ThreadGlobalGLContext::staticGLContext = new ThreadSpecific<ThreadGlobalGLContext>;
return *ThreadGlobalGLContext::staticGLContext;
}
GLContext* GLContext::sharingContext()
{
DEFINE_STATIC_LOCAL(OwnPtr<GLContext>, sharing, (createOffscreenContext()));
return sharing.get();
}
#if PLATFORM(X11)
// We do not want to call glXMakeContextCurrent using different Display pointers,
// because it might lead to crashes in some drivers (fglrx). We use a shared display
// pointer here.
static Display* gSharedX11Display = 0;
Display* GLContext::sharedX11Display()
{
if (!gSharedX11Display)
gSharedX11Display = XOpenDisplay(0);
return gSharedX11Display;
}
void GLContext::cleanupSharedX11Display()
{
if (!gSharedX11Display)
return;
XCloseDisplay(gSharedX11Display);
gSharedX11Display = 0;
}
#endif // PLATFORM(X11)
// Because of driver bugs, exiting the program when there are active pbuffers
// can crash the X server (this has been observed with the official Nvidia drivers).
// We need to ensure that we clean everything up on exit. There are several reasons
// that GraphicsContext3Ds will still be alive at exit, including user error (memory
// leaks) and the page cache. In any case, we don't want the X server to crash.
typedef Vector<GLContext*> ActiveContextList;
static ActiveContextList& activeContextList()
{
DEFINE_STATIC_LOCAL(ActiveContextList, activeContexts, ());
return activeContexts;
}
void GLContext::addActiveContext(GLContext* context)
{
static bool addedAtExitHandler = false;
if (!addedAtExitHandler) {
atexit(&GLContext::cleanupActiveContextsAtExit);
addedAtExitHandler = true;
}
activeContextList().append(context);
}
static bool gCleaningUpAtExit = false;
void GLContext::removeActiveContext(GLContext* context)
{
// If we are cleaning up the context list at exit, don't bother removing the context
// from the list, since we don't want to modify the list while it's being iterated.
if (gCleaningUpAtExit)
return;
ActiveContextList& contextList = activeContextList();
size_t i = contextList.find(context);
if (i != notFound)
contextList.remove(i);
}
void GLContext::cleanupActiveContextsAtExit()
{
gCleaningUpAtExit = true;
ActiveContextList& contextList = activeContextList();
for (size_t i = 0; i < contextList.size(); ++i)
delete contextList[i];
#if PLATFORM(X11)
cleanupSharedX11Display();
#endif
}
PassOwnPtr<GLContext> GLContext::createContextForWindow(uint64_t windowHandle, GLContext* sharingContext)
{
#if PLATFORM(GTK) && defined(GDK_WINDOWING_WAYLAND) && USE(EGL)
GdkDisplay* display = gdk_display_manager_get_default_display(gdk_display_manager_get());
if (GDK_IS_WAYLAND_DISPLAY(display)) {
if (OwnPtr<GLContext> eglContext = GLContextEGL::createContext(windowHandle, sharingContext))
return eglContext.release();
return nullptr;
}
#endif
#if USE(GLX)
if (OwnPtr<GLContext> glxContext = GLContextGLX::createContext(windowHandle, sharingContext))
return glxContext.release();
#endif
#if USE(EGL)
if (OwnPtr<GLContext> eglContext = GLContextEGL::createContext(windowHandle, sharingContext))
return eglContext.release();
#endif
return nullptr;
}
GLContext::GLContext()
{
addActiveContext(this);
}
PassOwnPtr<GLContext> GLContext::createOffscreenContext(GLContext* sharingContext)
{
return createContextForWindow(0, sharingContext);
}
GLContext::~GLContext()
{
if (this == currentContext()->context())
currentContext()->setContext(0);
removeActiveContext(this);
}
bool GLContext::makeContextCurrent()
{
currentContext()->setContext(this);
return true;
}
GLContext* GLContext::getCurrent()
{
return currentContext()->context();
}
} // namespace WebCore
#endif // USE(OPENGL)
|