2010-08-18 22:51:26 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "compositor.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
drm_authenticate(struct wl_client *client,
|
|
|
|
struct wl_drm *drm_base, uint32_t id)
|
|
|
|
{
|
|
|
|
struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
|
|
|
|
struct wlsc_compositor *compositor =
|
|
|
|
container_of(drm, struct wlsc_compositor, drm);
|
|
|
|
|
|
|
|
if (compositor->authenticate(compositor, id) < 0)
|
|
|
|
wl_client_post_event(client,
|
|
|
|
(struct wl_object *) compositor->wl_display,
|
|
|
|
WL_DISPLAY_INVALID_OBJECT, 0);
|
|
|
|
else
|
2010-12-02 01:07:41 +03:00
|
|
|
wl_client_post_event(client, &drm->object,
|
|
|
|
WL_DRM_AUTHENTICATED);
|
2010-08-18 22:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_buffer(struct wl_resource *resource, struct wl_client *client)
|
|
|
|
{
|
2010-10-07 05:17:40 +04:00
|
|
|
struct wlsc_drm_buffer *buffer =
|
2010-12-02 01:07:41 +03:00
|
|
|
container_of(resource, struct wlsc_drm_buffer, buffer.resource);
|
2010-10-07 05:17:40 +04:00
|
|
|
struct wlsc_compositor *compositor =
|
2010-12-02 01:07:41 +03:00
|
|
|
(struct wlsc_compositor *) buffer->buffer.compositor;
|
2010-08-18 22:51:26 +04:00
|
|
|
|
2010-09-24 18:01:18 +04:00
|
|
|
eglDestroyImageKHR(compositor->display, buffer->image);
|
2010-08-18 22:51:26 +04:00
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
|
|
|
|
{
|
2010-12-02 01:07:41 +03:00
|
|
|
wl_resource_destroy(&buffer->resource, client);
|
2010-08-18 22:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const static struct wl_buffer_interface buffer_interface = {
|
|
|
|
buffer_destroy
|
|
|
|
};
|
|
|
|
|
2010-10-07 05:17:40 +04:00
|
|
|
static void
|
|
|
|
drm_buffer_attach(struct wl_buffer *buffer_base, struct wl_surface *surface)
|
|
|
|
{
|
|
|
|
struct wlsc_surface *es = (struct wlsc_surface *) surface;
|
|
|
|
struct wlsc_drm_buffer *buffer =
|
|
|
|
(struct wlsc_drm_buffer *) buffer_base;
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, es->texture);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
|
2010-12-02 01:07:41 +03:00
|
|
|
es->visual = buffer->buffer.visual;
|
2010-10-07 05:17:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drm_buffer_damage(struct wl_buffer *buffer,
|
|
|
|
struct wl_surface *surface,
|
|
|
|
int32_t x, int32_t y, int32_t width, int32_t height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-06 18:46:37 +03:00
|
|
|
static struct wlsc_drm_buffer *
|
|
|
|
wlsc_drm_buffer_create_for_image(struct wlsc_compositor *compositor,
|
|
|
|
EGLImageKHR *image,
|
|
|
|
int32_t width, int32_t height,
|
|
|
|
struct wl_visual *visual)
|
|
|
|
{
|
|
|
|
struct wlsc_drm_buffer *buffer;
|
|
|
|
|
|
|
|
buffer = malloc(sizeof *buffer);
|
|
|
|
if (buffer == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buffer->buffer.compositor = &compositor->compositor;
|
|
|
|
buffer->buffer.width = width;
|
|
|
|
buffer->buffer.height = height;
|
|
|
|
buffer->buffer.visual = visual;
|
|
|
|
buffer->buffer.attach = drm_buffer_attach;
|
|
|
|
buffer->buffer.damage = drm_buffer_damage;
|
|
|
|
buffer->image = image;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2010-08-18 22:51:26 +04:00
|
|
|
static void
|
|
|
|
drm_create_buffer(struct wl_client *client, struct wl_drm *drm_base,
|
|
|
|
uint32_t id, uint32_t name, int32_t width, int32_t height,
|
|
|
|
uint32_t stride, struct wl_visual *visual)
|
|
|
|
{
|
|
|
|
struct wlsc_drm *drm = (struct wlsc_drm *) drm_base;
|
|
|
|
struct wlsc_compositor *compositor =
|
|
|
|
container_of(drm, struct wlsc_compositor, drm);
|
2010-10-07 05:17:40 +04:00
|
|
|
struct wlsc_drm_buffer *buffer;
|
2010-12-06 18:46:37 +03:00
|
|
|
EGLImageKHR image;
|
2010-08-18 22:51:26 +04:00
|
|
|
EGLint attribs[] = {
|
|
|
|
EGL_WIDTH, 0,
|
|
|
|
EGL_HEIGHT, 0,
|
2010-08-25 01:34:23 +04:00
|
|
|
EGL_DRM_BUFFER_STRIDE_MESA, 0,
|
|
|
|
EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
|
2010-08-18 22:51:26 +04:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
if (visual != &compositor->argb_visual &&
|
|
|
|
visual != &compositor->premultiplied_argb_visual &&
|
|
|
|
visual != &compositor->rgb_visual) {
|
|
|
|
/* FIXME: Define a real exception event instead of
|
|
|
|
* abusing this one */
|
|
|
|
wl_client_post_event(client,
|
|
|
|
(struct wl_object *) compositor->wl_display,
|
|
|
|
WL_DISPLAY_INVALID_OBJECT, 0);
|
|
|
|
fprintf(stderr, "invalid visual in create_buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
attribs[1] = width;
|
|
|
|
attribs[3] = height;
|
|
|
|
attribs[5] = stride / 4;
|
2010-12-06 18:46:37 +03:00
|
|
|
image = eglCreateImageKHR(compositor->display,
|
|
|
|
compositor->context,
|
|
|
|
EGL_DRM_BUFFER_MESA,
|
|
|
|
(EGLClientBuffer) name, attribs);
|
|
|
|
if (image == NULL) {
|
2010-08-18 22:51:26 +04:00
|
|
|
/* FIXME: Define a real exception event instead of
|
|
|
|
* abusing this one */
|
|
|
|
wl_client_post_event(client,
|
|
|
|
(struct wl_object *) compositor->wl_display,
|
|
|
|
WL_DISPLAY_INVALID_OBJECT, 0);
|
|
|
|
fprintf(stderr, "failed to create image for name %d\n", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-06 18:46:37 +03:00
|
|
|
buffer = wlsc_drm_buffer_create_for_image(compositor, image,
|
|
|
|
width, height, visual);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
eglDestroyImageKHR(compositor->display, image);
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-02 01:07:41 +03:00
|
|
|
buffer->buffer.resource.object.id = id;
|
|
|
|
buffer->buffer.resource.object.interface = &wl_buffer_interface;
|
|
|
|
buffer->buffer.resource.object.implementation = (void (**)(void))
|
2010-08-18 22:51:26 +04:00
|
|
|
&buffer_interface;
|
|
|
|
|
2010-12-02 01:07:41 +03:00
|
|
|
buffer->buffer.resource.destroy = destroy_buffer;
|
2010-08-18 22:51:26 +04:00
|
|
|
|
2010-12-02 01:07:41 +03:00
|
|
|
wl_client_add_resource(client, &buffer->buffer.resource);
|
2010-08-18 22:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const static struct wl_drm_interface drm_interface = {
|
|
|
|
drm_authenticate,
|
|
|
|
drm_create_buffer
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
post_drm_device(struct wl_client *client, struct wl_object *global)
|
|
|
|
{
|
2010-12-02 01:07:41 +03:00
|
|
|
struct wlsc_drm *drm = container_of(global, struct wlsc_drm, object);
|
2010-08-18 22:51:26 +04:00
|
|
|
|
|
|
|
wl_client_post_event(client, global, WL_DRM_DEVICE, drm->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wlsc_drm_init(struct wlsc_compositor *ec, int fd, const char *filename)
|
|
|
|
{
|
|
|
|
struct wlsc_drm *drm = &ec->drm;
|
|
|
|
|
|
|
|
drm->fd = fd;
|
|
|
|
drm->filename = strdup(filename);
|
|
|
|
if (drm->filename == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2010-12-02 01:07:41 +03:00
|
|
|
drm->object.interface = &wl_drm_interface;
|
|
|
|
drm->object.implementation = (void (**)(void)) &drm_interface;
|
|
|
|
wl_display_add_object(ec->wl_display, &drm->object);
|
|
|
|
wl_display_add_global(ec->wl_display, &drm->object, post_drm_device);
|
2010-08-18 22:51:26 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-07 05:17:40 +04:00
|
|
|
|
|
|
|
struct wlsc_drm_buffer *
|
|
|
|
wlsc_drm_buffer_create(struct wlsc_compositor *ec,
|
|
|
|
int width, int height, struct wl_visual *visual)
|
|
|
|
{
|
|
|
|
struct wlsc_drm_buffer *buffer;
|
2010-12-06 18:46:37 +03:00
|
|
|
EGLImageKHR image;
|
2010-10-07 05:17:40 +04:00
|
|
|
|
|
|
|
EGLint image_attribs[] = {
|
|
|
|
EGL_WIDTH, 0,
|
|
|
|
EGL_HEIGHT, 0,
|
|
|
|
EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
|
|
|
|
EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
image_attribs[1] = width;
|
|
|
|
image_attribs[3] = height;
|
|
|
|
|
2010-12-06 18:46:37 +03:00
|
|
|
image = eglCreateDRMImageMESA(ec->display, image_attribs);
|
|
|
|
if (image == NULL)
|
2010-10-07 05:17:40 +04:00
|
|
|
return NULL;
|
|
|
|
|
2010-12-06 18:46:37 +03:00
|
|
|
buffer = wlsc_drm_buffer_create_for_image(ec, image,
|
|
|
|
width, height, visual);
|
2010-10-07 05:17:40 +04:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|