Skip to content

Latest commit

 

History

History
928 lines (755 loc) · 26.1 KB

SDL_kmsdrmvideo.c

File metadata and controls

928 lines (755 loc) · 26.1 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
29
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"
#include "SDL_log.h"
Oct 26, 2017
Oct 26, 2017
30
#include "SDL_hints.h"
Feb 9, 2020
Feb 9, 2020
31
#include "../../events/SDL_events_c.h"
32
33
34
35
36
37
38
39
40
#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
41
#include "SDL_kmsdrmevents.h"
42
43
44
#include "SDL_kmsdrmopengles.h"
#include "SDL_kmsdrmmouse.h"
#include "SDL_kmsdrmdyn.h"
Oct 9, 2018
Oct 9, 2018
45
46
47
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
Feb 9, 2020
Feb 9, 2020
48
#include <poll.h>
Oct 9, 2018
Oct 9, 2018
50
#define KMSDRM_DRI_PATH "/dev/dri/"
51
52
static int
Oct 9, 2018
Oct 9, 2018
53
check_modestting(int devindex)
Oct 9, 2018
Oct 9, 2018
55
56
57
58
59
SDL_bool available = SDL_FALSE;
char device[512];
int drm_fd;
SDL_snprintf(device, sizeof (device), "%scard%d", KMSDRM_DRI_PATH, devindex);
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
71
72
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) {
available = SDL_TRUE;
}
73
74
75
76
77
78
79
80
81
82
KMSDRM_drmModeFreeResources(resources);
}
SDL_KMSDRM_UnloadSymbols();
}
close(drm_fd);
}
return available;
}
Oct 9, 2018
Oct 9, 2018
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
106
int len = SDL_strlen(res->d_name);
Jun 19, 2019
Jun 19, 2019
107
if (len > 4 && SDL_strncmp(res->d_name, "card", 4) == 0) {
Oct 9, 2018
Oct 9, 2018
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
devcount++;
}
}
closedir(folder);
}
return devcount;
}
static int
get_driindex(void)
{
const int devcount = get_dricount();
int i;
for (i = 0; i < devcount; i++) {
if (check_modestting(i)) {
return i;
}
}
return -ENOENT;
}
static int
KMSDRM_Available(void)
{
int ret = -ENOENT;
ret = get_driindex();
if (ret >= 0)
return 1;
return ret;
}
144
static void
Feb 9, 2020
Feb 9, 2020
145
KMSDRM_DeleteDevice(SDL_VideoDevice * device)
Feb 9, 2020
Feb 9, 2020
147
if (device->driverdata) {
148
149
150
SDL_free(device->driverdata);
device->driverdata = NULL;
}
Aug 2, 2017
Aug 2, 2017
151
152
SDL_free(device);
Feb 9, 2020
Feb 9, 2020
153
154
155
156
157
SDL_KMSDRM_UnloadSymbols();
}
static SDL_VideoDevice *
Feb 9, 2020
Feb 9, 2020
158
KMSDRM_CreateDevice(int devindex)
159
160
{
SDL_VideoDevice *device;
Feb 9, 2020
Feb 9, 2020
161
SDL_VideoData *viddata;
Oct 9, 2018
Oct 9, 2018
163
164
165
166
167
if (!devindex || (devindex > 99)) {
devindex = get_driindex();
}
if (devindex < 0) {
168
169
170
171
172
173
174
175
176
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
177
if (!device) {
178
SDL_OutOfMemory();
Aug 2, 2017
Aug 2, 2017
179
return NULL;
Feb 9, 2020
Feb 9, 2020
182
183
viddata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
if (!viddata) {
184
185
186
SDL_OutOfMemory();
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
187
188
viddata->devindex = devindex;
viddata->drm_fd = -1;
Feb 9, 2020
Feb 9, 2020
190
device->driverdata = viddata;
191
192
193
194
195
196
/* Setup all functions which we can handle */
device->VideoInit = KMSDRM_VideoInit;
device->VideoQuit = KMSDRM_VideoQuit;
device->GetDisplayModes = KMSDRM_GetDisplayModes;
device->SetDisplayMode = KMSDRM_SetDisplayMode;
Aug 28, 2017
Aug 28, 2017
197
198
device->CreateSDLWindow = KMSDRM_CreateWindow;
device->CreateSDLWindowFrom = KMSDRM_CreateWindowFrom;
199
200
201
202
203
204
205
206
207
208
209
210
211
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
212
#if SDL_VIDEO_OPENGL_EGL
213
214
215
216
217
218
219
220
221
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;
device->GL_SwapWindow = KMSDRM_GLES_SwapWindow;
device->GL_DeleteContext = KMSDRM_GLES_DeleteContext;
Aug 4, 2017
Aug 4, 2017
222
#endif
223
device->PumpEvents = KMSDRM_PumpEvents;
Feb 9, 2020
Feb 9, 2020
224
device->free = KMSDRM_DeleteDevice;
225
226
227
228
return device;
cleanup:
Feb 9, 2020
Feb 9, 2020
229
if (device)
230
SDL_free(device);
Feb 9, 2020
Feb 9, 2020
231
232
if (viddata)
SDL_free(viddata);
233
234
235
236
237
238
239
return NULL;
}
VideoBootStrap KMSDRM_bootstrap = {
"KMSDRM",
"KMS/DRM Video Driver",
KMSDRM_Available,
Feb 9, 2020
Feb 9, 2020
240
KMSDRM_CreateDevice
241
242
243
244
245
246
247
248
};
static void
KMSDRM_FBDestroyCallback(struct gbm_bo *bo, void *data)
{
KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)data;
Mar 13, 2019
Mar 13, 2019
249
if (fb_info && fb_info->drm_fd >= 0 && fb_info->fb_id != 0) {
250
251
252
253
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
254
SDL_free(fb_info);
255
256
257
258
259
}
KMSDRM_FBInfo *
KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
{
Feb 9, 2020
Feb 9, 2020
260
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
Feb 15, 2020
Feb 15, 2020
261
262
263
unsigned w,h;
int ret;
Uint32 stride, handle;
Feb 9, 2020
Feb 9, 2020
264
265
266
267
268
/* Check for an existing framebuffer */
KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)KMSDRM_gbm_bo_get_user_data(bo);
if (fb_info) {
269
270
271
return fb_info;
}
Feb 9, 2020
Feb 9, 2020
272
273
/* Create a structure that contains enough info to remove the framebuffer
when the backing buffer is destroyed */
274
fb_info = (KMSDRM_FBInfo *)SDL_calloc(1, sizeof(KMSDRM_FBInfo));
Feb 9, 2020
Feb 9, 2020
275
276
if (!fb_info) {
Aug 5, 2017
Aug 5, 2017
277
278
279
SDL_OutOfMemory();
return NULL;
}
Feb 9, 2020
Feb 9, 2020
281
fb_info->drm_fd = viddata->drm_fd;
Feb 9, 2020
Feb 9, 2020
283
/* Create framebuffer object for the buffer */
Feb 15, 2020
Feb 15, 2020
284
285
286
287
288
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
289
290
291
292
&fb_info->fb_id);
if (ret) {
SDL_free(fb_info);
return NULL;
Feb 9, 2020
Feb 9, 2020
294
295
296
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);
297
298
299
/* 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
300
301
302
303
return fb_info;
}
Feb 9, 2020
Feb 9, 2020
304
305
306
307
308
309
static void
KMSDRM_FlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
{
*((SDL_bool *) data) = SDL_FALSE;
}
Feb 9, 2020
Feb 9, 2020
311
312
313
KMSDRM_WaitPageFlip(_THIS, SDL_WindowData *windata, int timeout) {
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
drmEventContext ev = {0};
Feb 15, 2020
Feb 15, 2020
314
315
struct pollfd pfd = {0};
Feb 9, 2020
Feb 9, 2020
316
317
318
319
320
321
322
323
ev.version = DRM_EVENT_CONTEXT_VERSION;
ev.page_flip_handler = KMSDRM_FlipHandler;
pfd.fd = viddata->drm_fd;
pfd.events = POLLIN;
while (windata->waiting_for_flip) {
pfd.revents = 0;
Feb 9, 2020
Feb 9, 2020
325
if (poll(&pfd, 1, timeout) < 0) {
326
327
328
329
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "DRM poll error");
return SDL_FALSE;
}
Feb 9, 2020
Feb 9, 2020
330
if (pfd.revents & (POLLHUP | POLLERR)) {
331
332
333
334
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "DRM poll hup or error");
return SDL_FALSE;
}
Feb 9, 2020
Feb 9, 2020
335
336
337
if (pfd.revents & POLLIN) {
/* Page flip? If so, drmHandleEvent will unset windata->waiting_for_flip */
KMSDRM_drmHandleEvent(viddata->drm_fd, &ev);
338
339
340
341
342
343
344
} else {
/* Timed out and page flip didn't happen */
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Dropping frame while waiting_for_flip");
return SDL_FALSE;
}
}
Feb 9, 2020
Feb 9, 2020
345
return SDL_TRUE;
346
347
348
349
350
351
}
/*****************************************************************************/
/* SDL Video and Display initialization/handling functions */
/* _this is a SDL_VideoDevice * */
/*****************************************************************************/
Feb 27, 2020
Feb 27, 2020
352
static void
Feb 9, 2020
Feb 9, 2020
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
KMSDRM_DestroySurfaces(_THIS, SDL_Window * window)
{
SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
KMSDRM_WaitPageFlip(_this, windata, -1);
if (windata->curr_bo) {
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->curr_bo);
windata->curr_bo = NULL;
}
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
394
#if SDL_VIDEO_OPENGL_EGL
Feb 15, 2020
Feb 15, 2020
395
EGLContext egl_context;
Mar 25, 2020
Mar 25, 2020
396
#endif
Feb 9, 2020
Feb 9, 2020
397
398
399
400
401
402
403
if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm, surface_fmt, surface_flags)) {
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
404
egl_context = (EGLContext)SDL_GL_GetCurrentContext();
Feb 9, 2020
Feb 9, 2020
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
#endif
KMSDRM_DestroySurfaces(_this, window);
windata->gs = KMSDRM_gbm_surface_create(viddata->gbm, width, height, surface_fmt, surface_flags);
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);
windata->egl_surface_dirty = 0;
#endif
return 0;
}
430
431
432
433
int
KMSDRM_VideoInit(_THIS)
{
int ret = 0;
Feb 9, 2020
Feb 9, 2020
434
435
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata = NULL;
436
437
drmModeRes *resources = NULL;
drmModeEncoder *encoder = NULL;
Feb 15, 2020
Feb 15, 2020
438
439
char devname[32];
SDL_VideoDisplay display = {0};
Feb 9, 2020
Feb 9, 2020
441
442
443
dispdata = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
if (!dispdata) {
444
445
446
447
448
449
return SDL_OutOfMemory();
}
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoInit()");
/* Open /dev/dri/cardNN */
Feb 9, 2020
Feb 9, 2020
450
451
452
453
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
455
456
if (viddata->drm_fd < 0) {
ret = SDL_SetError("Could not open %s", devname);
457
458
459
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
460
461
462
463
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", viddata->drm_fd);
viddata->gbm = KMSDRM_gbm_create_device(viddata->drm_fd);
if (!viddata->gbm) {
464
465
466
467
ret = SDL_SetError("Couldn't create gbm device.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
468
469
/* Get all of the available connectors / devices / crtcs */
resources = KMSDRM_drmModeGetResources(viddata->drm_fd);
470
if (!resources) {
Feb 9, 2020
Feb 9, 2020
471
ret = SDL_SetError("drmModeGetResources(%d) failed", viddata->drm_fd);
472
473
474
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
475
476
477
478
for (int i = 0; i < resources->count_connectors; i++) {
drmModeConnector *conn = KMSDRM_drmModeGetConnector(viddata->drm_fd, resources->connectors[i]);
if (!conn) {
479
continue;
Feb 9, 2020
Feb 9, 2020
480
}
Feb 9, 2020
Feb 9, 2020
482
if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
483
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.",
Feb 9, 2020
Feb 9, 2020
484
485
conn->connector_id, conn->count_modes);
dispdata->conn = conn;
486
487
488
break;
}
Feb 9, 2020
Feb 9, 2020
489
KMSDRM_drmModeFreeConnector(conn);
Feb 9, 2020
Feb 9, 2020
492
if (!dispdata->conn) {
493
494
495
496
ret = SDL_SetError("No currently active connector found.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
497
498
499
/* 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
500
Feb 9, 2020
Feb 9, 2020
501
502
if (!encoder) {
continue;
Dec 1, 2018
Dec 1, 2018
503
504
}
Feb 9, 2020
Feb 9, 2020
505
506
if (encoder->encoder_id == dispdata->conn->encoder_id) {
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
507
508
509
510
break;
}
KMSDRM_drmModeFreeEncoder(encoder);
Aug 11, 2017
Aug 11, 2017
511
encoder = NULL;
Feb 9, 2020
Feb 9, 2020
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
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;
}
for (j = 0; j < dispdata->conn->count_encoders; j++) {
if (dispdata->conn->encoders[j] == encoder->encoder_id) {
break;
}
}
if (j != dispdata->conn->count_encoders) {
break;
}
KMSDRM_drmModeFreeEncoder(encoder);
encoder = NULL;
}
}
if (!encoder) {
539
540
541
542
ret = SDL_SetError("No connected encoder found.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
543
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
Dec 1, 2018
Dec 1, 2018
544
Feb 9, 2020
Feb 9, 2020
545
546
547
548
549
550
/* Try to find a CRTC connected to this encoder */
dispdata->saved_crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
if (!dispdata->saved_crtc) {
/* No CRTC was connected, find the first CRTC that can be connected */
for (int i = 0; i < resources->count_crtcs; i++) {
Dec 1, 2018
Dec 1, 2018
551
552
if (encoder->possible_crtcs & (1 << i)) {
encoder->crtc_id = resources->crtcs[i];
Feb 9, 2020
Feb 9, 2020
553
dispdata->saved_crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
Dec 1, 2018
Dec 1, 2018
554
555
556
557
558
break;
}
}
}
Feb 9, 2020
Feb 9, 2020
559
if (!dispdata->saved_crtc) {
560
561
562
563
ret = SDL_SetError("No CRTC found.");
goto cleanup;
}
Feb 9, 2020
Feb 9, 2020
564
565
566
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Saved crtc_id %u, fb_id %u, (%u,%u), %ux%u",
dispdata->saved_crtc->crtc_id, dispdata->saved_crtc->buffer_id, dispdata->saved_crtc->x,
dispdata->saved_crtc->y, dispdata->saved_crtc->width, dispdata->saved_crtc->height);
Feb 9, 2020
Feb 9, 2020
568
dispdata->crtc_id = encoder->crtc_id;
Feb 9, 2020
Feb 9, 2020
570
571
/* 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
573
574
FIXME find first mode that specifies DRM_MODE_TYPE_PREFERRED */
dispdata->mode = dispdata->saved_crtc->mode;
Feb 9, 2020
Feb 9, 2020
576
577
578
579
580
581
582
if (dispdata->saved_crtc->mode_valid == 0) {
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
"Current mode is invalid, selecting connector's mode #0.");
dispdata->mode = dispdata->conn->modes[0];
}
/* Setup the single display that's available */
Feb 15, 2020
Feb 15, 2020
583
Feb 9, 2020
Feb 9, 2020
584
585
586
587
588
589
590
591
592
593
594
595
596
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 */
drmModeFB *fb = drmModeGetFB(viddata->drm_fd, dispdata->saved_crtc->buffer_id);
display.desktop_mode.format = drmToSDLPixelFormat(fb->bpp, fb->depth);
drmModeFreeFB(fb);
#endif
display.current_mode = display.desktop_mode;
display.driverdata = dispdata;
597
598
599
600
601
602
603
604
SDL_AddVideoDisplay(&display);
#ifdef SDL_INPUT_LINUXEV
SDL_EVDEV_Init();
#endif
KMSDRM_InitMouse(_this);
Mar 11, 2019
Mar 11, 2019
605
606
return ret;
Feb 9, 2020
Feb 9, 2020
608
if (encoder)
609
KMSDRM_drmModeFreeEncoder(encoder);
Feb 9, 2020
Feb 9, 2020
610
if (resources)
611
612
613
614
KMSDRM_drmModeFreeResources(resources);
if (ret != 0) {
/* Error (complete) cleanup */
Feb 9, 2020
Feb 9, 2020
615
616
617
618
619
620
621
if (dispdata->conn) {
KMSDRM_drmModeFreeConnector(dispdata->conn);
dispdata->conn = NULL;
}
if (dispdata->saved_crtc) {
KMSDRM_drmModeFreeCrtc(dispdata->saved_crtc);
dispdata->saved_crtc = NULL;
Feb 9, 2020
Feb 9, 2020
623
624
625
if (viddata->gbm) {
KMSDRM_gbm_device_destroy(viddata->gbm);
viddata->gbm = NULL;
Feb 9, 2020
Feb 9, 2020
627
628
629
if (viddata->drm_fd >= 0) {
close(viddata->drm_fd);
viddata->drm_fd = -1;
Feb 9, 2020
Feb 9, 2020
631
SDL_free(dispdata);
632
633
634
635
636
637
638
}
return ret;
}
void
KMSDRM_VideoQuit(_THIS)
{
Feb 9, 2020
Feb 9, 2020
639
640
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
641
642
643
644
645
646
647
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoQuit()");
if (_this->gl_config.driver_loaded) {
SDL_GL_UnloadLibrary();
}
Feb 9, 2020
Feb 9, 2020
648
649
650
651
652
653
654
/* Clear out the window list */
SDL_free(viddata->windows);
viddata->windows = NULL;
viddata->max_windows = 0;
viddata->num_windows = 0;
/* Restore saved CRTC settings */
Mar 2, 2020
Mar 2, 2020
655
if (viddata->drm_fd >= 0 && dispdata && dispdata->conn && dispdata->saved_crtc) {
Feb 9, 2020
Feb 9, 2020
656
657
658
659
660
661
662
663
drmModeConnector *conn = dispdata->conn;
drmModeCrtc *crtc = dispdata->saved_crtc;
int ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd, crtc->crtc_id, crtc->buffer_id,
crtc->x, crtc->y, &conn->connector_id, 1, &crtc->mode);
if (ret != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not restore original CRTC mode");
Mar 2, 2020
Mar 2, 2020
666
if (dispdata && dispdata->conn) {
Feb 9, 2020
Feb 9, 2020
667
668
669
KMSDRM_drmModeFreeConnector(dispdata->conn);
dispdata->conn = NULL;
}
Mar 2, 2020
Mar 2, 2020
670
if (dispdata && dispdata->saved_crtc) {
Feb 9, 2020
Feb 9, 2020
671
672
673
674
675
676
KMSDRM_drmModeFreeCrtc(dispdata->saved_crtc);
dispdata->saved_crtc = NULL;
}
if (viddata->gbm) {
KMSDRM_gbm_device_destroy(viddata->gbm);
viddata->gbm = NULL;
Feb 9, 2020
Feb 9, 2020
678
679
680
681
if (viddata->drm_fd >= 0) {
close(viddata->drm_fd);
SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Closed DRM FD %d", viddata->drm_fd);
viddata->drm_fd = -1;
682
683
684
685
686
687
688
689
690
}
#ifdef SDL_INPUT_LINUXEV
SDL_EVDEV_Quit();
#endif
}
void
KMSDRM_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{
Feb 9, 2020
Feb 9, 2020
691
692
SDL_DisplayData *dispdata = display->driverdata;
drmModeConnector *conn = dispdata->conn;
Feb 15, 2020
Feb 15, 2020
693
SDL_DisplayMode mode;
Feb 9, 2020
Feb 9, 2020
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
for (int i = 0; i < conn->count_modes; i++) {
SDL_DisplayModeData *modedata = SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (modedata) {
modedata->mode_index = i;
}
mode.w = conn->modes[i].hdisplay;
mode.h = conn->modes[i].vdisplay;
mode.refresh_rate = conn->modes[i].vrefresh;
mode.format = SDL_PIXELFORMAT_ARGB8888;
mode.driverdata = modedata;
if (!SDL_AddDisplayMode(display, &mode)) {
SDL_free(modedata);
}
}
712
713
714
715
716
}
int
KMSDRM_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
Feb 9, 2020
Feb 9, 2020
717
718
719
SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
SDL_DisplayData *dispdata = (SDL_DisplayData *)display->driverdata;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
Feb 15, 2020
Feb 15, 2020
720
drmModeConnector *conn = dispdata->conn;
Feb 9, 2020
Feb 9, 2020
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
if (!modedata) {
return SDL_SetError("Mode doesn't have an associated index");
}
dispdata->mode = conn->modes[modedata->mode_index];
for (int i = 0; i < viddata->num_windows; i++) {
SDL_Window *window = viddata->windows[i];
SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
#if SDL_VIDEO_OPENGL_EGL
/* Can't recreate EGL surfaces right now, need to wait until SwapWindow
so the correct thread-local surface and context state are available */
windata->egl_surface_dirty = 1;
#else
if (KMSDRM_CreateSurfaces(_this, window)) {
return -1;
}
#endif
/* Tell app about the resize */
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, mode->w, mode->h);
}
746
747
748
749
750
751
return 0;
}
int
KMSDRM_CreateWindow(_THIS, SDL_Window * window)
{
Feb 9, 2020
Feb 9, 2020
752
SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
Feb 15, 2020
Feb 15, 2020
753
754
SDL_WindowData *windata;
SDL_VideoDisplay *display;
Feb 9, 2020
Feb 9, 2020
755
756
757
758
759
760
761
762
#if SDL_VIDEO_OPENGL_EGL
if (!_this->egl_data) {
if (SDL_GL_LoadLibrary(NULL) < 0) {
goto error;
}
}
#endif
763
764
/* Allocate window internal data */
Feb 15, 2020
Feb 15, 2020
765
windata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
Feb 9, 2020
Feb 9, 2020
766
767
if (!windata) {
Aug 4, 2017
Aug 4, 2017
768
769
SDL_OutOfMemory();
goto error;
770
771
772
}
/* Windows have one size for now */
Feb 15, 2020
Feb 15, 2020
773
display = SDL_GetDisplayForWindow(window);
774
775
776
777
778
779
window->w = display->desktop_mode.w;
window->h = display->desktop_mode.h;
/* Maybe you didn't ask for a fullscreen OpenGL window, but that's what you get */
window->flags |= (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
Feb 9, 2020
Feb 9, 2020
780
781
/* In case we want low-latency, double-buffer video, we take note here */
windata->double_buffer = SDL_FALSE;
Feb 9, 2020
Feb 9, 2020
783
784
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE)) {
windata->double_buffer = SDL_TRUE;
Feb 9, 2020
Feb 9, 2020
787
788
/* Setup driver data for this window */
window->driverdata = windata;
Feb 9, 2020
Feb 9, 2020
790
791
if (KMSDRM_CreateSurfaces(_this, window)) {
goto error;
Feb 9, 2020
Feb 9, 2020
794
795
796
797
/* Add window to the internal list of tracked windows. Note, while it may
seem odd to support multiple fullscreen windows, some apps create an
extra window as a dummy surface when working with multiple contexts */
windata->viddata = viddata;
Oct 26, 2017
Oct 26, 2017
798
Feb 9, 2020
Feb 9, 2020
799
800
801
802
803
if (viddata->num_windows >= viddata->max_windows) {
int new_max_windows = viddata->max_windows + 1;
viddata->windows = (SDL_Window **)SDL_realloc(viddata->windows,
new_max_windows * sizeof(SDL_Window *));
viddata->max_windows = new_max_windows;
Oct 21, 2017
Oct 21, 2017
804
Feb 9, 2020
Feb 9, 2020
805
806
807
808
809
if (!viddata->windows) {
SDL_OutOfMemory();
goto error;
}
}
Feb 9, 2020
Feb 9, 2020
811
viddata->windows[viddata->num_windows++] = window;
Mar 7, 2020
Mar 7, 2020
813
814
815
816
/* Focus on the newly created window */
SDL_SetMouseFocus(window);
SDL_SetKeyboardFocus(window);
817
818
819
return 0;
error:
Feb 9, 2020
Feb 9, 2020
820
821
KMSDRM_DestroyWindow(_this, window);
822
823
824
825
826
827
return -1;
}
void
KMSDRM_DestroyWindow(_THIS, SDL_Window * window)
{
Feb 9, 2020
Feb 9, 2020
828
SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
Feb 15, 2020
Feb 15, 2020
829
SDL_VideoData *viddata;
Feb 9, 2020
Feb 9, 2020
830
831
832
833
834
if (!windata) {
return;
}
/* Remove from the internal window list */
Feb 15, 2020
Feb 15, 2020
835
viddata = windata->viddata;
Feb 9, 2020
Feb 9, 2020
836
837
838
839
840
841
842
843
844
845
for (int i = 0; i < viddata->num_windows; i++) {
if (viddata->windows[i] == window) {
viddata->num_windows--;
for (int j = i; j < viddata->num_windows; j++) {
viddata->windows[j] = viddata->windows[j + 1];
}
break;
Feb 9, 2020
Feb 9, 2020
848
849
850
851
852
853
KMSDRM_DestroySurfaces(_this, window);
window->driverdata = NULL;
SDL_free(windata);
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
}
int
KMSDRM_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
{
return -1;
}
void
KMSDRM_SetWindowTitle(_THIS, SDL_Window * window)
{
}
void
KMSDRM_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
{
}
void
KMSDRM_SetWindowPosition(_THIS, SDL_Window * window)
{
}
void
KMSDRM_SetWindowSize(_THIS, SDL_Window * window)
{
}
void
KMSDRM_ShowWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_HideWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_RaiseWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_MaximizeWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_MinimizeWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_RestoreWindow(_THIS, SDL_Window * window)
{
}
void
KMSDRM_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
}
/*****************************************************************************/
/* SDL Window Manager function */
/*****************************************************************************/
SDL_bool
KMSDRM_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
{
if (info->version.major <= SDL_MAJOR_VERSION) {
return SDL_TRUE;
} else {
SDL_SetError("application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
/* Failed to get window manager information */
return SDL_FALSE;
}
#endif /* SDL_VIDEO_DRIVER_KMSDRM */
/* vi: set ts=4 sw=4 expandtab: */