20 TypeHandle eglGraphicsStateGuardian::_type_handle;
25 eglGraphicsStateGuardian::
29 GLES2GraphicsStateGuardian(engine, pipe)
31 GLESGraphicsStateGuardian(engine, pipe)
43 if (share_with !=
nullptr) {
44 _prepared_objects = share_with->get_prepared_objects();
45 _share_context = share_with->_context;
52 eglGraphicsStateGuardian::
53 ~eglGraphicsStateGuardian() {
54 if (_visuals !=
nullptr) {
57 if (_context != (EGLContext)
nullptr) {
58 if (!eglDestroyContext(_egl_display, _context)) {
59 egldisplay_cat.error() <<
"Failed to destroy EGL context: "
62 _context = (EGLContext)
nullptr;
71 bool &pbuffer_supported,
bool &pixmap_supported,
72 bool &slow, EGLConfig config) {
77 EGLint red_size, green_size, blue_size,
79 depth_size, stencil_size, samples, surface_type, caveat;
81 eglGetConfigAttrib(_egl_display, config, EGL_RED_SIZE, &red_size);
82 eglGetConfigAttrib(_egl_display, config, EGL_GREEN_SIZE, &green_size);
83 eglGetConfigAttrib(_egl_display, config, EGL_BLUE_SIZE, &blue_size);
84 eglGetConfigAttrib(_egl_display, config, EGL_ALPHA_SIZE, &alpha_size);
85 eglGetConfigAttrib(_egl_display, config, EGL_DEPTH_SIZE, &depth_size);
86 eglGetConfigAttrib(_egl_display, config, EGL_STENCIL_SIZE, &stencil_size);
87 eglGetConfigAttrib(_egl_display, config, EGL_SAMPLES, &samples);
88 eglGetConfigAttrib(_egl_display, config, EGL_SURFACE_TYPE, &surface_type);
89 eglGetConfigAttrib(_egl_display, config, EGL_CONFIG_CAVEAT, &caveat);
90 int err = eglGetError();
91 if (err != EGL_SUCCESS) {
92 egldisplay_cat.error() <<
"Failed to get EGL config attrib: "
96 pbuffer_supported =
false;
97 if ((surface_type & EGL_PBUFFER_BIT)!=0) {
98 pbuffer_supported =
true;
101 pixmap_supported =
false;
102 if ((surface_type & EGL_PIXMAP_BIT)!=0) {
103 pixmap_supported =
true;
107 if (caveat == EGL_SLOW_CONFIG) {
111 if ((surface_type & EGL_WINDOW_BIT)==0) {
116 properties.set_back_buffers(1);
117 properties.set_rgb_color(1);
118 properties.
set_rgba_bits(red_size, green_size, blue_size, alpha_size);
119 properties.set_stencil_bits(stencil_size);
120 properties.set_depth_bits(depth_size);
121 properties.set_multisamples(samples);
124 properties.set_force_software(slow);
125 properties.set_force_hardware(!slow);
134 X11_Display *display,
135 int screen,
bool need_pbuffer,
bool need_pixmap) {
138 _egl_display = eglGetDisplay((NativeDisplayType) display);
146 int attrib_list[] = {
148 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
151 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
153 EGL_SURFACE_TYPE, EGL_DONT_CARE,
159 int num_configs = 0, returned_configs;
160 if (!eglChooseConfig(_egl_display, attrib_list,
nullptr, num_configs, &returned_configs) || returned_configs <= 0) {
161 egldisplay_cat.error() <<
"eglChooseConfig failed: "
166 num_configs = returned_configs;
167 EGLConfig *configs =
new EGLConfig[num_configs];
169 if (!eglChooseConfig(_egl_display, attrib_list, configs, num_configs, &returned_configs) || returned_configs <= 0) {
170 egldisplay_cat.error() <<
"eglChooseConfig failed: "
176 int best_quality = 0;
180 for (
int i = 0; i < num_configs; ++i) {
182 bool pbuffer_supported, pixmap_supported, slow;
188 const char *pbuffertext = pbuffer_supported ?
" (pbuffer)" :
"";
189 const char *pixmaptext = pixmap_supported ?
" (pixmap)" :
"";
190 const char *slowtext = slow ?
" (slow)" :
"";
191 egldisplay_cat.debug()
192 << i <<
": " << fbprops << pbuffertext << pixmaptext << slowtext <<
"\n";
194 if ((quality > 0)&&(slow)) quality -= 10000000;
196 if (need_pbuffer && !pbuffer_supported) {
199 if (need_pixmap && !pixmap_supported) {
203 if (quality > best_quality) {
204 best_quality = quality;
206 best_props = fbprops;
209 int depth = DefaultDepth(_display, _screen);
210 _visual =
new XVisualInfo;
211 XMatchVisualInfo(_display, _screen, depth, TrueColor, _visual);
213 if (best_quality > 0) {
214 egldisplay_cat.debug()
215 <<
"Chosen config " << best_result <<
": " << best_props <<
"\n";
216 _fbconfig = configs[best_result];
218 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
219 _context = eglCreateContext(_egl_display, _fbconfig, _share_context, context_attribs);
221 _context = eglCreateContext(_egl_display, _fbconfig, _share_context,
nullptr);
223 int err = eglGetError();
224 if (_context && err == EGL_SUCCESS) {
228 if (properties.get_srgb_color()) {
229 const char *extensions = eglQueryString(_egl_display, EGL_EXTENSIONS);
230 if (extensions !=
nullptr) {
231 vector_string tokens;
234 if (std::find(tokens.begin(), tokens.end(),
"EGL_KHR_gl_colorspace") != tokens.end()) {
235 best_props.set_srgb_color(
true);
240 _fbprops = best_props;
246 egldisplay_cat.error()
247 <<
"Could not create EGL context!\n"
255 egldisplay_cat.error() <<
256 "Could not find a usable pixel format.\n";
267 GLES2GraphicsStateGuardian::reset();
269 GLESGraphicsStateGuardian::reset();
272 if (_gl_renderer ==
"Software Rasterizer") {
273 _fbprops.set_force_software(1);
274 _fbprops.set_force_hardware(0);
284 if (_egl_version_major < major_version) {
287 if (_egl_version_minor < minor_version) {
296 void eglGraphicsStateGuardian::
301 GLES2GraphicsStateGuardian::gl_flush();
303 GLESGraphicsStateGuardian::gl_flush();
310 GLenum eglGraphicsStateGuardian::
311 gl_get_error()
const {
315 return GLES2GraphicsStateGuardian::gl_get_error();
317 return GLESGraphicsStateGuardian::gl_get_error();
324 void eglGraphicsStateGuardian::
327 GLES2GraphicsStateGuardian::query_gl_version();
329 GLESGraphicsStateGuardian::query_gl_version();
334 if (!eglInitialize(_egl_display, &_egl_version_major, &_egl_version_minor)) {
335 egldisplay_cat.error() <<
"Failed to get EGL version number: "
343 if (gles2gsg_cat.is_debug()) {
346 if (glesgsg_cat.is_debug()) {
349 <<
"EGL_VERSION = " << _egl_version_major <<
"." << _egl_version_minor
359 void eglGraphicsStateGuardian::
360 get_extra_extensions() {
361 save_extensions(eglQueryString(_egl_display, EGL_EXTENSIONS));
370 void *eglGraphicsStateGuardian::
371 do_get_extension_func(
const char *name) {
372 return (
void *)eglGetProcAddress(name);