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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 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 "GLContextEGL.h"
#if USE(EGL)
#include "GraphicsContext3D.h"
#include <wtf/OwnPtr.h>
#if USE(CAIRO)
#include <cairo.h>
#endif
#if USE(OPENGL_ES_2)
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#include "OpenGLShims.h"
#endif
#if ENABLE(ACCELERATED_2D_CANVAS)
#include <cairo-gl.h>
#endif
namespace WebCore {
static EGLDisplay gSharedEGLDisplay = EGL_NO_DISPLAY;
#if USE(OPENGL_ES_2)
static const EGLenum gGLAPI = EGL_OPENGL_ES_API;
#else
static const EGLenum gGLAPI = EGL_OPENGL_API;
#endif
static EGLDisplay sharedEGLDisplay()
{
static bool initialized = false;
if (!initialized) {
initialized = true;
#if PLATFORM(X11)
gSharedEGLDisplay = eglGetDisplay(GLContext::sharedX11Display());
#else
gSharedEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
#endif
if (gSharedEGLDisplay != EGL_NO_DISPLAY && (!eglInitialize(gSharedEGLDisplay, 0, 0) || !eglBindAPI(gGLAPI)))
gSharedEGLDisplay = EGL_NO_DISPLAY;
}
return gSharedEGLDisplay;
}
static const EGLint gContextAttributes[] = {
#if USE(OPENGL_ES_2)
EGL_CONTEXT_CLIENT_VERSION, 2,
#endif
EGL_NONE
};
static bool getEGLConfig(EGLConfig* config, GLContextEGL::EGLSurfaceType surfaceType)
{
EGLint attributeList[] = {
#if USE(OPENGL_ES_2)
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#else
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
#endif
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_STENCIL_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_NONE,
EGL_NONE
};
switch (surfaceType) {
case GLContextEGL::PbufferSurface:
attributeList[13] = EGL_PBUFFER_BIT;
break;
case GLContextEGL::PixmapSurface:
attributeList[13] = EGL_PIXMAP_BIT;
break;
case GLContextEGL::WindowSurface:
attributeList[13] = EGL_WINDOW_BIT;
break;
}
EGLint numberConfigsReturned;
return eglChooseConfig(sharedEGLDisplay(), attributeList, config, 1, &numberConfigsReturned) && numberConfigsReturned;
}
PassOwnPtr<GLContextEGL> GLContextEGL::createWindowContext(EGLNativeWindowType window, GLContext* sharingContext)
{
EGLContext eglSharingContext = sharingContext ? static_cast<GLContextEGL*>(sharingContext)->m_context : 0;
EGLDisplay display = sharedEGLDisplay();
if (display == EGL_NO_DISPLAY)
return nullptr;
EGLConfig config;
if (!getEGLConfig(&config, WindowSurface))
return nullptr;
EGLContext context = eglCreateContext(display, config, eglSharingContext, gContextAttributes);
if (context == EGL_NO_CONTEXT)
return nullptr;
EGLSurface surface = eglCreateWindowSurface(display, config, window, 0);
if (surface == EGL_NO_SURFACE)
return nullptr;
return adoptPtr(new GLContextEGL(context, surface, WindowSurface));
}
PassOwnPtr<GLContextEGL> GLContextEGL::createPbufferContext(EGLContext sharingContext)
{
EGLDisplay display = sharedEGLDisplay();
if (display == EGL_NO_DISPLAY)
return nullptr;
EGLConfig config;
if (!getEGLConfig(&config, PbufferSurface))
return nullptr;
EGLContext context = eglCreateContext(display, config, sharingContext, gContextAttributes);
if (context == EGL_NO_CONTEXT)
return nullptr;
static const int pbufferAttributes[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttributes);
if (surface == EGL_NO_SURFACE) {
eglDestroyContext(display, context);
return nullptr;
}
return adoptPtr(new GLContextEGL(context, surface, PbufferSurface));
}
PassOwnPtr<GLContextEGL> GLContextEGL::createPixmapContext(EGLContext sharingContext)
{
#if PLATFORM(X11)
EGLDisplay display = sharedEGLDisplay();
if (display == EGL_NO_DISPLAY)
return nullptr;
EGLConfig config;
if (!getEGLConfig(&config, PixmapSurface))
return nullptr;
EGLContext context = eglCreateContext(display, config, sharingContext, gContextAttributes);
if (context == EGL_NO_CONTEXT)
return nullptr;
EGLint depth;
if (!eglGetConfigAttrib(display, config, EGL_DEPTH_SIZE, &depth))
return nullptr;
Pixmap pixmap = XCreatePixmap(sharedX11Display(), DefaultRootWindow(sharedX11Display()), 1, 1, depth);
if (!pixmap)
return nullptr;
EGLSurface surface = eglCreatePixmapSurface(display, config, pixmap, 0);
if (surface == EGL_NO_SURFACE)
return nullptr;
return adoptPtr(new GLContextEGL(context, surface, PixmapSurface));
#else
return nullptr;
#endif
}
PassOwnPtr<GLContextEGL> GLContextEGL::createContext(EGLNativeWindowType window, GLContext* sharingContext)
{
if (!sharedEGLDisplay())
return nullptr;
static bool initialized = false;
static bool success = true;
if (!initialized) {
#if !USE(OPENGL_ES_2)
success = initializeOpenGLShims();
#endif
initialized = true;
}
if (!success)
return nullptr;
EGLContext eglSharingContext = sharingContext ? static_cast<GLContextEGL*>(sharingContext)->m_context : 0;
OwnPtr<GLContextEGL> context = window ? createWindowContext(window, sharingContext) : nullptr;
if (!context)
context = createPixmapContext(eglSharingContext);
if (!context)
context = createPbufferContext(eglSharingContext);
return context.release();
}
GLContextEGL::GLContextEGL(EGLContext context, EGLSurface surface, EGLSurfaceType type)
: m_context(context)
, m_surface(surface)
, m_type(type)
, m_cairoDevice(0)
{
}
GLContextEGL::~GLContextEGL()
{
#if USE(CAIRO)
if (m_cairoDevice)
cairo_device_destroy(m_cairoDevice);
#endif
EGLDisplay display = sharedEGLDisplay();
if (m_context) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(display, m_context);
}
if (m_surface)
eglDestroySurface(display, m_surface);
}
bool GLContextEGL::canRenderToDefaultFramebuffer()
{
return m_type == WindowSurface;
}
IntSize GLContextEGL::defaultFrameBufferSize()
{
if (!canRenderToDefaultFramebuffer())
return IntSize();
EGLint width, height;
if (!eglQuerySurface(sharedEGLDisplay(), m_surface, EGL_WIDTH, &width)
|| !eglQuerySurface(sharedEGLDisplay(), m_surface, EGL_HEIGHT, &height))
return IntSize();
return IntSize(width, height);
}
bool GLContextEGL::makeContextCurrent()
{
ASSERT(m_context && m_surface);
GLContext::makeContextCurrent();
if (eglGetCurrentContext() == m_context)
return true;
return eglMakeCurrent(sharedEGLDisplay(), m_surface, m_surface, m_context);
}
void GLContextEGL::swapBuffers()
{
ASSERT(m_surface);
eglSwapBuffers(sharedEGLDisplay(), m_surface);
}
void GLContextEGL::waitNative()
{
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
}
cairo_device_t* GLContextEGL::cairoDevice()
{
if (m_cairoDevice)
return m_cairoDevice;
#if ENABLE(ACCELERATED_2D_CANVAS)
m_cairoDevice = cairo_egl_device_create(sharedEGLDisplay(), m_context);
#endif
return m_cairoDevice;
}
#if ENABLE(WEBGL)
PlatformGraphicsContext3D GLContextEGL::platformContext()
{
return m_context;
}
#endif
} // namespace WebCore
#endif // USE(EGL)
|