Merge branch 'master' into glsl-pp-rework-2
[mesa.git] / src / gallium / state_trackers / xorg / xorg_crtc.c
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 "xorg_tracker.h"
43 #include "xf86Modes.h"
44
45 #ifdef HAVE_XEXTPROTO_71
46 #include <X11/extensions/dpmsconst.h>
47 #else
48 #define DPMS_SERVER
49 #include <X11/extensions/dpms.h>
50 #endif
51
52 #include "pipe/p_inlines.h"
53 #include "util/u_rect.h"
54
55 #ifdef HAVE_LIBKMS
56 #include "libkms.h"
57 #endif
58
59 struct crtc_private
60 {
61 drmModeCrtcPtr drm_crtc;
62
63 /* hwcursor */
64 struct pipe_texture *cursor_tex;
65 struct kms_bo *cursor_bo;
66
67 unsigned cursor_handle;
68 };
69
70 static void
71 crtc_dpms(xf86CrtcPtr crtc, int mode)
72 {
73 switch (mode) {
74 case DPMSModeOn:
75 case DPMSModeStandby:
76 case DPMSModeSuspend:
77 break;
78 case DPMSModeOff:
79 break;
80 }
81 }
82
83 static Bool
84 crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
85 Rotation rotation, int x, int y)
86 {
87 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
88 modesettingPtr ms = modesettingPTR(crtc->scrn);
89 xf86OutputPtr output = NULL;
90 drmModeConnectorPtr drm_connector;
91 struct crtc_private *crtcp = crtc->driver_private;
92 drmModeCrtcPtr drm_crtc = crtcp->drm_crtc;
93 drmModeModeInfo drm_mode;
94 int i, ret;
95
96 for (i = 0; i < config->num_output; output = NULL, i++) {
97 output = config->output[i];
98
99 if (output->crtc == crtc)
100 break;
101 }
102
103 if (!output)
104 return FALSE;
105
106 drm_connector = output->driver_private;
107
108 drm_mode.clock = mode->Clock;
109 drm_mode.hdisplay = mode->HDisplay;
110 drm_mode.hsync_start = mode->HSyncStart;
111 drm_mode.hsync_end = mode->HSyncEnd;
112 drm_mode.htotal = mode->HTotal;
113 drm_mode.vdisplay = mode->VDisplay;
114 drm_mode.vsync_start = mode->VSyncStart;
115 drm_mode.vsync_end = mode->VSyncEnd;
116 drm_mode.vtotal = mode->VTotal;
117 drm_mode.flags = mode->Flags;
118 drm_mode.hskew = mode->HSkew;
119 drm_mode.vscan = mode->VScan;
120 drm_mode.vrefresh = mode->VRefresh;
121 if (!mode->name)
122 xf86SetModeDefaultName(mode);
123 strncpy(drm_mode.name, mode->name, DRM_DISPLAY_MODE_LEN);
124
125 ret = drmModeSetCrtc(ms->fd, drm_crtc->crtc_id, ms->fb_id, x, y,
126 &drm_connector->connector_id, 1, &drm_mode);
127
128 if (ret)
129 return FALSE;
130
131 crtc->x = x;
132 crtc->y = y;
133 crtc->mode = *mode;
134 crtc->rotation = rotation;
135
136 return TRUE;
137 }
138
139 static void
140 crtc_gamma_set(xf86CrtcPtr crtc, CARD16 * red, CARD16 * green, CARD16 * blue,
141 int size)
142 {
143 /* XXX: hockup */
144 }
145
146 static void *
147 crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height)
148 {
149 return NULL;
150 }
151
152 static PixmapPtr
153 crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
154 {
155 return NULL;
156 }
157
158 static void
159 crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
160 {
161 }
162
163 /*
164 * Cursor functions
165 */
166
167 static void
168 crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
169 {
170 /* XXX: See if this one is needed, as we only support ARGB cursors */
171 }
172
173 static void
174 crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
175 {
176 modesettingPtr ms = modesettingPTR(crtc->scrn);
177 struct crtc_private *crtcp = crtc->driver_private;
178
179 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y);
180 }
181
182 static void
183 crtc_load_cursor_argb_ga3d(xf86CrtcPtr crtc, CARD32 * image)
184 {
185 unsigned char *ptr;
186 modesettingPtr ms = modesettingPTR(crtc->scrn);
187 struct crtc_private *crtcp = crtc->driver_private;
188 struct pipe_transfer *transfer;
189
190 if (!crtcp->cursor_tex) {
191 struct pipe_texture templat;
192 unsigned pitch;
193
194 memset(&templat, 0, sizeof(templat));
195 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
196 templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
197 templat.target = PIPE_TEXTURE_2D;
198 templat.last_level = 0;
199 templat.depth0 = 1;
200 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
201 templat.width0 = 64;
202 templat.height0 = 64;
203
204 crtcp->cursor_tex = ms->screen->texture_create(ms->screen,
205 &templat);
206 ms->api->local_handle_from_texture(ms->api,
207 ms->screen,
208 crtcp->cursor_tex,
209 &pitch,
210 &crtcp->cursor_handle);
211 }
212
213 transfer = ms->screen->get_tex_transfer(ms->screen, crtcp->cursor_tex,
214 0, 0, 0,
215 PIPE_TRANSFER_WRITE,
216 0, 0, 64, 64);
217 ptr = ms->screen->transfer_map(ms->screen, transfer);
218 util_copy_rect(ptr, crtcp->cursor_tex->format,
219 transfer->stride, 0, 0,
220 64, 64, (void*)image, 64 * 4, 0, 0);
221 ms->screen->transfer_unmap(ms->screen, transfer);
222 ms->screen->tex_transfer_destroy(transfer);
223 }
224
225 #if HAVE_LIBKMS
226 static void
227 crtc_load_cursor_argb_kms(xf86CrtcPtr crtc, CARD32 * image)
228 {
229 modesettingPtr ms = modesettingPTR(crtc->scrn);
230 struct crtc_private *crtcp = crtc->driver_private;
231 unsigned char *ptr;
232
233 if (!crtcp->cursor_bo) {
234 unsigned attr[8];
235
236 attr[0] = KMS_BO_TYPE;
237 attr[1] = KMS_BO_TYPE_CURSOR;
238 attr[2] = KMS_WIDTH;
239 attr[3] = 64;
240 attr[4] = KMS_HEIGHT;
241 attr[5] = 64;
242 attr[6] = 0;
243
244 if (kms_bo_create(ms->kms, attr, &crtcp->cursor_bo))
245 return;
246
247 if (kms_bo_get_prop(crtcp->cursor_bo, KMS_HANDLE,
248 &crtcp->cursor_handle))
249 goto err_bo_destroy;
250 }
251
252 kms_bo_map(crtcp->cursor_bo, (void**)&ptr);
253 memcpy(ptr, image, 64*64*4);
254 kms_bo_unmap(crtcp->cursor_bo);
255
256 return;
257
258 err_bo_destroy:
259 kms_bo_destroy(&crtcp->cursor_bo);
260 }
261 #endif
262
263 static void
264 crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image)
265 {
266 modesettingPtr ms = modesettingPTR(crtc->scrn);
267 if (ms->screen)
268 crtc_load_cursor_argb_ga3d(crtc, image);
269 #ifdef HAVE_LIBKMS
270 else if (ms->kms)
271 crtc_load_cursor_argb_kms(crtc, image);
272 #endif
273 }
274
275 static void
276 crtc_show_cursor(xf86CrtcPtr crtc)
277 {
278 modesettingPtr ms = modesettingPTR(crtc->scrn);
279 struct crtc_private *crtcp = crtc->driver_private;
280
281 if (crtcp->cursor_tex || crtcp->cursor_bo)
282 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id,
283 crtcp->cursor_handle, 64, 64);
284 }
285
286 static void
287 crtc_hide_cursor(xf86CrtcPtr crtc)
288 {
289 modesettingPtr ms = modesettingPTR(crtc->scrn);
290 struct crtc_private *crtcp = crtc->driver_private;
291
292 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0);
293 }
294
295 /**
296 * Called at vt leave
297 */
298 void
299 xorg_crtc_cursor_destroy(xf86CrtcPtr crtc)
300 {
301 struct crtc_private *crtcp = crtc->driver_private;
302
303 if (crtcp->cursor_tex)
304 pipe_texture_reference(&crtcp->cursor_tex, NULL);
305 #ifdef HAVE_LIBKMS
306 if (crtcp->cursor_bo)
307 kms_bo_destroy(&crtcp->cursor_bo);
308 #endif
309 }
310
311 /*
312 * Misc functions
313 */
314
315 static void
316 crtc_destroy(xf86CrtcPtr crtc)
317 {
318 struct crtc_private *crtcp = crtc->driver_private;
319
320 xorg_crtc_cursor_destroy(crtc);
321
322 drmModeFreeCrtc(crtcp->drm_crtc);
323
324 xfree(crtcp);
325 crtc->driver_private = NULL;
326 }
327
328 static const xf86CrtcFuncsRec crtc_funcs = {
329 .dpms = crtc_dpms,
330 .set_mode_major = crtc_set_mode_major,
331
332 .set_cursor_colors = crtc_set_cursor_colors,
333 .set_cursor_position = crtc_set_cursor_position,
334 .show_cursor = crtc_show_cursor,
335 .hide_cursor = crtc_hide_cursor,
336 .load_cursor_argb = crtc_load_cursor_argb,
337
338 .shadow_create = crtc_shadow_create,
339 .shadow_allocate = crtc_shadow_allocate,
340 .shadow_destroy = crtc_shadow_destroy,
341
342 .gamma_set = crtc_gamma_set,
343 .destroy = crtc_destroy,
344 };
345
346 void
347 xorg_crtc_init(ScrnInfoPtr pScrn)
348 {
349 modesettingPtr ms = modesettingPTR(pScrn);
350 xf86CrtcPtr crtc;
351 drmModeResPtr res;
352 drmModeCrtcPtr drm_crtc = NULL;
353 struct crtc_private *crtcp;
354 int c;
355
356 res = drmModeGetResources(ms->fd);
357 if (res == 0) {
358 ErrorF("Failed drmModeGetResources %d\n", errno);
359 return;
360 }
361
362 for (c = 0; c < res->count_crtcs; c++) {
363 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]);
364
365 if (!drm_crtc)
366 continue;
367
368 crtc = xf86CrtcCreate(pScrn, &crtc_funcs);
369 if (crtc == NULL)
370 goto out;
371
372 crtcp = xcalloc(1, sizeof(struct crtc_private));
373 if (!crtcp) {
374 xf86CrtcDestroy(crtc);
375 goto out;
376 }
377
378 crtcp->drm_crtc = drm_crtc;
379
380 crtc->driver_private = crtcp;
381 }
382
383 out:
384 drmModeFreeResources(res);
385 }
386
387 /* vim: set sw=4 ts=8 sts=4: */