2 * Copyright © 2010 Kristian Høgsberg
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "compositor.h"
27 struct wlsc_shm_buffer {
28 struct wl_buffer buffer;
29 struct wl_list surfaces_attached_to;
36 destroy_buffer(struct wl_resource *resource, struct wl_client *client)
38 struct wlsc_shm_buffer *buffer =
39 container_of(resource, struct wlsc_shm_buffer, buffer.resource);
40 struct wlsc_surface *es, *next;
43 munmap(buffer->data, buffer->stride * buffer->buffer.height);
47 wl_list_for_each_safe(es, next, &buffer->surfaces_attached_to,
50 wl_list_remove(&es->buffer_link);
57 buffer_damage(struct wl_client *client, struct wl_buffer *buffer_base,
58 int32_t x, int32_t y, int32_t width, int32_t height)
60 struct wlsc_shm_buffer *buffer =
61 (struct wlsc_shm_buffer *) buffer_base;
62 struct wlsc_surface *es;
64 wl_list_for_each(es, &buffer->surfaces_attached_to, buffer_link) {
65 glBindTexture(GL_TEXTURE_2D, es->texture);
66 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
67 buffer->buffer.width, buffer->buffer.height, 0,
68 GL_BGRA_EXT, GL_UNSIGNED_BYTE, buffer->data);
69 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
70 * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
75 buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
77 wl_resource_destroy(&buffer->resource, client);
80 const static struct wl_buffer_interface buffer_interface = {
86 wlsc_is_shm_buffer(struct wl_buffer *buffer)
88 return buffer->resource.object.implementation ==
89 (void (**)(void)) &buffer_interface;
93 wlsc_shm_buffer_attach(struct wl_buffer *buffer_base,
94 struct wl_surface *surface)
96 struct wlsc_surface *es = (struct wlsc_surface *) surface;
97 struct wlsc_shm_buffer *buffer =
98 (struct wlsc_shm_buffer *) buffer_base;
100 glBindTexture(GL_TEXTURE_2D, es->texture);
102 /* Unbind any EGLImage texture that may be bound, so we don't
104 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
105 0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
106 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
107 buffer->buffer.width, buffer->buffer.height, 0,
108 GL_BGRA_EXT, GL_UNSIGNED_BYTE, buffer->data);
109 es->visual = buffer->buffer.visual;
112 wl_list_remove(&es->buffer_link);
113 wl_list_insert(&buffer->surfaces_attached_to, &es->buffer_link);
116 static struct wlsc_shm_buffer *
117 wlsc_shm_buffer_init(struct wlsc_compositor *compositor,
118 int32_t width, int32_t height,
119 int32_t stride, struct wl_visual *visual,
122 struct wlsc_shm_buffer *buffer;
124 buffer = malloc(sizeof *buffer);
128 buffer->buffer.compositor = &compositor->compositor;
129 buffer->buffer.width = width;
130 buffer->buffer.height = height;
131 buffer->buffer.visual = visual;
132 buffer->stride = stride;
135 wl_list_init(&buffer->surfaces_attached_to);
137 buffer->buffer.resource.object.interface = &wl_buffer_interface;
138 buffer->buffer.resource.object.implementation = (void (**)(void))
141 buffer->buffer.resource.destroy = destroy_buffer;
147 shm_create_buffer(struct wl_client *client, struct wl_shm *shm,
148 uint32_t id, int fd, int32_t width, int32_t height,
149 uint32_t stride, struct wl_visual *visual)
151 struct wlsc_compositor *compositor =
152 container_of((struct wlsc_shm *) shm,
153 struct wlsc_compositor, shm);
154 struct wlsc_shm_buffer *buffer;
157 /* FIXME: Define a real exception event instead of abusing the
158 * display.invalid_object error */
159 if (visual->object.interface != &wl_visual_interface) {
160 wl_client_post_event(client,
161 (struct wl_object *) compositor->wl_display,
162 WL_DISPLAY_INVALID_OBJECT, 0);
163 fprintf(stderr, "invalid visual in create_buffer\n");
168 if (width < 0 || height < 0 || stride < width) {
169 wl_client_post_event(client,
170 (struct wl_object *) compositor->wl_display,
171 WL_DISPLAY_INVALID_OBJECT, 0);
173 "invalid width, height or stride in create_buffer\n");
178 data = mmap(NULL, stride * height, PROT_READ, MAP_SHARED, fd, 0);
181 if (data == MAP_FAILED) {
182 /* FIXME: Define a real exception event instead of
183 * abusing this one */
184 wl_client_post_event(client,
185 (struct wl_object *) compositor->wl_display,
186 WL_DISPLAY_INVALID_OBJECT, 0);
187 fprintf(stderr, "failed to create image for fd %d\n", fd);
191 buffer = wlsc_shm_buffer_init(compositor, width, height,
192 stride, visual, data);
193 if (buffer == NULL) {
194 munmap(data, stride * height);
195 wl_client_post_no_memory(client);
200 buffer->buffer.resource.object.id = id;
202 wl_client_add_resource(client, &buffer->buffer.resource);
205 const static struct wl_shm_interface shm_interface = {
210 wlsc_shm_init(struct wlsc_compositor *ec)
212 struct wlsc_shm *shm = &ec->shm;
214 shm->object.interface = &wl_shm_interface;
215 shm->object.implementation = (void (**)(void)) &shm_interface;
216 wl_display_add_object(ec->wl_display, &shm->object);
217 wl_display_add_global(ec->wl_display, &shm->object, NULL);