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