neuwld

patched neuwld library (tracked)
git clone https://git.daat.foo/neuwld.git
Log | Files | Refs | README | LICENSE

wayland-shm.c (7654B)


      1 /* wld: wayland-shm.c
      2  *
      3  * Copyright (c) 2013, 2014 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #define _GNU_SOURCE /* Required for mkostemp */
     25 
     26 #include "pixman.h"
     27 #include "wayland-private.h"
     28 #include "wayland.h"
     29 #include "wld-private.h"
     30 
     31 #include <fcntl.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <sys/mman.h>
     35 #include <unistd.h>
     36 #include <wayland-client.h>
     37 
     38 static int
     39 wldtmp(char *name)
     40 {
     41 	int fd;
     42 	int flags;
     43 
     44 	fd = mkstemp(name);
     45 	if (fd < 0)
     46 		return -1;
     47 
     48 	flags = fcntl(fd, F_GETFD);
     49 	if (flags < 0 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
     50 		close(fd);
     51 		return -1;
     52 	}
     53 
     54 	return fd;
     55 }
     56 
     57 struct shm_context {
     58 	struct wayland_context base;
     59 	struct wl_registry *registry;
     60 	struct wl_shm *wl;
     61 	struct wl_array formats;
     62 };
     63 
     64 struct shm_buffer {
     65 	struct buffer base;
     66 	int fd;
     67 };
     68 
     69 #define WAYLAND_IMPL_NAME shm
     70 #include "interface/buffer.h"
     71 #include "interface/context.h"
     72 #include "interface/wayland.h"
     73 IMPL(shm_context, wld_context)
     74 IMPL(shm_buffer, wld_buffer)
     75 
     76 static void registry_global(void *data, struct wl_registry *registry,
     77                             uint32_t name, const char *interface,
     78                             uint32_t version);
     79 static void registry_global_remove(void *data, struct wl_registry *registry,
     80                                    uint32_t name);
     81 
     82 static void shm_format(void *data, struct wl_shm *wl, uint32_t format);
     83 
     84 const static struct wl_registry_listener registry_listener = {
     85 	.global = &registry_global,
     86 	.global_remove = &registry_global_remove
     87 };
     88 
     89 const static struct wl_shm_listener shm_listener = {
     90 	.format = &shm_format,
     91 };
     92 
     93 static inline uint32_t
     94 format_wld_to_shm(uint32_t format)
     95 {
     96 	switch (format) {
     97 	case WLD_FORMAT_ARGB8888:
     98 		return WL_SHM_FORMAT_ARGB8888;
     99 	case WLD_FORMAT_XRGB8888:
    100 		return WL_SHM_FORMAT_XRGB8888;
    101 	default:
    102 		return 0;
    103 	}
    104 }
    105 
    106 struct wayland_context *
    107 wayland_create_context(struct wl_display *display,
    108                        struct wl_event_queue *queue)
    109 {
    110 	struct shm_context *context;
    111 
    112 	if (!(context = malloc(sizeof *context)))
    113 		goto error0;
    114 
    115 	context_initialize(&context->base.base, &wld_context_impl);
    116 	context->wl = NULL;
    117 	wl_array_init(&context->formats);
    118 
    119 	if (!(context->registry = wl_display_get_registry(display))) {
    120 		DEBUG("Couldn't get registry\n");
    121 		goto error1;
    122 	}
    123 
    124 	wl_registry_add_listener(context->registry, &registry_listener, context);
    125 	wl_proxy_set_queue((struct wl_proxy *)context->registry, queue);
    126 
    127 	/* Wait for wl_shm global. */
    128 	wl_display_roundtrip_queue(display, queue);
    129 
    130 	if (!context->wl) {
    131 		DEBUG("No wl_shm global\n");
    132 		goto error2;
    133 	}
    134 
    135 	wl_shm_add_listener(context->wl, &shm_listener, context);
    136 
    137 	/* Wait for SHM formats. */
    138 	wl_display_roundtrip_queue(display, queue);
    139 
    140 	return &context->base;
    141 
    142 error2:
    143 	wl_registry_destroy(context->registry);
    144 error1:
    145 	wl_array_release(&context->formats);
    146 	free(context);
    147 error0:
    148 	return NULL;
    149 }
    150 
    151 bool
    152 wayland_has_format(struct wld_context *base, uint32_t format)
    153 {
    154 	struct shm_context *context = shm_context(base);
    155 	uint32_t *supported_format;
    156 	uint32_t shm_format = format_wld_to_shm(format);
    157 
    158 	wl_array_for_each (supported_format, &context->formats) {
    159 		if (*supported_format == shm_format)
    160 			return true;
    161 	}
    162 
    163 	return false;
    164 }
    165 
    166 struct wld_renderer *
    167 context_create_renderer(struct wld_context *context)
    168 {
    169 	return wld_create_renderer(wld_pixman_context);
    170 }
    171 
    172 struct buffer *
    173 context_create_buffer(struct wld_context *base,
    174                       uint32_t width, uint32_t height,
    175                       uint32_t format, uint32_t flags)
    176 {
    177 	struct shm_context *context = shm_context(base);
    178 	struct shm_buffer *buffer;
    179 	char name[] = "/tmp/wld-XXXXXX";
    180 	uint32_t pitch = width * format_bytes_per_pixel(format);
    181 	size_t size = pitch * height;
    182 	int fd;
    183 	struct wl_shm_pool *pool;
    184 	struct wl_buffer *wl;
    185 
    186 	if (!wayland_has_format(base, format))
    187 		goto error0;
    188 
    189 	if (!(buffer = malloc(sizeof *buffer)))
    190 		goto error0;
    191 
    192 	fd = wldtmp(name);
    193 
    194 	if (fd < 0)
    195 		goto error1;
    196 
    197 	unlink(name);
    198 
    199 #if defined(__linux__)
    200 	/* try to preallocate */
    201 	if (posix_fallocate(fd, 0, size) != 0 && ftruncate(fd, size) != 0)
    202 		goto error2;
    203 #else
    204 	/* ftruncate() is enough otherwise */
    205 	if (ftruncate(fd, size) != 0)
    206 		goto error2;
    207 #endif
    208 
    209 	if (!(pool = wl_shm_create_pool(context->wl, fd, size)))
    210 		goto error2;
    211 
    212 	wl = wl_shm_pool_create_buffer(pool, 0, width, height, pitch,
    213 	                               format_wld_to_shm(format));
    214 	wl_shm_pool_destroy(pool);
    215 
    216 	if (!wl)
    217 		goto error2;
    218 
    219 	buffer_initialize(&buffer->base, &wld_buffer_impl,
    220 	                  width, height, format, pitch);
    221 	buffer->fd = fd;
    222 
    223 	if (!(wayland_buffer_add_exporter(&buffer->base, wl)))
    224 		goto error3;
    225 
    226 	return &buffer->base;
    227 
    228 error3:
    229 	wl_buffer_destroy(wl);
    230 error2:
    231 	close(fd);
    232 error1:
    233 	free(buffer);
    234 error0:
    235 	return NULL;
    236 }
    237 
    238 struct buffer *
    239 context_import_buffer(struct wld_context *context,
    240                       uint32_t type, union wld_object object,
    241                       uint32_t width, uint32_t height,
    242                       uint32_t format, uint32_t pitch)
    243 {
    244 	return NULL;
    245 }
    246 
    247 void
    248 context_destroy(struct wld_context *base)
    249 {
    250 	struct shm_context *context = shm_context(base);
    251 
    252 	wl_shm_destroy(context->wl);
    253 	wl_registry_destroy(context->registry);
    254 	wl_array_release(&context->formats);
    255 	wl_event_queue_destroy(context->base.queue);
    256 	free(context);
    257 }
    258 
    259 /**** Buffer ****/
    260 
    261 bool
    262 buffer_map(struct buffer *base)
    263 {
    264 	struct shm_buffer *buffer = shm_buffer(&base->base);
    265 	void *data;
    266 
    267 	data = mmap(NULL, buffer->base.base.pitch * buffer->base.base.height,
    268 	            PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0);
    269 
    270 	if (data == MAP_FAILED)
    271 		return false;
    272 
    273 	buffer->base.base.map = data;
    274 
    275 	return true;
    276 }
    277 
    278 bool
    279 buffer_unmap(struct buffer *buffer)
    280 {
    281 	if (munmap(buffer->base.map,
    282 	           buffer->base.pitch * buffer->base.height)
    283 	    == -1) {
    284 		return false;
    285 	}
    286 
    287 	buffer->base.map = NULL;
    288 
    289 	return true;
    290 }
    291 
    292 void
    293 buffer_destroy(struct buffer *base)
    294 {
    295 	struct shm_buffer *buffer = shm_buffer(&base->base);
    296 
    297 	close(buffer->fd);
    298 	free(buffer);
    299 }
    300 
    301 void
    302 registry_global(void *data, struct wl_registry *registry, uint32_t name,
    303                 const char *interface, uint32_t version)
    304 {
    305 	struct shm_context *context = data;
    306 
    307 	if (strcmp(interface, "wl_shm") == 0)
    308 		context->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1);
    309 }
    310 
    311 void
    312 registry_global_remove(void *data, struct wl_registry *registry,
    313                        uint32_t name)
    314 {
    315 }
    316 
    317 void
    318 shm_format(void *data, struct wl_shm *wl, uint32_t format)
    319 {
    320 	struct shm_context *context = data;
    321 	uint32_t *added_format;
    322 
    323 	if (!(added_format = wl_array_add(&context->formats, sizeof format)))
    324 		return;
    325 	*added_format = format;
    326 }