shm: Add request for resizing pools
[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_pool {
37         struct wl_resource resource;
38         int refcount;
39         char *data;
40         int size;
41         int fd;
42 };
43
44 struct wl_shm_buffer {
45         struct wl_buffer buffer;
46         int32_t stride;
47         uint32_t format;
48         int offset;
49         struct wl_shm_pool *pool;
50 };
51
52 static void
53 shm_pool_unref(struct wl_shm_pool *pool)
54 {
55         pool->refcount--;
56         if (pool->refcount)
57                 return;
58
59         munmap(pool->data, pool->size);
60         close(pool->fd);
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->format = format;
124         buffer->stride = stride;
125         buffer->offset = offset;
126         buffer->pool = pool;
127         pool->refcount++;
128
129         buffer->buffer.resource.object.id = id;
130         buffer->buffer.resource.object.interface = &wl_buffer_interface;
131         buffer->buffer.resource.object.implementation = (void (**)(void))
132                 &shm_buffer_interface;
133
134         buffer->buffer.resource.data = buffer;
135         buffer->buffer.resource.client = resource->client;
136         buffer->buffer.resource.destroy = destroy_buffer;
137
138         wl_client_add_resource(client, &buffer->buffer.resource);
139 }
140
141 static void
142 destroy_pool(struct wl_resource *resource)
143 {
144         struct wl_shm_pool *pool = resource->data;
145
146         shm_pool_unref(pool);
147 }
148
149 static void
150 shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
151 {
152         wl_resource_destroy(resource);
153 }
154
155 static void
156 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
157                 int32_t size)
158 {
159         struct wl_shm_pool *pool = resource->data;
160         void *data;
161
162         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
163                     pool->fd, 0);
164
165         if (data == MAP_FAILED) {
166                 wl_resource_post_error(resource,
167                                        WL_SHM_ERROR_INVALID_FD,
168                                        "failed mmap fd %d", pool->fd);
169                 return;
170         }
171
172         munmap(pool->data, pool->size);
173         pool->data = data;
174         pool->size = size;
175 }
176
177 struct wl_shm_pool_interface shm_pool_interface = {
178         shm_pool_create_buffer,
179         shm_pool_destroy,
180         shm_pool_resize
181 };
182
183 static void
184 shm_create_pool(struct wl_client *client, struct wl_resource *resource,
185                 uint32_t id, int fd, int32_t size)
186 {
187         struct wl_shm_pool *pool;
188
189         pool = malloc(sizeof *pool);
190         if (pool == NULL) {
191                 wl_resource_post_no_memory(resource);
192                 close(fd);
193                 return;
194         }
195
196         if (size <= 0) {
197                 wl_resource_post_error(resource,
198                                        WL_SHM_ERROR_INVALID_STRIDE,
199                                        "invalid size (%d)", size);
200                 close(fd);
201                 return;
202         }
203
204         pool->refcount = 1;
205         pool->fd = fd;
206         pool->size = size;
207         pool->data = mmap(NULL, size,
208                           PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
209         if (pool->data == MAP_FAILED) {
210                 wl_resource_post_error(resource,
211                                        WL_SHM_ERROR_INVALID_FD,
212                                        "failed mmap fd %d", fd);
213                 return;
214         }
215
216         pool->resource.object.id = id;
217         pool->resource.object.interface = &wl_shm_pool_interface;
218         pool->resource.object.implementation =
219                 (void (**)(void)) &shm_pool_interface;
220
221         pool->resource.data = pool;
222         pool->resource.client = client;
223         pool->resource.destroy = destroy_pool;
224
225         wl_client_add_resource(client, &pool->resource);
226 }
227
228 static const struct wl_shm_interface shm_interface = {
229         shm_create_pool
230 };
231
232 static void
233 bind_shm(struct wl_client *client,
234          void *data, uint32_t version, uint32_t id)
235 {
236         struct wl_resource *resource;
237
238         resource = wl_client_add_object(client, &wl_shm_interface,
239                                         &shm_interface, id, data);
240
241         wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
242         wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
243 }
244
245 WL_EXPORT int
246 wl_display_init_shm(struct wl_display *display)
247 {
248         if (!wl_display_add_global(display, &wl_shm_interface, NULL, bind_shm))
249                 return -1;
250
251         return 0;
252 }
253
254 WL_EXPORT int
255 wl_buffer_is_shm(struct wl_buffer *buffer)
256 {
257         return buffer->resource.object.implementation == 
258                 (void (**)(void)) &shm_buffer_interface;
259 }
260
261 WL_EXPORT int32_t
262 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
263 {
264         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
265
266         if (!wl_buffer_is_shm(buffer_base))
267                 return 0;
268
269         return buffer->stride;
270 }
271
272 WL_EXPORT void *
273 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
274 {
275         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
276
277         if (!wl_buffer_is_shm(buffer_base))
278                 return NULL;
279
280         return buffer->pool->data + buffer->offset;
281 }
282
283 WL_EXPORT uint32_t
284 wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
285 {
286         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
287
288         return buffer->format;
289 }