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
;
171 p_atomic_inc(&drawable
->base
.stamp
);
174 static const __DRI2flushExtension dri2FlushExtension
= {
175 .base
= { __DRI2_FLUSH
, 4 },
177 .flush
= dri2_flush_drawable
,
178 .invalidate
= dri2_invalidate_drawable
,
179 .flush_with_flags
= dri_flush
,
183 * Retrieve __DRIbuffer from the DRI loader.
186 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
187 const enum st_attachment_type
*atts
,
190 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
191 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
193 __DRIbuffer
*buffers
;
195 unsigned attachments
[10];
196 unsigned num_attachments
, i
;
199 with_format
= dri_with_format(drawable
->sPriv
);
203 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
205 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
207 for (i
= 0; i
< *count
; i
++) {
208 enum pipe_format format
;
212 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
213 if (format
== PIPE_FORMAT_NONE
)
217 case ST_ATTACHMENT_FRONT_LEFT
:
221 att
= __DRI_BUFFER_FRONT_LEFT
;
223 case ST_ATTACHMENT_BACK_LEFT
:
224 att
= __DRI_BUFFER_BACK_LEFT
;
226 case ST_ATTACHMENT_FRONT_RIGHT
:
227 att
= __DRI_BUFFER_FRONT_RIGHT
;
229 case ST_ATTACHMENT_BACK_RIGHT
:
230 att
= __DRI_BUFFER_BACK_RIGHT
;
237 * In this switch statement we must support all formats that
238 * may occur as the stvis->color_format.
241 case PIPE_FORMAT_BGRA8888_UNORM
:
244 case PIPE_FORMAT_BGRX8888_UNORM
:
247 case PIPE_FORMAT_B5G6R5_UNORM
:
251 depth
= util_format_get_blocksizebits(format
);
252 assert(!"Unexpected format in dri2_drawable_get_buffers()");
255 attachments
[num_attachments
++] = att
;
257 attachments
[num_attachments
++] = depth
;
262 num_attachments
/= 2;
263 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
264 &dri_drawable
->w
, &dri_drawable
->h
,
265 attachments
, num_attachments
,
266 &num_buffers
, dri_drawable
->loaderPrivate
);
269 buffers
= loader
->getBuffers(dri_drawable
,
270 &dri_drawable
->w
, &dri_drawable
->h
,
271 attachments
, num_attachments
,
272 &num_buffers
, dri_drawable
->loaderPrivate
);
276 *count
= num_buffers
;
282 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
283 struct __DRIimageList
*images
,
284 const enum st_attachment_type
*statts
,
285 unsigned statts_count
)
287 __DRIdrawable
*dPriv
= drawable
->dPriv
;
288 __DRIscreen
*sPriv
= drawable
->sPriv
;
289 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
291 uint32_t buffer_mask
= 0;
294 for (i
= 0; i
< statts_count
; i
++) {
295 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
296 if (pf
== PIPE_FORMAT_NONE
)
300 case ST_ATTACHMENT_FRONT_LEFT
:
301 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
303 case ST_ATTACHMENT_BACK_LEFT
:
304 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
311 case PIPE_FORMAT_B5G6R5_UNORM
:
312 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
314 case PIPE_FORMAT_BGRX8888_UNORM
:
315 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
317 case PIPE_FORMAT_BGRA8888_UNORM
:
318 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
320 case PIPE_FORMAT_RGBA8888_UNORM
:
321 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
324 image_format
= __DRI_IMAGE_FORMAT_NONE
;
329 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
330 (uint32_t *) &drawable
->base
.stamp
,
331 dPriv
->loaderPrivate
, buffer_mask
,
336 dri2_allocate_buffer(__DRIscreen
*sPriv
,
337 unsigned attachment
, unsigned format
,
338 int width
, int height
)
340 struct dri_screen
*screen
= dri_screen(sPriv
);
341 struct dri2_buffer
*buffer
;
342 struct pipe_resource templ
;
345 struct winsys_handle whandle
;
347 switch (attachment
) {
348 case __DRI_BUFFER_FRONT_LEFT
:
349 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
350 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
352 case __DRI_BUFFER_BACK_LEFT
:
353 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
355 case __DRI_BUFFER_DEPTH
:
356 case __DRI_BUFFER_DEPTH_STENCIL
:
357 case __DRI_BUFFER_STENCIL
:
358 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
362 /* because we get the handle and stride */
363 bind
|= PIPE_BIND_SHARED
;
367 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
370 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
373 pf
= PIPE_FORMAT_Z16_UNORM
;
379 buffer
= CALLOC_STRUCT(dri2_buffer
);
383 memset(&templ
, 0, sizeof(templ
));
386 templ
.target
= PIPE_TEXTURE_2D
;
387 templ
.last_level
= 0;
388 templ
.width0
= width
;
389 templ
.height0
= height
;
391 templ
.array_size
= 1;
394 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
395 if (!buffer
->resource
) {
400 memset(&whandle
, 0, sizeof(whandle
));
401 if (screen
->can_share_buffer
)
402 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
404 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
406 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
,
407 buffer
->resource
, &whandle
,
408 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
410 buffer
->base
.attachment
= attachment
;
411 buffer
->base
.name
= whandle
.handle
;
412 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
413 buffer
->base
.pitch
= whandle
.stride
;
415 return &buffer
->base
;
419 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
421 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
423 pipe_resource_reference(&buffer
->resource
, NULL
);
428 * Backend functions for st_framebuffer interface.
432 dri2_allocate_textures(struct dri_context
*ctx
,
433 struct dri_drawable
*drawable
,
434 const enum st_attachment_type
*statts
,
435 unsigned statts_count
)
437 __DRIscreen
*sPriv
= drawable
->sPriv
;
438 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
439 struct dri_screen
*screen
= dri_screen(sPriv
);
440 struct pipe_resource templ
;
441 boolean alloc_depthstencil
= FALSE
;
443 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
444 /* Image specific variables */
445 struct __DRIimageList images
;
446 /* Dri2 specific variables */
447 __DRIbuffer
*buffers
= NULL
;
448 struct winsys_handle whandle
;
449 unsigned num_buffers
= statts_count
;
451 /* First get the buffers from the loader */
453 if (!dri_image_drawable_get_buffers(drawable
, &images
,
454 statts
, statts_count
))
458 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
459 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
460 drawable
->old_w
== dri_drawable
->w
&&
461 drawable
->old_h
== dri_drawable
->h
&&
462 memcmp(drawable
->old
, buffers
,
463 sizeof(__DRIbuffer
) * num_buffers
) == 0))
467 /* Second clean useless resources*/
469 /* See if we need a depth-stencil buffer. */
470 for (i
= 0; i
< statts_count
; i
++) {
471 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
472 alloc_depthstencil
= TRUE
;
477 /* Delete the resources we won't need. */
478 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
479 /* Don't delete the depth-stencil buffer, we can reuse it. */
480 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
483 /* Flush the texture before unreferencing, so that other clients can
484 * see what the driver has rendered.
486 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
487 struct pipe_context
*pipe
= ctx
->st
->pipe
;
488 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
491 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
494 if (drawable
->stvis
.samples
> 1) {
495 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
498 /* Don't delete MSAA resources for the attachments which are enabled,
499 * we can reuse them. */
500 for (j
= 0; j
< statts_count
; j
++) {
501 if (i
== statts
[j
]) {
508 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
513 /* Third use the buffers retrieved to fill the drawable info */
515 memset(&templ
, 0, sizeof(templ
));
516 templ
.target
= screen
->target
;
517 templ
.last_level
= 0;
519 templ
.array_size
= 1;
522 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
523 struct pipe_resource
**buf
=
524 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
525 struct pipe_resource
*texture
= images
.front
->texture
;
527 dri_drawable
->w
= texture
->width0
;
528 dri_drawable
->h
= texture
->height0
;
530 pipe_resource_reference(buf
, texture
);
533 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
534 struct pipe_resource
**buf
=
535 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
536 struct pipe_resource
*texture
= images
.back
->texture
;
538 dri_drawable
->w
= texture
->width0
;
539 dri_drawable
->h
= texture
->height0
;
541 pipe_resource_reference(buf
, texture
);
544 /* Note: if there is both a back and a front buffer,
545 * then they have the same size.
547 templ
.width0
= dri_drawable
->w
;
548 templ
.height0
= dri_drawable
->h
;
551 memset(&whandle
, 0, sizeof(whandle
));
553 /* Process DRI-provided buffers and get pipe_resources. */
554 for (i
= 0; i
< num_buffers
; i
++) {
555 __DRIbuffer
*buf
= &buffers
[i
];
556 enum st_attachment_type statt
;
557 enum pipe_format format
;
559 switch (buf
->attachment
) {
560 case __DRI_BUFFER_FRONT_LEFT
:
561 if (!screen
->auto_fake_front
) {
562 continue; /* invalid attachment */
565 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
566 statt
= ST_ATTACHMENT_FRONT_LEFT
;
568 case __DRI_BUFFER_BACK_LEFT
:
569 statt
= ST_ATTACHMENT_BACK_LEFT
;
572 continue; /* invalid attachment */
575 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
576 if (format
== PIPE_FORMAT_NONE
)
579 /* dri2_drawable_get_buffers has already filled dri_drawable->w
580 * and dri_drawable->h */
581 templ
.width0
= dri_drawable
->w
;
582 templ
.height0
= dri_drawable
->h
;
583 templ
.format
= format
;
585 whandle
.handle
= buf
->name
;
586 whandle
.stride
= buf
->pitch
;
588 if (screen
->can_share_buffer
)
589 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
591 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
592 drawable
->textures
[statt
] =
593 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
595 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
596 assert(drawable
->textures
[statt
]);
600 /* Allocate private MSAA colorbuffers. */
601 if (drawable
->stvis
.samples
> 1) {
602 for (i
= 0; i
< statts_count
; i
++) {
603 enum st_attachment_type statt
= statts
[i
];
605 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
608 if (drawable
->textures
[statt
]) {
609 templ
.format
= drawable
->textures
[statt
]->format
;
610 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
611 templ
.nr_samples
= drawable
->stvis
.samples
;
613 /* Try to reuse the resource.
614 * (the other resource parameters should be constant)
616 if (!drawable
->msaa_textures
[statt
] ||
617 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
618 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
619 /* Allocate a new one. */
620 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
622 drawable
->msaa_textures
[statt
] =
623 screen
->base
.screen
->resource_create(screen
->base
.screen
,
625 assert(drawable
->msaa_textures
[statt
]);
627 /* If there are any MSAA resources, we should initialize them
628 * such that they contain the same data as the single-sample
629 * resources we just got from the X server.
631 * The reason for this is that the state tracker (and
632 * therefore the app) can access the MSAA resources only.
633 * The single-sample resources are not exposed
634 * to the state tracker.
637 dri_pipe_blit(ctx
->st
->pipe
,
638 drawable
->msaa_textures
[statt
],
639 drawable
->textures
[statt
]);
643 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
648 /* Allocate a private depth-stencil buffer. */
649 if (alloc_depthstencil
) {
650 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
651 struct pipe_resource
**zsbuf
;
652 enum pipe_format format
;
655 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
658 templ
.format
= format
;
661 if (drawable
->stvis
.samples
> 1) {
662 templ
.nr_samples
= drawable
->stvis
.samples
;
663 zsbuf
= &drawable
->msaa_textures
[statt
];
666 templ
.nr_samples
= 0;
667 zsbuf
= &drawable
->textures
[statt
];
670 /* Try to reuse the resource.
671 * (the other resource parameters should be constant)
674 (*zsbuf
)->width0
!= templ
.width0
||
675 (*zsbuf
)->height0
!= templ
.height0
) {
676 /* Allocate a new one. */
677 pipe_resource_reference(zsbuf
, NULL
);
678 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
684 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
685 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
689 /* For DRI2, we may get the same buffers again from the server.
690 * To prevent useless imports of gem names, drawable->old* is used
691 * to bypass the import if we get the same buffers. This doesn't apply
692 * to DRI3/Wayland, users of image.loader, since the buffer is managed
693 * by the client (no import), and the back buffer is going to change
697 drawable
->old_num
= num_buffers
;
698 drawable
->old_w
= dri_drawable
->w
;
699 drawable
->old_h
= dri_drawable
->h
;
700 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
705 dri2_flush_frontbuffer(struct dri_context
*ctx
,
706 struct dri_drawable
*drawable
,
707 enum st_attachment_type statt
)
709 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
710 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
711 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
712 struct pipe_context
*pipe
= ctx
->st
->pipe
;
714 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
717 if (drawable
->stvis
.samples
> 1) {
718 /* Resolve the front buffer. */
719 dri_pipe_blit(ctx
->st
->pipe
,
720 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
721 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
724 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
725 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
728 pipe
->flush(pipe
, NULL
, 0);
731 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
733 else if (loader
->flushFrontBuffer
) {
734 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
739 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
740 struct dri_context
*ctx
,
741 struct pipe_resource
*res
)
747 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
749 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
752 if (!loader
->lookupEGLImage
)
755 img
= loader
->lookupEGLImage(screen
->sPriv
,
756 handle
, screen
->sPriv
->loaderPrivate
);
762 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
763 int width
, int height
, int format
,
764 struct winsys_handle
*whandle
,
767 struct dri_screen
*screen
= dri_screen(_screen
);
769 struct pipe_resource templ
;
773 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
775 pf
= dri2_format_to_pipe_format (format
);
776 if (pf
== PIPE_FORMAT_NONE
)
779 img
= CALLOC_STRUCT(__DRIimageRec
);
783 memset(&templ
, 0, sizeof(templ
));
784 templ
.bind
= tex_usage
;
786 templ
.target
= screen
->target
;
787 templ
.last_level
= 0;
788 templ
.width0
= width
;
789 templ
.height0
= height
;
791 templ
.array_size
= 1;
793 img
->texture
= screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
794 &templ
, whandle
, PIPE_HANDLE_USAGE_READ_WRITE
);
802 img
->dri_format
= format
;
804 img
->loader_private
= loaderPrivate
;
810 dri2_create_image_from_name(__DRIscreen
*_screen
,
811 int width
, int height
, int format
,
812 int name
, int pitch
, void *loaderPrivate
)
814 struct winsys_handle whandle
;
817 memset(&whandle
, 0, sizeof(whandle
));
818 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
819 whandle
.handle
= name
;
821 pf
= dri2_format_to_pipe_format (format
);
822 if (pf
== PIPE_FORMAT_NONE
)
825 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
827 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
828 &whandle
, loaderPrivate
);
832 dri2_create_image_from_fd(__DRIscreen
*_screen
,
833 int width
, int height
, int fourcc
,
834 int *fds
, int num_fds
, int *strides
,
835 int *offsets
, unsigned *error
,
836 int *dri_components
, void *loaderPrivate
)
838 struct winsys_handle whandle
;
840 __DRIimage
*img
= NULL
;
841 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
844 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
848 format
= convert_fourcc(fourcc
, dri_components
);
850 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
855 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
859 memset(&whandle
, 0, sizeof(whandle
));
860 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
861 whandle
.handle
= (unsigned)fds
[0];
862 whandle
.stride
= (unsigned)strides
[0];
863 whandle
.offset
= (unsigned)offsets
[0];
865 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
866 &whandle
, loaderPrivate
);
868 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
878 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
879 int renderbuffer
, void *loaderPrivate
)
881 struct dri_context
*ctx
= dri_context(context
);
883 if (!ctx
->st
->get_resource_for_egl_image
)
891 dri2_create_image(__DRIscreen
*_screen
,
892 int width
, int height
, int format
,
893 unsigned int use
, void *loaderPrivate
)
895 struct dri_screen
*screen
= dri_screen(_screen
);
897 struct pipe_resource templ
;
901 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
902 if (use
& __DRI_IMAGE_USE_SCANOUT
)
903 tex_usage
|= PIPE_BIND_SCANOUT
;
904 if (use
& __DRI_IMAGE_USE_SHARE
)
905 tex_usage
|= PIPE_BIND_SHARED
;
906 if (use
& __DRI_IMAGE_USE_LINEAR
)
907 tex_usage
|= PIPE_BIND_LINEAR
;
908 if (use
& __DRI_IMAGE_USE_CURSOR
) {
909 if (width
!= 64 || height
!= 64)
911 tex_usage
|= PIPE_BIND_CURSOR
;
914 pf
= dri2_format_to_pipe_format (format
);
915 if (pf
== PIPE_FORMAT_NONE
)
918 img
= CALLOC_STRUCT(__DRIimageRec
);
922 memset(&templ
, 0, sizeof(templ
));
923 templ
.bind
= tex_usage
;
925 templ
.target
= PIPE_TEXTURE_2D
;
926 templ
.last_level
= 0;
927 templ
.width0
= width
;
928 templ
.height0
= height
;
930 templ
.array_size
= 1;
932 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
940 img
->dri_format
= format
;
941 img
->dri_components
= 0;
944 img
->loader_private
= loaderPrivate
;
949 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
951 struct winsys_handle whandle
;
954 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
955 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
957 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
959 memset(&whandle
, 0, sizeof(whandle
));
962 case __DRI_IMAGE_ATTRIB_STRIDE
:
963 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
964 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
965 image
->texture
, &whandle
, usage
);
966 *value
= whandle
.stride
;
968 case __DRI_IMAGE_ATTRIB_HANDLE
:
969 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
970 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
971 image
->texture
, &whandle
, usage
);
972 *value
= whandle
.handle
;
974 case __DRI_IMAGE_ATTRIB_NAME
:
975 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
976 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
977 image
->texture
, &whandle
, usage
);
978 *value
= whandle
.handle
;
980 case __DRI_IMAGE_ATTRIB_FD
:
981 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
982 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
983 image
->texture
, &whandle
, usage
);
984 *value
= whandle
.handle
;
986 case __DRI_IMAGE_ATTRIB_FORMAT
:
987 *value
= image
->dri_format
;
989 case __DRI_IMAGE_ATTRIB_WIDTH
:
990 *value
= image
->texture
->width0
;
992 case __DRI_IMAGE_ATTRIB_HEIGHT
:
993 *value
= image
->texture
->height0
;
995 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
996 if (image
->dri_components
== 0)
998 *value
= image
->dri_components
;
1000 case __DRI_IMAGE_ATTRIB_FOURCC
:
1001 *value
= convert_to_fourcc(image
->dri_format
);
1003 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1012 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1016 img
= CALLOC_STRUCT(__DRIimageRec
);
1020 img
->texture
= NULL
;
1021 pipe_resource_reference(&img
->texture
, image
->texture
);
1022 img
->level
= image
->level
;
1023 img
->layer
= image
->layer
;
1024 img
->dri_format
= image
->dri_format
;
1025 /* This should be 0 for sub images, but dup is also used for base images. */
1026 img
->dri_components
= image
->dri_components
;
1027 img
->loader_private
= loaderPrivate
;
1033 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1036 * Gallium drivers are bad at adding usages to the resources
1037 * once opened again in another process, which is the main use
1038 * case for this, so we have to lie.
1047 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1048 int *names
, int num_names
, int *strides
, int *offsets
,
1049 void *loaderPrivate
)
1053 struct winsys_handle whandle
;
1058 format
= convert_fourcc(format
, &dri_components
);
1062 memset(&whandle
, 0, sizeof(whandle
));
1063 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1064 whandle
.handle
= names
[0];
1065 whandle
.stride
= strides
[0];
1066 whandle
.offset
= offsets
[0];
1068 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1069 &whandle
, loaderPrivate
);
1073 img
->dri_components
= dri_components
;
1078 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1085 if (image
->dri_components
== 0)
1088 img
= dri2_dup_image(image
, loaderPrivate
);
1092 /* set this to 0 for sub images. */
1093 img
->dri_components
= 0;
1098 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1099 int depth
, int level
, unsigned *error
,
1100 void *loaderPrivate
)
1103 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1104 struct gl_texture_object
*obj
;
1105 struct pipe_resource
*tex
;
1108 obj
= _mesa_lookup_texture(ctx
, texture
);
1109 if (!obj
|| obj
->Target
!= target
) {
1110 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1114 tex
= st_get_texobj_resource(obj
);
1116 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1120 if (target
== GL_TEXTURE_CUBE_MAP
)
1123 _mesa_test_texobj_completeness(ctx
, obj
);
1124 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1125 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1129 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1130 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1134 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1135 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1139 img
= CALLOC_STRUCT(__DRIimageRec
);
1141 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1147 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1149 img
->loader_private
= loaderPrivate
;
1151 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1152 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1157 pipe_resource_reference(&img
->texture
, tex
);
1159 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1164 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1165 int *fds
, int num_fds
, int *strides
, int *offsets
,
1166 void *loaderPrivate
)
1171 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1172 fds
, num_fds
, strides
, offsets
, NULL
,
1173 &dri_components
, loaderPrivate
);
1177 img
->dri_components
= dri_components
;
1182 dri2_from_dma_bufs(__DRIscreen
*screen
,
1183 int width
, int height
, int fourcc
,
1184 int *fds
, int num_fds
,
1185 int *strides
, int *offsets
,
1186 enum __DRIYUVColorSpace yuv_color_space
,
1187 enum __DRISampleRange sample_range
,
1188 enum __DRIChromaSiting horizontal_siting
,
1189 enum __DRIChromaSiting vertical_siting
,
1191 void *loaderPrivate
)
1196 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1197 fds
, num_fds
, strides
, offsets
, error
,
1198 &dri_components
, loaderPrivate
);
1202 img
->yuv_color_space
= yuv_color_space
;
1203 img
->sample_range
= sample_range
;
1204 img
->horizontal_siting
= horizontal_siting
;
1205 img
->vertical_siting
= vertical_siting
;
1206 img
->dri_components
= dri_components
;
1208 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1213 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1214 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1215 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1218 struct dri_context
*ctx
= dri_context(context
);
1219 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1220 struct pipe_screen
*screen
;
1221 struct pipe_fence_handle
*fence
;
1222 struct pipe_blit_info blit
;
1227 memset(&blit
, 0, sizeof(blit
));
1228 blit
.dst
.resource
= dst
->texture
;
1229 blit
.dst
.box
.x
= dstx0
;
1230 blit
.dst
.box
.y
= dsty0
;
1231 blit
.dst
.box
.width
= dstwidth
;
1232 blit
.dst
.box
.height
= dstheight
;
1233 blit
.dst
.box
.depth
= 1;
1234 blit
.dst
.format
= dst
->texture
->format
;
1235 blit
.src
.resource
= src
->texture
;
1236 blit
.src
.box
.x
= srcx0
;
1237 blit
.src
.box
.y
= srcy0
;
1238 blit
.src
.box
.width
= srcwidth
;
1239 blit
.src
.box
.height
= srcheight
;
1240 blit
.src
.box
.depth
= 1;
1241 blit
.src
.format
= src
->texture
->format
;
1242 blit
.mask
= PIPE_MASK_RGBA
;
1243 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1245 pipe
->blit(pipe
, &blit
);
1247 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1248 pipe
->flush_resource(pipe
, dst
->texture
);
1249 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1250 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1251 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1252 pipe
->flush_resource(pipe
, dst
->texture
);
1253 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1254 (void) screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
1255 screen
->fence_reference(screen
, &fence
, NULL
);
1260 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1261 int x0
, int y0
, int width
, int height
,
1262 unsigned int flags
, int *stride
, void **data
)
1264 struct dri_context
*ctx
= dri_context(context
);
1265 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1266 enum pipe_transfer_usage pipe_access
= 0;
1267 struct pipe_transfer
*trans
;
1270 if (!image
|| !data
|| *data
)
1273 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1274 pipe_access
|= PIPE_TRANSFER_READ
;
1275 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1276 pipe_access
|= PIPE_TRANSFER_WRITE
;
1278 map
= pipe_transfer_map(pipe
, image
->texture
,
1279 0, 0, pipe_access
, x0
, y0
, width
, height
,
1283 *stride
= trans
->stride
;
1290 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1292 struct dri_context
*ctx
= dri_context(context
);
1293 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1295 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1299 dri2_destroy_image(__DRIimage
*img
)
1301 pipe_resource_reference(&img
->texture
, NULL
);
1306 dri2_get_capabilities(__DRIscreen
*_screen
)
1308 struct dri_screen
*screen
= dri_screen(_screen
);
1310 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1313 /* The extension is modified during runtime if DRI_PRIME is detected */
1314 static __DRIimageExtension dri2ImageExtension
= {
1315 .base
= { __DRI_IMAGE
, 12 },
1317 .createImageFromName
= dri2_create_image_from_name
,
1318 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1319 .destroyImage
= dri2_destroy_image
,
1320 .createImage
= dri2_create_image
,
1321 .queryImage
= dri2_query_image
,
1322 .dupImage
= dri2_dup_image
,
1323 .validateUsage
= dri2_validate_usage
,
1324 .createImageFromNames
= dri2_from_names
,
1325 .fromPlanar
= dri2_from_planar
,
1326 .createImageFromTexture
= dri2_create_from_texture
,
1327 .createImageFromFds
= NULL
,
1328 .createImageFromDmaBufs
= NULL
,
1329 .blitImage
= dri2_blit_image
,
1330 .getCapabilities
= dri2_get_capabilities
,
1331 .mapImage
= dri2_map_image
,
1332 .unmapImage
= dri2_unmap_image
,
1337 dri2_is_opencl_interop_loaded_locked(struct dri_screen
*screen
)
1339 return screen
->opencl_dri_event_add_ref
&&
1340 screen
->opencl_dri_event_release
&&
1341 screen
->opencl_dri_event_wait
&&
1342 screen
->opencl_dri_event_get_fence
;
1346 dri2_load_opencl_interop(struct dri_screen
*screen
)
1348 #if defined(RTLD_DEFAULT)
1351 pipe_mutex_lock(screen
->opencl_func_mutex
);
1353 if (dri2_is_opencl_interop_loaded_locked(screen
)) {
1354 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1358 screen
->opencl_dri_event_add_ref
=
1359 dlsym(RTLD_DEFAULT
, "opencl_dri_event_add_ref");
1360 screen
->opencl_dri_event_release
=
1361 dlsym(RTLD_DEFAULT
, "opencl_dri_event_release");
1362 screen
->opencl_dri_event_wait
=
1363 dlsym(RTLD_DEFAULT
, "opencl_dri_event_wait");
1364 screen
->opencl_dri_event_get_fence
=
1365 dlsym(RTLD_DEFAULT
, "opencl_dri_event_get_fence");
1367 success
= dri2_is_opencl_interop_loaded_locked(screen
);
1368 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1376 struct dri_screen
*driscreen
;
1377 struct pipe_fence_handle
*pipe_fence
;
1382 dri2_create_fence(__DRIcontext
*_ctx
)
1384 struct pipe_context
*ctx
= dri_context(_ctx
)->st
->pipe
;
1385 struct dri2_fence
*fence
= CALLOC_STRUCT(dri2_fence
);
1390 ctx
->flush(ctx
, &fence
->pipe_fence
, 0);
1392 if (!fence
->pipe_fence
) {
1397 fence
->driscreen
= dri_screen(_ctx
->driScreenPriv
);
1402 dri2_get_fence_from_cl_event(__DRIscreen
*_screen
, intptr_t cl_event
)
1404 struct dri_screen
*driscreen
= dri_screen(_screen
);
1405 struct dri2_fence
*fence
;
1407 if (!dri2_load_opencl_interop(driscreen
))
1410 fence
= CALLOC_STRUCT(dri2_fence
);
1414 fence
->cl_event
= (void*)cl_event
;
1416 if (!driscreen
->opencl_dri_event_add_ref(fence
->cl_event
)) {
1421 fence
->driscreen
= driscreen
;
1426 dri2_destroy_fence(__DRIscreen
*_screen
, void *_fence
)
1428 struct dri_screen
*driscreen
= dri_screen(_screen
);
1429 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1430 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1432 if (fence
->pipe_fence
)
1433 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
1434 else if (fence
->cl_event
)
1435 driscreen
->opencl_dri_event_release(fence
->cl_event
);
1443 dri2_client_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
,
1446 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1447 struct dri_screen
*driscreen
= fence
->driscreen
;
1448 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1450 /* No need to flush. The context was flushed when the fence was created. */
1452 if (fence
->pipe_fence
)
1453 return screen
->fence_finish(screen
, fence
->pipe_fence
, timeout
);
1454 else if (fence
->cl_event
) {
1455 struct pipe_fence_handle
*pipe_fence
=
1456 driscreen
->opencl_dri_event_get_fence(fence
->cl_event
);
1459 return screen
->fence_finish(screen
, pipe_fence
, timeout
);
1461 return driscreen
->opencl_dri_event_wait(fence
->cl_event
, timeout
);
1470 dri2_server_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
)
1472 /* AFAIK, no driver currently supports parallel context execution. */
1475 static __DRI2fenceExtension dri2FenceExtension
= {
1476 .base
= { __DRI2_FENCE
, 1 },
1478 .create_fence
= dri2_create_fence
,
1479 .get_fence_from_cl_event
= dri2_get_fence_from_cl_event
,
1480 .destroy_fence
= dri2_destroy_fence
,
1481 .client_wait_sync
= dri2_client_wait_sync
,
1482 .server_wait_sync
= dri2_server_wait_sync
1485 static const __DRIrobustnessExtension dri2Robustness
= {
1486 .base
= { __DRI2_ROBUSTNESS
, 1 }
1490 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1491 mesa_glinterop_device_info
*out
)
1493 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1495 /* There is no version 0, thus we do not support it */
1496 if (out
->version
== 0)
1497 return MESA_GLINTEROP_INVALID_VERSION
;
1499 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1500 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1501 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1502 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1504 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1505 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1507 /* Instruct the caller that we support up-to version one of the interface */
1510 return MESA_GLINTEROP_SUCCESS
;
1514 dri2_interop_export_object(__DRIcontext
*_ctx
,
1515 mesa_glinterop_export_in
*in
,
1516 mesa_glinterop_export_out
*out
)
1518 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1519 struct pipe_screen
*screen
= st
->pipe
->screen
;
1520 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1521 struct pipe_resource
*res
= NULL
;
1522 struct winsys_handle whandle
;
1523 unsigned target
, usage
;
1526 /* There is no version 0, thus we do not support it */
1527 if (in
->version
== 0 || out
->version
== 0)
1528 return MESA_GLINTEROP_INVALID_VERSION
;
1530 /* Validate the target. */
1531 switch (in
->target
) {
1532 case GL_TEXTURE_BUFFER
:
1536 case GL_TEXTURE_RECTANGLE
:
1537 case GL_TEXTURE_1D_ARRAY
:
1538 case GL_TEXTURE_2D_ARRAY
:
1539 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1540 case GL_TEXTURE_CUBE_MAP
:
1541 case GL_TEXTURE_2D_MULTISAMPLE
:
1542 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1543 case GL_TEXTURE_EXTERNAL_OES
:
1544 case GL_RENDERBUFFER
:
1545 case GL_ARRAY_BUFFER
:
1546 target
= in
->target
;
1548 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1549 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1550 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1551 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1552 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1553 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1554 target
= GL_TEXTURE_CUBE_MAP
;
1557 return MESA_GLINTEROP_INVALID_TARGET
;
1560 /* Validate the simple case of miplevel. */
1561 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1563 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1565 /* Validate the OpenGL object and get pipe_resource. */
1566 mtx_lock(&ctx
->Shared
->Mutex
);
1568 if (target
== GL_ARRAY_BUFFER
) {
1571 * The error checking is based on the documentation of
1572 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1574 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1576 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1577 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1578 * a GL buffer object but does not have an existing data store or
1579 * the size of the buffer is 0."
1581 if (!buf
|| buf
->Size
== 0) {
1582 mtx_unlock(&ctx
->Shared
->Mutex
);
1583 return MESA_GLINTEROP_INVALID_OBJECT
;
1586 res
= st_buffer_object(buf
)->buffer
;
1588 /* this shouldn't happen */
1589 mtx_unlock(&ctx
->Shared
->Mutex
);
1590 return MESA_GLINTEROP_INVALID_OBJECT
;
1593 out
->buf_offset
= 0;
1594 out
->buf_size
= buf
->Size
;
1596 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1597 } else if (target
== GL_RENDERBUFFER
) {
1600 * The error checking is based on the documentation of
1601 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1603 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1605 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1606 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1607 * object or if the width or height of renderbuffer is zero."
1609 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1610 mtx_unlock(&ctx
->Shared
->Mutex
);
1611 return MESA_GLINTEROP_INVALID_OBJECT
;
1614 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1615 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1616 * renderbuffer object."
1618 if (rb
->NumSamples
> 1) {
1619 mtx_unlock(&ctx
->Shared
->Mutex
);
1620 return MESA_GLINTEROP_INVALID_OPERATION
;
1623 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1624 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1625 * required by the OpenCL implementation on the device."
1627 res
= st_renderbuffer(rb
)->texture
;
1629 mtx_unlock(&ctx
->Shared
->Mutex
);
1630 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1633 out
->internal_format
= rb
->InternalFormat
;
1634 out
->view_minlevel
= 0;
1635 out
->view_numlevels
= 1;
1636 out
->view_minlayer
= 0;
1637 out
->view_numlayers
= 1;
1641 * The error checking is based on the documentation of
1642 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1644 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1647 _mesa_test_texobj_completeness(ctx
, obj
);
1649 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1650 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1651 * type matches texture_target, if the specified miplevel of texture
1652 * is not defined, or if the width or height of the specified
1653 * miplevel is zero or if the GL texture object is incomplete."
1656 obj
->Target
!= target
||
1657 !obj
->_BaseComplete
||
1658 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1659 mtx_unlock(&ctx
->Shared
->Mutex
);
1660 return MESA_GLINTEROP_INVALID_OBJECT
;
1663 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1664 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1665 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1666 * implementations); or greater than the value of q (for both OpenGL
1667 * and OpenGL ES). levelbase and q are defined for the texture in
1668 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1669 * specification and section 3.7.10 of the OpenGL ES 2.0."
1671 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1672 mtx_unlock(&ctx
->Shared
->Mutex
);
1673 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1676 if (!st_finalize_texture(ctx
, st
->pipe
, obj
)) {
1677 mtx_unlock(&ctx
->Shared
->Mutex
);
1678 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1681 res
= st_get_texobj_resource(obj
);
1683 /* Incomplete texture buffer object? This shouldn't really occur. */
1684 mtx_unlock(&ctx
->Shared
->Mutex
);
1685 return MESA_GLINTEROP_INVALID_OBJECT
;
1688 if (target
== GL_TEXTURE_BUFFER
) {
1689 out
->internal_format
= obj
->BufferObjectFormat
;
1690 out
->buf_offset
= obj
->BufferOffset
;
1691 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1694 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1696 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1697 out
->view_minlevel
= obj
->MinLevel
;
1698 out
->view_numlevels
= obj
->NumLevels
;
1699 out
->view_minlayer
= obj
->MinLayer
;
1700 out
->view_numlayers
= obj
->NumLayers
;
1704 /* Get the handle. */
1705 switch (in
->access
) {
1706 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1707 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1709 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1710 usage
= PIPE_HANDLE_USAGE_READ
;
1712 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1713 usage
= PIPE_HANDLE_USAGE_WRITE
;
1719 memset(&whandle
, 0, sizeof(whandle
));
1720 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1722 success
= screen
->resource_get_handle(screen
, res
, &whandle
, usage
);
1723 mtx_unlock(&ctx
->Shared
->Mutex
);
1726 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1728 out
->dmabuf_fd
= whandle
.handle
;
1729 out
->out_driver_data_written
= 0;
1731 if (res
->target
== PIPE_BUFFER
)
1732 out
->buf_offset
+= whandle
.offset
;
1734 /* Instruct the caller that we support up-to version one of the interface */
1738 return MESA_GLINTEROP_SUCCESS
;
1741 static const __DRI2interopExtension dri2InteropExtension
= {
1742 .base
= { __DRI2_INTEROP
, 1 },
1743 .query_device_info
= dri2_interop_query_device_info
,
1744 .export_object
= dri2_interop_export_object
1748 * Backend function init_screen.
1751 static const __DRIextension
*dri_screen_extensions
[] = {
1752 &driTexBufferExtension
.base
,
1753 &dri2FlushExtension
.base
,
1754 &dri2ImageExtension
.base
,
1755 &dri2RendererQueryExtension
.base
,
1756 &dri2ConfigQueryExtension
.base
,
1757 &dri2ThrottleExtension
.base
,
1758 &dri2FenceExtension
.base
,
1759 &dri2InteropExtension
.base
,
1763 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1764 &driTexBufferExtension
.base
,
1765 &dri2FlushExtension
.base
,
1766 &dri2ImageExtension
.base
,
1767 &dri2RendererQueryExtension
.base
,
1768 &dri2ConfigQueryExtension
.base
,
1769 &dri2ThrottleExtension
.base
,
1770 &dri2FenceExtension
.base
,
1771 &dri2InteropExtension
.base
,
1772 &dri2Robustness
.base
,
1777 * This is the driver specific part of the createNewScreen entry point.
1779 * Returns the struct gl_config supported by this driver.
1781 static const __DRIconfig
**
1782 dri2_init_screen(__DRIscreen
* sPriv
)
1784 const __DRIconfig
**configs
;
1785 struct dri_screen
*screen
;
1786 struct pipe_screen
*pscreen
= NULL
;
1787 const struct drm_conf_ret
*throttle_ret
;
1788 const struct drm_conf_ret
*dmabuf_ret
;
1791 screen
= CALLOC_STRUCT(dri_screen
);
1795 screen
->sPriv
= sPriv
;
1796 screen
->fd
= sPriv
->fd
;
1797 pipe_mutex_init(screen
->opencl_func_mutex
);
1799 sPriv
->driverPrivate
= (void *)screen
;
1801 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1804 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1805 pscreen
= pipe_loader_create_screen(screen
->dev
);
1810 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1811 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1813 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1814 screen
->throttling_enabled
= TRUE
;
1815 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1818 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1821 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1822 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1823 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1824 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1828 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1829 sPriv
->extensions
= dri_robust_screen_extensions
;
1830 screen
->has_reset_status_query
= true;
1833 sPriv
->extensions
= dri_screen_extensions
;
1835 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1837 goto destroy_screen
;
1839 screen
->can_share_buffer
= true;
1840 screen
->auto_fake_front
= dri_with_format(sPriv
);
1841 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1842 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1847 dri_destroy_screen_helper(screen
);
1851 pipe_loader_release(&screen
->dev
, 1);
1861 * This is the driver specific part of the createNewScreen entry point.
1863 * Returns the struct gl_config supported by this driver.
1865 static const __DRIconfig
**
1866 dri_kms_init_screen(__DRIscreen
* sPriv
)
1868 #if defined(GALLIUM_SOFTPIPE)
1869 const __DRIconfig
**configs
;
1870 struct dri_screen
*screen
;
1871 struct pipe_screen
*pscreen
= NULL
;
1875 screen
= CALLOC_STRUCT(dri_screen
);
1879 screen
->sPriv
= sPriv
;
1880 screen
->fd
= sPriv
->fd
;
1882 sPriv
->driverPrivate
= (void *)screen
;
1884 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1887 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1888 pscreen
= pipe_loader_create_screen(screen
->dev
);
1893 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1894 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1895 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1896 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1899 sPriv
->extensions
= dri_screen_extensions
;
1901 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1903 goto destroy_screen
;
1905 screen
->can_share_buffer
= false;
1906 screen
->auto_fake_front
= dri_with_format(sPriv
);
1907 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1908 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1913 dri_destroy_screen_helper(screen
);
1917 pipe_loader_release(&screen
->dev
, 1);
1923 #endif // GALLIUM_SOFTPIPE
1928 dri2_create_buffer(__DRIscreen
* sPriv
,
1929 __DRIdrawable
* dPriv
,
1930 const struct gl_config
* visual
, boolean isPixmap
)
1932 struct dri_drawable
*drawable
= NULL
;
1934 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1937 drawable
= dPriv
->driverPrivate
;
1939 drawable
->allocate_textures
= dri2_allocate_textures
;
1940 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1941 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1947 * DRI driver virtual function table.
1949 * DRI versions differ in their implementation of init_screen and swap_buffers.
1951 const struct __DriverAPIRec galliumdrm_driver_api
= {
1952 .InitScreen
= dri2_init_screen
,
1953 .DestroyScreen
= dri_destroy_screen
,
1954 .CreateContext
= dri_create_context
,
1955 .DestroyContext
= dri_destroy_context
,
1956 .CreateBuffer
= dri2_create_buffer
,
1957 .DestroyBuffer
= dri_destroy_buffer
,
1958 .MakeCurrent
= dri_make_current
,
1959 .UnbindContext
= dri_unbind_context
,
1961 .AllocateBuffer
= dri2_allocate_buffer
,
1962 .ReleaseBuffer
= dri2_release_buffer
,
1966 * DRI driver virtual function table.
1968 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1969 * hook. The latter is used to explicitly initialise the kms_swrast driver
1970 * rather than selecting the approapriate driver as suggested by the loader.
1972 const struct __DriverAPIRec dri_kms_driver_api
= {
1973 .InitScreen
= dri_kms_init_screen
,
1974 .DestroyScreen
= dri_destroy_screen
,
1975 .CreateContext
= dri_create_context
,
1976 .DestroyContext
= dri_destroy_context
,
1977 .CreateBuffer
= dri2_create_buffer
,
1978 .DestroyBuffer
= dri_destroy_buffer
,
1979 .MakeCurrent
= dri_make_current
,
1980 .UnbindContext
= dri_unbind_context
,
1982 .AllocateBuffer
= dri2_allocate_buffer
,
1983 .ReleaseBuffer
= dri2_release_buffer
,
1986 /* This is the table of extensions that the loader will dlsym() for. */
1987 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1988 &driCoreExtension
.base
,
1989 &driImageDriverExtension
.base
,
1990 &driDRI2Extension
.base
,
1991 &gallium_config_options
.base
,
1995 /* vim: set sw=3 ts=8 sts=3 expandtab: */