15 #include "androidGraphicsStateGuardian.h"
16 #include "config_androiddisplay.h"
17 #include "lightReMutexHolder.h"
21 TypeHandle AndroidGraphicsStateGuardian::_type_handle;
28 AndroidGraphicsStateGuardian::
32 GLES2GraphicsStateGuardian(engine, pipe)
34 GLESGraphicsStateGuardian(engine, pipe)
44 _prepared_objects = share_with->get_prepared_objects();
45 _share_context = share_with->_context;
54 AndroidGraphicsStateGuardian::
55 ~AndroidGraphicsStateGuardian() {
56 if (_context != (EGLContext)NULL) {
57 if (!eglDestroyContext(_egl_display, _context)) {
58 androiddisplay_cat.error() <<
"Failed to destroy EGL context: "
59 << get_egl_error_string(eglGetError()) <<
"\n";
61 _context = (EGLContext)NULL;
73 bool &pbuffer_supported,
bool &pixmap_supported,
74 bool &slow, EGLConfig config) {
79 EGLint red_size, green_size, blue_size,
81 depth_size, stencil_size, samples, surface_type, caveat;
83 eglGetConfigAttrib(_egl_display, config, EGL_RED_SIZE, &red_size);
84 eglGetConfigAttrib(_egl_display, config, EGL_GREEN_SIZE, &green_size);
85 eglGetConfigAttrib(_egl_display, config, EGL_BLUE_SIZE, &blue_size);
86 eglGetConfigAttrib(_egl_display, config, EGL_ALPHA_SIZE, &alpha_size);
87 eglGetConfigAttrib(_egl_display, config, EGL_DEPTH_SIZE, &depth_size);
88 eglGetConfigAttrib(_egl_display, config, EGL_STENCIL_SIZE, &stencil_size);
89 eglGetConfigAttrib(_egl_display, config, EGL_SAMPLES, &samples);
90 eglGetConfigAttrib(_egl_display, config, EGL_SURFACE_TYPE, &surface_type);
91 eglGetConfigAttrib(_egl_display, config, EGL_CONFIG_CAVEAT, &caveat);
92 int err = eglGetError();
93 if (err != EGL_SUCCESS) {
94 androiddisplay_cat.error() <<
"Failed to get EGL config attrib: "
95 << get_egl_error_string(err) <<
"\n";
98 pbuffer_supported =
false;
99 if ((surface_type & EGL_PBUFFER_BIT)!=0) {
100 pbuffer_supported =
true;
103 pixmap_supported =
false;
104 if ((surface_type & EGL_PIXMAP_BIT)!=0) {
105 pixmap_supported =
true;
109 if (caveat == EGL_SLOW_CONFIG) {
113 if ((surface_type & EGL_WINDOW_BIT)==0) {
118 properties.set_back_buffers(1);
119 properties.set_rgb_color(1);
120 properties.
set_rgba_bits(red_size, green_size, blue_size, alpha_size);
121 properties.set_stencil_bits(stencil_size);
122 properties.set_depth_bits(depth_size);
123 properties.set_multisamples(samples);
126 properties.set_force_software(1);
127 properties.set_force_hardware(1);
138 bool need_pbuffer,
bool need_pixmap) {
140 _egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
144 int attrib_list[] = {
145 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
147 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
150 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
156 int num_configs = 0, returned_configs;
157 if (!eglChooseConfig(_egl_display, attrib_list, NULL, num_configs, &returned_configs) || returned_configs <= 0) {
158 androiddisplay_cat.error() <<
"eglChooseConfig failed: "
159 << get_egl_error_string(eglGetError()) <<
"\n";
163 num_configs = returned_configs;
164 EGLConfig *configs =
new EGLConfig[num_configs];
166 if (!eglChooseConfig(_egl_display, attrib_list, configs, num_configs, &returned_configs) || returned_configs <= 0) {
167 androiddisplay_cat.error() <<
"eglChooseConfig failed: "
168 << get_egl_error_string(eglGetError()) <<
"\n";
173 int best_quality = 0;
177 for (
int i = 0; i < num_configs; ++i) {
179 bool pbuffer_supported, pixmap_supported, slow;
184 const char *pbuffertext = pbuffer_supported ?
" (pbuffer)" :
"";
185 const char *pixmaptext = pixmap_supported ?
" (pixmap)" :
"";
186 const char *slowtext = slow ?
" (slow)" :
"";
187 androiddisplay_cat.debug()
188 << i <<
": " << fbprops << pbuffertext << pixmaptext << slowtext <<
"\n";
190 if ((quality > 0)&&(slow)) quality -= 10000000;
192 if (need_pbuffer && !pbuffer_supported) {
195 if (need_pixmap && !pixmap_supported) {
199 if (quality > best_quality) {
200 best_quality = quality;
202 best_props = fbprops;
206 if (best_quality > 0) {
207 androiddisplay_cat.debug()
208 <<
"Chosen config " << best_result <<
": " << best_props <<
"\n";
209 _fbconfig = configs[best_result];
210 eglGetConfigAttrib(_egl_display, _fbconfig, EGL_NATIVE_VISUAL_ID, &_format);
212 androiddisplay_cat.debug()
213 <<
"Window format: " << _format <<
"\n";
215 _fbprops = best_props;
219 androiddisplay_cat.error() <<
220 "Could not find a usable pixel format.\n";
233 if (_context != EGL_NO_CONTEXT) {
238 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
239 _context = eglCreateContext(_egl_display, _fbconfig, _share_context, context_attribs);
241 _context = eglCreateContext(_egl_display, _fbconfig, _share_context, NULL);
244 int err = eglGetError();
245 if (_context != EGL_NO_CONTEXT && err == EGL_SUCCESS) {
250 androiddisplay_cat.error()
251 <<
"Could not create EGL context!\n"
252 << get_egl_error_string(err) <<
"\n";
264 if (_context == EGL_NO_CONTEXT) {
268 if (!eglMakeCurrent(_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
269 androiddisplay_cat.error() <<
"Failed to call eglMakeCurrent: "
270 << get_egl_error_string(eglGetError()) <<
"\n";
275 eglDestroyContext(_egl_display, _context);
276 _context = EGL_NO_CONTEXT;
288 GLES2GraphicsStateGuardian::reset();
290 GLESGraphicsStateGuardian::reset();
294 if (_gl_renderer.find(
"PixelFlinger") != string::npos) {
295 _fbprops.set_force_software(1);
296 _fbprops.set_force_hardware(0);
298 _fbprops.set_force_hardware(1);
299 _fbprops.set_force_software(0);
311 if (_egl_version_major < major_version) {
314 if (_egl_version_minor < minor_version) {
325 void AndroidGraphicsStateGuardian::
328 GLES2GraphicsStateGuardian::gl_flush();
330 GLESGraphicsStateGuardian::gl_flush();
339 GLenum AndroidGraphicsStateGuardian::
340 gl_get_error()
const {
342 return GLES2GraphicsStateGuardian::gl_get_error();
344 return GLESGraphicsStateGuardian::gl_get_error();
353 void AndroidGraphicsStateGuardian::
356 GLES2GraphicsStateGuardian::query_gl_version();
358 GLESGraphicsStateGuardian::query_gl_version();
363 if (!eglInitialize(_egl_display, &_egl_version_major, &_egl_version_minor)) {
364 androiddisplay_cat.error() <<
"Failed to get EGL version number: "
365 << get_egl_error_string(eglGetError()) <<
"\n";
372 if (gles2gsg_cat.is_debug()) {
375 if (glesgsg_cat.is_debug()) {
378 <<
"EGL_VERSION = " << _egl_version_major <<
"." << _egl_version_minor
391 void AndroidGraphicsStateGuardian::
392 get_extra_extensions() {
393 save_extensions(eglQueryString(_egl_display, EGL_EXTENSIONS));
406 void *AndroidGraphicsStateGuardian::
407 do_get_extension_func(
const char *name) {
408 return (
void *)eglGetProcAddress(name);
void clear()
Unsets all properties that have been specified so far, and resets the FrameBufferProperties structure...
bool egl_is_at_least_version(int major_version, int minor_version) const
Returns true if the runtime GLX version number is at least the indicated value, false otherwise...
bool create_context()
Creates the context based on the config previously obtained in choose_pixel_format.
void choose_pixel_format(const FrameBufferProperties &properties, bool need_pbuffer, bool need_pixmap)
Selects a visual or fbconfig for all the windows and buffers that use this gsg.
virtual void reset()
Resets all internal state as if the gsg were newly created.
int get_quality(const FrameBufferProperties &reqs) const
Assumes that these properties are a description of a window.
An object to create GraphicsOutputs that share a particular 3-D API.
void get_properties(FrameBufferProperties &properties, bool &pbuffer_supported, bool &pixmap_supported, bool &slow, EGLConfig config)
Gets the FrameBufferProperties to match the indicated config.
void set_rgba_bits(int r, int g, int b, int a)
Convenience method for setting the red, green, blue and alpha bits in one go.
A tiny specialization on GLESGraphicsStateGuardian to add some egl-specific information.
void destroy_context()
Destroys the context previously created by create_context.
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...