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"
46 static PFNGLGETGRAPHICSRESETSTATUSEXTPROC glGetGraphicsResetStatus = 0;
48 static PFNGLGETGRAPHICSRESETSTATUSARBPROC glGetGraphicsResetStatus = 0;
50 static GLPlatformContext* m_currentContext = 0;
52 class GLCurrentContextWrapper : public GLPlatformContext {
55 GLCurrentContextWrapper()
58 // FIXME:: This is a workaround until support to build evas with EGL has been added.
59 #if USE(GLX) || PLATFORM(EFL)
60 m_contextHandle = glXGetCurrentContext();
62 m_contextHandle = eglGetCurrentContext();
65 m_currentContext = this;
68 virtual ~GLCurrentContextWrapper() { }
71 static PassOwnPtr<GLPlatformContext> createOffScreenContext()
74 return adoptPtr(new GLXOffScreenContext());
76 return adoptPtr(new EGLOffScreenContext());
82 static HashSet<String> parseExtensions(const String& extensionsString)
84 Vector<String> extNames;
85 extensionsString.split(" ", extNames);
86 HashSet<String> splitExtNames;
87 unsigned size = extNames.size();
88 for (unsigned i = 0; i < size; ++i)
89 splitExtNames.add(extNames[i]);
95 static void resolveResetStatusExtension()
97 static bool resolvedRobustnessExtension = false;
98 if (!resolvedRobustnessExtension) {
99 resolvedRobustnessExtension = true;
101 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSEXTPROC>(eglGetProcAddress("glGetGraphicsResetStatusEXT"));
103 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(eglGetProcAddress("glGetGraphicsResetStatusARB"));
105 glGetGraphicsResetStatus = reinterpret_cast<PFNGLGETGRAPHICSRESETSTATUSARBPROC>(glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glGetGraphicsResetStatusARB")));
110 PassOwnPtr<GLPlatformContext> GLPlatformContext::createContext(GraphicsContext3D::RenderStyle renderStyle)
112 #if !USE(OPENGL_ES_2)
113 if (!initializeOpenGLShims())
117 switch (renderStyle) {
118 case GraphicsContext3D::RenderOffscreen:
119 if (OwnPtr<GLPlatformContext> context = createOffScreenContext())
120 return context.release();
122 case GraphicsContext3D::RenderToCurrentGLContext:
123 if (OwnPtr<GLPlatformContext> context = adoptPtr(new GLCurrentContextWrapper()))
124 return context.release();
126 case GraphicsContext3D::RenderDirectlyToHostWindow:
127 ASSERT_NOT_REACHED();
134 bool GLPlatformContext::supportsGLExtension(const String& name)
136 static HashSet<String> supportedExtensions;
138 if (!supportedExtensions.size()) {
139 String rawExtensions = reinterpret_cast<const char*>(::glGetString(GL_EXTENSIONS));
140 supportedExtensions = parseExtensions(rawExtensions);
143 if (supportedExtensions.contains(name))
150 bool GLPlatformContext::supportsEGLExtension(EGLDisplay display, const String& name)
152 static HashSet<String> supportedExtensions;
154 if (!supportedExtensions.size()) {
155 if (display == EGL_NO_DISPLAY)
158 String rawExtensions = reinterpret_cast<const char*>(eglQueryString(display, EGL_EXTENSIONS));
159 supportedExtensions = parseExtensions(rawExtensions);
162 if (supportedExtensions.contains(name))
170 bool GLPlatformContext::supportsGLXExtension(Display* display, const String& name)
172 static HashSet<String> supportedExtensions;
174 if (!supportedExtensions.size()) {
178 String rawExtensions = glXQueryExtensionsString(display, DefaultScreen(display));
179 supportedExtensions = parseExtensions(rawExtensions);
182 if (supportedExtensions.contains(name))
189 GLPlatformContext::GLPlatformContext()
191 , m_resetLostContext(false)
195 GLPlatformContext::~GLPlatformContext()
197 if (this == m_currentContext)
198 m_currentContext = 0;
201 bool GLPlatformContext::makeCurrent(GLPlatformSurface* surface)
203 m_contextLost = false;
205 if (isCurrentContext())
208 m_currentContext = 0;
210 if (!surface || (surface && !surface->drawable()))
211 platformReleaseCurrent();
212 else if (platformMakeCurrent(surface))
213 m_currentContext = this;
215 if (m_resetLostContext) {
216 resolveResetStatusExtension();
218 if (glGetGraphicsResetStatus) {
219 GLenum status = glGetGraphicsResetStatus();
222 case PLATFORMCONTEXT_NO_ERROR:
224 case PLATFORMCONTEXT_GUILTY_CONTEXT_RESET:
225 m_contextLost = true;
227 case PLATFORMCONTEXT_INNOCENT_CONTEXT_RESET:
229 case PLATFORMCONTEXT_UNKNOWN_CONTEXT_RESET:
230 m_contextLost = true;
238 return m_currentContext;
241 bool GLPlatformContext::isValid() const
243 return !m_contextLost;
246 void GLPlatformContext::releaseCurrent()
248 if (!isCurrentContext())
251 m_currentContext = 0;
252 platformReleaseCurrent();
255 PlatformContext GLPlatformContext::handle() const
257 return m_contextHandle;
260 bool GLPlatformContext::isCurrentContext() const
265 bool GLPlatformContext::initialize(GLPlatformSurface*, PlatformContext)
270 GLPlatformContext* GLPlatformContext::getCurrent()
272 return m_currentContext;
275 bool GLPlatformContext::platformMakeCurrent(GLPlatformSurface*)
280 void GLPlatformContext::platformReleaseCurrent()
285 void GLPlatformContext::destroy()
288 m_resetLostContext = false;
290 if (this == m_currentContext)
291 m_currentContext = 0;
294 } // namespace WebCore