1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <memory> 6 7 #include "egl_stuff.h" 8 #include "main.h" 9 #include "xlib_window.h" 10 11 std::unique_ptr<GLInterface> g_main_gl_interface; 12 13 GLInterface* GLInterface::Create() { 14 return new EGLInterface; 15 } 16 17 bool EGLInterface::Init() { 18 if (!XlibInit()) 19 return false; 20 21 EGLNativeWindowType native_window = 22 static_cast<EGLNativeWindowType>(g_xlib_window); 23 surface_ = eglCreateWindowSurface(display_, config_, native_window, NULL); 24 CheckError(); 25 26 context_ = CreateContext(); 27 CheckError(); 28 29 eglMakeCurrent(display_, surface_, surface_, context_); 30 CheckError(); 31 32 eglQuerySurface(display_, surface_, EGL_WIDTH, &g_width); 33 eglQuerySurface(display_, surface_, EGL_HEIGHT, &g_height); 34 35 return true; 36 } 37 38 void EGLInterface::Cleanup() { 39 eglMakeCurrent(display_, NULL, NULL, NULL); 40 DeleteContext(context_); 41 eglDestroySurface(display_, surface_); 42 } 43 44 XVisualInfo* EGLInterface::GetXVisual() { 45 if (!config_) { 46 EGLint attribs[] = { 47 EGL_RED_SIZE, 1, 48 EGL_GREEN_SIZE, 1, 49 EGL_BLUE_SIZE, 1, 50 EGL_DEPTH_SIZE, 1, 51 EGL_STENCIL_SIZE, 1, 52 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 53 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 54 EGL_NONE 55 }; 56 57 EGLNativeDisplayType native_display = 58 static_cast<EGLNativeDisplayType>(g_xlib_display); 59 60 display_ = eglGetDisplay(native_display); 61 CheckError(); 62 63 eglInitialize(display_, NULL, NULL); 64 CheckError(); 65 66 EGLint num_configs = -1; 67 eglGetConfigs(display_, NULL, 0, &num_configs); 68 CheckError(); 69 70 eglChooseConfig(display_, attribs, &config_, 1, &num_configs); 71 CheckError(); 72 } 73 74 // TODO: for some reason on some systems EGL_NATIVE_VISUAL_ID returns an ID 75 // that XVisualIDFromVisual cannot find. Use default visual until this is 76 // resolved. 77 #if 0 78 EGLint visual_id; 79 eglGetConfigAttrib(display_, config_, EGL_NATIVE_VISUAL_ID, &visual_id); 80 CheckError(); 81 XVisualInfo vinfo_template; 82 vinfo_template.visualid = static_cast<VisualID>(visual_id); 83 #else 84 XVisualInfo vinfo_template; 85 vinfo_template.visualid = XVisualIDFromVisual(DefaultVisual( 86 g_xlib_display, DefaultScreen(g_xlib_display))); 87 #endif 88 89 int nitems = 0; 90 XVisualInfo* ret = XGetVisualInfo(g_xlib_display, VisualIDMask, 91 &vinfo_template, &nitems); 92 CHECK(nitems == 1); 93 return ret; 94 } 95 96 void EGLInterface::SwapBuffers() { 97 eglSwapBuffers(display_, surface_); 98 } 99 100 bool EGLInterface::SwapInterval(int interval) { 101 return (eglSwapInterval(display_, interval) == EGL_TRUE); 102 } 103 104 bool EGLInterface::MakeCurrent(const GLContext& context) { 105 return eglMakeCurrent(display_, surface_, surface_, context); 106 } 107 108 const GLContext EGLInterface::CreateContext() { 109 EGLint attribs[] = { 110 EGL_CONTEXT_CLIENT_VERSION, 2, 111 EGL_NONE 112 }; 113 CHECK(display_ != EGL_NO_DISPLAY); 114 CHECK(config_); 115 return eglCreateContext(display_, config_, NULL, attribs); 116 } 117 118 void EGLInterface::CheckError() { 119 CHECK_EQ(eglGetError(), EGL_SUCCESS); 120 } 121 122 void EGLInterface::DeleteContext(const GLContext& context) { 123 eglDestroyContext(display_, context); 124 } 125 126 void EGLInterface::TerminateGL() { 127 eglDestroySurface(display_, surface_); 128 eglTerminate(display_); 129 } 130