Chapter 3 EGL Introduction

Source: Internet
Author: User

EGL is a set of platform-independent APIs provided by khronos group. Its functions:

1> communication with Native Windows system;
2> query available configurations;
3> Create a drawing surface available for OpenGL ES );
4> synchronize rendering between APIs of different categories, for example, between OpenGL ES and openvg, or between OpenGL and the drawing commands of local windows;
5> Manage "rendering resources", such as rendering map ).

● Egldisplay

EGL can run on the X Window System of GNU/Linux, and the quartz of Microsoft Windows and MacOS X.
EGL abstracts the display systems of these platforms into an independent type: egldisplay.
The first step to use EGL is to initialize an available egldisplay:

 

Eglint majorversion; <br/> eglint minorversion; <br/> eglboolean success = egl_false; <br/> egldisplay display = eglgetdisplay (egl_default_display); <br/> If (display! = Egl_no_display) <br/>{< br/> success = eglinitialize (display, & majorversion, & minorversion); <br/>}< br/> If (success! = Egl_true) <br/>{< br/> eglint errno = eglgeterror (); <br/> If (errno! = Egl_success) <br/>{< br/> _ tchar errmsg [32]; <br/> _ stprintf (errmsg, _ T ("[EGL] initialization failed. error code: 0x % 04x "), errno ); <br/> // The egl_bad_display egldisplay parameter is incorrect. <br/> // The egl_not_initialized EGL cannot be initialized. <br/>}< br/>}

 

Three EGL functions are used here:

 

Egldisplay eglgetdisplay (Consumer ID); <br/> eglboolean eglinitialize (egldisplay display, eglint * majorversion, eglint * minorversion); <br/> eglint eglgeterror ();

 

● Eglconfig

After initialization, select a suitable "drawing surface ".

 

Eglboolean eglgetconfigs (egldisplay display, // initialized <br/> eglconfig * configs. // if it is null, egl_true and numconfigs are returned, all available configurations of the Graphic System <br/> eglint maxconfigs, // The capacity of the configs array above <br/> eglint * numconfigs ); // The actual number of available configurations returned by the graphic system, stored in the configs Array

 

Use Case:

 

Static const eglint config_attribs [] = <br/>{< br/> egl_red_size, 5, <br/> egl_green_size, 6, <br/> egl_blue_size, 5, <br/> egl_depth_size, 16, <br/> egl_alpha_size, egl_dont_care, <br/> egl_stencil_size, egl_dont_care, <br/> egl_surface_type, egl_window_bit, <br/> egl_none // Attribute Table end with this constant <br/>}; <br/> glint numconfigs; <br/> eglconfig; <br/> If (success! = Egl_false) <br/> success = eglgetconfigs (display, null, 0, & numconfigs); <br/> If (success! = Egl_false & numconfigs> 0) <br/> success = eglchooseconfig (display, config_attribs, & config, 1, & numconfigs );

 

You can query a property of a Configuration:

 

Eglboolean eglgetconfigattrib (egldisplay display, // initialized <br/> eglconfig, // a configuration <br/> eglint attribute, // an attribute <br/> eglint * value );

 

Let EGL select a configuration for you:

 

Eglboolean eglchooseconfig (egldisplay display, <br/> const eglint * attribs, // The attributes you want are defined in this array in advance <br/> eglconfig * configs, // The Graphic System returns several configurations that meet the conditions to the array <br/> eglint maxconfigs, // the size of the above array <br/> eglint * numconfigs ); // number of available configurations returned by the Graphic System

 

If multiple configurations are selected for EGL, put them in the array according to certain rules:
1> egl_config_caveat
2> egl_color_buffer_type
3> occupied by color Buffer
4> egl_buffer_size
5> egl_sample_buffers
6> egl_samples
7> egl_depth_size
8> egl_stencil_size
9> egl_alpha_mask_size
10> egl_native_visual_type
11> egl_config_id

● Eglsurface

 

Eglsurface eglcreatewindowsurface (egldisplay display, <br/> eglconfig, <br/> eglnativewindowtype window, // hwnd type on Windows <br/> const eglint * attribs ); // This attribute table is a non-peer Attribute Table.

 

The attribute table here is not used for OpenGL ES 2.0, but for other APIs, such as openvg. We only need to remember one: egl_render_buffer [egl_back_buffer, egl_front_buffer].
OpenGL ES 2.0 must work in a dual-buffer window system.
Of course, this attribute table can also be null or only one egl_none. That indicates that all attributes use the default value.
If the function returns egl_no_surface, it fails. Error code:
Egl_bad_match: attribute setting error. For example, egl_surface_type is not set to egl_window_bit.
Egl_bad_config: The graphic system does not support configuration errors.
Egl_bad_native_window: The Window handle is incorrect.
Egl_bad_alloc: the drawing surface cannot be created. For example, you have already created one.

 

● Pixel Buffer

OpenGL ES 2.0 can be rendered to Pixel Buffer, and hardware acceleration is also used. Pbuffer is often used to generate texture ing. To render a texture, more efficient framebuffer objects are often used.
Use egl_pbuffer_bit in egl_surface_type to create pbuffer:

 

Eglsurface eglcreatepbuffersurface (egldisplay display, <br/> eglconfig, <br/> const eglint * attribs );

 

Attributes used:

Egl_width, egl_height
Egl_largest_pbuffer: If the parameter is not suitable, the maximum pbuffer can be used.
Egl_texture_format: [egl_no_texture] to bind a pbuffer to a texture ing, specify the texture format.
Egl_texture_target: [egl_no_texture, egl_texture_2d]
Egl_mipmap_textrue: [egl_true, egl_false]

Egl_no_surface is returned when creation fails. Error code:
Egl_bad_alloc: Resource missing
Egl_bad_config: configuration error
Egl_bad_parameter: egl_width and egl_height are negative
Egl_bad_match: configuration error. If it is used for texture ing, the high-width parameter is incorrect. Only one egl_texture_format and egl_texture_target are not egl_no_texture.
Egl_bad_attribute: Specifies egl_texture_format, egl_texture_target, or egl_mipmap_textrue, but does not specify opengles in the configuration.

Example of using pbuffer:

 

Eglint cfgattribs [] = <br/> {<br/> egl_surface_type, egl_pbuffer_bit, <br/> egl_renderable_type, struct, <br/> egl_red_size, 5, <br/> egl_green_size, 6, <br/> egl_blue_size, 5, <br/> egl_depth_size, 1, <br/> egl_none <br/>}; <br/> const eglint max_config = 10; // select one of the 10 configurations <br/> eglconfig configs [max_config]; <br/> eglint numconfigs; <br/> If (! Eglchooseconfig (display, cfgattribs, configs, max_config, & numconfigs )) <br/>{< br/> // error <br/>}< br/> else <br/>{< br/> // select a configuration <br/>} <br/> eglint pbufattribs [] = <br/> {<br/> egl_width, 512, <br/> egl_height, 512, <br/> egl_largest_pbuffer, egl_true, <br/> egl_none <br/>}; <br/> eglrendersurface pbuffer = eglcreatepbuffersurface (display, config, pbufattribs); <br/> If (pbuffer = egl_no_surface) <br/> {<Br/> // creation failed. Various errors are reported. <br/>}< br/> eglint width, height; <br/> If (! Eglquerysurface (display, pbuffer, egl_height, & height) <br/> |! Eglquerysurface (display, pbuffer, egl_width, & width) <br/>{< br/> // No information is found, and an error is returned <br/>}

 

The biggest difference between pbuffer and normal window rendering is that it cannot be swap, either copy its value or modify it to bind it to a texture.

● Eglcontext

 

Eglcontext eglcreatecontext (egldisplay display, <br/> eglconfig, <br/> eglcontext context, // egl_no_context indicates that resources are not shared with other context <br/> const eglint * attribs) // currently, we only use egl_context_client_version <br/> const eglint attribs [] = <br/>{< br/> egl_context_client_version, 2, <br/> egl_none <br/> }; <br/> eglcontext context = eglcreatecontext (display, CFG, egl_no_context, attribs); <br/> If (context = egl_no _ Context) <br/>{< br/> If (egl_bad_config = eglgeterror () <br/>{< br/>... <br/>}< br/> If (! Eglmakecurrent (display, window, window, context) // both Windows indicate that the read and write operations are in the same window <br/>{< br/> // error <br/>}

 

● Rendering Synchronization

If you only use OpenGL ES 2.0, glfinish will ensure that all rendering work continues.
However, it is easier to use openvg or the local image API to render the font than to use OpenGL ES 2.0. Therefore, you may need to use multiple libraries for rendering in the same window.

You can use the EGL synchronization function: eglboolean eglwaitclient () to delay client execution and wait for the server side to complete rendering of OpenGL ES 2.0 or openvg.
If it fails, the error code egl_bad_current_surface is returned.

To wait until the rendering of the local image API is complete, use: eglboolean eglwaitnative (eglint engine ).
The engine parameter must be egl_core_native_engine. Other values are specified through the EGL extension.
If it fails, the error code egl_bad_parameter is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.