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