2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "GLPlatformContext.h"
29 #if USE(ACCELERATED_COMPOSITING)
32 #include "GLXContext.h"
34 #include "EGLContext.h"
37 #include "NotImplemented.h"
42 static PFNGLGETGRAPHICSRESETSTATUSEXTPROC glGetGraphicsResetStatus = 0;
44 static PFNGLGETGRAPHICSRESETSTATUSARBPROC glGetGraphicsResetStatus = 0;
46 static GLPlatformContext* m_currentContext = 0;
48 class GLCurrentContextWrapper : public GLPlatformContext {
51 GLCurrentContextWrapper()
55 m_contextHandle = glXGetCurrentContext();
57 m_contextHandle = eglGetCurrentContext();
60 m_currentContext = this;
63 virtual ~GLCurrentContextWrapper() { }
66 static PassOwnPtr<GLPlatformContext> createOffScreenContext()
69 return adoptPtr(new GLXOffScreenContext());
71 return adoptPtr(new EGLOffScreenContext());
77 static HashSet<String> parseExtensions(const String& extensionsString)
79 Vector<String> extNames;
80 extensionsString.split(" ", extNames);
81 HashSet<String> splitExtNames;
82 unsigned size = extNames.size();
83 for (unsigned i = 0; i < size; ++i)
84 splitExtNames.add(extNames[i]);
90 static void resolveResetStatusExtension()
92 static bool resolvedRobustnessExtension = false;
93 if (!resolvedRobustnessExtension) {
94 resolvedRobustnessExtension = true;
96 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSEXTPROC>(eglGetProcAddress("glGetGraphicsResetStatusEXT"));
98 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(eglGetProcAddress("glGetGraphicsResetStatusARB"));
100 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glGetGraphicsResetStatusARB")));
105 PassOwnPtr<GLPlatformContext> GLPlatformContext::createContext(GraphicsContext3D::RenderStyle renderStyle)
107 #if !USE(OPENGL_ES_2)
108 if (!initializeOpenGLShims())
112 switch (renderStyle) {
113 case GraphicsContext3D::RenderOffscreen:
114 if (OwnPtr<GLPlatformContext> context = createOffScreenContext())
115 return context.release();
117 case GraphicsContext3D::RenderToCurrentGLContext:
118 if (OwnPtr<GLPlatformContext> context = adoptPtr(new GLCurrentContextWrapper()))
119 return context.release();
121 case GraphicsContext3D::RenderDirectlyToHostWindow:
122 ASSERT_NOT_REACHED();
129 bool GLPlatformContext::supportsGLExtension(const String& name)
131 static HashSet<String> supportedExtensions;
133 if (!supportedExtensions.size()) {
134 String rawExtensions = reinterpret_cast<const char*>(::glGetString(GL_EXTENSIONS));
135 supportedExtensions = parseExtensions(rawExtensions);
138 if (supportedExtensions.contains(name))
145 bool GLPlatformContext::supportsEGLExtension(EGLDisplay display, const String& name)
147 static HashSet<String> supportedExtensions;
149 if (!supportedExtensions.size()) {
150 if (display == EGL_NO_DISPLAY)
153 String rawExtensions = reinterpret_cast<const char*>(eglQueryString(display, EGL_EXTENSIONS));
154 supportedExtensions = parseExtensions(rawExtensions);
157 if (supportedExtensions.contains(name))
165 bool GLPlatformContext::supportsGLXExtension(Display* display, const String& name)
167 static HashSet<String> supportedExtensions;
169 if (!supportedExtensions.size()) {
173 String rawExtensions = glXQueryExtensionsString(display, DefaultScreen(display));
174 supportedExtensions = parseExtensions(rawExtensions);
177 if (supportedExtensions.contains(name))
184 GLPlatformContext::GLPlatformContext()
186 , m_resetLostContext(false)
190 GLPlatformContext::~GLPlatformContext()
192 if (this == m_currentContext)
193 m_currentContext = 0;
196 bool GLPlatformContext::makeCurrent(GLPlatformSurface* surface)
198 m_contextLost = false;
200 if (isCurrentContext())
203 m_currentContext = 0;
205 if (!surface || (surface && !surface->drawable()))
206 platformReleaseCurrent();
207 else if (platformMakeCurrent(surface))
208 m_currentContext = this;
210 if (m_resetLostContext) {
211 resolveResetStatusExtension();
213 if (glGetGraphicsResetStatus) {
214 GLenum status = glGetGraphicsResetStatus();
217 case PLATFORMCONTEXT_NO_ERROR:
219 case PLATFORMCONTEXT_GUILTY_CONTEXT_RESET:
220 m_contextLost = true;
222 case PLATFORMCONTEXT_INNOCENT_CONTEXT_RESET:
224 case PLATFORMCONTEXT_UNKNOWN_CONTEXT_RESET:
225 m_contextLost = true;
233 return m_currentContext;
236 bool GLPlatformContext::isValid() const
238 return !m_contextLost;
241 void GLPlatformContext::releaseCurrent()
243 if (!isCurrentContext())
246 m_currentContext = 0;
247 platformReleaseCurrent();
250 PlatformContext GLPlatformContext::handle() const
252 return m_contextHandle;
255 bool GLPlatformContext::isCurrentContext() const
260 bool GLPlatformContext::initialize(GLPlatformSurface*, PlatformContext)
265 GLPlatformContext* GLPlatformContext::getCurrent()
267 return m_currentContext;
270 bool GLPlatformContext::platformMakeCurrent(GLPlatformSurface*)
275 void GLPlatformContext::platformReleaseCurrent()
280 void GLPlatformContext::destroy()
283 m_resetLostContext = false;
285 if (this == m_currentContext)
286 m_currentContext = 0;
289 } // namespace WebCore