Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / gallium / winsys / svga / drm / vmw_screen_dri.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 #include "pipe/p_compiler.h"
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_format.h"
31 #include "vmw_screen.h"
32
33 #include "vmw_screen.h"
34 #include "vmw_surface.h"
35 #include "vmw_fence.h"
36 #include "vmw_context.h"
37 #include "svga_drm_public.h"
38
39 #include "state_tracker/drm_driver.h"
40
41 #include "vmwgfx_drm.h"
42 #include <xf86drm.h>
43
44 #include <stdio.h>
45
46 struct dri1_api_version {
47 int major;
48 int minor;
49 int patch_level;
50 };
51
52 static struct svga_winsys_surface *
53 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
54 struct winsys_handle *whandle,
55 SVGA3dSurfaceFormat *format);
56 static boolean
57 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
58 struct svga_winsys_surface *surface,
59 unsigned stride,
60 struct winsys_handle *whandle);
61
62 static struct dri1_api_version drm_required = { 1, 0, 0 };
63 static struct dri1_api_version drm_compat = { 1, 0, 0 };
64 static struct dri1_api_version drm_scanout = { 0, 9, 0 };
65
66 static boolean
67 vmw_dri1_check_version(const struct dri1_api_version *cur,
68 const struct dri1_api_version *required,
69 const struct dri1_api_version *compat,
70 const char component[])
71 {
72 if (cur->major > required->major && cur->major <= compat->major)
73 return TRUE;
74 if (cur->major == required->major && cur->minor >= required->minor)
75 return TRUE;
76
77 fprintf(stderr, "%s version failure.\n", component);
78 fprintf(stderr, "%s version is %d.%d.%d and this driver can only work\n"
79 "with versions %d.%d.x through %d.x.x.\n",
80 component,
81 cur->major,
82 cur->minor,
83 cur->patch_level, required->major, required->minor, compat->major);
84 return FALSE;
85 }
86
87 /* This is actually the entrypoint to the entire driver,
88 * called by the target bootstrap code.
89 */
90 struct svga_winsys_screen *
91 svga_drm_winsys_screen_create(int fd)
92 {
93 struct vmw_winsys_screen *vws;
94 boolean use_old_scanout_flag = FALSE;
95
96 struct dri1_api_version drm_ver;
97 drmVersionPtr ver;
98
99 ver = drmGetVersion(fd);
100 if (ver == NULL)
101 return NULL;
102
103 drm_ver.major = ver->version_major;
104 drm_ver.minor = ver->version_minor;
105 drm_ver.patch_level = 0; /* ??? */
106
107 drmFreeVersion(ver);
108 if (!vmw_dri1_check_version(&drm_ver, &drm_required,
109 &drm_compat, "vmwgfx drm driver"))
110 return NULL;
111
112 if (!vmw_dri1_check_version(&drm_ver, &drm_scanout,
113 &drm_compat, "use old scanout field (not a error)"))
114 use_old_scanout_flag = TRUE;
115
116 vws = vmw_winsys_create( fd, use_old_scanout_flag );
117 if (!vws)
118 goto out_no_vws;
119
120 /* XXX do this properly */
121 vws->base.surface_from_handle = vmw_drm_surface_from_handle;
122 vws->base.surface_get_handle = vmw_drm_surface_get_handle;
123
124 return &vws->base;
125
126 out_no_vws:
127 return NULL;
128 }
129
130 static INLINE boolean
131 vmw_dri1_intersect_src_bbox(struct drm_clip_rect *dst,
132 int dst_x,
133 int dst_y,
134 const struct drm_clip_rect *src,
135 const struct drm_clip_rect *bbox)
136 {
137 int xy1;
138 int xy2;
139
140 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
141 (int)bbox->x1 + dst_x;
142 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
143 (int)bbox->x2 + dst_x;
144 if (xy1 >= xy2 || xy1 < 0)
145 return FALSE;
146
147 dst->x1 = xy1;
148 dst->x2 = xy2;
149
150 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
151 (int)bbox->y1 + dst_y;
152 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
153 (int)bbox->y2 + dst_y;
154 if (xy1 >= xy2 || xy1 < 0)
155 return FALSE;
156
157 dst->y1 = xy1;
158 dst->y2 = xy2;
159 return TRUE;
160 }
161
162 static struct svga_winsys_surface *
163 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
164 struct winsys_handle *whandle,
165 SVGA3dSurfaceFormat *format)
166 {
167 struct vmw_svga_winsys_surface *vsrf;
168 struct svga_winsys_surface *ssrf;
169 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
170 union drm_vmw_surface_reference_arg arg;
171 struct drm_vmw_surface_arg *req = &arg.req;
172 struct drm_vmw_surface_create_req *rep = &arg.rep;
173 int ret;
174 int i;
175
176 /**
177 * The vmware device specific handle is the hardware SID.
178 * FIXME: We probably want to move this to the ioctl implementations.
179 */
180
181 memset(&arg, 0, sizeof(arg));
182 req->sid = whandle->handle;
183
184 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,
185 &arg, sizeof(arg));
186
187 if (ret) {
188 fprintf(stderr, "Failed referencing shared surface. SID %d.\n"
189 "Error %d (%s).\n",
190 whandle->handle, ret, strerror(-ret));
191 return NULL;
192 }
193
194 if (rep->mip_levels[0] != 1) {
195 fprintf(stderr, "Incorrect number of mipmap levels on shared surface."
196 " SID %d, levels %d\n",
197 whandle->handle, rep->mip_levels[0]);
198 goto out_mip;
199 }
200
201 for (i=1; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
202 if (rep->mip_levels[i] != 0) {
203 fprintf(stderr, "Incorrect number of faces levels on shared surface."
204 " SID %d, face %d present.\n",
205 whandle->handle, i);
206 goto out_mip;
207 }
208 }
209
210 vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
211 if (!vsrf)
212 goto out_mip;
213
214 pipe_reference_init(&vsrf->refcnt, 1);
215 p_atomic_set(&vsrf->validated, 0);
216 vsrf->screen = vws;
217 vsrf->sid = whandle->handle;
218 ssrf = svga_winsys_surface(vsrf);
219 *format = rep->format;
220
221 return ssrf;
222
223 out_mip:
224 vmw_ioctl_surface_destroy(vws, whandle->handle);
225 return NULL;
226 }
227
228 static boolean
229 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
230 struct svga_winsys_surface *surface,
231 unsigned stride,
232 struct winsys_handle *whandle)
233 {
234 struct vmw_svga_winsys_surface *vsrf;
235
236 if (!surface)
237 return FALSE;
238
239 vsrf = vmw_svga_winsys_surface(surface);
240 whandle->handle = vsrf->sid;
241 whandle->stride = stride;
242
243 return TRUE;
244 }