-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prototype a new headless egl context
- Loading branch information
1 parent
3dc2a93
commit 537f649
Showing
3 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |