Debian Codegraph @

examples/opengl/egl_context.py

raw symbols SCIP
1 from pyglet.libs.egl import egl as libegl
2 from pyglet.libs.egl.egl import (
3 EGL_SINGLE_BUFFER, EGL_BACK_BUFFER, EGL_NONE, EGL_OPENGL_API, EGL_OPENGL_ES_API,
4 EGL_SUCCESS, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_BLUE_SIZE, EGL_GREEN_SIZE, EGL_RED_SIZE,
5 EGL_DEPTH_SIZE, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE, EGL_WIDTH, EGL_HEIGHT,
6 EGL_CONTEXT_MAJOR_VERSION, EGL_RENDER_BUFFER,
7 )
8
9
10 _buffer_types = {EGL_SINGLE_BUFFER: "EGL_RENDER_BUFFER",
11 EGL_BACK_BUFFER: "EGL_BACK_BUFFER",
12 EGL_NONE: "EGL_NONE"}
13
14 _api_types = {EGL_OPENGL_API: "EGL_OPENGL_API",
15 EGL_OPENGL_ES_API: "EGL_OPENGL_ES_API",
16 EGL_NONE: "EGL_NONE"}
17
18 # Initialize a display:
19 display = libegl.EGLNativeDisplayType()
20 display_connection = libegl.eglGetDisplay(display)
21
22 majorver = libegl.EGLint()
23 minorver = libegl.EGLint()
24 result = libegl.eglInitialize(display_connection, majorver, minorver)
25 assert result == 1, "EGL Initialization Failed"
26 egl_version = majorver.value, minorver.value
27 print(f"EGL version: {egl_version}")
28
29 # Get the number of configs:
30 num_configs = libegl.EGLint()
31 config_size = libegl.EGLint()
32 result = libegl.eglGetConfigs(display_connection, None, config_size, num_configs)
33 assert result == 1, "Failed to query Configs"
34
35 print("Number of configs available: ", num_configs.value)
36
37 # Choose a config:
38 config_attribs = (EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
39 EGL_BLUE_SIZE, 8,
40 EGL_GREEN_SIZE, 8,
41 EGL_RED_SIZE, 8,
42 EGL_DEPTH_SIZE, 8,
43 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
44 EGL_NONE)
45 config_attrib_array = (libegl.EGLint * len(config_attribs))(*config_attribs)
46 egl_config = libegl.EGLConfig()
47 result = libegl.eglChooseConfig(display_connection, config_attrib_array, egl_config, 1, num_configs)
48 assert result == 1, "Failed to choose Config"
49
50 # Create a surface:
51 pbufferwidth = 1
52 pbufferheight = 1
53 pbuffer_attribs = (EGL_WIDTH, pbufferwidth, EGL_HEIGHT, pbufferheight, EGL_NONE)
54 pbuffer_attrib_array = (libegl.EGLint * len(pbuffer_attribs))(*pbuffer_attribs)
55 surface = libegl.eglCreatePbufferSurface(display_connection, egl_config, pbuffer_attrib_array)
56 print("Surface id: ", surface)
57
58 # Bind the API:
59 result = libegl.eglBindAPI(libegl.EGL_OPENGL_API)
60 assert result == 1, "Failed to bind EGL_OPENGL_API"
61
62 # Create a context:
63 context_attribs = (EGL_CONTEXT_MAJOR_VERSION, 2, EGL_NONE)
64 context_attrib_array = (libegl.EGLint * len(context_attribs))(*context_attribs)
65 context = libegl.eglCreateContext(display_connection, egl_config, None, context_attrib_array)
66 print("Context id: ", context)
67
68 # Make context current:
69 result = libegl.eglMakeCurrent(display_connection, surface, surface, context)
70 assert result == 1, "Failed to make context current"
71
72 error_code = libegl.eglGetError()
73 assert error_code == EGL_SUCCESS, f"EGL Error code {error_code} returned"
74
75 # Print some context details:
76 buffer_type = libegl.EGLint()
77 libegl.eglQueryContext(display_connection, context, EGL_RENDER_BUFFER, buffer_type)
78 print("Buffer type: ", _buffer_types.get(buffer_type.value, "Unknown"))
79 print("API type: ", _api_types.get(libegl.eglQueryAPI(), "Unknown"))
80
81 # Terminate EGL:
82 libegl.eglTerminate(display_connection)