1 #include "cube-egl.h"
2 #include "cube.h"
3 
4 #include <kms++util/kms++util.h>
5 
6 using namespace std;
7 
print_egl_config(EGLDisplay dpy,EGLConfig cfg)8 static void print_egl_config(EGLDisplay dpy, EGLConfig cfg)
9 {
10 	auto getconf = [dpy, cfg](EGLint a) { EGLint v = -1; eglGetConfigAttrib(dpy, cfg, a, &v); return v; };
11 
12 	printf("EGL Config %d: color buf %d/%d/%d/%d = %d, depth %d, stencil %d, native visualid %d, native visualtype %d\n",
13 	       getconf(EGL_CONFIG_ID),
14 	       getconf(EGL_ALPHA_SIZE),
15 	       getconf(EGL_RED_SIZE),
16 	       getconf(EGL_GREEN_SIZE),
17 	       getconf(EGL_BLUE_SIZE),
18 	       getconf(EGL_BUFFER_SIZE),
19 	       getconf(EGL_DEPTH_SIZE),
20 	       getconf(EGL_STENCIL_SIZE),
21 	       getconf(EGL_NATIVE_VISUAL_ID),
22 	       getconf(EGL_NATIVE_VISUAL_TYPE));
23 }
24 
EglState(void * native_display)25 EglState::EglState(void* native_display)
26 {
27 	EGLBoolean b;
28 	EGLint major, minor, n;
29 
30 	static const EGLint context_attribs[] = {
31 		EGL_CONTEXT_CLIENT_VERSION, 2,
32 		EGL_NONE
33 	};
34 
35 	static const EGLint config_attribs[] = {
36 		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
37 		EGL_RED_SIZE, 8,
38 		EGL_GREEN_SIZE, 8,
39 		EGL_BLUE_SIZE, 8,
40 		EGL_ALPHA_SIZE, 0,
41 		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
42 		EGL_NONE
43 	};
44 
45 	m_display = eglGetDisplay((EGLNativeDisplayType)native_display);
46 	FAIL_IF(!m_display, "failed to get egl display");
47 
48 	b = eglInitialize(m_display, &major, &minor);
49 	FAIL_IF(!b, "failed to initialize");
50 
51 	if (s_verbose) {
52 		printf("Using display %p with EGL version %d.%d\n", m_display, major, minor);
53 
54 		printf("EGL_VENDOR:      %s\n", eglQueryString(m_display, EGL_VENDOR));
55 		printf("EGL_VERSION:     %s\n", eglQueryString(m_display, EGL_VERSION));
56 		printf("EGL_EXTENSIONS:  %s\n", eglQueryString(m_display, EGL_EXTENSIONS));
57 		printf("EGL_CLIENT_APIS: %s\n", eglQueryString(m_display, EGL_CLIENT_APIS));
58 	}
59 
60 	b = eglBindAPI(EGL_OPENGL_ES_API);
61 	FAIL_IF(!b, "failed to bind api EGL_OPENGL_ES_API");
62 
63 	if (s_verbose) {
64 		EGLint numConfigs;
65 		b = eglGetConfigs(m_display, nullptr, 0, &numConfigs);
66 		FAIL_IF(!b, "failed to get number of configs");
67 
68 		EGLConfig configs[numConfigs];
69 		b = eglGetConfigs(m_display, configs, numConfigs, &numConfigs);
70 
71 		printf("Available configs:\n");
72 
73 		for (int i = 0; i < numConfigs; ++i)
74 			print_egl_config(m_display, configs[i]);
75 	}
76 
77 	b = eglChooseConfig(m_display, config_attribs, &m_config, 1, &n);
78 	FAIL_IF(!b || n != 1, "failed to choose config");
79 
80 	if (s_verbose) {
81 		printf("Chosen config:\n");
82 		print_egl_config(m_display, m_config);
83 	}
84 
85 	m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, context_attribs);
86 	FAIL_IF(!m_context, "failed to create context");
87 
88 	EGLBoolean ok = eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context);
89 	FAIL_IF(!ok, "eglMakeCurrent() failed");
90 }
91 
~EglState()92 EglState::~EglState()
93 {
94 	eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
95 	eglTerminate(m_display);
96 }
97 
EglSurface(const EglState & egl,void * native_window)98 EglSurface::EglSurface(const EglState& egl, void* native_window)
99 	: egl(egl)
100 {
101 	esurface = eglCreateWindowSurface(egl.display(), egl.config(), (EGLNativeWindowType)native_window, NULL);
102 	FAIL_IF(esurface == EGL_NO_SURFACE, "failed to create egl surface");
103 }
104 
~EglSurface()105 EglSurface::~EglSurface()
106 {
107 	eglDestroySurface(egl.display(), esurface);
108 }
109 
make_current()110 void EglSurface::make_current()
111 {
112 	EGLBoolean ok = eglMakeCurrent(egl.display(), esurface, esurface, egl.context());
113 	FAIL_IF(!ok, "eglMakeCurrent() failed");
114 }
115 
swap_buffers()116 void EglSurface::swap_buffers()
117 {
118 	EGLBoolean ok = eglSwapBuffers(egl.display(), esurface);
119 	FAIL_IF(!ok, "eglMakeCurrent() failed");
120 }
121