Debian Codegraph @

experimental/wayland/gbm_egl_context.py

raw symbols SCIP
1 # from ctypes import *
2 #
3 # from . import xkbcommon
4 #
5 #
6 # ctx = xkbcommon.xkb_context_new(xkbcommon.XKB_CONTEXT_NO_FLAGS)
7 # print(ctx)
8 #
9 # # TODO: get a real string from Wayland:
10 # keymap_string = create_string_buffer(b"ACTUAL KEYMAP STRING HERE")
11 #
12 # keymap = xkbcommon.xkb_keymap_new_from_string(ctx, keymap_string, xkbcommon.XKB_KEYMAP_FORMAT_TEXT_V1,
13 # xkbcommon.XKB_KEYMAP_COMPILE_NO_FLAGS)
14 # print(keymap)
15 #
16 # state = xkbcommon.xkb_state_new(keymap)
17
18 import os
19
20 from pyglet.libs.wayland.gbm import *
21 from pyglet.libs.egl.egl import *
22 from pyglet.libs.egl.eglext import *
23
24 assert eglBindAPI(EGL_OPENGL_API) == EGL_TRUE
25
26 try:
27 cards = [f for f in os.listdir('/dev/dri/') if f.startswith('card')]
28 cards = [os.path.join('/dev/dri/', name) for name in cards]
29 assert cards
30 except (OSError, AssertionError):
31 print("unable to find DRI device")
32 exit()
33
34 # Open the first GBM device, and create a surface
35 fd = os.open(cards[0], os.O_RDWR | os.MFD_CLOEXEC)
36 gbm_device = gbm_create_device(fd)
37 gbm_surface = gbm_surface_create(gbm_device, 256, 256, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING)
38
39
40 # Get a native GBM platform display
41 dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_MESA, gbm_device, None)
42 print("Display: ", dpy)
43 # Initialize EGL:
44 major, minor = EGLint(), EGLint()
45 result = eglInitialize(dpy, major, minor)
46 assert result == 1, "EGL Initialization Failed"
47 print("Initialized EGL: ", major.value, minor.value)
48
49 # Get the number of configs:
50 num_configs = EGLint()
51 result = eglGetConfigs(dpy, None, 0, num_configs)
52 assert result == EGL_TRUE, "Failed to query Configs"
53 print("Number of configs available: ", num_configs.value)
54
55 configs = (EGLConfig * num_configs.value)()
56 result = eglGetConfigs(dpy, configs, num_configs, byref(num_configs))
57 assert result == EGL_TRUE, "Failed to query Configs"
58
59 for config in configs:
60 # For a test, just go with the first successful Config
61 srf = eglCreatePlatformWindowSurface(dpy, config, gbm_surface, None)
62 if srf:
63 print("Surface: ", srf)
64 break
65 else:
66 print("No Surface Found")
67 exit()
68
69
70 ctx = eglCreateContext(dpy, config, None, None)
71 print("Context:", ctx)
72
73 assert eglMakeCurrent(dpy, srf, srf, ctx) == EGL_TRUE, "Failed to Make Context Current"
74
75 eglDestroySurface(dpy, srf)
76 eglDestroyContext(dpy, ctx)
77 eglTerminate(dpy)
78
79 gbm_device_destroy(gbm_device)
80 os.close(fd)