0054-egl-null-add-support-for-DRI_PRIME-GPU-selection.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. From e92a09b72b44a0969983635803022f7dc1a50924 Mon Sep 17 00:00:00 2001
  2. From: Brendan King <Brendan.King@imgtec.com>
  3. Date: Thu, 20 May 2021 20:16:18 +0100
  4. Subject: [PATCH] egl/null: add support for DRI_PRIME GPU selection
  5. Add support for selecting the GPU to be used for rendering using
  6. the DRI_PRIME environment variable. If a different GPU is selected,
  7. a duplicate of the file descriptor for the original GPU/display is
  8. preserved, which can be obtained by calling the getDisplayFD
  9. function in the image loader extension.
  10. This change includes code, in function dri2_null_try_device, to
  11. skip display devices that are not supported by the PVR driver.
  12. This is to prevent failure on PC based test systems, that often
  13. include display hardware that is incompatible with the driver.
  14. This code would not be needed for systems that don't use the PVR
  15. driver, or on production systems that use the PVR driver.
  16. ---
  17. src/egl/drivers/dri2/egl_dri2.c | 1 +
  18. src/egl/drivers/dri2/platform_null.c | 133 +++++++++++++++++++--------
  19. 2 files changed, 98 insertions(+), 36 deletions(-)
  20. diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
  21. index ec4ac60..56b5175 100644
  22. --- a/src/egl/drivers/dri2/egl_dri2.c
  23. +++ b/src/egl/drivers/dri2/egl_dri2.c
  24. @@ -1369,6 +1369,7 @@ dri2_display_destroy(_EGLDisplay *disp)
  25. switch (disp->Platform) {
  26. case _EGL_PLATFORM_DRM:
  27. + case _EGL_PLATFORM_NULL:
  28. case _EGL_PLATFORM_WAYLAND:
  29. case _EGL_PLATFORM_X11:
  30. if (dri2_dpy->fd_dpy >= 0 && dri2_dpy->fd_dpy != dri2_dpy->fd)
  31. diff --git a/src/egl/drivers/dri2/platform_null.c b/src/egl/drivers/dri2/platform_null.c
  32. index 8efac19..0d81151 100644
  33. --- a/src/egl/drivers/dri2/platform_null.c
  34. +++ b/src/egl/drivers/dri2/platform_null.c
  35. @@ -45,6 +45,7 @@
  36. #include "egl_dri2.h"
  37. #include "loader.h"
  38. +#include "util/os_file.h"
  39. #define NULL_CARD_MINOR_MAX 63U
  40. @@ -975,7 +976,7 @@ swap_idle_get_target_frame(struct dri2_egl_surface *dri2_surf,
  41. * current vblank by the number of intervals set at the time swapBuffer
  42. * is called. For intervals of 1 or 0, we don't need a target frame.
  43. */
  44. - err = display_get_vblank_sequence(dri2_dpy->fd, current_vblank_out);
  45. + err = display_get_vblank_sequence(dri2_dpy->fd_dpy, current_vblank_out);
  46. if (err)
  47. return err;
  48. @@ -1025,7 +1026,7 @@ swap_vblank_state_transition(struct dri2_egl_surface *dri2_surf,
  49. uint32_t flags = DRM_VBLANK_ABSOLUTE | DRM_VBLANK_EVENT;
  50. int err;
  51. - err = display_request_vblank(dri2_dpy->fd, target_frame,
  52. + err = display_request_vblank(dri2_dpy->fd_dpy, target_frame,
  53. flags, dri2_surf);
  54. if (err) {
  55. dri2_surf->swap_state = SWAP_ERROR;
  56. @@ -1051,7 +1052,7 @@ swap_flip_state_transition(struct dri2_egl_surface *dri2_surf)
  57. flags |= DRM_MODE_PAGE_FLIP_ASYNC;
  58. }
  59. - err = display_output_flip(dri2_dpy->fd, &dri2_dpy->output,
  60. + err = display_output_flip(dri2_dpy->fd_dpy, &dri2_dpy->output,
  61. dri2_surf->swap_data->fb_id, flags, dri2_surf);
  62. if (err) {
  63. dri2_surf->swap_state = SWAP_ERROR;
  64. @@ -1071,7 +1072,7 @@ swap_poll_state_transition(struct dri2_egl_surface *dri2_surf)
  65. int err;
  66. /* dri2_surf->swap_state is being set inside the handler */
  67. - err = drm_event_process(dri2_dpy->fd);
  68. + err = drm_event_process(dri2_dpy->fd_dpy);
  69. if (err) {
  70. dri2_surf->swap_state = SWAP_ERROR;
  71. return err;
  72. @@ -1226,7 +1227,7 @@ add_fb_for_dri_image(struct dri2_egl_display *dri2_dpy, __DRIimage *image,
  73. handles[i] = (uint32_t) handle;
  74. }
  75. - return !drmModeAddFB2WithModifiers(dri2_dpy->fd, width, height,
  76. + return !drmModeAddFB2WithModifiers(dri2_dpy->fd_dpy, width, height,
  77. dri2_null_formats[format_idx].drm_format,
  78. handles, pitches, offsets, modifiers,
  79. fb_id_out, flags);
  80. @@ -1494,7 +1495,7 @@ dri2_null_create_window_surface(_EGLDisplay *disp, _EGLConfig *config,
  81. goto err_destroy_surface;
  82. }
  83. - err = display_output_modeset(dri2_dpy->fd, &dri2_dpy->output,
  84. + err = display_output_modeset(dri2_dpy->fd_dpy, &dri2_dpy->output,
  85. dri2_surf->front_buffer.fb_id);
  86. if (err) {
  87. _eglError(EGL_BAD_NATIVE_WINDOW, "window set mode");
  88. @@ -1608,11 +1609,11 @@ dri2_null_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
  89. dri2_dpy->image->destroyImage(dri2_surf->front_buffer.dri_image);
  90. if (dri2_surf->front_buffer.fb_id)
  91. - drmModeRmFB(dri2_dpy->fd, dri2_surf->front_buffer.fb_id);
  92. + drmModeRmFB(dri2_dpy->fd_dpy, dri2_surf->front_buffer.fb_id);
  93. for (unsigned i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
  94. if (dri2_surf->color_buffers[i].fb_id)
  95. - drmModeRmFB(dri2_dpy->fd, dri2_surf->color_buffers[i].fb_id);
  96. + drmModeRmFB(dri2_dpy->fd_dpy, dri2_surf->color_buffers[i].fb_id);
  97. if (dri2_surf->color_buffers[i].dri_image)
  98. dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
  99. }
  100. @@ -1769,12 +1770,22 @@ dri2_null_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
  101. (void) loaderPrivate;
  102. }
  103. +static int
  104. +dri2_null_get_display_fd(void *loaderPrivate)
  105. +{
  106. + _EGLDisplay *disp = loaderPrivate;
  107. + struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
  108. +
  109. + return dri2_dpy->fd_dpy;
  110. +}
  111. +
  112. static const __DRIimageLoaderExtension image_loader_extension = {
  113. - .base = { __DRI_IMAGE_LOADER, 2 },
  114. + .base = { __DRI_IMAGE_LOADER, 5 },
  115. .getBuffers = dri2_null_image_get_buffers,
  116. .flushFrontBuffer = dri2_null_flush_front_buffer,
  117. .getCapability = dri2_null_get_capability,
  118. + .getDisplayFD = dri2_null_get_display_fd,
  119. };
  120. static const __DRIextension *image_loader_extensions[] = {
  121. @@ -1803,12 +1814,74 @@ dri2_null_device_is_kms(int fd)
  122. return is_kms;
  123. }
  124. +static bool
  125. +dri2_null_try_device(_EGLDisplay *disp)
  126. +{
  127. + struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
  128. +
  129. + if (!dri2_null_device_is_kms(dri2_dpy->fd_dpy))
  130. + return false;
  131. +
  132. +#if 1
  133. + /* Skip devices not supported by the pvr driver */
  134. + {
  135. + char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd_dpy);
  136. + bool skip = !driver_name || !!strcmp(driver_name, "pvr");
  137. +
  138. + free(driver_name);
  139. +
  140. + if (skip)
  141. + return false;
  142. + }
  143. +#endif
  144. +
  145. + dri2_dpy->fd = os_dupfd_cloexec(dri2_dpy->fd_dpy);
  146. + if (dri2_dpy->fd < 0) {
  147. + _eglLog(_EGL_WARNING, "DRI2: failed to dup display FD");
  148. + dri2_dpy->fd = dri2_dpy->fd_dpy;
  149. + } else {
  150. + int fd_old;
  151. + bool is_different_gpu;
  152. +
  153. + fd_old = dri2_dpy->fd;
  154. + dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
  155. + &is_different_gpu);
  156. + if (dri2_dpy->fd == fd_old) {
  157. + close (dri2_dpy->fd);
  158. + dri2_dpy->fd = dri2_dpy->fd_dpy;
  159. + }
  160. + }
  161. +
  162. + dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
  163. + if (!dri2_dpy->driver_name)
  164. + return false;
  165. +
  166. + if (dri2_load_driver_dri3(disp)) {
  167. + _EGLDevice *dev = _eglAddDevice(dri2_dpy->fd, false);
  168. + if (!dev) {
  169. + dlclose(dri2_dpy->driver);
  170. + _eglLog(_EGL_WARNING, "DRI2: failed to find EGLDevice");
  171. + } else {
  172. + dri2_dpy->loader_extensions = image_loader_extensions;
  173. + dri2_dpy->own_device = 1;
  174. + disp->Device = dev;
  175. + return true;
  176. + }
  177. + }
  178. +
  179. + free(dri2_dpy->driver_name);
  180. + dri2_dpy->driver_name = NULL;
  181. +
  182. + return false;
  183. +}
  184. +
  185. static bool
  186. dri2_null_probe_device(_EGLDisplay *disp)
  187. {
  188. struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
  189. dri2_dpy->fd = -1;
  190. + dri2_dpy->fd_dpy = -1;
  191. for (unsigned i = 0; i <= NULL_CARD_MINOR_MAX; i++) {
  192. char *card_path;
  193. @@ -1816,32 +1889,20 @@ dri2_null_probe_device(_EGLDisplay *disp)
  194. if (asprintf(&card_path, DRM_DEV_NAME, DRM_DIR_NAME, i) < 0)
  195. continue;
  196. - dri2_dpy->fd = loader_open_device(card_path);
  197. + dri2_dpy->fd_dpy = loader_open_device(card_path);
  198. free(card_path);
  199. - if (dri2_dpy->fd < 0)
  200. + if (dri2_dpy->fd_dpy < 0)
  201. continue;
  202. - if (dri2_null_device_is_kms(dri2_dpy->fd)) {
  203. - dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
  204. - if (dri2_dpy->driver_name) {
  205. - if (dri2_load_driver_dri3(disp)) {
  206. - _EGLDevice *dev = _eglAddDevice(dri2_dpy->fd, false);
  207. - if (!dev) {
  208. - dlclose(dri2_dpy->driver);
  209. - _eglLog(_EGL_WARNING, "DRI2: failed to find EGLDevice");
  210. - } else {
  211. - dri2_dpy->loader_extensions = image_loader_extensions;
  212. - dri2_dpy->own_device = 1;
  213. - disp->Device = dev;
  214. - return true;
  215. - }
  216. - }
  217. - free(dri2_dpy->driver_name);
  218. - dri2_dpy->driver_name = NULL;
  219. - }
  220. - }
  221. + if (dri2_null_try_device(disp))
  222. + return true;
  223. +
  224. + close(dri2_dpy->fd_dpy);
  225. +
  226. + if (dri2_dpy->fd >= 0 && dri2_dpy->fd != dri2_dpy->fd_dpy)
  227. + close(dri2_dpy->fd);
  228. - close(dri2_dpy->fd);
  229. + dri2_dpy->fd_dpy = -1;
  230. dri2_dpy->fd = -1;
  231. }
  232. @@ -1902,7 +1963,7 @@ dri2_null_setup_swap_interval(_EGLDisplay *disp)
  233. dri2_setup_swap_interval(disp, swap_max_interval);
  234. - err = drmGetCap(dri2_dpy->fd, DRM_CAP_ASYNC_PAGE_FLIP, &value);
  235. + err = drmGetCap(dri2_dpy->fd_dpy, DRM_CAP_ASYNC_PAGE_FLIP, &value);
  236. if (err || value == 0) {
  237. /* DRM/KMS does not support async page flip. In order to support
  238. @@ -1950,7 +2011,7 @@ dri2_initialize_null(_EGLDisplay *disp)
  239. * modesetting if not. If this succeeds then universal planes will also have
  240. * been enabled.
  241. */
  242. - err = drmSetClientCap(dri2_dpy->fd, DRM_CLIENT_CAP_ATOMIC, 1);
  243. + err = drmSetClientCap(dri2_dpy->fd_dpy, DRM_CLIENT_CAP_ATOMIC, 1);
  244. dri2_dpy->atomic_enabled = !err;
  245. if (!dri2_dpy->atomic_enabled) {
  246. @@ -1958,7 +2019,7 @@ dri2_initialize_null(_EGLDisplay *disp)
  247. * Enable universal planes so that we can get the pixel formats for the
  248. * primary plane
  249. */
  250. - err = drmSetClientCap(dri2_dpy->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  251. + err = drmSetClientCap(dri2_dpy->fd_dpy, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  252. if (err) {
  253. _eglError(EGL_NOT_INITIALIZED, "failed to enable universal planes");
  254. goto cleanup;
  255. @@ -1994,7 +2055,7 @@ dri2_initialize_null(_EGLDisplay *disp)
  256. dri2_dpy->image->createImageWithModifiers;
  257. }
  258. - if (!display_output_init(dri2_dpy->fd, &dri2_dpy->output,
  259. + if (!display_output_init(dri2_dpy->fd_dpy, &dri2_dpy->output,
  260. dri2_dpy->atomic_enabled,
  261. prefer_in_formats,
  262. &dri2_dpy->in_formats_enabled)) {
  263. @@ -2028,7 +2089,7 @@ dri2_teardown_null(struct dri2_egl_display *dri2_dpy)
  264. drmModeAtomicFree(dri2_dpy->output.atomic_state);
  265. if (dri2_dpy->output.mode_blob_id)
  266. - drmModeDestroyPropertyBlob(dri2_dpy->fd, dri2_dpy->output.mode_blob_id);
  267. + drmModeDestroyPropertyBlob(dri2_dpy->fd_dpy, dri2_dpy->output.mode_blob_id);
  268. if (dri2_dpy->output.plane_prop_res) {
  269. for (unsigned i = 0; dri2_dpy->output.plane_prop_res[i]; i++)