summaryrefslogtreecommitdiff
path: root/dri3/dri3_screen.c
blob: 23e33f4014b5c84caa8b21f52b9c9ba9614a39ec (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright © 2013 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include "dri3_priv.h"
#include <syncsdk.h>
#include <misync.h>
#include <misyncshm.h>
#include <randrstr.h>
#include <drm_fourcc.h>
#include <unistd.h>

int
dri3_open(ClientPtr client, ScreenPtr screen, RRProviderPtr provider, int *fd)
{
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec *info = ds->info;

    if (info == NULL)
        return BadMatch;

    if (info->version >= 1 && info->open_client != NULL)
        return (*info->open_client) (client, screen, provider, fd);
    if (info->open != NULL)
        return (*info->open) (screen, provider, fd);

    return BadMatch;
}

int
dri3_pixmap_from_fds(PixmapPtr *ppixmap, ScreenPtr screen,
                     CARD8 num_fds, const int *fds,
                     CARD16 width, CARD16 height,
                     const CARD32 *strides, const CARD32 *offsets,
                     CARD8 depth, CARD8 bpp, CARD64 modifier)
{
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec *info = ds->info;
    PixmapPtr                   pixmap;

    if (!info)
        return BadImplementation;

    if (info->version >= 2 && info->pixmap_from_fds != NULL) {
        pixmap = (*info->pixmap_from_fds) (screen, num_fds, fds, width, height,
                                           strides, offsets, depth, bpp, modifier);
    } else if (info->pixmap_from_fd != NULL && num_fds == 1 &&
               modifier == DRM_FORMAT_MOD_INVALID) {
        pixmap = (*info->pixmap_from_fd) (screen, fds[0], width, height,
                                          strides[0], depth, bpp);
    } else {
        return BadImplementation;
    }

    if (!pixmap)
        return BadAlloc;

    *ppixmap = pixmap;
    return Success;
}

int
dri3_fds_from_pixmap(PixmapPtr pixmap, int *fds,
                     CARD32 *strides, CARD32 *offsets,
                     CARD64 *modifier)
{
    ScreenPtr                   screen = pixmap->drawable.pScreen;
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec *info = ds->info;

    if (!info)
        return 0;

    if (info->version >= 2 && info->fds_from_pixmap != NULL) {
        return (*info->fds_from_pixmap)(screen, pixmap, fds, strides, offsets,
                                        modifier);
    } else if (info->fd_from_pixmap != NULL) {
        CARD16 stride;
        CARD32 size;

        fds[0] = (*info->fd_from_pixmap)(screen, pixmap, &stride, &size);
        if (fds[0] < 0)
            return 0;

        strides[0] = stride;
        offsets[0] = 0;
        *modifier = DRM_FORMAT_MOD_INVALID;
        return 1;
    } else {
        return 0;
    }
}

int
dri3_fd_from_pixmap(PixmapPtr pixmap, CARD16 *stride, CARD32 *size)
{
    ScreenPtr                   screen = pixmap->drawable.pScreen;
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec  *info = ds->info;
    CARD32                      strides[4];
    CARD32                      offsets[4];
    CARD64                      modifier;
    int                         fds[4];
    int                         num_fds;

    if (!info)
        return -1;

    /* Preferentially use the old interface, allowing the implementation to
     * ensure the buffer is in a single-plane format which doesn't need
     * modifiers. */
    if (info->fd_from_pixmap != NULL)
        return (*info->fd_from_pixmap)(screen, pixmap, stride, size);

    if (info->version < 2 || info->fds_from_pixmap == NULL)
        return -1;

    /* If using the new interface, make sure that it's a single plane starting
     * at 0 within the BO. We don't check the modifier, as the client may
     * have an auxiliary mechanism for determining the modifier itself. */
    num_fds = info->fds_from_pixmap(screen, pixmap, fds, strides, offsets,
                                    &modifier);
    if (num_fds != 1 || offsets[0] != 0) {
        int i;
        for (i = 0; i < num_fds; i++)
            close(fds[i]);
        return -1;
    }

    *stride = strides[0];
    *size = size[0];
    return fds[0];
}

static int
cache_formats_and_modifiers(ScreenPtr screen)
{
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec *info = ds->info;
    CARD32                      num_formats;
    CARD32                     *formats;
    CARD32                      num_modifiers;
    CARD64                     *modifiers;
    int                         i;

    if (ds->formats_cached)
        return Success;

    if (!info)
        return BadImplementation;

    if (!info->get_formats || !info->get_modifiers) {
        ds->formats = NULL;
        ds->num_formats = 0;
        ds->formats_cached = TRUE;
        return Success;
    }

    if (!info->get_formats(screen, &num_formats, &formats))
        return BadAlloc;

    if (!num_formats) {
        ds->num_formats = 0;
        ds->formats_cached = TRUE;
        return Success;
    }

    ds->formats = calloc(num_formats, sizeof(dri3_dmabuf_format_rec));
    if (!ds->formats)
        return BadAlloc;

    for (i = 0; i < num_formats; i++) {
        dri3_dmabuf_format_ptr iter = &ds->formats[i];

        if (!info->get_modifiers(screen, formats[i],
                                 &num_modifiers,
                                 &modifiers))
            continue;

        if (!num_modifiers)
            continue;

        iter->format = formats[i];
        iter->num_modifiers = num_modifiers;
        iter->modifiers = modifiers;
    }

    ds->num_formats = i;
    ds->formats_cached = TRUE;

    return Success;
}

int
dri3_get_supported_modifiers(ScreenPtr screen, DrawablePtr drawable,
                             CARD8 depth, CARD8 bpp,
                             CARD32 *num_intersect_modifiers,
                             CARD64 **intersect_modifiers,
                             CARD32 *num_screen_modifiers,
                             CARD64 **screen_modifiers)
{
    dri3_screen_priv_ptr        ds = dri3_screen_priv(screen);
    const dri3_screen_info_rec *info = ds->info;
    int                         i, j;
    int                         ret;
    CARD32                      num_drawable_mods;
    CARD64                     *drawable_mods;
    CARD64                     *intersect_mods = NULL;
    CARD64                     *screen_mods = NULL;
    CARD32                      format;
    dri3_dmabuf_format_ptr      screen_format = NULL;

    ret = cache_formats_and_modifiers(screen);
    if (ret != Success)
        return ret;

    format = drm_format_for_depth(depth, bpp);
    if (format == 0)
        return BadValue;

    /* Find screen-global modifiers from cache
     */
    for (i = 0; i < ds->num_formats; i++) {
        if (ds->formats[i].format == format) {
            screen_format = &ds->formats[i];
            break;
        }
    }
    if (screen_format == NULL)
        return BadMatch;

    if (screen_format->num_modifiers == 0) {
        *num_screen_modifiers = 0;
        *num_intersect_modifiers = 0;
        return Success;
    }

    if (!info->get_drawable_modifiers ||
        !info->get_drawable_modifiers(drawable, format,
                                      &num_drawable_mods,
                                      &drawable_mods)) {
        num_drawable_mods = 0;
        drawable_mods = NULL;
    }

    /* We're allocating slightly more memory than necessary but it reduces
     * the complexity of finding the intersection set.
     */
    screen_mods = malloc(screen_format->num_modifiers * sizeof(CARD64));
    if (!screen_mods)
        return BadAlloc;
    if (num_drawable_mods > 0) {
        intersect_mods = malloc(screen_format->num_modifiers * sizeof(CARD64));
        if (!intersect_mods) {
            free(screen_mods);
            return BadAlloc;
        }
    }

    *num_screen_modifiers = 0;
    *num_intersect_modifiers = 0;
    for (i = 0; i < screen_format->num_modifiers; i++) {
        CARD64 modifier = screen_format->modifiers[i];
        Bool intersect = FALSE;

        for (j = 0; j < num_drawable_mods; j++) {
            if (drawable_mods[j] == modifier) {
                intersect = TRUE;
                break;
            }
        }

        if (intersect) {
            intersect_mods[*num_intersect_modifiers] = modifier;
            *num_intersect_modifiers += 1;
        } else {
            screen_mods[*num_screen_modifiers] = modifier;
            *num_screen_modifiers += 1;
        }
    }

    assert(*num_intersect_modifiers + *num_screen_modifiers == screen_format->num_modifiers);

    *intersect_modifiers = intersect_mods;
    *screen_modifiers = screen_mods;
    free(drawable_mods);

    return Success;
}