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
:
1097 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1101 img
= CALLOC_STRUCT(__DRIimageRec
);
1105 img
->texture
= NULL
;
1106 pipe_resource_reference(&img
->texture
, image
->texture
);
1107 img
->level
= image
->level
;
1108 img
->layer
= image
->layer
;
1109 img
->dri_format
= image
->dri_format
;
1110 /* This should be 0 for sub images, but dup is also used for base images. */
1111 img
->dri_components
= image
->dri_components
;
1112 img
->loader_private
= loaderPrivate
;
1118 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1121 * Gallium drivers are bad at adding usages to the resources
1122 * once opened again in another process, which is the main use
1123 * case for this, so we have to lie.
1132 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1133 int *names
, int num_names
, int *strides
, int *offsets
,
1134 void *loaderPrivate
)
1138 struct winsys_handle whandle
;
1143 format
= convert_fourcc(format
, &dri_components
);
1147 memset(&whandle
, 0, sizeof(whandle
));
1148 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1149 whandle
.handle
= names
[0];
1150 whandle
.stride
= strides
[0];
1151 whandle
.offset
= offsets
[0];
1152 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1154 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1155 1, &whandle
, loaderPrivate
);
1159 img
->dri_components
= dri_components
;
1164 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1171 if (image
->dri_components
== 0)
1174 img
= dri2_dup_image(image
, loaderPrivate
);
1178 if (img
->texture
->screen
->resource_changed
)
1179 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1182 /* set this to 0 for sub images. */
1183 img
->dri_components
= 0;
1188 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1189 int depth
, int level
, unsigned *error
,
1190 void *loaderPrivate
)
1193 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1194 struct gl_texture_object
*obj
;
1195 struct pipe_resource
*tex
;
1198 obj
= _mesa_lookup_texture(ctx
, texture
);
1199 if (!obj
|| obj
->Target
!= target
) {
1200 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1204 tex
= st_get_texobj_resource(obj
);
1206 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1210 if (target
== GL_TEXTURE_CUBE_MAP
)
1213 _mesa_test_texobj_completeness(ctx
, obj
);
1214 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1215 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1219 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1220 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1224 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1225 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1229 img
= CALLOC_STRUCT(__DRIimageRec
);
1231 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1237 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1239 img
->loader_private
= loaderPrivate
;
1241 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1242 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1247 pipe_resource_reference(&img
->texture
, tex
);
1249 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1254 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1255 int *fds
, int num_fds
, int *strides
, int *offsets
,
1256 void *loaderPrivate
)
1261 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1262 fds
, num_fds
, strides
, offsets
, NULL
,
1263 &dri_components
, loaderPrivate
);
1267 img
->dri_components
= dri_components
;
1272 dri2_from_dma_bufs(__DRIscreen
*screen
,
1273 int width
, int height
, int fourcc
,
1274 int *fds
, int num_fds
,
1275 int *strides
, int *offsets
,
1276 enum __DRIYUVColorSpace yuv_color_space
,
1277 enum __DRISampleRange sample_range
,
1278 enum __DRIChromaSiting horizontal_siting
,
1279 enum __DRIChromaSiting vertical_siting
,
1281 void *loaderPrivate
)
1286 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1287 fds
, num_fds
, strides
, offsets
, error
,
1288 &dri_components
, loaderPrivate
);
1292 img
->yuv_color_space
= yuv_color_space
;
1293 img
->sample_range
= sample_range
;
1294 img
->horizontal_siting
= horizontal_siting
;
1295 img
->vertical_siting
= vertical_siting
;
1296 img
->dri_components
= dri_components
;
1298 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1303 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1304 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1305 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1308 struct dri_context
*ctx
= dri_context(context
);
1309 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1310 struct pipe_screen
*screen
;
1311 struct pipe_fence_handle
*fence
;
1312 struct pipe_blit_info blit
;
1317 memset(&blit
, 0, sizeof(blit
));
1318 blit
.dst
.resource
= dst
->texture
;
1319 blit
.dst
.box
.x
= dstx0
;
1320 blit
.dst
.box
.y
= dsty0
;
1321 blit
.dst
.box
.width
= dstwidth
;
1322 blit
.dst
.box
.height
= dstheight
;
1323 blit
.dst
.box
.depth
= 1;
1324 blit
.dst
.format
= dst
->texture
->format
;
1325 blit
.src
.resource
= src
->texture
;
1326 blit
.src
.box
.x
= srcx0
;
1327 blit
.src
.box
.y
= srcy0
;
1328 blit
.src
.box
.width
= srcwidth
;
1329 blit
.src
.box
.height
= srcheight
;
1330 blit
.src
.box
.depth
= 1;
1331 blit
.src
.format
= src
->texture
->format
;
1332 blit
.mask
= PIPE_MASK_RGBA
;
1333 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1335 pipe
->blit(pipe
, &blit
);
1337 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1338 pipe
->flush_resource(pipe
, dst
->texture
);
1339 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1340 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1341 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1342 pipe
->flush_resource(pipe
, dst
->texture
);
1343 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1344 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1345 screen
->fence_reference(screen
, &fence
, NULL
);
1350 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1351 int x0
, int y0
, int width
, int height
,
1352 unsigned int flags
, int *stride
, void **data
)
1354 struct dri_context
*ctx
= dri_context(context
);
1355 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1356 enum pipe_transfer_usage pipe_access
= 0;
1357 struct pipe_transfer
*trans
;
1360 if (!image
|| !data
|| *data
)
1363 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1364 pipe_access
|= PIPE_TRANSFER_READ
;
1365 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1366 pipe_access
|= PIPE_TRANSFER_WRITE
;
1368 map
= pipe_transfer_map(pipe
, image
->texture
,
1369 0, 0, pipe_access
, x0
, y0
, width
, height
,
1373 *stride
= trans
->stride
;
1380 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1382 struct dri_context
*ctx
= dri_context(context
);
1383 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1385 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1389 dri2_destroy_image(__DRIimage
*img
)
1391 pipe_resource_reference(&img
->texture
, NULL
);
1396 dri2_get_capabilities(__DRIscreen
*_screen
)
1398 struct dri_screen
*screen
= dri_screen(_screen
);
1400 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1403 /* The extension is modified during runtime if DRI_PRIME is detected */
1404 static __DRIimageExtension dri2ImageExtension
= {
1405 .base
= { __DRI_IMAGE
, 12 },
1407 .createImageFromName
= dri2_create_image_from_name
,
1408 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1409 .destroyImage
= dri2_destroy_image
,
1410 .createImage
= dri2_create_image
,
1411 .queryImage
= dri2_query_image
,
1412 .dupImage
= dri2_dup_image
,
1413 .validateUsage
= dri2_validate_usage
,
1414 .createImageFromNames
= dri2_from_names
,
1415 .fromPlanar
= dri2_from_planar
,
1416 .createImageFromTexture
= dri2_create_from_texture
,
1417 .createImageFromFds
= NULL
,
1418 .createImageFromDmaBufs
= NULL
,
1419 .blitImage
= dri2_blit_image
,
1420 .getCapabilities
= dri2_get_capabilities
,
1421 .mapImage
= dri2_map_image
,
1422 .unmapImage
= dri2_unmap_image
,
1425 static const __DRIrobustnessExtension dri2Robustness
= {
1426 .base
= { __DRI2_ROBUSTNESS
, 1 }
1430 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1431 struct mesa_glinterop_device_info
*out
)
1433 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1435 /* There is no version 0, thus we do not support it */
1436 if (out
->version
== 0)
1437 return MESA_GLINTEROP_INVALID_VERSION
;
1439 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1440 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1441 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1442 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1444 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1445 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1447 /* Instruct the caller that we support up-to version one of the interface */
1450 return MESA_GLINTEROP_SUCCESS
;
1454 dri2_interop_export_object(__DRIcontext
*_ctx
,
1455 struct mesa_glinterop_export_in
*in
,
1456 struct mesa_glinterop_export_out
*out
)
1458 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1459 struct pipe_screen
*screen
= st
->pipe
->screen
;
1460 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1461 struct pipe_resource
*res
= NULL
;
1462 struct winsys_handle whandle
;
1463 unsigned target
, usage
;
1466 /* There is no version 0, thus we do not support it */
1467 if (in
->version
== 0 || out
->version
== 0)
1468 return MESA_GLINTEROP_INVALID_VERSION
;
1470 /* Validate the target. */
1471 switch (in
->target
) {
1472 case GL_TEXTURE_BUFFER
:
1476 case GL_TEXTURE_RECTANGLE
:
1477 case GL_TEXTURE_1D_ARRAY
:
1478 case GL_TEXTURE_2D_ARRAY
:
1479 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1480 case GL_TEXTURE_CUBE_MAP
:
1481 case GL_TEXTURE_2D_MULTISAMPLE
:
1482 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1483 case GL_TEXTURE_EXTERNAL_OES
:
1484 case GL_RENDERBUFFER
:
1485 case GL_ARRAY_BUFFER
:
1486 target
= in
->target
;
1488 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1489 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1490 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1491 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1492 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1493 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1494 target
= GL_TEXTURE_CUBE_MAP
;
1497 return MESA_GLINTEROP_INVALID_TARGET
;
1500 /* Validate the simple case of miplevel. */
1501 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1503 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1505 /* Validate the OpenGL object and get pipe_resource. */
1506 mtx_lock(&ctx
->Shared
->Mutex
);
1508 if (target
== GL_ARRAY_BUFFER
) {
1511 * The error checking is based on the documentation of
1512 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1514 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1516 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1517 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1518 * a GL buffer object but does not have an existing data store or
1519 * the size of the buffer is 0."
1521 if (!buf
|| buf
->Size
== 0) {
1522 mtx_unlock(&ctx
->Shared
->Mutex
);
1523 return MESA_GLINTEROP_INVALID_OBJECT
;
1526 res
= st_buffer_object(buf
)->buffer
;
1528 /* this shouldn't happen */
1529 mtx_unlock(&ctx
->Shared
->Mutex
);
1530 return MESA_GLINTEROP_INVALID_OBJECT
;
1533 out
->buf_offset
= 0;
1534 out
->buf_size
= buf
->Size
;
1536 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1537 } else if (target
== GL_RENDERBUFFER
) {
1540 * The error checking is based on the documentation of
1541 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1543 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1545 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1546 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1547 * object or if the width or height of renderbuffer is zero."
1549 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1550 mtx_unlock(&ctx
->Shared
->Mutex
);
1551 return MESA_GLINTEROP_INVALID_OBJECT
;
1554 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1555 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1556 * renderbuffer object."
1558 if (rb
->NumSamples
> 1) {
1559 mtx_unlock(&ctx
->Shared
->Mutex
);
1560 return MESA_GLINTEROP_INVALID_OPERATION
;
1563 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1564 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1565 * required by the OpenCL implementation on the device."
1567 res
= st_renderbuffer(rb
)->texture
;
1569 mtx_unlock(&ctx
->Shared
->Mutex
);
1570 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1573 out
->internal_format
= rb
->InternalFormat
;
1574 out
->view_minlevel
= 0;
1575 out
->view_numlevels
= 1;
1576 out
->view_minlayer
= 0;
1577 out
->view_numlayers
= 1;
1581 * The error checking is based on the documentation of
1582 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1584 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1587 _mesa_test_texobj_completeness(ctx
, obj
);
1589 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1590 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1591 * type matches texture_target, if the specified miplevel of texture
1592 * is not defined, or if the width or height of the specified
1593 * miplevel is zero or if the GL texture object is incomplete."
1596 obj
->Target
!= target
||
1597 !obj
->_BaseComplete
||
1598 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1599 mtx_unlock(&ctx
->Shared
->Mutex
);
1600 return MESA_GLINTEROP_INVALID_OBJECT
;
1603 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1604 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1605 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1606 * implementations); or greater than the value of q (for both OpenGL
1607 * and OpenGL ES). levelbase and q are defined for the texture in
1608 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1609 * specification and section 3.7.10 of the OpenGL ES 2.0."
1611 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1612 mtx_unlock(&ctx
->Shared
->Mutex
);
1613 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1616 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1617 mtx_unlock(&ctx
->Shared
->Mutex
);
1618 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1621 res
= st_get_texobj_resource(obj
);
1623 /* Incomplete texture buffer object? This shouldn't really occur. */
1624 mtx_unlock(&ctx
->Shared
->Mutex
);
1625 return MESA_GLINTEROP_INVALID_OBJECT
;
1628 if (target
== GL_TEXTURE_BUFFER
) {
1629 out
->internal_format
= obj
->BufferObjectFormat
;
1630 out
->buf_offset
= obj
->BufferOffset
;
1631 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1634 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1636 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1637 out
->view_minlevel
= obj
->MinLevel
;
1638 out
->view_numlevels
= obj
->NumLevels
;
1639 out
->view_minlayer
= obj
->MinLayer
;
1640 out
->view_numlayers
= obj
->NumLayers
;
1644 /* Get the handle. */
1645 switch (in
->access
) {
1646 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1647 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1649 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1650 usage
= PIPE_HANDLE_USAGE_READ
;
1652 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1653 usage
= PIPE_HANDLE_USAGE_WRITE
;
1659 memset(&whandle
, 0, sizeof(whandle
));
1660 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1662 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1664 mtx_unlock(&ctx
->Shared
->Mutex
);
1667 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1669 out
->dmabuf_fd
= whandle
.handle
;
1670 out
->out_driver_data_written
= 0;
1672 if (res
->target
== PIPE_BUFFER
)
1673 out
->buf_offset
+= whandle
.offset
;
1675 /* Instruct the caller that we support up-to version one of the interface */
1679 return MESA_GLINTEROP_SUCCESS
;
1682 static const __DRI2interopExtension dri2InteropExtension
= {
1683 .base
= { __DRI2_INTEROP
, 1 },
1684 .query_device_info
= dri2_interop_query_device_info
,
1685 .export_object
= dri2_interop_export_object
1689 * \brief the DRI2ConfigQueryExtension configQueryb method
1692 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1695 struct dri_screen
*screen
= dri_screen(sPriv
);
1697 if (!driCheckOption(&screen
->optionCache
, var
, DRI_BOOL
))
1698 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1700 *val
= driQueryOptionb(&screen
->optionCache
, var
);
1706 * \brief the DRI2ConfigQueryExtension configQueryi method
1709 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1711 struct dri_screen
*screen
= dri_screen(sPriv
);
1713 if (!driCheckOption(&screen
->optionCache
, var
, DRI_INT
) &&
1714 !driCheckOption(&screen
->optionCache
, var
, DRI_ENUM
))
1715 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
1717 *val
= driQueryOptioni(&screen
->optionCache
, var
);
1723 * \brief the DRI2ConfigQueryExtension configQueryf method
1726 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
1728 struct dri_screen
*screen
= dri_screen(sPriv
);
1730 if (!driCheckOption(&screen
->optionCache
, var
, DRI_FLOAT
))
1731 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
1733 *val
= driQueryOptionf(&screen
->optionCache
, var
);
1739 * \brief the DRI2ConfigQueryExtension struct.
1741 * We first query the driver option cache. Then the dri2 option cache.
1743 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
1744 .base
= { __DRI2_CONFIG_QUERY
, 1 },
1746 .configQueryb
= dri2GalliumConfigQueryb
,
1747 .configQueryi
= dri2GalliumConfigQueryi
,
1748 .configQueryf
= dri2GalliumConfigQueryf
,
1752 * Backend function init_screen.
1755 static const __DRIextension
*dri_screen_extensions
[] = {
1756 &driTexBufferExtension
.base
,
1757 &dri2FlushExtension
.base
,
1758 &dri2ImageExtension
.base
,
1759 &dri2RendererQueryExtension
.base
,
1760 &dri2GalliumConfigQueryExtension
.base
,
1761 &dri2ThrottleExtension
.base
,
1762 &dri2FenceExtension
.base
,
1763 &dri2InteropExtension
.base
,
1767 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1768 &driTexBufferExtension
.base
,
1769 &dri2FlushExtension
.base
,
1770 &dri2ImageExtension
.base
,
1771 &dri2RendererQueryExtension
.base
,
1772 &dri2ConfigQueryExtension
.base
,
1773 &dri2ThrottleExtension
.base
,
1774 &dri2FenceExtension
.base
,
1775 &dri2InteropExtension
.base
,
1776 &dri2Robustness
.base
,
1781 * This is the driver specific part of the createNewScreen entry point.
1783 * Returns the struct gl_config supported by this driver.
1785 static const __DRIconfig
**
1786 dri2_init_screen(__DRIscreen
* sPriv
)
1788 const __DRIconfig
**configs
;
1789 struct dri_screen
*screen
;
1790 struct pipe_screen
*pscreen
= NULL
;
1791 const struct drm_conf_ret
*throttle_ret
;
1792 const struct drm_conf_ret
*dmabuf_ret
;
1795 screen
= CALLOC_STRUCT(dri_screen
);
1799 screen
->sPriv
= sPriv
;
1800 screen
->fd
= sPriv
->fd
;
1801 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
1803 sPriv
->driverPrivate
= (void *)screen
;
1805 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1808 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1809 pscreen
= pipe_loader_create_screen(screen
->dev
);
1814 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1815 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1817 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1818 screen
->throttling_enabled
= TRUE
;
1819 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1822 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1825 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1826 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1827 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1828 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1832 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1833 sPriv
->extensions
= dri_robust_screen_extensions
;
1834 screen
->has_reset_status_query
= true;
1837 sPriv
->extensions
= dri_screen_extensions
;
1839 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1841 goto destroy_screen
;
1843 screen
->can_share_buffer
= true;
1844 screen
->auto_fake_front
= dri_with_format(sPriv
);
1845 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1846 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1851 dri_destroy_screen_helper(screen
);
1855 pipe_loader_release(&screen
->dev
, 1);
1865 * This is the driver specific part of the createNewScreen entry point.
1867 * Returns the struct gl_config supported by this driver.
1869 static const __DRIconfig
**
1870 dri_kms_init_screen(__DRIscreen
* sPriv
)
1872 #if defined(GALLIUM_SOFTPIPE)
1873 const __DRIconfig
**configs
;
1874 struct dri_screen
*screen
;
1875 struct pipe_screen
*pscreen
= NULL
;
1879 screen
= CALLOC_STRUCT(dri_screen
);
1883 screen
->sPriv
= sPriv
;
1884 screen
->fd
= sPriv
->fd
;
1886 sPriv
->driverPrivate
= (void *)screen
;
1888 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1891 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1892 pscreen
= pipe_loader_create_screen(screen
->dev
);
1897 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1898 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1899 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1900 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1903 sPriv
->extensions
= dri_screen_extensions
;
1905 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1907 goto destroy_screen
;
1909 screen
->can_share_buffer
= false;
1910 screen
->auto_fake_front
= dri_with_format(sPriv
);
1911 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1912 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1917 dri_destroy_screen_helper(screen
);
1921 pipe_loader_release(&screen
->dev
, 1);
1927 #endif // GALLIUM_SOFTPIPE
1932 dri2_create_buffer(__DRIscreen
* sPriv
,
1933 __DRIdrawable
* dPriv
,
1934 const struct gl_config
* visual
, boolean isPixmap
)
1936 struct dri_drawable
*drawable
= NULL
;
1938 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1941 drawable
= dPriv
->driverPrivate
;
1943 drawable
->allocate_textures
= dri2_allocate_textures
;
1944 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1945 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1951 * DRI driver virtual function table.
1953 * DRI versions differ in their implementation of init_screen and swap_buffers.
1955 const struct __DriverAPIRec galliumdrm_driver_api
= {
1956 .InitScreen
= dri2_init_screen
,
1957 .DestroyScreen
= dri_destroy_screen
,
1958 .CreateContext
= dri_create_context
,
1959 .DestroyContext
= dri_destroy_context
,
1960 .CreateBuffer
= dri2_create_buffer
,
1961 .DestroyBuffer
= dri_destroy_buffer
,
1962 .MakeCurrent
= dri_make_current
,
1963 .UnbindContext
= dri_unbind_context
,
1965 .AllocateBuffer
= dri2_allocate_buffer
,
1966 .ReleaseBuffer
= dri2_release_buffer
,
1970 * DRI driver virtual function table.
1972 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1973 * hook. The latter is used to explicitly initialise the kms_swrast driver
1974 * rather than selecting the approapriate driver as suggested by the loader.
1976 const struct __DriverAPIRec dri_kms_driver_api
= {
1977 .InitScreen
= dri_kms_init_screen
,
1978 .DestroyScreen
= dri_destroy_screen
,
1979 .CreateContext
= dri_create_context
,
1980 .DestroyContext
= dri_destroy_context
,
1981 .CreateBuffer
= dri2_create_buffer
,
1982 .DestroyBuffer
= dri_destroy_buffer
,
1983 .MakeCurrent
= dri_make_current
,
1984 .UnbindContext
= dri_unbind_context
,
1986 .AllocateBuffer
= dri2_allocate_buffer
,
1987 .ReleaseBuffer
= dri2_release_buffer
,
1990 /* This is the table of extensions that the loader will dlsym() for. */
1991 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1992 &driCoreExtension
.base
,
1993 &driImageDriverExtension
.base
,
1994 &driDRI2Extension
.base
,
1995 &gallium_config_options
.base
,
1996 &dri2FenceExtension
.base
,
2000 /* vim: set sw=3 ts=8 sts=3 expandtab: */