• 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) : EglState(native_display, 0) {}
26 
EglState(void * native_display,EGLint native_visual_id)27 EglState::EglState(void* native_display, EGLint native_visual_id)
28 	: m_native_visual_id(native_visual_id)
29 {
30 	EGLBoolean b;
31 	EGLint major, minor, n;
32 
33 	static const EGLint context_attribs[] = {
34 		EGL_CONTEXT_CLIENT_VERSION, 2,
35 		EGL_NONE
36 	};
37 
38 	static const EGLint config_attribs[] = {
39 		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
40 		EGL_RED_SIZE, 8,
41 		EGL_GREEN_SIZE, 8,
42 		EGL_BLUE_SIZE, 8,
43 		EGL_ALPHA_SIZE, 0,
44 		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
45 		EGL_NONE
46 	};
47 
48 	m_display = eglGetDisplay((EGLNativeDisplayType)native_display);
49 	FAIL_IF(!m_display, "failed to get egl display");
50 
51 	b = eglInitialize(m_display, &major, &minor);
52 	FAIL_IF(!b, "failed to initialize");
53 
54 	if (s_verbose) {
55 		printf("Using display %p with EGL version %d.%d\n", m_display, major, minor);
56 
57 		printf("EGL_VENDOR:      %s\n", eglQueryString(m_display, EGL_VENDOR));
58 		printf("EGL_VERSION:     %s\n", eglQueryString(m_display, EGL_VERSION));
59 		printf("EGL_EXTENSIONS:  %s\n", eglQueryString(m_display, EGL_EXTENSIONS));
60 		printf("EGL_CLIENT_APIS: %s\n", eglQueryString(m_display, EGL_CLIENT_APIS));
61 	}
62 
63 	b = eglBindAPI(EGL_OPENGL_ES_API);
64 	FAIL_IF(!b, "failed to bind api EGL_OPENGL_ES_API");
65 
66 	EGLint numConfigs;
67 	b = eglGetConfigs(m_display, nullptr, 0, &numConfigs);
68 	FAIL_IF(!b, "failed to get number of configs");
69 
70 	if (s_verbose) {
71 		EGLConfig configs[numConfigs];
72 		b = eglGetConfigs(m_display, configs, numConfigs, &numConfigs);
73 		FAIL_IF(!b, "failed to get configs");
74 
75 		printf("Available configs:\n");
76 
77 		for (int i = 0; i < numConfigs; ++i)
78 			print_egl_config(m_display, configs[i]);
79 	}
80 
81 	std::vector<EGLConfig> configs(numConfigs);
82 	b = eglChooseConfig(m_display, config_attribs, configs.data(), numConfigs, &n);
83 	FAIL_IF(!b || n < 1, "failed to choose config");
84 
85 	// elgChooseConfig does implement matching by EGL_NATIVE_VISUAL_ID, do a manual
86 	// loop. Picks the first returned if native_visual_id is not set.
87 	for (const auto& config : configs) {
88 		EGLint id;
89 		b = eglGetConfigAttrib(m_display, config, EGL_NATIVE_VISUAL_ID, &id);
90 		if (!b) {
91 			printf("failed to get native visual id\n");
92 			continue;
93 		}
94 
95 		if (id == native_visual_id || !native_visual_id) {
96 			m_config = config;
97 			break;
98 		}
99 	}
100 
101 	if (s_verbose) {
102 		printf("Chosen config:\n");
103 		print_egl_config(m_display, m_config);
104 	}
105 
106 	m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, context_attribs);
107 	FAIL_IF(!m_context, "failed to create context");
108 
109 	EGLBoolean ok = eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context);
110 	FAIL_IF(!ok, "eglMakeCurrent() failed");
111 }
112 
~EglState()113 EglState::~EglState()
114 {
115 	eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
116 	eglTerminate(m_display);
117 }
118 
EglSurface(const EglState & egl,void * native_window)119 EglSurface::EglSurface(const EglState& egl, void* native_window)
120 	: egl(egl)
121 {
122 	esurface = eglCreateWindowSurface(egl.display(), egl.config(), (EGLNativeWindowType)native_window, NULL);
123 	FAIL_IF(esurface == EGL_NO_SURFACE, "failed to create egl surface");
124 }
125 
~EglSurface()126 EglSurface::~EglSurface()
127 {
128 	eglDestroySurface(egl.display(), esurface);
129 }
130 
make_current()131 void EglSurface::make_current()
132 {
133 	EGLBoolean ok = eglMakeCurrent(egl.display(), esurface, esurface, egl.context());
134 	FAIL_IF(!ok, "eglMakeCurrent() failed");
135 }
136 
swap_buffers()137 void EglSurface::swap_buffers()
138 {
139 	EGLBoolean ok = eglSwapBuffers(egl.display(), esurface);
140 	FAIL_IF(!ok, "eglMakeCurrent() failed");
141 }
142