Skip to content

Commit

Permalink
prototype a new headless egl context
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcsdombi committed Dec 18, 2022
1 parent 3dc2a93 commit 537f649
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
107 changes: 107 additions & 0 deletions glcontext/headless.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <Python.h>

#include <EGL/egl.h>
#include <EGL/eglext.h>

int num_devices;
EGLDeviceEXT devices[64];

EGLContext context;
EGLDisplay display;
EGLConfig config;

PyObject * meth_devices(PyObject * self) {
PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT = (PFNEGLQUERYDEVICESEXTPROC)eglGetProcAddress("eglQueryDevicesEXT");
PFNEGLQUERYDEVICESTRINGEXTPROC eglQueryDeviceStringEXT = (PFNEGLQUERYDEVICESTRINGEXTPROC)eglGetProcAddress("eglQueryDeviceStringEXT");

if (!eglQueryDevicesEXT(0, NULL, &num_devices)) {
return NULL;
}

if (!eglQueryDevicesEXT(num_devices, devices, &num_devices)) {
return NULL;
}

PyObject * res = PyList_New(num_devices);
for (int i = 0; i < num_devices; ++i) {
const char * egl_extensions = eglQueryDeviceStringEXT(devices[i], EGL_EXTENSIONS);
PyObject * temp = PyUnicode_FromString(egl_extensions ? egl_extensions : "");
PyObject * extensions = PyObject_CallMethod(temp, "split", NULL);
Py_DECREF(temp);
PyList_SetItem(res, i, Py_BuildValue("{sisN}", "device", i, "extensions", extensions));
}
return res;
}

PyObject * meth_init(PyObject * self, PyObject * args, PyObject * kwargs) {
const char * keywords[] = {"device", NULL};

int device = 0;

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", (char **)keywords, &device)) {
return NULL;
}

if (device > num_devices) {
return NULL;
}

display = eglGetPlatformDisplay(EGL_PLATFORM_DEVICE_EXT, devices[device], 0);
if (display == EGL_NO_DISPLAY) {
return NULL;
}

if (!eglInitialize(display, NULL, NULL)) {
return NULL;
}

int config_attribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE,
};

int num_configs = 0;
if (!eglChooseConfig(display, config_attribs, &config, 1, &num_configs)) {
return NULL;
}

if (!eglBindAPI(EGL_OPENGL_API)) {
return NULL;
}

int context_attribs[] = {
EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
EGL_NONE,
};

context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
if (!context) {
return NULL;
}

eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
Py_RETURN_NONE;
}

PyObject * meth_load_opengl_function(PyObject * self, PyObject * arg) {
if (!PyUnicode_CheckExact(arg)) {
return NULL;
}
const char * name = PyUnicode_AsUTF8(arg);
return PyLong_FromVoidPtr((void *)eglGetProcAddress(name));
}

PyMethodDef module_methods[] = {
{"devices", (PyCFunction)meth_devices, METH_NOARGS},
{"init", (PyCFunction)meth_init, METH_VARARGS | METH_KEYWORDS},
{"load_opengl_function", (PyCFunction)meth_load_opengl_function, METH_O},
{},
};

PyModuleDef module_def = {PyModuleDef_HEAD_INIT, "headless", NULL, -1, module_methods};

extern "C" PyObject * PyInit_headless() {
PyObject * module = PyModule_Create(&module_def);
return module;
}
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
libraries=['dl'],
)

headless = Extension(
name='glcontext.headless',
sources=['glcontext/headless.cpp'],
libraries=['EGL'],
)

darwin = Extension(
name='glcontext.darwin',
sources=['glcontext/darwin.cpp'],
Expand All @@ -53,7 +59,7 @@

ext_modules = {
'windows': [wgl],
'linux': [x11, egl],
'linux': [x11, egl, headless],
'darwin': [darwin],
}

Expand Down
19 changes: 14 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import glcontext
import ctypes

backend = glcontext.default_backend()
ctx = backend(mode='standalone', glversion=330) #, libgl='libGL.so.1')
print(ctx)
print(ctx.load('glViewport'))
from glcontext import headless

devices = headless.devices()

headless.init(device=next(x['device'] for x in devices if 'EGL_MESA_device_software' in x['extensions']))

glGetStringPtr = headless.load_opengl_function('glGetString')
glGetString = ctypes.cast(glGetStringPtr, ctypes.CFUNCTYPE(ctypes.c_char_p, ctypes.c_uint32))

print(glGetString(0x1F00).decode())
print(glGetString(0x1F01).decode())
print(glGetString(0x1F02).decode())
print(glGetString(0x1F03).decode())

0 comments on commit 537f649

Please sign in to comment.