1 /**********************************************************
2 * Copyright 2009-2015 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
27 #include "pipe/p_compiler.h"
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_format.h"
32 #include "vmw_context.h"
33 #include "vmw_screen.h"
34 #include "vmw_surface.h"
35 #include "vmw_buffer.h"
36 #include "svga_drm_public.h"
37 #include "svga3d_surfacedefs.h"
39 #include "state_tracker/drm_driver.h"
41 #include "vmwgfx_drm.h"
47 struct dri1_api_version
{
53 static struct svga_winsys_surface
*
54 vmw_drm_surface_from_handle(struct svga_winsys_screen
*sws
,
55 struct winsys_handle
*whandle
,
56 SVGA3dSurfaceFormat
*format
);
58 static struct svga_winsys_surface
*
59 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen
*sws
,
60 struct winsys_handle
*whandle
,
61 SVGA3dSurfaceFormat
*format
);
63 vmw_drm_surface_get_handle(struct svga_winsys_screen
*sws
,
64 struct svga_winsys_surface
*surface
,
66 struct winsys_handle
*whandle
);
68 static struct dri1_api_version drm_required
= { 2, 1, 0 };
69 static struct dri1_api_version drm_compat
= { 2, 0, 0 };
72 vmw_dri1_check_version(const struct dri1_api_version
*cur
,
73 const struct dri1_api_version
*required
,
74 const struct dri1_api_version
*compat
,
75 const char component
[])
77 if (cur
->major
> required
->major
&& cur
->major
<= compat
->major
)
79 if (cur
->major
== required
->major
&& cur
->minor
>= required
->minor
)
82 vmw_error("%s version failure.\n", component
);
83 vmw_error("%s version is %d.%d.%d and this driver can only work\n"
84 "with versions %d.%d.x through %d.x.x.\n",
86 cur
->major
, cur
->minor
, cur
->patch_level
,
87 required
->major
, required
->minor
, compat
->major
);
91 /* This is actually the entrypoint to the entire driver,
92 * called by the target bootstrap code.
94 struct svga_winsys_screen
*
95 svga_drm_winsys_screen_create(int fd
)
97 struct vmw_winsys_screen
*vws
;
98 struct dri1_api_version drm_ver
;
101 ver
= drmGetVersion(fd
);
105 drm_ver
.major
= ver
->version_major
;
106 drm_ver
.minor
= ver
->version_minor
;
107 drm_ver
.patch_level
= 0; /* ??? */
110 if (!vmw_dri1_check_version(&drm_ver
, &drm_required
,
111 &drm_compat
, "vmwgfx drm driver"))
114 vws
= vmw_winsys_create(fd
);
118 /* XXX do this properly */
119 vws
->base
.surface_from_handle
= vws
->base
.have_gb_objects
?
120 vmw_drm_gb_surface_from_handle
: vmw_drm_surface_from_handle
;
121 vws
->base
.surface_get_handle
= vmw_drm_surface_get_handle
;
129 static inline boolean
130 vmw_dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
133 const struct drm_clip_rect
*src
,
134 const struct drm_clip_rect
*bbox
)
139 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
140 (int)bbox
->x1
+ dst_x
;
141 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
142 (int)bbox
->x2
+ dst_x
;
143 if (xy1
>= xy2
|| xy1
< 0)
149 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
150 (int)bbox
->y1
+ dst_y
;
151 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
152 (int)bbox
->y2
+ dst_y
;
153 if (xy1
>= xy2
|| xy1
< 0)
162 * vmw_drm_gb_surface_from_handle - Create a shared surface
164 * @sws: Screen to register the surface with.
165 * @whandle: struct winsys_handle identifying the kernel surface object
166 * @format: On successful return points to a value describing the
169 * Returns a refcounted pointer to a struct svga_winsys_surface
170 * embedded in a struct vmw_svga_winsys_surface on success or NULL
173 static struct svga_winsys_surface
*
174 vmw_drm_gb_surface_from_handle(struct svga_winsys_screen
*sws
,
175 struct winsys_handle
*whandle
,
176 SVGA3dSurfaceFormat
*format
)
178 struct vmw_svga_winsys_surface
*vsrf
;
179 struct svga_winsys_surface
*ssrf
;
180 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
181 SVGA3dSurfaceFlags flags
;
183 struct vmw_buffer_desc desc
;
184 struct pb_manager
*provider
= vws
->pools
.gmr
;
185 struct pb_buffer
*pb_buf
;
189 if (whandle
->offset
!= 0) {
190 fprintf(stderr
, "Attempt to import unsupported winsys offset %u\n",
195 ret
= vmw_ioctl_gb_surface_ref(vws
, whandle
, &flags
, format
,
196 &mip_levels
, &handle
, &desc
.region
);
199 fprintf(stderr
, "Failed referencing shared surface. SID %d.\n"
201 whandle
->handle
, ret
, strerror(-ret
));
205 if (mip_levels
!= 1) {
206 fprintf(stderr
, "Incorrect number of mipmap levels on shared surface."
207 " SID %d, levels %d\n",
208 whandle
->handle
, mip_levels
);
212 vsrf
= CALLOC_STRUCT(vmw_svga_winsys_surface
);
216 pipe_reference_init(&vsrf
->refcnt
, 1);
217 p_atomic_set(&vsrf
->validated
, 0);
220 vsrf
->size
= vmw_region_size(desc
.region
);
223 * Synchronize backing buffers of shared surfaces using the
224 * kernel, since we don't pass fence objects around between
227 desc
.pb_desc
.alignment
= 4096;
228 desc
.pb_desc
.usage
= VMW_BUFFER_USAGE_SHARED
| VMW_BUFFER_USAGE_SYNC
;
229 pb_buf
= provider
->create_buffer(provider
, vsrf
->size
, &desc
.pb_desc
);
230 vsrf
->buf
= vmw_svga_winsys_buffer_wrap(pb_buf
);
233 ssrf
= svga_winsys_surface(vsrf
);
240 vmw_ioctl_region_destroy(desc
.region
);
241 vmw_ioctl_surface_destroy(vws
, whandle
->handle
);
245 static struct svga_winsys_surface
*
246 vmw_drm_surface_from_handle(struct svga_winsys_screen
*sws
,
247 struct winsys_handle
*whandle
,
248 SVGA3dSurfaceFormat
*format
)
250 struct vmw_svga_winsys_surface
*vsrf
;
251 struct svga_winsys_surface
*ssrf
;
252 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
253 union drm_vmw_surface_reference_arg arg
;
254 struct drm_vmw_surface_arg
*req
= &arg
.req
;
255 struct drm_vmw_surface_create_req
*rep
= &arg
.rep
;
257 struct drm_vmw_size size
;
258 SVGA3dSize base_size
;
262 if (whandle
->offset
!= 0) {
263 fprintf(stderr
, "Attempt to import unsupported winsys offset %u\n",
268 switch (whandle
->type
) {
269 case DRM_API_HANDLE_TYPE_SHARED
:
270 case DRM_API_HANDLE_TYPE_KMS
:
271 handle
= whandle
->handle
;
273 case DRM_API_HANDLE_TYPE_FD
:
274 ret
= drmPrimeFDToHandle(vws
->ioctl
.drm_fd
, whandle
->handle
,
277 vmw_error("Failed to get handle from prime fd %d.\n",
278 (int) whandle
->handle
);
283 vmw_error("Attempt to import unsupported handle type %d.\n",
288 memset(&arg
, 0, sizeof(arg
));
290 rep
->size_addr
= (unsigned long)&size
;
292 ret
= drmCommandWriteRead(vws
->ioctl
.drm_fd
, DRM_VMW_REF_SURFACE
,
296 * Need to close the handle we got from prime.
298 if (whandle
->type
== DRM_API_HANDLE_TYPE_FD
)
299 vmw_ioctl_surface_destroy(vws
, handle
);
303 * Any attempt to share something other than a surface, like a dumb
304 * kms buffer, should fail here.
306 vmw_error("Failed referencing shared surface. SID %d.\n"
308 handle
, ret
, strerror(-ret
));
312 if (rep
->mip_levels
[0] != 1) {
313 vmw_error("Incorrect number of mipmap levels on shared surface."
314 " SID %d, levels %d\n",
315 handle
, rep
->mip_levels
[0]);
319 for (i
=1; i
< DRM_VMW_MAX_SURFACE_FACES
; ++i
) {
320 if (rep
->mip_levels
[i
] != 0) {
321 vmw_error("Incorrect number of faces levels on shared surface."
322 " SID %d, face %d present.\n",
328 vsrf
= CALLOC_STRUCT(vmw_svga_winsys_surface
);
332 pipe_reference_init(&vsrf
->refcnt
, 1);
333 p_atomic_set(&vsrf
->validated
, 0);
336 ssrf
= svga_winsys_surface(vsrf
);
337 *format
= rep
->format
;
339 /* Estimate usage, for early flushing. */
341 base_size
.width
= size
.width
;
342 base_size
.height
= size
.height
;
343 base_size
.depth
= size
.depth
;
344 vsrf
->size
= svga3dsurface_get_serialized_size(rep
->format
, base_size
,
351 vmw_ioctl_surface_destroy(vws
, handle
);
357 vmw_drm_surface_get_handle(struct svga_winsys_screen
*sws
,
358 struct svga_winsys_surface
*surface
,
360 struct winsys_handle
*whandle
)
362 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
363 struct vmw_svga_winsys_surface
*vsrf
;
369 vsrf
= vmw_svga_winsys_surface(surface
);
370 whandle
->handle
= vsrf
->sid
;
371 whandle
->stride
= stride
;
374 switch (whandle
->type
) {
375 case DRM_API_HANDLE_TYPE_SHARED
:
376 case DRM_API_HANDLE_TYPE_KMS
:
377 whandle
->handle
= vsrf
->sid
;
379 case DRM_API_HANDLE_TYPE_FD
:
380 ret
= drmPrimeHandleToFD(vws
->ioctl
.drm_fd
, vsrf
->sid
, DRM_CLOEXEC
,
381 (int *)&whandle
->handle
);
383 vmw_error("Failed to get file descriptor from prime.\n");
388 vmw_error("Attempt to export unsupported handle type %d.\n",