aboutsummaryrefslogtreecommitdiff
path: root/src/canvas-x11-egl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/canvas-x11-egl.cpp')
-rw-r--r--src/canvas-x11-egl.cpp264
1 files changed, 21 insertions, 243 deletions
diff --git a/src/canvas-x11-egl.cpp b/src/canvas-x11-egl.cpp
index 3f23fc8..0844b74 100644
--- a/src/canvas-x11-egl.cpp
+++ b/src/canvas-x11-egl.cpp
@@ -31,6 +31,14 @@
* Protected methods *
*********************/
+bool
+CanvasX11EGL::init_gl_winsys()
+{
+ egl_.init_display(reinterpret_cast<EGLNativeDisplayType>(xdpy_),
+ visual_config_);
+ return true;
+}
+
XVisualInfo *
CanvasX11EGL::get_xvisualinfo()
{
@@ -39,14 +47,9 @@ CanvasX11EGL::get_xvisualinfo()
int num_visuals;
EGLint vid;
- if (!ensure_egl_config())
- return 0;
-
- if (!eglGetConfigAttrib(egl_display_, egl_config_,
- EGL_NATIVE_VISUAL_ID, &vid))
+ if (!egl_.gotNativeConfig(vid))
{
- Log::error("eglGetConfigAttrib() failed with error: %d\n",
- eglGetError());
+ Log::error("Failed to get a native-renderable EGLConfig\n");
return 0;
}
@@ -65,35 +68,27 @@ CanvasX11EGL::get_xvisualinfo()
bool
CanvasX11EGL::make_current()
{
- if (!ensure_egl_surface())
- return false;
-
- if (!ensure_egl_context())
- return false;
-
- if (egl_context_ == eglGetCurrentContext())
- return true;
-
- if (!eglMakeCurrent(egl_display_, egl_surface_, egl_surface_, egl_context_)) {
- Log::error("Error: eglMakeCurrent failed with error %d\n", eglGetError());
+ egl_.init_surface(reinterpret_cast<EGLNativeWindowType>(xwin_));
+ if (!egl_.valid()) {
+ Log::error("CanvasX11EGL: Invalid EGL state\n");
return false;
}
- if (!eglSwapInterval(egl_display_, 0))
- Log::info("** Failed to set swap interval. Results may be bounded above by refresh rate.\n");
-
init_gl_extensions();
return true;
}
void
-CanvasX11EGL::get_glvisualconfig(GLVisualConfig &visual_config)
+CanvasX11EGL::swap_buffers()
{
- if (!ensure_egl_config())
- return;
+ egl_.swap();
+}
- get_glvisualconfig_egl(egl_config_, visual_config);
+void
+CanvasX11EGL::get_glvisualconfig(GLVisualConfig &visual_config)
+{
+ egl_.getVisualConfig(visual_config);
}
/*******************
@@ -101,184 +96,9 @@ CanvasX11EGL::get_glvisualconfig(GLVisualConfig &visual_config)
*******************/
bool
-CanvasX11EGL::ensure_egl_display()
-{
- if (egl_display_)
- return true;
-
- egl_display_ = eglGetDisplay((EGLNativeDisplayType) xdpy_);
- if (!egl_display_) {
- Log::error("eglGetDisplay() failed with error: %d\n",
- eglGetError());
- return false;
- }
- if (!eglInitialize(egl_display_, NULL, NULL)) {
- Log::error("eglInitialize() failed with error: %d\n",
- eglGetError());
- return false;
- egl_display_ = 0;
- }
-
- return true;
-}
-
-bool
-CanvasX11EGL::ensure_egl_config()
-{
- const EGLint attribs[] = {
- EGL_RED_SIZE, visual_config_.red,
- EGL_GREEN_SIZE, visual_config_.green,
- EGL_BLUE_SIZE, visual_config_.blue,
- EGL_ALPHA_SIZE, visual_config_.alpha,
- EGL_DEPTH_SIZE, visual_config_.depth,
- EGL_BUFFER_SIZE, visual_config_.buffer,
-#ifdef USE_GLESv2
- EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
-#elif USE_GL
- EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
-#endif
- EGL_NONE
- };
- EGLint num_configs;
- EGLint vid;
-
- if (egl_config_)
- return true;
-
- if (!ensure_egl_display())
- return false;
-
- /* Find out how many configs match the attributes */
- if (!eglChooseConfig(egl_display_, attribs, 0, 0, &num_configs)) {
- Log::error("eglChooseConfig() (explore) failed with error: %d\n",
- eglGetError());
- return false;
- }
-
- if (num_configs == 0) {
- Log::error("eglChooseConfig() didn't return any configs\n");
- return false;
- }
-
- /* Get all the matching configs */
- std::vector<EGLConfig> configs(num_configs);
-
- if (!eglChooseConfig(egl_display_, attribs, &(configs[0]),
- num_configs, &num_configs))
- {
- Log::error("eglChooseConfig() failed with error: %d\n",
- eglGetError());
- return false;
- }
-
- /* Select the best matching config */
- egl_config_ = select_best_config(configs);
-
- if (!eglGetConfigAttrib(egl_display_, egl_config_,
- EGL_NATIVE_VISUAL_ID, &vid))
- {
- Log::error("eglGetConfigAttrib() failed with error: %d\n",
- eglGetError());
- return false;
- }
-
- if (Options::show_debug) {
- int buf, red, green, blue, alpha, depth, id, native_id;
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_CONFIG_ID, &id);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_NATIVE_VISUAL_ID, &native_id);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_BUFFER_SIZE, &buf);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_RED_SIZE, &red);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_GREEN_SIZE, &green);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_BLUE_SIZE, &blue);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_ALPHA_SIZE, &alpha);
- eglGetConfigAttrib(egl_display_, egl_config_, EGL_DEPTH_SIZE, &depth);
- Log::debug("EGL chosen config ID: 0x%x Native Visual ID: 0x%x\n"
- " Buffer: %d bits\n"
- " Red: %d bits\n"
- " Green: %d bits\n"
- " Blue: %d bits\n"
- " Alpha: %d bits\n"
- " Depth: %d bits\n",
- id, native_id,
- buf, red, green, blue, alpha, depth);
- }
-
- return true;
-}
-
-bool
CanvasX11EGL::reset_context()
{
- if (!ensure_egl_display())
- return false;
-
- if (!egl_context_)
- return true;
-
- if (eglDestroyContext(egl_display_, egl_context_) == EGL_FALSE) {
- Log::debug("eglDestroyContext() failed with error: 0x%x\n",
- eglGetError());
- }
-
- egl_context_ = 0;
- return true;
-}
-
-bool
-CanvasX11EGL::ensure_egl_context()
-{
- if (egl_context_)
- return true;
-
- if (!ensure_egl_display())
- return false;
-
- if (!ensure_egl_config())
- return false;
-
- static const EGLint ctx_attribs[] = {
-#ifdef USE_GLESv2
- EGL_CONTEXT_CLIENT_VERSION, 2,
-#endif
- EGL_NONE
- };
-
- egl_context_ = eglCreateContext(egl_display_, egl_config_,
- EGL_NO_CONTEXT, ctx_attribs);
- if (!egl_context_) {
- Log::error("eglCreateContext() failed with error: 0x%x\n",
- eglGetError());
- return false;
- }
-
- return true;
-}
-
-bool
-CanvasX11EGL::ensure_egl_surface()
-{
- if (egl_surface_)
- return true;
-
- if (!ensure_egl_display())
- return false;
-
-#ifdef USE_GLESv2
- eglBindAPI(EGL_OPENGL_ES_API);
-#elif USE_GL
- eglBindAPI(EGL_OPENGL_API);
-#endif
-
- egl_surface_ = eglCreateWindowSurface(egl_display_, egl_config_,
- (EGLNativeWindowType) xwin_,
- NULL);
- if (!egl_surface_) {
- Log::error("eglCreateWindowSurface failed with error: %d\n",
- eglGetError());
- return false;
- }
-
- return true;
+ return egl_.reset();
}
void
@@ -296,45 +116,3 @@ CanvasX11EGL::init_gl_extensions()
GLExtensions::UnmapBuffer = glUnmapBuffer;
#endif
}
-
-void
-CanvasX11EGL::get_glvisualconfig_egl(EGLConfig config, GLVisualConfig &visual_config)
-{
- eglGetConfigAttrib(egl_display_, config, EGL_BUFFER_SIZE, &visual_config.buffer);
- eglGetConfigAttrib(egl_display_, config, EGL_RED_SIZE, &visual_config.red);
- eglGetConfigAttrib(egl_display_, config, EGL_GREEN_SIZE, &visual_config.green);
- eglGetConfigAttrib(egl_display_, config, EGL_BLUE_SIZE, &visual_config.blue);
- eglGetConfigAttrib(egl_display_, config, EGL_ALPHA_SIZE, &visual_config.alpha);
- eglGetConfigAttrib(egl_display_, config, EGL_DEPTH_SIZE, &visual_config.depth);
-}
-
-EGLConfig
-CanvasX11EGL::select_best_config(std::vector<EGLConfig> configs)
-{
- int best_score(INT_MIN);
- EGLConfig best_config(0);
-
- /*
- * Go through all the configs and choose the one with the best score,
- * i.e., the one better matching the requested config.
- */
- for (std::vector<EGLConfig>::const_iterator iter = configs.begin();
- iter != configs.end();
- iter++)
- {
- const EGLConfig config(*iter);
- GLVisualConfig vc;
- int score;
-
- get_glvisualconfig_egl(config, vc);
-
- score = vc.match_score(visual_config_);
-
- if (score > best_score) {
- best_score = score;
- best_config = config;
- }
- }
-
- return best_config;
-}