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