mesa: Add "shader/" path to #include statements in shader parser/lexer sources
[mesa.git] / src / gallium / winsys / drm / vmware / core / 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 "pipe/p_inlines.h"
29 #include "util/u_memory.h"
30 #include "vmw_screen.h"
31
32 #include "trace/tr_drm.h"
33
34 #include "vmw_screen.h"
35 #include "vmw_surface.h"
36 #include "vmw_fence.h"
37 #include "vmw_context.h"
38
39 #include <state_tracker/dri1_api.h>
40 #include <state_tracker/drm_api.h>
41 #include <vmwgfx_drm.h>
42 #include <xf86drm.h>
43
44 #include <stdio.h>
45
46 static struct dri1_api dri1_api_hooks;
47 static struct dri1_api_version ddx_required = { 0, 1, 0 };
48 static struct dri1_api_version ddx_compat = { 0, 0, 0 };
49 static struct dri1_api_version dri_required = { 4, 0, 0 };
50 static struct dri1_api_version dri_compat = { 4, 0, 0 };
51 static struct dri1_api_version drm_required = { 0, 1, 0 };
52 static struct dri1_api_version drm_compat = { 0, 0, 0 };
53
54 static boolean
55 vmw_dri1_check_version(const struct dri1_api_version *cur,
56 const struct dri1_api_version *required,
57 const struct dri1_api_version *compat,
58 const char component[])
59 {
60 if (cur->major > required->major && cur->major <= compat->major)
61 return TRUE;
62 if (cur->major == required->major && cur->minor >= required->minor)
63 return TRUE;
64
65 fprintf(stderr, "%s version failure.\n", component);
66 fprintf(stderr, "%s version is %d.%d.%d and this driver can only work\n"
67 "with versions %d.%d.x through %d.x.x.\n",
68 component,
69 cur->major,
70 cur->minor,
71 cur->patch_level, required->major, required->minor, compat->major);
72 return FALSE;
73 }
74
75 /* This is actually the entrypoint to the entire driver, called by the
76 * libGL (or EGL, or ...) code via the drm_api_hooks table at the
77 * bottom of the file.
78 */
79 static struct pipe_screen *
80 vmw_drm_create_screen(struct drm_api *drm_api,
81 int fd,
82 struct drm_create_screen_arg *arg)
83 {
84 struct vmw_winsys_screen *vws;
85 struct pipe_screen *screen;
86 struct dri1_create_screen_arg *dri1;
87
88 if (arg != NULL) {
89 switch (arg->mode) {
90 case DRM_CREATE_NORMAL:
91 break;
92 case DRM_CREATE_DRI1:
93 dri1 = (struct dri1_create_screen_arg *)arg;
94 if (!vmw_dri1_check_version(&dri1->ddx_version, &ddx_required,
95 &ddx_compat, "ddx - driver api"))
96 return NULL;
97 if (!vmw_dri1_check_version(&dri1->dri_version, &dri_required,
98 &dri_compat, "dri info"))
99 return NULL;
100 if (!vmw_dri1_check_version(&dri1->drm_version, &drm_required,
101 &drm_compat, "vmwgfx drm driver"))
102 return NULL;
103 dri1->api = &dri1_api_hooks;
104 break;
105 default:
106 return NULL;
107 }
108 }
109
110 vws = vmw_winsys_create( fd );
111 if (!vws)
112 goto out_no_vws;
113
114 screen = svga_screen_create( &vws->base );
115 if (!screen)
116 goto out_no_screen;
117
118 return screen;
119
120 /* Failure cases:
121 */
122 out_no_screen:
123 vmw_winsys_destroy( vws );
124
125 out_no_vws:
126 return NULL;
127 }
128
129 static INLINE boolean
130 vmw_dri1_intersect_src_bbox(struct drm_clip_rect *dst,
131 int dst_x,
132 int dst_y,
133 const struct drm_clip_rect *src,
134 const struct drm_clip_rect *bbox)
135 {
136 int xy1;
137 int xy2;
138
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)
144 return FALSE;
145
146 dst->x1 = xy1;
147 dst->x2 = xy2;
148
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)
154 return FALSE;
155
156 dst->y1 = xy1;
157 dst->y2 = xy2;
158 return TRUE;
159 }
160
161 /**
162 * No fancy get-surface-from-sarea stuff here.
163 * Just use the present blit.
164 */
165
166 static void
167 vmw_dri1_present_locked(struct pipe_context *locked_pipe,
168 struct pipe_surface *surf,
169 const struct drm_clip_rect *rect,
170 unsigned int num_clip,
171 int x_draw, int y_draw,
172 const struct drm_clip_rect *bbox,
173 struct pipe_fence_handle **p_fence)
174 {
175 struct svga_winsys_surface *srf =
176 svga_screen_texture_get_winsys_surface(surf->texture);
177 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
178 struct vmw_winsys_screen *vws =
179 vmw_winsys_screen(svga_winsys_screen(locked_pipe->screen));
180 struct drm_clip_rect clip;
181 int i;
182 struct
183 {
184 SVGA3dCmdHeader header;
185 SVGA3dCmdPresent body;
186 SVGA3dCopyRect rect;
187 } cmd;
188 boolean visible = FALSE;
189 uint32_t fence_seq = 0;
190
191 VMW_FUNC;
192 cmd.header.id = SVGA_3D_CMD_PRESENT;
193 cmd.header.size = sizeof cmd.body + sizeof cmd.rect;
194 cmd.body.sid = vsrf->sid;
195
196 for (i = 0; i < num_clip; ++i) {
197 if (!vmw_dri1_intersect_src_bbox(&clip, x_draw, y_draw, rect++, bbox))
198 continue;
199
200 cmd.rect.x = clip.x1;
201 cmd.rect.y = clip.y1;
202 cmd.rect.w = clip.x2 - clip.x1;
203 cmd.rect.h = clip.y2 - clip.y1;
204 cmd.rect.srcx = (int)clip.x1 - x_draw;
205 cmd.rect.srcy = (int)clip.y1 - y_draw;
206
207 vmw_printf("%s: Clip %d x %d y %d w %d h %d srcx %d srcy %d\n",
208 __FUNCTION__,
209 i,
210 cmd.rect.x,
211 cmd.rect.y,
212 cmd.rect.w, cmd.rect.h, cmd.rect.srcx, cmd.rect.srcy);
213
214 vmw_ioctl_command(vws, &cmd, sizeof cmd.header + cmd.header.size,
215 &fence_seq);
216 visible = TRUE;
217 }
218
219 *p_fence = (visible) ? vmw_pipe_fence(fence_seq) : NULL;
220 vmw_svga_winsys_surface_reference(&vsrf, NULL);
221 }
222
223 /**
224 * FIXME: We'd probably want to cache these buffers in the
225 * screen, based on handle.
226 */
227
228 static struct pipe_buffer *
229 vmw_drm_buffer_from_handle(struct drm_api *drm_api,
230 struct pipe_screen *screen,
231 const char *name,
232 unsigned handle)
233 {
234 struct vmw_svga_winsys_surface *vsrf;
235 struct svga_winsys_surface *ssrf;
236 struct vmw_winsys_screen *vws =
237 vmw_winsys_screen(svga_winsys_screen(screen));
238 struct pipe_buffer *buf;
239 union drm_vmw_surface_reference_arg arg;
240 struct drm_vmw_surface_arg *req = &arg.req;
241 struct drm_vmw_surface_create_req *rep = &arg.rep;
242 int ret;
243 int i;
244
245 /**
246 * The vmware device specific handle is the hardware SID.
247 * FIXME: We probably want to move this to the ioctl implementations.
248 */
249
250 memset(&arg, 0, sizeof(arg));
251 req->sid = handle;
252
253 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_REF_SURFACE,
254 &arg, sizeof(arg));
255
256 if (ret) {
257 fprintf(stderr, "Failed referencing shared surface. SID %d.\n"
258 "Error %d (%s).\n",
259 handle, ret, strerror(-ret));
260 return NULL;
261 }
262
263 if (rep->mip_levels[0] != 1) {
264 fprintf(stderr, "Incorrect number of mipmap levels on shared surface."
265 " SID %d, levels %d\n",
266 handle, rep->mip_levels[0]);
267 goto out_mip;
268 }
269
270 for (i=1; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
271 if (rep->mip_levels[i] != 0) {
272 fprintf(stderr, "Incorrect number of faces levels on shared surface."
273 " SID %d, face %d present.\n",
274 handle, i);
275 goto out_mip;
276 }
277 }
278
279 vsrf = CALLOC_STRUCT(vmw_svga_winsys_surface);
280 if (!vsrf)
281 goto out_mip;
282
283 pipe_reference_init(&vsrf->refcnt, 1);
284 p_atomic_set(&vsrf->validated, 0);
285 vsrf->sid = handle;
286 ssrf = svga_winsys_surface(vsrf);
287 buf = svga_screen_buffer_wrap_surface(screen, rep->format, ssrf);
288 if (!buf)
289 vmw_svga_winsys_surface_reference(&vsrf, NULL);
290
291 return buf;
292 out_mip:
293 vmw_ioctl_surface_destroy(vws, handle);
294 return NULL;
295 }
296
297 static struct pipe_texture *
298 vmw_drm_texture_from_handle(struct drm_api *drm_api,
299 struct pipe_screen *screen,
300 struct pipe_texture *templat,
301 const char *name,
302 unsigned stride,
303 unsigned handle)
304 {
305 struct pipe_buffer *buffer;
306 buffer = vmw_drm_buffer_from_handle(drm_api, screen, name, handle);
307
308 if (!buffer)
309 return NULL;
310
311 return screen->texture_blanket(screen, templat, &stride, buffer);
312 }
313
314 static boolean
315 vmw_drm_handle_from_buffer(struct drm_api *drm_api,
316 struct pipe_screen *screen,
317 struct pipe_buffer *buffer,
318 unsigned *handle)
319 {
320 struct svga_winsys_surface *surface =
321 svga_screen_buffer_get_winsys_surface(buffer);
322 struct vmw_svga_winsys_surface *vsrf;
323
324 if (!surface)
325 return FALSE;
326
327 vsrf = vmw_svga_winsys_surface(surface);
328 *handle = vsrf->sid;
329 vmw_svga_winsys_surface_reference(&vsrf, NULL);
330 return TRUE;
331 }
332
333 static boolean
334 vmw_drm_handle_from_texture(struct drm_api *drm_api,
335 struct pipe_screen *screen,
336 struct pipe_texture *texture,
337 unsigned *stride,
338 unsigned *handle)
339 {
340 struct pipe_buffer *buffer;
341
342 if (!svga_screen_buffer_from_texture(texture, &buffer, stride))
343 return FALSE;
344
345 return vmw_drm_handle_from_buffer(drm_api, screen, buffer, handle);
346 }
347
348 static struct pipe_context*
349 vmw_drm_create_context(struct drm_api *drm_api,
350 struct pipe_screen *screen)
351 {
352 return vmw_svga_context_create(screen);
353 }
354
355 static struct dri1_api dri1_api_hooks = {
356 .front_srf_locked = NULL,
357 .present_locked = vmw_dri1_present_locked
358 };
359
360 static struct drm_api vmw_drm_api_hooks = {
361 .name = "vmwgfx",
362 .create_screen = vmw_drm_create_screen,
363 .create_context = vmw_drm_create_context,
364 .texture_from_shared_handle = vmw_drm_texture_from_handle,
365 .shared_handle_from_texture = vmw_drm_handle_from_texture,
366 .local_handle_from_texture = vmw_drm_handle_from_texture,
367 };
368
369 struct drm_api* drm_api_create()
370 {
371 return trace_drm_create(&vmw_drm_api_hooks);
372 }