compositor: Use surface_attached_to list for shm-buffers
[profile/ivi/weston.git] / compositor / shm.c
1 /*
2  * Copyright © 2010 Kristian Høgsberg
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24
25 #include "compositor.h"
26
27 struct wlsc_shm_buffer {
28         struct wl_buffer buffer;
29         struct wl_list surfaces_attached_to;
30         int32_t stride;
31         void *data;
32         int mapped;
33 };
34
35 static void
36 destroy_buffer(struct wl_resource *resource, struct wl_client *client)
37 {
38         struct wlsc_shm_buffer *buffer =
39                 container_of(resource, struct wlsc_shm_buffer, buffer.resource);
40         struct wlsc_surface *es, *next;
41
42         if (buffer->mapped)
43                 munmap(buffer->data, buffer->stride * buffer->buffer.height);
44         else
45                 free(buffer->data);
46
47         wl_list_for_each_safe(es, next, &buffer->surfaces_attached_to,
48                               buffer_link) {
49                 es->buffer = NULL;
50                 wl_list_remove(&es->buffer_link);
51         }
52
53         free(buffer);
54 }
55
56 static void
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)
59 {
60         struct wlsc_shm_buffer *buffer =
61                 (struct wlsc_shm_buffer *) buffer_base;
62         struct wlsc_surface *es;
63
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. */
71         }
72 }
73
74 static void
75 buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
76 {
77         wl_resource_destroy(&buffer->resource, client);
78 }
79
80 const static struct wl_buffer_interface buffer_interface = {
81         buffer_damage,
82         buffer_destroy
83 };
84
85 int
86 wlsc_is_shm_buffer(struct wl_buffer *buffer)
87 {
88         return buffer->resource.object.implementation == 
89                 (void (**)(void)) &buffer_interface;
90 }
91
92 void
93 wlsc_shm_buffer_attach(struct wl_buffer *buffer_base,
94                        struct wl_surface *surface)
95 {
96         struct wlsc_surface *es = (struct wlsc_surface *) surface;
97         struct wlsc_shm_buffer *buffer =
98                 (struct wlsc_shm_buffer *) buffer_base;
99
100         glBindTexture(GL_TEXTURE_2D, es->texture);
101
102         /* Unbind any EGLImage texture that may be bound, so we don't
103          * overwrite it.*/
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;
110
111         if (es->buffer)
112                 wl_list_remove(&es->buffer_link);
113         wl_list_insert(&buffer->surfaces_attached_to, &es->buffer_link);
114 }
115
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,
120                      void *data)
121 {
122         struct wlsc_shm_buffer *buffer;
123
124         buffer = malloc(sizeof *buffer);
125         if (buffer == NULL)
126                 return NULL;
127
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;
133         buffer->data = data;
134
135         wl_list_init(&buffer->surfaces_attached_to);
136
137         buffer->buffer.resource.object.interface = &wl_buffer_interface;
138         buffer->buffer.resource.object.implementation = (void (**)(void))
139                 &buffer_interface;
140
141         buffer->buffer.resource.destroy = destroy_buffer;
142
143         return buffer;
144 }
145
146 static void
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)
150 {
151         struct wlsc_compositor *compositor =
152                 container_of((struct wlsc_shm *) shm,
153                              struct wlsc_compositor, shm);
154         struct wlsc_shm_buffer *buffer;
155         void *data;
156
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");
164                 close(fd);
165                 return;
166         }
167
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);
172                 fprintf(stderr,
173                         "invalid width, height or stride in create_buffer\n");
174                 close(fd);
175                 return;
176         }
177
178         data = mmap(NULL, stride * height, PROT_READ, MAP_SHARED, fd, 0);
179
180         close(fd);
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);
188                 return;
189         }
190
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);
196                 return;
197         }
198         buffer->mapped = 1;
199
200         buffer->buffer.resource.object.id = id;
201
202         wl_client_add_resource(client, &buffer->buffer.resource);
203 }
204
205 const static struct wl_shm_interface shm_interface = {
206         shm_create_buffer
207 };
208
209 int
210 wlsc_shm_init(struct wlsc_compositor *ec)
211 {
212         struct wlsc_shm *shm = &ec->shm;
213
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);
218
219         return 0;
220 }