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