SDL_kmsdrmvideo.h (8172B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 Atomic KMSDRM backend by Manuel Alfayate Corchete <redwindwanderer@gmail.com> 5 6 This software is provided 'as-is', without any express or implied 7 warranty. In no event will the authors be held liable for any damages 8 arising from the use of this software. 9 10 Permission is granted to anyone to use this software for any purpose, 11 including commercial applications, and to alter it and redistribute it 12 freely, subject to the following restrictions: 13 14 1. The origin of this software must not be misrepresented; you must not 15 claim that you wrote the original software. If you use this software 16 in a product, an acknowledgment in the product documentation would be 17 appreciated but is not required. 18 2. Altered source versions must be plainly marked as such, and must not be 19 misrepresented as being the original software. 20 3. This notice may not be removed or altered from any source distribution. 21 */ 22 23 #include "../../SDL_internal.h" 24 25 #ifndef __SDL_KMSDRMVIDEO_H__ 26 #define __SDL_KMSDRMVIDEO_H__ 27 28 #include "../SDL_sysvideo.h" 29 30 #include <fcntl.h> 31 #include <unistd.h> 32 #include <xf86drm.h> 33 #include <xf86drmMode.h> 34 35 #include <gbm.h> 36 #include <assert.h> 37 #if SDL_VIDEO_OPENGL_EGL 38 #include <EGL/egl.h> 39 #include <EGL/eglext.h> 40 #endif 41 42 /****************************************************************************************/ 43 /* Driverdata pointers are void struct* used to store backend-specific variables */ 44 /* and info that supports the SDL-side structs like SDL Display Devices, SDL_Windows... */ 45 /* which need to be "supported" with backend-side info and mechanisms to work. */ 46 /****************************************************************************************/ 47 48 typedef struct SDL_VideoData 49 { 50 int devindex; /* device index that was passed on creation */ 51 int drm_fd; /* DRM file desc */ 52 char devpath[32]; /* DRM dev path. */ 53 54 struct gbm_device *gbm_dev; 55 56 SDL_Window **windows; 57 unsigned int max_windows; 58 unsigned int num_windows; 59 60 SDL_bool video_init; /* Has VideoInit succeeded? */ 61 62 } SDL_VideoData; 63 64 typedef struct plane { 65 drmModePlane *plane; 66 drmModeObjectProperties *props; 67 drmModePropertyRes **props_info; 68 } plane; 69 70 typedef struct crtc { 71 drmModeCrtc *crtc; 72 drmModeObjectProperties *props; 73 drmModePropertyRes **props_info; 74 } crtc; 75 76 typedef struct connector { 77 drmModeConnector *connector; 78 drmModeObjectProperties *props; 79 drmModePropertyRes **props_info; 80 } connector; 81 82 /* More general driverdata info that gives support and substance to the SDL_Display. */ 83 typedef struct SDL_DisplayData 84 { 85 drmModeModeInfo mode; 86 drmModeModeInfo preferred_mode; 87 uint32_t atomic_flags; 88 89 plane *display_plane; 90 plane *cursor_plane; 91 crtc *crtc; 92 connector *connector; 93 94 /* Central atomic request list, used for the prop 95 changeset related to pageflip in SwapWindow. */ 96 drmModeAtomicReq *atomic_req; 97 98 int kms_in_fence_fd; 99 int kms_out_fence_fd; 100 101 EGLSyncKHR kms_fence; 102 EGLSyncKHR gpu_fence; 103 104 #if SDL_VIDEO_OPENGL_EGL 105 EGLSurface old_egl_surface; 106 #endif 107 108 SDL_bool modeset_pending; 109 SDL_bool gbm_init; 110 111 /* DRM & GBM cursor stuff lives here, not in an SDL_Cursor's driverdata struct, 112 because setting/unsetting up these is done on window creation/destruction, 113 where we may not have an SDL_Cursor at all (so no SDL_Cursor driverdata). 114 There's only one cursor GBM BO because we only support one cursor. */ 115 struct gbm_bo *cursor_bo; 116 uint64_t cursor_w, cursor_h; 117 118 } SDL_DisplayData; 119 120 /* Driverdata info that gives KMSDRM-side support and substance to the SDL_Window. */ 121 typedef struct SDL_WindowData 122 { 123 SDL_VideoData *viddata; 124 /* SDL internals expect EGL surface to be here, and in KMSDRM the GBM surface is 125 what supports the EGL surface on the driver side, so all these surfaces and buffers 126 are expected to be here, in the struct pointed by SDL_Window driverdata pointer: 127 this one. So don't try to move these to dispdata! */ 128 struct gbm_surface *gs; 129 struct gbm_bo *bo; 130 struct gbm_bo *next_bo; 131 132 #if SDL_VIDEO_OPENGL_EGL 133 EGLSurface egl_surface; 134 #endif 135 136 /* For scaling and AR correction. */ 137 int32_t src_w; 138 int32_t src_h; 139 int32_t output_w; 140 int32_t output_h; 141 int32_t output_x; 142 143 /* This dictates what approach we'll use for SwapBuffers. */ 144 int (*swap_window)(_THIS, SDL_Window * window); 145 146 } SDL_WindowData; 147 148 typedef struct SDL_DisplayModeData 149 { 150 int mode_index; 151 } SDL_DisplayModeData; 152 153 typedef struct KMSDRM_FBInfo 154 { 155 int drm_fd; /* DRM file desc */ 156 uint32_t fb_id; /* DRM framebuffer ID */ 157 } KMSDRM_FBInfo; 158 159 typedef struct KMSDRM_PlaneInfo 160 { 161 struct plane *plane; 162 uint32_t fb_id; 163 uint32_t crtc_id; 164 int32_t src_x; 165 int32_t src_y; 166 int32_t src_w; 167 int32_t src_h; 168 int32_t crtc_x; 169 int32_t crtc_y; 170 int32_t crtc_w; 171 int32_t crtc_h; 172 } KMSDRM_PlaneInfo; 173 174 /* Helper functions */ 175 int KMSDRM_CreateEGLSurface(_THIS, SDL_Window * window); 176 KMSDRM_FBInfo *KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo); 177 178 /* Atomic functions that are used from SDL_kmsdrmopengles.c and SDL_kmsdrmmouse.c */ 179 void drm_atomic_set_plane_props(struct KMSDRM_PlaneInfo *info); 180 181 void drm_atomic_waitpending(_THIS); 182 int drm_atomic_commit(_THIS, SDL_bool blocking); 183 int add_plane_property(drmModeAtomicReq *req, struct plane *plane, 184 const char *name, uint64_t value); 185 int add_crtc_property(drmModeAtomicReq *req, struct crtc *crtc, 186 const char *name, uint64_t value); 187 int add_connector_property(drmModeAtomicReq *req, struct connector *connector, 188 const char *name, uint64_t value); 189 int setup_plane(_THIS, struct plane **plane, uint32_t plane_type); 190 void free_plane(struct plane **plane); 191 192 /****************************************************************************/ 193 /* SDL_VideoDevice functions declaration */ 194 /****************************************************************************/ 195 196 /* Display and window functions */ 197 int KMSDRM_VideoInit(_THIS); 198 void KMSDRM_VideoQuit(_THIS); 199 void KMSDRM_GetDisplayModes(_THIS, SDL_VideoDisplay * display); 200 int KMSDRM_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode); 201 int KMSDRM_CreateWindow(_THIS, SDL_Window * window); 202 int KMSDRM_CreateWindowFrom(_THIS, SDL_Window * window, const void *data); 203 void KMSDRM_SetWindowTitle(_THIS, SDL_Window * window); 204 void KMSDRM_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon); 205 void KMSDRM_SetWindowPosition(_THIS, SDL_Window * window); 206 void KMSDRM_SetWindowSize(_THIS, SDL_Window * window); 207 void KMSDRM_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen); 208 void KMSDRM_ShowWindow(_THIS, SDL_Window * window); 209 void KMSDRM_HideWindow(_THIS, SDL_Window * window); 210 void KMSDRM_RaiseWindow(_THIS, SDL_Window * window); 211 void KMSDRM_MaximizeWindow(_THIS, SDL_Window * window); 212 void KMSDRM_MinimizeWindow(_THIS, SDL_Window * window); 213 void KMSDRM_RestoreWindow(_THIS, SDL_Window * window); 214 void KMSDRM_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed); 215 void KMSDRM_DestroyWindow(_THIS, SDL_Window * window); 216 217 /* Window manager function */ 218 SDL_bool KMSDRM_GetWindowWMInfo(_THIS, SDL_Window * window, 219 struct SDL_SysWMinfo *info); 220 221 /* OpenGL/OpenGL ES functions */ 222 int KMSDRM_GLES_LoadLibrary(_THIS, const char *path); 223 void *KMSDRM_GLES_GetProcAddress(_THIS, const char *proc); 224 void KMSDRM_GLES_UnloadLibrary(_THIS); 225 SDL_GLContext KMSDRM_GLES_CreateContext(_THIS, SDL_Window * window); 226 int KMSDRM_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context); 227 int KMSDRM_GLES_SetSwapInterval(_THIS, int interval); 228 int KMSDRM_GLES_GetSwapInterval(_THIS); 229 int KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window); 230 void KMSDRM_GLES_DeleteContext(_THIS, SDL_GLContext context); 231 232 #endif /* __SDL_KMSDRMVIDEO_H__ */ 233 234 /* vi: set ts=4 sw=4 expandtab: */