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