xf86Crtc.c 95.6 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 48
#include "xf86xv.h"

49 50
#define NO_OUTPUT_DEFAULT_WIDTH 1024
#define NO_OUTPUT_DEFAULT_HEIGHT 768
51 52 53 54
/*
 * Initialize xf86CrtcConfig structure
 */

55
int xf86CrtcConfigPrivateIndex = -1;
56

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

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

    config->funcs = funcs;

68 69
    scrn->privates[xf86CrtcConfigPrivateIndex].ptr = config;
}
70

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

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

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

92
    crtc = calloc(sizeof(xf86CrtcRec), 1);
93
    if (!crtc)
94
        return NULL;
95
    crtc->version = XF86_CRTC_VERSION;
96 97 98 99 100 101 102
    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;
103 104 105
    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);
106 107 108 109 110 111
    crtc->filter = NULL;
    crtc->params = NULL;
    crtc->nparams = 0;
    crtc->filter_width = 0;
    crtc->filter_height = 0;
    crtc->transform_in_use = FALSE;
112 113
    crtc->transformPresent = FALSE;
    crtc->desiredTransformPresent = FALSE;
114
    memset(&crtc->bounds, '\0', sizeof(crtc->bounds));
115

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

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

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

147 148
    (*crtc->funcs->destroy) (crtc);
    for (c = 0; c < xf86_config->num_crtc; c++)
149 150 151 152 153 154 155
        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;
        }
156
    free(crtc->params);
157
    free(crtc->gamma_red);
158
    free(crtc);
159 160 161 162 163 164
}

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

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

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

178
void
179
xf86CrtcSetScreenSubpixelOrder(ScreenPtr pScreen)
180
{
181 182 183 184 185
    int subpixel_order = SubPixelUnknown;
    Bool has_none = FALSE;
    ScrnInfoPtr scrn = xf86Screens[pScreen->myNum];
    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int c, o;
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
    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;
        }
232 233
    }
    if (subpixel_order == SubPixelUnknown && has_none)
234 235
        subpixel_order = SubPixelNone;
    PictureSetSubpixelOrder(pScreen, subpixel_order);
236 237
}

238 239 240
/**
 * Sets the given video mode on the given crtc
 */
241
Bool
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
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);
259

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

267 268
    adjusted_mode = xf86DuplicateMode(mode);

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

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

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

298
    didLock = crtc->funcs->lock(crtc);
299 300 301 302 303
    /* 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++) {
304
        xf86OutputPtr output = xf86_config->output[i];
305

306 307
        if (output->crtc != crtc)
            continue;
308

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

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

318 319
    if (!xf86CrtcRotate(crtc))
        goto done;
320

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

325 326
        if (output->crtc != crtc)
            continue;
327

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

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

    /* Set up the DPLL and any output state that needs to adjust or depend
     * on the DPLL.
     */
337
    crtc->funcs->mode_set(crtc, mode, adjusted_mode, crtc->x, crtc->y);
338 339 340 341 342
    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);
343 344
    }

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

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

        if (output->crtc == crtc)
            output->funcs->commit(output);
357 358 359
    }

    ret = TRUE;
360

361
 done:
362
    if (ret) {
363 364 365 366 367 368 369 370 371 372 373 374 375 376
        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;
377 378
    }

379 380
    free(adjusted_mode->name);
    free(adjusted_mode);
381

382
    if (didLock)
383
        crtc->funcs->unlock(crtc);
384 385 386 387

    return ret;
}

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

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

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

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/*
 * 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,
438
    OPTION_ROTATE,
439
    OPTION_PANNING,
440
    OPTION_PRIMARY,
441
    OPTION_DEFAULT_MODES,
442 443 444
} OutputOpts;

static OptionInfoRec xf86OutputOptions[] = {
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    {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},
461 462
};

463 464 465 466 467
enum {
    OPTION_MODEDEBUG,
};

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

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

    if (!output->name)
479
        return;
480

481
    free(output->options);
482

483 484
    output->options = xnfalloc(sizeof(xf86OutputOptions));
    memcpy(output->options, xf86OutputOptions, sizeof(xf86OutputOptions));
485 486

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

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

    /* check to see if this output was enabled in the config file */
520 521 522 523
    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;
524 525
    }
    /* or if this output was disabled in the config file */
526 527 528 529
    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;
530
    }
531 532 533

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

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

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

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

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

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

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

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

    if (name)
588
        len = strlen(name) + 1;
589
    else
590
        len = 0;
591

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

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

628
    xf86_config->output = outputs;
629

630 631 632 633
    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;
634
    }
635 636
    else {
        xf86_config->output[xf86_config->num_output] = output;
637 638 639 640
    }

    xf86_config->num_output++;

641 642 643
    return output;
}

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

649
    if (!newname)
650 651
        return FALSE;           /* so sorry... */

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

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

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

677 678
    (*output->funcs->destroy) (output);
    while (output->probed_modes)
679
        xf86DeleteMode(&output->probed_modes, output->probed_modes);
680
    for (o = 0; o < xf86_config->num_output; o++)
681 682 683 684 685 686 687
        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;
        }
688
    if (output->name && output->name != (char *) (output + 1))
689
        free(output->name);
690
    free(output);
691 692
}

693 694 695 696
/*
 * Called during CreateScreenResources to hook up RandR
 */
static Bool
697
xf86CrtcCreateScreenResources(ScreenPtr screen)
698
{
699 700
    ScrnInfoPtr scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
701 702 703

    screen->CreateScreenResources = config->CreateScreenResources;

704 705 706 707 708
    if (!(*screen->CreateScreenResources) (screen))
        return FALSE;

    if (!xf86RandR12CreateScreenResources(screen))
        return FALSE;
709 710 711 712

    return TRUE;
}

713 714 715 716
/*
 * Clean up config on server reset
 */
static Bool
717
xf86CrtcCloseScreen(int index, ScreenPtr screen)
718
{
719 720 721 722
    ScrnInfoPtr scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
    int o, c;

723 724
    screen->CloseScreen = config->CloseScreen;

725
    xf86RotateCloseScreen(screen);
726

727 728
    for (o = 0; o < config->num_output; o++) {
        xf86OutputPtr output = config->output[o];
729

730
        output->randr_output = NULL;
731
    }
732 733
    for (c = 0; c < config->num_crtc; c++) {
        xf86CrtcPtr crtc = config->crtc[c];
734

735
        crtc->randr_crtc = NULL;
736
    }
737
    xf86RandR12CloseScreen(screen);
738

739
    return screen->CloseScreen(index, screen);
740 741
}

742 743 744
/*
 * Called at ScreenInit time to set up
 */
745 746 747 748 749
#ifdef RANDR_13_INTERFACE
int
#else
Bool
#endif
750
xf86CrtcScreenInit(ScreenPtr screen)
751
{
752 753 754
    ScrnInfoPtr scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
    int c;
755 756

    /* Rotation */
757 758 759 760
    xf86DrvMsg(scrn->scrnIndex, X_INFO,
               "RandR 1.2 enabled, ignore the following RandR disabled message.\n");
    xf86DisableRandR();         /* Disable old RandR extension support */
    xf86RandR12Init(screen);
761 762

    /* support all rotations if every crtc has the shadow alloc funcs */
763 764 765 766 767
    for (c = 0; c < config->num_crtc; c++) {
        xf86CrtcPtr crtc = config->crtc[c];

        if (!crtc->funcs->shadow_allocate || !crtc->funcs->shadow_create)
            break;
768
    }
769 770 771 772 773 774 775 776 777
    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);
778
    }
779

780 781 782
    /* Wrap CreateScreenResources so we can initialize the RandR code */
    config->CreateScreenResources = screen->CreateScreenResources;
    screen->CreateScreenResources = xf86CrtcCreateScreenResources;
783 784 785

    config->CloseScreen = screen->CloseScreen;
    screen->CloseScreen = xf86CrtcCloseScreen;
786 787 788

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

790
#ifdef XFreeXDGA
Keith Packard's avatar
Keith Packard committed
791
    _xf86_di_dga_init_internal(screen);
792
#endif
793 794 795
#ifdef RANDR_13_INTERFACE
    return RANDR_INTERFACE_VERSION;
#else
796
    return TRUE;
797
#endif
798 799
}

800
static DisplayModePtr
801 802 803 804 805 806 807 808
xf86DefaultMode(xf86OutputPtr output, int width, int height)
{
    DisplayModePtr target_mode = NULL;
    DisplayModePtr mode;
    int target_diff = 0;
    int target_preferred = 0;
    int mm_height;

809 810
    mm_height = output->mm_height;
    if (!mm_height)
811
        mm_height = (768 * 25.4) / DEFAULT_DPI;
812
    /*
813
     * Pick a mode closest to DEFAULT_DPI
814
     */
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    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;
        }
835 836 837 838 839
    }
    return target_mode;
}

static DisplayModePtr
840 841 842 843 844 845 846 847
xf86ClosestMode(xf86OutputPtr output,
                DisplayModePtr match, Rotation match_rotation,
                int width, int height)
{
    DisplayModePtr target_mode = NULL;
    DisplayModePtr mode;
    int target_diff = 0;

848 849 850
    /*
     * Pick a mode closest to the specified mode
     */
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    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;
        }
875 876 877 878
    }
    return target_mode;
}

879
static DisplayModePtr
880
xf86OutputHasPreferredMode(xf86OutputPtr output, int width, int height)
881
{
882
    DisplayModePtr mode;
883

884 885 886 887
    for (mode = output->probed_modes; mode; mode = mode->next) {
        if (xf86ModeWidth(mode, output->initial_rotation) > width ||
            xf86ModeHeight(mode, output->initial_rotation) > height)
            continue;
888

889 890
        if (mode->type & M_T_PREFERRED)
            return mode;
891
    }
892 893 894 895
    return NULL;
}

static DisplayModePtr
896
xf86OutputHasUserPreferredMode(xf86OutputPtr output)
897 898 899 900
{
    DisplayModePtr mode, first = output->probed_modes;

    for (mode = first; mode && mode->next != first; mode = mode->next)
901 902
        if (mode->type & M_T_USERPREF)
            return mode;
903 904

    return NULL;
905 906 907
}

static int
908 909 910 911 912 913 914 915 916 917 918 919 920 921
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;

922
    if (n == config->num_output)
923
        return 0;
924
    output = config->output[n];
925

926 927 928 929 930
    /*
     * Compute score with this output disabled
     */
    best_crtcs[n] = NULL;
    best_crtc = NULL;
931
    best_score = xf86PickCrtcs(scrn, best_crtcs, modes, n + 1, width, height);
932
    if (modes[n] == NULL)
933 934 935
        return best_score;

    crtcs = malloc(config->num_output * sizeof(xf86CrtcPtr));
936
    if (!crtcs)
937
        return best_score;
938 939 940 941

    my_score = 1;
    /* Score outputs that are known to be connected higher */
    if (output->status == XF86OutputStatusConnected)
942
        my_score++;
943
    /* Score outputs with preferred modes higher */
944 945
    if (xf86OutputHasPreferredMode(output, width, height))
        my_score++;
946 947 948 949 950
    /*
     * Select a crtc for this output and
     * then attempt to configure the remaining
     * outputs
     */
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    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));
        }
988
    }
989
    free(crtcs);
990 991 992 993 994
    return best_score;
}

/*
 * Compute the virtual size necessary to place all of the available
995 996 997 998 999
 * crtcs in the specified configuration.
 *
 * canGrow indicates that the driver can make the screen larger than its initial
 * configuration.  If FALSE, this function will enlarge the screen to include
 * the largest available mode.
1000 1001 1002
 */

static void
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046