summaryrefslogtreecommitdiff
path: root/present/present_request.c
blob: ff1d71cc48da751b6e4e20ae9dc48242ada4d63d (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * 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.
 */
#include <dix-config.h>

#include "dix/dix_priv.h"

#include "present_priv.h"
#include "randrstr_priv.h"
#include <protocol-versions.h>

static int
proc_present_query_version(ClientPtr client)
{
    REQUEST(xPresentQueryVersionReq);
    xPresentQueryVersionReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .majorVersion = SERVER_PRESENT_MAJOR_VERSION,
        .minorVersion = SERVER_PRESENT_MINOR_VERSION
    };

    REQUEST_SIZE_MATCH(xPresentQueryVersionReq);
    /* From presentproto:
     *
     * The client sends the highest supported version to the server
     * and the server sends the highest version it supports, but no
     * higher than the requested version.
     */

    if (rep.majorVersion > stuff->majorVersion ||
        rep.minorVersion > stuff->minorVersion) {
        rep.majorVersion = stuff->majorVersion;
        rep.minorVersion = stuff->minorVersion;
    }

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swapl(&rep.majorVersion);
        swapl(&rep.minorVersion);
    }
    WriteToClient(client, sizeof(rep), &rep);
    return Success;
}

#define VERIFY_FENCE_OR_NONE(fence_ptr, fence_id, client, access) do {  \
        if ((fence_id) == None)                                         \
            (fence_ptr) = NULL;                                         \
        else {                                                          \
            int __rc__ = SyncVerifyFence(&fence_ptr, fence_id, client, access); \
            if (__rc__ != Success)                                      \
                return __rc__;                                          \
        }                                                               \
    } while (0)

#define VERIFY_CRTC_OR_NONE(crtc_ptr, crtc_id, client, access) do {     \
        if ((crtc_id) == None)                                          \
            (crtc_ptr) = NULL;                                          \
        else {                                                          \
            VERIFY_RR_CRTC(crtc_id, crtc_ptr, access);                  \
        }                                                               \
    } while (0)

static int
proc_present_pixmap_common(ClientPtr client,
                           Window req_window,
                           Pixmap req_pixmap,
                           CARD32 req_serial,
                           CARD32 req_valid,
                           CARD32 req_update,
                           INT16 req_x_off,
                           INT16 req_y_off,
                           CARD32 req_target_crtc,
                           XSyncFence req_wait_fence,
                           XSyncFence req_idle_fence,
#ifdef DRI3
                           struct dri3_syncobj *acquire_syncobj,
                           struct dri3_syncobj *release_syncobj,
                           CARD64 req_acquire_point,
                           CARD64 req_release_point,
#endif /* DRI3 */
                           CARD32 req_options,
                           CARD64 req_target_msc,
                           CARD64 req_divisor,
                           CARD64 req_remainder,
                           size_t base_req_size,
                           xPresentNotify *req_notifies)
{
    WindowPtr window;
    PixmapPtr pixmap;
    RegionPtr valid = NULL;
    RegionPtr update = NULL;
    RRCrtcPtr target_crtc;
    SyncFence *wait_fence;
    SyncFence *idle_fence;
    int nnotifies;
    present_notify_ptr notifies = NULL;
    int ret;

    ret = dixLookupWindow(&window, req_window, client, DixWriteAccess);
    if (ret != Success)
        return ret;
    ret = dixLookupResourceByType((void **) &pixmap, req_pixmap, RT_PIXMAP, client, DixReadAccess);
    if (ret != Success)
        return ret;

    if (window->drawable.depth != pixmap->drawable.depth)
        return BadMatch;

    VERIFY_REGION_OR_NONE(valid, req_valid, client, DixReadAccess);
    VERIFY_REGION_OR_NONE(update, req_update, client, DixReadAccess);

    VERIFY_CRTC_OR_NONE(target_crtc, req_target_crtc, client, DixReadAccess);

    VERIFY_FENCE_OR_NONE(wait_fence, req_wait_fence, client, DixReadAccess);
    VERIFY_FENCE_OR_NONE(idle_fence, req_idle_fence, client, DixWriteAccess);

    if (req_options & ~(PresentAllOptions)) {
        client->errorValue = req_options;
        return BadValue;
    }

    /*
     * Check to see if remainder is sane
     */
    if (req_divisor == 0) {
        if (req_remainder != 0) {
            client->errorValue = (CARD32)req_remainder;
            return BadValue;
        }
    } else {
        if (req_remainder >= req_divisor) {
            client->errorValue = (CARD32)req_remainder;
            return BadValue;
        }
    }

    nnotifies = (client->req_len << 2) - base_req_size;
    if (nnotifies % sizeof (xPresentNotify))
        return BadLength;

    nnotifies /= sizeof (xPresentNotify);
    if (nnotifies) {
        ret = present_create_notifies(client, nnotifies, req_notifies, &notifies);
        if (ret != Success)
            return ret;
    }

    ret = present_pixmap(window, pixmap, req_serial,
                         valid, update, req_x_off, req_y_off, target_crtc,
                         wait_fence, idle_fence,
#ifdef DRI3
                         acquire_syncobj, release_syncobj,
                         req_acquire_point, req_release_point,
#endif /* DRI3 */
                         req_options, req_target_msc, req_divisor, req_remainder,
                         notifies, nnotifies);

    if (ret != Success)
        present_destroy_notifies(notifies, nnotifies);
    return ret;
}

static int
proc_present_pixmap(ClientPtr client)
{
    REQUEST(xPresentPixmapReq);
    REQUEST_AT_LEAST_SIZE(xPresentPixmapReq);
    return proc_present_pixmap_common(client, stuff->window, stuff->pixmap, stuff->serial,
                                      stuff->valid, stuff->update, stuff->x_off, stuff->y_off,
                                      stuff->target_crtc,
                                      stuff->wait_fence, stuff->idle_fence,
#ifdef DRI3
                                      None, None, 0, 0,
#endif /* DRI3 */
                                      stuff->options, stuff->target_msc,
                                      stuff->divisor, stuff->remainder,
                                      sizeof (xPresentPixmapReq),
                                      (xPresentNotify *)(stuff + 1));
}

static int
proc_present_notify_msc(ClientPtr client)
{
    REQUEST(xPresentNotifyMSCReq);
    WindowPtr   window;
    int         rc;

    REQUEST_SIZE_MATCH(xPresentNotifyMSCReq);
    rc = dixLookupWindow(&window, stuff->window, client, DixReadAccess);
    if (rc != Success)
        return rc;

    /*
     * Check to see if remainder is sane
     */
    if (stuff->divisor == 0) {
        if (stuff->remainder != 0) {
            client->errorValue = (CARD32) stuff->remainder;
            return BadValue;
        }
    } else {
        if (stuff->remainder >= stuff->divisor) {
            client->errorValue = (CARD32) stuff->remainder;
            return BadValue;
        }
    }

    return present_notify_msc(window, stuff->serial,
                              stuff->target_msc, stuff->divisor, stuff->remainder);
}

static int
proc_present_select_input (ClientPtr client)
{
    REQUEST(xPresentSelectInputReq);
    WindowPtr window;
    int rc;

    REQUEST_SIZE_MATCH(xPresentSelectInputReq);

    rc = dixLookupWindow(&window, stuff->window, client, DixGetAttrAccess);
    if (rc != Success)
        return rc;

    if (stuff->eventMask & ~PresentAllEvents) {
        client->errorValue = stuff->eventMask;
        return BadValue;
    }
    return present_select_input(client, stuff->eid, window, stuff->eventMask);
}

static int
proc_present_query_capabilities (ClientPtr client)
{
    REQUEST(xPresentQueryCapabilitiesReq);
    xPresentQueryCapabilitiesReply rep = {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
    };
    WindowPtr   window;
    RRCrtcPtr   crtc = NULL;
    int         r;

    REQUEST_SIZE_MATCH(xPresentQueryCapabilitiesReq);
    r = dixLookupWindow(&window, stuff->target, client, DixGetAttrAccess);
    switch (r) {
    case Success:
        crtc = present_get_crtc(window);
        break;
    case BadWindow:
        VERIFY_RR_CRTC(stuff->target, crtc, DixGetAttrAccess);
        break;
    default:
        return r;
    }

    rep.capabilities = present_query_capabilities(crtc);

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swapl(&rep.capabilities);
    }
    WriteToClient(client, sizeof(rep), &rep);
    return Success;
}

#ifdef DRI3
static int
proc_present_pixmap_synced (ClientPtr client)
{
    REQUEST(xPresentPixmapSyncedReq);
    struct dri3_syncobj *acquire_syncobj;
    struct dri3_syncobj *release_syncobj;

    REQUEST_AT_LEAST_SIZE(xPresentPixmapSyncedReq);
    VERIFY_DRI3_SYNCOBJ(stuff->acquire_syncobj, acquire_syncobj, DixWriteAccess);
    VERIFY_DRI3_SYNCOBJ(stuff->release_syncobj, release_syncobj, DixWriteAccess);

    if (stuff->acquire_point == 0 || stuff->release_point == 0 ||
        (stuff->acquire_syncobj == stuff->release_syncobj &&
         stuff->acquire_point >= stuff->release_point))
        return BadValue;

    return proc_present_pixmap_common(client, stuff->window, stuff->pixmap, stuff->serial,
                                      stuff->valid, stuff->update, stuff->x_off, stuff->y_off,
                                      stuff->target_crtc,
                                      None, None,
                                      acquire_syncobj, release_syncobj,
                                      stuff->acquire_point, stuff->release_point,
                                      stuff->options, stuff->target_msc,
                                      stuff->divisor, stuff->remainder,
                                      sizeof (xPresentPixmapSyncedReq),
                                      (xPresentNotify *)(stuff + 1));
}
#endif /* DRI3 */

static int (*proc_present_vector[PresentNumberRequests]) (ClientPtr) = {
    proc_present_query_version,            /* 0 */
    proc_present_pixmap,                   /* 1 */
    proc_present_notify_msc,               /* 2 */
    proc_present_select_input,             /* 3 */
    proc_present_query_capabilities,       /* 4 */
#ifdef DRI3
    proc_present_pixmap_synced,            /* 5 */
#endif /* DRI3 */
};

int
proc_present_dispatch(ClientPtr client)
{
    REQUEST(xReq);
    if (stuff->data >= PresentNumberRequests || !proc_present_vector[stuff->data])
        return BadRequest;
    return (*proc_present_vector[stuff->data]) (client);
}

static int _X_COLD
sproc_present_query_version(ClientPtr client)
{
    REQUEST(xPresentQueryVersionReq);
    REQUEST_SIZE_MATCH(xPresentQueryVersionReq);

    swaps(&stuff->length);
    swapl(&stuff->majorVersion);
    swapl(&stuff->minorVersion);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}

static int _X_COLD
sproc_present_pixmap(ClientPtr client)
{
    REQUEST(xPresentPixmapReq);
    REQUEST_AT_LEAST_SIZE(xPresentPixmapReq);

    swaps(&stuff->length);
    swapl(&stuff->window);
    swapl(&stuff->pixmap);
    swapl(&stuff->valid);
    swapl(&stuff->update);
    swaps(&stuff->x_off);
    swaps(&stuff->y_off);
    swapll(&stuff->target_msc);
    swapll(&stuff->divisor);
    swapll(&stuff->remainder);
    swapl(&stuff->idle_fence);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}

static int _X_COLD
sproc_present_notify_msc(ClientPtr client)
{
    REQUEST(xPresentNotifyMSCReq);
    REQUEST_SIZE_MATCH(xPresentNotifyMSCReq);

    swaps(&stuff->length);
    swapl(&stuff->window);
    swapll(&stuff->target_msc);
    swapll(&stuff->divisor);
    swapll(&stuff->remainder);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}

static int _X_COLD
sproc_present_select_input (ClientPtr client)
{
    REQUEST(xPresentSelectInputReq);
    REQUEST_SIZE_MATCH(xPresentSelectInputReq);

    swaps(&stuff->length);
    swapl(&stuff->window);
    swapl(&stuff->eventMask);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}

static int _X_COLD
sproc_present_query_capabilities (ClientPtr client)
{
    REQUEST(xPresentQueryCapabilitiesReq);
    REQUEST_SIZE_MATCH(xPresentQueryCapabilitiesReq);
    swaps(&stuff->length);
    swapl(&stuff->target);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}


#ifdef DRI3
static int _X_COLD
sproc_present_pixmap_synced(ClientPtr client)
{
    REQUEST(xPresentPixmapSyncedReq);
    REQUEST_AT_LEAST_SIZE(xPresentPixmapSyncedReq);

    swaps(&stuff->length);

    swapl(&stuff->window);

    swapl(&stuff->pixmap);
    swapl(&stuff->serial);

    swapl(&stuff->valid);
    swapl(&stuff->update);

    swaps(&stuff->x_off);
    swaps(&stuff->y_off);
    swapl(&stuff->target_crtc);

    swapl(&stuff->acquire_syncobj);
    swapl(&stuff->release_syncobj);
    swapll(&stuff->acquire_point);
    swapll(&stuff->release_point);

    swapl(&stuff->options);

    swapll(&stuff->target_msc);
    swapll(&stuff->divisor);
    swapll(&stuff->remainder);
    return (*proc_present_vector[stuff->presentReqType]) (client);
}
#endif /* DRI3 */

static int (*sproc_present_vector[PresentNumberRequests]) (ClientPtr) = {
    sproc_present_query_version,           /* 0 */
    sproc_present_pixmap,                  /* 1 */
    sproc_present_notify_msc,              /* 2 */
    sproc_present_select_input,            /* 3 */
    sproc_present_query_capabilities,      /* 4 */
#ifdef DRI3
    sproc_present_pixmap_synced,           /* 5 */
#endif /* DRI3 */
};

int _X_COLD
sproc_present_dispatch(ClientPtr client)
{
    REQUEST(xReq);
    if (stuff->data >= PresentNumberRequests || !sproc_present_vector[stuff->data])
        return BadRequest;
    return (*sproc_present_vector[stuff->data]) (client);
}