Merge remote branch 'origin/mesa_7_6_branch'
[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 }
138
139 static void *
140 crtc_shadow_allocate(xf86CrtcPtr crtc, int width, int height)
141 {
142 return NULL;
143 }
144
145 static PixmapPtr
146 crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
147 {
148 return NULL;
149 }
150
151 static void
152 crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
153 {
154 }
155
156 /*
157 * Cursor functions
158 */
159
160 static void
161 crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
162 {
163 }
164
165 static void
166 crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
167 {
168 modesettingPtr ms = modesettingPTR(crtc->scrn);
169 struct crtc_private *crtcp = crtc->driver_private;
170
171 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y);
172 }
173 static void
174 crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image)
175 {
176 unsigned char *ptr;
177 modesettingPtr ms = modesettingPTR(crtc->scrn);
178 struct crtc_private *crtcp = crtc->driver_private;
179 struct pipe_transfer *transfer;
180
181 if (!crtcp->cursor_tex) {
182 struct pipe_texture templat;
183 unsigned pitch;
184
185 memset(&templat, 0, sizeof(templat));
186 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
187 templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
188 templat.target = PIPE_TEXTURE_2D;
189 templat.last_level = 0;
190 templat.depth[0] = 1;
191 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
192 templat.width[0] = 64;
193 templat.height[0] = 64;
194 pf_get_block(templat.format, &templat.block);
195
196 crtcp->cursor_tex = ms->screen->texture_create(ms->screen,
197 &templat);
198 ms->api->local_handle_from_texture(ms->api,
199 ms->screen,
200 crtcp->cursor_tex,
201 &pitch,
202 &crtcp->cursor_handle);
203 }
204
205 transfer = ms->screen->get_tex_transfer(ms->screen, crtcp->cursor_tex,
206 0, 0, 0,
207 PIPE_TRANSFER_WRITE,
208 0, 0, 64, 64);
209 ptr = ms->screen->transfer_map(ms->screen, transfer);
210 util_copy_rect(ptr, &crtcp->cursor_tex->block,
211 transfer->stride, 0, 0,
212 64, 64, (void*)image, 64 * 4, 0, 0);
213 ms->screen->transfer_unmap(ms->screen, transfer);
214 ms->screen->tex_transfer_destroy(transfer);
215 }
216
217 static void
218 crtc_show_cursor(xf86CrtcPtr crtc)
219 {
220 modesettingPtr ms = modesettingPTR(crtc->scrn);
221 struct crtc_private *crtcp = crtc->driver_private;
222
223 if (crtcp->cursor_tex)
224 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id,
225 crtcp->cursor_handle, 64, 64);
226 }
227
228 static void
229 crtc_hide_cursor(xf86CrtcPtr crtc)
230 {
231 modesettingPtr ms = modesettingPTR(crtc->scrn);
232 struct crtc_private *crtcp = crtc->driver_private;
233
234 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0);
235 }
236
237 void
238 crtc_cursor_destroy(xf86CrtcPtr crtc)
239 {
240 struct crtc_private *crtcp = crtc->driver_private;
241
242 if (crtcp->cursor_tex) {
243 pipe_texture_reference(&crtcp->cursor_tex, NULL);
244 }
245 }
246
247 /*
248 * Misc functions
249 */
250
251 static void
252 crtc_destroy(xf86CrtcPtr crtc)
253 {
254 struct crtc_private *crtcp = crtc->driver_private;
255
256 if (crtcp->cursor_tex)
257 pipe_texture_reference(&crtcp->cursor_tex, NULL);
258
259 drmModeFreeCrtc(crtcp->drm_crtc);
260 xfree(crtcp);
261 }
262
263 static const xf86CrtcFuncsRec crtc_funcs = {
264 .dpms = crtc_dpms,
265 .set_mode_major = crtc_set_mode_major,
266
267 .set_cursor_colors = crtc_set_cursor_colors,
268 .set_cursor_position = crtc_set_cursor_position,
269 .show_cursor = crtc_show_cursor,
270 .hide_cursor = crtc_hide_cursor,
271 .load_cursor_argb = crtc_load_cursor_argb,
272
273 .shadow_create = crtc_shadow_create,
274 .shadow_allocate = crtc_shadow_allocate,
275 .shadow_destroy = crtc_shadow_destroy,
276
277 .gamma_set = crtc_gamma_set,
278 .destroy = crtc_destroy,
279 };
280
281 void
282 crtc_init(ScrnInfoPtr pScrn)
283 {
284 modesettingPtr ms = modesettingPTR(pScrn);
285 xf86CrtcPtr crtc;
286 drmModeResPtr res;
287 drmModeCrtcPtr drm_crtc = NULL;
288 struct crtc_private *crtcp;
289 int c;
290
291 res = drmModeGetResources(ms->fd);
292 if (res == 0) {
293 ErrorF("Failed drmModeGetResources %d\n", errno);
294 return;
295 }
296
297 for (c = 0; c < res->count_crtcs; c++) {
298 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]);
299
300 if (!drm_crtc)
301 continue;
302
303 crtc = xf86CrtcCreate(pScrn, &crtc_funcs);
304 if (crtc == NULL)
305 goto out;
306
307 crtcp = xcalloc(1, sizeof(struct crtc_private));
308 if (!crtcp) {
309 xf86CrtcDestroy(crtc);
310 goto out;
311 }
312
313 crtcp->drm_crtc = drm_crtc;
314
315 crtc->driver_private = crtcp;
316 }
317
318 out:
319 drmModeFreeResources(res);
320 }
321
322 /* vim: set sw=4 ts=8 sts=4: */