Skip to content

Latest commit

 

History

History
1378 lines (1106 loc) · 40.8 KB

SDL_kmsdrmvideo.c

File metadata and controls

1378 lines (1106 loc) · 40.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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
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.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_KMSDRM
/* SDL internals */
#include "../SDL_sysvideo.h"
#include "SDL_syswm.h"
Oct 26, 2017
Oct 26, 2017
29
#include "SDL_hints.h"
Feb 9, 2020
Feb 9, 2020
30
#include "../../events/SDL_events_c.h"
31
32
33
34
35
36
37
38
39
#include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_keyboard_c.h"
#ifdef SDL_INPUT_LINUXEV
#include "../../core/linux/SDL_evdev.h"
#endif
/* KMS/DRM declarations */
#include "SDL_kmsdrmvideo.h"
Aug 22, 2017
Aug 22, 2017
40
#include "SDL_kmsdrmevents.h"
41
42
43
#include "SDL_kmsdrmopengles.h"
#include "SDL_kmsdrmmouse.h"
#include "SDL_kmsdrmdyn.h"
Oct 9, 2018
Oct 9, 2018
44
45
46
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
Feb 9, 2020
Feb 9, 2020
47
#include <poll.h>
Oct 9, 2018
Oct 9, 2018
49
#define KMSDRM_DRI_PATH "/dev/dri/"
50
51
static int
Jul 20, 2020
Jul 20, 2020
52
check_modesetting(int devindex)
Oct 9, 2018
Oct 9, 2018
54
55
56
57
58
SDL_bool available = SDL_FALSE;
char device[512];
int drm_fd;
SDL_snprintf(device, sizeof (device), "%scard%d", KMSDRM_DRI_PATH, devindex);
Jul 20, 2020
Jul 20, 2020
59
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "check_modesetting: probing \"%s\"", device);
Oct 9, 2018
Oct 9, 2018
61
drm_fd = open(device, O_RDWR | O_CLOEXEC);
62
63
64
if (drm_fd >= 0) {
if (SDL_KMSDRM_LoadSymbols()) {
drmModeRes *resources = KMSDRM_drmModeGetResources(drm_fd);
Feb 9, 2020
Feb 9, 2020
65
if (resources) {
Dec 1, 2018
Dec 1, 2018
66
67
68
69
70
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "%scard%d connector, encoder and CRTC counts are: %d %d %d",
KMSDRM_DRI_PATH, devindex,
resources->count_connectors, resources->count_encoders, resources->count_crtcs);
if (resources->count_connectors > 0 && resources->count_encoders > 0 && resources->count_crtcs > 0) {
Jul 20, 2020
Jul 20, 2020
71
72
73
74
75
76
77
78
79
80
81
82
for (int i = 0; i < resources->count_connectors; i++) {
drmModeConnector *conn = KMSDRM_drmModeGetConnector(drm_fd, resources->connectors[i]);
if (!conn) {
continue;
}
if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
available = SDL_TRUE;
}
KMSDRM_drmModeFreeConnector(conn);
Jul 20, 2020
Jul 20, 2020
83
84
85
if (available) {
break;
}
Jul 20, 2020
Jul 20, 2020
86
}
Dec 1, 2018
Dec 1, 2018
87
}
88
89
90
91
92
93
94
95
96
97
KMSDRM_drmModeFreeResources(resources);
}
SDL_KMSDRM_UnloadSymbols();
}
close(drm_fd);
}
return available;
}
Oct 9, 2018
Oct 9, 2018
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
static int get_dricount(void)
{
int devcount = 0;
struct dirent *res;
struct stat sb;
DIR *folder;
if (!(stat(KMSDRM_DRI_PATH, &sb) == 0
&& S_ISDIR(sb.st_mode))) {
printf("The path %s cannot be opened or is not available\n",
KMSDRM_DRI_PATH);
return 0;
}
if (access(KMSDRM_DRI_PATH, F_OK) == -1) {
printf("The path %s cannot be opened\n",
KMSDRM_DRI_PATH);
return 0;
}
folder = opendir(KMSDRM_DRI_PATH);
if (folder) {
while ((res = readdir(folder))) {
Jun 19, 2019
Jun 19, 2019
121
int len = SDL_strlen(res->d_name);
Jun 19, 2019
Jun 19, 2019
122
if (len > 4 && SDL_strncmp(res->d_name, "card", 4) == 0) {
Oct 9, 2018
Oct 9, 2018
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
devcount++;
}
}
closedir(folder);
}
return devcount;
}
static int
get_driindex(void)
{
const int devcount = get_dricount();
int i;
for (i = 0; i < devcount; i++) {
Jul 20, 2020
Jul 20, 2020
139
if (check_modesetting(i)) {
Oct 9, 2018
Oct 9, 2018
140
141
142
143
144
145
146
return i;
}
}
return -ENOENT;
}
Jul 28, 2020
Jul 28, 2020
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
/*********************************/
/* Atomic helper functions block */
/*********************************/
#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
static int add_connector_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
unsigned int i;
int prop_id = 0;
for (i = 0 ; i < dispdata->connector_props->count_props ; i++) {
if (strcmp(dispdata->connector_props_info[i]->name, name) == 0) {
prop_id = dispdata->connector_props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no connector property: %s\n", name);
return -EINVAL;
}
return KMSDRM_drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_crtc_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
unsigned int i;
int prop_id = -1;
for (i = 0 ; i < dispdata->crtc_props->count_props ; i++) {
if (strcmp(dispdata->crtc_props_info[i]->name, name) == 0) {
prop_id = dispdata->crtc_props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no crtc property: %s\n", name);
return -EINVAL;
}
return KMSDRM_drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
static int add_plane_property(drmModeAtomicReq *req, uint32_t obj_id,
const char *name, uint64_t value)
{
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
unsigned int i;
int prop_id = -1;
for (i = 0 ; i < dispdata->plane_props->count_props ; i++) {
if (strcmp(dispdata->plane_props_info[i]->name, name) == 0) {
prop_id = dispdata->plane_props_info[i]->prop_id;
break;
}
}
if (prop_id < 0) {
printf("no plane property: %s\n", name);
return -EINVAL;
}
return KMSDRM_drmModeAtomicAddProperty(req, obj_id, prop_id, value);
}
Aug 5, 2020
Aug 5, 2020
219
220
221
#if 0
static void get_plane_properties() {
Jul 28, 2020
Jul 28, 2020
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
uint32_t i;
dispdata->plane_ = drmModeObjectGetProperties(viddata->drm_fd,
plane->plane_id, DRM_MODE_OBJECT_PLANE);
if (!dispdata->type.props) {
printf("could not get %s %u properties: %s\n",
#type, id, strerror(errno));
return NULL;
}
dispdata->type->props_info = calloc(dispdata->type.props->count_props,
sizeof(*dispdata->type->props_info));
for (i = 0; i < dispdata->type->props->count_props; i++) {
dispdata->type.props_info[i] = drmModeGetProperty(viddata->drm_fd,
dispdata->type->props->props[i]);
}
return props;
Aug 5, 2020
Aug 5, 2020
239
}
Jul 28, 2020
Jul 28, 2020
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
void print_plane_info(_THIS, drmModePlanePtr plane)
{
char *plane_type;
drmModeRes *resources;
uint32_t type = 0;
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
drmModeObjectPropertiesPtr props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
plane->plane_id, DRM_MODE_OBJECT_PLANE);
/* Search the plane props for the plane type. */
for (int j = 0; j < props->count_props; j++) {
drmModePropertyPtr p = KMSDRM_drmModeGetProperty(viddata->drm_fd, props->props[j]);
if ((strcmp(p->name, "type") == 0)) {
type = props->prop_values[j];
}
KMSDRM_drmModeFreeProperty(p);
}
switch (type) {
case DRM_PLANE_TYPE_OVERLAY:
plane_type = "overlay";
break;
case DRM_PLANE_TYPE_PRIMARY:
plane_type = "primary";
break;
case DRM_PLANE_TYPE_CURSOR:
plane_type = "cursor";
break;
}
/* Remember that, to present a plane on screen, it has to be connected to a CRTC so the CRTC scans it,
scales it, etc... and presents it on screen. */
/* Now we look for the CRTCs supported by the plane. */
resources = KMSDRM_drmModeGetResources(viddata->drm_fd);
if (!resources)
return;
printf("--PLANE ID: %d\nPLANE TYPE: %s\nCRTC READING THIS PLANE: %d\nCRTCS SUPPORTED BY THIS PLANE: ", plane->plane_id, plane_type, plane->crtc_id);
for (int i = 0; i < resources->count_crtcs; i++) {
if (plane->possible_crtcs & (1 << i)) {
uint32_t crtc_id = resources->crtcs[i];
printf ("%d", crtc_id);
break;
}
}
printf ("\n\n");
}
void get_planes_info(_THIS)
{
drmModePlaneResPtr plane_resources;
uint32_t i;
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
plane_resources = KMSDRM_drmModeGetPlaneResources(viddata->drm_fd);
if (!plane_resources) {
printf("drmModeGetPlaneResources failed: %s\n", strerror(errno));
return;
}
printf("--Number of planes found: %d-- \n", plane_resources->count_planes);
printf("--Usable CRTC that we have chosen: %d-- \n", dispdata->crtc->crtc_id);
/* Iterate on all the available planes. */
for (i = 0; (i < plane_resources->count_planes); i++) {
uint32_t plane_id = plane_resources->planes[i];
drmModePlanePtr plane = KMSDRM_drmModeGetPlane(viddata->drm_fd, plane_id);
if (!plane) {
printf("drmModeGetPlane(%u) failed: %s\n", plane_id, strerror(errno));
continue;
}
/* Print plane info. */
print_plane_info(_this, plane);
KMSDRM_drmModeFreePlane(plane);
}
KMSDRM_drmModeFreePlaneResources(plane_resources);
}
Aug 5, 2020
Aug 5, 2020
334
#endif
Jul 28, 2020
Jul 28, 2020
335
Aug 5, 2020
Aug 5, 2020
336
337
338
/* Get a plane that is PRIMARY (there's no guarantee that we have overlays in all hardware,
so we can really only count on having one primary plane) and can use the CRTC we have chosen. */
uint32_t get_plane_id(_THIS, drmModeRes *resources)
Jul 28, 2020
Jul 28, 2020
339
340
341
{
drmModePlaneResPtr plane_resources;
uint32_t i, j;
Aug 5, 2020
Aug 5, 2020
342
uint32_t crtc_index = 0;
Jul 28, 2020
Jul 28, 2020
343
344
345
346
347
348
int ret = -EINVAL;
int found_primary = 0;
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
Aug 5, 2020
Aug 5, 2020
349
350
351
352
353
354
355
356
/* Get the crtc_index for the current CRTC. Needed to find out if a plane supports the CRTC. */
for (i = 0; i < resources->count_crtcs; i++) {
if (resources->crtcs[i] == dispdata->crtc_id) {
crtc_index = i;
break;
}
}
Jul 28, 2020
Jul 28, 2020
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
plane_resources = KMSDRM_drmModeGetPlaneResources(viddata->drm_fd);
if (!plane_resources) {
printf("drmModeGetPlaneResources failed: %s\n", strerror(errno));
return -1;
}
/* Iterate on all the available planes. */
for (i = 0; (i < plane_resources->count_planes) && !found_primary; i++) {
uint32_t plane_id = plane_resources->planes[i];
drmModePlanePtr plane = KMSDRM_drmModeGetPlane(viddata->drm_fd, plane_id);
if (!plane) {
printf("drmModeGetPlane(%u) failed: %s\n", plane_id, strerror(errno));
continue;
}
/* See if the current CRTC is available for this plane. */
Aug 5, 2020
Aug 5, 2020
375
if (plane->possible_crtcs & (1 << crtc_index)) {
Jul 28, 2020
Jul 28, 2020
376
Aug 5, 2020
Aug 5, 2020
377
378
drmModeObjectPropertiesPtr props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
plane_id, DRM_MODE_OBJECT_PLANE);
Jul 28, 2020
Jul 28, 2020
379
380
381
382
383
384
385
386
387
388
389
390
391
ret = plane_id;
/* Search the plane props, to see if it's a primary plane. */
for (j = 0; j < props->count_props; j++) {
drmModePropertyPtr p = KMSDRM_drmModeGetProperty(viddata->drm_fd, props->props[j]);
if ((strcmp(p->name, "type") == 0) &&
(props->prop_values[j] == DRM_PLANE_TYPE_PRIMARY)) {
/* found our primary plane, lets use that: */
found_primary = 1;
}
Aug 5, 2020
Aug 5, 2020
392
KMSDRM_drmModeFreeProperty(p);
Jul 28, 2020
Jul 28, 2020
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
}
KMSDRM_drmModeFreeObjectProperties(props);
}
KMSDRM_drmModeFreePlane(plane);
}
KMSDRM_drmModeFreePlaneResources(plane_resources);
return ret;
}
int drm_atomic_commit(_THIS, uint32_t fb_id, uint32_t flags)
{
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
uint32_t plane_id = dispdata->plane->plane_id;
uint32_t blob_id;
drmModeAtomicReq *req;
int ret;
req = KMSDRM_drmModeAtomicAlloc();
if (flags & DRM_MODE_ATOMIC_ALLOW_MODESET) {
if (add_connector_property(req, dispdata->connector->connector_id, "CRTC_ID",
dispdata->crtc_id) < 0)
return -1;
if (KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode),
&blob_id) != 0)
return -1;
if (add_crtc_property(req, dispdata->crtc_id, "MODE_ID", blob_id) < 0)
return -1;
if (add_crtc_property(req, dispdata->crtc_id, "ACTIVE", 1) < 0)
return -1;
}
add_plane_property(req, plane_id, "FB_ID", fb_id);
add_plane_property(req, plane_id, "CRTC_ID", dispdata->crtc_id);
add_plane_property(req, plane_id, "SRC_X", 0);
add_plane_property(req, plane_id, "SRC_Y", 0);
add_plane_property(req, plane_id, "SRC_W", dispdata->mode.hdisplay << 16);
add_plane_property(req, plane_id, "SRC_H", dispdata->mode.vdisplay << 16);
add_plane_property(req, plane_id, "CRTC_X", 0);
add_plane_property(req, plane_id, "CRTC_Y", 0);
add_plane_property(req, plane_id, "CRTC_W", dispdata->mode.hdisplay);
add_plane_property(req, plane_id, "CRTC_H", dispdata->mode.vdisplay);
if (dispdata->kms_in_fence_fd != -1) {
add_crtc_property(req, dispdata->crtc_id, "OUT_FENCE_PTR",
VOID2U64(&dispdata->kms_out_fence_fd));
add_plane_property(req, plane_id, "IN_FENCE_FD", dispdata->kms_in_fence_fd);
}
ret = KMSDRM_drmModeAtomicCommit(viddata->drm_fd, req, flags, NULL);
if (ret)
goto out;
if (dispdata->kms_in_fence_fd != -1) {
close(dispdata->kms_in_fence_fd);
dispdata->kms_in_fence_fd = -1;
}
out:
KMSDRM_drmModeAtomicFree(req);
return ret;
}
/***************************************/
/* End of Atomic helper functions block*/
/***************************************/
Oct 9, 2018
Oct 9, 2018
471
472
473
474
475
476
477
478
479
480
481
482
static int
KMSDRM_Available(void)
{
int ret = -ENOENT;
ret = get_driindex();
if (ret >= 0)
return 1;
return ret;
}
483
static void
Feb 9, 2020
Feb 9, 2020
484
KMSDRM_DeleteDevice(SDL_VideoDevice * device)
Feb 9, 2020
Feb 9, 2020
486
if (device->driverdata) {
487
488
489
SDL_free(device->driverdata);
device->driverdata = NULL;
}
Aug 2, 2017
Aug 2, 2017
490
491
SDL_free(device);
Feb 9, 2020
Feb 9, 2020
492
493
494
495
496
SDL_KMSDRM_UnloadSymbols();
}
static SDL_VideoDevice *
Feb 9, 2020
Feb 9, 2020
497
KMSDRM_CreateDevice(int devindex)
498
499
{
SDL_VideoDevice *device;
Feb 9, 2020
Feb 9, 2020
500
SDL_VideoData *viddata;
Jul 12, 2020
Jul 12, 2020
502
503
504
505
if (!KMSDRM_Available()) {
return NULL;
}
Oct 9, 2018
Oct 9, 2018
506
507
508
509
510
if (!devindex || (devindex > 99)) {
devindex = get_driindex();
}
if (devindex < 0) {
511
512
513
514
515
516
517
518
519
SDL_SetError("devindex (%d) must be between 0 and 99.\n", devindex);
return NULL;
}
if (!SDL_KMSDRM_LoadSymbols()) {
return NULL;
}
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
Feb 9, 2020
Feb 9, 2020
520
if (!device) {
521
SDL_OutOfMemory();
Aug 2, 2017
Aug 2, 2017
522
return NULL;
Feb 9, 2020
Feb 9, 2020
525
526
viddata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
if (!viddata) {
527
528
529
SDL_OutOfMemory();
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
530
531
viddata->devindex = devindex;
viddata->drm_fd = -1;
Feb 9, 2020
Feb 9, 2020
533
device->driverdata = viddata;
May 26, 2020
May 26, 2020
535
/* Setup all functions that can be handled from this backend. */
536
537
538
539
device->VideoInit = KMSDRM_VideoInit;
device->VideoQuit = KMSDRM_VideoQuit;
device->GetDisplayModes = KMSDRM_GetDisplayModes;
device->SetDisplayMode = KMSDRM_SetDisplayMode;
Aug 28, 2017
Aug 28, 2017
540
541
device->CreateSDLWindow = KMSDRM_CreateWindow;
device->CreateSDLWindowFrom = KMSDRM_CreateWindowFrom;
542
543
544
545
546
547
548
549
550
551
552
553
554
device->SetWindowTitle = KMSDRM_SetWindowTitle;
device->SetWindowIcon = KMSDRM_SetWindowIcon;
device->SetWindowPosition = KMSDRM_SetWindowPosition;
device->SetWindowSize = KMSDRM_SetWindowSize;
device->ShowWindow = KMSDRM_ShowWindow;
device->HideWindow = KMSDRM_HideWindow;
device->RaiseWindow = KMSDRM_RaiseWindow;
device->MaximizeWindow = KMSDRM_MaximizeWindow;
device->MinimizeWindow = KMSDRM_MinimizeWindow;
device->RestoreWindow = KMSDRM_RestoreWindow;
device->SetWindowGrab = KMSDRM_SetWindowGrab;
device->DestroyWindow = KMSDRM_DestroyWindow;
device->GetWindowWMInfo = KMSDRM_GetWindowWMInfo;
Aug 4, 2017
Aug 4, 2017
555
#if SDL_VIDEO_OPENGL_EGL
556
557
558
559
560
561
562
device->GL_LoadLibrary = KMSDRM_GLES_LoadLibrary;
device->GL_GetProcAddress = KMSDRM_GLES_GetProcAddress;
device->GL_UnloadLibrary = KMSDRM_GLES_UnloadLibrary;
device->GL_CreateContext = KMSDRM_GLES_CreateContext;
device->GL_MakeCurrent = KMSDRM_GLES_MakeCurrent;
device->GL_SetSwapInterval = KMSDRM_GLES_SetSwapInterval;
device->GL_GetSwapInterval = KMSDRM_GLES_GetSwapInterval;
Aug 5, 2020
Aug 5, 2020
563
564
565
566
567
568
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE))
device->GL_SwapWindow = KMSDRM_GLES_SwapWindowDB;
else
device->GL_SwapWindow = KMSDRM_GLES_SwapWindow;
569
device->GL_DeleteContext = KMSDRM_GLES_DeleteContext;
Aug 4, 2017
Aug 4, 2017
570
#endif
571
device->PumpEvents = KMSDRM_PumpEvents;
Feb 9, 2020
Feb 9, 2020
572
device->free = KMSDRM_DeleteDevice;
573
574
575
576
return device;
cleanup:
Feb 9, 2020
Feb 9, 2020
577
if (device)
578
SDL_free(device);
Feb 9, 2020
Feb 9, 2020
579
580
if (viddata)
SDL_free(viddata);
581
582
583
584
585
586
return NULL;
}
VideoBootStrap KMSDRM_bootstrap = {
"KMSDRM",
"KMS/DRM Video Driver",
Feb 9, 2020
Feb 9, 2020
587
KMSDRM_CreateDevice
588
589
590
591
592
593
594
595
};
static void
KMSDRM_FBDestroyCallback(struct gbm_bo *bo, void *data)
{
KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)data;
Mar 13, 2019
Mar 13, 2019
596
if (fb_info && fb_info->drm_fd >= 0 && fb_info->fb_id != 0) {
597
598
599
600
KMSDRM_drmModeRmFB(fb_info->drm_fd, fb_info->fb_id);
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Delete DRM FB %u", fb_info->fb_id);
}
Mar 11, 2019
Mar 11, 2019
601
SDL_free(fb_info);
602
603
604
605
606
}
KMSDRM_FBInfo *
KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
{
Feb 9, 2020
Feb 9, 2020
607
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
Feb 15, 2020
Feb 15, 2020
608
609
610
unsigned w,h;
int ret;
Uint32 stride, handle;
Feb 9, 2020
Feb 9, 2020
611
612
613
614
615
/* Check for an existing framebuffer */
KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)KMSDRM_gbm_bo_get_user_data(bo);
if (fb_info) {
616
617
618
return fb_info;
}
Feb 9, 2020
Feb 9, 2020
619
620
/* Create a structure that contains enough info to remove the framebuffer
when the backing buffer is destroyed */
621
fb_info = (KMSDRM_FBInfo *)SDL_calloc(1, sizeof(KMSDRM_FBInfo));
Feb 9, 2020
Feb 9, 2020
622
623
if (!fb_info) {
Aug 5, 2017
Aug 5, 2017
624
625
626
SDL_OutOfMemory();
return NULL;
}
Feb 9, 2020
Feb 9, 2020
628
fb_info->drm_fd = viddata->drm_fd;
Feb 9, 2020
Feb 9, 2020
630
/* Create framebuffer object for the buffer */
Feb 15, 2020
Feb 15, 2020
631
632
633
634
635
w = KMSDRM_gbm_bo_get_width(bo);
h = KMSDRM_gbm_bo_get_height(bo);
stride = KMSDRM_gbm_bo_get_stride(bo);
handle = KMSDRM_gbm_bo_get_handle(bo).u32;
ret = KMSDRM_drmModeAddFB(viddata->drm_fd, w, h, 24, 32, stride, handle,
Feb 9, 2020
Feb 9, 2020
636
637
638
639
&fb_info->fb_id);
if (ret) {
SDL_free(fb_info);
return NULL;
Feb 9, 2020
Feb 9, 2020
641
642
643
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "New DRM FB (%u): %ux%u, stride %u from BO %p",
fb_info->fb_id, w, h, stride, (void *)bo);
644
645
646
/* Associate our DRM framebuffer with this buffer object */
KMSDRM_gbm_bo_set_user_data(bo, fb_info, KMSDRM_FBDestroyCallback);
Feb 9, 2020
Feb 9, 2020
647
648
649
650
651
652
653
654
return fb_info;
}
/*****************************************************************************/
/* SDL Video and Display initialization/handling functions */
/* _this is a SDL_VideoDevice * */
/*****************************************************************************/
Feb 27, 2020
Feb 27, 2020
655
static void
Feb 9, 2020
Feb 9, 2020
656
657
658
659
KMSDRM_DestroySurfaces(_THIS, SDL_Window * window)
{
SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
Aug 5, 2020
Aug 5, 2020
660
661
662
if (windata->bo) {
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
windata->bo = NULL;
Feb 9, 2020
Feb 9, 2020
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
}
if (windata->next_bo) {
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->next_bo);
windata->next_bo = NULL;
}
#if SDL_VIDEO_OPENGL_EGL
SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (windata->egl_surface != EGL_NO_SURFACE) {
SDL_EGL_DestroySurface(_this, windata->egl_surface);
windata->egl_surface = EGL_NO_SURFACE;
}
#endif
if (windata->gs) {
KMSDRM_gbm_surface_destroy(windata->gs);
windata->gs = NULL;
}
}
int
KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
{
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
Uint32 width = dispdata->mode.hdisplay;
Uint32 height = dispdata->mode.vdisplay;
Uint32 surface_fmt = GBM_FORMAT_XRGB8888;
Uint32 surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
Mar 25, 2020
Mar 25, 2020
695
#if SDL_VIDEO_OPENGL_EGL
Feb 15, 2020
Feb 15, 2020
696
EGLContext egl_context;
Mar 25, 2020
Mar 25, 2020
697
#endif
Feb 9, 2020
Feb 9, 2020
698
Jul 20, 2020
Jul 20, 2020
699
if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev, surface_fmt, surface_flags)) {
Feb 9, 2020
Feb 9, 2020
700
701
702
703
704
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "GBM surface format not supported. Trying anyway.");
}
#if SDL_VIDEO_OPENGL_EGL
SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
Feb 15, 2020
Feb 15, 2020
705
egl_context = (EGLContext)SDL_GL_GetCurrentContext();
Feb 9, 2020
Feb 9, 2020
706
707
708
709
#endif
KMSDRM_DestroySurfaces(_this, window);
Jul 20, 2020
Jul 20, 2020
710
windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev, width, height, surface_fmt, surface_flags);
Feb 9, 2020
Feb 9, 2020
711
712
713
714
715
716
717
718
719
720
721
722
723
724
if (!windata->gs) {
return SDL_SetError("Could not create GBM surface");
}
#if SDL_VIDEO_OPENGL_EGL
windata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windata->gs);
if (windata->egl_surface == EGL_NO_SURFACE) {
return SDL_SetError("Could not create EGL window surface");
}
SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
Jul 19, 2020
Jul 19, 2020
725
windata->egl_surface_dirty = SDL_FALSE;
Feb 9, 2020
Feb 9, 2020
726
727
#endif
Aug 5, 2020
Aug 5, 2020
728
729
/* We take note here about the need to do a modeset in the atomic_commit(),
called in KMSDRM_GLES_SwapWindow(). */
Aug 5, 2020
Aug 5, 2020
730
dispdata->modeset_pending = SDL_TRUE;
Jul 19, 2020
Jul 19, 2020
731
Feb 9, 2020
Feb 9, 2020
732
733
734
return 0;
}
735
736
737
738
int
KMSDRM_VideoInit(_THIS)
{
int ret = 0;
Feb 9, 2020
Feb 9, 2020
739
740
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata = NULL;
741
742
drmModeRes *resources = NULL;
drmModeEncoder *encoder = NULL;
Feb 15, 2020
Feb 15, 2020
743
744
char devname[32];
SDL_VideoDisplay display = {0};
Feb 9, 2020
Feb 9, 2020
746
747
748
dispdata = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
if (!dispdata) {
749
750
751
752
753
754
return SDL_OutOfMemory();
}
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoInit()");
/* Open /dev/dri/cardNN */
Feb 9, 2020
Feb 9, 2020
755
756
757
758
SDL_snprintf(devname, sizeof(devname), "/dev/dri/card%d", viddata->devindex);
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opening device %s", devname);
viddata->drm_fd = open(devname, O_RDWR | O_CLOEXEC);
Feb 9, 2020
Feb 9, 2020
760
761
if (viddata->drm_fd < 0) {
ret = SDL_SetError("Could not open %s", devname);
762
763
764
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
765
766
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", viddata->drm_fd);
Jul 20, 2020
Jul 20, 2020
767
768
viddata->gbm_dev = KMSDRM_gbm_create_device(viddata->drm_fd);
if (!viddata->gbm_dev) {
769
770
771
772
ret = SDL_SetError("Couldn't create gbm device.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
773
774
/* Get all of the available connectors / devices / crtcs */
resources = KMSDRM_drmModeGetResources(viddata->drm_fd);
775
if (!resources) {
Feb 9, 2020
Feb 9, 2020
776
ret = SDL_SetError("drmModeGetResources(%d) failed", viddata->drm_fd);
777
778
779
goto cleanup;
}
Jul 28, 2020
Jul 28, 2020
780
/* Iterate on the available connectors to find a connected connector. */
Feb 9, 2020
Feb 9, 2020
781
782
783
784
for (int i = 0; i < resources->count_connectors; i++) {
drmModeConnector *conn = KMSDRM_drmModeGetConnector(viddata->drm_fd, resources->connectors[i]);
if (!conn) {
785
continue;
Feb 9, 2020
Feb 9, 2020
786
}
Feb 9, 2020
Feb 9, 2020
788
if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
789
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.",
Feb 9, 2020
Feb 9, 2020
790
conn->connector_id, conn->count_modes);
Jul 28, 2020
Jul 28, 2020
791
792
dispdata->connector = conn;
dispdata->connector_id = conn->connector_id;
793
794
795
break;
}
Feb 9, 2020
Feb 9, 2020
796
KMSDRM_drmModeFreeConnector(conn);
Jul 28, 2020
Jul 28, 2020
799
if (!dispdata->connector) {
800
801
802
803
ret = SDL_SetError("No currently active connector found.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
804
805
806
/* Try to find the connector's current encoder */
for (int i = 0; i < resources->count_encoders; i++) {
encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
Dec 1, 2018
Dec 1, 2018
807
Feb 9, 2020
Feb 9, 2020
808
809
if (!encoder) {
continue;
Dec 1, 2018
Dec 1, 2018
810
811
}
Jul 28, 2020
Jul 28, 2020
812
if (encoder->encoder_id == dispdata->connector->encoder_id) {
Feb 9, 2020
Feb 9, 2020
813
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
814
815
816
817
break;
}
KMSDRM_drmModeFreeEncoder(encoder);
Aug 11, 2017
Aug 11, 2017
818
encoder = NULL;
Feb 9, 2020
Feb 9, 2020
821
822
823
824
825
826
827
828
829
if (!encoder) {
/* No encoder was connected, find the first supported one */
for (int i = 0, j; i < resources->count_encoders; i++) {
encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
if (!encoder) {
continue;
}
Jul 28, 2020
Jul 28, 2020
830
831
for (j = 0; j < dispdata->connector->count_encoders; j++) {
if (dispdata->connector->encoders[j] == encoder->encoder_id) {
Feb 9, 2020
Feb 9, 2020
832
833
834
835
break;
}
}
Jul 28, 2020
Jul 28, 2020
836
if (j != dispdata->connector->count_encoders) {
Feb 9, 2020
Feb 9, 2020
837
838
839
840
841
842
843
844
845
break;
}
KMSDRM_drmModeFreeEncoder(encoder);
encoder = NULL;
}
}
if (!encoder) {
846
847
848
849
ret = SDL_SetError("No connected encoder found.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
850
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
Dec 1, 2018
Dec 1, 2018
851
Feb 9, 2020
Feb 9, 2020
852
/* Try to find a CRTC connected to this encoder */
Jul 28, 2020
Jul 28, 2020
853
dispdata->crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
Feb 9, 2020
Feb 9, 2020
854
Jul 28, 2020
Jul 28, 2020
855
856
/* If no CRTC was connected to the encoder, find the first CRTC that is supported by the encoder, and use that. */
if (!dispdata->crtc) {
Feb 9, 2020
Feb 9, 2020
857
for (int i = 0; i < resources->count_crtcs; i++) {
Dec 1, 2018
Dec 1, 2018
858
859
if (encoder->possible_crtcs & (1 << i)) {
encoder->crtc_id = resources->crtcs[i];
Jul 28, 2020
Jul 28, 2020
860
dispdata->crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
Dec 1, 2018
Dec 1, 2018
861
862
863
864
865
break;
}
}
}
Jul 28, 2020
Jul 28, 2020
866
if (!dispdata->crtc) {
867
868
869
870
ret = SDL_SetError("No CRTC found.");
goto cleanup;
}
Jul 28, 2020
Jul 28, 2020
871
872
873
874
875
876
877
878
879
880
881
882
/* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Saved crtc_id %u, fb_id %u, (%u,%u), %ux%u",
dispdata->crtc->crtc_id, dispdata->crtc->buffer_id, dispdata->crtc->x,
dispdata->crtc->y, dispdata->crtc->width, dispdata->crtc->height);
*/
dispdata->crtc_id = dispdata->crtc->crtc_id;
dispdata->crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, dispdata->crtc_id);
/****************/
/* Atomic block */
/****************/
Aug 3, 2020
Aug 3, 2020
883
884
885
/* Initialize the fences and their fds: */
dispdata->kms_fence = NULL;
dispdata->gpu_fence = NULL;
Jul 28, 2020
Jul 28, 2020
886
dispdata->kms_out_fence_fd = -1,
Aug 3, 2020
Aug 3, 2020
887
dispdata->kms_in_fence_fd = -1,
Aug 5, 2020
Aug 5, 2020
888
dispdata->modeset_pending = SDL_FALSE;
Jul 28, 2020
Jul 28, 2020
890
891
892
/*********************/
/* Atomic block ends */
/*********************/
Feb 9, 2020
Feb 9, 2020
894
895
/* Figure out the default mode to be set. If the current CRTC's mode isn't
valid, select the first mode supported by the connector
Feb 9, 2020
Feb 9, 2020
897
FIXME find first mode that specifies DRM_MODE_TYPE_PREFERRED */
Jul 28, 2020
Jul 28, 2020
898
dispdata->mode = dispdata->crtc->mode;
Jul 28, 2020
Jul 28, 2020
900
if (dispdata->crtc->mode_valid == 0) {
Feb 9, 2020
Feb 9, 2020
901
902
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
"Current mode is invalid, selecting connector's mode #0.");
Jul 28, 2020
Jul 28, 2020
903
dispdata->mode = dispdata->connector->modes[0];
Feb 9, 2020
Feb 9, 2020
904
905
906
}
/* Setup the single display that's available */
Feb 15, 2020
Feb 15, 2020
907
Feb 9, 2020
Feb 9, 2020
908
909
910
911
912
913
914
display.desktop_mode.w = dispdata->mode.hdisplay;
display.desktop_mode.h = dispdata->mode.vdisplay;
display.desktop_mode.refresh_rate = dispdata->mode.vrefresh;
#if 1
display.desktop_mode.format = SDL_PIXELFORMAT_ARGB8888;
#else
/* FIXME */
Jul 28, 2020
Jul 28, 2020
915
drmModeFB *fb = drmModeGetFB(viddata->drm_fd, dispdata->crtc->buffer_id);
Feb 9, 2020
Feb 9, 2020
916
917
918
display.desktop_mode.format = drmToSDLPixelFormat(fb->bpp, fb->depth);
drmModeFreeFB(fb);
#endif
Jun 2, 2020
Jun 2, 2020
919
920
/* DRM mode index for the desktop mode is needed to complete desktop mode init NOW,
Aug 5, 2020
Aug 5, 2020
921
922
923
so look for it in the DRM modes array.
This is needed because SetDisplayMode() uses the mode index, and some programs
change to fullscreen desktop video mode as they start. */
Jul 28, 2020
Jul 28, 2020
924
925
for (int i = 0; i < dispdata->connector->count_modes; i++) {
if (!SDL_memcmp(dispdata->connector->modes + i, &dispdata->crtc->mode, sizeof(drmModeModeInfo))) {
Jun 2, 2020
Jun 2, 2020
926
927
928
929
SDL_DisplayModeData *modedata = SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (modedata) {
modedata->mode_index = i;
display.desktop_mode.driverdata = modedata;
Aug 5, 2020
Aug 5, 2020
930
931
932
}
}
}
Jun 2, 2020
Jun 2, 2020
933
Feb 9, 2020
Feb 9, 2020
934
935
display.current_mode = display.desktop_mode;
display.driverdata = dispdata;
936
937
SDL_AddVideoDisplay(&display);
Jul 28, 2020
Jul 28, 2020
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/****************/
/* Atomic block */
/****************/
ret = KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_ATOMIC, 1);
if (ret) {
ret = SDL_SetError("no atomic modesetting support.");
goto cleanup;
}
ret = KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
if (ret) {
ret = SDL_SetError("no universal planes support.");
goto cleanup;
}
Aug 5, 2020
Aug 5, 2020
954
dispdata->plane_id = get_plane_id(_this, resources);
Jul 28, 2020
Jul 28, 2020
955
956
957
958
959
960
961
if (!dispdata->plane_id) {
ret = SDL_SetError("could not find a suitable plane.");
goto cleanup;
}
dispdata->plane = KMSDRM_drmModeGetPlane(viddata->drm_fd, dispdata->plane_id);
Aug 5, 2020
Aug 5, 2020
962
963
964
965
/* Use this if you ever need to see info on all available planes. */
#if 0
get_planes_info(_this);
#endif
Jul 28, 2020
Jul 28, 2020
966
967
968
969
970
971
972
973
974
975
/* We only do single plane to single crtc to single connector, no
* fancy multi-monitor or multi-plane stuff. So just grab the
* plane/crtc/connector property info for one of each:
*/
/* Get PLANE properties */
dispdata->plane_props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
dispdata->plane_id, DRM_MODE_OBJECT_PLANE);
Aug 5, 2020
Aug 5, 2020
976
dispdata->plane_props_info = SDL_calloc(dispdata->plane_props->count_props,
Jul 28, 2020
Jul 28, 2020
977
978
979
980
sizeof(dispdata->plane_props_info));
for (int i = 0; i < dispdata->plane_props->count_props; i++) {
dispdata->plane_props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
Aug 5, 2020
Aug 5, 2020
981
dispdata->plane_props->props[i]);
Jul 28, 2020
Jul 28, 2020
982
983
984
985
986
987
}
/* Get CRTC properties */
dispdata->crtc_props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
dispdata->crtc_id, DRM_MODE_OBJECT_CRTC);
Aug 5, 2020
Aug 5, 2020
988
dispdata->crtc_props_info = SDL_calloc(dispdata->crtc_props->count_props,
Jul 28, 2020
Jul 28, 2020
989
990
991
992
993
994
995
996
997
998
999
sizeof(dispdata->crtc_props_info));
for (int i = 0; i < dispdata->crtc_props->count_props; i++) {
dispdata->crtc_props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
dispdata->crtc_props->props[i]);
}
/* Get connector properties */
dispdata->connector_props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
dispdata->connector_id, DRM_MODE_OBJECT_CONNECTOR);
Aug 5, 2020
Aug 5, 2020
1000
dispdata->connector_props_info = SDL_calloc(dispdata->connector_props->count_props,