st/dri: add 32-bit RGBX/RGBA formats
[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 image->texture->screen->resource_get_handle(image->texture->screen,
1187 NULL, image->texture, &whandle, usage);
1188 *value = whandle.stride;
1189 return GL_TRUE;
1190 case __DRI_IMAGE_ATTRIB_OFFSET:
1191 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1192 image->texture->screen->resource_get_handle(image->texture->screen,
1193 NULL, image->texture, &whandle, usage);
1194 *value = whandle.offset;
1195 return GL_TRUE;
1196 case __DRI_IMAGE_ATTRIB_HANDLE:
1197 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1198 image->texture->screen->resource_get_handle(image->texture->screen,
1199 NULL, image->texture, &whandle, usage);
1200 *value = whandle.handle;
1201 return GL_TRUE;
1202 case __DRI_IMAGE_ATTRIB_NAME:
1203 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1204 image->texture->screen->resource_get_handle(image->texture->screen,
1205 NULL, image->texture, &whandle, usage);
1206 *value = whandle.handle;
1207 return GL_TRUE;
1208 case __DRI_IMAGE_ATTRIB_FD:
1209 whandle.type= DRM_API_HANDLE_TYPE_FD;
1210 if (!image->texture->screen->resource_get_handle(image->texture->screen,
1211 NULL, image->texture, &whandle, usage))
1212 return GL_FALSE;
1213
1214 *value = whandle.handle;
1215 return GL_TRUE;
1216 case __DRI_IMAGE_ATTRIB_FORMAT:
1217 *value = image->dri_format;
1218 return GL_TRUE;
1219 case __DRI_IMAGE_ATTRIB_WIDTH:
1220 *value = image->texture->width0;
1221 return GL_TRUE;
1222 case __DRI_IMAGE_ATTRIB_HEIGHT:
1223 *value = image->texture->height0;
1224 return GL_TRUE;
1225 case __DRI_IMAGE_ATTRIB_COMPONENTS:
1226 if (image->dri_components == 0)
1227 return GL_FALSE;
1228 *value = image->dri_components;
1229 return GL_TRUE;
1230 case __DRI_IMAGE_ATTRIB_FOURCC:
1231 *value = convert_to_fourcc(image->dri_format);
1232 return GL_TRUE;
1233 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1234 *value = 1;
1235 return GL_TRUE;
1236 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1237 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1238 image->texture->screen->resource_get_handle(image->texture->screen,
1239 NULL, image->texture, &whandle, usage);
1240 *value = (whandle.modifier >> 32) & 0xffffffff;
1241 return GL_TRUE;
1242 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1243 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1244 image->texture->screen->resource_get_handle(image->texture->screen,
1245 NULL, image->texture, &whandle, usage);
1246 *value = whandle.modifier & 0xffffffff;
1247 return GL_TRUE;
1248 default:
1249 return GL_FALSE;
1250 }
1251 }
1252
1253 static __DRIimage *
1254 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1255 {
1256 __DRIimage *img;
1257
1258 img = CALLOC_STRUCT(__DRIimageRec);
1259 if (!img)
1260 return NULL;
1261
1262 img->texture = NULL;
1263 pipe_resource_reference(&img->texture, image->texture);
1264 img->level = image->level;
1265 img->layer = image->layer;
1266 img->dri_format = image->dri_format;
1267 /* This should be 0 for sub images, but dup is also used for base images. */
1268 img->dri_components = image->dri_components;
1269 img->loader_private = loaderPrivate;
1270
1271 return img;
1272 }
1273
1274 static GLboolean
1275 dri2_validate_usage(__DRIimage *image, unsigned int use)
1276 {
1277 /*
1278 * Gallium drivers are bad at adding usages to the resources
1279 * once opened again in another process, which is the main use
1280 * case for this, so we have to lie.
1281 */
1282 if (image != NULL)
1283 return GL_TRUE;
1284 else
1285 return GL_FALSE;
1286 }
1287
1288 static __DRIimage *
1289 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1290 int *names, int num_names, int *strides, int *offsets,
1291 void *loaderPrivate)
1292 {
1293 __DRIimage *img;
1294 int dri_components;
1295 struct winsys_handle whandle;
1296
1297 if (num_names != 1)
1298 return NULL;
1299
1300 format = convert_fourcc(format, &dri_components);
1301 if (format == -1)
1302 return NULL;
1303
1304 memset(&whandle, 0, sizeof(whandle));
1305 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1306 whandle.handle = names[0];
1307 whandle.stride = strides[0];
1308 whandle.offset = offsets[0];
1309 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1310
1311 img = dri2_create_image_from_winsys(screen, width, height, format,
1312 1, &whandle, loaderPrivate);
1313 if (img == NULL)
1314 return NULL;
1315
1316 img->dri_components = dri_components;
1317 return img;
1318 }
1319
1320 static __DRIimage *
1321 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1322 {
1323 __DRIimage *img;
1324
1325 if (plane != 0)
1326 return NULL;
1327
1328 if (image->dri_components == 0)
1329 return NULL;
1330
1331 img = dri2_dup_image(image, loaderPrivate);
1332 if (img == NULL)
1333 return NULL;
1334
1335 if (img->texture->screen->resource_changed)
1336 img->texture->screen->resource_changed(img->texture->screen,
1337 img->texture);
1338
1339 /* set this to 0 for sub images. */
1340 img->dri_components = 0;
1341 return img;
1342 }
1343
1344 static __DRIimage *
1345 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1346 int depth, int level, unsigned *error,
1347 void *loaderPrivate)
1348 {
1349 __DRIimage *img;
1350 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1351 struct gl_texture_object *obj;
1352 struct pipe_resource *tex;
1353 GLuint face = 0;
1354
1355 obj = _mesa_lookup_texture(ctx, texture);
1356 if (!obj || obj->Target != target) {
1357 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1358 return NULL;
1359 }
1360
1361 tex = st_get_texobj_resource(obj);
1362 if (!tex) {
1363 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1364 return NULL;
1365 }
1366
1367 if (target == GL_TEXTURE_CUBE_MAP)
1368 face = depth;
1369
1370 _mesa_test_texobj_completeness(ctx, obj);
1371 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1372 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1373 return NULL;
1374 }
1375
1376 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1377 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1378 return NULL;
1379 }
1380
1381 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1382 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1383 return NULL;
1384 }
1385
1386 img = CALLOC_STRUCT(__DRIimageRec);
1387 if (!img) {
1388 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1389 return NULL;
1390 }
1391
1392 img->level = level;
1393 img->layer = depth;
1394 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1395
1396 img->loader_private = loaderPrivate;
1397
1398 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1399 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1400 free(img);
1401 return NULL;
1402 }
1403
1404 pipe_resource_reference(&img->texture, tex);
1405
1406 *error = __DRI_IMAGE_ERROR_SUCCESS;
1407 return img;
1408 }
1409
1410 static __DRIimage *
1411 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1412 int *fds, int num_fds, int *strides, int *offsets,
1413 void *loaderPrivate)
1414 {
1415 __DRIimage *img;
1416 int dri_components;
1417
1418 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1419 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1420 strides, offsets, NULL,
1421 &dri_components, loaderPrivate);
1422 if (img == NULL)
1423 return NULL;
1424
1425 img->dri_components = dri_components;
1426 return img;
1427 }
1428
1429 static boolean
1430 dri2_query_dma_buf_formats(__DRIscreen *_screen, int max, int *formats,
1431 int *count)
1432 {
1433 struct dri_screen *screen = dri_screen(_screen);
1434 struct pipe_screen *pscreen = screen->base.screen;
1435 const unsigned bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
1436 int i, j;
1437
1438 for (i = 0, j = 0; (i < ARRAY_SIZE(fourcc_formats)) &&
1439 (j < max || max == 0); i++) {
1440 if (pscreen->is_format_supported(pscreen,
1441 fourcc_to_pipe_format(
1442 fourcc_formats[i]),
1443 screen->target,
1444 0, bind)) {
1445 if (j < max)
1446 formats[j] = fourcc_formats[i];
1447 j++;
1448 }
1449 }
1450 *count = j;
1451 return true;
1452 }
1453
1454 static boolean
1455 dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1456 uint64_t *modifiers, unsigned int *external_only,
1457 int *count)
1458 {
1459 struct dri_screen *screen = dri_screen(_screen);
1460 struct pipe_screen *pscreen = screen->base.screen;
1461 enum pipe_format format = fourcc_to_pipe_format(fourcc);
1462 const unsigned usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
1463
1464 if (pscreen->query_dmabuf_modifiers != NULL &&
1465 pscreen->is_format_supported(pscreen, format, screen->target, 0, usage)) {
1466 pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
1467 external_only, count);
1468 return true;
1469 }
1470 return false;
1471 }
1472
1473 static __DRIimage *
1474 dri2_from_dma_bufs(__DRIscreen *screen,
1475 int width, int height, int fourcc,
1476 int *fds, int num_fds,
1477 int *strides, int *offsets,
1478 enum __DRIYUVColorSpace yuv_color_space,
1479 enum __DRISampleRange sample_range,
1480 enum __DRIChromaSiting horizontal_siting,
1481 enum __DRIChromaSiting vertical_siting,
1482 unsigned *error,
1483 void *loaderPrivate)
1484 {
1485 __DRIimage *img;
1486 int dri_components;
1487
1488 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1489 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1490 strides, offsets, error,
1491 &dri_components, loaderPrivate);
1492 if (img == NULL)
1493 return NULL;
1494
1495 img->yuv_color_space = yuv_color_space;
1496 img->sample_range = sample_range;
1497 img->horizontal_siting = horizontal_siting;
1498 img->vertical_siting = vertical_siting;
1499 img->dri_components = dri_components;
1500
1501 *error = __DRI_IMAGE_ERROR_SUCCESS;
1502 return img;
1503 }
1504
1505 static __DRIimage *
1506 dri2_from_dma_bufs2(__DRIscreen *screen,
1507 int width, int height, int fourcc,
1508 uint64_t modifier, int *fds, int num_fds,
1509 int *strides, int *offsets,
1510 enum __DRIYUVColorSpace yuv_color_space,
1511 enum __DRISampleRange sample_range,
1512 enum __DRIChromaSiting horizontal_siting,
1513 enum __DRIChromaSiting vertical_siting,
1514 unsigned *error,
1515 void *loaderPrivate)
1516 {
1517 __DRIimage *img;
1518 int dri_components;
1519
1520 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1521 modifier, fds, num_fds, strides, offsets,
1522 error, &dri_components, loaderPrivate);
1523 if (img == NULL)
1524 return NULL;
1525
1526 img->yuv_color_space = yuv_color_space;
1527 img->sample_range = sample_range;
1528 img->horizontal_siting = horizontal_siting;
1529 img->vertical_siting = vertical_siting;
1530 img->dri_components = dri_components;
1531
1532 *error = __DRI_IMAGE_ERROR_SUCCESS;
1533 return img;
1534 }
1535
1536 static void
1537 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1538 int dstx0, int dsty0, int dstwidth, int dstheight,
1539 int srcx0, int srcy0, int srcwidth, int srcheight,
1540 int flush_flag)
1541 {
1542 struct dri_context *ctx = dri_context(context);
1543 struct pipe_context *pipe = ctx->st->pipe;
1544 struct pipe_screen *screen;
1545 struct pipe_fence_handle *fence;
1546 struct pipe_blit_info blit;
1547
1548 if (!dst || !src)
1549 return;
1550
1551 memset(&blit, 0, sizeof(blit));
1552 blit.dst.resource = dst->texture;
1553 blit.dst.box.x = dstx0;
1554 blit.dst.box.y = dsty0;
1555 blit.dst.box.width = dstwidth;
1556 blit.dst.box.height = dstheight;
1557 blit.dst.box.depth = 1;
1558 blit.dst.format = dst->texture->format;
1559 blit.src.resource = src->texture;
1560 blit.src.box.x = srcx0;
1561 blit.src.box.y = srcy0;
1562 blit.src.box.width = srcwidth;
1563 blit.src.box.height = srcheight;
1564 blit.src.box.depth = 1;
1565 blit.src.format = src->texture->format;
1566 blit.mask = PIPE_MASK_RGBA;
1567 blit.filter = PIPE_TEX_FILTER_NEAREST;
1568
1569 pipe->blit(pipe, &blit);
1570
1571 if (flush_flag == __BLIT_FLAG_FLUSH) {
1572 pipe->flush_resource(pipe, dst->texture);
1573 ctx->st->flush(ctx->st, 0, NULL);
1574 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1575 screen = dri_screen(ctx->sPriv)->base.screen;
1576 pipe->flush_resource(pipe, dst->texture);
1577 ctx->st->flush(ctx->st, 0, &fence);
1578 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
1579 screen->fence_reference(screen, &fence, NULL);
1580 }
1581 }
1582
1583 static void *
1584 dri2_map_image(__DRIcontext *context, __DRIimage *image,
1585 int x0, int y0, int width, int height,
1586 unsigned int flags, int *stride, void **data)
1587 {
1588 struct dri_context *ctx = dri_context(context);
1589 struct pipe_context *pipe = ctx->st->pipe;
1590 enum pipe_transfer_usage pipe_access = 0;
1591 struct pipe_transfer *trans;
1592 void *map;
1593
1594 if (!image || !data || *data)
1595 return NULL;
1596
1597 if (flags & __DRI_IMAGE_TRANSFER_READ)
1598 pipe_access |= PIPE_TRANSFER_READ;
1599 if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1600 pipe_access |= PIPE_TRANSFER_WRITE;
1601
1602 map = pipe_transfer_map(pipe, image->texture,
1603 0, 0, pipe_access, x0, y0, width, height,
1604 &trans);
1605 if (map) {
1606 *data = trans;
1607 *stride = trans->stride;
1608 }
1609
1610 return map;
1611 }
1612
1613 static void
1614 dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1615 {
1616 struct dri_context *ctx = dri_context(context);
1617 struct pipe_context *pipe = ctx->st->pipe;
1618
1619 pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
1620 }
1621
1622 static void
1623 dri2_destroy_image(__DRIimage *img)
1624 {
1625 pipe_resource_reference(&img->texture, NULL);
1626 FREE(img);
1627 }
1628
1629 static int
1630 dri2_get_capabilities(__DRIscreen *_screen)
1631 {
1632 struct dri_screen *screen = dri_screen(_screen);
1633
1634 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1635 }
1636
1637 /* The extension is modified during runtime if DRI_PRIME is detected */
1638 static __DRIimageExtension dri2ImageExtension = {
1639 .base = { __DRI_IMAGE, 15 },
1640
1641 .createImageFromName = dri2_create_image_from_name,
1642 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1643 .destroyImage = dri2_destroy_image,
1644 .createImage = dri2_create_image,
1645 .queryImage = dri2_query_image,
1646 .dupImage = dri2_dup_image,
1647 .validateUsage = dri2_validate_usage,
1648 .createImageFromNames = dri2_from_names,
1649 .fromPlanar = dri2_from_planar,
1650 .createImageFromTexture = dri2_create_from_texture,
1651 .createImageFromFds = NULL,
1652 .createImageFromDmaBufs = NULL,
1653 .blitImage = dri2_blit_image,
1654 .getCapabilities = dri2_get_capabilities,
1655 .mapImage = dri2_map_image,
1656 .unmapImage = dri2_unmap_image,
1657 };
1658
1659 static const __DRIrobustnessExtension dri2Robustness = {
1660 .base = { __DRI2_ROBUSTNESS, 1 }
1661 };
1662
1663 static int
1664 dri2_interop_query_device_info(__DRIcontext *_ctx,
1665 struct mesa_glinterop_device_info *out)
1666 {
1667 struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1668
1669 /* There is no version 0, thus we do not support it */
1670 if (out->version == 0)
1671 return MESA_GLINTEROP_INVALID_VERSION;
1672
1673 out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1674 out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1675 out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1676 out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1677
1678 out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1679 out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1680
1681 /* Instruct the caller that we support up-to version one of the interface */
1682 out->version = 1;
1683
1684 return MESA_GLINTEROP_SUCCESS;
1685 }
1686
1687 static int
1688 dri2_interop_export_object(__DRIcontext *_ctx,
1689 struct mesa_glinterop_export_in *in,
1690 struct mesa_glinterop_export_out *out)
1691 {
1692 struct st_context_iface *st = dri_context(_ctx)->st;
1693 struct pipe_screen *screen = st->pipe->screen;
1694 struct gl_context *ctx = ((struct st_context *)st)->ctx;
1695 struct pipe_resource *res = NULL;
1696 struct winsys_handle whandle;
1697 unsigned target, usage;
1698 boolean success;
1699
1700 /* There is no version 0, thus we do not support it */
1701 if (in->version == 0 || out->version == 0)
1702 return MESA_GLINTEROP_INVALID_VERSION;
1703
1704 /* Validate the target. */
1705 switch (in->target) {
1706 case GL_TEXTURE_BUFFER:
1707 case GL_TEXTURE_1D:
1708 case GL_TEXTURE_2D:
1709 case GL_TEXTURE_3D:
1710 case GL_TEXTURE_RECTANGLE:
1711 case GL_TEXTURE_1D_ARRAY:
1712 case GL_TEXTURE_2D_ARRAY:
1713 case GL_TEXTURE_CUBE_MAP_ARRAY:
1714 case GL_TEXTURE_CUBE_MAP:
1715 case GL_TEXTURE_2D_MULTISAMPLE:
1716 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1717 case GL_TEXTURE_EXTERNAL_OES:
1718 case GL_RENDERBUFFER:
1719 case GL_ARRAY_BUFFER:
1720 target = in->target;
1721 break;
1722 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1723 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1724 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1725 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1726 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1727 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1728 target = GL_TEXTURE_CUBE_MAP;
1729 break;
1730 default:
1731 return MESA_GLINTEROP_INVALID_TARGET;
1732 }
1733
1734 /* Validate the simple case of miplevel. */
1735 if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1736 in->miplevel != 0)
1737 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1738
1739 /* Validate the OpenGL object and get pipe_resource. */
1740 mtx_lock(&ctx->Shared->Mutex);
1741
1742 if (target == GL_ARRAY_BUFFER) {
1743 /* Buffer objects.
1744 *
1745 * The error checking is based on the documentation of
1746 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1747 */
1748 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1749
1750 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1751 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1752 * a GL buffer object but does not have an existing data store or
1753 * the size of the buffer is 0."
1754 */
1755 if (!buf || buf->Size == 0) {
1756 mtx_unlock(&ctx->Shared->Mutex);
1757 return MESA_GLINTEROP_INVALID_OBJECT;
1758 }
1759
1760 res = st_buffer_object(buf)->buffer;
1761 if (!res) {
1762 /* this shouldn't happen */
1763 mtx_unlock(&ctx->Shared->Mutex);
1764 return MESA_GLINTEROP_INVALID_OBJECT;
1765 }
1766
1767 out->buf_offset = 0;
1768 out->buf_size = buf->Size;
1769
1770 buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1771 } else if (target == GL_RENDERBUFFER) {
1772 /* Renderbuffers.
1773 *
1774 * The error checking is based on the documentation of
1775 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1776 */
1777 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1778
1779 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1780 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1781 * object or if the width or height of renderbuffer is zero."
1782 */
1783 if (!rb || rb->Width == 0 || rb->Height == 0) {
1784 mtx_unlock(&ctx->Shared->Mutex);
1785 return MESA_GLINTEROP_INVALID_OBJECT;
1786 }
1787
1788 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1789 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1790 * renderbuffer object."
1791 */
1792 if (rb->NumSamples > 1) {
1793 mtx_unlock(&ctx->Shared->Mutex);
1794 return MESA_GLINTEROP_INVALID_OPERATION;
1795 }
1796
1797 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1798 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1799 * required by the OpenCL implementation on the device."
1800 */
1801 res = st_renderbuffer(rb)->texture;
1802 if (!res) {
1803 mtx_unlock(&ctx->Shared->Mutex);
1804 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1805 }
1806
1807 out->internal_format = rb->InternalFormat;
1808 out->view_minlevel = 0;
1809 out->view_numlevels = 1;
1810 out->view_minlayer = 0;
1811 out->view_numlayers = 1;
1812 } else {
1813 /* Texture objects.
1814 *
1815 * The error checking is based on the documentation of
1816 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1817 */
1818 struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1819
1820 if (obj)
1821 _mesa_test_texobj_completeness(ctx, obj);
1822
1823 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1824 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1825 * type matches texture_target, if the specified miplevel of texture
1826 * is not defined, or if the width or height of the specified
1827 * miplevel is zero or if the GL texture object is incomplete."
1828 */
1829 if (!obj ||
1830 obj->Target != target ||
1831 !obj->_BaseComplete ||
1832 (in->miplevel > 0 && !obj->_MipmapComplete)) {
1833 mtx_unlock(&ctx->Shared->Mutex);
1834 return MESA_GLINTEROP_INVALID_OBJECT;
1835 }
1836
1837 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1838 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1839 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1840 * implementations); or greater than the value of q (for both OpenGL
1841 * and OpenGL ES). levelbase and q are defined for the texture in
1842 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1843 * specification and section 3.7.10 of the OpenGL ES 2.0."
1844 */
1845 if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
1846 mtx_unlock(&ctx->Shared->Mutex);
1847 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1848 }
1849
1850 if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
1851 mtx_unlock(&ctx->Shared->Mutex);
1852 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1853 }
1854
1855 res = st_get_texobj_resource(obj);
1856 if (!res) {
1857 /* Incomplete texture buffer object? This shouldn't really occur. */
1858 mtx_unlock(&ctx->Shared->Mutex);
1859 return MESA_GLINTEROP_INVALID_OBJECT;
1860 }
1861
1862 if (target == GL_TEXTURE_BUFFER) {
1863 out->internal_format = obj->BufferObjectFormat;
1864 out->buf_offset = obj->BufferOffset;
1865 out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1866 obj->BufferSize;
1867
1868 obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1869 } else {
1870 out->internal_format = obj->Image[0][0]->InternalFormat;
1871 out->view_minlevel = obj->MinLevel;
1872 out->view_numlevels = obj->NumLevels;
1873 out->view_minlayer = obj->MinLayer;
1874 out->view_numlayers = obj->NumLayers;
1875 }
1876 }
1877
1878 /* Get the handle. */
1879 switch (in->access) {
1880 case MESA_GLINTEROP_ACCESS_READ_WRITE:
1881 usage = PIPE_HANDLE_USAGE_READ_WRITE;
1882 break;
1883 case MESA_GLINTEROP_ACCESS_READ_ONLY:
1884 usage = PIPE_HANDLE_USAGE_READ;
1885 break;
1886 case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
1887 usage = PIPE_HANDLE_USAGE_WRITE;
1888 break;
1889 default:
1890 usage = 0;
1891 }
1892
1893 memset(&whandle, 0, sizeof(whandle));
1894 whandle.type = DRM_API_HANDLE_TYPE_FD;
1895
1896 success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
1897 usage);
1898 mtx_unlock(&ctx->Shared->Mutex);
1899
1900 if (!success)
1901 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
1902
1903 out->dmabuf_fd = whandle.handle;
1904 out->out_driver_data_written = 0;
1905
1906 if (res->target == PIPE_BUFFER)
1907 out->buf_offset += whandle.offset;
1908
1909 /* Instruct the caller that we support up-to version one of the interface */
1910 in->version = 1;
1911 out->version = 1;
1912
1913 return MESA_GLINTEROP_SUCCESS;
1914 }
1915
1916 static const __DRI2interopExtension dri2InteropExtension = {
1917 .base = { __DRI2_INTEROP, 1 },
1918 .query_device_info = dri2_interop_query_device_info,
1919 .export_object = dri2_interop_export_object
1920 };
1921
1922 /**
1923 * \brief the DRI2ConfigQueryExtension configQueryb method
1924 */
1925 static int
1926 dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
1927 unsigned char *val)
1928 {
1929 struct dri_screen *screen = dri_screen(sPriv);
1930
1931 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
1932 return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
1933
1934 *val = driQueryOptionb(&screen->optionCache, var);
1935
1936 return 0;
1937 }
1938
1939 /**
1940 * \brief the DRI2ConfigQueryExtension configQueryi method
1941 */
1942 static int
1943 dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
1944 {
1945 struct dri_screen *screen = dri_screen(sPriv);
1946
1947 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
1948 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
1949 return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
1950
1951 *val = driQueryOptioni(&screen->optionCache, var);
1952
1953 return 0;
1954 }
1955
1956 /**
1957 * \brief the DRI2ConfigQueryExtension configQueryf method
1958 */
1959 static int
1960 dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
1961 {
1962 struct dri_screen *screen = dri_screen(sPriv);
1963
1964 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
1965 return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
1966
1967 *val = driQueryOptionf(&screen->optionCache, var);
1968
1969 return 0;
1970 }
1971
1972 /**
1973 * \brief the DRI2ConfigQueryExtension struct.
1974 *
1975 * We first query the driver option cache. Then the dri2 option cache.
1976 */
1977 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
1978 .base = { __DRI2_CONFIG_QUERY, 1 },
1979
1980 .configQueryb = dri2GalliumConfigQueryb,
1981 .configQueryi = dri2GalliumConfigQueryi,
1982 .configQueryf = dri2GalliumConfigQueryf,
1983 };
1984
1985 /*
1986 * Backend function init_screen.
1987 */
1988
1989 static const __DRIextension *dri_screen_extensions[] = {
1990 &driTexBufferExtension.base,
1991 &dri2FlushExtension.base,
1992 &dri2ImageExtension.base,
1993 &dri2RendererQueryExtension.base,
1994 &dri2GalliumConfigQueryExtension.base,
1995 &dri2ThrottleExtension.base,
1996 &dri2FenceExtension.base,
1997 &dri2InteropExtension.base,
1998 NULL
1999 };
2000
2001 static const __DRIextension *dri_robust_screen_extensions[] = {
2002 &driTexBufferExtension.base,
2003 &dri2FlushExtension.base,
2004 &dri2ImageExtension.base,
2005 &dri2RendererQueryExtension.base,
2006 &dri2ConfigQueryExtension.base,
2007 &dri2ThrottleExtension.base,
2008 &dri2FenceExtension.base,
2009 &dri2InteropExtension.base,
2010 &dri2Robustness.base,
2011 NULL
2012 };
2013
2014 /**
2015 * This is the driver specific part of the createNewScreen entry point.
2016 *
2017 * Returns the struct gl_config supported by this driver.
2018 */
2019 static const __DRIconfig **
2020 dri2_init_screen(__DRIscreen * sPriv)
2021 {
2022 const __DRIconfig **configs;
2023 struct dri_screen *screen;
2024 struct pipe_screen *pscreen = NULL;
2025 const struct drm_conf_ret *throttle_ret;
2026 const struct drm_conf_ret *dmabuf_ret;
2027 int fd;
2028
2029 screen = CALLOC_STRUCT(dri_screen);
2030 if (!screen)
2031 return NULL;
2032
2033 screen->sPriv = sPriv;
2034 screen->fd = sPriv->fd;
2035 (void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
2036
2037 sPriv->driverPrivate = (void *)screen;
2038
2039 if (screen->fd < 0 || (fd = fcntl(screen->fd, F_DUPFD_CLOEXEC, 3)) < 0)
2040 goto free_screen;
2041
2042
2043 if (pipe_loader_drm_probe_fd(&screen->dev, fd)) {
2044 unsigned flags =
2045 dri_init_options_get_screen_flags(screen, screen->dev->driver_name);
2046
2047 pscreen = pipe_loader_create_screen(screen->dev, flags);
2048 }
2049
2050 if (!pscreen)
2051 goto release_pipe;
2052
2053 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
2054 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
2055
2056 if (throttle_ret && throttle_ret->val.val_int != -1) {
2057 screen->throttling_enabled = TRUE;
2058 screen->default_throttle_frames = throttle_ret->val.val_int;
2059 }
2060
2061 if (pscreen->resource_create_with_modifiers)
2062 dri2ImageExtension.createImageWithModifiers =
2063 dri2_create_image_with_modifiers;
2064
2065 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
2066 uint64_t cap;
2067
2068 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2069 (cap & DRM_PRIME_CAP_IMPORT)) {
2070 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2071 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2072 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2073 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2074 dri2ImageExtension.queryDmaBufModifiers =
2075 dri2_query_dma_buf_modifiers;
2076 }
2077 }
2078
2079 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
2080 sPriv->extensions = dri_robust_screen_extensions;
2081 screen->has_reset_status_query = true;
2082 }
2083 else
2084 sPriv->extensions = dri_screen_extensions;
2085
2086 configs = dri_init_screen_helper(screen, pscreen);
2087 if (!configs)
2088 goto destroy_screen;
2089
2090 screen->can_share_buffer = true;
2091 screen->auto_fake_front = dri_with_format(sPriv);
2092 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2093 screen->lookup_egl_image = dri2_lookup_egl_image;
2094
2095 return configs;
2096
2097 destroy_screen:
2098 dri_destroy_screen_helper(screen);
2099
2100 release_pipe:
2101 if (screen->dev)
2102 pipe_loader_release(&screen->dev, 1);
2103 else
2104 close(fd);
2105
2106 free_screen:
2107 FREE(screen);
2108 return NULL;
2109 }
2110
2111 /**
2112 * This is the driver specific part of the createNewScreen entry point.
2113 *
2114 * Returns the struct gl_config supported by this driver.
2115 */
2116 static const __DRIconfig **
2117 dri_kms_init_screen(__DRIscreen * sPriv)
2118 {
2119 #if defined(GALLIUM_SOFTPIPE)
2120 const __DRIconfig **configs;
2121 struct dri_screen *screen;
2122 struct pipe_screen *pscreen = NULL;
2123 uint64_t cap;
2124 int fd;
2125
2126 screen = CALLOC_STRUCT(dri_screen);
2127 if (!screen)
2128 return NULL;
2129
2130 screen->sPriv = sPriv;
2131 screen->fd = sPriv->fd;
2132
2133 sPriv->driverPrivate = (void *)screen;
2134
2135 if (screen->fd < 0 || (fd = fcntl(screen->fd, F_DUPFD_CLOEXEC, 3)) < 0)
2136 goto free_screen;
2137
2138 unsigned flags = dri_init_options_get_screen_flags(screen, "swrast");
2139
2140 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
2141 pscreen = pipe_loader_create_screen(screen->dev, flags);
2142
2143 if (!pscreen)
2144 goto release_pipe;
2145
2146 if (pscreen->resource_create_with_modifiers)
2147 dri2ImageExtension.createImageWithModifiers =
2148 dri2_create_image_with_modifiers;
2149
2150 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2151 (cap & DRM_PRIME_CAP_IMPORT)) {
2152 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2153 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2154 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2155 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2156 dri2ImageExtension.queryDmaBufModifiers = dri2_query_dma_buf_modifiers;
2157 }
2158
2159 sPriv->extensions = dri_screen_extensions;
2160
2161 configs = dri_init_screen_helper(screen, pscreen);
2162 if (!configs)
2163 goto destroy_screen;
2164
2165 screen->can_share_buffer = false;
2166 screen->auto_fake_front = dri_with_format(sPriv);
2167 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2168 screen->lookup_egl_image = dri2_lookup_egl_image;
2169
2170 return configs;
2171
2172 destroy_screen:
2173 dri_destroy_screen_helper(screen);
2174
2175 release_pipe:
2176 if (screen->dev)
2177 pipe_loader_release(&screen->dev, 1);
2178 else
2179 close(fd);
2180
2181 free_screen:
2182 FREE(screen);
2183 #endif // GALLIUM_SOFTPIPE
2184 return NULL;
2185 }
2186
2187 static boolean
2188 dri2_create_buffer(__DRIscreen * sPriv,
2189 __DRIdrawable * dPriv,
2190 const struct gl_config * visual, boolean isPixmap)
2191 {
2192 struct dri_drawable *drawable = NULL;
2193
2194 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
2195 return FALSE;
2196
2197 drawable = dPriv->driverPrivate;
2198
2199 drawable->allocate_textures = dri2_allocate_textures;
2200 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
2201 drawable->update_tex_buffer = dri2_update_tex_buffer;
2202
2203 return TRUE;
2204 }
2205
2206 /**
2207 * DRI driver virtual function table.
2208 *
2209 * DRI versions differ in their implementation of init_screen and swap_buffers.
2210 */
2211 const struct __DriverAPIRec galliumdrm_driver_api = {
2212 .InitScreen = dri2_init_screen,
2213 .DestroyScreen = dri_destroy_screen,
2214 .CreateContext = dri_create_context,
2215 .DestroyContext = dri_destroy_context,
2216 .CreateBuffer = dri2_create_buffer,
2217 .DestroyBuffer = dri_destroy_buffer,
2218 .MakeCurrent = dri_make_current,
2219 .UnbindContext = dri_unbind_context,
2220
2221 .AllocateBuffer = dri2_allocate_buffer,
2222 .ReleaseBuffer = dri2_release_buffer,
2223 };
2224
2225 /**
2226 * DRI driver virtual function table.
2227 *
2228 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2229 * hook. The latter is used to explicitly initialise the kms_swrast driver
2230 * rather than selecting the approapriate driver as suggested by the loader.
2231 */
2232 const struct __DriverAPIRec dri_kms_driver_api = {
2233 .InitScreen = dri_kms_init_screen,
2234 .DestroyScreen = dri_destroy_screen,
2235 .CreateContext = dri_create_context,
2236 .DestroyContext = dri_destroy_context,
2237 .CreateBuffer = dri2_create_buffer,
2238 .DestroyBuffer = dri_destroy_buffer,
2239 .MakeCurrent = dri_make_current,
2240 .UnbindContext = dri_unbind_context,
2241
2242 .AllocateBuffer = dri2_allocate_buffer,
2243 .ReleaseBuffer = dri2_release_buffer,
2244 };
2245
2246 /* This is the table of extensions that the loader will dlsym() for. */
2247 const __DRIextension *galliumdrm_driver_extensions[] = {
2248 &driCoreExtension.base,
2249 &driImageDriverExtension.base,
2250 &driDRI2Extension.base,
2251 &gallium_config_options.base,
2252 &dri2FenceExtension.base,
2253 NULL
2254 };
2255
2256 /* vim: set sw=3 ts=8 sts=3 expandtab: */