egl: drop an indentation level in _eglFindDisplay() by replacing break/if with a...
[mesa.git] / src / egl / wayland / wayland-drm / wayland-drm.c
index 6624fbe9d4eeb8ab5ecad980d3aad7843aa52266..51cdd2cb845bcebc490e27297ab961c68c750a1b 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2011 Kristian Høgsberg
+ * Copyright © 2011 Benjamin Franzke
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 #include <stdlib.h>
 #include <string.h>
 #include <stddef.h>
+#include <unistd.h>
 
 #include <wayland-server.h>
 #include "wayland-drm.h"
 #include "wayland-drm-server-protocol.h"
 
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglimage.h"
-#include "egltypedefs.h"
-
-struct wl_drm {
-       struct wl_object object;
-       struct wl_display *display;
-
-       _EGLDisplay *edisp;
-
-       char *device_name;
-       authenticate_t authenticate;
-};
-
-static void
-drm_buffer_damage(struct wl_buffer *buffer_base,
-                 struct wl_surface *surface,
-                 int32_t x, int32_t y, int32_t width, int32_t height)
-{
-}
+#define MIN(x,y) (((x)<(y))?(x):(y))
 
 static void
-destroy_buffer(struct wl_resource *resource, struct wl_client *client)
+destroy_buffer(struct wl_resource *resource)
 {
-       struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) resource;
-       _EGLDriver *drv = buffer->drm->edisp->Driver;
+       struct wl_drm_buffer *buffer = wl_resource_get_user_data(resource);
+       struct wl_drm *drm = buffer->drm;
 
-       drv->API.DestroyImageKHR(drv, buffer->drm->edisp, buffer->image);
+       drm->callbacks.release_buffer(drm->user_data, buffer);
        free(buffer);
 }
 
 static void
-buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
+buffer_destroy(struct wl_client *client, struct wl_resource *resource)
 {
-       wl_resource_destroy(&buffer->resource, client);
+       wl_resource_destroy(resource);
 }
 
-const static struct wl_buffer_interface buffer_interface = {
-       buffer_destroy
-};
-
 static void
-drm_create_buffer(struct wl_client *client, struct wl_drm *drm,
-                 uint32_t id, uint32_t name, int32_t width, int32_t height,
-                 uint32_t stride, struct wl_visual *visual)
+create_buffer(struct wl_client *client, struct wl_resource *resource,
+              uint32_t id, uint32_t name, int fd,
+              int32_t width, int32_t height,
+              uint32_t format,
+              int32_t offset0, int32_t stride0,
+              int32_t offset1, int32_t stride1,
+              int32_t offset2, int32_t stride2)
 {
+       struct wl_drm *drm = wl_resource_get_user_data(resource);
        struct wl_drm_buffer *buffer;
-       EGLint attribs[] = {
-               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
-       };
-       _EGLDriver *drv = drm->edisp->Driver;
-
-       buffer = malloc(sizeof *buffer);
+
+       buffer = calloc(1, sizeof *buffer);
        if (buffer == NULL) {
-               wl_client_post_no_memory(client);
+               wl_resource_post_no_memory(resource);
                return;
        }
 
        buffer->drm = drm;
-       buffer->buffer.compositor = NULL;
-       buffer->buffer.width = width;
-       buffer->buffer.height = height;
-       buffer->buffer.visual = visual;
-       buffer->buffer.attach = NULL;
-       buffer->buffer.damage = drm_buffer_damage;
-
-       if (visual->object.interface != &wl_visual_interface) {
-               /* FIXME: Define a real exception event instead of
-                * abusing this one */
-               wl_client_post_event(client,
-                                    (struct wl_object *) drm->display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
-               fprintf(stderr, "invalid visual in create_buffer\n");
+       buffer->width = width;
+       buffer->height = height;
+       buffer->format = format;
+       buffer->offset[0] = offset0;
+       buffer->stride[0] = stride0;
+       buffer->offset[1] = offset1;
+       buffer->stride[1] = stride1;
+       buffer->offset[2] = offset2;
+       buffer->stride[2] = stride2;
+
+        drm->callbacks.reference_buffer(drm->user_data, name, fd, buffer);
+       if (buffer->driver_buffer == NULL) {
+               wl_resource_post_error(resource,
+                                      WL_DRM_ERROR_INVALID_NAME,
+                                      "invalid name");
                return;
        }
 
-       attribs[1] = width;
-       attribs[3] = height;
-       attribs[5] = stride / 4;
-       buffer->image = drv->API.CreateImageKHR(drv, drm->edisp,
-                                               EGL_NO_CONTEXT,
-                                               EGL_DRM_BUFFER_MESA,
-                                               (EGLClientBuffer) (intptr_t) name,
-                                               attribs);
-
-       if (buffer->image == NULL) {
-               /* FIXME: Define a real exception event instead of
-                * abusing this one */
-               wl_client_post_event(client,
-                                    (struct wl_object *) drm->display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
-               fprintf(stderr, "failed to create image for name %d\n", name);
+       buffer->resource =
+               wl_resource_create(client, &wl_buffer_interface, 1, id);
+       if (!buffer->resource) {
+               wl_resource_post_no_memory(resource);
+               free(buffer);
                return;
        }
 
-       buffer->buffer.resource.object.id = id;
-       buffer->buffer.resource.object.interface = &wl_buffer_interface;
-       buffer->buffer.resource.object.implementation = (void (**)(void))
-               &buffer_interface;
+       wl_resource_set_implementation(buffer->resource,
+                                      (void (**)(void)) &drm->buffer_interface,
+                                      buffer, destroy_buffer);
+}
+
+static void
+drm_create_buffer(struct wl_client *client, struct wl_resource *resource,
+                 uint32_t id, uint32_t name, int32_t width, int32_t height,
+                 uint32_t stride, uint32_t format)
+{
+        switch (format) {
+        case WL_DRM_FORMAT_ABGR2101010:
+        case WL_DRM_FORMAT_XBGR2101010:
+        case WL_DRM_FORMAT_ARGB2101010:
+        case WL_DRM_FORMAT_XRGB2101010:
+        case WL_DRM_FORMAT_ARGB8888:
+        case WL_DRM_FORMAT_XRGB8888:
+        case WL_DRM_FORMAT_YUYV:
+        case WL_DRM_FORMAT_RGB565:
+                break;
+        default:
+                wl_resource_post_error(resource,
+                                       WL_DRM_ERROR_INVALID_FORMAT,
+                                       "invalid format");
+           return;
+        }
+
+        create_buffer(client, resource, id,
+                      name, -1, width, height, format, 0, stride, 0, 0, 0, 0);
+}
 
-       buffer->buffer.resource.destroy = destroy_buffer;
+static void
+drm_create_planar_buffer(struct wl_client *client,
+                         struct wl_resource *resource,
+                         uint32_t id, uint32_t name,
+                         int32_t width, int32_t height, uint32_t format,
+                         int32_t offset0, int32_t stride0,
+                         int32_t offset1, int32_t stride1,
+                         int32_t offset2, int32_t stride2)
+{
+        switch (format) {
+       case WL_DRM_FORMAT_YUV410:
+       case WL_DRM_FORMAT_YUV411:
+       case WL_DRM_FORMAT_YUV420:
+       case WL_DRM_FORMAT_YUV422:
+       case WL_DRM_FORMAT_YUV444:
+       case WL_DRM_FORMAT_NV12:
+        case WL_DRM_FORMAT_NV16:
+                break;
+        default:
+                wl_resource_post_error(resource,
+                                       WL_DRM_ERROR_INVALID_FORMAT,
+                                       "invalid format");
+           return;
+        }
+
+        create_buffer(client, resource, id, name, -1, width, height, format,
+                      offset0, stride0, offset1, stride1, offset2, stride2);
+}
 
-       wl_client_add_resource(client, &buffer->buffer.resource);
+static void
+drm_create_prime_buffer(struct wl_client *client,
+                        struct wl_resource *resource,
+                        uint32_t id, int fd,
+                        int32_t width, int32_t height, uint32_t format,
+                        int32_t offset0, int32_t stride0,
+                        int32_t offset1, int32_t stride1,
+                        int32_t offset2, int32_t stride2)
+{
+        create_buffer(client, resource, id, 0, fd, width, height, format,
+                      offset0, stride0, offset1, stride1, offset2, stride2);
+        close(fd);
 }
 
 static void
 drm_authenticate(struct wl_client *client,
-                struct wl_drm *drm, uint32_t id)
+                struct wl_resource *resource, uint32_t id)
 {
-       if (drm->authenticate(drm->edisp, id) < 0)
-               wl_client_post_event(client,
-                                    (struct wl_object *) drm->display,
-                                    WL_DISPLAY_INVALID_OBJECT, 0);
+       struct wl_drm *drm = wl_resource_get_user_data(resource);
+
+       if (drm->callbacks.authenticate(drm->user_data, id) < 0)
+               wl_resource_post_error(resource,
+                                      WL_DRM_ERROR_AUTHENTICATE_FAIL,
+                                      "authenicate failed");
        else
-               wl_client_post_event(client, &drm->object,
-                                    WL_DRM_AUTHENTICATED);
+               wl_resource_post_event(resource, WL_DRM_AUTHENTICATED);
 }
 
-const static struct wl_drm_interface drm_interface = {
+static const struct wl_drm_interface drm_interface = {
        drm_authenticate,
-       drm_create_buffer
+       drm_create_buffer,
+        drm_create_planar_buffer,
+        drm_create_prime_buffer
 };
 
 static void
-post_drm_device(struct wl_client *client, struct wl_object *global)
+bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
 {
-       struct wl_drm *drm = (struct wl_drm *) global;
+       struct wl_drm *drm = data;
+       struct wl_resource *resource;
+        uint32_t capabilities;
+
+       resource = wl_resource_create(client, &wl_drm_interface,
+                                     MIN(version, 2), id);
+       if (!resource) {
+               wl_client_post_no_memory(client);
+               return;
+       }
+
+       wl_resource_set_implementation(resource, &drm_interface, data, NULL);
 
-       wl_client_post_event(client, global, WL_DRM_DEVICE, drm->device_name);
+       wl_resource_post_event(resource, WL_DRM_DEVICE, drm->device_name);
+
+       if (drm->callbacks.is_format_supported(drm->user_data,
+                                              WL_DRM_FORMAT_ARGB2101010)) {
+               wl_resource_post_event(resource, WL_DRM_FORMAT,
+                                      WL_DRM_FORMAT_ARGB2101010);
+       }
+
+       if (drm->callbacks.is_format_supported(drm->user_data,
+                                              WL_DRM_FORMAT_XRGB2101010)) {
+               wl_resource_post_event(resource, WL_DRM_FORMAT,
+                                      WL_DRM_FORMAT_XRGB2101010);
+       }
+
+       if (drm->callbacks.is_format_supported(drm->user_data,
+                                              WL_DRM_FORMAT_ABGR2101010)) {
+               wl_resource_post_event(resource, WL_DRM_FORMAT,
+                                      WL_DRM_FORMAT_ABGR2101010);
+       }
+
+       if (drm->callbacks.is_format_supported(drm->user_data,
+                                              WL_DRM_FORMAT_XBGR2101010)) {
+               wl_resource_post_event(resource, WL_DRM_FORMAT,
+                                      WL_DRM_FORMAT_XBGR2101010);
+       }
+
+       wl_resource_post_event(resource, WL_DRM_FORMAT,
+                              WL_DRM_FORMAT_ARGB8888);
+       wl_resource_post_event(resource, WL_DRM_FORMAT,
+                              WL_DRM_FORMAT_XRGB8888);
+        wl_resource_post_event(resource, WL_DRM_FORMAT,
+                               WL_DRM_FORMAT_RGB565);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV410);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV411);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV420);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV422);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUV444);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV12);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_NV16);
+        wl_resource_post_event(resource, WL_DRM_FORMAT, WL_DRM_FORMAT_YUYV);
+
+        capabilities = 0;
+        if (drm->flags & WAYLAND_DRM_PRIME)
+           capabilities |= WL_DRM_CAPABILITY_PRIME;
+
+        if (version >= 2)
+           wl_resource_post_event(resource, WL_DRM_CAPABILITIES, capabilities);
 }
 
 struct wl_drm *
-wayland_drm_init(struct wl_display *display, _EGLDisplay *disp,
-                authenticate_t authenticate, char *device_name)
+wayland_drm_init(struct wl_display *display, char *device_name,
+                 const struct wayland_drm_callbacks *callbacks, void *user_data,
+                 uint32_t flags)
 {
        struct wl_drm *drm;
 
        drm = malloc(sizeof *drm);
+       if (!drm)
+               return NULL;
 
        drm->display = display;
-       drm->edisp = disp;
-       drm->authenticate = authenticate;
        drm->device_name = strdup(device_name);
+       drm->callbacks = *callbacks;
+       drm->user_data = user_data;
+        drm->flags = flags;
+
+        drm->buffer_interface.destroy = buffer_destroy;
 
-       drm->object.interface = &wl_drm_interface;
-       drm->object.implementation = (void (**)(void)) &drm_interface;
-       wl_display_add_object(display, &drm->object);
-       wl_display_add_global(display, &drm->object, post_drm_device);
+       drm->wl_drm_global =
+               wl_global_create(display, &wl_drm_interface, 2,
+                                drm, bind_drm);
 
        return drm;
 }
 
 void
-wayland_drm_destroy(struct wl_drm *drm)
+wayland_drm_uninit(struct wl_drm *drm)
 {
        free(drm->device_name);
 
-       /* FIXME: need wl_display_del_{object,global} */
+       wl_global_destroy(drm->wl_drm_global);
 
        free(drm);
 }