xf86Crtc.c 97.3 KB
Newer Older
1 2
/*
 * Copyright © 2006 Keith Packard
3
 * Copyright © 2008 Red Hat, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>
26 27 28 29
#else
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
30 31 32 33 34 35 36 37 38 39
#endif

#include <stddef.h>
#include <string.h>
#include <stdio.h>

#include "xf86.h"
#include "xf86DDC.h"
#include "xf86Crtc.h"
#include "xf86Modes.h"
40
#include "xf86Priv.h"
41 42
#include "xf86RandR12.h"
#include "X11/extensions/render.h"
Peter Hutterer's avatar
Peter Hutterer committed
43
#include "X11/extensions/dpmsconst.h"
44
#include "X11/Xatom.h"
45
#include "picturestr.h"
46

47
#ifdef XV
48
#include "xf86xv.h"
49
#endif
50

51 52
#define NO_OUTPUT_DEFAULT_WIDTH 1024
#define NO_OUTPUT_DEFAULT_HEIGHT 768
53 54 55 56
/*
 * Initialize xf86CrtcConfig structure
 */

57
int xf86CrtcConfigPrivateIndex = -1;
58

59
void
60
xf86CrtcConfigInit(ScrnInfoPtr scrn, const xf86CrtcConfigFuncsRec * funcs)
61
{
62 63
    xf86CrtcConfigPtr config;

64
    if (xf86CrtcConfigPrivateIndex == -1)
65 66
        xf86CrtcConfigPrivateIndex = xf86AllocateScrnInfoPrivateIndex();
    config = xnfcalloc(1, sizeof(xf86CrtcConfigRec));
67 68 69

    config->funcs = funcs;

70 71
    scrn->privates[xf86CrtcConfigPrivateIndex].ptr = config;
}
72

73
void
74 75
xf86CrtcSetSizeRange(ScrnInfoPtr scrn,
                     int minWidth, int minHeight, int maxWidth, int maxHeight)
76
{
77
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
78 79 80 81 82 83 84 85 86 87

    config->minWidth = minWidth;
    config->minHeight = minHeight;
    config->maxWidth = maxWidth;
    config->maxHeight = maxHeight;
}

/*
 * Crtc functions
 */
88
xf86CrtcPtr
89
xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs)
90
{
91 92
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CrtcPtr crtc, *crtcs;
93

94
    crtc = calloc(sizeof(xf86CrtcRec), 1);
95
    if (!crtc)
96
        return NULL;
97
    crtc->version = XF86_CRTC_VERSION;
98 99 100 101 102 103 104
    crtc->scrn = scrn;
    crtc->funcs = funcs;
#ifdef RANDR_12_INTERFACE
    crtc->randr_crtc = NULL;
#endif
    crtc->rotation = RR_Rotate_0;
    crtc->desiredRotation = RR_Rotate_0;
105 106 107
    pixman_transform_init_identity(&crtc->crtc_to_framebuffer);
    pixman_f_transform_init_identity(&crtc->f_crtc_to_framebuffer);
    pixman_f_transform_init_identity(&crtc->f_framebuffer_to_crtc);
108 109 110 111 112 113
    crtc->filter = NULL;
    crtc->params = NULL;
    crtc->nparams = 0;
    crtc->filter_width = 0;
    crtc->filter_height = 0;
    crtc->transform_in_use = FALSE;
114 115
    crtc->transformPresent = FALSE;
    crtc->desiredTransformPresent = FALSE;
116
    memset(&crtc->bounds, '\0', sizeof(crtc->bounds));
117

118 119
    /* Preallocate gamma at a sensible size. */
    crtc->gamma_size = 256;
120
    crtc->gamma_red = malloc(3 * crtc->gamma_size * sizeof(CARD16));
121
    if (!crtc->gamma_red) {
122 123
        free(crtc);
        return NULL;
124
    }
125 126 127
    crtc->gamma_green = crtc->gamma_red + crtc->gamma_size;
    crtc->gamma_blue = crtc->gamma_green + crtc->gamma_size;

128
    if (xf86_config->crtc)
129 130
        crtcs = realloc(xf86_config->crtc,
                        (xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
131
    else
132 133 134 135 136
        crtcs = malloc((xf86_config->num_crtc + 1) * sizeof(xf86CrtcPtr));
    if (!crtcs) {
        free(crtc->gamma_red);
        free(crtc);
        return NULL;
137 138 139 140 141 142
    }
    xf86_config->crtc = crtcs;
    xf86_config->crtc[xf86_config->num_crtc++] = crtc;
    return crtc;
}

143
void
144
xf86CrtcDestroy(xf86CrtcPtr crtc)
145
{
146 147 148
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
    int c;

149 150
    (*crtc->funcs->destroy) (crtc);
    for (c = 0; c < xf86_config->num_crtc; c++)
151 152 153 154 155 156 157
        if (xf86_config->crtc[c] == crtc) {
            memmove(&xf86_config->crtc[c],
                    &xf86_config->crtc[c + 1],
                    ((xf86_config->num_crtc - (c + 1)) * sizeof(void *)));
            xf86_config->num_crtc--;
            break;
        }
158
    free(crtc->params);
159
    free(crtc->gamma_red);
160
    free(crtc);
161 162 163 164 165 166
}

/**
 * Return whether any outputs are connected to the specified pipe
 */

167
Bool
168
xf86CrtcInUse(xf86CrtcPtr crtc)
169
{
170 171 172 173
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
    int o;

174
    for (o = 0; o < xf86_config->num_output; o++)
175 176
        if (xf86_config->output[o]->crtc == crtc)
            return TRUE;
177 178 179
    return FALSE;
}

180
void
181
xf86CrtcSetScreenSubpixelOrder(ScreenPtr pScreen)
182
{
183 184
    int subpixel_order = SubPixelUnknown;
    Bool has_none = FALSE;
185
    ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
186 187
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int c, o;
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
    for (c = 0; c < xf86_config->num_crtc; c++) {
        xf86CrtcPtr crtc = xf86_config->crtc[c];

        for (o = 0; o < xf86_config->num_output; o++) {
            xf86OutputPtr output = xf86_config->output[o];

            if (output->crtc == crtc) {
                switch (output->subpixel_order) {
                case SubPixelNone:
                    has_none = TRUE;
                    break;
                case SubPixelUnknown:
                    break;
                default:
                    subpixel_order = output->subpixel_order;
                    break;
                }
            }
            if (subpixel_order != SubPixelUnknown)
                break;
        }
        if (subpixel_order != SubPixelUnknown) {
            static const int circle[4] = {
                SubPixelHorizontalRGB,
                SubPixelVerticalRGB,
                SubPixelHorizontalBGR,
                SubPixelVerticalBGR,
            };
            int rotate;
            int c;

            for (rotate = 0; rotate < 4; rotate++)
                if (crtc->rotation & (1 << rotate))
                    break;
            for (c = 0; c < 4; c++)
                if (circle[c] == subpixel_order)
                    break;
            c = (c + rotate) & 0x3;
            if ((crtc->rotation & RR_Reflect_X) && !(c & 1))
                c ^= 2;
            if ((crtc->rotation & RR_Reflect_Y) && (c & 1))
                c ^= 2;
            subpixel_order = circle[c];
            break;
        }
234 235
    }
    if (subpixel_order == SubPixelUnknown && has_none)
236 237
        subpixel_order = SubPixelNone;
    PictureSetSubpixelOrder(pScreen, subpixel_order);
238 239
}

240 241 242
/**
 * Sets the given video mode on the given crtc
 */
243
Bool
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
xf86CrtcSetModeTransform(xf86CrtcPtr crtc, DisplayModePtr mode,
                         Rotation rotation, RRTransformPtr transform, int x,
                         int y)
{
    ScrnInfoPtr scrn = crtc->scrn;
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int i;
    Bool ret = FALSE;
    Bool didLock = FALSE;
    DisplayModePtr adjusted_mode;
    DisplayModeRec saved_mode;
    int saved_x, saved_y;
    Rotation saved_rotation;
    RRTransformRec saved_transform;
    Bool saved_transform_present;

    crtc->enabled = xf86CrtcInUse(crtc);
261

262
    /* We only hit this if someone explicitly sends a "disabled" modeset. */
263 264 265 266
    if (!crtc->enabled) {
        /* Check everything for stuff that should be off. */
        xf86DisableUnusedFunctions(scrn);
        return TRUE;
267 268
    }

269 270
    adjusted_mode = xf86DuplicateMode(mode);

271 272 273 274
    saved_mode = crtc->mode;
    saved_x = crtc->x;
    saved_y = crtc->y;
    saved_rotation = crtc->rotation;
275
    if (crtc->transformPresent) {
276 277
        RRTransformInit(&saved_transform);
        RRTransformCopy(&saved_transform, &crtc->transform);
278
    }
279 280
    saved_transform_present = crtc->transformPresent;

281 282 283
    /* Update crtc values up front so the driver can rely on them for mode
     * setting.
     */
284 285 286 287 288
    crtc->mode = *mode;
    crtc->x = x;
    crtc->y = y;
    crtc->rotation = rotation;
    if (transform) {
289 290 291 292 293
        RRTransformCopy(&crtc->transform, transform);
        crtc->transformPresent = TRUE;
    }
    else
        crtc->transformPresent = FALSE;
294

295
    if (crtc->funcs->set_mode_major) {
296 297
        ret = crtc->funcs->set_mode_major(crtc, mode, rotation, x, y);
        goto done;
298 299
    }

300
    didLock = crtc->funcs->lock(crtc);
301 302 303 304 305
    /* Pass our mode to the outputs and the CRTC to give them a chance to
     * adjust it according to limitations or output properties, and also
     * a chance to reject the mode entirely.
     */
    for (i = 0; i < xf86_config->num_output; i++) {
306
        xf86OutputPtr output = xf86_config->output[i];
307

308 309
        if (output->crtc != crtc)
            continue;
310

311 312 313
        if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
            goto done;
        }
314 315
    }

316
    if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
317
        goto done;
318 319
    }

320 321
    if (!xf86CrtcRotate(crtc))
        goto done;
322

Dave Airlie's avatar
Dave Airlie committed
323
    /* Prepare the outputs and CRTCs before setting the mode. */
324
    for (i = 0; i < xf86_config->num_output; i++) {
325
        xf86OutputPtr output = xf86_config->output[i];
326

327 328
        if (output->crtc != crtc)
            continue;
329

330 331
        /* Disable the output as the first thing we do. */
        output->funcs->prepare(output);
332 333
    }

Dave Airlie's avatar
Dave Airlie committed
334
    crtc->funcs->prepare(crtc);
335 336 337 338

    /* Set up the DPLL and any output state that needs to adjust or depend
     * on the DPLL.
     */
339
    crtc->funcs->mode_set(crtc, mode, adjusted_mode, crtc->x, crtc->y);
340 341 342 343 344
    for (i = 0; i < xf86_config->num_output; i++) {
        xf86OutputPtr output = xf86_config->output[i];

        if (output->crtc == crtc)
            output->funcs->mode_set(output, mode, adjusted_mode);
345 346
    }

347
    /* Only upload when needed, to avoid unneeded delays. */
348
    if (!crtc->active && crtc->funcs->gamma_set)
349 350
        crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
                               crtc->gamma_blue, crtc->gamma_size);
351

352
    /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
Dave Airlie's avatar
Dave Airlie committed
353
    crtc->funcs->commit(crtc);
354 355 356 357 358
    for (i = 0; i < xf86_config->num_output; i++) {
        xf86OutputPtr output = xf86_config->output[i];

        if (output->crtc == crtc)
            output->funcs->commit(output);
359 360 361
    }

    ret = TRUE;
362

363
 done:
364
    if (ret) {
365 366 367 368 369 370 371 372 373 374 375 376 377 378
        crtc->active = TRUE;
        if (scrn->pScreen)
            xf86CrtcSetScreenSubpixelOrder(scrn->pScreen);
        if (scrn->ModeSet)
            scrn->ModeSet(scrn);
    }
    else {
        crtc->x = saved_x;
        crtc->y = saved_y;
        crtc->rotation = saved_rotation;
        crtc->mode = saved_mode;
        if (saved_transform_present)
            RRTransformCopy(&crtc->transform, &saved_transform);
        crtc->transformPresent = saved_transform_present;
379 380
    }

381 382
    free(adjusted_mode->name);
    free(adjusted_mode);
383

384
    if (didLock)
385
        crtc->funcs->unlock(crtc);
386 387 388 389

    return ret;
}

390 391 392 393 394
/**
 * Sets the given video mode on the given crtc, but without providing
 * a transform
 */
Bool
395 396
xf86CrtcSetMode(xf86CrtcPtr crtc, DisplayModePtr mode, Rotation rotation,
                int x, int y)
397
{
398
    return xf86CrtcSetModeTransform(crtc, mode, rotation, NULL, x, y);
399 400
}

Matthias Hopf's avatar
Matthias Hopf committed
401 402 403
/**
 * Pans the screen, does not change the mode
 */
404
void
405
xf86CrtcSetOrigin(xf86CrtcPtr crtc, int x, int y)
Matthias Hopf's avatar
Matthias Hopf committed
406
{
407 408 409 410 411
    ScrnInfoPtr scrn = crtc->scrn;

    crtc->x = x;
    crtc->y = y;
    if (crtc->funcs->set_origin) {
412 413 414 415 416
        if (!xf86CrtcRotate(crtc))
            return;
        crtc->funcs->set_origin(crtc, x, y);
        if (scrn->ModeSet)
            scrn->ModeSet(scrn);
417
    }
418
    else
419
        xf86CrtcSetMode(crtc, &crtc->mode, crtc->rotation, x, y);
Matthias Hopf's avatar
Matthias Hopf committed
420 421
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/*
 * Output functions
 */

extern XF86ConfigPtr xf86configptr;

typedef enum {
    OPTION_PREFERRED_MODE,
    OPTION_POSITION,
    OPTION_BELOW,
    OPTION_RIGHT_OF,
    OPTION_ABOVE,
    OPTION_LEFT_OF,
    OPTION_ENABLE,
    OPTION_DISABLE,
    OPTION_MIN_CLOCK,
    OPTION_MAX_CLOCK,
    OPTION_IGNORE,
440
    OPTION_ROTATE,
441
    OPTION_PANNING,
442
    OPTION_PRIMARY,
443
    OPTION_DEFAULT_MODES,
444 445 446
} OutputOpts;

static OptionInfoRec xf86OutputOptions[] = {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    {OPTION_PREFERRED_MODE, "PreferredMode", OPTV_STRING, {0}, FALSE},
    {OPTION_POSITION, "Position", OPTV_STRING, {0}, FALSE},
    {OPTION_BELOW, "Below", OPTV_STRING, {0}, FALSE},
    {OPTION_RIGHT_OF, "RightOf", OPTV_STRING, {0}, FALSE},
    {OPTION_ABOVE, "Above", OPTV_STRING, {0}, FALSE},
    {OPTION_LEFT_OF, "LeftOf", OPTV_STRING, {0}, FALSE},
    {OPTION_ENABLE, "Enable", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_DISABLE, "Disable", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_MIN_CLOCK, "MinClock", OPTV_FREQ, {0}, FALSE},
    {OPTION_MAX_CLOCK, "MaxClock", OPTV_FREQ, {0}, FALSE},
    {OPTION_IGNORE, "Ignore", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
    {OPTION_PANNING, "Panning", OPTV_STRING, {0}, FALSE},
    {OPTION_PRIMARY, "Primary", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_DEFAULT_MODES, "DefaultModes", OPTV_BOOLEAN, {0}, FALSE},
    {-1, NULL, OPTV_NONE, {0}, FALSE},
463 464
};

465 466 467 468 469
enum {
    OPTION_MODEDEBUG,
};

static OptionInfoRec xf86DeviceOptions[] = {
470 471
    {OPTION_MODEDEBUG, "ModeDebug", OPTV_BOOLEAN, {0}, FALSE},
    {-1, NULL, OPTV_NONE, {0}, FALSE},
472 473
};

474
static void
475
xf86OutputSetMonitor(xf86OutputPtr output)
476
{
477 478
    char *option_name;
    const char *monitor;
479 480

    if (!output->name)
481
        return;
482

483
    free(output->options);
484

485 486
    output->options = xnfalloc(sizeof(xf86OutputOptions));
    memcpy(output->options, xf86OutputOptions, sizeof(xf86OutputOptions));
487 488

    XNFasprintf(&option_name, "monitor-%s", output->name);
489
    monitor = xf86findOptionValue(output->scrn->options, option_name);
490
    if (!monitor)
491
        monitor = output->name;
492
    else
493
        xf86MarkOptionUsedByName(output->scrn->options, option_name);
494
    free(option_name);
495 496
    output->conf_monitor = xf86findMonitor(monitor,
                                           xf86configptr->conf_monitor_lst);
497 498 499 500
    /*
     * Find the monitor section of the screen and use that
     */
    if (!output->conf_monitor && output->use_screen_monitor)
501 502 503 504 505 506 507 508 509
        output->conf_monitor = xf86findMonitor(output->scrn->monitor->id,
                                               xf86configptr->conf_monitor_lst);
    if (output->conf_monitor) {
        xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
                   "Output %s using monitor section %s\n",
                   output->name, output->conf_monitor->mon_identifier);
        xf86ProcessOptions(output->scrn->scrnIndex,
                           output->conf_monitor->mon_option_lst,
                           output->options);
510 511
    }
    else
512 513
        xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
                   "Output %s has no monitor section\n", output->name);
514 515 516
}

static Bool
517
xf86OutputEnabled(xf86OutputPtr output, Bool strict)
518
{
519
    Bool enable, disable;
520 521

    /* check to see if this output was enabled in the config file */
522 523 524 525
    if (xf86GetOptValBool(output->options, OPTION_ENABLE, &enable) && enable) {
        xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
                   "Output %s enabled by config file\n", output->name);
        return TRUE;
526 527
    }
    /* or if this output was disabled in the config file */
528 529 530 531
    if (xf86GetOptValBool(output->options, OPTION_DISABLE, &disable) && disable) {
        xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
                   "Output %s disabled by config file\n", output->name);
        return FALSE;
532
    }
533 534 535

    /* If not, try to only light up the ones we know are connected */
    if (strict) {
536
        enable = output->status == XF86OutputStatusConnected;
537 538 539
    }
    /* But if that fails, try to light up even outputs we're unsure of */
    else {
540
        enable = output->status != XF86OutputStatusDisconnected;
541 542
    }

543 544
    xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
               "Output %s %sconnected\n", output->name, enable ? "" : "dis");
545
    return enable;
546 547 548
}

static Bool
549
xf86OutputIgnored(xf86OutputPtr output)
550
{
551
    return xf86ReturnOptValBool(output->options, OPTION_IGNORE, FALSE);
552 553
}

554
static const char *direction[4] = {
555 556 557
    "normal",
    "left",
    "inverted",
558 559 560 561
    "right"
};

static Rotation
562
xf86OutputInitialRotation(xf86OutputPtr output)
563
{
564 565 566
    char *rotate_name = xf86GetOptValString(output->options,
                                            OPTION_ROTATE);
    int i;
567

568
    if (!rotate_name) {
569 570 571
        if (output->initial_rotation)
            return output->initial_rotation;
        return RR_Rotate_0;
572
    }
573

574
    for (i = 0; i < 4; i++)
575 576
        if (xf86nameCompare(direction[i], rotate_name) == 0)
            return 1 << i;
577 578 579
    return RR_Rotate_0;
}

580
xf86OutputPtr
581 582
xf86OutputCreate(ScrnInfoPtr scrn,
                 const xf86OutputFuncsRec * funcs, const char *name)
583
{
584 585 586 587
    xf86OutputPtr output, *outputs;
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int len;
    Bool primary;
588 589

    if (name)
590
        len = strlen(name) + 1;
591
    else
592
        len = 0;
593

594
    output = calloc(sizeof(xf86OutputRec) + len, 1);
595
    if (!output)
596
        return NULL;
597 598
    output->scrn = scrn;
    output->funcs = funcs;
599 600 601
    if (name) {
        output->name = (char *) (output + 1);
        strcpy(output->name, name);
602 603
    }
    output->subpixel_order = SubPixelUnknown;
604 605 606 607
    /*
     * Use the old per-screen monitor section for the first output
     */
    output->use_screen_monitor = (xf86_config->num_output == 0);
608 609 610
#ifdef RANDR_12_INTERFACE
    output->randr_output = NULL;
#endif
611 612 613 614 615 616 617 618
    if (name) {
        xf86OutputSetMonitor(output);
        if (xf86OutputIgnored(output)) {
            free(output);
            return FALSE;
        }
    }

619
    if (xf86_config->output)
620 621 622
        outputs = realloc(xf86_config->output,
                          (xf86_config->num_output +
                           1) * sizeof(xf86OutputPtr));
623
    else
624 625 626 627
        outputs = malloc((xf86_config->num_output + 1) * sizeof(xf86OutputPtr));
    if (!outputs) {
        free(output);
        return NULL;
628
    }
629

630
    xf86_config->output = outputs;
631

632 633 634 635
    if (xf86GetOptValBool(output->options, OPTION_PRIMARY, &primary) && primary) {
        memmove(xf86_config->output + 1, xf86_config->output,
                xf86_config->num_output * sizeof(xf86OutputPtr));
        xf86_config->output[0] = output;
636
    }
637 638
    else {
        xf86_config->output[xf86_config->num_output] = output;
639 640 641 642
    }

    xf86_config->num_output++;

643 644 645
    return output;
}

646
Bool
647
xf86OutputRename(xf86OutputPtr output, const char *name)
648
{
649 650
    char *newname = strdup(name);

651
    if (!newname)
652 653
        return FALSE;           /* so sorry... */

654
    if (output->name && output->name != (char *) (output + 1))
655
        free(output->name);
656
    output->name = newname;
657 658 659
    xf86OutputSetMonitor(output);
    if (xf86OutputIgnored(output))
        return FALSE;
660 661 662
    return TRUE;
}

663
void
664
xf86OutputUseScreenMonitor(xf86OutputPtr output, Bool use_screen_monitor)
665
{
666 667 668
    if (use_screen_monitor != output->use_screen_monitor) {
        output->use_screen_monitor = use_screen_monitor;
        xf86OutputSetMonitor(output);
669 670 671
    }
}

672
void
673
xf86OutputDestroy(xf86OutputPtr output)
674
{
675 676 677 678
    ScrnInfoPtr scrn = output->scrn;
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int o;

679 680
    (*output->funcs->destroy) (output);
    while (output->probed_modes)
681
        xf86DeleteMode(&output->probed_modes, output->probed_modes);
682
    for (o = 0; o < xf86_config->num_output; o++)
683 684 685 686 687 688 689
        if (xf86_config->output[o] == output) {
            memmove(&xf86_config->output[o],
                    &xf86_config->output[o + 1],
                    ((xf86_config->num_output - (o + 1)) * sizeof(void *)));
            xf86_config->num_output--;
            break;
        }
690
    if (output->name && output->name != (char *) (output + 1))
691
        free(output->name);
692
    free(output);
693 694
}

695 696 697 698
/*
 * Called during CreateScreenResources to hook up RandR
 */
static Bool
699
xf86CrtcCreateScreenResources(ScreenPtr screen)
700
{
701
    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
702
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
703 704 705

    screen->CreateScreenResources = config->CreateScreenResources;

706 707 708 709 710
    if (!(*screen->CreateScreenResources) (screen))
        return FALSE;

    if (!xf86RandR12CreateScreenResources(screen))
        return FALSE;
711 712 713 714

    return TRUE;
}

715 716 717 718
/*
 * Clean up config on server reset
 */
static Bool
719
xf86CrtcCloseScreen(ScreenPtr screen)
720
{
721
    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
722 723 724
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
    int o, c;

725 726
    screen->CloseScreen = config->CloseScreen;

727
    xf86RotateCloseScreen(screen);
728

729 730 731 732
    xf86RandR12CloseScreen(screen);

    screen->CloseScreen(screen);

733 734
    for (o = 0; o < config->num_output; o++) {
        xf86OutputPtr output = config->output[o];
735

736
        output->randr_output = NULL;
737
    }
738 739
    for (c = 0; c < config->num_crtc; c++) {
        xf86CrtcPtr crtc = config->crtc[c];
740

741
        crtc->randr_crtc = NULL;
742
    }
743 744
    /* detach any providers */
    if (config->randr_provider) {
745 746 747 748 749
        if (config->randr_provider->offload_sink) {
            DetachOffloadGPU(screen);
            config->randr_provider->offload_sink = NULL;
        }
        else if (config->randr_provider->output_source) {
750 751 752 753
            DetachOutputGPU(screen);
            config->randr_provider->output_source = NULL;
        }
        else if (screen->current_master)
754 755
            DetachUnboundGPU(screen);
    }
756
    return TRUE;
757 758
}

759 760 761
/*
 * Called at ScreenInit time to set up
 */
762 763 764 765 766
#ifdef RANDR_13_INTERFACE
int
#else
Bool
#endif
767
xf86CrtcScreenInit(ScreenPtr screen)
768
{
769
    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
770 771
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
    int c;
772 773

    /* Rotation */
774 775 776 777
    xf86DrvMsg(scrn->scrnIndex, X_INFO,
               "RandR 1.2 enabled, ignore the following RandR disabled message.\n");
    xf86DisableRandR();         /* Disable old RandR extension support */
    xf86RandR12Init(screen);
778 779

    /* support all rotations if every crtc has the shadow alloc funcs */
780 781 782 783 784
    for (c = 0; c < config->num_crtc; c++) {
        xf86CrtcPtr crtc = config->crtc[c];

        if (!crtc->funcs->shadow_allocate || !crtc->funcs->shadow_create)
            break;
785
    }
786 787 788 789 790 791 792 793 794
    if (c == config->num_crtc) {
        xf86RandR12SetRotations(screen, RR_Rotate_0 | RR_Rotate_90 |
                                RR_Rotate_180 | RR_Rotate_270 |
                                RR_Reflect_X | RR_Reflect_Y);
        xf86RandR12SetTransformSupport(screen, TRUE);
    }
    else {
        xf86RandR12SetRotations(screen, RR_Rotate_0);
        xf86RandR12SetTransformSupport(screen, FALSE);
795
    }
796

797 798 799
    /* Wrap CreateScreenResources so we can initialize the RandR code */
    config->CreateScreenResources = screen->CreateScreenResources;
    screen->CreateScreenResources = xf86CrtcCreateScreenResources;
800 801 802

    config->CloseScreen = screen->CloseScreen;
    screen->CloseScreen = xf86CrtcCloseScreen;
803 804 805

    /* This might still be marked wrapped from a previous generation */
    config->BlockHandler = NULL;
806

807
#ifdef XFreeXDGA
Keith Packard's avatar
Keith Packard committed
808
    _xf86_di_dga_init_internal(screen);
809
#endif
810 811 812
#ifdef RANDR_13_INTERFACE
    return RANDR_INTERFACE_VERSION;
#else
813
    return TRUE;
814
#endif
815 816
}

817
static DisplayModePtr
818 819 820 821 822 823 824 825
xf86DefaultMode(xf86OutputPtr output, int width, int height)
{
    DisplayModePtr target_mode = NULL;
    DisplayModePtr mode;
    int target_diff = 0;
    int target_preferred = 0;
    int mm_height;

826 827
    mm_height = output->mm_height;
    if (!mm_height)
828
        mm_height = (768 * 25.4) / DEFAULT_DPI;
829
    /*
830
     * Pick a mode closest to DEFAULT_DPI
831
     */
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    for (mode = output->probed_modes; mode; mode = mode->next) {
        int dpi;
        int preferred = (((mode->type & M_T_PREFERRED) != 0) +
                         ((mode->type & M_T_USERPREF) != 0));
        int diff;

        if (xf86ModeWidth(mode, output->initial_rotation) > width ||
            xf86ModeHeight(mode, output->initial_rotation) > height)
            continue;

        /* yes, use VDisplay here, not xf86ModeHeight */
        dpi = (mode->VDisplay * 254) / (mm_height * 10);
        diff = dpi - DEFAULT_DPI;
        diff = diff < 0 ? -diff : diff;
        if (target_mode == NULL || (preferred > target_preferred) ||
            (preferred == target_preferred && diff < target_diff)) {
            target_mode = mode;
            target_diff = diff;
            target_preferred = preferred;
        }
852 853 854 855 856
    }
    return target_mode;
}

static DisplayModePtr
857 858 859 860 861 862 863 864
xf86ClosestMode(xf86OutputPtr output,
                DisplayModePtr match, Rotation match_rotation,
                int width, int height)
{
    DisplayModePtr target_mode = NULL;
    DisplayModePtr mode;
    int target_diff = 0;

865 866 867
    /*
     * Pick a mode closest to the specified mode
     */
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    for (mode = output->probed_modes; mode; mode = mode->next) {
        int dx, dy;
        int diff;

        if (xf86ModeWidth(mode, output->initial_rotation) > width ||
            xf86ModeHeight(mode, output->initial_rotation) > height)
            continue;

        /* exact matches are preferred */
        if (output->initial_rotation == match_rotation &&
            xf86ModesEqual(mode, match))
            return mode;

        dx = xf86ModeWidth(match, match_rotation) - xf86ModeWidth(mode,
                                                                  output->
                                                                  initial_rotation);
        dy = xf86ModeHeight(match, match_rotation) - xf86ModeHeight(mode,
                                                                    output->
                                                                    initial_rotation);
        diff = dx * dx + dy * dy;
        if (target_mode == NULL || diff < target_diff) {
            target_mode = mode;
            target_diff = diff;
        }
892 893 894 895
    }
    return target_mode;
}

896
static DisplayModePtr
897
xf86OutputHasPreferredMode(xf86OutputPtr output, int width, int height)
898
{
899
    DisplayModePtr mode;
900

901 902 903 904
    for (mode = output->probed_modes; mode; mode = mode->next) {
        if (xf86ModeWidth(mode, output->initial_rotation) > width ||
            xf86ModeHeight(mode, output->initial_rotation) > height)
            continue;
905

906 907
        if (mode->type & M_T_PREFERRED)
            return mode;
908
    }
909 910 911 912
    return NULL;
}

static DisplayModePtr
913
xf86OutputHasUserPreferredMode(xf86OutputPtr output)
914 915 916 917
{
    DisplayModePtr mode, first = output->probed_modes;

    for (mode = first; mode && mode->next != first; mode = mode->next)
918 919
        if (mode->type & M_T_USERPREF)
            return mode;
920 921

    return NULL;
922 923 924
}

static int
925 926 927 928 929 930 931 932 933 934 935 936 937 938
xf86PickCrtcs(ScrnInfoPtr scrn,
              xf86CrtcPtr * best_crtcs,
              DisplayModePtr * modes, int n, int width, int height)
{
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
    int c, o;
    xf86OutputPtr output;
    xf86CrtcPtr crtc;
    xf86CrtcPtr *crtcs;
    xf86CrtcPtr best_crtc;
    int best_score;
    int score;
    int my_score;

939
    if (n == config->num_output)
940
        return 0;
941
    output = config->output[n];
942

943 944 945 946 947
    /*
     * Compute score with this output disabled
     */
    best_crtcs[n] = NULL;
    best_crtc = NULL;
948
    best_score = xf86PickCrtcs(scrn, best_crtcs, modes, n + 1, width, height);
949
    if (modes[n] == NULL)
950 951 952
        return best_score;

    crtcs = malloc(config->num_output * sizeof(xf86CrtcPtr));
953
    if (!crtcs)
954
        return best_score;
955 956 957 958

    my_score = 1;
    /* Score outputs that are known to be connected higher */
    if (output->status == XF86OutputStatusConnected)
959
        my_score++;
960
    /* Score outputs with preferred modes higher */
961 962
    if (xf86OutputHasPreferredMode(output, width, height))
        my_score++;
963 964 965 966 967
    /*
     * Select a crtc for this output and
     * then attempt to configure the remaining
     * outputs
     */
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    for (c = 0; c < config->num_crtc; c++) {
        if ((output->possible_crtcs & (1 << c)) == 0)
            continue;

        crtc = config->crtc[c];
        /*
         * Check to see if some other output is
         * using this crtc
         */
        for (o = 0; o < n; o++)
            if (best_crtcs[o] == crtc)
                break;
        if (o < n) {
            /*
             * If the two outputs desire the same mode,
             * see if they can be cloned
             */
            if (xf86ModesEqual(modes[o], modes[n]) &&
                config->output[o]->initial_rotation ==
                config->output[n]->initial_rotation &&
                config->output[o]->initial_x == config->output[n]->initial_x &&
                config->output[o]->initial_y == config->output[n]->initial_y) {
                if ((output->possible_clones & (1 << o)) == 0)
                    continue;   /* nope, try next CRTC */
            }
            else
                continue;       /* different modes, can't clone */
        }
        crtcs[n] = crtc;
        memcpy(crtcs, best_crtcs, n * sizeof(xf86CrtcPtr));
        score =
            my_score + xf86PickCrtcs(scrn, crtcs, modes, n + 1, width, height);
        if (score > best_score) {
            best_crtc = crtc;
            best_score = score;
            memcpy(best_crtcs, crtcs, config->num_output * sizeof(xf86CrtcPtr));
        }
1005