boo/lib/x11/GraphicsContextXCB.cpp

154 lines
4.2 KiB
C++
Raw Normal View History

2015-08-18 22:43:30 +00:00
#include "boo/IGraphicsContext.hpp"
#include "boo/IWindow.hpp"
2015-05-06 00:50:57 +00:00
2015-05-10 07:02:18 +00:00
#include <xcb/xcb.h>
#include <xcb/glx.h>
#include <GL/glx.h>
#include <GL/glcorearb.h>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
2015-05-10 07:02:18 +00:00
2015-05-06 00:50:57 +00:00
namespace boo
{
2015-08-18 19:40:26 +00:00
struct GraphicsContextXCB : IGraphicsContext
2015-05-06 00:50:57 +00:00
{
EGraphicsAPI m_api;
EPixelFormat m_pf;
IWindow* m_parentWindow;
2015-05-10 07:02:18 +00:00
xcb_connection_t* m_xcbConn;
xcb_glx_fbconfig_t m_fbconfig = 0;
xcb_visualid_t m_visualid = 0;
xcb_glx_window_t m_glxWindow = 0;
xcb_glx_context_t m_glxCtx = 0;
xcb_glx_context_tag_t m_glxCtxTag = 0;
std::thread* m_commandThread = NULL;
2015-05-06 00:50:57 +00:00
public:
IWindowCallback* m_callback;
2015-08-18 19:40:26 +00:00
GraphicsContextXCB(EGraphicsAPI api, IWindow* parentWindow, xcb_connection_t* conn, uint32_t& visualIdOut)
2015-05-06 00:50:57 +00:00
: m_api(api),
m_pf(PF_RGBA8),
2015-05-10 07:02:18 +00:00
m_parentWindow(parentWindow),
m_xcbConn(conn)
{
2015-05-10 07:02:18 +00:00
/* WTF freedesktop?? Fix this awful API and your nonexistant docs */
xcb_glx_get_fb_configs_reply_t* fbconfigs =
xcb_glx_get_fb_configs_reply(m_xcbConn, xcb_glx_get_fb_configs(m_xcbConn, 0), NULL);
struct conf_prop
{
uint32_t key;
uint32_t val;
}* props = (struct conf_prop*)xcb_glx_get_fb_configs_property_list(fbconfigs);
for (uint32_t i=0 ; i<fbconfigs->num_FB_configs ; ++i)
{
struct conf_prop* configProps = &props[fbconfigs->num_properties * i];
uint32_t fbconfId, visualId, depthSize, colorSize, doubleBuffer;
for (uint32_t j=0 ; j<fbconfigs->num_properties ; ++j)
{
struct conf_prop* prop = &configProps[j];
if (prop->key == GLX_FBCONFIG_ID)
fbconfId = prop->val;
if (prop->key == GLX_VISUAL_ID)
visualId = prop->val;
else if (prop->key == GLX_DEPTH_SIZE)
depthSize = prop->val;
else if (prop->key == GLX_BUFFER_SIZE)
colorSize = prop->val;
else if (prop->key == GLX_DOUBLEBUFFER)
doubleBuffer = prop->val;
}
/* Double-buffer only */
if (!doubleBuffer)
continue;
if (m_pf == PF_RGBA8 && colorSize >= 32)
{
m_fbconfig = fbconfId;
m_visualid = visualId;
break;
}
else if (m_pf == PF_RGBA8_Z24 && colorSize >= 32 && depthSize >= 24)
{
m_fbconfig = fbconfId;
m_visualid = visualId;
break;
}
else if (m_pf == PF_RGBAF32 && colorSize >= 128)
{
m_fbconfig = fbconfId;
m_visualid = visualId;
break;
}
else if (m_pf == PF_RGBAF32_Z24 && colorSize >= 128 && depthSize >= 24)
{
m_fbconfig = fbconfId;
m_visualid = visualId;
break;
}
}
free(fbconfigs);
if (!m_fbconfig)
{
2015-08-18 19:40:26 +00:00
fprintf(stderr, "unable to find suitable pixel format");
2015-05-10 07:02:18 +00:00
return;
}
visualIdOut = m_visualid;
}
2015-05-06 00:50:57 +00:00
2015-08-18 19:40:26 +00:00
~GraphicsContextXCB()
2015-05-06 00:50:57 +00:00
{
}
void _setCallback(IWindowCallback* cb)
{
m_callback = cb;
}
EGraphicsAPI getAPI() const
{
return m_api;
}
EPixelFormat getPixelFormat() const
{
return m_pf;
}
void setPixelFormat(EPixelFormat pf)
{
if (pf > PF_RGBAF32_Z24)
return;
m_pf = pf;
}
void initializeContext()
{
2015-05-10 07:02:18 +00:00
m_glxWindow = xcb_generate_id(m_xcbConn);
xcb_glx_create_window(m_xcbConn, 0, m_fbconfig,
m_parentWindow->getPlatformHandle(),
m_glxWindow, 0, NULL);
2015-05-06 00:50:57 +00:00
}
};
2015-08-18 19:40:26 +00:00
IGraphicsContext* _GraphicsContextXCBNew(IGraphicsContext::EGraphicsAPI api,
IWindow* parentWindow, xcb_connection_t* conn,
uint32_t& visualIdOut)
2015-05-06 00:50:57 +00:00
{
2015-08-18 19:40:26 +00:00
return new GraphicsContextXCB(api, parentWindow, conn, visualIdOut);
2015-05-06 00:50:57 +00:00
}
}