2 * Mesa 3-D graphics library
4 * Copyright 2009, VMware, Inc.
6 * Copyright (C) 2010 LunarG Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
33 #include "GL/mesa_glinterop.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_debug.h"
38 #include "state_tracker/drm_driver.h"
39 #include "state_tracker/st_cb_bufferobjects.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_texture.h"
43 #include "state_tracker/st_context.h"
44 #include "pipe-loader/pipe_loader.h"
45 #include "main/bufferobj.h"
46 #include "main/texobj.h"
48 #include "dri_screen.h"
49 #include "dri_context.h"
50 #include "dri_drawable.h"
51 #include "dri_query_renderer.h"
52 #include "dri2_buffer.h"
54 static int convert_fourcc(int format
, int *dri_components_p
)
58 case __DRI_IMAGE_FOURCC_RGB565
:
59 format
= __DRI_IMAGE_FORMAT_RGB565
;
60 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
62 case __DRI_IMAGE_FOURCC_ARGB8888
:
63 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
64 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
66 case __DRI_IMAGE_FOURCC_XRGB8888
:
67 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
68 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
70 case __DRI_IMAGE_FOURCC_ABGR8888
:
71 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
72 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
74 case __DRI_IMAGE_FOURCC_XBGR8888
:
75 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
76 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
78 case __DRI_IMAGE_FOURCC_R8
:
79 format
= __DRI_IMAGE_FORMAT_R8
;
80 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
82 case __DRI_IMAGE_FOURCC_GR88
:
83 format
= __DRI_IMAGE_FORMAT_GR88
;
84 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
87 * For multi-planar YUV formats, we return the format of the first
88 * plane only. Since there is only one caller which supports multi-
89 * planar YUV it gets to figure out the remaining planes on it's
92 case __DRI_IMAGE_FOURCC_YUV420
:
93 case __DRI_IMAGE_FOURCC_YVU420
:
94 format
= __DRI_IMAGE_FORMAT_R8
;
95 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
97 case __DRI_IMAGE_FOURCC_NV12
:
98 format
= __DRI_IMAGE_FORMAT_R8
;
99 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
104 *dri_components_p
= dri_components
;
108 /* NOTE this probably isn't going to do the right thing for YUV images
109 * (but I think the same can be said for intel_query_image()). I think
110 * only needed for exporting dmabuf's, so I think I won't loose much
113 static int convert_to_fourcc(int format
)
116 case __DRI_IMAGE_FORMAT_RGB565
:
117 format
= __DRI_IMAGE_FOURCC_RGB565
;
119 case __DRI_IMAGE_FORMAT_ARGB8888
:
120 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
122 case __DRI_IMAGE_FORMAT_XRGB8888
:
123 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
125 case __DRI_IMAGE_FORMAT_ABGR8888
:
126 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
128 case __DRI_IMAGE_FORMAT_XBGR8888
:
129 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
131 case __DRI_IMAGE_FORMAT_R8
:
132 format
= __DRI_IMAGE_FOURCC_R8
;
134 case __DRI_IMAGE_FORMAT_GR88
:
135 format
= __DRI_IMAGE_FOURCC_GR88
;
143 static enum pipe_format
dri2_format_to_pipe_format (int format
)
148 case __DRI_IMAGE_FORMAT_RGB565
:
149 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
151 case __DRI_IMAGE_FORMAT_XRGB8888
:
152 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
154 case __DRI_IMAGE_FORMAT_ARGB8888
:
155 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
157 case __DRI_IMAGE_FORMAT_ABGR8888
:
158 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
160 case __DRI_IMAGE_FORMAT_R8
:
161 pf
= PIPE_FORMAT_R8_UNORM
;
163 case __DRI_IMAGE_FORMAT_GR88
:
164 pf
= PIPE_FORMAT_RG88_UNORM
;
167 pf
= PIPE_FORMAT_NONE
;
175 * DRI2 flush extension.
178 dri2_flush_drawable(__DRIdrawable
*dPriv
)
180 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
184 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
186 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
188 dri2InvalidateDrawable(dPriv
);
189 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
190 drawable
->texture_mask
= 0;
192 p_atomic_inc(&drawable
->base
.stamp
);
195 static const __DRI2flushExtension dri2FlushExtension
= {
196 .base
= { __DRI2_FLUSH
, 4 },
198 .flush
= dri2_flush_drawable
,
199 .invalidate
= dri2_invalidate_drawable
,
200 .flush_with_flags
= dri_flush
,
204 * Retrieve __DRIbuffer from the DRI loader.
207 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
208 const enum st_attachment_type
*atts
,
211 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
212 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
214 __DRIbuffer
*buffers
;
216 unsigned attachments
[10];
217 unsigned num_attachments
, i
;
220 with_format
= dri_with_format(drawable
->sPriv
);
224 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
226 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
228 for (i
= 0; i
< *count
; i
++) {
229 enum pipe_format format
;
233 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
234 if (format
== PIPE_FORMAT_NONE
)
238 case ST_ATTACHMENT_FRONT_LEFT
:
242 att
= __DRI_BUFFER_FRONT_LEFT
;
244 case ST_ATTACHMENT_BACK_LEFT
:
245 att
= __DRI_BUFFER_BACK_LEFT
;
247 case ST_ATTACHMENT_FRONT_RIGHT
:
248 att
= __DRI_BUFFER_FRONT_RIGHT
;
250 case ST_ATTACHMENT_BACK_RIGHT
:
251 att
= __DRI_BUFFER_BACK_RIGHT
;
258 * In this switch statement we must support all formats that
259 * may occur as the stvis->color_format.
262 case PIPE_FORMAT_BGRA8888_UNORM
:
265 case PIPE_FORMAT_BGRX8888_UNORM
:
268 case PIPE_FORMAT_B5G6R5_UNORM
:
272 depth
= util_format_get_blocksizebits(format
);
273 assert(!"Unexpected format in dri2_drawable_get_buffers()");
276 attachments
[num_attachments
++] = att
;
278 attachments
[num_attachments
++] = depth
;
283 num_attachments
/= 2;
284 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
285 &dri_drawable
->w
, &dri_drawable
->h
,
286 attachments
, num_attachments
,
287 &num_buffers
, dri_drawable
->loaderPrivate
);
290 buffers
= loader
->getBuffers(dri_drawable
,
291 &dri_drawable
->w
, &dri_drawable
->h
,
292 attachments
, num_attachments
,
293 &num_buffers
, dri_drawable
->loaderPrivate
);
297 *count
= num_buffers
;
303 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
304 struct __DRIimageList
*images
,
305 const enum st_attachment_type
*statts
,
306 unsigned statts_count
)
308 __DRIdrawable
*dPriv
= drawable
->dPriv
;
309 __DRIscreen
*sPriv
= drawable
->sPriv
;
310 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
312 uint32_t buffer_mask
= 0;
315 for (i
= 0; i
< statts_count
; i
++) {
316 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
317 if (pf
== PIPE_FORMAT_NONE
)
321 case ST_ATTACHMENT_FRONT_LEFT
:
322 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
324 case ST_ATTACHMENT_BACK_LEFT
:
325 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
332 case PIPE_FORMAT_B5G6R5_UNORM
:
333 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
335 case PIPE_FORMAT_BGRX8888_UNORM
:
336 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
338 case PIPE_FORMAT_BGRA8888_UNORM
:
339 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
341 case PIPE_FORMAT_RGBA8888_UNORM
:
342 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
345 image_format
= __DRI_IMAGE_FORMAT_NONE
;
350 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
351 (uint32_t *) &drawable
->base
.stamp
,
352 dPriv
->loaderPrivate
, buffer_mask
,
357 dri2_allocate_buffer(__DRIscreen
*sPriv
,
358 unsigned attachment
, unsigned format
,
359 int width
, int height
)
361 struct dri_screen
*screen
= dri_screen(sPriv
);
362 struct dri2_buffer
*buffer
;
363 struct pipe_resource templ
;
366 struct winsys_handle whandle
;
368 switch (attachment
) {
369 case __DRI_BUFFER_FRONT_LEFT
:
370 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
371 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
373 case __DRI_BUFFER_BACK_LEFT
:
374 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
376 case __DRI_BUFFER_DEPTH
:
377 case __DRI_BUFFER_DEPTH_STENCIL
:
378 case __DRI_BUFFER_STENCIL
:
379 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
383 /* because we get the handle and stride */
384 bind
|= PIPE_BIND_SHARED
;
388 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
391 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
394 pf
= PIPE_FORMAT_Z16_UNORM
;
400 buffer
= CALLOC_STRUCT(dri2_buffer
);
404 memset(&templ
, 0, sizeof(templ
));
407 templ
.target
= PIPE_TEXTURE_2D
;
408 templ
.last_level
= 0;
409 templ
.width0
= width
;
410 templ
.height0
= height
;
412 templ
.array_size
= 1;
415 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
416 if (!buffer
->resource
) {
421 memset(&whandle
, 0, sizeof(whandle
));
422 if (screen
->can_share_buffer
)
423 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
425 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
427 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
428 buffer
->resource
, &whandle
,
429 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
431 buffer
->base
.attachment
= attachment
;
432 buffer
->base
.name
= whandle
.handle
;
433 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
434 buffer
->base
.pitch
= whandle
.stride
;
436 return &buffer
->base
;
440 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
442 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
444 pipe_resource_reference(&buffer
->resource
, NULL
);
449 * Backend functions for st_framebuffer interface.
453 dri2_allocate_textures(struct dri_context
*ctx
,
454 struct dri_drawable
*drawable
,
455 const enum st_attachment_type
*statts
,
456 unsigned statts_count
)
458 __DRIscreen
*sPriv
= drawable
->sPriv
;
459 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
460 struct dri_screen
*screen
= dri_screen(sPriv
);
461 struct pipe_resource templ
;
462 boolean alloc_depthstencil
= FALSE
;
464 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
465 /* Image specific variables */
466 struct __DRIimageList images
;
467 /* Dri2 specific variables */
468 __DRIbuffer
*buffers
= NULL
;
469 struct winsys_handle whandle
;
470 unsigned num_buffers
= statts_count
;
472 /* First get the buffers from the loader */
474 if (!dri_image_drawable_get_buffers(drawable
, &images
,
475 statts
, statts_count
))
479 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
480 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
481 drawable
->old_w
== dri_drawable
->w
&&
482 drawable
->old_h
== dri_drawable
->h
&&
483 memcmp(drawable
->old
, buffers
,
484 sizeof(__DRIbuffer
) * num_buffers
) == 0))
488 /* Second clean useless resources*/
490 /* See if we need a depth-stencil buffer. */
491 for (i
= 0; i
< statts_count
; i
++) {
492 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
493 alloc_depthstencil
= TRUE
;
498 /* Delete the resources we won't need. */
499 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
500 /* Don't delete the depth-stencil buffer, we can reuse it. */
501 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
504 /* Flush the texture before unreferencing, so that other clients can
505 * see what the driver has rendered.
507 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
508 struct pipe_context
*pipe
= ctx
->st
->pipe
;
509 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
512 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
515 if (drawable
->stvis
.samples
> 1) {
516 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
519 /* Don't delete MSAA resources for the attachments which are enabled,
520 * we can reuse them. */
521 for (j
= 0; j
< statts_count
; j
++) {
522 if (i
== statts
[j
]) {
529 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
534 /* Third use the buffers retrieved to fill the drawable info */
536 memset(&templ
, 0, sizeof(templ
));
537 templ
.target
= screen
->target
;
538 templ
.last_level
= 0;
540 templ
.array_size
= 1;
543 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
544 struct pipe_resource
**buf
=
545 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
546 struct pipe_resource
*texture
= images
.front
->texture
;
548 dri_drawable
->w
= texture
->width0
;
549 dri_drawable
->h
= texture
->height0
;
551 pipe_resource_reference(buf
, texture
);
554 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
555 struct pipe_resource
**buf
=
556 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
557 struct pipe_resource
*texture
= images
.back
->texture
;
559 dri_drawable
->w
= texture
->width0
;
560 dri_drawable
->h
= texture
->height0
;
562 pipe_resource_reference(buf
, texture
);
565 /* Note: if there is both a back and a front buffer,
566 * then they have the same size.
568 templ
.width0
= dri_drawable
->w
;
569 templ
.height0
= dri_drawable
->h
;
572 memset(&whandle
, 0, sizeof(whandle
));
574 /* Process DRI-provided buffers and get pipe_resources. */
575 for (i
= 0; i
< num_buffers
; i
++) {
576 __DRIbuffer
*buf
= &buffers
[i
];
577 enum st_attachment_type statt
;
578 enum pipe_format format
;
580 switch (buf
->attachment
) {
581 case __DRI_BUFFER_FRONT_LEFT
:
582 if (!screen
->auto_fake_front
) {
583 continue; /* invalid attachment */
586 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
587 statt
= ST_ATTACHMENT_FRONT_LEFT
;
589 case __DRI_BUFFER_BACK_LEFT
:
590 statt
= ST_ATTACHMENT_BACK_LEFT
;
593 continue; /* invalid attachment */
596 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
597 if (format
== PIPE_FORMAT_NONE
)
600 /* dri2_drawable_get_buffers has already filled dri_drawable->w
601 * and dri_drawable->h */
602 templ
.width0
= dri_drawable
->w
;
603 templ
.height0
= dri_drawable
->h
;
604 templ
.format
= format
;
606 whandle
.handle
= buf
->name
;
607 whandle
.stride
= buf
->pitch
;
609 if (screen
->can_share_buffer
)
610 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
612 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
613 drawable
->textures
[statt
] =
614 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
616 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
617 assert(drawable
->textures
[statt
]);
621 /* Allocate private MSAA colorbuffers. */
622 if (drawable
->stvis
.samples
> 1) {
623 for (i
= 0; i
< statts_count
; i
++) {
624 enum st_attachment_type statt
= statts
[i
];
626 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
629 if (drawable
->textures
[statt
]) {
630 templ
.format
= drawable
->textures
[statt
]->format
;
631 templ
.bind
= drawable
->textures
[statt
]->bind
& ~PIPE_BIND_SCANOUT
;
632 templ
.nr_samples
= drawable
->stvis
.samples
;
634 /* Try to reuse the resource.
635 * (the other resource parameters should be constant)
637 if (!drawable
->msaa_textures
[statt
] ||
638 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
639 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
640 /* Allocate a new one. */
641 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
643 drawable
->msaa_textures
[statt
] =
644 screen
->base
.screen
->resource_create(screen
->base
.screen
,
646 assert(drawable
->msaa_textures
[statt
]);
648 /* If there are any MSAA resources, we should initialize them
649 * such that they contain the same data as the single-sample
650 * resources we just got from the X server.
652 * The reason for this is that the state tracker (and
653 * therefore the app) can access the MSAA resources only.
654 * The single-sample resources are not exposed
655 * to the state tracker.
658 dri_pipe_blit(ctx
->st
->pipe
,
659 drawable
->msaa_textures
[statt
],
660 drawable
->textures
[statt
]);
664 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
669 /* Allocate a private depth-stencil buffer. */
670 if (alloc_depthstencil
) {
671 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
672 struct pipe_resource
**zsbuf
;
673 enum pipe_format format
;
676 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
679 templ
.format
= format
;
682 if (drawable
->stvis
.samples
> 1) {
683 templ
.nr_samples
= drawable
->stvis
.samples
;
684 zsbuf
= &drawable
->msaa_textures
[statt
];
687 templ
.nr_samples
= 0;
688 zsbuf
= &drawable
->textures
[statt
];
691 /* Try to reuse the resource.
692 * (the other resource parameters should be constant)
695 (*zsbuf
)->width0
!= templ
.width0
||
696 (*zsbuf
)->height0
!= templ
.height0
) {
697 /* Allocate a new one. */
698 pipe_resource_reference(zsbuf
, NULL
);
699 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
705 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
706 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
710 /* For DRI2, we may get the same buffers again from the server.
711 * To prevent useless imports of gem names, drawable->old* is used
712 * to bypass the import if we get the same buffers. This doesn't apply
713 * to DRI3/Wayland, users of image.loader, since the buffer is managed
714 * by the client (no import), and the back buffer is going to change
718 drawable
->old_num
= num_buffers
;
719 drawable
->old_w
= dri_drawable
->w
;
720 drawable
->old_h
= dri_drawable
->h
;
721 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
726 dri2_flush_frontbuffer(struct dri_context
*ctx
,
727 struct dri_drawable
*drawable
,
728 enum st_attachment_type statt
)
730 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
731 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
732 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
733 struct pipe_context
*pipe
= ctx
->st
->pipe
;
735 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
738 if (drawable
->stvis
.samples
> 1) {
739 /* Resolve the front buffer. */
740 dri_pipe_blit(ctx
->st
->pipe
,
741 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
742 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
745 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
746 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
749 pipe
->flush(pipe
, NULL
, 0);
752 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
754 else if (loader
->flushFrontBuffer
) {
755 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
760 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
761 struct dri_context
*ctx
,
762 struct pipe_resource
*res
)
768 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
770 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
773 if (!loader
->lookupEGLImage
)
776 img
= loader
->lookupEGLImage(screen
->sPriv
,
777 handle
, screen
->sPriv
->loaderPrivate
);
783 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
784 int width
, int height
, int format
,
785 int num_handles
, struct winsys_handle
*whandle
,
788 struct dri_screen
*screen
= dri_screen(_screen
);
789 struct pipe_screen
*pscreen
= screen
->base
.screen
;
791 struct pipe_resource templ
;
796 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
798 pf
= dri2_format_to_pipe_format (format
);
799 if (pf
== PIPE_FORMAT_NONE
)
802 img
= CALLOC_STRUCT(__DRIimageRec
);
806 memset(&templ
, 0, sizeof(templ
));
807 templ
.bind
= tex_usage
;
808 templ
.target
= screen
->target
;
809 templ
.last_level
= 0;
811 templ
.array_size
= 1;
813 for (i
= num_handles
- 1; i
>= 0; i
--) {
814 struct pipe_resource
*tex
;
816 /* TODO: something a lot less ugly */
819 templ
.width0
= width
;
820 templ
.height0
= height
;
824 templ
.width0
= width
/ 2;
825 templ
.height0
= height
/ 2;
826 templ
.format
= (num_handles
== 2) ?
827 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
828 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
831 templ
.width0
= width
/ 2;
832 templ
.height0
= height
/ 2;
833 templ
.format
= PIPE_FORMAT_R8_UNORM
;
836 unreachable("too many planes!");
839 tex
= pscreen
->resource_from_handle(pscreen
,
840 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
842 pipe_resource_reference(&img
->texture
, NULL
);
847 tex
->next
= img
->texture
;
853 img
->dri_format
= format
;
855 img
->loader_private
= loaderPrivate
;
861 dri2_create_image_from_name(__DRIscreen
*_screen
,
862 int width
, int height
, int format
,
863 int name
, int pitch
, void *loaderPrivate
)
865 struct winsys_handle whandle
;
868 memset(&whandle
, 0, sizeof(whandle
));
869 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
870 whandle
.handle
= name
;
872 pf
= dri2_format_to_pipe_format (format
);
873 if (pf
== PIPE_FORMAT_NONE
)
876 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
878 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
879 1, &whandle
, loaderPrivate
);
883 dri2_create_image_from_fd(__DRIscreen
*_screen
,
884 int width
, int height
, int fourcc
,
885 int *fds
, int num_fds
, int *strides
,
886 int *offsets
, unsigned *error
,
887 int *dri_components
, void *loaderPrivate
)
889 struct winsys_handle whandles
[3];
891 __DRIimage
*img
= NULL
;
892 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
893 int expected_num_fds
, i
;
896 case __DRI_IMAGE_FOURCC_YUV420
:
897 case __DRI_IMAGE_FOURCC_YVU420
:
898 expected_num_fds
= 3;
900 case __DRI_IMAGE_FOURCC_NV12
:
901 expected_num_fds
= 2;
904 expected_num_fds
= 1;
908 if (num_fds
!= expected_num_fds
) {
909 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
913 format
= convert_fourcc(fourcc
, dri_components
);
915 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
919 memset(whandles
, 0, sizeof(whandles
));
921 for (i
= 0; i
< num_fds
; i
++) {
923 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
927 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
928 whandles
[i
].handle
= (unsigned)fds
[i
];
929 whandles
[i
].stride
= (unsigned)strides
[i
];
930 whandles
[i
].offset
= (unsigned)offsets
[i
];
933 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
934 /* convert to YUV420 by swapping 2nd and 3rd planes: */
935 struct winsys_handle tmp
= whandles
[1];
936 whandles
[1] = whandles
[2];
938 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
941 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
942 num_fds
, whandles
, loaderPrivate
);
944 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
954 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
955 int renderbuffer
, void *loaderPrivate
)
957 struct dri_context
*ctx
= dri_context(context
);
959 if (!ctx
->st
->get_resource_for_egl_image
)
967 dri2_create_image(__DRIscreen
*_screen
,
968 int width
, int height
, int format
,
969 unsigned int use
, void *loaderPrivate
)
971 struct dri_screen
*screen
= dri_screen(_screen
);
973 struct pipe_resource templ
;
977 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
978 if (use
& __DRI_IMAGE_USE_SCANOUT
)
979 tex_usage
|= PIPE_BIND_SCANOUT
;
980 if (use
& __DRI_IMAGE_USE_SHARE
)
981 tex_usage
|= PIPE_BIND_SHARED
;
982 if (use
& __DRI_IMAGE_USE_LINEAR
)
983 tex_usage
|= PIPE_BIND_LINEAR
;
984 if (use
& __DRI_IMAGE_USE_CURSOR
) {
985 if (width
!= 64 || height
!= 64)
987 tex_usage
|= PIPE_BIND_CURSOR
;
990 pf
= dri2_format_to_pipe_format (format
);
991 if (pf
== PIPE_FORMAT_NONE
)
994 img
= CALLOC_STRUCT(__DRIimageRec
);
998 memset(&templ
, 0, sizeof(templ
));
999 templ
.bind
= tex_usage
;
1001 templ
.target
= PIPE_TEXTURE_2D
;
1002 templ
.last_level
= 0;
1003 templ
.width0
= width
;
1004 templ
.height0
= height
;
1006 templ
.array_size
= 1;
1008 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1009 if (!img
->texture
) {
1016 img
->dri_format
= format
;
1017 img
->dri_components
= 0;
1020 img
->loader_private
= loaderPrivate
;
1025 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1027 struct winsys_handle whandle
;
1030 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1031 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1033 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1035 memset(&whandle
, 0, sizeof(whandle
));
1038 case __DRI_IMAGE_ATTRIB_STRIDE
:
1039 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1040 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1041 NULL
, image
->texture
, &whandle
, usage
);
1042 *value
= whandle
.stride
;
1044 case __DRI_IMAGE_ATTRIB_HANDLE
:
1045 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1046 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1047 NULL
, image
->texture
, &whandle
, usage
);
1048 *value
= whandle
.handle
;
1050 case __DRI_IMAGE_ATTRIB_NAME
:
1051 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1052 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1053 NULL
, image
->texture
, &whandle
, usage
);
1054 *value
= whandle
.handle
;
1056 case __DRI_IMAGE_ATTRIB_FD
:
1057 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1058 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1059 NULL
, image
->texture
, &whandle
, usage
))
1062 *value
= whandle
.handle
;
1064 case __DRI_IMAGE_ATTRIB_FORMAT
:
1065 *value
= image
->dri_format
;
1067 case __DRI_IMAGE_ATTRIB_WIDTH
:
1068 *value
= image
->texture
->width0
;
1070 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1071 *value
= image
->texture
->height0
;
1073 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1074 if (image
->dri_components
== 0)
1076 *value
= image
->dri_components
;
1078 case __DRI_IMAGE_ATTRIB_FOURCC
:
1079 *value
= convert_to_fourcc(image
->dri_format
);
1081 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1090 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1094 img
= CALLOC_STRUCT(__DRIimageRec
);
1098 img
->texture
= NULL
;
1099 pipe_resource_reference(&img
->texture
, image
->texture
);
1100 img
->level
= image
->level
;
1101 img
->layer
= image
->layer
;
1102 img
->dri_format
= image
->dri_format
;
1103 /* This should be 0 for sub images, but dup is also used for base images. */
1104 img
->dri_components
= image
->dri_components
;
1105 img
->loader_private
= loaderPrivate
;
1111 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1114 * Gallium drivers are bad at adding usages to the resources
1115 * once opened again in another process, which is the main use
1116 * case for this, so we have to lie.
1125 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1126 int *names
, int num_names
, int *strides
, int *offsets
,
1127 void *loaderPrivate
)
1131 struct winsys_handle whandle
;
1136 format
= convert_fourcc(format
, &dri_components
);
1140 memset(&whandle
, 0, sizeof(whandle
));
1141 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1142 whandle
.handle
= names
[0];
1143 whandle
.stride
= strides
[0];
1144 whandle
.offset
= offsets
[0];
1146 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1147 1, &whandle
, loaderPrivate
);
1151 img
->dri_components
= dri_components
;
1156 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1163 if (image
->dri_components
== 0)
1166 img
= dri2_dup_image(image
, loaderPrivate
);
1170 /* set this to 0 for sub images. */
1171 img
->dri_components
= 0;
1176 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1177 int depth
, int level
, unsigned *error
,
1178 void *loaderPrivate
)
1181 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1182 struct gl_texture_object
*obj
;
1183 struct pipe_resource
*tex
;
1186 obj
= _mesa_lookup_texture(ctx
, texture
);
1187 if (!obj
|| obj
->Target
!= target
) {
1188 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1192 tex
= st_get_texobj_resource(obj
);
1194 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1198 if (target
== GL_TEXTURE_CUBE_MAP
)
1201 _mesa_test_texobj_completeness(ctx
, obj
);
1202 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1203 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1207 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1208 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1212 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1213 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1217 img
= CALLOC_STRUCT(__DRIimageRec
);
1219 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1225 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1227 img
->loader_private
= loaderPrivate
;
1229 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1230 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1235 pipe_resource_reference(&img
->texture
, tex
);
1237 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1242 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1243 int *fds
, int num_fds
, int *strides
, int *offsets
,
1244 void *loaderPrivate
)
1249 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1250 fds
, num_fds
, strides
, offsets
, NULL
,
1251 &dri_components
, loaderPrivate
);
1255 img
->dri_components
= dri_components
;
1260 dri2_from_dma_bufs(__DRIscreen
*screen
,
1261 int width
, int height
, int fourcc
,
1262 int *fds
, int num_fds
,
1263 int *strides
, int *offsets
,
1264 enum __DRIYUVColorSpace yuv_color_space
,
1265 enum __DRISampleRange sample_range
,
1266 enum __DRIChromaSiting horizontal_siting
,
1267 enum __DRIChromaSiting vertical_siting
,
1269 void *loaderPrivate
)
1274 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1275 fds
, num_fds
, strides
, offsets
, error
,
1276 &dri_components
, loaderPrivate
);
1280 img
->yuv_color_space
= yuv_color_space
;
1281 img
->sample_range
= sample_range
;
1282 img
->horizontal_siting
= horizontal_siting
;
1283 img
->vertical_siting
= vertical_siting
;
1284 img
->dri_components
= dri_components
;
1286 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1291 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1292 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1293 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1296 struct dri_context
*ctx
= dri_context(context
);
1297 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1298 struct pipe_screen
*screen
;
1299 struct pipe_fence_handle
*fence
;
1300 struct pipe_blit_info blit
;
1305 memset(&blit
, 0, sizeof(blit
));
1306 blit
.dst
.resource
= dst
->texture
;
1307 blit
.dst
.box
.x
= dstx0
;
1308 blit
.dst
.box
.y
= dsty0
;
1309 blit
.dst
.box
.width
= dstwidth
;
1310 blit
.dst
.box
.height
= dstheight
;
1311 blit
.dst
.box
.depth
= 1;
1312 blit
.dst
.format
= dst
->texture
->format
;
1313 blit
.src
.resource
= src
->texture
;
1314 blit
.src
.box
.x
= srcx0
;
1315 blit
.src
.box
.y
= srcy0
;
1316 blit
.src
.box
.width
= srcwidth
;
1317 blit
.src
.box
.height
= srcheight
;
1318 blit
.src
.box
.depth
= 1;
1319 blit
.src
.format
= src
->texture
->format
;
1320 blit
.mask
= PIPE_MASK_RGBA
;
1321 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1323 pipe
->blit(pipe
, &blit
);
1325 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1326 pipe
->flush_resource(pipe
, dst
->texture
);
1327 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1328 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1329 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1330 pipe
->flush_resource(pipe
, dst
->texture
);
1331 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1332 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1333 screen
->fence_reference(screen
, &fence
, NULL
);
1338 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1339 int x0
, int y0
, int width
, int height
,
1340 unsigned int flags
, int *stride
, void **data
)
1342 struct dri_context
*ctx
= dri_context(context
);
1343 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1344 enum pipe_transfer_usage pipe_access
= 0;
1345 struct pipe_transfer
*trans
;
1348 if (!image
|| !data
|| *data
)
1351 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1352 pipe_access
|= PIPE_TRANSFER_READ
;
1353 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1354 pipe_access
|= PIPE_TRANSFER_WRITE
;
1356 map
= pipe_transfer_map(pipe
, image
->texture
,
1357 0, 0, pipe_access
, x0
, y0
, width
, height
,
1361 *stride
= trans
->stride
;
1368 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1370 struct dri_context
*ctx
= dri_context(context
);
1371 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1373 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1377 dri2_destroy_image(__DRIimage
*img
)
1379 pipe_resource_reference(&img
->texture
, NULL
);
1384 dri2_get_capabilities(__DRIscreen
*_screen
)
1386 struct dri_screen
*screen
= dri_screen(_screen
);
1388 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1391 /* The extension is modified during runtime if DRI_PRIME is detected */
1392 static __DRIimageExtension dri2ImageExtension
= {
1393 .base
= { __DRI_IMAGE
, 12 },
1395 .createImageFromName
= dri2_create_image_from_name
,
1396 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1397 .destroyImage
= dri2_destroy_image
,
1398 .createImage
= dri2_create_image
,
1399 .queryImage
= dri2_query_image
,
1400 .dupImage
= dri2_dup_image
,
1401 .validateUsage
= dri2_validate_usage
,
1402 .createImageFromNames
= dri2_from_names
,
1403 .fromPlanar
= dri2_from_planar
,
1404 .createImageFromTexture
= dri2_create_from_texture
,
1405 .createImageFromFds
= NULL
,
1406 .createImageFromDmaBufs
= NULL
,
1407 .blitImage
= dri2_blit_image
,
1408 .getCapabilities
= dri2_get_capabilities
,
1409 .mapImage
= dri2_map_image
,
1410 .unmapImage
= dri2_unmap_image
,
1415 dri2_is_opencl_interop_loaded_locked(struct dri_screen
*screen
)
1417 return screen
->opencl_dri_event_add_ref
&&
1418 screen
->opencl_dri_event_release
&&
1419 screen
->opencl_dri_event_wait
&&
1420 screen
->opencl_dri_event_get_fence
;
1424 dri2_load_opencl_interop(struct dri_screen
*screen
)
1426 #if defined(RTLD_DEFAULT)
1429 pipe_mutex_lock(screen
->opencl_func_mutex
);
1431 if (dri2_is_opencl_interop_loaded_locked(screen
)) {
1432 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1436 screen
->opencl_dri_event_add_ref
=
1437 dlsym(RTLD_DEFAULT
, "opencl_dri_event_add_ref");
1438 screen
->opencl_dri_event_release
=
1439 dlsym(RTLD_DEFAULT
, "opencl_dri_event_release");
1440 screen
->opencl_dri_event_wait
=
1441 dlsym(RTLD_DEFAULT
, "opencl_dri_event_wait");
1442 screen
->opencl_dri_event_get_fence
=
1443 dlsym(RTLD_DEFAULT
, "opencl_dri_event_get_fence");
1445 success
= dri2_is_opencl_interop_loaded_locked(screen
);
1446 pipe_mutex_unlock(screen
->opencl_func_mutex
);
1454 struct dri_screen
*driscreen
;
1455 struct pipe_fence_handle
*pipe_fence
;
1460 dri2_create_fence(__DRIcontext
*_ctx
)
1462 struct pipe_context
*ctx
= dri_context(_ctx
)->st
->pipe
;
1463 struct dri2_fence
*fence
= CALLOC_STRUCT(dri2_fence
);
1468 ctx
->flush(ctx
, &fence
->pipe_fence
, 0);
1470 if (!fence
->pipe_fence
) {
1475 fence
->driscreen
= dri_screen(_ctx
->driScreenPriv
);
1480 dri2_get_fence_from_cl_event(__DRIscreen
*_screen
, intptr_t cl_event
)
1482 struct dri_screen
*driscreen
= dri_screen(_screen
);
1483 struct dri2_fence
*fence
;
1485 if (!dri2_load_opencl_interop(driscreen
))
1488 fence
= CALLOC_STRUCT(dri2_fence
);
1492 fence
->cl_event
= (void*)cl_event
;
1494 if (!driscreen
->opencl_dri_event_add_ref(fence
->cl_event
)) {
1499 fence
->driscreen
= driscreen
;
1504 dri2_destroy_fence(__DRIscreen
*_screen
, void *_fence
)
1506 struct dri_screen
*driscreen
= dri_screen(_screen
);
1507 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1508 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1510 if (fence
->pipe_fence
)
1511 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
1512 else if (fence
->cl_event
)
1513 driscreen
->opencl_dri_event_release(fence
->cl_event
);
1521 dri2_client_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
,
1524 struct dri2_fence
*fence
= (struct dri2_fence
*)_fence
;
1525 struct dri_screen
*driscreen
= fence
->driscreen
;
1526 struct pipe_screen
*screen
= driscreen
->base
.screen
;
1528 /* No need to flush. The context was flushed when the fence was created. */
1530 if (fence
->pipe_fence
)
1531 return screen
->fence_finish(screen
, NULL
, fence
->pipe_fence
, timeout
);
1532 else if (fence
->cl_event
) {
1533 struct pipe_fence_handle
*pipe_fence
=
1534 driscreen
->opencl_dri_event_get_fence(fence
->cl_event
);
1537 return screen
->fence_finish(screen
, NULL
, pipe_fence
, timeout
);
1539 return driscreen
->opencl_dri_event_wait(fence
->cl_event
, timeout
);
1548 dri2_server_wait_sync(__DRIcontext
*_ctx
, void *_fence
, unsigned flags
)
1550 /* AFAIK, no driver currently supports parallel context execution. */
1553 static __DRI2fenceExtension dri2FenceExtension
= {
1554 .base
= { __DRI2_FENCE
, 1 },
1556 .create_fence
= dri2_create_fence
,
1557 .get_fence_from_cl_event
= dri2_get_fence_from_cl_event
,
1558 .destroy_fence
= dri2_destroy_fence
,
1559 .client_wait_sync
= dri2_client_wait_sync
,
1560 .server_wait_sync
= dri2_server_wait_sync
1563 static const __DRIrobustnessExtension dri2Robustness
= {
1564 .base
= { __DRI2_ROBUSTNESS
, 1 }
1568 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1569 struct mesa_glinterop_device_info
*out
)
1571 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1573 /* There is no version 0, thus we do not support it */
1574 if (out
->version
== 0)
1575 return MESA_GLINTEROP_INVALID_VERSION
;
1577 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1578 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1579 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1580 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1582 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1583 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1585 /* Instruct the caller that we support up-to version one of the interface */
1588 return MESA_GLINTEROP_SUCCESS
;
1592 dri2_interop_export_object(__DRIcontext
*_ctx
,
1593 struct mesa_glinterop_export_in
*in
,
1594 struct mesa_glinterop_export_out
*out
)
1596 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1597 struct pipe_screen
*screen
= st
->pipe
->screen
;
1598 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1599 struct pipe_resource
*res
= NULL
;
1600 struct winsys_handle whandle
;
1601 unsigned target
, usage
;
1604 /* There is no version 0, thus we do not support it */
1605 if (in
->version
== 0 || out
->version
== 0)
1606 return MESA_GLINTEROP_INVALID_VERSION
;
1608 /* Validate the target. */
1609 switch (in
->target
) {
1610 case GL_TEXTURE_BUFFER
:
1614 case GL_TEXTURE_RECTANGLE
:
1615 case GL_TEXTURE_1D_ARRAY
:
1616 case GL_TEXTURE_2D_ARRAY
:
1617 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1618 case GL_TEXTURE_CUBE_MAP
:
1619 case GL_TEXTURE_2D_MULTISAMPLE
:
1620 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1621 case GL_TEXTURE_EXTERNAL_OES
:
1622 case GL_RENDERBUFFER
:
1623 case GL_ARRAY_BUFFER
:
1624 target
= in
->target
;
1626 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1627 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1628 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1629 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1630 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1631 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1632 target
= GL_TEXTURE_CUBE_MAP
;
1635 return MESA_GLINTEROP_INVALID_TARGET
;
1638 /* Validate the simple case of miplevel. */
1639 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1641 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1643 /* Validate the OpenGL object and get pipe_resource. */
1644 mtx_lock(&ctx
->Shared
->Mutex
);
1646 if (target
== GL_ARRAY_BUFFER
) {
1649 * The error checking is based on the documentation of
1650 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1652 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1654 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1655 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1656 * a GL buffer object but does not have an existing data store or
1657 * the size of the buffer is 0."
1659 if (!buf
|| buf
->Size
== 0) {
1660 mtx_unlock(&ctx
->Shared
->Mutex
);
1661 return MESA_GLINTEROP_INVALID_OBJECT
;
1664 res
= st_buffer_object(buf
)->buffer
;
1666 /* this shouldn't happen */
1667 mtx_unlock(&ctx
->Shared
->Mutex
);
1668 return MESA_GLINTEROP_INVALID_OBJECT
;
1671 out
->buf_offset
= 0;
1672 out
->buf_size
= buf
->Size
;
1674 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1675 } else if (target
== GL_RENDERBUFFER
) {
1678 * The error checking is based on the documentation of
1679 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1681 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1683 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1684 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1685 * object or if the width or height of renderbuffer is zero."
1687 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1688 mtx_unlock(&ctx
->Shared
->Mutex
);
1689 return MESA_GLINTEROP_INVALID_OBJECT
;
1692 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1693 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1694 * renderbuffer object."
1696 if (rb
->NumSamples
> 1) {
1697 mtx_unlock(&ctx
->Shared
->Mutex
);
1698 return MESA_GLINTEROP_INVALID_OPERATION
;
1701 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1702 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1703 * required by the OpenCL implementation on the device."
1705 res
= st_renderbuffer(rb
)->texture
;
1707 mtx_unlock(&ctx
->Shared
->Mutex
);
1708 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1711 out
->internal_format
= rb
->InternalFormat
;
1712 out
->view_minlevel
= 0;
1713 out
->view_numlevels
= 1;
1714 out
->view_minlayer
= 0;
1715 out
->view_numlayers
= 1;
1719 * The error checking is based on the documentation of
1720 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1722 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1725 _mesa_test_texobj_completeness(ctx
, obj
);
1727 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1728 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1729 * type matches texture_target, if the specified miplevel of texture
1730 * is not defined, or if the width or height of the specified
1731 * miplevel is zero or if the GL texture object is incomplete."
1734 obj
->Target
!= target
||
1735 !obj
->_BaseComplete
||
1736 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1737 mtx_unlock(&ctx
->Shared
->Mutex
);
1738 return MESA_GLINTEROP_INVALID_OBJECT
;
1741 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1742 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1743 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1744 * implementations); or greater than the value of q (for both OpenGL
1745 * and OpenGL ES). levelbase and q are defined for the texture in
1746 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1747 * specification and section 3.7.10 of the OpenGL ES 2.0."
1749 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1750 mtx_unlock(&ctx
->Shared
->Mutex
);
1751 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1754 if (!st_finalize_texture(ctx
, st
->pipe
, obj
)) {
1755 mtx_unlock(&ctx
->Shared
->Mutex
);
1756 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1759 res
= st_get_texobj_resource(obj
);
1761 /* Incomplete texture buffer object? This shouldn't really occur. */
1762 mtx_unlock(&ctx
->Shared
->Mutex
);
1763 return MESA_GLINTEROP_INVALID_OBJECT
;
1766 if (target
== GL_TEXTURE_BUFFER
) {
1767 out
->internal_format
= obj
->BufferObjectFormat
;
1768 out
->buf_offset
= obj
->BufferOffset
;
1769 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1772 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1774 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1775 out
->view_minlevel
= obj
->MinLevel
;
1776 out
->view_numlevels
= obj
->NumLevels
;
1777 out
->view_minlayer
= obj
->MinLayer
;
1778 out
->view_numlayers
= obj
->NumLayers
;
1782 /* Get the handle. */
1783 switch (in
->access
) {
1784 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1785 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1787 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1788 usage
= PIPE_HANDLE_USAGE_READ
;
1790 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1791 usage
= PIPE_HANDLE_USAGE_WRITE
;
1797 memset(&whandle
, 0, sizeof(whandle
));
1798 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1800 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1802 mtx_unlock(&ctx
->Shared
->Mutex
);
1805 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1807 out
->dmabuf_fd
= whandle
.handle
;
1808 out
->out_driver_data_written
= 0;
1810 if (res
->target
== PIPE_BUFFER
)
1811 out
->buf_offset
+= whandle
.offset
;
1813 /* Instruct the caller that we support up-to version one of the interface */
1817 return MESA_GLINTEROP_SUCCESS
;
1820 static const __DRI2interopExtension dri2InteropExtension
= {
1821 .base
= { __DRI2_INTEROP
, 1 },
1822 .query_device_info
= dri2_interop_query_device_info
,
1823 .export_object
= dri2_interop_export_object
1827 * Backend function init_screen.
1830 static const __DRIextension
*dri_screen_extensions
[] = {
1831 &driTexBufferExtension
.base
,
1832 &dri2FlushExtension
.base
,
1833 &dri2ImageExtension
.base
,
1834 &dri2RendererQueryExtension
.base
,
1835 &dri2ConfigQueryExtension
.base
,
1836 &dri2ThrottleExtension
.base
,
1837 &dri2FenceExtension
.base
,
1838 &dri2InteropExtension
.base
,
1842 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1843 &driTexBufferExtension
.base
,
1844 &dri2FlushExtension
.base
,
1845 &dri2ImageExtension
.base
,
1846 &dri2RendererQueryExtension
.base
,
1847 &dri2ConfigQueryExtension
.base
,
1848 &dri2ThrottleExtension
.base
,
1849 &dri2FenceExtension
.base
,
1850 &dri2InteropExtension
.base
,
1851 &dri2Robustness
.base
,
1856 * This is the driver specific part of the createNewScreen entry point.
1858 * Returns the struct gl_config supported by this driver.
1860 static const __DRIconfig
**
1861 dri2_init_screen(__DRIscreen
* sPriv
)
1863 const __DRIconfig
**configs
;
1864 struct dri_screen
*screen
;
1865 struct pipe_screen
*pscreen
= NULL
;
1866 const struct drm_conf_ret
*throttle_ret
;
1867 const struct drm_conf_ret
*dmabuf_ret
;
1870 screen
= CALLOC_STRUCT(dri_screen
);
1874 screen
->sPriv
= sPriv
;
1875 screen
->fd
= sPriv
->fd
;
1876 pipe_mutex_init(screen
->opencl_func_mutex
);
1878 sPriv
->driverPrivate
= (void *)screen
;
1880 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1883 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
))
1884 pscreen
= pipe_loader_create_screen(screen
->dev
);
1889 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1890 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1892 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1893 screen
->throttling_enabled
= TRUE
;
1894 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1897 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1900 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1901 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1902 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1903 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1907 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
1908 sPriv
->extensions
= dri_robust_screen_extensions
;
1909 screen
->has_reset_status_query
= true;
1912 sPriv
->extensions
= dri_screen_extensions
;
1914 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1916 goto destroy_screen
;
1918 screen
->can_share_buffer
= true;
1919 screen
->auto_fake_front
= dri_with_format(sPriv
);
1920 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1921 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1926 dri_destroy_screen_helper(screen
);
1930 pipe_loader_release(&screen
->dev
, 1);
1940 * This is the driver specific part of the createNewScreen entry point.
1942 * Returns the struct gl_config supported by this driver.
1944 static const __DRIconfig
**
1945 dri_kms_init_screen(__DRIscreen
* sPriv
)
1947 #if defined(GALLIUM_SOFTPIPE)
1948 const __DRIconfig
**configs
;
1949 struct dri_screen
*screen
;
1950 struct pipe_screen
*pscreen
= NULL
;
1954 screen
= CALLOC_STRUCT(dri_screen
);
1958 screen
->sPriv
= sPriv
;
1959 screen
->fd
= sPriv
->fd
;
1961 sPriv
->driverPrivate
= (void *)screen
;
1963 if (screen
->fd
< 0 || (fd
= dup(screen
->fd
)) < 0)
1966 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
1967 pscreen
= pipe_loader_create_screen(screen
->dev
);
1972 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1973 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1974 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1975 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1978 sPriv
->extensions
= dri_screen_extensions
;
1980 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1982 goto destroy_screen
;
1984 screen
->can_share_buffer
= false;
1985 screen
->auto_fake_front
= dri_with_format(sPriv
);
1986 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1987 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1992 dri_destroy_screen_helper(screen
);
1996 pipe_loader_release(&screen
->dev
, 1);
2002 #endif // GALLIUM_SOFTPIPE
2007 dri2_create_buffer(__DRIscreen
* sPriv
,
2008 __DRIdrawable
* dPriv
,
2009 const struct gl_config
* visual
, boolean isPixmap
)
2011 struct dri_drawable
*drawable
= NULL
;
2013 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2016 drawable
= dPriv
->driverPrivate
;
2018 drawable
->allocate_textures
= dri2_allocate_textures
;
2019 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2020 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2026 * DRI driver virtual function table.
2028 * DRI versions differ in their implementation of init_screen and swap_buffers.
2030 const struct __DriverAPIRec galliumdrm_driver_api
= {
2031 .InitScreen
= dri2_init_screen
,
2032 .DestroyScreen
= dri_destroy_screen
,
2033 .CreateContext
= dri_create_context
,
2034 .DestroyContext
= dri_destroy_context
,
2035 .CreateBuffer
= dri2_create_buffer
,
2036 .DestroyBuffer
= dri_destroy_buffer
,
2037 .MakeCurrent
= dri_make_current
,
2038 .UnbindContext
= dri_unbind_context
,
2040 .AllocateBuffer
= dri2_allocate_buffer
,
2041 .ReleaseBuffer
= dri2_release_buffer
,
2045 * DRI driver virtual function table.
2047 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2048 * hook. The latter is used to explicitly initialise the kms_swrast driver
2049 * rather than selecting the approapriate driver as suggested by the loader.
2051 const struct __DriverAPIRec dri_kms_driver_api
= {
2052 .InitScreen
= dri_kms_init_screen
,
2053 .DestroyScreen
= dri_destroy_screen
,
2054 .CreateContext
= dri_create_context
,
2055 .DestroyContext
= dri_destroy_context
,
2056 .CreateBuffer
= dri2_create_buffer
,
2057 .DestroyBuffer
= dri_destroy_buffer
,
2058 .MakeCurrent
= dri_make_current
,
2059 .UnbindContext
= dri_unbind_context
,
2061 .AllocateBuffer
= dri2_allocate_buffer
,
2062 .ReleaseBuffer
= dri2_release_buffer
,
2065 /* This is the table of extensions that the loader will dlsym() for. */
2066 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2067 &driCoreExtension
.base
,
2068 &driImageDriverExtension
.base
,
2069 &driDRI2Extension
.base
,
2070 &gallium_config_options
.base
,
2071 &dri2FenceExtension
.base
,
2075 /* vim: set sw=3 ts=8 sts=3 expandtab: */