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
Create()13 GLInterface* GLInterface::Create() {
14 return new EGLInterface;
15 }
16
Init()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
Cleanup()38 void EGLInterface::Cleanup() {
39 eglMakeCurrent(display_, NULL, NULL, NULL);
40 DeleteContext(context_);
41 eglDestroySurface(display_, surface_);
42 }
43
GetXVisual()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 EGLNativeDisplayType native_display =
57 static_cast<EGLNativeDisplayType>(g_xlib_display);
58
59 display_ = eglGetDisplay(native_display);
60 CheckError();
61
62 eglInitialize(display_, NULL, NULL);
63 CheckError();
64
65 EGLint num_configs = -1;
66 eglGetConfigs(display_, NULL, 0, &num_configs);
67 CheckError();
68
69 eglChooseConfig(display_, attribs, &config_, 1, &num_configs);
70 CheckError();
71 }
72
73 // TODO: for some reason on some systems EGL_NATIVE_VISUAL_ID returns an ID
74 // that XVisualIDFromVisual cannot find. Use default visual until this is
75 // resolved.
76 #if 0
77 EGLint visual_id;
78 eglGetConfigAttrib(display_, config_, EGL_NATIVE_VISUAL_ID, &visual_id);
79 CheckError();
80 XVisualInfo vinfo_template;
81 vinfo_template.visualid = static_cast<VisualID>(visual_id);
82 #else
83 XVisualInfo vinfo_template;
84 vinfo_template.visualid = XVisualIDFromVisual(
85 DefaultVisual(g_xlib_display, DefaultScreen(g_xlib_display)));
86 #endif
87
88 int nitems = 0;
89 XVisualInfo* ret =
90 XGetVisualInfo(g_xlib_display, VisualIDMask, &vinfo_template, &nitems);
91 CHECK(nitems == 1);
92 return ret;
93 }
94
SwapBuffers()95 void EGLInterface::SwapBuffers() {
96 eglSwapBuffers(display_, surface_);
97 }
98
SwapInterval(int interval)99 bool EGLInterface::SwapInterval(int interval) {
100 return (eglSwapInterval(display_, interval) == EGL_TRUE);
101 }
102
MakeCurrent(const GLContext & context)103 bool EGLInterface::MakeCurrent(const GLContext& context) {
104 return eglMakeCurrent(display_, surface_, surface_, context);
105 }
106
CreateContext()107 const GLContext EGLInterface::CreateContext() {
108 EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
109 CHECK(display_ != EGL_NO_DISPLAY);
110 CHECK(config_);
111 return eglCreateContext(display_, config_, NULL, attribs);
112 }
113
CheckError()114 void EGLInterface::CheckError() {
115 CHECK_EQ(eglGetError(), EGL_SUCCESS);
116 }
117
DeleteContext(const GLContext & context)118 void EGLInterface::DeleteContext(const GLContext& context) {
119 eglDestroyContext(display_, context);
120 }
121
TerminateGL()122 void EGLInterface::TerminateGL() {
123 eglDestroySurface(display_, surface_);
124 eglTerminate(display_);
125 }
126