st/xorg vmwgfx/xorg: Add a pre-init customizer callback
[mesa.git] / src / gallium / targets / xorg-vmwgfx / vmw_screen.c
1 /**********************************************************
2 * Copyright 2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 /**
27 * @file
28 * Contains the init code for the VMware Xorg driver.
29 *
30 * @author Jakob Bornecrantz <jakob@vmware.com>
31 */
32
33 #include "vmw_hook.h"
34 #include "vmw_driver.h"
35 #include <pipe/p_context.h>
36
37 #include "cursorstr.h"
38
39 void vmw_winsys_screen_set_throttling(struct pipe_screen *screen,
40 uint32_t throttle_us);
41
42
43 /* modified version of crtc functions */
44 xf86CrtcFuncsRec vmw_screen_crtc_funcs;
45
46 static void
47 vmw_screen_cursor_load_argb(xf86CrtcPtr crtc, CARD32 *image)
48 {
49 struct vmw_customizer *vmw =
50 vmw_customizer(xorg_customizer(crtc->scrn));
51 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
52 xf86CrtcFuncsPtr funcs = vmw->cursor_priv;
53 CursorPtr c = config->cursor;
54
55 /* Run the ioctl before uploading the image */
56 vmw_ioctl_cursor_bypass(vmw, c->bits->xhot, c->bits->yhot);
57
58 funcs->load_cursor_argb(crtc, image);
59 }
60
61 static void
62 vmw_screen_cursor_init(struct vmw_customizer *vmw)
63 {
64 ScrnInfoPtr pScrn = vmw->pScrn;
65 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
66 int i;
67
68 /* XXX assume that all crtc's have the same function struct */
69
70 /* Save old struct need to call the old functions as well */
71 vmw->cursor_priv = (void*)(config->crtc[0]->funcs);
72 memcpy(&vmw_screen_crtc_funcs, vmw->cursor_priv, sizeof(xf86CrtcFuncsRec));
73 vmw_screen_crtc_funcs.load_cursor_argb = vmw_screen_cursor_load_argb;
74
75 for (i = 0; i < config->num_crtc; i++)
76 config->crtc[i]->funcs = &vmw_screen_crtc_funcs;
77 }
78
79 static void
80 vmw_screen_cursor_close(struct vmw_customizer *vmw)
81 {
82 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(vmw->pScrn);
83 int i;
84
85 vmw_ioctl_cursor_bypass(vmw, 0, 0);
86
87 for (i = 0; i < config->num_crtc; i++)
88 config->crtc[i]->funcs = vmw->cursor_priv;
89 }
90
91 static void
92 vmw_context_throttle(CustomizerPtr cust,
93 struct pipe_context *pipe,
94 enum xorg_throttling_reason reason)
95 {
96 switch (reason) {
97 case THROTTLE_RENDER:
98 vmw_winsys_screen_set_throttling(pipe->screen, 20000);
99 break;
100 default:
101 vmw_winsys_screen_set_throttling(pipe->screen, 0);
102 }
103 }
104
105 static void
106 vmw_context_no_throttle(CustomizerPtr cust,
107 struct pipe_context *pipe,
108 enum xorg_throttling_reason reason)
109 {
110 vmw_winsys_screen_set_throttling(pipe->screen, 0);
111 }
112
113 static Bool
114 vmw_pre_init(CustomizerPtr cust, int fd)
115 {
116 struct vmw_customizer *vmw = vmw_customizer(cust);
117
118 vmw->fd = fd;
119
120 return TRUE;
121 }
122
123 static Bool
124 vmw_screen_init(CustomizerPtr cust)
125 {
126 struct vmw_customizer *vmw = vmw_customizer(cust);
127 drmVersionPtr ver;
128
129 ver = drmGetVersion(vmw->fd);
130 if (ver == NULL ||
131 (ver->version_major == 1 && ver->version_minor < 1)) {
132 cust->swap_throttling = TRUE;
133 cust->dirty_throttling = TRUE;
134 cust->winsys_context_throttle = vmw_context_no_throttle;
135 } else {
136 cust->swap_throttling = TRUE;
137 cust->dirty_throttling = FALSE;
138 cust->winsys_context_throttle = vmw_context_throttle;
139 debug_printf("%s: Enabling kernel throttling.\n", __func__);
140 }
141
142 if (ver)
143 drmFreeVersion(ver);
144
145 vmw_screen_cursor_init(vmw);
146
147 vmw_ctrl_ext_init(vmw);
148
149 /* if gallium is used then we don't need to do anything more. */
150 if (xorg_has_gallium(vmw->pScrn))
151 return TRUE;
152
153 vmw_video_init(vmw);
154
155 return TRUE;
156 }
157
158 static Bool
159 vmw_screen_close(CustomizerPtr cust)
160 {
161 struct vmw_customizer *vmw = vmw_customizer(cust);
162
163 if (!vmw)
164 return TRUE;
165
166 vmw_screen_cursor_close(vmw);
167
168 vmw_video_close(vmw);
169
170 return TRUE;
171 }
172
173 static Bool
174 vmw_screen_enter_vt(CustomizerPtr cust)
175 {
176 debug_printf("%s: enter\n", __func__);
177
178 return TRUE;
179 }
180
181 static Bool
182 vmw_screen_leave_vt(CustomizerPtr cust)
183 {
184 struct vmw_customizer *vmw = vmw_customizer(cust);
185
186 debug_printf("%s: enter\n", __func__);
187
188 vmw_video_stop_all(vmw);
189
190 return TRUE;
191 }
192
193 /*
194 * Functions for setting up hooks into the xorg state tracker
195 */
196
197 static Bool (*vmw_screen_pre_init_saved)(ScrnInfoPtr pScrn, int flags) = NULL;
198
199 static Bool
200 vmw_screen_pre_init(ScrnInfoPtr pScrn, int flags)
201 {
202 struct vmw_customizer *vmw;
203 CustomizerPtr cust;
204
205 vmw = xnfcalloc(1, sizeof(*vmw));
206 if (!vmw)
207 return FALSE;
208
209 cust = &vmw->base;
210
211 cust->winsys_pre_init = vmw_pre_init;
212 cust->winsys_screen_init = vmw_screen_init;
213 cust->winsys_screen_close = vmw_screen_close;
214 cust->winsys_enter_vt = vmw_screen_enter_vt;
215 cust->winsys_leave_vt = vmw_screen_leave_vt;
216 cust->no_3d = TRUE;
217 vmw->pScrn = pScrn;
218
219 pScrn->driverPrivate = cust;
220
221 pScrn->PreInit = vmw_screen_pre_init_saved;
222 if (!pScrn->PreInit(pScrn, flags))
223 return FALSE;
224
225 return TRUE;
226 }
227
228 void
229 vmw_screen_set_functions(ScrnInfoPtr pScrn)
230 {
231 assert(!vmw_screen_pre_init_saved);
232
233 vmw_screen_pre_init_saved = pScrn->PreInit;
234 pScrn->PreInit = vmw_screen_pre_init;
235 }