st/xorg: Clean up cursor functions a bit
[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 /*
177 * Cursor functions
178 */
179
180 static void
181 crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
182 {
183 }
184
185 static void
186 crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
187 {
188 modesettingPtr ms = modesettingPTR(crtc->scrn);
189 struct crtc_private *crtcp = crtc->driver_private;
190
191 drmModeMoveCursor(ms->fd, crtcp->drm_crtc->crtc_id, x, y);
192 }
193 static void
194 crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 * image)
195 {
196 unsigned char *ptr;
197 modesettingPtr ms = modesettingPTR(crtc->scrn);
198 struct crtc_private *crtcp = crtc->driver_private;
199 struct pipe_transfer *transfer;
200
201 if (!crtcp->cursor_tex) {
202 struct pipe_texture templat;
203 unsigned pitch;
204
205 memset(&templat, 0, sizeof(templat));
206 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
207 templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
208 templat.target = PIPE_TEXTURE_2D;
209 templat.last_level = 0;
210 templat.depth[0] = 1;
211 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
212 templat.width[0] = 64;
213 templat.height[0] = 64;
214 pf_get_block(templat.format, &templat.block);
215
216 crtcp->cursor_tex = ms->screen->texture_create(ms->screen,
217 &templat);
218 ms->api->local_handle_from_texture(ms->api,
219 ms->screen,
220 crtcp->cursor_tex,
221 &pitch,
222 &crtcp->cursor_handle);
223 }
224
225 transfer = ms->screen->get_tex_transfer(ms->screen, crtcp->cursor_tex,
226 0, 0, 0,
227 PIPE_TRANSFER_WRITE,
228 0, 0, 64, 64);
229 ptr = ms->screen->transfer_map(ms->screen, transfer);
230 util_copy_rect(ptr, &crtcp->cursor_tex->block,
231 transfer->stride, 0, 0,
232 64, 64, (void*)image, 64 * 4, 0, 0);
233 ms->screen->transfer_unmap(ms->screen, transfer);
234 ms->screen->tex_transfer_destroy(transfer);
235 }
236
237 static void
238 crtc_show_cursor(xf86CrtcPtr crtc)
239 {
240 modesettingPtr ms = modesettingPTR(crtc->scrn);
241 struct crtc_private *crtcp = crtc->driver_private;
242
243 if (crtcp->cursor_tex)
244 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id,
245 crtcp->cursor_handle, 64, 64);
246 }
247
248 static void
249 crtc_hide_cursor(xf86CrtcPtr crtc)
250 {
251 modesettingPtr ms = modesettingPTR(crtc->scrn);
252 struct crtc_private *crtcp = crtc->driver_private;
253
254 drmModeSetCursor(ms->fd, crtcp->drm_crtc->crtc_id, 0, 0, 0);
255 }
256
257 void
258 crtc_cursor_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 }
266
267 /*
268 * Misc functions
269 */
270
271 static void
272 crtc_destroy(xf86CrtcPtr crtc)
273 {
274 struct crtc_private *crtcp = crtc->driver_private;
275
276 if (crtcp->cursor_tex)
277 pipe_texture_reference(&crtcp->cursor_tex, NULL);
278
279 drmModeFreeCrtc(crtcp->drm_crtc);
280 xfree(crtcp);
281 }
282
283 static const xf86CrtcFuncsRec crtc_funcs = {
284 .dpms = crtc_dpms,
285
286 .lock = crtc_lock,
287 .unlock = crtc_unlock,
288 .mode_fixup = crtc_mode_fixup,
289 .prepare = crtc_prepare,
290 .mode_set = crtc_mode_set,
291 .commit = crtc_commit,
292
293 .set_cursor_colors = crtc_set_cursor_colors,
294 .set_cursor_position = crtc_set_cursor_position,
295 .show_cursor = crtc_show_cursor,
296 .hide_cursor = crtc_hide_cursor,
297 .load_cursor_argb = crtc_load_cursor_argb,
298
299 .shadow_create = crtc_shadow_create,
300 .shadow_allocate = crtc_shadow_allocate,
301 .shadow_destroy = crtc_shadow_destroy,
302
303 .gamma_set = crtc_gamma_set,
304 .destroy = crtc_destroy,
305 };
306
307 void
308 crtc_init(ScrnInfoPtr pScrn)
309 {
310 modesettingPtr ms = modesettingPTR(pScrn);
311 xf86CrtcPtr crtc;
312 drmModeResPtr res;
313 drmModeCrtcPtr drm_crtc = NULL;
314 struct crtc_private *crtcp;
315 int c;
316
317 res = drmModeGetResources(ms->fd);
318 if (res == 0) {
319 ErrorF("Failed drmModeGetResources %d\n", errno);
320 return;
321 }
322
323 for (c = 0; c < res->count_crtcs; c++) {
324 drm_crtc = drmModeGetCrtc(ms->fd, res->crtcs[c]);
325 if (!drm_crtc)
326 continue;
327
328 crtc = xf86CrtcCreate(pScrn, &crtc_funcs);
329 if (crtc == NULL)
330 goto out;
331
332 crtcp = xcalloc(1, sizeof(struct crtc_private));
333 if (!crtcp) {
334 xf86CrtcDestroy(crtc);
335 goto out;
336 }
337
338 crtcp->drm_crtc = drm_crtc;
339
340 crtc->driver_private = crtcp;
341
342 }
343
344 out:
345 drmModeFreeResources(res);
346 }
347
348 /* vim: set sw=4 ts=8 sts=4: */