-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DRMContext.cpp
872 lines (712 loc) · 24.6 KB
/
DRMContext.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
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2020 Andrew Mickelson
// 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/DRM/DRMContext.hpp>
#include <SFML/Window/DRM/WindowImplDRM.hpp>
#include <SFML/System/Err.hpp>
#include <SFML/System/Sleep.hpp>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <xf86drm.h>
// We check for this definition in order to avoid multiple definitions of GLAD
// entities during unity builds of SFML.
#ifndef SF_GLAD_EGL_IMPLEMENTATION_INCLUDED
#define SF_GLAD_EGL_IMPLEMENTATION_INCLUDED
#define SF_GLAD_EGL_IMPLEMENTATION
#include <glad/egl.h>
#endif
namespace
{
struct DrmFb
{
gbm_bo* bo;
Uint32 fbId;
};
bool initialized = false;
sf::priv::Drm drmNode;
drmEventContext drmEventCtx;
pollfd pollFD;
gbm_device* gbmDevice = NULL;
int contextCount = 0;
EGLDisplay display = EGL_NO_DISPLAY;
int waitingForFlip = 0;
static void pageFlipHandler(int fd, unsigned int frame,
unsigned int sec, unsigned int usec, void* data)
{
// suppress unused param warning
(void)fd, (void)frame, (void)sec, (void)usec;
int* temp = static_cast<int*>(data);
*temp = 0;
}
static bool waitForFlip(int timeout)
{
while (waitingForFlip)
{
pollFD.revents = 0;
if (poll(&pollFD, 1, timeout) < 0)
return false;
if (pollFD.revents & (POLLHUP | POLLERR))
return false;
if (pollFD.revents & POLLIN)
{
drmHandleEvent(drmNode.fd, &drmEventCtx);
}
else
{
return false;
}
}
return true;
}
void cleanup()
{
if (!initialized)
return;
drmModeSetCrtc(drmNode.fd,
drmNode.originalCrtc->crtc_id,
drmNode.originalCrtc->buffer_id,
drmNode.originalCrtc->x,
drmNode.originalCrtc->y,
&drmNode.connectorId,
1,
&drmNode.originalCrtc->mode);
drmModeFreeConnector(drmNode.savedConnector);
drmModeFreeEncoder(drmNode.savedEncoder);
drmModeFreeCrtc(drmNode.originalCrtc);
eglTerminate(display);
display = EGL_NO_DISPLAY;
gbm_device_destroy(gbmDevice);
gbmDevice = NULL;
close(drmNode.fd);
drmNode.fd = -1;
drmNode.mode = 0;
std::memset(&pollFD, 0, sizeof(pollfd));
std::memset(&drmEventCtx, 0, sizeof(drmEventContext));
waitingForFlip = 0;
initialized = false;
}
void drmFbDestroyCallback(gbm_bo* bo, void* data)
{
int drmFd = gbm_device_get_fd(gbm_bo_get_device(bo));
sf::priv::DrmFb* fb = static_cast<sf::priv::DrmFb*>(data);
if (fb->fbId)
drmModeRmFB(drmFd, fb->fbId);
delete fb;
}
sf::priv::DrmFb* drmFbGetFromBo(gbm_bo& bo)
{
int drmFd = gbm_device_get_fd(gbm_bo_get_device(&bo));
sf::priv::DrmFb* fb = static_cast<sf::priv::DrmFb*>(gbm_bo_get_user_data(&bo));
if (fb)
return fb;
fb = new sf::priv::DrmFb();
fb->bo = &bo;
const sf::Uint32 width = gbm_bo_get_width(&bo);
const sf::Uint32 height = gbm_bo_get_height(&bo);
const sf::Uint32 format = gbm_bo_get_format(&bo);
sf::Uint32 strides[4] = {0};
sf::Uint32 handles[4] = {0};
sf::Uint32 offsets[4] = {0};
uint64_t modifiers[4] = {0};
modifiers[0] = gbm_bo_get_modifier(&bo);
const int num_planes = gbm_bo_get_plane_count(&bo);
for (int i = 0; i < num_planes; ++i)
{
strides[i] = gbm_bo_get_stride_for_plane(&bo, i);
handles[i] = gbm_bo_get_handle(&bo).u32;
offsets[i] = gbm_bo_get_offset(&bo, i);
modifiers[i] = modifiers[0];
}
sf::Uint32 flags = 0;
if (modifiers[0])
{
flags = DRM_MODE_FB_MODIFIERS;
}
int ret = drmModeAddFB2WithModifiers(drmFd, width, height, format, handles, strides, offsets, modifiers, &fb->fbId, flags);
if (ret)
{
std::memset(handles, 0, 16);
handles[0] = gbm_bo_get_handle(&bo).u32;
std::memset(strides, 0, 16);
strides[0] = gbm_bo_get_stride(&bo);
std::memset(offsets, 0, 16);
ret = drmModeAddFB2(drmFd, width, height, format, handles, strides, offsets, &fb->fbId, 0);
}
if (ret)
{
sf::err() << "Failed to create fb: " << std::strerror(errno) << std::endl;
delete fb;
return NULL;
}
gbm_bo_set_user_data(&bo, fb, drmFbDestroyCallback);
return fb;
}
sf::Uint32 findCrtcForEncoder(const drmModeRes& resources, const drmModeEncoder& encoder)
{
for (int i = 0; i < resources.count_crtcs; ++i)
{
// Possible_crtcs is a bitmask as described here:
// https://dvdhrm.wordpress.com/2012/09/13/linux-drm-mode-setting-api
const sf::Uint32 crtcMask = 1U << i;
const sf::Uint32 crtcId = resources.crtcs[i];
if (encoder.possible_crtcs & crtcMask)
{
return crtcId;
}
}
// No match found
return 0;
}
sf::Uint32 findCrtcForConnector(const sf::priv::Drm& drm, const drmModeRes& resources, const drmModeConnector& connector)
{
for (int i = 0; i < connector.count_encoders; ++i)
{
const sf::Uint32 encoderId = connector.encoders[i];
const drmModeEncoderPtr encoder = drmModeGetEncoder(drm.fd, encoderId);
if (encoder)
{
const sf::Uint32 crtcId = findCrtcForEncoder(resources, *encoder);
drmModeFreeEncoder(encoder);
if (crtcId != 0)
{
return crtcId;
}
}
}
// No match found
return 0;
}
int getResources(int fd, drmModeResPtr& resources)
{
resources = drmModeGetResources(fd);
if (resources == NULL)
return -1;
return 0;
}
int hasMonitorConnected(int fd, drmModeRes& resources)
{
drmModeConnectorPtr connector;
for (int i = 0; i < resources.count_connectors; ++i)
{
connector = drmModeGetConnector(fd, resources.connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED)
{
// There is a monitor connected
drmModeFreeConnector(connector);
connector = NULL;
return 1;
}
drmModeFreeConnector(connector);
connector = NULL;
}
return 0;
}
int findDrmDevice(drmModeResPtr& resources)
{
static const int maxDrmDevices = 64;
drmDevicePtr devices[maxDrmDevices] = { NULL };
const int numDevices = drmGetDevices2(0, devices, maxDrmDevices);
if (numDevices < 0)
{
sf::err() << "drmGetDevices2 failed: " << std::strerror(-numDevices) << std::endl;
return -1;
}
int fd = -1;
for (int i = 0; i < numDevices; ++i)
{
drmDevicePtr device = devices[i];
int ret;
if (!(device->available_nodes & (1 << DRM_NODE_PRIMARY)))
continue;
// OK, it's a primary device. If we can get the drmModeResources, it means it's also a KMS-capable device.
fd = open(device->nodes[DRM_NODE_PRIMARY], O_RDWR);
if (fd < 0)
continue;
ret = getResources(fd, resources);
#ifdef SFML_DEBUG
sf::err() << "DRM device used: " << i << std::endl;
#endif
if(!ret && hasMonitorConnected(fd, *resources) != 0)
break;
close(fd);
fd = -1;
}
drmFreeDevices(devices, numDevices);
if (fd < 0)
sf::err() << "No drm device found!" << std::endl;
return fd;
}
int initDrm(sf::priv::Drm& drm, const char* device, const char* modeStr, unsigned int vrefresh)
{
drmModeResPtr resources;
if (device)
{
drm.fd = open(device, O_RDWR);
const int ret = getResources(drm.fd, resources);
if (ret < 0 && errno == EOPNOTSUPP)
sf::err() << device << " does not look like a modeset device" << std::endl;
}
else
{
drm.fd = findDrmDevice(resources);
}
if (drm.fd < 0)
{
sf::err() << "Could not open drm device" << std::endl;
return -1;
}
if (!resources)
{
sf::err() << "drmModeGetResources failed: " << std::strerror(errno) << std::endl;
return -1;
}
// Find a connected connector:
drmModeConnectorPtr connector = NULL;
for (int i = 0; i < resources->count_connectors; ++i)
{
connector = drmModeGetConnector(drm.fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED)
{
// It's connected, let's use this!
break;
}
drmModeFreeConnector(connector);
connector = NULL;
}
if (!connector)
{
// We could be fancy and listen for hotplug events and wait for a connector..
sf::err() << "No connected connector!" << std::endl;
return -1;
}
// Find user requested mode:
if (modeStr && *modeStr)
{
for (int i = 0; i < connector->count_modes; ++i)
{
drmModeModeInfoPtr currentMode = &connector->modes[i];
if (std::strcmp(currentMode->name, modeStr) == 0)
{
if (vrefresh == 0 || currentMode->vrefresh == vrefresh)
{
drm.mode = currentMode;
break;
}
}
}
if (!drm.mode)
sf::err() << "Requested mode not found, using default mode!" << std::endl;
}
// Find encoder:
drmModeEncoderPtr encoder = NULL;
for (int i = 0; i < resources->count_encoders; ++i)
{
encoder = drmModeGetEncoder(drm.fd, resources->encoders[i]);
if (encoder->encoder_id == connector->encoder_id)
break;
drmModeFreeEncoder(encoder);
encoder = NULL;
}
if (encoder)
{
drm.crtcId = encoder->crtc_id;
}
else
{
const sf::Uint32 crtcId = findCrtcForConnector(drm, *resources, *connector);
if (crtcId == 0)
{
sf::err() << "No crtc found!" << std::endl;
return -1;
}
drm.crtcId = crtcId;
}
drmModeFreeResources(resources);
drm.connectorId = connector->connector_id;
drm.savedConnector = connector;
drm.savedEncoder = encoder;
// Get original display mode so we can restore display mode after program exits
drm.originalCrtc = drmModeGetCrtc(drm.fd, drm.crtcId);
// Let's use the current mode rather than the preferred one if the user didn't specify a mode with env vars
if (!drm.mode)
{
#ifdef SFML_DEBUG
sf::err() << "DRM using the current mode" << std::endl;
#endif
drm.mode = &(drm.originalCrtc->mode);
}
#ifdef SFML_DEBUG
sf::err() << "DRM Mode used: " << drm.mode->name << "@" << drm.mode->vrefresh << std::endl;
#endif
return 0;
}
void checkInit()
{
if (initialized)
return;
// Use environment variable "SFML_DRM_DEVICE" (or NULL if not set)
char* deviceString = std::getenv("SFML_DRM_DEVICE");
if (deviceString && !*deviceString)
deviceString = NULL;
// Use environment variable "SFML_DRM_MODE" (or NULL if not set)
char* modeString = std::getenv("SFML_DRM_MODE");
// Use environment variable "SFML_DRM_REFRESH" (or 0 if not set)
// Use in combination with mode to request specific refresh rate for the mode
// if multiple refresh rates for same mode might be supported
unsigned int refreshRate = 0;
char* refreshString = std::getenv("SFML_DRM_REFRESH");
if (refreshString)
refreshRate = static_cast<unsigned int>(atoi(refreshString));
if (initDrm(drmNode,
deviceString, // device
modeString, // requested mode
refreshRate) < 0) // screen refresh rate
{
sf::err() << "Error initializing DRM" << std::endl;
return;
}
gbmDevice = gbm_create_device(drmNode.fd);
std::atexit(cleanup);
initialized = true;
pollFD.fd = drmNode.fd;
pollFD.events = POLLIN;
drmEventCtx.version = 2;
drmEventCtx.page_flip_handler = pageFlipHandler;
}
EGLDisplay getInitializedDisplay()
{
checkInit();
if (display == EGL_NO_DISPLAY)
{
gladLoaderLoadEGL(EGL_NO_DISPLAY);
eglCheck(display = eglGetDisplay(reinterpret_cast<EGLNativeDisplayType>(gbmDevice)));
EGLint major, minor;
eglCheck(eglInitialize(display, &major, &minor));
gladLoaderLoadEGL(display);
#if defined(SFML_OPENGL_ES)
if (!eglBindAPI(EGL_OPENGL_ES_API))
{
sf::err() << "failed to bind api EGL_OPENGL_ES_API" << std::endl;
}
#else
if (!eglBindAPI(EGL_OPENGL_API))
{
sf::err() << "failed to bind api EGL_OPENGL_API" << std::endl;
}
#endif
}
return display;
}
}
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
DRMContext::DRMContext(DRMContext* shared) :
m_display (EGL_NO_DISPLAY),
m_context (EGL_NO_CONTEXT),
m_surface (EGL_NO_SURFACE),
m_config (NULL),
m_currentBO (NULL),
m_nextBO (NULL),
m_gbmSurface (NULL),
m_width (0),
m_height (0),
m_shown (false),
m_scanOut (false)
{
contextCount++;
// Get the initialized EGL display
m_display = getInitializedDisplay();
// Get the best EGL config matching the default video settings
m_config = getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, ContextSettings());
updateSettings();
// Create EGL context
createContext(shared);
if (shared)
createSurface(shared->m_width, shared->m_height, VideoMode::getDesktopMode().bitsPerPixel, false);
else // create a surface to force the GL to initialize (seems to be required for glGetString() etc )
createSurface(1, 1, VideoMode::getDesktopMode().bitsPerPixel, false);
}
////////////////////////////////////////////////////////////
DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel) :
m_display (EGL_NO_DISPLAY),
m_context (EGL_NO_CONTEXT),
m_surface (EGL_NO_SURFACE),
m_config (NULL),
m_currentBO (NULL),
m_nextBO (NULL),
m_gbmSurface (NULL),
m_width (0),
m_height (0),
m_shown (false),
m_scanOut (false)
{
contextCount++;
// Get the initialized EGL display
m_display = getInitializedDisplay();
// Get the best EGL config matching the requested video settings
m_config = getBestConfig(m_display, bitsPerPixel, settings);
updateSettings();
// Create EGL context
createContext(shared);
if (owner)
{
Vector2u size = owner->getSize();
createSurface(size.x, size.y, bitsPerPixel, true);
}
}
////////////////////////////////////////////////////////////
DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, unsigned int width, unsigned int height) :
m_display (EGL_NO_DISPLAY),
m_context (EGL_NO_CONTEXT),
m_surface (EGL_NO_SURFACE),
m_config (NULL),
m_currentBO (NULL),
m_nextBO (NULL),
m_gbmSurface (NULL),
m_width (0),
m_height (0),
m_shown (false),
m_scanOut (false)
{
contextCount++;
// Get the initialized EGL display
m_display = getInitializedDisplay();
// Get the best EGL config matching the requested video settings
m_config = getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, settings);
updateSettings();
// Create EGL context
createContext(shared);
createSurface(width, height, VideoMode::getDesktopMode().bitsPerPixel, false);
}
////////////////////////////////////////////////////////////
DRMContext::~DRMContext()
{
// Deactivate the current context
EGLContext currentContext;
eglCheck(currentContext = eglGetCurrentContext());
if (currentContext == m_context)
{
eglCheck(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
}
// Destroy context
if (m_context != EGL_NO_CONTEXT)
{
eglCheck(eglDestroyContext(m_display, m_context));
m_context = EGL_NO_CONTEXT;
}
// Destroy surface
if (m_surface != EGL_NO_SURFACE)
{
eglCheck(eglDestroySurface(m_display, m_surface));
m_surface = EGL_NO_SURFACE;
}
if (m_currentBO)
gbm_surface_release_buffer(m_gbmSurface, m_currentBO);
if (m_nextBO)
gbm_surface_release_buffer(m_gbmSurface, m_nextBO);
if (m_gbmSurface)
gbm_surface_destroy(m_gbmSurface);
contextCount--;
if (contextCount == 0)
cleanup();
}
////////////////////////////////////////////////////////////
bool DRMContext::makeCurrent(bool current)
{
const EGLSurface surface = current ? m_surface : EGL_NO_SURFACE;
const EGLContext context = current ? m_context : EGL_NO_CONTEXT;
return m_surface != EGL_NO_SURFACE && eglMakeCurrent(m_display, surface, surface, context);
}
////////////////////////////////////////////////////////////
void DRMContext::display()
{
if (m_surface == EGL_NO_SURFACE)
return;
if (!m_scanOut)
{
eglCheck(eglSwapBuffers(m_display, m_surface));
return;
}
// Handle display of buffer to the screen
DrmFb* fb = NULL;
if (!waitForFlip(-1))
return;
if (m_currentBO)
{
gbm_surface_release_buffer(m_gbmSurface, m_currentBO);
m_currentBO = NULL;
}
eglCheck(eglSwapBuffers(m_display, m_surface));
m_currentBO = m_nextBO;
// This call must be preceeded by a single call to eglSwapBuffers()
m_nextBO = gbm_surface_lock_front_buffer(m_gbmSurface);
if (!m_nextBO)
return;
fb = drmFbGetFromBo(*m_nextBO);
if (!fb)
{
err() << "Failed to get FB from buffer object" << std::endl;
return;
}
// If first time, need to first call drmModeSetCrtc()
if (!m_shown)
{
if (drmModeSetCrtc(drmNode.fd, drmNode.crtcId, fb->fbId, 0, 0, &drmNode.connectorId, 1, drmNode.mode))
{
err() << "Failed to set mode: " << std::strerror(errno) << std::endl;
std::abort();
}
m_shown = true;
}
// Do page flip
if (!drmModePageFlip(drmNode.fd, drmNode.crtcId, fb->fbId, DRM_MODE_PAGE_FLIP_EVENT, &waitingForFlip))
waitingForFlip = 1;
}
////////////////////////////////////////////////////////////
void DRMContext::setVerticalSyncEnabled(bool enabled)
{
eglCheck(eglSwapInterval(m_display, enabled ? 1 : 0));
}
////////////////////////////////////////////////////////////
void DRMContext::createContext(DRMContext* shared)
{
const EGLint contextVersion[] =
{
EGL_CONTEXT_CLIENT_VERSION, 1,
EGL_NONE
};
EGLContext toShared;
if (shared)
toShared = shared->m_context;
else
toShared = EGL_NO_CONTEXT;
if (toShared != EGL_NO_CONTEXT)
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
// Create EGL context
eglCheck(m_context = eglCreateContext(m_display, m_config, toShared, contextVersion));
if (m_context == EGL_NO_CONTEXT)
err() << "Failed to create EGL context" << std::endl;
}
////////////////////////////////////////////////////////////
void DRMContext::createSurface(unsigned int width, unsigned int height, unsigned int /*bpp*/, bool scanout)
{
sf::Uint32 flags = GBM_BO_USE_RENDERING;
m_scanOut = scanout;
if (m_scanOut)
flags |= GBM_BO_USE_SCANOUT;
m_gbmSurface = gbm_surface_create(
gbmDevice,
width,
height,
GBM_FORMAT_ARGB8888,
flags);
if (!m_gbmSurface)
{
err() << "Failed to create gbm surface." << std::endl;
return;
}
m_width = width;
m_height = height;
eglCheck(m_surface = eglCreateWindowSurface(m_display, m_config, reinterpret_cast<EGLNativeWindowType>(m_gbmSurface), NULL));
if (m_surface == EGL_NO_SURFACE)
{
err() << "Failed to create EGL Surface" << std::endl;
}
}
////////////////////////////////////////////////////////////
void DRMContext::destroySurface()
{
eglCheck(eglDestroySurface(m_display, m_surface));
m_surface = EGL_NO_SURFACE;
gbm_surface_destroy(m_gbmSurface);
m_gbmSurface = NULL;
// Ensure that this context is no longer active since our surface is now destroyed
setActive(false);
}
////////////////////////////////////////////////////////////
EGLConfig DRMContext::getBestConfig(EGLDisplay display, unsigned int bitsPerPixel, const ContextSettings& settings)
{
// Set our video settings constraint
const EGLint attributes[] =
{
EGL_BUFFER_SIZE, static_cast<EGLint>(bitsPerPixel),
EGL_DEPTH_SIZE, static_cast<EGLint>(settings.depthBits),
EGL_STENCIL_SIZE, static_cast<EGLint>(settings.stencilBits),
EGL_SAMPLE_BUFFERS, static_cast<EGLint>(settings.antialiasingLevel),
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
#if defined(SFML_OPENGL_ES)
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
#else
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
#endif
EGL_NONE
};
EGLint configCount;
EGLConfig configs[1];
// Ask EGL for the best config matching our video settings
eglCheck(eglChooseConfig(display, attributes, configs, 1, &configCount));
return configs[0];
}
////////////////////////////////////////////////////////////
void DRMContext::updateSettings()
{
EGLint tmp;
// Update the internal context settings with the current config
eglCheck(eglGetConfigAttrib(m_display, m_config, EGL_DEPTH_SIZE, &tmp));
m_settings.depthBits = static_cast<unsigned int>(tmp);
eglCheck(eglGetConfigAttrib(m_display, m_config, EGL_STENCIL_SIZE, &tmp));
m_settings.stencilBits = static_cast<unsigned int>(tmp);
eglCheck(eglGetConfigAttrib(m_display, m_config, EGL_SAMPLES, &tmp));
m_settings.antialiasingLevel = static_cast<unsigned int>(tmp);
m_settings.majorVersion = 1;
m_settings.minorVersion = 1;
m_settings.attributeFlags = ContextSettings::Default;
}
////////////////////////////////////////////////////////////
GlFunctionPointer DRMContext::getFunction(const char* name)
{
return reinterpret_cast<GlFunctionPointer>(eglGetProcAddress(name));
}
////////////////////////////////////////////////////////////
Drm* DRMContext::getDRM()
{
checkInit();
return &drmNode;
}
} // namespace priv
} // namespace sf