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