egl/dri2: Fix memory leak.
[mesa.git] / src / egl / drivers / dri2 / egl_dri2.c
index f09ae14f10c1f26a96c26d6dd9453fd379d23fa9..0ffbc68649c157aa60ffc18bb2cf0534b456b9ba 100644 (file)
@@ -111,13 +111,15 @@ dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
 
 struct dri2_egl_config *
 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
-               int depth, EGLint surface_type, const EGLint *attr_list)
+               int depth, EGLint surface_type, const EGLint *attr_list,
+               const unsigned int *rgba_masks)
 {
    struct dri2_egl_config *conf;
    struct dri2_egl_display *dri2_dpy;
    _EGLConfig base;
    unsigned int attrib, value, double_buffer;
    EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
+   unsigned int dri_masks[4] = { 0, 0, 0, 0 };
    _EGLConfig *matching_config;
    EGLint num_configs = 0;
    EGLint config_id;
@@ -165,6 +167,22 @@ dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
         double_buffer = value;
         break;
 
+      case __DRI_ATTRIB_RED_MASK:
+         dri_masks[0] = value;
+         break;
+
+      case __DRI_ATTRIB_GREEN_MASK:
+         dri_masks[1] = value;
+         break;
+
+      case __DRI_ATTRIB_BLUE_MASK:
+         dri_masks[2] = value;
+         break;
+
+      case __DRI_ATTRIB_ALPHA_MASK:
+         dri_masks[3] = value;
+         break;
+
       default:
         key = dri2_to_egl_attribute_map[attrib];
         if (key != 0)
@@ -180,6 +198,9 @@ dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
    if (depth > 0 && depth != base.BufferSize)
       return NULL;
 
+   if (rgba_masks && memcmp(rgba_masks, dri_masks, sizeof(dri_masks)))
+      return NULL;
+
    base.NativeRenderable = EGL_TRUE;
 
    base.SurfaceType = surface_type;
@@ -238,8 +259,14 @@ dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
       return NULL;
    }
 
-   conf->base.SurfaceType |= surface_type & (!double_buffer ? EGL_PIXMAP_BIT:
-         (EGL_WINDOW_BIT | EGL_PBUFFER_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT));
+   if (double_buffer) {
+      surface_type &= ~EGL_PIXMAP_BIT;
+
+      conf->base.MinSwapInterval = dri2_dpy->min_swap_interval;
+      conf->base.MaxSwapInterval = dri2_dpy->max_swap_interval;
+   }
+
+   conf->base.SurfaceType |= surface_type;
 
    return conf;
 }
@@ -279,7 +306,7 @@ struct dri2_extension_match {
 
 static struct dri2_extension_match dri2_driver_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
-   { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) },
+   { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
    { NULL, 0, 0 }
 };
 
@@ -293,12 +320,12 @@ static struct dri2_extension_match dri2_core_extensions[] = {
 static struct dri2_extension_match swrast_driver_extensions[] = {
    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
    { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
-   { NULL }
+   { NULL, 0, 0 }
 };
 
 static struct dri2_extension_match swrast_core_extensions[] = {
    { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
-   { NULL }
+   { NULL, 0, 0 }
 };
 
 static EGLBoolean
@@ -435,16 +462,13 @@ dri2_setup_screen(_EGLDisplay *disp)
    unsigned int api_mask;
 
    if (dri2_dpy->dri2) {
-      if (dri2_dpy->dri2->base.version >= 2)
-         api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
-      else
-         api_mask = 1 << __DRI_API_OPENGL;
+      api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
    } else {
       assert(dri2_dpy->swrast);
-      if (dri2_dpy->swrast->base.version >= 2)
-         api_mask = 1 << __DRI_API_OPENGL | 1 << __DRI_API_GLES | 1 << __DRI_API_GLES2;
-      else
-         api_mask = 1 << __DRI_API_OPENGL;
+      api_mask = 1 << __DRI_API_OPENGL |
+                 1 << __DRI_API_GLES |
+                 1 << __DRI_API_GLES2 |
+                 1 << __DRI_API_GLES3;
    }
 
    disp->ClientAPIs = 0;
@@ -454,26 +478,28 @@ dri2_setup_screen(_EGLDisplay *disp)
       disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
    if (api_mask & (1 << __DRI_API_GLES2))
       disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
+   if (api_mask & (1 << __DRI_API_GLES3))
+      disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
 
-   if (dri2_dpy->dri2) {
-      if (dri2_dpy->dri2->base.version >= 2) {
-         disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE;
-         disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE;
-         disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE;
-      }
-   } else {
-      assert(dri2_dpy->swrast);
-      if (dri2_dpy->swrast->base.version >= 2) {
-         disp->Extensions.KHR_surfaceless_gles1 = EGL_TRUE;
-         disp->Extensions.KHR_surfaceless_gles2 = EGL_TRUE;
-         disp->Extensions.KHR_surfaceless_opengl = EGL_TRUE;
-      }
+   assert(dri2_dpy->dri2 || dri2_dpy->swrast);
+   disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
+
+   if (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) {
+      disp->Extensions.KHR_create_context = EGL_TRUE;
+
+      if (dri2_dpy->robustness)
+         disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
    }
 
    if (dri2_dpy->image) {
       disp->Extensions.MESA_drm_image = EGL_TRUE;
       disp->Extensions.KHR_image_base = EGL_TRUE;
       disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
+      if (dri2_dpy->image->base.version >= 5 &&
+          dri2_dpy->image->createImageFromTexture) {
+         disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
+         disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
+      }
    }
 }
 
@@ -506,8 +532,19 @@ dri2_create_screen(_EGLDisplay *disp)
    extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
    
    if (dri2_dpy->dri2) {
+      unsigned i;
+
       if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
          goto cleanup_dri_screen;
+
+      for (i = 0; extensions[i]; i++) {
+        if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) {
+            dri2_dpy->robustness = (__DRIrobustnessExtension *) extensions[i];
+        }
+        if (strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0) {
+           dri2_dpy->config = (__DRI2configQueryExtension *) extensions[i];
+        }
+      }
    } else {
       assert(dri2_dpy->swrast);
       if (!dri2_bind_extensions(dri2_dpy, swrast_core_extensions, extensions))
@@ -555,6 +592,12 @@ dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
          return EGL_TRUE;
       return dri2_initialize_wayland(drv, disp);
 #endif
+#endif
+#ifdef HAVE_ANDROID_PLATFORM
+   case _EGL_PLATFORM_ANDROID:
+      if (disp->Options.TestOnly)
+         return EGL_TRUE;
+      return dri2_initialize_android(drv, disp);
 #endif
 
    default:
@@ -579,6 +622,7 @@ dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
       close(dri2_dpy->fd);
    if (dri2_dpy->driver)
       dlclose(dri2_dpy->driver);
+   free(dri2_dpy->device_name);
 
    if (disp->PlatformDisplay == NULL) {
       switch (disp->Platform) {
@@ -587,14 +631,9 @@ dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
          xcb_disconnect(dri2_dpy->conn);
          break;
 #endif
-#ifdef HAVE_WAYLAND_PLATFORM
-      case _EGL_PLATFORM_WAYLAND:
-         wl_display_destroy(dri2_dpy->wl_dpy);
-         break;
-#endif
 #ifdef HAVE_DRM_PLATFORM
       case _EGL_PLATFORM_DRM:
-         if (dri2_dpy->own_gbm_device) {
+         if (dri2_dpy->own_device) {
             gbm_device_destroy(&dri2_dpy->gbm_dri->base.base);
          }
          break;
@@ -610,6 +649,69 @@ dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
    return EGL_TRUE;
 }
 
+/**
+ * Set the error code after a call to
+ * dri2_egl_display::dri2::createContextAttribs.
+ */
+static void
+dri2_create_context_attribs_error(int dri_error)
+{
+   EGLint egl_error;
+
+   switch (dri_error) {
+   case __DRI_CTX_ERROR_SUCCESS:
+      return;
+
+   case __DRI_CTX_ERROR_NO_MEMORY:
+      egl_error = EGL_BAD_ALLOC;
+      break;
+
+  /* From the EGL_KHR_create_context spec, section "Errors":
+   *
+   *   * If <config> does not support a client API context compatible
+   *     with the requested API major and minor version, [...] context flags,
+   *     and context reset notification behavior (for client API types where
+   *     these attributes are supported), then an EGL_BAD_MATCH error is
+   *     generated.
+   *
+   *   * If an OpenGL ES context is requested and the values for
+   *     attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
+   *     EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
+   *     is not defined, than an EGL_BAD_MATCH error is generated.
+   *
+   *   * If an OpenGL context is requested, the requested version is
+   *     greater than 3.2, and the value for attribute
+   *     EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
+   *     bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
+   *     EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
+   *     one of these bits set; or if the implementation does not support
+   *     the requested profile, then an EGL_BAD_MATCH error is generated.
+   */
+   case __DRI_CTX_ERROR_BAD_API:
+   case __DRI_CTX_ERROR_BAD_VERSION:
+   case __DRI_CTX_ERROR_BAD_FLAG:
+      egl_error = EGL_BAD_MATCH;
+      break;
+
+  /* From the EGL_KHR_create_context spec, section "Errors":
+   *
+   *   * If an attribute name or attribute value in <attrib_list> is not
+   *     recognized (including unrecognized bits in bitmask attributes),
+   *     then an EGL_BAD_ATTRIBUTE error is generated."
+   */
+   case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
+   case __DRI_CTX_ERROR_UNKNOWN_FLAG:
+      egl_error = EGL_BAD_ATTRIBUTE;
+      break;
+
+   default:
+      assert(0);
+      egl_error = EGL_BAD_MATCH;
+      break;
+   }
+
+   _eglError(egl_error, "dri2_create_context");
+}
 
 /**
  * Called via eglCreateContext(), drv->API.CreateContext().
@@ -621,6 +723,8 @@ dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
    struct dri2_egl_context *dri2_ctx;
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
+   __DRIcontext *shared =
+      dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
    struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
    const __DRIconfig *dri_config;
    int api;
@@ -638,23 +742,33 @@ dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
 
    switch (dri2_ctx->base.ClientAPI) {
    case EGL_OPENGL_ES_API:
-      switch (dri2_ctx->base.ClientVersion) {
+      switch (dri2_ctx->base.ClientMajorVersion) {
       case 1:
          api = __DRI_API_GLES;
          break;
       case 2:
          api = __DRI_API_GLES2;
          break;
+      case 3:
+         api = __DRI_API_GLES3;
+         break;
       default:
         _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
         return NULL;
       }
       break;
    case EGL_OPENGL_API:
-      api = __DRI_API_OPENGL;
+      if ((dri2_ctx->base.ClientMajorVersion >= 4
+           || (dri2_ctx->base.ClientMajorVersion == 3
+               && dri2_ctx->base.ClientMinorVersion >= 2))
+          && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
+         api = __DRI_API_OPENGL_CORE;
+      else
+         api = __DRI_API_OPENGL;
       break;
    default:
       _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
+      free(dri2_ctx);
       return NULL;
    }
 
@@ -670,49 +784,84 @@ dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
          dri_config = dri2_config->dri_double_config;
       else
          dri_config = dri2_config->dri_single_config;
+
+      /* EGL_WINDOW_BIT is set only when there is a dri_double_config.  This
+       * makes sure the back buffer will always be used.
+       */
+      if (conf->SurfaceType & EGL_WINDOW_BIT)
+         dri2_ctx->base.WindowRenderBuffer = EGL_BACK_BUFFER;
    }
    else
       dri_config = NULL;
 
    if (dri2_dpy->dri2) {
-      if (dri2_dpy->dri2->base.version >= 2) {
+      if (dri2_dpy->dri2->base.version >= 3) {
+         unsigned error;
+         unsigned num_attribs = 0;
+         uint32_t ctx_attribs[8];
+
+         ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
+         ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMajorVersion;
+         ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
+         ctx_attribs[num_attribs++] = dri2_ctx->base.ClientMinorVersion;
+
+         if (dri2_ctx->base.Flags != 0) {
+            /* If the implementation doesn't support the __DRI2_ROBUSTNESS
+             * extension, don't even try to send it the robust-access flag.
+             * It may explode.  Instead, generate the required EGL error here.
+             */
+            if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
+                && !dri2_dpy->robustness) {
+               _eglError(EGL_BAD_MATCH, "eglCreateContext");
+               goto cleanup;
+            }
+
+            ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
+            ctx_attribs[num_attribs++] = dri2_ctx->base.Flags;
+         }
+
+         if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
+            /* If the implementation doesn't support the __DRI2_ROBUSTNESS
+             * extension, don't even try to send it a reset strategy.  It may
+             * explode.  Instead, generate the required EGL error here.
+             */
+            if (!dri2_dpy->robustness) {
+               _eglError(EGL_BAD_CONFIG, "eglCreateContext");
+               goto cleanup;
+            }
+
+            ctx_attribs[num_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
+            ctx_attribs[num_attribs++] = __DRI_CTX_RESET_LOSE_CONTEXT;
+         }
+
+         assert(num_attribs <= ARRAY_SIZE(ctx_attribs));
+
+        dri2_ctx->dri_context =
+           dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
+                                                 api,
+                                                 dri_config,
+                                                 shared,
+                                                 num_attribs / 2,
+                                                 ctx_attribs,
+                                                 & error,
+                                                 dri2_ctx);
+        dri2_create_context_attribs_error(error);
+      } else {
         dri2_ctx->dri_context =
            dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
                                                   api,
                                                   dri_config,
-                                                  dri2_ctx_shared ? 
-                                                  dri2_ctx_shared->dri_context : NULL,
+                                                   shared,
                                                   dri2_ctx);
-      } else if (api == __DRI_API_OPENGL) {
-        dri2_ctx->dri_context =
-           dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
-                                            dri_config,
-                                            dri2_ctx_shared ? 
-                                            dri2_ctx_shared->dri_context : NULL,
-                                            dri2_ctx);
-      } else {
-        /* fail */
       }
    } else {
       assert(dri2_dpy->swrast);
-      if (dri2_dpy->swrast->base.version >= 2) {
-        dri2_ctx->dri_context =
-           dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
-                                                    api,
-                                                    dri_config,
-                                                    dri2_ctx_shared ? 
-                                                    dri2_ctx_shared->dri_context : NULL,
-                                                    dri2_ctx);
-      } else if (api == __DRI_API_OPENGL) {
-        dri2_ctx->dri_context =
-           dri2_dpy->core->createNewContext(dri2_dpy->dri_screen,
-                                            dri_config,
-                                            dri2_ctx_shared ?
-                                            dri2_ctx_shared->dri_context : NULL,
-                                            dri2_ctx);
-      } else {
-        /* fail */
-      }
+      dri2_ctx->dri_context =
+         dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
+                                                  api,
+                                                  dri_config,
+                                                  shared,
+                                                  dri2_ctx);
    }
 
    if (!dri2_ctx->dri_context)
@@ -921,37 +1070,51 @@ dri2_release_tex_image(_EGLDriver *drv,
 }
 
 static _EGLImage *
-dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
-                                  EGLClientBuffer buffer,
-                                  const EGLint *attr_list)
+dri2_create_image(_EGLDisplay *disp, __DRIimage *dri_image)
 {
-   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
    struct dri2_egl_image *dri2_img;
-   GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
 
-   if (renderbuffer == 0) {
-      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
-      return EGL_NO_IMAGE_KHR;
+   if (dri_image == NULL) {
+      _eglError(EGL_BAD_ALLOC, "dri2_create_image");
+      return NULL;
    }
 
    dri2_img = malloc(sizeof *dri2_img);
    if (!dri2_img) {
-      _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
-      return EGL_NO_IMAGE_KHR;
+      _eglError(EGL_BAD_ALLOC, "dri2_create_image");
+      return NULL;
    }
 
    if (!_eglInitImage(&dri2_img->base, disp)) {
       free(dri2_img);
+      return NULL;
+   }
+
+   dri2_img->dri_image = dri_image;
+
+   return &dri2_img->base;
+}
+
+static _EGLImage *
+dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
+                                  EGLClientBuffer buffer,
+                                  const EGLint *attr_list)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
+   GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
+   __DRIimage *dri_image;
+
+   if (renderbuffer == 0) {
+      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
       return EGL_NO_IMAGE_KHR;
    }
 
-   dri2_img->dri_image = 
+   dri_image =
       dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
-                                                  renderbuffer,
-                                                  dri2_img);
+                                                   renderbuffer, NULL);
 
-   return &dri2_img->base;
+   return dri2_create_image(disp, dri_image);
 }
 
 static _EGLImage *
@@ -959,11 +1122,9 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
                                  EGLClientBuffer buffer, const EGLint *attr_list)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   struct dri2_egl_image *dri2_img;
    EGLint format, name, pitch, err;
    _EGLImageAttribs attrs;
-
-   (void) ctx;
+   __DRIimage *dri_image;
 
    name = (EGLint) (uintptr_t) buffer;
 
@@ -989,79 +1150,188 @@ dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
       return NULL;
    }
 
-   dri2_img = malloc(sizeof *dri2_img);
-   if (!dri2_img) {
-      _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm");
-      return NULL;
-   }
-
-   if (!_eglInitImage(&dri2_img->base, disp)) {
-      free(dri2_img);
-      return NULL;
-   }
-
-   dri2_img->dri_image =
+   dri_image =
       dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
                                           attrs.Width,
                                           attrs.Height,
                                           format,
                                           name,
                                           pitch,
-                                          dri2_img);
-   if (dri2_img->dri_image == NULL) {
-      free(dri2_img);
-      _eglError(EGL_BAD_ALLOC, "dri2_create_image_mesa_drm");
-      return NULL;
-   }
+                                          NULL);
 
-   return &dri2_img->base;
+   return dri2_create_image(disp, dri_image);
 }
 
 #ifdef HAVE_WAYLAND_PLATFORM
-static _EGLImage *
-dri2_reference_drm_image(_EGLDisplay *disp, _EGLContext *ctx,
-                        __DRIimage *dri_image, EGLint width, EGLint height)
-{
-   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   EGLint attr_list[] = {
-               EGL_WIDTH,              0,
-               EGL_HEIGHT,             0,
-               EGL_DRM_BUFFER_STRIDE_MESA,     0,
-               EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
-               EGL_NONE
-   };
-   EGLint name, stride;
-   
-   dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_NAME, &name);
-   dri2_dpy->image->queryImage(dri_image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
-
-   attr_list[1] = width;
-   attr_list[3] = height;
-   attr_list[5] = stride / 4;
 
-   return dri2_create_image_mesa_drm_buffer(disp, ctx,
-                                           (EGLClientBuffer)(intptr_t) name,
-                                           attr_list);
-}
+/* This structure describes how a wl_buffer maps to one or more
+ * __DRIimages.  A wl_drm_buffer stores the wl_drm format code and the
+ * offsets and strides of the planes in the buffer.  This table maps a
+ * wl_drm format code to a description of the planes in the buffer
+ * that lets us create a __DRIimage for each of the planes. */
+
+static const struct wl_drm_components_descriptor {
+   uint32_t dri_components;
+   EGLint components;
+   int nplanes;
+} wl_drm_components[] = {
+   { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
+   { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
+   { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
+   { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
+   { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
+};
 
 static _EGLImage *
 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
                                    EGLClientBuffer _buffer,
                                    const EGLint *attr_list)
 {
-   struct wl_buffer *buffer = (struct wl_buffer *) _buffer;
-   (void) attr_list;
+   struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer;
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   const struct wl_drm_components_descriptor *f;
+   __DRIimage *dri_image;
+   _EGLImageAttribs attrs;
+   EGLint err;
+   int32_t plane;
 
-   if (!wayland_buffer_is_drm(buffer))
+   if (!wayland_buffer_is_drm(&buffer->buffer))
        return NULL;
 
-   return dri2_reference_drm_image(disp, ctx,
-                                   wayland_drm_buffer_get_buffer(buffer),
-                                   buffer->width,
-                                   buffer->height);
+   err = _eglParseImageAttribList(&attrs, disp, attr_list);
+   plane = attrs.PlaneWL;
+   if (err != EGL_SUCCESS) {
+      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
+      return NULL;
+   }
+
+   f = buffer->driver_format;
+   if (plane < 0 || plane >= f->nplanes) {
+      _eglError(EGL_BAD_PARAMETER,
+                "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
+      return NULL;
+   }
+
+   dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
+
+   if (dri_image == NULL) {
+      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
+      return NULL;
+   }
+
+   return dri2_create_image(disp, dri_image);
 }
 #endif
 
+/**
+ * Set the error code after a call to
+ * dri2_egl_image::dri_image::createImageFromTexture.
+ */
+static void
+dri2_create_image_khr_texture_error(int dri_error)
+{
+   EGLint egl_error;
+
+   switch (dri_error) {
+   case __DRI_IMAGE_ERROR_SUCCESS:
+      return;
+
+   case __DRI_IMAGE_ERROR_BAD_ALLOC:
+      egl_error = EGL_BAD_ALLOC;
+      break;
+
+   case __DRI_IMAGE_ERROR_BAD_MATCH:
+      egl_error = EGL_BAD_MATCH;
+      break;
+
+   case __DRI_IMAGE_ERROR_BAD_PARAMETER:
+      egl_error = EGL_BAD_PARAMETER;
+      break;
+
+   default:
+      assert(0);
+      egl_error = EGL_BAD_MATCH;
+      break;
+   }
+
+   _eglError(egl_error, "dri2_create_image_khr_texture");
+}
+
+static _EGLImage *
+dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
+                                  EGLenum target,
+                                  EGLClientBuffer buffer,
+                                  const EGLint *attr_list)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
+   struct dri2_egl_image *dri2_img;
+   GLuint texture = (GLuint) (uintptr_t) buffer;
+   _EGLImageAttribs attrs;
+   GLuint depth;
+   GLenum gl_target;
+   unsigned error;
+
+   if (texture == 0) {
+      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
+      return EGL_NO_IMAGE_KHR;
+   }
+
+   if (_eglParseImageAttribList(&attrs, disp, attr_list) != EGL_SUCCESS)
+      return EGL_NO_IMAGE_KHR;
+
+   switch (target) {
+   case EGL_GL_TEXTURE_2D_KHR:
+      depth = 0;
+      gl_target = GL_TEXTURE_2D;
+      break;
+   case EGL_GL_TEXTURE_3D_KHR:
+      depth = attrs.GLTextureZOffset;
+      gl_target = GL_TEXTURE_3D;
+      break;
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
+      depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
+      gl_target = GL_TEXTURE_CUBE_MAP;
+      break;
+   default:
+      _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
+      return EGL_NO_IMAGE_KHR;
+   }
+
+   dri2_img = malloc(sizeof *dri2_img);
+   if (!dri2_img) {
+      _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
+      return EGL_NO_IMAGE_KHR;
+   }
+
+   if (!_eglInitImage(&dri2_img->base, disp)) {
+      _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
+      free(dri2_img);
+      return EGL_NO_IMAGE_KHR;
+   }
+
+   dri2_img->dri_image =
+      dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
+                                              gl_target,
+                                              texture,
+                                              depth,
+                                              attrs.GLTextureLevel,
+                                              &error,
+                                              dri2_img);
+   dri2_create_image_khr_texture_error(error);
+
+   if (!dri2_img->dri_image) {
+      free(dri2_img);
+      return EGL_NO_IMAGE_KHR;
+   }
+   return &dri2_img->base;
+}
+
 _EGLImage *
 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
                      _EGLContext *ctx, EGLenum target,
@@ -1070,6 +1340,14 @@ dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
    (void) drv;
 
    switch (target) {
+   case EGL_GL_TEXTURE_2D_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
+   case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
+      return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
    case EGL_GL_RENDERBUFFER_KHR:
       return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
    case EGL_DRM_BUFFER_MESA:
@@ -1211,32 +1489,46 @@ dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
 
 #ifdef HAVE_WAYLAND_PLATFORM
 
-static void *
+static void
 dri2_wl_reference_buffer(void *user_data, uint32_t name,
-                        int32_t width, int32_t height,
-                        uint32_t stride, struct wl_visual *visual)
+                         struct wl_drm_buffer *buffer)
 {
    _EGLDisplay *disp = user_data;
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   __DRIimage *image;
+   __DRIimage *img;
+   int i, dri_components = 0;
+
+   img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
+                                               buffer->buffer.width,
+                                               buffer->buffer.height,
+                                               buffer->format, (int*)&name, 1,
+                                               buffer->stride,
+                                               buffer->offset,
+                                               NULL);
+
+   if (img == NULL)
+      return;
+
+   dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
 
-   image = dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
-                                               width, height, 
-                                               __DRI_IMAGE_FORMAT_ARGB8888,
-                                               name, stride / 4,
-                                               NULL);
+   buffer->driver_format = NULL;
+   for (i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
+      if (wl_drm_components[i].dri_components == dri_components)
+         buffer->driver_format = &wl_drm_components[i];
 
-   return image;
+   if (buffer->driver_format == NULL)
+      dri2_dpy->image->destroyImage(img);
+   else
+      buffer->driver_buffer = img;
 }
 
 static void
-dri2_wl_release_buffer(void *user_data, void *buffer)
+dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
 {
    _EGLDisplay *disp = user_data;
-   __DRIimage *image = buffer;
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
 
-   dri2_dpy->image->destroyImage(image);
+   dri2_dpy->image->destroyImage(buffer->driver_buffer);
 }
 
 static struct wayland_drm_callbacks wl_drm_callbacks = {
@@ -1285,6 +1577,33 @@ dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
 
    return EGL_TRUE;
 }
+
+static EGLBoolean
+dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
+                             struct wl_buffer *_buffer,
+                             EGLint attribute, EGLint *value)
+{
+   struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer;
+   const struct wl_drm_components_descriptor *format;
+
+   if (!wayland_buffer_is_drm(&buffer->buffer))
+      return EGL_FALSE;
+
+   format = buffer->driver_format;
+   switch (attribute) {
+   case EGL_TEXTURE_FORMAT:
+      *value = format->components;
+      return EGL_TRUE;
+   case EGL_WIDTH:
+      *value = buffer->buffer.width;
+      break;
+   case EGL_HEIGHT:
+      *value = buffer->buffer.height;
+      break;
+   }
+
+   return EGL_FALSE;
+}
 #endif
 
 static void
@@ -1302,7 +1621,11 @@ dri2_load(_EGLDriver *drv)
 {
    struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
 #ifdef HAVE_SHARED_GLAPI
+#ifdef HAVE_ANDROID_PLATFORM
+   const char *libname = "libglapi.so";
+#else
    const char *libname = "libglapi.so.0";
+#endif
 #else
    /*
     * Both libGL.so and libglapi.so are glapi providers.  There is no way to
@@ -1343,18 +1666,16 @@ dri2_load(_EGLDriver *drv)
  * Create a new _EGLDriver object and init its dispatch table.
  */
 _EGLDriver *
-_EGL_MAIN(const char *args)
+_eglBuiltInDriverDRI2(const char *args)
 {
    struct dri2_egl_driver *dri2_drv;
 
    (void) args;
 
-   dri2_drv = malloc(sizeof *dri2_drv);
+   dri2_drv = calloc(1, sizeof *dri2_drv);
    if (!dri2_drv)
       return NULL;
 
-   memset(dri2_drv, 0, sizeof *dri2_drv);
-
    if (!dri2_load(&dri2_drv->base)) {
       free(dri2_drv);
       return NULL;
@@ -1378,6 +1699,7 @@ _EGL_MAIN(const char *args)
 #ifdef HAVE_WAYLAND_PLATFORM
    dri2_drv->base.API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
    dri2_drv->base.API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
+   dri2_drv->base.API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
 #endif
 
    dri2_drv->base.Name = "DRI2";