aboutsummaryrefslogtreecommitdiff
path: root/src/wld/wayland-shm.c
blob: 19840f6ebcfe65b6ff2e941e0887cccb468a70a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* wld: wayland-shm.c
 *
 * Copyright (c) 2013, 2014 Michael Forney
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#define _GNU_SOURCE /* Required for mkostemp */

#include "wld/wayland.h"
#include "wld/wayland-private.h"
#include "wld/wld-private.h"
#include "wld/pixman.h"

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>

struct shm_context
{
    struct wayland_context base;
    struct wl_registry * registry;
    struct wl_shm * wl;
    struct wl_array formats;
};

struct shm_buffer
{
    struct buffer base;
    int fd;
};

#define WAYLAND_IMPL_NAME shm
#include "interface/context.h"
#include "interface/buffer.h"
#include "interface/wayland.h"
IMPL(shm_context, wld_context)
IMPL(shm_buffer, wld_buffer)

static void registry_global(void * data, struct wl_registry * registry,
                            uint32_t name, const char * interface,
                            uint32_t version);
static void registry_global_remove(void * data, struct wl_registry * registry,
                                   uint32_t name);

static void shm_format(void * data, struct wl_shm * wl, uint32_t format);

const static struct wl_registry_listener registry_listener = {
    .global = &registry_global,
    .global_remove = &registry_global_remove
};

const static struct wl_shm_listener shm_listener = {
    .format = &shm_format,
};

static inline uint32_t format_wld_to_shm(uint32_t format)
{
    switch (format)
    {
        case WLD_FORMAT_ARGB8888:
            return WL_SHM_FORMAT_ARGB8888;
        case WLD_FORMAT_XRGB8888:
            return WL_SHM_FORMAT_XRGB8888;
        default:
            return 0;
    }
}

struct wayland_context * wayland_create_context(struct wl_display * display,
                                                struct wl_event_queue * queue)
{
    struct shm_context * context;

    if (!(context = malloc(sizeof *context)))
        goto error0;

    context_initialize(&context->base.base, &wld_context_impl);
    context->wl = NULL;
    wl_array_init(&context->formats);

    if (!(context->registry = wl_display_get_registry(display)))
    {
        DEBUG("Couldn't get registry\n");
        goto error1;
    }

    wl_registry_add_listener(context->registry, &registry_listener, context);
    wl_proxy_set_queue((struct wl_proxy *) context->registry, queue);

    /* Wait for wl_shm global. */
    wl_display_roundtrip_queue(display, queue);

    if (!context->wl)
    {
        DEBUG("No wl_shm global\n");
        goto error2;
    }

    wl_shm_add_listener(context->wl, &shm_listener, context);

    /* Wait for SHM formats. */
    wl_display_roundtrip_queue(display, queue);

    return &context->base;

  error2:
    wl_registry_destroy(context->registry);
  error1:
    wl_array_release(&context->formats);
    free(context);
  error0:
    return NULL;
}

bool wayland_has_format(struct wld_context * base, uint32_t format)
{
    struct shm_context * context = shm_context(base);
    uint32_t * supported_format;
    uint32_t shm_format = format_wld_to_shm(format);

    wl_array_for_each(supported_format, &context->formats)
    {
        if (*supported_format == shm_format)
            return true;
    }

    return false;
}

struct wld_renderer * context_create_renderer(struct wld_context * context)
{
    return wld_create_renderer(wld_pixman_context);
}

struct buffer * context_create_buffer(struct wld_context * base,
                                      uint32_t width, uint32_t height,
                                      uint32_t format, uint32_t flags)
{
    struct shm_context * context = shm_context(base);
    struct shm_buffer * buffer;
    char name[] = "/tmp/wld-XXXXXX";
    uint32_t pitch = width * format_bytes_per_pixel(format);
    size_t size = pitch * height;
    int fd;
    struct wl_shm_pool * pool;
    struct wl_buffer * wl;

    if (!wayland_has_format(base, format))
        goto error0;

    if (!(buffer = malloc(sizeof *buffer)))
        goto error0;

    fd = mkostemp(name, O_CLOEXEC);

    if (fd < 0)
        goto error1;

    unlink(name);

    if (posix_fallocate(fd, 0, size) != 0)
        goto error2;

    if (!(pool = wl_shm_create_pool(context->wl, fd, size)))
        goto error2;

    wl = wl_shm_pool_create_buffer(pool, 0, width, height, pitch,
                                   format_wld_to_shm(format));
    wl_shm_pool_destroy(pool);

    if (!wl)
        goto error2;

    buffer_initialize(&buffer->base, &wld_buffer_impl,
                      width, height, format, pitch);
    buffer->fd = fd;

    if (!(wayland_buffer_add_exporter(&buffer->base, wl)))
        goto error3;

    return &buffer->base;

  error3:
    wl_buffer_destroy(wl);
  error2:
    close(fd);
  error1:
    free(buffer);
  error0:
    return NULL;
}

struct buffer * context_import_buffer(struct wld_context * context,
                                      uint32_t type, union wld_object object,
                                      uint32_t width, uint32_t height,
                                      uint32_t format, uint32_t pitch)
{
    return NULL;
}

void context_destroy(struct wld_context * base)
{
    struct shm_context * context = shm_context(base);

    wl_shm_destroy(context->wl);
    wl_registry_destroy(context->registry);
    wl_array_release(&context->formats);
    wl_event_queue_destroy(context->base.queue);
    free(context);
}

/**** Buffer ****/

bool buffer_map(struct buffer * base)
{
    struct shm_buffer * buffer = shm_buffer(&base->base);
    void * data;

    data = mmap(NULL, buffer->base.base.pitch * buffer->base.base.height,
                PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0);

    if (data == MAP_FAILED)
        return false;

    buffer->base.base.map = data;

    return true;
}

bool buffer_unmap(struct buffer * buffer)
{
    if (munmap(buffer->base.map,
               buffer->base.pitch * buffer->base.height) == -1)
    {
        return false;
    }

    buffer->base.map = NULL;

    return true;
}

void buffer_destroy(struct buffer * base)
{
    struct shm_buffer * buffer = shm_buffer(&base->base);

    close(buffer->fd);
    free(buffer);
}

void registry_global(void * data, struct wl_registry * registry, uint32_t name,
                     const char * interface, uint32_t version)
{
    struct shm_context * context = data;

    if (strcmp(interface, "wl_shm") == 0)
        context->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1);
}

void registry_global_remove(void * data, struct wl_registry * registry,
                            uint32_t name)
{
}

void shm_format(void * data, struct wl_shm * wl, uint32_t format)
{
    struct shm_context * context = data;
    uint32_t * added_format;

    if (!(added_format = wl_array_add(&context->formats, sizeof format)))
        return;
    *added_format = format;
}