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 "mesa_interface.h"
25 #include "git_sha1.h"
26 #include "util/format/u_format.h"
27 #include "util/u_memory.h"
28 #include "util/u_inlines.h"
29 #include "util/box.h"
30 #include "util/log.h"
31 #include "pipe/p_context.h"
32 #include "pipe-loader/pipe_loader.h"
33 #include "state_tracker/st_context.h"
34 #include "zink/zink_public.h"
35 #include "zink/zink_kopper.h"
36
37 #include "dri_screen.h"
38 #include "dri_context.h"
39 #include "dri_drawable.h"
40 #include "dri_helpers.h"
41
42 #include <vulkan/vulkan.h>
43
44 #ifdef VK_USE_PLATFORM_XCB_KHR
45 #include <xcb/xcb.h>
46 #include <xcb/dri3.h>
47 #include <xcb/present.h>
48 #include <xcb/xfixes.h>
49 #include "util/libsync.h"
50 #include <X11/Xlib-xcb.h>
51 #include "drm-uapi/drm_fourcc.h"
52 #include "loader_dri3_helper.h"
53 #endif
54
55 static struct dri_drawable *
56 kopper_create_drawable(struct dri_screen *screen, const struct gl_config *visual,
57 bool isPixmap, void *loaderPrivate);
58
59 struct pipe_screen *
kopper_init_screen(struct dri_screen * screen,bool driver_name_is_inferred)60 kopper_init_screen(struct dri_screen *screen, bool driver_name_is_inferred)
61 {
62 struct pipe_screen *pscreen = NULL;
63
64 if (!screen->kopper_loader) {
65 fprintf(stderr, "mesa: Kopper interface not found!\n"
66 " Ensure the versions of %s built with this version of Zink are\n"
67 " in your library path!\n", KOPPER_LIB_NAMES);
68 return NULL;
69 }
70
71 screen->can_share_buffer = true;
72
73 bool success;
74 #ifdef HAVE_LIBDRM
75 if (screen->fd != -1)
76 success = pipe_loader_drm_probe_fd(&screen->dev, screen->fd, false);
77 else
78 success = pipe_loader_vk_probe_dri(&screen->dev);
79 #else
80 success = pipe_loader_vk_probe_dri(&screen->dev);
81 #endif
82
83 if (success)
84 pscreen = pipe_loader_create_screen(screen->dev, driver_name_is_inferred);
85
86 if (!pscreen)
87 return NULL;
88
89 assert(pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY));
90 screen->is_sw = zink_kopper_is_cpu(pscreen);
91
92 return pscreen;
93 }
94
95 // copypasta alert
96
97 extern bool
98 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
99 struct __DRIimageList *images,
100 const enum st_attachment_type *statts,
101 unsigned statts_count);
102
103 #ifdef VK_USE_PLATFORM_XCB_KHR
104 /* Translate from the pipe_format enums used by Gallium to the DRM FourCC
105 * codes used by dmabuf import */
106 static int
pipe_format_to_fourcc(enum pipe_format format)107 pipe_format_to_fourcc(enum pipe_format format)
108 {
109 switch (format) {
110 case PIPE_FORMAT_BGRA8888_SRGB: return __DRI_IMAGE_FOURCC_SABGR8888;
111 case PIPE_FORMAT_BGRX8888_SRGB: return __DRI_IMAGE_FOURCC_SXRGB8888;
112 case PIPE_FORMAT_RGBA8888_SRGB: return __DRI_IMAGE_FOURCC_SABGR8888;
113 case PIPE_FORMAT_B5G6R5_UNORM: return DRM_FORMAT_RGB565;
114 case PIPE_FORMAT_BGRX8888_UNORM: return DRM_FORMAT_XRGB8888;
115 case PIPE_FORMAT_BGRA8888_UNORM: return DRM_FORMAT_ARGB8888;
116 case PIPE_FORMAT_RGBA8888_UNORM: return DRM_FORMAT_ABGR8888;
117 case PIPE_FORMAT_RGBX8888_UNORM: return DRM_FORMAT_XBGR8888;
118 case PIPE_FORMAT_B10G10R10X2_UNORM: return DRM_FORMAT_XRGB2101010;
119 case PIPE_FORMAT_B10G10R10A2_UNORM: return DRM_FORMAT_ARGB2101010;
120 case PIPE_FORMAT_R10G10B10X2_UNORM: return DRM_FORMAT_XBGR2101010;
121 case PIPE_FORMAT_R10G10B10A2_UNORM: return DRM_FORMAT_ABGR2101010;
122 case PIPE_FORMAT_R16G16B16A16_FLOAT: return DRM_FORMAT_XBGR16161616F;
123 case PIPE_FORMAT_R16G16B16X16_FLOAT: return DRM_FORMAT_ABGR16161616F;
124 case PIPE_FORMAT_B5G5R5A1_UNORM: return DRM_FORMAT_ARGB1555;
125 case PIPE_FORMAT_R5G5B5A1_UNORM: return DRM_FORMAT_ABGR1555;
126 case PIPE_FORMAT_B4G4R4A4_UNORM: return DRM_FORMAT_ARGB4444;
127 case PIPE_FORMAT_R4G4B4A4_UNORM: return DRM_FORMAT_ABGR4444;
128 default: return DRM_FORMAT_INVALID;
129 }
130 }
131
132 /** kopper_get_pixmap_buffer
133 *
134 * Get the DRM object for a pixmap from the X server and
135 * wrap that with a __DRIimage structure using createImageFromDmaBufs
136 */
137 static struct pipe_resource *
kopper_get_pixmap_buffer(struct dri_drawable * drawable,enum pipe_format pf)138 kopper_get_pixmap_buffer(struct dri_drawable *drawable,
139 enum pipe_format pf)
140 {
141 xcb_drawable_t pixmap;
142 int width;
143 int height;
144 uint32_t fourcc = pipe_format_to_fourcc(pf);
145 struct kopper_loader_info *info = &drawable->info;
146 assert(info->bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
147 VkXcbSurfaceCreateInfoKHR *xcb = (VkXcbSurfaceCreateInfoKHR *)&info->bos;
148 xcb_connection_t *conn = xcb->connection;
149 pixmap = xcb->window;
150
151 if (drawable->image)
152 return drawable->image->texture;
153
154 /* FIXME: probably broken for OBS studio?
155 * see dri3_get_pixmap_buffer()
156 */
157 struct dri_screen *screen = drawable->screen;
158
159 drawable->image = loader_dri3_get_pixmap_buffer(conn, pixmap, opaque_dri_screen(screen),
160 fourcc, drawable->screen->dmabuf_import, &width, &height, drawable);
161 if (!drawable->image)
162 return NULL;
163
164 drawable->w = width;
165 drawable->h = height;
166
167 return drawable->image->texture;
168 }
169 #endif //VK_USE_PLATFORM_XCB_KHR
170
171 static void
kopper_allocate_textures(struct dri_context * ctx,struct dri_drawable * drawable,const enum st_attachment_type * statts,unsigned statts_count)172 kopper_allocate_textures(struct dri_context *ctx,
173 struct dri_drawable *drawable,
174 const enum st_attachment_type *statts,
175 unsigned statts_count)
176 {
177 struct dri_screen *screen = drawable->screen;
178 struct pipe_resource templ;
179 unsigned width, height;
180 bool resized;
181 unsigned i;
182 struct __DRIimageList images;
183 const __DRIimageLoaderExtension *image = screen->image.loader;
184
185 bool is_window = drawable->is_window;
186 bool is_pixmap = !is_window && drawable->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
187
188 /* Wait for glthread to finish because we can't use pipe_context from
189 * multiple threads.
190 */
191 _mesa_glthread_finish(ctx->st->ctx);
192
193 /* First get the buffers from the loader */
194 if (image) {
195 if (!dri_image_drawable_get_buffers(drawable, &images,
196 statts, statts_count))
197 return;
198 }
199
200 if (image) {
201 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
202 struct pipe_resource **buf =
203 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
204 struct pipe_resource *texture = images.front->texture;
205
206 drawable->w = texture->width0;
207 drawable->h = texture->height0;
208
209 pipe_resource_reference(buf, texture);
210 }
211
212 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
213 struct pipe_resource **buf =
214 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
215 struct pipe_resource *texture = images.back->texture;
216
217 drawable->w = texture->width0;
218 drawable->h = texture->height0;
219
220 pipe_resource_reference(buf, texture);
221 }
222
223 if (images.image_mask & __DRI_IMAGE_BUFFER_SHARED) {
224 struct pipe_resource **buf =
225 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
226 struct pipe_resource *texture = images.back->texture;
227
228 drawable->w = texture->width0;
229 drawable->h = texture->height0;
230
231 pipe_resource_reference(buf, texture);
232
233 ctx->is_shared_buffer_bound = true;
234 } else {
235 ctx->is_shared_buffer_bound = false;
236 }
237 }
238
239 /* check size after possible loader image resize */
240 width = drawable->w;
241 height = drawable->h;
242
243 resized = (drawable->old_w != width ||
244 drawable->old_h != height);
245
246 if (!image) {
247 /* remove outdated textures */
248 if (resized) {
249 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
250 if (drawable->textures[i] && i < ST_ATTACHMENT_DEPTH_STENCIL && !is_pixmap) {
251 drawable->textures[i]->width0 = width;
252 drawable->textures[i]->height0 = height;
253 /* force all contexts to revalidate framebuffer */
254 p_atomic_inc(&drawable->base.stamp);
255 } else
256 pipe_resource_reference(&drawable->textures[i], NULL);
257 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
258 if (is_pixmap && i == ST_ATTACHMENT_FRONT_LEFT) {
259 FREE(drawable->image);
260 drawable->image = NULL;
261 }
262 }
263 }
264 }
265
266 drawable->old_w = width;
267 drawable->old_h = height;
268
269 memset(&templ, 0, sizeof(templ));
270 templ.target = screen->target;
271 templ.width0 = width;
272 templ.height0 = height;
273 templ.depth0 = 1;
274 templ.array_size = 1;
275 templ.last_level = 0;
276
277 #if 0
278 XXX do this once swapinterval is hooked up
279 /* pixmaps always have front buffers.
280 * Exchange swaps also mandate fake front buffers.
281 */
282 if (draw->type != LOADER_DRI3_DRAWABLE_WINDOW)
283 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
284 #endif
285
286 uint32_t attachments = 0;
287 for (i = 0; i < statts_count; i++)
288 attachments |= BITFIELD_BIT(statts[i]);
289 bool front_only = attachments & ST_ATTACHMENT_FRONT_LEFT_MASK && !(attachments & ST_ATTACHMENT_BACK_LEFT_MASK);
290
291 for (i = 0; i < statts_count; i++) {
292 enum pipe_format format;
293 unsigned bind;
294
295 dri_drawable_get_format(drawable, statts[i], &format, &bind);
296 templ.format = format;
297
298 /* the texture already exists or not requested */
299 if (!drawable->textures[statts[i]]) {
300 if (statts[i] == ST_ATTACHMENT_BACK_LEFT ||
301 statts[i] == ST_ATTACHMENT_DEPTH_STENCIL ||
302 (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
303 bind |= PIPE_BIND_DISPLAY_TARGET;
304
305 if (format == PIPE_FORMAT_NONE)
306 continue;
307
308 templ.bind = bind;
309 templ.nr_samples = 0;
310 templ.nr_storage_samples = 0;
311
312 if (statts[i] < ST_ATTACHMENT_DEPTH_STENCIL && is_window) {
313 void *data;
314 if (statts[i] == ST_ATTACHMENT_BACK_LEFT || (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
315 data = &drawable->info;
316 else
317 data = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
318 assert(data);
319 drawable->textures[statts[i]] =
320 screen->base.screen->resource_create_drawable(screen->base.screen, &templ, data);
321 drawable->window_valid = !!drawable->textures[statts[i]];
322 }
323 #ifdef VK_USE_PLATFORM_XCB_KHR
324 else if (is_pixmap && statts[i] == ST_ATTACHMENT_FRONT_LEFT && !screen->is_sw) {
325 drawable->textures[statts[i]] = kopper_get_pixmap_buffer(drawable, format);
326 if (drawable->textures[statts[i]])
327 dri_image_fence_sync(ctx, drawable->image);
328 }
329 #endif
330 if (!drawable->textures[statts[i]])
331 drawable->textures[statts[i]] =
332 screen->base.screen->resource_create(screen->base.screen, &templ);
333 }
334 if (drawable->stvis.samples > 1 && !drawable->msaa_textures[statts[i]]) {
335 templ.bind = bind &
336 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET);
337 templ.nr_samples = drawable->stvis.samples;
338 templ.nr_storage_samples = drawable->stvis.samples;
339 drawable->msaa_textures[statts[i]] =
340 screen->base.screen->resource_create(screen->base.screen, &templ);
341
342 dri_pipe_blit(ctx->st->pipe,
343 drawable->msaa_textures[statts[i]],
344 drawable->textures[statts[i]]);
345 }
346 }
347 }
348
349 static inline void
get_drawable_info(struct dri_drawable * drawable,int * x,int * y,int * w,int * h)350 get_drawable_info(struct dri_drawable *drawable, int *x, int *y, int *w, int *h)
351 {
352 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
353
354 if (loader)
355 loader->getDrawableInfo(opaque_dri_drawable(drawable),
356 x, y, w, h,
357 drawable->loaderPrivate);
358 }
359
360 static void
kopper_update_drawable_info(struct dri_drawable * drawable)361 kopper_update_drawable_info(struct dri_drawable *drawable)
362 {
363 struct dri_screen *screen = drawable->screen;
364 bool is_window = drawable->info.bos.sType != 0;
365 int x, y;
366 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
367 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
368 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
369
370 bool do_kopper_update = is_window && ptex && screen->fd == -1;
371 if (drawable->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR && do_kopper_update)
372 zink_kopper_update(kopper_get_zink_screen(screen->base.screen), ptex, &drawable->w, &drawable->h);
373 else
374 get_drawable_info(drawable, &x, &y, &drawable->w, &drawable->h);
375 }
376
377 static inline void
kopper_present_texture(struct pipe_context * pipe,struct dri_drawable * drawable,struct pipe_resource * ptex,unsigned nboxes,struct pipe_box * sub_box)378 kopper_present_texture(struct pipe_context *pipe, struct dri_drawable *drawable,
379 struct pipe_resource *ptex, unsigned nboxes, struct pipe_box *sub_box)
380 {
381 struct dri_screen *screen = drawable->screen;
382
383 screen->base.screen->flush_frontbuffer(screen->base.screen, pipe, ptex, 0, 0, drawable, nboxes, sub_box);
384 }
385
386 static inline void
kopper_copy_to_front(struct pipe_context * pipe,struct dri_drawable * drawable,struct pipe_resource * ptex,unsigned nrects,struct pipe_box * boxes)387 kopper_copy_to_front(struct pipe_context *pipe,
388 struct dri_drawable *drawable,
389 struct pipe_resource *ptex,
390 unsigned nrects,
391 struct pipe_box *boxes)
392 {
393 kopper_present_texture(pipe, drawable, ptex, nrects, boxes);
394
395 drawable->lastStamp++;
396 p_atomic_inc(&drawable->base.stamp);
397 }
398
399 static bool
kopper_flush_frontbuffer(struct dri_context * ctx,struct dri_drawable * drawable,enum st_attachment_type statt)400 kopper_flush_frontbuffer(struct dri_context *ctx,
401 struct dri_drawable *drawable,
402 enum st_attachment_type statt)
403 {
404 struct pipe_resource *ptex;
405
406 if (!ctx || statt != ST_ATTACHMENT_FRONT_LEFT)
407 return false;
408
409 /* Wait for glthread to finish because we can't use pipe_context from
410 * multiple threads.
411 */
412 _mesa_glthread_finish(ctx->st->ctx);
413
414 /* prevent recursion */
415 if (drawable->flushing)
416 return true;
417
418 drawable->flushing = true;
419
420 if (drawable->stvis.samples > 1) {
421 /* Resolve the front buffer. */
422 dri_pipe_blit(ctx->st->pipe,
423 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
424 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
425 }
426 ptex = drawable->textures[statt];
427
428 if (ptex) {
429 ctx->st->pipe->flush_resource(ctx->st->pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
430 struct pipe_screen *screen = drawable->screen->base.screen;
431 struct st_context *st;
432 struct pipe_fence_handle *new_fence = NULL;
433
434 st = ctx->st;
435
436 st_context_flush(st, ST_FLUSH_FRONT, &new_fence, NULL, NULL);
437 drawable->flushing = false;
438
439 /* throttle on the previous fence */
440 if (drawable->throttle_fence) {
441 screen->fence_finish(screen, NULL, drawable->throttle_fence, OS_TIMEOUT_INFINITE);
442 screen->fence_reference(screen, &drawable->throttle_fence, NULL);
443 }
444 drawable->throttle_fence = new_fence;
445 kopper_copy_to_front(st->pipe, ctx->draw, ptex, 0, NULL);
446 }
447
448 return true;
449 }
450
451 static inline void
get_image(struct dri_drawable * drawable,int x,int y,int width,int height,void * data)452 get_image(struct dri_drawable *drawable, int x, int y, int width, int height, void *data)
453 {
454 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
455
456 loader->getImage(opaque_dri_drawable(drawable),
457 x, y, width, height,
458 data, drawable->loaderPrivate);
459 }
460
461 static inline bool
get_image_shm(struct dri_drawable * drawable,int x,int y,int width,int height,struct pipe_resource * res)462 get_image_shm(struct dri_drawable *drawable, int x, int y, int width, int height,
463 struct pipe_resource *res)
464 {
465 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
466 struct winsys_handle whandle;
467
468 whandle.type = WINSYS_HANDLE_TYPE_SHMID;
469
470 if (loader->base.version < 4 || !loader->getImageShm)
471 return false;
472
473 if (!res->screen->resource_get_handle(res->screen, NULL, res, &whandle, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE))
474 return false;
475
476 if (loader->base.version > 5 && loader->getImageShm2)
477 return loader->getImageShm2(opaque_dri_drawable(drawable), x, y, width, height, whandle.handle, drawable->loaderPrivate);
478
479 loader->getImageShm(opaque_dri_drawable(drawable), x, y, width, height, whandle.handle, drawable->loaderPrivate);
480 return true;
481 }
482
483 static void
kopper_update_tex_buffer(struct dri_drawable * drawable,struct dri_context * ctx,struct pipe_resource * res)484 kopper_update_tex_buffer(struct dri_drawable *drawable,
485 struct dri_context *ctx,
486 struct pipe_resource *res)
487 {
488 struct dri_screen *screen = drawable->screen;
489 if (screen->has_dmabuf || drawable->is_window || drawable->info.bos.sType != VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR)
490 return;
491 drisw_update_tex_buffer(drawable, ctx, res);
492 }
493
494 static void
kopper_flush_swapbuffers(struct dri_context * ctx,struct dri_drawable * drawable)495 kopper_flush_swapbuffers(struct dri_context *ctx,
496 struct dri_drawable *drawable)
497 {
498 /* does this actually need to do anything? */
499 }
500
501 static void
502 kopper_swap_buffers(struct dri_drawable *drawable);
503 static void
504 kopper_swap_buffers_with_damage(struct dri_drawable *drawable, int nrects, const int *rects);
505
506 void
kopper_init_drawable(struct dri_drawable * drawable,bool isPixmap,int alphaBits)507 kopper_init_drawable(struct dri_drawable *drawable, bool isPixmap, int alphaBits)
508 {
509 struct dri_screen *screen = drawable->screen;
510
511 drawable->allocate_textures = kopper_allocate_textures;
512 drawable->update_drawable_info = kopper_update_drawable_info;
513 drawable->flush_frontbuffer = kopper_flush_frontbuffer;
514 drawable->update_tex_buffer = kopper_update_tex_buffer;
515 drawable->flush_swapbuffers = kopper_flush_swapbuffers;
516 drawable->swap_buffers = kopper_swap_buffers;
517 drawable->swap_buffers_with_damage = kopper_swap_buffers_with_damage;
518
519 drawable->info.has_alpha = alphaBits > 0;
520 if (screen->kopper_loader->SetSurfaceCreateInfo)
521 screen->kopper_loader->SetSurfaceCreateInfo(drawable->loaderPrivate,
522 &drawable->info);
523 drawable->is_window = !isPixmap && drawable->info.bos.sType != 0;
524 }
525
526 int64_t
kopperSwapBuffersWithDamage(__DRIdrawable * dPriv,uint32_t flush_flags,int nrects,const int * rects)527 kopperSwapBuffersWithDamage(__DRIdrawable *dPriv, uint32_t flush_flags, int nrects, const int *rects)
528 {
529 struct dri_drawable *drawable = dri_drawable(dPriv);
530 struct dri_context *ctx = dri_get_current();
531 struct pipe_resource *ptex;
532
533 if (!ctx)
534 return 0;
535
536 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
537 if (!ptex)
538 return 0;
539
540 /* ensure invalidation is applied before renderpass ends */
541 if (flush_flags & __DRI2_FLUSH_INVALIDATE_ANCILLARY)
542 _mesa_glthread_invalidate_zsbuf(ctx->st->ctx);
543
544 /* Wait for glthread to finish because we can't use pipe_context from
545 * multiple threads.
546 */
547 _mesa_glthread_finish(ctx->st->ctx);
548
549 drawable->texture_stamp = drawable->lastStamp - 1;
550
551 dri_flush(opaque_dri_context(ctx), opaque_dri_drawable(drawable),
552 __DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT | flush_flags,
553 __DRI2_THROTTLE_SWAPBUFFER);
554
555 struct pipe_box stack_boxes[64];
556 if (nrects > ARRAY_SIZE(stack_boxes))
557 nrects = 0;
558 if (nrects) {
559 for (unsigned int i = 0; i < nrects; i++) {
560 const int *rect = &rects[i * 4];
561
562 u_box_2d(rect[0], rect[1], rect[2], rect[3], &stack_boxes[i]);
563 }
564 }
565
566 kopper_copy_to_front(ctx->st->pipe, drawable, ptex, nrects, stack_boxes);
567 if (drawable->is_window && !zink_kopper_check(ptex))
568 return -1;
569 if (!drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
570 return 0;
571 }
572
573 /* have to manually swap the pointers here to make frontbuffer readback work */
574 drawable->textures[ST_ATTACHMENT_BACK_LEFT] = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
575 drawable->textures[ST_ATTACHMENT_FRONT_LEFT] = ptex;
576
577 return 0;
578 }
579
580 int64_t
kopperSwapBuffers(__DRIdrawable * dPriv,uint32_t flush_flags)581 kopperSwapBuffers(__DRIdrawable *dPriv, uint32_t flush_flags)
582 {
583 return kopperSwapBuffersWithDamage(dPriv, flush_flags, 0, NULL);
584 }
585
586 static void
kopper_swap_buffers_with_damage(struct dri_drawable * drawable,int nrects,const int * rects)587 kopper_swap_buffers_with_damage(struct dri_drawable *drawable, int nrects, const int *rects)
588 {
589
590 kopperSwapBuffersWithDamage(opaque_dri_drawable(drawable), 0, nrects, rects);
591 }
592
593 static void
kopper_swap_buffers(struct dri_drawable * drawable)594 kopper_swap_buffers(struct dri_drawable *drawable)
595 {
596 kopper_swap_buffers_with_damage(drawable, 0, NULL);
597 }
598
599 void
kopperSetSwapInterval(__DRIdrawable * dPriv,int interval)600 kopperSetSwapInterval(__DRIdrawable *dPriv, int interval)
601 {
602 struct dri_drawable *drawable = dri_drawable(dPriv);
603 struct dri_screen *screen = drawable->screen;
604 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
605 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
606 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
607
608 /* can't set swap interval on non-windows */
609 if (!drawable->window_valid)
610 return;
611 /* the conditional is because we can be called before buffer allocation. If
612 * we're before allocation, then the initial_swap_interval will be used when
613 * the swapchain is eventually created.
614 */
615 if (ptex) {
616 struct pipe_screen *pscreen = kopper_get_zink_screen(screen->base.screen);
617 zink_kopper_set_swap_interval(pscreen, ptex, interval);
618 }
619 drawable->info.initial_swap_interval = interval;
620 }
621
622 int
kopperQueryBufferAge(__DRIdrawable * dPriv)623 kopperQueryBufferAge(__DRIdrawable *dPriv)
624 {
625 struct dri_drawable *drawable = dri_drawable(dPriv);
626 struct dri_context *ctx = dri_get_current();
627 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
628 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
629 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
630
631 /* can't get buffer age from non-window swapchain */
632 if (!drawable->window_valid)
633 return 0;
634
635 /* Wait for glthread to finish because we can't use pipe_context from
636 * multiple threads.
637 */
638 _mesa_glthread_finish(ctx->st->ctx);
639
640 return zink_kopper_query_buffer_age(ctx->st->pipe, ptex);
641 }
642
643
644 /* vim: set sw=3 ts=8 sts=3 expandtab: */
645