-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathRenderAPI_OpenGLGLX.cpp
More file actions
829 lines (714 loc) · 29.3 KB
/
RenderAPI_OpenGLGLX.cpp
File metadata and controls
829 lines (714 loc) · 29.3 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include "RenderAPI_OpenGLGLX.h"
#include "Log.h"
#include <cassert>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#endif
#ifndef GL_HANDLE_TYPE_OPAQUE_FD_EXT
#define GL_HANDLE_TYPE_OPAQUE_FD_EXT 0x9586
#endif
GLXContext RenderAPI_OpenGLGLX::unity_context = nullptr;
Display* RenderAPI_OpenGLGLX::unity_display = nullptr;
namespace {
bool staticMakeCurrent(void* data, bool current)
{
auto that = static_cast<RenderAPI_OpenGLGLX*>(data);
return that->makeCurrent(current);
}
}
RenderAPI* CreateRenderAPI_OpenGLGLX(UnityGfxRenderer apiType)
{
return new RenderAPI_OpenGLGLX(apiType);
}
RenderAPI_OpenGLGLX::RenderAPI_OpenGLGLX(UnityGfxRenderer apiType) :
RenderAPI_OpenGLBase(apiType)
{
}
bool RenderAPI_OpenGLGLX::makeCurrent(bool current)
{
if (current)
{
GLXContext currentCtx = glXGetCurrentContext();
if (currentCtx == m_context)
return true;
return glXMakeContextCurrent(m_display, m_pbuffer, m_pbuffer, m_context);
}
else
{
if (glXGetCurrentContext() != m_context)
return true;
return glXMakeContextCurrent(m_display, None, None, nullptr);
}
}
void* RenderAPI_OpenGLGLX::get_proc_address(void* /*data*/, const char* procname)
{
return reinterpret_cast<void*>(glXGetProcAddressARB(
reinterpret_cast<const GLubyte*>(procname)));
}
void RenderAPI_OpenGLGLX::setVlcContext(libvlc_media_player_t *mp)
{
if (unity_context == nullptr) {
libvlc_media_player_t* prev = m_pending_mp;
m_pending_mp = mp;
assert((prev == nullptr || prev == mp) && "second setVlcContext while one is pending");
DEBUG("[GLX] Unity context not ready, deferring setVlcContext");
return;
}
m_pending_mp = nullptr;
DEBUG("[GLX] subscribing to DMA-BUF opengl output callbacks %p", this);
libvlc_video_set_output_callbacks(mp, libvlc_video_engine_opengl,
dmabuf_setup, dmabuf_cleanup, nullptr, dmabuf_resize, dmabuf_swap,
staticMakeCurrent, get_proc_address, nullptr, nullptr, this);
}
void RenderAPI_OpenGLGLX::unsetVlcContext(libvlc_media_player_t *mp)
{
if (m_pending_mp == mp) {
m_pending_mp = nullptr;
return; // Cancelled pending setVlcContext before it was promoted
}
// Callbacks were already registered — disable them
libvlc_video_set_output_callbacks(mp, libvlc_video_engine_disable,
nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr);
}
void RenderAPI_OpenGLGLX::retrieveOpenGLContext()
{
if (unity_context != nullptr)
return;
unity_context = glXGetCurrentContext();
if (unity_context == nullptr) {
DEBUG("[GLX] glXGetCurrentContext failed");
return;
}
unity_display = glXGetCurrentDisplay();
DEBUG("[GLX] retrieved Unity OpenGL context %p display %p", unity_context, unity_display);
}
void RenderAPI_OpenGLGLX::ensureCurrentContext()
{
if (glXGetCurrentContext() == nullptr) {
makeCurrent(true);
}
}
void RenderAPI_OpenGLGLX::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)
{
(void)interfaces;
if (type == kUnityGfxDeviceEventInitialize) {
if (m_context) {
return;
}
DEBUG("[GLX] Entering ProcessDeviceEvent with kUnityGfxDeviceEventInitialize");
if (unity_context == nullptr) {
DEBUG("[GLX] Failed to retrieve OpenGL Context... aborting.");
return;
}
m_display = unity_display;
if (m_display == nullptr) {
DEBUG("[GLX] unity_display is NULL");
return;
}
int screen = DefaultScreen(m_display);
// Match Unity's direct/indirect rendering mode
Bool unity_direct = glXIsDirect(m_display, unity_context);
DEBUG("[GLX] Unity context is %s rendering", unity_direct ? "direct" : "indirect");
// Query Unity context's FBConfig and render type
int unity_fbc_id = 0;
glXQueryContext(m_display, unity_context, GLX_FBCONFIG_ID, &unity_fbc_id);
DEBUG("[GLX] Unity context FBConfig ID = 0x%x", unity_fbc_id);
int unity_render_type = 0;
glXQueryContext(m_display, unity_context, GLX_RENDER_TYPE, &unity_render_type);
DEBUG("[GLX] Unity context render type = 0x%x", unity_render_type);
// Gather all RGBA pbuffer-capable FBConfigs
static const int pb_attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
None
};
int fbcount = 0;
GLXFBConfig* all_fbc = glXChooseFBConfig(m_display, screen, pb_attribs, &fbcount);
if (!all_fbc || fbcount == 0) {
DEBUG("[GLX] no PBuffer-capable FBConfigs found");
return;
}
DEBUG("[GLX] found %d pbuffer-capable FBConfigs", fbcount);
// Sort: put Unity's exact FBConfig first if present
for (int i = 1; i < fbcount; i++) {
int id = 0;
glXGetFBConfigAttrib(m_display, all_fbc[i], GLX_FBCONFIG_ID, &id);
if (id == unity_fbc_id) {
GLXFBConfig tmp = all_fbc[0];
all_fbc[0] = all_fbc[i];
all_fbc[i] = tmp;
DEBUG("[GLX] prioritized Unity's FBConfig (id=0x%x)", unity_fbc_id);
break;
}
}
typedef GLXContext (*PFNGLXCREATECONTEXTATTRIBSARBPROC)(
Display*, GLXFBConfig, GLXContext, Bool, const int*);
auto glXCreateContextAttribsARB =
reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB")));
GLXFBConfig chosen_fbc = nullptr;
bool shared_context = false;
// Try each FBConfig until shared context creation succeeds
for (int fi = 0; fi < fbcount && !m_context; fi++) {
GLXFBConfig try_fbc = all_fbc[fi];
if (fi < 5 || fi == fbcount - 1) {
int fbc_id = 0;
glXGetFBConfigAttrib(m_display, try_fbc, GLX_FBCONFIG_ID, &fbc_id);
DEBUG("[GLX] trying FBConfig %d/%d (id=0x%x)", fi + 1, fbcount, fbc_id);
}
if (glXCreateContextAttribsARB) {
static const int profiles[][3] = {
{4, 5, GLX_CONTEXT_CORE_PROFILE_BIT_ARB},
{3, 3, GLX_CONTEXT_CORE_PROFILE_BIT_ARB},
};
for (auto& p : profiles) {
const int ctx_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, p[0],
GLX_CONTEXT_MINOR_VERSION_ARB, p[1],
GLX_CONTEXT_PROFILE_MASK_ARB, p[2],
None
};
m_context = glXCreateContextAttribsARB(
m_display, try_fbc, unity_context, unity_direct, ctx_attribs);
if (m_context) {
DEBUG("[GLX] Created GL %d.%d core shared context (fbc %d/%d)",
p[0], p[1], fi + 1, fbcount);
chosen_fbc = try_fbc;
shared_context = true;
break;
}
if (m_context) { glXDestroyContext(m_display, m_context); m_context = nullptr; }
}
}
if (!m_context) {
m_context = glXCreateNewContext(m_display, try_fbc, GLX_RGBA_TYPE,
unity_context, unity_direct);
if (m_context) {
DEBUG("[GLX] Created shared context via glXCreateNewContext (fbc %d/%d)",
fi + 1, fbcount);
chosen_fbc = try_fbc;
shared_context = true;
} else {
if (m_context) { glXDestroyContext(m_display, m_context); m_context = nullptr; }
}
}
}
// Non-shared context fallback
if (!m_context) {
DEBUG("[GLX] all %d FBConfigs failed for shared context, creating non-shared", fbcount);
chosen_fbc = all_fbc[0];
m_context = glXCreateNewContext(m_display, chosen_fbc, GLX_RGBA_TYPE,
nullptr, unity_direct);
if (m_context) {
DEBUG("[GLX] created non-shared context");
} else {
if (m_context) { glXDestroyContext(m_display, m_context); m_context = nullptr; }
DEBUG("[GLX] even non-shared context creation failed");
}
}
if (!m_context) {
XFree(all_fbc);
DEBUG("[GLX] all context creation attempts failed");
return;
}
// Create pbuffer
static const int pbuffer_attribs[] = {
GLX_PBUFFER_WIDTH, 2,
GLX_PBUFFER_HEIGHT, 2,
None
};
m_pbuffer = glXCreatePbuffer(m_display, chosen_fbc, pbuffer_attribs);
XFree(all_fbc);
if (m_pbuffer == None) {
DEBUG("[GLX] glXCreatePbuffer() failed");
glXDestroyContext(m_display, m_context);
m_context = nullptr;
return;
}
if (!initDMABuf()) {
DEBUG("[GLX] DMA-BUF initialization failed");
if (m_pbuffer) {
glXDestroyPbuffer(m_display, m_pbuffer);
m_pbuffer = None;
}
glXDestroyContext(m_display, m_context);
m_context = nullptr;
return;
}
DEBUG("[GLX] kUnityGfxDeviceEventInitialize success disp=%p pbuf=%lx ctx=%p shared=%d dmabuf=%d",
m_display, m_pbuffer, m_context, shared_context,
1
);
libvlc_media_player_t* pending = m_pending_mp;
m_pending_mp = nullptr;
if (pending)
setVlcContext(pending);
} else if (type == kUnityGfxDeviceEventShutdown) {
DEBUG("[GLX] kUnityGfxDeviceEventShutdown");
shutdownInternal();
}
}
void RenderAPI_OpenGLGLX::shutdownInternal()
{
releaseDMABufResources();
if (m_context) {
glXDestroyContext(m_display, m_context);
m_context = nullptr;
}
if (m_pbuffer) {
glXDestroyPbuffer(m_display, m_pbuffer);
m_pbuffer = None;
}
}
RenderAPI_OpenGLGLX::~RenderAPI_OpenGLGLX()
{
shutdownInternal();
}
void RenderAPI_OpenGLGLX::performRenderThreadWork()
{
std::lock_guard<std::mutex> lock(m_dmabuf_lock);
if (m_unity_textures_imported || m_dmabuf_width == 0 || m_dmabuf_height == 0)
return;
if (glXGetCurrentContext() == nullptr) {
DEBUG("[GLX] no GL context current on render thread");
return;
}
DEBUG("[GLX] importing DMA-BUF textures into Unity context (render thread)");
for (size_t i = 0; i < kDMABufSlots; i++) {
if (!importDMABufToUnityContext(m_dmabuf_buffers[i], m_dmabuf_width, m_dmabuf_height)) {
DEBUG("[GLX] failed to import DMA-BUF buffer %zu into Unity context", i);
return;
}
}
m_unity_textures_imported = true;
DEBUG("[GLX] all DMA-BUF textures imported into Unity context");
}
// ==========================================================================
// DMA-BUF implementation (via GL_EXT_memory_object_fd)
// ==========================================================================
#ifndef GL_HANDLE_TYPE_OPAQUE_FD_EXT
#define GL_HANDLE_TYPE_OPAQUE_FD_EXT 0x9586
#endif
#ifndef GL_DEDICATED_MEMORY_OBJECT_EXT
#define GL_DEDICATED_MEMORY_OBJECT_EXT 0x9581
#endif
bool RenderAPI_OpenGLGLX::loadMemoryObjectExtensions()
{
glCreateMemoryObjectsEXT = reinterpret_cast<PFNGLCREATEMEMORYOBJECTSEXTPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glCreateMemoryObjectsEXT")));
glTexStorageMem2DEXT = reinterpret_cast<PFNGLTEXSTORAGEMEM2DEXTPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glTexStorageMem2DEXT")));
glImportMemoryFdEXT = reinterpret_cast<PFNGLIMPORTMEMORYFDEXTPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glImportMemoryFdEXT")));
glDeleteMemoryObjectsEXT = reinterpret_cast<PFNGLDELETEMEMORYOBJECTSEXTPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glDeleteMemoryObjectsEXT")));
glMemoryObjectParameterivEXT = reinterpret_cast<PFNGLMEMORYOBJECTPARAMETERIVEXTPROC_>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glMemoryObjectParameterivEXT")));
if (!glCreateMemoryObjectsEXT || !glTexStorageMem2DEXT ||
!glImportMemoryFdEXT || !glDeleteMemoryObjectsEXT) {
DEBUG("[GLX] GL_EXT_memory_object_fd extension functions not available");
return false;
}
// Load raw GL core functions (bypass Unity's GL wrapper)
raw_glGenTextures = reinterpret_cast<PFNGLGENTEXTURESPROC_RAW>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glGenTextures")));
raw_glBindTexture = reinterpret_cast<PFNGLBINDTEXTUREPROC_RAW>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glBindTexture")));
raw_glTexParameteri = reinterpret_cast<PFNGLTEXPARAMETERIPROC_RAW>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glTexParameteri")));
raw_glDeleteTextures = reinterpret_cast<PFNGLDELETETEXTURESPROC_RAW>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glDeleteTextures")));
if (!raw_glGenTextures || !raw_glBindTexture || !raw_glTexParameteri) {
DEBUG("[GLX] failed to load raw GL function pointers");
return false;
}
DEBUG("[GLX] GL_EXT_memory_object_fd extensions loaded");
return true;
}
bool RenderAPI_OpenGLGLX::initDMABuf()
{
// Check GL extensions — need a current context
GLXContext prev_ctx = glXGetCurrentContext();
GLXDrawable prev_draw = glXGetCurrentDrawable();
GLXDrawable prev_read = glXGetCurrentReadDrawable();
makeCurrent(true);
const char* extensions = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
if (!extensions ||
(strstr(extensions, "GL_EXT_memory_object") == nullptr ||
strstr(extensions, "GL_EXT_memory_object_fd") == nullptr)) {
DEBUG("[GLX] GL_EXT_memory_object_fd not in extension string");
glXMakeContextCurrent(m_display, prev_draw, prev_read, prev_ctx);
return false;
}
if (!loadMemoryObjectExtensions()) {
glXMakeContextCurrent(m_display, prev_draw, prev_read, prev_ctx);
return false;
}
// Restore Unity's context
glXMakeContextCurrent(m_display, prev_draw, prev_read, prev_ctx);
// Open DRM render node
DIR* dir = opendir("/dev/dri");
if (!dir) {
DEBUG("[GLX] cannot open /dev/dri");
return false;
}
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
if (strncmp(entry->d_name, "renderD", 7) == 0) {
char path[280];
snprintf(path, sizeof(path), "/dev/dri/%.255s", entry->d_name);
m_drm_fd = open(path, O_RDWR | O_CLOEXEC);
if (m_drm_fd >= 0) {
DEBUG("[GLX] opened DRM render node %s (fd=%d)", path, m_drm_fd);
break;
}
}
}
closedir(dir);
if (m_drm_fd < 0) {
DEBUG("[GLX] no DRM render node found");
return false;
}
m_gbm_device = gbm_create_device(m_drm_fd);
if (!m_gbm_device) {
DEBUG("[GLX] gbm_create_device failed");
close(m_drm_fd);
m_drm_fd = -1;
return false;
}
DEBUG("[GLX] GBM device created");
return true;
}
typedef void (*PFN_glMemoryObjectParameterivEXT)(GLuint, GLenum, const GLint*);
static bool importMemoryFd(
PFNGLCREATEMEMORYOBJECTSEXTPROC glCreateMemoryObjectsEXT,
PFNGLIMPORTMEMORYFDEXTPROC glImportMemoryFdEXT,
PFNGLDELETEMEMORYOBJECTSEXTPROC glDeleteMemoryObjectsEXT,
PFN_glMemoryObjectParameterivEXT glMemoryObjectParameterivEXT,
PFNGLTEXSTORAGEMEM2DEXTPROC glTexStorageMem2DEXT,
GLuint& mem_obj, GLuint tex, int dmabuf_fd, uint64_t size,
unsigned w, unsigned h, const char* label)
{
glCreateMemoryObjectsEXT(1, &mem_obj);
// Mark as dedicated allocation (required by some drivers for DMA-BUF)
if (glMemoryObjectParameterivEXT) {
GLint dedicated = GL_TRUE;
glMemoryObjectParameterivEXT(mem_obj, GL_DEDICATED_MEMORY_OBJECT_EXT, &dedicated);
}
// dup() because glImportMemoryFdEXT takes ownership of the fd
int import_fd = dup(dmabuf_fd);
if (import_fd < 0) {
DEBUG("[GLX] dup(dmabuf_fd) failed for %s import", label);
if (glDeleteMemoryObjectsEXT && mem_obj) {
glDeleteMemoryObjectsEXT(1, &mem_obj);
mem_obj = 0;
}
return false;
}
clearGlErrors();
glImportMemoryFdEXT(mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, import_fd);
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
DEBUG("[GLX] glImportMemoryFdEXT failed for %s, GL error=0x%x", label, err);
if (glDeleteMemoryObjectsEXT && mem_obj) {
glDeleteMemoryObjectsEXT(1, &mem_obj);
mem_obj = 0;
}
close(import_fd);
return false;
}
clearGlErrors();
glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, w, h, mem_obj, 0);
err = glGetError();
if (err != GL_NO_ERROR) {
DEBUG("[GLX] glTexStorageMem2DEXT failed for %s, GL error=0x%x (size=%lu, %ux%u)",
label, err, (unsigned long)size, w, h);
if (glDeleteMemoryObjectsEXT && mem_obj) {
glDeleteMemoryObjectsEXT(1, &mem_obj);
mem_obj = 0;
}
return false;
}
DEBUG("[GLX] memory object imported for %s: tex=%u mem=%u size=%lu",
label, tex, mem_obj, (unsigned long)size);
return true;
}
bool RenderAPI_OpenGLGLX::createDMABufBuffer(DMABufBuffer& buf, unsigned w, unsigned h)
{
auto cleanup_failed_buffer = [&]() {
if (buf.vlc_fbo) { glDeleteFramebuffers(1, &buf.vlc_fbo); buf.vlc_fbo = 0; }
if (buf.vlc_tex) { glDeleteTextures(1, &buf.vlc_tex); buf.vlc_tex = 0; }
if (buf.vlc_mem_obj && glDeleteMemoryObjectsEXT) {
glDeleteMemoryObjectsEXT(1, &buf.vlc_mem_obj); buf.vlc_mem_obj = 0;
}
if (buf.dmabuf_fd >= 0) { close(buf.dmabuf_fd); buf.dmabuf_fd = -1; }
if (buf.bo) { gbm_bo_destroy(buf.bo); buf.bo = nullptr; }
buf.stride = 0;
buf.size = 0;
};
buf.bo = gbm_bo_create(m_gbm_device, w, h, GBM_FORMAT_ABGR8888,
GBM_BO_USE_RENDERING | GBM_BO_USE_LINEAR);
if (!buf.bo) {
DEBUG("[GLX] gbm_bo_create failed %ux%u", w, h);
return false;
}
buf.dmabuf_fd = gbm_bo_get_fd(buf.bo);
if (buf.dmabuf_fd < 0) {
DEBUG("[GLX] gbm_bo_get_fd failed");
gbm_bo_destroy(buf.bo);
buf.bo = nullptr;
return false;
}
buf.stride = gbm_bo_get_stride(buf.bo);
// Get true DMA-BUF allocation size via lseek (stride*height may be too small)
off_t real_size = lseek(buf.dmabuf_fd, 0, SEEK_END);
lseek(buf.dmabuf_fd, 0, SEEK_SET);
if (real_size <= 0) {
// Fallback to stride * height
buf.size = (uint64_t)buf.stride * h;
DEBUG("[GLX] lseek failed, using stride*height=%lu", (unsigned long)buf.size);
} else {
buf.size = (uint64_t)real_size;
}
DEBUG("[GLX] DMA-BUF: fd=%d stride=%u size=%lu %ux%u",
buf.dmabuf_fd, buf.stride, (unsigned long)buf.size, w, h);
// Import into VLC's GL context
glGenTextures(1, &buf.vlc_tex);
glBindTexture(GL_TEXTURE_2D, buf.vlc_tex);
if (!importMemoryFd(glCreateMemoryObjectsEXT, glImportMemoryFdEXT, glDeleteMemoryObjectsEXT,
glMemoryObjectParameterivEXT, glTexStorageMem2DEXT,
buf.vlc_mem_obj, buf.vlc_tex, buf.dmabuf_fd, buf.size,
w, h, "VLC")) {
cleanup_failed_buffer();
return false;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Create FBO for VLC rendering
glGenFramebuffers(1, &buf.vlc_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, buf.vlc_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, buf.vlc_tex, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
DEBUG("[GLX] DMA-BUF FBO incomplete, status=0x%x", status);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
cleanup_failed_buffer();
return false;
}
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
DEBUG("[GLX] DMA-BUF buffer created: vlc_tex=%u vlc_fbo=%u", buf.vlc_tex, buf.vlc_fbo);
return true;
}
bool RenderAPI_OpenGLGLX::importDMABufToUnityContext(DMABufBuffer& buf, unsigned w, unsigned h)
{
// Use raw GL function pointers to bypass Unity's GL wrapper.
raw_glGenTextures(1, &buf.unity_tex);
raw_glBindTexture(GL_TEXTURE_2D, buf.unity_tex);
if (!importMemoryFd(glCreateMemoryObjectsEXT, glImportMemoryFdEXT, glDeleteMemoryObjectsEXT,
glMemoryObjectParameterivEXT, glTexStorageMem2DEXT,
buf.unity_mem_obj, buf.unity_tex, buf.dmabuf_fd, buf.size,
w, h, "Unity")) {
if (buf.unity_tex) { raw_glDeleteTextures(1, &buf.unity_tex); buf.unity_tex = 0; }
buf.unity_mem_obj = 0;
return false;
}
raw_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
raw_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
raw_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
raw_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
raw_glBindTexture(GL_TEXTURE_2D, 0);
DEBUG("[GLX] DMA-BUF imported to Unity context: unity_tex=%u", buf.unity_tex);
return true;
}
void RenderAPI_OpenGLGLX::releaseDMABufResources()
{
// Release VLC-side GL resources (need VLC context current)
if (m_context) {
GLXContext prev_ctx = glXGetCurrentContext();
GLXDrawable prev_draw = glXGetCurrentDrawable();
GLXDrawable prev_read = glXGetCurrentReadDrawable();
makeCurrent(true);
for (auto& buf : m_dmabuf_buffers) {
if (buf.fence) { glDeleteSync(buf.fence); buf.fence = nullptr; }
if (buf.vlc_fbo) { glDeleteFramebuffers(1, &buf.vlc_fbo); buf.vlc_fbo = 0; }
if (buf.vlc_tex) { glDeleteTextures(1, &buf.vlc_tex); buf.vlc_tex = 0; }
if (buf.vlc_mem_obj && glDeleteMemoryObjectsEXT) {
glDeleteMemoryObjectsEXT(1, &buf.vlc_mem_obj); buf.vlc_mem_obj = 0;
}
}
glXMakeContextCurrent(m_display, prev_draw, prev_read, prev_ctx);
}
for (auto& buf : m_dmabuf_buffers) {
buf.unity_tex = 0;
buf.unity_mem_obj = 0;
if (buf.dmabuf_fd >= 0) { close(buf.dmabuf_fd); buf.dmabuf_fd = -1; }
if (buf.bo) { gbm_bo_destroy(buf.bo); buf.bo = nullptr; }
buf.stride = 0;
buf.size = 0;
}
m_unity_textures_imported = false;
m_dmabuf_width = 0;
m_dmabuf_height = 0;
if (m_gbm_device) { gbm_device_destroy(m_gbm_device); m_gbm_device = nullptr; }
if (m_drm_fd >= 0) { close(m_drm_fd); m_drm_fd = -1; }
}
// --- DMA-BUF VLC callbacks ---
bool RenderAPI_OpenGLGLX::dmabuf_setup(void** opaque,
const libvlc_video_setup_device_cfg_t* cfg,
libvlc_video_setup_device_info_t* out)
{
(void)cfg; (void)out;
DEBUG("[GLX] DMA-BUF output callback setup");
auto* that = static_cast<RenderAPI_OpenGLGLX*>(*opaque);
that->m_dmabuf_width = 0;
that->m_dmabuf_height = 0;
#if defined(SHOW_WATERMARK)
that->makeCurrent(true);
bool ok = that->watermark.setup();
that->makeCurrent(false);
return ok;
#else
return true;
#endif
}
void RenderAPI_OpenGLGLX::dmabuf_cleanup(void* opaque)
{
DEBUG("[GLX] DMA-BUF output callback cleanup");
auto* that = static_cast<RenderAPI_OpenGLGLX*>(opaque);
that->makeCurrent(true);
for (auto& buf : that->m_dmabuf_buffers) {
if (buf.fence) { glDeleteSync(buf.fence); buf.fence = nullptr; }
if (buf.vlc_fbo) { glDeleteFramebuffers(1, &buf.vlc_fbo); buf.vlc_fbo = 0; }
if (buf.vlc_tex) { glDeleteTextures(1, &buf.vlc_tex); buf.vlc_tex = 0; }
if (buf.vlc_mem_obj && that->glDeleteMemoryObjectsEXT) {
that->glDeleteMemoryObjectsEXT(1, &buf.vlc_mem_obj); buf.vlc_mem_obj = 0;
}
}
#if defined(SHOW_WATERMARK)
that->watermark.cleanup();
#endif
that->makeCurrent(false);
}
bool RenderAPI_OpenGLGLX::dmabuf_resize(void* opaque,
const libvlc_video_render_cfg_t* cfg,
libvlc_video_output_cfg_t* output)
{
auto* that = static_cast<RenderAPI_OpenGLGLX*>(opaque);
DEBUG("[GLX] DMA-BUF resize %ux%u", cfg->width, cfg->height);
that->makeCurrent(true);
bool ok = true;
{
std::lock_guard<std::mutex> lock(that->m_dmabuf_lock);
if (cfg->width != that->m_dmabuf_width || cfg->height != that->m_dmabuf_height) {
for (auto& buf : that->m_dmabuf_buffers) {
if (buf.fence) { glDeleteSync(buf.fence); buf.fence = nullptr; }
if (buf.vlc_fbo) { glDeleteFramebuffers(1, &buf.vlc_fbo); buf.vlc_fbo = 0; }
if (buf.vlc_tex) { glDeleteTextures(1, &buf.vlc_tex); buf.vlc_tex = 0; }
if (buf.vlc_mem_obj && that->glDeleteMemoryObjectsEXT) {
that->glDeleteMemoryObjectsEXT(1, &buf.vlc_mem_obj); buf.vlc_mem_obj = 0;
}
if (buf.dmabuf_fd >= 0) { close(buf.dmabuf_fd); buf.dmabuf_fd = -1; }
if (buf.bo) { gbm_bo_destroy(buf.bo); buf.bo = nullptr; }
buf.unity_tex = 0;
buf.unity_mem_obj = 0;
}
// Create new buffers
for (size_t i = 0; i < kDMABufSlots; i++) {
if (!that->createDMABufBuffer(that->m_dmabuf_buffers[i], cfg->width, cfg->height)) {
DEBUG("[GLX] DMA-BUF buffer creation failed for slot %zu", i);
ok = false;
break;
}
}
if (ok) {
that->m_dmabuf_width = cfg->width;
that->m_dmabuf_height = cfg->height;
that->m_unity_textures_imported = false;
}
}
if (ok) {
that->m_idx_render = 0;
that->m_idx_swap = 1;
that->m_idx_display = 2;
that->m_updated = false;
glBindFramebuffer(GL_FRAMEBUFFER, that->m_dmabuf_buffers[that->m_idx_render].vlc_fbo);
}
}
if (ok) {
output->opengl_format = GL_RGBA;
output->full_range = true;
output->colorspace = libvlc_video_colorspace_BT709;
output->primaries = libvlc_video_primaries_BT709;
output->transfer = libvlc_video_transfer_func_SRGB;
output->orientation = libvlc_video_orient_bottom_right;
}
that->makeCurrent(false);
return ok;
}
void RenderAPI_OpenGLGLX::dmabuf_swap(void* opaque)
{
auto* that = static_cast<RenderAPI_OpenGLGLX*>(opaque);
std::lock_guard<std::mutex> lock(that->m_dmabuf_lock);
#if defined(SHOW_WATERMARK)
if (that->m_dmabuf_width > 0 && that->m_dmabuf_height > 0) {
that->watermark.draw(that->m_dmabuf_buffers[that->m_idx_render].vlc_fbo,
that->m_dmabuf_width, that->m_dmabuf_height);
}
#endif
auto& rendered = that->m_dmabuf_buffers[that->m_idx_render];
if (rendered.fence) {
glDeleteSync(rendered.fence);
rendered.fence = nullptr;
}
rendered.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
glFlush();
that->m_updated = true;
std::swap(that->m_idx_swap, that->m_idx_render);
glBindFramebuffer(GL_FRAMEBUFFER, that->m_dmabuf_buffers[that->m_idx_render].vlc_fbo);
}
void* RenderAPI_OpenGLGLX::getVideoFrame(unsigned, unsigned, bool* out_updated)
{
std::lock_guard<std::mutex> lock(m_dmabuf_lock);
if (out_updated)
*out_updated = false;
if (m_dmabuf_width == 0 || m_dmabuf_height == 0)
return nullptr;
// Textures not yet imported by render thread
if (!m_unity_textures_imported)
return nullptr;
if (m_updated) {
std::swap(m_idx_swap, m_idx_display);
m_updated = false;
if (out_updated)
*out_updated = true;
}
auto& display = m_dmabuf_buffers[m_idx_display];
if (display.fence) {
GLenum r = glClientWaitSync(display.fence, GL_SYNC_FLUSH_COMMANDS_BIT, 16000000);
if (r == GL_ALREADY_SIGNALED || r == GL_CONDITION_SATISFIED) {
glDeleteSync(display.fence);
display.fence = nullptr;
} else {
DEBUG("[GLX] glClientWaitSync did not signal (r=0x%x), skipping frame", r);
if (out_updated)
*out_updated = false;
return nullptr;
}
}
return (void*)(size_t)display.unity_tex;
}