+#ifdef EGL_MESA_drm_image
+
+static struct pipe_resource *
+egl_g3d_create_drm_buffer(_EGLDisplay *dpy, _EGLImage *img,
+ const EGLint *attribs)
+{
+ struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+ struct pipe_screen *screen = gdpy->native->screen;
+ struct pipe_resource templ;
+ _EGLImageAttribs attrs;
+ EGLint format, valid_use;
+
+ if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
+ return NULL;
+
+ if (attrs.Width <= 0 || attrs.Height <= 0) {
+ _eglLog(_EGL_DEBUG, "bad width or height (%dx%d)",
+ attrs.Width, attrs.Height);
+ return NULL;
+ }
+
+ switch (attrs.DRMBufferFormatMESA) {
+ case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
+ format = PIPE_FORMAT_B8G8R8A8_UNORM;
+ break;
+ default:
+ _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
+ attrs.DRMBufferFormatMESA);
+ return NULL;
+ break;
+ }
+
+ valid_use = EGL_DRM_BUFFER_USE_SCANOUT_MESA |
+ EGL_DRM_BUFFER_USE_SHARE_MESA |
+ EGL_DRM_BUFFER_USE_CURSOR_MESA;
+ if (attrs.DRMBufferUseMESA & ~valid_use) {
+ _eglLog(_EGL_DEBUG, "bad image use bit 0x%04x",
+ attrs.DRMBufferUseMESA);
+ return NULL;
+ }
+
+ memset(&templ, 0, sizeof(templ));
+ templ.target = PIPE_TEXTURE_2D;
+ templ.format = format;
+ templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
+ templ.width0 = attrs.Width;
+ templ.height0 = attrs.Height;
+ templ.depth0 = 1;
+ templ.array_size = 1;
+
+ /*
+ * XXX fix apps (e.g. wayland) and pipe drivers (e.g. i915) and remove the
+ * size check
+ */
+ if ((attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA) &&
+ attrs.Width >= 640 && attrs.Height >= 480)
+ templ.bind |= PIPE_BIND_SCANOUT;
+ if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
+ templ.bind |= PIPE_BIND_SHARED;
+ if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA) {
+ if (attrs.Width != 64 || attrs.Height != 64)
+ return NULL;
+ templ.bind |= PIPE_BIND_CURSOR;
+ }
+
+ return screen->resource_create(screen, &templ);
+}
+
+static struct pipe_resource *
+egl_g3d_reference_drm_buffer(_EGLDisplay *dpy, EGLint name,
+ _EGLImage *img, const EGLint *attribs)
+{
+ struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+ _EGLImageAttribs attrs;
+ EGLint format;
+ struct native_buffer nbuf;
+
+ if (!dpy->Extensions.MESA_drm_image)
+ return NULL;
+
+ if (_eglParseImageAttribList(&attrs, dpy, attribs) != EGL_SUCCESS)
+ return NULL;
+
+ if (attrs.Width <= 0 || attrs.Height <= 0 ||
+ attrs.DRMBufferStrideMESA <= 0) {
+ _eglLog(_EGL_DEBUG, "bad width, height, or stride (%dx%dx%d)",
+ attrs.Width, attrs.Height, attrs.DRMBufferStrideMESA);
+ return NULL;
+ }
+
+ switch (attrs.DRMBufferFormatMESA) {
+ case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
+ format = PIPE_FORMAT_B8G8R8A8_UNORM;
+ break;
+ default:
+ _eglLog(_EGL_DEBUG, "bad image format value 0x%04x",
+ attrs.DRMBufferFormatMESA);
+ return NULL;
+ break;
+ }
+
+ memset(&nbuf, 0, sizeof(nbuf));
+ nbuf.type = NATIVE_BUFFER_DRM;
+ nbuf.u.drm.templ.target = PIPE_TEXTURE_2D;
+ nbuf.u.drm.templ.format = format;
+ nbuf.u.drm.templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
+ nbuf.u.drm.templ.width0 = attrs.Width;
+ nbuf.u.drm.templ.height0 = attrs.Height;
+ nbuf.u.drm.templ.depth0 = 1;
+ nbuf.u.drm.templ.array_size = 1;
+
+ nbuf.u.drm.name = name;
+ nbuf.u.drm.stride =
+ attrs.DRMBufferStrideMESA * util_format_get_blocksize(format);
+
+ return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf);
+}
+
+#endif /* EGL_MESA_drm_image */
+
+#ifdef EGL_WL_bind_wayland_display
+
+static struct pipe_resource *
+egl_g3d_reference_wl_buffer(_EGLDisplay *dpy, struct wl_resource *buffer,
+ _EGLImage *img, const EGLint *attribs)
+{
+ struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+ struct pipe_resource *resource = NULL, *tmp = NULL;
+
+ if (!gdpy->native->wayland_bufmgr)
+ return NULL;
+
+ tmp = gdpy->native->wayland_bufmgr->buffer_get_resource(gdpy->native, buffer);
+
+ pipe_resource_reference(&resource, tmp);
+
+ return resource;
+}
+
+#endif /* EGL_WL_bind_wayland_display */
+
+#ifdef EGL_ANDROID_image_native_buffer
+
+static struct pipe_resource *
+egl_g3d_reference_android_native_buffer(_EGLDisplay *dpy,
+ struct ANativeWindowBuffer *buf)
+{
+ struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+ struct native_buffer nbuf;
+
+ memset(&nbuf, 0, sizeof(nbuf));
+ nbuf.type = NATIVE_BUFFER_ANDROID;
+ nbuf.u.android = buf;
+
+ return gdpy->native->buffer->import_buffer(gdpy->native, &nbuf);
+}
+
+#endif /* EGL_ANDROID_image_native_buffer */
+