2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
38 #include "xorg-server.h"
42 #include <cursorstr.h>
43 #include "xorg_tracker.h"
44 #include "xf86Modes.h"
46 #ifdef HAVE_XEXTPROTO_71
47 #include <X11/extensions/dpmsconst.h>
50 #include <X11/extensions/dpms.h>
53 #include "state_tracker/drm_driver.h"
54 #include "util/u_inlines.h"
55 #include "util/u_rect.h"
63 drmModeCrtcPtr drm_crtc
;
66 struct pipe_resource
*cursor_tex
;
67 struct kms_bo
*cursor_bo
;
69 unsigned cursor_handle
;
73 crtc_dpms(xf86CrtcPtr crtc
, int mode
)
75 /* ScrnInfoPtr pScrn = crtc->scrn; */
88 crtc_set_mode_major(xf86CrtcPtr crtc
, DisplayModePtr mode
,
89 Rotation rotation
, int x
, int y
)
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
;
98 unsigned int connector_id
;
100 for (i
= 0; i
< config
->num_output
; output
= NULL
, i
++) {
101 output
= config
->output
[i
];
103 if (output
->crtc
== crtc
)
110 connector_id
= xorg_output_get_id(output
);
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
;
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';
130 ret
= drmModeSetCrtc(ms
->fd
, drm_crtc
->crtc_id
, ms
->fb_id
, x
, y
,
131 &connector_id
, 1, &drm_mode
);
136 /* Only set gamma when needed, to avoid unneeded delays. */
137 #if defined(XF86_CRTC_VERSION) && XF86_CRTC_VERSION >= 3
138 if (!crtc
->active
&& crtc
->version
>= 3)
139 crtc
->funcs
->gamma_set(crtc
, crtc
->gamma_red
, crtc
->gamma_green
,
140 crtc
->gamma_blue
, crtc
->gamma_size
);
147 crtc
->rotation
= rotation
;
153 crtc_gamma_set(xf86CrtcPtr crtc
, CARD16
* red
, CARD16
* green
, CARD16
* blue
,
156 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
157 struct crtc_private
*crtcp
= crtc
->driver_private
;
159 drmModeCrtcSetGamma(ms
->fd
, crtcp
->drm_crtc
->crtc_id
, size
, red
, green
, blue
);
162 #if 0 /* Implement and enable to enable rotation and reflection. */
164 crtc_shadow_allocate(xf86CrtcPtr crtc
, int width
, int height
)
166 /* ScrnInfoPtr pScrn = crtc->scrn; */
172 crtc_shadow_create(xf86CrtcPtr crtc
, void *data
, int width
, int height
)
174 /* ScrnInfoPtr pScrn = crtc->scrn; */
180 crtc_shadow_destroy(xf86CrtcPtr crtc
, PixmapPtr rotate_pixmap
, void *data
)
182 /* ScrnInfoPtr pScrn = crtc->scrn; */
192 crtc_set_cursor_colors(xf86CrtcPtr crtc
, int bg
, int fg
)
194 /* XXX: See if this one is needed, as we only support ARGB cursors */
198 crtc_set_cursor_position(xf86CrtcPtr crtc
, int x
, int y
)
200 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
201 struct crtc_private
*crtcp
= crtc
->driver_private
;
203 drmModeMoveCursor(ms
->fd
, crtcp
->drm_crtc
->crtc_id
, x
, y
);
207 crtc_load_cursor_argb_ga3d(xf86CrtcPtr crtc
, CARD32
* image
)
210 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
211 struct crtc_private
*crtcp
= crtc
->driver_private
;
212 struct pipe_transfer
*transfer
;
213 struct pipe_fence_handle
*fence
= NULL
;
214 struct pipe_context
*ctx
= ms
->ctx
;
215 struct pipe_screen
*screen
= ms
->screen
;
217 if (!crtcp
->cursor_tex
) {
218 struct pipe_resource templat
;
219 struct winsys_handle whandle
;
221 memset(&templat
, 0, sizeof(templat
));
222 templat
.bind
|= PIPE_BIND_RENDER_TARGET
;
223 templat
.bind
|= PIPE_BIND_SCANOUT
;
224 templat
.bind
|= PIPE_BIND_CURSOR
;
225 templat
.target
= PIPE_TEXTURE_2D
;
226 templat
.last_level
= 0;
228 templat
.array_size
= 1;
229 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
231 templat
.height0
= 64;
233 memset(&whandle
, 0, sizeof(whandle
));
234 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
236 crtcp
->cursor_tex
= screen
->resource_create(screen
, &templat
);
237 screen
->resource_get_handle(screen
, crtcp
->cursor_tex
, &whandle
);
239 crtcp
->cursor_handle
= whandle
.handle
;
242 transfer
= pipe_get_transfer(ctx
, crtcp
->cursor_tex
,
246 ptr
= ctx
->transfer_map(ctx
, transfer
);
247 util_copy_rect(ptr
, crtcp
->cursor_tex
->format
,
248 transfer
->stride
, 0, 0,
249 64, 64, (void*)image
, 64 * 4, 0, 0);
250 ctx
->transfer_unmap(ctx
, transfer
);
251 ctx
->transfer_destroy(ctx
, transfer
);
252 ctx
->flush(ctx
, &fence
);
255 screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
256 screen
->fence_reference(screen
, &fence
, NULL
);
259 if (crtc
->cursor_shown
)
260 drmModeSetCursor(ms
->fd
, crtcp
->drm_crtc
->crtc_id
,
261 crtcp
->cursor_handle
, 64, 64);
266 crtc_load_cursor_argb_kms(xf86CrtcPtr crtc
, CARD32
* image
)
268 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
269 struct crtc_private
*crtcp
= crtc
->driver_private
;
272 if (!crtcp
->cursor_bo
) {
275 attr
[0] = KMS_BO_TYPE
;
276 #ifdef KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8
277 attr
[1] = KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8
;
279 attr
[1] = KMS_BO_TYPE_CURSOR
;
283 attr
[4] = KMS_HEIGHT
;
287 if (kms_bo_create(ms
->kms
, attr
, &crtcp
->cursor_bo
))
290 if (kms_bo_get_prop(crtcp
->cursor_bo
, KMS_HANDLE
,
291 &crtcp
->cursor_handle
))
295 kms_bo_map(crtcp
->cursor_bo
, (void**)&ptr
);
296 memcpy(ptr
, image
, 64*64*4);
297 kms_bo_unmap(crtcp
->cursor_bo
);
299 if (crtc
->cursor_shown
)
300 drmModeSetCursor(ms
->fd
, crtcp
->drm_crtc
->crtc_id
,
301 crtcp
->cursor_handle
, 64, 64);
306 kms_bo_destroy(&crtcp
->cursor_bo
);
311 crtc_load_cursor_argb(xf86CrtcPtr crtc
, CARD32
* image
)
313 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(crtc
->scrn
);
314 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
316 /* Older X servers have cursor reference counting bugs leading to use of
317 * freed memory and consequently random crashes. Should be fixed as of
318 * xserver 1.8, but this workaround shouldn't hurt anyway.
321 config
->cursor
->refcnt
++;
324 FreeCursor(ms
->cursor
, None
);
326 ms
->cursor
= config
->cursor
;
329 crtc_load_cursor_argb_ga3d(crtc
, image
);
332 crtc_load_cursor_argb_kms(crtc
, image
);
337 crtc_show_cursor(xf86CrtcPtr crtc
)
339 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
340 struct crtc_private
*crtcp
= crtc
->driver_private
;
342 if (crtcp
->cursor_tex
|| crtcp
->cursor_bo
)
343 drmModeSetCursor(ms
->fd
, crtcp
->drm_crtc
->crtc_id
,
344 crtcp
->cursor_handle
, 64, 64);
348 crtc_hide_cursor(xf86CrtcPtr crtc
)
350 modesettingPtr ms
= modesettingPTR(crtc
->scrn
);
351 struct crtc_private
*crtcp
= crtc
->driver_private
;
353 drmModeSetCursor(ms
->fd
, crtcp
->drm_crtc
->crtc_id
, 0, 0, 0);
360 xorg_crtc_cursor_destroy(xf86CrtcPtr crtc
)
362 struct crtc_private
*crtcp
= crtc
->driver_private
;
364 if (crtcp
->cursor_tex
)
365 pipe_resource_reference(&crtcp
->cursor_tex
, NULL
);
367 if (crtcp
->cursor_bo
)
368 kms_bo_destroy(&crtcp
->cursor_bo
);
377 crtc_destroy(xf86CrtcPtr crtc
)
379 struct crtc_private
*crtcp
= crtc
->driver_private
;
381 xorg_crtc_cursor_destroy(crtc
);
383 drmModeFreeCrtc(crtcp
->drm_crtc
);
386 crtc
->driver_private
= NULL
;
389 static const xf86CrtcFuncsRec crtc_funcs
= {
391 .set_mode_major
= crtc_set_mode_major
,
393 .set_cursor_colors
= crtc_set_cursor_colors
,
394 .set_cursor_position
= crtc_set_cursor_position
,
395 .show_cursor
= crtc_show_cursor
,
396 .hide_cursor
= crtc_hide_cursor
,
397 .load_cursor_argb
= crtc_load_cursor_argb
,
399 .shadow_create
= NULL
,
400 .shadow_allocate
= NULL
,
401 .shadow_destroy
= NULL
,
403 .gamma_set
= crtc_gamma_set
,
404 .destroy
= crtc_destroy
,
408 xorg_crtc_init(ScrnInfoPtr pScrn
)
410 modesettingPtr ms
= modesettingPTR(pScrn
);
413 drmModeCrtcPtr drm_crtc
= NULL
;
414 struct crtc_private
*crtcp
;
417 res
= drmModeGetResources(ms
->fd
);
419 ErrorF("Failed drmModeGetResources %d\n", errno
);
423 for (c
= 0; c
< res
->count_crtcs
; c
++) {
424 drm_crtc
= drmModeGetCrtc(ms
->fd
, res
->crtcs
[c
]);
429 crtc
= xf86CrtcCreate(pScrn
, &crtc_funcs
);
433 crtcp
= calloc(1, sizeof(struct crtc_private
));
435 xf86CrtcDestroy(crtc
);
439 crtcp
->drm_crtc
= drm_crtc
;
441 crtc
->driver_private
= crtcp
;
445 drmModeFreeResources(res
);
448 /* vim: set sw=4 ts=8 sts=4: */