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_helpers.h"
49 #include "dri_drawable.h"
50 #include "dri_query_renderer.h"
51 #include "dri2_buffer.h"
53 #ifndef DRM_FORMAT_MOD_INVALID
54 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
57 static const int fourcc_formats
[] = {
58 __DRI_IMAGE_FOURCC_ARGB2101010
,
59 __DRI_IMAGE_FOURCC_XRGB2101010
,
60 __DRI_IMAGE_FOURCC_ARGB8888
,
61 __DRI_IMAGE_FOURCC_ABGR8888
,
62 __DRI_IMAGE_FOURCC_SARGB8888
,
63 __DRI_IMAGE_FOURCC_XRGB8888
,
64 __DRI_IMAGE_FOURCC_XBGR8888
,
65 __DRI_IMAGE_FOURCC_ARGB1555
,
66 __DRI_IMAGE_FOURCC_RGB565
,
67 __DRI_IMAGE_FOURCC_R8
,
68 __DRI_IMAGE_FOURCC_R16
,
69 __DRI_IMAGE_FOURCC_GR88
,
70 __DRI_IMAGE_FOURCC_GR1616
,
71 __DRI_IMAGE_FOURCC_YUV410
,
72 __DRI_IMAGE_FOURCC_YUV411
,
73 __DRI_IMAGE_FOURCC_YUV420
,
74 __DRI_IMAGE_FOURCC_YUV422
,
75 __DRI_IMAGE_FOURCC_YUV444
,
76 __DRI_IMAGE_FOURCC_YVU410
,
77 __DRI_IMAGE_FOURCC_YVU411
,
78 __DRI_IMAGE_FOURCC_YVU420
,
79 __DRI_IMAGE_FOURCC_YVU422
,
80 __DRI_IMAGE_FOURCC_YVU444
,
81 __DRI_IMAGE_FOURCC_NV12
,
82 __DRI_IMAGE_FOURCC_NV16
,
83 __DRI_IMAGE_FOURCC_YUYV
86 static int convert_fourcc(int format
, int *dri_components_p
)
90 case __DRI_IMAGE_FOURCC_RGB565
:
91 format
= __DRI_IMAGE_FORMAT_RGB565
;
92 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
94 case __DRI_IMAGE_FOURCC_ARGB8888
:
95 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
96 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
98 case __DRI_IMAGE_FOURCC_XRGB8888
:
99 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
100 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
102 case __DRI_IMAGE_FOURCC_ABGR8888
:
103 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
104 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
106 case __DRI_IMAGE_FOURCC_XBGR8888
:
107 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
108 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
110 case __DRI_IMAGE_FOURCC_ARGB2101010
:
111 format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
112 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
114 case __DRI_IMAGE_FOURCC_XRGB2101010
:
115 format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
116 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
118 case __DRI_IMAGE_FOURCC_R8
:
119 format
= __DRI_IMAGE_FORMAT_R8
;
120 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
122 case __DRI_IMAGE_FOURCC_GR88
:
123 format
= __DRI_IMAGE_FORMAT_GR88
;
124 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
126 case __DRI_IMAGE_FOURCC_R16
:
127 format
= __DRI_IMAGE_FORMAT_R16
;
128 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
130 case __DRI_IMAGE_FOURCC_GR1616
:
131 format
= __DRI_IMAGE_FORMAT_GR1616
;
132 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
134 case __DRI_IMAGE_FOURCC_YUYV
:
135 format
= __DRI_IMAGE_FORMAT_YUYV
;
136 dri_components
= __DRI_IMAGE_COMPONENTS_Y_XUXV
;
139 * For multi-planar YUV formats, we return the format of the first
140 * plane only. Since there is only one caller which supports multi-
141 * planar YUV it gets to figure out the remaining planes on it's
144 case __DRI_IMAGE_FOURCC_YUV420
:
145 case __DRI_IMAGE_FOURCC_YVU420
:
146 format
= __DRI_IMAGE_FORMAT_R8
;
147 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
149 case __DRI_IMAGE_FOURCC_NV12
:
150 format
= __DRI_IMAGE_FORMAT_R8
;
151 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
156 *dri_components_p
= dri_components
;
160 /* NOTE this probably isn't going to do the right thing for YUV images
161 * (but I think the same can be said for intel_query_image()). I think
162 * only needed for exporting dmabuf's, so I think I won't loose much
165 static int convert_to_fourcc(int format
)
168 case __DRI_IMAGE_FORMAT_RGB565
:
169 format
= __DRI_IMAGE_FOURCC_RGB565
;
171 case __DRI_IMAGE_FORMAT_ARGB8888
:
172 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
174 case __DRI_IMAGE_FORMAT_XRGB8888
:
175 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
177 case __DRI_IMAGE_FORMAT_ABGR8888
:
178 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
180 case __DRI_IMAGE_FORMAT_XBGR8888
:
181 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
183 case __DRI_IMAGE_FORMAT_ARGB2101010
:
184 format
= __DRI_IMAGE_FOURCC_ARGB2101010
;
186 case __DRI_IMAGE_FORMAT_XRGB2101010
:
187 format
= __DRI_IMAGE_FOURCC_XRGB2101010
;
189 case __DRI_IMAGE_FORMAT_R8
:
190 format
= __DRI_IMAGE_FOURCC_R8
;
192 case __DRI_IMAGE_FORMAT_GR88
:
193 format
= __DRI_IMAGE_FOURCC_GR88
;
201 static enum pipe_format
dri2_format_to_pipe_format (int format
)
206 case __DRI_IMAGE_FORMAT_RGB565
:
207 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
209 case __DRI_IMAGE_FORMAT_XRGB8888
:
210 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
212 case __DRI_IMAGE_FORMAT_ARGB8888
:
213 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
215 case __DRI_IMAGE_FORMAT_XBGR8888
:
216 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
218 case __DRI_IMAGE_FORMAT_ABGR8888
:
219 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
221 case __DRI_IMAGE_FORMAT_XRGB2101010
:
222 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
224 case __DRI_IMAGE_FORMAT_ARGB2101010
:
225 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
227 case __DRI_IMAGE_FORMAT_R8
:
228 pf
= PIPE_FORMAT_R8_UNORM
;
230 case __DRI_IMAGE_FORMAT_GR88
:
231 pf
= PIPE_FORMAT_RG88_UNORM
;
233 case __DRI_IMAGE_FORMAT_R16
:
234 pf
= PIPE_FORMAT_R16_UNORM
;
236 case __DRI_IMAGE_FORMAT_GR1616
:
237 pf
= PIPE_FORMAT_R16G16_UNORM
;
239 case __DRI_IMAGE_FORMAT_YUYV
:
240 pf
= PIPE_FORMAT_YUYV
;
243 pf
= PIPE_FORMAT_NONE
;
250 static enum pipe_format
fourcc_to_pipe_format(int fourcc
)
255 case __DRI_IMAGE_FOURCC_R8
:
256 pf
= PIPE_FORMAT_R8_UNORM
;
258 case __DRI_IMAGE_FOURCC_GR88
:
259 pf
= PIPE_FORMAT_RG88_UNORM
;
261 case __DRI_IMAGE_FOURCC_ARGB1555
:
262 pf
= PIPE_FORMAT_B5G5R5A1_UNORM
;
264 case __DRI_IMAGE_FOURCC_R16
:
265 pf
= PIPE_FORMAT_R16_UNORM
;
267 case __DRI_IMAGE_FOURCC_GR1616
:
268 pf
= PIPE_FORMAT_RG1616_UNORM
;
270 case __DRI_IMAGE_FOURCC_RGB565
:
271 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
273 case __DRI_IMAGE_FOURCC_ARGB8888
:
274 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
276 case __DRI_IMAGE_FOURCC_XRGB8888
:
277 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
279 case __DRI_IMAGE_FOURCC_ABGR8888
:
280 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
282 case __DRI_IMAGE_FOURCC_XBGR8888
:
283 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
285 case __DRI_IMAGE_FOURCC_ARGB2101010
:
286 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
288 case __DRI_IMAGE_FOURCC_XRGB2101010
:
289 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
292 case __DRI_IMAGE_FOURCC_NV12
:
293 pf
= PIPE_FORMAT_NV12
;
295 case __DRI_IMAGE_FOURCC_YUYV
:
296 pf
= PIPE_FORMAT_YUYV
;
298 case __DRI_IMAGE_FOURCC_YUV420
:
299 case __DRI_IMAGE_FOURCC_YVU420
:
300 pf
= PIPE_FORMAT_YV12
;
303 case __DRI_IMAGE_FOURCC_SARGB8888
:
304 case __DRI_IMAGE_FOURCC_YUV410
:
305 case __DRI_IMAGE_FOURCC_YUV411
:
306 case __DRI_IMAGE_FOURCC_YUV422
:
307 case __DRI_IMAGE_FOURCC_YUV444
:
308 case __DRI_IMAGE_FOURCC_NV16
:
309 case __DRI_IMAGE_FOURCC_YVU410
:
310 case __DRI_IMAGE_FOURCC_YVU411
:
311 case __DRI_IMAGE_FOURCC_YVU422
:
312 case __DRI_IMAGE_FOURCC_YVU444
:
314 pf
= PIPE_FORMAT_NONE
;
321 * DRI2 flush extension.
324 dri2_flush_drawable(__DRIdrawable
*dPriv
)
326 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
330 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
332 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
334 dri2InvalidateDrawable(dPriv
);
335 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
336 drawable
->texture_mask
= 0;
338 p_atomic_inc(&drawable
->base
.stamp
);
341 static const __DRI2flushExtension dri2FlushExtension
= {
342 .base
= { __DRI2_FLUSH
, 4 },
344 .flush
= dri2_flush_drawable
,
345 .invalidate
= dri2_invalidate_drawable
,
346 .flush_with_flags
= dri_flush
,
350 * Retrieve __DRIbuffer from the DRI loader.
353 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
354 const enum st_attachment_type
*atts
,
357 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
358 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
360 __DRIbuffer
*buffers
;
362 unsigned attachments
[10];
363 unsigned num_attachments
, i
;
366 with_format
= dri_with_format(drawable
->sPriv
);
370 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
372 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
374 for (i
= 0; i
< *count
; i
++) {
375 enum pipe_format format
;
379 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
380 if (format
== PIPE_FORMAT_NONE
)
384 case ST_ATTACHMENT_FRONT_LEFT
:
388 att
= __DRI_BUFFER_FRONT_LEFT
;
390 case ST_ATTACHMENT_BACK_LEFT
:
391 att
= __DRI_BUFFER_BACK_LEFT
;
393 case ST_ATTACHMENT_FRONT_RIGHT
:
394 att
= __DRI_BUFFER_FRONT_RIGHT
;
396 case ST_ATTACHMENT_BACK_RIGHT
:
397 att
= __DRI_BUFFER_BACK_RIGHT
;
404 * In this switch statement we must support all formats that
405 * may occur as the stvis->color_format.
408 case PIPE_FORMAT_B10G10R10A2_UNORM
:
409 case PIPE_FORMAT_BGRA8888_UNORM
:
410 case PIPE_FORMAT_RGBA8888_UNORM
:
413 case PIPE_FORMAT_B10G10R10X2_UNORM
:
416 case PIPE_FORMAT_BGRX8888_UNORM
:
417 case PIPE_FORMAT_RGBX8888_UNORM
:
420 case PIPE_FORMAT_B5G6R5_UNORM
:
424 depth
= util_format_get_blocksizebits(format
);
425 assert(!"Unexpected format in dri2_drawable_get_buffers()");
428 attachments
[num_attachments
++] = att
;
430 attachments
[num_attachments
++] = depth
;
435 num_attachments
/= 2;
436 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
437 &dri_drawable
->w
, &dri_drawable
->h
,
438 attachments
, num_attachments
,
439 &num_buffers
, dri_drawable
->loaderPrivate
);
442 buffers
= loader
->getBuffers(dri_drawable
,
443 &dri_drawable
->w
, &dri_drawable
->h
,
444 attachments
, num_attachments
,
445 &num_buffers
, dri_drawable
->loaderPrivate
);
449 *count
= num_buffers
;
455 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
456 struct __DRIimageList
*images
,
457 const enum st_attachment_type
*statts
,
458 unsigned statts_count
)
460 __DRIdrawable
*dPriv
= drawable
->dPriv
;
461 __DRIscreen
*sPriv
= drawable
->sPriv
;
462 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
464 uint32_t buffer_mask
= 0;
467 for (i
= 0; i
< statts_count
; i
++) {
468 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
469 if (pf
== PIPE_FORMAT_NONE
)
473 case ST_ATTACHMENT_FRONT_LEFT
:
474 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
476 case ST_ATTACHMENT_BACK_LEFT
:
477 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
484 case PIPE_FORMAT_B5G6R5_UNORM
:
485 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
487 case PIPE_FORMAT_BGRX8888_UNORM
:
488 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
490 case PIPE_FORMAT_BGRA8888_UNORM
:
491 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
493 case PIPE_FORMAT_RGBX8888_UNORM
:
494 image_format
= __DRI_IMAGE_FORMAT_XBGR8888
;
496 case PIPE_FORMAT_RGBA8888_UNORM
:
497 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
499 case PIPE_FORMAT_B10G10R10X2_UNORM
:
500 image_format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
502 case PIPE_FORMAT_B10G10R10A2_UNORM
:
503 image_format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
506 image_format
= __DRI_IMAGE_FORMAT_NONE
;
511 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
512 (uint32_t *) &drawable
->base
.stamp
,
513 dPriv
->loaderPrivate
, buffer_mask
,
518 dri2_allocate_buffer(__DRIscreen
*sPriv
,
519 unsigned attachment
, unsigned format
,
520 int width
, int height
)
522 struct dri_screen
*screen
= dri_screen(sPriv
);
523 struct dri2_buffer
*buffer
;
524 struct pipe_resource templ
;
527 struct winsys_handle whandle
;
529 switch (attachment
) {
530 case __DRI_BUFFER_FRONT_LEFT
:
531 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
532 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
534 case __DRI_BUFFER_BACK_LEFT
:
535 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
537 case __DRI_BUFFER_DEPTH
:
538 case __DRI_BUFFER_DEPTH_STENCIL
:
539 case __DRI_BUFFER_STENCIL
:
540 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
544 /* because we get the handle and stride */
545 bind
|= PIPE_BIND_SHARED
;
549 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
552 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
555 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
558 pf
= PIPE_FORMAT_Z16_UNORM
;
564 buffer
= CALLOC_STRUCT(dri2_buffer
);
568 memset(&templ
, 0, sizeof(templ
));
571 templ
.target
= PIPE_TEXTURE_2D
;
572 templ
.last_level
= 0;
573 templ
.width0
= width
;
574 templ
.height0
= height
;
576 templ
.array_size
= 1;
579 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
580 if (!buffer
->resource
) {
585 memset(&whandle
, 0, sizeof(whandle
));
586 if (screen
->can_share_buffer
)
587 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
589 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
591 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
592 buffer
->resource
, &whandle
,
593 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
595 buffer
->base
.attachment
= attachment
;
596 buffer
->base
.name
= whandle
.handle
;
597 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
598 buffer
->base
.pitch
= whandle
.stride
;
600 return &buffer
->base
;
604 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
606 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
608 pipe_resource_reference(&buffer
->resource
, NULL
);
613 * Backend functions for st_framebuffer interface.
617 dri2_allocate_textures(struct dri_context
*ctx
,
618 struct dri_drawable
*drawable
,
619 const enum st_attachment_type
*statts
,
620 unsigned statts_count
)
622 __DRIscreen
*sPriv
= drawable
->sPriv
;
623 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
624 struct dri_screen
*screen
= dri_screen(sPriv
);
625 struct pipe_resource templ
;
626 boolean alloc_depthstencil
= FALSE
;
628 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
629 /* Image specific variables */
630 struct __DRIimageList images
;
631 /* Dri2 specific variables */
632 __DRIbuffer
*buffers
= NULL
;
633 struct winsys_handle whandle
;
634 unsigned num_buffers
= statts_count
;
636 /* First get the buffers from the loader */
638 if (!dri_image_drawable_get_buffers(drawable
, &images
,
639 statts
, statts_count
))
643 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
644 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
645 drawable
->old_w
== dri_drawable
->w
&&
646 drawable
->old_h
== dri_drawable
->h
&&
647 memcmp(drawable
->old
, buffers
,
648 sizeof(__DRIbuffer
) * num_buffers
) == 0))
652 /* Second clean useless resources*/
654 /* See if we need a depth-stencil buffer. */
655 for (i
= 0; i
< statts_count
; i
++) {
656 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
657 alloc_depthstencil
= TRUE
;
662 /* Delete the resources we won't need. */
663 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
664 /* Don't delete the depth-stencil buffer, we can reuse it. */
665 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
668 /* Flush the texture before unreferencing, so that other clients can
669 * see what the driver has rendered.
671 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
672 struct pipe_context
*pipe
= ctx
->st
->pipe
;
673 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
676 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
679 if (drawable
->stvis
.samples
> 1) {
680 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
683 /* Don't delete MSAA resources for the attachments which are enabled,
684 * we can reuse them. */
685 for (j
= 0; j
< statts_count
; j
++) {
686 if (i
== statts
[j
]) {
693 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
698 /* Third use the buffers retrieved to fill the drawable info */
700 memset(&templ
, 0, sizeof(templ
));
701 templ
.target
= screen
->target
;
702 templ
.last_level
= 0;
704 templ
.array_size
= 1;
707 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
708 struct pipe_resource
**buf
=
709 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
710 struct pipe_resource
*texture
= images
.front
->texture
;
712 dri_drawable
->w
= texture
->width0
;
713 dri_drawable
->h
= texture
->height0
;
715 pipe_resource_reference(buf
, texture
);
718 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
719 struct pipe_resource
**buf
=
720 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
721 struct pipe_resource
*texture
= images
.back
->texture
;
723 dri_drawable
->w
= texture
->width0
;
724 dri_drawable
->h
= texture
->height0
;
726 pipe_resource_reference(buf
, texture
);
729 /* Note: if there is both a back and a front buffer,
730 * then they have the same size.
732 templ
.width0
= dri_drawable
->w
;
733 templ
.height0
= dri_drawable
->h
;
736 memset(&whandle
, 0, sizeof(whandle
));
738 /* Process DRI-provided buffers and get pipe_resources. */
739 for (i
= 0; i
< num_buffers
; i
++) {
740 __DRIbuffer
*buf
= &buffers
[i
];
741 enum st_attachment_type statt
;
742 enum pipe_format format
;
744 switch (buf
->attachment
) {
745 case __DRI_BUFFER_FRONT_LEFT
:
746 if (!screen
->auto_fake_front
) {
747 continue; /* invalid attachment */
750 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
751 statt
= ST_ATTACHMENT_FRONT_LEFT
;
753 case __DRI_BUFFER_BACK_LEFT
:
754 statt
= ST_ATTACHMENT_BACK_LEFT
;
757 continue; /* invalid attachment */
760 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
761 if (format
== PIPE_FORMAT_NONE
)
764 /* dri2_drawable_get_buffers has already filled dri_drawable->w
765 * and dri_drawable->h */
766 templ
.width0
= dri_drawable
->w
;
767 templ
.height0
= dri_drawable
->h
;
768 templ
.format
= format
;
770 whandle
.handle
= buf
->name
;
771 whandle
.stride
= buf
->pitch
;
773 if (screen
->can_share_buffer
)
774 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
776 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
777 drawable
->textures
[statt
] =
778 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
780 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
781 assert(drawable
->textures
[statt
]);
785 /* Allocate private MSAA colorbuffers. */
786 if (drawable
->stvis
.samples
> 1) {
787 for (i
= 0; i
< statts_count
; i
++) {
788 enum st_attachment_type statt
= statts
[i
];
790 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
793 if (drawable
->textures
[statt
]) {
794 templ
.format
= drawable
->textures
[statt
]->format
;
795 templ
.bind
= drawable
->textures
[statt
]->bind
&
796 ~(PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
797 templ
.nr_samples
= drawable
->stvis
.samples
;
799 /* Try to reuse the resource.
800 * (the other resource parameters should be constant)
802 if (!drawable
->msaa_textures
[statt
] ||
803 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
804 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
805 /* Allocate a new one. */
806 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
808 drawable
->msaa_textures
[statt
] =
809 screen
->base
.screen
->resource_create(screen
->base
.screen
,
811 assert(drawable
->msaa_textures
[statt
]);
813 /* If there are any MSAA resources, we should initialize them
814 * such that they contain the same data as the single-sample
815 * resources we just got from the X server.
817 * The reason for this is that the state tracker (and
818 * therefore the app) can access the MSAA resources only.
819 * The single-sample resources are not exposed
820 * to the state tracker.
823 dri_pipe_blit(ctx
->st
->pipe
,
824 drawable
->msaa_textures
[statt
],
825 drawable
->textures
[statt
]);
829 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
834 /* Allocate a private depth-stencil buffer. */
835 if (alloc_depthstencil
) {
836 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
837 struct pipe_resource
**zsbuf
;
838 enum pipe_format format
;
841 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
844 templ
.format
= format
;
845 templ
.bind
= bind
& ~PIPE_BIND_SHARED
;
847 if (drawable
->stvis
.samples
> 1) {
848 templ
.nr_samples
= drawable
->stvis
.samples
;
849 zsbuf
= &drawable
->msaa_textures
[statt
];
852 templ
.nr_samples
= 0;
853 zsbuf
= &drawable
->textures
[statt
];
856 /* Try to reuse the resource.
857 * (the other resource parameters should be constant)
860 (*zsbuf
)->width0
!= templ
.width0
||
861 (*zsbuf
)->height0
!= templ
.height0
) {
862 /* Allocate a new one. */
863 pipe_resource_reference(zsbuf
, NULL
);
864 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
870 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
871 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
875 /* For DRI2, we may get the same buffers again from the server.
876 * To prevent useless imports of gem names, drawable->old* is used
877 * to bypass the import if we get the same buffers. This doesn't apply
878 * to DRI3/Wayland, users of image.loader, since the buffer is managed
879 * by the client (no import), and the back buffer is going to change
883 drawable
->old_num
= num_buffers
;
884 drawable
->old_w
= dri_drawable
->w
;
885 drawable
->old_h
= dri_drawable
->h
;
886 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
891 dri2_flush_frontbuffer(struct dri_context
*ctx
,
892 struct dri_drawable
*drawable
,
893 enum st_attachment_type statt
)
895 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
896 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
897 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
898 struct pipe_context
*pipe
= ctx
->st
->pipe
;
900 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
903 if (drawable
->stvis
.samples
> 1) {
904 /* Resolve the front buffer. */
905 dri_pipe_blit(ctx
->st
->pipe
,
906 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
907 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
910 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
911 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
914 pipe
->flush(pipe
, NULL
, 0);
917 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
919 else if (loader
->flushFrontBuffer
) {
920 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
925 * The struct dri_drawable flush_swapbuffers callback
928 dri2_flush_swapbuffers(struct dri_context
*ctx
,
929 struct dri_drawable
*drawable
)
931 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
932 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
934 if (image
&& image
->base
.version
>= 3 && image
->flushSwapBuffers
) {
935 image
->flushSwapBuffers(dri_drawable
, dri_drawable
->loaderPrivate
);
940 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
941 struct dri_context
*ctx
,
942 struct pipe_resource
*res
)
948 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
949 int width
, int height
, int format
,
950 int num_handles
, struct winsys_handle
*whandle
,
953 struct dri_screen
*screen
= dri_screen(_screen
);
954 struct pipe_screen
*pscreen
= screen
->base
.screen
;
956 struct pipe_resource templ
;
961 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
963 pf
= dri2_format_to_pipe_format (format
);
964 if (pf
== PIPE_FORMAT_NONE
)
967 img
= CALLOC_STRUCT(__DRIimageRec
);
971 memset(&templ
, 0, sizeof(templ
));
972 templ
.bind
= tex_usage
;
973 templ
.target
= screen
->target
;
974 templ
.last_level
= 0;
976 templ
.array_size
= 1;
978 for (i
= num_handles
- 1; i
>= 0; i
--) {
979 struct pipe_resource
*tex
;
981 /* TODO: something a lot less ugly */
984 templ
.width0
= width
;
985 templ
.height0
= height
;
989 templ
.width0
= width
/ 2;
990 templ
.height0
= height
/ 2;
991 templ
.format
= (num_handles
== 2) ?
992 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
993 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
996 templ
.width0
= width
/ 2;
997 templ
.height0
= height
/ 2;
998 templ
.format
= PIPE_FORMAT_R8_UNORM
;
1001 unreachable("too many planes!");
1004 tex
= pscreen
->resource_from_handle(pscreen
,
1005 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
1007 pipe_resource_reference(&img
->texture
, NULL
);
1012 tex
->next
= img
->texture
;
1018 img
->dri_format
= format
;
1020 img
->loader_private
= loaderPrivate
;
1026 dri2_create_image_from_name(__DRIscreen
*_screen
,
1027 int width
, int height
, int format
,
1028 int name
, int pitch
, void *loaderPrivate
)
1030 struct winsys_handle whandle
;
1031 enum pipe_format pf
;
1033 memset(&whandle
, 0, sizeof(whandle
));
1034 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1035 whandle
.handle
= name
;
1036 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1038 pf
= dri2_format_to_pipe_format (format
);
1039 if (pf
== PIPE_FORMAT_NONE
)
1042 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
1044 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1045 1, &whandle
, loaderPrivate
);
1049 dri2_create_image_from_fd(__DRIscreen
*_screen
,
1050 int width
, int height
, int fourcc
,
1051 uint64_t modifier
, int *fds
, int num_fds
,
1052 int *strides
, int *offsets
, unsigned *error
,
1053 int *dri_components
, void *loaderPrivate
)
1055 struct winsys_handle whandles
[3];
1057 __DRIimage
*img
= NULL
;
1058 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
1059 int expected_num_fds
, i
;
1062 case __DRI_IMAGE_FOURCC_YUV420
:
1063 case __DRI_IMAGE_FOURCC_YVU420
:
1064 expected_num_fds
= 3;
1066 case __DRI_IMAGE_FOURCC_NV12
:
1067 expected_num_fds
= 2;
1070 expected_num_fds
= 1;
1074 if (num_fds
!= expected_num_fds
) {
1075 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1079 format
= convert_fourcc(fourcc
, dri_components
);
1081 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1085 memset(whandles
, 0, sizeof(whandles
));
1087 for (i
= 0; i
< num_fds
; i
++) {
1089 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1093 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
1094 whandles
[i
].handle
= (unsigned)fds
[i
];
1095 whandles
[i
].stride
= (unsigned)strides
[i
];
1096 whandles
[i
].offset
= (unsigned)offsets
[i
];
1097 whandles
[i
].modifier
= modifier
;
1100 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
1101 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1102 struct winsys_handle tmp
= whandles
[1];
1103 whandles
[1] = whandles
[2];
1105 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
1108 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1109 num_fds
, whandles
, loaderPrivate
);
1111 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1121 dri2_create_image_common(__DRIscreen
*_screen
,
1122 int width
, int height
,
1123 int format
, unsigned int use
,
1124 const uint64_t *modifiers
,
1125 const unsigned count
,
1126 void *loaderPrivate
)
1128 struct dri_screen
*screen
= dri_screen(_screen
);
1130 struct pipe_resource templ
;
1132 enum pipe_format pf
;
1134 /* createImageWithModifiers doesn't supply usage, and we should not get
1135 * here with both modifiers and a usage flag.
1137 assert(!(use
&& (modifiers
!= NULL
)));
1139 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1141 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1142 tex_usage
|= PIPE_BIND_SCANOUT
;
1143 if (use
& __DRI_IMAGE_USE_SHARE
)
1144 tex_usage
|= PIPE_BIND_SHARED
;
1145 if (use
& __DRI_IMAGE_USE_LINEAR
)
1146 tex_usage
|= PIPE_BIND_LINEAR
;
1147 if (use
& __DRI_IMAGE_USE_CURSOR
) {
1148 if (width
!= 64 || height
!= 64)
1150 tex_usage
|= PIPE_BIND_CURSOR
;
1153 pf
= dri2_format_to_pipe_format (format
);
1154 if (pf
== PIPE_FORMAT_NONE
)
1157 img
= CALLOC_STRUCT(__DRIimageRec
);
1161 memset(&templ
, 0, sizeof(templ
));
1162 templ
.bind
= tex_usage
;
1164 templ
.target
= PIPE_TEXTURE_2D
;
1165 templ
.last_level
= 0;
1166 templ
.width0
= width
;
1167 templ
.height0
= height
;
1169 templ
.array_size
= 1;
1174 ->resource_create_with_modifiers(screen
->base
.screen
,
1180 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1181 if (!img
->texture
) {
1188 img
->dri_format
= format
;
1189 img
->dri_components
= 0;
1192 img
->loader_private
= loaderPrivate
;
1197 dri2_create_image(__DRIscreen
*_screen
,
1198 int width
, int height
, int format
,
1199 unsigned int use
, void *loaderPrivate
)
1201 return dri2_create_image_common(_screen
, width
, height
, format
, use
,
1202 NULL
/* modifiers */, 0 /* count */,
1207 dri2_create_image_with_modifiers(__DRIscreen
*dri_screen
,
1208 int width
, int height
, int format
,
1209 const uint64_t *modifiers
,
1210 const unsigned count
,
1211 void *loaderPrivate
)
1213 return dri2_create_image_common(dri_screen
, width
, height
, format
,
1214 0 /* use */, modifiers
, count
,
1219 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1221 struct winsys_handle whandle
;
1224 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1225 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1227 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1229 memset(&whandle
, 0, sizeof(whandle
));
1232 case __DRI_IMAGE_ATTRIB_STRIDE
:
1233 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1234 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1235 NULL
, image
->texture
, &whandle
, usage
))
1237 *value
= whandle
.stride
;
1239 case __DRI_IMAGE_ATTRIB_OFFSET
:
1240 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1241 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1242 NULL
, image
->texture
, &whandle
, usage
))
1244 *value
= whandle
.offset
;
1246 case __DRI_IMAGE_ATTRIB_HANDLE
:
1247 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1248 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1249 NULL
, image
->texture
, &whandle
, usage
))
1251 *value
= whandle
.handle
;
1253 case __DRI_IMAGE_ATTRIB_NAME
:
1254 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1255 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1256 NULL
, image
->texture
, &whandle
, usage
))
1258 *value
= whandle
.handle
;
1260 case __DRI_IMAGE_ATTRIB_FD
:
1261 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1262 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1263 NULL
, image
->texture
, &whandle
, usage
))
1266 *value
= whandle
.handle
;
1268 case __DRI_IMAGE_ATTRIB_FORMAT
:
1269 *value
= image
->dri_format
;
1271 case __DRI_IMAGE_ATTRIB_WIDTH
:
1272 *value
= image
->texture
->width0
;
1274 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1275 *value
= image
->texture
->height0
;
1277 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1278 if (image
->dri_components
== 0)
1280 *value
= image
->dri_components
;
1282 case __DRI_IMAGE_ATTRIB_FOURCC
:
1283 *value
= convert_to_fourcc(image
->dri_format
);
1285 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1288 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1289 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1290 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1291 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1292 NULL
, image
->texture
, &whandle
, usage
))
1294 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1296 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1298 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1299 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1300 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1301 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1302 NULL
, image
->texture
, &whandle
, usage
))
1304 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1306 *value
= whandle
.modifier
& 0xffffffff;
1314 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1318 img
= CALLOC_STRUCT(__DRIimageRec
);
1322 img
->texture
= NULL
;
1323 pipe_resource_reference(&img
->texture
, image
->texture
);
1324 img
->level
= image
->level
;
1325 img
->layer
= image
->layer
;
1326 img
->dri_format
= image
->dri_format
;
1327 /* This should be 0 for sub images, but dup is also used for base images. */
1328 img
->dri_components
= image
->dri_components
;
1329 img
->loader_private
= loaderPrivate
;
1335 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1337 if (!image
|| !image
->texture
)
1340 struct pipe_screen
*screen
= image
->texture
->screen
;
1341 if (!screen
->check_resource_capability
)
1344 /* We don't want to check these:
1345 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1346 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1349 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1350 bind
|= PIPE_BIND_SCANOUT
;
1351 if (use
& __DRI_IMAGE_USE_LINEAR
)
1352 bind
|= PIPE_BIND_LINEAR
;
1353 if (use
& __DRI_IMAGE_USE_CURSOR
)
1354 bind
|= PIPE_BIND_CURSOR
;
1359 return screen
->check_resource_capability(screen
, image
->texture
, bind
);
1363 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1364 int *names
, int num_names
, int *strides
, int *offsets
,
1365 void *loaderPrivate
)
1369 struct winsys_handle whandle
;
1374 format
= convert_fourcc(format
, &dri_components
);
1378 memset(&whandle
, 0, sizeof(whandle
));
1379 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1380 whandle
.handle
= names
[0];
1381 whandle
.stride
= strides
[0];
1382 whandle
.offset
= offsets
[0];
1383 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1385 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1386 1, &whandle
, loaderPrivate
);
1390 img
->dri_components
= dri_components
;
1395 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1402 if (image
->dri_components
== 0)
1405 img
= dri2_dup_image(image
, loaderPrivate
);
1409 if (img
->texture
->screen
->resource_changed
)
1410 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1413 /* set this to 0 for sub images. */
1414 img
->dri_components
= 0;
1419 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1420 int *fds
, int num_fds
, int *strides
, int *offsets
,
1421 void *loaderPrivate
)
1426 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1427 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1428 strides
, offsets
, NULL
,
1429 &dri_components
, loaderPrivate
);
1433 img
->dri_components
= dri_components
;
1438 dri2_query_dma_buf_formats(__DRIscreen
*_screen
, int max
, int *formats
,
1441 struct dri_screen
*screen
= dri_screen(_screen
);
1442 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1443 const unsigned bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1446 for (i
= 0, j
= 0; (i
< ARRAY_SIZE(fourcc_formats
)) &&
1447 (j
< max
|| max
== 0); i
++) {
1448 if (pscreen
->is_format_supported(pscreen
,
1449 fourcc_to_pipe_format(
1454 formats
[j
] = fourcc_formats
[i
];
1463 dri2_query_dma_buf_modifiers(__DRIscreen
*_screen
, int fourcc
, int max
,
1464 uint64_t *modifiers
, unsigned int *external_only
,
1467 struct dri_screen
*screen
= dri_screen(_screen
);
1468 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1469 enum pipe_format format
= fourcc_to_pipe_format(fourcc
);
1470 const unsigned usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1472 if (pscreen
->query_dmabuf_modifiers
!= NULL
&&
1473 pscreen
->is_format_supported(pscreen
, format
, screen
->target
, 0, usage
)) {
1474 pscreen
->query_dmabuf_modifiers(pscreen
, format
, max
, modifiers
,
1475 external_only
, count
);
1482 dri2_from_dma_bufs(__DRIscreen
*screen
,
1483 int width
, int height
, int fourcc
,
1484 int *fds
, int num_fds
,
1485 int *strides
, int *offsets
,
1486 enum __DRIYUVColorSpace yuv_color_space
,
1487 enum __DRISampleRange sample_range
,
1488 enum __DRIChromaSiting horizontal_siting
,
1489 enum __DRIChromaSiting vertical_siting
,
1491 void *loaderPrivate
)
1496 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1497 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1498 strides
, offsets
, error
,
1499 &dri_components
, loaderPrivate
);
1503 img
->yuv_color_space
= yuv_color_space
;
1504 img
->sample_range
= sample_range
;
1505 img
->horizontal_siting
= horizontal_siting
;
1506 img
->vertical_siting
= vertical_siting
;
1507 img
->dri_components
= dri_components
;
1509 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1514 dri2_from_dma_bufs2(__DRIscreen
*screen
,
1515 int width
, int height
, int fourcc
,
1516 uint64_t modifier
, int *fds
, int num_fds
,
1517 int *strides
, int *offsets
,
1518 enum __DRIYUVColorSpace yuv_color_space
,
1519 enum __DRISampleRange sample_range
,
1520 enum __DRIChromaSiting horizontal_siting
,
1521 enum __DRIChromaSiting vertical_siting
,
1523 void *loaderPrivate
)
1528 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1529 modifier
, fds
, num_fds
, strides
, offsets
,
1530 error
, &dri_components
, loaderPrivate
);
1534 img
->yuv_color_space
= yuv_color_space
;
1535 img
->sample_range
= sample_range
;
1536 img
->horizontal_siting
= horizontal_siting
;
1537 img
->vertical_siting
= vertical_siting
;
1538 img
->dri_components
= dri_components
;
1540 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1545 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1546 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1547 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1550 struct dri_context
*ctx
= dri_context(context
);
1551 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1552 struct pipe_screen
*screen
;
1553 struct pipe_fence_handle
*fence
;
1554 struct pipe_blit_info blit
;
1559 memset(&blit
, 0, sizeof(blit
));
1560 blit
.dst
.resource
= dst
->texture
;
1561 blit
.dst
.box
.x
= dstx0
;
1562 blit
.dst
.box
.y
= dsty0
;
1563 blit
.dst
.box
.width
= dstwidth
;
1564 blit
.dst
.box
.height
= dstheight
;
1565 blit
.dst
.box
.depth
= 1;
1566 blit
.dst
.format
= dst
->texture
->format
;
1567 blit
.src
.resource
= src
->texture
;
1568 blit
.src
.box
.x
= srcx0
;
1569 blit
.src
.box
.y
= srcy0
;
1570 blit
.src
.box
.width
= srcwidth
;
1571 blit
.src
.box
.height
= srcheight
;
1572 blit
.src
.box
.depth
= 1;
1573 blit
.src
.format
= src
->texture
->format
;
1574 blit
.mask
= PIPE_MASK_RGBA
;
1575 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1577 pipe
->blit(pipe
, &blit
);
1579 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1580 pipe
->flush_resource(pipe
, dst
->texture
);
1581 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1582 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1583 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1584 pipe
->flush_resource(pipe
, dst
->texture
);
1585 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1586 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1587 screen
->fence_reference(screen
, &fence
, NULL
);
1592 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1593 int x0
, int y0
, int width
, int height
,
1594 unsigned int flags
, int *stride
, void **data
)
1596 struct dri_context
*ctx
= dri_context(context
);
1597 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1598 enum pipe_transfer_usage pipe_access
= 0;
1599 struct pipe_transfer
*trans
;
1602 if (!image
|| !data
|| *data
)
1605 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1606 pipe_access
|= PIPE_TRANSFER_READ
;
1607 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1608 pipe_access
|= PIPE_TRANSFER_WRITE
;
1610 map
= pipe_transfer_map(pipe
, image
->texture
,
1611 0, 0, pipe_access
, x0
, y0
, width
, height
,
1615 *stride
= trans
->stride
;
1622 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1624 struct dri_context
*ctx
= dri_context(context
);
1625 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1627 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1631 dri2_get_capabilities(__DRIscreen
*_screen
)
1633 struct dri_screen
*screen
= dri_screen(_screen
);
1635 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1638 /* The extension is modified during runtime if DRI_PRIME is detected */
1639 static __DRIimageExtension dri2ImageExtension
= {
1640 .base
= { __DRI_IMAGE
, 17 },
1642 .createImageFromName
= dri2_create_image_from_name
,
1643 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1644 .destroyImage
= dri2_destroy_image
,
1645 .createImage
= dri2_create_image
,
1646 .queryImage
= dri2_query_image
,
1647 .dupImage
= dri2_dup_image
,
1648 .validateUsage
= dri2_validate_usage
,
1649 .createImageFromNames
= dri2_from_names
,
1650 .fromPlanar
= dri2_from_planar
,
1651 .createImageFromTexture
= dri2_create_from_texture
,
1652 .createImageFromFds
= NULL
,
1653 .createImageFromDmaBufs
= NULL
,
1654 .blitImage
= dri2_blit_image
,
1655 .getCapabilities
= dri2_get_capabilities
,
1656 .mapImage
= dri2_map_image
,
1657 .unmapImage
= dri2_unmap_image
,
1658 .createImageWithModifiers
= NULL
,
1659 .createImageFromDmaBufs2
= NULL
,
1660 .queryDmaBufFormats
= NULL
,
1661 .queryDmaBufModifiers
= NULL
,
1662 .queryDmaBufFormatModifierAttribs
= NULL
,
1663 .createImageFromRenderbuffer2
= dri2_create_image_from_renderbuffer2
,
1666 static const __DRIrobustnessExtension dri2Robustness
= {
1667 .base
= { __DRI2_ROBUSTNESS
, 1 }
1671 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1672 struct mesa_glinterop_device_info
*out
)
1674 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1676 /* There is no version 0, thus we do not support it */
1677 if (out
->version
== 0)
1678 return MESA_GLINTEROP_INVALID_VERSION
;
1680 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1681 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1682 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1683 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1685 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1686 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1688 /* Instruct the caller that we support up-to version one of the interface */
1691 return MESA_GLINTEROP_SUCCESS
;
1695 dri2_interop_export_object(__DRIcontext
*_ctx
,
1696 struct mesa_glinterop_export_in
*in
,
1697 struct mesa_glinterop_export_out
*out
)
1699 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1700 struct pipe_screen
*screen
= st
->pipe
->screen
;
1701 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1702 struct pipe_resource
*res
= NULL
;
1703 struct winsys_handle whandle
;
1704 unsigned target
, usage
;
1707 /* There is no version 0, thus we do not support it */
1708 if (in
->version
== 0 || out
->version
== 0)
1709 return MESA_GLINTEROP_INVALID_VERSION
;
1711 /* Validate the target. */
1712 switch (in
->target
) {
1713 case GL_TEXTURE_BUFFER
:
1717 case GL_TEXTURE_RECTANGLE
:
1718 case GL_TEXTURE_1D_ARRAY
:
1719 case GL_TEXTURE_2D_ARRAY
:
1720 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1721 case GL_TEXTURE_CUBE_MAP
:
1722 case GL_TEXTURE_2D_MULTISAMPLE
:
1723 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1724 case GL_TEXTURE_EXTERNAL_OES
:
1725 case GL_RENDERBUFFER
:
1726 case GL_ARRAY_BUFFER
:
1727 target
= in
->target
;
1729 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1730 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1731 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1732 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1733 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1734 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1735 target
= GL_TEXTURE_CUBE_MAP
;
1738 return MESA_GLINTEROP_INVALID_TARGET
;
1741 /* Validate the simple case of miplevel. */
1742 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1744 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1746 /* Validate the OpenGL object and get pipe_resource. */
1747 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1749 if (target
== GL_ARRAY_BUFFER
) {
1752 * The error checking is based on the documentation of
1753 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1755 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1757 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1758 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1759 * a GL buffer object but does not have an existing data store or
1760 * the size of the buffer is 0."
1762 if (!buf
|| buf
->Size
== 0) {
1763 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1764 return MESA_GLINTEROP_INVALID_OBJECT
;
1767 res
= st_buffer_object(buf
)->buffer
;
1769 /* this shouldn't happen */
1770 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1771 return MESA_GLINTEROP_INVALID_OBJECT
;
1774 out
->buf_offset
= 0;
1775 out
->buf_size
= buf
->Size
;
1777 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1778 } else if (target
== GL_RENDERBUFFER
) {
1781 * The error checking is based on the documentation of
1782 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1784 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1786 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1787 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1788 * object or if the width or height of renderbuffer is zero."
1790 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1791 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1792 return MESA_GLINTEROP_INVALID_OBJECT
;
1795 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1796 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1797 * renderbuffer object."
1799 if (rb
->NumSamples
> 1) {
1800 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1801 return MESA_GLINTEROP_INVALID_OPERATION
;
1804 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1805 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1806 * required by the OpenCL implementation on the device."
1808 res
= st_renderbuffer(rb
)->texture
;
1810 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1811 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1814 out
->internal_format
= rb
->InternalFormat
;
1815 out
->view_minlevel
= 0;
1816 out
->view_numlevels
= 1;
1817 out
->view_minlayer
= 0;
1818 out
->view_numlayers
= 1;
1822 * The error checking is based on the documentation of
1823 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1825 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1828 _mesa_test_texobj_completeness(ctx
, obj
);
1830 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1831 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1832 * type matches texture_target, if the specified miplevel of texture
1833 * is not defined, or if the width or height of the specified
1834 * miplevel is zero or if the GL texture object is incomplete."
1837 obj
->Target
!= target
||
1838 !obj
->_BaseComplete
||
1839 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1840 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1841 return MESA_GLINTEROP_INVALID_OBJECT
;
1844 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1845 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1846 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1847 * implementations); or greater than the value of q (for both OpenGL
1848 * and OpenGL ES). levelbase and q are defined for the texture in
1849 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1850 * specification and section 3.7.10 of the OpenGL ES 2.0."
1852 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1853 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1854 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1857 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1858 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1859 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1862 res
= st_get_texobj_resource(obj
);
1864 /* Incomplete texture buffer object? This shouldn't really occur. */
1865 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1866 return MESA_GLINTEROP_INVALID_OBJECT
;
1869 if (target
== GL_TEXTURE_BUFFER
) {
1870 out
->internal_format
= obj
->BufferObjectFormat
;
1871 out
->buf_offset
= obj
->BufferOffset
;
1872 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1875 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1877 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1878 out
->view_minlevel
= obj
->MinLevel
;
1879 out
->view_numlevels
= obj
->NumLevels
;
1880 out
->view_minlayer
= obj
->MinLayer
;
1881 out
->view_numlayers
= obj
->NumLayers
;
1885 /* Get the handle. */
1886 switch (in
->access
) {
1887 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1888 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1890 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1891 usage
= PIPE_HANDLE_USAGE_READ
;
1893 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1894 usage
= PIPE_HANDLE_USAGE_WRITE
;
1900 memset(&whandle
, 0, sizeof(whandle
));
1901 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1903 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1905 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1908 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1910 out
->dmabuf_fd
= whandle
.handle
;
1911 out
->out_driver_data_written
= 0;
1913 if (res
->target
== PIPE_BUFFER
)
1914 out
->buf_offset
+= whandle
.offset
;
1916 /* Instruct the caller that we support up-to version one of the interface */
1920 return MESA_GLINTEROP_SUCCESS
;
1923 static const __DRI2interopExtension dri2InteropExtension
= {
1924 .base
= { __DRI2_INTEROP
, 1 },
1925 .query_device_info
= dri2_interop_query_device_info
,
1926 .export_object
= dri2_interop_export_object
1930 * \brief the DRI2ConfigQueryExtension configQueryb method
1933 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1936 struct dri_screen
*screen
= dri_screen(sPriv
);
1938 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_BOOL
))
1939 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1941 *val
= driQueryOptionb(&screen
->dev
->option_cache
, var
);
1947 * \brief the DRI2ConfigQueryExtension configQueryi method
1950 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1952 struct dri_screen
*screen
= dri_screen(sPriv
);
1954 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_INT
) &&
1955 !driCheckOption(&screen
->dev
->option_cache
, var
, DRI_ENUM
))
1956 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
1958 *val
= driQueryOptioni(&screen
->dev
->option_cache
, var
);
1964 * \brief the DRI2ConfigQueryExtension configQueryf method
1967 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
1969 struct dri_screen
*screen
= dri_screen(sPriv
);
1971 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_FLOAT
))
1972 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
1974 *val
= driQueryOptionf(&screen
->dev
->option_cache
, var
);
1980 * \brief the DRI2ConfigQueryExtension struct.
1982 * We first query the driver option cache. Then the dri2 option cache.
1984 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
1985 .base
= { __DRI2_CONFIG_QUERY
, 1 },
1987 .configQueryb
= dri2GalliumConfigQueryb
,
1988 .configQueryi
= dri2GalliumConfigQueryi
,
1989 .configQueryf
= dri2GalliumConfigQueryf
,
1993 * Backend function init_screen.
1996 static const __DRIextension
*dri_screen_extensions
[] = {
1997 &driTexBufferExtension
.base
,
1998 &dri2FlushExtension
.base
,
1999 &dri2ImageExtension
.base
,
2000 &dri2RendererQueryExtension
.base
,
2001 &dri2GalliumConfigQueryExtension
.base
,
2002 &dri2ThrottleExtension
.base
,
2003 &dri2FenceExtension
.base
,
2004 &dri2InteropExtension
.base
,
2005 &dri2NoErrorExtension
.base
,
2009 static const __DRIextension
*dri_robust_screen_extensions
[] = {
2010 &driTexBufferExtension
.base
,
2011 &dri2FlushExtension
.base
,
2012 &dri2ImageExtension
.base
,
2013 &dri2RendererQueryExtension
.base
,
2014 &dri2GalliumConfigQueryExtension
.base
,
2015 &dri2ThrottleExtension
.base
,
2016 &dri2FenceExtension
.base
,
2017 &dri2InteropExtension
.base
,
2018 &dri2Robustness
.base
,
2019 &dri2NoErrorExtension
.base
,
2024 * This is the driver specific part of the createNewScreen entry point.
2026 * Returns the struct gl_config supported by this driver.
2028 static const __DRIconfig
**
2029 dri2_init_screen(__DRIscreen
* sPriv
)
2031 const __DRIconfig
**configs
;
2032 struct dri_screen
*screen
;
2033 struct pipe_screen
*pscreen
= NULL
;
2034 const struct drm_conf_ret
*throttle_ret
;
2035 const struct drm_conf_ret
*dmabuf_ret
;
2038 screen
= CALLOC_STRUCT(dri_screen
);
2042 screen
->sPriv
= sPriv
;
2043 screen
->fd
= sPriv
->fd
;
2044 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
2046 sPriv
->driverPrivate
= (void *)screen
;
2048 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2052 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
)) {
2053 dri_init_options(screen
);
2055 pscreen
= pipe_loader_create_screen(screen
->dev
);
2061 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
2062 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
2064 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
2065 screen
->throttling_enabled
= TRUE
;
2066 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
2069 if (pscreen
->resource_create_with_modifiers
)
2070 dri2ImageExtension
.createImageWithModifiers
=
2071 dri2_create_image_with_modifiers
;
2073 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
2076 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2077 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2078 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2079 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2080 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2081 if (pscreen
->query_dmabuf_modifiers
) {
2082 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2083 dri2ImageExtension
.queryDmaBufModifiers
=
2084 dri2_query_dma_buf_modifiers
;
2089 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
2090 sPriv
->extensions
= dri_robust_screen_extensions
;
2091 screen
->has_reset_status_query
= true;
2094 sPriv
->extensions
= dri_screen_extensions
;
2096 configs
= dri_init_screen_helper(screen
, pscreen
);
2098 goto destroy_screen
;
2100 screen
->can_share_buffer
= true;
2101 screen
->auto_fake_front
= dri_with_format(sPriv
);
2102 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2103 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2108 dri_destroy_screen_helper(screen
);
2112 pipe_loader_release(&screen
->dev
, 1);
2122 * This is the driver specific part of the createNewScreen entry point.
2124 * Returns the struct gl_config supported by this driver.
2126 static const __DRIconfig
**
2127 dri_kms_init_screen(__DRIscreen
* sPriv
)
2129 #if defined(GALLIUM_SOFTPIPE)
2130 const __DRIconfig
**configs
;
2131 struct dri_screen
*screen
;
2132 struct pipe_screen
*pscreen
= NULL
;
2136 screen
= CALLOC_STRUCT(dri_screen
);
2140 screen
->sPriv
= sPriv
;
2141 screen
->fd
= sPriv
->fd
;
2143 sPriv
->driverPrivate
= (void *)screen
;
2145 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2148 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
)) {
2149 dri_init_options(screen
);
2150 pscreen
= pipe_loader_create_screen(screen
->dev
);
2156 if (pscreen
->resource_create_with_modifiers
)
2157 dri2ImageExtension
.createImageWithModifiers
=
2158 dri2_create_image_with_modifiers
;
2160 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2161 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2162 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2163 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2164 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2165 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2166 dri2ImageExtension
.queryDmaBufModifiers
= dri2_query_dma_buf_modifiers
;
2169 sPriv
->extensions
= dri_screen_extensions
;
2171 configs
= dri_init_screen_helper(screen
, pscreen
);
2173 goto destroy_screen
;
2175 screen
->can_share_buffer
= false;
2176 screen
->auto_fake_front
= dri_with_format(sPriv
);
2177 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2178 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2183 dri_destroy_screen_helper(screen
);
2187 pipe_loader_release(&screen
->dev
, 1);
2193 #endif // GALLIUM_SOFTPIPE
2198 dri2_create_buffer(__DRIscreen
* sPriv
,
2199 __DRIdrawable
* dPriv
,
2200 const struct gl_config
* visual
, boolean isPixmap
)
2202 struct dri_drawable
*drawable
= NULL
;
2204 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2207 drawable
= dPriv
->driverPrivate
;
2209 drawable
->allocate_textures
= dri2_allocate_textures
;
2210 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2211 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2212 drawable
->flush_swapbuffers
= dri2_flush_swapbuffers
;
2218 * DRI driver virtual function table.
2220 * DRI versions differ in their implementation of init_screen and swap_buffers.
2222 const struct __DriverAPIRec galliumdrm_driver_api
= {
2223 .InitScreen
= dri2_init_screen
,
2224 .DestroyScreen
= dri_destroy_screen
,
2225 .CreateContext
= dri_create_context
,
2226 .DestroyContext
= dri_destroy_context
,
2227 .CreateBuffer
= dri2_create_buffer
,
2228 .DestroyBuffer
= dri_destroy_buffer
,
2229 .MakeCurrent
= dri_make_current
,
2230 .UnbindContext
= dri_unbind_context
,
2232 .AllocateBuffer
= dri2_allocate_buffer
,
2233 .ReleaseBuffer
= dri2_release_buffer
,
2237 * DRI driver virtual function table.
2239 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2240 * hook. The latter is used to explicitly initialise the kms_swrast driver
2241 * rather than selecting the approapriate driver as suggested by the loader.
2243 const struct __DriverAPIRec dri_kms_driver_api
= {
2244 .InitScreen
= dri_kms_init_screen
,
2245 .DestroyScreen
= dri_destroy_screen
,
2246 .CreateContext
= dri_create_context
,
2247 .DestroyContext
= dri_destroy_context
,
2248 .CreateBuffer
= dri2_create_buffer
,
2249 .DestroyBuffer
= dri_destroy_buffer
,
2250 .MakeCurrent
= dri_make_current
,
2251 .UnbindContext
= dri_unbind_context
,
2253 .AllocateBuffer
= dri2_allocate_buffer
,
2254 .ReleaseBuffer
= dri2_release_buffer
,
2257 /* This is the table of extensions that the loader will dlsym() for. */
2258 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2259 &driCoreExtension
.base
,
2260 &driImageDriverExtension
.base
,
2261 &driDRI2Extension
.base
,
2262 &gallium_config_options
.base
,
2266 /* vim: set sw=3 ts=8 sts=3 expandtab: */