Tizen 2.0 Release
[profile/ivi/osmesa.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 #include "../../winsys/svga/drm/vmwgfx_drm.h"
39
40 void vmw_winsys_screen_set_throttling(struct pipe_screen *screen,
41                                       uint32_t throttle_us);
42
43
44 /* modified version of crtc functions */
45 xf86CrtcFuncsRec vmw_screen_crtc_funcs;
46
47 static void
48 vmw_screen_cursor_load_argb(xf86CrtcPtr crtc, CARD32 *image)
49 {
50     struct vmw_customizer *vmw =
51         vmw_customizer(xorg_customizer(crtc->scrn));
52     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
53     xf86CrtcFuncsPtr funcs = vmw->cursor_priv;
54     CursorPtr c = config->cursor;
55
56     /* Run the ioctl before uploading the image */
57     vmw_ioctl_cursor_bypass(vmw, c->bits->xhot, c->bits->yhot);
58
59     funcs->load_cursor_argb(crtc, image);
60 }
61
62 static void
63 vmw_screen_cursor_init(struct vmw_customizer *vmw)
64 {
65     ScrnInfoPtr pScrn = vmw->pScrn;
66     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
67     int i;
68
69     /* XXX assume that all crtc's have the same function struct */
70
71     /* Save old struct need to call the old functions as well */
72     vmw->cursor_priv = (void*)(config->crtc[0]->funcs);
73     memcpy(&vmw_screen_crtc_funcs, vmw->cursor_priv, sizeof(xf86CrtcFuncsRec));
74     vmw_screen_crtc_funcs.load_cursor_argb = vmw_screen_cursor_load_argb;
75
76     for (i = 0; i < config->num_crtc; i++)
77         config->crtc[i]->funcs = &vmw_screen_crtc_funcs;
78 }
79
80 static void
81 vmw_screen_cursor_close(struct vmw_customizer *vmw)
82 {
83     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(vmw->pScrn);
84     int i;
85
86     vmw_ioctl_cursor_bypass(vmw, 0, 0);
87
88     for (i = 0; i < config->num_crtc; i++)
89         config->crtc[i]->funcs = vmw->cursor_priv;
90 }
91
92 static void
93 vmw_context_throttle(CustomizerPtr cust,
94                      struct pipe_context *pipe,
95                      enum xorg_throttling_reason reason)
96 {
97     switch (reason) {
98     case THROTTLE_RENDER:
99         vmw_winsys_screen_set_throttling(pipe->screen, 20000);
100         break;
101     default:
102       vmw_winsys_screen_set_throttling(pipe->screen, 0);
103     }
104 }
105
106 static void
107 vmw_context_no_throttle(CustomizerPtr cust,
108                      struct pipe_context *pipe,
109                      enum xorg_throttling_reason reason)
110 {
111     vmw_winsys_screen_set_throttling(pipe->screen, 0);
112 }
113
114 static Bool
115 vmw_check_fb_size(CustomizerPtr cust,
116                   unsigned long pitch,
117                   unsigned long height)
118 {
119     struct vmw_customizer *vmw = vmw_customizer(cust);
120
121     /**
122      *  1) Is there a pitch alignment?
123      *  2) The 1024 byte pad is an arbitrary value to be on
124      */
125
126     return ((uint64_t) pitch * height + 1024ULL < vmw->max_fb_size);
127 }
128
129 static Bool
130 vmw_pre_init(CustomizerPtr cust, int fd)
131 {
132     struct vmw_customizer *vmw = vmw_customizer(cust);
133     drmVersionPtr ver;
134
135     vmw->fd = fd;
136
137     ver = drmGetVersion(vmw->fd);
138     if (ver == NULL ||
139         (ver->version_major == 1 && ver->version_minor < 1)) {
140         cust->swap_throttling = TRUE;
141         cust->dirty_throttling = TRUE;
142         cust->winsys_context_throttle = vmw_context_no_throttle;
143     } else {
144         cust->swap_throttling = TRUE;
145         cust->dirty_throttling = FALSE;
146         cust->winsys_context_throttle = vmw_context_throttle;
147         debug_printf("%s: Enabling kernel throttling.\n", __func__);
148
149         if (ver->version_major > 1 ||
150             (ver->version_major == 1 && ver->version_minor >= 3)) {
151             struct drm_vmw_getparam_arg arg;
152             int ret;
153
154             arg.param = DRM_VMW_PARAM_MAX_FB_SIZE;
155             ret = drmCommandWriteRead(fd, DRM_VMW_GET_PARAM, &arg,
156                                       sizeof(arg));
157             if (!ret) {
158                 vmw->max_fb_size = arg.value;
159                 cust->winsys_check_fb_size = vmw_check_fb_size;
160                 debug_printf("%s: Enabling fb size check.\n", __func__);
161             }
162         }
163     }
164
165     if (ver)
166         drmFreeVersion(ver);
167
168     return TRUE;
169 }
170
171 static Bool
172 vmw_screen_init(CustomizerPtr cust)
173 {
174     struct vmw_customizer *vmw = vmw_customizer(cust);
175
176     vmw_screen_cursor_init(vmw);
177
178     vmw_ctrl_ext_init(vmw);
179
180     /* if gallium is used then we don't need to do anything more. */
181     if (xorg_has_gallium(vmw->pScrn))
182         return TRUE;
183
184     vmw_video_init(vmw);
185
186     return TRUE;
187 }
188
189 static Bool
190 vmw_screen_close(CustomizerPtr cust)
191 {
192     struct vmw_customizer *vmw = vmw_customizer(cust);
193
194     if (!vmw)
195         return TRUE;
196
197     vmw_screen_cursor_close(vmw);
198
199     vmw_video_close(vmw);
200
201     return TRUE;
202 }
203
204 static Bool
205 vmw_screen_enter_vt(CustomizerPtr cust)
206 {
207     debug_printf("%s: enter\n", __func__);
208
209     return TRUE;
210 }
211
212 static Bool
213 vmw_screen_leave_vt(CustomizerPtr cust)
214 {
215     struct vmw_customizer *vmw = vmw_customizer(cust);
216
217     debug_printf("%s: enter\n", __func__);
218
219     vmw_video_stop_all(vmw);
220
221     return TRUE;
222 }
223
224 /*
225  * Functions for setting up hooks into the xorg state tracker
226  */
227
228 static Bool (*vmw_screen_pre_init_saved)(ScrnInfoPtr pScrn, int flags) = NULL;
229
230 static Bool
231 vmw_screen_pre_init(ScrnInfoPtr pScrn, int flags)
232 {
233     struct vmw_customizer *vmw;
234     CustomizerPtr cust;
235
236     vmw = xnfcalloc(1, sizeof(*vmw));
237     if (!vmw)
238         return FALSE;
239
240     cust = &vmw->base;
241
242     cust->winsys_pre_init = vmw_pre_init;
243     cust->winsys_screen_init = vmw_screen_init;
244     cust->winsys_screen_close = vmw_screen_close;
245     cust->winsys_enter_vt = vmw_screen_enter_vt;
246     cust->winsys_leave_vt = vmw_screen_leave_vt;
247     cust->no_3d = TRUE;
248     cust->unhidden_hw_cursor_update = TRUE;
249     vmw->pScrn = pScrn;
250
251     pScrn->driverPrivate = cust;
252
253     pScrn->PreInit = vmw_screen_pre_init_saved;
254     if (!pScrn->PreInit(pScrn, flags))
255         return FALSE;
256
257     return TRUE;
258 }
259
260 void
261 vmw_screen_set_functions(ScrnInfoPtr pScrn)
262 {
263     assert(!vmw_screen_pre_init_saved);
264
265     vmw_screen_pre_init_saved = pScrn->PreInit;
266     pScrn->PreInit = vmw_screen_pre_init;
267 }