svga/winsys: Implement surface sharing using prime fd handles
[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
32 #include "vmw_context.h"
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 #include <fcntl.h>
44
45 struct dri1_api_version {
46 int major;
47 int minor;
48 int patch_level;
49 };
50
51 static struct svga_winsys_surface *
52 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
53 struct winsys_handle *whandle,
54 SVGA3dSurfaceFormat *format);
55 static boolean
56 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
57 struct svga_winsys_surface *surface,
58 unsigned stride,
59 struct winsys_handle *whandle);
60
61 static struct dri1_api_version drm_required = { 2, 1, 0 };
62 static struct dri1_api_version drm_compat = { 2, 0, 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 vmw_error("%s version failure.\n", component);
76 vmw_error("%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, cur->minor, cur->patch_level,
80 required->major, required->minor, compat->major);
81 return FALSE;
82 }
83
84 /* This is actually the entrypoint to the entire driver,
85 * called by the target bootstrap code.
86 */
87 struct svga_winsys_screen *
88 svga_drm_winsys_screen_create(int fd)
89 {
90 struct vmw_winsys_screen *vws;
91 struct dri1_api_version drm_ver;
92 drmVersionPtr ver;
93
94 ver = drmGetVersion(fd);
95 if (ver == NULL)
96 return NULL;
97
98 drm_ver.major = ver->version_major;
99 drm_ver.minor = ver->version_minor;
100 drm_ver.patch_level = 0; /* ??? */
101
102 drmFreeVersion(ver);
103 if (!vmw_dri1_check_version(&drm_ver, &drm_required,
104 &drm_compat, "vmwgfx drm driver"))
105 return NULL;
106
107 vws = vmw_winsys_create( fd, FALSE );
108 if (!vws)
109 goto out_no_vws;
110
111 /* XXX do this properly */
112 vws->base.surface_from_handle = vmw_drm_surface_from_handle;
113 vws->base.surface_get_handle = vmw_drm_surface_get_handle;
114
115 return &vws->base;
116
117 out_no_vws:
118 return NULL;
119 }
120
121 static INLINE boolean
122 vmw_dri1_intersect_src_bbox(struct drm_clip_rect *dst,
123 int dst_x,
124 int dst_y,
125 const struct drm_clip_rect *src,
126 const struct drm_clip_rect *bbox)
127 {
128 int xy1;
129 int xy2;
130
131 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
132 (int)bbox->x1 + dst_x;
133 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
134 (int)bbox->x2 + dst_x;
135 if (xy1 >= xy2 || xy1 < 0)
136 return FALSE;
137
138 dst->x1 = xy1;
139 dst->x2 = xy2;
140
141 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
142 (int)bbox->y1 + dst_y;
143 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
144 (int)bbox->y2 + dst_y;
145 if (xy1 >= xy2 || xy1 < 0)
146 return FALSE;
147
148 dst->y1 = xy1;
149 dst->y2 = xy2;
150 return TRUE;
151 }
152
153 static struct svga_winsys_surface *
154 vmw_drm_surface_from_handle(struct svga_winsys_screen *sws,
155 struct winsys_handle *whandle,
156 SVGA3dSurfaceFormat *format)
157 {
158 struct vmw_svga_winsys_surface *vsrf;
159 struct svga_winsys_surface *ssrf;
160 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
161 union drm_vmw_surface_reference_arg arg;
162 struct drm_vmw_surface_arg *req = &arg.req;
163 struct drm_vmw_surface_create_req *rep = &arg.rep;
164 uint32_t handle = 0;
165 int ret;
166 int i;
167
168 switch (whandle->type) {
169 case DRM_API_HANDLE_TYPE_SHARED:
170 case DRM_API_HANDLE_TYPE_KMS:
171 handle = whandle->handle;
172 break;
173 case DRM_API_HANDLE_TYPE_FD:
174 ret = drmPrimeFDToHandle(vws->ioctl.drm_fd, whandle->handle,
175 &handle);
176 if (ret) {
177 vmw_error("Failed to get handle from prime fd %d.\n",
178 (int) whandle->handle);
179 return NULL;
180 }
181 break;
182 default:
183 vmw_error("Attempt to import unsupported handle type %d.\n",
184 whandle->type);
185 return NULL;
186 }
187
188 memset(&arg, 0, sizeof(arg));
189 req->sid = handle;
190
191 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,
192 &arg, sizeof(arg));
193
194 /*
195 * Need to close the handle we got from prime.
196 */
197 if (whandle->type == DRM_API_HANDLE_TYPE_FD)
198 vmw_ioctl_surface_destroy(vws, handle);
199
200 if (ret) {
201 /*
202 * Any attempt to share something other than a surface, like a dumb
203 * kms buffer, should fail here.
204 */
205 vmw_error("Failed referencing shared surface. SID %d.\n"
206 "Error %d (%s).\n",
207 handle, ret, strerror(-ret));
208 return NULL;
209 }
210
211 if (rep->mip_levels[0] != 1) {
212 vmw_error("Incorrect number of mipmap levels on shared surface."
213 " SID %d, levels %d\n",
214 handle, rep->mip_levels[0]);
215 goto out_mip;
216 }
217
218 for (i=1; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
219 if (rep->mip_levels[i] != 0) {
220 vmw_error("Incorrect number of faces levels on shared surface."
221 " SID %d, face %d present.\n",
222 handle, i);
223 goto out_mip;
224 }
225 }
226
227 vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
228 if (!vsrf)
229 goto out_mip;
230
231 pipe_reference_init(&vsrf->refcnt, 1);
232 p_atomic_set(&vsrf->validated, 0);
233 vsrf->screen = vws;
234 vsrf->sid = handle;
235 ssrf = svga_winsys_surface(vsrf);
236 *format = rep->format;
237
238 return ssrf;
239
240 out_mip:
241 vmw_ioctl_surface_destroy(vws, handle);
242
243 return NULL;
244 }
245
246 static boolean
247 vmw_drm_surface_get_handle(struct svga_winsys_screen *sws,
248 struct svga_winsys_surface *surface,
249 unsigned stride,
250 struct winsys_handle *whandle)
251 {
252 struct vmw_winsys_screen *vws = vmw_winsys_screen(sws);
253 struct vmw_svga_winsys_surface *vsrf;
254 int ret;
255
256 if (!surface)
257 return FALSE;
258
259 vsrf = vmw_svga_winsys_surface(surface);
260 whandle->handle = vsrf->sid;
261 whandle->stride = stride;
262
263 switch (whandle->type) {
264 case DRM_API_HANDLE_TYPE_SHARED:
265 case DRM_API_HANDLE_TYPE_KMS:
266 whandle->handle = vsrf->sid;
267 break;
268 case DRM_API_HANDLE_TYPE_FD:
269 ret = drmPrimeHandleToFD(vws->ioctl.drm_fd, vsrf->sid, DRM_CLOEXEC,
270 (int *)&whandle->handle);
271 if (ret) {
272 vmw_error("Failed to get file descriptor from prime.\n");
273 return FALSE;
274 }
275 break;
276 default:
277 vmw_error("Attempt to export unsupported handle type %d.\n",
278 whandle->type);
279 return FALSE;
280 }
281
282 return TRUE;
283 }