90d2fcfb7530c1af0dc60759b3ef590f1a47ebc9
[profile/ivi/wayland.git] / wayland / 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         struct wl_object object;
38         const struct wl_shm_callbacks *callbacks;
39 };
40
41 struct wl_shm_buffer {
42         struct wl_buffer buffer;
43         struct wl_shm *shm;
44         int32_t stride;
45         void *data;
46 };
47
48 static void
49 destroy_buffer(struct wl_resource *resource, struct wl_client *client)
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_buffer *buffer_base,
63                   int32_t x, int32_t y, int32_t width, int32_t height)
64 {
65         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
66
67         buffer->shm->callbacks->buffer_damaged(buffer_base, x, y,
68                                                width, height);
69 }
70
71 static void
72 shm_buffer_destroy(struct wl_client *client, struct wl_buffer *buffer)
73 {
74         wl_resource_destroy(&buffer->resource, client, 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, struct wl_visual *visual,
86                    void *data)
87 {
88         struct wl_shm_buffer *buffer;
89
90         buffer = calloc(1, sizeof *buffer);
91         if (buffer == NULL)
92                 return NULL;
93
94         buffer->buffer.width = width;
95         buffer->buffer.height = height;
96         buffer->buffer.visual = visual;
97         buffer->buffer.client = client;
98         buffer->stride = stride;
99         buffer->data = data;
100
101         buffer->buffer.resource.object.id = id;
102         buffer->buffer.resource.object.interface = &wl_buffer_interface;
103         buffer->buffer.resource.object.implementation = (void (**)(void))
104                 &shm_buffer_interface;
105
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_shm *shm,
117                   uint32_t id, int fd, int32_t width, int32_t height,
118                   uint32_t stride, struct wl_visual *visual)
119 {
120         struct wl_shm_buffer *buffer;
121         void *data;
122
123         if (!visual || visual->object.interface != &wl_visual_interface) {
124                 wl_client_post_error(client, &shm->object,
125                                      WL_SHM_ERROR_INVALID_VISUAL,
126                                      "invalid visual");
127                 close(fd);
128                 return;
129         }
130
131         if (width < 0 || height < 0 || stride < width) {
132                 wl_client_post_error(client, &shm->object,
133                                      WL_SHM_ERROR_INVALID_STRIDE,
134                                      "invalid width, height or stride (%dx%d, %u)",
135                                      width, height, stride);
136                 close(fd);
137                 return;
138         }
139
140         data = mmap(NULL, stride * height,
141                     PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
142
143         close(fd);
144         if (data == MAP_FAILED) {
145                 wl_client_post_error(client, &shm->object,
146                                      WL_SHM_ERROR_INVALID_FD,
147                                      "failed mmap fd %d", fd);
148                 return;
149         }
150
151         buffer = wl_shm_buffer_init(shm, client, id,
152                                     width, height, stride, visual,
153                                     data);
154         if (buffer == NULL) {
155                 munmap(data, stride * height);
156                 wl_client_post_no_memory(client);
157                 return;
158         }
159
160         wl_client_add_resource(client, &buffer->buffer.resource);
161 }
162
163 const static struct wl_shm_interface shm_interface = {
164         shm_create_buffer
165 };
166
167
168 WL_EXPORT struct wl_shm *
169 wl_shm_init(struct wl_display *display,
170             const struct wl_shm_callbacks *callbacks)
171 {
172         struct wl_shm *shm;
173
174         shm = malloc(sizeof *shm);
175         if (!shm)
176                 return NULL;
177
178         shm->object.interface = &wl_shm_interface;
179         shm->object.implementation = (void (**)(void)) &shm_interface;
180         wl_display_add_object(display, &shm->object);
181         wl_display_add_global(display, &shm->object, NULL);
182
183         shm->callbacks = callbacks;
184
185         return shm;
186 }
187
188 WL_EXPORT void
189 wl_shm_finish(struct wl_shm *shm)
190 {
191         /* FIXME: add wl_display_del_{object,global} */
192
193         free(shm);
194 }
195
196 WL_EXPORT int
197 wl_buffer_is_shm(struct wl_buffer *buffer)
198 {
199         return buffer->resource.object.implementation == 
200                 (void (**)(void)) &shm_buffer_interface;
201 }
202
203 WL_EXPORT int32_t
204 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
205 {
206         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
207
208         if (!wl_buffer_is_shm(buffer_base))
209                 return 0;
210
211         return buffer->stride;
212 }
213
214 WL_EXPORT void *
215 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
216 {
217         struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
218
219         if (!wl_buffer_is_shm(buffer_base))
220                 return NULL;
221
222         return buffer->data;
223 }