0042-egl-null-add-support-for-DRM-image-format-modifiers.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. From dbf265558ceb9688f5ffd626ab8969bd836ca60f Mon Sep 17 00:00:00 2001
  2. From: Luigi Santivetti <luigi.santivetti@imgtec.com>
  3. Date: Thu, 26 Sep 2019 13:32:15 +0100
  4. Subject: [PATCH] egl/null: add support for DRM image format modifiers
  5. This change introduces support for image modifiers to platform_null. In
  6. order for it to create an image with modifiers, it relies on libdrm to
  7. iterate all formats with associated modifiers supported by the display
  8. for the primary drm plane in use.
  9. drmModeFormatModifierBlobIterNext() is added to the DRM api in a different
  10. change and it is not upstream at present.
  11. Internal notes:
  12. [1] IN_FORMATS blobs are available since kernel 4.14:
  13. - db1689aa61bd1efb5ce9b896e7aa860a85b7f1b6
  14. - https://patchwork.freedesktop.org/patch/168543
  15. [2] the dri image->base.version threshold is 14.
  16. - Unlike for platform_wayland, where no details were found regarding
  17. why it's using 15
  18. - dri_interface.h makes createImageWithModifiers available since
  19. version 14
  20. - dri/gbm_dri.c as an example checks for minimum version 14.
  21. Change-Id: I0f7b030f6e1943690692674bf18daabfc153208a
  22. Signed-off-by: Luigi Santivetti <luigi.santivetti@imgtec.com>
  23. ---
  24. meson.build | 5 +
  25. src/egl/drivers/dri2/egl_dri2.h | 3 +
  26. src/egl/drivers/dri2/platform_null.c | 221 +++++++++++++++++++++++----
  27. 3 files changed, 203 insertions(+), 26 deletions(-)
  28. diff --git a/meson.build b/meson.build
  29. index 21d93d3..ca89b8a 100644
  30. --- a/meson.build
  31. +++ b/meson.build
  32. @@ -1581,6 +1581,11 @@ if with_gallium_etnaviv
  33. _drm_ver = '2.4.89'
  34. endif
  35. +# platform_null relies on DRM format-modifier iterators available since 2.4.108
  36. +if with_platform_null
  37. + _drm_ver = '2.4.108'
  38. +endif
  39. +
  40. # Loop over the enables versions and get the highest libdrm requirement for all
  41. # active drivers.
  42. _drm_blame = ''
  43. diff --git a/src/egl/drivers/dri2/egl_dri2.h b/src/egl/drivers/dri2/egl_dri2.h
  44. index eb6e455..32ad720 100644
  45. --- a/src/egl/drivers/dri2/egl_dri2.h
  46. +++ b/src/egl/drivers/dri2/egl_dri2.h
  47. @@ -117,6 +117,8 @@ struct display_output {
  48. uint32_t mode_blob_id;
  49. unsigned formats;
  50. drmModeAtomicReq *atomic_state;
  51. + uint32_t in_formats_id;
  52. + struct u_vector modifiers;
  53. };
  54. #endif
  55. @@ -280,6 +282,7 @@ struct dri2_egl_display
  56. #ifdef HAVE_NULL_PLATFORM
  57. bool atomic_enabled;
  58. + bool in_formats_enabled;
  59. struct display_output output;
  60. #endif
  61. diff --git a/src/egl/drivers/dri2/platform_null.c b/src/egl/drivers/dri2/platform_null.c
  62. index d36dc0c..2c79199 100644
  63. --- a/src/egl/drivers/dri2/platform_null.c
  64. +++ b/src/egl/drivers/dri2/platform_null.c
  65. @@ -40,6 +40,7 @@
  66. #include <sys/types.h>
  67. #include <sys/stat.h>
  68. #include <unistd.h>
  69. +#include <util/u_vector.h>
  70. #include <xf86drm.h>
  71. #include "egl_dri2.h"
  72. @@ -156,6 +157,18 @@ format_idx_get_from_drm_format(uint32_t drm_format)
  73. return -1;
  74. }
  75. +static inline uint32_t
  76. +blob_id_from_property_value(uint64_t prop_value)
  77. +{
  78. + /* The KMS properties documetation, 01.org/linuxgraphics, says:
  79. + *
  80. + * For all property types except blob properties the value is a 64-bit
  81. + * unsigned integer.
  82. + */
  83. + assert(!(prop_value >> 32));
  84. + return (uint32_t) prop_value;
  85. +}
  86. +
  87. static int
  88. atomic_state_add_object_properties(drmModeAtomicReq *atomic_state,
  89. const struct object_property *props,
  90. @@ -645,7 +658,66 @@ drm_event_process(int fd)
  91. }
  92. static bool
  93. -display_output_init(int fd, struct display_output *output, bool use_atomic)
  94. +plane_init_in_formats(int fd, drmModePlane *plane, struct u_vector *modifiers,
  95. + uint32_t *in_formats_id_out, unsigned *formats_out)
  96. +{
  97. + uint32_t blob_id, prev_fmt = DRM_FORMAT_INVALID, count_formats = 0;
  98. + drmModeFormatModifierIterator drm_iter = {0};
  99. + drmModePropertyBlobRes *blob;
  100. + uint64_t prop_value;
  101. + int idx, err;
  102. +
  103. + assert(plane && in_formats_id_out && formats_out);
  104. +
  105. + err = !object_property_value_for_name(fd, plane->plane_id,
  106. + DRM_MODE_OBJECT_PLANE,
  107. + "IN_FORMATS", &prop_value);
  108. + if (err)
  109. + return false;
  110. +
  111. + blob_id = blob_id_from_property_value(prop_value);
  112. + blob = drmModeGetPropertyBlob(fd, blob_id);
  113. +
  114. + while (drmModeFormatModifierBlobIterNext(blob, &drm_iter)) {
  115. + if (drm_iter.fmt != prev_fmt) {
  116. + prev_fmt = drm_iter.fmt;
  117. + count_formats++;
  118. +
  119. + idx = format_idx_get_from_drm_format(drm_iter.fmt);
  120. + if (idx < 0)
  121. + continue;
  122. +
  123. + *formats_out |= (1 << idx);
  124. + }
  125. + }
  126. +
  127. + drmModeFreePropertyBlob(blob);
  128. +
  129. + if (!count_formats) {
  130. + /* None of the formats in the IN_FORMATS blob has associated modifiers */
  131. + _eglLog(_EGL_WARNING, "no format-modifiers found in IN_FORMATS");
  132. + return false;
  133. + }
  134. +
  135. + if (plane->count_formats != count_formats)
  136. + /* Only some of the formats in the IN_FORMATS blob have associated modifiers,
  137. + * try to use this subset.
  138. + */
  139. + _eglLog(_EGL_WARNING, "discarding formats without modifiers");
  140. +
  141. + /* Allocate space for modifiers, if ENOMEM fallback to plane formats */
  142. + if (!u_vector_init(modifiers, sizeof(uint64_t), 64)) {
  143. + _eglLog(_EGL_WARNING, "failed to allocate modifiers");
  144. + return false;
  145. + }
  146. +
  147. + *in_formats_id_out = blob_id;
  148. + return true;
  149. +}
  150. +
  151. +static bool
  152. +display_output_init(int fd, struct display_output *output, bool use_atomic,
  153. + bool prefer_in_formats, bool *in_formats_enabled_out)
  154. {
  155. drmModeRes *resources;
  156. drmModeConnector *connector;
  157. @@ -674,16 +746,34 @@ display_output_init(int fd, struct display_output *output, bool use_atomic)
  158. goto err_free_plane;
  159. output->mode = connector->modes[mode_idx];
  160. - /* Record the display supported formats */
  161. - for (unsigned i = 0; i < plane->count_formats; i++) {
  162. - int format_idx;
  163. + assert(in_formats_enabled_out && !(*in_formats_enabled_out));
  164. - format_idx = format_idx_get_from_drm_format(plane->formats[i]);
  165. - if (format_idx == -1)
  166. - continue;
  167. + /* Track display supported formats. Look them up from IN_FORMATS blobs
  168. + * if they are available, otherwise use plane formats.
  169. + */
  170. + if (prefer_in_formats)
  171. + *in_formats_enabled_out = plane_init_in_formats(fd, plane,
  172. + &output->modifiers,
  173. + &output->in_formats_id,
  174. + &output->formats);
  175. - output->formats |= (1 << format_idx);
  176. + if (!*in_formats_enabled_out) {
  177. + _eglLog(_EGL_WARNING, "fallback to plane formats");
  178. +
  179. + for (unsigned i = 0; i < plane->count_formats; i++) {
  180. + int format_idx;
  181. +
  182. + format_idx = format_idx_get_from_drm_format(plane->formats[i]);
  183. + if (format_idx == -1)
  184. + continue;
  185. +
  186. + output->formats |= (1 << format_idx);
  187. + }
  188. }
  189. +
  190. + /* At this point we can only shut down if the look up failed and
  191. + * it is safe to pass NULL to drmModeFreeFormats().
  192. + */
  193. if (!output->formats)
  194. goto err_free_plane;
  195. @@ -983,10 +1073,12 @@ static bool
  196. add_fb_for_dri_image(struct dri2_egl_display *dri2_dpy, __DRIimage *image,
  197. uint32_t *fb_id_out)
  198. {
  199. + uint64_t modifiers[4] = {0};
  200. uint32_t handles[4] = {0};
  201. uint32_t pitches[4] = {0};
  202. uint32_t offsets[4] = {0};
  203. - int handle, stride, width, height, format;
  204. + uint32_t flags = 0;
  205. + int handle, stride, width, height, format, l_mod, h_mod;
  206. int format_idx;
  207. dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &handle);
  208. @@ -1001,9 +1093,47 @@ add_fb_for_dri_image(struct dri2_egl_display *dri2_dpy, __DRIimage *image,
  209. format_idx = format_idx_get_from_dri_image_format(format);
  210. assert(format_idx != -1);
  211. - return !drmModeAddFB2(dri2_dpy->fd, width, height,
  212. - dri2_null_formats[format_idx].drm_format,
  213. - handles, pitches, offsets, fb_id_out, 0);
  214. + if (dri2_dpy->in_formats_enabled) {
  215. + dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER, &h_mod);
  216. + dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER, &l_mod);
  217. +
  218. + modifiers[0] = combine_u32_into_u64((uint32_t) h_mod, (uint32_t) l_mod);
  219. + flags |= DRM_MODE_FB_MODIFIERS;
  220. + }
  221. +
  222. + return !drmModeAddFB2WithModifiers(dri2_dpy->fd, width, height,
  223. + dri2_null_formats[format_idx].drm_format,
  224. + handles, pitches, offsets, modifiers,
  225. + fb_id_out, flags);
  226. +}
  227. +
  228. +static __DRIimage *
  229. +create_image(struct dri2_egl_surface *dri2_surf, uint32_t flags)
  230. +{
  231. + struct dri2_egl_display *dri2_dpy =
  232. + dri2_egl_display(dri2_surf->base.Resource.Display);
  233. + uint32_t count_modifiers;
  234. + uint64_t *modifiers;
  235. +
  236. + if (dri2_dpy->in_formats_enabled) {
  237. + count_modifiers = u_vector_length(&dri2_dpy->output.modifiers);
  238. + modifiers = u_vector_tail(&dri2_dpy->output.modifiers);
  239. +
  240. + return dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen,
  241. + dri2_surf->base.Width,
  242. + dri2_surf->base.Height,
  243. + dri2_surf->format,
  244. + modifiers,
  245. + count_modifiers,
  246. + NULL);
  247. + }
  248. +
  249. + return dri2_dpy->image->createImage(dri2_dpy->dri_screen,
  250. + dri2_surf->base.Width,
  251. + dri2_surf->base.Height,
  252. + dri2_surf->format,
  253. + flags,
  254. + NULL);
  255. }
  256. static bool
  257. @@ -1016,12 +1146,7 @@ get_front_bo(struct dri2_egl_surface *dri2_surf)
  258. if (dri2_surf->base.Type == EGL_WINDOW_BIT)
  259. use |= __DRI_IMAGE_USE_SCANOUT;
  260. - dri2_surf->front = dri2_dpy->image->createImage(dri2_dpy->dri_screen,
  261. - dri2_surf->base.Width,
  262. - dri2_surf->base.Height,
  263. - dri2_surf->format,
  264. - use,
  265. - NULL);
  266. + dri2_surf->front = create_image(dri2_surf, use);
  267. if (!dri2_surf->front)
  268. return false;
  269. @@ -1058,13 +1183,8 @@ get_back_bo(struct dri2_egl_surface *dri2_surf)
  270. }
  271. if (!dri2_surf->back->dri_image) {
  272. - dri2_surf->back->dri_image =
  273. - dri2_dpy->image->createImage(dri2_dpy->dri_screen,
  274. - dri2_surf->base.Width,
  275. - dri2_surf->base.Height,
  276. - dri2_surf->format,
  277. - __DRI_IMAGE_USE_SCANOUT,
  278. - NULL);
  279. + dri2_surf->back->dri_image = create_image(dri2_surf,
  280. + __DRI_IMAGE_USE_SCANOUT);
  281. if (!dri2_surf->back->dri_image)
  282. goto err_unlock;
  283. }
  284. @@ -1094,6 +1214,30 @@ static void surface_swap_queue_init(struct dri2_egl_surface *dri2_surf)
  285. swap_queue[i].kms_in_fence_fd = -1;
  286. }
  287. +static bool
  288. +in_formats_get_modifiers(const int fd, const uint32_t in_formats_id,
  289. + const int drm_format, struct u_vector *modifiers)
  290. +{
  291. + drmModeFormatModifierIterator drm_iter = {0};
  292. + drmModePropertyBlobRes *blob;
  293. + uint64_t *mod = NULL;
  294. +
  295. + blob = drmModeGetPropertyBlob(fd, in_formats_id);
  296. +
  297. + while (drmModeFormatModifierBlobIterNext(blob, &drm_iter)) {
  298. + if (drm_iter.fmt == drm_format) {
  299. + assert(drm_iter.mod != DRM_FORMAT_MOD_INVALID);
  300. +
  301. + mod = u_vector_add(modifiers);
  302. + *mod = drm_iter.mod;
  303. + }
  304. + }
  305. +
  306. + drmModeFreePropertyBlob(blob);
  307. +
  308. + return mod != NULL;
  309. +}
  310. +
  311. static _EGLSurface *
  312. create_surface(_EGLDisplay *disp, _EGLConfig *config, EGLint type,
  313. const EGLint *attrib_list)
  314. @@ -1105,6 +1249,7 @@ create_surface(_EGLDisplay *disp, _EGLConfig *config, EGLint type,
  315. const __DRIconfig *dri_config;
  316. _EGLSurface *surf;
  317. int format_idx;
  318. + bool ret;
  319. dri2_surf = calloc(1, sizeof(*dri2_surf));
  320. if (!dri2_surf) {
  321. @@ -1137,6 +1282,15 @@ create_surface(_EGLDisplay *disp, _EGLConfig *config, EGLint type,
  322. dri2_surf->format = dri2_null_formats[format_idx].dri_image_format;
  323. + if (dri2_dpy->in_formats_enabled) {
  324. + ret = in_formats_get_modifiers(dri2_dpy->fd,
  325. + dri2_dpy->output.in_formats_id,
  326. + dri2_null_formats[format_idx].drm_format,
  327. + &dri2_dpy->output.modifiers);
  328. + if (!ret)
  329. + goto err_free_surface;
  330. + }
  331. +
  332. surface_swap_queue_init(dri2_surf);
  333. return surf;
  334. @@ -1536,6 +1690,8 @@ EGLBoolean
  335. dri2_initialize_null(_EGLDisplay *disp)
  336. {
  337. struct dri2_egl_display *dri2_dpy;
  338. + bool prefer_in_formats = false;
  339. + uint64_t value;
  340. int err;
  341. dri2_dpy = calloc(1, sizeof(*dri2_dpy));
  342. @@ -1589,8 +1745,19 @@ dri2_initialize_null(_EGLDisplay *disp)
  343. goto cleanup;
  344. }
  345. + err = drmGetCap(dri2_dpy->fd_dpy, DRM_CAP_ADDFB2_MODIFIERS, &value);
  346. + if (!err && value) {
  347. + /* in_formats could be supported by the platform, however not being
  348. + * actually enabled, i.e. in_formats init can still fail.
  349. + */
  350. + prefer_in_formats = dri2_dpy->image->base.version >= 14 &&
  351. + dri2_dpy->image->createImageWithModifiers;
  352. + }
  353. +
  354. if (!display_output_init(dri2_dpy->fd, &dri2_dpy->output,
  355. - dri2_dpy->atomic_enabled)) {
  356. + dri2_dpy->atomic_enabled,
  357. + prefer_in_formats,
  358. + &dri2_dpy->in_formats_enabled)) {
  359. _eglError(EGL_NOT_INITIALIZED, "failed to create output");
  360. goto cleanup;
  361. }
  362. @@ -1640,4 +1807,6 @@ dri2_teardown_null(struct dri2_egl_display *dri2_dpy)
  363. drmModeFreeProperty(dri2_dpy->output.connector_prop_res[i]);
  364. free(dri2_dpy->output.connector_prop_res);
  365. }
  366. +
  367. + u_vector_finish(&dri2_dpy->output.modifiers);
  368. }