Newer
Older
/*
* Copyright © 2007 Red Hat, Inc.
* Copyright © 2019 NVIDIA CORPORATION
*
* 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 (including the next
* paragraph) 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.
*
* Authors:
* Dave Airlie <airlied@redhat.com>
* Aaron Plattner <aplattner@nvidia.com>
#ifdef HAVE_DIX_CONFIG_H
#include "dix-config.h"
#endif
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "dumb_bo.h"
#include "inputstr.h"
#include "xf86str.h"
#include "X11/Xatom.h"
#include "micmap.h"
#include "xf86cmap.h"
#include "xf86DDC.h"
Louis-Francis Ratté-Boulianne
committed
#include <drm_fourcc.h>
#include <drm_mode.h>
#include <xf86drm.h>
#include "xf86Crtc.h"
#include "drmmode_display.h"
#include "present.h"
#include <X11/extensions/dpmsconst.h>
static Bool drmmode_xf86crtc_resize(ScrnInfoPtr scrn, int width, int height);
static PixmapPtr drmmode_create_pixmap_header(ScreenPtr pScreen, int width, int height,
int depth, int bitsPerPixel, int devKind,
void *pPixData);
static const struct drm_color_ctm ctm_identity = { {
1UL << 32, 0, 0,
0, 1UL << 32, 0,
0, 0, 1UL << 32
} };
static Bool ctm_is_identity(const struct drm_color_ctm *ctm)
{
const size_t matrix_len = sizeof(ctm->matrix) / sizeof(ctm->matrix[0]);
const uint64_t one = 1ULL << 32;
const uint64_t neg_zero = 1ULL << 63;
int i;
for (i = 0; i < matrix_len; i++) {
const Bool diagonal = i / 3 == i % 3;
const uint64_t val = ctm->matrix[i];
if ((diagonal && val != one) ||
(!diagonal && val != 0 && val != neg_zero)) {
return FALSE;
}
}
return TRUE;
}
static inline uint32_t *
formats_ptr(struct drm_format_modifier_blob *blob)
{
return (uint32_t *)(((char *)blob) + blob->formats_offset);
}
static inline struct drm_format_modifier *
modifiers_ptr(struct drm_format_modifier_blob *blob)
{
return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
}
Louis-Francis Ratté-Boulianne
committed
static uint32_t
get_opaque_format(uint32_t format)
{
switch (format) {
case DRM_FORMAT_ARGB8888:
return DRM_FORMAT_XRGB8888;
case DRM_FORMAT_ARGB2101010:
return DRM_FORMAT_XRGB2101010;
default:
return format;
}
}
Louis-Francis Ratté-Boulianne
committed
Bool
drmmode_is_format_supported(ScrnInfoPtr scrn, uint32_t format, uint64_t modifier)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
int c, i, j;
Louis-Francis Ratté-Boulianne
committed
/* BO are imported as opaque surface, so let's pretend there is no alpha */
format = get_opaque_format(format);
Louis-Francis Ratté-Boulianne
committed
for (c = 0; c < xf86_config->num_crtc; c++) {
xf86CrtcPtr crtc = xf86_config->crtc[c];
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
Bool found = FALSE;
if (!crtc->enabled)
continue;
if (drmmode_crtc->num_formats == 0)
continue;
Louis-Francis Ratté-Boulianne
committed
for (i = 0; i < drmmode_crtc->num_formats; i++) {
drmmode_format_ptr iter = &drmmode_crtc->formats[i];
if (iter->format != format)
continue;
if (modifier == DRM_FORMAT_MOD_INVALID ||
iter->num_modifiers == 0) {
Louis-Francis Ratté-Boulianne
committed
found = TRUE;
break;
}
for (j = 0; j < iter->num_modifiers; j++) {
if (iter->modifiers[j] == modifier) {
found = TRUE;
break;
}
}
break;
}
if (!found)
return FALSE;
}
return TRUE;
}
Louis-Francis Ratté-Boulianne
committed
#ifdef GBM_BO_WITH_MODIFIERS
static uint32_t
get_modifiers_set(ScrnInfoPtr scrn, uint32_t format, uint64_t **modifiers,
Bool enabled_crtc_only, Bool exclude_multiplane)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
modesettingPtr ms = modesettingPTR(scrn);
drmmode_ptr drmmode = &ms->drmmode;
int c, i, j, k, count_modifiers = 0;
uint64_t *tmp, *ret = NULL;
Louis-Francis Ratté-Boulianne
committed
/* BOs are imported as opaque surfaces, so pretend the same thing here */
format = get_opaque_format(format);
Louis-Francis Ratté-Boulianne
committed
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
*modifiers = NULL;
for (c = 0; c < xf86_config->num_crtc; c++) {
xf86CrtcPtr crtc = xf86_config->crtc[c];
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
if (enabled_crtc_only && !crtc->enabled)
continue;
for (i = 0; i < drmmode_crtc->num_formats; i++) {
drmmode_format_ptr iter = &drmmode_crtc->formats[i];
if (iter->format != format)
continue;
for (j = 0; j < iter->num_modifiers; j++) {
Bool found = FALSE;
/* Don't choose multi-plane formats for our screen pixmap.
* These will get used with frontbuffer rendering, which will
* lead to worse-than-tearing with multi-plane formats, as the
* primary and auxiliary planes go out of sync. */
if (exclude_multiplane &&
gbm_device_get_format_modifier_plane_count(drmmode->gbm,
format,
iter->modifiers[j]) > 1) {
continue;
}
for (k = 0; k < count_modifiers; k++) {
if (iter->modifiers[j] == ret[k])
found = TRUE;
}
if (!found) {
count_modifiers++;
tmp = realloc(ret, count_modifiers * sizeof(uint64_t));
if (!tmp) {
free(ret);
return 0;
}
ret = tmp;
ret[count_modifiers - 1] = iter->modifiers[j];
}
}
}
}
*modifiers = ret;
return count_modifiers;
}
static Bool
get_drawable_modifiers(DrawablePtr draw, uint32_t format,
uint32_t *num_modifiers, uint64_t **modifiers)
{
ScrnInfoPtr scrn = xf86ScreenToScrn(draw->pScreen);
modesettingPtr ms = modesettingPTR(scrn);
if (!present_can_window_flip((WindowPtr) draw) ||
!ms->drmmode.pageflip || ms->drmmode.dri2_flipping || !scrn->vtSema) {
*num_modifiers = 0;
*modifiers = NULL;
return TRUE;
}
*num_modifiers = get_modifiers_set(scrn, format, modifiers, TRUE, FALSE);
return TRUE;
}
Louis-Francis Ratté-Boulianne
committed
#endif
static Bool
drmmode_zaphod_string_matches(ScrnInfoPtr scrn, const char *s, char *output_name)
{
char **token = xstrtokenize(s, ", \t\n\r");
Bool ret = FALSE;
if (!token)
return FALSE;
for (int i = 0; token[i]; i++) {
if (strcmp(token[i], output_name) == 0)
ret = TRUE;
free(token[i]);
}
free(token);
return ret;
Louis-Francis Ratté-Boulianne
committed
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
static uint64_t
drmmode_prop_get_value(drmmode_prop_info_ptr info,
drmModeObjectPropertiesPtr props,
uint64_t def)
{
unsigned int i;
if (info->prop_id == 0)
return def;
for (i = 0; i < props->count_props; i++) {
unsigned int j;
if (props->props[i] != info->prop_id)
continue;
/* Simple (non-enum) types can return the value directly */
if (info->num_enum_values == 0)
return props->prop_values[i];
/* Map from raw value to enum value */
for (j = 0; j < info->num_enum_values; j++) {
if (!info->enum_values[j].valid)
continue;
if (info->enum_values[j].value != props->prop_values[i])
continue;
return j;
}
}
return def;
}
static uint32_t
drmmode_prop_info_update(drmmode_ptr drmmode,
drmmode_prop_info_ptr info,
unsigned int num_infos,
drmModeObjectProperties *props)
{
drmModePropertyRes *prop;
uint32_t valid_mask = 0;
unsigned i, j;
assert(num_infos <= 32 && "update return type");
for (i = 0; i < props->count_props; i++) {
Bool props_incomplete = FALSE;
unsigned int k;
for (j = 0; j < num_infos; j++) {
if (info[j].prop_id == props->props[i])
break;
if (!info[j].prop_id)
props_incomplete = TRUE;
}
/* We've already discovered this property. */
if (j != num_infos)
continue;
/* We haven't found this property ID, but as we've already
* found all known properties, we don't need to look any
* further. */
if (!props_incomplete)
break;
prop = drmModeGetProperty(drmmode->fd, props->props[i]);
if (!prop)
continue;
for (j = 0; j < num_infos; j++) {
if (!strcmp(prop->name, info[j].name))
break;
}
/* We don't know/care about this property. */
if (j == num_infos) {
drmModeFreeProperty(prop);
continue;
}
info[j].prop_id = props->props[i];
info[j].value = props->prop_values[i];
Louis-Francis Ratté-Boulianne
committed
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
valid_mask |= 1U << j;
if (info[j].num_enum_values == 0) {
drmModeFreeProperty(prop);
continue;
}
if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
xf86DrvMsg(drmmode->scrn->scrnIndex, X_WARNING,
"expected property %s to be an enum,"
" but it is not; ignoring\n", prop->name);
drmModeFreeProperty(prop);
continue;
}
for (k = 0; k < info[j].num_enum_values; k++) {
int l;
if (info[j].enum_values[k].valid)
continue;
for (l = 0; l < prop->count_enums; l++) {
if (!strcmp(prop->enums[l].name,
info[j].enum_values[k].name))
break;
}
if (l == prop->count_enums)
continue;
info[j].enum_values[k].valid = TRUE;
info[j].enum_values[k].value = prop->enums[l].value;
}
drmModeFreeProperty(prop);
}
return valid_mask;
}
static Bool
drmmode_prop_info_copy(drmmode_prop_info_ptr dst,
const drmmode_prop_info_rec *src,
unsigned int num_props,
Bool copy_prop_id)
{
unsigned int i;
memcpy(dst, src, num_props * sizeof(*dst));
for (i = 0; i < num_props; i++) {
unsigned int j;
if (copy_prop_id)
dst[i].prop_id = src[i].prop_id;
else
dst[i].prop_id = 0;
if (src[i].num_enum_values == 0)
continue;
dst[i].enum_values =
malloc(src[i].num_enum_values *
sizeof(*dst[i].enum_values));
if (!dst[i].enum_values)
goto err;
memcpy(dst[i].enum_values, src[i].enum_values,
src[i].num_enum_values * sizeof(*dst[i].enum_values));
for (j = 0; j < dst[i].num_enum_values; j++)
dst[i].enum_values[j].valid = FALSE;
}
return TRUE;
err:
while (i--)
free(dst[i].enum_values);
return FALSE;
}
static void
drmmode_prop_info_free(drmmode_prop_info_ptr info, int num_props)
{
int i;
for (i = 0; i < num_props; i++)
free(info[i].enum_values);
}
static void
drmmode_ConvertToKMode(ScrnInfoPtr scrn,
drmModeModeInfo * kmode, DisplayModePtr mode);
Louis-Francis Ratté-Boulianne
committed
static int
plane_add_prop(drmModeAtomicReq *req, drmmode_crtc_private_ptr drmmode_crtc,
enum drmmode_plane_property prop, uint64_t val)
{
drmmode_prop_info_ptr info = &drmmode_crtc->props_plane[prop];
int ret;
if (!info)
return -1;
ret = drmModeAtomicAddProperty(req, drmmode_crtc->plane_id,
info->prop_id, val);
return (ret <= 0) ? -1 : 0;
}
Louis-Francis Ratté-Boulianne
committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
static int
plane_add_props(drmModeAtomicReq *req, xf86CrtcPtr crtc,
uint32_t fb_id, int x, int y)
{
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
int ret = 0;
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_FB_ID,
fb_id);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_CRTC_ID,
fb_id ? drmmode_crtc->mode_crtc->crtc_id : 0);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_SRC_X, x << 16);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_SRC_Y, y << 16);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_SRC_W,
crtc->mode.HDisplay << 16);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_SRC_H,
crtc->mode.VDisplay << 16);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_CRTC_X, 0);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_CRTC_Y, 0);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_CRTC_W,
crtc->mode.HDisplay);
ret |= plane_add_prop(req, drmmode_crtc, DRMMODE_PLANE_CRTC_H,
crtc->mode.VDisplay);
return ret;
}
Louis-Francis Ratté-Boulianne
committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
static int
crtc_add_prop(drmModeAtomicReq *req, drmmode_crtc_private_ptr drmmode_crtc,
enum drmmode_crtc_property prop, uint64_t val)
{
drmmode_prop_info_ptr info = &drmmode_crtc->props[prop];
int ret;
if (!info)
return -1;
ret = drmModeAtomicAddProperty(req, drmmode_crtc->mode_crtc->crtc_id,
info->prop_id, val);
return (ret <= 0) ? -1 : 0;
}
static int
connector_add_prop(drmModeAtomicReq *req, drmmode_output_private_ptr drmmode_output,
enum drmmode_connector_property prop, uint64_t val)
{
drmmode_prop_info_ptr info = &drmmode_output->props_connector[prop];
int ret;
if (!info)
return -1;
ret = drmModeAtomicAddProperty(req, drmmode_output->output_id,
info->prop_id, val);
return (ret <= 0) ? -1 : 0;
}
static int
drmmode_CompareKModes(const drmModeModeInfo * kmode, const drmModeModeInfo * other)
Louis-Francis Ratté-Boulianne
committed
{
return memcmp(kmode, other, sizeof(*kmode));
}
static int
drm_mode_ensure_blob(xf86CrtcPtr crtc, const drmModeModeInfo* mode_info)
Louis-Francis Ratté-Boulianne
committed
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
drmmode_mode_ptr mode;
int ret;
if (drmmode_crtc->current_mode &&
drmmode_CompareKModes(&drmmode_crtc->current_mode->mode_info, mode_info) == 0)
Louis-Francis Ratté-Boulianne
committed
return 0;
mode = calloc(sizeof(drmmode_mode_rec), 1);
if (!mode)
return -1;
mode->mode_info = *mode_info;
Louis-Francis Ratté-Boulianne
committed
ret = drmModeCreatePropertyBlob(ms->fd,
&mode->mode_info,
sizeof(mode->mode_info),
&mode->blob_id);
drmmode_crtc->current_mode = mode;
xorg_list_add(&mode->entry, &drmmode_crtc->mode_list);
return ret;
}
static int
crtc_add_dpms_props(drmModeAtomicReq *req, xf86CrtcPtr crtc,
int new_dpms, Bool *active)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
Bool crtc_active = FALSE;
int i;
int ret = 0;
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
Louis-Francis Ratté-Boulianne
committed
drmmode_output_private_ptr drmmode_output = output->driver_private;
Louis-Francis Ratté-Boulianne
committed
if (output->crtc != crtc) {
if (drmmode_output->current_crtc == crtc) {
ret |= connector_add_prop(req, drmmode_output,
DRMMODE_CONNECTOR_CRTC_ID, 0);
}
continue;
Louis-Francis Ratté-Boulianne
committed
}
if (drmmode_output->output_id == -1)
continue;
if (new_dpms == DPMSModeOn)
crtc_active = TRUE;
ret |= connector_add_prop(req, drmmode_output,
DRMMODE_CONNECTOR_CRTC_ID,
crtc_active ?
drmmode_crtc->mode_crtc->crtc_id : 0);
}
if (crtc_active) {
drmModeModeInfo kmode;
drmmode_ConvertToKMode(crtc->scrn, &kmode, &crtc->mode);
ret |= drm_mode_ensure_blob(crtc, &kmode);
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_ACTIVE, 1);
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_MODE_ID,
drmmode_crtc->current_mode->blob_id);
} else {
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_ACTIVE, 0);
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_MODE_ID, 0);
}
if (active)
*active = crtc_active;
return ret;
}
Louis-Francis Ratté-Boulianne
committed
static void
drm_mode_destroy(xf86CrtcPtr crtc, drmmode_mode_ptr mode)
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
if (mode->blob_id)
drmModeDestroyPropertyBlob(ms->fd, mode->blob_id);
xorg_list_del(&mode->entry);
free(mode);
}
Louis-Francis Ratté-Boulianne
committed
static int
drmmode_crtc_can_test_mode(xf86CrtcPtr crtc)
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
Louis-Francis Ratté-Boulianne
committed
return ms->atomic_modeset;
}
drmmode_crtc_get_fb_id(xf86CrtcPtr crtc, uint32_t *fb_id, int *x, int *y)
{
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
drmmode_ptr drmmode = drmmode_crtc->drmmode;
drmmode_tearfree_ptr trf = &drmmode_crtc->tearfree;
int ret;
*fb_id = 0;
if (drmmode_crtc->prime_pixmap) {
if (!drmmode->reverse_prime_offload_mode) {
msPixmapPrivPtr ppriv =
msGetPixmapPriv(drmmode, drmmode_crtc->prime_pixmap);
*fb_id = ppriv->fb_id;
*x = 0;
} else
*x = drmmode_crtc->prime_pixmap_x;
*y = 0;
}
else if (trf->buf[trf->back_idx ^ 1].px) {
*fb_id = trf->buf[trf->back_idx ^ 1].fb_id;
*x = *y = 0;
}
else if (drmmode_crtc->rotate_fb_id) {
*fb_id = drmmode_crtc->rotate_fb_id;
*x = *y = 0;
}
else {
*fb_id = drmmode->fb_id;
*x = crtc->x;
*y = crtc->y;
}
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
ret = drmmode_bo_import(drmmode, &drmmode->front_bo,
&drmmode->fb_id);
if (ret < 0) {
ErrorF("failed to add fb %d\n", ret);
return FALSE;
}
*fb_id = drmmode->fb_id;
}
return TRUE;
}
void
drmmode_set_dpms(ScrnInfoPtr scrn, int dpms, int flags)
{
modesettingPtr ms = modesettingPTR(scrn);
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
drmModeAtomicReq *req = drmModeAtomicAlloc();
uint32_t mode_flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
int ret = 0;
int i;
assert(ms->atomic_modeset);
if (!req)
return;
Louis-Francis Ratté-Boulianne
committed
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
drmmode_output_private_ptr drmmode_output = output->driver_private;
if (output->crtc != NULL)
continue;
ret = connector_add_prop(req, drmmode_output,
DRMMODE_CONNECTOR_CRTC_ID, 0);
}
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
Bool active = FALSE;
ret |= crtc_add_dpms_props(req, crtc, dpms, &active);
if (dpms == DPMSModeOn && active && drmmode_crtc->need_modeset) {
uint32_t fb_id;
int x, y;
if (!drmmode_crtc_get_fb_id(crtc, &fb_id, &x, &y))
continue;
ret |= plane_add_props(req, crtc, fb_id, x, y);
drmmode_crtc->need_modeset = FALSE;
}
}
if (ret == 0)
drmModeAtomicCommit(ms->fd, req, mode_flags, NULL);
drmModeAtomicFree(req);
ms->pending_modeset = TRUE;
xf86DPMSSet(scrn, dpms, flags);
ms->pending_modeset = FALSE;
}
static int
drmmode_output_disable(xf86OutputPtr output)
{
modesettingPtr ms = modesettingPTR(output->scrn);
drmmode_output_private_ptr drmmode_output = output->driver_private;
xf86CrtcPtr crtc = drmmode_output->current_crtc;
drmModeAtomicReq *req = drmModeAtomicAlloc();
uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
assert(ms->atomic_modeset);
if (!req)
return 1;
ret |= connector_add_prop(req, drmmode_output,
DRMMODE_CONNECTOR_CRTC_ID, 0);
if (crtc)
ret |= crtc_add_dpms_props(req, crtc, DPMSModeOff, NULL);
if (ret == 0)
ret = drmModeAtomicCommit(ms->fd, req, flags, NULL);
Louis-Francis Ratté-Boulianne
committed
if (ret == 0)
drmmode_output->current_crtc = NULL;
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
drmModeAtomicFree(req);
return ret;
}
static int
drmmode_crtc_disable(xf86CrtcPtr crtc)
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
drmModeAtomicReq *req = drmModeAtomicAlloc();
uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
int ret = 0;
assert(ms->atomic_modeset);
if (!req)
return 1;
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_ACTIVE, 0);
ret |= crtc_add_prop(req, drmmode_crtc,
DRMMODE_CRTC_MODE_ID, 0);
if (ret == 0)
ret = drmModeAtomicCommit(ms->fd, req, flags, NULL);
drmModeAtomicFree(req);
return ret;
}
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
static void
drmmode_set_ctm(xf86CrtcPtr crtc, const struct drm_color_ctm *ctm)
{
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
drmmode_ptr drmmode = drmmode_crtc->drmmode;
drmmode_prop_info_ptr ctm_info =
&drmmode_crtc->props[DRMMODE_CRTC_CTM];
int ret;
uint32_t blob_id = 0;
if (ctm_info->prop_id == 0)
return;
if (ctm && drmmode_crtc->use_gamma_lut && !ctm_is_identity(ctm)) {
ret = drmModeCreatePropertyBlob(drmmode->fd, ctm, sizeof(*ctm), &blob_id);
if (ret != 0) {
xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
"Failed to create CTM property blob: %d\n", ret);
blob_id = 0;
}
}
ret = drmModeObjectSetProperty(drmmode->fd,
drmmode_crtc->mode_crtc->crtc_id,
DRM_MODE_OBJECT_CRTC, ctm_info->prop_id,
blob_id);
if (ret != 0)
xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
"Failed to set CTM property: %d\n", ret);
drmModeDestroyPropertyBlob(drmmode->fd, blob_id);
}
static int
drmmode_crtc_set_mode(xf86CrtcPtr crtc, Bool test_only)
Louis-Francis Ratté-Boulianne
committed
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
Louis-Francis Ratté-Boulianne
committed
drmmode_ptr drmmode = drmmode_crtc->drmmode;
drmModeModeInfo kmode;
Louis-Francis Ratté-Boulianne
committed
int output_count = 0;
uint32_t *output_ids = NULL;
uint32_t fb_id;
int x, y;
Louis-Francis Ratté-Boulianne
committed
int i, ret = 0;
const struct drm_color_ctm *ctm = NULL;
Louis-Francis Ratté-Boulianne
committed
if (!drmmode_crtc_get_fb_id(crtc, &fb_id, &x, &y))
return 1;
Louis-Francis Ratté-Boulianne
committed
#ifdef GLAMOR_HAS_GBM
/* Make sure any pending drawing will be visible in a new scanout buffer */
if (drmmode->glamor)
glamor_finish(crtc->scrn->pScreen);
Louis-Francis Ratté-Boulianne
committed
if (ms->atomic_modeset) {
drmModeAtomicReq *req = drmModeAtomicAlloc();
Bool active;
uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
Louis-Francis Ratté-Boulianne
committed
if (!req)
return 1;
ret |= crtc_add_dpms_props(req, crtc, DPMSModeOn, &active);
ret |= plane_add_props(req, crtc, active ? fb_id : 0, x, y);
Louis-Francis Ratté-Boulianne
committed
Louis-Francis Ratté-Boulianne
committed
/* Orphaned CRTCs need to be disabled right now in atomic mode */
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr other_crtc = xf86_config->crtc[i];
drmmode_crtc_private_ptr other_drmmode_crtc = other_crtc->driver_private;
int lost_outputs = 0;
int remaining_outputs = 0;
Louis-Francis Ratté-Boulianne
committed
if (other_crtc == crtc)
continue;
for (j = 0; j < xf86_config->num_output; j++) {
xf86OutputPtr output = xf86_config->output[j];
Louis-Francis Ratté-Boulianne
committed
drmmode_output_private_ptr drmmode_output = output->driver_private;
if (drmmode_output->current_crtc == other_crtc) {
if (output->crtc == crtc)
lost_outputs++;
else
remaining_outputs++;
}
}
if (lost_outputs > 0 && remaining_outputs == 0) {
ret |= crtc_add_prop(req, other_drmmode_crtc,
DRMMODE_CRTC_ACTIVE, 0);
ret |= crtc_add_prop(req, other_drmmode_crtc,
DRMMODE_CRTC_MODE_ID, 0);
}
}
if (test_only)
flags |= DRM_MODE_ATOMIC_TEST_ONLY;
Louis-Francis Ratté-Boulianne
committed
if (ret == 0)
ret = drmModeAtomicCommit(ms->fd, req, flags, NULL);
Louis-Francis Ratté-Boulianne
committed
Louis-Francis Ratté-Boulianne
committed
if (ret == 0 && !test_only) {
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
drmmode_output_private_ptr drmmode_output = output->driver_private;
if (output->crtc == crtc)
drmmode_output->current_crtc = crtc;
else if (drmmode_output->current_crtc == crtc)
drmmode_output->current_crtc = NULL;
}
}
Louis-Francis Ratté-Boulianne
committed
drmModeAtomicFree(req);
return ret;
}
Louis-Francis Ratté-Boulianne
committed
output_ids = calloc(sizeof(uint32_t), xf86_config->num_output);
if (!output_ids)
return -1;
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
drmmode_output_private_ptr drmmode_output;
if (output->crtc != crtc)
continue;
drmmode_output = output->driver_private;
if (drmmode_output->output_id == -1)
continue;
output_ids[output_count] = drmmode_output->output_id;
output_count++;
Louis-Francis Ratté-Boulianne
committed
}
drmmode_ConvertToKMode(crtc->scrn, &kmode, &crtc->mode);
Louis-Francis Ratté-Boulianne
committed
ret = drmModeSetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
fb_id, x, y, output_ids, output_count, &kmode);
if (!ret && !ms->atomic_modeset) {
drmmode_crtc->src_x = x;
drmmode_crtc->src_y = y;
}
Louis-Francis Ratté-Boulianne
committed
Louis-Francis Ratté-Boulianne
committed
free(output_ids);
return ret;
Louis-Francis Ratté-Boulianne
committed
}
int
drmmode_crtc_flip(xf86CrtcPtr crtc, uint32_t fb_id, int x, int y,
uint32_t flags, void *data)
{
modesettingPtr ms = modesettingPTR(crtc->scrn);
drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
int ret;
if (ms->atomic_modeset) {
drmModeAtomicReq *req = drmModeAtomicAlloc();
if (!req)
return 1;
ret = plane_add_props(req, crtc, fb_id, x, y);
flags |= DRM_MODE_ATOMIC_NONBLOCK;
if (ret == 0)
ret = drmModeAtomicCommit(ms->fd, req, flags, data);
drmModeAtomicFree(req);
return ret;
}
/* The frame buffer source coordinates may change when switching between the
* primary frame buffer and a per-CRTC frame buffer. Set the correct source
* coordinates if they differ for this flip.
*/
if (drmmode_crtc->src_x != x || drmmode_crtc->src_y != y) {
ret = drmModeSetPlane(ms->fd, drmmode_crtc->plane_id,
drmmode_crtc->mode_crtc->crtc_id, fb_id, 0,
0, 0, crtc->mode.HDisplay, crtc->mode.VDisplay,
x << 16, y << 16, crtc->mode.HDisplay << 16,
crtc->mode.VDisplay << 16);
if (ret) {
xf86DrvMsg(crtc->scrn->scrnIndex, X_WARNING,
"error changing fb src coordinates for flip: %d\n", ret);
return ret;
}
drmmode_crtc->src_x = x;
drmmode_crtc->src_y = y;
}
return drmModePageFlip(ms->fd, drmmode_crtc->mode_crtc->crtc_id,
fb_id, flags, data);
}
Louis-Francis Ratté-Boulianne
committed
drmmode_bo_destroy(drmmode_ptr drmmode, drmmode_bo *bo)
{
int ret;
#ifdef GLAMOR_HAS_GBM
if (bo->gbm) {
gbm_bo_destroy(bo->gbm);
bo->gbm = NULL;
}
#endif
if (bo->dumb) {
ret = dumb_bo_destroy(drmmode->fd, bo->dumb);