1 /*
2 * Copyright 2020 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "util/format/u_format.h"
25 #include "util/u_memory.h"
26 #include "util/u_inlines.h"
27 #include "util/u_box.h"
28 #include "util/log.h"
29 #include "pipe/p_context.h"
30 #include "pipe-loader/pipe_loader.h"
31 #include "state_tracker/st_context.h"
32 #include "os/os_process.h"
33 #include "zink/zink_public.h"
34 #include "zink/zink_kopper.h"
35 #include "driver_trace/tr_screen.h"
36
37 #include "dri_screen.h"
38 #include "dri_context.h"
39 #include "dri_drawable.h"
40 #include "dri_helpers.h"
41 #include "dri_query_renderer.h"
42
43 #include <vulkan/vulkan.h>
44
45 #ifdef VK_USE_PLATFORM_XCB_KHR
46 #include <xcb/xcb.h>
47 #include <xcb/dri3.h>
48 #include <xcb/present.h>
49 #include <xcb/xfixes.h>
50 #include "util/libsync.h"
51 #include <X11/Xlib-xcb.h>
52 #include "drm-uapi/drm_fourcc.h"
53 #endif
54
55 struct kopper_drawable {
56 struct dri_drawable base;
57 struct kopper_loader_info info;
58 __DRIimage *image; //texture_from_pixmap
59 bool is_window;
60 };
61
62 struct kopper_screen {
63 struct dri_screen base;
64 struct pipe_screen *screen; //unwrapped
65 bool has_dmabuf;
66 bool has_modifiers;
67 bool is_sw;
68 };
69
70 extern const __DRIimageExtension driVkImageExtension;
71 extern const __DRIimageExtension driVkImageExtensionSw;
72
73 static void
kopper_flush_drawable(__DRIdrawable * dPriv)74 kopper_flush_drawable(__DRIdrawable *dPriv)
75 {
76 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
77 }
78
79 static inline void
kopper_invalidate_drawable(__DRIdrawable * dPriv)80 kopper_invalidate_drawable(__DRIdrawable *dPriv)
81 {
82 struct dri_drawable *drawable = dri_drawable(dPriv);
83
84 drawable->texture_stamp = dPriv->lastStamp - 1;
85
86 p_atomic_inc(&drawable->base.stamp);
87 }
88
89 static const __DRI2flushExtension driVkFlushExtension = {
90 .base = { __DRI2_FLUSH, 4 },
91
92 .flush = kopper_flush_drawable,
93 .invalidate = kopper_invalidate_drawable,
94 .flush_with_flags = dri_flush,
95 };
96
97 static const __DRIrobustnessExtension dri2Robustness = {
98 .base = { __DRI2_ROBUSTNESS, 1 }
99 };
100
101 const __DRIkopperExtension driKopperExtension;
102
103 static const __DRIextension *drivk_screen_extensions[] = {
104 &driTexBufferExtension.base,
105 &dri2RendererQueryExtension.base,
106 &dri2ConfigQueryExtension.base,
107 &dri2FenceExtension.base,
108 &dri2Robustness.base,
109 &driVkImageExtension.base,
110 &dri2FlushControlExtension.base,
111 &driVkFlushExtension.base,
112 &driKopperExtension.base,
113 NULL
114 };
115
116 static const __DRIextension *drivk_sw_screen_extensions[] = {
117 &driTexBufferExtension.base,
118 &dri2RendererQueryExtension.base,
119 &dri2ConfigQueryExtension.base,
120 &dri2FenceExtension.base,
121 &dri2Robustness.base,
122 &driVkImageExtensionSw.base,
123 &dri2FlushControlExtension.base,
124 &driVkFlushExtension.base,
125 NULL
126 };
127
128 static const __DRIconfig **
kopper_init_screen(__DRIscreen * sPriv)129 kopper_init_screen(__DRIscreen * sPriv)
130 {
131 const __DRIconfig **configs;
132 struct dri_screen *screen;
133 struct kopper_screen *kscreen;
134 struct pipe_screen *pscreen = NULL;
135
136 if (!sPriv->kopper_loader) {
137 fprintf(stderr, "mesa: Kopper interface not found!\n"
138 " Ensure the versions of %s built with this version of Zink are\n"
139 " in your library path!\n", KOPPER_LIB_NAMES);
140 return NULL;
141 }
142 kscreen = CALLOC_STRUCT(kopper_screen);
143 if (!kscreen)
144 return NULL;
145 screen = &kscreen->base;
146
147 screen->sPriv = sPriv;
148 screen->fd = sPriv->fd;
149 screen->can_share_buffer = true;
150
151 sPriv->driverPrivate = (void *)kscreen;
152
153 bool success;
154 if (screen->fd != -1)
155 success = pipe_loader_drm_probe_fd(&screen->dev, screen->fd);
156 else
157 success = pipe_loader_vk_probe_dri(&screen->dev, NULL);
158 if (success) {
159 pscreen = pipe_loader_create_screen(screen->dev);
160 dri_init_options(screen);
161 }
162
163 if (!pscreen)
164 goto fail;
165
166 kscreen->screen = trace_screen_unwrap(pscreen);
167
168 configs = dri_init_screen_helper(screen, pscreen);
169 if (!configs)
170 goto fail;
171
172 assert(pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY));
173 screen->has_reset_status_query = true;
174 screen->lookup_egl_image = dri2_lookup_egl_image;
175 kscreen->has_dmabuf = pscreen->get_param(pscreen, PIPE_CAP_DMABUF);
176 kscreen->has_modifiers = pscreen->query_dmabuf_modifiers != NULL;
177 kscreen->is_sw = zink_kopper_is_cpu(pscreen);
178 if (kscreen->has_dmabuf)
179 sPriv->extensions = drivk_screen_extensions;
180 else
181 sPriv->extensions = drivk_sw_screen_extensions;
182
183 const __DRIimageLookupExtension *image = sPriv->dri2.image;
184 if (image &&
185 image->base.version >= 2 &&
186 image->validateEGLImage &&
187 image->lookupEGLImageValidated) {
188 screen->validate_egl_image = dri2_validate_egl_image;
189 screen->lookup_egl_image_validated = dri2_lookup_egl_image_validated;
190 }
191
192 return configs;
193 fail:
194 dri_destroy_screen_helper(screen);
195 if (screen->dev)
196 pipe_loader_release(&screen->dev, 1);
197 FREE(screen);
198 return NULL;
199 }
200
201 // copypasta alert
202
203 static inline void
drisw_present_texture(struct pipe_context * pipe,__DRIdrawable * dPriv,struct pipe_resource * ptex,struct pipe_box * sub_box)204 drisw_present_texture(struct pipe_context *pipe, __DRIdrawable *dPriv,
205 struct pipe_resource *ptex, struct pipe_box *sub_box)
206 {
207 struct dri_drawable *drawable = dri_drawable(dPriv);
208 struct dri_screen *screen = dri_screen(drawable->sPriv);
209
210 screen->base.screen->flush_frontbuffer(screen->base.screen, pipe, ptex, 0, 0, drawable, sub_box);
211 }
212
213 extern bool
214 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
215 struct __DRIimageList *images,
216 const enum st_attachment_type *statts,
217 unsigned statts_count);
218
219 #ifdef VK_USE_PLATFORM_XCB_KHR
220 static int
get_dri_format(enum pipe_format pf)221 get_dri_format(enum pipe_format pf)
222 {
223 int image_format;
224 switch (pf) {
225 case PIPE_FORMAT_R16G16B16A16_FLOAT:
226 image_format = __DRI_IMAGE_FORMAT_ABGR16161616F;
227 break;
228 case PIPE_FORMAT_R16G16B16X16_FLOAT:
229 image_format = __DRI_IMAGE_FORMAT_XBGR16161616F;
230 break;
231 case PIPE_FORMAT_B5G5R5A1_UNORM:
232 image_format = __DRI_IMAGE_FORMAT_ARGB1555;
233 break;
234 case PIPE_FORMAT_B5G6R5_UNORM:
235 image_format = __DRI_IMAGE_FORMAT_RGB565;
236 break;
237 case PIPE_FORMAT_BGRX8888_UNORM:
238 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
239 break;
240 case PIPE_FORMAT_BGRA8888_UNORM:
241 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
242 break;
243 case PIPE_FORMAT_RGBX8888_UNORM:
244 image_format = __DRI_IMAGE_FORMAT_XBGR8888;
245 break;
246 case PIPE_FORMAT_RGBA8888_UNORM:
247 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
248 break;
249 case PIPE_FORMAT_B10G10R10X2_UNORM:
250 image_format = __DRI_IMAGE_FORMAT_XRGB2101010;
251 break;
252 case PIPE_FORMAT_B10G10R10A2_UNORM:
253 image_format = __DRI_IMAGE_FORMAT_ARGB2101010;
254 break;
255 case PIPE_FORMAT_R10G10B10X2_UNORM:
256 image_format = __DRI_IMAGE_FORMAT_XBGR2101010;
257 break;
258 case PIPE_FORMAT_R10G10B10A2_UNORM:
259 image_format = __DRI_IMAGE_FORMAT_ABGR2101010;
260 break;
261 default:
262 image_format = __DRI_IMAGE_FORMAT_NONE;
263 break;
264 }
265 return image_format;
266 }
267
268 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
269 * the createImageFromFds call takes DRM_FORMAT codes. To avoid
270 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
271 * translate to DRM_FORMAT codes in the call to createImageFromFds
272 */
273 static int
image_format_to_fourcc(int format)274 image_format_to_fourcc(int format)
275 {
276
277 /* Convert from __DRI_IMAGE_FORMAT to DRM_FORMAT (sigh) */
278 switch (format) {
279 case __DRI_IMAGE_FORMAT_SARGB8: return __DRI_IMAGE_FOURCC_SARGB8888;
280 case __DRI_IMAGE_FORMAT_SABGR8: return __DRI_IMAGE_FOURCC_SABGR8888;
281 case __DRI_IMAGE_FORMAT_SXRGB8: return __DRI_IMAGE_FOURCC_SXRGB8888;
282 case __DRI_IMAGE_FORMAT_RGB565: return DRM_FORMAT_RGB565;
283 case __DRI_IMAGE_FORMAT_XRGB8888: return DRM_FORMAT_XRGB8888;
284 case __DRI_IMAGE_FORMAT_ARGB8888: return DRM_FORMAT_ARGB8888;
285 case __DRI_IMAGE_FORMAT_ABGR8888: return DRM_FORMAT_ABGR8888;
286 case __DRI_IMAGE_FORMAT_XBGR8888: return DRM_FORMAT_XBGR8888;
287 case __DRI_IMAGE_FORMAT_XRGB2101010: return DRM_FORMAT_XRGB2101010;
288 case __DRI_IMAGE_FORMAT_ARGB2101010: return DRM_FORMAT_ARGB2101010;
289 case __DRI_IMAGE_FORMAT_XBGR2101010: return DRM_FORMAT_XBGR2101010;
290 case __DRI_IMAGE_FORMAT_ABGR2101010: return DRM_FORMAT_ABGR2101010;
291 case __DRI_IMAGE_FORMAT_XBGR16161616F: return DRM_FORMAT_XBGR16161616F;
292 case __DRI_IMAGE_FORMAT_ABGR16161616F: return DRM_FORMAT_ABGR16161616F;
293 }
294 return 0;
295 }
296
297 #ifdef HAVE_DRI3_MODIFIERS
298 static __DRIimage *
dri3_create_image_from_buffers(xcb_connection_t * c,xcb_dri3_buffers_from_pixmap_reply_t * bp_reply,unsigned int format,__DRIscreen * dri_screen,const __DRIimageExtension * image,void * loaderPrivate)299 dri3_create_image_from_buffers(xcb_connection_t *c,
300 xcb_dri3_buffers_from_pixmap_reply_t *bp_reply,
301 unsigned int format,
302 __DRIscreen *dri_screen,
303 const __DRIimageExtension *image,
304 void *loaderPrivate)
305 {
306 __DRIimage *ret;
307 int *fds;
308 uint32_t *strides_in, *offsets_in;
309 int strides[4], offsets[4];
310 unsigned error;
311 int i;
312
313 if (bp_reply->nfd > 4)
314 return NULL;
315
316 fds = xcb_dri3_buffers_from_pixmap_reply_fds(c, bp_reply);
317 strides_in = xcb_dri3_buffers_from_pixmap_strides(bp_reply);
318 offsets_in = xcb_dri3_buffers_from_pixmap_offsets(bp_reply);
319 for (i = 0; i < bp_reply->nfd; i++) {
320 strides[i] = strides_in[i];
321 offsets[i] = offsets_in[i];
322 }
323
324 ret = image->createImageFromDmaBufs2(dri_screen,
325 bp_reply->width,
326 bp_reply->height,
327 image_format_to_fourcc(format),
328 bp_reply->modifier,
329 fds, bp_reply->nfd,
330 strides, offsets,
331 0, 0, 0, 0, /* UNDEFINED */
332 &error, loaderPrivate);
333
334 for (i = 0; i < bp_reply->nfd; i++)
335 close(fds[i]);
336
337 return ret;
338 }
339 #endif
340
341 static __DRIimage *
dri3_create_image(xcb_connection_t * c,xcb_dri3_buffer_from_pixmap_reply_t * bp_reply,unsigned int format,__DRIscreen * dri_screen,const __DRIimageExtension * image,void * loaderPrivate)342 dri3_create_image(xcb_connection_t *c,
343 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
344 unsigned int format,
345 __DRIscreen *dri_screen,
346 const __DRIimageExtension *image,
347 void *loaderPrivate)
348 {
349 int *fds;
350 __DRIimage *image_planar, *ret;
351 int stride, offset;
352
353 /* Get an FD for the pixmap object
354 */
355 fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply);
356
357 stride = bp_reply->stride;
358 offset = 0;
359
360 /* createImageFromFds creates a wrapper __DRIimage structure which
361 * can deal with multiple planes for things like Yuv images. So, once
362 * we've gotten the planar wrapper, pull the single plane out of it and
363 * discard the wrapper.
364 */
365 image_planar = image->createImageFromFds(dri_screen,
366 bp_reply->width,
367 bp_reply->height,
368 image_format_to_fourcc(format),
369 fds, 1,
370 &stride, &offset, loaderPrivate);
371 close(fds[0]);
372 if (!image_planar)
373 return NULL;
374
375 ret = image->fromPlanar(image_planar, 0, loaderPrivate);
376
377 if (!ret)
378 ret = image_planar;
379 else
380 image->destroyImage(image_planar);
381
382 return ret;
383 }
384
385
386 static void
handle_in_fence(__DRIcontext * context,__DRIimage * img)387 handle_in_fence(__DRIcontext *context, __DRIimage *img)
388 {
389 struct dri_context *ctx = dri_context(context);
390 struct pipe_context *pipe = ctx->st->pipe;
391 struct pipe_fence_handle *fence;
392 int fd = img->in_fence_fd;
393
394 if (fd == -1)
395 return;
396
397 validate_fence_fd(fd);
398
399 img->in_fence_fd = -1;
400
401 pipe->create_fence_fd(pipe, &fence, fd, PIPE_FD_TYPE_NATIVE_SYNC);
402 pipe->fence_server_sync(pipe, fence);
403 pipe->screen->fence_reference(pipe->screen, &fence, NULL);
404
405 close(fd);
406 }
407
408 /** kopper_get_pixmap_buffer
409 *
410 * Get the DRM object for a pixmap from the X server and
411 * wrap that with a __DRIimage structure using createImageFromFds
412 */
413 static struct pipe_resource *
kopper_get_pixmap_buffer(struct kopper_drawable * cdraw,enum pipe_format pf)414 kopper_get_pixmap_buffer(struct kopper_drawable *cdraw,
415 enum pipe_format pf)
416 {
417 xcb_drawable_t pixmap;
418 int width;
419 int height;
420 int format = get_dri_format(pf);
421 __DRIscreen *cur_screen;
422 struct kopper_loader_info *info = &cdraw->info;
423 assert(info->bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
424 xcb_connection_t *conn = info->xcb.connection;
425 pixmap = info->xcb.window;
426
427 if (cdraw->image)
428 return cdraw->image->texture;
429
430 /* FIXME: probably broken for OBS studio?
431 * see dri3_get_pixmap_buffer()
432 */
433 cur_screen = cdraw->base.sPriv;
434
435 #ifdef HAVE_DRI3_MODIFIERS
436 struct kopper_screen *kscreen = (struct kopper_screen*)cur_screen->driverPrivate;
437 if (kscreen->has_modifiers) {
438 xcb_dri3_buffers_from_pixmap_cookie_t bps_cookie;
439 xcb_dri3_buffers_from_pixmap_reply_t *bps_reply;
440 xcb_generic_error_t *error;
441
442 bps_cookie = xcb_dri3_buffers_from_pixmap(conn, pixmap);
443 bps_reply = xcb_dri3_buffers_from_pixmap_reply(conn, bps_cookie, &error);
444 if (!bps_reply) {
445 mesa_loge("kopper: could not create texture from pixmap (%u)", error->error_code);
446 return NULL;
447 }
448 cdraw->image =
449 dri3_create_image_from_buffers(conn, bps_reply, format,
450 cur_screen, &driVkImageExtension,
451 cdraw);
452 width = bps_reply->width;
453 height = bps_reply->height;
454 free(bps_reply);
455 } else
456 #endif
457 {
458 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
459 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
460 xcb_generic_error_t *error;
461
462 bp_cookie = xcb_dri3_buffer_from_pixmap(conn, pixmap);
463 bp_reply = xcb_dri3_buffer_from_pixmap_reply(conn, bp_cookie, &error);
464 if (!bp_reply) {
465 mesa_loge("kopper: could not create texture from pixmap (%u)", error->error_code);
466 return NULL;
467 }
468
469 cdraw->image = dri3_create_image(conn, bp_reply, format,
470 cur_screen, &driVkImageExtension,
471 cdraw);
472 width = bp_reply->width;
473 height = bp_reply->height;
474 free(bp_reply);
475 }
476
477 cdraw->base.dPriv->w = width;
478 cdraw->base.dPriv->h = height;
479
480 return cdraw->image->texture;
481 }
482 #endif //VK_USE_PLATFORM_XCB_KHR
483
484 static void
kopper_allocate_textures(struct dri_context * ctx,struct dri_drawable * drawable,const enum st_attachment_type * statts,unsigned statts_count)485 kopper_allocate_textures(struct dri_context *ctx,
486 struct dri_drawable *drawable,
487 const enum st_attachment_type *statts,
488 unsigned statts_count)
489 {
490 struct dri_screen *screen = dri_screen(drawable->sPriv);
491 struct pipe_resource templ;
492 unsigned width, height;
493 boolean resized;
494 unsigned i;
495 struct __DRIimageList images;
496 __DRIdrawable *dri_drawable = drawable->dPriv;
497 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
498 struct kopper_drawable *cdraw = (struct kopper_drawable *)drawable;
499 struct kopper_screen *kscreen = (struct kopper_screen*)drawable->sPriv->driverPrivate;
500
501 bool is_window = cdraw->is_window;
502 bool is_pixmap = !is_window && cdraw->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
503
504 width = drawable->dPriv->w;
505 height = drawable->dPriv->h;
506
507 resized = (drawable->old_w != width ||
508 drawable->old_h != height);
509
510 /* First get the buffers from the loader */
511 if (image) {
512 if (!dri_image_drawable_get_buffers(drawable, &images,
513 statts, statts_count))
514 return;
515 }
516
517 if (image) {
518 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
519 struct pipe_resource **buf =
520 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
521 struct pipe_resource *texture = images.front->texture;
522
523 dri_drawable->w = texture->width0;
524 dri_drawable->h = texture->height0;
525
526 pipe_resource_reference(buf, texture);
527 }
528
529 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
530 struct pipe_resource **buf =
531 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
532 struct pipe_resource *texture = images.back->texture;
533
534 dri_drawable->w = texture->width0;
535 dri_drawable->h = texture->height0;
536
537 pipe_resource_reference(buf, texture);
538 }
539
540 if (images.image_mask & __DRI_IMAGE_BUFFER_SHARED) {
541 struct pipe_resource **buf =
542 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
543 struct pipe_resource *texture = images.back->texture;
544
545 dri_drawable->w = texture->width0;
546 dri_drawable->h = texture->height0;
547
548 pipe_resource_reference(buf, texture);
549
550 ctx->is_shared_buffer_bound = true;
551 } else {
552 ctx->is_shared_buffer_bound = false;
553 }
554 } else {
555 /* remove outdated textures */
556 if (resized) {
557 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
558 if (drawable->textures[i] && i < ST_ATTACHMENT_DEPTH_STENCIL && !is_pixmap) {
559 drawable->textures[i]->width0 = width;
560 drawable->textures[i]->height0 = height;
561 /* force all contexts to revalidate framebuffer */
562 p_atomic_inc(&drawable->base.stamp);
563 } else
564 pipe_resource_reference(&drawable->textures[i], NULL);
565 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
566 if (is_pixmap && i == ST_ATTACHMENT_FRONT_LEFT) {
567 FREE(cdraw->image);
568 cdraw->image = NULL;
569 }
570 }
571 }
572 }
573
574 drawable->old_w = width;
575 drawable->old_h = height;
576
577 memset(&templ, 0, sizeof(templ));
578 templ.target = screen->target;
579 templ.width0 = width;
580 templ.height0 = height;
581 templ.depth0 = 1;
582 templ.array_size = 1;
583 templ.last_level = 0;
584
585 #if 0
586 XXX do this once swapinterval is hooked up
587 /* pixmaps always have front buffers.
588 * Exchange swaps also mandate fake front buffers.
589 */
590 if (draw->type != LOADER_DRI3_DRAWABLE_WINDOW ||
591 draw->swap_method == __DRI_ATTRIB_SWAP_EXCHANGE)
592 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
593 #endif
594
595 uint32_t attachments = 0;
596 for (i = 0; i < statts_count; i++)
597 attachments |= BITFIELD_BIT(statts[i]);
598 bool front_only = attachments & ST_ATTACHMENT_FRONT_LEFT_MASK && !(attachments & ST_ATTACHMENT_BACK_LEFT_MASK);
599
600 for (i = 0; i < statts_count; i++) {
601 enum pipe_format format;
602 unsigned bind;
603
604 dri_drawable_get_format(drawable, statts[i], &format, &bind);
605
606 /* the texture already exists or not requested */
607 if (!drawable->textures[statts[i]]) {
608 if (statts[i] == ST_ATTACHMENT_BACK_LEFT ||
609 statts[i] == ST_ATTACHMENT_DEPTH_STENCIL ||
610 (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
611 bind |= PIPE_BIND_DISPLAY_TARGET;
612
613 if (format == PIPE_FORMAT_NONE)
614 continue;
615
616 templ.format = format;
617 templ.bind = bind;
618 templ.nr_samples = 0;
619 templ.nr_storage_samples = 0;
620
621 if (statts[i] < ST_ATTACHMENT_DEPTH_STENCIL && is_window) {
622 void *data;
623 if (statts[i] == ST_ATTACHMENT_BACK_LEFT || (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
624 data = &cdraw->info;
625 else
626 data = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
627 assert(data);
628 drawable->textures[statts[i]] =
629 screen->base.screen->resource_create_drawable(screen->base.screen, &templ, data);
630 }
631 #ifdef VK_USE_PLATFORM_XCB_KHR
632 else if (is_pixmap && statts[i] == ST_ATTACHMENT_FRONT_LEFT && !kscreen->is_sw) {
633 drawable->textures[statts[i]] = kopper_get_pixmap_buffer(cdraw, format);
634 handle_in_fence(ctx->cPriv, cdraw->image);
635 }
636 #endif
637 else {
638 drawable->textures[statts[i]] =
639 screen->base.screen->resource_create(screen->base.screen, &templ);
640 }
641 }
642 if (drawable->stvis.samples > 1 && !drawable->msaa_textures[statts[i]]) {
643 templ.bind = templ.bind &
644 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET);
645 templ.nr_samples = drawable->stvis.samples;
646 templ.nr_storage_samples = drawable->stvis.samples;
647 drawable->msaa_textures[statts[i]] =
648 screen->base.screen->resource_create(screen->base.screen, &templ);
649
650 dri_pipe_blit(ctx->st->pipe,
651 drawable->msaa_textures[statts[i]],
652 drawable->textures[statts[i]]);
653 }
654 }
655 }
656
657 static inline void
get_drawable_info(__DRIdrawable * dPriv,int * x,int * y,int * w,int * h)658 get_drawable_info(__DRIdrawable *dPriv, int *x, int *y, int *w, int *h)
659 {
660 __DRIscreen *sPriv = dPriv->driScreenPriv;
661 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
662
663 if (loader)
664 loader->getDrawableInfo(dPriv,
665 x, y, w, h,
666 dPriv->loaderPrivate);
667 }
668
669 static void
kopper_update_drawable_info(struct dri_drawable * drawable)670 kopper_update_drawable_info(struct dri_drawable *drawable)
671 {
672 __DRIdrawable *dPriv = drawable->dPriv;
673 __DRIscreen *sPriv = dPriv->driScreenPriv;
674 struct kopper_drawable *cdraw = (struct kopper_drawable *)drawable;
675 bool is_window = cdraw->info.bos.sType != 0;
676 int x, y;
677 struct kopper_screen *kscreen = (struct kopper_screen*)sPriv->driverPrivate;
678 struct pipe_screen *screen = kscreen->screen;
679 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
680 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
681 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
682
683 bool do_kopper_update = is_window && ptex && kscreen->base.fd == -1;
684 if (cdraw->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR && do_kopper_update)
685 zink_kopper_update(screen, ptex, &dPriv->w, &dPriv->h);
686 else
687 get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h);
688 }
689
690 static inline void
kopper_present_texture(struct pipe_context * pipe,__DRIdrawable * dPriv,struct pipe_resource * ptex,struct pipe_box * sub_box)691 kopper_present_texture(struct pipe_context *pipe, __DRIdrawable *dPriv,
692 struct pipe_resource *ptex, struct pipe_box *sub_box)
693 {
694 struct dri_drawable *drawable = dri_drawable(dPriv);
695 struct dri_screen *screen = dri_screen(drawable->sPriv);
696
697 screen->base.screen->flush_frontbuffer(screen->base.screen, pipe, ptex, 0, 0, drawable, sub_box);
698 }
699
700 static inline void
kopper_copy_to_front(struct pipe_context * pipe,__DRIdrawable * dPriv,struct pipe_resource * ptex)701 kopper_copy_to_front(struct pipe_context *pipe,
702 __DRIdrawable * dPriv,
703 struct pipe_resource *ptex)
704 {
705 kopper_present_texture(pipe, dPriv, ptex, NULL);
706
707 kopper_invalidate_drawable(dPriv);
708 }
709
710 static bool
kopper_flush_frontbuffer(struct dri_context * ctx,struct dri_drawable * drawable,enum st_attachment_type statt)711 kopper_flush_frontbuffer(struct dri_context *ctx,
712 struct dri_drawable *drawable,
713 enum st_attachment_type statt)
714 {
715 struct pipe_resource *ptex;
716
717 if (!ctx || statt != ST_ATTACHMENT_FRONT_LEFT)
718 return false;
719
720 if (drawable) {
721 /* prevent recursion */
722 if (drawable->flushing)
723 return true;
724
725 drawable->flushing = true;
726 }
727
728 if (drawable->stvis.samples > 1) {
729 /* Resolve the front buffer. */
730 dri_pipe_blit(ctx->st->pipe,
731 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
732 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
733 }
734 ptex = drawable->textures[statt];
735
736 if (ptex) {
737 ctx->st->pipe->flush_resource(ctx->st->pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
738 struct pipe_screen *screen = drawable->screen->base.screen;
739 struct st_context_iface *st;
740 struct pipe_fence_handle *new_fence = NULL;
741 st = ctx->st;
742 if (st->thread_finish)
743 st->thread_finish(st);
744
745 st->flush(st, ST_FLUSH_FRONT, &new_fence, NULL, NULL);
746 if (drawable) {
747 drawable->flushing = false;
748 }
749 /* throttle on the previous fence */
750 if (drawable->throttle_fence) {
751 screen->fence_finish(screen, NULL, drawable->throttle_fence, PIPE_TIMEOUT_INFINITE);
752 screen->fence_reference(screen, &drawable->throttle_fence, NULL);
753 }
754 drawable->throttle_fence = new_fence;
755 kopper_copy_to_front(st->pipe, ctx->dPriv, ptex);
756 }
757
758 return true;
759 }
760
761 static inline void
get_image(__DRIdrawable * dPriv,int x,int y,int width,int height,void * data)762 get_image(__DRIdrawable *dPriv, int x, int y, int width, int height, void *data)
763 {
764 __DRIscreen *sPriv = dPriv->driScreenPriv;
765 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
766
767 loader->getImage(dPriv,
768 x, y, width, height,
769 data, dPriv->loaderPrivate);
770 }
771
772 static inline bool
get_image_shm(__DRIdrawable * dPriv,int x,int y,int width,int height,struct pipe_resource * res)773 get_image_shm(__DRIdrawable *dPriv, int x, int y, int width, int height,
774 struct pipe_resource *res)
775 {
776 __DRIscreen *sPriv = dPriv->driScreenPriv;
777 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
778 struct winsys_handle whandle;
779
780 whandle.type = WINSYS_HANDLE_TYPE_SHMID;
781
782 if (loader->base.version < 4 || !loader->getImageShm)
783 return FALSE;
784
785 if (!res->screen->resource_get_handle(res->screen, NULL, res, &whandle, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE))
786 return FALSE;
787
788 if (loader->base.version > 5 && loader->getImageShm2)
789 return loader->getImageShm2(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
790
791 loader->getImageShm(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
792 return TRUE;
793 }
794
795 static void
kopper_update_tex_buffer(struct dri_drawable * drawable,struct dri_context * ctx,struct pipe_resource * res)796 kopper_update_tex_buffer(struct dri_drawable *drawable,
797 struct dri_context *ctx,
798 struct pipe_resource *res)
799 {
800 __DRIdrawable *dPriv = drawable->dPriv;
801 __DRIscreen *sPriv = dPriv->driScreenPriv;
802 struct kopper_screen *kscreen = (struct kopper_screen*)sPriv->driverPrivate;
803 struct kopper_drawable *cdraw = (struct kopper_drawable *)drawable;
804 struct st_context *st_ctx = (struct st_context *)ctx->st;
805 struct pipe_context *pipe = st_ctx->pipe;
806 struct pipe_transfer *transfer;
807 char *map;
808 int x, y, w, h;
809 int ximage_stride, line;
810 if (kscreen->has_dmabuf || cdraw->is_window || cdraw->info.bos.sType != VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR)
811 return;
812 int cpp = util_format_get_blocksize(res->format);
813
814 get_drawable_info(dPriv, &x, &y, &w, &h);
815
816 map = pipe_texture_map(pipe, res,
817 0, 0, // level, layer,
818 PIPE_MAP_WRITE,
819 x, y, w, h, &transfer);
820
821 /* Copy the Drawable content to the mapped texture buffer */
822 if (!get_image_shm(dPriv, x, y, w, h, res))
823 get_image(dPriv, x, y, w, h, map);
824
825 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
826 get_image() has a pitch rounded up to 4 bytes. */
827 ximage_stride = ((w * cpp) + 3) & -4;
828 for (line = h-1; line; --line) {
829 memmove(&map[line * transfer->stride],
830 &map[line * ximage_stride],
831 ximage_stride);
832 }
833
834 pipe_texture_unmap(pipe, transfer);
835 }
836
837 static void
kopper_flush_swapbuffers(struct dri_context * ctx,struct dri_drawable * drawable)838 kopper_flush_swapbuffers(struct dri_context *ctx,
839 struct dri_drawable *drawable)
840 {
841 /* does this actually need to do anything? */
842 }
843
844 // XXX this frees its second argument as a side effect - regardless of success
845 // - since the point is to use it as the superclass initializer before we add
846 // our own state. kindagross but easier than fixing the object model first.
847 static struct kopper_drawable *
kopper_create_drawable(__DRIdrawable * dPriv,struct dri_drawable * base)848 kopper_create_drawable(__DRIdrawable *dPriv, struct dri_drawable *base)
849 {
850 struct kopper_drawable *_ret = CALLOC_STRUCT(kopper_drawable);
851
852 if (!_ret)
853 goto out;
854 struct dri_drawable *ret = &_ret->base;
855
856 // copy all the elements
857 *ret = *base;
858
859 // relocate references to the old struct
860 ret->base.visual = &ret->stvis;
861 ret->base.st_manager_private = (void *) ret;
862 dPriv->driverPrivate = ret;
863
864 // and fill in the vtable
865 ret->allocate_textures = kopper_allocate_textures;
866 ret->update_drawable_info = kopper_update_drawable_info;
867 ret->flush_frontbuffer = kopper_flush_frontbuffer;
868 ret->update_tex_buffer = kopper_update_tex_buffer;
869 ret->flush_swapbuffers = kopper_flush_swapbuffers;
870
871 out:
872 free(base);
873 return _ret;
874 }
875
876 static boolean
kopper_create_buffer(__DRIscreen * sPriv,__DRIdrawable * dPriv,const struct gl_config * visual,boolean isPixmap)877 kopper_create_buffer(__DRIscreen * sPriv,
878 __DRIdrawable * dPriv,
879 const struct gl_config *visual, boolean isPixmap)
880 {
881 struct kopper_drawable *drawable = NULL;
882
883 /* always pass !pixmap because it isn't "handled" or relevant */
884 if (!dri_create_buffer(sPriv, dPriv, visual, false))
885 return FALSE;
886
887 drawable = kopper_create_drawable(dPriv, dPriv->driverPrivate);
888 if (!drawable)
889 return FALSE;
890
891 drawable->info.has_alpha = visual->alphaBits > 0;
892 if (sPriv->kopper_loader->SetSurfaceCreateInfo)
893 sPriv->kopper_loader->SetSurfaceCreateInfo(dPriv->loaderPrivate,
894 &drawable->info);
895 drawable->is_window = !isPixmap && drawable->info.bos.sType != 0;
896
897 return TRUE;
898 }
899
900 static int64_t
kopperSwapBuffers(__DRIdrawable * dPriv)901 kopperSwapBuffers(__DRIdrawable *dPriv)
902 {
903 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
904 struct dri_drawable *drawable = dri_drawable(dPriv);
905 struct kopper_drawable *kdraw = (struct kopper_drawable *)drawable;
906 struct pipe_resource *ptex;
907
908 if (!ctx)
909 return 0;
910
911 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
912 if (!ptex)
913 return 0;
914
915 drawable->texture_stamp = dPriv->lastStamp - 1;
916 dri_flush(ctx->cPriv, dPriv, __DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT, __DRI2_THROTTLE_SWAPBUFFER);
917 kopper_copy_to_front(ctx->st->pipe, dPriv, ptex);
918 if (kdraw->is_window && !zink_kopper_check(ptex))
919 return -1;
920 if (!drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
921 return 0;
922 }
923
924 /* have to manually swap the pointers here to make frontbuffer readback work */
925 drawable->textures[ST_ATTACHMENT_BACK_LEFT] = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
926 drawable->textures[ST_ATTACHMENT_FRONT_LEFT] = ptex;
927
928 return 0;
929 }
930
931 static void
kopper_swap_buffers(__DRIdrawable * dPriv)932 kopper_swap_buffers(__DRIdrawable *dPriv)
933 {
934 kopperSwapBuffers(dPriv);
935 }
936
937 static __DRIdrawable *
kopperCreateNewDrawable(__DRIscreen * screen,const __DRIconfig * config,void * data,int is_pixmap)938 kopperCreateNewDrawable(__DRIscreen *screen,
939 const __DRIconfig *config,
940 void *data,
941 int is_pixmap)
942 {
943 __DRIdrawable *pdraw;
944
945 assert(data != NULL);
946
947 pdraw = malloc(sizeof *pdraw);
948 if (!pdraw)
949 return NULL;
950
951 pdraw->loaderPrivate = data;
952
953 pdraw->driScreenPriv = screen;
954 pdraw->driContextPriv = NULL;
955 pdraw->refcount = 0;
956 pdraw->lastStamp = 0;
957 pdraw->w = 0;
958 pdraw->h = 0;
959
960 //dri_get_drawable(pdraw);
961 pdraw->refcount++;
962
963 if (!screen->driver->CreateBuffer(screen, pdraw, &config->modes,
964 is_pixmap)) {
965 free(pdraw);
966 return NULL;
967 }
968
969 pdraw->dri2.stamp = pdraw->lastStamp + 1;
970
971 return pdraw;
972 }
973
974 static void
kopperSetSwapInterval(__DRIdrawable * dPriv,int interval)975 kopperSetSwapInterval(__DRIdrawable *dPriv, int interval)
976 {
977 struct dri_drawable *drawable = dri_drawable(dPriv);
978 struct kopper_drawable *cdraw = (struct kopper_drawable *)drawable;
979 struct dri_screen *screen = dri_screen(drawable->sPriv);
980 struct kopper_screen *kscreen = (struct kopper_screen *)screen;
981 struct pipe_screen *pscreen = kscreen->screen;
982 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
983 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
984 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
985
986 /* the conditional is because we can be called before buffer allocation. If
987 * we're before allocation, then the initial_swap_interval will be used when
988 * the swapchain is eventually created.
989 */
990 if (ptex)
991 zink_kopper_set_swap_interval(pscreen, ptex, interval);
992 cdraw->info.initial_swap_interval = interval;
993 }
994
995 static int
kopperQueryBufferAge(__DRIdrawable * dPriv)996 kopperQueryBufferAge(__DRIdrawable *dPriv)
997 {
998 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
999 struct dri_drawable *drawable = dri_drawable(dPriv);
1000 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
1001 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
1002 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
1003
1004 return zink_kopper_query_buffer_age(ctx->st->pipe, ptex);
1005 }
1006
1007 const __DRIkopperExtension driKopperExtension = {
1008 .base = { __DRI_KOPPER, 1 },
1009 .createNewDrawable = kopperCreateNewDrawable,
1010 .swapBuffers = kopperSwapBuffers,
1011 .setSwapInterval = kopperSetSwapInterval,
1012 .queryBufferAge = kopperQueryBufferAge,
1013 };
1014
1015 const struct __DriverAPIRec galliumvk_driver_api = {
1016 .InitScreen = kopper_init_screen,
1017 .DestroyScreen = dri_destroy_screen,
1018 .CreateBuffer = kopper_create_buffer,
1019 .DestroyBuffer = dri_destroy_buffer,
1020 .SwapBuffers = kopper_swap_buffers,
1021 .CopySubBuffer = NULL,
1022 };
1023
1024 static const struct __DRIDriverVtableExtensionRec galliumvk_vtable = {
1025 .base = { __DRI_DRIVER_VTABLE, 1 },
1026 .vtable = &galliumvk_driver_api,
1027 };
1028
1029 const __DRIextension *galliumvk_driver_extensions[] = {
1030 &driCoreExtension.base,
1031 &driSWRastExtension.base,
1032 &driDRI2Extension.base,
1033 &driImageDriverExtension.base,
1034 &driKopperExtension.base,
1035 &gallium_config_options.base,
1036 &galliumvk_vtable.base,
1037 NULL
1038 };
1039
1040 /* vim: set sw=3 ts=8 sts=3 expandtab: */
1041