2 * Copyright © 2008 Kristian Høgsberg
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.
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
23 * Kristian Høgsberg <krh@bitplanet.net>
24 * Benjamin Franzke <benjaminfranzke@googlemail.com>
36 #include "wayland-private.h"
37 #include "wayland-server.h"
40 struct wl_resource resource;
46 struct wl_shm_buffer {
47 struct wl_buffer buffer;
51 struct wl_shm_pool *pool;
55 shm_pool_unref(struct wl_shm_pool *pool)
61 munmap(pool->data, pool->size);
66 destroy_buffer(struct wl_resource *resource)
68 struct wl_shm_buffer *buffer =
69 container_of(resource, struct wl_shm_buffer, buffer.resource);
72 shm_pool_unref(buffer->pool);
77 shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
79 wl_resource_destroy(resource);
82 static const struct wl_buffer_interface shm_buffer_interface = {
87 shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
88 uint32_t id, int32_t offset,
89 int32_t width, int32_t height,
90 int32_t stride, uint32_t format)
92 struct wl_shm_pool *pool = resource->data;
93 struct wl_shm_buffer *buffer;
96 case WL_SHM_FORMAT_ARGB8888:
97 case WL_SHM_FORMAT_XRGB8888:
100 wl_resource_post_error(resource,
101 WL_SHM_ERROR_INVALID_FORMAT,
106 if (offset < 0 || width <= 0 || height <= 0 || stride < width ||
107 INT32_MAX / stride <= height ||
108 offset > pool->size - stride * height) {
109 wl_resource_post_error(resource,
110 WL_SHM_ERROR_INVALID_STRIDE,
111 "invalid width, height or stride (%dx%d, %u)",
112 width, height, stride);
116 buffer = malloc(sizeof *buffer);
117 if (buffer == NULL) {
118 wl_resource_post_no_memory(resource);
122 buffer->buffer.width = width;
123 buffer->buffer.height = height;
124 buffer->buffer.busy_count = 0;
125 buffer->format = format;
126 buffer->stride = stride;
127 buffer->offset = offset;
131 buffer->buffer.resource.object.id = id;
132 buffer->buffer.resource.object.interface = &wl_buffer_interface;
133 buffer->buffer.resource.object.implementation = (void (**)(void))
134 &shm_buffer_interface;
136 buffer->buffer.resource.data = buffer;
137 buffer->buffer.resource.client = resource->client;
138 buffer->buffer.resource.destroy = destroy_buffer;
140 wl_client_add_resource(client, &buffer->buffer.resource);
144 destroy_pool(struct wl_resource *resource)
146 struct wl_shm_pool *pool = resource->data;
148 shm_pool_unref(pool);
152 shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
154 wl_resource_destroy(resource);
158 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
161 struct wl_shm_pool *pool = resource->data;
164 data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
166 if (data == MAP_FAILED) {
167 wl_resource_post_error(resource,
168 WL_SHM_ERROR_INVALID_FD,
177 struct wl_shm_pool_interface shm_pool_interface = {
178 shm_pool_create_buffer,
184 shm_create_pool(struct wl_client *client, struct wl_resource *resource,
185 uint32_t id, int fd, int32_t size)
187 struct wl_shm_pool *pool;
189 pool = malloc(sizeof *pool);
191 wl_resource_post_no_memory(resource);
196 wl_resource_post_error(resource,
197 WL_SHM_ERROR_INVALID_STRIDE,
198 "invalid size (%d)", size);
204 pool->data = mmap(NULL, size,
205 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
206 if (pool->data == MAP_FAILED) {
207 wl_resource_post_error(resource,
208 WL_SHM_ERROR_INVALID_FD,
209 "failed mmap fd %d", fd);
214 pool->resource.object.id = id;
215 pool->resource.object.interface = &wl_shm_pool_interface;
216 pool->resource.object.implementation =
217 (void (**)(void)) &shm_pool_interface;
219 pool->resource.data = pool;
220 pool->resource.client = client;
221 pool->resource.destroy = destroy_pool;
223 wl_client_add_resource(client, &pool->resource);
233 static const struct wl_shm_interface shm_interface = {
238 bind_shm(struct wl_client *client,
239 void *data, uint32_t version, uint32_t id)
241 struct wl_resource *resource;
243 resource = wl_client_add_object(client, &wl_shm_interface,
244 &shm_interface, id, data);
246 wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
247 wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
251 wl_display_init_shm(struct wl_display *display)
253 if (!wl_display_add_global(display, &wl_shm_interface, NULL, bind_shm))
259 WL_EXPORT struct wl_buffer *
260 wl_shm_buffer_create(struct wl_client *client,
261 uint32_t id, int32_t width, int32_t height,
262 int32_t stride, uint32_t format)
264 struct wl_shm_buffer *buffer;
267 case WL_SHM_FORMAT_ARGB8888:
268 case WL_SHM_FORMAT_XRGB8888:
274 buffer = malloc(sizeof *buffer + stride * height);
278 buffer->buffer.width = width;
279 buffer->buffer.height = height;
280 buffer->buffer.busy_count = 0;
281 buffer->format = format;
282 buffer->stride = stride;
286 buffer->buffer.resource.object.id = id;
287 buffer->buffer.resource.object.interface = &wl_buffer_interface;
288 buffer->buffer.resource.object.implementation = (void (**)(void))
289 &shm_buffer_interface;
291 buffer->buffer.resource.data = buffer;
292 buffer->buffer.resource.client = client;
293 buffer->buffer.resource.destroy = destroy_buffer;
295 wl_client_add_resource(client, &buffer->buffer.resource);
297 return &buffer->buffer;
301 wl_buffer_is_shm(struct wl_buffer *buffer)
303 return buffer->resource.object.implementation ==
304 (void (**)(void)) &shm_buffer_interface;
308 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
310 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
312 if (!wl_buffer_is_shm(buffer_base))
315 return buffer->stride;
319 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
321 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
323 if (!wl_buffer_is_shm(buffer_base))
327 return buffer->pool->data + buffer->offset;
333 wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
335 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
337 return buffer->format;
341 wl_shm_buffer_get_width(struct wl_buffer *buffer_base)
343 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
345 return buffer->buffer.width;
349 wl_shm_buffer_get_height(struct wl_buffer *buffer_base)
351 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
353 return buffer->buffer.height;