b6d7d039e208aa8eb1db87aed5958f54da412121
[mesa.git] / src / gallium / frontends / dri / dri2.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright 2009, VMware, Inc.
5 * All Rights Reserved.
6 * Copyright (C) 2010 LunarG Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
29 */
30
31 #include <xf86drm.h>
32 #include "GL/mesa_glinterop.h"
33 #include "util/disk_cache.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/format/u_format.h"
37 #include "util/u_debug.h"
38 #include "frontend/drm_driver.h"
39 #include "state_tracker/st_cb_bufferobjects.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_texture.h"
43 #include "state_tracker/st_context.h"
44 #include "pipe-loader/pipe_loader.h"
45 #include "main/bufferobj.h"
46 #include "main/texobj.h"
47
48 #include "dri_util.h"
49
50 #include "dri_helpers.h"
51 #include "dri_drawable.h"
52 #include "dri_query_renderer.h"
53
54 #include "drm-uapi/drm_fourcc.h"
55
56 struct dri2_buffer
57 {
58 __DRIbuffer base;
59 struct pipe_resource *resource;
60 };
61
62 static inline struct dri2_buffer *
63 dri2_buffer(__DRIbuffer * driBufferPriv)
64 {
65 return (struct dri2_buffer *) driBufferPriv;
66 }
67
68 /**
69 * DRI2 flush extension.
70 */
71 static void
72 dri2_flush_drawable(__DRIdrawable *dPriv)
73 {
74 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
75 }
76
77 static void
78 dri2_invalidate_drawable(__DRIdrawable *dPriv)
79 {
80 struct dri_drawable *drawable = dri_drawable(dPriv);
81
82 dri2InvalidateDrawable(dPriv);
83 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
84 drawable->texture_mask = 0;
85
86 p_atomic_inc(&drawable->base.stamp);
87 }
88
89 static const __DRI2flushExtension dri2FlushExtension = {
90 .base = { __DRI2_FLUSH, 4 },
91
92 .flush = dri2_flush_drawable,
93 .invalidate = dri2_invalidate_drawable,
94 .flush_with_flags = dri_flush,
95 };
96
97 /**
98 * Retrieve __DRIbuffer from the DRI loader.
99 */
100 static __DRIbuffer *
101 dri2_drawable_get_buffers(struct dri_drawable *drawable,
102 const enum st_attachment_type *atts,
103 unsigned *count)
104 {
105 __DRIdrawable *dri_drawable = drawable->dPriv;
106 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
107 boolean with_format;
108 __DRIbuffer *buffers;
109 int num_buffers;
110 unsigned attachments[10];
111 unsigned num_attachments, i;
112
113 assert(loader);
114 with_format = dri_with_format(drawable->sPriv);
115
116 num_attachments = 0;
117
118 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
119 if (!with_format)
120 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
121
122 for (i = 0; i < *count; i++) {
123 enum pipe_format format;
124 unsigned bind;
125 int att, depth;
126
127 dri_drawable_get_format(drawable, atts[i], &format, &bind);
128 if (format == PIPE_FORMAT_NONE)
129 continue;
130
131 switch (atts[i]) {
132 case ST_ATTACHMENT_FRONT_LEFT:
133 /* already added */
134 if (!with_format)
135 continue;
136 att = __DRI_BUFFER_FRONT_LEFT;
137 break;
138 case ST_ATTACHMENT_BACK_LEFT:
139 att = __DRI_BUFFER_BACK_LEFT;
140 break;
141 case ST_ATTACHMENT_FRONT_RIGHT:
142 att = __DRI_BUFFER_FRONT_RIGHT;
143 break;
144 case ST_ATTACHMENT_BACK_RIGHT:
145 att = __DRI_BUFFER_BACK_RIGHT;
146 break;
147 default:
148 continue;
149 }
150
151 /*
152 * In this switch statement we must support all formats that
153 * may occur as the stvis->color_format.
154 */
155 switch(format) {
156 case PIPE_FORMAT_R16G16B16A16_FLOAT:
157 depth = 64;
158 break;
159 case PIPE_FORMAT_R16G16B16X16_FLOAT:
160 depth = 48;
161 break;
162 case PIPE_FORMAT_B10G10R10A2_UNORM:
163 case PIPE_FORMAT_R10G10B10A2_UNORM:
164 case PIPE_FORMAT_BGRA8888_UNORM:
165 case PIPE_FORMAT_RGBA8888_UNORM:
166 depth = 32;
167 break;
168 case PIPE_FORMAT_R10G10B10X2_UNORM:
169 case PIPE_FORMAT_B10G10R10X2_UNORM:
170 depth = 30;
171 break;
172 case PIPE_FORMAT_BGRX8888_UNORM:
173 case PIPE_FORMAT_RGBX8888_UNORM:
174 depth = 24;
175 break;
176 case PIPE_FORMAT_B5G6R5_UNORM:
177 depth = 16;
178 break;
179 default:
180 depth = util_format_get_blocksizebits(format);
181 assert(!"Unexpected format in dri2_drawable_get_buffers()");
182 }
183
184 attachments[num_attachments++] = att;
185 if (with_format) {
186 attachments[num_attachments++] = depth;
187 }
188 }
189
190 if (with_format) {
191 num_attachments /= 2;
192 buffers = loader->getBuffersWithFormat(dri_drawable,
193 &dri_drawable->w, &dri_drawable->h,
194 attachments, num_attachments,
195 &num_buffers, dri_drawable->loaderPrivate);
196 }
197 else {
198 buffers = loader->getBuffers(dri_drawable,
199 &dri_drawable->w, &dri_drawable->h,
200 attachments, num_attachments,
201 &num_buffers, dri_drawable->loaderPrivate);
202 }
203
204 if (buffers)
205 *count = num_buffers;
206
207 return buffers;
208 }
209
210 static bool
211 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
212 struct __DRIimageList *images,
213 const enum st_attachment_type *statts,
214 unsigned statts_count)
215 {
216 __DRIdrawable *dPriv = drawable->dPriv;
217 __DRIscreen *sPriv = drawable->sPriv;
218 unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
219 enum pipe_format pf;
220 uint32_t buffer_mask = 0;
221 unsigned i, bind;
222
223 for (i = 0; i < statts_count; i++) {
224 dri_drawable_get_format(drawable, statts[i], &pf, &bind);
225 if (pf == PIPE_FORMAT_NONE)
226 continue;
227
228 switch (statts[i]) {
229 case ST_ATTACHMENT_FRONT_LEFT:
230 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
231 break;
232 case ST_ATTACHMENT_BACK_LEFT:
233 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
234 break;
235 default:
236 continue;
237 }
238
239 switch (pf) {
240 case PIPE_FORMAT_R16G16B16A16_FLOAT:
241 image_format = __DRI_IMAGE_FORMAT_ABGR16161616F;
242 break;
243 case PIPE_FORMAT_R16G16B16X16_FLOAT:
244 image_format = __DRI_IMAGE_FORMAT_XBGR16161616F;
245 break;
246 case PIPE_FORMAT_B5G5R5A1_UNORM:
247 image_format = __DRI_IMAGE_FORMAT_ARGB1555;
248 break;
249 case PIPE_FORMAT_B5G6R5_UNORM:
250 image_format = __DRI_IMAGE_FORMAT_RGB565;
251 break;
252 case PIPE_FORMAT_BGRX8888_UNORM:
253 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
254 break;
255 case PIPE_FORMAT_BGRA8888_UNORM:
256 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
257 break;
258 case PIPE_FORMAT_RGBX8888_UNORM:
259 image_format = __DRI_IMAGE_FORMAT_XBGR8888;
260 break;
261 case PIPE_FORMAT_RGBA8888_UNORM:
262 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
263 break;
264 case PIPE_FORMAT_B10G10R10X2_UNORM:
265 image_format = __DRI_IMAGE_FORMAT_XRGB2101010;
266 break;
267 case PIPE_FORMAT_B10G10R10A2_UNORM:
268 image_format = __DRI_IMAGE_FORMAT_ARGB2101010;
269 break;
270 case PIPE_FORMAT_R10G10B10X2_UNORM:
271 image_format = __DRI_IMAGE_FORMAT_XBGR2101010;
272 break;
273 case PIPE_FORMAT_R10G10B10A2_UNORM:
274 image_format = __DRI_IMAGE_FORMAT_ABGR2101010;
275 break;
276 default:
277 image_format = __DRI_IMAGE_FORMAT_NONE;
278 break;
279 }
280 }
281
282 return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
283 (uint32_t *) &drawable->base.stamp,
284 dPriv->loaderPrivate, buffer_mask,
285 images);
286 }
287
288 static __DRIbuffer *
289 dri2_allocate_buffer(__DRIscreen *sPriv,
290 unsigned attachment, unsigned format,
291 int width, int height)
292 {
293 struct dri_screen *screen = dri_screen(sPriv);
294 struct dri2_buffer *buffer;
295 struct pipe_resource templ;
296 enum pipe_format pf;
297 unsigned bind = 0;
298 struct winsys_handle whandle;
299
300 switch (attachment) {
301 case __DRI_BUFFER_FRONT_LEFT:
302 case __DRI_BUFFER_FAKE_FRONT_LEFT:
303 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
304 break;
305 case __DRI_BUFFER_BACK_LEFT:
306 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
307 break;
308 case __DRI_BUFFER_DEPTH:
309 case __DRI_BUFFER_DEPTH_STENCIL:
310 case __DRI_BUFFER_STENCIL:
311 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
312 break;
313 }
314
315 /* because we get the handle and stride */
316 bind |= PIPE_BIND_SHARED;
317
318 switch (format) {
319 case 64:
320 pf = PIPE_FORMAT_R16G16B16A16_FLOAT;
321 break;
322 case 48:
323 pf = PIPE_FORMAT_R16G16B16X16_FLOAT;
324 break;
325 case 32:
326 pf = PIPE_FORMAT_BGRA8888_UNORM;
327 break;
328 case 30:
329 pf = PIPE_FORMAT_B10G10R10X2_UNORM;
330 break;
331 case 24:
332 pf = PIPE_FORMAT_BGRX8888_UNORM;
333 break;
334 case 16:
335 pf = PIPE_FORMAT_Z16_UNORM;
336 break;
337 default:
338 return NULL;
339 }
340
341 buffer = CALLOC_STRUCT(dri2_buffer);
342 if (!buffer)
343 return NULL;
344
345 memset(&templ, 0, sizeof(templ));
346 templ.bind = bind;
347 templ.format = pf;
348 templ.target = PIPE_TEXTURE_2D;
349 templ.last_level = 0;
350 templ.width0 = width;
351 templ.height0 = height;
352 templ.depth0 = 1;
353 templ.array_size = 1;
354
355 buffer->resource =
356 screen->base.screen->resource_create(screen->base.screen, &templ);
357 if (!buffer->resource) {
358 FREE(buffer);
359 return NULL;
360 }
361
362 memset(&whandle, 0, sizeof(whandle));
363 if (screen->can_share_buffer)
364 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
365 else
366 whandle.type = WINSYS_HANDLE_TYPE_KMS;
367
368 screen->base.screen->resource_get_handle(screen->base.screen, NULL,
369 buffer->resource, &whandle,
370 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
371
372 buffer->base.attachment = attachment;
373 buffer->base.name = whandle.handle;
374 buffer->base.cpp = util_format_get_blocksize(pf);
375 buffer->base.pitch = whandle.stride;
376
377 return &buffer->base;
378 }
379
380 static void
381 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
382 {
383 struct dri2_buffer *buffer = dri2_buffer(bPriv);
384
385 pipe_resource_reference(&buffer->resource, NULL);
386 FREE(buffer);
387 }
388
389 /*
390 * Backend functions for st_framebuffer interface.
391 */
392
393 static void
394 dri2_allocate_textures(struct dri_context *ctx,
395 struct dri_drawable *drawable,
396 const enum st_attachment_type *statts,
397 unsigned statts_count)
398 {
399 __DRIscreen *sPriv = drawable->sPriv;
400 __DRIdrawable *dri_drawable = drawable->dPriv;
401 struct dri_screen *screen = dri_screen(sPriv);
402 struct pipe_resource templ;
403 boolean alloc_depthstencil = FALSE;
404 unsigned i, j, bind;
405 const __DRIimageLoaderExtension *image = sPriv->image.loader;
406 /* Image specific variables */
407 struct __DRIimageList images;
408 /* Dri2 specific variables */
409 __DRIbuffer *buffers = NULL;
410 struct winsys_handle whandle;
411 unsigned num_buffers = statts_count;
412
413 /* First get the buffers from the loader */
414 if (image) {
415 if (!dri_image_drawable_get_buffers(drawable, &images,
416 statts, statts_count))
417 return;
418 }
419 else {
420 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
421 if (!buffers || (drawable->old_num == num_buffers &&
422 drawable->old_w == dri_drawable->w &&
423 drawable->old_h == dri_drawable->h &&
424 memcmp(drawable->old, buffers,
425 sizeof(__DRIbuffer) * num_buffers) == 0))
426 return;
427 }
428
429 /* Second clean useless resources*/
430
431 /* See if we need a depth-stencil buffer. */
432 for (i = 0; i < statts_count; i++) {
433 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
434 alloc_depthstencil = TRUE;
435 break;
436 }
437 }
438
439 /* Delete the resources we won't need. */
440 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
441 /* Don't delete the depth-stencil buffer, we can reuse it. */
442 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
443 continue;
444
445 /* Flush the texture before unreferencing, so that other clients can
446 * see what the driver has rendered.
447 */
448 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
449 struct pipe_context *pipe = ctx->st->pipe;
450 pipe->flush_resource(pipe, drawable->textures[i]);
451 }
452
453 pipe_resource_reference(&drawable->textures[i], NULL);
454 }
455
456 if (drawable->stvis.samples > 1) {
457 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
458 boolean del = TRUE;
459
460 /* Don't delete MSAA resources for the attachments which are enabled,
461 * we can reuse them. */
462 for (j = 0; j < statts_count; j++) {
463 if (i == statts[j]) {
464 del = FALSE;
465 break;
466 }
467 }
468
469 if (del) {
470 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
471 }
472 }
473 }
474
475 /* Third use the buffers retrieved to fill the drawable info */
476
477 memset(&templ, 0, sizeof(templ));
478 templ.target = screen->target;
479 templ.last_level = 0;
480 templ.depth0 = 1;
481 templ.array_size = 1;
482
483 if (image) {
484 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
485 struct pipe_resource **buf =
486 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
487 struct pipe_resource *texture = images.front->texture;
488
489 dri_drawable->w = texture->width0;
490 dri_drawable->h = texture->height0;
491
492 pipe_resource_reference(buf, texture);
493 }
494
495 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
496 struct pipe_resource **buf =
497 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
498 struct pipe_resource *texture = images.back->texture;
499
500 dri_drawable->w = texture->width0;
501 dri_drawable->h = texture->height0;
502
503 pipe_resource_reference(buf, texture);
504 }
505
506 /* Note: if there is both a back and a front buffer,
507 * then they have the same size.
508 */
509 templ.width0 = dri_drawable->w;
510 templ.height0 = dri_drawable->h;
511 }
512 else {
513 memset(&whandle, 0, sizeof(whandle));
514
515 /* Process DRI-provided buffers and get pipe_resources. */
516 for (i = 0; i < num_buffers; i++) {
517 __DRIbuffer *buf = &buffers[i];
518 enum st_attachment_type statt;
519 enum pipe_format format;
520
521 switch (buf->attachment) {
522 case __DRI_BUFFER_FRONT_LEFT:
523 if (!screen->auto_fake_front) {
524 continue; /* invalid attachment */
525 }
526 /* fallthrough */
527 case __DRI_BUFFER_FAKE_FRONT_LEFT:
528 statt = ST_ATTACHMENT_FRONT_LEFT;
529 break;
530 case __DRI_BUFFER_BACK_LEFT:
531 statt = ST_ATTACHMENT_BACK_LEFT;
532 break;
533 default:
534 continue; /* invalid attachment */
535 }
536
537 dri_drawable_get_format(drawable, statt, &format, &bind);
538 if (format == PIPE_FORMAT_NONE)
539 continue;
540
541 /* dri2_drawable_get_buffers has already filled dri_drawable->w
542 * and dri_drawable->h */
543 templ.width0 = dri_drawable->w;
544 templ.height0 = dri_drawable->h;
545 templ.format = format;
546 templ.bind = bind;
547 whandle.handle = buf->name;
548 whandle.stride = buf->pitch;
549 whandle.offset = 0;
550 whandle.format = format;
551 whandle.modifier = DRM_FORMAT_MOD_INVALID;
552 if (screen->can_share_buffer)
553 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
554 else
555 whandle.type = WINSYS_HANDLE_TYPE_KMS;
556 drawable->textures[statt] =
557 screen->base.screen->resource_from_handle(screen->base.screen,
558 &templ, &whandle,
559 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
560 assert(drawable->textures[statt]);
561 }
562 }
563
564 /* Allocate private MSAA colorbuffers. */
565 if (drawable->stvis.samples > 1) {
566 for (i = 0; i < statts_count; i++) {
567 enum st_attachment_type statt = statts[i];
568
569 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
570 continue;
571
572 if (drawable->textures[statt]) {
573 templ.format = drawable->textures[statt]->format;
574 templ.bind = drawable->textures[statt]->bind &
575 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
576 templ.nr_samples = drawable->stvis.samples;
577 templ.nr_storage_samples = drawable->stvis.samples;
578
579 /* Try to reuse the resource.
580 * (the other resource parameters should be constant)
581 */
582 if (!drawable->msaa_textures[statt] ||
583 drawable->msaa_textures[statt]->width0 != templ.width0 ||
584 drawable->msaa_textures[statt]->height0 != templ.height0) {
585 /* Allocate a new one. */
586 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
587
588 drawable->msaa_textures[statt] =
589 screen->base.screen->resource_create(screen->base.screen,
590 &templ);
591 assert(drawable->msaa_textures[statt]);
592
593 /* If there are any MSAA resources, we should initialize them
594 * such that they contain the same data as the single-sample
595 * resources we just got from the X server.
596 *
597 * The reason for this is that the gallium frontend (and
598 * therefore the app) can access the MSAA resources only.
599 * The single-sample resources are not exposed
600 * to the gallium frontend.
601 *
602 */
603 dri_pipe_blit(ctx->st->pipe,
604 drawable->msaa_textures[statt],
605 drawable->textures[statt]);
606 }
607 }
608 else {
609 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
610 }
611 }
612 }
613
614 /* Allocate a private depth-stencil buffer. */
615 if (alloc_depthstencil) {
616 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
617 struct pipe_resource **zsbuf;
618 enum pipe_format format;
619 unsigned bind;
620
621 dri_drawable_get_format(drawable, statt, &format, &bind);
622
623 if (format) {
624 templ.format = format;
625 templ.bind = bind & ~PIPE_BIND_SHARED;
626
627 if (drawable->stvis.samples > 1) {
628 templ.nr_samples = drawable->stvis.samples;
629 templ.nr_storage_samples = drawable->stvis.samples;
630 zsbuf = &drawable->msaa_textures[statt];
631 }
632 else {
633 templ.nr_samples = 0;
634 templ.nr_storage_samples = 0;
635 zsbuf = &drawable->textures[statt];
636 }
637
638 /* Try to reuse the resource.
639 * (the other resource parameters should be constant)
640 */
641 if (!*zsbuf ||
642 (*zsbuf)->width0 != templ.width0 ||
643 (*zsbuf)->height0 != templ.height0) {
644 /* Allocate a new one. */
645 pipe_resource_reference(zsbuf, NULL);
646 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
647 &templ);
648 assert(*zsbuf);
649 }
650 }
651 else {
652 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
653 pipe_resource_reference(&drawable->textures[statt], NULL);
654 }
655 }
656
657 /* For DRI2, we may get the same buffers again from the server.
658 * To prevent useless imports of gem names, drawable->old* is used
659 * to bypass the import if we get the same buffers. This doesn't apply
660 * to DRI3/Wayland, users of image.loader, since the buffer is managed
661 * by the client (no import), and the back buffer is going to change
662 * at every redraw.
663 */
664 if (!image) {
665 drawable->old_num = num_buffers;
666 drawable->old_w = dri_drawable->w;
667 drawable->old_h = dri_drawable->h;
668 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
669 }
670 }
671
672 static void
673 dri2_flush_frontbuffer(struct dri_context *ctx,
674 struct dri_drawable *drawable,
675 enum st_attachment_type statt)
676 {
677 __DRIdrawable *dri_drawable = drawable->dPriv;
678 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
679 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
680 struct pipe_context *pipe = ctx->st->pipe;
681
682 if (statt != ST_ATTACHMENT_FRONT_LEFT)
683 return;
684
685 if (drawable->stvis.samples > 1) {
686 /* Resolve the front buffer. */
687 dri_pipe_blit(ctx->st->pipe,
688 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
689 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
690 }
691
692 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
693 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
694 }
695
696 pipe->flush(pipe, NULL, 0);
697
698 if (image) {
699 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
700 }
701 else if (loader->flushFrontBuffer) {
702 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
703 }
704 }
705
706 /**
707 * The struct dri_drawable flush_swapbuffers callback
708 */
709 static void
710 dri2_flush_swapbuffers(struct dri_context *ctx,
711 struct dri_drawable *drawable)
712 {
713 __DRIdrawable *dri_drawable = drawable->dPriv;
714 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
715
716 if (image && image->base.version >= 3 && image->flushSwapBuffers) {
717 image->flushSwapBuffers(dri_drawable, dri_drawable->loaderPrivate);
718 }
719 }
720
721 static void
722 dri2_update_tex_buffer(struct dri_drawable *drawable,
723 struct dri_context *ctx,
724 struct pipe_resource *res)
725 {
726 /* no-op */
727 }
728
729 static __DRIimage *
730 dri2_create_image_from_winsys(__DRIscreen *_screen,
731 int width, int height, const struct dri2_format_mapping *map,
732 int num_handles, struct winsys_handle *whandle,
733 void *loaderPrivate)
734 {
735 struct dri_screen *screen = dri_screen(_screen);
736 struct pipe_screen *pscreen = screen->base.screen;
737 __DRIimage *img;
738 struct pipe_resource templ;
739 unsigned tex_usage = 0;
740 int i;
741 bool use_lowered = false;
742
743 if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
744 PIPE_BIND_RENDER_TARGET))
745 tex_usage |= PIPE_BIND_RENDER_TARGET;
746 if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
747 PIPE_BIND_SAMPLER_VIEW))
748 tex_usage |= PIPE_BIND_SAMPLER_VIEW;
749
750 if (!tex_usage && util_format_is_yuv(map->pipe_format)) {
751 /* YUV format sampling can be emulated by the GL gallium frontend by
752 * using multiple samplers of varying formats.
753 * If no tex_usage is set and we detect a YUV format,
754 * test for support of all planes' sampler formats and
755 * add sampler view usage.
756 */
757 use_lowered = true;
758 if (dri2_yuv_dma_buf_supported(screen, map))
759 tex_usage |= PIPE_BIND_SAMPLER_VIEW;
760 }
761
762 if (!tex_usage)
763 return NULL;
764
765 img = CALLOC_STRUCT(__DRIimageRec);
766 if (!img)
767 return NULL;
768
769 memset(&templ, 0, sizeof(templ));
770 templ.bind = tex_usage;
771 templ.target = screen->target;
772 templ.last_level = 0;
773 templ.depth0 = 1;
774 templ.array_size = 1;
775
776 for (i = (use_lowered ? map->nplanes : num_handles) - 1; i >= 0; i--) {
777 struct pipe_resource *tex;
778
779 templ.next = img->texture;
780 templ.width0 = width >> map->planes[i].width_shift;
781 templ.height0 = height >> map->planes[i].height_shift;
782 if (use_lowered)
783 templ.format = dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format);
784 else
785 templ.format = map->pipe_format;
786 assert(templ.format != PIPE_FORMAT_NONE);
787
788 tex = pscreen->resource_from_handle(pscreen,
789 &templ, &whandle[use_lowered ? map->planes[i].buffer_index : i],
790 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
791 if (!tex) {
792 pipe_resource_reference(&img->texture, NULL);
793 FREE(img);
794 return NULL;
795 }
796
797 img->texture = tex;
798 }
799
800 img->level = 0;
801 img->layer = 0;
802 img->use = 0;
803 img->loader_private = loaderPrivate;
804
805 return img;
806 }
807
808 static __DRIimage *
809 dri2_create_image_from_name(__DRIscreen *_screen,
810 int width, int height, int format,
811 int name, int pitch, void *loaderPrivate)
812 {
813 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
814 struct winsys_handle whandle;
815 __DRIimage *img;
816
817 if (!map)
818 return NULL;
819
820 memset(&whandle, 0, sizeof(whandle));
821 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
822 whandle.handle = name;
823 whandle.format = map->pipe_format;
824 whandle.modifier = DRM_FORMAT_MOD_INVALID;
825
826 whandle.stride = pitch * util_format_get_blocksize(map->pipe_format);
827
828 img = dri2_create_image_from_winsys(_screen, width, height, map,
829 1, &whandle, loaderPrivate);
830
831 if (!img)
832 return NULL;
833
834 img->dri_components = map->dri_components;
835 img->dri_fourcc = map->dri_fourcc;
836 img->dri_format = map->dri_format;
837
838 return img;
839 }
840
841 static unsigned
842 dri2_get_modifier_num_planes(uint64_t modifier, int fourcc)
843 {
844 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
845
846 if (!map)
847 return 0;
848
849 switch (modifier) {
850 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
851 case I915_FORMAT_MOD_Y_TILED_CCS:
852 return 2;
853 case DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED:
854 case DRM_FORMAT_MOD_ARM_AFBC(
855 AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 |
856 AFBC_FORMAT_MOD_SPARSE |
857 AFBC_FORMAT_MOD_YTR):
858 case DRM_FORMAT_MOD_ARM_AFBC(
859 AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 |
860 AFBC_FORMAT_MOD_SPARSE):
861 case DRM_FORMAT_MOD_BROADCOM_UIF:
862 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
863 case DRM_FORMAT_MOD_LINEAR:
864 /* DRM_FORMAT_MOD_NONE is the same as LINEAR */
865 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_EIGHT_GOB:
866 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_FOUR_GOB:
867 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_ONE_GOB:
868 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_SIXTEEN_GOB:
869 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_THIRTYTWO_GOB:
870 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_TWO_GOB:
871 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
872 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
873 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
874 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
875 case DRM_FORMAT_MOD_VIVANTE_TILED:
876 /* FD_FORMAT_MOD_QCOM_TILED is not in drm_fourcc.h */
877 case I915_FORMAT_MOD_X_TILED:
878 case I915_FORMAT_MOD_Y_TILED:
879 case DRM_FORMAT_MOD_INVALID:
880 return map->nplanes;
881 default:
882 return 0;
883 }
884 }
885
886 static __DRIimage *
887 dri2_create_image_from_fd(__DRIscreen *_screen,
888 int width, int height, int fourcc,
889 uint64_t modifier, int *fds, int num_fds,
890 int *strides, int *offsets, unsigned *error,
891 void *loaderPrivate)
892 {
893 struct winsys_handle whandles[3];
894 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
895 __DRIimage *img = NULL;
896 unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
897 int i, expected_num_fds;
898 int num_handles = dri2_get_modifier_num_planes(modifier, fourcc);
899
900 if (!map || num_handles == 0) {
901 err = __DRI_IMAGE_ERROR_BAD_MATCH;
902 goto exit;
903 }
904
905 switch (fourcc) {
906 case DRM_FORMAT_YUYV:
907 case DRM_FORMAT_UYVY:
908 expected_num_fds = 1;
909 break;
910 default:
911 expected_num_fds = num_handles;
912 break;
913 }
914
915 if (num_fds != expected_num_fds) {
916 err = __DRI_IMAGE_ERROR_BAD_MATCH;
917 goto exit;
918 }
919
920 memset(whandles, 0, sizeof(whandles));
921
922 for (i = 0; i < num_fds; i++) {
923 if (fds[i] < 0) {
924 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
925 goto exit;
926 }
927
928 whandles[i].type = WINSYS_HANDLE_TYPE_FD;
929 whandles[i].handle = (unsigned)fds[i];
930 whandles[i].stride = (unsigned)strides[i];
931 whandles[i].offset = (unsigned)offsets[i];
932 whandles[i].format = map->pipe_format;
933 whandles[i].modifier = modifier;
934 whandles[i].plane = i;
935 }
936
937 img = dri2_create_image_from_winsys(_screen, width, height, map,
938 num_fds, whandles, loaderPrivate);
939 if(img == NULL) {
940 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
941 goto exit;
942 }
943
944 img->dri_components = map->dri_components;
945 img->dri_fourcc = fourcc;
946 img->dri_format = map->dri_format;
947 img->imported_dmabuf = TRUE;
948
949 exit:
950 if (error)
951 *error = err;
952
953 return img;
954 }
955
956 static __DRIimage *
957 dri2_create_image_common(__DRIscreen *_screen,
958 int width, int height,
959 int format, unsigned int use,
960 const uint64_t *modifiers,
961 const unsigned count,
962 void *loaderPrivate)
963 {
964 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
965 struct dri_screen *screen = dri_screen(_screen);
966 __DRIimage *img;
967 struct pipe_resource templ;
968 unsigned tex_usage;
969
970 if (!map)
971 return NULL;
972
973 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
974
975 if (use & __DRI_IMAGE_USE_SCANOUT)
976 tex_usage |= PIPE_BIND_SCANOUT;
977 if (use & __DRI_IMAGE_USE_SHARE)
978 tex_usage |= PIPE_BIND_SHARED;
979 if (use & __DRI_IMAGE_USE_LINEAR)
980 tex_usage |= PIPE_BIND_LINEAR;
981 if (use & __DRI_IMAGE_USE_CURSOR) {
982 if (width != 64 || height != 64)
983 return NULL;
984 tex_usage |= PIPE_BIND_CURSOR;
985 }
986
987 img = CALLOC_STRUCT(__DRIimageRec);
988 if (!img)
989 return NULL;
990
991 memset(&templ, 0, sizeof(templ));
992 templ.bind = tex_usage;
993 templ.format = map->pipe_format;
994 templ.target = PIPE_TEXTURE_2D;
995 templ.last_level = 0;
996 templ.width0 = width;
997 templ.height0 = height;
998 templ.depth0 = 1;
999 templ.array_size = 1;
1000
1001 if (modifiers)
1002 img->texture =
1003 screen->base.screen
1004 ->resource_create_with_modifiers(screen->base.screen,
1005 &templ,
1006 modifiers,
1007 count);
1008 else
1009 img->texture =
1010 screen->base.screen->resource_create(screen->base.screen, &templ);
1011 if (!img->texture) {
1012 FREE(img);
1013 return NULL;
1014 }
1015
1016 img->level = 0;
1017 img->layer = 0;
1018 img->dri_format = format;
1019 img->dri_fourcc = map->dri_fourcc;
1020 img->dri_components = 0;
1021 img->use = use;
1022
1023 img->loader_private = loaderPrivate;
1024 return img;
1025 }
1026
1027 static __DRIimage *
1028 dri2_create_image(__DRIscreen *_screen,
1029 int width, int height, int format,
1030 unsigned int use, void *loaderPrivate)
1031 {
1032 return dri2_create_image_common(_screen, width, height, format, use,
1033 NULL /* modifiers */, 0 /* count */,
1034 loaderPrivate);
1035 }
1036
1037 static __DRIimage *
1038 dri2_create_image_with_modifiers(__DRIscreen *dri_screen,
1039 int width, int height, int format,
1040 const uint64_t *modifiers,
1041 const unsigned count,
1042 void *loaderPrivate)
1043 {
1044 return dri2_create_image_common(dri_screen, width, height, format,
1045 __DRI_IMAGE_USE_SHARE, modifiers, count,
1046 loaderPrivate);
1047 }
1048
1049 static bool
1050 dri2_query_image_common(__DRIimage *image, int attrib, int *value)
1051 {
1052 switch (attrib) {
1053 case __DRI_IMAGE_ATTRIB_FORMAT:
1054 *value = image->dri_format;
1055 return true;
1056 case __DRI_IMAGE_ATTRIB_WIDTH:
1057 *value = image->texture->width0;
1058 return true;
1059 case __DRI_IMAGE_ATTRIB_HEIGHT:
1060 *value = image->texture->height0;
1061 return true;
1062 case __DRI_IMAGE_ATTRIB_COMPONENTS:
1063 if (image->dri_components == 0)
1064 return false;
1065 *value = image->dri_components;
1066 return true;
1067 case __DRI_IMAGE_ATTRIB_FOURCC:
1068 if (image->dri_fourcc) {
1069 *value = image->dri_fourcc;
1070 } else {
1071 const struct dri2_format_mapping *map;
1072
1073 map = dri2_get_mapping_by_format(image->dri_format);
1074 if (!map)
1075 return false;
1076
1077 *value = map->dri_fourcc;
1078 }
1079 return true;
1080 default:
1081 return false;
1082 }
1083 }
1084
1085 static bool
1086 dri2_query_image_by_resource_handle(__DRIimage *image, int attrib, int *value)
1087 {
1088 struct pipe_screen *pscreen = image->texture->screen;
1089 struct winsys_handle whandle;
1090 unsigned usage;
1091 memset(&whandle, 0, sizeof(whandle));
1092 whandle.plane = image->plane;
1093
1094 switch (attrib) {
1095 case __DRI_IMAGE_ATTRIB_STRIDE:
1096 case __DRI_IMAGE_ATTRIB_OFFSET:
1097 case __DRI_IMAGE_ATTRIB_HANDLE:
1098 whandle.type = WINSYS_HANDLE_TYPE_KMS;
1099 break;
1100 case __DRI_IMAGE_ATTRIB_NAME:
1101 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1102 break;
1103 case __DRI_IMAGE_ATTRIB_FD:
1104 whandle.type = WINSYS_HANDLE_TYPE_FD;
1105 break;
1106 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1107 *value = 1;
1108 return true;
1109 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1110 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1111 whandle.type = WINSYS_HANDLE_TYPE_KMS;
1112 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1113 break;
1114 default:
1115 return false;
1116 }
1117
1118 usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1119
1120 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1121 usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1122
1123 if (!pscreen->resource_get_handle(pscreen, NULL, image->texture,
1124 &whandle, usage))
1125 return false;
1126
1127 switch (attrib) {
1128 case __DRI_IMAGE_ATTRIB_STRIDE:
1129 *value = whandle.stride;
1130 return true;
1131 case __DRI_IMAGE_ATTRIB_OFFSET:
1132 *value = whandle.offset;
1133 return true;
1134 case __DRI_IMAGE_ATTRIB_HANDLE:
1135 case __DRI_IMAGE_ATTRIB_NAME:
1136 case __DRI_IMAGE_ATTRIB_FD:
1137 *value = whandle.handle;
1138 return true;
1139 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1140 if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1141 return false;
1142 *value = (whandle.modifier >> 32) & 0xffffffff;
1143 return true;
1144 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1145 if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1146 return false;
1147 *value = whandle.modifier & 0xffffffff;
1148 return true;
1149 default:
1150 return false;
1151 }
1152 }
1153
1154 static bool
1155 dri2_resource_get_param(__DRIimage *image, enum pipe_resource_param param,
1156 unsigned handle_usage, uint64_t *value)
1157 {
1158 struct pipe_screen *pscreen = image->texture->screen;
1159 if (!pscreen->resource_get_param)
1160 return false;
1161
1162 return pscreen->resource_get_param(pscreen, NULL, image->texture,
1163 image->plane, 0, param, handle_usage,
1164 value);
1165 }
1166
1167 static bool
1168 dri2_query_image_by_resource_param(__DRIimage *image, int attrib, int *value)
1169 {
1170 enum pipe_resource_param param;
1171 uint64_t res_param;
1172 unsigned handle_usage;
1173
1174 if (!image->texture->screen->resource_get_param)
1175 return false;
1176
1177 switch (attrib) {
1178 case __DRI_IMAGE_ATTRIB_STRIDE:
1179 param = PIPE_RESOURCE_PARAM_STRIDE;
1180 break;
1181 case __DRI_IMAGE_ATTRIB_OFFSET:
1182 param = PIPE_RESOURCE_PARAM_OFFSET;
1183 break;
1184 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1185 param = PIPE_RESOURCE_PARAM_NPLANES;
1186 break;
1187 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1188 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1189 param = PIPE_RESOURCE_PARAM_MODIFIER;
1190 break;
1191 case __DRI_IMAGE_ATTRIB_HANDLE:
1192 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS;
1193 break;
1194 case __DRI_IMAGE_ATTRIB_NAME:
1195 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED;
1196 break;
1197 case __DRI_IMAGE_ATTRIB_FD:
1198 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD;
1199 break;
1200 default:
1201 return false;
1202 }
1203
1204 handle_usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1205
1206 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1207 handle_usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1208
1209 if (!dri2_resource_get_param(image, param, handle_usage, &res_param))
1210 return false;
1211
1212 switch (attrib) {
1213 case __DRI_IMAGE_ATTRIB_STRIDE:
1214 case __DRI_IMAGE_ATTRIB_OFFSET:
1215 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1216 if (res_param > INT_MAX)
1217 return false;
1218 *value = (int)res_param;
1219 return true;
1220 case __DRI_IMAGE_ATTRIB_HANDLE:
1221 case __DRI_IMAGE_ATTRIB_NAME:
1222 case __DRI_IMAGE_ATTRIB_FD:
1223 if (res_param > UINT_MAX)
1224 return false;
1225 *value = (int)res_param;
1226 return true;
1227 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1228 if (res_param == DRM_FORMAT_MOD_INVALID)
1229 return false;
1230 *value = (res_param >> 32) & 0xffffffff;
1231 return true;
1232 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1233 if (res_param == DRM_FORMAT_MOD_INVALID)
1234 return false;
1235 *value = res_param & 0xffffffff;
1236 return true;
1237 default:
1238 return false;
1239 }
1240 }
1241
1242 static GLboolean
1243 dri2_query_image(__DRIimage *image, int attrib, int *value)
1244 {
1245 if (dri2_query_image_common(image, attrib, value))
1246 return GL_TRUE;
1247 else if (dri2_query_image_by_resource_param(image, attrib, value))
1248 return GL_TRUE;
1249 else if (dri2_query_image_by_resource_handle(image, attrib, value))
1250 return GL_TRUE;
1251 else
1252 return GL_FALSE;
1253 }
1254
1255 static __DRIimage *
1256 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1257 {
1258 __DRIimage *img;
1259
1260 img = CALLOC_STRUCT(__DRIimageRec);
1261 if (!img)
1262 return NULL;
1263
1264 img->texture = NULL;
1265 pipe_resource_reference(&img->texture, image->texture);
1266 img->level = image->level;
1267 img->layer = image->layer;
1268 img->dri_format = image->dri_format;
1269 /* This should be 0 for sub images, but dup is also used for base images. */
1270 img->dri_components = image->dri_components;
1271 img->loader_private = loaderPrivate;
1272
1273 return img;
1274 }
1275
1276 static GLboolean
1277 dri2_validate_usage(__DRIimage *image, unsigned int use)
1278 {
1279 if (!image || !image->texture)
1280 return false;
1281
1282 struct pipe_screen *screen = image->texture->screen;
1283 if (!screen->check_resource_capability)
1284 return true;
1285
1286 /* We don't want to check these:
1287 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1288 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1289 */
1290 unsigned bind = 0;
1291 if (use & __DRI_IMAGE_USE_SCANOUT)
1292 bind |= PIPE_BIND_SCANOUT;
1293 if (use & __DRI_IMAGE_USE_LINEAR)
1294 bind |= PIPE_BIND_LINEAR;
1295 if (use & __DRI_IMAGE_USE_CURSOR)
1296 bind |= PIPE_BIND_CURSOR;
1297
1298 if (!bind)
1299 return true;
1300
1301 return screen->check_resource_capability(screen, image->texture, bind);
1302 }
1303
1304 static __DRIimage *
1305 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1306 int *names, int num_names, int *strides, int *offsets,
1307 void *loaderPrivate)
1308 {
1309 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
1310 __DRIimage *img;
1311 struct winsys_handle whandle;
1312
1313 if (!map)
1314 return NULL;
1315
1316 if (num_names != 1)
1317 return NULL;
1318
1319 memset(&whandle, 0, sizeof(whandle));
1320 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1321 whandle.handle = names[0];
1322 whandle.stride = strides[0];
1323 whandle.offset = offsets[0];
1324 whandle.format = map->pipe_format;
1325 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1326
1327 img = dri2_create_image_from_winsys(screen, width, height, map,
1328 1, &whandle, loaderPrivate);
1329 if (img == NULL)
1330 return NULL;
1331
1332 img->dri_components = map->dri_components;
1333 img->dri_fourcc = map->dri_fourcc;
1334 img->dri_format = map->pipe_format;
1335
1336 return img;
1337 }
1338
1339 static __DRIimage *
1340 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1341 {
1342 __DRIimage *img;
1343
1344 if (plane < 0) {
1345 return NULL;
1346 } else if (plane > 0) {
1347 uint64_t planes;
1348 if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_NPLANES, 0,
1349 &planes) ||
1350 plane >= planes) {
1351 return NULL;
1352 }
1353 }
1354
1355 if (image->dri_components == 0) {
1356 uint64_t modifier;
1357 if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_MODIFIER, 0,
1358 &modifier) ||
1359 modifier == DRM_FORMAT_MOD_INVALID) {
1360 return NULL;
1361 }
1362 }
1363
1364 img = dri2_dup_image(image, loaderPrivate);
1365 if (img == NULL)
1366 return NULL;
1367
1368 if (img->texture->screen->resource_changed)
1369 img->texture->screen->resource_changed(img->texture->screen,
1370 img->texture);
1371
1372 /* set this to 0 for sub images. */
1373 img->dri_components = 0;
1374 img->plane = plane;
1375 return img;
1376 }
1377
1378 static __DRIimage *
1379 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1380 int *fds, int num_fds, int *strides, int *offsets,
1381 void *loaderPrivate)
1382 {
1383 return dri2_create_image_from_fd(screen, width, height, fourcc,
1384 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1385 strides, offsets, NULL, loaderPrivate);
1386 }
1387
1388 static boolean
1389 dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1390 uint64_t *modifiers, unsigned int *external_only,
1391 int *count)
1392 {
1393 struct dri_screen *screen = dri_screen(_screen);
1394 struct pipe_screen *pscreen = screen->base.screen;
1395 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
1396 enum pipe_format format;
1397
1398 if (!map)
1399 return false;
1400
1401 format = map->pipe_format;
1402
1403 if (pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1404 PIPE_BIND_RENDER_TARGET) ||
1405 pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1406 PIPE_BIND_SAMPLER_VIEW) ||
1407 dri2_yuv_dma_buf_supported(screen, map)) {
1408 if (pscreen->query_dmabuf_modifiers != NULL)
1409 pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
1410 external_only, count);
1411 else
1412 *count = 0;
1413 return true;
1414 }
1415 return false;
1416 }
1417
1418 static boolean
1419 dri2_query_dma_buf_format_modifier_attribs(__DRIscreen *_screen,
1420 uint32_t fourcc, uint64_t modifier,
1421 int attrib, uint64_t *value)
1422 {
1423 struct dri_screen *screen = dri_screen(_screen);
1424 struct pipe_screen *pscreen = screen->base.screen;
1425
1426 if (!pscreen->query_dmabuf_modifiers)
1427 return false;
1428
1429 switch (attrib) {
1430 case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT: {
1431 uint64_t mod_planes = dri2_get_modifier_num_planes(modifier, fourcc);
1432 if (mod_planes > 0)
1433 *value = mod_planes;
1434 return mod_planes > 0;
1435 }
1436 default:
1437 return false;
1438 }
1439 }
1440
1441 static __DRIimage *
1442 dri2_from_dma_bufs(__DRIscreen *screen,
1443 int width, int height, int fourcc,
1444 int *fds, int num_fds,
1445 int *strides, int *offsets,
1446 enum __DRIYUVColorSpace yuv_color_space,
1447 enum __DRISampleRange sample_range,
1448 enum __DRIChromaSiting horizontal_siting,
1449 enum __DRIChromaSiting vertical_siting,
1450 unsigned *error,
1451 void *loaderPrivate)
1452 {
1453 __DRIimage *img;
1454
1455 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1456 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1457 strides, offsets, error, loaderPrivate);
1458 if (img == NULL)
1459 return NULL;
1460
1461 img->yuv_color_space = yuv_color_space;
1462 img->sample_range = sample_range;
1463 img->horizontal_siting = horizontal_siting;
1464 img->vertical_siting = vertical_siting;
1465
1466 *error = __DRI_IMAGE_ERROR_SUCCESS;
1467 return img;
1468 }
1469
1470 static __DRIimage *
1471 dri2_from_dma_bufs2(__DRIscreen *screen,
1472 int width, int height, int fourcc,
1473 uint64_t modifier, int *fds, int num_fds,
1474 int *strides, int *offsets,
1475 enum __DRIYUVColorSpace yuv_color_space,
1476 enum __DRISampleRange sample_range,
1477 enum __DRIChromaSiting horizontal_siting,
1478 enum __DRIChromaSiting vertical_siting,
1479 unsigned *error,
1480 void *loaderPrivate)
1481 {
1482 __DRIimage *img;
1483
1484 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1485 modifier, fds, num_fds, strides, offsets,
1486 error, loaderPrivate);
1487 if (img == NULL)
1488 return NULL;
1489
1490 img->yuv_color_space = yuv_color_space;
1491 img->sample_range = sample_range;
1492 img->horizontal_siting = horizontal_siting;
1493 img->vertical_siting = vertical_siting;
1494
1495 *error = __DRI_IMAGE_ERROR_SUCCESS;
1496 return img;
1497 }
1498
1499 static void
1500 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1501 int dstx0, int dsty0, int dstwidth, int dstheight,
1502 int srcx0, int srcy0, int srcwidth, int srcheight,
1503 int flush_flag)
1504 {
1505 struct dri_context *ctx = dri_context(context);
1506 struct pipe_context *pipe = ctx->st->pipe;
1507 struct pipe_screen *screen;
1508 struct pipe_fence_handle *fence;
1509 struct pipe_blit_info blit;
1510
1511 if (!dst || !src)
1512 return;
1513
1514 memset(&blit, 0, sizeof(blit));
1515 blit.dst.resource = dst->texture;
1516 blit.dst.box.x = dstx0;
1517 blit.dst.box.y = dsty0;
1518 blit.dst.box.width = dstwidth;
1519 blit.dst.box.height = dstheight;
1520 blit.dst.box.depth = 1;
1521 blit.dst.format = dst->texture->format;
1522 blit.src.resource = src->texture;
1523 blit.src.box.x = srcx0;
1524 blit.src.box.y = srcy0;
1525 blit.src.box.width = srcwidth;
1526 blit.src.box.height = srcheight;
1527 blit.src.box.depth = 1;
1528 blit.src.format = src->texture->format;
1529 blit.mask = PIPE_MASK_RGBA;
1530 blit.filter = PIPE_TEX_FILTER_NEAREST;
1531
1532 pipe->blit(pipe, &blit);
1533
1534 if (flush_flag == __BLIT_FLAG_FLUSH) {
1535 pipe->flush_resource(pipe, dst->texture);
1536 ctx->st->flush(ctx->st, 0, NULL, NULL, NULL);
1537 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1538 screen = dri_screen(ctx->sPriv)->base.screen;
1539 pipe->flush_resource(pipe, dst->texture);
1540 ctx->st->flush(ctx->st, 0, &fence, NULL, NULL);
1541 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
1542 screen->fence_reference(screen, &fence, NULL);
1543 }
1544 }
1545
1546 static void *
1547 dri2_map_image(__DRIcontext *context, __DRIimage *image,
1548 int x0, int y0, int width, int height,
1549 unsigned int flags, int *stride, void **data)
1550 {
1551 struct dri_context *ctx = dri_context(context);
1552 struct pipe_context *pipe = ctx->st->pipe;
1553 enum pipe_transfer_usage pipe_access = 0;
1554 struct pipe_resource *resource = image->texture;
1555 struct pipe_transfer *trans;
1556 void *map;
1557
1558 if (!image || !data || *data)
1559 return NULL;
1560
1561 unsigned plane = image->plane;
1562 if (plane >= dri2_get_mapping_by_format(image->dri_format)->nplanes)
1563 return NULL;
1564
1565 while (plane--)
1566 resource = resource->next;
1567
1568 if (flags & __DRI_IMAGE_TRANSFER_READ)
1569 pipe_access |= PIPE_TRANSFER_READ;
1570 if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1571 pipe_access |= PIPE_TRANSFER_WRITE;
1572
1573 map = pipe_transfer_map(pipe, resource, 0, 0, pipe_access, x0, y0,
1574 width, height, &trans);
1575 if (map) {
1576 *data = trans;
1577 *stride = trans->stride;
1578 }
1579
1580 return map;
1581 }
1582
1583 static void
1584 dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1585 {
1586 struct dri_context *ctx = dri_context(context);
1587 struct pipe_context *pipe = ctx->st->pipe;
1588
1589 pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
1590 }
1591
1592 static int
1593 dri2_get_capabilities(__DRIscreen *_screen)
1594 {
1595 struct dri_screen *screen = dri_screen(_screen);
1596
1597 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1598 }
1599
1600 /* The extension is modified during runtime if DRI_PRIME is detected */
1601 static __DRIimageExtension dri2ImageExtension = {
1602 .base = { __DRI_IMAGE, 17 },
1603
1604 .createImageFromName = dri2_create_image_from_name,
1605 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1606 .destroyImage = dri2_destroy_image,
1607 .createImage = dri2_create_image,
1608 .queryImage = dri2_query_image,
1609 .dupImage = dri2_dup_image,
1610 .validateUsage = dri2_validate_usage,
1611 .createImageFromNames = dri2_from_names,
1612 .fromPlanar = dri2_from_planar,
1613 .createImageFromTexture = dri2_create_from_texture,
1614 .createImageFromFds = NULL,
1615 .createImageFromDmaBufs = NULL,
1616 .blitImage = dri2_blit_image,
1617 .getCapabilities = dri2_get_capabilities,
1618 .mapImage = dri2_map_image,
1619 .unmapImage = dri2_unmap_image,
1620 .createImageWithModifiers = NULL,
1621 .createImageFromDmaBufs2 = NULL,
1622 .queryDmaBufFormats = NULL,
1623 .queryDmaBufModifiers = NULL,
1624 .queryDmaBufFormatModifierAttribs = NULL,
1625 .createImageFromRenderbuffer2 = dri2_create_image_from_renderbuffer2,
1626 };
1627
1628 static const __DRIrobustnessExtension dri2Robustness = {
1629 .base = { __DRI2_ROBUSTNESS, 1 }
1630 };
1631
1632 static int
1633 dri2_interop_query_device_info(__DRIcontext *_ctx,
1634 struct mesa_glinterop_device_info *out)
1635 {
1636 struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1637
1638 /* There is no version 0, thus we do not support it */
1639 if (out->version == 0)
1640 return MESA_GLINTEROP_INVALID_VERSION;
1641
1642 out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1643 out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1644 out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1645 out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1646
1647 out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1648 out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1649
1650 /* Instruct the caller that we support up-to version one of the interface */
1651 out->version = 1;
1652
1653 return MESA_GLINTEROP_SUCCESS;
1654 }
1655
1656 static int
1657 dri2_interop_export_object(__DRIcontext *_ctx,
1658 struct mesa_glinterop_export_in *in,
1659 struct mesa_glinterop_export_out *out)
1660 {
1661 struct st_context_iface *st = dri_context(_ctx)->st;
1662 struct pipe_screen *screen = st->pipe->screen;
1663 struct gl_context *ctx = ((struct st_context *)st)->ctx;
1664 struct pipe_resource *res = NULL;
1665 struct winsys_handle whandle;
1666 unsigned target, usage;
1667 boolean success;
1668
1669 /* There is no version 0, thus we do not support it */
1670 if (in->version == 0 || out->version == 0)
1671 return MESA_GLINTEROP_INVALID_VERSION;
1672
1673 /* Validate the target. */
1674 switch (in->target) {
1675 case GL_TEXTURE_BUFFER:
1676 case GL_TEXTURE_1D:
1677 case GL_TEXTURE_2D:
1678 case GL_TEXTURE_3D:
1679 case GL_TEXTURE_RECTANGLE:
1680 case GL_TEXTURE_1D_ARRAY:
1681 case GL_TEXTURE_2D_ARRAY:
1682 case GL_TEXTURE_CUBE_MAP_ARRAY:
1683 case GL_TEXTURE_CUBE_MAP:
1684 case GL_TEXTURE_2D_MULTISAMPLE:
1685 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1686 case GL_TEXTURE_EXTERNAL_OES:
1687 case GL_RENDERBUFFER:
1688 case GL_ARRAY_BUFFER:
1689 target = in->target;
1690 break;
1691 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1692 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1693 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1694 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1695 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1696 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1697 target = GL_TEXTURE_CUBE_MAP;
1698 break;
1699 default:
1700 return MESA_GLINTEROP_INVALID_TARGET;
1701 }
1702
1703 /* Validate the simple case of miplevel. */
1704 if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1705 in->miplevel != 0)
1706 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1707
1708 /* Validate the OpenGL object and get pipe_resource. */
1709 simple_mtx_lock(&ctx->Shared->Mutex);
1710
1711 if (target == GL_ARRAY_BUFFER) {
1712 /* Buffer objects.
1713 *
1714 * The error checking is based on the documentation of
1715 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1716 */
1717 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1718
1719 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1720 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1721 * a GL buffer object but does not have an existing data store or
1722 * the size of the buffer is 0."
1723 */
1724 if (!buf || buf->Size == 0) {
1725 simple_mtx_unlock(&ctx->Shared->Mutex);
1726 return MESA_GLINTEROP_INVALID_OBJECT;
1727 }
1728
1729 res = st_buffer_object(buf)->buffer;
1730 if (!res) {
1731 /* this shouldn't happen */
1732 simple_mtx_unlock(&ctx->Shared->Mutex);
1733 return MESA_GLINTEROP_INVALID_OBJECT;
1734 }
1735
1736 out->buf_offset = 0;
1737 out->buf_size = buf->Size;
1738
1739 buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1740 } else if (target == GL_RENDERBUFFER) {
1741 /* Renderbuffers.
1742 *
1743 * The error checking is based on the documentation of
1744 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1745 */
1746 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1747
1748 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1749 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1750 * object or if the width or height of renderbuffer is zero."
1751 */
1752 if (!rb || rb->Width == 0 || rb->Height == 0) {
1753 simple_mtx_unlock(&ctx->Shared->Mutex);
1754 return MESA_GLINTEROP_INVALID_OBJECT;
1755 }
1756
1757 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1758 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1759 * renderbuffer object."
1760 */
1761 if (rb->NumSamples > 1) {
1762 simple_mtx_unlock(&ctx->Shared->Mutex);
1763 return MESA_GLINTEROP_INVALID_OPERATION;
1764 }
1765
1766 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1767 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1768 * required by the OpenCL implementation on the device."
1769 */
1770 res = st_renderbuffer(rb)->texture;
1771 if (!res) {
1772 simple_mtx_unlock(&ctx->Shared->Mutex);
1773 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1774 }
1775
1776 out->internal_format = rb->InternalFormat;
1777 out->view_minlevel = 0;
1778 out->view_numlevels = 1;
1779 out->view_minlayer = 0;
1780 out->view_numlayers = 1;
1781 } else {
1782 /* Texture objects.
1783 *
1784 * The error checking is based on the documentation of
1785 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1786 */
1787 struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1788
1789 if (obj)
1790 _mesa_test_texobj_completeness(ctx, obj);
1791
1792 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1793 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1794 * type matches texture_target, if the specified miplevel of texture
1795 * is not defined, or if the width or height of the specified
1796 * miplevel is zero or if the GL texture object is incomplete."
1797 */
1798 if (!obj ||
1799 obj->Target != target ||
1800 !obj->_BaseComplete ||
1801 (in->miplevel > 0 && !obj->_MipmapComplete)) {
1802 simple_mtx_unlock(&ctx->Shared->Mutex);
1803 return MESA_GLINTEROP_INVALID_OBJECT;
1804 }
1805
1806 if (target == GL_TEXTURE_BUFFER) {
1807 struct st_buffer_object *stBuf =
1808 st_buffer_object(obj->BufferObject);
1809
1810 if (!stBuf || !stBuf->buffer) {
1811 /* this shouldn't happen */
1812 simple_mtx_unlock(&ctx->Shared->Mutex);
1813 return MESA_GLINTEROP_INVALID_OBJECT;
1814 }
1815 res = stBuf->buffer;
1816
1817 out->internal_format = obj->BufferObjectFormat;
1818 out->buf_offset = obj->BufferOffset;
1819 out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1820 obj->BufferSize;
1821
1822 obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1823 } else {
1824 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1825 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1826 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1827 * implementations); or greater than the value of q (for both OpenGL
1828 * and OpenGL ES). levelbase and q are defined for the texture in
1829 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1830 * specification and section 3.7.10 of the OpenGL ES 2.0."
1831 */
1832 if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
1833 simple_mtx_unlock(&ctx->Shared->Mutex);
1834 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1835 }
1836
1837 if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
1838 simple_mtx_unlock(&ctx->Shared->Mutex);
1839 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1840 }
1841
1842 res = st_get_texobj_resource(obj);
1843 if (!res) {
1844 /* Incomplete texture buffer object? This shouldn't really occur. */
1845 simple_mtx_unlock(&ctx->Shared->Mutex);
1846 return MESA_GLINTEROP_INVALID_OBJECT;
1847 }
1848
1849 out->internal_format = obj->Image[0][0]->InternalFormat;
1850 out->view_minlevel = obj->MinLevel;
1851 out->view_numlevels = obj->NumLevels;
1852 out->view_minlayer = obj->MinLayer;
1853 out->view_numlayers = obj->NumLayers;
1854 }
1855 }
1856
1857 /* Get the handle. */
1858 switch (in->access) {
1859 case MESA_GLINTEROP_ACCESS_READ_ONLY:
1860 usage = 0;
1861 break;
1862 case MESA_GLINTEROP_ACCESS_READ_WRITE:
1863 case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
1864 usage = PIPE_HANDLE_USAGE_SHADER_WRITE;
1865 break;
1866 default:
1867 usage = 0;
1868 }
1869
1870 memset(&whandle, 0, sizeof(whandle));
1871 whandle.type = WINSYS_HANDLE_TYPE_FD;
1872
1873 success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
1874 usage);
1875 simple_mtx_unlock(&ctx->Shared->Mutex);
1876
1877 if (!success)
1878 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
1879
1880 out->dmabuf_fd = whandle.handle;
1881 out->out_driver_data_written = 0;
1882
1883 if (res->target == PIPE_BUFFER)
1884 out->buf_offset += whandle.offset;
1885
1886 /* Instruct the caller that we support up-to version one of the interface */
1887 in->version = 1;
1888 out->version = 1;
1889
1890 return MESA_GLINTEROP_SUCCESS;
1891 }
1892
1893 static const __DRI2interopExtension dri2InteropExtension = {
1894 .base = { __DRI2_INTEROP, 1 },
1895 .query_device_info = dri2_interop_query_device_info,
1896 .export_object = dri2_interop_export_object
1897 };
1898
1899 /**
1900 * \brief the DRI2bufferDamageExtension set_damage_region method
1901 */
1902 static void
1903 dri2_set_damage_region(__DRIdrawable *dPriv, unsigned int nrects, int *rects)
1904 {
1905 struct dri_drawable *drawable = dri_drawable(dPriv);
1906 struct pipe_box *boxes = NULL;
1907
1908 if (nrects) {
1909 boxes = CALLOC(nrects, sizeof(*boxes));
1910 assert(boxes);
1911
1912 for (unsigned int i = 0; i < nrects; i++) {
1913 int *rect = &rects[i * 4];
1914
1915 u_box_2d(rect[0], rect[1], rect[2], rect[3], &boxes[i]);
1916 }
1917 }
1918
1919 FREE(drawable->damage_rects);
1920 drawable->damage_rects = boxes;
1921 drawable->num_damage_rects = nrects;
1922
1923 /* Only apply the damage region if the BACK_LEFT texture is up-to-date. */
1924 if (drawable->texture_stamp == drawable->dPriv->lastStamp &&
1925 (drawable->texture_mask & (1 << ST_ATTACHMENT_BACK_LEFT))) {
1926 struct pipe_screen *screen = drawable->screen->base.screen;
1927 struct pipe_resource *resource;
1928
1929 if (drawable->stvis.samples > 1)
1930 resource = drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
1931 else
1932 resource = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
1933
1934 screen->set_damage_region(screen, resource,
1935 drawable->num_damage_rects,
1936 drawable->damage_rects);
1937 }
1938 }
1939
1940 static __DRI2bufferDamageExtension dri2BufferDamageExtension = {
1941 .base = { __DRI2_BUFFER_DAMAGE, 1 },
1942 };
1943
1944 /**
1945 * \brief the DRI2ConfigQueryExtension configQueryb method
1946 */
1947 static int
1948 dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
1949 unsigned char *val)
1950 {
1951 struct dri_screen *screen = dri_screen(sPriv);
1952
1953 if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
1954 return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
1955
1956 *val = driQueryOptionb(&screen->dev->option_cache, var);
1957
1958 return 0;
1959 }
1960
1961 /**
1962 * \brief the DRI2ConfigQueryExtension configQueryi method
1963 */
1964 static int
1965 dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
1966 {
1967 struct dri_screen *screen = dri_screen(sPriv);
1968
1969 if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
1970 !driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
1971 return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
1972
1973 *val = driQueryOptioni(&screen->dev->option_cache, var);
1974
1975 return 0;
1976 }
1977
1978 /**
1979 * \brief the DRI2ConfigQueryExtension configQueryf method
1980 */
1981 static int
1982 dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
1983 {
1984 struct dri_screen *screen = dri_screen(sPriv);
1985
1986 if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
1987 return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
1988
1989 *val = driQueryOptionf(&screen->dev->option_cache, var);
1990
1991 return 0;
1992 }
1993
1994 /**
1995 * \brief the DRI2ConfigQueryExtension struct.
1996 *
1997 * We first query the driver option cache. Then the dri2 option cache.
1998 */
1999 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
2000 .base = { __DRI2_CONFIG_QUERY, 1 },
2001
2002 .configQueryb = dri2GalliumConfigQueryb,
2003 .configQueryi = dri2GalliumConfigQueryi,
2004 .configQueryf = dri2GalliumConfigQueryf,
2005 };
2006
2007 /**
2008 * \brief the DRI2blobExtension set_cache_funcs method
2009 */
2010 static void
2011 set_blob_cache_funcs(__DRIscreen *sPriv, __DRIblobCacheSet set,
2012 __DRIblobCacheGet get)
2013 {
2014 struct dri_screen *screen = dri_screen(sPriv);
2015 struct pipe_screen *pscreen = screen->base.screen;
2016
2017 if (!pscreen->get_disk_shader_cache)
2018 return;
2019
2020 struct disk_cache *cache = pscreen->get_disk_shader_cache(pscreen);
2021
2022 if (!cache)
2023 return;
2024
2025 disk_cache_set_callbacks(cache, set, get);
2026 }
2027
2028 static const __DRI2blobExtension driBlobExtension = {
2029 .base = { __DRI2_BLOB, 1 },
2030 .set_cache_funcs = set_blob_cache_funcs
2031 };
2032
2033 /*
2034 * Backend function init_screen.
2035 */
2036
2037 static const __DRIextension *dri_screen_extensions[] = {
2038 &driTexBufferExtension.base,
2039 &dri2FlushExtension.base,
2040 &dri2ImageExtension.base,
2041 &dri2RendererQueryExtension.base,
2042 &dri2GalliumConfigQueryExtension.base,
2043 &dri2ThrottleExtension.base,
2044 &dri2FenceExtension.base,
2045 &dri2BufferDamageExtension.base,
2046 &dri2InteropExtension.base,
2047 &dri2NoErrorExtension.base,
2048 &driBlobExtension.base,
2049 NULL
2050 };
2051
2052 static const __DRIextension *dri_robust_screen_extensions[] = {
2053 &driTexBufferExtension.base,
2054 &dri2FlushExtension.base,
2055 &dri2ImageExtension.base,
2056 &dri2RendererQueryExtension.base,
2057 &dri2GalliumConfigQueryExtension.base,
2058 &dri2ThrottleExtension.base,
2059 &dri2FenceExtension.base,
2060 &dri2InteropExtension.base,
2061 &dri2BufferDamageExtension.base,
2062 &dri2Robustness.base,
2063 &dri2NoErrorExtension.base,
2064 &driBlobExtension.base,
2065 NULL
2066 };
2067
2068 /**
2069 * This is the driver specific part of the createNewScreen entry point.
2070 *
2071 * Returns the struct gl_config supported by this driver.
2072 */
2073 static const __DRIconfig **
2074 dri2_init_screen(__DRIscreen * sPriv)
2075 {
2076 const __DRIconfig **configs;
2077 struct dri_screen *screen;
2078 struct pipe_screen *pscreen = NULL;
2079
2080 screen = CALLOC_STRUCT(dri_screen);
2081 if (!screen)
2082 return NULL;
2083
2084 screen->sPriv = sPriv;
2085 screen->fd = sPriv->fd;
2086 (void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
2087
2088 sPriv->driverPrivate = (void *)screen;
2089
2090 if (pipe_loader_drm_probe_fd(&screen->dev, screen->fd)) {
2091 dri_init_options(screen);
2092
2093 pscreen = pipe_loader_create_screen(screen->dev);
2094 }
2095
2096 if (!pscreen)
2097 goto release_pipe;
2098
2099 screen->throttle = pscreen->get_param(pscreen, PIPE_CAP_THROTTLE);
2100
2101 if (pscreen->resource_create_with_modifiers)
2102 dri2ImageExtension.createImageWithModifiers =
2103 dri2_create_image_with_modifiers;
2104
2105 if (pscreen->get_param(pscreen, PIPE_CAP_DMABUF)) {
2106 uint64_t cap;
2107
2108 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2109 (cap & DRM_PRIME_CAP_IMPORT)) {
2110 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2111 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2112 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2113 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2114 dri2ImageExtension.queryDmaBufModifiers =
2115 dri2_query_dma_buf_modifiers;
2116 dri2ImageExtension.queryDmaBufFormatModifierAttribs =
2117 dri2_query_dma_buf_format_modifier_attribs;
2118 }
2119 }
2120
2121 if (pscreen->set_damage_region)
2122 dri2BufferDamageExtension.set_damage_region = dri2_set_damage_region;
2123
2124 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
2125 sPriv->extensions = dri_robust_screen_extensions;
2126 screen->has_reset_status_query = true;
2127 }
2128 else
2129 sPriv->extensions = dri_screen_extensions;
2130
2131 configs = dri_init_screen_helper(screen, pscreen);
2132 if (!configs)
2133 goto destroy_screen;
2134
2135 screen->can_share_buffer = true;
2136 screen->auto_fake_front = dri_with_format(sPriv);
2137 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2138 screen->lookup_egl_image = dri2_lookup_egl_image;
2139
2140 return configs;
2141
2142 destroy_screen:
2143 dri_destroy_screen_helper(screen);
2144
2145 release_pipe:
2146 if (screen->dev)
2147 pipe_loader_release(&screen->dev, 1);
2148
2149 FREE(screen);
2150 return NULL;
2151 }
2152
2153 /**
2154 * This is the driver specific part of the createNewScreen entry point.
2155 *
2156 * Returns the struct gl_config supported by this driver.
2157 */
2158 static const __DRIconfig **
2159 dri_kms_init_screen(__DRIscreen * sPriv)
2160 {
2161 #if defined(GALLIUM_SOFTPIPE)
2162 const __DRIconfig **configs;
2163 struct dri_screen *screen;
2164 struct pipe_screen *pscreen = NULL;
2165 uint64_t cap;
2166
2167 screen = CALLOC_STRUCT(dri_screen);
2168 if (!screen)
2169 return NULL;
2170
2171 screen->sPriv = sPriv;
2172 screen->fd = sPriv->fd;
2173
2174 sPriv->driverPrivate = (void *)screen;
2175
2176 if (pipe_loader_sw_probe_kms(&screen->dev, screen->fd)) {
2177 dri_init_options(screen);
2178 pscreen = pipe_loader_create_screen(screen->dev);
2179 }
2180
2181 if (!pscreen)
2182 goto release_pipe;
2183
2184 if (pscreen->resource_create_with_modifiers)
2185 dri2ImageExtension.createImageWithModifiers =
2186 dri2_create_image_with_modifiers;
2187
2188 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2189 (cap & DRM_PRIME_CAP_IMPORT)) {
2190 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2191 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2192 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2193 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2194 dri2ImageExtension.queryDmaBufModifiers = dri2_query_dma_buf_modifiers;
2195 }
2196
2197 sPriv->extensions = dri_screen_extensions;
2198
2199 configs = dri_init_screen_helper(screen, pscreen);
2200 if (!configs)
2201 goto destroy_screen;
2202
2203 screen->can_share_buffer = false;
2204 screen->auto_fake_front = dri_with_format(sPriv);
2205 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2206 screen->lookup_egl_image = dri2_lookup_egl_image;
2207
2208 return configs;
2209
2210 destroy_screen:
2211 dri_destroy_screen_helper(screen);
2212
2213 release_pipe:
2214 if (screen->dev)
2215 pipe_loader_release(&screen->dev, 1);
2216
2217 FREE(screen);
2218 #endif // GALLIUM_SOFTPIPE
2219 return NULL;
2220 }
2221
2222 static boolean
2223 dri2_create_buffer(__DRIscreen * sPriv,
2224 __DRIdrawable * dPriv,
2225 const struct gl_config * visual, boolean isPixmap)
2226 {
2227 struct dri_drawable *drawable = NULL;
2228
2229 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
2230 return FALSE;
2231
2232 drawable = dPriv->driverPrivate;
2233
2234 drawable->allocate_textures = dri2_allocate_textures;
2235 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
2236 drawable->update_tex_buffer = dri2_update_tex_buffer;
2237 drawable->flush_swapbuffers = dri2_flush_swapbuffers;
2238
2239 return TRUE;
2240 }
2241
2242 /**
2243 * DRI driver virtual function table.
2244 *
2245 * DRI versions differ in their implementation of init_screen and swap_buffers.
2246 */
2247 const struct __DriverAPIRec galliumdrm_driver_api = {
2248 .InitScreen = dri2_init_screen,
2249 .DestroyScreen = dri_destroy_screen,
2250 .CreateContext = dri_create_context,
2251 .DestroyContext = dri_destroy_context,
2252 .CreateBuffer = dri2_create_buffer,
2253 .DestroyBuffer = dri_destroy_buffer,
2254 .MakeCurrent = dri_make_current,
2255 .UnbindContext = dri_unbind_context,
2256
2257 .AllocateBuffer = dri2_allocate_buffer,
2258 .ReleaseBuffer = dri2_release_buffer,
2259 };
2260
2261 /**
2262 * DRI driver virtual function table.
2263 *
2264 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2265 * hook. The latter is used to explicitly initialise the kms_swrast driver
2266 * rather than selecting the approapriate driver as suggested by the loader.
2267 */
2268 const struct __DriverAPIRec dri_kms_driver_api = {
2269 .InitScreen = dri_kms_init_screen,
2270 .DestroyScreen = dri_destroy_screen,
2271 .CreateContext = dri_create_context,
2272 .DestroyContext = dri_destroy_context,
2273 .CreateBuffer = dri2_create_buffer,
2274 .DestroyBuffer = dri_destroy_buffer,
2275 .MakeCurrent = dri_make_current,
2276 .UnbindContext = dri_unbind_context,
2277
2278 .AllocateBuffer = dri2_allocate_buffer,
2279 .ReleaseBuffer = dri2_release_buffer,
2280 };
2281
2282 /* This is the table of extensions that the loader will dlsym() for. */
2283 const __DRIextension *galliumdrm_driver_extensions[] = {
2284 &driCoreExtension.base,
2285 &driImageDriverExtension.base,
2286 &driDRI2Extension.base,
2287 &gallium_config_options.base,
2288 NULL
2289 };
2290
2291 /* vim: set sw=3 ts=8 sts=3 expandtab: */