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 "util/u_inlines.h"
54 #include "util/u_rect.h"
62 drmModeCrtcPtr drm_crtc;
65 struct pipe_resource *cursor_tex;
66 struct kms_bo *cursor_bo;
68 unsigned cursor_handle;
72 crtc_dpms(xf86CrtcPtr crtc, int mode)
74 /* ScrnInfoPtr pScrn = crtc->scrn; */
87 crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
88 Rotation rotation, int x, int y)
90 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
91 modesettingPtr ms = modesettingPTR(crtc->scrn);
92 xf86OutputPtr output = NULL;
93 drmModeConnectorPtr drm_connector;
94 struct crtc_private *crtcp = crtc->driver_private;
95 drmModeCrtcPtr drm_crtc = crtcp->drm_crtc;
96 drmModeModeInfo drm_mode;
99 for (i = 0; i < config->num_output; output = NULL, i++) {
100 output = config->output[i];
102 if (output->crtc == crtc)
109 drm_connector = output->driver_private;
111 drm_mode.clock = mode->Clock;
112 drm_mode.hdisplay = mode->HDisplay;
113 drm_mode.hsync_start = mode->HSyncStart;
114 drm_mode.hsync_end = mode->HSyncEnd;
115 drm_mode.htotal = mode->HTotal;
116 drm_mode.vdisplay = mode->VDisplay;
117 drm_mode.vsync_start = mode->VSyncStart;
118 drm_mode.vsync_end = mode->VSyncEnd;
119 drm_mode.vtotal = mode->VTotal;
120 drm_mode.flags = mode->Flags;
121 drm_mode.hskew = mode->HSkew;
122 drm_mode.vscan = mode->VScan;
123 drm_mode.vrefresh = mode->VRefresh;
125 xf86SetModeDefaultName(mode);
126 strncpy(drm_mode.name, mode->name, DRM_DISPLAY_MODE_LEN - 1);
127 drm_mode.name[DRM_DISPLAY_MODE_LEN - 1] = '\0';
129 ret = drmModeSetCrtc(ms->fd, drm_crtc->crtc_id, ms->fb_id, x, y,
130 &drm_connector->connector_id, 1, &drm_mode);
138 crtc->rotation = rotation;
144 crtc_gamma_set(xf86CrtcPtr crtc, CARD16 * red, CARD16 * green, CARD16 * blue,
151 crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height)
153 /* ScrnInfoPtr pScrn = crtc->scrn; */
159 crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
161 /* ScrnInfoPtr pScrn = crtc->scrn; */
167 crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
169 /* ScrnInfoPtr pScrn = crtc->scrn; */
177 crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
179 /* XXX: See if this one is needed, as we only support ARGB cursors */
183 crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
185 modesettingPtr ms = modesettingPTR(crtc->scrn);
186 struct crtc_private *crtcp = crtc->driver_private;
188 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y);
192 crtc_load_cursor_argb_ga3d(xf86CrtcPtr crtc, CARD32 * image)
195 modesettingPtr ms = modesettingPTR(crtc->scrn);
196 struct crtc_private *crtcp = crtc->driver_private;
197 struct pipe_transfer *transfer;
199 if (!crtcp->cursor_tex) {
200 struct pipe_resource templat;
201 struct winsys_handle whandle;
203 memset(&templat, 0, sizeof(templat));
204 templat.bind |= PIPE_BIND_RENDER_TARGET;
205 templat.bind |= PIPE_BIND_SCANOUT;
206 templat.target = PIPE_TEXTURE_2D;
207 templat.last_level = 0;
209 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
211 templat.height0 = 64;
213 memset(&whandle, 0, sizeof(whandle));
214 whandle.type = DRM_API_HANDLE_TYPE_KMS;
216 crtcp->cursor_tex = ms->screen->resource_create(ms->screen,
218 ms->screen->resource_get_handle(ms->screen, crtcp->cursor_tex, &whandle);
220 crtcp->cursor_handle = whandle.handle;
223 transfer = pipe_get_transfer(ms->ctx, crtcp->cursor_tex,
227 ptr = ms->ctx->transfer_map(ms->ctx, transfer);
228 util_copy_rect(ptr, crtcp->cursor_tex->format,
229 transfer->stride, 0, 0,
230 64, 64, (void*)image, 64 * 4, 0, 0);
231 ms->ctx->transfer_unmap(ms->ctx, transfer);
232 ms->ctx->transfer_destroy(ms->ctx, transfer);
237 crtc_load_cursor_argb_kms(xf86CrtcPtr crtc, CARD32 * image)
239 modesettingPtr ms = modesettingPTR(crtc->scrn);
240 struct crtc_private *crtcp = crtc->driver_private;
243 if (!crtcp->cursor_bo) {
246 attr[0] = KMS_BO_TYPE;
247 #ifdef KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8
248 attr[1] = KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
250 attr[1] = KMS_BO_TYPE_CURSOR;
254 attr[4] = KMS_HEIGHT;
258 if (kms_bo_create(ms->kms, attr, &crtcp->cursor_bo))
261 if (kms_bo_get_prop(crtcp->cursor_bo, KMS_HANDLE,
262 &crtcp->cursor_handle))
266 kms_bo_map(crtcp->cursor_bo, (void**)&ptr);
267 memcpy(ptr, image, 64*64*4);
268 kms_bo_unmap(crtcp->cursor_bo);
273 kms_bo_destroy(&crtcp->cursor_bo);
278 crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image)
280 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
281 modesettingPtr ms = modesettingPTR(crtc->scrn);
283 /* Older X servers have cursor reference counting bugs leading to use of
284 * freed memory and consequently random crashes. Should be fixed as of
285 * xserver 1.8, but this workaround shouldn't hurt anyway.
288 config->cursor->refcnt++;
291 FreeCursor(ms->cursor, None);
293 ms->cursor = config->cursor;
296 crtc_load_cursor_argb_ga3d(crtc, image);
299 crtc_load_cursor_argb_kms(crtc, image);
304 crtc_show_cursor(xf86CrtcPtr crtc)
306 modesettingPtr ms = modesettingPTR(crtc->scrn);
307 struct crtc_private *crtcp = crtc->driver_private;
309 if (crtcp->cursor_tex || crtcp->cursor_bo)
310 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id,
311 crtcp->cursor_handle, 64, 64);
315 crtc_hide_cursor(xf86CrtcPtr crtc)
317 modesettingPtr ms = modesettingPTR(crtc->scrn);
318 struct crtc_private *crtcp = crtc->driver_private;
320 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0);
327 xorg_crtc_cursor_destroy(xf86CrtcPtr crtc)
329 struct crtc_private *crtcp = crtc->driver_private;
331 if (crtcp->cursor_tex)
332 pipe_resource_reference(&crtcp->cursor_tex, NULL);
334 if (crtcp->cursor_bo)
335 kms_bo_destroy(&crtcp->cursor_bo);
344 crtc_destroy(xf86CrtcPtr crtc)
346 struct crtc_private *crtcp = crtc->driver_private;
348 xorg_crtc_cursor_destroy(crtc);
350 drmModeFreeCrtc(crtcp->drm_crtc);
353 crtc->driver_private = NULL;
356 static const xf86CrtcFuncsRec crtc_funcs = {
358 .set_mode_major = crtc_set_mode_major,
360 .set_cursor_colors = crtc_set_cursor_colors,
361 .set_cursor_position = crtc_set_cursor_position,
362 .show_cursor = crtc_show_cursor,
363 .hide_cursor = crtc_hide_cursor,
364 .load_cursor_argb = crtc_load_cursor_argb,
366 .shadow_create = crtc_shadow_create,
367 .shadow_allocate = crtc_shadow_allocate,
368 .shadow_destroy = crtc_shadow_destroy,
370 .gamma_set = crtc_gamma_set,
371 .destroy = crtc_destroy,
375 xorg_crtc_init(ScrnInfoPtr pScrn)
377 modesettingPtr ms = modesettingPTR(pScrn);
380 drmModeCrtcPtr drm_crtc = NULL;
381 struct crtc_private *crtcp;
384 res = drmModeGetResources(ms->fd);
386 ErrorF("Failed drmModeGetResources %d\n", errno);
390 for (c = 0; c < res->count_crtcs; c++) {
391 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]);
396 crtc = xf86CrtcCreate(pScrn, &crtc_funcs);
400 crtcp = xcalloc(1, sizeof(struct crtc_private));
402 xf86CrtcDestroy(crtc);
406 crtcp->drm_crtc = drm_crtc;
408 crtc->driver_private = crtcp;
412 drmModeFreeResources(res);
415 /* vim: set sw=4 ts=8 sts=4: */