GLPlatformContext.cpp [plain text]
#include "config.h"
#if ENABLE(GRAPHICS_CONTEXT_3D)
#include "GLPlatformContext.h"
#if USE(GLX)
#include "GLXContext.h"
#elif USE(EGL)
#include "EGLContext.h"
#endif
#include "NotImplemented.h"
namespace WebCore {
#if USE(OPENGL_ES_2)
static PFNGLGETGRAPHICSRESETSTATUSEXTPROC glGetGraphicsResetStatus = 0;
#else
static PFNGLGETGRAPHICSRESETSTATUSARBPROC glGetGraphicsResetStatus = 0;
#endif
class GLCurrentContextWrapper : public GLPlatformContext {
public:
GLCurrentContextWrapper()
: GLPlatformContext()
{
#if USE(GLX)
m_contextHandle = glXGetCurrentContext();
#elif USE(EGL)
m_contextHandle = eglGetCurrentContext();
#endif
}
virtual ~GLCurrentContextWrapper() { }
bool isCurrentContext() const override
{
return true;
}
};
static std::unique_ptr<GLPlatformContext> createOffScreenContext()
{
#if USE(GLX)
return std::make_unique<GLXOffScreenContext>();
#elif USE(EGL)
return std::make_unique<EGLOffScreenContext>();
#else
return nullptr;
#endif
}
static HashSet<String> parseExtensions(const String& extensionsString)
{
Vector<String> extNames;
extensionsString.split(' ', extNames);
HashSet<String> splitExtNames;
unsigned size = extNames.size();
for (unsigned i = 0; i < size; ++i)
splitExtNames.add(extNames[i]);
extNames.clear();
return splitExtNames;
}
static void resolveResetStatusExtension()
{
static bool resolvedRobustnessExtension = false;
if (!resolvedRobustnessExtension) {
resolvedRobustnessExtension = true;
#if USE(OPENGL_ES_2)
glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSEXTPROC>(eglGetProcAddress("glGetGraphicsResetStatusEXT"));
#elif USE(EGL)
glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(eglGetProcAddress("glGetGraphicsResetStatusARB"));
#elif USE(GLX)
glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glGetGraphicsResetStatusARB")));
#endif
}
}
std::unique_ptr<GLPlatformContext> GLPlatformContext::createContext(GraphicsContext3D::RenderStyle renderStyle)
{
#if !USE(OPENGL_ES_2)
if (!initializeOpenGLShims())
return nullptr;
#endif
switch (renderStyle) {
case GraphicsContext3D::RenderOffscreen:
return createOffScreenContext();
case GraphicsContext3D::RenderToCurrentGLContext:
return std::make_unique<GLCurrentContextWrapper>();
case GraphicsContext3D::RenderDirectlyToHostWindow:
ASSERT_NOT_REACHED();
break;
}
return nullptr;
}
bool GLPlatformContext::supportsGLExtension(const String& name)
{
static HashSet<String> supportedExtensions;
if (!supportedExtensions.size()) {
String rawExtensions = reinterpret_cast<const char*>(::glGetString(GL_EXTENSIONS));
supportedExtensions = parseExtensions(rawExtensions);
}
if (supportedExtensions.contains(name))
return true;
return false;
}
#if USE(EGL)
bool GLPlatformContext::supportsEGLExtension(EGLDisplay display, const String& name)
{
static HashSet<String> supportedExtensions;
if (!supportedExtensions.size()) {
if (display == EGL_NO_DISPLAY)
return false;
String rawExtensions = reinterpret_cast<const char*>(eglQueryString(display, EGL_EXTENSIONS));
supportedExtensions = parseExtensions(rawExtensions);
}
if (supportedExtensions.contains(name))
return true;
return false;
}
#endif
#if USE(GLX)
bool GLPlatformContext::supportsGLXExtension(Display* display, const String& name)
{
static HashSet<String> supportedExtensions;
if (!supportedExtensions.size()) {
if (!display)
return false;
String rawExtensions = glXQueryExtensionsString(display, DefaultScreen(display));
supportedExtensions = parseExtensions(rawExtensions);
}
if (supportedExtensions.contains(name))
return true;
return false;
}
#endif
GLPlatformContext::GLPlatformContext()
: m_contextHandle(0)
, m_resetLostContext(false)
{
}
GLPlatformContext::~GLPlatformContext()
{
}
bool GLPlatformContext::makeCurrent(GLPlatformSurface* surface)
{
m_contextLost = false;
if (isCurrentContext() && (!surface || surface->isCurrentDrawable()))
return true;
GLPlatformContext* currentContext = 0;
if (!surface || (surface && !surface->drawable()))
platformReleaseCurrent();
else if (platformMakeCurrent(surface)) {
currentContext = this;
surface->onMakeCurrent();
}
if (m_resetLostContext) {
resolveResetStatusExtension();
if (glGetGraphicsResetStatus) {
GLenum status = glGetGraphicsResetStatus();
switch (status) {
case PLATFORMCONTEXT_NO_ERROR:
break;
case PLATFORMCONTEXT_GUILTY_CONTEXT_RESET:
m_contextLost = true;
break;
case PLATFORMCONTEXT_INNOCENT_CONTEXT_RESET:
break;
case PLATFORMCONTEXT_UNKNOWN_CONTEXT_RESET:
m_contextLost = true;
break;
default:
break;
}
}
}
return currentContext;
}
bool GLPlatformContext::isValid() const
{
return !m_contextLost;
}
void GLPlatformContext::releaseCurrent()
{
if (isCurrentContext())
platformReleaseCurrent();
}
PlatformContext GLPlatformContext::handle() const
{
return m_contextHandle;
}
bool GLPlatformContext::initialize(GLPlatformSurface*, PlatformContext)
{
return true;
}
bool GLPlatformContext::platformMakeCurrent(GLPlatformSurface*)
{
return true;
}
void GLPlatformContext::platformReleaseCurrent()
{
notImplemented();
}
void GLPlatformContext::destroy()
{
m_contextHandle = 0;
m_resetLostContext = false;
}
}
#endif