EGL and GLX working

This commit is contained in:
Henrique Santiago 2013-02-28 04:17:04 -03:00 committed by Eduardo Bart
parent 77995a2e88
commit 96bbe20588
11 changed files with 76 additions and 108 deletions

View File

@ -304,18 +304,24 @@ endif()
if(FRAMEWORK_GRAPHICS)
set(OPENGLES "OFF" CACHE "Use OpenGL ES 1.0 or 2.0 (for mobiles devices)" STRING)
if(OPENGLES)
set(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextegl.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextegl.h
)
if(OPENGLES STREQUAL "2.0")
find_package(OpenGLES2 REQUIRED)
find_package(EGL REQUIRED)
set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=2)
set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${EGL_INCLUDE_DIR} ${OPENGLES2_INCLUDE_DIR})
set(framework_LIBRARIES ${framework_LIBRARIES} ${EGL_LIBRARY} ${OPENGLES2_LIBRARY})
ELSEif(OPENGLES STREQUAL "1.0")
elseif(OPENGLES STREQUAL "1.0")
find_package(OpenGLES1 REQUIRED)
find_package(EGL REQUIRED)
set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=1)
set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${EGL_INCLUDE_DIR} ${OPENGLES1_INCLUDE_DIR})
set(framework_LIBRARIES ${framework_LIBRARIES} ${EGL_LIBRARY} ${OPENGLES1_LIBRARY})
endif()
else()
## TODO: CMake Documentation says that this is not the right
# Thing for Mac OS X, but it works for now.
@ -381,11 +387,15 @@ if(FRAMEWORK_GRAPHICS)
)
else()
set(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextglx.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextglx.h
${CMAKE_CURRENT_LIST_DIR}/platform/x11window.cpp
${CMAKE_CURRENT_LIST_DIR}/platform/x11window.h
)
if(NOT OPENGLES)
set(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextglx.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/ogl/graphicscontextglx.h
)
endif()
endif()
endif()

View File

@ -44,6 +44,7 @@
typedef char GLchar;
// define OpenGL ES 2.0 API just to make compile, it wont actually be used
inline void glBlendEquation (GLenum mode) { }
inline void glBlendFuncSeparate (GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) { }
inline void glBindFramebuffer (GLenum target, GLuint framebuffer) { }
inline void glDeleteFramebuffers (GLsizei n, const GLuint* framebuffers) { }

View File

@ -25,7 +25,7 @@
#include "declarations.h"
class GraphicsContext
class GraphicsContext : public stdext::shared_object
{
public:
GraphicsContext(const std::string& name);

View File

@ -21,6 +21,7 @@
*/
#include "graphicscontextegl.h"
#include <framework/platform/x11window.h>
GraphicsContextEGL::GraphicsContextEGL() :
GraphicsContext("EGL")
@ -31,10 +32,13 @@ GraphicsContextEGL::GraphicsContextEGL() :
m_eglSurface = 0;
}
void GraphicsContextEGL::create(WindowType window, DisplayType display)
void GraphicsContextEGL::create()
{
m_window = window;
m_display = display;
#ifdef WIN32
// TODO
#else
Display *display = g_x11Window.getDisplay();
#endif
m_eglDisplay = eglGetDisplay(display);
if(m_eglDisplay == EGL_NO_DISPLAY)
@ -58,8 +62,10 @@ void GraphicsContextEGL::create(WindowType window, DisplayType display)
EGLint numConfig;
#ifdef WIN32
if(!eglGetConfigs(m_eglDisplay, NULL, 0, &numConfig))
g_logger.fatal("No valid GL configurations");
#endif
if(!eglChooseConfig(m_eglDisplay, configList, &m_eglConfig, 1, &numConfig))
g_logger.fatal("Failed to choose EGL config");
@ -67,6 +73,22 @@ void GraphicsContextEGL::create(WindowType window, DisplayType display)
if(numConfig != 1)
g_logger.warning("Didn't got the exact EGL config");
EGLint vid;
if(!eglGetConfigAttrib(m_eglDisplay, m_eglConfig, EGL_NATIVE_VISUAL_ID, &vid))
g_logger.fatal("Unable to get visual EGL visual id");
#ifndef WIN32
XVisualInfo visTemplate;
int numVisuals;
memset(&visTemplate, 0, sizeof(visTemplate));
visTemplate.visualid = vid;
g_x11Window.setVisual(XGetVisualInfo(display, VisualIDMask, &visTemplate, &numVisuals));
if(!g_x11Window.getVisual())
g_logger.fatal("Couldn't choose RGBA, double buffered visual");
g_x11Window.setRootWindow(DefaultRootWindow(display));
#endif
EGLint contextAtrrList[] = {
#if OPENGL_ES==2
EGL_CONTEXT_CLIENT_VERSION, 2,
@ -76,9 +98,11 @@ void GraphicsContextEGL::create(WindowType window, DisplayType display)
EGL_NONE
};
#ifdef WIN32
m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig, window, NULL);
if(m_eglSurface == EGL_NO_SURFACE)
g_logger.fatal(stdext::format("Unable to create EGL surface: %s", eglGetError()));
#endif
m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, EGL_NO_CONTEXT, contextAtrrList);
if(m_eglContext == EGL_NO_CONTEXT )
@ -103,6 +127,13 @@ void GraphicsContextEGL::destroy()
void GraphicsContextEGL::restore()
{
#ifndef WIN32
Window window = g_x11Window.getWindow();
m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig, window, NULL);
if(m_eglSurface == EGL_NO_SURFACE)
g_logger.fatal(stdext::format("Unable to create EGL surface: %s", eglGetError()));
#endif
if(!eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext))
g_logger.fatal("Unable to make current EGL context");
}

View File

@ -31,7 +31,7 @@ class GraphicsContextEGL : public GraphicsContext
public:
GraphicsContextEGL();
void create(WindowType window, DisplayType display);
void create();
void destroy();
void restore();

View File

@ -22,7 +22,6 @@
#include "graphicscontextglx.h"
GraphicsContextGLX::GraphicsContextGLX() :
GraphicsContext("GLX"), m_window(dynamic_cast<X11Window&>(g_window))
{
@ -32,6 +31,9 @@ GraphicsContextGLX::GraphicsContextGLX() :
void GraphicsContextGLX::create()
{
if(!glXQueryExtension(m_window.getDisplay(), NULL, NULL))
g_logger.fatal("GLX not supported");
static int attrList[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True,

View File

@ -303,6 +303,7 @@ void PainterOGL::updateGlBlendEquation()
{
if(!g_graphics.canUseBlendEquation())
return;
if(m_blendEquation == BlendEquation_Add)
glBlendEquation(0x8006); // GL_FUNC_ADD
else if(m_blendEquation == BlendEquation_Max)

View File

@ -33,6 +33,7 @@ WIN32Window window;
#include "x11window.h"
#include <framework/core/clock.h>
X11Window window;
X11Window& g_x11Window = window;
#endif
#endif

View File

@ -97,9 +97,6 @@ public:
void setOnInputEvent(const OnInputEventCallback& onInputEvent) { m_onInputEvent = onInputEvent; }
protected:
virtual void internalCreateContext() = 0;
virtual void internalDestroyContext() = 0;
virtual void internalRestoreContext() = 0;
virtual int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) = 0;
void updateUnmaximizedCoords();

View File

@ -24,6 +24,7 @@
#include <framework/core/resourcemanager.h>
#include <framework/graphics/image.h>
#include <framework/graphics/ogl/graphicscontextglx.h>
#include <framework/graphics/ogl/graphicscontextegl.h>
#include <unistd.h>
#define LSB_BIT_SET(p, n) (p[(n)/8] |= (1 <<((n)%8)))
@ -43,9 +44,11 @@ X11Window::X11Window()
m_wmDelete = 0;
m_minimumSize = Size(600,480);
m_size = Size(600,480);
#ifndef OPENGL_ES
m_graphicsContext = GraphicsContextPtr(new GraphicsContextGLX);
dump << GraphicsContextPtr(new GraphicsContextGLX())->getName().c_str();
dump << (new GraphicsContextGLX())->getName().c_str();
#else
m_graphicsContext = GraphicsContextPtr(new GraphicsContextEGL);
#endif
m_keyMap[XK_Escape] = Fw::KeyEscape;
m_keyMap[XK_Tab] = Fw::KeyTab;
@ -203,9 +206,7 @@ X11Window::X11Window()
void X11Window::init()
{
internalOpenDisplay();
internalCheckGL();
internalChooseGLVisual();
internalCreateContext();
m_graphicsContext->create();
internalCreateWindow();
}
@ -235,7 +236,7 @@ void X11Window::terminate()
m_colormap = 0;
}
internalDestroyContext();
m_graphicsContext->destroy();
if(m_visual) {
XFree(m_visual);
@ -323,7 +324,7 @@ void X11Window::internalCreateWindow()
if(!internalSetupWindowInput())
g_logger.warning("Input of special keys may be messed up, because window input initialization failed");
internalRestoreContext();
m_graphicsContext->restore();
}
bool X11Window::internalSetupWindowInput()
@ -350,78 +351,6 @@ bool X11Window::internalSetupWindowInput()
return true;
}
void X11Window::internalCheckGL()
{
#ifdef OPENGL_ES
m_eglDisplay = eglGetDisplay((EGLNativeDisplayType)m_display);
if(m_eglDisplay == EGL_NO_DISPLAY)
g_logger.fatal("EGL not supported");
if(!eglInitialize(m_eglDisplay, NULL, NULL))
g_logger.fatal("Unable to initialize EGL");
#else
if(!glXQueryExtension(m_display, NULL, NULL))
g_logger.fatal("GLX not supported");
#endif
}
void X11Window::internalChooseGLVisual()
{
#ifdef OPENGL_ES
static int attrList[] = {
#if OPENGL_ES==2
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#else
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
#endif
EGL_RED_SIZE, 4,
EGL_GREEN_SIZE, 4,
EGL_BLUE_SIZE, 4,
EGL_ALPHA_SIZE, 4,
EGL_NONE
};
EGLint numConfig;
XVisualInfo visTemplate;
int numVisuals;
if(!eglChooseConfig(m_eglDisplay, attrList, &m_eglConfig, 1, &numConfig))
g_logger.fatal("Failed to choose EGL config");
if(numConfig != 1)
g_logger.warning("Didn't got the exact EGL config");
EGLint vid;
if(!eglGetConfigAttrib(m_eglDisplay, m_eglConfig, EGL_NATIVE_VISUAL_ID, &vid))
g_logger.fatal("Unable to get visual EGL visual id");
memset(&visTemplate, 0, sizeof(visTemplate));
visTemplate.visualid = vid;
m_visual = XGetVisualInfo(m_display, VisualIDMask, &visTemplate, &numVisuals);
if(!m_visual)
g_logger.fatal("Couldn't choose RGBA, double buffered visual");
m_rootWindow = DefaultRootWindow(m_display);
#else
#endif
}
void X11Window::internalCreateContext()
{
m_graphicsContext->create();
}
void X11Window::internalDestroyContext()
{
m_graphicsContext->destroy();
}
void X11Window::internalRestoreContext()
{
m_graphicsContext->restore();
}
void X11Window::move(const Point& pos)
{
m_position = pos;

View File

@ -38,12 +38,6 @@ class X11Window : public PlatformWindow
void internalCreateWindow();
bool internalSetupWindowInput();
void internalCheckGL();
void internalChooseGLVisual();
void internalCreateContext();
void internalDestroyContext();
void internalRestoreContext();
public:
X11Window();
@ -99,5 +93,7 @@ private:
std::string m_clipboardText;
};
extern X11Window& g_x11Window;
#endif