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>
32 #include "GL/mesa_glinterop.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "util/u_format.h"
36 #include "util/u_debug.h"
37 #include "state_tracker/drm_driver.h"
38 #include "state_tracker/st_cb_bufferobjects.h"
39 #include "state_tracker/st_cb_fbo.h"
40 #include "state_tracker/st_cb_texture.h"
41 #include "state_tracker/st_texture.h"
42 #include "state_tracker/st_context.h"
43 #include "pipe-loader/pipe_loader.h"
44 #include "main/bufferobj.h"
45 #include "main/texobj.h"
49 #include "dri_helpers.h"
50 #include "dri_drawable.h"
51 #include "dri_query_renderer.h"
53 #ifndef DRM_FORMAT_MOD_INVALID
54 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
60 struct pipe_resource
*resource
;
63 static inline struct dri2_buffer
*
64 dri2_buffer(__DRIbuffer
* driBufferPriv
)
66 return (struct dri2_buffer
*) driBufferPriv
;
69 static const int fourcc_formats
[] = {
70 __DRI_IMAGE_FOURCC_ARGB2101010
,
71 __DRI_IMAGE_FOURCC_XRGB2101010
,
72 __DRI_IMAGE_FOURCC_ABGR2101010
,
73 __DRI_IMAGE_FOURCC_XBGR2101010
,
74 __DRI_IMAGE_FOURCC_ARGB8888
,
75 __DRI_IMAGE_FOURCC_ABGR8888
,
76 __DRI_IMAGE_FOURCC_SARGB8888
,
77 __DRI_IMAGE_FOURCC_XRGB8888
,
78 __DRI_IMAGE_FOURCC_XBGR8888
,
79 __DRI_IMAGE_FOURCC_ARGB1555
,
80 __DRI_IMAGE_FOURCC_RGB565
,
81 __DRI_IMAGE_FOURCC_R8
,
82 __DRI_IMAGE_FOURCC_R16
,
83 __DRI_IMAGE_FOURCC_GR88
,
84 __DRI_IMAGE_FOURCC_GR1616
,
85 __DRI_IMAGE_FOURCC_YUV410
,
86 __DRI_IMAGE_FOURCC_YUV411
,
87 __DRI_IMAGE_FOURCC_YUV420
,
88 __DRI_IMAGE_FOURCC_YUV422
,
89 __DRI_IMAGE_FOURCC_YUV444
,
90 __DRI_IMAGE_FOURCC_YVU410
,
91 __DRI_IMAGE_FOURCC_YVU411
,
92 __DRI_IMAGE_FOURCC_YVU420
,
93 __DRI_IMAGE_FOURCC_YVU422
,
94 __DRI_IMAGE_FOURCC_YVU444
,
95 __DRI_IMAGE_FOURCC_NV12
,
96 __DRI_IMAGE_FOURCC_NV16
,
97 __DRI_IMAGE_FOURCC_YUYV
100 static int convert_fourcc(int format
, int *dri_components_p
)
104 case __DRI_IMAGE_FOURCC_ARGB1555
:
105 format
= __DRI_IMAGE_FORMAT_ARGB1555
;
106 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
108 case __DRI_IMAGE_FOURCC_RGB565
:
109 format
= __DRI_IMAGE_FORMAT_RGB565
;
110 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
112 case __DRI_IMAGE_FOURCC_ARGB8888
:
113 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
114 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
116 case __DRI_IMAGE_FOURCC_XRGB8888
:
117 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
118 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
120 case __DRI_IMAGE_FOURCC_ABGR8888
:
121 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
122 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
124 case __DRI_IMAGE_FOURCC_XBGR8888
:
125 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
126 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
128 case __DRI_IMAGE_FOURCC_ARGB2101010
:
129 format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
130 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
132 case __DRI_IMAGE_FOURCC_XRGB2101010
:
133 format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
134 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
136 case __DRI_IMAGE_FOURCC_ABGR2101010
:
137 format
= __DRI_IMAGE_FORMAT_ABGR2101010
;
138 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
140 case __DRI_IMAGE_FOURCC_XBGR2101010
:
141 format
= __DRI_IMAGE_FORMAT_XBGR2101010
;
142 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
144 case __DRI_IMAGE_FOURCC_R8
:
145 format
= __DRI_IMAGE_FORMAT_R8
;
146 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
148 case __DRI_IMAGE_FOURCC_GR88
:
149 format
= __DRI_IMAGE_FORMAT_GR88
;
150 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
152 case __DRI_IMAGE_FOURCC_R16
:
153 format
= __DRI_IMAGE_FORMAT_R16
;
154 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
156 case __DRI_IMAGE_FOURCC_GR1616
:
157 format
= __DRI_IMAGE_FORMAT_GR1616
;
158 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
160 case __DRI_IMAGE_FOURCC_YUYV
:
161 format
= __DRI_IMAGE_FORMAT_YUYV
;
162 dri_components
= __DRI_IMAGE_COMPONENTS_Y_XUXV
;
165 * For multi-planar YUV formats, we return the format of the first
166 * plane only. Since there is only one caller which supports multi-
167 * planar YUV it gets to figure out the remaining planes on it's
170 case __DRI_IMAGE_FOURCC_YUV420
:
171 case __DRI_IMAGE_FOURCC_YVU420
:
172 format
= __DRI_IMAGE_FORMAT_R8
;
173 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
175 case __DRI_IMAGE_FOURCC_NV12
:
176 format
= __DRI_IMAGE_FORMAT_R8
;
177 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
182 *dri_components_p
= dri_components
;
186 /* NOTE this probably isn't going to do the right thing for YUV images
187 * (but I think the same can be said for intel_query_image()). I think
188 * only needed for exporting dmabuf's, so I think I won't loose much
191 static int convert_to_fourcc(int format
)
194 case __DRI_IMAGE_FORMAT_ARGB1555
:
195 format
= __DRI_IMAGE_FOURCC_ARGB1555
;
197 case __DRI_IMAGE_FORMAT_RGB565
:
198 format
= __DRI_IMAGE_FOURCC_RGB565
;
200 case __DRI_IMAGE_FORMAT_ARGB8888
:
201 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
203 case __DRI_IMAGE_FORMAT_XRGB8888
:
204 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
206 case __DRI_IMAGE_FORMAT_ABGR8888
:
207 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
209 case __DRI_IMAGE_FORMAT_XBGR8888
:
210 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
212 case __DRI_IMAGE_FORMAT_ARGB2101010
:
213 format
= __DRI_IMAGE_FOURCC_ARGB2101010
;
215 case __DRI_IMAGE_FORMAT_XRGB2101010
:
216 format
= __DRI_IMAGE_FOURCC_XRGB2101010
;
218 case __DRI_IMAGE_FORMAT_ABGR2101010
:
219 format
= __DRI_IMAGE_FOURCC_ABGR2101010
;
221 case __DRI_IMAGE_FORMAT_XBGR2101010
:
222 format
= __DRI_IMAGE_FOURCC_XBGR2101010
;
224 case __DRI_IMAGE_FORMAT_R8
:
225 format
= __DRI_IMAGE_FOURCC_R8
;
227 case __DRI_IMAGE_FORMAT_GR88
:
228 format
= __DRI_IMAGE_FOURCC_GR88
;
236 static enum pipe_format
dri2_format_to_pipe_format (int format
)
241 case __DRI_IMAGE_FORMAT_ARGB1555
:
242 pf
= PIPE_FORMAT_B5G5R5A1_UNORM
;
244 case __DRI_IMAGE_FORMAT_RGB565
:
245 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
247 case __DRI_IMAGE_FORMAT_XRGB8888
:
248 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
250 case __DRI_IMAGE_FORMAT_ARGB8888
:
251 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
253 case __DRI_IMAGE_FORMAT_XBGR8888
:
254 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
256 case __DRI_IMAGE_FORMAT_ABGR8888
:
257 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
259 case __DRI_IMAGE_FORMAT_XRGB2101010
:
260 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
262 case __DRI_IMAGE_FORMAT_ARGB2101010
:
263 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
265 case __DRI_IMAGE_FORMAT_XBGR2101010
:
266 pf
= PIPE_FORMAT_R10G10B10X2_UNORM
;
268 case __DRI_IMAGE_FORMAT_ABGR2101010
:
269 pf
= PIPE_FORMAT_R10G10B10A2_UNORM
;
271 case __DRI_IMAGE_FORMAT_R8
:
272 pf
= PIPE_FORMAT_R8_UNORM
;
274 case __DRI_IMAGE_FORMAT_GR88
:
275 pf
= PIPE_FORMAT_RG88_UNORM
;
277 case __DRI_IMAGE_FORMAT_R16
:
278 pf
= PIPE_FORMAT_R16_UNORM
;
280 case __DRI_IMAGE_FORMAT_GR1616
:
281 pf
= PIPE_FORMAT_R16G16_UNORM
;
283 case __DRI_IMAGE_FORMAT_YUYV
:
284 pf
= PIPE_FORMAT_YUYV
;
287 pf
= PIPE_FORMAT_NONE
;
294 static enum pipe_format
fourcc_to_pipe_format(int fourcc
)
299 case __DRI_IMAGE_FOURCC_R8
:
300 pf
= PIPE_FORMAT_R8_UNORM
;
302 case __DRI_IMAGE_FOURCC_GR88
:
303 pf
= PIPE_FORMAT_RG88_UNORM
;
305 case __DRI_IMAGE_FOURCC_ARGB1555
:
306 pf
= PIPE_FORMAT_B5G5R5A1_UNORM
;
308 case __DRI_IMAGE_FOURCC_R16
:
309 pf
= PIPE_FORMAT_R16_UNORM
;
311 case __DRI_IMAGE_FOURCC_GR1616
:
312 pf
= PIPE_FORMAT_RG1616_UNORM
;
314 case __DRI_IMAGE_FOURCC_RGB565
:
315 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
317 case __DRI_IMAGE_FOURCC_ARGB8888
:
318 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
320 case __DRI_IMAGE_FOURCC_XRGB8888
:
321 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
323 case __DRI_IMAGE_FOURCC_ABGR8888
:
324 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
326 case __DRI_IMAGE_FOURCC_XBGR8888
:
327 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
329 case __DRI_IMAGE_FOURCC_ARGB2101010
:
330 pf
= PIPE_FORMAT_B10G10R10A2_UNORM
;
332 case __DRI_IMAGE_FOURCC_XRGB2101010
:
333 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
335 case __DRI_IMAGE_FOURCC_ABGR2101010
:
336 pf
= PIPE_FORMAT_R10G10B10A2_UNORM
;
338 case __DRI_IMAGE_FOURCC_XBGR2101010
:
339 pf
= PIPE_FORMAT_R10G10B10X2_UNORM
;
342 case __DRI_IMAGE_FOURCC_NV12
:
343 pf
= PIPE_FORMAT_NV12
;
345 case __DRI_IMAGE_FOURCC_YUYV
:
346 pf
= PIPE_FORMAT_YUYV
;
348 case __DRI_IMAGE_FOURCC_YUV420
:
349 case __DRI_IMAGE_FOURCC_YVU420
:
350 pf
= PIPE_FORMAT_YV12
;
353 case __DRI_IMAGE_FOURCC_SARGB8888
:
354 case __DRI_IMAGE_FOURCC_YUV410
:
355 case __DRI_IMAGE_FOURCC_YUV411
:
356 case __DRI_IMAGE_FOURCC_YUV422
:
357 case __DRI_IMAGE_FOURCC_YUV444
:
358 case __DRI_IMAGE_FOURCC_NV16
:
359 case __DRI_IMAGE_FOURCC_YVU410
:
360 case __DRI_IMAGE_FOURCC_YVU411
:
361 case __DRI_IMAGE_FOURCC_YVU422
:
362 case __DRI_IMAGE_FOURCC_YVU444
:
364 pf
= PIPE_FORMAT_NONE
;
371 * DRI2 flush extension.
374 dri2_flush_drawable(__DRIdrawable
*dPriv
)
376 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
380 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
382 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
384 dri2InvalidateDrawable(dPriv
);
385 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
386 drawable
->texture_mask
= 0;
388 p_atomic_inc(&drawable
->base
.stamp
);
391 static const __DRI2flushExtension dri2FlushExtension
= {
392 .base
= { __DRI2_FLUSH
, 4 },
394 .flush
= dri2_flush_drawable
,
395 .invalidate
= dri2_invalidate_drawable
,
396 .flush_with_flags
= dri_flush
,
400 * Retrieve __DRIbuffer from the DRI loader.
403 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
404 const enum st_attachment_type
*atts
,
407 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
408 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
410 __DRIbuffer
*buffers
;
412 unsigned attachments
[10];
413 unsigned num_attachments
, i
;
416 with_format
= dri_with_format(drawable
->sPriv
);
420 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
422 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
424 for (i
= 0; i
< *count
; i
++) {
425 enum pipe_format format
;
429 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
430 if (format
== PIPE_FORMAT_NONE
)
434 case ST_ATTACHMENT_FRONT_LEFT
:
438 att
= __DRI_BUFFER_FRONT_LEFT
;
440 case ST_ATTACHMENT_BACK_LEFT
:
441 att
= __DRI_BUFFER_BACK_LEFT
;
443 case ST_ATTACHMENT_FRONT_RIGHT
:
444 att
= __DRI_BUFFER_FRONT_RIGHT
;
446 case ST_ATTACHMENT_BACK_RIGHT
:
447 att
= __DRI_BUFFER_BACK_RIGHT
;
454 * In this switch statement we must support all formats that
455 * may occur as the stvis->color_format.
458 case PIPE_FORMAT_B10G10R10A2_UNORM
:
459 case PIPE_FORMAT_R10G10B10A2_UNORM
:
460 case PIPE_FORMAT_BGRA8888_UNORM
:
461 case PIPE_FORMAT_RGBA8888_UNORM
:
464 case PIPE_FORMAT_R10G10B10X2_UNORM
:
465 case PIPE_FORMAT_B10G10R10X2_UNORM
:
468 case PIPE_FORMAT_BGRX8888_UNORM
:
469 case PIPE_FORMAT_RGBX8888_UNORM
:
472 case PIPE_FORMAT_B5G6R5_UNORM
:
476 depth
= util_format_get_blocksizebits(format
);
477 assert(!"Unexpected format in dri2_drawable_get_buffers()");
480 attachments
[num_attachments
++] = att
;
482 attachments
[num_attachments
++] = depth
;
487 num_attachments
/= 2;
488 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
489 &dri_drawable
->w
, &dri_drawable
->h
,
490 attachments
, num_attachments
,
491 &num_buffers
, dri_drawable
->loaderPrivate
);
494 buffers
= loader
->getBuffers(dri_drawable
,
495 &dri_drawable
->w
, &dri_drawable
->h
,
496 attachments
, num_attachments
,
497 &num_buffers
, dri_drawable
->loaderPrivate
);
501 *count
= num_buffers
;
507 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
508 struct __DRIimageList
*images
,
509 const enum st_attachment_type
*statts
,
510 unsigned statts_count
)
512 __DRIdrawable
*dPriv
= drawable
->dPriv
;
513 __DRIscreen
*sPriv
= drawable
->sPriv
;
514 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
516 uint32_t buffer_mask
= 0;
519 for (i
= 0; i
< statts_count
; i
++) {
520 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
521 if (pf
== PIPE_FORMAT_NONE
)
525 case ST_ATTACHMENT_FRONT_LEFT
:
526 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
528 case ST_ATTACHMENT_BACK_LEFT
:
529 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
536 case PIPE_FORMAT_B5G5R5A1_UNORM
:
537 image_format
= __DRI_IMAGE_FORMAT_ARGB1555
;
539 case PIPE_FORMAT_B5G6R5_UNORM
:
540 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
542 case PIPE_FORMAT_BGRX8888_UNORM
:
543 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
545 case PIPE_FORMAT_BGRA8888_UNORM
:
546 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
548 case PIPE_FORMAT_RGBX8888_UNORM
:
549 image_format
= __DRI_IMAGE_FORMAT_XBGR8888
;
551 case PIPE_FORMAT_RGBA8888_UNORM
:
552 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
554 case PIPE_FORMAT_B10G10R10X2_UNORM
:
555 image_format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
557 case PIPE_FORMAT_B10G10R10A2_UNORM
:
558 image_format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
560 case PIPE_FORMAT_R10G10B10X2_UNORM
:
561 image_format
= __DRI_IMAGE_FORMAT_XBGR2101010
;
563 case PIPE_FORMAT_R10G10B10A2_UNORM
:
564 image_format
= __DRI_IMAGE_FORMAT_ABGR2101010
;
567 image_format
= __DRI_IMAGE_FORMAT_NONE
;
572 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
573 (uint32_t *) &drawable
->base
.stamp
,
574 dPriv
->loaderPrivate
, buffer_mask
,
579 dri2_allocate_buffer(__DRIscreen
*sPriv
,
580 unsigned attachment
, unsigned format
,
581 int width
, int height
)
583 struct dri_screen
*screen
= dri_screen(sPriv
);
584 struct dri2_buffer
*buffer
;
585 struct pipe_resource templ
;
588 struct winsys_handle whandle
;
590 switch (attachment
) {
591 case __DRI_BUFFER_FRONT_LEFT
:
592 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
593 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
595 case __DRI_BUFFER_BACK_LEFT
:
596 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
598 case __DRI_BUFFER_DEPTH
:
599 case __DRI_BUFFER_DEPTH_STENCIL
:
600 case __DRI_BUFFER_STENCIL
:
601 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
605 /* because we get the handle and stride */
606 bind
|= PIPE_BIND_SHARED
;
610 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
613 pf
= PIPE_FORMAT_B10G10R10X2_UNORM
;
616 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
619 pf
= PIPE_FORMAT_Z16_UNORM
;
625 buffer
= CALLOC_STRUCT(dri2_buffer
);
629 memset(&templ
, 0, sizeof(templ
));
632 templ
.target
= PIPE_TEXTURE_2D
;
633 templ
.last_level
= 0;
634 templ
.width0
= width
;
635 templ
.height0
= height
;
637 templ
.array_size
= 1;
640 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
641 if (!buffer
->resource
) {
646 memset(&whandle
, 0, sizeof(whandle
));
647 if (screen
->can_share_buffer
)
648 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
650 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
652 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
653 buffer
->resource
, &whandle
,
654 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
656 buffer
->base
.attachment
= attachment
;
657 buffer
->base
.name
= whandle
.handle
;
658 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
659 buffer
->base
.pitch
= whandle
.stride
;
661 return &buffer
->base
;
665 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
667 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
669 pipe_resource_reference(&buffer
->resource
, NULL
);
674 * Backend functions for st_framebuffer interface.
678 dri2_allocate_textures(struct dri_context
*ctx
,
679 struct dri_drawable
*drawable
,
680 const enum st_attachment_type
*statts
,
681 unsigned statts_count
)
683 __DRIscreen
*sPriv
= drawable
->sPriv
;
684 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
685 struct dri_screen
*screen
= dri_screen(sPriv
);
686 struct pipe_resource templ
;
687 boolean alloc_depthstencil
= FALSE
;
689 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
690 /* Image specific variables */
691 struct __DRIimageList images
;
692 /* Dri2 specific variables */
693 __DRIbuffer
*buffers
= NULL
;
694 struct winsys_handle whandle
;
695 unsigned num_buffers
= statts_count
;
697 /* First get the buffers from the loader */
699 if (!dri_image_drawable_get_buffers(drawable
, &images
,
700 statts
, statts_count
))
704 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
705 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
706 drawable
->old_w
== dri_drawable
->w
&&
707 drawable
->old_h
== dri_drawable
->h
&&
708 memcmp(drawable
->old
, buffers
,
709 sizeof(__DRIbuffer
) * num_buffers
) == 0))
713 /* Second clean useless resources*/
715 /* See if we need a depth-stencil buffer. */
716 for (i
= 0; i
< statts_count
; i
++) {
717 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
718 alloc_depthstencil
= TRUE
;
723 /* Delete the resources we won't need. */
724 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
725 /* Don't delete the depth-stencil buffer, we can reuse it. */
726 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
729 /* Flush the texture before unreferencing, so that other clients can
730 * see what the driver has rendered.
732 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
733 struct pipe_context
*pipe
= ctx
->st
->pipe
;
734 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
737 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
740 if (drawable
->stvis
.samples
> 1) {
741 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
744 /* Don't delete MSAA resources for the attachments which are enabled,
745 * we can reuse them. */
746 for (j
= 0; j
< statts_count
; j
++) {
747 if (i
== statts
[j
]) {
754 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
759 /* Third use the buffers retrieved to fill the drawable info */
761 memset(&templ
, 0, sizeof(templ
));
762 templ
.target
= screen
->target
;
763 templ
.last_level
= 0;
765 templ
.array_size
= 1;
768 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
769 struct pipe_resource
**buf
=
770 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
771 struct pipe_resource
*texture
= images
.front
->texture
;
773 dri_drawable
->w
= texture
->width0
;
774 dri_drawable
->h
= texture
->height0
;
776 pipe_resource_reference(buf
, texture
);
779 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
780 struct pipe_resource
**buf
=
781 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
782 struct pipe_resource
*texture
= images
.back
->texture
;
784 dri_drawable
->w
= texture
->width0
;
785 dri_drawable
->h
= texture
->height0
;
787 pipe_resource_reference(buf
, texture
);
790 /* Note: if there is both a back and a front buffer,
791 * then they have the same size.
793 templ
.width0
= dri_drawable
->w
;
794 templ
.height0
= dri_drawable
->h
;
797 memset(&whandle
, 0, sizeof(whandle
));
799 /* Process DRI-provided buffers and get pipe_resources. */
800 for (i
= 0; i
< num_buffers
; i
++) {
801 __DRIbuffer
*buf
= &buffers
[i
];
802 enum st_attachment_type statt
;
803 enum pipe_format format
;
805 switch (buf
->attachment
) {
806 case __DRI_BUFFER_FRONT_LEFT
:
807 if (!screen
->auto_fake_front
) {
808 continue; /* invalid attachment */
811 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
812 statt
= ST_ATTACHMENT_FRONT_LEFT
;
814 case __DRI_BUFFER_BACK_LEFT
:
815 statt
= ST_ATTACHMENT_BACK_LEFT
;
818 continue; /* invalid attachment */
821 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
822 if (format
== PIPE_FORMAT_NONE
)
825 /* dri2_drawable_get_buffers has already filled dri_drawable->w
826 * and dri_drawable->h */
827 templ
.width0
= dri_drawable
->w
;
828 templ
.height0
= dri_drawable
->h
;
829 templ
.format
= format
;
831 whandle
.handle
= buf
->name
;
832 whandle
.stride
= buf
->pitch
;
834 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
835 if (screen
->can_share_buffer
)
836 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
838 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
839 drawable
->textures
[statt
] =
840 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
842 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
843 assert(drawable
->textures
[statt
]);
847 /* Allocate private MSAA colorbuffers. */
848 if (drawable
->stvis
.samples
> 1) {
849 for (i
= 0; i
< statts_count
; i
++) {
850 enum st_attachment_type statt
= statts
[i
];
852 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
855 if (drawable
->textures
[statt
]) {
856 templ
.format
= drawable
->textures
[statt
]->format
;
857 templ
.bind
= drawable
->textures
[statt
]->bind
&
858 ~(PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
859 templ
.nr_samples
= drawable
->stvis
.samples
;
860 templ
.nr_storage_samples
= drawable
->stvis
.samples
;
862 /* Try to reuse the resource.
863 * (the other resource parameters should be constant)
865 if (!drawable
->msaa_textures
[statt
] ||
866 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
867 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
868 /* Allocate a new one. */
869 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
871 drawable
->msaa_textures
[statt
] =
872 screen
->base
.screen
->resource_create(screen
->base
.screen
,
874 assert(drawable
->msaa_textures
[statt
]);
876 /* If there are any MSAA resources, we should initialize them
877 * such that they contain the same data as the single-sample
878 * resources we just got from the X server.
880 * The reason for this is that the state tracker (and
881 * therefore the app) can access the MSAA resources only.
882 * The single-sample resources are not exposed
883 * to the state tracker.
886 dri_pipe_blit(ctx
->st
->pipe
,
887 drawable
->msaa_textures
[statt
],
888 drawable
->textures
[statt
]);
892 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
897 /* Allocate a private depth-stencil buffer. */
898 if (alloc_depthstencil
) {
899 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
900 struct pipe_resource
**zsbuf
;
901 enum pipe_format format
;
904 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
907 templ
.format
= format
;
908 templ
.bind
= bind
& ~PIPE_BIND_SHARED
;
910 if (drawable
->stvis
.samples
> 1) {
911 templ
.nr_samples
= drawable
->stvis
.samples
;
912 templ
.nr_storage_samples
= drawable
->stvis
.samples
;
913 zsbuf
= &drawable
->msaa_textures
[statt
];
916 templ
.nr_samples
= 0;
917 templ
.nr_storage_samples
= 0;
918 zsbuf
= &drawable
->textures
[statt
];
921 /* Try to reuse the resource.
922 * (the other resource parameters should be constant)
925 (*zsbuf
)->width0
!= templ
.width0
||
926 (*zsbuf
)->height0
!= templ
.height0
) {
927 /* Allocate a new one. */
928 pipe_resource_reference(zsbuf
, NULL
);
929 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
935 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
936 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
940 /* For DRI2, we may get the same buffers again from the server.
941 * To prevent useless imports of gem names, drawable->old* is used
942 * to bypass the import if we get the same buffers. This doesn't apply
943 * to DRI3/Wayland, users of image.loader, since the buffer is managed
944 * by the client (no import), and the back buffer is going to change
948 drawable
->old_num
= num_buffers
;
949 drawable
->old_w
= dri_drawable
->w
;
950 drawable
->old_h
= dri_drawable
->h
;
951 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
956 dri2_flush_frontbuffer(struct dri_context
*ctx
,
957 struct dri_drawable
*drawable
,
958 enum st_attachment_type statt
)
960 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
961 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
962 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
963 struct pipe_context
*pipe
= ctx
->st
->pipe
;
965 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
968 if (drawable
->stvis
.samples
> 1) {
969 /* Resolve the front buffer. */
970 dri_pipe_blit(ctx
->st
->pipe
,
971 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
972 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
975 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
976 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
979 pipe
->flush(pipe
, NULL
, 0);
982 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
984 else if (loader
->flushFrontBuffer
) {
985 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
990 * The struct dri_drawable flush_swapbuffers callback
993 dri2_flush_swapbuffers(struct dri_context
*ctx
,
994 struct dri_drawable
*drawable
)
996 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
997 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
999 if (image
&& image
->base
.version
>= 3 && image
->flushSwapBuffers
) {
1000 image
->flushSwapBuffers(dri_drawable
, dri_drawable
->loaderPrivate
);
1005 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
1006 struct dri_context
*ctx
,
1007 struct pipe_resource
*res
)
1013 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
1014 int width
, int height
, int format
,
1015 int num_handles
, struct winsys_handle
*whandle
,
1016 void *loaderPrivate
)
1018 struct dri_screen
*screen
= dri_screen(_screen
);
1019 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1021 struct pipe_resource templ
;
1023 enum pipe_format pf
;
1026 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1028 pf
= dri2_format_to_pipe_format (format
);
1029 if (pf
== PIPE_FORMAT_NONE
)
1032 img
= CALLOC_STRUCT(__DRIimageRec
);
1036 memset(&templ
, 0, sizeof(templ
));
1037 templ
.bind
= tex_usage
;
1038 templ
.target
= screen
->target
;
1039 templ
.last_level
= 0;
1041 templ
.array_size
= 1;
1043 for (i
= num_handles
- 1; i
>= 0; i
--) {
1044 struct pipe_resource
*tex
;
1046 /* TODO: something a lot less ugly */
1049 templ
.width0
= width
;
1050 templ
.height0
= height
;
1054 templ
.width0
= width
/ 2;
1055 templ
.height0
= height
/ 2;
1056 templ
.format
= (num_handles
== 2) ?
1057 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
1058 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
1061 templ
.width0
= width
/ 2;
1062 templ
.height0
= height
/ 2;
1063 templ
.format
= PIPE_FORMAT_R8_UNORM
;
1066 unreachable("too many planes!");
1069 tex
= pscreen
->resource_from_handle(pscreen
,
1070 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
1072 pipe_resource_reference(&img
->texture
, NULL
);
1077 tex
->next
= img
->texture
;
1083 img
->dri_format
= format
;
1085 img
->loader_private
= loaderPrivate
;
1091 dri2_create_image_from_name(__DRIscreen
*_screen
,
1092 int width
, int height
, int format
,
1093 int name
, int pitch
, void *loaderPrivate
)
1095 struct winsys_handle whandle
;
1096 enum pipe_format pf
;
1098 memset(&whandle
, 0, sizeof(whandle
));
1099 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1100 whandle
.handle
= name
;
1101 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1103 pf
= dri2_format_to_pipe_format (format
);
1104 if (pf
== PIPE_FORMAT_NONE
)
1107 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
1109 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1110 1, &whandle
, loaderPrivate
);
1114 dri2_create_image_from_fd(__DRIscreen
*_screen
,
1115 int width
, int height
, int fourcc
,
1116 uint64_t modifier
, int *fds
, int num_fds
,
1117 int *strides
, int *offsets
, unsigned *error
,
1118 int *dri_components
, void *loaderPrivate
)
1120 struct winsys_handle whandles
[3];
1122 __DRIimage
*img
= NULL
;
1123 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
1124 int expected_num_fds
, i
;
1127 case __DRI_IMAGE_FOURCC_YUV420
:
1128 case __DRI_IMAGE_FOURCC_YVU420
:
1129 expected_num_fds
= 3;
1131 case __DRI_IMAGE_FOURCC_NV12
:
1132 expected_num_fds
= 2;
1135 expected_num_fds
= 1;
1139 if (num_fds
!= expected_num_fds
) {
1140 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1144 format
= convert_fourcc(fourcc
, dri_components
);
1146 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1150 memset(whandles
, 0, sizeof(whandles
));
1152 for (i
= 0; i
< num_fds
; i
++) {
1154 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1158 whandles
[i
].type
= WINSYS_HANDLE_TYPE_FD
;
1159 whandles
[i
].handle
= (unsigned)fds
[i
];
1160 whandles
[i
].stride
= (unsigned)strides
[i
];
1161 whandles
[i
].offset
= (unsigned)offsets
[i
];
1162 whandles
[i
].modifier
= modifier
;
1165 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
1166 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1167 struct winsys_handle tmp
= whandles
[1];
1168 whandles
[1] = whandles
[2];
1170 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
1173 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1174 num_fds
, whandles
, loaderPrivate
);
1176 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1186 dri2_create_image_common(__DRIscreen
*_screen
,
1187 int width
, int height
,
1188 int format
, unsigned int use
,
1189 const uint64_t *modifiers
,
1190 const unsigned count
,
1191 void *loaderPrivate
)
1193 struct dri_screen
*screen
= dri_screen(_screen
);
1195 struct pipe_resource templ
;
1197 enum pipe_format pf
;
1199 /* createImageWithModifiers doesn't supply usage, and we should not get
1200 * here with both modifiers and a usage flag.
1202 assert(!(use
&& (modifiers
!= NULL
)));
1204 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1206 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1207 tex_usage
|= PIPE_BIND_SCANOUT
;
1208 if (use
& __DRI_IMAGE_USE_SHARE
)
1209 tex_usage
|= PIPE_BIND_SHARED
;
1210 if (use
& __DRI_IMAGE_USE_LINEAR
)
1211 tex_usage
|= PIPE_BIND_LINEAR
;
1212 if (use
& __DRI_IMAGE_USE_CURSOR
) {
1213 if (width
!= 64 || height
!= 64)
1215 tex_usage
|= PIPE_BIND_CURSOR
;
1218 pf
= dri2_format_to_pipe_format (format
);
1219 if (pf
== PIPE_FORMAT_NONE
)
1222 img
= CALLOC_STRUCT(__DRIimageRec
);
1226 memset(&templ
, 0, sizeof(templ
));
1227 templ
.bind
= tex_usage
;
1229 templ
.target
= PIPE_TEXTURE_2D
;
1230 templ
.last_level
= 0;
1231 templ
.width0
= width
;
1232 templ
.height0
= height
;
1234 templ
.array_size
= 1;
1239 ->resource_create_with_modifiers(screen
->base
.screen
,
1245 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1246 if (!img
->texture
) {
1253 img
->dri_format
= format
;
1254 img
->dri_components
= 0;
1257 img
->loader_private
= loaderPrivate
;
1262 dri2_create_image(__DRIscreen
*_screen
,
1263 int width
, int height
, int format
,
1264 unsigned int use
, void *loaderPrivate
)
1266 return dri2_create_image_common(_screen
, width
, height
, format
, use
,
1267 NULL
/* modifiers */, 0 /* count */,
1272 dri2_create_image_with_modifiers(__DRIscreen
*dri_screen
,
1273 int width
, int height
, int format
,
1274 const uint64_t *modifiers
,
1275 const unsigned count
,
1276 void *loaderPrivate
)
1278 return dri2_create_image_common(dri_screen
, width
, height
, format
,
1279 0 /* use */, modifiers
, count
,
1284 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1286 struct winsys_handle whandle
;
1289 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1290 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1292 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1294 memset(&whandle
, 0, sizeof(whandle
));
1297 case __DRI_IMAGE_ATTRIB_STRIDE
:
1298 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1299 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1300 NULL
, image
->texture
, &whandle
, usage
))
1302 *value
= whandle
.stride
;
1304 case __DRI_IMAGE_ATTRIB_OFFSET
:
1305 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1306 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1307 NULL
, image
->texture
, &whandle
, usage
))
1309 *value
= whandle
.offset
;
1311 case __DRI_IMAGE_ATTRIB_HANDLE
:
1312 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1313 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1314 NULL
, image
->texture
, &whandle
, usage
))
1316 *value
= whandle
.handle
;
1318 case __DRI_IMAGE_ATTRIB_NAME
:
1319 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1320 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1321 NULL
, image
->texture
, &whandle
, usage
))
1323 *value
= whandle
.handle
;
1325 case __DRI_IMAGE_ATTRIB_FD
:
1326 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
1327 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1328 NULL
, image
->texture
, &whandle
, usage
))
1331 *value
= whandle
.handle
;
1333 case __DRI_IMAGE_ATTRIB_FORMAT
:
1334 *value
= image
->dri_format
;
1336 case __DRI_IMAGE_ATTRIB_WIDTH
:
1337 *value
= image
->texture
->width0
;
1339 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1340 *value
= image
->texture
->height0
;
1342 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1343 if (image
->dri_components
== 0)
1345 *value
= image
->dri_components
;
1347 case __DRI_IMAGE_ATTRIB_FOURCC
:
1348 *value
= convert_to_fourcc(image
->dri_format
);
1349 return *value
!= -1;
1350 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1353 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1354 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1355 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1356 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1357 NULL
, image
->texture
, &whandle
, usage
))
1359 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1361 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1363 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1364 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
1365 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1366 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1367 NULL
, image
->texture
, &whandle
, usage
))
1369 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1371 *value
= whandle
.modifier
& 0xffffffff;
1379 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1383 img
= CALLOC_STRUCT(__DRIimageRec
);
1387 img
->texture
= NULL
;
1388 pipe_resource_reference(&img
->texture
, image
->texture
);
1389 img
->level
= image
->level
;
1390 img
->layer
= image
->layer
;
1391 img
->dri_format
= image
->dri_format
;
1392 /* This should be 0 for sub images, but dup is also used for base images. */
1393 img
->dri_components
= image
->dri_components
;
1394 img
->loader_private
= loaderPrivate
;
1400 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1402 if (!image
|| !image
->texture
)
1405 struct pipe_screen
*screen
= image
->texture
->screen
;
1406 if (!screen
->check_resource_capability
)
1409 /* We don't want to check these:
1410 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1411 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1414 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1415 bind
|= PIPE_BIND_SCANOUT
;
1416 if (use
& __DRI_IMAGE_USE_LINEAR
)
1417 bind
|= PIPE_BIND_LINEAR
;
1418 if (use
& __DRI_IMAGE_USE_CURSOR
)
1419 bind
|= PIPE_BIND_CURSOR
;
1424 return screen
->check_resource_capability(screen
, image
->texture
, bind
);
1428 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1429 int *names
, int num_names
, int *strides
, int *offsets
,
1430 void *loaderPrivate
)
1434 struct winsys_handle whandle
;
1439 format
= convert_fourcc(format
, &dri_components
);
1443 memset(&whandle
, 0, sizeof(whandle
));
1444 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
1445 whandle
.handle
= names
[0];
1446 whandle
.stride
= strides
[0];
1447 whandle
.offset
= offsets
[0];
1448 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1450 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1451 1, &whandle
, loaderPrivate
);
1455 img
->dri_components
= dri_components
;
1460 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1467 if (image
->dri_components
== 0)
1470 img
= dri2_dup_image(image
, loaderPrivate
);
1474 if (img
->texture
->screen
->resource_changed
)
1475 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1478 /* set this to 0 for sub images. */
1479 img
->dri_components
= 0;
1484 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1485 int *fds
, int num_fds
, int *strides
, int *offsets
,
1486 void *loaderPrivate
)
1491 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1492 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1493 strides
, offsets
, NULL
,
1494 &dri_components
, loaderPrivate
);
1498 img
->dri_components
= dri_components
;
1503 dri2_query_dma_buf_formats(__DRIscreen
*_screen
, int max
, int *formats
,
1506 struct dri_screen
*screen
= dri_screen(_screen
);
1507 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1508 const unsigned bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1511 for (i
= 0, j
= 0; (i
< ARRAY_SIZE(fourcc_formats
)) &&
1512 (j
< max
|| max
== 0); i
++) {
1513 /* The sRGB format is not a real FourCC as defined by drm_fourcc.h, so we
1514 * must not leak it out to clients.
1516 if (fourcc_formats
[i
] == __DRI_IMAGE_FOURCC_SARGB8888
)
1519 if (pscreen
->is_format_supported(pscreen
,
1520 fourcc_to_pipe_format(
1525 formats
[j
] = fourcc_formats
[i
];
1534 dri2_query_dma_buf_modifiers(__DRIscreen
*_screen
, int fourcc
, int max
,
1535 uint64_t *modifiers
, unsigned int *external_only
,
1538 struct dri_screen
*screen
= dri_screen(_screen
);
1539 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1540 enum pipe_format format
= fourcc_to_pipe_format(fourcc
);
1541 const unsigned usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1543 if (pscreen
->query_dmabuf_modifiers
!= NULL
&&
1544 pscreen
->is_format_supported(pscreen
, format
, screen
->target
, 0, 0,
1546 pscreen
->query_dmabuf_modifiers(pscreen
, format
, max
, modifiers
,
1547 external_only
, count
);
1554 dri2_from_dma_bufs(__DRIscreen
*screen
,
1555 int width
, int height
, int fourcc
,
1556 int *fds
, int num_fds
,
1557 int *strides
, int *offsets
,
1558 enum __DRIYUVColorSpace yuv_color_space
,
1559 enum __DRISampleRange sample_range
,
1560 enum __DRIChromaSiting horizontal_siting
,
1561 enum __DRIChromaSiting vertical_siting
,
1563 void *loaderPrivate
)
1568 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1569 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1570 strides
, offsets
, error
,
1571 &dri_components
, loaderPrivate
);
1575 img
->yuv_color_space
= yuv_color_space
;
1576 img
->sample_range
= sample_range
;
1577 img
->horizontal_siting
= horizontal_siting
;
1578 img
->vertical_siting
= vertical_siting
;
1579 img
->dri_components
= dri_components
;
1581 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1586 dri2_from_dma_bufs2(__DRIscreen
*screen
,
1587 int width
, int height
, int fourcc
,
1588 uint64_t modifier
, int *fds
, int num_fds
,
1589 int *strides
, int *offsets
,
1590 enum __DRIYUVColorSpace yuv_color_space
,
1591 enum __DRISampleRange sample_range
,
1592 enum __DRIChromaSiting horizontal_siting
,
1593 enum __DRIChromaSiting vertical_siting
,
1595 void *loaderPrivate
)
1600 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1601 modifier
, fds
, num_fds
, strides
, offsets
,
1602 error
, &dri_components
, loaderPrivate
);
1606 img
->yuv_color_space
= yuv_color_space
;
1607 img
->sample_range
= sample_range
;
1608 img
->horizontal_siting
= horizontal_siting
;
1609 img
->vertical_siting
= vertical_siting
;
1610 img
->dri_components
= dri_components
;
1612 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1617 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1618 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1619 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1622 struct dri_context
*ctx
= dri_context(context
);
1623 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1624 struct pipe_screen
*screen
;
1625 struct pipe_fence_handle
*fence
;
1626 struct pipe_blit_info blit
;
1631 memset(&blit
, 0, sizeof(blit
));
1632 blit
.dst
.resource
= dst
->texture
;
1633 blit
.dst
.box
.x
= dstx0
;
1634 blit
.dst
.box
.y
= dsty0
;
1635 blit
.dst
.box
.width
= dstwidth
;
1636 blit
.dst
.box
.height
= dstheight
;
1637 blit
.dst
.box
.depth
= 1;
1638 blit
.dst
.format
= dst
->texture
->format
;
1639 blit
.src
.resource
= src
->texture
;
1640 blit
.src
.box
.x
= srcx0
;
1641 blit
.src
.box
.y
= srcy0
;
1642 blit
.src
.box
.width
= srcwidth
;
1643 blit
.src
.box
.height
= srcheight
;
1644 blit
.src
.box
.depth
= 1;
1645 blit
.src
.format
= src
->texture
->format
;
1646 blit
.mask
= PIPE_MASK_RGBA
;
1647 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1649 pipe
->blit(pipe
, &blit
);
1651 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1652 pipe
->flush_resource(pipe
, dst
->texture
);
1653 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1654 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1655 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1656 pipe
->flush_resource(pipe
, dst
->texture
);
1657 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1658 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1659 screen
->fence_reference(screen
, &fence
, NULL
);
1664 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1665 int x0
, int y0
, int width
, int height
,
1666 unsigned int flags
, int *stride
, void **data
)
1668 struct dri_context
*ctx
= dri_context(context
);
1669 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1670 enum pipe_transfer_usage pipe_access
= 0;
1671 struct pipe_transfer
*trans
;
1674 if (!image
|| !data
|| *data
)
1677 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1678 pipe_access
|= PIPE_TRANSFER_READ
;
1679 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1680 pipe_access
|= PIPE_TRANSFER_WRITE
;
1682 map
= pipe_transfer_map(pipe
, image
->texture
,
1683 0, 0, pipe_access
, x0
, y0
, width
, height
,
1687 *stride
= trans
->stride
;
1694 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1696 struct dri_context
*ctx
= dri_context(context
);
1697 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1699 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1703 dri2_get_capabilities(__DRIscreen
*_screen
)
1705 struct dri_screen
*screen
= dri_screen(_screen
);
1707 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1710 /* The extension is modified during runtime if DRI_PRIME is detected */
1711 static __DRIimageExtension dri2ImageExtension
= {
1712 .base
= { __DRI_IMAGE
, 17 },
1714 .createImageFromName
= dri2_create_image_from_name
,
1715 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1716 .destroyImage
= dri2_destroy_image
,
1717 .createImage
= dri2_create_image
,
1718 .queryImage
= dri2_query_image
,
1719 .dupImage
= dri2_dup_image
,
1720 .validateUsage
= dri2_validate_usage
,
1721 .createImageFromNames
= dri2_from_names
,
1722 .fromPlanar
= dri2_from_planar
,
1723 .createImageFromTexture
= dri2_create_from_texture
,
1724 .createImageFromFds
= NULL
,
1725 .createImageFromDmaBufs
= NULL
,
1726 .blitImage
= dri2_blit_image
,
1727 .getCapabilities
= dri2_get_capabilities
,
1728 .mapImage
= dri2_map_image
,
1729 .unmapImage
= dri2_unmap_image
,
1730 .createImageWithModifiers
= NULL
,
1731 .createImageFromDmaBufs2
= NULL
,
1732 .queryDmaBufFormats
= NULL
,
1733 .queryDmaBufModifiers
= NULL
,
1734 .queryDmaBufFormatModifierAttribs
= NULL
,
1735 .createImageFromRenderbuffer2
= dri2_create_image_from_renderbuffer2
,
1738 static const __DRIrobustnessExtension dri2Robustness
= {
1739 .base
= { __DRI2_ROBUSTNESS
, 1 }
1743 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1744 struct mesa_glinterop_device_info
*out
)
1746 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1748 /* There is no version 0, thus we do not support it */
1749 if (out
->version
== 0)
1750 return MESA_GLINTEROP_INVALID_VERSION
;
1752 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1753 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1754 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1755 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1757 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1758 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1760 /* Instruct the caller that we support up-to version one of the interface */
1763 return MESA_GLINTEROP_SUCCESS
;
1767 dri2_interop_export_object(__DRIcontext
*_ctx
,
1768 struct mesa_glinterop_export_in
*in
,
1769 struct mesa_glinterop_export_out
*out
)
1771 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1772 struct pipe_screen
*screen
= st
->pipe
->screen
;
1773 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1774 struct pipe_resource
*res
= NULL
;
1775 struct winsys_handle whandle
;
1776 unsigned target
, usage
;
1779 /* There is no version 0, thus we do not support it */
1780 if (in
->version
== 0 || out
->version
== 0)
1781 return MESA_GLINTEROP_INVALID_VERSION
;
1783 /* Validate the target. */
1784 switch (in
->target
) {
1785 case GL_TEXTURE_BUFFER
:
1789 case GL_TEXTURE_RECTANGLE
:
1790 case GL_TEXTURE_1D_ARRAY
:
1791 case GL_TEXTURE_2D_ARRAY
:
1792 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1793 case GL_TEXTURE_CUBE_MAP
:
1794 case GL_TEXTURE_2D_MULTISAMPLE
:
1795 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1796 case GL_TEXTURE_EXTERNAL_OES
:
1797 case GL_RENDERBUFFER
:
1798 case GL_ARRAY_BUFFER
:
1799 target
= in
->target
;
1801 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1802 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1803 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1805 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1806 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1807 target
= GL_TEXTURE_CUBE_MAP
;
1810 return MESA_GLINTEROP_INVALID_TARGET
;
1813 /* Validate the simple case of miplevel. */
1814 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1816 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1818 /* Validate the OpenGL object and get pipe_resource. */
1819 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1821 if (target
== GL_ARRAY_BUFFER
) {
1824 * The error checking is based on the documentation of
1825 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1827 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1829 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1830 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1831 * a GL buffer object but does not have an existing data store or
1832 * the size of the buffer is 0."
1834 if (!buf
|| buf
->Size
== 0) {
1835 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1836 return MESA_GLINTEROP_INVALID_OBJECT
;
1839 res
= st_buffer_object(buf
)->buffer
;
1841 /* this shouldn't happen */
1842 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1843 return MESA_GLINTEROP_INVALID_OBJECT
;
1846 out
->buf_offset
= 0;
1847 out
->buf_size
= buf
->Size
;
1849 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1850 } else if (target
== GL_RENDERBUFFER
) {
1853 * The error checking is based on the documentation of
1854 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1856 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1858 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1859 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1860 * object or if the width or height of renderbuffer is zero."
1862 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1863 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1864 return MESA_GLINTEROP_INVALID_OBJECT
;
1867 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1868 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1869 * renderbuffer object."
1871 if (rb
->NumSamples
> 1) {
1872 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1873 return MESA_GLINTEROP_INVALID_OPERATION
;
1876 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1877 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1878 * required by the OpenCL implementation on the device."
1880 res
= st_renderbuffer(rb
)->texture
;
1882 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1883 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1886 out
->internal_format
= rb
->InternalFormat
;
1887 out
->view_minlevel
= 0;
1888 out
->view_numlevels
= 1;
1889 out
->view_minlayer
= 0;
1890 out
->view_numlayers
= 1;
1894 * The error checking is based on the documentation of
1895 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1897 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1900 _mesa_test_texobj_completeness(ctx
, obj
);
1902 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1903 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1904 * type matches texture_target, if the specified miplevel of texture
1905 * is not defined, or if the width or height of the specified
1906 * miplevel is zero or if the GL texture object is incomplete."
1909 obj
->Target
!= target
||
1910 !obj
->_BaseComplete
||
1911 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1912 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1913 return MESA_GLINTEROP_INVALID_OBJECT
;
1916 if (target
== GL_TEXTURE_BUFFER
) {
1917 struct st_buffer_object
*stBuf
=
1918 st_buffer_object(obj
->BufferObject
);
1920 if (!stBuf
|| !stBuf
->buffer
) {
1921 /* this shouldn't happen */
1922 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1923 return MESA_GLINTEROP_INVALID_OBJECT
;
1925 res
= stBuf
->buffer
;
1927 out
->internal_format
= obj
->BufferObjectFormat
;
1928 out
->buf_offset
= obj
->BufferOffset
;
1929 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1932 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1934 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1935 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1936 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1937 * implementations); or greater than the value of q (for both OpenGL
1938 * and OpenGL ES). levelbase and q are defined for the texture in
1939 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1940 * specification and section 3.7.10 of the OpenGL ES 2.0."
1942 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1943 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1944 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1947 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1948 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1949 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1952 res
= st_get_texobj_resource(obj
);
1954 /* Incomplete texture buffer object? This shouldn't really occur. */
1955 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1956 return MESA_GLINTEROP_INVALID_OBJECT
;
1959 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1960 out
->view_minlevel
= obj
->MinLevel
;
1961 out
->view_numlevels
= obj
->NumLevels
;
1962 out
->view_minlayer
= obj
->MinLayer
;
1963 out
->view_numlayers
= obj
->NumLayers
;
1967 /* Get the handle. */
1968 switch (in
->access
) {
1969 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1970 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1972 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1973 usage
= PIPE_HANDLE_USAGE_READ
;
1975 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1976 usage
= PIPE_HANDLE_USAGE_WRITE
;
1982 memset(&whandle
, 0, sizeof(whandle
));
1983 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
1985 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1987 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1990 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1992 out
->dmabuf_fd
= whandle
.handle
;
1993 out
->out_driver_data_written
= 0;
1995 if (res
->target
== PIPE_BUFFER
)
1996 out
->buf_offset
+= whandle
.offset
;
1998 /* Instruct the caller that we support up-to version one of the interface */
2002 return MESA_GLINTEROP_SUCCESS
;
2005 static const __DRI2interopExtension dri2InteropExtension
= {
2006 .base
= { __DRI2_INTEROP
, 1 },
2007 .query_device_info
= dri2_interop_query_device_info
,
2008 .export_object
= dri2_interop_export_object
2012 * \brief the DRI2ConfigQueryExtension configQueryb method
2015 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
2018 struct dri_screen
*screen
= dri_screen(sPriv
);
2020 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_BOOL
))
2021 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
2023 *val
= driQueryOptionb(&screen
->dev
->option_cache
, var
);
2029 * \brief the DRI2ConfigQueryExtension configQueryi method
2032 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
2034 struct dri_screen
*screen
= dri_screen(sPriv
);
2036 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_INT
) &&
2037 !driCheckOption(&screen
->dev
->option_cache
, var
, DRI_ENUM
))
2038 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
2040 *val
= driQueryOptioni(&screen
->dev
->option_cache
, var
);
2046 * \brief the DRI2ConfigQueryExtension configQueryf method
2049 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
2051 struct dri_screen
*screen
= dri_screen(sPriv
);
2053 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_FLOAT
))
2054 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
2056 *val
= driQueryOptionf(&screen
->dev
->option_cache
, var
);
2062 * \brief the DRI2ConfigQueryExtension struct.
2064 * We first query the driver option cache. Then the dri2 option cache.
2066 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
2067 .base
= { __DRI2_CONFIG_QUERY
, 1 },
2069 .configQueryb
= dri2GalliumConfigQueryb
,
2070 .configQueryi
= dri2GalliumConfigQueryi
,
2071 .configQueryf
= dri2GalliumConfigQueryf
,
2075 * Backend function init_screen.
2078 static const __DRIextension
*dri_screen_extensions
[] = {
2079 &driTexBufferExtension
.base
,
2080 &dri2FlushExtension
.base
,
2081 &dri2ImageExtension
.base
,
2082 &dri2RendererQueryExtension
.base
,
2083 &dri2GalliumConfigQueryExtension
.base
,
2084 &dri2ThrottleExtension
.base
,
2085 &dri2FenceExtension
.base
,
2086 &dri2InteropExtension
.base
,
2087 &dri2NoErrorExtension
.base
,
2091 static const __DRIextension
*dri_robust_screen_extensions
[] = {
2092 &driTexBufferExtension
.base
,
2093 &dri2FlushExtension
.base
,
2094 &dri2ImageExtension
.base
,
2095 &dri2RendererQueryExtension
.base
,
2096 &dri2GalliumConfigQueryExtension
.base
,
2097 &dri2ThrottleExtension
.base
,
2098 &dri2FenceExtension
.base
,
2099 &dri2InteropExtension
.base
,
2100 &dri2Robustness
.base
,
2101 &dri2NoErrorExtension
.base
,
2106 * This is the driver specific part of the createNewScreen entry point.
2108 * Returns the struct gl_config supported by this driver.
2110 static const __DRIconfig
**
2111 dri2_init_screen(__DRIscreen
* sPriv
)
2113 const __DRIconfig
**configs
;
2114 struct dri_screen
*screen
;
2115 struct pipe_screen
*pscreen
= NULL
;
2116 const struct drm_conf_ret
*throttle_ret
;
2117 const struct drm_conf_ret
*dmabuf_ret
;
2119 screen
= CALLOC_STRUCT(dri_screen
);
2123 screen
->sPriv
= sPriv
;
2124 screen
->fd
= sPriv
->fd
;
2125 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
2127 sPriv
->driverPrivate
= (void *)screen
;
2129 if (pipe_loader_drm_probe_fd(&screen
->dev
, screen
->fd
)) {
2130 dri_init_options(screen
);
2132 pscreen
= pipe_loader_create_screen(screen
->dev
);
2138 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
2139 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
2141 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
2142 screen
->throttling_enabled
= TRUE
;
2143 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
2146 if (pscreen
->resource_create_with_modifiers
)
2147 dri2ImageExtension
.createImageWithModifiers
=
2148 dri2_create_image_with_modifiers
;
2150 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
2153 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2154 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2155 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2156 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2157 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2158 if (pscreen
->query_dmabuf_modifiers
) {
2159 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2160 dri2ImageExtension
.queryDmaBufModifiers
=
2161 dri2_query_dma_buf_modifiers
;
2166 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
2167 sPriv
->extensions
= dri_robust_screen_extensions
;
2168 screen
->has_reset_status_query
= true;
2171 sPriv
->extensions
= dri_screen_extensions
;
2173 configs
= dri_init_screen_helper(screen
, pscreen
);
2175 goto destroy_screen
;
2177 screen
->can_share_buffer
= true;
2178 screen
->auto_fake_front
= dri_with_format(sPriv
);
2179 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2180 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2185 dri_destroy_screen_helper(screen
);
2189 pipe_loader_release(&screen
->dev
, 1);
2196 * This is the driver specific part of the createNewScreen entry point.
2198 * Returns the struct gl_config supported by this driver.
2200 static const __DRIconfig
**
2201 dri_kms_init_screen(__DRIscreen
* sPriv
)
2203 #if defined(GALLIUM_SOFTPIPE)
2204 const __DRIconfig
**configs
;
2205 struct dri_screen
*screen
;
2206 struct pipe_screen
*pscreen
= NULL
;
2209 screen
= CALLOC_STRUCT(dri_screen
);
2213 screen
->sPriv
= sPriv
;
2214 screen
->fd
= sPriv
->fd
;
2216 sPriv
->driverPrivate
= (void *)screen
;
2218 if (pipe_loader_sw_probe_kms(&screen
->dev
, screen
->fd
)) {
2219 dri_init_options(screen
);
2220 pscreen
= pipe_loader_create_screen(screen
->dev
);
2226 if (pscreen
->resource_create_with_modifiers
)
2227 dri2ImageExtension
.createImageWithModifiers
=
2228 dri2_create_image_with_modifiers
;
2230 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2231 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2232 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2233 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2234 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2235 if (pscreen
->query_dmabuf_modifiers
) {
2236 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2237 dri2ImageExtension
.queryDmaBufModifiers
= dri2_query_dma_buf_modifiers
;
2241 sPriv
->extensions
= dri_screen_extensions
;
2243 configs
= dri_init_screen_helper(screen
, pscreen
);
2245 goto destroy_screen
;
2247 screen
->can_share_buffer
= false;
2248 screen
->auto_fake_front
= dri_with_format(sPriv
);
2249 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2250 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2255 dri_destroy_screen_helper(screen
);
2259 pipe_loader_release(&screen
->dev
, 1);
2262 #endif // GALLIUM_SOFTPIPE
2267 dri2_create_buffer(__DRIscreen
* sPriv
,
2268 __DRIdrawable
* dPriv
,
2269 const struct gl_config
* visual
, boolean isPixmap
)
2271 struct dri_drawable
*drawable
= NULL
;
2273 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2276 drawable
= dPriv
->driverPrivate
;
2278 drawable
->allocate_textures
= dri2_allocate_textures
;
2279 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2280 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2281 drawable
->flush_swapbuffers
= dri2_flush_swapbuffers
;
2287 * DRI driver virtual function table.
2289 * DRI versions differ in their implementation of init_screen and swap_buffers.
2291 const struct __DriverAPIRec galliumdrm_driver_api
= {
2292 .InitScreen
= dri2_init_screen
,
2293 .DestroyScreen
= dri_destroy_screen
,
2294 .CreateContext
= dri_create_context
,
2295 .DestroyContext
= dri_destroy_context
,
2296 .CreateBuffer
= dri2_create_buffer
,
2297 .DestroyBuffer
= dri_destroy_buffer
,
2298 .MakeCurrent
= dri_make_current
,
2299 .UnbindContext
= dri_unbind_context
,
2301 .AllocateBuffer
= dri2_allocate_buffer
,
2302 .ReleaseBuffer
= dri2_release_buffer
,
2306 * DRI driver virtual function table.
2308 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2309 * hook. The latter is used to explicitly initialise the kms_swrast driver
2310 * rather than selecting the approapriate driver as suggested by the loader.
2312 const struct __DriverAPIRec dri_kms_driver_api
= {
2313 .InitScreen
= dri_kms_init_screen
,
2314 .DestroyScreen
= dri_destroy_screen
,
2315 .CreateContext
= dri_create_context
,
2316 .DestroyContext
= dri_destroy_context
,
2317 .CreateBuffer
= dri2_create_buffer
,
2318 .DestroyBuffer
= dri_destroy_buffer
,
2319 .MakeCurrent
= dri_make_current
,
2320 .UnbindContext
= dri_unbind_context
,
2322 .AllocateBuffer
= dri2_allocate_buffer
,
2323 .ReleaseBuffer
= dri2_release_buffer
,
2326 /* This is the table of extensions that the loader will dlsym() for. */
2327 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2328 &driCoreExtension
.base
,
2329 &driImageDriverExtension
.base
,
2330 &driDRI2Extension
.base
,
2331 &gallium_config_options
.base
,
2335 /* vim: set sw=3 ts=8 sts=3 expandtab: */