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_ABGR2101010
,
61 __DRI_IMAGE_FOURCC_XBGR2101010
,
62 __DRI_IMAGE_FOURCC_ARGB8888
,
63 __DRI_IMAGE_FOURCC_ABGR8888
,
64 __DRI_IMAGE_FOURCC_SARGB8888
,
65 __DRI_IMAGE_FOURCC_XRGB8888
,
66 __DRI_IMAGE_FOURCC_XBGR8888
,
67 __DRI_IMAGE_FOURCC_ARGB1555
,
68 __DRI_IMAGE_FOURCC_RGB565
,
69 __DRI_IMAGE_FOURCC_R8
,
70 __DRI_IMAGE_FOURCC_R16
,
71 __DRI_IMAGE_FOURCC_GR88
,
72 __DRI_IMAGE_FOURCC_GR1616
,
73 __DRI_IMAGE_FOURCC_YUV410
,
74 __DRI_IMAGE_FOURCC_YUV411
,
75 __DRI_IMAGE_FOURCC_YUV420
,
76 __DRI_IMAGE_FOURCC_YUV422
,
77 __DRI_IMAGE_FOURCC_YUV444
,
78 __DRI_IMAGE_FOURCC_YVU410
,
79 __DRI_IMAGE_FOURCC_YVU411
,
80 __DRI_IMAGE_FOURCC_YVU420
,
81 __DRI_IMAGE_FOURCC_YVU422
,
82 __DRI_IMAGE_FOURCC_YVU444
,
83 __DRI_IMAGE_FOURCC_NV12
,
84 __DRI_IMAGE_FOURCC_NV16
,
85 __DRI_IMAGE_FOURCC_YUYV
88 static int convert_fourcc(int format
, int *dri_components_p
)
92 case __DRI_IMAGE_FOURCC_RGB565
:
93 format
= __DRI_IMAGE_FORMAT_RGB565
;
94 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
96 case __DRI_IMAGE_FOURCC_ARGB8888
:
97 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
98 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
100 case __DRI_IMAGE_FOURCC_XRGB8888
:
101 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
102 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
104 case __DRI_IMAGE_FOURCC_ABGR8888
:
105 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
106 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
108 case __DRI_IMAGE_FOURCC_XBGR8888
:
109 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
110 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
112 case __DRI_IMAGE_FOURCC_ARGB2101010
:
113 format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
114 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
116 case __DRI_IMAGE_FOURCC_XRGB2101010
:
117 format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
118 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
120 case __DRI_IMAGE_FOURCC_ABGR2101010
:
121 format
= __DRI_IMAGE_FORMAT_ABGR2101010
;
122 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
124 case __DRI_IMAGE_FOURCC_XBGR2101010
:
125 format
= __DRI_IMAGE_FORMAT_XBGR2101010
;
126 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
128 case __DRI_IMAGE_FOURCC_R8
:
129 format
= __DRI_IMAGE_FORMAT_R8
;
130 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
132 case __DRI_IMAGE_FOURCC_GR88
:
133 format
= __DRI_IMAGE_FORMAT_GR88
;
134 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
136 case __DRI_IMAGE_FOURCC_R16
:
137 format
= __DRI_IMAGE_FORMAT_R16
;
138 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
140 case __DRI_IMAGE_FOURCC_GR1616
:
141 format
= __DRI_IMAGE_FORMAT_GR1616
;
142 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
144 case __DRI_IMAGE_FOURCC_YUYV
:
145 format
= __DRI_IMAGE_FORMAT_YUYV
;
146 dri_components
= __DRI_IMAGE_COMPONENTS_Y_XUXV
;
149 * For multi-planar YUV formats, we return the format of the first
150 * plane only. Since there is only one caller which supports multi-
151 * planar YUV it gets to figure out the remaining planes on it's
154 case __DRI_IMAGE_FOURCC_YUV420
:
155 case __DRI_IMAGE_FOURCC_YVU420
:
156 format
= __DRI_IMAGE_FORMAT_R8
;
157 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
159 case __DRI_IMAGE_FOURCC_NV12
:
160 format
= __DRI_IMAGE_FORMAT_R8
;
161 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
166 *dri_components_p
= dri_components
;
170 /* NOTE this probably isn't going to do the right thing for YUV images
171 * (but I think the same can be said for intel_query_image()). I think
172 * only needed for exporting dmabuf's, so I think I won't loose much
175 static int convert_to_fourcc(int format
)
178 case __DRI_IMAGE_FORMAT_RGB565
:
179 format
= __DRI_IMAGE_FOURCC_RGB565
;
181 case __DRI_IMAGE_FORMAT_ARGB8888
:
182 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
184 case __DRI_IMAGE_FORMAT_XRGB8888
:
185 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
187 case __DRI_IMAGE_FORMAT_ABGR8888
:
188 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
190 case __DRI_IMAGE_FORMAT_XBGR8888
:
191 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
193 case __DRI_IMAGE_FORMAT_ARGB2101010
:
194 format
= __DRI_IMAGE_FOURCC_ARGB2101010
;
196 case __DRI_IMAGE_FORMAT_XRGB2101010
:
197 format
= __DRI_IMAGE_FOURCC_XRGB2101010
;
199 case __DRI_IMAGE_FORMAT_ABGR2101010
:
200 format
= __DRI_IMAGE_FOURCC_ABGR2101010
;
202 case __DRI_IMAGE_FORMAT_XBGR2101010
:
203 format
= __DRI_IMAGE_FOURCC_XBGR2101010
;
205 case __DRI_IMAGE_FORMAT_R8
:
206 format
= __DRI_IMAGE_FOURCC_R8
;
208 case __DRI_IMAGE_FORMAT_GR88
:
209 format
= __DRI_IMAGE_FOURCC_GR88
;
217 static enum pipe_format
dri2_format_to_pipe_format (int format
)
222 case __DRI_IMAGE_FORMAT_RGB565
:
223 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
225 case __DRI_IMAGE_FORMAT_XRGB8888
:
226 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
228 case __DRI_IMAGE_FORMAT_ARGB8888
:
229 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
231 case __DRI_IMAGE_FORMAT_XBGR8888
:
232 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
234 case __DRI_IMAGE_FORMAT_ABGR8888
:
235 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
237 case __DRI_IMAGE_FORMAT_XRGB2101010
:
238 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
240 case __DRI_IMAGE_FORMAT_ARGB2101010
:
241 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
243 case __DRI_IMAGE_FORMAT_XBGR2101010
:
244 pf
= PIPE_FORMAT_R10G10B10X2_UNORM
;
246 case __DRI_IMAGE_FORMAT_ABGR2101010
:
247 pf
= PIPE_FORMAT_R10G10B10A2_UNORM
;
249 case __DRI_IMAGE_FORMAT_R8
:
250 pf
= PIPE_FORMAT_R8_UNORM
;
252 case __DRI_IMAGE_FORMAT_GR88
:
253 pf
= PIPE_FORMAT_RG88_UNORM
;
255 case __DRI_IMAGE_FORMAT_R16
:
256 pf
= PIPE_FORMAT_R16_UNORM
;
258 case __DRI_IMAGE_FORMAT_GR1616
:
259 pf
= PIPE_FORMAT_R16G16_UNORM
;
261 case __DRI_IMAGE_FORMAT_YUYV
:
262 pf
= PIPE_FORMAT_YUYV
;
265 pf
= PIPE_FORMAT_NONE
;
272 static enum pipe_format
fourcc_to_pipe_format(int fourcc
)
277 case __DRI_IMAGE_FOURCC_R8
:
278 pf
= PIPE_FORMAT_R8_UNORM
;
280 case __DRI_IMAGE_FOURCC_GR88
:
281 pf
= PIPE_FORMAT_RG88_UNORM
;
283 case __DRI_IMAGE_FOURCC_ARGB1555
:
284 pf
= PIPE_FORMAT_B5G5R5A1_UNORM
;
286 case __DRI_IMAGE_FOURCC_R16
:
287 pf
= PIPE_FORMAT_R16_UNORM
;
289 case __DRI_IMAGE_FOURCC_GR1616
:
290 pf
= PIPE_FORMAT_RG1616_UNORM
;
292 case __DRI_IMAGE_FOURCC_RGB565
:
293 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
295 case __DRI_IMAGE_FOURCC_ARGB8888
:
296 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
298 case __DRI_IMAGE_FOURCC_XRGB8888
:
299 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
301 case __DRI_IMAGE_FOURCC_ABGR8888
:
302 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
304 case __DRI_IMAGE_FOURCC_XBGR8888
:
305 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
307 case __DRI_IMAGE_FOURCC_ARGB2101010
:
308 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
310 case __DRI_IMAGE_FOURCC_XRGB2101010
:
311 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
313 case __DRI_IMAGE_FOURCC_ABGR2101010
:
314 pf
= PIPE_FORMAT_R10G10B10A2_UNORM
;
316 case __DRI_IMAGE_FOURCC_XBGR2101010
:
317 pf
= PIPE_FORMAT_R10G10B10X2_UNORM
;
320 case __DRI_IMAGE_FOURCC_NV12
:
321 pf
= PIPE_FORMAT_NV12
;
323 case __DRI_IMAGE_FOURCC_YUYV
:
324 pf
= PIPE_FORMAT_YUYV
;
326 case __DRI_IMAGE_FOURCC_YUV420
:
327 case __DRI_IMAGE_FOURCC_YVU420
:
328 pf
= PIPE_FORMAT_YV12
;
331 case __DRI_IMAGE_FOURCC_SARGB8888
:
332 case __DRI_IMAGE_FOURCC_YUV410
:
333 case __DRI_IMAGE_FOURCC_YUV411
:
334 case __DRI_IMAGE_FOURCC_YUV422
:
335 case __DRI_IMAGE_FOURCC_YUV444
:
336 case __DRI_IMAGE_FOURCC_NV16
:
337 case __DRI_IMAGE_FOURCC_YVU410
:
338 case __DRI_IMAGE_FOURCC_YVU411
:
339 case __DRI_IMAGE_FOURCC_YVU422
:
340 case __DRI_IMAGE_FOURCC_YVU444
:
342 pf
= PIPE_FORMAT_NONE
;
349 * DRI2 flush extension.
352 dri2_flush_drawable(__DRIdrawable
*dPriv
)
354 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
358 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
360 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
362 dri2InvalidateDrawable(dPriv
);
363 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
364 drawable
->texture_mask
= 0;
366 p_atomic_inc(&drawable
->base
.stamp
);
369 static const __DRI2flushExtension dri2FlushExtension
= {
370 .base
= { __DRI2_FLUSH
, 4 },
372 .flush
= dri2_flush_drawable
,
373 .invalidate
= dri2_invalidate_drawable
,
374 .flush_with_flags
= dri_flush
,
378 * Retrieve __DRIbuffer from the DRI loader.
381 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
382 const enum st_attachment_type
*atts
,
385 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
386 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
388 __DRIbuffer
*buffers
;
390 unsigned attachments
[10];
391 unsigned num_attachments
, i
;
394 with_format
= dri_with_format(drawable
->sPriv
);
398 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
400 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
402 for (i
= 0; i
< *count
; i
++) {
403 enum pipe_format format
;
407 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
408 if (format
== PIPE_FORMAT_NONE
)
412 case ST_ATTACHMENT_FRONT_LEFT
:
416 att
= __DRI_BUFFER_FRONT_LEFT
;
418 case ST_ATTACHMENT_BACK_LEFT
:
419 att
= __DRI_BUFFER_BACK_LEFT
;
421 case ST_ATTACHMENT_FRONT_RIGHT
:
422 att
= __DRI_BUFFER_FRONT_RIGHT
;
424 case ST_ATTACHMENT_BACK_RIGHT
:
425 att
= __DRI_BUFFER_BACK_RIGHT
;
432 * In this switch statement we must support all formats that
433 * may occur as the stvis->color_format.
436 case PIPE_FORMAT_B10G10R10A2_UNORM
:
437 case PIPE_FORMAT_R10G10B10A2_UNORM
:
438 case PIPE_FORMAT_BGRA8888_UNORM
:
439 case PIPE_FORMAT_RGBA8888_UNORM
:
442 case PIPE_FORMAT_R10G10B10X2_UNORM
:
443 case PIPE_FORMAT_B10G10R10X2_UNORM
:
446 case PIPE_FORMAT_BGRX8888_UNORM
:
447 case PIPE_FORMAT_RGBX8888_UNORM
:
450 case PIPE_FORMAT_B5G6R5_UNORM
:
454 depth
= util_format_get_blocksizebits(format
);
455 assert(!"Unexpected format in dri2_drawable_get_buffers()");
458 attachments
[num_attachments
++] = att
;
460 attachments
[num_attachments
++] = depth
;
465 num_attachments
/= 2;
466 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
467 &dri_drawable
->w
, &dri_drawable
->h
,
468 attachments
, num_attachments
,
469 &num_buffers
, dri_drawable
->loaderPrivate
);
472 buffers
= loader
->getBuffers(dri_drawable
,
473 &dri_drawable
->w
, &dri_drawable
->h
,
474 attachments
, num_attachments
,
475 &num_buffers
, dri_drawable
->loaderPrivate
);
479 *count
= num_buffers
;
485 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
486 struct __DRIimageList
*images
,
487 const enum st_attachment_type
*statts
,
488 unsigned statts_count
)
490 __DRIdrawable
*dPriv
= drawable
->dPriv
;
491 __DRIscreen
*sPriv
= drawable
->sPriv
;
492 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
494 uint32_t buffer_mask
= 0;
497 for (i
= 0; i
< statts_count
; i
++) {
498 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
499 if (pf
== PIPE_FORMAT_NONE
)
503 case ST_ATTACHMENT_FRONT_LEFT
:
504 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
506 case ST_ATTACHMENT_BACK_LEFT
:
507 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
514 case PIPE_FORMAT_B5G6R5_UNORM
:
515 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
517 case PIPE_FORMAT_BGRX8888_UNORM
:
518 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
520 case PIPE_FORMAT_BGRA8888_UNORM
:
521 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
523 case PIPE_FORMAT_RGBX8888_UNORM
:
524 image_format
= __DRI_IMAGE_FORMAT_XBGR8888
;
526 case PIPE_FORMAT_RGBA8888_UNORM
:
527 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
529 case PIPE_FORMAT_B10G10R10X2_UNORM
:
530 image_format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
532 case PIPE_FORMAT_B10G10R10A2_UNORM
:
533 image_format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
535 case PIPE_FORMAT_R10G10B10X2_UNORM
:
536 image_format
= __DRI_IMAGE_FORMAT_XBGR2101010
;
538 case PIPE_FORMAT_R10G10B10A2_UNORM
:
539 image_format
= __DRI_IMAGE_FORMAT_ABGR2101010
;
542 image_format
= __DRI_IMAGE_FORMAT_NONE
;
547 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
548 (uint32_t *) &drawable
->base
.stamp
,
549 dPriv
->loaderPrivate
, buffer_mask
,
554 dri2_allocate_buffer(__DRIscreen
*sPriv
,
555 unsigned attachment
, unsigned format
,
556 int width
, int height
)
558 struct dri_screen
*screen
= dri_screen(sPriv
);
559 struct dri2_buffer
*buffer
;
560 struct pipe_resource templ
;
563 struct winsys_handle whandle
;
565 switch (attachment
) {
566 case __DRI_BUFFER_FRONT_LEFT
:
567 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
568 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
570 case __DRI_BUFFER_BACK_LEFT
:
571 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
573 case __DRI_BUFFER_DEPTH
:
574 case __DRI_BUFFER_DEPTH_STENCIL
:
575 case __DRI_BUFFER_STENCIL
:
576 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
580 /* because we get the handle and stride */
581 bind
|= PIPE_BIND_SHARED
;
585 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
588 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
591 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
594 pf
= PIPE_FORMAT_Z16_UNORM
;
600 buffer
= CALLOC_STRUCT(dri2_buffer
);
604 memset(&templ
, 0, sizeof(templ
));
607 templ
.target
= PIPE_TEXTURE_2D
;
608 templ
.last_level
= 0;
609 templ
.width0
= width
;
610 templ
.height0
= height
;
612 templ
.array_size
= 1;
615 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
616 if (!buffer
->resource
) {
621 memset(&whandle
, 0, sizeof(whandle
));
622 if (screen
->can_share_buffer
)
623 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
625 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
627 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
628 buffer
->resource
, &whandle
,
629 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
631 buffer
->base
.attachment
= attachment
;
632 buffer
->base
.name
= whandle
.handle
;
633 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
634 buffer
->base
.pitch
= whandle
.stride
;
636 return &buffer
->base
;
640 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
642 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
644 pipe_resource_reference(&buffer
->resource
, NULL
);
649 * Backend functions for st_framebuffer interface.
653 dri2_allocate_textures(struct dri_context
*ctx
,
654 struct dri_drawable
*drawable
,
655 const enum st_attachment_type
*statts
,
656 unsigned statts_count
)
658 __DRIscreen
*sPriv
= drawable
->sPriv
;
659 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
660 struct dri_screen
*screen
= dri_screen(sPriv
);
661 struct pipe_resource templ
;
662 boolean alloc_depthstencil
= FALSE
;
664 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
665 /* Image specific variables */
666 struct __DRIimageList images
;
667 /* Dri2 specific variables */
668 __DRIbuffer
*buffers
= NULL
;
669 struct winsys_handle whandle
;
670 unsigned num_buffers
= statts_count
;
672 /* First get the buffers from the loader */
674 if (!dri_image_drawable_get_buffers(drawable
, &images
,
675 statts
, statts_count
))
679 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
680 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
681 drawable
->old_w
== dri_drawable
->w
&&
682 drawable
->old_h
== dri_drawable
->h
&&
683 memcmp(drawable
->old
, buffers
,
684 sizeof(__DRIbuffer
) * num_buffers
) == 0))
688 /* Second clean useless resources*/
690 /* See if we need a depth-stencil buffer. */
691 for (i
= 0; i
< statts_count
; i
++) {
692 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
693 alloc_depthstencil
= TRUE
;
698 /* Delete the resources we won't need. */
699 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
700 /* Don't delete the depth-stencil buffer, we can reuse it. */
701 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
704 /* Flush the texture before unreferencing, so that other clients can
705 * see what the driver has rendered.
707 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
708 struct pipe_context
*pipe
= ctx
->st
->pipe
;
709 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
712 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
715 if (drawable
->stvis
.samples
> 1) {
716 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
719 /* Don't delete MSAA resources for the attachments which are enabled,
720 * we can reuse them. */
721 for (j
= 0; j
< statts_count
; j
++) {
722 if (i
== statts
[j
]) {
729 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
734 /* Third use the buffers retrieved to fill the drawable info */
736 memset(&templ
, 0, sizeof(templ
));
737 templ
.target
= screen
->target
;
738 templ
.last_level
= 0;
740 templ
.array_size
= 1;
743 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
744 struct pipe_resource
**buf
=
745 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
746 struct pipe_resource
*texture
= images
.front
->texture
;
748 dri_drawable
->w
= texture
->width0
;
749 dri_drawable
->h
= texture
->height0
;
751 pipe_resource_reference(buf
, texture
);
754 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
755 struct pipe_resource
**buf
=
756 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
757 struct pipe_resource
*texture
= images
.back
->texture
;
759 dri_drawable
->w
= texture
->width0
;
760 dri_drawable
->h
= texture
->height0
;
762 pipe_resource_reference(buf
, texture
);
765 /* Note: if there is both a back and a front buffer,
766 * then they have the same size.
768 templ
.width0
= dri_drawable
->w
;
769 templ
.height0
= dri_drawable
->h
;
772 memset(&whandle
, 0, sizeof(whandle
));
774 /* Process DRI-provided buffers and get pipe_resources. */
775 for (i
= 0; i
< num_buffers
; i
++) {
776 __DRIbuffer
*buf
= &buffers
[i
];
777 enum st_attachment_type statt
;
778 enum pipe_format format
;
780 switch (buf
->attachment
) {
781 case __DRI_BUFFER_FRONT_LEFT
:
782 if (!screen
->auto_fake_front
) {
783 continue; /* invalid attachment */
786 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
787 statt
= ST_ATTACHMENT_FRONT_LEFT
;
789 case __DRI_BUFFER_BACK_LEFT
:
790 statt
= ST_ATTACHMENT_BACK_LEFT
;
793 continue; /* invalid attachment */
796 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
797 if (format
== PIPE_FORMAT_NONE
)
800 /* dri2_drawable_get_buffers has already filled dri_drawable->w
801 * and dri_drawable->h */
802 templ
.width0
= dri_drawable
->w
;
803 templ
.height0
= dri_drawable
->h
;
804 templ
.format
= format
;
806 whandle
.handle
= buf
->name
;
807 whandle
.stride
= buf
->pitch
;
809 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
810 if (screen
->can_share_buffer
)
811 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
813 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
814 drawable
->textures
[statt
] =
815 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
817 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
818 assert(drawable
->textures
[statt
]);
822 /* Allocate private MSAA colorbuffers. */
823 if (drawable
->stvis
.samples
> 1) {
824 for (i
= 0; i
< statts_count
; i
++) {
825 enum st_attachment_type statt
= statts
[i
];
827 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
830 if (drawable
->textures
[statt
]) {
831 templ
.format
= drawable
->textures
[statt
]->format
;
832 templ
.bind
= drawable
->textures
[statt
]->bind
&
833 ~(PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
834 templ
.nr_samples
= drawable
->stvis
.samples
;
836 /* Try to reuse the resource.
837 * (the other resource parameters should be constant)
839 if (!drawable
->msaa_textures
[statt
] ||
840 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
841 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
842 /* Allocate a new one. */
843 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
845 drawable
->msaa_textures
[statt
] =
846 screen
->base
.screen
->resource_create(screen
->base
.screen
,
848 assert(drawable
->msaa_textures
[statt
]);
850 /* If there are any MSAA resources, we should initialize them
851 * such that they contain the same data as the single-sample
852 * resources we just got from the X server.
854 * The reason for this is that the state tracker (and
855 * therefore the app) can access the MSAA resources only.
856 * The single-sample resources are not exposed
857 * to the state tracker.
860 dri_pipe_blit(ctx
->st
->pipe
,
861 drawable
->msaa_textures
[statt
],
862 drawable
->textures
[statt
]);
866 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
871 /* Allocate a private depth-stencil buffer. */
872 if (alloc_depthstencil
) {
873 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
874 struct pipe_resource
**zsbuf
;
875 enum pipe_format format
;
878 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
881 templ
.format
= format
;
882 templ
.bind
= bind
& ~PIPE_BIND_SHARED
;
884 if (drawable
->stvis
.samples
> 1) {
885 templ
.nr_samples
= drawable
->stvis
.samples
;
886 zsbuf
= &drawable
->msaa_textures
[statt
];
889 templ
.nr_samples
= 0;
890 zsbuf
= &drawable
->textures
[statt
];
893 /* Try to reuse the resource.
894 * (the other resource parameters should be constant)
897 (*zsbuf
)->width0
!= templ
.width0
||
898 (*zsbuf
)->height0
!= templ
.height0
) {
899 /* Allocate a new one. */
900 pipe_resource_reference(zsbuf
, NULL
);
901 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
907 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
908 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
912 /* For DRI2, we may get the same buffers again from the server.
913 * To prevent useless imports of gem names, drawable->old* is used
914 * to bypass the import if we get the same buffers. This doesn't apply
915 * to DRI3/Wayland, users of image.loader, since the buffer is managed
916 * by the client (no import), and the back buffer is going to change
920 drawable
->old_num
= num_buffers
;
921 drawable
->old_w
= dri_drawable
->w
;
922 drawable
->old_h
= dri_drawable
->h
;
923 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
928 dri2_flush_frontbuffer(struct dri_context
*ctx
,
929 struct dri_drawable
*drawable
,
930 enum st_attachment_type statt
)
932 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
933 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
934 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
935 struct pipe_context
*pipe
= ctx
->st
->pipe
;
937 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
940 if (drawable
->stvis
.samples
> 1) {
941 /* Resolve the front buffer. */
942 dri_pipe_blit(ctx
->st
->pipe
,
943 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
944 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
947 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
948 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
951 pipe
->flush(pipe
, NULL
, 0);
954 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
956 else if (loader
->flushFrontBuffer
) {
957 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
962 * The struct dri_drawable flush_swapbuffers callback
965 dri2_flush_swapbuffers(struct dri_context
*ctx
,
966 struct dri_drawable
*drawable
)
968 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
969 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
971 if (image
&& image
->base
.version
>= 3 && image
->flushSwapBuffers
) {
972 image
->flushSwapBuffers(dri_drawable
, dri_drawable
->loaderPrivate
);
977 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
978 struct dri_context
*ctx
,
979 struct pipe_resource
*res
)
985 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
986 int width
, int height
, int format
,
987 int num_handles
, struct winsys_handle
*whandle
,
990 struct dri_screen
*screen
= dri_screen(_screen
);
991 struct pipe_screen
*pscreen
= screen
->base
.screen
;
993 struct pipe_resource templ
;
998 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1000 pf
= dri2_format_to_pipe_format (format
);
1001 if (pf
== PIPE_FORMAT_NONE
)
1004 img
= CALLOC_STRUCT(__DRIimageRec
);
1008 memset(&templ
, 0, sizeof(templ
));
1009 templ
.bind
= tex_usage
;
1010 templ
.target
= screen
->target
;
1011 templ
.last_level
= 0;
1013 templ
.array_size
= 1;
1015 for (i
= num_handles
- 1; i
>= 0; i
--) {
1016 struct pipe_resource
*tex
;
1018 /* TODO: something a lot less ugly */
1021 templ
.width0
= width
;
1022 templ
.height0
= height
;
1026 templ
.width0
= width
/ 2;
1027 templ
.height0
= height
/ 2;
1028 templ
.format
= (num_handles
== 2) ?
1029 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
1030 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
1033 templ
.width0
= width
/ 2;
1034 templ
.height0
= height
/ 2;
1035 templ
.format
= PIPE_FORMAT_R8_UNORM
;
1038 unreachable("too many planes!");
1041 tex
= pscreen
->resource_from_handle(pscreen
,
1042 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
1044 pipe_resource_reference(&img
->texture
, NULL
);
1049 tex
->next
= img
->texture
;
1055 img
->dri_format
= format
;
1057 img
->loader_private
= loaderPrivate
;
1063 dri2_create_image_from_name(__DRIscreen
*_screen
,
1064 int width
, int height
, int format
,
1065 int name
, int pitch
, void *loaderPrivate
)
1067 struct winsys_handle whandle
;
1068 enum pipe_format pf
;
1070 memset(&whandle
, 0, sizeof(whandle
));
1071 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1072 whandle
.handle
= name
;
1073 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1075 pf
= dri2_format_to_pipe_format (format
);
1076 if (pf
== PIPE_FORMAT_NONE
)
1079 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
1081 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1082 1, &whandle
, loaderPrivate
);
1086 dri2_create_image_from_fd(__DRIscreen
*_screen
,
1087 int width
, int height
, int fourcc
,
1088 uint64_t modifier
, int *fds
, int num_fds
,
1089 int *strides
, int *offsets
, unsigned *error
,
1090 int *dri_components
, void *loaderPrivate
)
1092 struct winsys_handle whandles
[3];
1094 __DRIimage
*img
= NULL
;
1095 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
1096 int expected_num_fds
, i
;
1099 case __DRI_IMAGE_FOURCC_YUV420
:
1100 case __DRI_IMAGE_FOURCC_YVU420
:
1101 expected_num_fds
= 3;
1103 case __DRI_IMAGE_FOURCC_NV12
:
1104 expected_num_fds
= 2;
1107 expected_num_fds
= 1;
1111 if (num_fds
!= expected_num_fds
) {
1112 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1116 format
= convert_fourcc(fourcc
, dri_components
);
1118 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1122 memset(whandles
, 0, sizeof(whandles
));
1124 for (i
= 0; i
< num_fds
; i
++) {
1126 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1130 whandles
[i
].type
= WINSYS_HANDLE_TYPE_FD
;
1131 whandles
[i
].handle
= (unsigned)fds
[i
];
1132 whandles
[i
].stride
= (unsigned)strides
[i
];
1133 whandles
[i
].offset
= (unsigned)offsets
[i
];
1134 whandles
[i
].modifier
= modifier
;
1137 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
1138 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1139 struct winsys_handle tmp
= whandles
[1];
1140 whandles
[1] = whandles
[2];
1142 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
1145 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1146 num_fds
, whandles
, loaderPrivate
);
1148 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1158 dri2_create_image_common(__DRIscreen
*_screen
,
1159 int width
, int height
,
1160 int format
, unsigned int use
,
1161 const uint64_t *modifiers
,
1162 const unsigned count
,
1163 void *loaderPrivate
)
1165 struct dri_screen
*screen
= dri_screen(_screen
);
1167 struct pipe_resource templ
;
1169 enum pipe_format pf
;
1171 /* createImageWithModifiers doesn't supply usage, and we should not get
1172 * here with both modifiers and a usage flag.
1174 assert(!(use
&& (modifiers
!= NULL
)));
1176 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1178 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1179 tex_usage
|= PIPE_BIND_SCANOUT
;
1180 if (use
& __DRI_IMAGE_USE_SHARE
)
1181 tex_usage
|= PIPE_BIND_SHARED
;
1182 if (use
& __DRI_IMAGE_USE_LINEAR
)
1183 tex_usage
|= PIPE_BIND_LINEAR
;
1184 if (use
& __DRI_IMAGE_USE_CURSOR
) {
1185 if (width
!= 64 || height
!= 64)
1187 tex_usage
|= PIPE_BIND_CURSOR
;
1190 pf
= dri2_format_to_pipe_format (format
);
1191 if (pf
== PIPE_FORMAT_NONE
)
1194 img
= CALLOC_STRUCT(__DRIimageRec
);
1198 memset(&templ
, 0, sizeof(templ
));
1199 templ
.bind
= tex_usage
;
1201 templ
.target
= PIPE_TEXTURE_2D
;
1202 templ
.last_level
= 0;
1203 templ
.width0
= width
;
1204 templ
.height0
= height
;
1206 templ
.array_size
= 1;
1211 ->resource_create_with_modifiers(screen
->base
.screen
,
1217 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1218 if (!img
->texture
) {
1225 img
->dri_format
= format
;
1226 img
->dri_components
= 0;
1229 img
->loader_private
= loaderPrivate
;
1234 dri2_create_image(__DRIscreen
*_screen
,
1235 int width
, int height
, int format
,
1236 unsigned int use
, void *loaderPrivate
)
1238 return dri2_create_image_common(_screen
, width
, height
, format
, use
,
1239 NULL
/* modifiers */, 0 /* count */,
1244 dri2_create_image_with_modifiers(__DRIscreen
*dri_screen
,
1245 int width
, int height
, int format
,
1246 const uint64_t *modifiers
,
1247 const unsigned count
,
1248 void *loaderPrivate
)
1250 return dri2_create_image_common(dri_screen
, width
, height
, format
,
1251 0 /* use */, modifiers
, count
,
1256 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1258 struct winsys_handle whandle
;
1261 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1262 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1264 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1266 memset(&whandle
, 0, sizeof(whandle
));
1269 case __DRI_IMAGE_ATTRIB_STRIDE
:
1270 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1271 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1272 NULL
, image
->texture
, &whandle
, usage
))
1274 *value
= whandle
.stride
;
1276 case __DRI_IMAGE_ATTRIB_OFFSET
:
1277 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1278 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1279 NULL
, image
->texture
, &whandle
, usage
))
1281 *value
= whandle
.offset
;
1283 case __DRI_IMAGE_ATTRIB_HANDLE
:
1284 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1285 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1286 NULL
, image
->texture
, &whandle
, usage
))
1288 *value
= whandle
.handle
;
1290 case __DRI_IMAGE_ATTRIB_NAME
:
1291 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1292 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1293 NULL
, image
->texture
, &whandle
, usage
))
1295 *value
= whandle
.handle
;
1297 case __DRI_IMAGE_ATTRIB_FD
:
1298 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
1299 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1300 NULL
, image
->texture
, &whandle
, usage
))
1303 *value
= whandle
.handle
;
1305 case __DRI_IMAGE_ATTRIB_FORMAT
:
1306 *value
= image
->dri_format
;
1308 case __DRI_IMAGE_ATTRIB_WIDTH
:
1309 *value
= image
->texture
->width0
;
1311 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1312 *value
= image
->texture
->height0
;
1314 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1315 if (image
->dri_components
== 0)
1317 *value
= image
->dri_components
;
1319 case __DRI_IMAGE_ATTRIB_FOURCC
:
1320 *value
= convert_to_fourcc(image
->dri_format
);
1321 return *value
!= -1;
1322 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1325 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1326 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1327 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1328 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1329 NULL
, image
->texture
, &whandle
, usage
))
1331 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1333 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1335 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1336 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1337 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1338 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1339 NULL
, image
->texture
, &whandle
, usage
))
1341 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1343 *value
= whandle
.modifier
& 0xffffffff;
1351 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1355 img
= CALLOC_STRUCT(__DRIimageRec
);
1359 img
->texture
= NULL
;
1360 pipe_resource_reference(&img
->texture
, image
->texture
);
1361 img
->level
= image
->level
;
1362 img
->layer
= image
->layer
;
1363 img
->dri_format
= image
->dri_format
;
1364 /* This should be 0 for sub images, but dup is also used for base images. */
1365 img
->dri_components
= image
->dri_components
;
1366 img
->loader_private
= loaderPrivate
;
1372 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1374 if (!image
|| !image
->texture
)
1377 struct pipe_screen
*screen
= image
->texture
->screen
;
1378 if (!screen
->check_resource_capability
)
1381 /* We don't want to check these:
1382 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1383 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1386 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1387 bind
|= PIPE_BIND_SCANOUT
;
1388 if (use
& __DRI_IMAGE_USE_LINEAR
)
1389 bind
|= PIPE_BIND_LINEAR
;
1390 if (use
& __DRI_IMAGE_USE_CURSOR
)
1391 bind
|= PIPE_BIND_CURSOR
;
1396 return screen
->check_resource_capability(screen
, image
->texture
, bind
);
1400 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1401 int *names
, int num_names
, int *strides
, int *offsets
,
1402 void *loaderPrivate
)
1406 struct winsys_handle whandle
;
1411 format
= convert_fourcc(format
, &dri_components
);
1415 memset(&whandle
, 0, sizeof(whandle
));
1416 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1417 whandle
.handle
= names
[0];
1418 whandle
.stride
= strides
[0];
1419 whandle
.offset
= offsets
[0];
1420 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1422 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1423 1, &whandle
, loaderPrivate
);
1427 img
->dri_components
= dri_components
;
1432 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1439 if (image
->dri_components
== 0)
1442 img
= dri2_dup_image(image
, loaderPrivate
);
1446 if (img
->texture
->screen
->resource_changed
)
1447 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1450 /* set this to 0 for sub images. */
1451 img
->dri_components
= 0;
1456 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1457 int *fds
, int num_fds
, int *strides
, int *offsets
,
1458 void *loaderPrivate
)
1463 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1464 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1465 strides
, offsets
, NULL
,
1466 &dri_components
, loaderPrivate
);
1470 img
->dri_components
= dri_components
;
1475 dri2_query_dma_buf_formats(__DRIscreen
*_screen
, int max
, int *formats
,
1478 struct dri_screen
*screen
= dri_screen(_screen
);
1479 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1480 const unsigned bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1483 for (i
= 0, j
= 0; (i
< ARRAY_SIZE(fourcc_formats
)) &&
1484 (j
< max
|| max
== 0); i
++) {
1485 if (pscreen
->is_format_supported(pscreen
,
1486 fourcc_to_pipe_format(
1491 formats
[j
] = fourcc_formats
[i
];
1500 dri2_query_dma_buf_modifiers(__DRIscreen
*_screen
, int fourcc
, int max
,
1501 uint64_t *modifiers
, unsigned int *external_only
,
1504 struct dri_screen
*screen
= dri_screen(_screen
);
1505 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1506 enum pipe_format format
= fourcc_to_pipe_format(fourcc
);
1507 const unsigned usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1509 if (pscreen
->query_dmabuf_modifiers
!= NULL
&&
1510 pscreen
->is_format_supported(pscreen
, format
, screen
->target
, 0, usage
)) {
1511 pscreen
->query_dmabuf_modifiers(pscreen
, format
, max
, modifiers
,
1512 external_only
, count
);
1519 dri2_from_dma_bufs(__DRIscreen
*screen
,
1520 int width
, int height
, int fourcc
,
1521 int *fds
, int num_fds
,
1522 int *strides
, int *offsets
,
1523 enum __DRIYUVColorSpace yuv_color_space
,
1524 enum __DRISampleRange sample_range
,
1525 enum __DRIChromaSiting horizontal_siting
,
1526 enum __DRIChromaSiting vertical_siting
,
1528 void *loaderPrivate
)
1533 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1534 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1535 strides
, offsets
, error
,
1536 &dri_components
, loaderPrivate
);
1540 img
->yuv_color_space
= yuv_color_space
;
1541 img
->sample_range
= sample_range
;
1542 img
->horizontal_siting
= horizontal_siting
;
1543 img
->vertical_siting
= vertical_siting
;
1544 img
->dri_components
= dri_components
;
1546 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1551 dri2_from_dma_bufs2(__DRIscreen
*screen
,
1552 int width
, int height
, int fourcc
,
1553 uint64_t modifier
, int *fds
, int num_fds
,
1554 int *strides
, int *offsets
,
1555 enum __DRIYUVColorSpace yuv_color_space
,
1556 enum __DRISampleRange sample_range
,
1557 enum __DRIChromaSiting horizontal_siting
,
1558 enum __DRIChromaSiting vertical_siting
,
1560 void *loaderPrivate
)
1565 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1566 modifier
, fds
, num_fds
, strides
, offsets
,
1567 error
, &dri_components
, loaderPrivate
);
1571 img
->yuv_color_space
= yuv_color_space
;
1572 img
->sample_range
= sample_range
;
1573 img
->horizontal_siting
= horizontal_siting
;
1574 img
->vertical_siting
= vertical_siting
;
1575 img
->dri_components
= dri_components
;
1577 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1582 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1583 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1584 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1587 struct dri_context
*ctx
= dri_context(context
);
1588 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1589 struct pipe_screen
*screen
;
1590 struct pipe_fence_handle
*fence
;
1591 struct pipe_blit_info blit
;
1596 memset(&blit
, 0, sizeof(blit
));
1597 blit
.dst
.resource
= dst
->texture
;
1598 blit
.dst
.box
.x
= dstx0
;
1599 blit
.dst
.box
.y
= dsty0
;
1600 blit
.dst
.box
.width
= dstwidth
;
1601 blit
.dst
.box
.height
= dstheight
;
1602 blit
.dst
.box
.depth
= 1;
1603 blit
.dst
.format
= dst
->texture
->format
;
1604 blit
.src
.resource
= src
->texture
;
1605 blit
.src
.box
.x
= srcx0
;
1606 blit
.src
.box
.y
= srcy0
;
1607 blit
.src
.box
.width
= srcwidth
;
1608 blit
.src
.box
.height
= srcheight
;
1609 blit
.src
.box
.depth
= 1;
1610 blit
.src
.format
= src
->texture
->format
;
1611 blit
.mask
= PIPE_MASK_RGBA
;
1612 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1614 pipe
->blit(pipe
, &blit
);
1616 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1617 pipe
->flush_resource(pipe
, dst
->texture
);
1618 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1619 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1620 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1621 pipe
->flush_resource(pipe
, dst
->texture
);
1622 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1623 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1624 screen
->fence_reference(screen
, &fence
, NULL
);
1629 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1630 int x0
, int y0
, int width
, int height
,
1631 unsigned int flags
, int *stride
, void **data
)
1633 struct dri_context
*ctx
= dri_context(context
);
1634 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1635 enum pipe_transfer_usage pipe_access
= 0;
1636 struct pipe_transfer
*trans
;
1639 if (!image
|| !data
|| *data
)
1642 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1643 pipe_access
|= PIPE_TRANSFER_READ
;
1644 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1645 pipe_access
|= PIPE_TRANSFER_WRITE
;
1647 map
= pipe_transfer_map(pipe
, image
->texture
,
1648 0, 0, pipe_access
, x0
, y0
, width
, height
,
1652 *stride
= trans
->stride
;
1659 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1661 struct dri_context
*ctx
= dri_context(context
);
1662 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1664 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1668 dri2_get_capabilities(__DRIscreen
*_screen
)
1670 struct dri_screen
*screen
= dri_screen(_screen
);
1672 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1675 /* The extension is modified during runtime if DRI_PRIME is detected */
1676 static __DRIimageExtension dri2ImageExtension
= {
1677 .base
= { __DRI_IMAGE
, 17 },
1679 .createImageFromName
= dri2_create_image_from_name
,
1680 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1681 .destroyImage
= dri2_destroy_image
,
1682 .createImage
= dri2_create_image
,
1683 .queryImage
= dri2_query_image
,
1684 .dupImage
= dri2_dup_image
,
1685 .validateUsage
= dri2_validate_usage
,
1686 .createImageFromNames
= dri2_from_names
,
1687 .fromPlanar
= dri2_from_planar
,
1688 .createImageFromTexture
= dri2_create_from_texture
,
1689 .createImageFromFds
= NULL
,
1690 .createImageFromDmaBufs
= NULL
,
1691 .blitImage
= dri2_blit_image
,
1692 .getCapabilities
= dri2_get_capabilities
,
1693 .mapImage
= dri2_map_image
,
1694 .unmapImage
= dri2_unmap_image
,
1695 .createImageWithModifiers
= NULL
,
1696 .createImageFromDmaBufs2
= NULL
,
1697 .queryDmaBufFormats
= NULL
,
1698 .queryDmaBufModifiers
= NULL
,
1699 .queryDmaBufFormatModifierAttribs
= NULL
,
1700 .createImageFromRenderbuffer2
= dri2_create_image_from_renderbuffer2
,
1703 static const __DRIrobustnessExtension dri2Robustness
= {
1704 .base
= { __DRI2_ROBUSTNESS
, 1 }
1708 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1709 struct mesa_glinterop_device_info
*out
)
1711 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1713 /* There is no version 0, thus we do not support it */
1714 if (out
->version
== 0)
1715 return MESA_GLINTEROP_INVALID_VERSION
;
1717 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1718 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1719 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1720 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1722 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1723 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1725 /* Instruct the caller that we support up-to version one of the interface */
1728 return MESA_GLINTEROP_SUCCESS
;
1732 dri2_interop_export_object(__DRIcontext
*_ctx
,
1733 struct mesa_glinterop_export_in
*in
,
1734 struct mesa_glinterop_export_out
*out
)
1736 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1737 struct pipe_screen
*screen
= st
->pipe
->screen
;
1738 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1739 struct pipe_resource
*res
= NULL
;
1740 struct winsys_handle whandle
;
1741 unsigned target
, usage
;
1744 /* There is no version 0, thus we do not support it */
1745 if (in
->version
== 0 || out
->version
== 0)
1746 return MESA_GLINTEROP_INVALID_VERSION
;
1748 /* Validate the target. */
1749 switch (in
->target
) {
1750 case GL_TEXTURE_BUFFER
:
1754 case GL_TEXTURE_RECTANGLE
:
1755 case GL_TEXTURE_1D_ARRAY
:
1756 case GL_TEXTURE_2D_ARRAY
:
1757 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1758 case GL_TEXTURE_CUBE_MAP
:
1759 case GL_TEXTURE_2D_MULTISAMPLE
:
1760 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1761 case GL_TEXTURE_EXTERNAL_OES
:
1762 case GL_RENDERBUFFER
:
1763 case GL_ARRAY_BUFFER
:
1764 target
= in
->target
;
1766 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1767 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1768 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1769 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1770 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1771 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1772 target
= GL_TEXTURE_CUBE_MAP
;
1775 return MESA_GLINTEROP_INVALID_TARGET
;
1778 /* Validate the simple case of miplevel. */
1779 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1781 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1783 /* Validate the OpenGL object and get pipe_resource. */
1784 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1786 if (target
== GL_ARRAY_BUFFER
) {
1789 * The error checking is based on the documentation of
1790 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1792 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1794 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1795 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1796 * a GL buffer object but does not have an existing data store or
1797 * the size of the buffer is 0."
1799 if (!buf
|| buf
->Size
== 0) {
1800 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1801 return MESA_GLINTEROP_INVALID_OBJECT
;
1804 res
= st_buffer_object(buf
)->buffer
;
1806 /* this shouldn't happen */
1807 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1808 return MESA_GLINTEROP_INVALID_OBJECT
;
1811 out
->buf_offset
= 0;
1812 out
->buf_size
= buf
->Size
;
1814 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1815 } else if (target
== GL_RENDERBUFFER
) {
1818 * The error checking is based on the documentation of
1819 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1821 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1823 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1824 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1825 * object or if the width or height of renderbuffer is zero."
1827 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1828 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1829 return MESA_GLINTEROP_INVALID_OBJECT
;
1832 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1833 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1834 * renderbuffer object."
1836 if (rb
->NumSamples
> 1) {
1837 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1838 return MESA_GLINTEROP_INVALID_OPERATION
;
1841 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1842 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1843 * required by the OpenCL implementation on the device."
1845 res
= st_renderbuffer(rb
)->texture
;
1847 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1848 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1851 out
->internal_format
= rb
->InternalFormat
;
1852 out
->view_minlevel
= 0;
1853 out
->view_numlevels
= 1;
1854 out
->view_minlayer
= 0;
1855 out
->view_numlayers
= 1;
1859 * The error checking is based on the documentation of
1860 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1862 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1865 _mesa_test_texobj_completeness(ctx
, obj
);
1867 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1868 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1869 * type matches texture_target, if the specified miplevel of texture
1870 * is not defined, or if the width or height of the specified
1871 * miplevel is zero or if the GL texture object is incomplete."
1874 obj
->Target
!= target
||
1875 !obj
->_BaseComplete
||
1876 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1877 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1878 return MESA_GLINTEROP_INVALID_OBJECT
;
1881 if (target
== GL_TEXTURE_BUFFER
) {
1882 struct st_buffer_object
*stBuf
=
1883 st_buffer_object(obj
->BufferObject
);
1885 if (!stBuf
|| !stBuf
->buffer
) {
1886 /* this shouldn't happen */
1887 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1888 return MESA_GLINTEROP_INVALID_OBJECT
;
1890 res
= stBuf
->buffer
;
1892 out
->internal_format
= obj
->BufferObjectFormat
;
1893 out
->buf_offset
= obj
->BufferOffset
;
1894 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1897 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1899 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1900 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1901 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1902 * implementations); or greater than the value of q (for both OpenGL
1903 * and OpenGL ES). levelbase and q are defined for the texture in
1904 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1905 * specification and section 3.7.10 of the OpenGL ES 2.0."
1907 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1908 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1909 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1912 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1913 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1914 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1917 res
= st_get_texobj_resource(obj
);
1919 /* Incomplete texture buffer object? This shouldn't really occur. */
1920 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1921 return MESA_GLINTEROP_INVALID_OBJECT
;
1924 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1925 out
->view_minlevel
= obj
->MinLevel
;
1926 out
->view_numlevels
= obj
->NumLevels
;
1927 out
->view_minlayer
= obj
->MinLayer
;
1928 out
->view_numlayers
= obj
->NumLayers
;
1932 /* Get the handle. */
1933 switch (in
->access
) {
1934 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1935 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1937 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1938 usage
= PIPE_HANDLE_USAGE_READ
;
1940 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1941 usage
= PIPE_HANDLE_USAGE_WRITE
;
1947 memset(&whandle
, 0, sizeof(whandle
));
1948 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
1950 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1952 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1955 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1957 out
->dmabuf_fd
= whandle
.handle
;
1958 out
->out_driver_data_written
= 0;
1960 if (res
->target
== PIPE_BUFFER
)
1961 out
->buf_offset
+= whandle
.offset
;
1963 /* Instruct the caller that we support up-to version one of the interface */
1967 return MESA_GLINTEROP_SUCCESS
;
1970 static const __DRI2interopExtension dri2InteropExtension
= {
1971 .base
= { __DRI2_INTEROP
, 1 },
1972 .query_device_info
= dri2_interop_query_device_info
,
1973 .export_object
= dri2_interop_export_object
1977 * \brief the DRI2ConfigQueryExtension configQueryb method
1980 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1983 struct dri_screen
*screen
= dri_screen(sPriv
);
1985 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_BOOL
))
1986 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1988 *val
= driQueryOptionb(&screen
->dev
->option_cache
, var
);
1994 * \brief the DRI2ConfigQueryExtension configQueryi method
1997 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1999 struct dri_screen
*screen
= dri_screen(sPriv
);
2001 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_INT
) &&
2002 !driCheckOption(&screen
->dev
->option_cache
, var
, DRI_ENUM
))
2003 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
2005 *val
= driQueryOptioni(&screen
->dev
->option_cache
, var
);
2011 * \brief the DRI2ConfigQueryExtension configQueryf method
2014 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
2016 struct dri_screen
*screen
= dri_screen(sPriv
);
2018 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_FLOAT
))
2019 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
2021 *val
= driQueryOptionf(&screen
->dev
->option_cache
, var
);
2027 * \brief the DRI2ConfigQueryExtension struct.
2029 * We first query the driver option cache. Then the dri2 option cache.
2031 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
2032 .base
= { __DRI2_CONFIG_QUERY
, 1 },
2034 .configQueryb
= dri2GalliumConfigQueryb
,
2035 .configQueryi
= dri2GalliumConfigQueryi
,
2036 .configQueryf
= dri2GalliumConfigQueryf
,
2040 * Backend function init_screen.
2043 static const __DRIextension
*dri_screen_extensions
[] = {
2044 &driTexBufferExtension
.base
,
2045 &dri2FlushExtension
.base
,
2046 &dri2ImageExtension
.base
,
2047 &dri2RendererQueryExtension
.base
,
2048 &dri2GalliumConfigQueryExtension
.base
,
2049 &dri2ThrottleExtension
.base
,
2050 &dri2FenceExtension
.base
,
2051 &dri2InteropExtension
.base
,
2052 &dri2NoErrorExtension
.base
,
2056 static const __DRIextension
*dri_robust_screen_extensions
[] = {
2057 &driTexBufferExtension
.base
,
2058 &dri2FlushExtension
.base
,
2059 &dri2ImageExtension
.base
,
2060 &dri2RendererQueryExtension
.base
,
2061 &dri2GalliumConfigQueryExtension
.base
,
2062 &dri2ThrottleExtension
.base
,
2063 &dri2FenceExtension
.base
,
2064 &dri2InteropExtension
.base
,
2065 &dri2Robustness
.base
,
2066 &dri2NoErrorExtension
.base
,
2071 * This is the driver specific part of the createNewScreen entry point.
2073 * Returns the struct gl_config supported by this driver.
2075 static const __DRIconfig
**
2076 dri2_init_screen(__DRIscreen
* sPriv
)
2078 const __DRIconfig
**configs
;
2079 struct dri_screen
*screen
;
2080 struct pipe_screen
*pscreen
= NULL
;
2081 const struct drm_conf_ret
*throttle_ret
;
2082 const struct drm_conf_ret
*dmabuf_ret
;
2085 screen
= CALLOC_STRUCT(dri_screen
);
2089 screen
->sPriv
= sPriv
;
2090 screen
->fd
= sPriv
->fd
;
2091 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
2093 sPriv
->driverPrivate
= (void *)screen
;
2095 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2099 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
)) {
2100 dri_init_options(screen
);
2102 pscreen
= pipe_loader_create_screen(screen
->dev
);
2108 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
2109 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
2111 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
2112 screen
->throttling_enabled
= TRUE
;
2113 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
2116 if (pscreen
->resource_create_with_modifiers
)
2117 dri2ImageExtension
.createImageWithModifiers
=
2118 dri2_create_image_with_modifiers
;
2120 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
2123 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2124 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2125 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2126 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2127 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2128 if (pscreen
->query_dmabuf_modifiers
) {
2129 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2130 dri2ImageExtension
.queryDmaBufModifiers
=
2131 dri2_query_dma_buf_modifiers
;
2136 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
2137 sPriv
->extensions
= dri_robust_screen_extensions
;
2138 screen
->has_reset_status_query
= true;
2141 sPriv
->extensions
= dri_screen_extensions
;
2143 configs
= dri_init_screen_helper(screen
, pscreen
);
2145 goto destroy_screen
;
2147 screen
->can_share_buffer
= true;
2148 screen
->auto_fake_front
= dri_with_format(sPriv
);
2149 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2150 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2155 dri_destroy_screen_helper(screen
);
2159 pipe_loader_release(&screen
->dev
, 1);
2169 * This is the driver specific part of the createNewScreen entry point.
2171 * Returns the struct gl_config supported by this driver.
2173 static const __DRIconfig
**
2174 dri_kms_init_screen(__DRIscreen
* sPriv
)
2176 #if defined(GALLIUM_SOFTPIPE)
2177 const __DRIconfig
**configs
;
2178 struct dri_screen
*screen
;
2179 struct pipe_screen
*pscreen
= NULL
;
2183 screen
= CALLOC_STRUCT(dri_screen
);
2187 screen
->sPriv
= sPriv
;
2188 screen
->fd
= sPriv
->fd
;
2190 sPriv
->driverPrivate
= (void *)screen
;
2192 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2195 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
)) {
2196 dri_init_options(screen
);
2197 pscreen
= pipe_loader_create_screen(screen
->dev
);
2203 if (pscreen
->resource_create_with_modifiers
)
2204 dri2ImageExtension
.createImageWithModifiers
=
2205 dri2_create_image_with_modifiers
;
2207 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2208 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2209 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2210 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2211 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2212 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2213 dri2ImageExtension
.queryDmaBufModifiers
= dri2_query_dma_buf_modifiers
;
2216 sPriv
->extensions
= dri_screen_extensions
;
2218 configs
= dri_init_screen_helper(screen
, pscreen
);
2220 goto destroy_screen
;
2222 screen
->can_share_buffer
= false;
2223 screen
->auto_fake_front
= dri_with_format(sPriv
);
2224 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2225 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2230 dri_destroy_screen_helper(screen
);
2234 pipe_loader_release(&screen
->dev
, 1);
2240 #endif // GALLIUM_SOFTPIPE
2245 dri2_create_buffer(__DRIscreen
* sPriv
,
2246 __DRIdrawable
* dPriv
,
2247 const struct gl_config
* visual
, boolean isPixmap
)
2249 struct dri_drawable
*drawable
= NULL
;
2251 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2254 drawable
= dPriv
->driverPrivate
;
2256 drawable
->allocate_textures
= dri2_allocate_textures
;
2257 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2258 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2259 drawable
->flush_swapbuffers
= dri2_flush_swapbuffers
;
2265 * DRI driver virtual function table.
2267 * DRI versions differ in their implementation of init_screen and swap_buffers.
2269 const struct __DriverAPIRec galliumdrm_driver_api
= {
2270 .InitScreen
= dri2_init_screen
,
2271 .DestroyScreen
= dri_destroy_screen
,
2272 .CreateContext
= dri_create_context
,
2273 .DestroyContext
= dri_destroy_context
,
2274 .CreateBuffer
= dri2_create_buffer
,
2275 .DestroyBuffer
= dri_destroy_buffer
,
2276 .MakeCurrent
= dri_make_current
,
2277 .UnbindContext
= dri_unbind_context
,
2279 .AllocateBuffer
= dri2_allocate_buffer
,
2280 .ReleaseBuffer
= dri2_release_buffer
,
2284 * DRI driver virtual function table.
2286 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2287 * hook. The latter is used to explicitly initialise the kms_swrast driver
2288 * rather than selecting the approapriate driver as suggested by the loader.
2290 const struct __DriverAPIRec dri_kms_driver_api
= {
2291 .InitScreen
= dri_kms_init_screen
,
2292 .DestroyScreen
= dri_destroy_screen
,
2293 .CreateContext
= dri_create_context
,
2294 .DestroyContext
= dri_destroy_context
,
2295 .CreateBuffer
= dri2_create_buffer
,
2296 .DestroyBuffer
= dri_destroy_buffer
,
2297 .MakeCurrent
= dri_make_current
,
2298 .UnbindContext
= dri_unbind_context
,
2300 .AllocateBuffer
= dri2_allocate_buffer
,
2301 .ReleaseBuffer
= dri2_release_buffer
,
2304 /* This is the table of extensions that the loader will dlsym() for. */
2305 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2306 &driCoreExtension
.base
,
2307 &driImageDriverExtension
.base
,
2308 &driDRI2Extension
.base
,
2309 &gallium_config_options
.base
,
2313 /* vim: set sw=3 ts=8 sts=3 expandtab: */