2 * Mesa 3-D graphics library
4 * Copyright 2009, VMware, Inc.
6 * Copyright (C) 2010 LunarG Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
33 #include "GL/mesa_glinterop.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_debug.h"
38 #include "state_tracker/drm_driver.h"
39 #include "state_tracker/st_cb_bufferobjects.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_texture.h"
43 #include "state_tracker/st_context.h"
44 #include "pipe-loader/pipe_loader.h"
45 #include "main/bufferobj.h"
46 #include "main/texobj.h"
48 #include "dri_screen.h"
49 #include "dri_context.h"
50 #include "dri_drawable.h"
51 #include "dri_query_renderer.h"
52 #include "dri2_buffer.h"
54 static int convert_fourcc(int format
, int *dri_components_p
)
58 case __DRI_IMAGE_FOURCC_RGB565
:
59 format
= __DRI_IMAGE_FORMAT_RGB565
;
60 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
62 case __DRI_IMAGE_FOURCC_ARGB8888
:
63 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
64 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
66 case __DRI_IMAGE_FOURCC_XRGB8888
:
67 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
68 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
70 case __DRI_IMAGE_FOURCC_ABGR8888
:
71 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
72 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
74 case __DRI_IMAGE_FOURCC_XBGR8888
:
75 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
76 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
81 *dri_components_p
= dri_components
;
85 static int convert_to_fourcc(int format
)
88 case __DRI_IMAGE_FORMAT_RGB565
:
89 format
= __DRI_IMAGE_FOURCC_RGB565
;
91 case __DRI_IMAGE_FORMAT_ARGB8888
:
92 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
94 case __DRI_IMAGE_FORMAT_XRGB8888
:
95 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
97 case __DRI_IMAGE_FORMAT_ABGR8888
:
98 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
100 case __DRI_IMAGE_FORMAT_XBGR8888
:
101 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
109 static enum pipe_format
dri2_format_to_pipe_format (int format
)
114 case __DRI_IMAGE_FORMAT_RGB565
:
115 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
117 case __DRI_IMAGE_FORMAT_XRGB8888
:
118 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
120 case __DRI_IMAGE_FORMAT_ARGB8888
:
121 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
123 case __DRI_IMAGE_FORMAT_ABGR8888
:
124 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
127 pf
= PIPE_FORMAT_NONE
;
135 * DRI2 flush extension.
138 dri2_flush_drawable(__DRIdrawable
*dPriv
)
140 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
144 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
146 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
148 dri2InvalidateDrawable(dPriv
);
149 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
151 p_atomic_inc(&drawable
->base
.stamp
);
154 static const __DRI2flushExtension dri2FlushExtension
= {
155 .base
= { __DRI2_FLUSH
, 4 },
157 .flush
= dri2_flush_drawable
,
158 .invalidate
= dri2_invalidate_drawable
,
159 .flush_with_flags
= dri_flush
,
163 * Retrieve __DRIbuffer from the DRI loader.
166 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
167 const enum st_attachment_type
*atts
,
170 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
171 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
173 __DRIbuffer
*buffers
;
175 unsigned attachments
[10];
176 unsigned num_attachments
, i
;
179 with_format
= dri_with_format(drawable
->sPriv
);
183 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
185 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
187 for (i
= 0; i
< *count
; i
++) {
188 enum pipe_format format
;
192 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
193 if (format
== PIPE_FORMAT_NONE
)
197 case ST_ATTACHMENT_FRONT_LEFT
:
201 att
= __DRI_BUFFER_FRONT_LEFT
;
203 case ST_ATTACHMENT_BACK_LEFT
:
204 att
= __DRI_BUFFER_BACK_LEFT
;
206 case ST_ATTACHMENT_FRONT_RIGHT
:
207 att
= __DRI_BUFFER_FRONT_RIGHT
;
209 case ST_ATTACHMENT_BACK_RIGHT
:
210 att
= __DRI_BUFFER_BACK_RIGHT
;
217 * In this switch statement we must support all formats that
218 * may occur as the stvis->color_format.
221 case PIPE_FORMAT_BGRA8888_UNORM
:
224 case PIPE_FORMAT_BGRX8888_UNORM
:
227 case PIPE_FORMAT_B5G6R5_UNORM
:
231 depth
= util_format_get_blocksizebits(format
);
232 assert(!"Unexpected format in dri2_drawable_get_buffers()");
235 attachments
[num_attachments
++] = att
;
237 attachments
[num_attachments
++] = depth
;
242 num_attachments
/= 2;
243 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
244 &dri_drawable
->w
, &dri_drawable
->h
,
245 attachments
, num_attachments
,
246 &num_buffers
, dri_drawable
->loaderPrivate
);
249 buffers
= loader
->getBuffers(dri_drawable
,
250 &dri_drawable
->w
, &dri_drawable
->h
,
251 attachments
, num_attachments
,
252 &num_buffers
, dri_drawable
->loaderPrivate
);
256 *count
= num_buffers
;
262 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
263 struct __DRIimageList
*images
,
264 const enum st_attachment_type
*statts
,
265 unsigned statts_count
)
267 __DRIdrawable
*dPriv
= drawable
->dPriv
;
268 __DRIscreen
*sPriv
= drawable
->sPriv
;
269 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
271 uint32_t buffer_mask
= 0;
274 for (i
= 0; i
< statts_count
; i
++) {
275 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
276 if (pf
== PIPE_FORMAT_NONE
)
280 case ST_ATTACHMENT_FRONT_LEFT
:
281 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
283 case ST_ATTACHMENT_BACK_LEFT
:
284 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
291 case PIPE_FORMAT_B5G6R5_UNORM
:
292 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
294 case PIPE_FORMAT_BGRX8888_UNORM
:
295 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
297 case PIPE_FORMAT_BGRA8888_UNORM
:
298 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
300 case PIPE_FORMAT_RGBA8888_UNORM
:
301 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
304 image_format
= __DRI_IMAGE_FORMAT_NONE
;
309 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
310 (uint32_t *) &drawable
->base
.stamp
,
311 dPriv
->loaderPrivate
, buffer_mask
,
316 dri2_allocate_buffer(__DRIscreen
*sPriv
,
317 unsigned attachment
, unsigned format
,
318 int width
, int height
)
320 struct dri_screen
*screen
= dri_screen(sPriv
);
321 struct dri2_buffer
*buffer
;
322 struct pipe_resource templ
;
325 struct winsys_handle whandle
;
327 switch (attachment
) {
328 case __DRI_BUFFER_FRONT_LEFT
:
329 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
330 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
332 case __DRI_BUFFER_BACK_LEFT
:
333 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
335 case __DRI_BUFFER_DEPTH
:
336 case __DRI_BUFFER_DEPTH_STENCIL
:
337 case __DRI_BUFFER_STENCIL
:
338 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
342 /* because we get the handle and stride */
343 bind
|= PIPE_BIND_SHARED
;
347 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
350 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
353 pf
= PIPE_FORMAT_Z16_UNORM
;
359 buffer
= CALLOC_STRUCT(dri2_buffer
);
363 memset(&templ
, 0, sizeof(templ
));
366 templ
.target
= PIPE_TEXTURE_2D
;
367 templ
.last_level
= 0;
368 templ
.width0
= width
;
369 templ
.height0
= height
;
371 templ
.array_size
= 1;
374 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
375 if (!buffer
->resource
) {
380 memset(&whandle
, 0, sizeof(whandle
));
381 if (screen
->can_share_buffer
)
382 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
384 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
386 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
,
387 buffer
->resource
, &whandle
,
388 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
390 buffer
->base
.attachment
= attachment
;
391 buffer
->base
.name
= whandle
.handle
;
392 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
393 buffer
->base
.pitch
= whandle
.stride
;
395 return &buffer
->base
;
399 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
401 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
403 pipe_resource_reference(&buffer
->resource
, NULL
);
408 * Backend functions for st_framebuffer interface.
412 dri2_allocate_textures(struct dri_context
*ctx
,
413 struct dri_drawable
*drawable
,
414 const enum st_attachment_type
*statts
,
415 unsigned statts_count
)
417 __DRIscreen
*sPriv
= drawable
->sPriv
;
418 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
419 struct dri_screen
*screen
= dri_screen(sPriv
);
420 struct pipe_resource templ
;
421 boolean alloc_depthstencil
= FALSE
;
423 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
424 /* Image specific variables */
425 struct __DRIimageList images
;
426 /* Dri2 specific variables */
427 __DRIbuffer
*buffers
= NULL
;
428 struct winsys_handle whandle
;
429 unsigned num_buffers
= statts_count
;
431 /* First get the buffers from the loader */
433 if (!dri_image_drawable_get_buffers(drawable
, &images
,
434 statts
, statts_count
))
438 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
439 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
440 drawable
->old_w
== dri_drawable
->w
&&
441 drawable
->old_h
== dri_drawable
->h
&&
442 memcmp(drawable
->old
, buffers
,
443 sizeof(__DRIbuffer
) * num_buffers
) == 0))
447 /* Second clean useless resources*/
449 /* See if we need a depth-stencil buffer. */
450 for (i
= 0; i
< statts_count
; i
++) {
451 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
452 alloc_depthstencil
= TRUE
;
457 /* Delete the resources we won't need. */
458 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
459 /* Don't delete the depth-stencil buffer, we can reuse it. */
460 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
463 /* Flush the texture before unreferencing, so that other clients can
464 * see what the driver has rendered.
466 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
467 struct pipe_context
*pipe
= ctx
->st
->pipe
;
468 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
471 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
474 if (drawable
->stvis
.samples
> 1) {
475 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
478 /* Don't delete MSAA resources for the attachments which are enabled,
479 * we can reuse them. */
480 for (j
= 0; j
< statts_count
; j
++) {
481 if (i
== statts
[j
]) {
488 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
493 /* Third use the buffers retrieved to fill the drawable info */
495 memset(&templ
, 0, sizeof(templ
));
496 templ
.target
= screen
->target
;
497 templ
.last_level
= 0;
499 templ
.array_size
= 1;
502 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
503 struct pipe_resource
**buf
=
504 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
505 struct pipe_resource
*texture
= images
.front
->texture
;
507 dri_drawable
->w
= texture
->width0
;
508 dri_drawable
->h
= texture
->height0
;
510 pipe_resource_reference(buf
, texture
);
513 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
514 struct pipe_resource
**buf
=
515 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
516 struct pipe_resource
*texture
= images
.back
->texture
;
518 dri_drawable
->w
= texture
->width0
;
519 dri_drawable
->h
= texture
->height0
;
521 pipe_resource_reference(buf
, texture
);
524 /* Note: if there is both a back and a front buffer,
525 * then they have the same size.
527 templ
.width0
= dri_drawable
->w
;
528 templ
.height0
= dri_drawable
->h
;
531 memset(&whandle
, 0, sizeof(whandle
));
533 /* Process DRI-provided buffers and get pipe_resources. */
534 for (i
= 0; i
< num_buffers
; i
++) {
535 __DRIbuffer
*buf
= &buffers
[i
];
536 enum st_attachment_type statt
;
537 enum pipe_format format
;
539 switch (buf
->attachment
) {
540 case __DRI_BUFFER_FRONT_LEFT
:
541 if (!screen
->auto_fake_front
) {
542 continue; /* invalid attachment */
545 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
546 statt
= ST_ATTACHMENT_FRONT_LEFT
;
548 case __DRI_BUFFER_BACK_LEFT
:
549 statt
= ST_ATTACHMENT_BACK_LEFT
;
552 continue; /* invalid attachment */
555 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
556 if (format
== PIPE_FORMAT_NONE
)
559 /* dri2_drawable_get_buffers has already filled dri_drawable->w
560 * and dri_drawable->h */
561 templ
.width0
= dri_drawable
->w
;
562 templ
.height0
= dri_drawable
->h
;
563 templ
.format
= format
;
565 whandle
.handle
= buf
->name
;
566 whandle
.stride
= buf
->pitch
;
568 if (screen
->can_share_buffer
)
569 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
571 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
572 drawable
->textures
[statt
] =
573 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
575 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
576 assert(drawable
->textures
[statt
]);
580 /* Allocate private MSAA colorbuffers. */
581 if (drawable
->stvis
.samples
> 1) {
582 for (i
= 0; i
< statts_count
; i
++) {
583 enum st_attachment_type statt
= statts
[i
];
585 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
588 if (drawable
->textures
[statt
]) {
589 templ
.format
= drawable
->textures
[statt
]->format
;
590 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
591 templ
.nr_samples
= drawable
->stvis
.samples
;
593 /* Try to reuse the resource.
594 * (the other resource parameters should be constant)
596 if (!drawable
->msaa_textures
[statt
] ||
597 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
598 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
599 /* Allocate a new one. */
600 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
602 drawable
->msaa_textures
[statt
] =
603 screen
->base
.screen
->resource_create(screen
->base
.screen
,
605 assert(drawable
->msaa_textures
[statt
]);
607 /* If there are any MSAA resources, we should initialize them
608 * such that they contain the same data as the single-sample
609 * resources we just got from the X server.
611 * The reason for this is that the state tracker (and
612 * therefore the app) can access the MSAA resources only.
613 * The single-sample resources are not exposed
614 * to the state tracker.
617 dri_pipe_blit(ctx
->st
->pipe
,
618 drawable
->msaa_textures
[statt
],
619 drawable
->textures
[statt
]);
623 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
628 /* Allocate a private depth-stencil buffer. */
629 if (alloc_depthstencil
) {
630 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
631 struct pipe_resource
**zsbuf
;
632 enum pipe_format format
;
635 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
638 templ
.format
= format
;
641 if (drawable
->stvis
.samples
> 1) {
642 templ
.nr_samples
= drawable
->stvis
.samples
;
643 zsbuf
= &drawable
->msaa_textures
[statt
];
646 templ
.nr_samples
= 0;
647 zsbuf
= &drawable
->textures
[statt
];
650 /* Try to reuse the resource.
651 * (the other resource parameters should be constant)
654 (*zsbuf
)->width0
!= templ
.width0
||
655 (*zsbuf
)->height0
!= templ
.height0
) {
656 /* Allocate a new one. */
657 pipe_resource_reference(zsbuf
, NULL
);
658 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
664 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
665 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
669 /* For DRI2, we may get the same buffers again from the server.
670 * To prevent useless imports of gem names, drawable->old* is used
671 * to bypass the import if we get the same buffers. This doesn't apply
672 * to DRI3/Wayland, users of image.loader, since the buffer is managed
673 * by the client (no import), and the back buffer is going to change
677 drawable
->old_num
= num_buffers
;
678 drawable
->old_w
= dri_drawable
->w
;
679 drawable
->old_h
= dri_drawable
->h
;
680 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
685 dri2_flush_frontbuffer(struct dri_context
*ctx
,
686 struct dri_drawable
*drawable
,
687 enum st_attachment_type statt
)
689 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
690 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
691 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
692 struct pipe_context
*pipe
= ctx
->st
->pipe
;
694 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
697 if (drawable
->stvis
.samples
> 1) {
698 /* Resolve the front buffer. */
699 dri_pipe_blit(ctx
->st
->pipe
,
700 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
701 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
704 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
705 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
708 pipe
->flush(pipe
, NULL
, 0);
711 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
713 else if (loader
->flushFrontBuffer
) {
714 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
719 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
720 struct dri_context
*ctx
,
721 struct pipe_resource
*res
)
727 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
729 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
732 if (!loader
->lookupEGLImage
)
735 img
= loader
->lookupEGLImage(screen
->sPriv
,
736 handle
, screen
->sPriv
->loaderPrivate
);
742 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
743 int width
, int height
, int format
,
744 struct winsys_handle
*whandle
,
747 struct dri_screen
*screen
= dri_screen(_screen
);
749 struct pipe_resource templ
;
753 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
755 pf
= dri2_format_to_pipe_format (format
);
756 if (pf
== PIPE_FORMAT_NONE
)
759 img
= CALLOC_STRUCT(__DRIimageRec
);
763 memset(&templ
, 0, sizeof(templ
));
764 templ
.bind
= tex_usage
;
766 templ
.target
= screen
->target
;
767 templ
.last_level
= 0;
768 templ
.width0
= width
;
769 templ
.height0
= height
;
771 templ
.array_size
= 1;
775 img
->texture
= screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
776 &templ
, whandle
, PIPE_HANDLE_USAGE_READ_WRITE
);
784 img
->dri_format
= format
;
786 img
->loader_private
= loaderPrivate
;
792 dri2_create_image_from_name(__DRIscreen
*_screen
,
793 int width
, int height
, int format
,
794 int name
, int pitch
, void *loaderPrivate
)
796 struct winsys_handle whandle
;
799 memset(&whandle
, 0, sizeof(whandle
));
800 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
801 whandle
.handle
= name
;
803 pf
= dri2_format_to_pipe_format (format
);
804 if (pf
== PIPE_FORMAT_NONE
)
807 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
809 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
810 &whandle
, loaderPrivate
);
814 dri2_create_image_from_fd(__DRIscreen
*_screen
,
815 int width
, int height
, int format
,
816 int fd
, int stride
, void *loaderPrivate
)
818 struct winsys_handle whandle
;
823 memset(&whandle
, 0, sizeof(whandle
));
824 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
825 whandle
.handle
= (unsigned)fd
;
826 whandle
.stride
= stride
;
828 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
829 &whandle
, loaderPrivate
);
833 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
834 int renderbuffer
, void *loaderPrivate
)
836 struct dri_context
*ctx
= dri_context(context
);
838 if (!ctx
->st
->get_resource_for_egl_image
)
846 dri2_create_image(__DRIscreen
*_screen
,
847 int width
, int height
, int format
,
848 unsigned int use
, void *loaderPrivate
)
850 struct dri_screen
*screen
= dri_screen(_screen
);
852 struct pipe_resource templ
;
856 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
857 if (use
& __DRI_IMAGE_USE_SCANOUT
)
858 tex_usage
|= PIPE_BIND_SCANOUT
;
859 if (use
& __DRI_IMAGE_USE_SHARE
)
860 tex_usage
|= PIPE_BIND_SHARED
;
861 if (use
& __DRI_IMAGE_USE_LINEAR
)
862 tex_usage
|= PIPE_BIND_LINEAR
;
863 if (use
& __DRI_IMAGE_USE_CURSOR
) {
864 if (width
!= 64 || height
!= 64)
866 tex_usage
|= PIPE_BIND_CURSOR
;
869 pf
= dri2_format_to_pipe_format (format
);
870 if (pf
== PIPE_FORMAT_NONE
)
873 img
= CALLOC_STRUCT(__DRIimageRec
);
877 memset(&templ
, 0, sizeof(templ
));
878 templ
.bind
= tex_usage
;
880 templ
.target
= PIPE_TEXTURE_2D
;
881 templ
.last_level
= 0;
882 templ
.width0
= width
;
883 templ
.height0
= height
;
885 templ
.array_size
= 1;
887 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
895 img
->dri_format
= format
;
896 img
->dri_components
= 0;
899 img
->loader_private
= loaderPrivate
;
904 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
906 struct winsys_handle whandle
;
909 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
910 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
912 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
914 memset(&whandle
, 0, sizeof(whandle
));
917 case __DRI_IMAGE_ATTRIB_STRIDE
:
918 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
919 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
920 image
->texture
, &whandle
, usage
);
921 *value
= whandle
.stride
;
923 case __DRI_IMAGE_ATTRIB_HANDLE
:
924 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
925 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
926 image
->texture
, &whandle
, usage
);
927 *value
= whandle
.handle
;
929 case __DRI_IMAGE_ATTRIB_NAME
:
930 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
931 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
932 image
->texture
, &whandle
, usage
);
933 *value
= whandle
.handle
;
935 case __DRI_IMAGE_ATTRIB_FD
:
936 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
937 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
938 image
->texture
, &whandle
, usage
);
939 *value
= whandle
.handle
;
941 case __DRI_IMAGE_ATTRIB_FORMAT
:
942 *value
= image
->dri_format
;
944 case __DRI_IMAGE_ATTRIB_WIDTH
:
945 *value
= image
->texture
->width0
;
947 case __DRI_IMAGE_ATTRIB_HEIGHT
:
948 *value
= image
->texture
->height0
;
950 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
951 if (image
->dri_components
== 0)
953 *value
= image
->dri_components
;
955 case __DRI_IMAGE_ATTRIB_FOURCC
:
956 *value
= convert_to_fourcc(image
->dri_format
);
958 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
967 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
971 img
= CALLOC_STRUCT(__DRIimageRec
);
976 pipe_resource_reference(&img
->texture
, image
->texture
);
977 img
->level
= image
->level
;
978 img
->layer
= image
->layer
;
979 img
->dri_format
= image
->dri_format
;
980 /* This should be 0 for sub images, but dup is also used for base images. */
981 img
->dri_components
= image
->dri_components
;
982 img
->loader_private
= loaderPrivate
;
988 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
991 * Gallium drivers are bad at adding usages to the resources
992 * once opened again in another process, which is the main use
993 * case for this, so we have to lie.
1002 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1003 int *names
, int num_names
, int *strides
, int *offsets
,
1004 void *loaderPrivate
)
1008 struct winsys_handle whandle
;
1012 if (offsets
[0] != 0)
1015 format
= convert_fourcc(format
, &dri_components
);
1019 memset(&whandle
, 0, sizeof(whandle
));
1020 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1021 whandle
.handle
= names
[0];
1022 whandle
.stride
= strides
[0];
1024 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1025 &whandle
, loaderPrivate
);
1029 img
->dri_components
= dri_components
;
1034 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1041 if (image
->dri_components
== 0)
1044 img
= dri2_dup_image(image
, loaderPrivate
);
1048 /* set this to 0 for sub images. */
1049 img
->dri_components
= 0;
1054 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1055 int depth
, int level
, unsigned *error
,
1056 void *loaderPrivate
)
1059 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1060 struct gl_texture_object
*obj
;
1061 struct pipe_resource
*tex
;
1064 obj
= _mesa_lookup_texture(ctx
, texture
);
1065 if (!obj
|| obj
->Target
!= target
) {
1066 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1070 tex
= st_get_texobj_resource(obj
);
1072 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1076 if (target
== GL_TEXTURE_CUBE_MAP
)
1079 _mesa_test_texobj_completeness(ctx
, obj
);
1080 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1081 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1085 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1086 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1090 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1091 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1095 img
= CALLOC_STRUCT(__DRIimageRec
);
1097 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1103 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1105 img
->loader_private
= loaderPrivate
;
1107 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1108 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1113 pipe_resource_reference(&img
->texture
, tex
);
1115 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1120 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1121 int *fds
, int num_fds
, int *strides
, int *offsets
,
1122 void *loaderPrivate
)
1125 int format
, dri_components
;
1129 if (offsets
[0] != 0)
1132 format
= convert_fourcc(fourcc
, &dri_components
);
1136 img
= dri2_create_image_from_fd(screen
, width
, height
, format
,
1137 fds
[0], strides
[0], loaderPrivate
);
1141 img
->dri_components
= dri_components
;
1146 dri2_from_dma_bufs(__DRIscreen
*screen
,
1147 int width
, int height
, int fourcc
,
1148 int *fds
, int num_fds
,
1149 int *strides
, int *offsets
,
1150 enum __DRIYUVColorSpace yuv_color_space
,
1151 enum __DRISampleRange sample_range
,
1152 enum __DRIChromaSiting horizontal_siting
,
1153 enum __DRIChromaSiting vertical_siting
,
1155 void *loaderPrivate
)
1158 int format
, dri_components
;
1160 if (num_fds
!= 1 || offsets
[0] != 0) {
1161 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1165 format
= convert_fourcc(fourcc
, &dri_components
);
1167 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1171 img
= dri2_create_image_from_fd(screen
, width
, height
, format
,
1172 fds
[0], strides
[0], loaderPrivate
);
1174 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1178 img
->yuv_color_space
= yuv_color_space
;
1179 img
->sample_range
= sample_range
;
1180 img
->horizontal_siting
= horizontal_siting
;
1181 img
->vertical_siting
= vertical_siting
;
1182 img
->dri_components
= dri_components
;
1184 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1189 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1190 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1191 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1194 struct dri_context
*ctx
= dri_context(context
);
1195 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1196 struct pipe_screen
*screen
;
1197 struct pipe_fence_handle
*fence
;
1198 struct pipe_blit_info blit
;
1203 memset(&blit
, 0, sizeof(blit
));
1204 blit
.dst
.resource
= dst
->texture
;
1205 blit
.dst
.box
.x
= dstx0
;
1206 blit
.dst
.box
.y
= dsty0
;
1207 blit
.dst
.box
.width
= dstwidth
;
1208 blit
.dst
.box
.height
= dstheight
;
1209 blit
.dst
.box
.depth
= 1;
1210 blit
.dst
.format
= dst
->texture
->format
;
1211 blit
.src
.resource
= src
->texture
;
1212 blit
.src
.box
.x
= srcx0
;
1213 blit
.src
.box
.y
= srcy0
;
1214 blit
.src
.box
.width
= srcwidth
;
1215 blit
.src
.box
.height
= srcheight
;
1216 blit
.src
.box
.depth
= 1;
1217 blit
.src
.format
= src
->texture
->format
;
1218 blit
.mask
= PIPE_MASK_RGBA
;
1219 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1221 pipe
->blit(pipe
, &blit
);
1223 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1224 pipe
->flush_resource(pipe
, dst
->texture
);
1225 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1226 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1227 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1228 pipe
->flush_resource(pipe
, dst
->texture
);
1229 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1230 (void) screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
1231 screen
->fence_reference(screen
, &fence
, NULL
);
1236 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1237 int x0
, int y0
, int width
, int height
,
1238 unsigned int flags
, int *stride
, void **data
)
1240 struct dri_context
*ctx
= dri_context(context
);
1241 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1242 enum pipe_transfer_usage pipe_access
= 0;
1243 struct pipe_transfer
*trans
;
1246 if (!image
|| !data
|| *data
)
1249 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1250 pipe_access
|= PIPE_TRANSFER_READ
;
1251 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1252 pipe_access
|= PIPE_TRANSFER_WRITE
;
1254 map
= pipe_transfer_map(pipe
, image
->texture
,
1255 0, 0, pipe_access
, x0
, y0
, width
, height
,
1259 *stride
= trans
->stride
;
1266 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1268 struct dri_context
*ctx
= dri_context(context
);
1269 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1271 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1275 dri2_destroy_image(__DRIimage
*img
)
1277 pipe_resource_reference(&img
->texture
, NULL
);
1282 dri2_get_capabilities(__DRIscreen
*_screen
)
1284 struct dri_screen
*screen
= dri_screen(_screen
);
1286 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1289 /* The extension is modified during runtime if DRI_PRIME is detected */
1290 static __DRIimageExtension dri2ImageExtension
= {
1291 .base
= { __DRI_IMAGE
, 12 },
1293 .createImageFromName
= dri2_create_image_from_name
,
1294 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1295 .destroyImage
= dri2_destroy_image
,
1296 .createImage
= dri2_create_image
,
1297 .queryImage
= dri2_query_image
,
1298 .dupImage
= dri2_dup_image
,
1299 .validateUsage
= dri2_validate_usage
,
1300 .createImageFromNames
= dri2_from_names
,
1301 .fromPlanar
= dri2_from_planar
,
1302 .createImageFromTexture
= dri2_create_from_texture
,
1303 .createImageFromFds
= NULL
,
1304 .createImageFromDmaBufs
= NULL
,
1305 .blitImage
= dri2_blit_image
,
1306 .getCapabilities
= dri2_get_capabilities
,
1307 .mapImage
= dri2_map_image
,
1308 .unmapImage
= dri2_unmap_image
,
1313 dri2_is_opencl_interop_loaded_locked(struct dri_screen
*screen
)
1315 return screen
->opencl_dri_event_add_ref
&&
1316 screen
->opencl_dri_event_release
&&
1317 screen
->opencl_dri_event_wait
&&
1318 screen
->opencl_dri_event_get_fence
;
1322 dri2_load_opencl_interop(struct dri_screen
*screen
)
1324 #if defined(RTLD_DEFAULT)
1327 pipe_mutex_lock(screen
->opencl_func_mutex
);
1329 if (dri2_is_opencl_interop_loaded_locked(screen
)) {
1330 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1334 screen
->opencl_dri_event_add_ref
=
1335 dlsym(RTLD_DEFAULT
, "opencl_dri_event_add_ref");
1336 screen
->opencl_dri_event_release
=
1337 dlsym(RTLD_DEFAULT
, "opencl_dri_event_release");
1338 screen
->opencl_dri_event_wait
=
1339 dlsym(RTLD_DEFAULT
, "opencl_dri_event_wait");
1340 screen
->opencl_dri_event_get_fence
=
1341 dlsym(RTLD_DEFAULT
, "opencl_dri_event_get_fence");
1343 success
= dri2_is_opencl_interop_loaded_locked(screen
);
1344 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1352 struct dri_screen
*driscreen
;
1353 struct pipe_fence_handle
*pipe_fence
;
1358 dri2_create_fence(__DRIcontext
*_ctx
)
1360 struct pipe_context
*ctx
= dri_context(_ctx
)->st
->pipe
;
1361 struct dri2_fence
*fence
= CALLOC_STRUCT(dri2_fence
);
1366 ctx
->flush(ctx
, &fence
->pipe_fence
, 0);
1368 if (!fence
->pipe_fence
) {
1373 fence
->driscreen
= dri_screen(_ctx
->driScreenPriv
);
1378 dri2_get_fence_from_cl_event(__DRIscreen
*_screen
, intptr_t cl_event
)
1380 struct dri_screen
*driscreen
= dri_screen(_screen
);
1381 struct dri2_fence
*fence
;
1383 if (!dri2_load_opencl_interop(driscreen
))
1386 fence
= CALLOC_STRUCT(dri2_fence
);
1390 fence
->cl_event
= (void*)cl_event
;
1392 if (!driscreen
->opencl_dri_event_add_ref(fence
->cl_event
)) {
1397 fence
->driscreen
= driscreen
;
1402 dri2_destroy_fence(__DRIscreen
*_screen
, void *_fence
)
1404 struct dri_screen
*driscreen
= dri_screen(_screen
);
1405 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1406 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1408 if (fence
->pipe_fence
)
1409 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
1410 else if (fence
->cl_event
)
1411 driscreen
->opencl_dri_event_release(fence
->cl_event
);
1419 dri2_client_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
,
1422 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1423 struct dri_screen
*driscreen
= fence
->driscreen
;
1424 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1426 /* No need to flush. The context was flushed when the fence was created. */
1428 if (fence
->pipe_fence
)
1429 return screen
->fence_finish(screen
, fence
->pipe_fence
, timeout
);
1430 else if (fence
->cl_event
) {
1431 struct pipe_fence_handle
*pipe_fence
=
1432 driscreen
->opencl_dri_event_get_fence(fence
->cl_event
);
1435 return screen
->fence_finish(screen
, pipe_fence
, timeout
);
1437 return driscreen
->opencl_dri_event_wait(fence
->cl_event
, timeout
);
1446 dri2_server_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
)
1448 /* AFAIK, no driver currently supports parallel context execution. */
1451 static __DRI2fenceExtension dri2FenceExtension
= {
1452 .base
= { __DRI2_FENCE
, 1 },
1454 .create_fence
= dri2_create_fence
,
1455 .get_fence_from_cl_event
= dri2_get_fence_from_cl_event
,
1456 .destroy_fence
= dri2_destroy_fence
,
1457 .client_wait_sync
= dri2_client_wait_sync
,
1458 .server_wait_sync
= dri2_server_wait_sync
1461 static const __DRIrobustnessExtension dri2Robustness
= {
1462 .base
= { __DRI2_ROBUSTNESS
, 1 }
1466 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1467 mesa_glinterop_device_info
*out
)
1469 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1471 if (!out
->struct_version
)
1472 return MESA_GLINTEROP_INVALID_VALUE
;
1474 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1475 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1476 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1477 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1479 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1480 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1482 out
->interop_version
= 1;
1484 return MESA_GLINTEROP_SUCCESS
;
1488 dri2_interop_export_object(__DRIcontext
*_ctx
,
1489 const mesa_glinterop_export_in
*in
,
1490 mesa_glinterop_export_out
*out
)
1492 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1493 struct pipe_screen
*screen
= st
->pipe
->screen
;
1494 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1495 struct pipe_resource
*res
= NULL
;
1496 struct winsys_handle whandle
;
1497 unsigned target
, usage
;
1500 if (!in
->struct_version
|| !out
->struct_version
)
1501 return MESA_GLINTEROP_INVALID_VALUE
;
1503 /* Validate the target. */
1504 switch (in
->target
) {
1505 case GL_TEXTURE_BUFFER
:
1509 case GL_TEXTURE_RECTANGLE
:
1510 case GL_TEXTURE_1D_ARRAY
:
1511 case GL_TEXTURE_2D_ARRAY
:
1512 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1513 case GL_TEXTURE_CUBE_MAP
:
1514 case GL_TEXTURE_2D_MULTISAMPLE
:
1515 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1516 case GL_TEXTURE_EXTERNAL_OES
:
1517 case GL_RENDERBUFFER
:
1518 case GL_ARRAY_BUFFER
:
1519 target
= in
->target
;
1521 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1522 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1523 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1524 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1525 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1526 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1527 target
= GL_TEXTURE_CUBE_MAP
;
1530 return MESA_GLINTEROP_INVALID_TARGET
;
1533 /* Validate the simple case of miplevel. */
1534 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1536 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1538 /* Validate the OpenGL object and get pipe_resource. */
1539 mtx_lock(&ctx
->Shared
->Mutex
);
1541 if (target
== GL_ARRAY_BUFFER
) {
1544 * The error checking is based on the documentation of
1545 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1547 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1549 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1550 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1551 * a GL buffer object but does not have an existing data store or
1552 * the size of the buffer is 0."
1554 if (!buf
|| buf
->Size
== 0) {
1555 mtx_unlock(&ctx
->Shared
->Mutex
);
1556 return MESA_GLINTEROP_INVALID_OBJECT
;
1559 res
= st_buffer_object(buf
)->buffer
;
1561 /* this shouldn't happen */
1562 mtx_unlock(&ctx
->Shared
->Mutex
);
1563 return MESA_GLINTEROP_INVALID_OBJECT
;
1566 out
->buf_offset
= 0;
1567 out
->buf_size
= buf
->Size
;
1569 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1570 } else if (target
== GL_RENDERBUFFER
) {
1573 * The error checking is based on the documentation of
1574 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1576 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1578 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1579 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1580 * object or if the width or height of renderbuffer is zero."
1582 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1583 mtx_unlock(&ctx
->Shared
->Mutex
);
1584 return MESA_GLINTEROP_INVALID_OBJECT
;
1587 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1588 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1589 * renderbuffer object."
1591 if (rb
->NumSamples
> 1) {
1592 mtx_unlock(&ctx
->Shared
->Mutex
);
1593 return MESA_GLINTEROP_INVALID_OPERATION
;
1596 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1597 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1598 * required by the OpenCL implementation on the device."
1600 res
= st_renderbuffer(rb
)->texture
;
1602 mtx_unlock(&ctx
->Shared
->Mutex
);
1603 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1606 out
->internalformat
= rb
->InternalFormat
;
1607 out
->view_minlevel
= 0;
1608 out
->view_numlevels
= 1;
1609 out
->view_minlayer
= 0;
1610 out
->view_numlayers
= 1;
1614 * The error checking is based on the documentation of
1615 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1617 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1620 _mesa_test_texobj_completeness(ctx
, obj
);
1622 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1623 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1624 * type matches texture_target, if the specified miplevel of texture
1625 * is not defined, or if the width or height of the specified
1626 * miplevel is zero or if the GL texture object is incomplete."
1629 obj
->Target
!= target
||
1630 !obj
->_BaseComplete
||
1631 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1632 mtx_unlock(&ctx
->Shared
->Mutex
);
1633 return MESA_GLINTEROP_INVALID_OBJECT
;
1636 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1637 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1638 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1639 * implementations); or greater than the value of q (for both OpenGL
1640 * and OpenGL ES). levelbase and q are defined for the texture in
1641 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1642 * specification and section 3.7.10 of the OpenGL ES 2.0."
1644 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1645 mtx_unlock(&ctx
->Shared
->Mutex
);
1646 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1649 if (!st_finalize_texture(ctx
, st
->pipe
, obj
)) {
1650 mtx_unlock(&ctx
->Shared
->Mutex
);
1651 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1654 res
= st_get_texobj_resource(obj
);
1656 /* Incomplete texture buffer object? This shouldn't really occur. */
1657 mtx_unlock(&ctx
->Shared
->Mutex
);
1658 return MESA_GLINTEROP_INVALID_OBJECT
;
1661 if (target
== GL_TEXTURE_BUFFER
) {
1662 out
->internalformat
= obj
->BufferObjectFormat
;
1663 out
->buf_offset
= obj
->BufferOffset
;
1664 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1667 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1669 out
->internalformat
= obj
->Image
[0][0]->InternalFormat
;
1670 out
->view_minlevel
= obj
->MinLevel
;
1671 out
->view_numlevels
= obj
->NumLevels
;
1672 out
->view_minlayer
= obj
->MinLayer
;
1673 out
->view_numlayers
= obj
->NumLayers
;
1677 /* Get the handle. */
1678 switch (in
->access
) {
1679 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1680 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1682 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1683 usage
= PIPE_HANDLE_USAGE_READ
;
1685 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1686 usage
= PIPE_HANDLE_USAGE_WRITE
;
1692 memset(&whandle
, 0, sizeof(whandle
));
1693 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1695 success
= screen
->resource_get_handle(screen
, res
, &whandle
, usage
);
1696 mtx_unlock(&ctx
->Shared
->Mutex
);
1699 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1701 out
->dmabuf_fd
= whandle
.handle
;
1702 out
->out_driver_data_written
= 0;
1704 if (res
->target
== PIPE_BUFFER
)
1705 out
->buf_offset
+= whandle
.offset
;
1707 return MESA_GLINTEROP_SUCCESS
;
1710 static const __DRI2interopExtension dri2InteropExtension
= {
1711 .base
= { __DRI2_INTEROP
, 1 },
1712 .query_device_info
= dri2_interop_query_device_info
,
1713 .export_object
= dri2_interop_export_object
1717 * Backend function init_screen.
1720 static const __DRIextension
*dri_screen_extensions
[] = {
1721 &driTexBufferExtension
.base
,
1722 &dri2FlushExtension
.base
,
1723 &dri2ImageExtension
.base
,
1724 &dri2RendererQueryExtension
.base
,
1725 &dri2ConfigQueryExtension
.base
,
1726 &dri2ThrottleExtension
.base
,
1727 &dri2FenceExtension
.base
,
1728 &dri2InteropExtension
.base
,
1732 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1733 &driTexBufferExtension
.base
,
1734 &dri2FlushExtension
.base
,
1735 &dri2ImageExtension
.base
,
1736 &dri2RendererQueryExtension
.base
,
1737 &dri2ConfigQueryExtension
.base
,
1738 &dri2ThrottleExtension
.base
,
1739 &dri2FenceExtension
.base
,
1740 &dri2InteropExtension
.base
,
1741 &dri2Robustness
.base
,
1746 * This is the driver specific part of the createNewScreen entry point.
1748 * Returns the struct gl_config supported by this driver.
1750 static const __DRIconfig
**
1751 dri2_init_screen(__DRIscreen
* sPriv
)
1753 const __DRIconfig
**configs
;
1754 struct dri_screen
*screen
;
1755 struct pipe_screen
*pscreen
= NULL
;
1756 const struct drm_conf_ret
*throttle_ret
;
1757 const struct drm_conf_ret
*dmabuf_ret
;
1760 screen
= CALLOC_STRUCT(dri_screen
);
1764 screen
->sPriv
= sPriv
;
1765 screen
->fd
= sPriv
->fd
;
1766 pipe_mutex_init(screen
->opencl_func_mutex
);
1768 sPriv
->driverPrivate
= (void *)screen
;
1770 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1773 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1774 pscreen
= pipe_loader_create_screen(screen
->dev
);
1779 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1780 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1782 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1783 screen
->throttling_enabled
= TRUE
;
1784 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1787 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1790 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1791 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1792 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1793 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1797 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1798 sPriv
->extensions
= dri_robust_screen_extensions
;
1799 screen
->has_reset_status_query
= true;
1802 sPriv
->extensions
= dri_screen_extensions
;
1804 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1808 screen
->can_share_buffer
= true;
1809 screen
->auto_fake_front
= dri_with_format(sPriv
);
1810 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1811 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1815 dri_destroy_screen_helper(screen
);
1817 pipe_loader_release(&screen
->dev
, 1);
1825 * This is the driver specific part of the createNewScreen entry point.
1827 * Returns the struct gl_config supported by this driver.
1829 static const __DRIconfig
**
1830 dri_kms_init_screen(__DRIscreen
* sPriv
)
1832 #if defined(GALLIUM_SOFTPIPE)
1833 const __DRIconfig
**configs
;
1834 struct dri_screen
*screen
;
1835 struct pipe_screen
*pscreen
= NULL
;
1839 screen
= CALLOC_STRUCT(dri_screen
);
1843 screen
->sPriv
= sPriv
;
1844 screen
->fd
= sPriv
->fd
;
1846 sPriv
->driverPrivate
= (void *)screen
;
1848 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1851 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1852 pscreen
= pipe_loader_create_screen(screen
->dev
);
1857 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1858 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1859 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1860 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1863 sPriv
->extensions
= dri_screen_extensions
;
1865 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1869 screen
->can_share_buffer
= false;
1870 screen
->auto_fake_front
= dri_with_format(sPriv
);
1871 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1872 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1876 dri_destroy_screen_helper(screen
);
1878 pipe_loader_release(&screen
->dev
, 1);
1882 #endif // GALLIUM_SOFTPIPE
1887 dri2_create_buffer(__DRIscreen
* sPriv
,
1888 __DRIdrawable
* dPriv
,
1889 const struct gl_config
* visual
, boolean isPixmap
)
1891 struct dri_drawable
*drawable
= NULL
;
1893 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1896 drawable
= dPriv
->driverPrivate
;
1898 drawable
->allocate_textures
= dri2_allocate_textures
;
1899 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1900 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1906 * DRI driver virtual function table.
1908 * DRI versions differ in their implementation of init_screen and swap_buffers.
1910 const struct __DriverAPIRec galliumdrm_driver_api
= {
1911 .InitScreen
= dri2_init_screen
,
1912 .DestroyScreen
= dri_destroy_screen
,
1913 .CreateContext
= dri_create_context
,
1914 .DestroyContext
= dri_destroy_context
,
1915 .CreateBuffer
= dri2_create_buffer
,
1916 .DestroyBuffer
= dri_destroy_buffer
,
1917 .MakeCurrent
= dri_make_current
,
1918 .UnbindContext
= dri_unbind_context
,
1920 .AllocateBuffer
= dri2_allocate_buffer
,
1921 .ReleaseBuffer
= dri2_release_buffer
,
1925 * DRI driver virtual function table.
1927 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1928 * hook. The latter is used to explicitly initialise the kms_swrast driver
1929 * rather than selecting the approapriate driver as suggested by the loader.
1931 const struct __DriverAPIRec dri_kms_driver_api
= {
1932 .InitScreen
= dri_kms_init_screen
,
1933 .DestroyScreen
= dri_destroy_screen
,
1934 .CreateContext
= dri_create_context
,
1935 .DestroyContext
= dri_destroy_context
,
1936 .CreateBuffer
= dri2_create_buffer
,
1937 .DestroyBuffer
= dri_destroy_buffer
,
1938 .MakeCurrent
= dri_make_current
,
1939 .UnbindContext
= dri_unbind_context
,
1941 .AllocateBuffer
= dri2_allocate_buffer
,
1942 .ReleaseBuffer
= dri2_release_buffer
,
1945 /* This is the table of extensions that the loader will dlsym() for. */
1946 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1947 &driCoreExtension
.base
,
1948 &driImageDriverExtension
.base
,
1949 &driDRI2Extension
.base
,
1950 &gallium_config_options
.base
,
1954 /* vim: set sw=3 ts=8 sts=3 expandtab: */