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_extensions.h"
52 #include "dri_query_renderer.h"
53 #include "dri2_buffer.h"
55 #ifndef DRM_FORMAT_MOD_INVALID
56 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
59 static int convert_fourcc(int format
, int *dri_components_p
)
63 case __DRI_IMAGE_FOURCC_RGB565
:
64 format
= __DRI_IMAGE_FORMAT_RGB565
;
65 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
67 case __DRI_IMAGE_FOURCC_ARGB8888
:
68 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
69 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
71 case __DRI_IMAGE_FOURCC_XRGB8888
:
72 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
73 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
75 case __DRI_IMAGE_FOURCC_ABGR8888
:
76 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
77 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
79 case __DRI_IMAGE_FOURCC_XBGR8888
:
80 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
81 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
83 case __DRI_IMAGE_FOURCC_R8
:
84 format
= __DRI_IMAGE_FORMAT_R8
;
85 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
87 case __DRI_IMAGE_FOURCC_GR88
:
88 format
= __DRI_IMAGE_FORMAT_GR88
;
89 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
92 * For multi-planar YUV formats, we return the format of the first
93 * plane only. Since there is only one caller which supports multi-
94 * planar YUV it gets to figure out the remaining planes on it's
97 case __DRI_IMAGE_FOURCC_YUV420
:
98 case __DRI_IMAGE_FOURCC_YVU420
:
99 format
= __DRI_IMAGE_FORMAT_R8
;
100 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
102 case __DRI_IMAGE_FOURCC_NV12
:
103 format
= __DRI_IMAGE_FORMAT_R8
;
104 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
109 *dri_components_p
= dri_components
;
113 /* NOTE this probably isn't going to do the right thing for YUV images
114 * (but I think the same can be said for intel_query_image()). I think
115 * only needed for exporting dmabuf's, so I think I won't loose much
118 static int convert_to_fourcc(int format
)
121 case __DRI_IMAGE_FORMAT_RGB565
:
122 format
= __DRI_IMAGE_FOURCC_RGB565
;
124 case __DRI_IMAGE_FORMAT_ARGB8888
:
125 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
127 case __DRI_IMAGE_FORMAT_XRGB8888
:
128 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
130 case __DRI_IMAGE_FORMAT_ABGR8888
:
131 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
133 case __DRI_IMAGE_FORMAT_XBGR8888
:
134 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
136 case __DRI_IMAGE_FORMAT_R8
:
137 format
= __DRI_IMAGE_FOURCC_R8
;
139 case __DRI_IMAGE_FORMAT_GR88
:
140 format
= __DRI_IMAGE_FOURCC_GR88
;
148 static enum pipe_format
dri2_format_to_pipe_format (int format
)
153 case __DRI_IMAGE_FORMAT_RGB565
:
154 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
156 case __DRI_IMAGE_FORMAT_XRGB8888
:
157 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
159 case __DRI_IMAGE_FORMAT_ARGB8888
:
160 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
162 case __DRI_IMAGE_FORMAT_ABGR8888
:
163 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
165 case __DRI_IMAGE_FORMAT_R8
:
166 pf
= PIPE_FORMAT_R8_UNORM
;
168 case __DRI_IMAGE_FORMAT_GR88
:
169 pf
= PIPE_FORMAT_RG88_UNORM
;
172 pf
= PIPE_FORMAT_NONE
;
180 * DRI2 flush extension.
183 dri2_flush_drawable(__DRIdrawable
*dPriv
)
185 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
189 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
191 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
193 dri2InvalidateDrawable(dPriv
);
194 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
195 drawable
->texture_mask
= 0;
197 p_atomic_inc(&drawable
->base
.stamp
);
200 static const __DRI2flushExtension dri2FlushExtension
= {
201 .base
= { __DRI2_FLUSH
, 4 },
203 .flush
= dri2_flush_drawable
,
204 .invalidate
= dri2_invalidate_drawable
,
205 .flush_with_flags
= dri_flush
,
209 * Retrieve __DRIbuffer from the DRI loader.
212 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
213 const enum st_attachment_type
*atts
,
216 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
217 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
219 __DRIbuffer
*buffers
;
221 unsigned attachments
[10];
222 unsigned num_attachments
, i
;
225 with_format
= dri_with_format(drawable
->sPriv
);
229 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
231 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
233 for (i
= 0; i
< *count
; i
++) {
234 enum pipe_format format
;
238 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
239 if (format
== PIPE_FORMAT_NONE
)
243 case ST_ATTACHMENT_FRONT_LEFT
:
247 att
= __DRI_BUFFER_FRONT_LEFT
;
249 case ST_ATTACHMENT_BACK_LEFT
:
250 att
= __DRI_BUFFER_BACK_LEFT
;
252 case ST_ATTACHMENT_FRONT_RIGHT
:
253 att
= __DRI_BUFFER_FRONT_RIGHT
;
255 case ST_ATTACHMENT_BACK_RIGHT
:
256 att
= __DRI_BUFFER_BACK_RIGHT
;
263 * In this switch statement we must support all formats that
264 * may occur as the stvis->color_format.
267 case PIPE_FORMAT_BGRA8888_UNORM
:
270 case PIPE_FORMAT_BGRX8888_UNORM
:
273 case PIPE_FORMAT_B5G6R5_UNORM
:
277 depth
= util_format_get_blocksizebits(format
);
278 assert(!"Unexpected format in dri2_drawable_get_buffers()");
281 attachments
[num_attachments
++] = att
;
283 attachments
[num_attachments
++] = depth
;
288 num_attachments
/= 2;
289 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
290 &dri_drawable
->w
, &dri_drawable
->h
,
291 attachments
, num_attachments
,
292 &num_buffers
, dri_drawable
->loaderPrivate
);
295 buffers
= loader
->getBuffers(dri_drawable
,
296 &dri_drawable
->w
, &dri_drawable
->h
,
297 attachments
, num_attachments
,
298 &num_buffers
, dri_drawable
->loaderPrivate
);
302 *count
= num_buffers
;
308 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
309 struct __DRIimageList
*images
,
310 const enum st_attachment_type
*statts
,
311 unsigned statts_count
)
313 __DRIdrawable
*dPriv
= drawable
->dPriv
;
314 __DRIscreen
*sPriv
= drawable
->sPriv
;
315 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
317 uint32_t buffer_mask
= 0;
320 for (i
= 0; i
< statts_count
; i
++) {
321 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
322 if (pf
== PIPE_FORMAT_NONE
)
326 case ST_ATTACHMENT_FRONT_LEFT
:
327 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
329 case ST_ATTACHMENT_BACK_LEFT
:
330 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
337 case PIPE_FORMAT_B5G6R5_UNORM
:
338 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
340 case PIPE_FORMAT_BGRX8888_UNORM
:
341 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
343 case PIPE_FORMAT_BGRA8888_UNORM
:
344 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
346 case PIPE_FORMAT_RGBA8888_UNORM
:
347 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
350 image_format
= __DRI_IMAGE_FORMAT_NONE
;
355 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
356 (uint32_t *) &drawable
->base
.stamp
,
357 dPriv
->loaderPrivate
, buffer_mask
,
362 dri2_allocate_buffer(__DRIscreen
*sPriv
,
363 unsigned attachment
, unsigned format
,
364 int width
, int height
)
366 struct dri_screen
*screen
= dri_screen(sPriv
);
367 struct dri2_buffer
*buffer
;
368 struct pipe_resource templ
;
371 struct winsys_handle whandle
;
373 switch (attachment
) {
374 case __DRI_BUFFER_FRONT_LEFT
:
375 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
376 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
378 case __DRI_BUFFER_BACK_LEFT
:
379 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
381 case __DRI_BUFFER_DEPTH
:
382 case __DRI_BUFFER_DEPTH_STENCIL
:
383 case __DRI_BUFFER_STENCIL
:
384 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
388 /* because we get the handle and stride */
389 bind
|= PIPE_BIND_SHARED
;
393 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
396 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
399 pf
= PIPE_FORMAT_Z16_UNORM
;
405 buffer
= CALLOC_STRUCT(dri2_buffer
);
409 memset(&templ
, 0, sizeof(templ
));
412 templ
.target
= PIPE_TEXTURE_2D
;
413 templ
.last_level
= 0;
414 templ
.width0
= width
;
415 templ
.height0
= height
;
417 templ
.array_size
= 1;
420 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
421 if (!buffer
->resource
) {
426 memset(&whandle
, 0, sizeof(whandle
));
427 if (screen
->can_share_buffer
)
428 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
430 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
432 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
433 buffer
->resource
, &whandle
,
434 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
436 buffer
->base
.attachment
= attachment
;
437 buffer
->base
.name
= whandle
.handle
;
438 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
439 buffer
->base
.pitch
= whandle
.stride
;
441 return &buffer
->base
;
445 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
447 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
449 pipe_resource_reference(&buffer
->resource
, NULL
);
454 * Backend functions for st_framebuffer interface.
458 dri2_allocate_textures(struct dri_context
*ctx
,
459 struct dri_drawable
*drawable
,
460 const enum st_attachment_type
*statts
,
461 unsigned statts_count
)
463 __DRIscreen
*sPriv
= drawable
->sPriv
;
464 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
465 struct dri_screen
*screen
= dri_screen(sPriv
);
466 struct pipe_resource templ
;
467 boolean alloc_depthstencil
= FALSE
;
469 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
470 /* Image specific variables */
471 struct __DRIimageList images
;
472 /* Dri2 specific variables */
473 __DRIbuffer
*buffers
= NULL
;
474 struct winsys_handle whandle
;
475 unsigned num_buffers
= statts_count
;
477 /* First get the buffers from the loader */
479 if (!dri_image_drawable_get_buffers(drawable
, &images
,
480 statts
, statts_count
))
484 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
485 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
486 drawable
->old_w
== dri_drawable
->w
&&
487 drawable
->old_h
== dri_drawable
->h
&&
488 memcmp(drawable
->old
, buffers
,
489 sizeof(__DRIbuffer
) * num_buffers
) == 0))
493 /* Second clean useless resources*/
495 /* See if we need a depth-stencil buffer. */
496 for (i
= 0; i
< statts_count
; i
++) {
497 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
498 alloc_depthstencil
= TRUE
;
503 /* Delete the resources we won't need. */
504 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
505 /* Don't delete the depth-stencil buffer, we can reuse it. */
506 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
509 /* Flush the texture before unreferencing, so that other clients can
510 * see what the driver has rendered.
512 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
513 struct pipe_context
*pipe
= ctx
->st
->pipe
;
514 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
517 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
520 if (drawable
->stvis
.samples
> 1) {
521 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
524 /* Don't delete MSAA resources for the attachments which are enabled,
525 * we can reuse them. */
526 for (j
= 0; j
< statts_count
; j
++) {
527 if (i
== statts
[j
]) {
534 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
539 /* Third use the buffers retrieved to fill the drawable info */
541 memset(&templ
, 0, sizeof(templ
));
542 templ
.target
= screen
->target
;
543 templ
.last_level
= 0;
545 templ
.array_size
= 1;
548 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
549 struct pipe_resource
**buf
=
550 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
551 struct pipe_resource
*texture
= images
.front
->texture
;
553 dri_drawable
->w
= texture
->width0
;
554 dri_drawable
->h
= texture
->height0
;
556 pipe_resource_reference(buf
, texture
);
559 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
560 struct pipe_resource
**buf
=
561 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
562 struct pipe_resource
*texture
= images
.back
->texture
;
564 dri_drawable
->w
= texture
->width0
;
565 dri_drawable
->h
= texture
->height0
;
567 pipe_resource_reference(buf
, texture
);
570 /* Note: if there is both a back and a front buffer,
571 * then they have the same size.
573 templ
.width0
= dri_drawable
->w
;
574 templ
.height0
= dri_drawable
->h
;
577 memset(&whandle
, 0, sizeof(whandle
));
579 /* Process DRI-provided buffers and get pipe_resources. */
580 for (i
= 0; i
< num_buffers
; i
++) {
581 __DRIbuffer
*buf
= &buffers
[i
];
582 enum st_attachment_type statt
;
583 enum pipe_format format
;
585 switch (buf
->attachment
) {
586 case __DRI_BUFFER_FRONT_LEFT
:
587 if (!screen
->auto_fake_front
) {
588 continue; /* invalid attachment */
591 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
592 statt
= ST_ATTACHMENT_FRONT_LEFT
;
594 case __DRI_BUFFER_BACK_LEFT
:
595 statt
= ST_ATTACHMENT_BACK_LEFT
;
598 continue; /* invalid attachment */
601 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
602 if (format
== PIPE_FORMAT_NONE
)
605 /* dri2_drawable_get_buffers has already filled dri_drawable->w
606 * and dri_drawable->h */
607 templ
.width0
= dri_drawable
->w
;
608 templ
.height0
= dri_drawable
->h
;
609 templ
.format
= format
;
611 whandle
.handle
= buf
->name
;
612 whandle
.stride
= buf
->pitch
;
614 if (screen
->can_share_buffer
)
615 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
617 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
618 drawable
->textures
[statt
] =
619 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
621 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
622 assert(drawable
->textures
[statt
]);
626 /* Allocate private MSAA colorbuffers. */
627 if (drawable
->stvis
.samples
> 1) {
628 for (i
= 0; i
< statts_count
; i
++) {
629 enum st_attachment_type statt
= statts
[i
];
631 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
634 if (drawable
->textures
[statt
]) {
635 templ
.format
= drawable
->textures
[statt
]->format
;
636 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
637 templ
.nr_samples
= drawable
->stvis
.samples
;
639 /* Try to reuse the resource.
640 * (the other resource parameters should be constant)
642 if (!drawable
->msaa_textures
[statt
] ||
643 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
644 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
645 /* Allocate a new one. */
646 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
648 drawable
->msaa_textures
[statt
] =
649 screen
->base
.screen
->resource_create(screen
->base
.screen
,
651 assert(drawable
->msaa_textures
[statt
]);
653 /* If there are any MSAA resources, we should initialize them
654 * such that they contain the same data as the single-sample
655 * resources we just got from the X server.
657 * The reason for this is that the state tracker (and
658 * therefore the app) can access the MSAA resources only.
659 * The single-sample resources are not exposed
660 * to the state tracker.
663 dri_pipe_blit(ctx
->st
->pipe
,
664 drawable
->msaa_textures
[statt
],
665 drawable
->textures
[statt
]);
669 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
674 /* Allocate a private depth-stencil buffer. */
675 if (alloc_depthstencil
) {
676 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
677 struct pipe_resource
**zsbuf
;
678 enum pipe_format format
;
681 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
684 templ
.format
= format
;
687 if (drawable
->stvis
.samples
> 1) {
688 templ
.nr_samples
= drawable
->stvis
.samples
;
689 zsbuf
= &drawable
->msaa_textures
[statt
];
692 templ
.nr_samples
= 0;
693 zsbuf
= &drawable
->textures
[statt
];
696 /* Try to reuse the resource.
697 * (the other resource parameters should be constant)
700 (*zsbuf
)->width0
!= templ
.width0
||
701 (*zsbuf
)->height0
!= templ
.height0
) {
702 /* Allocate a new one. */
703 pipe_resource_reference(zsbuf
, NULL
);
704 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
710 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
711 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
715 /* For DRI2, we may get the same buffers again from the server.
716 * To prevent useless imports of gem names, drawable->old* is used
717 * to bypass the import if we get the same buffers. This doesn't apply
718 * to DRI3/Wayland, users of image.loader, since the buffer is managed
719 * by the client (no import), and the back buffer is going to change
723 drawable
->old_num
= num_buffers
;
724 drawable
->old_w
= dri_drawable
->w
;
725 drawable
->old_h
= dri_drawable
->h
;
726 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
731 dri2_flush_frontbuffer(struct dri_context
*ctx
,
732 struct dri_drawable
*drawable
,
733 enum st_attachment_type statt
)
735 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
736 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
737 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
738 struct pipe_context
*pipe
= ctx
->st
->pipe
;
740 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
743 if (drawable
->stvis
.samples
> 1) {
744 /* Resolve the front buffer. */
745 dri_pipe_blit(ctx
->st
->pipe
,
746 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
747 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
750 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
751 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
754 pipe
->flush(pipe
, NULL
, 0);
757 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
759 else if (loader
->flushFrontBuffer
) {
760 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
765 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
766 struct dri_context
*ctx
,
767 struct pipe_resource
*res
)
773 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
775 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
778 if (!loader
->lookupEGLImage
)
781 img
= loader
->lookupEGLImage(screen
->sPriv
,
782 handle
, screen
->sPriv
->loaderPrivate
);
788 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
789 int width
, int height
, int format
,
790 int num_handles
, struct winsys_handle
*whandle
,
793 struct dri_screen
*screen
= dri_screen(_screen
);
794 struct pipe_screen
*pscreen
= screen
->base
.screen
;
796 struct pipe_resource templ
;
801 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
803 pf
= dri2_format_to_pipe_format (format
);
804 if (pf
== PIPE_FORMAT_NONE
)
807 img
= CALLOC_STRUCT(__DRIimageRec
);
811 memset(&templ
, 0, sizeof(templ
));
812 templ
.bind
= tex_usage
;
813 templ
.target
= screen
->target
;
814 templ
.last_level
= 0;
816 templ
.array_size
= 1;
818 for (i
= num_handles
- 1; i
>= 0; i
--) {
819 struct pipe_resource
*tex
;
821 /* TODO: something a lot less ugly */
824 templ
.width0
= width
;
825 templ
.height0
= height
;
829 templ
.width0
= width
/ 2;
830 templ
.height0
= height
/ 2;
831 templ
.format
= (num_handles
== 2) ?
832 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
833 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
836 templ
.width0
= width
/ 2;
837 templ
.height0
= height
/ 2;
838 templ
.format
= PIPE_FORMAT_R8_UNORM
;
841 unreachable("too many planes!");
844 tex
= pscreen
->resource_from_handle(pscreen
,
845 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
847 pipe_resource_reference(&img
->texture
, NULL
);
852 tex
->next
= img
->texture
;
858 img
->dri_format
= format
;
860 img
->loader_private
= loaderPrivate
;
866 dri2_create_image_from_name(__DRIscreen
*_screen
,
867 int width
, int height
, int format
,
868 int name
, int pitch
, void *loaderPrivate
)
870 struct winsys_handle whandle
;
873 memset(&whandle
, 0, sizeof(whandle
));
874 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
875 whandle
.handle
= name
;
876 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
878 pf
= dri2_format_to_pipe_format (format
);
879 if (pf
== PIPE_FORMAT_NONE
)
882 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
884 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
885 1, &whandle
, loaderPrivate
);
889 dri2_create_image_from_fd(__DRIscreen
*_screen
,
890 int width
, int height
, int fourcc
,
891 int *fds
, int num_fds
, int *strides
,
892 int *offsets
, unsigned *error
,
893 int *dri_components
, void *loaderPrivate
)
895 struct winsys_handle whandles
[3];
897 __DRIimage
*img
= NULL
;
898 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
899 int expected_num_fds
, i
;
902 case __DRI_IMAGE_FOURCC_YUV420
:
903 case __DRI_IMAGE_FOURCC_YVU420
:
904 expected_num_fds
= 3;
906 case __DRI_IMAGE_FOURCC_NV12
:
907 expected_num_fds
= 2;
910 expected_num_fds
= 1;
914 if (num_fds
!= expected_num_fds
) {
915 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
919 format
= convert_fourcc(fourcc
, dri_components
);
921 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
925 memset(whandles
, 0, sizeof(whandles
));
927 for (i
= 0; i
< num_fds
; i
++) {
929 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
933 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
934 whandles
[i
].handle
= (unsigned)fds
[i
];
935 whandles
[i
].stride
= (unsigned)strides
[i
];
936 whandles
[i
].offset
= (unsigned)offsets
[i
];
937 whandles
[i
].modifier
= DRM_FORMAT_MOD_INVALID
;
940 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
941 /* convert to YUV420 by swapping 2nd and 3rd planes: */
942 struct winsys_handle tmp
= whandles
[1];
943 whandles
[1] = whandles
[2];
945 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
948 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
949 num_fds
, whandles
, loaderPrivate
);
951 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
961 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
962 int renderbuffer
, void *loaderPrivate
)
964 struct dri_context
*ctx
= dri_context(context
);
966 if (!ctx
->st
->get_resource_for_egl_image
)
974 dri2_create_image(__DRIscreen
*_screen
,
975 int width
, int height
, int format
,
976 unsigned int use
, void *loaderPrivate
)
978 struct dri_screen
*screen
= dri_screen(_screen
);
980 struct pipe_resource templ
;
984 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
985 if (use
& __DRI_IMAGE_USE_SCANOUT
)
986 tex_usage
|= PIPE_BIND_SCANOUT
;
987 if (use
& __DRI_IMAGE_USE_SHARE
)
988 tex_usage
|= PIPE_BIND_SHARED
;
989 if (use
& __DRI_IMAGE_USE_LINEAR
)
990 tex_usage
|= PIPE_BIND_LINEAR
;
991 if (use
& __DRI_IMAGE_USE_CURSOR
) {
992 if (width
!= 64 || height
!= 64)
994 tex_usage
|= PIPE_BIND_CURSOR
;
997 pf
= dri2_format_to_pipe_format (format
);
998 if (pf
== PIPE_FORMAT_NONE
)
1001 img
= CALLOC_STRUCT(__DRIimageRec
);
1005 memset(&templ
, 0, sizeof(templ
));
1006 templ
.bind
= tex_usage
;
1008 templ
.target
= PIPE_TEXTURE_2D
;
1009 templ
.last_level
= 0;
1010 templ
.width0
= width
;
1011 templ
.height0
= height
;
1013 templ
.array_size
= 1;
1015 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1016 if (!img
->texture
) {
1023 img
->dri_format
= format
;
1024 img
->dri_components
= 0;
1027 img
->loader_private
= loaderPrivate
;
1032 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1034 struct winsys_handle whandle
;
1037 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1038 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1040 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1042 memset(&whandle
, 0, sizeof(whandle
));
1045 case __DRI_IMAGE_ATTRIB_STRIDE
:
1046 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1047 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1048 NULL
, image
->texture
, &whandle
, usage
);
1049 *value
= whandle
.stride
;
1051 case __DRI_IMAGE_ATTRIB_HANDLE
:
1052 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1053 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1054 NULL
, image
->texture
, &whandle
, usage
);
1055 *value
= whandle
.handle
;
1057 case __DRI_IMAGE_ATTRIB_NAME
:
1058 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1059 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1060 NULL
, image
->texture
, &whandle
, usage
);
1061 *value
= whandle
.handle
;
1063 case __DRI_IMAGE_ATTRIB_FD
:
1064 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1065 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1066 NULL
, image
->texture
, &whandle
, usage
))
1069 *value
= whandle
.handle
;
1071 case __DRI_IMAGE_ATTRIB_FORMAT
:
1072 *value
= image
->dri_format
;
1074 case __DRI_IMAGE_ATTRIB_WIDTH
:
1075 *value
= image
->texture
->width0
;
1077 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1078 *value
= image
->texture
->height0
;
1080 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1081 if (image
->dri_components
== 0)
1083 *value
= image
->dri_components
;
1085 case __DRI_IMAGE_ATTRIB_FOURCC
:
1086 *value
= convert_to_fourcc(image
->dri_format
);
1088 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1091 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1092 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1093 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1094 NULL
, image
->texture
, &whandle
, usage
);
1095 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1097 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1098 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1099 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1100 NULL
, image
->texture
, &whandle
, usage
);
1101 *value
= whandle
.modifier
& 0xffffffff;
1109 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1113 img
= CALLOC_STRUCT(__DRIimageRec
);
1117 img
->texture
= NULL
;
1118 pipe_resource_reference(&img
->texture
, image
->texture
);
1119 img
->level
= image
->level
;
1120 img
->layer
= image
->layer
;
1121 img
->dri_format
= image
->dri_format
;
1122 /* This should be 0 for sub images, but dup is also used for base images. */
1123 img
->dri_components
= image
->dri_components
;
1124 img
->loader_private
= loaderPrivate
;
1130 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1133 * Gallium drivers are bad at adding usages to the resources
1134 * once opened again in another process, which is the main use
1135 * case for this, so we have to lie.
1144 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1145 int *names
, int num_names
, int *strides
, int *offsets
,
1146 void *loaderPrivate
)
1150 struct winsys_handle whandle
;
1155 format
= convert_fourcc(format
, &dri_components
);
1159 memset(&whandle
, 0, sizeof(whandle
));
1160 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1161 whandle
.handle
= names
[0];
1162 whandle
.stride
= strides
[0];
1163 whandle
.offset
= offsets
[0];
1164 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1166 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1167 1, &whandle
, loaderPrivate
);
1171 img
->dri_components
= dri_components
;
1176 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1183 if (image
->dri_components
== 0)
1186 img
= dri2_dup_image(image
, loaderPrivate
);
1190 if (img
->texture
->screen
->resource_changed
)
1191 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1194 /* set this to 0 for sub images. */
1195 img
->dri_components
= 0;
1200 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1201 int depth
, int level
, unsigned *error
,
1202 void *loaderPrivate
)
1205 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1206 struct gl_texture_object
*obj
;
1207 struct pipe_resource
*tex
;
1210 obj
= _mesa_lookup_texture(ctx
, texture
);
1211 if (!obj
|| obj
->Target
!= target
) {
1212 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1216 tex
= st_get_texobj_resource(obj
);
1218 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1222 if (target
== GL_TEXTURE_CUBE_MAP
)
1225 _mesa_test_texobj_completeness(ctx
, obj
);
1226 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1227 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1231 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1232 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1236 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1237 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1241 img
= CALLOC_STRUCT(__DRIimageRec
);
1243 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1249 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1251 img
->loader_private
= loaderPrivate
;
1253 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1254 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1259 pipe_resource_reference(&img
->texture
, tex
);
1261 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1266 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1267 int *fds
, int num_fds
, int *strides
, int *offsets
,
1268 void *loaderPrivate
)
1273 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1274 fds
, num_fds
, strides
, offsets
, NULL
,
1275 &dri_components
, loaderPrivate
);
1279 img
->dri_components
= dri_components
;
1284 dri2_from_dma_bufs(__DRIscreen
*screen
,
1285 int width
, int height
, int fourcc
,
1286 int *fds
, int num_fds
,
1287 int *strides
, int *offsets
,
1288 enum __DRIYUVColorSpace yuv_color_space
,
1289 enum __DRISampleRange sample_range
,
1290 enum __DRIChromaSiting horizontal_siting
,
1291 enum __DRIChromaSiting vertical_siting
,
1293 void *loaderPrivate
)
1298 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1299 fds
, num_fds
, strides
, offsets
, error
,
1300 &dri_components
, loaderPrivate
);
1304 img
->yuv_color_space
= yuv_color_space
;
1305 img
->sample_range
= sample_range
;
1306 img
->horizontal_siting
= horizontal_siting
;
1307 img
->vertical_siting
= vertical_siting
;
1308 img
->dri_components
= dri_components
;
1310 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1315 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1316 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1317 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1320 struct dri_context
*ctx
= dri_context(context
);
1321 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1322 struct pipe_screen
*screen
;
1323 struct pipe_fence_handle
*fence
;
1324 struct pipe_blit_info blit
;
1329 memset(&blit
, 0, sizeof(blit
));
1330 blit
.dst
.resource
= dst
->texture
;
1331 blit
.dst
.box
.x
= dstx0
;
1332 blit
.dst
.box
.y
= dsty0
;
1333 blit
.dst
.box
.width
= dstwidth
;
1334 blit
.dst
.box
.height
= dstheight
;
1335 blit
.dst
.box
.depth
= 1;
1336 blit
.dst
.format
= dst
->texture
->format
;
1337 blit
.src
.resource
= src
->texture
;
1338 blit
.src
.box
.x
= srcx0
;
1339 blit
.src
.box
.y
= srcy0
;
1340 blit
.src
.box
.width
= srcwidth
;
1341 blit
.src
.box
.height
= srcheight
;
1342 blit
.src
.box
.depth
= 1;
1343 blit
.src
.format
= src
->texture
->format
;
1344 blit
.mask
= PIPE_MASK_RGBA
;
1345 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1347 pipe
->blit(pipe
, &blit
);
1349 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1350 pipe
->flush_resource(pipe
, dst
->texture
);
1351 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1352 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1353 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1354 pipe
->flush_resource(pipe
, dst
->texture
);
1355 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1356 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1357 screen
->fence_reference(screen
, &fence
, NULL
);
1362 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1363 int x0
, int y0
, int width
, int height
,
1364 unsigned int flags
, int *stride
, void **data
)
1366 struct dri_context
*ctx
= dri_context(context
);
1367 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1368 enum pipe_transfer_usage pipe_access
= 0;
1369 struct pipe_transfer
*trans
;
1372 if (!image
|| !data
|| *data
)
1375 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1376 pipe_access
|= PIPE_TRANSFER_READ
;
1377 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1378 pipe_access
|= PIPE_TRANSFER_WRITE
;
1380 map
= pipe_transfer_map(pipe
, image
->texture
,
1381 0, 0, pipe_access
, x0
, y0
, width
, height
,
1385 *stride
= trans
->stride
;
1392 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1394 struct dri_context
*ctx
= dri_context(context
);
1395 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1397 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1401 dri2_destroy_image(__DRIimage
*img
)
1403 pipe_resource_reference(&img
->texture
, NULL
);
1408 dri2_get_capabilities(__DRIscreen
*_screen
)
1410 struct dri_screen
*screen
= dri_screen(_screen
);
1412 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1415 /* The extension is modified during runtime if DRI_PRIME is detected */
1416 static __DRIimageExtension dri2ImageExtension
= {
1417 .base
= { __DRI_IMAGE
, 12 },
1419 .createImageFromName
= dri2_create_image_from_name
,
1420 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1421 .destroyImage
= dri2_destroy_image
,
1422 .createImage
= dri2_create_image
,
1423 .queryImage
= dri2_query_image
,
1424 .dupImage
= dri2_dup_image
,
1425 .validateUsage
= dri2_validate_usage
,
1426 .createImageFromNames
= dri2_from_names
,
1427 .fromPlanar
= dri2_from_planar
,
1428 .createImageFromTexture
= dri2_create_from_texture
,
1429 .createImageFromFds
= NULL
,
1430 .createImageFromDmaBufs
= NULL
,
1431 .blitImage
= dri2_blit_image
,
1432 .getCapabilities
= dri2_get_capabilities
,
1433 .mapImage
= dri2_map_image
,
1434 .unmapImage
= dri2_unmap_image
,
1437 static const __DRIrobustnessExtension dri2Robustness
= {
1438 .base
= { __DRI2_ROBUSTNESS
, 1 }
1442 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1443 struct mesa_glinterop_device_info
*out
)
1445 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1447 /* There is no version 0, thus we do not support it */
1448 if (out
->version
== 0)
1449 return MESA_GLINTEROP_INVALID_VERSION
;
1451 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1452 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1453 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1454 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1456 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1457 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1459 /* Instruct the caller that we support up-to version one of the interface */
1462 return MESA_GLINTEROP_SUCCESS
;
1466 dri2_interop_export_object(__DRIcontext
*_ctx
,
1467 struct mesa_glinterop_export_in
*in
,
1468 struct mesa_glinterop_export_out
*out
)
1470 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1471 struct pipe_screen
*screen
= st
->pipe
->screen
;
1472 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1473 struct pipe_resource
*res
= NULL
;
1474 struct winsys_handle whandle
;
1475 unsigned target
, usage
;
1478 /* There is no version 0, thus we do not support it */
1479 if (in
->version
== 0 || out
->version
== 0)
1480 return MESA_GLINTEROP_INVALID_VERSION
;
1482 /* Validate the target. */
1483 switch (in
->target
) {
1484 case GL_TEXTURE_BUFFER
:
1488 case GL_TEXTURE_RECTANGLE
:
1489 case GL_TEXTURE_1D_ARRAY
:
1490 case GL_TEXTURE_2D_ARRAY
:
1491 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1492 case GL_TEXTURE_CUBE_MAP
:
1493 case GL_TEXTURE_2D_MULTISAMPLE
:
1494 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1495 case GL_TEXTURE_EXTERNAL_OES
:
1496 case GL_RENDERBUFFER
:
1497 case GL_ARRAY_BUFFER
:
1498 target
= in
->target
;
1500 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1501 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1502 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1503 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1504 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1505 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1506 target
= GL_TEXTURE_CUBE_MAP
;
1509 return MESA_GLINTEROP_INVALID_TARGET
;
1512 /* Validate the simple case of miplevel. */
1513 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1515 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1517 /* Validate the OpenGL object and get pipe_resource. */
1518 mtx_lock(&ctx
->Shared
->Mutex
);
1520 if (target
== GL_ARRAY_BUFFER
) {
1523 * The error checking is based on the documentation of
1524 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1526 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1528 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1529 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1530 * a GL buffer object but does not have an existing data store or
1531 * the size of the buffer is 0."
1533 if (!buf
|| buf
->Size
== 0) {
1534 mtx_unlock(&ctx
->Shared
->Mutex
);
1535 return MESA_GLINTEROP_INVALID_OBJECT
;
1538 res
= st_buffer_object(buf
)->buffer
;
1540 /* this shouldn't happen */
1541 mtx_unlock(&ctx
->Shared
->Mutex
);
1542 return MESA_GLINTEROP_INVALID_OBJECT
;
1545 out
->buf_offset
= 0;
1546 out
->buf_size
= buf
->Size
;
1548 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1549 } else if (target
== GL_RENDERBUFFER
) {
1552 * The error checking is based on the documentation of
1553 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1555 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1557 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1558 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1559 * object or if the width or height of renderbuffer is zero."
1561 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1562 mtx_unlock(&ctx
->Shared
->Mutex
);
1563 return MESA_GLINTEROP_INVALID_OBJECT
;
1566 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1567 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1568 * renderbuffer object."
1570 if (rb
->NumSamples
> 1) {
1571 mtx_unlock(&ctx
->Shared
->Mutex
);
1572 return MESA_GLINTEROP_INVALID_OPERATION
;
1575 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1576 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1577 * required by the OpenCL implementation on the device."
1579 res
= st_renderbuffer(rb
)->texture
;
1581 mtx_unlock(&ctx
->Shared
->Mutex
);
1582 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1585 out
->internal_format
= rb
->InternalFormat
;
1586 out
->view_minlevel
= 0;
1587 out
->view_numlevels
= 1;
1588 out
->view_minlayer
= 0;
1589 out
->view_numlayers
= 1;
1593 * The error checking is based on the documentation of
1594 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1596 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1599 _mesa_test_texobj_completeness(ctx
, obj
);
1601 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1602 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1603 * type matches texture_target, if the specified miplevel of texture
1604 * is not defined, or if the width or height of the specified
1605 * miplevel is zero or if the GL texture object is incomplete."
1608 obj
->Target
!= target
||
1609 !obj
->_BaseComplete
||
1610 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1611 mtx_unlock(&ctx
->Shared
->Mutex
);
1612 return MESA_GLINTEROP_INVALID_OBJECT
;
1615 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1616 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1617 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1618 * implementations); or greater than the value of q (for both OpenGL
1619 * and OpenGL ES). levelbase and q are defined for the texture in
1620 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1621 * specification and section 3.7.10 of the OpenGL ES 2.0."
1623 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1624 mtx_unlock(&ctx
->Shared
->Mutex
);
1625 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1628 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1629 mtx_unlock(&ctx
->Shared
->Mutex
);
1630 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1633 res
= st_get_texobj_resource(obj
);
1635 /* Incomplete texture buffer object? This shouldn't really occur. */
1636 mtx_unlock(&ctx
->Shared
->Mutex
);
1637 return MESA_GLINTEROP_INVALID_OBJECT
;
1640 if (target
== GL_TEXTURE_BUFFER
) {
1641 out
->internal_format
= obj
->BufferObjectFormat
;
1642 out
->buf_offset
= obj
->BufferOffset
;
1643 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1646 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1648 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1649 out
->view_minlevel
= obj
->MinLevel
;
1650 out
->view_numlevels
= obj
->NumLevels
;
1651 out
->view_minlayer
= obj
->MinLayer
;
1652 out
->view_numlayers
= obj
->NumLayers
;
1656 /* Get the handle. */
1657 switch (in
->access
) {
1658 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1659 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1661 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1662 usage
= PIPE_HANDLE_USAGE_READ
;
1664 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1665 usage
= PIPE_HANDLE_USAGE_WRITE
;
1671 memset(&whandle
, 0, sizeof(whandle
));
1672 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1674 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1676 mtx_unlock(&ctx
->Shared
->Mutex
);
1679 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1681 out
->dmabuf_fd
= whandle
.handle
;
1682 out
->out_driver_data_written
= 0;
1684 if (res
->target
== PIPE_BUFFER
)
1685 out
->buf_offset
+= whandle
.offset
;
1687 /* Instruct the caller that we support up-to version one of the interface */
1691 return MESA_GLINTEROP_SUCCESS
;
1694 static const __DRI2interopExtension dri2InteropExtension
= {
1695 .base
= { __DRI2_INTEROP
, 1 },
1696 .query_device_info
= dri2_interop_query_device_info
,
1697 .export_object
= dri2_interop_export_object
1701 * \brief the DRI2ConfigQueryExtension configQueryb method
1704 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1707 struct dri_screen
*screen
= dri_screen(sPriv
);
1709 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
1710 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1712 *val
= driQueryOptionb(&screen
->optionCache
, var
);
1718 * \brief the DRI2ConfigQueryExtension configQueryi method
1721 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1723 struct dri_screen
*screen
= dri_screen(sPriv
);
1725 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
1726 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
1727 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
1729 *val
= driQueryOptioni(&screen
->optionCache
, var
);
1735 * \brief the DRI2ConfigQueryExtension configQueryf method
1738 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
1740 struct dri_screen
*screen
= dri_screen(sPriv
);
1742 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
1743 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
1745 *val
= driQueryOptionf(&screen
->optionCache
, var
);
1751 * \brief the DRI2ConfigQueryExtension struct.
1753 * We first query the driver option cache. Then the dri2 option cache.
1755 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
1756 .base
= { __DRI2_CONFIG_QUERY
, 1 },
1758 .configQueryb
= dri2GalliumConfigQueryb
,
1759 .configQueryi
= dri2GalliumConfigQueryi
,
1760 .configQueryf
= dri2GalliumConfigQueryf
,
1764 * Backend function init_screen.
1767 static const __DRIextension
*dri_screen_extensions
[] = {
1768 &driTexBufferExtension
.base
,
1769 &dri2FlushExtension
.base
,
1770 &dri2ImageExtension
.base
,
1771 &dri2RendererQueryExtension
.base
,
1772 &dri2GalliumConfigQueryExtension
.base
,
1773 &dri2ThrottleExtension
.base
,
1774 &dri2FenceExtension
.base
,
1775 &dri2InteropExtension
.base
,
1779 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1780 &driTexBufferExtension
.base
,
1781 &dri2FlushExtension
.base
,
1782 &dri2ImageExtension
.base
,
1783 &dri2RendererQueryExtension
.base
,
1784 &dri2ConfigQueryExtension
.base
,
1785 &dri2ThrottleExtension
.base
,
1786 &dri2FenceExtension
.base
,
1787 &dri2InteropExtension
.base
,
1788 &dri2Robustness
.base
,
1793 * This is the driver specific part of the createNewScreen entry point.
1795 * Returns the struct gl_config supported by this driver.
1797 static const __DRIconfig
**
1798 dri2_init_screen(__DRIscreen
* sPriv
)
1800 const __DRIconfig
**configs
;
1801 struct dri_screen
*screen
;
1802 struct pipe_screen
*pscreen
= NULL
;
1803 const struct drm_conf_ret
*throttle_ret
;
1804 const struct drm_conf_ret
*dmabuf_ret
;
1807 screen
= CALLOC_STRUCT(dri_screen
);
1811 screen
->sPriv
= sPriv
;
1812 screen
->fd
= sPriv
->fd
;
1813 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
1815 sPriv
->driverPrivate
= (void *)screen
;
1817 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1820 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1821 pscreen
= pipe_loader_create_screen(screen
->dev
);
1826 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1827 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1829 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1830 screen
->throttling_enabled
= TRUE
;
1831 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1834 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1837 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1838 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1839 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1840 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1844 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1845 sPriv
->extensions
= dri_robust_screen_extensions
;
1846 screen
->has_reset_status_query
= true;
1849 sPriv
->extensions
= dri_screen_extensions
;
1851 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1853 goto destroy_screen
;
1855 screen
->can_share_buffer
= true;
1856 screen
->auto_fake_front
= dri_with_format(sPriv
);
1857 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1858 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1863 dri_destroy_screen_helper(screen
);
1867 pipe_loader_release(&screen
->dev
, 1);
1877 * This is the driver specific part of the createNewScreen entry point.
1879 * Returns the struct gl_config supported by this driver.
1881 static const __DRIconfig
**
1882 dri_kms_init_screen(__DRIscreen
* sPriv
)
1884 #if defined(GALLIUM_SOFTPIPE)
1885 const __DRIconfig
**configs
;
1886 struct dri_screen
*screen
;
1887 struct pipe_screen
*pscreen
= NULL
;
1891 screen
= CALLOC_STRUCT(dri_screen
);
1895 screen
->sPriv
= sPriv
;
1896 screen
->fd
= sPriv
->fd
;
1898 sPriv
->driverPrivate
= (void *)screen
;
1900 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1903 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1904 pscreen
= pipe_loader_create_screen(screen
->dev
);
1909 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1910 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1911 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1912 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1915 sPriv
->extensions
= dri_screen_extensions
;
1917 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1919 goto destroy_screen
;
1921 screen
->can_share_buffer
= false;
1922 screen
->auto_fake_front
= dri_with_format(sPriv
);
1923 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1924 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1929 dri_destroy_screen_helper(screen
);
1933 pipe_loader_release(&screen
->dev
, 1);
1939 #endif // GALLIUM_SOFTPIPE
1944 dri2_create_buffer(__DRIscreen
* sPriv
,
1945 __DRIdrawable
* dPriv
,
1946 const struct gl_config
* visual
, boolean isPixmap
)
1948 struct dri_drawable
*drawable
= NULL
;
1950 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1953 drawable
= dPriv
->driverPrivate
;
1955 drawable
->allocate_textures
= dri2_allocate_textures
;
1956 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1957 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1963 * DRI driver virtual function table.
1965 * DRI versions differ in their implementation of init_screen and swap_buffers.
1967 const struct __DriverAPIRec galliumdrm_driver_api
= {
1968 .InitScreen
= dri2_init_screen
,
1969 .DestroyScreen
= dri_destroy_screen
,
1970 .CreateContext
= dri_create_context
,
1971 .DestroyContext
= dri_destroy_context
,
1972 .CreateBuffer
= dri2_create_buffer
,
1973 .DestroyBuffer
= dri_destroy_buffer
,
1974 .MakeCurrent
= dri_make_current
,
1975 .UnbindContext
= dri_unbind_context
,
1977 .AllocateBuffer
= dri2_allocate_buffer
,
1978 .ReleaseBuffer
= dri2_release_buffer
,
1982 * DRI driver virtual function table.
1984 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1985 * hook. The latter is used to explicitly initialise the kms_swrast driver
1986 * rather than selecting the approapriate driver as suggested by the loader.
1988 const struct __DriverAPIRec dri_kms_driver_api
= {
1989 .InitScreen
= dri_kms_init_screen
,
1990 .DestroyScreen
= dri_destroy_screen
,
1991 .CreateContext
= dri_create_context
,
1992 .DestroyContext
= dri_destroy_context
,
1993 .CreateBuffer
= dri2_create_buffer
,
1994 .DestroyBuffer
= dri_destroy_buffer
,
1995 .MakeCurrent
= dri_make_current
,
1996 .UnbindContext
= dri_unbind_context
,
1998 .AllocateBuffer
= dri2_allocate_buffer
,
1999 .ReleaseBuffer
= dri2_release_buffer
,
2002 /* This is the table of extensions that the loader will dlsym() for. */
2003 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2004 &driCoreExtension
.base
,
2005 &driImageDriverExtension
.base
,
2006 &driDRI2Extension
.base
,
2007 &gallium_config_options
.base
,
2008 &dri2FenceExtension
.base
,
2012 /* vim: set sw=3 ts=8 sts=3 expandtab: */