2998c3d028cf841214eab3c7c10e4cec67bdc947
[profile/ivi/wayland.git] / src / wayland-shm.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  *
22  * Authors:
23  *    Kristian Høgsberg <krh@bitplanet.net>
24  *    Benjamin Franzke <benjaminfranzke@googlemail.com>
25  *
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32 #include <unistd.h>
33
34 #include "wayland-server.h"
35
36 struct wl_shm {
37         const struct wl_shm_callbacks *callbacks;
38 };
39
40 struct wl_shm_buffer {
41         struct wl_buffer buffer;
42         struct wl_shm *shm;
43         int32_t stride;
44         uint32_t format;
45         void *data;
46 };
47
48 static void
49 destroy_buffer(struct wl_resource *resource)
50 {
51         struct wl_shm_buffer *buffer =
52                 container_of(resource, struct wl_shm_buffer, buffer.resource);
53
54         munmap(buffer->data, buffer->stride * buffer->buffer.height);
55
56         buffer->shm->callbacks->buffer_destroyed(&buffer->buffer);
57
58         free(buffer);
59 }
60
61 static void
62 shm_buffer_damage(struct wl_client *client, struct wl_resource *resource,
63                   int32_t x, int32_t y, int32_t width, int32_t height)
64 {
65         struct wl_shm_buffer *buffer = resource->data;
66
67         buffer->shm->callbacks->buffer_damaged(&buffer->buffer, x, y,
68                                                width, height);
69 }
70
71 static void
72 shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
73 {
74         wl_resource_destroy(resource, 0);
75 }
76
77 const static struct wl_buffer_interface shm_buffer_interface = {
78         shm_buffer_damage,
79         shm_buffer_destroy
80 };
81
82 static struct wl_shm_buffer *
83 wl_shm_buffer_init(struct wl_shm *shm, struct wl_client *client, uint32_t id,
84                    int32_t width, int32_t height,
85                    int32_t stride, uint32_t format, void *data)
86 {
87         struct wl_shm_buffer *buffer;
88
89         buffer = calloc(1, sizeof *buffer);
90         if (buffer == NULL)
91                 return NULL;
92
93         buffer->buffer.width = width;
94         buffer->buffer.height = height;
95         buffer->format = format;
96         buffer->stride = stride;
97         buffer->data = data;
98
99         buffer->buffer.resource.object.id = id;
100         buffer->buffer.resource.object.interface = &wl_buffer_interface;
101         buffer->buffer.resource.object.implementation = (void (**)(void))
102                 &shm_buffer_interface;
103
104         buffer->buffer.resource.data = buffer;
105         buffer->buffer.resource.client = client;
106         buffer->buffer.resource.destroy = destroy_buffer;
107
108         buffer->shm = shm;
109         
110         buffer->shm->callbacks->buffer_created(&buffer->buffer);
111
112         return buffer;
113 }
114
115 static void
116 shm_create_buffer(struct wl_client *client, struct wl_resource *resource,
117                   uint32_t id, int fd, int32_t width, int32_t height,
118                   uint32_t stride, uint32_t format)
119 {
120         struct wl_shm *shm = resource->data;
121         struct wl_shm_buffer *buffer;
122         void *data;
123
124
125         switch (format) {
126         case WL_SHM_FORMAT_ARGB8888:
127         case WL_SHM_FORMAT_XRGB8888:
128                 break;
129         default:
130                 wl_resource_post_error(resource,
131                                        WL_SHM_ERROR_INVALID_FORMAT,
132                                        "invalid format");
133                 close(fd);
134                 return;
135         }
136
137         if (width < 0 || height < 0 || stride < width) {
138                 wl_resource_post_error(resource,
139                                        WL_SHM_ERROR_INVALID_STRIDE,
140                                        "invalid width, height or stride (%dx%d, %u)",
141                                        width, height, stride);
142                 close(fd);
143                 return;
144         }
145
146         data = mmap(NULL, stride * height,
147                     PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
148
149         close(fd);
150         if (data == MAP_FAILED) {
151                 wl_resource_post_error(resource,
152                                        WL_SHM_ERROR_INVALID_FD,
153                                        "failed mmap fd %d", fd);
154                 return;
155         }
156
157         buffer = wl_shm_buffer_init(shm, client, id,
158                                     width, height, stride, format, data);
159         if (buffer == NULL) {
160                 munmap(data, stride * height);
161                 wl_resource_post_no_memory(resource);
162                 return;
163         }
164
165         wl_client_add_resource(client, &buffer->buffer.resource);
166 }
167
168 const static struct wl_shm_interface shm_interface = {
169         shm_create_buffer
170 };
171
172 static void
173 bind_shm(struct wl_client *client,
174          void *data, uint32_t version, uint32_t id)
175 {
176         struct wl_resource *resource;
177
178         resource = wl_client_add_object(client, &wl_shm_interface,
179                                         &shm_interface, id, data);
180
181         wl_resource_post_event(resource, WL_SHM_FORMAT,
182                                WL_SHM_FORMAT_ARGB8888);
183         wl_resource_post_event(resource, WL_SHM_FORMAT,
184                                WL_SHM_FORMAT_XRGB8888);
185 }
186
187 WL_EXPORT struct wl_shm *
188 wl_shm_init(struct wl_display *display,
189             const struct wl_shm_callbacks *callbacks)
190 {
191         struct wl_shm *shm;
192
193         shm = malloc(sizeof *shm);
194         if (!shm)
195                 return NULL;
196
197         if (!wl_display_add_global(display,
198                                    &wl_shm_interface, shm, bind_shm)) {
199
200                 free(shm);
201                 return NULL;
202         }
203
204         shm->callbacks = callbacks;
205
206         return shm;
207 }
208
209 WL_EXPORT void
210 wl_shm_finish(struct wl_shm *shm)
211 {
212         /* FIXME: add wl_display_del_{object,global} */
213
214         free(shm);
215 }
216
217 WL_EXPORT int
218 wl_buffer_is_shm(struct wl_buffer *buffer)
219 {
220         return buffer->resource.object.implementation == 
221                 (void (**)(void)) &shm_buffer_interface;
222 }
223
224 WL_EXPORT int32_t
225 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
226 {
227         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
228
229         if (!wl_buffer_is_shm(buffer_base))
230                 return 0;
231
232         return buffer->stride;
233 }
234
235 WL_EXPORT void *
236 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
237 {
238         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
239
240         if (!wl_buffer_is_shm(buffer_base))
241                 return NULL;
242
243         return buffer->data;
244 }
245
246 WL_EXPORT uint32_t
247 wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
248 {
249         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
250
251         return buffer->format;
252 }