Re: [Mesa-dev] [PATCH kmscube v3] formats: use weston's egl config matching logic, centralize format

2018-02-16 Thread Daniel Stone
Hi Ilia,

On 10 February 2018 at 01:49, Ilia Mirkin  wrote:
> The GBM surface format has to match the DRM mode. Both are used in a
> couple of places, so unify it so that it's only set in one place. Note
> that the GBM and DRM formats are identical.

Pushed this one to master with review, thanks for doing this!

Cheers,
Daniel
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


[Mesa-dev] [PATCH kmscube v3] formats: use weston's egl config matching logic, centralize format

2018-02-09 Thread Ilia Mirkin
The GBM surface format has to match the DRM mode. Both are used in a
couple of places, so unify it so that it's only set in one place. Note
that the GBM and DRM formats are identical.

Signed-off-by: Ilia Mirkin 
Reviewed-by: Eric Engestrom 
---

v2 -> v3:
 - get rid of format defines in common.h
 - use gbm format for drmModeAddFB2 as they're the same formats

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

diff --git a/common.c b/common.c
index b76c994..a092cdb 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_XRGB;
 
 #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_XRGB,
+   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();
}
gbm.surface = gbm_surface_create_with_modifiers(gbm.dev, w, h,
-   GBM_FORMAT_XRGB, 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,
+   ))
+   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 < 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) {
+   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, >config, 1, ) 
|| n != 1) {
-   printf("failed to choose config: %d\n", n);
+   if (!egl_choose_config(egl->display, config_attribs, gbm->format,
+   >config)) {
+   printf("failed to choose config\n");
return -1;
}
 
diff --git a/common.h b/common.h
index 11ec26e..dc87825 100644
--- a/common.h
+++ b/common.h
@@ -60,6 +60,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..1ec2820 100644
--- a/drm-common.c
+++ b/drm-common.c
@@ -47,7 +47,7 @@ struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo)
 {
int drm_fd = gbm_device_get_fd(gbm_bo_get_device(bo));
struct drm_fb *fb =