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->caps.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 struct dri_image 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 #ifndef GLX_USE_APPLE
160 drawable->image = loader_dri3_get_pixmap_buffer(conn, pixmap, screen,
161 fourcc, drawable->screen->dmabuf_import, &width, &height, drawable);
162 #else
163 drawable->image = NULL;
164 #endif
165 if (!drawable->image)
166 return NULL;
167
168 drawable->w = width;
169 drawable->h = height;
170
171 return drawable->image->texture;
172 }
173 #endif //VK_USE_PLATFORM_XCB_KHR
174
175 static void
kopper_allocate_textures(struct dri_context * ctx,struct dri_drawable * drawable,const enum st_attachment_type * statts,unsigned statts_count)176 kopper_allocate_textures(struct dri_context *ctx,
177 struct dri_drawable *drawable,
178 const enum st_attachment_type *statts,
179 unsigned statts_count)
180 {
181 struct dri_screen *screen = drawable->screen;
182 struct pipe_resource templ;
183 unsigned width, height;
184 bool resized;
185 unsigned i;
186 struct __DRIimageList images;
187 const __DRIimageLoaderExtension *image = screen->image.loader;
188
189 bool is_window = drawable->is_window;
190 bool is_pixmap = !is_window && drawable->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
191
192 /* Wait for glthread to finish because we can't use pipe_context from
193 * multiple threads.
194 */
195 _mesa_glthread_finish(ctx->st->ctx);
196
197 /* First get the buffers from the loader */
198 if (image) {
199 if (!dri_image_drawable_get_buffers(drawable, &images,
200 statts, statts_count))
201 return;
202 }
203
204 if (image) {
205 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
206 struct pipe_resource **buf =
207 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
208 struct pipe_resource *texture = images.front->texture;
209
210 drawable->w = texture->width0;
211 drawable->h = texture->height0;
212
213 pipe_resource_reference(buf, texture);
214 }
215
216 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
217 struct pipe_resource **buf =
218 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
219 struct pipe_resource *texture = images.back->texture;
220
221 drawable->w = texture->width0;
222 drawable->h = texture->height0;
223
224 pipe_resource_reference(buf, texture);
225 }
226
227 if (images.image_mask & __DRI_IMAGE_BUFFER_SHARED) {
228 struct pipe_resource **buf =
229 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
230 struct pipe_resource *texture = images.back->texture;
231
232 drawable->w = texture->width0;
233 drawable->h = texture->height0;
234
235 pipe_resource_reference(buf, texture);
236
237 ctx->is_shared_buffer_bound = true;
238 } else {
239 ctx->is_shared_buffer_bound = false;
240 }
241 }
242
243 /* check size after possible loader image resize */
244 width = drawable->w;
245 height = drawable->h;
246
247 resized = (drawable->old_w != width ||
248 drawable->old_h != height);
249
250 if (!image) {
251 /* remove outdated textures */
252 if (resized) {
253 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
254 if (drawable->textures[i] && i < ST_ATTACHMENT_DEPTH_STENCIL && !is_pixmap) {
255 drawable->textures[i]->width0 = width;
256 drawable->textures[i]->height0 = height;
257 /* force all contexts to revalidate framebuffer */
258 p_atomic_inc(&drawable->base.stamp);
259 } else
260 pipe_resource_reference(&drawable->textures[i], NULL);
261 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
262 if (is_pixmap && i == ST_ATTACHMENT_FRONT_LEFT) {
263 FREE(drawable->image);
264 drawable->image = NULL;
265 }
266 }
267 }
268 }
269
270 drawable->old_w = width;
271 drawable->old_h = height;
272
273 memset(&templ, 0, sizeof(templ));
274 templ.target = screen->target;
275 templ.width0 = width;
276 templ.height0 = height;
277 templ.depth0 = 1;
278 templ.array_size = 1;
279 templ.last_level = 0;
280
281 #if 0
282 XXX do this once swapinterval is hooked up
283 /* pixmaps always have front buffers.
284 * Exchange swaps also mandate fake front buffers.
285 */
286 if (draw->type != LOADER_DRI3_DRAWABLE_WINDOW)
287 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
288 #endif
289
290 uint32_t attachments = 0;
291 for (i = 0; i < statts_count; i++)
292 attachments |= BITFIELD_BIT(statts[i]);
293 bool front_only = attachments & ST_ATTACHMENT_FRONT_LEFT_MASK && !(attachments & ST_ATTACHMENT_BACK_LEFT_MASK);
294
295 for (i = 0; i < statts_count; i++) {
296 enum pipe_format format;
297 unsigned bind;
298
299 dri_drawable_get_format(drawable, statts[i], &format, &bind);
300 templ.format = format;
301
302 /* the texture already exists or not requested */
303 if (!drawable->textures[statts[i]]) {
304 if (statts[i] == ST_ATTACHMENT_BACK_LEFT ||
305 statts[i] == ST_ATTACHMENT_DEPTH_STENCIL ||
306 (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
307 bind |= PIPE_BIND_DISPLAY_TARGET;
308
309 if (format == PIPE_FORMAT_NONE)
310 continue;
311
312 templ.bind = bind;
313 templ.nr_samples = 0;
314 templ.nr_storage_samples = 0;
315
316 if (statts[i] < ST_ATTACHMENT_DEPTH_STENCIL && is_window) {
317 void *data;
318 if (statts[i] == ST_ATTACHMENT_BACK_LEFT || (statts[i] == ST_ATTACHMENT_FRONT_LEFT && front_only))
319 data = &drawable->info;
320 else
321 data = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
322 assert(data);
323 drawable->textures[statts[i]] =
324 screen->base.screen->resource_create_drawable(screen->base.screen, &templ, data);
325 drawable->window_valid = !!drawable->textures[statts[i]];
326 }
327 #ifdef VK_USE_PLATFORM_XCB_KHR
328 else if (is_pixmap && statts[i] == ST_ATTACHMENT_FRONT_LEFT && !screen->is_sw) {
329 drawable->textures[statts[i]] = kopper_get_pixmap_buffer(drawable, format);
330 if (drawable->textures[statts[i]])
331 dri_image_fence_sync(ctx, drawable->image);
332 }
333 #endif
334 if (!drawable->textures[statts[i]])
335 drawable->textures[statts[i]] =
336 screen->base.screen->resource_create(screen->base.screen, &templ);
337 }
338 if (drawable->stvis.samples > 1 && !drawable->msaa_textures[statts[i]]) {
339 templ.bind = bind &
340 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET);
341 templ.nr_samples = drawable->stvis.samples;
342 templ.nr_storage_samples = drawable->stvis.samples;
343 drawable->msaa_textures[statts[i]] =
344 screen->base.screen->resource_create(screen->base.screen, &templ);
345
346 dri_pipe_blit(ctx->st->pipe,
347 drawable->msaa_textures[statts[i]],
348 drawable->textures[statts[i]]);
349 }
350 }
351 }
352
353 static inline void
get_drawable_info(struct dri_drawable * drawable,int * x,int * y,int * w,int * h)354 get_drawable_info(struct dri_drawable *drawable, int *x, int *y, int *w, int *h)
355 {
356 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
357
358 if (loader)
359 loader->getDrawableInfo(drawable, x, y, w, h,
360 drawable->loaderPrivate);
361 }
362
363 static void
kopper_update_drawable_info(struct dri_drawable * drawable)364 kopper_update_drawable_info(struct dri_drawable *drawable)
365 {
366 struct dri_screen *screen = drawable->screen;
367 bool is_window = drawable->info.bos.sType != 0;
368 int x, y;
369 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
370 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
371 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
372
373 bool do_kopper_update = is_window && ptex && screen->fd == -1;
374 if (drawable->info.bos.sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR && do_kopper_update)
375 zink_kopper_update(kopper_get_zink_screen(screen->base.screen), ptex, &drawable->w, &drawable->h);
376 else
377 get_drawable_info(drawable, &x, &y, &drawable->w, &drawable->h);
378 }
379
380 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)381 kopper_present_texture(struct pipe_context *pipe, struct dri_drawable *drawable,
382 struct pipe_resource *ptex, unsigned nboxes, struct pipe_box *sub_box)
383 {
384 struct dri_screen *screen = drawable->screen;
385
386 screen->base.screen->flush_frontbuffer(screen->base.screen, pipe, ptex, 0, 0, drawable, nboxes, sub_box);
387 }
388
389 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)390 kopper_copy_to_front(struct pipe_context *pipe,
391 struct dri_drawable *drawable,
392 struct pipe_resource *ptex,
393 unsigned nrects,
394 struct pipe_box *boxes)
395 {
396 kopper_present_texture(pipe, drawable, ptex, nrects, boxes);
397
398 drawable->lastStamp++;
399 p_atomic_inc(&drawable->base.stamp);
400 }
401
402 static bool
kopper_flush_frontbuffer(struct dri_context * ctx,struct dri_drawable * drawable,enum st_attachment_type statt)403 kopper_flush_frontbuffer(struct dri_context *ctx,
404 struct dri_drawable *drawable,
405 enum st_attachment_type statt)
406 {
407 struct pipe_resource *ptex;
408
409 if (!ctx || statt != ST_ATTACHMENT_FRONT_LEFT)
410 return false;
411
412 /* Wait for glthread to finish because we can't use pipe_context from
413 * multiple threads.
414 */
415 _mesa_glthread_finish(ctx->st->ctx);
416
417 /* prevent recursion */
418 if (drawable->flushing)
419 return true;
420
421 drawable->flushing = true;
422
423 if (drawable->stvis.samples > 1) {
424 /* Resolve the front buffer. */
425 dri_pipe_blit(ctx->st->pipe,
426 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
427 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
428 }
429 ptex = drawable->textures[statt];
430
431 if (ptex) {
432 ctx->st->pipe->flush_resource(ctx->st->pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
433 struct pipe_screen *screen = drawable->screen->base.screen;
434 struct st_context *st;
435 struct pipe_fence_handle *new_fence = NULL;
436
437 st = ctx->st;
438
439 st_context_flush(st, ST_FLUSH_FRONT, &new_fence, NULL, NULL);
440 drawable->flushing = false;
441
442 /* throttle on the previous fence */
443 if (drawable->throttle_fence) {
444 screen->fence_finish(screen, NULL, drawable->throttle_fence, OS_TIMEOUT_INFINITE);
445 screen->fence_reference(screen, &drawable->throttle_fence, NULL);
446 }
447 drawable->throttle_fence = new_fence;
448 kopper_copy_to_front(st->pipe, ctx->draw, ptex, 0, NULL);
449 }
450
451 return true;
452 }
453
454 static inline void
get_image(struct dri_drawable * drawable,int x,int y,int width,int height,void * data)455 get_image(struct dri_drawable *drawable, int x, int y, int width, int height, void *data)
456 {
457 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
458
459 loader->getImage(drawable, x, y, width, height,
460 data, drawable->loaderPrivate);
461 }
462
463 static inline bool
get_image_shm(struct dri_drawable * drawable,int x,int y,int width,int height,struct pipe_resource * res)464 get_image_shm(struct dri_drawable *drawable, int x, int y, int width, int height,
465 struct pipe_resource *res)
466 {
467 const __DRIswrastLoaderExtension *loader = drawable->screen->swrast_loader;
468 struct winsys_handle whandle;
469
470 whandle.type = WINSYS_HANDLE_TYPE_SHMID;
471
472 if (loader->base.version < 4 || !loader->getImageShm)
473 return false;
474
475 if (!res->screen->resource_get_handle(res->screen, NULL, res, &whandle, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE))
476 return false;
477
478 if (loader->base.version > 5 && loader->getImageShm2)
479 return loader->getImageShm2(drawable, x, y, width, height, whandle.handle, drawable->loaderPrivate);
480
481 loader->getImageShm(drawable, x, y, width, height, whandle.handle, drawable->loaderPrivate);
482 return true;
483 }
484
485 static void
kopper_update_tex_buffer(struct dri_drawable * drawable,struct dri_context * ctx,struct pipe_resource * res)486 kopper_update_tex_buffer(struct dri_drawable *drawable,
487 struct dri_context *ctx,
488 struct pipe_resource *res)
489 {
490 struct dri_screen *screen = drawable->screen;
491 if (screen->has_dmabuf || drawable->is_window || drawable->info.bos.sType != VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR)
492 return;
493 drisw_update_tex_buffer(drawable, ctx, res);
494 }
495
496 static void
kopper_flush_swapbuffers(struct dri_context * ctx,struct dri_drawable * drawable)497 kopper_flush_swapbuffers(struct dri_context *ctx,
498 struct dri_drawable *drawable)
499 {
500 /* does this actually need to do anything? */
501 }
502
503 static void
504 kopper_swap_buffers(struct dri_drawable *drawable);
505 static void
506 kopper_swap_buffers_with_damage(struct dri_drawable *drawable, int nrects, const int *rects);
507
508 void
kopper_init_drawable(struct dri_drawable * drawable,bool isPixmap,int alphaBits)509 kopper_init_drawable(struct dri_drawable *drawable, bool isPixmap, int alphaBits)
510 {
511 struct dri_screen *screen = drawable->screen;
512
513 drawable->allocate_textures = kopper_allocate_textures;
514 drawable->update_drawable_info = kopper_update_drawable_info;
515 drawable->flush_frontbuffer = kopper_flush_frontbuffer;
516 drawable->update_tex_buffer = kopper_update_tex_buffer;
517 drawable->flush_swapbuffers = kopper_flush_swapbuffers;
518 drawable->swap_buffers = kopper_swap_buffers;
519 drawable->swap_buffers_with_damage = kopper_swap_buffers_with_damage;
520
521 drawable->info.has_alpha = alphaBits > 0;
522 if (screen->kopper_loader->SetSurfaceCreateInfo)
523 screen->kopper_loader->SetSurfaceCreateInfo(drawable->loaderPrivate,
524 &drawable->info);
525 drawable->is_window = !isPixmap && drawable->info.bos.sType != 0;
526 }
527
528 int64_t
kopperSwapBuffersWithDamage(struct dri_drawable * drawable,uint32_t flush_flags,int nrects,const int * rects)529 kopperSwapBuffersWithDamage(struct dri_drawable *drawable, uint32_t flush_flags, int nrects, const int *rects)
530 {
531 struct dri_context *ctx = dri_get_current();
532 struct pipe_resource *ptex;
533
534 if (!ctx)
535 return 0;
536
537 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
538 if (!ptex)
539 return 0;
540
541 /* ensure invalidation is applied before renderpass ends */
542 if (flush_flags & __DRI2_FLUSH_INVALIDATE_ANCILLARY)
543 _mesa_glthread_invalidate_zsbuf(ctx->st->ctx);
544
545 /* Wait for glthread to finish because we can't use pipe_context from
546 * multiple threads.
547 */
548 _mesa_glthread_finish(ctx->st->ctx);
549
550 drawable->texture_stamp = drawable->lastStamp - 1;
551
552 dri_flush(ctx, drawable,
553 __DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT | flush_flags,
554 __DRI2_THROTTLE_SWAPBUFFER);
555
556 struct pipe_box stack_boxes[64];
557 if (nrects > ARRAY_SIZE(stack_boxes))
558 nrects = 0;
559 if (nrects) {
560 for (unsigned int i = 0; i < nrects; i++) {
561 const int *rect = &rects[i * 4];
562
563 u_box_2d(rect[0], rect[1], rect[2], rect[3], &stack_boxes[i]);
564 }
565 }
566
567 kopper_copy_to_front(ctx->st->pipe, drawable, ptex, nrects, stack_boxes);
568 if (drawable->is_window && !zink_kopper_check(ptex))
569 return -1;
570 if (!drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
571 return 0;
572 }
573
574 /* have to manually swap the pointers here to make frontbuffer readback work */
575 drawable->textures[ST_ATTACHMENT_BACK_LEFT] = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
576 drawable->textures[ST_ATTACHMENT_FRONT_LEFT] = ptex;
577
578 return 0;
579 }
580
581 int64_t
kopperSwapBuffers(struct dri_drawable * dPriv,uint32_t flush_flags)582 kopperSwapBuffers(struct dri_drawable *dPriv, uint32_t flush_flags)
583 {
584 return kopperSwapBuffersWithDamage(dPriv, flush_flags, 0, NULL);
585 }
586
587 static void
kopper_swap_buffers_with_damage(struct dri_drawable * drawable,int nrects,const int * rects)588 kopper_swap_buffers_with_damage(struct dri_drawable *drawable, int nrects, const int *rects)
589 {
590
591 kopperSwapBuffersWithDamage(drawable, 0, nrects, rects);
592 }
593
594 static void
kopper_swap_buffers(struct dri_drawable * drawable)595 kopper_swap_buffers(struct dri_drawable *drawable)
596 {
597 kopper_swap_buffers_with_damage(drawable, 0, NULL);
598 }
599
600 void
kopperSetSwapInterval(struct dri_drawable * drawable,int interval)601 kopperSetSwapInterval(struct dri_drawable *drawable, int interval)
602 {
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(struct dri_drawable * drawable)623 kopperQueryBufferAge(struct dri_drawable *drawable)
624 {
625 struct dri_context *ctx = dri_get_current();
626 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT] ?
627 drawable->textures[ST_ATTACHMENT_BACK_LEFT] :
628 drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
629
630 /* can't get buffer age from non-window swapchain */
631 if (!drawable->window_valid)
632 return 0;
633
634 /* Wait for glthread to finish because we can't use pipe_context from
635 * multiple threads.
636 */
637 _mesa_glthread_finish(ctx->st->ctx);
638
639 return zink_kopper_query_buffer_age(ctx->st->pipe, ptex);
640 }
641
642
643 /* vim: set sw=3 ts=8 sts=3 expandtab: */
644