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