st/dri: Check get-handle return value in queryImage
[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 & ~PIPE_BIND_SCANOUT;
736 templ.nr_samples = drawable->stvis.samples;
737
738 /* Try to reuse the resource.
739 * (the other resource parameters should be constant)
740 */
741 if (!drawable->msaa_textures[statt] ||
742 drawable->msaa_textures[statt]->width0 != templ.width0 ||
743 drawable->msaa_textures[statt]->height0 != templ.height0) {
744 /* Allocate a new one. */
745 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
746
747 drawable->msaa_textures[statt] =
748 screen->base.screen->resource_create(screen->base.screen,
749 &templ);
750 assert(drawable->msaa_textures[statt]);
751
752 /* If there are any MSAA resources, we should initialize them
753 * such that they contain the same data as the single-sample
754 * resources we just got from the X server.
755 *
756 * The reason for this is that the state tracker (and
757 * therefore the app) can access the MSAA resources only.
758 * The single-sample resources are not exposed
759 * to the state tracker.
760 *
761 */
762 dri_pipe_blit(ctx->st->pipe,
763 drawable->msaa_textures[statt],
764 drawable->textures[statt]);
765 }
766 }
767 else {
768 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
769 }
770 }
771 }
772
773 /* Allocate a private depth-stencil buffer. */
774 if (alloc_depthstencil) {
775 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
776 struct pipe_resource **zsbuf;
777 enum pipe_format format;
778 unsigned bind;
779
780 dri_drawable_get_format(drawable, statt, &format, &bind);
781
782 if (format) {
783 templ.format = format;
784 templ.bind = bind;
785
786 if (drawable->stvis.samples > 1) {
787 templ.nr_samples = drawable->stvis.samples;
788 zsbuf = &drawable->msaa_textures[statt];
789 }
790 else {
791 templ.nr_samples = 0;
792 zsbuf = &drawable->textures[statt];
793 }
794
795 /* Try to reuse the resource.
796 * (the other resource parameters should be constant)
797 */
798 if (!*zsbuf ||
799 (*zsbuf)->width0 != templ.width0 ||
800 (*zsbuf)->height0 != templ.height0) {
801 /* Allocate a new one. */
802 pipe_resource_reference(zsbuf, NULL);
803 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
804 &templ);
805 assert(*zsbuf);
806 }
807 }
808 else {
809 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
810 pipe_resource_reference(&drawable->textures[statt], NULL);
811 }
812 }
813
814 /* For DRI2, we may get the same buffers again from the server.
815 * To prevent useless imports of gem names, drawable->old* is used
816 * to bypass the import if we get the same buffers. This doesn't apply
817 * to DRI3/Wayland, users of image.loader, since the buffer is managed
818 * by the client (no import), and the back buffer is going to change
819 * at every redraw.
820 */
821 if (!image) {
822 drawable->old_num = num_buffers;
823 drawable->old_w = dri_drawable->w;
824 drawable->old_h = dri_drawable->h;
825 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
826 }
827 }
828
829 static void
830 dri2_flush_frontbuffer(struct dri_context *ctx,
831 struct dri_drawable *drawable,
832 enum st_attachment_type statt)
833 {
834 __DRIdrawable *dri_drawable = drawable->dPriv;
835 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
836 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
837 struct pipe_context *pipe = ctx->st->pipe;
838
839 if (statt != ST_ATTACHMENT_FRONT_LEFT)
840 return;
841
842 if (drawable->stvis.samples > 1) {
843 /* Resolve the front buffer. */
844 dri_pipe_blit(ctx->st->pipe,
845 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
846 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
847 }
848
849 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
850 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
851 }
852
853 pipe->flush(pipe, NULL, 0);
854
855 if (image) {
856 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
857 }
858 else if (loader->flushFrontBuffer) {
859 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
860 }
861 }
862
863 static void
864 dri2_update_tex_buffer(struct dri_drawable *drawable,
865 struct dri_context *ctx,
866 struct pipe_resource *res)
867 {
868 /* no-op */
869 }
870
871 static __DRIimage *
872 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
873 {
874 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
875 __DRIimage *img;
876
877 if (!loader->lookupEGLImage)
878 return NULL;
879
880 img = loader->lookupEGLImage(screen->sPriv,
881 handle, screen->sPriv->loaderPrivate);
882
883 return img;
884 }
885
886 static __DRIimage *
887 dri2_create_image_from_winsys(__DRIscreen *_screen,
888 int width, int height, int format,
889 int num_handles, struct winsys_handle *whandle,
890 void *loaderPrivate)
891 {
892 struct dri_screen *screen = dri_screen(_screen);
893 struct pipe_screen *pscreen = screen->base.screen;
894 __DRIimage *img;
895 struct pipe_resource templ;
896 unsigned tex_usage;
897 enum pipe_format pf;
898 int i;
899
900 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
901
902 pf = dri2_format_to_pipe_format (format);
903 if (pf == PIPE_FORMAT_NONE)
904 return NULL;
905
906 img = CALLOC_STRUCT(__DRIimageRec);
907 if (!img)
908 return NULL;
909
910 memset(&templ, 0, sizeof(templ));
911 templ.bind = tex_usage;
912 templ.target = screen->target;
913 templ.last_level = 0;
914 templ.depth0 = 1;
915 templ.array_size = 1;
916
917 for (i = num_handles - 1; i >= 0; i--) {
918 struct pipe_resource *tex;
919
920 /* TODO: something a lot less ugly */
921 switch (i) {
922 case 0:
923 templ.width0 = width;
924 templ.height0 = height;
925 templ.format = pf;
926 break;
927 case 1:
928 templ.width0 = width / 2;
929 templ.height0 = height / 2;
930 templ.format = (num_handles == 2) ?
931 PIPE_FORMAT_RG88_UNORM : /* NV12, etc */
932 PIPE_FORMAT_R8_UNORM; /* I420, etc */
933 break;
934 case 2:
935 templ.width0 = width / 2;
936 templ.height0 = height / 2;
937 templ.format = PIPE_FORMAT_R8_UNORM;
938 break;
939 default:
940 unreachable("too many planes!");
941 }
942
943 tex = pscreen->resource_from_handle(pscreen,
944 &templ, &whandle[i], PIPE_HANDLE_USAGE_READ_WRITE);
945 if (!tex) {
946 pipe_resource_reference(&img->texture, NULL);
947 FREE(img);
948 return NULL;
949 }
950
951 tex->next = img->texture;
952 img->texture = tex;
953 }
954
955 img->level = 0;
956 img->layer = 0;
957 img->dri_format = format;
958 img->use = 0;
959 img->loader_private = loaderPrivate;
960
961 return img;
962 }
963
964 static __DRIimage *
965 dri2_create_image_from_name(__DRIscreen *_screen,
966 int width, int height, int format,
967 int name, int pitch, void *loaderPrivate)
968 {
969 struct winsys_handle whandle;
970 enum pipe_format pf;
971
972 memset(&whandle, 0, sizeof(whandle));
973 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
974 whandle.handle = name;
975 whandle.modifier = DRM_FORMAT_MOD_INVALID;
976
977 pf = dri2_format_to_pipe_format (format);
978 if (pf == PIPE_FORMAT_NONE)
979 return NULL;
980
981 whandle.stride = pitch * util_format_get_blocksize(pf);
982
983 return dri2_create_image_from_winsys(_screen, width, height, format,
984 1, &whandle, loaderPrivate);
985 }
986
987 static __DRIimage *
988 dri2_create_image_from_fd(__DRIscreen *_screen,
989 int width, int height, int fourcc,
990 uint64_t modifier, int *fds, int num_fds,
991 int *strides, int *offsets, unsigned *error,
992 int *dri_components, void *loaderPrivate)
993 {
994 struct winsys_handle whandles[3];
995 int format;
996 __DRIimage *img = NULL;
997 unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
998 int expected_num_fds, i;
999
1000 switch (fourcc) {
1001 case __DRI_IMAGE_FOURCC_YUV420:
1002 case __DRI_IMAGE_FOURCC_YVU420:
1003 expected_num_fds = 3;
1004 break;
1005 case __DRI_IMAGE_FOURCC_NV12:
1006 expected_num_fds = 2;
1007 break;
1008 default:
1009 expected_num_fds = 1;
1010 break;
1011 }
1012
1013 if (num_fds != expected_num_fds) {
1014 err = __DRI_IMAGE_ERROR_BAD_MATCH;
1015 goto exit;
1016 }
1017
1018 format = convert_fourcc(fourcc, dri_components);
1019 if (format == -1) {
1020 err = __DRI_IMAGE_ERROR_BAD_MATCH;
1021 goto exit;
1022 }
1023
1024 memset(whandles, 0, sizeof(whandles));
1025
1026 for (i = 0; i < num_fds; i++) {
1027 if (fds[i] < 0) {
1028 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
1029 goto exit;
1030 }
1031
1032 whandles[i].type = DRM_API_HANDLE_TYPE_FD;
1033 whandles[i].handle = (unsigned)fds[i];
1034 whandles[i].stride = (unsigned)strides[i];
1035 whandles[i].offset = (unsigned)offsets[i];
1036 whandles[i].modifier = modifier;
1037 }
1038
1039 if (fourcc == __DRI_IMAGE_FOURCC_YVU420) {
1040 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1041 struct winsys_handle tmp = whandles[1];
1042 whandles[1] = whandles[2];
1043 whandles[2] = tmp;
1044 fourcc = __DRI_IMAGE_FOURCC_YUV420;
1045 }
1046
1047 img = dri2_create_image_from_winsys(_screen, width, height, format,
1048 num_fds, whandles, loaderPrivate);
1049 if(img == NULL)
1050 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
1051
1052 exit:
1053 if (error)
1054 *error = err;
1055
1056 return img;
1057 }
1058
1059 static __DRIimage *
1060 dri2_create_image_from_renderbuffer(__DRIcontext *context,
1061 int renderbuffer, void *loaderPrivate)
1062 {
1063 struct dri_context *ctx = dri_context(context);
1064
1065 if (!ctx->st->get_resource_for_egl_image)
1066 return NULL;
1067
1068 /* TODO */
1069 return NULL;
1070 }
1071
1072 static __DRIimage *
1073 dri2_create_image_common(__DRIscreen *_screen,
1074 int width, int height,
1075 int format, unsigned int use,
1076 const uint64_t *modifiers,
1077 const unsigned count,
1078 void *loaderPrivate)
1079 {
1080 struct dri_screen *screen = dri_screen(_screen);
1081 __DRIimage *img;
1082 struct pipe_resource templ;
1083 unsigned tex_usage;
1084 enum pipe_format pf;
1085
1086 /* createImageWithModifiers doesn't supply usage, and we should not get
1087 * here with both modifiers and a usage flag.
1088 */
1089 assert(!(use && (modifiers != NULL)));
1090
1091 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
1092
1093 if (use & __DRI_IMAGE_USE_SCANOUT)
1094 tex_usage |= PIPE_BIND_SCANOUT;
1095 if (use & __DRI_IMAGE_USE_SHARE)
1096 tex_usage |= PIPE_BIND_SHARED;
1097 if (use & __DRI_IMAGE_USE_LINEAR)
1098 tex_usage |= PIPE_BIND_LINEAR;
1099 if (use & __DRI_IMAGE_USE_CURSOR) {
1100 if (width != 64 || height != 64)
1101 return NULL;
1102 tex_usage |= PIPE_BIND_CURSOR;
1103 }
1104
1105 pf = dri2_format_to_pipe_format (format);
1106 if (pf == PIPE_FORMAT_NONE)
1107 return NULL;
1108
1109 img = CALLOC_STRUCT(__DRIimageRec);
1110 if (!img)
1111 return NULL;
1112
1113 memset(&templ, 0, sizeof(templ));
1114 templ.bind = tex_usage;
1115 templ.format = pf;
1116 templ.target = PIPE_TEXTURE_2D;
1117 templ.last_level = 0;
1118 templ.width0 = width;
1119 templ.height0 = height;
1120 templ.depth0 = 1;
1121 templ.array_size = 1;
1122
1123 if (modifiers)
1124 img->texture =
1125 screen->base.screen
1126 ->resource_create_with_modifiers(screen->base.screen,
1127 &templ,
1128 modifiers,
1129 count);
1130 else
1131 img->texture =
1132 screen->base.screen->resource_create(screen->base.screen, &templ);
1133 if (!img->texture) {
1134 FREE(img);
1135 return NULL;
1136 }
1137
1138 img->level = 0;
1139 img->layer = 0;
1140 img->dri_format = format;
1141 img->dri_components = 0;
1142 img->use = use;
1143
1144 img->loader_private = loaderPrivate;
1145 return img;
1146 }
1147
1148 static __DRIimage *
1149 dri2_create_image(__DRIscreen *_screen,
1150 int width, int height, int format,
1151 unsigned int use, void *loaderPrivate)
1152 {
1153 return dri2_create_image_common(_screen, width, height, format, use,
1154 NULL /* modifiers */, 0 /* count */,
1155 loaderPrivate);
1156 }
1157
1158 static __DRIimage *
1159 dri2_create_image_with_modifiers(__DRIscreen *dri_screen,
1160 int width, int height, int format,
1161 const uint64_t *modifiers,
1162 const unsigned count,
1163 void *loaderPrivate)
1164 {
1165 return dri2_create_image_common(dri_screen, width, height, format,
1166 0 /* use */, modifiers, count,
1167 loaderPrivate);
1168 }
1169
1170 static GLboolean
1171 dri2_query_image(__DRIimage *image, int attrib, int *value)
1172 {
1173 struct winsys_handle whandle;
1174 unsigned usage;
1175
1176 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1177 usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
1178 else
1179 usage = PIPE_HANDLE_USAGE_READ_WRITE;
1180
1181 memset(&whandle, 0, sizeof(whandle));
1182
1183 switch (attrib) {
1184 case __DRI_IMAGE_ATTRIB_STRIDE:
1185 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1186 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1187 NULL, image->texture, &whandle, usage))
1188 return GL_FALSE;
1189 *value = whandle.stride;
1190 return GL_TRUE;
1191 case __DRI_IMAGE_ATTRIB_OFFSET:
1192 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1193 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1194 NULL, image->texture, &whandle, usage))
1195 return GL_FALSE;
1196 *value = whandle.offset;
1197 return GL_TRUE;
1198 case __DRI_IMAGE_ATTRIB_HANDLE:
1199 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1200 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1201 NULL, image->texture, &whandle, usage))
1202 return GL_FALSE;
1203 *value = whandle.handle;
1204 return GL_TRUE;
1205 case __DRI_IMAGE_ATTRIB_NAME:
1206 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1207 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1208 NULL, image->texture, &whandle, usage))
1209 return GL_FALSE;
1210 *value = whandle.handle;
1211 return GL_TRUE;
1212 case __DRI_IMAGE_ATTRIB_FD:
1213 whandle.type= DRM_API_HANDLE_TYPE_FD;
1214 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1215 NULL, image->texture, &whandle, usage))
1216 return GL_FALSE;
1217
1218 *value = whandle.handle;
1219 return GL_TRUE;
1220 case __DRI_IMAGE_ATTRIB_FORMAT:
1221 *value = image->dri_format;
1222 return GL_TRUE;
1223 case __DRI_IMAGE_ATTRIB_WIDTH:
1224 *value = image->texture->width0;
1225 return GL_TRUE;
1226 case __DRI_IMAGE_ATTRIB_HEIGHT:
1227 *value = image->texture->height0;
1228 return GL_TRUE;
1229 case __DRI_IMAGE_ATTRIB_COMPONENTS:
1230 if (image->dri_components == 0)
1231 return GL_FALSE;
1232 *value = image->dri_components;
1233 return GL_TRUE;
1234 case __DRI_IMAGE_ATTRIB_FOURCC:
1235 *value = convert_to_fourcc(image->dri_format);
1236 return GL_TRUE;
1237 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1238 *value = 1;
1239 return GL_TRUE;
1240 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1241 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1242 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1243 NULL, image->texture, &whandle, usage))
1244 return GL_FALSE;
1245 *value = (whandle.modifier >> 32) & 0xffffffff;
1246 return GL_TRUE;
1247 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1248 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1249 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1250 NULL, image->texture, &whandle, usage))
1251 return GL_FALSE;
1252 *value = whandle.modifier & 0xffffffff;
1253 return GL_TRUE;
1254 default:
1255 return GL_FALSE;
1256 }
1257 }
1258
1259 static __DRIimage *
1260 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1261 {
1262 __DRIimage *img;
1263
1264 img = CALLOC_STRUCT(__DRIimageRec);
1265 if (!img)
1266 return NULL;
1267
1268 img->texture = NULL;
1269 pipe_resource_reference(&img->texture, image->texture);
1270 img->level = image->level;
1271 img->layer = image->layer;
1272 img->dri_format = image->dri_format;
1273 /* This should be 0 for sub images, but dup is also used for base images. */
1274 img->dri_components = image->dri_components;
1275 img->loader_private = loaderPrivate;
1276
1277 return img;
1278 }
1279
1280 static GLboolean
1281 dri2_validate_usage(__DRIimage *image, unsigned int use)
1282 {
1283 /*
1284 * Gallium drivers are bad at adding usages to the resources
1285 * once opened again in another process, which is the main use
1286 * case for this, so we have to lie.
1287 */
1288 if (image != NULL)
1289 return GL_TRUE;
1290 else
1291 return GL_FALSE;
1292 }
1293
1294 static __DRIimage *
1295 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1296 int *names, int num_names, int *strides, int *offsets,
1297 void *loaderPrivate)
1298 {
1299 __DRIimage *img;
1300 int dri_components;
1301 struct winsys_handle whandle;
1302
1303 if (num_names != 1)
1304 return NULL;
1305
1306 format = convert_fourcc(format, &dri_components);
1307 if (format == -1)
1308 return NULL;
1309
1310 memset(&whandle, 0, sizeof(whandle));
1311 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1312 whandle.handle = names[0];
1313 whandle.stride = strides[0];
1314 whandle.offset = offsets[0];
1315 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1316
1317 img = dri2_create_image_from_winsys(screen, width, height, format,
1318 1, &whandle, loaderPrivate);
1319 if (img == NULL)
1320 return NULL;
1321
1322 img->dri_components = dri_components;
1323 return img;
1324 }
1325
1326 static __DRIimage *
1327 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1328 {
1329 __DRIimage *img;
1330
1331 if (plane != 0)
1332 return NULL;
1333
1334 if (image->dri_components == 0)
1335 return NULL;
1336
1337 img = dri2_dup_image(image, loaderPrivate);
1338 if (img == NULL)
1339 return NULL;
1340
1341 if (img->texture->screen->resource_changed)
1342 img->texture->screen->resource_changed(img->texture->screen,
1343 img->texture);
1344
1345 /* set this to 0 for sub images. */
1346 img->dri_components = 0;
1347 return img;
1348 }
1349
1350 static __DRIimage *
1351 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1352 int depth, int level, unsigned *error,
1353 void *loaderPrivate)
1354 {
1355 __DRIimage *img;
1356 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1357 struct gl_texture_object *obj;
1358 struct pipe_resource *tex;
1359 GLuint face = 0;
1360
1361 obj = _mesa_lookup_texture(ctx, texture);
1362 if (!obj || obj->Target != target) {
1363 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1364 return NULL;
1365 }
1366
1367 tex = st_get_texobj_resource(obj);
1368 if (!tex) {
1369 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1370 return NULL;
1371 }
1372
1373 if (target == GL_TEXTURE_CUBE_MAP)
1374 face = depth;
1375
1376 _mesa_test_texobj_completeness(ctx, obj);
1377 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1378 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1379 return NULL;
1380 }
1381
1382 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1383 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1384 return NULL;
1385 }
1386
1387 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1388 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1389 return NULL;
1390 }
1391
1392 img = CALLOC_STRUCT(__DRIimageRec);
1393 if (!img) {
1394 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1395 return NULL;
1396 }
1397
1398 img->level = level;
1399 img->layer = depth;
1400 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1401
1402 img->loader_private = loaderPrivate;
1403
1404 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1405 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1406 free(img);
1407 return NULL;
1408 }
1409
1410 pipe_resource_reference(&img->texture, tex);
1411
1412 *error = __DRI_IMAGE_ERROR_SUCCESS;
1413 return img;
1414 }
1415
1416 static __DRIimage *
1417 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1418 int *fds, int num_fds, int *strides, int *offsets,
1419 void *loaderPrivate)
1420 {
1421 __DRIimage *img;
1422 int dri_components;
1423
1424 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1425 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1426 strides, offsets, NULL,
1427 &dri_components, loaderPrivate);
1428 if (img == NULL)
1429 return NULL;
1430
1431 img->dri_components = dri_components;
1432 return img;
1433 }
1434
1435 static boolean
1436 dri2_query_dma_buf_formats(__DRIscreen *_screen, int max, int *formats,
1437 int *count)
1438 {
1439 struct dri_screen *screen = dri_screen(_screen);
1440 struct pipe_screen *pscreen = screen->base.screen;
1441 const unsigned bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
1442 int i, j;
1443
1444 for (i = 0, j = 0; (i < ARRAY_SIZE(fourcc_formats)) &&
1445 (j < max || max == 0); i++) {
1446 if (pscreen->is_format_supported(pscreen,
1447 fourcc_to_pipe_format(
1448 fourcc_formats[i]),
1449 screen->target,
1450 0, bind)) {
1451 if (j < max)
1452 formats[j] = fourcc_formats[i];
1453 j++;
1454 }
1455 }
1456 *count = j;
1457 return true;
1458 }
1459
1460 static boolean
1461 dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1462 uint64_t *modifiers, unsigned int *external_only,
1463 int *count)
1464 {
1465 struct dri_screen *screen = dri_screen(_screen);
1466 struct pipe_screen *pscreen = screen->base.screen;
1467 enum pipe_format format = fourcc_to_pipe_format(fourcc);
1468 const unsigned usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
1469
1470 if (pscreen->query_dmabuf_modifiers != NULL &&
1471 pscreen->is_format_supported(pscreen, format, screen->target, 0, usage)) {
1472 pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
1473 external_only, count);
1474 return true;
1475 }
1476 return false;
1477 }
1478
1479 static __DRIimage *
1480 dri2_from_dma_bufs(__DRIscreen *screen,
1481 int width, int height, int fourcc,
1482 int *fds, int num_fds,
1483 int *strides, int *offsets,
1484 enum __DRIYUVColorSpace yuv_color_space,
1485 enum __DRISampleRange sample_range,
1486 enum __DRIChromaSiting horizontal_siting,
1487 enum __DRIChromaSiting vertical_siting,
1488 unsigned *error,
1489 void *loaderPrivate)
1490 {
1491 __DRIimage *img;
1492 int dri_components;
1493
1494 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1495 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1496 strides, offsets, error,
1497 &dri_components, loaderPrivate);
1498 if (img == NULL)
1499 return NULL;
1500
1501 img->yuv_color_space = yuv_color_space;
1502 img->sample_range = sample_range;
1503 img->horizontal_siting = horizontal_siting;
1504 img->vertical_siting = vertical_siting;
1505 img->dri_components = dri_components;
1506
1507 *error = __DRI_IMAGE_ERROR_SUCCESS;
1508 return img;
1509 }
1510
1511 static __DRIimage *
1512 dri2_from_dma_bufs2(__DRIscreen *screen,
1513 int width, int height, int fourcc,
1514 uint64_t modifier, int *fds, int num_fds,
1515 int *strides, int *offsets,
1516 enum __DRIYUVColorSpace yuv_color_space,
1517 enum __DRISampleRange sample_range,
1518 enum __DRIChromaSiting horizontal_siting,
1519 enum __DRIChromaSiting vertical_siting,
1520 unsigned *error,
1521 void *loaderPrivate)
1522 {
1523 __DRIimage *img;
1524 int dri_components;
1525
1526 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1527 modifier, fds, num_fds, strides, offsets,
1528 error, &dri_components, loaderPrivate);
1529 if (img == NULL)
1530 return NULL;
1531
1532 img->yuv_color_space = yuv_color_space;
1533 img->sample_range = sample_range;
1534 img->horizontal_siting = horizontal_siting;
1535 img->vertical_siting = vertical_siting;
1536 img->dri_components = dri_components;
1537
1538 *error = __DRI_IMAGE_ERROR_SUCCESS;
1539 return img;
1540 }
1541
1542 static void
1543 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1544 int dstx0, int dsty0, int dstwidth, int dstheight,
1545 int srcx0, int srcy0, int srcwidth, int srcheight,
1546 int flush_flag)
1547 {
1548 struct dri_context *ctx = dri_context(context);
1549 struct pipe_context *pipe = ctx->st->pipe;
1550 struct pipe_screen *screen;
1551 struct pipe_fence_handle *fence;
1552 struct pipe_blit_info blit;
1553
1554 if (!dst || !src)
1555 return;
1556
1557 memset(&blit, 0, sizeof(blit));
1558 blit.dst.resource = dst->texture;
1559 blit.dst.box.x = dstx0;
1560 blit.dst.box.y = dsty0;
1561 blit.dst.box.width = dstwidth;
1562 blit.dst.box.height = dstheight;
1563 blit.dst.box.depth = 1;
1564 blit.dst.format = dst->texture->format;
1565 blit.src.resource = src->texture;
1566 blit.src.box.x = srcx0;
1567 blit.src.box.y = srcy0;
1568 blit.src.box.width = srcwidth;
1569 blit.src.box.height = srcheight;
1570 blit.src.box.depth = 1;
1571 blit.src.format = src->texture->format;
1572 blit.mask = PIPE_MASK_RGBA;
1573 blit.filter = PIPE_TEX_FILTER_NEAREST;
1574
1575 pipe->blit(pipe, &blit);
1576
1577 if (flush_flag == __BLIT_FLAG_FLUSH) {
1578 pipe->flush_resource(pipe, dst->texture);
1579 ctx->st->flush(ctx->st, 0, NULL);
1580 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1581 screen = dri_screen(ctx->sPriv)->base.screen;
1582 pipe->flush_resource(pipe, dst->texture);
1583 ctx->st->flush(ctx->st, 0, &fence);
1584 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
1585 screen->fence_reference(screen, &fence, NULL);
1586 }
1587 }
1588
1589 static void *
1590 dri2_map_image(__DRIcontext *context, __DRIimage *image,
1591 int x0, int y0, int width, int height,
1592 unsigned int flags, int *stride, void **data)
1593 {
1594 struct dri_context *ctx = dri_context(context);
1595 struct pipe_context *pipe = ctx->st->pipe;
1596 enum pipe_transfer_usage pipe_access = 0;
1597 struct pipe_transfer *trans;
1598 void *map;
1599
1600 if (!image || !data || *data)
1601 return NULL;
1602
1603 if (flags & __DRI_IMAGE_TRANSFER_READ)
1604 pipe_access |= PIPE_TRANSFER_READ;
1605 if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1606 pipe_access |= PIPE_TRANSFER_WRITE;
1607
1608 map = pipe_transfer_map(pipe, image->texture,
1609 0, 0, pipe_access, x0, y0, width, height,
1610 &trans);
1611 if (map) {
1612 *data = trans;
1613 *stride = trans->stride;
1614 }
1615
1616 return map;
1617 }
1618
1619 static void
1620 dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1621 {
1622 struct dri_context *ctx = dri_context(context);
1623 struct pipe_context *pipe = ctx->st->pipe;
1624
1625 pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
1626 }
1627
1628 static void
1629 dri2_destroy_image(__DRIimage *img)
1630 {
1631 pipe_resource_reference(&img->texture, NULL);
1632 FREE(img);
1633 }
1634
1635 static int
1636 dri2_get_capabilities(__DRIscreen *_screen)
1637 {
1638 struct dri_screen *screen = dri_screen(_screen);
1639
1640 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1641 }
1642
1643 /* The extension is modified during runtime if DRI_PRIME is detected */
1644 static __DRIimageExtension dri2ImageExtension = {
1645 .base = { __DRI_IMAGE, 15 },
1646
1647 .createImageFromName = dri2_create_image_from_name,
1648 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1649 .destroyImage = dri2_destroy_image,
1650 .createImage = dri2_create_image,
1651 .queryImage = dri2_query_image,
1652 .dupImage = dri2_dup_image,
1653 .validateUsage = dri2_validate_usage,
1654 .createImageFromNames = dri2_from_names,
1655 .fromPlanar = dri2_from_planar,
1656 .createImageFromTexture = dri2_create_from_texture,
1657 .createImageFromFds = NULL,
1658 .createImageFromDmaBufs = NULL,
1659 .blitImage = dri2_blit_image,
1660 .getCapabilities = dri2_get_capabilities,
1661 .mapImage = dri2_map_image,
1662 .unmapImage = dri2_unmap_image,
1663 };
1664
1665 static const __DRIrobustnessExtension dri2Robustness = {
1666 .base = { __DRI2_ROBUSTNESS, 1 }
1667 };
1668
1669 static int
1670 dri2_interop_query_device_info(__DRIcontext *_ctx,
1671 struct mesa_glinterop_device_info *out)
1672 {
1673 struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1674
1675 /* There is no version 0, thus we do not support it */
1676 if (out->version == 0)
1677 return MESA_GLINTEROP_INVALID_VERSION;
1678
1679 out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1680 out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1681 out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1682 out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1683
1684 out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1685 out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1686
1687 /* Instruct the caller that we support up-to version one of the interface */
1688 out->version = 1;
1689
1690 return MESA_GLINTEROP_SUCCESS;
1691 }
1692
1693 static int
1694 dri2_interop_export_object(__DRIcontext *_ctx,
1695 struct mesa_glinterop_export_in *in,
1696 struct mesa_glinterop_export_out *out)
1697 {
1698 struct st_context_iface *st = dri_context(_ctx)->st;
1699 struct pipe_screen *screen = st->pipe->screen;
1700 struct gl_context *ctx = ((struct st_context *)st)->ctx;
1701 struct pipe_resource *res = NULL;
1702 struct winsys_handle whandle;
1703 unsigned target, usage;
1704 boolean success;
1705
1706 /* There is no version 0, thus we do not support it */
1707 if (in->version == 0 || out->version == 0)
1708 return MESA_GLINTEROP_INVALID_VERSION;
1709
1710 /* Validate the target. */
1711 switch (in->target) {
1712 case GL_TEXTURE_BUFFER:
1713 case GL_TEXTURE_1D:
1714 case GL_TEXTURE_2D:
1715 case GL_TEXTURE_3D:
1716 case GL_TEXTURE_RECTANGLE:
1717 case GL_TEXTURE_1D_ARRAY:
1718 case GL_TEXTURE_2D_ARRAY:
1719 case GL_TEXTURE_CUBE_MAP_ARRAY:
1720 case GL_TEXTURE_CUBE_MAP:
1721 case GL_TEXTURE_2D_MULTISAMPLE:
1722 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1723 case GL_TEXTURE_EXTERNAL_OES:
1724 case GL_RENDERBUFFER:
1725 case GL_ARRAY_BUFFER:
1726 target = in->target;
1727 break;
1728 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1729 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1730 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1731 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1732 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1734 target = GL_TEXTURE_CUBE_MAP;
1735 break;
1736 default:
1737 return MESA_GLINTEROP_INVALID_TARGET;
1738 }
1739
1740 /* Validate the simple case of miplevel. */
1741 if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1742 in->miplevel != 0)
1743 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1744
1745 /* Validate the OpenGL object and get pipe_resource. */
1746 mtx_lock(&ctx->Shared->Mutex);
1747
1748 if (target == GL_ARRAY_BUFFER) {
1749 /* Buffer objects.
1750 *
1751 * The error checking is based on the documentation of
1752 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1753 */
1754 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1755
1756 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1757 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1758 * a GL buffer object but does not have an existing data store or
1759 * the size of the buffer is 0."
1760 */
1761 if (!buf || buf->Size == 0) {
1762 mtx_unlock(&ctx->Shared->Mutex);
1763 return MESA_GLINTEROP_INVALID_OBJECT;
1764 }
1765
1766 res = st_buffer_object(buf)->buffer;
1767 if (!res) {
1768 /* this shouldn't happen */
1769 mtx_unlock(&ctx->Shared->Mutex);
1770 return MESA_GLINTEROP_INVALID_OBJECT;
1771 }
1772
1773 out->buf_offset = 0;
1774 out->buf_size = buf->Size;
1775
1776 buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1777 } else if (target == GL_RENDERBUFFER) {
1778 /* Renderbuffers.
1779 *
1780 * The error checking is based on the documentation of
1781 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1782 */
1783 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1784
1785 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1786 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1787 * object or if the width or height of renderbuffer is zero."
1788 */
1789 if (!rb || rb->Width == 0 || rb->Height == 0) {
1790 mtx_unlock(&ctx->Shared->Mutex);
1791 return MESA_GLINTEROP_INVALID_OBJECT;
1792 }
1793
1794 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1795 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1796 * renderbuffer object."
1797 */
1798 if (rb->NumSamples > 1) {
1799 mtx_unlock(&ctx->Shared->Mutex);
1800 return MESA_GLINTEROP_INVALID_OPERATION;
1801 }
1802
1803 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1804 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1805 * required by the OpenCL implementation on the device."
1806 */
1807 res = st_renderbuffer(rb)->texture;
1808 if (!res) {
1809 mtx_unlock(&ctx->Shared->Mutex);
1810 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1811 }
1812
1813 out->internal_format = rb->InternalFormat;
1814 out->view_minlevel = 0;
1815 out->view_numlevels = 1;
1816 out->view_minlayer = 0;
1817 out->view_numlayers = 1;
1818 } else {
1819 /* Texture objects.
1820 *
1821 * The error checking is based on the documentation of
1822 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1823 */
1824 struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1825
1826 if (obj)
1827 _mesa_test_texobj_completeness(ctx, obj);
1828
1829 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1830 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1831 * type matches texture_target, if the specified miplevel of texture
1832 * is not defined, or if the width or height of the specified
1833 * miplevel is zero or if the GL texture object is incomplete."
1834 */
1835 if (!obj ||
1836 obj->Target != target ||
1837 !obj->_BaseComplete ||
1838 (in->miplevel > 0 && !obj->_MipmapComplete)) {
1839 mtx_unlock(&ctx->Shared->Mutex);
1840 return MESA_GLINTEROP_INVALID_OBJECT;
1841 }
1842
1843 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1844 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1845 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1846 * implementations); or greater than the value of q (for both OpenGL
1847 * and OpenGL ES). levelbase and q are defined for the texture in
1848 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1849 * specification and section 3.7.10 of the OpenGL ES 2.0."
1850 */
1851 if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
1852 mtx_unlock(&ctx->Shared->Mutex);
1853 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1854 }
1855
1856 if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
1857 mtx_unlock(&ctx->Shared->Mutex);
1858 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1859 }
1860
1861 res = st_get_texobj_resource(obj);
1862 if (!res) {
1863 /* Incomplete texture buffer object? This shouldn't really occur. */
1864 mtx_unlock(&ctx->Shared->Mutex);
1865 return MESA_GLINTEROP_INVALID_OBJECT;
1866 }
1867
1868 if (target == GL_TEXTURE_BUFFER) {
1869 out->internal_format = obj->BufferObjectFormat;
1870 out->buf_offset = obj->BufferOffset;
1871 out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1872 obj->BufferSize;
1873
1874 obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1875 } else {
1876 out->internal_format = obj->Image[0][0]->InternalFormat;
1877 out->view_minlevel = obj->MinLevel;
1878 out->view_numlevels = obj->NumLevels;
1879 out->view_minlayer = obj->MinLayer;
1880 out->view_numlayers = obj->NumLayers;
1881 }
1882 }
1883
1884 /* Get the handle. */
1885 switch (in->access) {
1886 case MESA_GLINTEROP_ACCESS_READ_WRITE:
1887 usage = PIPE_HANDLE_USAGE_READ_WRITE;
1888 break;
1889 case MESA_GLINTEROP_ACCESS_READ_ONLY:
1890 usage = PIPE_HANDLE_USAGE_READ;
1891 break;
1892 case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
1893 usage = PIPE_HANDLE_USAGE_WRITE;
1894 break;
1895 default:
1896 usage = 0;
1897 }
1898
1899 memset(&whandle, 0, sizeof(whandle));
1900 whandle.type = DRM_API_HANDLE_TYPE_FD;
1901
1902 success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
1903 usage);
1904 mtx_unlock(&ctx->Shared->Mutex);
1905
1906 if (!success)
1907 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
1908
1909 out->dmabuf_fd = whandle.handle;
1910 out->out_driver_data_written = 0;
1911
1912 if (res->target == PIPE_BUFFER)
1913 out->buf_offset += whandle.offset;
1914
1915 /* Instruct the caller that we support up-to version one of the interface */
1916 in->version = 1;
1917 out->version = 1;
1918
1919 return MESA_GLINTEROP_SUCCESS;
1920 }
1921
1922 static const __DRI2interopExtension dri2InteropExtension = {
1923 .base = { __DRI2_INTEROP, 1 },
1924 .query_device_info = dri2_interop_query_device_info,
1925 .export_object = dri2_interop_export_object
1926 };
1927
1928 /**
1929 * \brief the DRI2ConfigQueryExtension configQueryb method
1930 */
1931 static int
1932 dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
1933 unsigned char *val)
1934 {
1935 struct dri_screen *screen = dri_screen(sPriv);
1936
1937 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
1938 return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
1939
1940 *val = driQueryOptionb(&screen->optionCache, var);
1941
1942 return 0;
1943 }
1944
1945 /**
1946 * \brief the DRI2ConfigQueryExtension configQueryi method
1947 */
1948 static int
1949 dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
1950 {
1951 struct dri_screen *screen = dri_screen(sPriv);
1952
1953 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
1954 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
1955 return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
1956
1957 *val = driQueryOptioni(&screen->optionCache, var);
1958
1959 return 0;
1960 }
1961
1962 /**
1963 * \brief the DRI2ConfigQueryExtension configQueryf method
1964 */
1965 static int
1966 dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
1967 {
1968 struct dri_screen *screen = dri_screen(sPriv);
1969
1970 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
1971 return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
1972
1973 *val = driQueryOptionf(&screen->optionCache, var);
1974
1975 return 0;
1976 }
1977
1978 /**
1979 * \brief the DRI2ConfigQueryExtension struct.
1980 *
1981 * We first query the driver option cache. Then the dri2 option cache.
1982 */
1983 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
1984 .base = { __DRI2_CONFIG_QUERY, 1 },
1985
1986 .configQueryb = dri2GalliumConfigQueryb,
1987 .configQueryi = dri2GalliumConfigQueryi,
1988 .configQueryf = dri2GalliumConfigQueryf,
1989 };
1990
1991 /*
1992 * Backend function init_screen.
1993 */
1994
1995 static const __DRIextension *dri_screen_extensions[] = {
1996 &driTexBufferExtension.base,
1997 &dri2FlushExtension.base,
1998 &dri2ImageExtension.base,
1999 &dri2RendererQueryExtension.base,
2000 &dri2GalliumConfigQueryExtension.base,
2001 &dri2ThrottleExtension.base,
2002 &dri2FenceExtension.base,
2003 &dri2InteropExtension.base,
2004 &dri2NoErrorExtension.base,
2005 NULL
2006 };
2007
2008 static const __DRIextension *dri_robust_screen_extensions[] = {
2009 &driTexBufferExtension.base,
2010 &dri2FlushExtension.base,
2011 &dri2ImageExtension.base,
2012 &dri2RendererQueryExtension.base,
2013 &dri2GalliumConfigQueryExtension.base,
2014 &dri2ThrottleExtension.base,
2015 &dri2FenceExtension.base,
2016 &dri2InteropExtension.base,
2017 &dri2Robustness.base,
2018 &dri2NoErrorExtension.base,
2019 NULL
2020 };
2021
2022 /**
2023 * This is the driver specific part of the createNewScreen entry point.
2024 *
2025 * Returns the struct gl_config supported by this driver.
2026 */
2027 static const __DRIconfig **
2028 dri2_init_screen(__DRIscreen * sPriv)
2029 {
2030 const __DRIconfig **configs;
2031 struct dri_screen *screen;
2032 struct pipe_screen *pscreen = NULL;
2033 const struct drm_conf_ret *throttle_ret;
2034 const struct drm_conf_ret *dmabuf_ret;
2035 int fd;
2036
2037 screen = CALLOC_STRUCT(dri_screen);
2038 if (!screen)
2039 return NULL;
2040
2041 screen->sPriv = sPriv;
2042 screen->fd = sPriv->fd;
2043 (void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
2044
2045 sPriv->driverPrivate = (void *)screen;
2046
2047 if (screen->fd < 0 || (fd = fcntl(screen->fd, F_DUPFD_CLOEXEC, 3)) < 0)
2048 goto free_screen;
2049
2050
2051 if (pipe_loader_drm_probe_fd(&screen->dev, fd)) {
2052 unsigned flags =
2053 dri_init_options_get_screen_flags(screen, screen->dev->driver_name);
2054
2055 pscreen = pipe_loader_create_screen(screen->dev, flags);
2056 }
2057
2058 if (!pscreen)
2059 goto release_pipe;
2060
2061 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
2062 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
2063
2064 if (throttle_ret && throttle_ret->val.val_int != -1) {
2065 screen->throttling_enabled = TRUE;
2066 screen->default_throttle_frames = throttle_ret->val.val_int;
2067 }
2068
2069 if (pscreen->resource_create_with_modifiers)
2070 dri2ImageExtension.createImageWithModifiers =
2071 dri2_create_image_with_modifiers;
2072
2073 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
2074 uint64_t cap;
2075
2076 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2077 (cap & DRM_PRIME_CAP_IMPORT)) {
2078 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2079 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2080 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2081 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2082 dri2ImageExtension.queryDmaBufModifiers =
2083 dri2_query_dma_buf_modifiers;
2084 }
2085 }
2086
2087 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
2088 sPriv->extensions = dri_robust_screen_extensions;
2089 screen->has_reset_status_query = true;
2090 }
2091 else
2092 sPriv->extensions = dri_screen_extensions;
2093
2094 configs = dri_init_screen_helper(screen, pscreen);
2095 if (!configs)
2096 goto destroy_screen;
2097
2098 screen->can_share_buffer = true;
2099 screen->auto_fake_front = dri_with_format(sPriv);
2100 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2101 screen->lookup_egl_image = dri2_lookup_egl_image;
2102
2103 return configs;
2104
2105 destroy_screen:
2106 dri_destroy_screen_helper(screen);
2107
2108 release_pipe:
2109 if (screen->dev)
2110 pipe_loader_release(&screen->dev, 1);
2111 else
2112 close(fd);
2113
2114 free_screen:
2115 FREE(screen);
2116 return NULL;
2117 }
2118
2119 /**
2120 * This is the driver specific part of the createNewScreen entry point.
2121 *
2122 * Returns the struct gl_config supported by this driver.
2123 */
2124 static const __DRIconfig **
2125 dri_kms_init_screen(__DRIscreen * sPriv)
2126 {
2127 #if defined(GALLIUM_SOFTPIPE)
2128 const __DRIconfig **configs;
2129 struct dri_screen *screen;
2130 struct pipe_screen *pscreen = NULL;
2131 uint64_t cap;
2132 int fd;
2133
2134 screen = CALLOC_STRUCT(dri_screen);
2135 if (!screen)
2136 return NULL;
2137
2138 screen->sPriv = sPriv;
2139 screen->fd = sPriv->fd;
2140
2141 sPriv->driverPrivate = (void *)screen;
2142
2143 if (screen->fd < 0 || (fd = fcntl(screen->fd, F_DUPFD_CLOEXEC, 3)) < 0)
2144 goto free_screen;
2145
2146 unsigned flags = dri_init_options_get_screen_flags(screen, "swrast");
2147
2148 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
2149 pscreen = pipe_loader_create_screen(screen->dev, flags);
2150
2151 if (!pscreen)
2152 goto release_pipe;
2153
2154 if (pscreen->resource_create_with_modifiers)
2155 dri2ImageExtension.createImageWithModifiers =
2156 dri2_create_image_with_modifiers;
2157
2158 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2159 (cap & DRM_PRIME_CAP_IMPORT)) {
2160 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2161 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2162 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2163 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2164 dri2ImageExtension.queryDmaBufModifiers = dri2_query_dma_buf_modifiers;
2165 }
2166
2167 sPriv->extensions = dri_screen_extensions;
2168
2169 configs = dri_init_screen_helper(screen, pscreen);
2170 if (!configs)
2171 goto destroy_screen;
2172
2173 screen->can_share_buffer = false;
2174 screen->auto_fake_front = dri_with_format(sPriv);
2175 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2176 screen->lookup_egl_image = dri2_lookup_egl_image;
2177
2178 return configs;
2179
2180 destroy_screen:
2181 dri_destroy_screen_helper(screen);
2182
2183 release_pipe:
2184 if (screen->dev)
2185 pipe_loader_release(&screen->dev, 1);
2186 else
2187 close(fd);
2188
2189 free_screen:
2190 FREE(screen);
2191 #endif // GALLIUM_SOFTPIPE
2192 return NULL;
2193 }
2194
2195 static boolean
2196 dri2_create_buffer(__DRIscreen * sPriv,
2197 __DRIdrawable * dPriv,
2198 const struct gl_config * visual, boolean isPixmap)
2199 {
2200 struct dri_drawable *drawable = NULL;
2201
2202 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
2203 return FALSE;
2204
2205 drawable = dPriv->driverPrivate;
2206
2207 drawable->allocate_textures = dri2_allocate_textures;
2208 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
2209 drawable->update_tex_buffer = dri2_update_tex_buffer;
2210
2211 return TRUE;
2212 }
2213
2214 /**
2215 * DRI driver virtual function table.
2216 *
2217 * DRI versions differ in their implementation of init_screen and swap_buffers.
2218 */
2219 const struct __DriverAPIRec galliumdrm_driver_api = {
2220 .InitScreen = dri2_init_screen,
2221 .DestroyScreen = dri_destroy_screen,
2222 .CreateContext = dri_create_context,
2223 .DestroyContext = dri_destroy_context,
2224 .CreateBuffer = dri2_create_buffer,
2225 .DestroyBuffer = dri_destroy_buffer,
2226 .MakeCurrent = dri_make_current,
2227 .UnbindContext = dri_unbind_context,
2228
2229 .AllocateBuffer = dri2_allocate_buffer,
2230 .ReleaseBuffer = dri2_release_buffer,
2231 };
2232
2233 /**
2234 * DRI driver virtual function table.
2235 *
2236 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2237 * hook. The latter is used to explicitly initialise the kms_swrast driver
2238 * rather than selecting the approapriate driver as suggested by the loader.
2239 */
2240 const struct __DriverAPIRec dri_kms_driver_api = {
2241 .InitScreen = dri_kms_init_screen,
2242 .DestroyScreen = dri_destroy_screen,
2243 .CreateContext = dri_create_context,
2244 .DestroyContext = dri_destroy_context,
2245 .CreateBuffer = dri2_create_buffer,
2246 .DestroyBuffer = dri_destroy_buffer,
2247 .MakeCurrent = dri_make_current,
2248 .UnbindContext = dri_unbind_context,
2249
2250 .AllocateBuffer = dri2_allocate_buffer,
2251 .ReleaseBuffer = dri2_release_buffer,
2252 };
2253
2254 /* This is the table of extensions that the loader will dlsym() for. */
2255 const __DRIextension *galliumdrm_driver_extensions[] = {
2256 &driCoreExtension.base,
2257 &driImageDriverExtension.base,
2258 &driDRI2Extension.base,
2259 &gallium_config_options.base,
2260 NULL
2261 };
2262
2263 /* vim: set sw=3 ts=8 sts=3 expandtab: */