diff --git a/compositor/Makefile.am b/compositor/Makefile.am index d2a30796..e90830ad 100644 --- a/compositor/Makefile.am +++ b/compositor/Makefile.am @@ -13,17 +13,14 @@ compositor_LDADD = \ if ENABLE_DRM_COMPOSITOR drm_compositor_sources = compositor-drm.c tty.c evdev.c -drm_sources = drm.c endif if ENABLE_X11_COMPOSITOR x11_compositor_sources = compositor-x11.c -drm_sources = drm.c endif if ENABLE_WAYLAND_COMPOSITOR wayland_compositor_sources = compositor-wayland.c -drm_sources = drm.c endif compositor_SOURCES = \ @@ -36,8 +33,7 @@ compositor_SOURCES = \ shm.c \ $(drm_compositor_sources) \ $(x11_compositor_sources) \ - $(wayland_compositor_sources) \ - $(drm_sources) + $(wayland_compositor_sources) udevrulesddir = $(sysconfdir)/udev/rules.d diff --git a/compositor/compositor-drm.c b/compositor/compositor-drm.c index c484404c..e55c5f20 100644 --- a/compositor/compositor-drm.c +++ b/compositor/compositor-drm.c @@ -22,6 +22,9 @@ #include #include +#include +#include + #define GL_GLEXT_PROTOTYPES #define EGL_EGLEXT_PROTOTYPES #include @@ -352,7 +355,7 @@ drm_compositor_create(struct wl_display *display, int connector) ec->base.destroy = drm_destroy; ec->base.present = drm_compositor_present; - ec->base.create_buffer = wlsc_drm_buffer_create; + ec->base.create_buffer = wlsc_shm_buffer_create; ec->base.focus = 1; glGenFramebuffers(1, &ec->base.fbo); diff --git a/compositor/compositor-wayland.c b/compositor/compositor-wayland.c index 9fdecfd0..07822286 100644 --- a/compositor/compositor-wayland.c +++ b/compositor/compositor-wayland.c @@ -464,7 +464,7 @@ wayland_compositor_create(struct wl_display *display, int width, int height) c->base.destroy = wayland_destroy; c->base.present = wayland_compositor_present; - c->base.create_buffer = wlsc_drm_buffer_create; + c->base.create_buffer = wlsc_shm_buffer_create; /* Can't init base class until we have a current egl context */ if (wlsc_compositor_init(&c->base, display) < 0) diff --git a/compositor/compositor-x11.c b/compositor/compositor-x11.c index 5631f696..123933d4 100644 --- a/compositor/compositor-x11.c +++ b/compositor/compositor-x11.c @@ -566,7 +566,7 @@ x11_compositor_create(struct wl_display *display, int width, int height) c->base.destroy = x11_destroy; c->base.present = x11_compositor_present; - c->base.create_buffer = wlsc_drm_buffer_create; + c->base.create_buffer = wlsc_shm_buffer_create; /* Can't init base class until we have a current egl context */ if (wlsc_compositor_init(&c->base, display) < 0) diff --git a/compositor/compositor.c b/compositor/compositor.c index 6b754477..c1db8f29 100644 --- a/compositor/compositor.c +++ b/compositor/compositor.c @@ -314,7 +314,7 @@ create_buffer_from_png(struct wlsc_compositor *ec, if(pixels == NULL) return NULL; - buffer = ec->create_buffer(ec, width, height, + buffer = ec->create_buffer(ec, width, height, width * 4, &ec->compositor.premultiplied_argb_visual, pixels); diff --git a/compositor/compositor.h b/compositor/compositor.h index c345585c..7f79da70 100644 --- a/compositor/compositor.h +++ b/compositor/compositor.h @@ -19,8 +19,6 @@ #ifndef _WAYLAND_SYSTEM_COMPOSITOR_H_ #define _WAYLAND_SYSTEM_COMPOSITOR_H_ -#include -#include #include #include #include "wayland-server.h" @@ -116,8 +114,8 @@ struct wlsc_compositor { void (*present)(struct wlsc_compositor *c); struct wl_buffer *(*create_buffer)(struct wlsc_compositor *c, int32_t width, int32_t height, - struct wl_visual *visual, - const void *data); + int32_t stride, struct wl_visual *visual, + void *data); }; #define MODIFIER_CTRL (1 << 8) @@ -205,9 +203,10 @@ uint32_t get_time(void); struct wl_buffer * -wlsc_drm_buffer_create(struct wlsc_compositor *ec, - int width, int height, - struct wl_visual *visual, const void *data); +wlsc_shm_buffer_create(struct wlsc_compositor *ec, + int32_t width, int32_t height, + int32_t stride, struct wl_visual *visual, + void *data); int wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display); @@ -234,10 +233,6 @@ shell_resize(struct wl_client *client, struct wl_shell *shell, struct wl_surface *surface, struct wl_input_device *device, uint32_t time, uint32_t edges); -struct wl_buffer * -wl_buffer_create_drm(struct wlsc_compositor *compositor, - struct wl_visual *visual); - struct wlsc_compositor * x11_compositor_create(struct wl_display *display, int width, int height); diff --git a/compositor/drm.c b/compositor/drm.c deleted file mode 100644 index 05bbc4a2..00000000 --- a/compositor/drm.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 -#include -#include - -#include "compositor.h" - -struct wlsc_drm_buffer { - struct wl_buffer buffer; - EGLImageKHR image; -}; - -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); - es->visual = buffer->buffer.visual; -} - -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) -{ -} - -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; -} - -struct wl_buffer * -wlsc_drm_buffer_create(struct wlsc_compositor *ec, int width, int height, - struct wl_visual *visual, const void *data) -{ - struct wlsc_drm_buffer *buffer; - EGLImageKHR image; - GLuint texture; - - 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; - - image = eglCreateDRMImageMESA(ec->display, image_attribs); - if (image == NULL) - return NULL; - - buffer = wlsc_drm_buffer_create_for_image(ec, image, - width, height, visual); - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image); - - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, - GL_BGRA_EXT, GL_UNSIGNED_BYTE, data); - - glDeleteTextures(1, &texture); - - return &buffer->buffer; -} diff --git a/compositor/shm.c b/compositor/shm.c index 389eeaa4..967ff727 100644 --- a/compositor/shm.c +++ b/compositor/shm.c @@ -28,6 +28,7 @@ struct wlsc_shm_buffer { struct wl_buffer buffer; int32_t stride; void *data; + int mapped; }; static void @@ -36,7 +37,10 @@ destroy_buffer(struct wl_resource *resource, struct wl_client *client) struct wlsc_shm_buffer *buffer = container_of(resource, struct wlsc_shm_buffer, buffer.resource); - munmap(buffer->data, buffer->stride * buffer->buffer.height); + if (buffer->mapped) + munmap(buffer->data, buffer->stride * buffer->buffer.height); + else + free(buffer->data); free(buffer); } @@ -87,6 +91,30 @@ shm_buffer_damage(struct wl_buffer *buffer_base, * support any unpack attributes except GL_UNPACK_ALIGNMENT. */ } +static struct wlsc_shm_buffer * +wlsc_shm_buffer_init(struct wlsc_compositor *compositor, + int32_t width, int32_t height, + int32_t stride, struct wl_visual *visual, + void *data) +{ + struct wlsc_shm_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 = shm_buffer_attach; + buffer->buffer.damage = shm_buffer_damage; + buffer->stride = stride; + buffer->data = data; + + return buffer; +} + static void shm_create_buffer(struct wl_client *client, struct wl_shm *shm, uint32_t id, int fd, int32_t width, int32_t height, @@ -96,6 +124,7 @@ shm_create_buffer(struct wl_client *client, struct wl_shm *shm, container_of((struct wlsc_shm *) shm, struct wlsc_compositor, shm); struct wlsc_shm_buffer *buffer; + void *data; /* FIXME: Define a real exception event instead of abusing the * display.invalid_object error */ @@ -118,27 +147,12 @@ shm_create_buffer(struct wl_client *client, struct wl_shm *shm, return; } - buffer = malloc(sizeof *buffer); - if (buffer == NULL) { - close(fd); - wl_client_post_no_memory(client); - return; - } + data = mmap(NULL, stride * height, PROT_READ, MAP_SHARED, fd, 0); - buffer->buffer.compositor = (struct wl_compositor *) compositor; - buffer->buffer.width = width; - buffer->buffer.height = height; - buffer->buffer.visual = visual; - buffer->buffer.attach = shm_buffer_attach; - buffer->buffer.damage = shm_buffer_damage; - buffer->stride = stride; - buffer->data = - mmap(NULL, stride * height, PROT_READ, MAP_SHARED, fd, 0); close(fd); - if (buffer->data == MAP_FAILED) { + if (data == MAP_FAILED) { /* FIXME: Define a real exception event instead of * abusing this one */ - free(buffer); wl_client_post_event(client, (struct wl_object *) compositor->wl_display, WL_DISPLAY_INVALID_OBJECT, 0); @@ -146,6 +160,15 @@ shm_create_buffer(struct wl_client *client, struct wl_shm *shm, return; } + buffer = wlsc_shm_buffer_init(compositor, width, height, + stride, visual, data); + if (buffer == NULL) { + munmap(data, stride * height); + wl_client_post_no_memory(client); + return; + } + buffer->mapped = 1; + buffer->buffer.resource.object.id = id; buffer->buffer.resource.object.interface = &wl_buffer_interface; buffer->buffer.resource.object.implementation = (void (**)(void)) @@ -172,3 +195,24 @@ wlsc_shm_init(struct wlsc_compositor *ec) return 0; } + +struct wl_buffer * +wlsc_shm_buffer_create(struct wlsc_compositor *ec, int width, int height, + int stride, struct wl_visual *visual, + void *data) +{ + struct wlsc_shm_buffer *buffer; + void *pixels; + + pixels = malloc(stride * height); + if (!pixels) + return NULL; + + memcpy(pixels, data, stride * height); + + buffer = wlsc_shm_buffer_init(ec, width, height, + stride, visual, pixels); + buffer->mapped = 0; + + return &buffer->buffer; +}