forked from PhotonVision/photon-libcamera-gl-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadless_opengl.cpp
More file actions
138 lines (117 loc) · 4.04 KB
/
headless_opengl.cpp
File metadata and controls
138 lines (117 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "headless_opengl.h"
#include "glerror.h"
#include <fcntl.h>
#include <gbm.h>
#include <unistd.h>
#include <vector>
#include <EGL/eglext.h>
// The following code related to DRM/GBM was adapted from the following sources:
// https://github.com/eyelash/tutorials/blob/master/drm-gbm.c
// and
// https://www.raspberrypi.org/forums/viewtopic.php?t=243707#p1499181
//
// I am not the original author of this code, I have only modified it.
static int matchConfigToVisual(EGLDisplay display, EGLint visualId,
EGLConfig *configs, int count) {
EGLint id;
for (int i = 0; i < count; ++i) {
if (!eglGetConfigAttrib(display, configs[i], EGL_NATIVE_VISUAL_ID, &id))
continue;
if (id == visualId)
return i;
}
return -1;
}
static const EGLint configAttribs[] = {EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_DEPTH_SIZE,
8,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_NONE};
static const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
EGL_NONE
};
HeadlessData createHeadless() {
std::vector<std::string> paths = {"/dev/dri/card1", "/dev/dri/card0"};
int device = -1;
for (const auto &path : paths) {
device = open(path.c_str(), O_RDWR | O_CLOEXEC);
if (device != -1) {
break;
}
}
// no device could be opened
if (device == -1) {
throw std::runtime_error("Unable to open graphics fd");
}
gbm_device *gbmDevice = gbm_create_device(device);
if (gbmDevice == nullptr) {
throw std::runtime_error("Unable to create GBM device");
}
EGLDisplay display = eglGetDisplay((EGLNativeDisplayType)gbmDevice);
if (display == EGL_NO_DISPLAY) {
gbm_device_destroy(gbmDevice);
close(device);
throw std::runtime_error("Unable to get EGL display");
}
int major, minor;
if (eglInitialize(display, &major, &minor) == EGL_FALSE) {
eglTerminate(display);
gbm_device_destroy(gbmDevice);
close(device);
EGLERROR();
}
eglBindAPI(EGL_OPENGL_ES_API);
EGLERROR();
printf("Initialized EGL version: %d.%d\n", major, minor);
EGLint count;
EGLint numConfigs;
eglGetConfigs(display, nullptr, 0, &count);
EGLERROR();
auto *configs = new EGLConfig[count];
if (!eglChooseConfig(display, configAttribs, configs, count, &numConfigs)) {
eglTerminate(display);
gbm_device_destroy(gbmDevice);
close(device);
EGLERROR();
}
// I am not exactly sure why the EGL config must match the GBM format.
// But it works!
int configIndex =
matchConfigToVisual(display, GBM_FORMAT_XRGB8888, configs, numConfigs);
if (configIndex < 0) {
eglTerminate(display);
gbm_device_destroy(gbmDevice);
close(device);
EGLERROR();
}
EGLContext context = eglCreateContext(display, configs[configIndex],
EGL_NO_CONTEXT, contextAttribs);
if (context == EGL_NO_CONTEXT) {
eglTerminate(display);
gbm_device_destroy(gbmDevice);
close(device);
EGLERROR();
}
delete[] configs;
HeadlessData ret{.gbmFd = device,
.gbmDevice = gbmDevice,
.display = display,
.context = context};
return ret;
}
#include <iostream>
void destroyHeadless(HeadlessData status) {
std::cout << "Destroying headless" << std::endl;
eglDestroyContext(status.display, status.context);
eglTerminate(status.display);
gbm_device_destroy(status.gbmDevice);
close(status.gbmFd);
}