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-server.h"
39 struct wl_resource resource;
45 struct wl_shm_buffer {
46 struct wl_buffer buffer;
50 struct wl_shm_pool *pool;
54 shm_pool_unref(struct wl_shm_pool *pool)
60 munmap(pool->data, pool->size);
65 destroy_buffer(struct wl_resource *resource)
67 struct wl_shm_buffer *buffer =
68 container_of(resource, struct wl_shm_buffer, buffer.resource);
71 shm_pool_unref(buffer->pool);
76 shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
78 wl_resource_destroy(resource);
81 static const struct wl_buffer_interface shm_buffer_interface = {
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)
91 struct wl_shm_pool *pool = resource->data;
92 struct wl_shm_buffer *buffer;
95 case WL_SHM_FORMAT_ARGB8888:
96 case WL_SHM_FORMAT_XRGB8888:
99 wl_resource_post_error(resource,
100 WL_SHM_ERROR_INVALID_FORMAT,
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);
115 buffer = malloc(sizeof *buffer);
116 if (buffer == NULL) {
117 wl_resource_post_no_memory(resource);
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;
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;
135 buffer->buffer.resource.data = buffer;
136 buffer->buffer.resource.client = resource->client;
137 buffer->buffer.resource.destroy = destroy_buffer;
139 wl_client_add_resource(client, &buffer->buffer.resource);
143 destroy_pool(struct wl_resource *resource)
145 struct wl_shm_pool *pool = resource->data;
147 shm_pool_unref(pool);
151 shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
153 wl_resource_destroy(resource);
157 shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
160 struct wl_shm_pool *pool = resource->data;
163 data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
165 if (data == MAP_FAILED) {
166 wl_resource_post_error(resource,
167 WL_SHM_ERROR_INVALID_FD,
176 struct wl_shm_pool_interface shm_pool_interface = {
177 shm_pool_create_buffer,
183 shm_create_pool(struct wl_client *client, struct wl_resource *resource,
184 uint32_t id, int fd, int32_t size)
186 struct wl_shm_pool *pool;
188 pool = malloc(sizeof *pool);
190 wl_resource_post_no_memory(resource);
196 wl_resource_post_error(resource,
197 WL_SHM_ERROR_INVALID_STRIDE,
198 "invalid size (%d)", size);
205 pool->data = mmap(NULL, size,
206 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
208 if (pool->data == MAP_FAILED) {
209 wl_resource_post_error(resource,
210 WL_SHM_ERROR_INVALID_FD,
211 "failed mmap fd %d", fd);
215 pool->resource.object.id = id;
216 pool->resource.object.interface = &wl_shm_pool_interface;
217 pool->resource.object.implementation =
218 (void (**)(void)) &shm_pool_interface;
220 pool->resource.data = pool;
221 pool->resource.client = client;
222 pool->resource.destroy = destroy_pool;
224 wl_client_add_resource(client, &pool->resource);
227 static const struct wl_shm_interface shm_interface = {
232 bind_shm(struct wl_client *client,
233 void *data, uint32_t version, uint32_t id)
235 struct wl_resource *resource;
237 resource = wl_client_add_object(client, &wl_shm_interface,
238 &shm_interface, id, data);
240 wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
241 wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
245 wl_display_init_shm(struct wl_display *display)
247 if (!wl_display_add_global(display, &wl_shm_interface, NULL, bind_shm))
254 wl_buffer_is_shm(struct wl_buffer *buffer)
256 return buffer->resource.object.implementation ==
257 (void (**)(void)) &shm_buffer_interface;
261 wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
263 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
265 if (!wl_buffer_is_shm(buffer_base))
268 return buffer->stride;
272 wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
274 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
276 if (!wl_buffer_is_shm(buffer_base))
279 return buffer->pool->data + buffer->offset;
283 wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
285 struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
287 return buffer->format;