2 * Mesa 3-D graphics library
4 * Copyright 2009, VMware, Inc.
6 * Copyright (C) 2010 LunarG Inc.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
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"
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"
54 static int convert_fourcc(int format
, int *dri_components_p
)
58 case __DRI_IMAGE_FOURCC_RGB565
:
59 format
= __DRI_IMAGE_FORMAT_RGB565
;
60 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
62 case __DRI_IMAGE_FOURCC_ARGB8888
:
63 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
64 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
66 case __DRI_IMAGE_FOURCC_XRGB8888
:
67 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
68 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
70 case __DRI_IMAGE_FOURCC_ABGR8888
:
71 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
72 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
74 case __DRI_IMAGE_FOURCC_XBGR8888
:
75 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
76 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
78 case __DRI_IMAGE_FOURCC_R8
:
79 format
= __DRI_IMAGE_FORMAT_R8
;
80 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
82 case __DRI_IMAGE_FOURCC_GR88
:
83 format
= __DRI_IMAGE_FORMAT_GR88
;
84 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
89 *dri_components_p
= dri_components
;
93 static int convert_to_fourcc(int format
)
96 case __DRI_IMAGE_FORMAT_RGB565
:
97 format
= __DRI_IMAGE_FOURCC_RGB565
;
99 case __DRI_IMAGE_FORMAT_ARGB8888
:
100 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
102 case __DRI_IMAGE_FORMAT_XRGB8888
:
103 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
105 case __DRI_IMAGE_FORMAT_ABGR8888
:
106 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
108 case __DRI_IMAGE_FORMAT_XBGR8888
:
109 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
111 case __DRI_IMAGE_FORMAT_R8
:
112 format
= __DRI_IMAGE_FOURCC_R8
;
114 case __DRI_IMAGE_FORMAT_GR88
:
115 format
= __DRI_IMAGE_FOURCC_GR88
;
123 static enum pipe_format
dri2_format_to_pipe_format (int format
)
128 case __DRI_IMAGE_FORMAT_RGB565
:
129 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
131 case __DRI_IMAGE_FORMAT_XRGB8888
:
132 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
134 case __DRI_IMAGE_FORMAT_ARGB8888
:
135 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
137 case __DRI_IMAGE_FORMAT_ABGR8888
:
138 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
140 case __DRI_IMAGE_FORMAT_R8
:
141 pf
= PIPE_FORMAT_R8_UNORM
;
143 case __DRI_IMAGE_FORMAT_GR88
:
144 pf
= PIPE_FORMAT_RG88_UNORM
;
147 pf
= PIPE_FORMAT_NONE
;
155 * DRI2 flush extension.
158 dri2_flush_drawable(__DRIdrawable
*dPriv
)
160 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
164 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
166 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
168 dri2InvalidateDrawable(dPriv
);
169 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
170 drawable
->texture_mask
= 0;
172 p_atomic_inc(&drawable
->base
.stamp
);
175 static const __DRI2flushExtension dri2FlushExtension
= {
176 .base
= { __DRI2_FLUSH
, 4 },
178 .flush
= dri2_flush_drawable
,
179 .invalidate
= dri2_invalidate_drawable
,
180 .flush_with_flags
= dri_flush
,
184 * Retrieve __DRIbuffer from the DRI loader.
187 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
188 const enum st_attachment_type
*atts
,
191 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
192 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
194 __DRIbuffer
*buffers
;
196 unsigned attachments
[10];
197 unsigned num_attachments
, i
;
200 with_format
= dri_with_format(drawable
->sPriv
);
204 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
206 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
208 for (i
= 0; i
< *count
; i
++) {
209 enum pipe_format format
;
213 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
214 if (format
== PIPE_FORMAT_NONE
)
218 case ST_ATTACHMENT_FRONT_LEFT
:
222 att
= __DRI_BUFFER_FRONT_LEFT
;
224 case ST_ATTACHMENT_BACK_LEFT
:
225 att
= __DRI_BUFFER_BACK_LEFT
;
227 case ST_ATTACHMENT_FRONT_RIGHT
:
228 att
= __DRI_BUFFER_FRONT_RIGHT
;
230 case ST_ATTACHMENT_BACK_RIGHT
:
231 att
= __DRI_BUFFER_BACK_RIGHT
;
238 * In this switch statement we must support all formats that
239 * may occur as the stvis->color_format.
242 case PIPE_FORMAT_BGRA8888_UNORM
:
245 case PIPE_FORMAT_BGRX8888_UNORM
:
248 case PIPE_FORMAT_B5G6R5_UNORM
:
252 depth
= util_format_get_blocksizebits(format
);
253 assert(!"Unexpected format in dri2_drawable_get_buffers()");
256 attachments
[num_attachments
++] = att
;
258 attachments
[num_attachments
++] = depth
;
263 num_attachments
/= 2;
264 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
265 &dri_drawable
->w
, &dri_drawable
->h
,
266 attachments
, num_attachments
,
267 &num_buffers
, dri_drawable
->loaderPrivate
);
270 buffers
= loader
->getBuffers(dri_drawable
,
271 &dri_drawable
->w
, &dri_drawable
->h
,
272 attachments
, num_attachments
,
273 &num_buffers
, dri_drawable
->loaderPrivate
);
277 *count
= num_buffers
;
283 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
284 struct __DRIimageList
*images
,
285 const enum st_attachment_type
*statts
,
286 unsigned statts_count
)
288 __DRIdrawable
*dPriv
= drawable
->dPriv
;
289 __DRIscreen
*sPriv
= drawable
->sPriv
;
290 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
292 uint32_t buffer_mask
= 0;
295 for (i
= 0; i
< statts_count
; i
++) {
296 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
297 if (pf
== PIPE_FORMAT_NONE
)
301 case ST_ATTACHMENT_FRONT_LEFT
:
302 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
304 case ST_ATTACHMENT_BACK_LEFT
:
305 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
312 case PIPE_FORMAT_B5G6R5_UNORM
:
313 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
315 case PIPE_FORMAT_BGRX8888_UNORM
:
316 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
318 case PIPE_FORMAT_BGRA8888_UNORM
:
319 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
321 case PIPE_FORMAT_RGBA8888_UNORM
:
322 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
325 image_format
= __DRI_IMAGE_FORMAT_NONE
;
330 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
331 (uint32_t *) &drawable
->base
.stamp
,
332 dPriv
->loaderPrivate
, buffer_mask
,
337 dri2_allocate_buffer(__DRIscreen
*sPriv
,
338 unsigned attachment
, unsigned format
,
339 int width
, int height
)
341 struct dri_screen
*screen
= dri_screen(sPriv
);
342 struct dri2_buffer
*buffer
;
343 struct pipe_resource templ
;
346 struct winsys_handle whandle
;
348 switch (attachment
) {
349 case __DRI_BUFFER_FRONT_LEFT
:
350 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
351 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
353 case __DRI_BUFFER_BACK_LEFT
:
354 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
356 case __DRI_BUFFER_DEPTH
:
357 case __DRI_BUFFER_DEPTH_STENCIL
:
358 case __DRI_BUFFER_STENCIL
:
359 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
363 /* because we get the handle and stride */
364 bind
|= PIPE_BIND_SHARED
;
368 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
371 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
374 pf
= PIPE_FORMAT_Z16_UNORM
;
380 buffer
= CALLOC_STRUCT(dri2_buffer
);
384 memset(&templ
, 0, sizeof(templ
));
387 templ
.target
= PIPE_TEXTURE_2D
;
388 templ
.last_level
= 0;
389 templ
.width0
= width
;
390 templ
.height0
= height
;
392 templ
.array_size
= 1;
395 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
396 if (!buffer
->resource
) {
401 memset(&whandle
, 0, sizeof(whandle
));
402 if (screen
->can_share_buffer
)
403 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
405 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
407 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
,
408 buffer
->resource
, &whandle
,
409 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
411 buffer
->base
.attachment
= attachment
;
412 buffer
->base
.name
= whandle
.handle
;
413 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
414 buffer
->base
.pitch
= whandle
.stride
;
416 return &buffer
->base
;
420 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
422 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
424 pipe_resource_reference(&buffer
->resource
, NULL
);
429 * Backend functions for st_framebuffer interface.
433 dri2_allocate_textures(struct dri_context
*ctx
,
434 struct dri_drawable
*drawable
,
435 const enum st_attachment_type
*statts
,
436 unsigned statts_count
)
438 __DRIscreen
*sPriv
= drawable
->sPriv
;
439 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
440 struct dri_screen
*screen
= dri_screen(sPriv
);
441 struct pipe_resource templ
;
442 boolean alloc_depthstencil
= FALSE
;
444 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
445 /* Image specific variables */
446 struct __DRIimageList images
;
447 /* Dri2 specific variables */
448 __DRIbuffer
*buffers
= NULL
;
449 struct winsys_handle whandle
;
450 unsigned num_buffers
= statts_count
;
452 /* First get the buffers from the loader */
454 if (!dri_image_drawable_get_buffers(drawable
, &images
,
455 statts
, statts_count
))
459 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
460 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
461 drawable
->old_w
== dri_drawable
->w
&&
462 drawable
->old_h
== dri_drawable
->h
&&
463 memcmp(drawable
->old
, buffers
,
464 sizeof(__DRIbuffer
) * num_buffers
) == 0))
468 /* Second clean useless resources*/
470 /* See if we need a depth-stencil buffer. */
471 for (i
= 0; i
< statts_count
; i
++) {
472 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
473 alloc_depthstencil
= TRUE
;
478 /* Delete the resources we won't need. */
479 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
480 /* Don't delete the depth-stencil buffer, we can reuse it. */
481 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
484 /* Flush the texture before unreferencing, so that other clients can
485 * see what the driver has rendered.
487 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
488 struct pipe_context
*pipe
= ctx
->st
->pipe
;
489 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
492 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
495 if (drawable
->stvis
.samples
> 1) {
496 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
499 /* Don't delete MSAA resources for the attachments which are enabled,
500 * we can reuse them. */
501 for (j
= 0; j
< statts_count
; j
++) {
502 if (i
== statts
[j
]) {
509 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
514 /* Third use the buffers retrieved to fill the drawable info */
516 memset(&templ
, 0, sizeof(templ
));
517 templ
.target
= screen
->target
;
518 templ
.last_level
= 0;
520 templ
.array_size
= 1;
523 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
524 struct pipe_resource
**buf
=
525 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
526 struct pipe_resource
*texture
= images
.front
->texture
;
528 dri_drawable
->w
= texture
->width0
;
529 dri_drawable
->h
= texture
->height0
;
531 pipe_resource_reference(buf
, texture
);
534 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
535 struct pipe_resource
**buf
=
536 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
537 struct pipe_resource
*texture
= images
.back
->texture
;
539 dri_drawable
->w
= texture
->width0
;
540 dri_drawable
->h
= texture
->height0
;
542 pipe_resource_reference(buf
, texture
);
545 /* Note: if there is both a back and a front buffer,
546 * then they have the same size.
548 templ
.width0
= dri_drawable
->w
;
549 templ
.height0
= dri_drawable
->h
;
552 memset(&whandle
, 0, sizeof(whandle
));
554 /* Process DRI-provided buffers and get pipe_resources. */
555 for (i
= 0; i
< num_buffers
; i
++) {
556 __DRIbuffer
*buf
= &buffers
[i
];
557 enum st_attachment_type statt
;
558 enum pipe_format format
;
560 switch (buf
->attachment
) {
561 case __DRI_BUFFER_FRONT_LEFT
:
562 if (!screen
->auto_fake_front
) {
563 continue; /* invalid attachment */
566 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
567 statt
= ST_ATTACHMENT_FRONT_LEFT
;
569 case __DRI_BUFFER_BACK_LEFT
:
570 statt
= ST_ATTACHMENT_BACK_LEFT
;
573 continue; /* invalid attachment */
576 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
577 if (format
== PIPE_FORMAT_NONE
)
580 /* dri2_drawable_get_buffers has already filled dri_drawable->w
581 * and dri_drawable->h */
582 templ
.width0
= dri_drawable
->w
;
583 templ
.height0
= dri_drawable
->h
;
584 templ
.format
= format
;
586 whandle
.handle
= buf
->name
;
587 whandle
.stride
= buf
->pitch
;
589 if (screen
->can_share_buffer
)
590 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
592 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
593 drawable
->textures
[statt
] =
594 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
596 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
597 assert(drawable
->textures
[statt
]);
601 /* Allocate private MSAA colorbuffers. */
602 if (drawable
->stvis
.samples
> 1) {
603 for (i
= 0; i
< statts_count
; i
++) {
604 enum st_attachment_type statt
= statts
[i
];
606 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
609 if (drawable
->textures
[statt
]) {
610 templ
.format
= drawable
->textures
[statt
]->format
;
611 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
612 templ
.nr_samples
= drawable
->stvis
.samples
;
614 /* Try to reuse the resource.
615 * (the other resource parameters should be constant)
617 if (!drawable
->msaa_textures
[statt
] ||
618 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
619 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
620 /* Allocate a new one. */
621 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
623 drawable
->msaa_textures
[statt
] =
624 screen
->base
.screen
->resource_create(screen
->base
.screen
,
626 assert(drawable
->msaa_textures
[statt
]);
628 /* If there are any MSAA resources, we should initialize them
629 * such that they contain the same data as the single-sample
630 * resources we just got from the X server.
632 * The reason for this is that the state tracker (and
633 * therefore the app) can access the MSAA resources only.
634 * The single-sample resources are not exposed
635 * to the state tracker.
638 dri_pipe_blit(ctx
->st
->pipe
,
639 drawable
->msaa_textures
[statt
],
640 drawable
->textures
[statt
]);
644 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
649 /* Allocate a private depth-stencil buffer. */
650 if (alloc_depthstencil
) {
651 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
652 struct pipe_resource
**zsbuf
;
653 enum pipe_format format
;
656 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
659 templ
.format
= format
;
662 if (drawable
->stvis
.samples
> 1) {
663 templ
.nr_samples
= drawable
->stvis
.samples
;
664 zsbuf
= &drawable
->msaa_textures
[statt
];
667 templ
.nr_samples
= 0;
668 zsbuf
= &drawable
->textures
[statt
];
671 /* Try to reuse the resource.
672 * (the other resource parameters should be constant)
675 (*zsbuf
)->width0
!= templ
.width0
||
676 (*zsbuf
)->height0
!= templ
.height0
) {
677 /* Allocate a new one. */
678 pipe_resource_reference(zsbuf
, NULL
);
679 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
685 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
686 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
690 /* For DRI2, we may get the same buffers again from the server.
691 * To prevent useless imports of gem names, drawable->old* is used
692 * to bypass the import if we get the same buffers. This doesn't apply
693 * to DRI3/Wayland, users of image.loader, since the buffer is managed
694 * by the client (no import), and the back buffer is going to change
698 drawable
->old_num
= num_buffers
;
699 drawable
->old_w
= dri_drawable
->w
;
700 drawable
->old_h
= dri_drawable
->h
;
701 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
706 dri2_flush_frontbuffer(struct dri_context
*ctx
,
707 struct dri_drawable
*drawable
,
708 enum st_attachment_type statt
)
710 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
711 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
712 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
713 struct pipe_context
*pipe
= ctx
->st
->pipe
;
715 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
718 if (drawable
->stvis
.samples
> 1) {
719 /* Resolve the front buffer. */
720 dri_pipe_blit(ctx
->st
->pipe
,
721 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
722 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
725 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
726 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
729 pipe
->flush(pipe
, NULL
, 0);
732 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
734 else if (loader
->flushFrontBuffer
) {
735 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
740 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
741 struct dri_context
*ctx
,
742 struct pipe_resource
*res
)
748 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
750 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
753 if (!loader
->lookupEGLImage
)
756 img
= loader
->lookupEGLImage(screen
->sPriv
,
757 handle
, screen
->sPriv
->loaderPrivate
);
763 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
764 int width
, int height
, int format
,
765 struct winsys_handle
*whandle
,
768 struct dri_screen
*screen
= dri_screen(_screen
);
770 struct pipe_resource templ
;
774 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
776 pf
= dri2_format_to_pipe_format (format
);
777 if (pf
== PIPE_FORMAT_NONE
)
780 img
= CALLOC_STRUCT(__DRIimageRec
);
784 memset(&templ
, 0, sizeof(templ
));
785 templ
.bind
= tex_usage
;
787 templ
.target
= screen
->target
;
788 templ
.last_level
= 0;
789 templ
.width0
= width
;
790 templ
.height0
= height
;
792 templ
.array_size
= 1;
794 img
->texture
= screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
795 &templ
, whandle
, PIPE_HANDLE_USAGE_READ_WRITE
);
803 img
->dri_format
= format
;
805 img
->loader_private
= loaderPrivate
;
811 dri2_create_image_from_name(__DRIscreen
*_screen
,
812 int width
, int height
, int format
,
813 int name
, int pitch
, void *loaderPrivate
)
815 struct winsys_handle whandle
;
818 memset(&whandle
, 0, sizeof(whandle
));
819 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
820 whandle
.handle
= name
;
822 pf
= dri2_format_to_pipe_format (format
);
823 if (pf
== PIPE_FORMAT_NONE
)
826 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
828 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
829 &whandle
, loaderPrivate
);
833 dri2_create_image_from_fd(__DRIscreen
*_screen
,
834 int width
, int height
, int fourcc
,
835 int *fds
, int num_fds
, int *strides
,
836 int *offsets
, unsigned *error
,
837 int *dri_components
, void *loaderPrivate
)
839 struct winsys_handle whandle
;
841 __DRIimage
*img
= NULL
;
842 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
845 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
849 format
= convert_fourcc(fourcc
, dri_components
);
851 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
856 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
860 memset(&whandle
, 0, sizeof(whandle
));
861 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
862 whandle
.handle
= (unsigned)fds
[0];
863 whandle
.stride
= (unsigned)strides
[0];
864 whandle
.offset
= (unsigned)offsets
[0];
866 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
867 &whandle
, loaderPrivate
);
869 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
879 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
880 int renderbuffer
, void *loaderPrivate
)
882 struct dri_context
*ctx
= dri_context(context
);
884 if (!ctx
->st
->get_resource_for_egl_image
)
892 dri2_create_image(__DRIscreen
*_screen
,
893 int width
, int height
, int format
,
894 unsigned int use
, void *loaderPrivate
)
896 struct dri_screen
*screen
= dri_screen(_screen
);
898 struct pipe_resource templ
;
902 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
903 if (use
& __DRI_IMAGE_USE_SCANOUT
)
904 tex_usage
|= PIPE_BIND_SCANOUT
;
905 if (use
& __DRI_IMAGE_USE_SHARE
)
906 tex_usage
|= PIPE_BIND_SHARED
;
907 if (use
& __DRI_IMAGE_USE_LINEAR
)
908 tex_usage
|= PIPE_BIND_LINEAR
;
909 if (use
& __DRI_IMAGE_USE_CURSOR
) {
910 if (width
!= 64 || height
!= 64)
912 tex_usage
|= PIPE_BIND_CURSOR
;
915 pf
= dri2_format_to_pipe_format (format
);
916 if (pf
== PIPE_FORMAT_NONE
)
919 img
= CALLOC_STRUCT(__DRIimageRec
);
923 memset(&templ
, 0, sizeof(templ
));
924 templ
.bind
= tex_usage
;
926 templ
.target
= PIPE_TEXTURE_2D
;
927 templ
.last_level
= 0;
928 templ
.width0
= width
;
929 templ
.height0
= height
;
931 templ
.array_size
= 1;
933 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
941 img
->dri_format
= format
;
942 img
->dri_components
= 0;
945 img
->loader_private
= loaderPrivate
;
950 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
952 struct winsys_handle whandle
;
955 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
956 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
958 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
960 memset(&whandle
, 0, sizeof(whandle
));
963 case __DRI_IMAGE_ATTRIB_STRIDE
:
964 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
965 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
966 image
->texture
, &whandle
, usage
);
967 *value
= whandle
.stride
;
969 case __DRI_IMAGE_ATTRIB_HANDLE
:
970 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
971 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
972 image
->texture
, &whandle
, usage
);
973 *value
= whandle
.handle
;
975 case __DRI_IMAGE_ATTRIB_NAME
:
976 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
977 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
978 image
->texture
, &whandle
, usage
);
979 *value
= whandle
.handle
;
981 case __DRI_IMAGE_ATTRIB_FD
:
982 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
983 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
984 image
->texture
, &whandle
, usage
);
985 *value
= whandle
.handle
;
987 case __DRI_IMAGE_ATTRIB_FORMAT
:
988 *value
= image
->dri_format
;
990 case __DRI_IMAGE_ATTRIB_WIDTH
:
991 *value
= image
->texture
->width0
;
993 case __DRI_IMAGE_ATTRIB_HEIGHT
:
994 *value
= image
->texture
->height0
;
996 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
997 if (image
->dri_components
== 0)
999 *value
= image
->dri_components
;
1001 case __DRI_IMAGE_ATTRIB_FOURCC
:
1002 *value
= convert_to_fourcc(image
->dri_format
);
1004 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1013 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1017 img
= CALLOC_STRUCT(__DRIimageRec
);
1021 img
->texture
= NULL
;
1022 pipe_resource_reference(&img
->texture
, image
->texture
);
1023 img
->level
= image
->level
;
1024 img
->layer
= image
->layer
;
1025 img
->dri_format
= image
->dri_format
;
1026 /* This should be 0 for sub images, but dup is also used for base images. */
1027 img
->dri_components
= image
->dri_components
;
1028 img
->loader_private
= loaderPrivate
;
1034 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1037 * Gallium drivers are bad at adding usages to the resources
1038 * once opened again in another process, which is the main use
1039 * case for this, so we have to lie.
1048 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1049 int *names
, int num_names
, int *strides
, int *offsets
,
1050 void *loaderPrivate
)
1054 struct winsys_handle whandle
;
1059 format
= convert_fourcc(format
, &dri_components
);
1063 memset(&whandle
, 0, sizeof(whandle
));
1064 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1065 whandle
.handle
= names
[0];
1066 whandle
.stride
= strides
[0];
1067 whandle
.offset
= offsets
[0];
1069 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1070 &whandle
, loaderPrivate
);
1074 img
->dri_components
= dri_components
;
1079 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1086 if (image
->dri_components
== 0)
1089 img
= dri2_dup_image(image
, loaderPrivate
);
1093 /* set this to 0 for sub images. */
1094 img
->dri_components
= 0;
1099 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1100 int depth
, int level
, unsigned *error
,
1101 void *loaderPrivate
)
1104 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1105 struct gl_texture_object
*obj
;
1106 struct pipe_resource
*tex
;
1109 obj
= _mesa_lookup_texture(ctx
, texture
);
1110 if (!obj
|| obj
->Target
!= target
) {
1111 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1115 tex
= st_get_texobj_resource(obj
);
1117 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1121 if (target
== GL_TEXTURE_CUBE_MAP
)
1124 _mesa_test_texobj_completeness(ctx
, obj
);
1125 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1126 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1130 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1131 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1135 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1136 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1140 img
= CALLOC_STRUCT(__DRIimageRec
);
1142 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1148 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1150 img
->loader_private
= loaderPrivate
;
1152 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1153 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1158 pipe_resource_reference(&img
->texture
, tex
);
1160 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1165 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1166 int *fds
, int num_fds
, int *strides
, int *offsets
,
1167 void *loaderPrivate
)
1172 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1173 fds
, num_fds
, strides
, offsets
, NULL
,
1174 &dri_components
, loaderPrivate
);
1178 img
->dri_components
= dri_components
;
1183 dri2_from_dma_bufs(__DRIscreen
*screen
,
1184 int width
, int height
, int fourcc
,
1185 int *fds
, int num_fds
,
1186 int *strides
, int *offsets
,
1187 enum __DRIYUVColorSpace yuv_color_space
,
1188 enum __DRISampleRange sample_range
,
1189 enum __DRIChromaSiting horizontal_siting
,
1190 enum __DRIChromaSiting vertical_siting
,
1192 void *loaderPrivate
)
1197 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1198 fds
, num_fds
, strides
, offsets
, error
,
1199 &dri_components
, loaderPrivate
);
1203 img
->yuv_color_space
= yuv_color_space
;
1204 img
->sample_range
= sample_range
;
1205 img
->horizontal_siting
= horizontal_siting
;
1206 img
->vertical_siting
= vertical_siting
;
1207 img
->dri_components
= dri_components
;
1209 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1214 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1215 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1216 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1219 struct dri_context
*ctx
= dri_context(context
);
1220 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1221 struct pipe_screen
*screen
;
1222 struct pipe_fence_handle
*fence
;
1223 struct pipe_blit_info blit
;
1228 memset(&blit
, 0, sizeof(blit
));
1229 blit
.dst
.resource
= dst
->texture
;
1230 blit
.dst
.box
.x
= dstx0
;
1231 blit
.dst
.box
.y
= dsty0
;
1232 blit
.dst
.box
.width
= dstwidth
;
1233 blit
.dst
.box
.height
= dstheight
;
1234 blit
.dst
.box
.depth
= 1;
1235 blit
.dst
.format
= dst
->texture
->format
;
1236 blit
.src
.resource
= src
->texture
;
1237 blit
.src
.box
.x
= srcx0
;
1238 blit
.src
.box
.y
= srcy0
;
1239 blit
.src
.box
.width
= srcwidth
;
1240 blit
.src
.box
.height
= srcheight
;
1241 blit
.src
.box
.depth
= 1;
1242 blit
.src
.format
= src
->texture
->format
;
1243 blit
.mask
= PIPE_MASK_RGBA
;
1244 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1246 pipe
->blit(pipe
, &blit
);
1248 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1249 pipe
->flush_resource(pipe
, dst
->texture
);
1250 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1251 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1252 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1253 pipe
->flush_resource(pipe
, dst
->texture
);
1254 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1255 (void) screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
1256 screen
->fence_reference(screen
, &fence
, NULL
);
1261 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1262 int x0
, int y0
, int width
, int height
,
1263 unsigned int flags
, int *stride
, void **data
)
1265 struct dri_context
*ctx
= dri_context(context
);
1266 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1267 enum pipe_transfer_usage pipe_access
= 0;
1268 struct pipe_transfer
*trans
;
1271 if (!image
|| !data
|| *data
)
1274 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1275 pipe_access
|= PIPE_TRANSFER_READ
;
1276 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1277 pipe_access
|= PIPE_TRANSFER_WRITE
;
1279 map
= pipe_transfer_map(pipe
, image
->texture
,
1280 0, 0, pipe_access
, x0
, y0
, width
, height
,
1284 *stride
= trans
->stride
;
1291 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1293 struct dri_context
*ctx
= dri_context(context
);
1294 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1296 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1300 dri2_destroy_image(__DRIimage
*img
)
1302 pipe_resource_reference(&img
->texture
, NULL
);
1307 dri2_get_capabilities(__DRIscreen
*_screen
)
1309 struct dri_screen
*screen
= dri_screen(_screen
);
1311 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1314 /* The extension is modified during runtime if DRI_PRIME is detected */
1315 static __DRIimageExtension dri2ImageExtension
= {
1316 .base
= { __DRI_IMAGE
, 12 },
1318 .createImageFromName
= dri2_create_image_from_name
,
1319 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1320 .destroyImage
= dri2_destroy_image
,
1321 .createImage
= dri2_create_image
,
1322 .queryImage
= dri2_query_image
,
1323 .dupImage
= dri2_dup_image
,
1324 .validateUsage
= dri2_validate_usage
,
1325 .createImageFromNames
= dri2_from_names
,
1326 .fromPlanar
= dri2_from_planar
,
1327 .createImageFromTexture
= dri2_create_from_texture
,
1328 .createImageFromFds
= NULL
,
1329 .createImageFromDmaBufs
= NULL
,
1330 .blitImage
= dri2_blit_image
,
1331 .getCapabilities
= dri2_get_capabilities
,
1332 .mapImage
= dri2_map_image
,
1333 .unmapImage
= dri2_unmap_image
,
1338 dri2_is_opencl_interop_loaded_locked(struct dri_screen
*screen
)
1340 return screen
->opencl_dri_event_add_ref
&&
1341 screen
->opencl_dri_event_release
&&
1342 screen
->opencl_dri_event_wait
&&
1343 screen
->opencl_dri_event_get_fence
;
1347 dri2_load_opencl_interop(struct dri_screen
*screen
)
1349 #if defined(RTLD_DEFAULT)
1352 pipe_mutex_lock(screen
->opencl_func_mutex
);
1354 if (dri2_is_opencl_interop_loaded_locked(screen
)) {
1355 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1359 screen
->opencl_dri_event_add_ref
=
1360 dlsym(RTLD_DEFAULT
, "opencl_dri_event_add_ref");
1361 screen
->opencl_dri_event_release
=
1362 dlsym(RTLD_DEFAULT
, "opencl_dri_event_release");
1363 screen
->opencl_dri_event_wait
=
1364 dlsym(RTLD_DEFAULT
, "opencl_dri_event_wait");
1365 screen
->opencl_dri_event_get_fence
=
1366 dlsym(RTLD_DEFAULT
, "opencl_dri_event_get_fence");
1368 success
= dri2_is_opencl_interop_loaded_locked(screen
);
1369 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1377 struct dri_screen
*driscreen
;
1378 struct pipe_fence_handle
*pipe_fence
;
1383 dri2_create_fence(__DRIcontext
*_ctx
)
1385 struct pipe_context
*ctx
= dri_context(_ctx
)->st
->pipe
;
1386 struct dri2_fence
*fence
= CALLOC_STRUCT(dri2_fence
);
1391 ctx
->flush(ctx
, &fence
->pipe_fence
, 0);
1393 if (!fence
->pipe_fence
) {
1398 fence
->driscreen
= dri_screen(_ctx
->driScreenPriv
);
1403 dri2_get_fence_from_cl_event(__DRIscreen
*_screen
, intptr_t cl_event
)
1405 struct dri_screen
*driscreen
= dri_screen(_screen
);
1406 struct dri2_fence
*fence
;
1408 if (!dri2_load_opencl_interop(driscreen
))
1411 fence
= CALLOC_STRUCT(dri2_fence
);
1415 fence
->cl_event
= (void*)cl_event
;
1417 if (!driscreen
->opencl_dri_event_add_ref(fence
->cl_event
)) {
1422 fence
->driscreen
= driscreen
;
1427 dri2_destroy_fence(__DRIscreen
*_screen
, void *_fence
)
1429 struct dri_screen
*driscreen
= dri_screen(_screen
);
1430 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1431 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1433 if (fence
->pipe_fence
)
1434 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
1435 else if (fence
->cl_event
)
1436 driscreen
->opencl_dri_event_release(fence
->cl_event
);
1444 dri2_client_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
,
1447 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1448 struct dri_screen
*driscreen
= fence
->driscreen
;
1449 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1451 /* No need to flush. The context was flushed when the fence was created. */
1453 if (fence
->pipe_fence
)
1454 return screen
->fence_finish(screen
, fence
->pipe_fence
, timeout
);
1455 else if (fence
->cl_event
) {
1456 struct pipe_fence_handle
*pipe_fence
=
1457 driscreen
->opencl_dri_event_get_fence(fence
->cl_event
);
1460 return screen
->fence_finish(screen
, pipe_fence
, timeout
);
1462 return driscreen
->opencl_dri_event_wait(fence
->cl_event
, timeout
);
1471 dri2_server_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
)
1473 /* AFAIK, no driver currently supports parallel context execution. */
1476 static __DRI2fenceExtension dri2FenceExtension
= {
1477 .base
= { __DRI2_FENCE
, 1 },
1479 .create_fence
= dri2_create_fence
,
1480 .get_fence_from_cl_event
= dri2_get_fence_from_cl_event
,
1481 .destroy_fence
= dri2_destroy_fence
,
1482 .client_wait_sync
= dri2_client_wait_sync
,
1483 .server_wait_sync
= dri2_server_wait_sync
1486 static const __DRIrobustnessExtension dri2Robustness
= {
1487 .base
= { __DRI2_ROBUSTNESS
, 1 }
1491 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1492 struct mesa_glinterop_device_info
*out
)
1494 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1496 /* There is no version 0, thus we do not support it */
1497 if (out
->version
== 0)
1498 return MESA_GLINTEROP_INVALID_VERSION
;
1500 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1501 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1502 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1503 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1505 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1506 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1508 /* Instruct the caller that we support up-to version one of the interface */
1511 return MESA_GLINTEROP_SUCCESS
;
1515 dri2_interop_export_object(__DRIcontext
*_ctx
,
1516 struct mesa_glinterop_export_in
*in
,
1517 struct mesa_glinterop_export_out
*out
)
1519 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1520 struct pipe_screen
*screen
= st
->pipe
->screen
;
1521 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1522 struct pipe_resource
*res
= NULL
;
1523 struct winsys_handle whandle
;
1524 unsigned target
, usage
;
1527 /* There is no version 0, thus we do not support it */
1528 if (in
->version
== 0 || out
->version
== 0)
1529 return MESA_GLINTEROP_INVALID_VERSION
;
1531 /* Validate the target. */
1532 switch (in
->target
) {
1533 case GL_TEXTURE_BUFFER
:
1537 case GL_TEXTURE_RECTANGLE
:
1538 case GL_TEXTURE_1D_ARRAY
:
1539 case GL_TEXTURE_2D_ARRAY
:
1540 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1541 case GL_TEXTURE_CUBE_MAP
:
1542 case GL_TEXTURE_2D_MULTISAMPLE
:
1543 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1544 case GL_TEXTURE_EXTERNAL_OES
:
1545 case GL_RENDERBUFFER
:
1546 case GL_ARRAY_BUFFER
:
1547 target
= in
->target
;
1549 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1550 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1551 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1552 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1553 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1554 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1555 target
= GL_TEXTURE_CUBE_MAP
;
1558 return MESA_GLINTEROP_INVALID_TARGET
;
1561 /* Validate the simple case of miplevel. */
1562 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1564 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1566 /* Validate the OpenGL object and get pipe_resource. */
1567 mtx_lock(&ctx
->Shared
->Mutex
);
1569 if (target
== GL_ARRAY_BUFFER
) {
1572 * The error checking is based on the documentation of
1573 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1575 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1577 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1578 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1579 * a GL buffer object but does not have an existing data store or
1580 * the size of the buffer is 0."
1582 if (!buf
|| buf
->Size
== 0) {
1583 mtx_unlock(&ctx
->Shared
->Mutex
);
1584 return MESA_GLINTEROP_INVALID_OBJECT
;
1587 res
= st_buffer_object(buf
)->buffer
;
1589 /* this shouldn't happen */
1590 mtx_unlock(&ctx
->Shared
->Mutex
);
1591 return MESA_GLINTEROP_INVALID_OBJECT
;
1594 out
->buf_offset
= 0;
1595 out
->buf_size
= buf
->Size
;
1597 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1598 } else if (target
== GL_RENDERBUFFER
) {
1601 * The error checking is based on the documentation of
1602 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1604 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1606 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1607 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1608 * object or if the width or height of renderbuffer is zero."
1610 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1611 mtx_unlock(&ctx
->Shared
->Mutex
);
1612 return MESA_GLINTEROP_INVALID_OBJECT
;
1615 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1616 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1617 * renderbuffer object."
1619 if (rb
->NumSamples
> 1) {
1620 mtx_unlock(&ctx
->Shared
->Mutex
);
1621 return MESA_GLINTEROP_INVALID_OPERATION
;
1624 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1625 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1626 * required by the OpenCL implementation on the device."
1628 res
= st_renderbuffer(rb
)->texture
;
1630 mtx_unlock(&ctx
->Shared
->Mutex
);
1631 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1634 out
->internal_format
= rb
->InternalFormat
;
1635 out
->view_minlevel
= 0;
1636 out
->view_numlevels
= 1;
1637 out
->view_minlayer
= 0;
1638 out
->view_numlayers
= 1;
1642 * The error checking is based on the documentation of
1643 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1645 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1648 _mesa_test_texobj_completeness(ctx
, obj
);
1650 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1651 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1652 * type matches texture_target, if the specified miplevel of texture
1653 * is not defined, or if the width or height of the specified
1654 * miplevel is zero or if the GL texture object is incomplete."
1657 obj
->Target
!= target
||
1658 !obj
->_BaseComplete
||
1659 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1660 mtx_unlock(&ctx
->Shared
->Mutex
);
1661 return MESA_GLINTEROP_INVALID_OBJECT
;
1664 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1665 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1666 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1667 * implementations); or greater than the value of q (for both OpenGL
1668 * and OpenGL ES). levelbase and q are defined for the texture in
1669 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1670 * specification and section 3.7.10 of the OpenGL ES 2.0."
1672 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1673 mtx_unlock(&ctx
->Shared
->Mutex
);
1674 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1677 if (!st_finalize_texture(ctx
, st
->pipe
, obj
)) {
1678 mtx_unlock(&ctx
->Shared
->Mutex
);
1679 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1682 res
= st_get_texobj_resource(obj
);
1684 /* Incomplete texture buffer object? This shouldn't really occur. */
1685 mtx_unlock(&ctx
->Shared
->Mutex
);
1686 return MESA_GLINTEROP_INVALID_OBJECT
;
1689 if (target
== GL_TEXTURE_BUFFER
) {
1690 out
->internal_format
= obj
->BufferObjectFormat
;
1691 out
->buf_offset
= obj
->BufferOffset
;
1692 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1695 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1697 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1698 out
->view_minlevel
= obj
->MinLevel
;
1699 out
->view_numlevels
= obj
->NumLevels
;
1700 out
->view_minlayer
= obj
->MinLayer
;
1701 out
->view_numlayers
= obj
->NumLayers
;
1705 /* Get the handle. */
1706 switch (in
->access
) {
1707 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1708 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1710 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1711 usage
= PIPE_HANDLE_USAGE_READ
;
1713 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1714 usage
= PIPE_HANDLE_USAGE_WRITE
;
1720 memset(&whandle
, 0, sizeof(whandle
));
1721 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1723 success
= screen
->resource_get_handle(screen
, res
, &whandle
, usage
);
1724 mtx_unlock(&ctx
->Shared
->Mutex
);
1727 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1729 out
->dmabuf_fd
= whandle
.handle
;
1730 out
->out_driver_data_written
= 0;
1732 if (res
->target
== PIPE_BUFFER
)
1733 out
->buf_offset
+= whandle
.offset
;
1735 /* Instruct the caller that we support up-to version one of the interface */
1739 return MESA_GLINTEROP_SUCCESS
;
1742 static const __DRI2interopExtension dri2InteropExtension
= {
1743 .base
= { __DRI2_INTEROP
, 1 },
1744 .query_device_info
= dri2_interop_query_device_info
,
1745 .export_object
= dri2_interop_export_object
1749 * Backend function init_screen.
1752 static const __DRIextension
*dri_screen_extensions
[] = {
1753 &driTexBufferExtension
.base
,
1754 &dri2FlushExtension
.base
,
1755 &dri2ImageExtension
.base
,
1756 &dri2RendererQueryExtension
.base
,
1757 &dri2ConfigQueryExtension
.base
,
1758 &dri2ThrottleExtension
.base
,
1759 &dri2FenceExtension
.base
,
1760 &dri2InteropExtension
.base
,
1764 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1765 &driTexBufferExtension
.base
,
1766 &dri2FlushExtension
.base
,
1767 &dri2ImageExtension
.base
,
1768 &dri2RendererQueryExtension
.base
,
1769 &dri2ConfigQueryExtension
.base
,
1770 &dri2ThrottleExtension
.base
,
1771 &dri2FenceExtension
.base
,
1772 &dri2InteropExtension
.base
,
1773 &dri2Robustness
.base
,
1778 * This is the driver specific part of the createNewScreen entry point.
1780 * Returns the struct gl_config supported by this driver.
1782 static const __DRIconfig
**
1783 dri2_init_screen(__DRIscreen
* sPriv
)
1785 const __DRIconfig
**configs
;
1786 struct dri_screen
*screen
;
1787 struct pipe_screen
*pscreen
= NULL
;
1788 const struct drm_conf_ret
*throttle_ret
;
1789 const struct drm_conf_ret
*dmabuf_ret
;
1792 screen
= CALLOC_STRUCT(dri_screen
);
1796 screen
->sPriv
= sPriv
;
1797 screen
->fd
= sPriv
->fd
;
1798 pipe_mutex_init(screen
->opencl_func_mutex
);
1800 sPriv
->driverPrivate
= (void *)screen
;
1802 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1805 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1806 pscreen
= pipe_loader_create_screen(screen
->dev
);
1811 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1812 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1814 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1815 screen
->throttling_enabled
= TRUE
;
1816 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1819 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
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
;
1829 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1830 sPriv
->extensions
= dri_robust_screen_extensions
;
1831 screen
->has_reset_status_query
= true;
1834 sPriv
->extensions
= dri_screen_extensions
;
1836 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1838 goto destroy_screen
;
1840 screen
->can_share_buffer
= true;
1841 screen
->auto_fake_front
= dri_with_format(sPriv
);
1842 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1843 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1848 dri_destroy_screen_helper(screen
);
1852 pipe_loader_release(&screen
->dev
, 1);
1862 * This is the driver specific part of the createNewScreen entry point.
1864 * Returns the struct gl_config supported by this driver.
1866 static const __DRIconfig
**
1867 dri_kms_init_screen(__DRIscreen
* sPriv
)
1869 #if defined(GALLIUM_SOFTPIPE)
1870 const __DRIconfig
**configs
;
1871 struct dri_screen
*screen
;
1872 struct pipe_screen
*pscreen
= NULL
;
1876 screen
= CALLOC_STRUCT(dri_screen
);
1880 screen
->sPriv
= sPriv
;
1881 screen
->fd
= sPriv
->fd
;
1883 sPriv
->driverPrivate
= (void *)screen
;
1885 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1888 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1889 pscreen
= pipe_loader_create_screen(screen
->dev
);
1894 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1895 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1896 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1897 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1900 sPriv
->extensions
= dri_screen_extensions
;
1902 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1904 goto destroy_screen
;
1906 screen
->can_share_buffer
= false;
1907 screen
->auto_fake_front
= dri_with_format(sPriv
);
1908 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1909 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1914 dri_destroy_screen_helper(screen
);
1918 pipe_loader_release(&screen
->dev
, 1);
1924 #endif // GALLIUM_SOFTPIPE
1929 dri2_create_buffer(__DRIscreen
* sPriv
,
1930 __DRIdrawable
* dPriv
,
1931 const struct gl_config
* visual
, boolean isPixmap
)
1933 struct dri_drawable
*drawable
= NULL
;
1935 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1938 drawable
= dPriv
->driverPrivate
;
1940 drawable
->allocate_textures
= dri2_allocate_textures
;
1941 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1942 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1948 * DRI driver virtual function table.
1950 * DRI versions differ in their implementation of init_screen and swap_buffers.
1952 const struct __DriverAPIRec galliumdrm_driver_api
= {
1953 .InitScreen
= dri2_init_screen
,
1954 .DestroyScreen
= dri_destroy_screen
,
1955 .CreateContext
= dri_create_context
,
1956 .DestroyContext
= dri_destroy_context
,
1957 .CreateBuffer
= dri2_create_buffer
,
1958 .DestroyBuffer
= dri_destroy_buffer
,
1959 .MakeCurrent
= dri_make_current
,
1960 .UnbindContext
= dri_unbind_context
,
1962 .AllocateBuffer
= dri2_allocate_buffer
,
1963 .ReleaseBuffer
= dri2_release_buffer
,
1967 * DRI driver virtual function table.
1969 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1970 * hook. The latter is used to explicitly initialise the kms_swrast driver
1971 * rather than selecting the approapriate driver as suggested by the loader.
1973 const struct __DriverAPIRec dri_kms_driver_api
= {
1974 .InitScreen
= dri_kms_init_screen
,
1975 .DestroyScreen
= dri_destroy_screen
,
1976 .CreateContext
= dri_create_context
,
1977 .DestroyContext
= dri_destroy_context
,
1978 .CreateBuffer
= dri2_create_buffer
,
1979 .DestroyBuffer
= dri_destroy_buffer
,
1980 .MakeCurrent
= dri_make_current
,
1981 .UnbindContext
= dri_unbind_context
,
1983 .AllocateBuffer
= dri2_allocate_buffer
,
1984 .ReleaseBuffer
= dri2_release_buffer
,
1987 /* This is the table of extensions that the loader will dlsym() for. */
1988 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1989 &driCoreExtension
.base
,
1990 &driImageDriverExtension
.base
,
1991 &driDRI2Extension
.base
,
1992 &gallium_config_options
.base
,
1996 /* vim: set sw=3 ts=8 sts=3 expandtab: */