rrprovider.c 15 KB
Newer Older
1
2
/*
 * Copyright © 2012 Red Hat Inc.
3
 * Copyright 2019 DisplayLink (UK) Ltd.
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
 *
 * 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.
 *
 * Authors: Dave Airlie
 */

#include "randrstr.h"
#include "swaprep.h"

29
30
#include <X11/Xatom.h>

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
RESTYPE RRProviderType;

/*
 * Initialize provider type error value
 */
void
RRProviderInitErrorValue(void)
{
    SetResourceTypeErrorValue(RRProviderType, RRErrorBase + BadRRProvider);
}

#define ADD_PROVIDER(_pScreen) do {                                 \
    pScrPriv = rrGetScrPriv((_pScreen));                            \
    if (pScrPriv->provider) {                                   \
        providers[count_providers] = pScrPriv->provider->id;    \
        if (client->swapped)                                    \
            swapl(&providers[count_providers]);                 \
        count_providers++;                                      \
    }                                                           \
    } while(0)

int
ProcRRGetProviders (ClientPtr client)
{
    REQUEST(xRRGetProvidersReq);
    xRRGetProvidersReply rep;
    WindowPtr pWin;
    ScreenPtr pScreen;
    rrScrPrivPtr pScrPriv;
    int rc;
    CARD8 *extra;
    unsigned int extraLen;
    RRProvider *providers;
    int total_providers = 0, count_providers = 0;
65
    ScreenPtr iter;
66
67
68
69
70
71
72
73
74
75
76
77

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

    pScreen = pWin->drawable.pScreen;

    pScrPriv = rrGetScrPriv(pScreen);

    if (pScrPriv->provider)
        total_providers++;
78
    xorg_list_for_each_entry(iter, &pScreen->secondary_list, secondary_head) {
79
80
81
        pScrPriv = rrGetScrPriv(iter);
        total_providers += pScrPriv->provider ? 1 : 0;
    }
82
83
84
85
86

    pScrPriv = rrGetScrPriv(pScreen);

    if (!pScrPriv)
    {
87
88
89
90
91
92
93
        rep = (xRRGetProvidersReply) {
            .type = X_Reply,
            .sequenceNumber = client->sequence,
            .length = 0,
            .timestamp = currentTime.milliseconds,
            .nProviders = 0
        };
94
95
96
        extra = NULL;
        extraLen = 0;
    } else {
97
98
99
100
101
102
103
        rep = (xRRGetProvidersReply) {
            .type = X_Reply,
            .sequenceNumber = client->sequence,
            .timestamp = pScrPriv->lastSetTime.milliseconds,
            .nProviders = total_providers,
            .length = total_providers
        };
104
105
106
107
108
109
110
111
112
113
        extraLen = rep.length << 2;
        if (extraLen) {
            extra = malloc(extraLen);
            if (!extra)
                return BadAlloc;
        } else
            extra = NULL;

        providers = (RRProvider *)extra;
        ADD_PROVIDER(pScreen);
114
        xorg_list_for_each_entry(iter, &pScreen->secondary_list, secondary_head) {
115
116
            ADD_PROVIDER(iter);
        }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    }

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swapl(&rep.timestamp);
        swaps(&rep.nProviders);
    }
    WriteToClient(client, sizeof(xRRGetProvidersReply), (char *)&rep);
    if (extraLen)
    {
        WriteToClient (client, extraLen, (char *) extra);
        free(extra);
    }
    return Success;
}

int
ProcRRGetProviderInfo (ClientPtr client)
{
    REQUEST(xRRGetProviderInfoReq);
    xRRGetProviderInfoReply rep;
Dave Airlie's avatar
Dave Airlie committed
139
    rrScrPrivPtr pScrPriv, pScrProvPriv;
140
141
142
143
144
145
146
147
148
149
150
    RRProviderPtr provider;
    ScreenPtr pScreen;
    CARD8 *extra;
    unsigned int extraLen = 0;
    RRCrtc *crtcs;
    RROutput *outputs;
    int i;
    char *name;
    ScreenPtr provscreen;
    RRProvider *providers;
    uint32_t *prov_cap;
Peter Hutterer's avatar
Peter Hutterer committed
151

152
153
154
155
156
157
    REQUEST_SIZE_MATCH(xRRGetProviderInfoReq);
    VERIFY_RR_PROVIDER(stuff->provider, provider, DixReadAccess);

    pScreen = provider->pScreen;
    pScrPriv = rrGetScrPriv(pScreen);

158
159
160
161
162
163
164
165
166
167
168
169
    rep = (xRRGetProviderInfoReply) {
        .type = X_Reply,
        .status = RRSetConfigSuccess,
        .sequenceNumber = client->sequence,
        .length = 0,
        .capabilities = provider->capabilities,
        .nameLength = provider->nameLength,
        .timestamp = pScrPriv->lastSetTime.milliseconds,
        .nCrtcs = pScrPriv->numCrtcs,
        .nOutputs = pScrPriv->numOutputs,
        .nAssociatedProviders = 0
    };
170
171

    /* count associated providers */
172
173
    if (provider->offload_sink)
        rep.nAssociatedProviders++;
174
175
    if (provider->output_source &&
            provider->output_source != provider->offload_sink)
176
        rep.nAssociatedProviders++;
177
178
    xorg_list_for_each_entry(provscreen, &pScreen->secondary_list, secondary_head) {
        if (provscreen->is_output_secondary || provscreen->is_offload_secondary)
179
180
            rep.nAssociatedProviders++;
    }
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
    rep.length = (pScrPriv->numCrtcs + pScrPriv->numOutputs +
                  (rep.nAssociatedProviders * 2) + bytes_to_int32(rep.nameLength));

    extraLen = rep.length << 2;
    if (extraLen) {
        extra = malloc(extraLen);
        if (!extra)
            return BadAlloc;
    }
    else
        extra = NULL;

    crtcs = (RRCrtc *)extra;
    outputs = (RROutput *)(crtcs + rep.nCrtcs);
    providers = (RRProvider *)(outputs + rep.nOutputs);
    prov_cap = (unsigned int *)(providers + rep.nAssociatedProviders);
    name = (char *)(prov_cap + rep.nAssociatedProviders);

    for (i = 0; i < pScrPriv->numCrtcs; i++) {
        crtcs[i] = pScrPriv->crtcs[i]->id;
        if (client->swapped)
            swapl(&crtcs[i]);
    }

    for (i = 0; i < pScrPriv->numOutputs; i++) {
        outputs[i] = pScrPriv->outputs[i]->id;
        if (client->swapped)
            swapl(&outputs[i]);
    }

Dave Airlie's avatar
Dave Airlie committed
212
    i = 0;
213
214
215
216
217
218
219
220
221
    if (provider->offload_sink) {
        providers[i] = provider->offload_sink->id;
        if (client->swapped)
            swapl(&providers[i]);
        prov_cap[i] = RR_Capability_SinkOffload;
        if (client->swapped)
            swapl(&prov_cap[i]);
        i++;
    }
Dave Airlie's avatar
Dave Airlie committed
222
223
224
225
226
227
228
229
    if (provider->output_source) {
        providers[i] = provider->output_source->id;
        if (client->swapped)
            swapl(&providers[i]);
        prov_cap[i] = RR_Capability_SourceOutput;
            swapl(&prov_cap[i]);
        i++;
    }
230
231
    xorg_list_for_each_entry(provscreen, &pScreen->secondary_list, secondary_head) {
        if (!provscreen->is_output_secondary && !provscreen->is_offload_secondary)
232
            continue;
Dave Airlie's avatar
Dave Airlie committed
233
234
235
236
        pScrProvPriv = rrGetScrPriv(provscreen);
        providers[i] = pScrProvPriv->provider->id;
        if (client->swapped)
            swapl(&providers[i]);
237
        prov_cap[i] = 0;
238
        if (provscreen->is_output_secondary)
239
            prov_cap[i] |= RR_Capability_SinkOutput;
240
        if (provscreen->is_offload_secondary)
241
            prov_cap[i] |= RR_Capability_SourceOffload;
Dave Airlie's avatar
Dave Airlie committed
242
243
244
245
246
        if (client->swapped)
            swapl(&prov_cap[i]);
        i++;
    }

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    memcpy(name, provider->name, rep.nameLength);
    if (client->swapped) {
              swaps(&rep.sequenceNumber);
        swapl(&rep.length);
        swapl(&rep.capabilities);
        swaps(&rep.nCrtcs);
        swaps(&rep.nOutputs);
        swaps(&rep.nameLength);
    }
    WriteToClient(client, sizeof(xRRGetProviderInfoReply), (char *)&rep);
    if (extraLen)
    {
        WriteToClient (client, extraLen, (char *) extra);
        free(extra);
    }
    return Success;
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
static void
RRInitPrimeSyncProps(ScreenPtr pScreen)
{
    /*
     * TODO: When adding support for different sources for different outputs,
     * make sure this sets up the output properties only on outputs associated
     * with the correct source provider.
     */

    rrScrPrivPtr pScrPriv = rrGetScrPriv(pScreen);

    const char *syncStr = PRIME_SYNC_PROP;
    Atom syncProp = MakeAtom(syncStr, strlen(syncStr), TRUE);

    int defaultVal = TRUE;
280
    INT32 validVals[2] = {FALSE, TRUE};
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

    int i;
    for (i = 0; i < pScrPriv->numOutputs; i++) {
        if (!RRQueryOutputProperty(pScrPriv->outputs[i], syncProp)) {
            RRConfigureOutputProperty(pScrPriv->outputs[i], syncProp,
                                      TRUE, FALSE, FALSE,
                                      2, &validVals[0]);
            RRChangeOutputProperty(pScrPriv->outputs[i], syncProp, XA_INTEGER,
                                   8, PropModeReplace, 1, &defaultVal,
                                   FALSE, FALSE);
        }
    }
}

static void
RRFiniPrimeSyncProps(ScreenPtr pScreen)
{
    /*
     * TODO: When adding support for different sources for different outputs,
     * make sure this tears down the output properties only on outputs
     * associated with the correct source provider.
     */

    rrScrPrivPtr pScrPriv = rrGetScrPriv(pScreen);
    int i;

    const char *syncStr = PRIME_SYNC_PROP;
    Atom syncProp = MakeAtom(syncStr, strlen(syncStr), FALSE);
    if (syncProp == None)
        return;

    for (i = 0; i < pScrPriv->numOutputs; i++) {
        RRDeleteOutputProperty(pScrPriv->outputs[i], syncProp);
    }
}

Dave Airlie's avatar
Dave Airlie committed
317
318
319
320
321
322
323
324
int
ProcRRSetProviderOutputSource(ClientPtr client)
{
    REQUEST(xRRSetProviderOutputSourceReq);
    rrScrPrivPtr pScrPriv;
    RRProviderPtr provider, source_provider = NULL;
    ScreenPtr pScreen;

325
    REQUEST_SIZE_MATCH(xRRSetProviderOutputSourceReq);
Dave Airlie's avatar
Dave Airlie committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

    VERIFY_RR_PROVIDER(stuff->provider, provider, DixReadAccess);

    if (!(provider->capabilities & RR_Capability_SinkOutput))
        return BadValue;

    if (stuff->source_provider) {
        VERIFY_RR_PROVIDER(stuff->source_provider, source_provider, DixReadAccess);

        if (!(source_provider->capabilities & RR_Capability_SourceOutput))
            return BadValue;
    }

    pScreen = provider->pScreen;
    pScrPriv = rrGetScrPriv(pScreen);

342
343
344
    if (!pScreen->isGPU)
        return BadValue;

Dave Airlie's avatar
Dave Airlie committed
345
346
    pScrPriv->rrProviderSetOutputSource(pScreen, provider, source_provider);

347
348
    RRInitPrimeSyncProps(pScreen);

349
350
351
    provider->changed = TRUE;
    RRSetChanged(pScreen);

Dave Airlie's avatar
Dave Airlie committed
352
353
354
355
356
    RRTellChanged (pScreen);

    return Success;
}

357
358
359
360
361
362
363
364
int
ProcRRSetProviderOffloadSink(ClientPtr client)
{
    REQUEST(xRRSetProviderOffloadSinkReq);
    rrScrPrivPtr pScrPriv;
    RRProviderPtr provider, sink_provider = NULL;
    ScreenPtr pScreen;

365
    REQUEST_SIZE_MATCH(xRRSetProviderOffloadSinkReq);
366
367
368
369

    VERIFY_RR_PROVIDER(stuff->provider, provider, DixReadAccess);
    if (!(provider->capabilities & RR_Capability_SourceOffload))
        return BadValue;
370
371
    if (!provider->pScreen->isGPU)
        return BadValue;
372
373
374
375
376
377
378
379
380
381
382

    if (stuff->sink_provider) {
        VERIFY_RR_PROVIDER(stuff->sink_provider, sink_provider, DixReadAccess);
        if (!(sink_provider->capabilities & RR_Capability_SinkOffload))
            return BadValue;
    }
    pScreen = provider->pScreen;
    pScrPriv = rrGetScrPriv(pScreen);

    pScrPriv->rrProviderSetOffloadSink(pScreen, provider, sink_provider);

383
384
385
    provider->changed = TRUE;
    RRSetChanged(pScreen);

386
387
388
389
390
    RRTellChanged (pScreen);

    return Success;
}

391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
RRProviderPtr
RRProviderCreate(ScreenPtr pScreen, const char *name,
                 int nameLength)
{
    RRProviderPtr provider;
    rrScrPrivPtr pScrPriv;

    pScrPriv = rrGetScrPriv(pScreen);

    provider = calloc(1, sizeof(RRProviderRec) + nameLength + 1);
    if (!provider)
        return NULL;

    provider->id = FakeClientID(0);
    provider->pScreen = pScreen;
    provider->name = (char *) (provider + 1);
    provider->nameLength = nameLength;
    memcpy(provider->name, name, nameLength);
    provider->name[nameLength] = '\0';
410
    provider->changed = FALSE;
411

412
    if (!AddResource (provider->id, RRProviderType, (void *) provider))
413
414
415
416
417
418
419
420
421
422
423
        return NULL;
    pScrPriv->provider = provider;
    return provider;
}

/*
 * Destroy a provider at shutdown
 */
void
RRProviderDestroy (RRProviderPtr provider)
{
424
    RRFiniPrimeSyncProps(provider->pScreen);
425
426
427
428
429
430
431
432
433
434
    FreeResource (provider->id, 0);
}

void
RRProviderSetCapabilities(RRProviderPtr provider, uint32_t capabilities)
{
    provider->capabilities = capabilities;
}

static int
435
RRProviderDestroyResource (void *value, XID pid)
436
437
438
439
440
441
442
443
{
    RRProviderPtr provider = (RRProviderPtr)value;
    ScreenPtr pScreen = provider->pScreen;

    if (pScreen)
    {
        rrScrPriv(pScreen);

Dave Airlie's avatar
Dave Airlie committed
444
445
        if (pScrPriv->rrProviderDestroy)
            (*pScrPriv->rrProviderDestroy)(pScreen, provider);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
        pScrPriv->provider = NULL;
    }
    free(provider);
    return 1;
}

Bool
RRProviderInit(void)
{
    RRProviderType = CreateNewResourceType(RRProviderDestroyResource, "Provider");
    if (!RRProviderType)
        return FALSE;

    return TRUE;
}

extern _X_EXPORT Bool
RRProviderLookup(XID id, RRProviderPtr *provider_p)
{
    int rc = dixLookupResourceByType((void **)provider_p, id,
                                   RRProviderType, NullClient, DixReadAccess);
    if (rc == Success)
        return TRUE;
    return FALSE;
}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

void
RRDeliverProviderEvent(ClientPtr client, WindowPtr pWin, RRProviderPtr provider)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;

    rrScrPriv(pScreen);

    xRRProviderChangeNotifyEvent pe = {
        .type = RRNotify + RREventBase,
        .subCode = RRNotify_ProviderChange,
        .timestamp = pScrPriv->lastSetTime.milliseconds,
        .window = pWin->drawable.id,
        .provider = provider->id
    };

    WriteEventsToClient(client, 1, (xEvent *) &pe);
}
489
490

void
491
RRProviderAutoConfigGpuScreen(ScreenPtr pScreen, ScreenPtr primaryScreen)
492
{
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    rrScrPrivPtr pScrPriv;
    rrScrPrivPtr primaryPriv;
    RRProviderPtr provider;
    RRProviderPtr primary_provider;

    /* Bail out if RandR wasn't initialized. */
    if (!dixPrivateKeyRegistered(rrPrivKey))
        return;

    pScrPriv = rrGetScrPriv(pScreen);
    primaryPriv = rrGetScrPriv(primaryScreen);

    provider = pScrPriv->provider;
    primary_provider = primaryPriv->provider;
507

508
    if (!provider || !primary_provider)
509
510
511
        return;

    if ((provider->capabilities & RR_Capability_SinkOutput) &&
512
513
        (primary_provider->capabilities & RR_Capability_SourceOutput)) {
        pScrPriv->rrProviderSetOutputSource(pScreen, provider, primary_provider);
514
        RRInitPrimeSyncProps(pScreen);
515

516
517
        primaryPriv->configChanged = TRUE;
        RRSetChanged(primaryScreen);
518
519
520
    }

    if ((provider->capabilities & RR_Capability_SourceOffload) &&
521
522
        (primary_provider->capabilities & RR_Capability_SinkOffload))
        pScrPriv->rrProviderSetOffloadSink(pScreen, provider, primary_provider);
523
}