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 static int convert_fourcc(int format
, int *dri_components_p
)
59 case __DRI_IMAGE_FOURCC_RGB565
:
60 format
= __DRI_IMAGE_FORMAT_RGB565
;
61 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
63 case __DRI_IMAGE_FOURCC_ARGB8888
:
64 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
65 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
67 case __DRI_IMAGE_FOURCC_XRGB8888
:
68 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
69 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
71 case __DRI_IMAGE_FOURCC_ABGR8888
:
72 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
73 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
75 case __DRI_IMAGE_FOURCC_XBGR8888
:
76 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
77 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
79 case __DRI_IMAGE_FOURCC_R8
:
80 format
= __DRI_IMAGE_FORMAT_R8
;
81 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
83 case __DRI_IMAGE_FOURCC_GR88
:
84 format
= __DRI_IMAGE_FORMAT_GR88
;
85 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
88 * For multi-planar YUV formats, we return the format of the first
89 * plane only. Since there is only one caller which supports multi-
90 * planar YUV it gets to figure out the remaining planes on it's
93 case __DRI_IMAGE_FOURCC_YUV420
:
94 case __DRI_IMAGE_FOURCC_YVU420
:
95 format
= __DRI_IMAGE_FORMAT_R8
;
96 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
98 case __DRI_IMAGE_FOURCC_NV12
:
99 format
= __DRI_IMAGE_FORMAT_R8
;
100 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
105 *dri_components_p
= dri_components
;
109 /* NOTE this probably isn't going to do the right thing for YUV images
110 * (but I think the same can be said for intel_query_image()). I think
111 * only needed for exporting dmabuf's, so I think I won't loose much
114 static int convert_to_fourcc(int format
)
117 case __DRI_IMAGE_FORMAT_RGB565
:
118 format
= __DRI_IMAGE_FOURCC_RGB565
;
120 case __DRI_IMAGE_FORMAT_ARGB8888
:
121 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
123 case __DRI_IMAGE_FORMAT_XRGB8888
:
124 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
126 case __DRI_IMAGE_FORMAT_ABGR8888
:
127 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
129 case __DRI_IMAGE_FORMAT_XBGR8888
:
130 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
132 case __DRI_IMAGE_FORMAT_R8
:
133 format
= __DRI_IMAGE_FOURCC_R8
;
135 case __DRI_IMAGE_FORMAT_GR88
:
136 format
= __DRI_IMAGE_FOURCC_GR88
;
144 static enum pipe_format
dri2_format_to_pipe_format (int format
)
149 case __DRI_IMAGE_FORMAT_RGB565
:
150 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
152 case __DRI_IMAGE_FORMAT_XRGB8888
:
153 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
155 case __DRI_IMAGE_FORMAT_ARGB8888
:
156 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
158 case __DRI_IMAGE_FORMAT_ABGR8888
:
159 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
161 case __DRI_IMAGE_FORMAT_R8
:
162 pf
= PIPE_FORMAT_R8_UNORM
;
164 case __DRI_IMAGE_FORMAT_GR88
:
165 pf
= PIPE_FORMAT_RG88_UNORM
;
168 pf
= PIPE_FORMAT_NONE
;
176 * DRI2 flush extension.
179 dri2_flush_drawable(__DRIdrawable
*dPriv
)
181 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
185 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
187 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
189 dri2InvalidateDrawable(dPriv
);
190 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
191 drawable
->texture_mask
= 0;
193 p_atomic_inc(&drawable
->base
.stamp
);
196 static const __DRI2flushExtension dri2FlushExtension
= {
197 .base
= { __DRI2_FLUSH
, 4 },
199 .flush
= dri2_flush_drawable
,
200 .invalidate
= dri2_invalidate_drawable
,
201 .flush_with_flags
= dri_flush
,
205 * Retrieve __DRIbuffer from the DRI loader.
208 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
209 const enum st_attachment_type
*atts
,
212 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
213 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
215 __DRIbuffer
*buffers
;
217 unsigned attachments
[10];
218 unsigned num_attachments
, i
;
221 with_format
= dri_with_format(drawable
->sPriv
);
225 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
227 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
229 for (i
= 0; i
< *count
; i
++) {
230 enum pipe_format format
;
234 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
235 if (format
== PIPE_FORMAT_NONE
)
239 case ST_ATTACHMENT_FRONT_LEFT
:
243 att
= __DRI_BUFFER_FRONT_LEFT
;
245 case ST_ATTACHMENT_BACK_LEFT
:
246 att
= __DRI_BUFFER_BACK_LEFT
;
248 case ST_ATTACHMENT_FRONT_RIGHT
:
249 att
= __DRI_BUFFER_FRONT_RIGHT
;
251 case ST_ATTACHMENT_BACK_RIGHT
:
252 att
= __DRI_BUFFER_BACK_RIGHT
;
259 * In this switch statement we must support all formats that
260 * may occur as the stvis->color_format.
263 case PIPE_FORMAT_BGRA8888_UNORM
:
266 case PIPE_FORMAT_BGRX8888_UNORM
:
269 case PIPE_FORMAT_B5G6R5_UNORM
:
273 depth
= util_format_get_blocksizebits(format
);
274 assert(!"Unexpected format in dri2_drawable_get_buffers()");
277 attachments
[num_attachments
++] = att
;
279 attachments
[num_attachments
++] = depth
;
284 num_attachments
/= 2;
285 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
286 &dri_drawable
->w
, &dri_drawable
->h
,
287 attachments
, num_attachments
,
288 &num_buffers
, dri_drawable
->loaderPrivate
);
291 buffers
= loader
->getBuffers(dri_drawable
,
292 &dri_drawable
->w
, &dri_drawable
->h
,
293 attachments
, num_attachments
,
294 &num_buffers
, dri_drawable
->loaderPrivate
);
298 *count
= num_buffers
;
304 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
305 struct __DRIimageList
*images
,
306 const enum st_attachment_type
*statts
,
307 unsigned statts_count
)
309 __DRIdrawable
*dPriv
= drawable
->dPriv
;
310 __DRIscreen
*sPriv
= drawable
->sPriv
;
311 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
313 uint32_t buffer_mask
= 0;
316 for (i
= 0; i
< statts_count
; i
++) {
317 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
318 if (pf
== PIPE_FORMAT_NONE
)
322 case ST_ATTACHMENT_FRONT_LEFT
:
323 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
325 case ST_ATTACHMENT_BACK_LEFT
:
326 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
333 case PIPE_FORMAT_B5G6R5_UNORM
:
334 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
336 case PIPE_FORMAT_BGRX8888_UNORM
:
337 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
339 case PIPE_FORMAT_BGRA8888_UNORM
:
340 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
342 case PIPE_FORMAT_RGBA8888_UNORM
:
343 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
346 image_format
= __DRI_IMAGE_FORMAT_NONE
;
351 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
352 (uint32_t *) &drawable
->base
.stamp
,
353 dPriv
->loaderPrivate
, buffer_mask
,
358 dri2_allocate_buffer(__DRIscreen
*sPriv
,
359 unsigned attachment
, unsigned format
,
360 int width
, int height
)
362 struct dri_screen
*screen
= dri_screen(sPriv
);
363 struct dri2_buffer
*buffer
;
364 struct pipe_resource templ
;
367 struct winsys_handle whandle
;
369 switch (attachment
) {
370 case __DRI_BUFFER_FRONT_LEFT
:
371 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
372 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
374 case __DRI_BUFFER_BACK_LEFT
:
375 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
377 case __DRI_BUFFER_DEPTH
:
378 case __DRI_BUFFER_DEPTH_STENCIL
:
379 case __DRI_BUFFER_STENCIL
:
380 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
384 /* because we get the handle and stride */
385 bind
|= PIPE_BIND_SHARED
;
389 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
392 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
395 pf
= PIPE_FORMAT_Z16_UNORM
;
401 buffer
= CALLOC_STRUCT(dri2_buffer
);
405 memset(&templ
, 0, sizeof(templ
));
408 templ
.target
= PIPE_TEXTURE_2D
;
409 templ
.last_level
= 0;
410 templ
.width0
= width
;
411 templ
.height0
= height
;
413 templ
.array_size
= 1;
416 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
417 if (!buffer
->resource
) {
422 memset(&whandle
, 0, sizeof(whandle
));
423 if (screen
->can_share_buffer
)
424 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
426 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
428 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
429 buffer
->resource
, &whandle
,
430 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
432 buffer
->base
.attachment
= attachment
;
433 buffer
->base
.name
= whandle
.handle
;
434 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
435 buffer
->base
.pitch
= whandle
.stride
;
437 return &buffer
->base
;
441 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
443 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
445 pipe_resource_reference(&buffer
->resource
, NULL
);
450 * Backend functions for st_framebuffer interface.
454 dri2_allocate_textures(struct dri_context
*ctx
,
455 struct dri_drawable
*drawable
,
456 const enum st_attachment_type
*statts
,
457 unsigned statts_count
)
459 __DRIscreen
*sPriv
= drawable
->sPriv
;
460 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
461 struct dri_screen
*screen
= dri_screen(sPriv
);
462 struct pipe_resource templ
;
463 boolean alloc_depthstencil
= FALSE
;
465 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
466 /* Image specific variables */
467 struct __DRIimageList images
;
468 /* Dri2 specific variables */
469 __DRIbuffer
*buffers
= NULL
;
470 struct winsys_handle whandle
;
471 unsigned num_buffers
= statts_count
;
473 /* First get the buffers from the loader */
475 if (!dri_image_drawable_get_buffers(drawable
, &images
,
476 statts
, statts_count
))
480 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
481 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
482 drawable
->old_w
== dri_drawable
->w
&&
483 drawable
->old_h
== dri_drawable
->h
&&
484 memcmp(drawable
->old
, buffers
,
485 sizeof(__DRIbuffer
) * num_buffers
) == 0))
489 /* Second clean useless resources*/
491 /* See if we need a depth-stencil buffer. */
492 for (i
= 0; i
< statts_count
; i
++) {
493 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
494 alloc_depthstencil
= TRUE
;
499 /* Delete the resources we won't need. */
500 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
501 /* Don't delete the depth-stencil buffer, we can reuse it. */
502 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
505 /* Flush the texture before unreferencing, so that other clients can
506 * see what the driver has rendered.
508 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
509 struct pipe_context
*pipe
= ctx
->st
->pipe
;
510 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
513 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
516 if (drawable
->stvis
.samples
> 1) {
517 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
520 /* Don't delete MSAA resources for the attachments which are enabled,
521 * we can reuse them. */
522 for (j
= 0; j
< statts_count
; j
++) {
523 if (i
== statts
[j
]) {
530 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
535 /* Third use the buffers retrieved to fill the drawable info */
537 memset(&templ
, 0, sizeof(templ
));
538 templ
.target
= screen
->target
;
539 templ
.last_level
= 0;
541 templ
.array_size
= 1;
544 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
545 struct pipe_resource
**buf
=
546 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
547 struct pipe_resource
*texture
= images
.front
->texture
;
549 dri_drawable
->w
= texture
->width0
;
550 dri_drawable
->h
= texture
->height0
;
552 pipe_resource_reference(buf
, texture
);
555 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
556 struct pipe_resource
**buf
=
557 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
558 struct pipe_resource
*texture
= images
.back
->texture
;
560 dri_drawable
->w
= texture
->width0
;
561 dri_drawable
->h
= texture
->height0
;
563 pipe_resource_reference(buf
, texture
);
566 /* Note: if there is both a back and a front buffer,
567 * then they have the same size.
569 templ
.width0
= dri_drawable
->w
;
570 templ
.height0
= dri_drawable
->h
;
573 memset(&whandle
, 0, sizeof(whandle
));
575 /* Process DRI-provided buffers and get pipe_resources. */
576 for (i
= 0; i
< num_buffers
; i
++) {
577 __DRIbuffer
*buf
= &buffers
[i
];
578 enum st_attachment_type statt
;
579 enum pipe_format format
;
581 switch (buf
->attachment
) {
582 case __DRI_BUFFER_FRONT_LEFT
:
583 if (!screen
->auto_fake_front
) {
584 continue; /* invalid attachment */
587 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
588 statt
= ST_ATTACHMENT_FRONT_LEFT
;
590 case __DRI_BUFFER_BACK_LEFT
:
591 statt
= ST_ATTACHMENT_BACK_LEFT
;
594 continue; /* invalid attachment */
597 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
598 if (format
== PIPE_FORMAT_NONE
)
601 /* dri2_drawable_get_buffers has already filled dri_drawable->w
602 * and dri_drawable->h */
603 templ
.width0
= dri_drawable
->w
;
604 templ
.height0
= dri_drawable
->h
;
605 templ
.format
= format
;
607 whandle
.handle
= buf
->name
;
608 whandle
.stride
= buf
->pitch
;
610 if (screen
->can_share_buffer
)
611 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
613 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
614 drawable
->textures
[statt
] =
615 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
617 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
618 assert(drawable
->textures
[statt
]);
622 /* Allocate private MSAA colorbuffers. */
623 if (drawable
->stvis
.samples
> 1) {
624 for (i
= 0; i
< statts_count
; i
++) {
625 enum st_attachment_type statt
= statts
[i
];
627 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
630 if (drawable
->textures
[statt
]) {
631 templ
.format
= drawable
->textures
[statt
]->format
;
632 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
633 templ
.nr_samples
= drawable
->stvis
.samples
;
635 /* Try to reuse the resource.
636 * (the other resource parameters should be constant)
638 if (!drawable
->msaa_textures
[statt
] ||
639 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
640 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
641 /* Allocate a new one. */
642 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
644 drawable
->msaa_textures
[statt
] =
645 screen
->base
.screen
->resource_create(screen
->base
.screen
,
647 assert(drawable
->msaa_textures
[statt
]);
649 /* If there are any MSAA resources, we should initialize them
650 * such that they contain the same data as the single-sample
651 * resources we just got from the X server.
653 * The reason for this is that the state tracker (and
654 * therefore the app) can access the MSAA resources only.
655 * The single-sample resources are not exposed
656 * to the state tracker.
659 dri_pipe_blit(ctx
->st
->pipe
,
660 drawable
->msaa_textures
[statt
],
661 drawable
->textures
[statt
]);
665 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
670 /* Allocate a private depth-stencil buffer. */
671 if (alloc_depthstencil
) {
672 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
673 struct pipe_resource
**zsbuf
;
674 enum pipe_format format
;
677 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
680 templ
.format
= format
;
683 if (drawable
->stvis
.samples
> 1) {
684 templ
.nr_samples
= drawable
->stvis
.samples
;
685 zsbuf
= &drawable
->msaa_textures
[statt
];
688 templ
.nr_samples
= 0;
689 zsbuf
= &drawable
->textures
[statt
];
692 /* Try to reuse the resource.
693 * (the other resource parameters should be constant)
696 (*zsbuf
)->width0
!= templ
.width0
||
697 (*zsbuf
)->height0
!= templ
.height0
) {
698 /* Allocate a new one. */
699 pipe_resource_reference(zsbuf
, NULL
);
700 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
706 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
707 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
711 /* For DRI2, we may get the same buffers again from the server.
712 * To prevent useless imports of gem names, drawable->old* is used
713 * to bypass the import if we get the same buffers. This doesn't apply
714 * to DRI3/Wayland, users of image.loader, since the buffer is managed
715 * by the client (no import), and the back buffer is going to change
719 drawable
->old_num
= num_buffers
;
720 drawable
->old_w
= dri_drawable
->w
;
721 drawable
->old_h
= dri_drawable
->h
;
722 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
727 dri2_flush_frontbuffer(struct dri_context
*ctx
,
728 struct dri_drawable
*drawable
,
729 enum st_attachment_type statt
)
731 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
732 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
733 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
734 struct pipe_context
*pipe
= ctx
->st
->pipe
;
736 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
739 if (drawable
->stvis
.samples
> 1) {
740 /* Resolve the front buffer. */
741 dri_pipe_blit(ctx
->st
->pipe
,
742 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
743 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
746 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
747 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
750 pipe
->flush(pipe
, NULL
, 0);
753 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
755 else if (loader
->flushFrontBuffer
) {
756 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
761 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
762 struct dri_context
*ctx
,
763 struct pipe_resource
*res
)
769 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
771 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
774 if (!loader
->lookupEGLImage
)
777 img
= loader
->lookupEGLImage(screen
->sPriv
,
778 handle
, screen
->sPriv
->loaderPrivate
);
784 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
785 int width
, int height
, int format
,
786 int num_handles
, struct winsys_handle
*whandle
,
789 struct dri_screen
*screen
= dri_screen(_screen
);
790 struct pipe_screen
*pscreen
= screen
->base
.screen
;
792 struct pipe_resource templ
;
797 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
799 pf
= dri2_format_to_pipe_format (format
);
800 if (pf
== PIPE_FORMAT_NONE
)
803 img
= CALLOC_STRUCT(__DRIimageRec
);
807 memset(&templ
, 0, sizeof(templ
));
808 templ
.bind
= tex_usage
;
809 templ
.target
= screen
->target
;
810 templ
.last_level
= 0;
812 templ
.array_size
= 1;
814 for (i
= num_handles
- 1; i
>= 0; i
--) {
815 struct pipe_resource
*tex
;
817 /* TODO: something a lot less ugly */
820 templ
.width0
= width
;
821 templ
.height0
= height
;
825 templ
.width0
= width
/ 2;
826 templ
.height0
= height
/ 2;
827 templ
.format
= (num_handles
== 2) ?
828 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
829 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
832 templ
.width0
= width
/ 2;
833 templ
.height0
= height
/ 2;
834 templ
.format
= PIPE_FORMAT_R8_UNORM
;
837 unreachable("too many planes!");
840 tex
= pscreen
->resource_from_handle(pscreen
,
841 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
843 pipe_resource_reference(&img
->texture
, NULL
);
848 tex
->next
= img
->texture
;
854 img
->dri_format
= format
;
856 img
->loader_private
= loaderPrivate
;
862 dri2_create_image_from_name(__DRIscreen
*_screen
,
863 int width
, int height
, int format
,
864 int name
, int pitch
, void *loaderPrivate
)
866 struct winsys_handle whandle
;
869 memset(&whandle
, 0, sizeof(whandle
));
870 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
871 whandle
.handle
= name
;
873 pf
= dri2_format_to_pipe_format (format
);
874 if (pf
== PIPE_FORMAT_NONE
)
877 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
879 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
880 1, &whandle
, loaderPrivate
);
884 dri2_create_image_from_fd(__DRIscreen
*_screen
,
885 int width
, int height
, int fourcc
,
886 int *fds
, int num_fds
, int *strides
,
887 int *offsets
, unsigned *error
,
888 int *dri_components
, void *loaderPrivate
)
890 struct winsys_handle whandles
[3];
892 __DRIimage
*img
= NULL
;
893 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
894 int expected_num_fds
, i
;
897 case __DRI_IMAGE_FOURCC_YUV420
:
898 case __DRI_IMAGE_FOURCC_YVU420
:
899 expected_num_fds
= 3;
901 case __DRI_IMAGE_FOURCC_NV12
:
902 expected_num_fds
= 2;
905 expected_num_fds
= 1;
909 if (num_fds
!= expected_num_fds
) {
910 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
914 format
= convert_fourcc(fourcc
, dri_components
);
916 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
920 memset(whandles
, 0, sizeof(whandles
));
922 for (i
= 0; i
< num_fds
; i
++) {
924 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
928 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
929 whandles
[i
].handle
= (unsigned)fds
[i
];
930 whandles
[i
].stride
= (unsigned)strides
[i
];
931 whandles
[i
].offset
= (unsigned)offsets
[i
];
934 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
935 /* convert to YUV420 by swapping 2nd and 3rd planes: */
936 struct winsys_handle tmp
= whandles
[1];
937 whandles
[1] = whandles
[2];
939 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
942 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
943 num_fds
, whandles
, loaderPrivate
);
945 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
955 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
956 int renderbuffer
, void *loaderPrivate
)
958 struct dri_context
*ctx
= dri_context(context
);
960 if (!ctx
->st
->get_resource_for_egl_image
)
968 dri2_create_image(__DRIscreen
*_screen
,
969 int width
, int height
, int format
,
970 unsigned int use
, void *loaderPrivate
)
972 struct dri_screen
*screen
= dri_screen(_screen
);
974 struct pipe_resource templ
;
978 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
979 if (use
& __DRI_IMAGE_USE_SCANOUT
)
980 tex_usage
|= PIPE_BIND_SCANOUT
;
981 if (use
& __DRI_IMAGE_USE_SHARE
)
982 tex_usage
|= PIPE_BIND_SHARED
;
983 if (use
& __DRI_IMAGE_USE_LINEAR
)
984 tex_usage
|= PIPE_BIND_LINEAR
;
985 if (use
& __DRI_IMAGE_USE_CURSOR
) {
986 if (width
!= 64 || height
!= 64)
988 tex_usage
|= PIPE_BIND_CURSOR
;
991 pf
= dri2_format_to_pipe_format (format
);
992 if (pf
== PIPE_FORMAT_NONE
)
995 img
= CALLOC_STRUCT(__DRIimageRec
);
999 memset(&templ
, 0, sizeof(templ
));
1000 templ
.bind
= tex_usage
;
1002 templ
.target
= PIPE_TEXTURE_2D
;
1003 templ
.last_level
= 0;
1004 templ
.width0
= width
;
1005 templ
.height0
= height
;
1007 templ
.array_size
= 1;
1009 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1010 if (!img
->texture
) {
1017 img
->dri_format
= format
;
1018 img
->dri_components
= 0;
1021 img
->loader_private
= loaderPrivate
;
1026 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1028 struct winsys_handle whandle
;
1031 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1032 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1034 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1036 memset(&whandle
, 0, sizeof(whandle
));
1039 case __DRI_IMAGE_ATTRIB_STRIDE
:
1040 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1041 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1042 NULL
, image
->texture
, &whandle
, usage
);
1043 *value
= whandle
.stride
;
1045 case __DRI_IMAGE_ATTRIB_HANDLE
:
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
.handle
;
1051 case __DRI_IMAGE_ATTRIB_NAME
:
1052 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
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_FD
:
1058 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1059 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1060 NULL
, image
->texture
, &whandle
, usage
))
1063 *value
= whandle
.handle
;
1065 case __DRI_IMAGE_ATTRIB_FORMAT
:
1066 *value
= image
->dri_format
;
1068 case __DRI_IMAGE_ATTRIB_WIDTH
:
1069 *value
= image
->texture
->width0
;
1071 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1072 *value
= image
->texture
->height0
;
1074 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1075 if (image
->dri_components
== 0)
1077 *value
= image
->dri_components
;
1079 case __DRI_IMAGE_ATTRIB_FOURCC
:
1080 *value
= convert_to_fourcc(image
->dri_format
);
1082 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1091 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1095 img
= CALLOC_STRUCT(__DRIimageRec
);
1099 img
->texture
= NULL
;
1100 pipe_resource_reference(&img
->texture
, image
->texture
);
1101 img
->level
= image
->level
;
1102 img
->layer
= image
->layer
;
1103 img
->dri_format
= image
->dri_format
;
1104 /* This should be 0 for sub images, but dup is also used for base images. */
1105 img
->dri_components
= image
->dri_components
;
1106 img
->loader_private
= loaderPrivate
;
1112 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1115 * Gallium drivers are bad at adding usages to the resources
1116 * once opened again in another process, which is the main use
1117 * case for this, so we have to lie.
1126 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1127 int *names
, int num_names
, int *strides
, int *offsets
,
1128 void *loaderPrivate
)
1132 struct winsys_handle whandle
;
1137 format
= convert_fourcc(format
, &dri_components
);
1141 memset(&whandle
, 0, sizeof(whandle
));
1142 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1143 whandle
.handle
= names
[0];
1144 whandle
.stride
= strides
[0];
1145 whandle
.offset
= offsets
[0];
1147 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1148 1, &whandle
, loaderPrivate
);
1152 img
->dri_components
= dri_components
;
1157 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1164 if (image
->dri_components
== 0)
1167 img
= dri2_dup_image(image
, loaderPrivate
);
1171 if (img
->texture
->screen
->resource_changed
)
1172 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1175 /* set this to 0 for sub images. */
1176 img
->dri_components
= 0;
1181 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1182 int depth
, int level
, unsigned *error
,
1183 void *loaderPrivate
)
1186 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1187 struct gl_texture_object
*obj
;
1188 struct pipe_resource
*tex
;
1191 obj
= _mesa_lookup_texture(ctx
, texture
);
1192 if (!obj
|| obj
->Target
!= target
) {
1193 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1197 tex
= st_get_texobj_resource(obj
);
1199 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1203 if (target
== GL_TEXTURE_CUBE_MAP
)
1206 _mesa_test_texobj_completeness(ctx
, obj
);
1207 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1208 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1212 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1213 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1217 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1218 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1222 img
= CALLOC_STRUCT(__DRIimageRec
);
1224 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1230 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1232 img
->loader_private
= loaderPrivate
;
1234 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1235 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1240 pipe_resource_reference(&img
->texture
, tex
);
1242 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1247 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1248 int *fds
, int num_fds
, int *strides
, int *offsets
,
1249 void *loaderPrivate
)
1254 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1255 fds
, num_fds
, strides
, offsets
, NULL
,
1256 &dri_components
, loaderPrivate
);
1260 img
->dri_components
= dri_components
;
1265 dri2_from_dma_bufs(__DRIscreen
*screen
,
1266 int width
, int height
, int fourcc
,
1267 int *fds
, int num_fds
,
1268 int *strides
, int *offsets
,
1269 enum __DRIYUVColorSpace yuv_color_space
,
1270 enum __DRISampleRange sample_range
,
1271 enum __DRIChromaSiting horizontal_siting
,
1272 enum __DRIChromaSiting vertical_siting
,
1274 void *loaderPrivate
)
1279 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1280 fds
, num_fds
, strides
, offsets
, error
,
1281 &dri_components
, loaderPrivate
);
1285 img
->yuv_color_space
= yuv_color_space
;
1286 img
->sample_range
= sample_range
;
1287 img
->horizontal_siting
= horizontal_siting
;
1288 img
->vertical_siting
= vertical_siting
;
1289 img
->dri_components
= dri_components
;
1291 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1296 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1297 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1298 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1301 struct dri_context
*ctx
= dri_context(context
);
1302 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1303 struct pipe_screen
*screen
;
1304 struct pipe_fence_handle
*fence
;
1305 struct pipe_blit_info blit
;
1310 memset(&blit
, 0, sizeof(blit
));
1311 blit
.dst
.resource
= dst
->texture
;
1312 blit
.dst
.box
.x
= dstx0
;
1313 blit
.dst
.box
.y
= dsty0
;
1314 blit
.dst
.box
.width
= dstwidth
;
1315 blit
.dst
.box
.height
= dstheight
;
1316 blit
.dst
.box
.depth
= 1;
1317 blit
.dst
.format
= dst
->texture
->format
;
1318 blit
.src
.resource
= src
->texture
;
1319 blit
.src
.box
.x
= srcx0
;
1320 blit
.src
.box
.y
= srcy0
;
1321 blit
.src
.box
.width
= srcwidth
;
1322 blit
.src
.box
.height
= srcheight
;
1323 blit
.src
.box
.depth
= 1;
1324 blit
.src
.format
= src
->texture
->format
;
1325 blit
.mask
= PIPE_MASK_RGBA
;
1326 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1328 pipe
->blit(pipe
, &blit
);
1330 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1331 pipe
->flush_resource(pipe
, dst
->texture
);
1332 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1333 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1334 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1335 pipe
->flush_resource(pipe
, dst
->texture
);
1336 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1337 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1338 screen
->fence_reference(screen
, &fence
, NULL
);
1343 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1344 int x0
, int y0
, int width
, int height
,
1345 unsigned int flags
, int *stride
, void **data
)
1347 struct dri_context
*ctx
= dri_context(context
);
1348 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1349 enum pipe_transfer_usage pipe_access
= 0;
1350 struct pipe_transfer
*trans
;
1353 if (!image
|| !data
|| *data
)
1356 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1357 pipe_access
|= PIPE_TRANSFER_READ
;
1358 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1359 pipe_access
|= PIPE_TRANSFER_WRITE
;
1361 map
= pipe_transfer_map(pipe
, image
->texture
,
1362 0, 0, pipe_access
, x0
, y0
, width
, height
,
1366 *stride
= trans
->stride
;
1373 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1375 struct dri_context
*ctx
= dri_context(context
);
1376 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1378 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1382 dri2_destroy_image(__DRIimage
*img
)
1384 pipe_resource_reference(&img
->texture
, NULL
);
1389 dri2_get_capabilities(__DRIscreen
*_screen
)
1391 struct dri_screen
*screen
= dri_screen(_screen
);
1393 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1396 /* The extension is modified during runtime if DRI_PRIME is detected */
1397 static __DRIimageExtension dri2ImageExtension
= {
1398 .base
= { __DRI_IMAGE
, 12 },
1400 .createImageFromName
= dri2_create_image_from_name
,
1401 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1402 .destroyImage
= dri2_destroy_image
,
1403 .createImage
= dri2_create_image
,
1404 .queryImage
= dri2_query_image
,
1405 .dupImage
= dri2_dup_image
,
1406 .validateUsage
= dri2_validate_usage
,
1407 .createImageFromNames
= dri2_from_names
,
1408 .fromPlanar
= dri2_from_planar
,
1409 .createImageFromTexture
= dri2_create_from_texture
,
1410 .createImageFromFds
= NULL
,
1411 .createImageFromDmaBufs
= NULL
,
1412 .blitImage
= dri2_blit_image
,
1413 .getCapabilities
= dri2_get_capabilities
,
1414 .mapImage
= dri2_map_image
,
1415 .unmapImage
= dri2_unmap_image
,
1418 static const __DRIrobustnessExtension dri2Robustness
= {
1419 .base
= { __DRI2_ROBUSTNESS
, 1 }
1423 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1424 struct mesa_glinterop_device_info
*out
)
1426 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1428 /* There is no version 0, thus we do not support it */
1429 if (out
->version
== 0)
1430 return MESA_GLINTEROP_INVALID_VERSION
;
1432 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1433 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1434 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1435 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1437 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1438 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1440 /* Instruct the caller that we support up-to version one of the interface */
1443 return MESA_GLINTEROP_SUCCESS
;
1447 dri2_interop_export_object(__DRIcontext
*_ctx
,
1448 struct mesa_glinterop_export_in
*in
,
1449 struct mesa_glinterop_export_out
*out
)
1451 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1452 struct pipe_screen
*screen
= st
->pipe
->screen
;
1453 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1454 struct pipe_resource
*res
= NULL
;
1455 struct winsys_handle whandle
;
1456 unsigned target
, usage
;
1459 /* There is no version 0, thus we do not support it */
1460 if (in
->version
== 0 || out
->version
== 0)
1461 return MESA_GLINTEROP_INVALID_VERSION
;
1463 /* Validate the target. */
1464 switch (in
->target
) {
1465 case GL_TEXTURE_BUFFER
:
1469 case GL_TEXTURE_RECTANGLE
:
1470 case GL_TEXTURE_1D_ARRAY
:
1471 case GL_TEXTURE_2D_ARRAY
:
1472 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1473 case GL_TEXTURE_CUBE_MAP
:
1474 case GL_TEXTURE_2D_MULTISAMPLE
:
1475 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1476 case GL_TEXTURE_EXTERNAL_OES
:
1477 case GL_RENDERBUFFER
:
1478 case GL_ARRAY_BUFFER
:
1479 target
= in
->target
;
1481 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1482 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1483 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1484 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1485 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1486 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1487 target
= GL_TEXTURE_CUBE_MAP
;
1490 return MESA_GLINTEROP_INVALID_TARGET
;
1493 /* Validate the simple case of miplevel. */
1494 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1496 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1498 /* Validate the OpenGL object and get pipe_resource. */
1499 mtx_lock(&ctx
->Shared
->Mutex
);
1501 if (target
== GL_ARRAY_BUFFER
) {
1504 * The error checking is based on the documentation of
1505 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1507 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1509 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1510 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1511 * a GL buffer object but does not have an existing data store or
1512 * the size of the buffer is 0."
1514 if (!buf
|| buf
->Size
== 0) {
1515 mtx_unlock(&ctx
->Shared
->Mutex
);
1516 return MESA_GLINTEROP_INVALID_OBJECT
;
1519 res
= st_buffer_object(buf
)->buffer
;
1521 /* this shouldn't happen */
1522 mtx_unlock(&ctx
->Shared
->Mutex
);
1523 return MESA_GLINTEROP_INVALID_OBJECT
;
1526 out
->buf_offset
= 0;
1527 out
->buf_size
= buf
->Size
;
1529 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1530 } else if (target
== GL_RENDERBUFFER
) {
1533 * The error checking is based on the documentation of
1534 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1536 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1538 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1539 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1540 * object or if the width or height of renderbuffer is zero."
1542 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1543 mtx_unlock(&ctx
->Shared
->Mutex
);
1544 return MESA_GLINTEROP_INVALID_OBJECT
;
1547 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1548 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1549 * renderbuffer object."
1551 if (rb
->NumSamples
> 1) {
1552 mtx_unlock(&ctx
->Shared
->Mutex
);
1553 return MESA_GLINTEROP_INVALID_OPERATION
;
1556 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1557 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1558 * required by the OpenCL implementation on the device."
1560 res
= st_renderbuffer(rb
)->texture
;
1562 mtx_unlock(&ctx
->Shared
->Mutex
);
1563 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1566 out
->internal_format
= rb
->InternalFormat
;
1567 out
->view_minlevel
= 0;
1568 out
->view_numlevels
= 1;
1569 out
->view_minlayer
= 0;
1570 out
->view_numlayers
= 1;
1574 * The error checking is based on the documentation of
1575 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1577 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1580 _mesa_test_texobj_completeness(ctx
, obj
);
1582 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1583 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1584 * type matches texture_target, if the specified miplevel of texture
1585 * is not defined, or if the width or height of the specified
1586 * miplevel is zero or if the GL texture object is incomplete."
1589 obj
->Target
!= target
||
1590 !obj
->_BaseComplete
||
1591 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1592 mtx_unlock(&ctx
->Shared
->Mutex
);
1593 return MESA_GLINTEROP_INVALID_OBJECT
;
1596 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1597 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1598 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1599 * implementations); or greater than the value of q (for both OpenGL
1600 * and OpenGL ES). levelbase and q are defined for the texture in
1601 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1602 * specification and section 3.7.10 of the OpenGL ES 2.0."
1604 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1605 mtx_unlock(&ctx
->Shared
->Mutex
);
1606 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1609 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1610 mtx_unlock(&ctx
->Shared
->Mutex
);
1611 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1614 res
= st_get_texobj_resource(obj
);
1616 /* Incomplete texture buffer object? This shouldn't really occur. */
1617 mtx_unlock(&ctx
->Shared
->Mutex
);
1618 return MESA_GLINTEROP_INVALID_OBJECT
;
1621 if (target
== GL_TEXTURE_BUFFER
) {
1622 out
->internal_format
= obj
->BufferObjectFormat
;
1623 out
->buf_offset
= obj
->BufferOffset
;
1624 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1627 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1629 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1630 out
->view_minlevel
= obj
->MinLevel
;
1631 out
->view_numlevels
= obj
->NumLevels
;
1632 out
->view_minlayer
= obj
->MinLayer
;
1633 out
->view_numlayers
= obj
->NumLayers
;
1637 /* Get the handle. */
1638 switch (in
->access
) {
1639 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1640 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1642 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1643 usage
= PIPE_HANDLE_USAGE_READ
;
1645 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1646 usage
= PIPE_HANDLE_USAGE_WRITE
;
1652 memset(&whandle
, 0, sizeof(whandle
));
1653 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1655 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1657 mtx_unlock(&ctx
->Shared
->Mutex
);
1660 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1662 out
->dmabuf_fd
= whandle
.handle
;
1663 out
->out_driver_data_written
= 0;
1665 if (res
->target
== PIPE_BUFFER
)
1666 out
->buf_offset
+= whandle
.offset
;
1668 /* Instruct the caller that we support up-to version one of the interface */
1672 return MESA_GLINTEROP_SUCCESS
;
1675 static const __DRI2interopExtension dri2InteropExtension
= {
1676 .base
= { __DRI2_INTEROP
, 1 },
1677 .query_device_info
= dri2_interop_query_device_info
,
1678 .export_object
= dri2_interop_export_object
1682 * Backend function init_screen.
1685 static const __DRIextension
*dri_screen_extensions
[] = {
1686 &driTexBufferExtension
.base
,
1687 &dri2FlushExtension
.base
,
1688 &dri2ImageExtension
.base
,
1689 &dri2RendererQueryExtension
.base
,
1690 &dri2ConfigQueryExtension
.base
,
1691 &dri2ThrottleExtension
.base
,
1692 &dri2FenceExtension
.base
,
1693 &dri2InteropExtension
.base
,
1697 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1698 &driTexBufferExtension
.base
,
1699 &dri2FlushExtension
.base
,
1700 &dri2ImageExtension
.base
,
1701 &dri2RendererQueryExtension
.base
,
1702 &dri2ConfigQueryExtension
.base
,
1703 &dri2ThrottleExtension
.base
,
1704 &dri2FenceExtension
.base
,
1705 &dri2InteropExtension
.base
,
1706 &dri2Robustness
.base
,
1711 * This is the driver specific part of the createNewScreen entry point.
1713 * Returns the struct gl_config supported by this driver.
1715 static const __DRIconfig
**
1716 dri2_init_screen(__DRIscreen
* sPriv
)
1718 const __DRIconfig
**configs
;
1719 struct dri_screen
*screen
;
1720 struct pipe_screen
*pscreen
= NULL
;
1721 const struct drm_conf_ret
*throttle_ret
;
1722 const struct drm_conf_ret
*dmabuf_ret
;
1725 screen
= CALLOC_STRUCT(dri_screen
);
1729 screen
->sPriv
= sPriv
;
1730 screen
->fd
= sPriv
->fd
;
1731 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
1733 sPriv
->driverPrivate
= (void *)screen
;
1735 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1738 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1739 pscreen
= pipe_loader_create_screen(screen
->dev
);
1744 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1745 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1747 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1748 screen
->throttling_enabled
= TRUE
;
1749 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1752 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1755 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1756 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1757 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1758 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1762 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1763 sPriv
->extensions
= dri_robust_screen_extensions
;
1764 screen
->has_reset_status_query
= true;
1767 sPriv
->extensions
= dri_screen_extensions
;
1769 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1771 goto destroy_screen
;
1773 screen
->can_share_buffer
= true;
1774 screen
->auto_fake_front
= dri_with_format(sPriv
);
1775 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1776 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1781 dri_destroy_screen_helper(screen
);
1785 pipe_loader_release(&screen
->dev
, 1);
1795 * This is the driver specific part of the createNewScreen entry point.
1797 * Returns the struct gl_config supported by this driver.
1799 static const __DRIconfig
**
1800 dri_kms_init_screen(__DRIscreen
* sPriv
)
1802 #if defined(GALLIUM_SOFTPIPE)
1803 const __DRIconfig
**configs
;
1804 struct dri_screen
*screen
;
1805 struct pipe_screen
*pscreen
= NULL
;
1809 screen
= CALLOC_STRUCT(dri_screen
);
1813 screen
->sPriv
= sPriv
;
1814 screen
->fd
= sPriv
->fd
;
1816 sPriv
->driverPrivate
= (void *)screen
;
1818 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
1821 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1822 pscreen
= pipe_loader_create_screen(screen
->dev
);
1827 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1828 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1829 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1830 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1833 sPriv
->extensions
= dri_screen_extensions
;
1835 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1837 goto destroy_screen
;
1839 screen
->can_share_buffer
= false;
1840 screen
->auto_fake_front
= dri_with_format(sPriv
);
1841 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1842 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1847 dri_destroy_screen_helper(screen
);
1851 pipe_loader_release(&screen
->dev
, 1);
1857 #endif // GALLIUM_SOFTPIPE
1862 dri2_create_buffer(__DRIscreen
* sPriv
,
1863 __DRIdrawable
* dPriv
,
1864 const struct gl_config
* visual
, boolean isPixmap
)
1866 struct dri_drawable
*drawable
= NULL
;
1868 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1871 drawable
= dPriv
->driverPrivate
;
1873 drawable
->allocate_textures
= dri2_allocate_textures
;
1874 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1875 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1881 * DRI driver virtual function table.
1883 * DRI versions differ in their implementation of init_screen and swap_buffers.
1885 const struct __DriverAPIRec galliumdrm_driver_api
= {
1886 .InitScreen
= dri2_init_screen
,
1887 .DestroyScreen
= dri_destroy_screen
,
1888 .CreateContext
= dri_create_context
,
1889 .DestroyContext
= dri_destroy_context
,
1890 .CreateBuffer
= dri2_create_buffer
,
1891 .DestroyBuffer
= dri_destroy_buffer
,
1892 .MakeCurrent
= dri_make_current
,
1893 .UnbindContext
= dri_unbind_context
,
1895 .AllocateBuffer
= dri2_allocate_buffer
,
1896 .ReleaseBuffer
= dri2_release_buffer
,
1900 * DRI driver virtual function table.
1902 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1903 * hook. The latter is used to explicitly initialise the kms_swrast driver
1904 * rather than selecting the approapriate driver as suggested by the loader.
1906 const struct __DriverAPIRec dri_kms_driver_api
= {
1907 .InitScreen
= dri_kms_init_screen
,
1908 .DestroyScreen
= dri_destroy_screen
,
1909 .CreateContext
= dri_create_context
,
1910 .DestroyContext
= dri_destroy_context
,
1911 .CreateBuffer
= dri2_create_buffer
,
1912 .DestroyBuffer
= dri_destroy_buffer
,
1913 .MakeCurrent
= dri_make_current
,
1914 .UnbindContext
= dri_unbind_context
,
1916 .AllocateBuffer
= dri2_allocate_buffer
,
1917 .ReleaseBuffer
= dri2_release_buffer
,
1920 /* This is the table of extensions that the loader will dlsym() for. */
1921 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1922 &driCoreExtension
.base
,
1923 &driImageDriverExtension
.base
,
1924 &driDRI2Extension
.base
,
1925 &gallium_config_options
.base
,
1926 &dri2FenceExtension
.base
,
1930 /* vim: set sw=3 ts=8 sts=3 expandtab: */