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