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
|
/*
* 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"
#if USE(OPENGL) || USE(OPENGL_ES)
#include "GLContext.h"
#include <wtf/ThreadSpecific.h>
#include <wtf/text/StringToIntegerConversion.h>
#if USE(LIBEPOXY)
#include <epoxy/gl.h>
#elif USE(OPENGL_ES)
#include <GLES2/gl2.h>
#else
#include "OpenGLShims.h"
#endif
#if USE(EGL)
#include "GLContextEGL.h"
#endif
#if USE(GLX)
#include "GLContextGLX.h"
#endif
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 { nullptr };
};
ThreadSpecific<ThreadGlobalGLContext>* ThreadGlobalGLContext::staticGLContext;
inline ThreadGlobalGLContext* currentContext()
{
if (!ThreadGlobalGLContext::staticGLContext)
ThreadGlobalGLContext::staticGLContext = new ThreadSpecific<ThreadGlobalGLContext>;
return *ThreadGlobalGLContext::staticGLContext;
}
static bool initializeOpenGLShimsIfNeeded()
{
#if USE(OPENGL_ES) || USE(LIBEPOXY) || (USE(ANGLE) && !(PLATFORM(GTK) || PLATFORM(WPE)))
return true;
#else
static bool initialized = false;
static bool success = true;
if (!initialized) {
success = initializeOpenGLShims();
initialized = true;
}
return success;
#endif
}
std::unique_ptr<GLContext> GLContext::createContextForWindow(GLNativeWindowType windowHandle, PlatformDisplay* platformDisplay)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
PlatformDisplay& display = platformDisplay ? *platformDisplay : PlatformDisplay::sharedDisplay();
#if PLATFORM(WAYLAND)
if (display.type() == PlatformDisplay::Type::Wayland) {
if (auto eglContext = GLContextEGL::createContext(windowHandle, display))
return eglContext;
return nullptr;
}
#endif
#if USE(GLX)
if (display.type() == PlatformDisplay::Type::X11) {
if (auto glxContext = GLContextGLX::createContext(windowHandle, display))
return glxContext;
}
#endif
#if USE(EGL)
if (auto eglContext = GLContextEGL::createContext(windowHandle, display))
return eglContext;
#endif
return nullptr;
}
std::unique_ptr<GLContext> GLContext::createOffscreenContext(PlatformDisplay* platformDisplay)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
return createContextForWindow(0, platformDisplay ? platformDisplay : &PlatformDisplay::sharedDisplay());
}
std::unique_ptr<GLContext> GLContext::createSharingContext(PlatformDisplay& display)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
#if USE(GLX)
if (display.type() == PlatformDisplay::Type::X11) {
if (auto glxContext = GLContextGLX::createSharingContext(display))
return glxContext;
}
#endif
#if USE(EGL) || PLATFORM(WAYLAND) || PLATFORM(WPE)
if (auto eglContext = GLContextEGL::createSharingContext(display))
return eglContext;
#endif
return nullptr;
}
GLContext::GLContext(PlatformDisplay& display)
: m_display(display)
{
}
GLContext::~GLContext()
{
if (this == currentContext()->context())
currentContext()->setContext(nullptr);
}
bool GLContext::makeContextCurrent()
{
currentContext()->setContext(this);
return true;
}
GLContext* GLContext::current()
{
return currentContext()->context();
}
bool GLContext::isExtensionSupported(const char* extensionList, const char* extension)
{
if (!extensionList)
return false;
ASSERT(extension);
int extensionLen = strlen(extension);
const char* extensionListPtr = extensionList;
while ((extensionListPtr = strstr(extensionListPtr, extension))) {
if (extensionListPtr[extensionLen] == ' ' || extensionListPtr[extensionLen] == '\0')
return true;
extensionListPtr += extensionLen;
}
return false;
}
unsigned GLContext::version()
{
if (!m_version) {
// Version string can start with the version number (all versions except GLES 1 and 2) or with
// "OpenGL". Different fields inside the version string are separated by spaces.
auto versionString = String::fromLatin1(reinterpret_cast<const char*>(::glGetString(GL_VERSION)));
Vector<String> versionStringComponents = versionString.split(' ');
Vector<String> versionDigits;
if (versionStringComponents[0] == "OpenGL"_s) {
// If the version string starts with "OpenGL" it can be GLES 1 or 2. In GLES1 version string starts
// with "OpenGL ES-<profile> major.minor" and in GLES2 with "OpenGL ES major.minor". Version is the
// third component in both cases.
versionDigits = versionStringComponents[2].split('.');
} else {
// Version is the first component. The version number is always "major.minor" or
// "major.minor.release". Ignore the release number.
versionDigits = versionStringComponents[0].split('.');
}
m_version = parseIntegerAllowingTrailingJunk<unsigned>(versionDigits[0]).value_or(0) * 100
+ parseIntegerAllowingTrailingJunk<unsigned>(versionDigits[1]).value_or(0) * 10;
}
return m_version;
}
} // namespace WebCore
#endif
|