• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
64 	if (s_verbose) {
65 		EGLint numConfigs;
66 		b = eglGetConfigs(m_display, nullptr, 0, &numConfigs);
67 		FAIL_IF(!b, "failed to get number of configs");
68 
69 		EGLConfig configs[numConfigs];
70 		b = eglGetConfigs(m_display, configs, numConfigs, &numConfigs);
71 
72 		printf("Available configs:\n");
73 
74 		for (int i = 0; i < numConfigs; ++i)
75 			print_egl_config(m_display, configs[i]);
76 	}
77 
78 	b = eglChooseConfig(m_display, config_attribs, &m_config, 1, &n);
79 	FAIL_IF(!b || n != 1, "failed to choose config");
80 
81 	if (s_verbose) {
82 		printf("Chosen config:\n");
83 		print_egl_config(m_display, m_config);
84 	}
85 
86 	m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, context_attribs);
87 	FAIL_IF(!m_context, "failed to create context");
88 
89 	EGLBoolean ok = eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context);
90 	FAIL_IF(!ok, "eglMakeCurrent() failed");
91 }
92 
~EglState()93 EglState::~EglState()
94 {
95 	eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
96 	eglTerminate(m_display);
97 }
98 
EglSurface(const EglState & egl,void * native_window)99 EglSurface::EglSurface(const EglState &egl, void *native_window)
100 	: egl(egl)
101 {
102 	esurface = eglCreateWindowSurface(egl.display(), egl.config(), (EGLNativeWindowType)native_window, NULL);
103 	FAIL_IF(esurface == EGL_NO_SURFACE, "failed to create egl surface");
104 }
105 
~EglSurface()106 EglSurface::~EglSurface()
107 {
108 	eglDestroySurface(egl.display(), esurface);
109 }
110 
make_current()111 void EglSurface::make_current()
112 {
113 	EGLBoolean ok = eglMakeCurrent(egl.display(), esurface, esurface, egl.context());
114 	FAIL_IF(!ok, "eglMakeCurrent() failed");
115 }
116 
swap_buffers()117 void EglSurface::swap_buffers()
118 {
119 	EGLBoolean ok = eglSwapBuffers(egl.display(), esurface);
120 	FAIL_IF(!ok, "eglMakeCurrent() failed");
121 }
122