Commit 2067a7db8e4a36ba40ab34a55b3166ca28638a60

Manuel Alfayate Corchete 2021-01-11T20:29:09

[KMS/DRM] Fix fullscreen to windowed transition. Fix aspect ratio correction without using planes.

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
diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c
index 4dab504..4b25620 100644
--- a/src/video/kmsdrm/SDL_kmsdrmopengles.c
+++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c
@@ -28,6 +28,7 @@
 #include "SDL_kmsdrmvideo.h"
 #include "SDL_kmsdrmopengles.h"
 #include "SDL_kmsdrmdyn.h"
+#include <errno.h>
 
 #ifndef EGL_PLATFORM_GBM_MESA
 #define EGL_PLATFORM_GBM_MESA 0x31D7
@@ -70,6 +71,8 @@ KMSDRM_GLES_UnloadLibrary(_THIS) {
 SDL_EGL_CreateContext_impl(KMSDRM)
 
 int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
+
+    /*
     if (!_this->egl_data) {
         return SDL_SetError("EGL not initialized");
     }
@@ -78,11 +81,23 @@ int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
         _this->egl_data->egl_swapinterval = interval;
     } else {
         return SDL_SetError("Only swap intervals of 0 or 1 are supported");
-    }
+    }*/
+
+    /* Issuing a new pageflip before the previous has completed
+       causes drmModePageFlip() to return EBUSY errors. Don't do it. */
+    _this->egl_data->egl_swapinterval = 1;
 
     return 0;
 }
 
+/* Check if we need to re-create the GBM and EGL surfaces and setup a new mode on the CRTC
+   matching the new GBM surface size so the buffers from these surfaces can be displayed
+   on the CRTC (remember we need the CRTC mode and the buffer size to match). */
+/*KMSDRM_CheckKMSDRM (_THIS, window) {
+
+
+}*/
+
 int
 KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
     SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
@@ -117,56 +132,92 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
         return 0;
     }
 
-    /* Lock the next front buffer so it can't be allocated as a back buffer */
+    /* From the GBM surface, get the next BO to become the next front buffer,
+       and lock it so it can't be allocated as a back buffer (to prevent EGL
+       from drawing into it!) */
     windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
     if (!windata->next_bo) {
         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
         return 0;
     }
 
-    /* Get the fb_info for the next front buffer. */
+    /* Get an actual usable fb for the next front buffer. */
     fb_info = KMSDRM_FBFromBO(_this, windata->next_bo);
     if (!fb_info) {
         return 0;
     }
 
+#if 0
+    /* Get an actual usable fb (of the specified dimensions) form the BO,
+       so we can pass it to the CRTC. */
+    fb_info = KMSDRM_FBFromBO2(_this, windata->next_bo, window->w, window->h);
+    if (!fb_info) {
+        return 0;
+    }
+#endif
+
+#if 0
+    /* If the window size does not match the current videomode configured on the CRTC,
+       update the current video mode and configure the new mode on the CRTC.  */
+    if (dispdata->mode.hdisplay != window->w || dispdata->mode.vdisplay != window->h) { 
+        for (int i = 0; i < dispdata->connector->count_modes; i++) {
+            drmModeModeInfo mode = dispdata->connector->modes[i] ;
+            if (mode.hdisplay == window->w && mode.vdisplay == window->h) {
+                dispdata->mode = mode;
+	        ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd,
+		     dispdata->crtc->crtc_id, fb_info->fb_id, 0, 0,
+		     &dispdata->connector->connector_id, 1, &dispdata->mode);
+
+	        if (ret) {
+		    SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set mode on CRTC.");
+		    printf("********Could not set mode on CRTC.\n");
+                }
+                return 0;
+            }
+        }
+        return 0;
+    }
+#endif
+
+#if 0
+    /* Get an actual usable fb for the next front buffer. */
+    fb_info = KMSDRM_FBFromBO(_this, windata->next_bo);
+    if (!fb_info) {
+        return 0;
+    }
+#endif
+
+#if 1 
     /* Do we have a modeset pending? If so, configure the new mode on the CRTC.
        Has to be done before the upcoming pageflip issue, so the buffer with the
        new size is big enough so the CRTC doesn't read out of bounds. */
     if (dispdata->modeset_pending) {
 
-        /***************************************************************************/
-        /* This is fundamental.                                                    */
-        /* We can't display an fb smaller than the resolution currently configured */
-        /* on the CRTC, because the CRTC would be scanning out of bounds, and      */
-        /* drmModeSetCrtc() would fail.                                            */
-        /* A possible solution would be scaling on the primary plane with          */
-        /* drmModeSetPlane(), but primary plane scaling is not supported in most   */
-        /* LEGACY-only hardware, so never use drmModeSetPlane().                   */
-        /***************************************************************************/
-
         ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd,
           dispdata->crtc->crtc_id, fb_info->fb_id, 0, 0,
           &dispdata->connector->connector_id, 1, &dispdata->mode);
 
+        dispdata->modeset_pending = SDL_FALSE;
+
         if (ret) {
             SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set videomode on CRTC.");
-
         }
+
         return 0;
     }
+#endif
 
     /* Issue pageflip on the next front buffer.
        The pageflip will be done during the next vblank. */
     ret = KMSDRM_drmModePageFlip(viddata->drm_fd, dispdata->crtc->crtc_id,
 	     fb_info->fb_id, DRM_MODE_PAGE_FLIP_EVENT, &windata->waiting_for_flip);
 
-    if (_this->egl_data->egl_swapinterval == 1) {
-	if (ret == 0) {
+    if (ret == 0) {
+        if (_this->egl_data->egl_swapinterval == 1) {
 	    windata->waiting_for_flip = SDL_TRUE;
-	} else {
-	    SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
-	}
+        }
+    } else {
+	SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
     }
 
     /* If we are in double-buffer mode, wait immediately for vsync
diff --git a/src/video/kmsdrm/SDL_kmsdrmsym.h b/src/video/kmsdrm/SDL_kmsdrmsym.h
index f09276f..b706445 100644
--- a/src/video/kmsdrm/SDL_kmsdrmsym.h
+++ b/src/video/kmsdrm/SDL_kmsdrmsym.h
@@ -45,6 +45,16 @@ SDL_KMSDRM_SYM(drmModeResPtr,drmModeGetResources,(int fd))
 SDL_KMSDRM_SYM(int,drmModeAddFB,(int fd, uint32_t width, uint32_t height, uint8_t depth,
                                  uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
                                  uint32_t *buf_id))
+
+SDL_KMSDRM_SYM(int,drmModeAddFB2,(int fd, uint32_t width, uint32_t height,
+                                  uint32_t pixel_format,
+                                  uint32_t bo_handles[4],
+                                  uint32_t pitches[4],
+                                  uint32_t offsets[4],
+                                  uint32_t * buf_id,
+                                  uint32_t flags 
+))
+
 SDL_KMSDRM_SYM(int,drmModeRmFB,(int fd, uint32_t bufferId))
 SDL_KMSDRM_SYM(drmModeFBPtr,drmModeGetFB,(int fd, uint32_t buf))
 SDL_KMSDRM_SYM(drmModeCrtcPtr,drmModeGetCrtc,(int fd, uint32_t crtcId))
@@ -91,6 +101,8 @@ SDL_KMSDRM_SYM(struct gbm_device *,gbm_create_device,(int fd))
 SDL_KMSDRM_SYM(unsigned int,gbm_bo_get_width,(struct gbm_bo *bo))
 SDL_KMSDRM_SYM(unsigned int,gbm_bo_get_height,(struct gbm_bo *bo))
 SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_stride,(struct gbm_bo *bo))
+SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_format,(struct gbm_bo *bo))
+SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_offset,(struct gbm_bo *bo, int plane))
 SDL_KMSDRM_SYM(union gbm_bo_handle,gbm_bo_get_handle,(struct gbm_bo *bo))
 SDL_KMSDRM_SYM(int,gbm_bo_write,(struct gbm_bo *bo, const void *buf, size_t count))
 SDL_KMSDRM_SYM(struct gbm_device *,gbm_bo_get_device,(struct gbm_bo *bo))
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index bd1ec72..781b796 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -332,6 +332,115 @@ KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
     return fb_info;
 }
 
+#if 0
+/* Get an actual usable fb from a BO, specifying the size of the desired fb. */
+KMSDRM_FBInfo *
+KMSDRM_FBFromBO2(_THIS, struct gbm_bo *bo, int width, int height)
+{
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    int ret;
+    Uint32 stride, handle;
+
+    uint32_t format, strides[4] = {0}, handles[4] = {0}, offsets[4] = {0}; 
+
+    /* Check for an existing framebuffer */
+    KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)KMSDRM_gbm_bo_get_user_data(bo);
+
+    if (fb_info) {
+        /* TODO We used to simply return the old fb_info if there was one here.
+           Now free it AND the actual buffer IF the sizes have changed (maybe
+           add them to fb_info? to avoid leaking.)*/
+        SDL_free(fb_info);
+    }
+
+    /* Create a structure that contains enough info to remove the framebuffer
+       when the backing buffer is destroyed */
+    fb_info = (KMSDRM_FBInfo *)SDL_calloc(1, sizeof(KMSDRM_FBInfo));
+
+    if (!fb_info) {
+        SDL_OutOfMemory();
+        return NULL;
+    }
+
+    fb_info->drm_fd = viddata->drm_fd;
+
+#if 0
+    /* Create framebuffer object for the buffer */
+    stride = KMSDRM_gbm_bo_get_stride(bo);
+    handle = KMSDRM_gbm_bo_get_handle(bo).u32;
+    ret = KMSDRM_drmModeAddFB(viddata->drm_fd, width, height, 24, 32, stride, handle,
+                                  &fb_info->fb_id);
+#endif
+
+    handles[0] = KMSDRM_gbm_bo_get_handle(bo).u32;
+    strides[0] = KMSDRM_gbm_bo_get_stride(bo);
+    offsets[0] = KMSDRM_gbm_bo_get_offset(bo, 0);
+    format = KMSDRM_gbm_bo_get_format(bo);
+
+    ret = KMSDRM_drmModeAddFB2(viddata->drm_fd, width, height, format,
+        handles, strides, offsets, &fb_info->fb_id, 0);
+
+
+    if (ret) {
+      SDL_free(fb_info);
+      return NULL;
+    }
+
+    /* Associate our DRM framebuffer with this buffer object */
+    KMSDRM_gbm_bo_set_user_data(bo, fb_info, KMSDRM_FBDestroyCallback);
+
+    return fb_info;
+}
+#endif
+
+#if 0
+/* Get an actual usable fb from a BO, specifying the size of the desired fb. */
+KMSDRM_FBInfo *
+KMSDRM_FBFromBO2(_THIS, struct gbm_bo *bo, int width, int height)
+{
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    int ret;
+    Uint32 stride, handle;
+
+    /* Check for an existing framebuffer */
+    KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)KMSDRM_gbm_bo_get_user_data(bo);
+
+    if (fb_info) {
+        /* TODO We used to simply return the old fb_info if there was one here.
+           Now free it AND the actual buffer IF the sizes have changed (maybe
+           add them to fb_info? to avoid leaking.)*/
+        SDL_free(fb_info);
+    }
+
+    /* Create a structure that contains enough info to remove the framebuffer
+       when the backing buffer is destroyed */
+    fb_info = (KMSDRM_FBInfo *)SDL_calloc(1, sizeof(KMSDRM_FBInfo));
+
+    if (!fb_info) {
+        SDL_OutOfMemory();
+        return NULL;
+    }
+
+    fb_info->drm_fd = viddata->drm_fd;
+
+    /* Create framebuffer object for the buffer */
+    stride = KMSDRM_gbm_bo_get_stride(bo);
+    handle = KMSDRM_gbm_bo_get_handle(bo).u32;
+    ret = KMSDRM_drmModeAddFB(viddata->drm_fd, width, height, 24, 32, stride, handle,
+                                  &fb_info->fb_id);
+
+    if (ret) {
+      SDL_free(fb_info);
+      return NULL;
+    }
+
+    /* Associate our DRM framebuffer with this buffer object */
+    KMSDRM_gbm_bo_set_user_data(bo, fb_info, KMSDRM_FBDestroyCallback);
+
+    return fb_info;
+}
+#endif
+
 static void
 KMSDRM_FlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
 {
@@ -376,6 +485,21 @@ KMSDRM_WaitPageFlip(_THIS, SDL_WindowData *windata, int timeout) {
     return SDL_TRUE;
 }
 
+/* Given w and h, returns a DRM video mode from the modes available on the connector.
+   Returns NULL if no mode matching the specified size is found. */
+drmModeModeInfo*
+KMSDRM_GetConnectorMode(drmModeConnector *connector, uint32_t width, uint32_t height) {
+    int i = 0;
+    for (i = 0; i < connector->count_modes; i++) {
+	if (connector->modes[i].hdisplay == width &&
+            connector->modes[i].vdisplay == height)
+        {
+	    return &connector->modes[i];
+	}
+    }
+    return NULL;
+}
+
 /*****************************************************************************/
 /* SDL Video and Display initialization/handling functions                   */
 /* _this is a SDL_VideoDevice *                                              */
@@ -413,9 +537,8 @@ int KMSDRM_DisplayDataInit (_THIS, SDL_DisplayData *dispdata) {
     int ret = 0;
     unsigned i,j;
 
-    dispdata->modeset_pending = SDL_FALSE;
     dispdata->gbm_init = SDL_FALSE;
-
+    dispdata->modeset_pending = SDL_FALSE;
     dispdata->cursor_bo = NULL;
 
     /* Open /dev/dri/cardNN (/dev/drmN if on OpenBSD) */
@@ -693,6 +816,8 @@ KMSDRM_DestroySurfaces(_THIS, SDL_Window *window)
     }
 }
 
+/* This determines the size of the fb, which comes from the GBM surface
+   that we create here. */
 int
 KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
 {
@@ -701,7 +826,6 @@ KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
     SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
     uint32_t surface_fmt = GBM_FORMAT_ARGB8888;
     uint32_t surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
-    uint32_t width, height;
 
     EGLContext egl_context;
 
@@ -714,16 +838,6 @@ KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
         KMSDRM_DestroySurfaces(_this, window);
     }
 
-    if ( ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
-      || ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN))
-    {
-        width = dispdata->mode.hdisplay;
-        height = dispdata->mode.vdisplay;
-    } else {
-        width = window->w;
-        height = window->h;
-    }
-
     if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev,
              surface_fmt, surface_flags)) {
         SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
@@ -731,7 +845,7 @@ KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
     }
 
     windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev,
-                      width, height, surface_fmt, surface_flags);
+                      dispdata->surface_w, dispdata->surface_h, surface_fmt, surface_flags);
 
     if (!windata->gs) {
         return SDL_SetError("Could not create GBM surface");
@@ -857,6 +971,7 @@ KMSDRM_VideoQuit(_THIS)
     viddata->video_init = SDL_FALSE;
 }
 
+/* Read modes from the connector modes, and store them in display->display_modes. */
 void
 KMSDRM_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
 {
@@ -905,12 +1020,13 @@ KMSDRM_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
         return SDL_SetError("Mode doesn't have an associated index");
     }
 
-    /* Take note of the new mode to be set. */
+    /* Take note of the new mode to be set, and leave the modeset pending. */
     dispdata->mode = conn->modes[modedata->mode_index];
+    dispdata->modeset_pending = SDL_TRUE; 
 
-    /* Take note that we have to change mode in SwapWindow(). We have to do it there
-       because we need a buffer of the new size so the drmModeSetCrtc() call succeeds.  */
-    dispdata->modeset_pending = SDL_TRUE;
+    /* Set the new surface size. */
+    dispdata->surface_w = dispdata->mode.hdisplay; 
+    dispdata->surface_h = dispdata->mode.vdisplay; 
 
     for (i = 0; i < viddata->num_windows; i++) {
         SDL_Window *window = viddata->windows[i];
@@ -1002,62 +1118,58 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
     SDL_bool vulkan_mode = viddata->vulkan_mode; /* Do we have any Vulkan windows? */
     NativeDisplayType egl_display;
     int ret = 0;
+    drmModeModeInfo *mode;
 
     if ( !(dispdata->gbm_init) && !is_vulkan && !vulkan_mode ) {
- 
-         /* If this is not a Vulkan Window, then this is a GL window, so at the
-            end of this function, we must have marked the window as being OPENGL
-            and we must have loaded the GL library: both things are needed so the
-            GL_CreateRenderer() and GL_LoadFunctions() calls in SDL_CreateWindow()
-            succeed without having to re-create the window.
-            We must load the EGL library too, which can't be loaded until the GBM
-            device has been created, because SDL_EGL_Library() function uses it. */ 
-
-         /* Maybe you didn't ask for an OPENGL window, but that's what you will get.
-            See previous comment on why. */
-         window->flags |= SDL_WINDOW_OPENGL;
-
-         /* We need that the fb that SDL gives us has the same size as the videomode
-            currently configure on the CRTC, because the LEGACY interface doesn't
-            support scaling on the primary plane on most hardware (and overlay
-            planes are not present in all hw), so the CRTC reads the PRIMARY PLANE
-            without any scaling, and that's all.
-            So AR-correctin is also impossible on the LEGACY interface. */
-         window->w = dispdata->mode.hdisplay;
-         window->h = dispdata->mode.vdisplay;
-
-         /* Reopen FD, create gbm dev, setup display plane, etc,.
+
+        /* If this is not a Vulkan Window, then this is a GL window, so at the
+	   end of this function, we must have marked the window as being OPENGL
+	   and we must have loaded the GL library: both things are needed so the
+	   GL_CreateRenderer() and GL_LoadFunctions() calls in SDL_CreateWindow()
+	   succeed without having to re-create the window.
+	   We must load the EGL library too, which can't be loaded until the GBM
+	   device has been created, because SDL_EGL_Library() function uses it. */ 
+
+	/* Maybe you didn't ask for an OPENGL window, but that's what you will get.
+	   See previous comment on why. */
+        window->flags |= SDL_WINDOW_OPENGL;
+
+        /* Don't force fullscreen on all windows: it confuses programs that try
+           to set a window fullscreen after creating it as non-fullscreen (sm64ex) */
+        // window->flags |= SDL_WINDOW_FULLSCREEN;
+
+        /* Reopen FD, create gbm dev, setup display plane, etc,.
             but only when we come here for the first time,
             and only if it's not a VK window. */
-         if ((ret = KMSDRM_GBMInit(_this, dispdata))) {
+        if ((ret = KMSDRM_GBMInit(_this, dispdata))) {
                  goto cleanup;
-         }
-
-         /* Manually load the GL library. KMSDRM_EGL_LoadLibrary() has already
-            been called by SDL_CreateWindow() but we don't do anything there,
-            precisely to be able to load it here.
-            If we let SDL_CreateWindow() load the lib, it will be loaded
-            before we call KMSDRM_GBMInit(), causing GLES programs to fail. */
-         if (!_this->egl_data) {
-             egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
-             if (SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA)) {
-                 goto cleanup;
-             }
+        }
+
+        /* Manually load the GL library. KMSDRM_EGL_LoadLibrary() has already
+           been called by SDL_CreateWindow() but we don't do anything there,
+           precisely to be able to load it here.
+           If we let SDL_CreateWindow() load the lib, it will be loaded
+           before we call KMSDRM_GBMInit(), causing GLES programs to fail. */
+        if (!_this->egl_data) {
+            egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
+            if (SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA)) {
+                goto cleanup;
+            }
 
-             if (SDL_GL_LoadLibrary(NULL) < 0) {
+            if (SDL_GL_LoadLibrary(NULL) < 0) {
                 goto cleanup;
-             }
-         }
+            }
+        }
      
-         /* Can't init mouse stuff sooner because cursor plane is not ready,
-            so we do it here. */
-         KMSDRM_InitMouse(_this);
+	/* Can't init mouse stuff sooner because cursor plane is not ready,
+	   so we do it here. */
+	KMSDRM_InitMouse(_this);
 
-         /* Since we take cursor buffer way from the cursor plane and
-            destroy the cursor GBM BO when we destroy a window, we must
-            also manually re-show the cursor on screen, if necessary,
-            when we create a window. */
-         KMSDRM_InitCursor();
+	/* Since we take cursor buffer way from the cursor plane and
+	   destroy the cursor GBM BO when we destroy a window, we must
+	   also manually re-show the cursor on screen, if necessary,
+	   when we create a window. */
+	KMSDRM_InitCursor();
     }
 
     /* Allocate window internal data */
@@ -1067,31 +1179,37 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
         goto cleanup;
     }
 
-    /* Don't force fullscreen on all windows: it confuses programs that try
-       to set a window fullscreen after creating it as non-fullscreen (sm64ex) */
-    // window->flags |= SDL_WINDOW_FULLSCREEN;
-
     /* Setup driver data for this window */
     windata->viddata = viddata;
     window->driverdata = windata;
 
+    /* We simply IGNORE if it's a fullscreen window, window->flags desn't reflect it.
+       If it's fullscreen, we will manage that in KMSDRM_SetWindwoFullscreen(),
+       which will be called by SDL later, if necessary. */
+    mode = KMSDRM_GetConnectorMode(dispdata->connector, window->w, window->h );
+
     if (!is_vulkan && !vulkan_mode) {
-        /* Create the window surfaces. Needs the window diverdata in place. */
-        if ((ret = KMSDRM_CreateSurfaces(_this, window))) {
-            goto cleanup;
-        }
 
-        /* Configure the current video mode on the CRTC, without changing the buffer
-           it's pointing to (well, that the primary plane connected to the CRTC is
-           pointing to). */
-        ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd, dispdata->crtc->crtc_id,
-            /*fb_info->fb_id*/ -1, 0, 0, &dispdata->connector->connector_id, 1,
-            &dispdata->mode);
+        /* Try to find a matching video mode for the window, with fallback to the
+            original mode if not available, and configure that mode into the CRTC. */
+	if (mode) {
+	    dispdata->surface_w = window->w;
+	    dispdata->surface_h = window->h;
+	    dispdata->mode = *mode;
+	}
+	else {
+	    dispdata->surface_w = dispdata->original_mode.hdisplay;
+	    dispdata->surface_h = dispdata->original_mode.vdisplay;
+	    dispdata->mode = dispdata->original_mode;
+	}
 
-	if (ret) {
-	    SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set CRTC");
+	dispdata->modeset_pending = SDL_TRUE;
+
+        /* Create the window surfaces with the size we have just chosen.
+           Needs the window diverdata in place. */
+        if ((ret = KMSDRM_CreateSurfaces(_this, window))) {
             goto cleanup;
-	}
+        }
     }
 
     /* Add window to the internal list of tracked windows. Note, while it may
@@ -1118,6 +1236,11 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
     SDL_SetMouseFocus(window);
     SDL_SetKeyboardFocus(window);
 
+    /* Tell app about the size we have determined for the window,
+       so SDL pre-scales to that size for us. */
+    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED,
+        dispdata->surface_w, dispdata->surface_h);
+
     /***********************************************************/
     /* Tell SDL that the mouse has entered the window using an */
     /* artificial event: we have no windowing system to tell   */
@@ -1137,19 +1260,57 @@ cleanup:
 }
 
 /*****************************************************************************/
-/* Re-create a window surfaces without destroying the window itself.         */ 
+/* Re-create a window surfaces without destroying the window itself,         */ 
+/* and set a videomode on the CRTC that matches the surfaces size.           */
 /* To be used by SetWindowSize() and SetWindowFullscreen().                  */
 /*****************************************************************************/
-static int
+void
 KMSDRM_ReconfigureWindow( _THIS, SDL_Window * window) {
-    SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */
 
-    if (!is_vulkan) {
-        if (KMSDRM_CreateSurfaces(_this, window)) {
-            return -1;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+
+    if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) ==
+			 SDL_WINDOW_FULLSCREEN_DESKTOP)
+    {
+	/* Update the current mode to the desktop mode,
+	   take note of pending mode configuration to the CRTC,
+	   and recreate the GBM surface with the same size as the size. */
+	dispdata->surface_w = dispdata->original_mode.hdisplay;
+	dispdata->surface_h = dispdata->original_mode.vdisplay;
+	dispdata->mode = dispdata->original_mode;
+    }
+    else
+    {
+	/* Try to find a valid video mode matching the size of the window. */
+	drmModeModeInfo *mode = KMSDRM_GetConnectorMode(dispdata->connector,
+	  window->windowed.w, window->windowed.h );
+
+	if (mode) {
+            /* If matching mode found, recreate the GBM surface with the size
+               of that mode and configure it on the CRTC. */
+	    dispdata->surface_w = window->windowed.w;
+	    dispdata->surface_h = window->windowed.h;
+	    dispdata->mode = *mode;
+	}
+	else {
+            /* If not matching mode found, recreate the GBM surfaces with the
+               size of the mode that was originally configured on the CRTC,
+               and setup that mode on the CRTC. */
+	    dispdata->surface_w = dispdata->original_mode.hdisplay;
+	    dispdata->surface_h = dispdata->original_mode.vdisplay;
+	    dispdata->mode = dispdata->original_mode;
         }
     }
-    return 0;
+
+    /* Recreate the GBM (and EGL) surfaces, and mark the CRTC mode/fb setting
+       as pending so it's done on SwaWindon.  */
+    KMSDRM_CreateSurfaces(_this, window);
+    dispdata->modeset_pending = SDL_TRUE;
+
+    /* Tell app about the size we have determined for the window,
+       so SDL pre-scales to that size for us. */
+    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED,
+    dispdata->surface_w, dispdata->surface_h);
 }
 
 int
@@ -1173,15 +1334,17 @@ KMSDRM_SetWindowPosition(_THIS, SDL_Window * window)
 void
 KMSDRM_SetWindowSize(_THIS, SDL_Window * window)
 {
-    if (KMSDRM_ReconfigureWindow(_this, window)) {
-        SDL_SetError("Can't reconfigure window on SetWindowSize.");
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    if (!viddata->vulkan_mode) {
+        KMSDRM_ReconfigureWindow(_this, window);
     }
 }
 void
 KMSDRM_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
 {
-    if (KMSDRM_ReconfigureWindow(_this, window)) {
-        SDL_SetError("Can't reconfigure window on SetWindowFullscreen.");
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    if (!viddata->vulkan_mode) {
+        KMSDRM_ReconfigureWindow(_this, window);
     }
 }
 void
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index d90f3ed..74af9f0 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -62,6 +62,7 @@ typedef struct SDL_DisplayData
     drmModeCrtc *crtc;
     drmModeModeInfo mode;
     drmModeModeInfo original_mode;
+    drmModeModeInfo next_mode; /* New mode to be set on the CRTC. */
 
     drmModeCrtc *saved_crtc;    /* CRTC to restore on quit */
 
@@ -74,9 +75,11 @@ typedef struct SDL_DisplayData
     struct gbm_bo *cursor_bo;
     uint64_t cursor_w, cursor_h;
 
+    SDL_bool set_default_cursor_pending;
     SDL_bool modeset_pending;
 
-    SDL_bool set_default_cursor_pending;
+    uint32_t surface_w;
+    uint32_t surface_h;
 
 } SDL_DisplayData;
 
@@ -107,6 +110,7 @@ typedef struct KMSDRM_FBInfo
 /* Helper functions */
 int KMSDRM_CreateSurfaces(_THIS, SDL_Window * window);
 KMSDRM_FBInfo *KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo);
+KMSDRM_FBInfo *KMSDRM_FBFromBO2(_THIS, struct gbm_bo *bo, int w, int h);
 SDL_bool KMSDRM_WaitPageFlip(_THIS, SDL_WindowData *windata, int timeout);
 
 /****************************************************************************/