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_BGRA8888_UNORM
:
409 case PIPE_FORMAT_RGBA8888_UNORM
:
412 case PIPE_FORMAT_BGRX8888_UNORM
:
413 case PIPE_FORMAT_RGBX8888_UNORM
:
416 case PIPE_FORMAT_B5G6R5_UNORM
:
420 depth
= util_format_get_blocksizebits(format
);
421 assert(!"Unexpected format in dri2_drawable_get_buffers()");
424 attachments
[num_attachments
++] = att
;
426 attachments
[num_attachments
++] = depth
;
431 num_attachments
/= 2;
432 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
433 &dri_drawable
->w
, &dri_drawable
->h
,
434 attachments
, num_attachments
,
435 &num_buffers
, dri_drawable
->loaderPrivate
);
438 buffers
= loader
->getBuffers(dri_drawable
,
439 &dri_drawable
->w
, &dri_drawable
->h
,
440 attachments
, num_attachments
,
441 &num_buffers
, dri_drawable
->loaderPrivate
);
445 *count
= num_buffers
;
451 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
452 struct __DRIimageList
*images
,
453 const enum st_attachment_type
*statts
,
454 unsigned statts_count
)
456 __DRIdrawable
*dPriv
= drawable
->dPriv
;
457 __DRIscreen
*sPriv
= drawable
->sPriv
;
458 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
460 uint32_t buffer_mask
= 0;
463 for (i
= 0; i
< statts_count
; i
++) {
464 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
465 if (pf
== PIPE_FORMAT_NONE
)
469 case ST_ATTACHMENT_FRONT_LEFT
:
470 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
472 case ST_ATTACHMENT_BACK_LEFT
:
473 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
480 case PIPE_FORMAT_B5G6R5_UNORM
:
481 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
483 case PIPE_FORMAT_BGRX8888_UNORM
:
484 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
486 case PIPE_FORMAT_BGRA8888_UNORM
:
487 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
489 case PIPE_FORMAT_RGBX8888_UNORM
:
490 image_format
= __DRI_IMAGE_FORMAT_XBGR8888
;
492 case PIPE_FORMAT_RGBA8888_UNORM
:
493 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
496 image_format
= __DRI_IMAGE_FORMAT_NONE
;
501 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
502 (uint32_t *) &drawable
->base
.stamp
,
503 dPriv
->loaderPrivate
, buffer_mask
,
508 dri2_allocate_buffer(__DRIscreen
*sPriv
,
509 unsigned attachment
, unsigned format
,
510 int width
, int height
)
512 struct dri_screen
*screen
= dri_screen(sPriv
);
513 struct dri2_buffer
*buffer
;
514 struct pipe_resource templ
;
517 struct winsys_handle whandle
;
519 switch (attachment
) {
520 case __DRI_BUFFER_FRONT_LEFT
:
521 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
522 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
524 case __DRI_BUFFER_BACK_LEFT
:
525 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
527 case __DRI_BUFFER_DEPTH
:
528 case __DRI_BUFFER_DEPTH_STENCIL
:
529 case __DRI_BUFFER_STENCIL
:
530 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
534 /* because we get the handle and stride */
535 bind
|= PIPE_BIND_SHARED
;
539 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
542 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
545 pf
= PIPE_FORMAT_Z16_UNORM
;
551 buffer
= CALLOC_STRUCT(dri2_buffer
);
555 memset(&templ
, 0, sizeof(templ
));
558 templ
.target
= PIPE_TEXTURE_2D
;
559 templ
.last_level
= 0;
560 templ
.width0
= width
;
561 templ
.height0
= height
;
563 templ
.array_size
= 1;
566 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
567 if (!buffer
->resource
) {
572 memset(&whandle
, 0, sizeof(whandle
));
573 if (screen
->can_share_buffer
)
574 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
576 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
578 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
579 buffer
->resource
, &whandle
,
580 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
582 buffer
->base
.attachment
= attachment
;
583 buffer
->base
.name
= whandle
.handle
;
584 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
585 buffer
->base
.pitch
= whandle
.stride
;
587 return &buffer
->base
;
591 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
593 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
595 pipe_resource_reference(&buffer
->resource
, NULL
);
600 * Backend functions for st_framebuffer interface.
604 dri2_allocate_textures(struct dri_context
*ctx
,
605 struct dri_drawable
*drawable
,
606 const enum st_attachment_type
*statts
,
607 unsigned statts_count
)
609 __DRIscreen
*sPriv
= drawable
->sPriv
;
610 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
611 struct dri_screen
*screen
= dri_screen(sPriv
);
612 struct pipe_resource templ
;
613 boolean alloc_depthstencil
= FALSE
;
615 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
616 /* Image specific variables */
617 struct __DRIimageList images
;
618 /* Dri2 specific variables */
619 __DRIbuffer
*buffers
= NULL
;
620 struct winsys_handle whandle
;
621 unsigned num_buffers
= statts_count
;
623 /* First get the buffers from the loader */
625 if (!dri_image_drawable_get_buffers(drawable
, &images
,
626 statts
, statts_count
))
630 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
631 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
632 drawable
->old_w
== dri_drawable
->w
&&
633 drawable
->old_h
== dri_drawable
->h
&&
634 memcmp(drawable
->old
, buffers
,
635 sizeof(__DRIbuffer
) * num_buffers
) == 0))
639 /* Second clean useless resources*/
641 /* See if we need a depth-stencil buffer. */
642 for (i
= 0; i
< statts_count
; i
++) {
643 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
644 alloc_depthstencil
= TRUE
;
649 /* Delete the resources we won't need. */
650 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
651 /* Don't delete the depth-stencil buffer, we can reuse it. */
652 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
655 /* Flush the texture before unreferencing, so that other clients can
656 * see what the driver has rendered.
658 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
659 struct pipe_context
*pipe
= ctx
->st
->pipe
;
660 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
663 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
666 if (drawable
->stvis
.samples
> 1) {
667 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
670 /* Don't delete MSAA resources for the attachments which are enabled,
671 * we can reuse them. */
672 for (j
= 0; j
< statts_count
; j
++) {
673 if (i
== statts
[j
]) {
680 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
685 /* Third use the buffers retrieved to fill the drawable info */
687 memset(&templ
, 0, sizeof(templ
));
688 templ
.target
= screen
->target
;
689 templ
.last_level
= 0;
691 templ
.array_size
= 1;
694 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
695 struct pipe_resource
**buf
=
696 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
697 struct pipe_resource
*texture
= images
.front
->texture
;
699 dri_drawable
->w
= texture
->width0
;
700 dri_drawable
->h
= texture
->height0
;
702 pipe_resource_reference(buf
, texture
);
705 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
706 struct pipe_resource
**buf
=
707 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
708 struct pipe_resource
*texture
= images
.back
->texture
;
710 dri_drawable
->w
= texture
->width0
;
711 dri_drawable
->h
= texture
->height0
;
713 pipe_resource_reference(buf
, texture
);
716 /* Note: if there is both a back and a front buffer,
717 * then they have the same size.
719 templ
.width0
= dri_drawable
->w
;
720 templ
.height0
= dri_drawable
->h
;
723 memset(&whandle
, 0, sizeof(whandle
));
725 /* Process DRI-provided buffers and get pipe_resources. */
726 for (i
= 0; i
< num_buffers
; i
++) {
727 __DRIbuffer
*buf
= &buffers
[i
];
728 enum st_attachment_type statt
;
729 enum pipe_format format
;
731 switch (buf
->attachment
) {
732 case __DRI_BUFFER_FRONT_LEFT
:
733 if (!screen
->auto_fake_front
) {
734 continue; /* invalid attachment */
737 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
738 statt
= ST_ATTACHMENT_FRONT_LEFT
;
740 case __DRI_BUFFER_BACK_LEFT
:
741 statt
= ST_ATTACHMENT_BACK_LEFT
;
744 continue; /* invalid attachment */
747 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
748 if (format
== PIPE_FORMAT_NONE
)
751 /* dri2_drawable_get_buffers has already filled dri_drawable->w
752 * and dri_drawable->h */
753 templ
.width0
= dri_drawable
->w
;
754 templ
.height0
= dri_drawable
->h
;
755 templ
.format
= format
;
757 whandle
.handle
= buf
->name
;
758 whandle
.stride
= buf
->pitch
;
760 if (screen
->can_share_buffer
)
761 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
763 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
764 drawable
->textures
[statt
] =
765 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
767 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
768 assert(drawable
->textures
[statt
]);
772 /* Allocate private MSAA colorbuffers. */
773 if (drawable
->stvis
.samples
> 1) {
774 for (i
= 0; i
< statts_count
; i
++) {
775 enum st_attachment_type statt
= statts
[i
];
777 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
780 if (drawable
->textures
[statt
]) {
781 templ
.format
= drawable
->textures
[statt
]->format
;
782 templ
.bind
= drawable
->textures
[statt
]->bind
&
783 ~(PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
784 templ
.nr_samples
= drawable
->stvis
.samples
;
786 /* Try to reuse the resource.
787 * (the other resource parameters should be constant)
789 if (!drawable
->msaa_textures
[statt
] ||
790 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
791 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
792 /* Allocate a new one. */
793 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
795 drawable
->msaa_textures
[statt
] =
796 screen
->base
.screen
->resource_create(screen
->base
.screen
,
798 assert(drawable
->msaa_textures
[statt
]);
800 /* If there are any MSAA resources, we should initialize them
801 * such that they contain the same data as the single-sample
802 * resources we just got from the X server.
804 * The reason for this is that the state tracker (and
805 * therefore the app) can access the MSAA resources only.
806 * The single-sample resources are not exposed
807 * to the state tracker.
810 dri_pipe_blit(ctx
->st
->pipe
,
811 drawable
->msaa_textures
[statt
],
812 drawable
->textures
[statt
]);
816 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
821 /* Allocate a private depth-stencil buffer. */
822 if (alloc_depthstencil
) {
823 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
824 struct pipe_resource
**zsbuf
;
825 enum pipe_format format
;
828 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
831 templ
.format
= format
;
832 templ
.bind
= bind
& ~PIPE_BIND_SHARED
;
834 if (drawable
->stvis
.samples
> 1) {
835 templ
.nr_samples
= drawable
->stvis
.samples
;
836 zsbuf
= &drawable
->msaa_textures
[statt
];
839 templ
.nr_samples
= 0;
840 zsbuf
= &drawable
->textures
[statt
];
843 /* Try to reuse the resource.
844 * (the other resource parameters should be constant)
847 (*zsbuf
)->width0
!= templ
.width0
||
848 (*zsbuf
)->height0
!= templ
.height0
) {
849 /* Allocate a new one. */
850 pipe_resource_reference(zsbuf
, NULL
);
851 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
857 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
858 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
862 /* For DRI2, we may get the same buffers again from the server.
863 * To prevent useless imports of gem names, drawable->old* is used
864 * to bypass the import if we get the same buffers. This doesn't apply
865 * to DRI3/Wayland, users of image.loader, since the buffer is managed
866 * by the client (no import), and the back buffer is going to change
870 drawable
->old_num
= num_buffers
;
871 drawable
->old_w
= dri_drawable
->w
;
872 drawable
->old_h
= dri_drawable
->h
;
873 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
878 dri2_flush_frontbuffer(struct dri_context
*ctx
,
879 struct dri_drawable
*drawable
,
880 enum st_attachment_type statt
)
882 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
883 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
884 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
885 struct pipe_context
*pipe
= ctx
->st
->pipe
;
887 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
890 if (drawable
->stvis
.samples
> 1) {
891 /* Resolve the front buffer. */
892 dri_pipe_blit(ctx
->st
->pipe
,
893 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
894 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
897 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
898 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
901 pipe
->flush(pipe
, NULL
, 0);
904 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
906 else if (loader
->flushFrontBuffer
) {
907 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
912 * The struct dri_drawable flush_swapbuffers callback
915 dri2_flush_swapbuffers(struct dri_context
*ctx
,
916 struct dri_drawable
*drawable
)
918 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
919 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
921 if (image
&& image
->base
.version
>= 3 && image
->flushSwapBuffers
) {
922 image
->flushSwapBuffers(dri_drawable
, dri_drawable
->loaderPrivate
);
927 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
928 struct dri_context
*ctx
,
929 struct pipe_resource
*res
)
935 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
936 int width
, int height
, int format
,
937 int num_handles
, struct winsys_handle
*whandle
,
940 struct dri_screen
*screen
= dri_screen(_screen
);
941 struct pipe_screen
*pscreen
= screen
->base
.screen
;
943 struct pipe_resource templ
;
948 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
950 pf
= dri2_format_to_pipe_format (format
);
951 if (pf
== PIPE_FORMAT_NONE
)
954 img
= CALLOC_STRUCT(__DRIimageRec
);
958 memset(&templ
, 0, sizeof(templ
));
959 templ
.bind
= tex_usage
;
960 templ
.target
= screen
->target
;
961 templ
.last_level
= 0;
963 templ
.array_size
= 1;
965 for (i
= num_handles
- 1; i
>= 0; i
--) {
966 struct pipe_resource
*tex
;
968 /* TODO: something a lot less ugly */
971 templ
.width0
= width
;
972 templ
.height0
= height
;
976 templ
.width0
= width
/ 2;
977 templ
.height0
= height
/ 2;
978 templ
.format
= (num_handles
== 2) ?
979 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
980 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
983 templ
.width0
= width
/ 2;
984 templ
.height0
= height
/ 2;
985 templ
.format
= PIPE_FORMAT_R8_UNORM
;
988 unreachable("too many planes!");
991 tex
= pscreen
->resource_from_handle(pscreen
,
992 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
994 pipe_resource_reference(&img
->texture
, NULL
);
999 tex
->next
= img
->texture
;
1005 img
->dri_format
= format
;
1007 img
->loader_private
= loaderPrivate
;
1013 dri2_create_image_from_name(__DRIscreen
*_screen
,
1014 int width
, int height
, int format
,
1015 int name
, int pitch
, void *loaderPrivate
)
1017 struct winsys_handle whandle
;
1018 enum pipe_format pf
;
1020 memset(&whandle
, 0, sizeof(whandle
));
1021 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1022 whandle
.handle
= name
;
1023 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1025 pf
= dri2_format_to_pipe_format (format
);
1026 if (pf
== PIPE_FORMAT_NONE
)
1029 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
1031 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1032 1, &whandle
, loaderPrivate
);
1036 dri2_create_image_from_fd(__DRIscreen
*_screen
,
1037 int width
, int height
, int fourcc
,
1038 uint64_t modifier
, int *fds
, int num_fds
,
1039 int *strides
, int *offsets
, unsigned *error
,
1040 int *dri_components
, void *loaderPrivate
)
1042 struct winsys_handle whandles
[3];
1044 __DRIimage
*img
= NULL
;
1045 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
1046 int expected_num_fds
, i
;
1049 case __DRI_IMAGE_FOURCC_YUV420
:
1050 case __DRI_IMAGE_FOURCC_YVU420
:
1051 expected_num_fds
= 3;
1053 case __DRI_IMAGE_FOURCC_NV12
:
1054 expected_num_fds
= 2;
1057 expected_num_fds
= 1;
1061 if (num_fds
!= expected_num_fds
) {
1062 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1066 format
= convert_fourcc(fourcc
, dri_components
);
1068 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1072 memset(whandles
, 0, sizeof(whandles
));
1074 for (i
= 0; i
< num_fds
; i
++) {
1076 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1080 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
1081 whandles
[i
].handle
= (unsigned)fds
[i
];
1082 whandles
[i
].stride
= (unsigned)strides
[i
];
1083 whandles
[i
].offset
= (unsigned)offsets
[i
];
1084 whandles
[i
].modifier
= modifier
;
1087 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
1088 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1089 struct winsys_handle tmp
= whandles
[1];
1090 whandles
[1] = whandles
[2];
1092 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
1095 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1096 num_fds
, whandles
, loaderPrivate
);
1098 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1108 dri2_create_image_common(__DRIscreen
*_screen
,
1109 int width
, int height
,
1110 int format
, unsigned int use
,
1111 const uint64_t *modifiers
,
1112 const unsigned count
,
1113 void *loaderPrivate
)
1115 struct dri_screen
*screen
= dri_screen(_screen
);
1117 struct pipe_resource templ
;
1119 enum pipe_format pf
;
1121 /* createImageWithModifiers doesn't supply usage, and we should not get
1122 * here with both modifiers and a usage flag.
1124 assert(!(use
&& (modifiers
!= NULL
)));
1126 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1128 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1129 tex_usage
|= PIPE_BIND_SCANOUT
;
1130 if (use
& __DRI_IMAGE_USE_SHARE
)
1131 tex_usage
|= PIPE_BIND_SHARED
;
1132 if (use
& __DRI_IMAGE_USE_LINEAR
)
1133 tex_usage
|= PIPE_BIND_LINEAR
;
1134 if (use
& __DRI_IMAGE_USE_CURSOR
) {
1135 if (width
!= 64 || height
!= 64)
1137 tex_usage
|= PIPE_BIND_CURSOR
;
1140 pf
= dri2_format_to_pipe_format (format
);
1141 if (pf
== PIPE_FORMAT_NONE
)
1144 img
= CALLOC_STRUCT(__DRIimageRec
);
1148 memset(&templ
, 0, sizeof(templ
));
1149 templ
.bind
= tex_usage
;
1151 templ
.target
= PIPE_TEXTURE_2D
;
1152 templ
.last_level
= 0;
1153 templ
.width0
= width
;
1154 templ
.height0
= height
;
1156 templ
.array_size
= 1;
1161 ->resource_create_with_modifiers(screen
->base
.screen
,
1167 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1168 if (!img
->texture
) {
1175 img
->dri_format
= format
;
1176 img
->dri_components
= 0;
1179 img
->loader_private
= loaderPrivate
;
1184 dri2_create_image(__DRIscreen
*_screen
,
1185 int width
, int height
, int format
,
1186 unsigned int use
, void *loaderPrivate
)
1188 return dri2_create_image_common(_screen
, width
, height
, format
, use
,
1189 NULL
/* modifiers */, 0 /* count */,
1194 dri2_create_image_with_modifiers(__DRIscreen
*dri_screen
,
1195 int width
, int height
, int format
,
1196 const uint64_t *modifiers
,
1197 const unsigned count
,
1198 void *loaderPrivate
)
1200 return dri2_create_image_common(dri_screen
, width
, height
, format
,
1201 0 /* use */, modifiers
, count
,
1206 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1208 struct winsys_handle whandle
;
1211 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1212 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1214 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1216 memset(&whandle
, 0, sizeof(whandle
));
1219 case __DRI_IMAGE_ATTRIB_STRIDE
:
1220 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1221 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1222 NULL
, image
->texture
, &whandle
, usage
))
1224 *value
= whandle
.stride
;
1226 case __DRI_IMAGE_ATTRIB_OFFSET
:
1227 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1228 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1229 NULL
, image
->texture
, &whandle
, usage
))
1231 *value
= whandle
.offset
;
1233 case __DRI_IMAGE_ATTRIB_HANDLE
:
1234 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1235 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1236 NULL
, image
->texture
, &whandle
, usage
))
1238 *value
= whandle
.handle
;
1240 case __DRI_IMAGE_ATTRIB_NAME
:
1241 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1242 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1243 NULL
, image
->texture
, &whandle
, usage
))
1245 *value
= whandle
.handle
;
1247 case __DRI_IMAGE_ATTRIB_FD
:
1248 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1249 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1250 NULL
, image
->texture
, &whandle
, usage
))
1253 *value
= whandle
.handle
;
1255 case __DRI_IMAGE_ATTRIB_FORMAT
:
1256 *value
= image
->dri_format
;
1258 case __DRI_IMAGE_ATTRIB_WIDTH
:
1259 *value
= image
->texture
->width0
;
1261 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1262 *value
= image
->texture
->height0
;
1264 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1265 if (image
->dri_components
== 0)
1267 *value
= image
->dri_components
;
1269 case __DRI_IMAGE_ATTRIB_FOURCC
:
1270 *value
= convert_to_fourcc(image
->dri_format
);
1272 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1275 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1276 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1277 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1278 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1279 NULL
, image
->texture
, &whandle
, usage
))
1281 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1283 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1285 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1286 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1287 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1288 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1289 NULL
, image
->texture
, &whandle
, usage
))
1291 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1293 *value
= whandle
.modifier
& 0xffffffff;
1301 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1305 img
= CALLOC_STRUCT(__DRIimageRec
);
1309 img
->texture
= NULL
;
1310 pipe_resource_reference(&img
->texture
, image
->texture
);
1311 img
->level
= image
->level
;
1312 img
->layer
= image
->layer
;
1313 img
->dri_format
= image
->dri_format
;
1314 /* This should be 0 for sub images, but dup is also used for base images. */
1315 img
->dri_components
= image
->dri_components
;
1316 img
->loader_private
= loaderPrivate
;
1322 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1324 if (!image
|| !image
->texture
)
1327 struct pipe_screen
*screen
= image
->texture
->screen
;
1328 if (!screen
->check_resource_capability
)
1331 /* We don't want to check these:
1332 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1333 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1336 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1337 bind
|= PIPE_BIND_SCANOUT
;
1338 if (use
& __DRI_IMAGE_USE_LINEAR
)
1339 bind
|= PIPE_BIND_LINEAR
;
1340 if (use
& __DRI_IMAGE_USE_CURSOR
)
1341 bind
|= PIPE_BIND_CURSOR
;
1346 return screen
->check_resource_capability(screen
, image
->texture
, bind
);
1350 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1351 int *names
, int num_names
, int *strides
, int *offsets
,
1352 void *loaderPrivate
)
1356 struct winsys_handle whandle
;
1361 format
= convert_fourcc(format
, &dri_components
);
1365 memset(&whandle
, 0, sizeof(whandle
));
1366 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1367 whandle
.handle
= names
[0];
1368 whandle
.stride
= strides
[0];
1369 whandle
.offset
= offsets
[0];
1370 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1372 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1373 1, &whandle
, loaderPrivate
);
1377 img
->dri_components
= dri_components
;
1382 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1389 if (image
->dri_components
== 0)
1392 img
= dri2_dup_image(image
, loaderPrivate
);
1396 if (img
->texture
->screen
->resource_changed
)
1397 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1400 /* set this to 0 for sub images. */
1401 img
->dri_components
= 0;
1406 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1407 int *fds
, int num_fds
, int *strides
, int *offsets
,
1408 void *loaderPrivate
)
1413 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1414 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1415 strides
, offsets
, NULL
,
1416 &dri_components
, loaderPrivate
);
1420 img
->dri_components
= dri_components
;
1425 dri2_query_dma_buf_formats(__DRIscreen
*_screen
, int max
, int *formats
,
1428 struct dri_screen
*screen
= dri_screen(_screen
);
1429 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1430 const unsigned bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1433 for (i
= 0, j
= 0; (i
< ARRAY_SIZE(fourcc_formats
)) &&
1434 (j
< max
|| max
== 0); i
++) {
1435 if (pscreen
->is_format_supported(pscreen
,
1436 fourcc_to_pipe_format(
1441 formats
[j
] = fourcc_formats
[i
];
1450 dri2_query_dma_buf_modifiers(__DRIscreen
*_screen
, int fourcc
, int max
,
1451 uint64_t *modifiers
, unsigned int *external_only
,
1454 struct dri_screen
*screen
= dri_screen(_screen
);
1455 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1456 enum pipe_format format
= fourcc_to_pipe_format(fourcc
);
1457 const unsigned usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1459 if (pscreen
->query_dmabuf_modifiers
!= NULL
&&
1460 pscreen
->is_format_supported(pscreen
, format
, screen
->target
, 0, usage
)) {
1461 pscreen
->query_dmabuf_modifiers(pscreen
, format
, max
, modifiers
,
1462 external_only
, count
);
1469 dri2_from_dma_bufs(__DRIscreen
*screen
,
1470 int width
, int height
, int fourcc
,
1471 int *fds
, int num_fds
,
1472 int *strides
, int *offsets
,
1473 enum __DRIYUVColorSpace yuv_color_space
,
1474 enum __DRISampleRange sample_range
,
1475 enum __DRIChromaSiting horizontal_siting
,
1476 enum __DRIChromaSiting vertical_siting
,
1478 void *loaderPrivate
)
1483 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1484 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1485 strides
, offsets
, error
,
1486 &dri_components
, loaderPrivate
);
1490 img
->yuv_color_space
= yuv_color_space
;
1491 img
->sample_range
= sample_range
;
1492 img
->horizontal_siting
= horizontal_siting
;
1493 img
->vertical_siting
= vertical_siting
;
1494 img
->dri_components
= dri_components
;
1496 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1501 dri2_from_dma_bufs2(__DRIscreen
*screen
,
1502 int width
, int height
, int fourcc
,
1503 uint64_t modifier
, int *fds
, int num_fds
,
1504 int *strides
, int *offsets
,
1505 enum __DRIYUVColorSpace yuv_color_space
,
1506 enum __DRISampleRange sample_range
,
1507 enum __DRIChromaSiting horizontal_siting
,
1508 enum __DRIChromaSiting vertical_siting
,
1510 void *loaderPrivate
)
1515 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1516 modifier
, fds
, num_fds
, strides
, offsets
,
1517 error
, &dri_components
, loaderPrivate
);
1521 img
->yuv_color_space
= yuv_color_space
;
1522 img
->sample_range
= sample_range
;
1523 img
->horizontal_siting
= horizontal_siting
;
1524 img
->vertical_siting
= vertical_siting
;
1525 img
->dri_components
= dri_components
;
1527 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1532 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1533 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1534 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1537 struct dri_context
*ctx
= dri_context(context
);
1538 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1539 struct pipe_screen
*screen
;
1540 struct pipe_fence_handle
*fence
;
1541 struct pipe_blit_info blit
;
1546 memset(&blit
, 0, sizeof(blit
));
1547 blit
.dst
.resource
= dst
->texture
;
1548 blit
.dst
.box
.x
= dstx0
;
1549 blit
.dst
.box
.y
= dsty0
;
1550 blit
.dst
.box
.width
= dstwidth
;
1551 blit
.dst
.box
.height
= dstheight
;
1552 blit
.dst
.box
.depth
= 1;
1553 blit
.dst
.format
= dst
->texture
->format
;
1554 blit
.src
.resource
= src
->texture
;
1555 blit
.src
.box
.x
= srcx0
;
1556 blit
.src
.box
.y
= srcy0
;
1557 blit
.src
.box
.width
= srcwidth
;
1558 blit
.src
.box
.height
= srcheight
;
1559 blit
.src
.box
.depth
= 1;
1560 blit
.src
.format
= src
->texture
->format
;
1561 blit
.mask
= PIPE_MASK_RGBA
;
1562 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1564 pipe
->blit(pipe
, &blit
);
1566 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1567 pipe
->flush_resource(pipe
, dst
->texture
);
1568 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1569 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1570 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1571 pipe
->flush_resource(pipe
, dst
->texture
);
1572 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1573 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1574 screen
->fence_reference(screen
, &fence
, NULL
);
1579 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1580 int x0
, int y0
, int width
, int height
,
1581 unsigned int flags
, int *stride
, void **data
)
1583 struct dri_context
*ctx
= dri_context(context
);
1584 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1585 enum pipe_transfer_usage pipe_access
= 0;
1586 struct pipe_transfer
*trans
;
1589 if (!image
|| !data
|| *data
)
1592 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1593 pipe_access
|= PIPE_TRANSFER_READ
;
1594 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1595 pipe_access
|= PIPE_TRANSFER_WRITE
;
1597 map
= pipe_transfer_map(pipe
, image
->texture
,
1598 0, 0, pipe_access
, x0
, y0
, width
, height
,
1602 *stride
= trans
->stride
;
1609 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1611 struct dri_context
*ctx
= dri_context(context
);
1612 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1614 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1618 dri2_get_capabilities(__DRIscreen
*_screen
)
1620 struct dri_screen
*screen
= dri_screen(_screen
);
1622 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1625 /* The extension is modified during runtime if DRI_PRIME is detected */
1626 static __DRIimageExtension dri2ImageExtension
= {
1627 .base
= { __DRI_IMAGE
, 17 },
1629 .createImageFromName
= dri2_create_image_from_name
,
1630 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1631 .destroyImage
= dri2_destroy_image
,
1632 .createImage
= dri2_create_image
,
1633 .queryImage
= dri2_query_image
,
1634 .dupImage
= dri2_dup_image
,
1635 .validateUsage
= dri2_validate_usage
,
1636 .createImageFromNames
= dri2_from_names
,
1637 .fromPlanar
= dri2_from_planar
,
1638 .createImageFromTexture
= dri2_create_from_texture
,
1639 .createImageFromFds
= NULL
,
1640 .createImageFromDmaBufs
= NULL
,
1641 .blitImage
= dri2_blit_image
,
1642 .getCapabilities
= dri2_get_capabilities
,
1643 .mapImage
= dri2_map_image
,
1644 .unmapImage
= dri2_unmap_image
,
1645 .createImageWithModifiers
= NULL
,
1646 .createImageFromDmaBufs2
= NULL
,
1647 .queryDmaBufFormats
= NULL
,
1648 .queryDmaBufModifiers
= NULL
,
1649 .queryDmaBufFormatModifierAttribs
= NULL
,
1650 .createImageFromRenderbuffer2
= dri2_create_image_from_renderbuffer2
,
1653 static const __DRIrobustnessExtension dri2Robustness
= {
1654 .base
= { __DRI2_ROBUSTNESS
, 1 }
1658 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1659 struct mesa_glinterop_device_info
*out
)
1661 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1663 /* There is no version 0, thus we do not support it */
1664 if (out
->version
== 0)
1665 return MESA_GLINTEROP_INVALID_VERSION
;
1667 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1668 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1669 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1670 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1672 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1673 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1675 /* Instruct the caller that we support up-to version one of the interface */
1678 return MESA_GLINTEROP_SUCCESS
;
1682 dri2_interop_export_object(__DRIcontext
*_ctx
,
1683 struct mesa_glinterop_export_in
*in
,
1684 struct mesa_glinterop_export_out
*out
)
1686 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1687 struct pipe_screen
*screen
= st
->pipe
->screen
;
1688 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1689 struct pipe_resource
*res
= NULL
;
1690 struct winsys_handle whandle
;
1691 unsigned target
, usage
;
1694 /* There is no version 0, thus we do not support it */
1695 if (in
->version
== 0 || out
->version
== 0)
1696 return MESA_GLINTEROP_INVALID_VERSION
;
1698 /* Validate the target. */
1699 switch (in
->target
) {
1700 case GL_TEXTURE_BUFFER
:
1704 case GL_TEXTURE_RECTANGLE
:
1705 case GL_TEXTURE_1D_ARRAY
:
1706 case GL_TEXTURE_2D_ARRAY
:
1707 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1708 case GL_TEXTURE_CUBE_MAP
:
1709 case GL_TEXTURE_2D_MULTISAMPLE
:
1710 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1711 case GL_TEXTURE_EXTERNAL_OES
:
1712 case GL_RENDERBUFFER
:
1713 case GL_ARRAY_BUFFER
:
1714 target
= in
->target
;
1716 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1717 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1718 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1719 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1720 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1721 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1722 target
= GL_TEXTURE_CUBE_MAP
;
1725 return MESA_GLINTEROP_INVALID_TARGET
;
1728 /* Validate the simple case of miplevel. */
1729 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1731 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1733 /* Validate the OpenGL object and get pipe_resource. */
1734 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1736 if (target
== GL_ARRAY_BUFFER
) {
1739 * The error checking is based on the documentation of
1740 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1742 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1744 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1745 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1746 * a GL buffer object but does not have an existing data store or
1747 * the size of the buffer is 0."
1749 if (!buf
|| buf
->Size
== 0) {
1750 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1751 return MESA_GLINTEROP_INVALID_OBJECT
;
1754 res
= st_buffer_object(buf
)->buffer
;
1756 /* this shouldn't happen */
1757 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1758 return MESA_GLINTEROP_INVALID_OBJECT
;
1761 out
->buf_offset
= 0;
1762 out
->buf_size
= buf
->Size
;
1764 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1765 } else if (target
== GL_RENDERBUFFER
) {
1768 * The error checking is based on the documentation of
1769 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1771 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1773 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1774 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1775 * object or if the width or height of renderbuffer is zero."
1777 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1778 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1779 return MESA_GLINTEROP_INVALID_OBJECT
;
1782 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1783 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1784 * renderbuffer object."
1786 if (rb
->NumSamples
> 1) {
1787 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1788 return MESA_GLINTEROP_INVALID_OPERATION
;
1791 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1792 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1793 * required by the OpenCL implementation on the device."
1795 res
= st_renderbuffer(rb
)->texture
;
1797 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1798 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1801 out
->internal_format
= rb
->InternalFormat
;
1802 out
->view_minlevel
= 0;
1803 out
->view_numlevels
= 1;
1804 out
->view_minlayer
= 0;
1805 out
->view_numlayers
= 1;
1809 * The error checking is based on the documentation of
1810 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1812 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1815 _mesa_test_texobj_completeness(ctx
, obj
);
1817 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1818 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1819 * type matches texture_target, if the specified miplevel of texture
1820 * is not defined, or if the width or height of the specified
1821 * miplevel is zero or if the GL texture object is incomplete."
1824 obj
->Target
!= target
||
1825 !obj
->_BaseComplete
||
1826 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1827 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1828 return MESA_GLINTEROP_INVALID_OBJECT
;
1831 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1832 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1833 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1834 * implementations); or greater than the value of q (for both OpenGL
1835 * and OpenGL ES). levelbase and q are defined for the texture in
1836 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1837 * specification and section 3.7.10 of the OpenGL ES 2.0."
1839 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1840 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1841 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1844 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1845 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1846 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1849 res
= st_get_texobj_resource(obj
);
1851 /* Incomplete texture buffer object? This shouldn't really occur. */
1852 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1853 return MESA_GLINTEROP_INVALID_OBJECT
;
1856 if (target
== GL_TEXTURE_BUFFER
) {
1857 out
->internal_format
= obj
->BufferObjectFormat
;
1858 out
->buf_offset
= obj
->BufferOffset
;
1859 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1862 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1864 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1865 out
->view_minlevel
= obj
->MinLevel
;
1866 out
->view_numlevels
= obj
->NumLevels
;
1867 out
->view_minlayer
= obj
->MinLayer
;
1868 out
->view_numlayers
= obj
->NumLayers
;
1872 /* Get the handle. */
1873 switch (in
->access
) {
1874 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1875 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1877 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1878 usage
= PIPE_HANDLE_USAGE_READ
;
1880 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1881 usage
= PIPE_HANDLE_USAGE_WRITE
;
1887 memset(&whandle
, 0, sizeof(whandle
));
1888 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1890 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1892 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1895 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1897 out
->dmabuf_fd
= whandle
.handle
;
1898 out
->out_driver_data_written
= 0;
1900 if (res
->target
== PIPE_BUFFER
)
1901 out
->buf_offset
+= whandle
.offset
;
1903 /* Instruct the caller that we support up-to version one of the interface */
1907 return MESA_GLINTEROP_SUCCESS
;
1910 static const __DRI2interopExtension dri2InteropExtension
= {
1911 .base
= { __DRI2_INTEROP
, 1 },
1912 .query_device_info
= dri2_interop_query_device_info
,
1913 .export_object
= dri2_interop_export_object
1917 * \brief the DRI2ConfigQueryExtension configQueryb method
1920 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1923 struct dri_screen
*screen
= dri_screen(sPriv
);
1925 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_BOOL
))
1926 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1928 *val
= driQueryOptionb(&screen
->dev
->option_cache
, var
);
1934 * \brief the DRI2ConfigQueryExtension configQueryi method
1937 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1939 struct dri_screen
*screen
= dri_screen(sPriv
);
1941 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_INT
) &&
1942 !driCheckOption(&screen
->dev
->option_cache
, var
, DRI_ENUM
))
1943 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
1945 *val
= driQueryOptioni(&screen
->dev
->option_cache
, var
);
1951 * \brief the DRI2ConfigQueryExtension configQueryf method
1954 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
1956 struct dri_screen
*screen
= dri_screen(sPriv
);
1958 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_FLOAT
))
1959 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
1961 *val
= driQueryOptionf(&screen
->dev
->option_cache
, var
);
1967 * \brief the DRI2ConfigQueryExtension struct.
1969 * We first query the driver option cache. Then the dri2 option cache.
1971 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
1972 .base
= { __DRI2_CONFIG_QUERY
, 1 },
1974 .configQueryb
= dri2GalliumConfigQueryb
,
1975 .configQueryi
= dri2GalliumConfigQueryi
,
1976 .configQueryf
= dri2GalliumConfigQueryf
,
1980 * Backend function init_screen.
1983 static const __DRIextension
*dri_screen_extensions
[] = {
1984 &driTexBufferExtension
.base
,
1985 &dri2FlushExtension
.base
,
1986 &dri2ImageExtension
.base
,
1987 &dri2RendererQueryExtension
.base
,
1988 &dri2GalliumConfigQueryExtension
.base
,
1989 &dri2ThrottleExtension
.base
,
1990 &dri2FenceExtension
.base
,
1991 &dri2InteropExtension
.base
,
1992 &dri2NoErrorExtension
.base
,
1993 &dri2FlushControlExtension
.base
,
1997 static const __DRIextension
*dri_robust_screen_extensions
[] = {
1998 &driTexBufferExtension
.base
,
1999 &dri2FlushExtension
.base
,
2000 &dri2ImageExtension
.base
,
2001 &dri2RendererQueryExtension
.base
,
2002 &dri2GalliumConfigQueryExtension
.base
,
2003 &dri2ThrottleExtension
.base
,
2004 &dri2FenceExtension
.base
,
2005 &dri2InteropExtension
.base
,
2006 &dri2Robustness
.base
,
2007 &dri2NoErrorExtension
.base
,
2008 &dri2FlushControlExtension
.base
,
2013 * This is the driver specific part of the createNewScreen entry point.
2015 * Returns the struct gl_config supported by this driver.
2017 static const __DRIconfig
**
2018 dri2_init_screen(__DRIscreen
* sPriv
)
2020 const __DRIconfig
**configs
;
2021 struct dri_screen
*screen
;
2022 struct pipe_screen
*pscreen
= NULL
;
2023 const struct drm_conf_ret
*throttle_ret
;
2024 const struct drm_conf_ret
*dmabuf_ret
;
2027 screen
= CALLOC_STRUCT(dri_screen
);
2031 screen
->sPriv
= sPriv
;
2032 screen
->fd
= sPriv
->fd
;
2033 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
2035 sPriv
->driverPrivate
= (void *)screen
;
2037 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2041 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
)) {
2042 dri_init_options(screen
);
2044 pscreen
= pipe_loader_create_screen(screen
->dev
);
2050 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
2051 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
2053 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
2054 screen
->throttling_enabled
= TRUE
;
2055 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
2058 if (pscreen
->resource_create_with_modifiers
)
2059 dri2ImageExtension
.createImageWithModifiers
=
2060 dri2_create_image_with_modifiers
;
2062 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
2065 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2066 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2067 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2068 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2069 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2070 if (pscreen
->query_dmabuf_modifiers
) {
2071 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2072 dri2ImageExtension
.queryDmaBufModifiers
=
2073 dri2_query_dma_buf_modifiers
;
2078 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
2079 sPriv
->extensions
= dri_robust_screen_extensions
;
2080 screen
->has_reset_status_query
= true;
2083 sPriv
->extensions
= dri_screen_extensions
;
2085 configs
= dri_init_screen_helper(screen
, pscreen
);
2087 goto destroy_screen
;
2089 screen
->can_share_buffer
= true;
2090 screen
->auto_fake_front
= dri_with_format(sPriv
);
2091 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2092 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2097 dri_destroy_screen_helper(screen
);
2101 pipe_loader_release(&screen
->dev
, 1);
2111 * This is the driver specific part of the createNewScreen entry point.
2113 * Returns the struct gl_config supported by this driver.
2115 static const __DRIconfig
**
2116 dri_kms_init_screen(__DRIscreen
* sPriv
)
2118 #if defined(GALLIUM_SOFTPIPE)
2119 const __DRIconfig
**configs
;
2120 struct dri_screen
*screen
;
2121 struct pipe_screen
*pscreen
= NULL
;
2125 screen
= CALLOC_STRUCT(dri_screen
);
2129 screen
->sPriv
= sPriv
;
2130 screen
->fd
= sPriv
->fd
;
2132 sPriv
->driverPrivate
= (void *)screen
;
2134 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2137 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
)) {
2138 dri_init_options(screen
);
2139 pscreen
= pipe_loader_create_screen(screen
->dev
);
2145 if (pscreen
->resource_create_with_modifiers
)
2146 dri2ImageExtension
.createImageWithModifiers
=
2147 dri2_create_image_with_modifiers
;
2149 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2150 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2151 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2152 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2153 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2154 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2155 dri2ImageExtension
.queryDmaBufModifiers
= dri2_query_dma_buf_modifiers
;
2158 sPriv
->extensions
= dri_screen_extensions
;
2160 configs
= dri_init_screen_helper(screen
, pscreen
);
2162 goto destroy_screen
;
2164 screen
->can_share_buffer
= false;
2165 screen
->auto_fake_front
= dri_with_format(sPriv
);
2166 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2167 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2172 dri_destroy_screen_helper(screen
);
2176 pipe_loader_release(&screen
->dev
, 1);
2182 #endif // GALLIUM_SOFTPIPE
2187 dri2_create_buffer(__DRIscreen
* sPriv
,
2188 __DRIdrawable
* dPriv
,
2189 const struct gl_config
* visual
, boolean isPixmap
)
2191 struct dri_drawable
*drawable
= NULL
;
2193 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2196 drawable
= dPriv
->driverPrivate
;
2198 drawable
->allocate_textures
= dri2_allocate_textures
;
2199 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2200 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2201 drawable
->flush_swapbuffers
= dri2_flush_swapbuffers
;
2207 * DRI driver virtual function table.
2209 * DRI versions differ in their implementation of init_screen and swap_buffers.
2211 const struct __DriverAPIRec galliumdrm_driver_api
= {
2212 .InitScreen
= dri2_init_screen
,
2213 .DestroyScreen
= dri_destroy_screen
,
2214 .CreateContext
= dri_create_context
,
2215 .DestroyContext
= dri_destroy_context
,
2216 .CreateBuffer
= dri2_create_buffer
,
2217 .DestroyBuffer
= dri_destroy_buffer
,
2218 .MakeCurrent
= dri_make_current
,
2219 .UnbindContext
= dri_unbind_context
,
2221 .AllocateBuffer
= dri2_allocate_buffer
,
2222 .ReleaseBuffer
= dri2_release_buffer
,
2226 * DRI driver virtual function table.
2228 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2229 * hook. The latter is used to explicitly initialise the kms_swrast driver
2230 * rather than selecting the approapriate driver as suggested by the loader.
2232 const struct __DriverAPIRec dri_kms_driver_api
= {
2233 .InitScreen
= dri_kms_init_screen
,
2234 .DestroyScreen
= dri_destroy_screen
,
2235 .CreateContext
= dri_create_context
,
2236 .DestroyContext
= dri_destroy_context
,
2237 .CreateBuffer
= dri2_create_buffer
,
2238 .DestroyBuffer
= dri_destroy_buffer
,
2239 .MakeCurrent
= dri_make_current
,
2240 .UnbindContext
= dri_unbind_context
,
2242 .AllocateBuffer
= dri2_allocate_buffer
,
2243 .ReleaseBuffer
= dri2_release_buffer
,
2246 /* This is the table of extensions that the loader will dlsym() for. */
2247 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2248 &driCoreExtension
.base
,
2249 &driImageDriverExtension
.base
,
2250 &driDRI2Extension
.base
,
2251 &gallium_config_options
.base
,
2255 /* vim: set sw=3 ts=8 sts=3 expandtab: */