xorg_crtc.c revision 2ab7a8a3ebf337aeed61166719adef9da4a1278a
1/* 2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * 26 * Author: Alan Hourihane <alanh@tungstengraphics.com> 27 * Author: Jakob Bornecrantz <wallbraker@gmail.com> 28 * 29 */ 30 31#include <unistd.h> 32#include <string.h> 33#include <assert.h> 34#include <stdlib.h> 35#include <math.h> 36#include <stdint.h> 37 38#include "xorg-server.h" 39#include <xf86.h> 40#include <xf86i2c.h> 41#include <xf86Crtc.h> 42#include <cursorstr.h> 43#include "xorg_tracker.h" 44#include "xf86Modes.h" 45 46#ifdef HAVE_XEXTPROTO_71 47#include <X11/extensions/dpmsconst.h> 48#else 49#define DPMS_SERVER 50#include <X11/extensions/dpms.h> 51#endif 52 53#include "state_tracker/drm_driver.h" 54#include "util/u_inlines.h" 55#include "util/u_rect.h" 56 57#ifdef HAVE_LIBKMS 58#include "libkms.h" 59#endif 60 61struct crtc_private 62{ 63 drmModeCrtcPtr drm_crtc; 64 65 /* hwcursor */ 66 struct pipe_resource *cursor_tex; 67 struct kms_bo *cursor_bo; 68 69 unsigned cursor_handle; 70}; 71 72static void 73crtc_dpms(xf86CrtcPtr crtc, int mode) 74{ 75 /* ScrnInfoPtr pScrn = crtc->scrn; */ 76 77 switch (mode) { 78 case DPMSModeOn: 79 case DPMSModeStandby: 80 case DPMSModeSuspend: 81 break; 82 case DPMSModeOff: 83 break; 84 } 85} 86 87static Bool 88crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode, 89 Rotation rotation, int x, int y) 90{ 91 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn); 92 modesettingPtr ms = modesettingPTR(crtc->scrn); 93 xf86OutputPtr output = NULL; 94 struct crtc_private *crtcp = crtc->driver_private; 95 drmModeCrtcPtr drm_crtc = crtcp->drm_crtc; 96 drmModeModeInfo drm_mode; 97 int i, ret; 98 unsigned int connector_id; 99 100 for (i = 0; i < config->num_output; output = NULL, i++) { 101 output = config->output[i]; 102 103 if (output->crtc == crtc) 104 break; 105 } 106 107 if (!output) 108 return FALSE; 109 110 connector_id = xorg_output_get_id(output); 111 112 drm_mode.clock = mode->Clock; 113 drm_mode.hdisplay = mode->HDisplay; 114 drm_mode.hsync_start = mode->HSyncStart; 115 drm_mode.hsync_end = mode->HSyncEnd; 116 drm_mode.htotal = mode->HTotal; 117 drm_mode.vdisplay = mode->VDisplay; 118 drm_mode.vsync_start = mode->VSyncStart; 119 drm_mode.vsync_end = mode->VSyncEnd; 120 drm_mode.vtotal = mode->VTotal; 121 drm_mode.flags = mode->Flags; 122 drm_mode.hskew = mode->HSkew; 123 drm_mode.vscan = mode->VScan; 124 drm_mode.vrefresh = mode->VRefresh; 125 if (!mode->name) 126 xf86SetModeDefaultName(mode); 127 strncpy(drm_mode.name, mode->name, DRM_DISPLAY_MODE_LEN - 1); 128 drm_mode.name[DRM_DISPLAY_MODE_LEN - 1] = '\0'; 129 130 ret = drmModeSetCrtc(ms->fd, drm_crtc->crtc_id, ms->fb_id, x, y, 131 &connector_id, 1, &drm_mode); 132 133 if (ret) 134 return FALSE; 135 136 crtc->x = x; 137 crtc->y = y; 138 crtc->mode = *mode; 139 crtc->rotation = rotation; 140 141 return TRUE; 142} 143 144static void 145crtc_gamma_set(xf86CrtcPtr crtc, CARD16 * red, CARD16 * green, CARD16 * blue, 146 int size) 147{ 148 /* XXX: hockup */ 149} 150 151#if 0 /* Implement and enable to enable rotation and reflection. */ 152static void * 153crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height) 154{ 155 /* ScrnInfoPtr pScrn = crtc->scrn; */ 156 157 return NULL; 158} 159 160static PixmapPtr 161crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height) 162{ 163 /* ScrnInfoPtr pScrn = crtc->scrn; */ 164 165 return NULL; 166} 167 168static void 169crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data) 170{ 171 /* ScrnInfoPtr pScrn = crtc->scrn; */ 172} 173 174#endif 175 176/* 177 * Cursor functions 178 */ 179 180static void 181crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg) 182{ 183 /* XXX: See if this one is needed, as we only support ARGB cursors */ 184} 185 186static void 187crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y) 188{ 189 modesettingPtr ms = modesettingPTR(crtc->scrn); 190 struct crtc_private *crtcp = crtc->driver_private; 191 192 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y); 193} 194 195static void 196crtc_load_cursor_argb_ga3d(xf86CrtcPtr crtc, CARD32 * image) 197{ 198 unsigned char *ptr; 199 modesettingPtr ms = modesettingPTR(crtc->scrn); 200 struct crtc_private *crtcp = crtc->driver_private; 201 struct pipe_transfer *transfer; 202 203 if (!crtcp->cursor_tex) { 204 struct pipe_resource templat; 205 struct winsys_handle whandle; 206 207 memset(&templat, 0, sizeof(templat)); 208 templat.bind |= PIPE_BIND_RENDER_TARGET; 209 templat.bind |= PIPE_BIND_SCANOUT; 210 templat.target = PIPE_TEXTURE_2D; 211 templat.last_level = 0; 212 templat.depth0 = 1; 213 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM; 214 templat.width0 = 64; 215 templat.height0 = 64; 216 217 memset(&whandle, 0, sizeof(whandle)); 218 whandle.type = DRM_API_HANDLE_TYPE_KMS; 219 220 crtcp->cursor_tex = ms->screen->resource_create(ms->screen, 221 &templat); 222 ms->screen->resource_get_handle(ms->screen, crtcp->cursor_tex, &whandle); 223 224 crtcp->cursor_handle = whandle.handle; 225 } 226 227 transfer = pipe_get_transfer(ms->ctx, crtcp->cursor_tex, 228 0, 0, 0, 229 PIPE_TRANSFER_WRITE, 230 0, 0, 64, 64); 231 ptr = ms->ctx->transfer_map(ms->ctx, transfer); 232 util_copy_rect(ptr, crtcp->cursor_tex->format, 233 transfer->stride, 0, 0, 234 64, 64, (void*)image, 64 * 4, 0, 0); 235 ms->ctx->transfer_unmap(ms->ctx, transfer); 236 ms->ctx->transfer_destroy(ms->ctx, transfer); 237 238 if (crtc->cursor_shown) 239 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 240 crtcp->cursor_handle, 64, 64); 241} 242 243#if HAVE_LIBKMS 244static void 245crtc_load_cursor_argb_kms(xf86CrtcPtr crtc, CARD32 * image) 246{ 247 modesettingPtr ms = modesettingPTR(crtc->scrn); 248 struct crtc_private *crtcp = crtc->driver_private; 249 unsigned char *ptr; 250 251 if (!crtcp->cursor_bo) { 252 unsigned attr[8]; 253 254 attr[0] = KMS_BO_TYPE; 255#ifdef KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8 256 attr[1] = KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8; 257#else 258 attr[1] = KMS_BO_TYPE_CURSOR; 259#endif 260 attr[2] = KMS_WIDTH; 261 attr[3] = 64; 262 attr[4] = KMS_HEIGHT; 263 attr[5] = 64; 264 attr[6] = 0; 265 266 if (kms_bo_create(ms->kms, attr, &crtcp->cursor_bo)) 267 return; 268 269 if (kms_bo_get_prop(crtcp->cursor_bo, KMS_HANDLE, 270 &crtcp->cursor_handle)) 271 goto err_bo_destroy; 272 } 273 274 kms_bo_map(crtcp->cursor_bo, (void**)&ptr); 275 memcpy(ptr, image, 64*64*4); 276 kms_bo_unmap(crtcp->cursor_bo); 277 278 if (crtc->cursor_shown) 279 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 280 crtcp->cursor_handle, 64, 64); 281 282 return; 283 284err_bo_destroy: 285 kms_bo_destroy(&crtcp->cursor_bo); 286} 287#endif 288 289static void 290crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image) 291{ 292 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn); 293 modesettingPtr ms = modesettingPTR(crtc->scrn); 294 295 /* Older X servers have cursor reference counting bugs leading to use of 296 * freed memory and consequently random crashes. Should be fixed as of 297 * xserver 1.8, but this workaround shouldn't hurt anyway. 298 */ 299 if (config->cursor) 300 config->cursor->refcnt++; 301 302 if (ms->cursor) 303 FreeCursor(ms->cursor, None); 304 305 ms->cursor = config->cursor; 306 307 if (ms->screen) 308 crtc_load_cursor_argb_ga3d(crtc, image); 309#ifdef HAVE_LIBKMS 310 else if (ms->kms) 311 crtc_load_cursor_argb_kms(crtc, image); 312#endif 313} 314 315static void 316crtc_show_cursor(xf86CrtcPtr crtc) 317{ 318 modesettingPtr ms = modesettingPTR(crtc->scrn); 319 struct crtc_private *crtcp = crtc->driver_private; 320 321 if (crtcp->cursor_tex || crtcp->cursor_bo) 322 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 323 crtcp->cursor_handle, 64, 64); 324} 325 326static void 327crtc_hide_cursor(xf86CrtcPtr crtc) 328{ 329 modesettingPtr ms = modesettingPTR(crtc->scrn); 330 struct crtc_private *crtcp = crtc->driver_private; 331 332 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0); 333} 334 335/** 336 * Called at vt leave 337 */ 338void 339xorg_crtc_cursor_destroy(xf86CrtcPtr crtc) 340{ 341 struct crtc_private *crtcp = crtc->driver_private; 342 343 if (crtcp->cursor_tex) 344 pipe_resource_reference(&crtcp->cursor_tex, NULL); 345#ifdef HAVE_LIBKMS 346 if (crtcp->cursor_bo) 347 kms_bo_destroy(&crtcp->cursor_bo); 348#endif 349} 350 351/* 352 * Misc functions 353 */ 354 355static void 356crtc_destroy(xf86CrtcPtr crtc) 357{ 358 struct crtc_private *crtcp = crtc->driver_private; 359 360 xorg_crtc_cursor_destroy(crtc); 361 362 drmModeFreeCrtc(crtcp->drm_crtc); 363 364 free(crtcp); 365 crtc->driver_private = NULL; 366} 367 368static const xf86CrtcFuncsRec crtc_funcs = { 369 .dpms = crtc_dpms, 370 .set_mode_major = crtc_set_mode_major, 371 372 .set_cursor_colors = crtc_set_cursor_colors, 373 .set_cursor_position = crtc_set_cursor_position, 374 .show_cursor = crtc_show_cursor, 375 .hide_cursor = crtc_hide_cursor, 376 .load_cursor_argb = crtc_load_cursor_argb, 377 378 .shadow_create = NULL, 379 .shadow_allocate = NULL, 380 .shadow_destroy = NULL, 381 382 .gamma_set = crtc_gamma_set, 383 .destroy = crtc_destroy, 384}; 385 386void 387xorg_crtc_init(ScrnInfoPtr pScrn) 388{ 389 modesettingPtr ms = modesettingPTR(pScrn); 390 xf86CrtcPtr crtc; 391 drmModeResPtr res; 392 drmModeCrtcPtr drm_crtc = NULL; 393 struct crtc_private *crtcp; 394 int c; 395 396 res = drmModeGetResources(ms->fd); 397 if (res == 0) { 398 ErrorF("Failed drmModeGetResources %d\n", errno); 399 return; 400 } 401 402 for (c = 0; c < res->count_crtcs; c++) { 403 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]); 404 405 if (!drm_crtc) 406 continue; 407 408 crtc = xf86CrtcCreate(pScrn, &crtc_funcs); 409 if (crtc == NULL) 410 goto out; 411 412 crtcp = calloc(1, sizeof(struct crtc_private)); 413 if (!crtcp) { 414 xf86CrtcDestroy(crtc); 415 goto out; 416 } 417 418 crtcp->drm_crtc = drm_crtc; 419 420 crtc->driver_private = crtcp; 421 } 422 423 out: 424 drmModeFreeResources(res); 425} 426 427/* vim: set sw=4 ts=8 sts=4: */ 428