90c4cf765533cd2023f6a9d03849b2ea6879e68d
[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 #define _GNU_SOURCE
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include <unistd.h>
35
36 #include "wayland-server.h"
37
38 struct wl_shm_pool {
39         struct wl_resource resource;
40         int refcount;
41         char *data;
42         int size;
43 };
44
45 struct wl_shm_buffer {
46         struct wl_buffer buffer;
47         int32_t stride;
48         uint32_t format;
49         int offset;
50         struct wl_shm_pool *pool;
51 };
52
53 static void
54 shm_pool_unref(struct wl_shm_pool *pool)
55 {
56         pool->refcount--;
57         if (pool->refcount)
58                 return;
59
60         munmap(pool->data, pool->size);
61         free(pool);
62 }
63
64 static void
65 destroy_buffer(struct wl_resource *resource)
66 {
67         struct wl_shm_buffer *buffer =
68                 container_of(resource, struct wl_shm_buffer, buffer.resource);
69
70         if (buffer->pool)
71                 shm_pool_unref(buffer->pool);
72         free(buffer);
73 }
74
75 static void
76 shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
77 {
78         wl_resource_destroy(resource);
79 }
80
81 static const struct wl_buffer_interface shm_buffer_interface = {
82         shm_buffer_destroy
83 };
84
85 static void
86 shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
87                        uint32_t id, int32_t offset,
88                        int32_t width, int32_t height,
89                        int32_t stride, uint32_t format)
90 {
91         struct wl_shm_pool *pool = resource->data;
92         struct wl_shm_buffer *buffer;
93
94         switch (format) {
95         case WL_SHM_FORMAT_ARGB8888:
96         case WL_SHM_FORMAT_XRGB8888:
97                 break;
98         default:
99                 wl_resource_post_error(resource,
100                                        WL_SHM_ERROR_INVALID_FORMAT,
101                                        "invalid format");
102                 return;
103         }
104
105         if (offset < 0 || width <= 0 || height <= 0 || stride < width ||
106             INT32_MAX / stride <= height ||
107             offset > pool->size - stride * height) {
108                 wl_resource_post_error(resource,
109                                        WL_SHM_ERROR_INVALID_STRIDE,
110                                        "invalid width, height or stride (%dx%d, %u)",
111                                        width, height, stride);
112                 return;
113         }
114
115         buffer = malloc(sizeof *buffer);
116         if (buffer == NULL) {
117                 wl_resource_post_no_memory(resource);
118                 return;
119         }
120
121         buffer->buffer.width = width;
122         buffer->buffer.height = height;
123         buffer->buffer.busy_count = 0;
124         buffer->format = format;
125         buffer->stride = stride;
126         buffer->offset = offset;
127         buffer->pool = pool;
128         pool->refcount++;
129
130         buffer->buffer.resource.object.id = id;
131         buffer->buffer.resource.object.interface = &wl_buffer_interface;
132         buffer->buffer.resource.object.implementation = (void (**)(void))
133                 &shm_buffer_interface;
134
135         buffer->buffer.resource.data = buffer;
136         buffer->buffer.resource.client = resource->client;
137         buffer->buffer.resource.destroy = destroy_buffer;
138
139         wl_client_add_resource(client, &buffer->buffer.resource);
140 }
141
142 static void
143 destroy_pool(struct wl_resource *resource)
144 {
145         struct wl_shm_pool *pool = resource->data;
146
147         shm_pool_unref(pool);
148 }
149
150 static void
151 shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
152 {
153         wl_resource_destroy(resource);
154 }
155
156 static void
157 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
158                 int32_t size)
159 {
160         struct wl_shm_pool *pool = resource->data;
161         void *data;
162
163         data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
164
165         if (data == MAP_FAILED) {
166                 wl_resource_post_error(resource,
167                                        WL_SHM_ERROR_INVALID_FD,
168                                        "failed mremap");
169                 return;
170         }
171
172         pool->data = data;
173         pool->size = size;
174 }
175
176 struct wl_shm_pool_interface shm_pool_interface = {
177         shm_pool_create_buffer,
178         shm_pool_destroy,
179         shm_pool_resize
180 };
181
182 static void
183 shm_create_pool(struct wl_client *client, struct wl_resource *resource,
184                 uint32_t id, int fd, int32_t size)
185 {
186         struct wl_shm_pool *pool;
187
188         pool = malloc(sizeof *pool);
189         if (pool == NULL) {
190                 wl_resource_post_no_memory(resource);
191                 goto err_close;
192         }
193
194         if (size <= 0) {
195                 wl_resource_post_error(resource,
196                                        WL_SHM_ERROR_INVALID_STRIDE,
197                                        "invalid size (%d)", size);
198                 goto err_free;
199         }
200
201         pool->refcount = 1;
202         pool->size = size;
203         pool->data = mmap(NULL, size,
204                           PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
205         if (pool->data == MAP_FAILED) {
206                 wl_resource_post_error(resource,
207                                        WL_SHM_ERROR_INVALID_FD,
208                                        "failed mmap fd %d", fd);
209                 goto err_free;
210         }
211         close(fd);
212
213         pool->resource.object.id = id;
214         pool->resource.object.interface = &wl_shm_pool_interface;
215         pool->resource.object.implementation =
216                 (void (**)(void)) &shm_pool_interface;
217
218         pool->resource.data = pool;
219         pool->resource.client = client;
220         pool->resource.destroy = destroy_pool;
221
222         wl_client_add_resource(client, &pool->resource);
223
224         return;
225
226 err_close:
227         close(fd);
228 err_free:
229         free(pool);
230 }
231
232 static const struct wl_shm_interface shm_interface = {
233         shm_create_pool
234 };
235
236 static void
237 bind_shm(struct wl_client *client,
238          void *data, uint32_t version, uint32_t id)
239 {
240         struct wl_resource *resource;
241
242         resource = wl_client_add_object(client, &wl_shm_interface,
243                                         &shm_interface, id, data);
244
245         wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
246         wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
247 }
248
249 WL_EXPORT int
250 wl_display_init_shm(struct wl_display *display)
251 {
252         if (!wl_display_add_global(display, &wl_shm_interface, NULL, bind_shm))
253                 return -1;
254
255         return 0;
256 }
257
258 WL_EXPORT struct wl_buffer *
259 wl_shm_buffer_create(struct wl_client *client,
260                      uint32_t id, int32_t width, int32_t height,
261                      int32_t stride, uint32_t format)
262 {
263         struct wl_shm_buffer *buffer;
264                              
265         switch (format) {
266         case WL_SHM_FORMAT_ARGB8888:
267         case WL_SHM_FORMAT_XRGB8888:
268                 break;
269         default:
270                 return NULL;
271         }
272
273         buffer = malloc(sizeof *buffer + stride * height);
274         if (buffer == NULL)
275                 return NULL;
276
277         buffer->buffer.width = width;
278         buffer->buffer.height = height;
279         buffer->buffer.busy_count = 0;
280         buffer->format = format;
281         buffer->stride = stride;
282         buffer->offset = 0;
283         buffer->pool = NULL;
284
285         buffer->buffer.resource.object.id = id;
286         buffer->buffer.resource.object.interface = &wl_buffer_interface;
287         buffer->buffer.resource.object.implementation = (void (**)(void))
288                 &shm_buffer_interface;
289
290         buffer->buffer.resource.data = buffer;
291         buffer->buffer.resource.client = client;
292         buffer->buffer.resource.destroy = destroy_buffer;
293
294         wl_client_add_resource(client, &buffer->buffer.resource);
295
296         return &buffer->buffer;
297 }
298
299 WL_EXPORT int
300 wl_buffer_is_shm(struct wl_buffer *buffer)
301 {
302         return buffer->resource.object.implementation == 
303                 (void (**)(void)) &shm_buffer_interface;
304 }
305
306 WL_EXPORT int32_t
307 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
308 {
309         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
310
311         if (!wl_buffer_is_shm(buffer_base))
312                 return 0;
313
314         return buffer->stride;
315 }
316
317 WL_EXPORT void *
318 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
319 {
320         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
321
322         if (!wl_buffer_is_shm(buffer_base))
323                 return NULL;
324
325         if (buffer->pool)
326                 return buffer->pool->data + buffer->offset;
327         else
328                 return buffer + 1;
329 }
330
331 WL_EXPORT uint32_t
332 wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
333 {
334         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
335
336         return buffer->format;
337 }
338
339 WL_EXPORT int32_t
340 wl_shm_buffer_get_width(struct wl_buffer *buffer_base)
341 {
342         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
343
344         return buffer->buffer.width;
345 }
346
347 WL_EXPORT int32_t
348 wl_shm_buffer_get_height(struct wl_buffer *buffer_base)
349 {
350         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
351
352         return buffer->buffer.height;
353 }