The GBM surface format has to match the DRM mode. Both are used in a
couple of places, so move the defines to a common place so that they can
be adjusted easily.

Signed-off-by: Ilia Mirkin <imir...@alum.mit.edu>
Reviewed-by: Eric Engestrom <eric.engest...@imgtec.com>
---

v1 -> v2:
 - Take Eric's review comments into account
 - Use gbm format from the 'gbm' structure (sorta addressing Daniel's
   comment)

 common.c     | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 common.h     |  4 ++++
 drm-common.c |  4 ++--
 3 files changed, 79 insertions(+), 7 deletions(-)

diff --git a/common.c b/common.c
index b76c994..c23d677 100644
--- a/common.c
+++ b/common.c
@@ -47,6 +47,7 @@ get_modifiers(uint64_t **mods)
 const struct gbm * init_gbm(int drm_fd, int w, int h, uint64_t modifier)
 {
        gbm.dev = gbm_create_device(drm_fd);
+       gbm.format = GBM_FORMAT;
 
 #ifndef HAVE_GBM_MODIFIERS
        if (modifier != DRM_FORMAT_MOD_INVALID) {
@@ -54,7 +55,7 @@ const struct gbm * init_gbm(int drm_fd, int w, int h, 
uint64_t modifier)
                return NULL;
        }
        gbm.surface = gbm_surface_create(gbm.dev, w, h,
-                       GBM_FORMAT_XRGB8888,
+                       gbm.format,
                        GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
 #else
        uint64_t *mods;
@@ -66,7 +67,7 @@ const struct gbm * init_gbm(int drm_fd, int w, int h, 
uint64_t modifier)
                count = get_modifiers(&mods);
        }
        gbm.surface = gbm_surface_create_with_modifiers(gbm.dev, w, h,
-                       GBM_FORMAT_XRGB8888, mods, count);
+                       gbm.format, mods, count);
 #endif
 
        if (!gbm.surface) {
@@ -100,9 +101,75 @@ static bool has_ext(const char *extension_list, const char 
*ext)
        }
 }
 
+static int
+match_config_to_visual(EGLDisplay egl_display,
+                      EGLint visual_id,
+                      EGLConfig *configs,
+                      int count)
+{
+       int i;
+
+       for (i = 0; i < count; ++i) {
+               EGLint id;
+
+               if (!eglGetConfigAttrib(egl_display,
+                               configs[i], EGL_NATIVE_VISUAL_ID,
+                               &id))
+                       continue;
+
+               if (id == visual_id)
+                       return i;
+       }
+
+       return -1;
+}
+
+static bool
+egl_choose_config(EGLDisplay egl_display, const EGLint *attribs,
+                  EGLint visual_id, EGLConfig *config_out)
+{
+       EGLint count = 0;
+       EGLint matched = 0;
+       EGLConfig *configs;
+       int config_index = -1;
+
+       if (!eglGetConfigs(egl_display, NULL, 0, &count) || count < 1) {
+               printf("No EGL configs to choose from.\n");
+               return false;
+       }
+       configs = malloc(count * sizeof *configs);
+       if (!configs)
+               return false;
+
+       if (!eglChooseConfig(egl_display, attribs, configs,
+                             count, &matched) || !matched) {
+               printf("No EGL configs with appropriate attributes.\n");
+               goto out;
+       }
+
+       if (!visual_id)
+               config_index = 0;
+
+       if (config_index == -1)
+               config_index = match_config_to_visual(egl_display,
+                                                     visual_id,
+                                                     configs,
+                                                     matched);
+
+       if (config_index != -1)
+               *config_out = configs[config_index];
+
+out:
+       free(configs);
+       if (config_index == -1)
+               return false;
+
+       return true;
+}
+
 int init_egl(struct egl *egl, const struct gbm *gbm)
 {
-       EGLint major, minor, n;
+       EGLint major, minor;
 
        static const EGLint context_attribs[] = {
                EGL_CONTEXT_CLIENT_VERSION, 2,
@@ -174,8 +241,9 @@ int init_egl(struct egl *egl, const struct gbm *gbm)
                return -1;
        }
 
-       if (!eglChooseConfig(egl->display, config_attribs, &egl->config, 1, &n) 
|| n != 1) {
-               printf("failed to choose config: %d\n", n);
+       if (!egl_choose_config(egl->display, config_attribs, gbm->format,
+                               &egl->config)) {
+               printf("failed to choose config\n");
                return -1;
        }
 
diff --git a/common.h b/common.h
index 11ec26e..7c3c909 100644
--- a/common.h
+++ b/common.h
@@ -40,6 +40,9 @@
 #define DRM_FORMAT_MOD_INVALID ((((__u64)0) << 56) | ((1ULL << 56) - 1))
 #endif
 
+#define GBM_FORMAT GBM_FORMAT_XRGB8888
+#define DRM_FORMAT DRM_FORMAT_XRGB8888
+
 #ifndef EGL_KHR_platform_gbm
 #define EGL_KHR_platform_gbm 1
 #define EGL_PLATFORM_GBM_KHR              0x31D7
@@ -60,6 +63,7 @@ EGLAPI EGLSurface EGLAPIENTRY 
eglCreatePlatformPixmapSurfaceEXT (EGLDisplay dpy,
 struct gbm {
        struct gbm_device *dev;
        struct gbm_surface *surface;
+       uint32_t format;
        int width, height;
 };
 
diff --git a/drm-common.c b/drm-common.c
index 4b55745..f022eb0 100644
--- a/drm-common.c
+++ b/drm-common.c
@@ -78,7 +78,7 @@ struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo)
        }
 
        ret = drmModeAddFB2WithModifiers(drm_fd, width, height,
-                       DRM_FORMAT_XRGB8888, handles, strides, offsets,
+                       DRM_FORMAT, handles, strides, offsets,
                        modifiers, &fb->fb_id, flags);
 #endif
        if (ret) {
@@ -88,7 +88,7 @@ struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo)
                memcpy(handles, (uint32_t 
[4]){gbm_bo_get_handle(bo).u32,0,0,0}, 16);
                memcpy(strides, (uint32_t [4]){gbm_bo_get_stride(bo),0,0,0}, 
16);
                memset(offsets, 0, 16);
-               ret = drmModeAddFB2(drm_fd, width, height, DRM_FORMAT_XRGB8888,
+               ret = drmModeAddFB2(drm_fd, width, height, DRM_FORMAT,
                                handles, strides, offsets, &fb->fb_id, 0);
        }
 
-- 
2.13.6

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to