st/xorg: Touch up xorg_crtc.c
[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 struct crtc_private
56 {
57 drmModeCrtcPtr drm_crtc;
58
59 /* hwcursor */
60 struct pipe_texture *cursor_tex;
61 unsigned cursor_handle;
62 };
63
64 static void
65 crtc_dpms(xf86CrtcPtr crtc, int mode)
66 {
67 switch (mode) {
68 case DPMSModeOn:
69 case DPMSModeStandby:
70 case DPMSModeSuspend:
71 break;
72 case DPMSModeOff:
73 break;
74 }
75 }
76
77 static Bool
78 crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
79 Rotation rotation, int x, int y)
80 {
81 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
82 modesettingPtr ms = modesettingPTR(crtc->scrn);
83 xf86OutputPtr output = NULL;
84 drmModeConnectorPtr drm_connector;
85 struct crtc_private *crtcp = crtc->driver_private;
86 drmModeCrtcPtr drm_crtc = crtcp->drm_crtc;
87 drmModeModeInfo drm_mode;
88 int i, ret;
89
90 for (i = 0; i < config->num_output; output = NULL, i++) {
91 output = config->output[i];
92
93 if (output->crtc == crtc)
94 break;
95 }
96
97 if (!output)
98 return FALSE;
99
100 drm_connector = output->driver_private;
101
102 drm_mode.clock = mode->Clock;
103 drm_mode.hdisplay = mode->HDisplay;
104 drm_mode.hsync_start = mode->HSyncStart;
105 drm_mode.hsync_end = mode->HSyncEnd;
106 drm_mode.htotal = mode->HTotal;
107 drm_mode.vdisplay = mode->VDisplay;
108 drm_mode.vsync_start = mode->VSyncStart;
109 drm_mode.vsync_end = mode->VSyncEnd;
110 drm_mode.vtotal = mode->VTotal;
111 drm_mode.flags = mode->Flags;
112 drm_mode.hskew = mode->HSkew;
113 drm_mode.vscan = mode->VScan;
114 drm_mode.vrefresh = mode->VRefresh;
115 if (!mode->name)
116 xf86SetModeDefaultName(mode);
117 strncpy(drm_mode.name, mode->name, DRM_DISPLAY_MODE_LEN);
118
119 ret = drmModeSetCrtc(ms->fd, drm_crtc->crtc_id, ms->fb_id, x, y,
120 &drm_connector->connector_id, 1, &drm_mode);
121
122 if (ret)
123 return FALSE;
124
125 crtc->x = x;
126 crtc->y = y;
127 crtc->mode = *mode;
128 crtc->rotation = rotation;
129
130 return TRUE;
131 }
132
133 static void
134 crtc_gamma_set(xf86CrtcPtr crtc, CARD16 * red, CARD16 * green, CARD16 * blue,
135 int size)
136 {
137 /* XXX: hockup */
138 }
139
140 static void *
141 crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height)
142 {
143 return NULL;
144 }
145
146 static PixmapPtr
147 crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
148 {
149 return NULL;
150 }
151
152 static void
153 crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
154 {
155 }
156
157 /*
158 * Cursor functions
159 */
160
161 static void
162 crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
163 {
164 /* XXX: See if this one is needed, as we only support ARGB cursors */
165 }
166
167 static void
168 crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
169 {
170 modesettingPtr ms = modesettingPTR(crtc->scrn);
171 struct crtc_private *crtcp = crtc->driver_private;
172
173 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y);
174 }
175
176 static void
177 crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image)
178 {
179 unsigned char *ptr;
180 modesettingPtr ms = modesettingPTR(crtc->scrn);
181 struct crtc_private *crtcp = crtc->driver_private;
182 struct pipe_transfer *transfer;
183
184 if (!crtcp->cursor_tex) {
185 struct pipe_texture templat;
186 unsigned pitch;
187
188 memset(&templat, 0, sizeof(templat));
189 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
190 templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
191 templat.target = PIPE_TEXTURE_2D;
192 templat.last_level = 0;
193 templat.depth[0] = 1;
194 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
195 templat.width[0] = 64;
196 templat.height[0] = 64;
197 pf_get_block(templat.format, &templat.block);
198
199 crtcp->cursor_tex = ms->screen->texture_create(ms->screen,
200 &templat);
201 ms->api->local_handle_from_texture(ms->api,
202 ms->screen,
203 crtcp->cursor_tex,
204 &pitch,
205 &crtcp->cursor_handle);
206 }
207
208 transfer = ms->screen->get_tex_transfer(ms->screen, crtcp->cursor_tex,
209 0, 0, 0,
210 PIPE_TRANSFER_WRITE,
211 0, 0, 64, 64);
212 ptr = ms->screen->transfer_map(ms->screen, transfer);
213 util_copy_rect(ptr, &crtcp->cursor_tex->block,
214 transfer->stride, 0, 0,
215 64, 64, (void*)image, 64 * 4, 0, 0);
216 ms->screen->transfer_unmap(ms->screen, transfer);
217 ms->screen->tex_transfer_destroy(transfer);
218 }
219
220 static void
221 crtc_show_cursor(xf86CrtcPtr crtc)
222 {
223 modesettingPtr ms = modesettingPTR(crtc->scrn);
224 struct crtc_private *crtcp = crtc->driver_private;
225
226 if (crtcp->cursor_tex)
227 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id,
228 crtcp->cursor_handle, 64, 64);
229 }
230
231 static void
232 crtc_hide_cursor(xf86CrtcPtr crtc)
233 {
234 modesettingPtr ms = modesettingPTR(crtc->scrn);
235 struct crtc_private *crtcp = crtc->driver_private;
236
237 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0);
238 }
239
240 /**
241 * Called at vt leave
242 */
243 void
244 xorg_crtc_cursor_destroy(xf86CrtcPtr crtc)
245 {
246 struct crtc_private *crtcp = crtc->driver_private;
247
248 if (crtcp->cursor_tex) {
249 pipe_texture_reference(&crtcp->cursor_tex, NULL);
250 }
251 }
252
253 /*
254 * Misc functions
255 */
256
257 static void
258 crtc_destroy(xf86CrtcPtr crtc)
259 {
260 struct crtc_private *crtcp = crtc->driver_private;
261
262 if (crtcp->cursor_tex)
263 pipe_texture_reference(&crtcp->cursor_tex, NULL);
264
265 drmModeFreeCrtc(crtcp->drm_crtc);
266 xfree(crtcp);
267 }
268
269 static const xf86CrtcFuncsRec crtc_funcs = {
270 .dpms = crtc_dpms,
271 .set_mode_major = crtc_set_mode_major,
272
273 .set_cursor_colors = crtc_set_cursor_colors,
274 .set_cursor_position = crtc_set_cursor_position,
275 .show_cursor = crtc_show_cursor,
276 .hide_cursor = crtc_hide_cursor,
277 .load_cursor_argb = crtc_load_cursor_argb,
278
279 .shadow_create = crtc_shadow_create,
280 .shadow_allocate = crtc_shadow_allocate,
281 .shadow_destroy = crtc_shadow_destroy,
282
283 .gamma_set = crtc_gamma_set,
284 .destroy = crtc_destroy,
285 };
286
287 void
288 xorg_crtc_init(ScrnInfoPtr pScrn)
289 {
290 modesettingPtr ms = modesettingPTR(pScrn);
291 xf86CrtcPtr crtc;
292 drmModeResPtr res;
293 drmModeCrtcPtr drm_crtc = NULL;
294 struct crtc_private *crtcp;
295 int c;
296
297 res = drmModeGetResources(ms->fd);
298 if (res == 0) {
299 ErrorF("Failed drmModeGetResources %d\n", errno);
300 return;
301 }
302
303 for (c = 0; c < res->count_crtcs; c++) {
304 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]);
305
306 if (!drm_crtc)
307 continue;
308
309 crtc = xf86CrtcCreate(pScrn, &crtc_funcs);
310 if (crtc == NULL)
311 goto out;
312
313 crtcp = xcalloc(1, sizeof(struct crtc_private));
314 if (!crtcp) {
315 xf86CrtcDestroy(crtc);
316 goto out;
317 }
318
319 crtcp->drm_crtc = drm_crtc;
320
321 crtc->driver_private = crtcp;
322 }
323
324 out:
325 drmModeFreeResources(res);
326 }
327
328 /* vim: set sw=4 ts=8 sts=4: */