Difficult-Rocket/libs/pyglet/canvas/headless.py

72 lines
2.1 KiB
Python
Raw Normal View History

2021-04-16 23:21:06 +08:00
import pyglet
import warnings
2021-08-13 12:25:29 +08:00
2021-04-16 23:21:06 +08:00
from .base import Display, Screen, ScreenMode, Canvas
from ctypes import *
from pyglet.libs.egl import egl
from pyglet.libs.egl import eglext
class HeadlessDisplay(Display):
def __init__(self):
super().__init__()
# TODO: fix this placeholder:
self._screens = [HeadlessScreen(self, 0, 0, 1920, 1080)]
num_devices = egl.EGLint()
eglext.eglQueryDevicesEXT(0, None, byref(num_devices))
if num_devices.value > 0:
headless_device = pyglet.options['headless_device']
if headless_device < 0 or headless_device >= num_devices.value:
2023-01-25 20:38:17 +08:00
raise ValueError(f'Invalid EGL devide id: {headless_device}')
2021-04-16 23:21:06 +08:00
devices = (eglext.EGLDeviceEXT * num_devices.value)()
eglext.eglQueryDevicesEXT(num_devices.value, devices, byref(num_devices))
2021-08-13 12:25:29 +08:00
self._display_connection = eglext.eglGetPlatformDisplayEXT(
eglext.EGL_PLATFORM_DEVICE_EXT, devices[headless_device], None)
2021-04-16 23:21:06 +08:00
else:
2021-08-13 12:25:29 +08:00
warnings.warn('No device available for EGL device platform. Using native display type.')
2021-04-16 23:21:06 +08:00
display = egl.EGLNativeDisplayType()
self._display_connection = egl.eglGetDisplay(display)
egl.eglInitialize(self._display_connection, None, None)
def get_screens(self):
return self._screens
def __del__(self):
egl.eglTerminate(self._display_connection)
class HeadlessCanvas(Canvas):
def __init__(self, display, egl_surface):
2021-08-13 12:25:29 +08:00
super().__init__(display)
2021-04-16 23:21:06 +08:00
self.egl_surface = egl_surface
2021-08-13 12:25:29 +08:00
2021-04-16 23:21:06 +08:00
class HeadlessScreen(Screen):
def __init__(self, display, x, y, width, height):
super().__init__(display, x, y, width, height)
def get_matching_configs(self, template):
canvas = HeadlessCanvas(self.display, None)
configs = template.match(canvas)
# XXX deprecate
for config in configs:
config.screen = self
return configs
def get_modes(self):
pass
def get_mode(self):
pass
def set_mode(self, mode):
pass
def restore_mode(self):
pass