2 * Mesa 3-D graphics library
4 * Copyright 2009, VMware, Inc.
6 * Copyright (C) 2010 LunarG Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
33 #include "GL/mesa_glinterop.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_debug.h"
38 #include "state_tracker/drm_driver.h"
39 #include "state_tracker/st_cb_bufferobjects.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_texture.h"
43 #include "state_tracker/st_context.h"
44 #include "pipe-loader/pipe_loader.h"
45 #include "main/bufferobj.h"
46 #include "main/texobj.h"
48 #include "dri_screen.h"
49 #include "dri_context.h"
50 #include "dri_drawable.h"
51 #include "dri_extensions.h"
52 #include "dri_query_renderer.h"
53 #include "dri2_buffer.h"
55 #ifndef DRM_FORMAT_MOD_INVALID
56 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
59 static const int fourcc_formats
[] = {
60 __DRI_IMAGE_FOURCC_ARGB8888
,
61 __DRI_IMAGE_FOURCC_ABGR8888
,
62 __DRI_IMAGE_FOURCC_SARGB8888
,
63 __DRI_IMAGE_FOURCC_XRGB8888
,
64 __DRI_IMAGE_FOURCC_XBGR8888
,
65 __DRI_IMAGE_FOURCC_ARGB1555
,
66 __DRI_IMAGE_FOURCC_RGB565
,
67 __DRI_IMAGE_FOURCC_R8
,
68 __DRI_IMAGE_FOURCC_R16
,
69 __DRI_IMAGE_FOURCC_GR88
,
70 __DRI_IMAGE_FOURCC_GR1616
,
71 __DRI_IMAGE_FOURCC_YUV410
,
72 __DRI_IMAGE_FOURCC_YUV411
,
73 __DRI_IMAGE_FOURCC_YUV420
,
74 __DRI_IMAGE_FOURCC_YUV422
,
75 __DRI_IMAGE_FOURCC_YUV444
,
76 __DRI_IMAGE_FOURCC_YVU410
,
77 __DRI_IMAGE_FOURCC_YVU411
,
78 __DRI_IMAGE_FOURCC_YVU420
,
79 __DRI_IMAGE_FOURCC_YVU422
,
80 __DRI_IMAGE_FOURCC_YVU444
,
81 __DRI_IMAGE_FOURCC_NV12
,
82 __DRI_IMAGE_FOURCC_NV16
,
83 __DRI_IMAGE_FOURCC_YUYV
86 static int convert_fourcc(int format
, int *dri_components_p
)
90 case __DRI_IMAGE_FOURCC_RGB565
:
91 format
= __DRI_IMAGE_FORMAT_RGB565
;
92 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
94 case __DRI_IMAGE_FOURCC_ARGB8888
:
95 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
96 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
98 case __DRI_IMAGE_FOURCC_XRGB8888
:
99 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
100 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
102 case __DRI_IMAGE_FOURCC_ABGR8888
:
103 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
104 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
106 case __DRI_IMAGE_FOURCC_XBGR8888
:
107 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
108 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
110 case __DRI_IMAGE_FOURCC_R8
:
111 format
= __DRI_IMAGE_FORMAT_R8
;
112 dri_components
= __DRI_IMAGE_COMPONENTS_R
;
114 case __DRI_IMAGE_FOURCC_GR88
:
115 format
= __DRI_IMAGE_FORMAT_GR88
;
116 dri_components
= __DRI_IMAGE_COMPONENTS_RG
;
119 * For multi-planar YUV formats, we return the format of the first
120 * plane only. Since there is only one caller which supports multi-
121 * planar YUV it gets to figure out the remaining planes on it's
124 case __DRI_IMAGE_FOURCC_YUV420
:
125 case __DRI_IMAGE_FOURCC_YVU420
:
126 format
= __DRI_IMAGE_FORMAT_R8
;
127 dri_components
= __DRI_IMAGE_COMPONENTS_Y_U_V
;
129 case __DRI_IMAGE_FOURCC_NV12
:
130 format
= __DRI_IMAGE_FORMAT_R8
;
131 dri_components
= __DRI_IMAGE_COMPONENTS_Y_UV
;
136 *dri_components_p
= dri_components
;
140 /* NOTE this probably isn't going to do the right thing for YUV images
141 * (but I think the same can be said for intel_query_image()). I think
142 * only needed for exporting dmabuf's, so I think I won't loose much
145 static int convert_to_fourcc(int format
)
148 case __DRI_IMAGE_FORMAT_RGB565
:
149 format
= __DRI_IMAGE_FOURCC_RGB565
;
151 case __DRI_IMAGE_FORMAT_ARGB8888
:
152 format
= __DRI_IMAGE_FOURCC_ARGB8888
;
154 case __DRI_IMAGE_FORMAT_XRGB8888
:
155 format
= __DRI_IMAGE_FOURCC_XRGB8888
;
157 case __DRI_IMAGE_FORMAT_ABGR8888
:
158 format
= __DRI_IMAGE_FOURCC_ABGR8888
;
160 case __DRI_IMAGE_FORMAT_XBGR8888
:
161 format
= __DRI_IMAGE_FOURCC_XBGR8888
;
163 case __DRI_IMAGE_FORMAT_R8
:
164 format
= __DRI_IMAGE_FOURCC_R8
;
166 case __DRI_IMAGE_FORMAT_GR88
:
167 format
= __DRI_IMAGE_FOURCC_GR88
;
175 static enum pipe_format
dri2_format_to_pipe_format (int format
)
180 case __DRI_IMAGE_FORMAT_RGB565
:
181 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
183 case __DRI_IMAGE_FORMAT_XRGB8888
:
184 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
186 case __DRI_IMAGE_FORMAT_ARGB8888
:
187 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
189 case __DRI_IMAGE_FORMAT_XBGR8888
:
190 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
192 case __DRI_IMAGE_FORMAT_ABGR8888
:
193 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
195 case __DRI_IMAGE_FORMAT_R8
:
196 pf
= PIPE_FORMAT_R8_UNORM
;
198 case __DRI_IMAGE_FORMAT_GR88
:
199 pf
= PIPE_FORMAT_RG88_UNORM
;
202 pf
= PIPE_FORMAT_NONE
;
209 static enum pipe_format
fourcc_to_pipe_format(int fourcc
)
214 case __DRI_IMAGE_FOURCC_R8
:
215 pf
= PIPE_FORMAT_R8_UNORM
;
217 case __DRI_IMAGE_FOURCC_GR88
:
218 pf
= PIPE_FORMAT_RG88_UNORM
;
220 case __DRI_IMAGE_FOURCC_ARGB1555
:
221 pf
= PIPE_FORMAT_B5G5R5A1_UNORM
;
223 case __DRI_IMAGE_FOURCC_R16
:
224 pf
= PIPE_FORMAT_R16_UNORM
;
226 case __DRI_IMAGE_FOURCC_GR1616
:
227 pf
= PIPE_FORMAT_RG1616_UNORM
;
229 case __DRI_IMAGE_FOURCC_RGB565
:
230 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
232 case __DRI_IMAGE_FOURCC_ARGB8888
:
233 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
235 case __DRI_IMAGE_FOURCC_XRGB8888
:
236 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
238 case __DRI_IMAGE_FOURCC_ABGR8888
:
239 pf
= PIPE_FORMAT_RGBA8888_UNORM
;
241 case __DRI_IMAGE_FOURCC_XBGR8888
:
242 pf
= PIPE_FORMAT_RGBX8888_UNORM
;
245 case __DRI_IMAGE_FOURCC_NV12
:
246 pf
= PIPE_FORMAT_NV12
;
248 case __DRI_IMAGE_FOURCC_YUYV
:
249 pf
= PIPE_FORMAT_YUYV
;
251 case __DRI_IMAGE_FOURCC_YUV420
:
252 case __DRI_IMAGE_FOURCC_YVU420
:
253 pf
= PIPE_FORMAT_YV12
;
256 case __DRI_IMAGE_FOURCC_SARGB8888
:
257 case __DRI_IMAGE_FOURCC_YUV410
:
258 case __DRI_IMAGE_FOURCC_YUV411
:
259 case __DRI_IMAGE_FOURCC_YUV422
:
260 case __DRI_IMAGE_FOURCC_YUV444
:
261 case __DRI_IMAGE_FOURCC_NV16
:
262 case __DRI_IMAGE_FOURCC_YVU410
:
263 case __DRI_IMAGE_FOURCC_YVU411
:
264 case __DRI_IMAGE_FOURCC_YVU422
:
265 case __DRI_IMAGE_FOURCC_YVU444
:
267 pf
= PIPE_FORMAT_NONE
;
274 * DRI2 flush extension.
277 dri2_flush_drawable(__DRIdrawable
*dPriv
)
279 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
283 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
285 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
287 dri2InvalidateDrawable(dPriv
);
288 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
289 drawable
->texture_mask
= 0;
291 p_atomic_inc(&drawable
->base
.stamp
);
294 static const __DRI2flushExtension dri2FlushExtension
= {
295 .base
= { __DRI2_FLUSH
, 4 },
297 .flush
= dri2_flush_drawable
,
298 .invalidate
= dri2_invalidate_drawable
,
299 .flush_with_flags
= dri_flush
,
303 * Retrieve __DRIbuffer from the DRI loader.
306 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
307 const enum st_attachment_type
*atts
,
310 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
311 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
313 __DRIbuffer
*buffers
;
315 unsigned attachments
[10];
316 unsigned num_attachments
, i
;
319 with_format
= dri_with_format(drawable
->sPriv
);
323 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
325 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
327 for (i
= 0; i
< *count
; i
++) {
328 enum pipe_format format
;
332 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
333 if (format
== PIPE_FORMAT_NONE
)
337 case ST_ATTACHMENT_FRONT_LEFT
:
341 att
= __DRI_BUFFER_FRONT_LEFT
;
343 case ST_ATTACHMENT_BACK_LEFT
:
344 att
= __DRI_BUFFER_BACK_LEFT
;
346 case ST_ATTACHMENT_FRONT_RIGHT
:
347 att
= __DRI_BUFFER_FRONT_RIGHT
;
349 case ST_ATTACHMENT_BACK_RIGHT
:
350 att
= __DRI_BUFFER_BACK_RIGHT
;
357 * In this switch statement we must support all formats that
358 * may occur as the stvis->color_format.
361 case PIPE_FORMAT_BGRA8888_UNORM
:
362 case PIPE_FORMAT_RGBA8888_UNORM
:
365 case PIPE_FORMAT_BGRX8888_UNORM
:
366 case PIPE_FORMAT_RGBX8888_UNORM
:
369 case PIPE_FORMAT_B5G6R5_UNORM
:
373 depth
= util_format_get_blocksizebits(format
);
374 assert(!"Unexpected format in dri2_drawable_get_buffers()");
377 attachments
[num_attachments
++] = att
;
379 attachments
[num_attachments
++] = depth
;
384 num_attachments
/= 2;
385 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
386 &dri_drawable
->w
, &dri_drawable
->h
,
387 attachments
, num_attachments
,
388 &num_buffers
, dri_drawable
->loaderPrivate
);
391 buffers
= loader
->getBuffers(dri_drawable
,
392 &dri_drawable
->w
, &dri_drawable
->h
,
393 attachments
, num_attachments
,
394 &num_buffers
, dri_drawable
->loaderPrivate
);
398 *count
= num_buffers
;
404 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
405 struct __DRIimageList
*images
,
406 const enum st_attachment_type
*statts
,
407 unsigned statts_count
)
409 __DRIdrawable
*dPriv
= drawable
->dPriv
;
410 __DRIscreen
*sPriv
= drawable
->sPriv
;
411 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
413 uint32_t buffer_mask
= 0;
416 for (i
= 0; i
< statts_count
; i
++) {
417 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
418 if (pf
== PIPE_FORMAT_NONE
)
422 case ST_ATTACHMENT_FRONT_LEFT
:
423 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
425 case ST_ATTACHMENT_BACK_LEFT
:
426 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
433 case PIPE_FORMAT_B5G6R5_UNORM
:
434 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
436 case PIPE_FORMAT_BGRX8888_UNORM
:
437 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
439 case PIPE_FORMAT_BGRA8888_UNORM
:
440 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
442 case PIPE_FORMAT_RGBX8888_UNORM
:
443 image_format
= __DRI_IMAGE_FORMAT_XBGR8888
;
445 case PIPE_FORMAT_RGBA8888_UNORM
:
446 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
449 image_format
= __DRI_IMAGE_FORMAT_NONE
;
454 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
455 (uint32_t *) &drawable
->base
.stamp
,
456 dPriv
->loaderPrivate
, buffer_mask
,
461 dri2_allocate_buffer(__DRIscreen
*sPriv
,
462 unsigned attachment
, unsigned format
,
463 int width
, int height
)
465 struct dri_screen
*screen
= dri_screen(sPriv
);
466 struct dri2_buffer
*buffer
;
467 struct pipe_resource templ
;
470 struct winsys_handle whandle
;
472 switch (attachment
) {
473 case __DRI_BUFFER_FRONT_LEFT
:
474 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
475 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
477 case __DRI_BUFFER_BACK_LEFT
:
478 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
480 case __DRI_BUFFER_DEPTH
:
481 case __DRI_BUFFER_DEPTH_STENCIL
:
482 case __DRI_BUFFER_STENCIL
:
483 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
487 /* because we get the handle and stride */
488 bind
|= PIPE_BIND_SHARED
;
492 pf
= PIPE_FORMAT_BGRA8888_UNORM
;
495 pf
= PIPE_FORMAT_BGRX8888_UNORM
;
498 pf
= PIPE_FORMAT_Z16_UNORM
;
504 buffer
= CALLOC_STRUCT(dri2_buffer
);
508 memset(&templ
, 0, sizeof(templ
));
511 templ
.target
= PIPE_TEXTURE_2D
;
512 templ
.last_level
= 0;
513 templ
.width0
= width
;
514 templ
.height0
= height
;
516 templ
.array_size
= 1;
519 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
520 if (!buffer
->resource
) {
525 memset(&whandle
, 0, sizeof(whandle
));
526 if (screen
->can_share_buffer
)
527 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
529 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
531 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
, NULL
,
532 buffer
->resource
, &whandle
,
533 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
535 buffer
->base
.attachment
= attachment
;
536 buffer
->base
.name
= whandle
.handle
;
537 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
538 buffer
->base
.pitch
= whandle
.stride
;
540 return &buffer
->base
;
544 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
546 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
548 pipe_resource_reference(&buffer
->resource
, NULL
);
553 * Backend functions for st_framebuffer interface.
557 dri2_allocate_textures(struct dri_context
*ctx
,
558 struct dri_drawable
*drawable
,
559 const enum st_attachment_type
*statts
,
560 unsigned statts_count
)
562 __DRIscreen
*sPriv
= drawable
->sPriv
;
563 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
564 struct dri_screen
*screen
= dri_screen(sPriv
);
565 struct pipe_resource templ
;
566 boolean alloc_depthstencil
= FALSE
;
568 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
569 /* Image specific variables */
570 struct __DRIimageList images
;
571 /* Dri2 specific variables */
572 __DRIbuffer
*buffers
= NULL
;
573 struct winsys_handle whandle
;
574 unsigned num_buffers
= statts_count
;
576 /* First get the buffers from the loader */
578 if (!dri_image_drawable_get_buffers(drawable
, &images
,
579 statts
, statts_count
))
583 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
584 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
585 drawable
->old_w
== dri_drawable
->w
&&
586 drawable
->old_h
== dri_drawable
->h
&&
587 memcmp(drawable
->old
, buffers
,
588 sizeof(__DRIbuffer
) * num_buffers
) == 0))
592 /* Second clean useless resources*/
594 /* See if we need a depth-stencil buffer. */
595 for (i
= 0; i
< statts_count
; i
++) {
596 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
597 alloc_depthstencil
= TRUE
;
602 /* Delete the resources we won't need. */
603 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
604 /* Don't delete the depth-stencil buffer, we can reuse it. */
605 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
608 /* Flush the texture before unreferencing, so that other clients can
609 * see what the driver has rendered.
611 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
612 struct pipe_context
*pipe
= ctx
->st
->pipe
;
613 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
616 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
619 if (drawable
->stvis
.samples
> 1) {
620 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
623 /* Don't delete MSAA resources for the attachments which are enabled,
624 * we can reuse them. */
625 for (j
= 0; j
< statts_count
; j
++) {
626 if (i
== statts
[j
]) {
633 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
638 /* Third use the buffers retrieved to fill the drawable info */
640 memset(&templ
, 0, sizeof(templ
));
641 templ
.target
= screen
->target
;
642 templ
.last_level
= 0;
644 templ
.array_size
= 1;
647 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
648 struct pipe_resource
**buf
=
649 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
650 struct pipe_resource
*texture
= images
.front
->texture
;
652 dri_drawable
->w
= texture
->width0
;
653 dri_drawable
->h
= texture
->height0
;
655 pipe_resource_reference(buf
, texture
);
658 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
659 struct pipe_resource
**buf
=
660 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
661 struct pipe_resource
*texture
= images
.back
->texture
;
663 dri_drawable
->w
= texture
->width0
;
664 dri_drawable
->h
= texture
->height0
;
666 pipe_resource_reference(buf
, texture
);
669 /* Note: if there is both a back and a front buffer,
670 * then they have the same size.
672 templ
.width0
= dri_drawable
->w
;
673 templ
.height0
= dri_drawable
->h
;
676 memset(&whandle
, 0, sizeof(whandle
));
678 /* Process DRI-provided buffers and get pipe_resources. */
679 for (i
= 0; i
< num_buffers
; i
++) {
680 __DRIbuffer
*buf
= &buffers
[i
];
681 enum st_attachment_type statt
;
682 enum pipe_format format
;
684 switch (buf
->attachment
) {
685 case __DRI_BUFFER_FRONT_LEFT
:
686 if (!screen
->auto_fake_front
) {
687 continue; /* invalid attachment */
690 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
691 statt
= ST_ATTACHMENT_FRONT_LEFT
;
693 case __DRI_BUFFER_BACK_LEFT
:
694 statt
= ST_ATTACHMENT_BACK_LEFT
;
697 continue; /* invalid attachment */
700 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
701 if (format
== PIPE_FORMAT_NONE
)
704 /* dri2_drawable_get_buffers has already filled dri_drawable->w
705 * and dri_drawable->h */
706 templ
.width0
= dri_drawable
->w
;
707 templ
.height0
= dri_drawable
->h
;
708 templ
.format
= format
;
710 whandle
.handle
= buf
->name
;
711 whandle
.stride
= buf
->pitch
;
713 if (screen
->can_share_buffer
)
714 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
716 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
717 drawable
->textures
[statt
] =
718 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
720 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
);
721 assert(drawable
->textures
[statt
]);
725 /* Allocate private MSAA colorbuffers. */
726 if (drawable
->stvis
.samples
> 1) {
727 for (i
= 0; i
< statts_count
; i
++) {
728 enum st_attachment_type statt
= statts
[i
];
730 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
733 if (drawable
->textures
[statt
]) {
734 templ
.format
= drawable
->textures
[statt
]->format
;
735 templ
.bind
= drawable
->textures
[statt
]->bind
&
736 ~(PIPE_BIND_SCANOUT
| PIPE_BIND_SHARED
);
737 templ
.nr_samples
= drawable
->stvis
.samples
;
739 /* Try to reuse the resource.
740 * (the other resource parameters should be constant)
742 if (!drawable
->msaa_textures
[statt
] ||
743 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
744 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
745 /* Allocate a new one. */
746 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
748 drawable
->msaa_textures
[statt
] =
749 screen
->base
.screen
->resource_create(screen
->base
.screen
,
751 assert(drawable
->msaa_textures
[statt
]);
753 /* If there are any MSAA resources, we should initialize them
754 * such that they contain the same data as the single-sample
755 * resources we just got from the X server.
757 * The reason for this is that the state tracker (and
758 * therefore the app) can access the MSAA resources only.
759 * The single-sample resources are not exposed
760 * to the state tracker.
763 dri_pipe_blit(ctx
->st
->pipe
,
764 drawable
->msaa_textures
[statt
],
765 drawable
->textures
[statt
]);
769 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
774 /* Allocate a private depth-stencil buffer. */
775 if (alloc_depthstencil
) {
776 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
777 struct pipe_resource
**zsbuf
;
778 enum pipe_format format
;
781 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
784 templ
.format
= format
;
785 templ
.bind
= bind
& ~PIPE_BIND_SHARED
;
787 if (drawable
->stvis
.samples
> 1) {
788 templ
.nr_samples
= drawable
->stvis
.samples
;
789 zsbuf
= &drawable
->msaa_textures
[statt
];
792 templ
.nr_samples
= 0;
793 zsbuf
= &drawable
->textures
[statt
];
796 /* Try to reuse the resource.
797 * (the other resource parameters should be constant)
800 (*zsbuf
)->width0
!= templ
.width0
||
801 (*zsbuf
)->height0
!= templ
.height0
) {
802 /* Allocate a new one. */
803 pipe_resource_reference(zsbuf
, NULL
);
804 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
810 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
811 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
815 /* For DRI2, we may get the same buffers again from the server.
816 * To prevent useless imports of gem names, drawable->old* is used
817 * to bypass the import if we get the same buffers. This doesn't apply
818 * to DRI3/Wayland, users of image.loader, since the buffer is managed
819 * by the client (no import), and the back buffer is going to change
823 drawable
->old_num
= num_buffers
;
824 drawable
->old_w
= dri_drawable
->w
;
825 drawable
->old_h
= dri_drawable
->h
;
826 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
831 dri2_flush_frontbuffer(struct dri_context
*ctx
,
832 struct dri_drawable
*drawable
,
833 enum st_attachment_type statt
)
835 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
836 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
837 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
838 struct pipe_context
*pipe
= ctx
->st
->pipe
;
840 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
843 if (drawable
->stvis
.samples
> 1) {
844 /* Resolve the front buffer. */
845 dri_pipe_blit(ctx
->st
->pipe
,
846 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
847 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
850 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
851 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
854 pipe
->flush(pipe
, NULL
, 0);
857 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
859 else if (loader
->flushFrontBuffer
) {
860 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
865 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
866 struct dri_context
*ctx
,
867 struct pipe_resource
*res
)
873 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
875 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
878 if (!loader
->lookupEGLImage
)
881 img
= loader
->lookupEGLImage(screen
->sPriv
,
882 handle
, screen
->sPriv
->loaderPrivate
);
888 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
889 int width
, int height
, int format
,
890 int num_handles
, struct winsys_handle
*whandle
,
893 struct dri_screen
*screen
= dri_screen(_screen
);
894 struct pipe_screen
*pscreen
= screen
->base
.screen
;
896 struct pipe_resource templ
;
901 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
903 pf
= dri2_format_to_pipe_format (format
);
904 if (pf
== PIPE_FORMAT_NONE
)
907 img
= CALLOC_STRUCT(__DRIimageRec
);
911 memset(&templ
, 0, sizeof(templ
));
912 templ
.bind
= tex_usage
;
913 templ
.target
= screen
->target
;
914 templ
.last_level
= 0;
916 templ
.array_size
= 1;
918 for (i
= num_handles
- 1; i
>= 0; i
--) {
919 struct pipe_resource
*tex
;
921 /* TODO: something a lot less ugly */
924 templ
.width0
= width
;
925 templ
.height0
= height
;
929 templ
.width0
= width
/ 2;
930 templ
.height0
= height
/ 2;
931 templ
.format
= (num_handles
== 2) ?
932 PIPE_FORMAT_RG88_UNORM
: /* NV12, etc */
933 PIPE_FORMAT_R8_UNORM
; /* I420, etc */
936 templ
.width0
= width
/ 2;
937 templ
.height0
= height
/ 2;
938 templ
.format
= PIPE_FORMAT_R8_UNORM
;
941 unreachable("too many planes!");
944 tex
= pscreen
->resource_from_handle(pscreen
,
945 &templ
, &whandle
[i
], PIPE_HANDLE_USAGE_READ_WRITE
);
947 pipe_resource_reference(&img
->texture
, NULL
);
952 tex
->next
= img
->texture
;
958 img
->dri_format
= format
;
960 img
->loader_private
= loaderPrivate
;
966 dri2_create_image_from_name(__DRIscreen
*_screen
,
967 int width
, int height
, int format
,
968 int name
, int pitch
, void *loaderPrivate
)
970 struct winsys_handle whandle
;
973 memset(&whandle
, 0, sizeof(whandle
));
974 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
975 whandle
.handle
= name
;
976 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
978 pf
= dri2_format_to_pipe_format (format
);
979 if (pf
== PIPE_FORMAT_NONE
)
982 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
984 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
985 1, &whandle
, loaderPrivate
);
989 dri2_create_image_from_fd(__DRIscreen
*_screen
,
990 int width
, int height
, int fourcc
,
991 uint64_t modifier
, int *fds
, int num_fds
,
992 int *strides
, int *offsets
, unsigned *error
,
993 int *dri_components
, void *loaderPrivate
)
995 struct winsys_handle whandles
[3];
997 __DRIimage
*img
= NULL
;
998 unsigned err
= __DRI_IMAGE_ERROR_SUCCESS
;
999 int expected_num_fds
, i
;
1002 case __DRI_IMAGE_FOURCC_YUV420
:
1003 case __DRI_IMAGE_FOURCC_YVU420
:
1004 expected_num_fds
= 3;
1006 case __DRI_IMAGE_FOURCC_NV12
:
1007 expected_num_fds
= 2;
1010 expected_num_fds
= 1;
1014 if (num_fds
!= expected_num_fds
) {
1015 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1019 format
= convert_fourcc(fourcc
, dri_components
);
1021 err
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1025 memset(whandles
, 0, sizeof(whandles
));
1027 for (i
= 0; i
< num_fds
; i
++) {
1029 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1033 whandles
[i
].type
= DRM_API_HANDLE_TYPE_FD
;
1034 whandles
[i
].handle
= (unsigned)fds
[i
];
1035 whandles
[i
].stride
= (unsigned)strides
[i
];
1036 whandles
[i
].offset
= (unsigned)offsets
[i
];
1037 whandles
[i
].modifier
= modifier
;
1040 if (fourcc
== __DRI_IMAGE_FOURCC_YVU420
) {
1041 /* convert to YUV420 by swapping 2nd and 3rd planes: */
1042 struct winsys_handle tmp
= whandles
[1];
1043 whandles
[1] = whandles
[2];
1045 fourcc
= __DRI_IMAGE_FOURCC_YUV420
;
1048 img
= dri2_create_image_from_winsys(_screen
, width
, height
, format
,
1049 num_fds
, whandles
, loaderPrivate
);
1051 err
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1061 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
1062 int renderbuffer
, void *loaderPrivate
)
1064 struct dri_context
*ctx
= dri_context(context
);
1066 if (!ctx
->st
->get_resource_for_egl_image
)
1074 dri2_create_image_common(__DRIscreen
*_screen
,
1075 int width
, int height
,
1076 int format
, unsigned int use
,
1077 const uint64_t *modifiers
,
1078 const unsigned count
,
1079 void *loaderPrivate
)
1081 struct dri_screen
*screen
= dri_screen(_screen
);
1083 struct pipe_resource templ
;
1085 enum pipe_format pf
;
1087 /* createImageWithModifiers doesn't supply usage, and we should not get
1088 * here with both modifiers and a usage flag.
1090 assert(!(use
&& (modifiers
!= NULL
)));
1092 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1094 if (use
& __DRI_IMAGE_USE_SCANOUT
)
1095 tex_usage
|= PIPE_BIND_SCANOUT
;
1096 if (use
& __DRI_IMAGE_USE_SHARE
)
1097 tex_usage
|= PIPE_BIND_SHARED
;
1098 if (use
& __DRI_IMAGE_USE_LINEAR
)
1099 tex_usage
|= PIPE_BIND_LINEAR
;
1100 if (use
& __DRI_IMAGE_USE_CURSOR
) {
1101 if (width
!= 64 || height
!= 64)
1103 tex_usage
|= PIPE_BIND_CURSOR
;
1106 pf
= dri2_format_to_pipe_format (format
);
1107 if (pf
== PIPE_FORMAT_NONE
)
1110 img
= CALLOC_STRUCT(__DRIimageRec
);
1114 memset(&templ
, 0, sizeof(templ
));
1115 templ
.bind
= tex_usage
;
1117 templ
.target
= PIPE_TEXTURE_2D
;
1118 templ
.last_level
= 0;
1119 templ
.width0
= width
;
1120 templ
.height0
= height
;
1122 templ
.array_size
= 1;
1127 ->resource_create_with_modifiers(screen
->base
.screen
,
1133 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
1134 if (!img
->texture
) {
1141 img
->dri_format
= format
;
1142 img
->dri_components
= 0;
1145 img
->loader_private
= loaderPrivate
;
1150 dri2_create_image(__DRIscreen
*_screen
,
1151 int width
, int height
, int format
,
1152 unsigned int use
, void *loaderPrivate
)
1154 return dri2_create_image_common(_screen
, width
, height
, format
, use
,
1155 NULL
/* modifiers */, 0 /* count */,
1160 dri2_create_image_with_modifiers(__DRIscreen
*dri_screen
,
1161 int width
, int height
, int format
,
1162 const uint64_t *modifiers
,
1163 const unsigned count
,
1164 void *loaderPrivate
)
1166 return dri2_create_image_common(dri_screen
, width
, height
, format
,
1167 0 /* use */, modifiers
, count
,
1172 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
1174 struct winsys_handle whandle
;
1177 if (image
->use
& __DRI_IMAGE_USE_BACKBUFFER
)
1178 usage
= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
| PIPE_HANDLE_USAGE_READ
;
1180 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1182 memset(&whandle
, 0, sizeof(whandle
));
1185 case __DRI_IMAGE_ATTRIB_STRIDE
:
1186 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1187 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1188 NULL
, image
->texture
, &whandle
, usage
))
1190 *value
= whandle
.stride
;
1192 case __DRI_IMAGE_ATTRIB_OFFSET
:
1193 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1194 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1195 NULL
, image
->texture
, &whandle
, usage
))
1197 *value
= whandle
.offset
;
1199 case __DRI_IMAGE_ATTRIB_HANDLE
:
1200 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1201 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1202 NULL
, image
->texture
, &whandle
, usage
))
1204 *value
= whandle
.handle
;
1206 case __DRI_IMAGE_ATTRIB_NAME
:
1207 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1208 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1209 NULL
, image
->texture
, &whandle
, usage
))
1211 *value
= whandle
.handle
;
1213 case __DRI_IMAGE_ATTRIB_FD
:
1214 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1215 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1216 NULL
, image
->texture
, &whandle
, usage
))
1219 *value
= whandle
.handle
;
1221 case __DRI_IMAGE_ATTRIB_FORMAT
:
1222 *value
= image
->dri_format
;
1224 case __DRI_IMAGE_ATTRIB_WIDTH
:
1225 *value
= image
->texture
->width0
;
1227 case __DRI_IMAGE_ATTRIB_HEIGHT
:
1228 *value
= image
->texture
->height0
;
1230 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
1231 if (image
->dri_components
== 0)
1233 *value
= image
->dri_components
;
1235 case __DRI_IMAGE_ATTRIB_FOURCC
:
1236 *value
= convert_to_fourcc(image
->dri_format
);
1238 case __DRI_IMAGE_ATTRIB_NUM_PLANES
:
1241 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
:
1242 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1243 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1244 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1245 NULL
, image
->texture
, &whandle
, usage
))
1247 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1249 *value
= (whandle
.modifier
>> 32) & 0xffffffff;
1251 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
:
1252 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1253 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1254 if (!image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
1255 NULL
, image
->texture
, &whandle
, usage
))
1257 if (whandle
.modifier
== DRM_FORMAT_MOD_INVALID
)
1259 *value
= whandle
.modifier
& 0xffffffff;
1267 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
1271 img
= CALLOC_STRUCT(__DRIimageRec
);
1275 img
->texture
= NULL
;
1276 pipe_resource_reference(&img
->texture
, image
->texture
);
1277 img
->level
= image
->level
;
1278 img
->layer
= image
->layer
;
1279 img
->dri_format
= image
->dri_format
;
1280 /* This should be 0 for sub images, but dup is also used for base images. */
1281 img
->dri_components
= image
->dri_components
;
1282 img
->loader_private
= loaderPrivate
;
1288 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
1291 * Gallium drivers are bad at adding usages to the resources
1292 * once opened again in another process, which is the main use
1293 * case for this, so we have to lie.
1302 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
1303 int *names
, int num_names
, int *strides
, int *offsets
,
1304 void *loaderPrivate
)
1308 struct winsys_handle whandle
;
1313 format
= convert_fourcc(format
, &dri_components
);
1317 memset(&whandle
, 0, sizeof(whandle
));
1318 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
1319 whandle
.handle
= names
[0];
1320 whandle
.stride
= strides
[0];
1321 whandle
.offset
= offsets
[0];
1322 whandle
.modifier
= DRM_FORMAT_MOD_INVALID
;
1324 img
= dri2_create_image_from_winsys(screen
, width
, height
, format
,
1325 1, &whandle
, loaderPrivate
);
1329 img
->dri_components
= dri_components
;
1334 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
1341 if (image
->dri_components
== 0)
1344 img
= dri2_dup_image(image
, loaderPrivate
);
1348 if (img
->texture
->screen
->resource_changed
)
1349 img
->texture
->screen
->resource_changed(img
->texture
->screen
,
1352 /* set this to 0 for sub images. */
1353 img
->dri_components
= 0;
1358 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1359 int depth
, int level
, unsigned *error
,
1360 void *loaderPrivate
)
1363 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1364 struct gl_texture_object
*obj
;
1365 struct pipe_resource
*tex
;
1368 obj
= _mesa_lookup_texture(ctx
, texture
);
1369 if (!obj
|| obj
->Target
!= target
) {
1370 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1374 tex
= st_get_texobj_resource(obj
);
1376 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1380 if (target
== GL_TEXTURE_CUBE_MAP
)
1383 _mesa_test_texobj_completeness(ctx
, obj
);
1384 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1385 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1389 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1390 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1394 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1395 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1399 img
= CALLOC_STRUCT(__DRIimageRec
);
1401 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1407 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1409 img
->loader_private
= loaderPrivate
;
1411 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1412 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1417 pipe_resource_reference(&img
->texture
, tex
);
1419 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1424 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1425 int *fds
, int num_fds
, int *strides
, int *offsets
,
1426 void *loaderPrivate
)
1431 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1432 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1433 strides
, offsets
, NULL
,
1434 &dri_components
, loaderPrivate
);
1438 img
->dri_components
= dri_components
;
1443 dri2_query_dma_buf_formats(__DRIscreen
*_screen
, int max
, int *formats
,
1446 struct dri_screen
*screen
= dri_screen(_screen
);
1447 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1448 const unsigned bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1451 for (i
= 0, j
= 0; (i
< ARRAY_SIZE(fourcc_formats
)) &&
1452 (j
< max
|| max
== 0); i
++) {
1453 if (pscreen
->is_format_supported(pscreen
,
1454 fourcc_to_pipe_format(
1459 formats
[j
] = fourcc_formats
[i
];
1468 dri2_query_dma_buf_modifiers(__DRIscreen
*_screen
, int fourcc
, int max
,
1469 uint64_t *modifiers
, unsigned int *external_only
,
1472 struct dri_screen
*screen
= dri_screen(_screen
);
1473 struct pipe_screen
*pscreen
= screen
->base
.screen
;
1474 enum pipe_format format
= fourcc_to_pipe_format(fourcc
);
1475 const unsigned usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
1477 if (pscreen
->query_dmabuf_modifiers
!= NULL
&&
1478 pscreen
->is_format_supported(pscreen
, format
, screen
->target
, 0, usage
)) {
1479 pscreen
->query_dmabuf_modifiers(pscreen
, format
, max
, modifiers
,
1480 external_only
, count
);
1487 dri2_from_dma_bufs(__DRIscreen
*screen
,
1488 int width
, int height
, int fourcc
,
1489 int *fds
, int num_fds
,
1490 int *strides
, int *offsets
,
1491 enum __DRIYUVColorSpace yuv_color_space
,
1492 enum __DRISampleRange sample_range
,
1493 enum __DRIChromaSiting horizontal_siting
,
1494 enum __DRIChromaSiting vertical_siting
,
1496 void *loaderPrivate
)
1501 img
= dri2_create_image_from_fd(screen
, width
, height
, fourcc
,
1502 DRM_FORMAT_MOD_INVALID
, fds
, num_fds
,
1503 strides
, offsets
, error
,
1504 &dri_components
, loaderPrivate
);
1508 img
->yuv_color_space
= yuv_color_space
;
1509 img
->sample_range
= sample_range
;
1510 img
->horizontal_siting
= horizontal_siting
;
1511 img
->vertical_siting
= vertical_siting
;
1512 img
->dri_components
= dri_components
;
1514 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1519 dri2_from_dma_bufs2(__DRIscreen
*screen
,
1520 int width
, int height
, int fourcc
,
1521 uint64_t modifier
, 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 modifier
, fds
, num_fds
, strides
, offsets
,
1535 error
, &dri_components
, loaderPrivate
);
1539 img
->yuv_color_space
= yuv_color_space
;
1540 img
->sample_range
= sample_range
;
1541 img
->horizontal_siting
= horizontal_siting
;
1542 img
->vertical_siting
= vertical_siting
;
1543 img
->dri_components
= dri_components
;
1545 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1550 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1551 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1552 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1555 struct dri_context
*ctx
= dri_context(context
);
1556 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1557 struct pipe_screen
*screen
;
1558 struct pipe_fence_handle
*fence
;
1559 struct pipe_blit_info blit
;
1564 memset(&blit
, 0, sizeof(blit
));
1565 blit
.dst
.resource
= dst
->texture
;
1566 blit
.dst
.box
.x
= dstx0
;
1567 blit
.dst
.box
.y
= dsty0
;
1568 blit
.dst
.box
.width
= dstwidth
;
1569 blit
.dst
.box
.height
= dstheight
;
1570 blit
.dst
.box
.depth
= 1;
1571 blit
.dst
.format
= dst
->texture
->format
;
1572 blit
.src
.resource
= src
->texture
;
1573 blit
.src
.box
.x
= srcx0
;
1574 blit
.src
.box
.y
= srcy0
;
1575 blit
.src
.box
.width
= srcwidth
;
1576 blit
.src
.box
.height
= srcheight
;
1577 blit
.src
.box
.depth
= 1;
1578 blit
.src
.format
= src
->texture
->format
;
1579 blit
.mask
= PIPE_MASK_RGBA
;
1580 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1582 pipe
->blit(pipe
, &blit
);
1584 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1585 pipe
->flush_resource(pipe
, dst
->texture
);
1586 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1587 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1588 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1589 pipe
->flush_resource(pipe
, dst
->texture
);
1590 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1591 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
1592 screen
->fence_reference(screen
, &fence
, NULL
);
1597 dri2_map_image(__DRIcontext
*context
, __DRIimage
*image
,
1598 int x0
, int y0
, int width
, int height
,
1599 unsigned int flags
, int *stride
, void **data
)
1601 struct dri_context
*ctx
= dri_context(context
);
1602 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1603 enum pipe_transfer_usage pipe_access
= 0;
1604 struct pipe_transfer
*trans
;
1607 if (!image
|| !data
|| *data
)
1610 if (flags
& __DRI_IMAGE_TRANSFER_READ
)
1611 pipe_access
|= PIPE_TRANSFER_READ
;
1612 if (flags
& __DRI_IMAGE_TRANSFER_WRITE
)
1613 pipe_access
|= PIPE_TRANSFER_WRITE
;
1615 map
= pipe_transfer_map(pipe
, image
->texture
,
1616 0, 0, pipe_access
, x0
, y0
, width
, height
,
1620 *stride
= trans
->stride
;
1627 dri2_unmap_image(__DRIcontext
*context
, __DRIimage
*image
, void *data
)
1629 struct dri_context
*ctx
= dri_context(context
);
1630 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1632 pipe_transfer_unmap(pipe
, (struct pipe_transfer
*)data
);
1636 dri2_destroy_image(__DRIimage
*img
)
1638 pipe_resource_reference(&img
->texture
, NULL
);
1643 dri2_get_capabilities(__DRIscreen
*_screen
)
1645 struct dri_screen
*screen
= dri_screen(_screen
);
1647 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1650 /* The extension is modified during runtime if DRI_PRIME is detected */
1651 static __DRIimageExtension dri2ImageExtension
= {
1652 .base
= { __DRI_IMAGE
, 15 },
1654 .createImageFromName
= dri2_create_image_from_name
,
1655 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1656 .destroyImage
= dri2_destroy_image
,
1657 .createImage
= dri2_create_image
,
1658 .queryImage
= dri2_query_image
,
1659 .dupImage
= dri2_dup_image
,
1660 .validateUsage
= dri2_validate_usage
,
1661 .createImageFromNames
= dri2_from_names
,
1662 .fromPlanar
= dri2_from_planar
,
1663 .createImageFromTexture
= dri2_create_from_texture
,
1664 .createImageFromFds
= NULL
,
1665 .createImageFromDmaBufs
= NULL
,
1666 .blitImage
= dri2_blit_image
,
1667 .getCapabilities
= dri2_get_capabilities
,
1668 .mapImage
= dri2_map_image
,
1669 .unmapImage
= dri2_unmap_image
,
1672 static const __DRIrobustnessExtension dri2Robustness
= {
1673 .base
= { __DRI2_ROBUSTNESS
, 1 }
1677 dri2_interop_query_device_info(__DRIcontext
*_ctx
,
1678 struct mesa_glinterop_device_info
*out
)
1680 struct pipe_screen
*screen
= dri_context(_ctx
)->st
->pipe
->screen
;
1682 /* There is no version 0, thus we do not support it */
1683 if (out
->version
== 0)
1684 return MESA_GLINTEROP_INVALID_VERSION
;
1686 out
->pci_segment_group
= screen
->get_param(screen
, PIPE_CAP_PCI_GROUP
);
1687 out
->pci_bus
= screen
->get_param(screen
, PIPE_CAP_PCI_BUS
);
1688 out
->pci_device
= screen
->get_param(screen
, PIPE_CAP_PCI_DEVICE
);
1689 out
->pci_function
= screen
->get_param(screen
, PIPE_CAP_PCI_FUNCTION
);
1691 out
->vendor_id
= screen
->get_param(screen
, PIPE_CAP_VENDOR_ID
);
1692 out
->device_id
= screen
->get_param(screen
, PIPE_CAP_DEVICE_ID
);
1694 /* Instruct the caller that we support up-to version one of the interface */
1697 return MESA_GLINTEROP_SUCCESS
;
1701 dri2_interop_export_object(__DRIcontext
*_ctx
,
1702 struct mesa_glinterop_export_in
*in
,
1703 struct mesa_glinterop_export_out
*out
)
1705 struct st_context_iface
*st
= dri_context(_ctx
)->st
;
1706 struct pipe_screen
*screen
= st
->pipe
->screen
;
1707 struct gl_context
*ctx
= ((struct st_context
*)st
)->ctx
;
1708 struct pipe_resource
*res
= NULL
;
1709 struct winsys_handle whandle
;
1710 unsigned target
, usage
;
1713 /* There is no version 0, thus we do not support it */
1714 if (in
->version
== 0 || out
->version
== 0)
1715 return MESA_GLINTEROP_INVALID_VERSION
;
1717 /* Validate the target. */
1718 switch (in
->target
) {
1719 case GL_TEXTURE_BUFFER
:
1723 case GL_TEXTURE_RECTANGLE
:
1724 case GL_TEXTURE_1D_ARRAY
:
1725 case GL_TEXTURE_2D_ARRAY
:
1726 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1727 case GL_TEXTURE_CUBE_MAP
:
1728 case GL_TEXTURE_2D_MULTISAMPLE
:
1729 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1730 case GL_TEXTURE_EXTERNAL_OES
:
1731 case GL_RENDERBUFFER
:
1732 case GL_ARRAY_BUFFER
:
1733 target
= in
->target
;
1735 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1736 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1737 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1738 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1739 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1740 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1741 target
= GL_TEXTURE_CUBE_MAP
;
1744 return MESA_GLINTEROP_INVALID_TARGET
;
1747 /* Validate the simple case of miplevel. */
1748 if ((target
== GL_RENDERBUFFER
|| target
== GL_ARRAY_BUFFER
) &&
1750 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1752 /* Validate the OpenGL object and get pipe_resource. */
1753 mtx_lock(&ctx
->Shared
->Mutex
);
1755 if (target
== GL_ARRAY_BUFFER
) {
1758 * The error checking is based on the documentation of
1759 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1761 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, in
->obj
);
1763 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1764 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1765 * a GL buffer object but does not have an existing data store or
1766 * the size of the buffer is 0."
1768 if (!buf
|| buf
->Size
== 0) {
1769 mtx_unlock(&ctx
->Shared
->Mutex
);
1770 return MESA_GLINTEROP_INVALID_OBJECT
;
1773 res
= st_buffer_object(buf
)->buffer
;
1775 /* this shouldn't happen */
1776 mtx_unlock(&ctx
->Shared
->Mutex
);
1777 return MESA_GLINTEROP_INVALID_OBJECT
;
1780 out
->buf_offset
= 0;
1781 out
->buf_size
= buf
->Size
;
1783 buf
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1784 } else if (target
== GL_RENDERBUFFER
) {
1787 * The error checking is based on the documentation of
1788 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1790 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, in
->obj
);
1792 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1793 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1794 * object or if the width or height of renderbuffer is zero."
1796 if (!rb
|| rb
->Width
== 0 || rb
->Height
== 0) {
1797 mtx_unlock(&ctx
->Shared
->Mutex
);
1798 return MESA_GLINTEROP_INVALID_OBJECT
;
1801 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1802 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1803 * renderbuffer object."
1805 if (rb
->NumSamples
> 1) {
1806 mtx_unlock(&ctx
->Shared
->Mutex
);
1807 return MESA_GLINTEROP_INVALID_OPERATION
;
1810 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1811 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1812 * required by the OpenCL implementation on the device."
1814 res
= st_renderbuffer(rb
)->texture
;
1816 mtx_unlock(&ctx
->Shared
->Mutex
);
1817 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1820 out
->internal_format
= rb
->InternalFormat
;
1821 out
->view_minlevel
= 0;
1822 out
->view_numlevels
= 1;
1823 out
->view_minlayer
= 0;
1824 out
->view_numlayers
= 1;
1828 * The error checking is based on the documentation of
1829 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1831 struct gl_texture_object
*obj
= _mesa_lookup_texture(ctx
, in
->obj
);
1834 _mesa_test_texobj_completeness(ctx
, obj
);
1836 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1837 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1838 * type matches texture_target, if the specified miplevel of texture
1839 * is not defined, or if the width or height of the specified
1840 * miplevel is zero or if the GL texture object is incomplete."
1843 obj
->Target
!= target
||
1844 !obj
->_BaseComplete
||
1845 (in
->miplevel
> 0 && !obj
->_MipmapComplete
)) {
1846 mtx_unlock(&ctx
->Shared
->Mutex
);
1847 return MESA_GLINTEROP_INVALID_OBJECT
;
1850 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1851 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1852 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1853 * implementations); or greater than the value of q (for both OpenGL
1854 * and OpenGL ES). levelbase and q are defined for the texture in
1855 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1856 * specification and section 3.7.10 of the OpenGL ES 2.0."
1858 if (in
->miplevel
< obj
->BaseLevel
|| in
->miplevel
> obj
->_MaxLevel
) {
1859 mtx_unlock(&ctx
->Shared
->Mutex
);
1860 return MESA_GLINTEROP_INVALID_MIP_LEVEL
;
1863 if (!st_finalize_texture(ctx
, st
->pipe
, obj
, 0)) {
1864 mtx_unlock(&ctx
->Shared
->Mutex
);
1865 return MESA_GLINTEROP_OUT_OF_RESOURCES
;
1868 res
= st_get_texobj_resource(obj
);
1870 /* Incomplete texture buffer object? This shouldn't really occur. */
1871 mtx_unlock(&ctx
->Shared
->Mutex
);
1872 return MESA_GLINTEROP_INVALID_OBJECT
;
1875 if (target
== GL_TEXTURE_BUFFER
) {
1876 out
->internal_format
= obj
->BufferObjectFormat
;
1877 out
->buf_offset
= obj
->BufferOffset
;
1878 out
->buf_size
= obj
->BufferSize
== -1 ? obj
->BufferObject
->Size
:
1881 obj
->BufferObject
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
1883 out
->internal_format
= obj
->Image
[0][0]->InternalFormat
;
1884 out
->view_minlevel
= obj
->MinLevel
;
1885 out
->view_numlevels
= obj
->NumLevels
;
1886 out
->view_minlayer
= obj
->MinLayer
;
1887 out
->view_numlayers
= obj
->NumLayers
;
1891 /* Get the handle. */
1892 switch (in
->access
) {
1893 case MESA_GLINTEROP_ACCESS_READ_WRITE
:
1894 usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
1896 case MESA_GLINTEROP_ACCESS_READ_ONLY
:
1897 usage
= PIPE_HANDLE_USAGE_READ
;
1899 case MESA_GLINTEROP_ACCESS_WRITE_ONLY
:
1900 usage
= PIPE_HANDLE_USAGE_WRITE
;
1906 memset(&whandle
, 0, sizeof(whandle
));
1907 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
1909 success
= screen
->resource_get_handle(screen
, st
->pipe
, res
, &whandle
,
1911 mtx_unlock(&ctx
->Shared
->Mutex
);
1914 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY
;
1916 out
->dmabuf_fd
= whandle
.handle
;
1917 out
->out_driver_data_written
= 0;
1919 if (res
->target
== PIPE_BUFFER
)
1920 out
->buf_offset
+= whandle
.offset
;
1922 /* Instruct the caller that we support up-to version one of the interface */
1926 return MESA_GLINTEROP_SUCCESS
;
1929 static const __DRI2interopExtension dri2InteropExtension
= {
1930 .base
= { __DRI2_INTEROP
, 1 },
1931 .query_device_info
= dri2_interop_query_device_info
,
1932 .export_object
= dri2_interop_export_object
1936 * \brief the DRI2ConfigQueryExtension configQueryb method
1939 dri2GalliumConfigQueryb(__DRIscreen
*sPriv
, const char *var
,
1942 struct dri_screen
*screen
= dri_screen(sPriv
);
1944 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_BOOL
))
1945 return dri2ConfigQueryExtension
.configQueryb(sPriv
, var
, val
);
1947 *val
= driQueryOptionb(&screen
->dev
->option_cache
, var
);
1953 * \brief the DRI2ConfigQueryExtension configQueryi method
1956 dri2GalliumConfigQueryi(__DRIscreen
*sPriv
, const char *var
, int *val
)
1958 struct dri_screen
*screen
= dri_screen(sPriv
);
1960 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_INT
) &&
1961 !driCheckOption(&screen
->dev
->option_cache
, var
, DRI_ENUM
))
1962 return dri2ConfigQueryExtension
.configQueryi(sPriv
, var
, val
);
1964 *val
= driQueryOptioni(&screen
->dev
->option_cache
, var
);
1970 * \brief the DRI2ConfigQueryExtension configQueryf method
1973 dri2GalliumConfigQueryf(__DRIscreen
*sPriv
, const char *var
, float *val
)
1975 struct dri_screen
*screen
= dri_screen(sPriv
);
1977 if (!driCheckOption(&screen
->dev
->option_cache
, var
, DRI_FLOAT
))
1978 return dri2ConfigQueryExtension
.configQueryf(sPriv
, var
, val
);
1980 *val
= driQueryOptionf(&screen
->dev
->option_cache
, var
);
1986 * \brief the DRI2ConfigQueryExtension struct.
1988 * We first query the driver option cache. Then the dri2 option cache.
1990 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension
= {
1991 .base
= { __DRI2_CONFIG_QUERY
, 1 },
1993 .configQueryb
= dri2GalliumConfigQueryb
,
1994 .configQueryi
= dri2GalliumConfigQueryi
,
1995 .configQueryf
= dri2GalliumConfigQueryf
,
1999 * Backend function init_screen.
2002 static const __DRIextension
*dri_screen_extensions
[] = {
2003 &driTexBufferExtension
.base
,
2004 &dri2FlushExtension
.base
,
2005 &dri2ImageExtension
.base
,
2006 &dri2RendererQueryExtension
.base
,
2007 &dri2GalliumConfigQueryExtension
.base
,
2008 &dri2ThrottleExtension
.base
,
2009 &dri2FenceExtension
.base
,
2010 &dri2InteropExtension
.base
,
2011 &dri2NoErrorExtension
.base
,
2015 static const __DRIextension
*dri_robust_screen_extensions
[] = {
2016 &driTexBufferExtension
.base
,
2017 &dri2FlushExtension
.base
,
2018 &dri2ImageExtension
.base
,
2019 &dri2RendererQueryExtension
.base
,
2020 &dri2GalliumConfigQueryExtension
.base
,
2021 &dri2ThrottleExtension
.base
,
2022 &dri2FenceExtension
.base
,
2023 &dri2InteropExtension
.base
,
2024 &dri2Robustness
.base
,
2025 &dri2NoErrorExtension
.base
,
2030 * This is the driver specific part of the createNewScreen entry point.
2032 * Returns the struct gl_config supported by this driver.
2034 static const __DRIconfig
**
2035 dri2_init_screen(__DRIscreen
* sPriv
)
2037 const __DRIconfig
**configs
;
2038 struct dri_screen
*screen
;
2039 struct pipe_screen
*pscreen
= NULL
;
2040 const struct drm_conf_ret
*throttle_ret
;
2041 const struct drm_conf_ret
*dmabuf_ret
;
2044 screen
= CALLOC_STRUCT(dri_screen
);
2048 screen
->sPriv
= sPriv
;
2049 screen
->fd
= sPriv
->fd
;
2050 (void) mtx_init(&screen
->opencl_func_mutex
, mtx_plain
);
2052 sPriv
->driverPrivate
= (void *)screen
;
2054 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2058 if (pipe_loader_drm_probe_fd(&screen
->dev
, fd
)) {
2059 struct pipe_screen_config config
= {};
2062 dri_init_options_get_screen_flags(screen
);
2064 pscreen
= pipe_loader_create_screen(screen
->dev
, &config
);
2070 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
2071 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
2073 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
2074 screen
->throttling_enabled
= TRUE
;
2075 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
2078 if (pscreen
->resource_create_with_modifiers
)
2079 dri2ImageExtension
.createImageWithModifiers
=
2080 dri2_create_image_with_modifiers
;
2082 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
2085 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2086 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2087 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2088 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2089 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2090 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2091 dri2ImageExtension
.queryDmaBufModifiers
=
2092 dri2_query_dma_buf_modifiers
;
2096 if (pscreen
->get_param(pscreen
, PIPE_CAP_DEVICE_RESET_STATUS_QUERY
)) {
2097 sPriv
->extensions
= dri_robust_screen_extensions
;
2098 screen
->has_reset_status_query
= true;
2101 sPriv
->extensions
= dri_screen_extensions
;
2103 configs
= dri_init_screen_helper(screen
, pscreen
);
2105 goto destroy_screen
;
2107 screen
->can_share_buffer
= true;
2108 screen
->auto_fake_front
= dri_with_format(sPriv
);
2109 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2110 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2115 dri_destroy_screen_helper(screen
);
2119 pipe_loader_release(&screen
->dev
, 1);
2129 * This is the driver specific part of the createNewScreen entry point.
2131 * Returns the struct gl_config supported by this driver.
2133 static const __DRIconfig
**
2134 dri_kms_init_screen(__DRIscreen
* sPriv
)
2136 #if defined(GALLIUM_SOFTPIPE)
2137 const __DRIconfig
**configs
;
2138 struct dri_screen
*screen
;
2139 struct pipe_screen
*pscreen
= NULL
;
2143 screen
= CALLOC_STRUCT(dri_screen
);
2147 screen
->sPriv
= sPriv
;
2148 screen
->fd
= sPriv
->fd
;
2150 sPriv
->driverPrivate
= (void *)screen
;
2152 if (screen
->fd
< 0 || (fd
= fcntl(screen
->fd
, F_DUPFD_CLOEXEC
, 3)) < 0)
2155 struct pipe_screen_config config
= {};
2157 config
.flags
= dri_init_options_get_screen_flags(screen
);
2159 if (pipe_loader_sw_probe_kms(&screen
->dev
, fd
))
2160 pscreen
= pipe_loader_create_screen(screen
->dev
, &config
);
2165 if (pscreen
->resource_create_with_modifiers
)
2166 dri2ImageExtension
.createImageWithModifiers
=
2167 dri2_create_image_with_modifiers
;
2169 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2170 (cap
& DRM_PRIME_CAP_IMPORT
)) {
2171 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
2172 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
2173 dri2ImageExtension
.createImageFromDmaBufs2
= dri2_from_dma_bufs2
;
2174 dri2ImageExtension
.queryDmaBufFormats
= dri2_query_dma_buf_formats
;
2175 dri2ImageExtension
.queryDmaBufModifiers
= dri2_query_dma_buf_modifiers
;
2178 sPriv
->extensions
= dri_screen_extensions
;
2180 configs
= dri_init_screen_helper(screen
, pscreen
);
2182 goto destroy_screen
;
2184 screen
->can_share_buffer
= false;
2185 screen
->auto_fake_front
= dri_with_format(sPriv
);
2186 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
2187 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
2192 dri_destroy_screen_helper(screen
);
2196 pipe_loader_release(&screen
->dev
, 1);
2202 #endif // GALLIUM_SOFTPIPE
2207 dri2_create_buffer(__DRIscreen
* sPriv
,
2208 __DRIdrawable
* dPriv
,
2209 const struct gl_config
* visual
, boolean isPixmap
)
2211 struct dri_drawable
*drawable
= NULL
;
2213 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
2216 drawable
= dPriv
->driverPrivate
;
2218 drawable
->allocate_textures
= dri2_allocate_textures
;
2219 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
2220 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
2226 * DRI driver virtual function table.
2228 * DRI versions differ in their implementation of init_screen and swap_buffers.
2230 const struct __DriverAPIRec galliumdrm_driver_api
= {
2231 .InitScreen
= dri2_init_screen
,
2232 .DestroyScreen
= dri_destroy_screen
,
2233 .CreateContext
= dri_create_context
,
2234 .DestroyContext
= dri_destroy_context
,
2235 .CreateBuffer
= dri2_create_buffer
,
2236 .DestroyBuffer
= dri_destroy_buffer
,
2237 .MakeCurrent
= dri_make_current
,
2238 .UnbindContext
= dri_unbind_context
,
2240 .AllocateBuffer
= dri2_allocate_buffer
,
2241 .ReleaseBuffer
= dri2_release_buffer
,
2245 * DRI driver virtual function table.
2247 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2248 * hook. The latter is used to explicitly initialise the kms_swrast driver
2249 * rather than selecting the approapriate driver as suggested by the loader.
2251 const struct __DriverAPIRec dri_kms_driver_api
= {
2252 .InitScreen
= dri_kms_init_screen
,
2253 .DestroyScreen
= dri_destroy_screen
,
2254 .CreateContext
= dri_create_context
,
2255 .DestroyContext
= dri_destroy_context
,
2256 .CreateBuffer
= dri2_create_buffer
,
2257 .DestroyBuffer
= dri_destroy_buffer
,
2258 .MakeCurrent
= dri_make_current
,
2259 .UnbindContext
= dri_unbind_context
,
2261 .AllocateBuffer
= dri2_allocate_buffer
,
2262 .ReleaseBuffer
= dri2_release_buffer
,
2265 /* This is the table of extensions that the loader will dlsym() for. */
2266 const __DRIextension
*galliumdrm_driver_extensions
[] = {
2267 &driCoreExtension
.base
,
2268 &driImageDriverExtension
.base
,
2269 &driDRI2Extension
.base
,
2270 &gallium_config_options
.base
,
2274 /* vim: set sw=3 ts=8 sts=3 expandtab: */