forked from PhotonVision/photon-libcamera-gl-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_hsv_thresholder.cpp
536 lines (441 loc) · 17 KB
/
gl_hsv_thresholder.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "gl_hsv_thresholder.h"
#include "glerror.h"
#include "gl_shader_source.h"
#include <EGL/eglext.h>
#include <GLES2/gl2ext.h>
#include <libdrm/drm_fourcc.h>
#include <iostream>
#include <stdexcept>
#define GLERROR() glerror(__LINE__)
#define EGLERROR() eglerror(__LINE__)
GLuint make_shader(GLenum type, const char *source) {
auto shader = glCreateShader(type);
void *ctx = eglGetCurrentContext();
// printf("Shader idx: %i context ptr: %lu\n", (int) shader, (size_t)ctx);
if (!shader) {
throw std::runtime_error("failed to create shader");
}
glShaderSource(shader, 1, &source, nullptr);
GLERROR();
glCompileShader(shader);
GLERROR();
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status) {
GLint log_size;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_size);
std::string out;
out.resize(log_size);
glGetShaderInfoLog(shader, log_size, nullptr, out.data());
glDeleteShader(shader);
throw std::runtime_error("failed to compile shader with error: " + out);
}
return shader;
}
GLuint make_program(const char *vertex_source, const char *fragment_source) {
auto vertex_shader = make_shader(GL_VERTEX_SHADER, vertex_source);
auto fragment_shader = make_shader(GL_FRAGMENT_SHADER, fragment_source);
auto program = glCreateProgram();
glAttachShader(program, vertex_shader);
GLERROR();
glAttachShader(program, fragment_shader);
GLERROR();
glLinkProgram(program);
GLERROR();
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
GLint log_size;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_size);
std::string out;
out.resize(log_size);
glGetProgramInfoLog(program, log_size, nullptr, out.data());
throw std::runtime_error("failed to link program with error: " + out);
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return program;
}
GlHsvThresholder::GlHsvThresholder(int width, int height)
: m_width(width), m_height(height) {
m_status = createHeadless();
m_context = m_status.context;
m_display = m_status.display;
}
GlHsvThresholder::~GlHsvThresholder() {
for (auto& program : m_programs)
glDeleteProgram(program);
glDeleteBuffers(1, &m_quad_vbo);
for (const auto [key, value]: m_framebuffers) {
glDeleteFramebuffers(1, &value);
}
destroyHeadless(m_status);
}
static void on_gl_error(EGLenum error,const char *command,EGLint messageType,EGLLabelKHR threadLabel,EGLLabelKHR objectLabel,const char* message)
{
printf("Error111: %s\n", message);
}
void GlHsvThresholder::start(const std::vector<int> &output_buf_fds) {
static auto glEGLImageTargetTexture2DOES =
(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress(
"glEGLImageTargetTexture2DOES");
static auto eglCreateImageKHR =
(PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
static auto eglDestroyImageKHR =
(PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
static auto glDebugMessageCallbackKHR =
(PFNEGLDEBUGMESSAGECONTROLKHRPROC)eglGetProcAddress("glDebugMessageCallbackKHR");
if (!eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context)) {
throw std::runtime_error("failed to bind egl context");
}
EGLERROR();
// glEnable(GL_DEBUG_OUTPUT_KHR);
// GLERROR();
// glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
// GLERROR();
// glDebugMessageCallbackKHR(on_gl_error, nullptr);
// GLERROR();
m_programs.reserve(5);
m_programs[0] = make_program(VERTEX_SOURCE, NONE_FRAGMENT_SOURCE);
m_programs[1] = make_program(VERTEX_SOURCE, HSV_FRAGMENT_SOURCE);
m_programs[2] = make_program(VERTEX_SOURCE, GRAY_FRAGMENT_SOURCE);
m_programs[3] = make_program(VERTEX_SOURCE, TILING_FRAGMENT_SOURCE);
m_programs[4] = make_program(VERTEX_SOURCE, THRESHOLDING_FRAGMENT_SOURCE);
for (auto fd : output_buf_fds) {
GLuint out_tex;
glGenTextures(1, &out_tex);
GLERROR();
glBindTexture(GL_TEXTURE_2D, out_tex);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLERROR();
const EGLint image_attribs[] = {EGL_WIDTH,
static_cast<EGLint>(m_width),
EGL_HEIGHT,
static_cast<EGLint>(m_height),
EGL_LINUX_DRM_FOURCC_EXT,
DRM_FORMAT_ABGR8888,
EGL_DMA_BUF_PLANE0_FD_EXT,
static_cast<EGLint>(fd),
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
0,
EGL_DMA_BUF_PLANE0_PITCH_EXT,
static_cast<EGLint>(m_width * 4),
EGL_NONE};
auto image =
eglCreateImageKHR(m_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT,
nullptr, image_attribs);
EGLERROR();
if (!image) {
throw std::runtime_error("failed to import fd " +
std::to_string(fd));
}
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
GLERROR();
eglDestroyImageKHR(m_display, image);
GLERROR();
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
GLERROR();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
GLERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, out_tex, 0);
GLERROR();
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("failed to complete framebuffer");
}
m_framebuffers.emplace(fd, framebuffer);
m_renderable.push(fd);
}
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
{
static GLfloat quad_varray[] = {
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
};
GLuint quad_vbo;
glGenBuffers(1, &quad_vbo);
GLERROR();
glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
GLERROR();
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_varray), quad_varray,
GL_STATIC_DRAW);
GLERROR();
m_quad_vbo = quad_vbo;
}
{
GLuint grayscale_texture;
glGenTextures(1, &grayscale_texture);
GLERROR();
glBindTexture(GL_TEXTURE_2D, grayscale_texture);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLERROR();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
GLERROR();
m_grayscale_texture = grayscale_texture;
}
{
GLuint grayscale_buffer;
glGenFramebuffers(1, &grayscale_buffer);
GLERROR();
glBindFramebuffer(GL_FRAMEBUFFER, grayscale_buffer);
GLERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_grayscale_texture, 0);
GLERROR();
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("failed to complete grayscale_buffer");
}
m_grayscale_buffer = grayscale_buffer;
}
{
GLuint min_max_texture;
glGenTextures(1, &min_max_texture);
GLERROR();
glBindTexture(GL_TEXTURE_2D, min_max_texture);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
GLERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
GLERROR();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width / 4, m_height / 4, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
GLERROR();
m_min_max_texture = min_max_texture;
}
{
GLuint min_max_framebuffer;
glGenFramebuffers(1, &min_max_framebuffer);
GLERROR();
glBindFramebuffer(GL_FRAMEBUFFER, min_max_framebuffer);
GLERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_min_max_texture, 0);
GLERROR();
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("failed to complete grayscale_buffer");
}
m_min_max_framebuffer = min_max_framebuffer;
}
}
void GlHsvThresholder::release() {
if (!eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
throw std::runtime_error("failed to bind egl context");
}
}
int GlHsvThresholder::testFrame(
const std::array<GlHsvThresholder::DmaBufPlaneData, 3> &yuv_plane_data,
EGLint encoding, EGLint range, ProcessType type) {
static auto glEGLImageTargetTexture2DOES =
(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress(
"glEGLImageTargetTexture2DOES");
static auto eglCreateImageKHR =
(PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
static auto eglDestroyImageKHR =
(PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
if (!glEGLImageTargetTexture2DOES) {
throw std::runtime_error(
"cannot get address of glEGLImageTargetTexture2DOES");
}
int framebuffer_fd;
{
std::scoped_lock lock(m_renderable_mutex);
if (!m_renderable.empty()) {
framebuffer_fd = m_renderable.front();
// std::cout << "yes framebuffer" << std::endl;
m_renderable.pop();
} else {
std::cout << "no framebuffer, skipping" << std::endl;
return 0;
}
}
// Begin code setup that does not change with type
EGLint attribs[] = {EGL_WIDTH,
m_width,
EGL_HEIGHT,
m_height,
EGL_LINUX_DRM_FOURCC_EXT,
DRM_FORMAT_YUV420,
EGL_DMA_BUF_PLANE0_FD_EXT,
yuv_plane_data[0].fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
yuv_plane_data[0].offset,
EGL_DMA_BUF_PLANE0_PITCH_EXT,
yuv_plane_data[0].pitch,
EGL_DMA_BUF_PLANE1_FD_EXT,
yuv_plane_data[1].fd,
EGL_DMA_BUF_PLANE1_OFFSET_EXT,
yuv_plane_data[1].offset,
EGL_DMA_BUF_PLANE1_PITCH_EXT,
yuv_plane_data[1].pitch,
EGL_DMA_BUF_PLANE2_FD_EXT,
yuv_plane_data[2].fd,
EGL_DMA_BUF_PLANE2_OFFSET_EXT,
yuv_plane_data[2].offset,
EGL_DMA_BUF_PLANE2_PITCH_EXT,
yuv_plane_data[2].pitch,
EGL_YUV_COLOR_SPACE_HINT_EXT,
encoding,
EGL_SAMPLE_RANGE_HINT_EXT,
range,
EGL_NONE};
auto image = eglCreateImageKHR(m_display, EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT, nullptr, attribs);
EGLERROR();
if (!image) {
throw std::runtime_error("failed to import fd " +
std::to_string(yuv_plane_data[0].fd));
}
GLuint texture;
glGenTextures(1, &texture);
GLERROR();
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
GLERROR();
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLERROR();
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLERROR();
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image);
GLERROR();
eglDestroyImageKHR(m_display, image);
EGLERROR();
// End portion of code that does not change with type
GLuint initial_program = -1;
if (type == ProcessType::None) {
initial_program = m_programs[0];
} else if (type == ProcessType::Hsv) {
initial_program = m_programs[1];
} else if (type == ProcessType::Gray || type == ProcessType::Adaptive) {
initial_program = m_programs[2];
}
glUseProgram(initial_program);
GLERROR();
glUniform1i(glGetUniformLocation(initial_program, "tex"), 0);
GLERROR();
if (type == ProcessType::Hsv) {
auto lll = glGetUniformLocation(initial_program, "lowerThresh");
auto uuu = glGetUniformLocation(initial_program, "upperThresh");
auto invert = glGetUniformLocation(initial_program, "invertHue");
std::lock_guard lock{m_hsv_mutex};
glUniform3f(lll, m_hsvLower[0], m_hsvLower[1], m_hsvLower[2]);
GLERROR();
glUniform3f(uuu, m_hsvUpper[0], m_hsvUpper[1], m_hsvUpper[2]);
GLERROR();
glUniform1i(invert, m_invertHue);
GLERROR();
}
glActiveTexture(GL_TEXTURE0);
GLERROR();
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
GLERROR();
glBindBuffer(GL_ARRAY_BUFFER, m_quad_vbo);
GLERROR();
auto attr_loc = glGetAttribLocation(initial_program, "vertex");
glEnableVertexAttribArray(attr_loc);
GLERROR();
glVertexAttribPointer(attr_loc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
GLERROR();
if (type != ProcessType::Adaptive) {
auto out_framebuffer = m_framebuffers.at(framebuffer_fd);
glBindFramebuffer(GL_FRAMEBUFFER, out_framebuffer);
GLERROR();
glViewport(0, 0, m_width, m_height);
GLERROR();
glClear(GL_COLOR_BUFFER_BIT);
GLERROR();
glDrawArrays(GL_TRIANGLES, 0, 6);
GLERROR();
glFinish();
GLERROR();
glDeleteTextures(1, &texture);
GLERROR();
return framebuffer_fd;
} else {
glBindFramebuffer(GL_FRAMEBUFFER, m_grayscale_buffer);
GLERROR();
glViewport(0, 0, m_width, m_height);
GLERROR();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLERROR();
glDrawArrays(GL_TRIANGLES, 0, 6);
GLERROR();
glBindFramebuffer(GL_FRAMEBUFFER, m_min_max_framebuffer);
GLERROR();
glViewport(0, 0, m_width / 4, m_height / 4);
GLERROR();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLERROR();
glActiveTexture(GL_TEXTURE0);
GLERROR();
glBindTexture(GL_TEXTURE_2D, m_grayscale_texture);
GLERROR();
glUseProgram(m_programs[3]);
GLERROR();
glUniform1i(glGetUniformLocation(m_programs[3], "tex"), 0);
GLERROR();
auto res = glGetUniformLocation(m_programs[3], "resolution_in");
GLERROR();
glUniform2f(res, m_width, m_height);
GLERROR();
glDrawArrays(GL_TRIANGLES, 0, 6);
GLERROR();
// TODO: This finish may be unneeded
glFinish();
GLERROR();
auto out_framebuffer = m_framebuffers.at(framebuffer_fd);
glBindFramebuffer(GL_FRAMEBUFFER, out_framebuffer);
GLERROR();
glViewport(0, 0, m_width, m_height);
GLERROR();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLERROR();
glUseProgram(m_programs[4]);
GLERROR();
glUniform1i(glGetUniformLocation(m_programs[4], "tex"), 0);
GLERROR();
glUniform1i(glGetUniformLocation(m_programs[4], "tiles"), 1);
GLERROR();
glActiveTexture(GL_TEXTURE0);
GLERROR();
glBindTexture(GL_TEXTURE_2D, m_grayscale_texture);
GLERROR();
glActiveTexture(GL_TEXTURE1);
GLERROR();
glBindTexture(GL_TEXTURE_2D, m_min_max_texture);
GLERROR();
auto tile_res = glGetUniformLocation(m_programs[4], "tile_resolution");
GLERROR();
glUniform2f(tile_res, m_width / 4, m_height / 4);
GLERROR();
glDrawArrays(GL_TRIANGLES, 0, 6);
GLERROR();
glFinish();
GLERROR();
return framebuffer_fd;
}
}
void GlHsvThresholder::returnBuffer(int fd) {
std::scoped_lock lock(m_renderable_mutex);
m_renderable.push(fd);
}
void GlHsvThresholder::setHsvThresholds(double hl, double sl, double vl,
double hu, double su, double vu,
bool hueInverted) {
std::lock_guard lock{m_hsv_mutex};
m_hsvLower[0] = hl;
m_hsvLower[1] = sl;
m_hsvLower[2] = vl;
m_hsvUpper[0] = hu;
m_hsvUpper[1] = su;
m_hsvUpper[2] = vu;
m_invertHue = hueInverted;
}