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 "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_format.h"
35 #include "util/u_debug.h"
36 #include "state_tracker/drm_driver.h"
37 #include "state_tracker/st_texture.h"
38 #include "state_tracker/st_context.h"
39 #include "pipe-loader/pipe_loader.h"
40 #include "main/texobj.h"
42 #include "dri_screen.h"
43 #include "dri_context.h"
44 #include "dri_drawable.h"
45 #include "dri2_buffer.h"
47 static int convert_fourcc(int format
, int *dri_components_p
)
51 case __DRI_IMAGE_FOURCC_RGB565
:
52 format
= __DRI_IMAGE_FORMAT_RGB565
;
53 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
55 case __DRI_IMAGE_FOURCC_ARGB8888
:
56 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
57 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
59 case __DRI_IMAGE_FOURCC_XRGB8888
:
60 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
61 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
63 case __DRI_IMAGE_FOURCC_ABGR8888
:
64 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
65 dri_components
= __DRI_IMAGE_COMPONENTS_RGBA
;
67 case __DRI_IMAGE_FOURCC_XBGR8888
:
68 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
69 dri_components
= __DRI_IMAGE_COMPONENTS_RGB
;
74 *dri_components_p
= dri_components
;
79 * DRI2 flush extension.
82 dri2_flush_drawable(__DRIdrawable
*dPriv
)
84 dri_flush(dPriv
->driContextPriv
, dPriv
, __DRI2_FLUSH_DRAWABLE
, -1);
88 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
90 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
92 dri2InvalidateDrawable(dPriv
);
93 drawable
->dPriv
->lastStamp
= drawable
->dPriv
->dri2
.stamp
;
95 p_atomic_inc(&drawable
->base
.stamp
);
98 static const __DRI2flushExtension dri2FlushExtension
= {
99 .base
= { __DRI2_FLUSH
, 4 },
101 .flush
= dri2_flush_drawable
,
102 .invalidate
= dri2_invalidate_drawable
,
103 .flush_with_flags
= dri_flush
,
107 * Retrieve __DRIbuffer from the DRI loader.
110 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
111 const enum st_attachment_type
*atts
,
114 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
115 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
117 __DRIbuffer
*buffers
;
119 unsigned attachments
[10];
120 unsigned num_attachments
, i
;
123 with_format
= dri_with_format(drawable
->sPriv
);
127 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
129 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
131 for (i
= 0; i
< *count
; i
++) {
132 enum pipe_format format
;
136 dri_drawable_get_format(drawable
, atts
[i
], &format
, &bind
);
137 if (format
== PIPE_FORMAT_NONE
)
141 case ST_ATTACHMENT_FRONT_LEFT
:
145 att
= __DRI_BUFFER_FRONT_LEFT
;
147 case ST_ATTACHMENT_BACK_LEFT
:
148 att
= __DRI_BUFFER_BACK_LEFT
;
150 case ST_ATTACHMENT_FRONT_RIGHT
:
151 att
= __DRI_BUFFER_FRONT_RIGHT
;
153 case ST_ATTACHMENT_BACK_RIGHT
:
154 att
= __DRI_BUFFER_BACK_RIGHT
;
161 * In this switch statement we must support all formats that
162 * may occur as the stvis->color_format.
165 case PIPE_FORMAT_B8G8R8A8_UNORM
:
168 case PIPE_FORMAT_B8G8R8X8_UNORM
:
171 case PIPE_FORMAT_B5G6R5_UNORM
:
175 depth
= util_format_get_blocksizebits(format
);
176 assert(!"Unexpected format in dri2_drawable_get_buffers()");
179 attachments
[num_attachments
++] = att
;
181 attachments
[num_attachments
++] = depth
;
186 num_attachments
/= 2;
187 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
188 &dri_drawable
->w
, &dri_drawable
->h
,
189 attachments
, num_attachments
,
190 &num_buffers
, dri_drawable
->loaderPrivate
);
193 buffers
= loader
->getBuffers(dri_drawable
,
194 &dri_drawable
->w
, &dri_drawable
->h
,
195 attachments
, num_attachments
,
196 &num_buffers
, dri_drawable
->loaderPrivate
);
200 *count
= num_buffers
;
206 dri_image_drawable_get_buffers(struct dri_drawable
*drawable
,
207 struct __DRIimageList
*images
,
208 const enum st_attachment_type
*statts
,
209 unsigned statts_count
)
211 __DRIdrawable
*dPriv
= drawable
->dPriv
;
212 __DRIscreen
*sPriv
= drawable
->sPriv
;
213 unsigned int image_format
= __DRI_IMAGE_FORMAT_NONE
;
215 uint32_t buffer_mask
= 0;
218 for (i
= 0; i
< statts_count
; i
++) {
219 dri_drawable_get_format(drawable
, statts
[i
], &pf
, &bind
);
220 if (pf
== PIPE_FORMAT_NONE
)
224 case ST_ATTACHMENT_FRONT_LEFT
:
225 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
227 case ST_ATTACHMENT_BACK_LEFT
:
228 buffer_mask
|= __DRI_IMAGE_BUFFER_BACK
;
235 case PIPE_FORMAT_B5G6R5_UNORM
:
236 image_format
= __DRI_IMAGE_FORMAT_RGB565
;
238 case PIPE_FORMAT_B8G8R8X8_UNORM
:
239 image_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
241 case PIPE_FORMAT_B8G8R8A8_UNORM
:
242 image_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
244 case PIPE_FORMAT_R8G8B8A8_UNORM
:
245 image_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
248 image_format
= __DRI_IMAGE_FORMAT_NONE
;
253 return (*sPriv
->image
.loader
->getBuffers
) (dPriv
, image_format
,
254 (uint32_t *) &drawable
->base
.stamp
,
255 dPriv
->loaderPrivate
, buffer_mask
,
260 dri2_allocate_buffer(__DRIscreen
*sPriv
,
261 unsigned attachment
, unsigned format
,
262 int width
, int height
)
264 struct dri_screen
*screen
= dri_screen(sPriv
);
265 struct dri2_buffer
*buffer
;
266 struct pipe_resource templ
;
269 struct winsys_handle whandle
;
271 switch (attachment
) {
272 case __DRI_BUFFER_FRONT_LEFT
:
273 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
274 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
276 case __DRI_BUFFER_BACK_LEFT
:
277 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
279 case __DRI_BUFFER_DEPTH
:
280 case __DRI_BUFFER_DEPTH_STENCIL
:
281 case __DRI_BUFFER_STENCIL
:
282 bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
286 /* because we get the handle and stride */
287 bind
|= PIPE_BIND_SHARED
;
291 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
294 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
297 pf
= PIPE_FORMAT_Z16_UNORM
;
303 buffer
= CALLOC_STRUCT(dri2_buffer
);
307 memset(&templ
, 0, sizeof(templ
));
310 templ
.target
= PIPE_TEXTURE_2D
;
311 templ
.last_level
= 0;
312 templ
.width0
= width
;
313 templ
.height0
= height
;
315 templ
.array_size
= 1;
318 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
319 if (!buffer
->resource
) {
324 memset(&whandle
, 0, sizeof(whandle
));
325 if (screen
->can_share_buffer
)
326 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
328 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
330 screen
->base
.screen
->resource_get_handle(screen
->base
.screen
,
331 buffer
->resource
, &whandle
);
333 buffer
->base
.attachment
= attachment
;
334 buffer
->base
.name
= whandle
.handle
;
335 buffer
->base
.cpp
= util_format_get_blocksize(pf
);
336 buffer
->base
.pitch
= whandle
.stride
;
338 return &buffer
->base
;
342 dri2_release_buffer(__DRIscreen
*sPriv
, __DRIbuffer
*bPriv
)
344 struct dri2_buffer
*buffer
= dri2_buffer(bPriv
);
346 pipe_resource_reference(&buffer
->resource
, NULL
);
351 * Backend functions for st_framebuffer interface.
355 dri2_allocate_textures(struct dri_context
*ctx
,
356 struct dri_drawable
*drawable
,
357 const enum st_attachment_type
*statts
,
358 unsigned statts_count
)
360 __DRIscreen
*sPriv
= drawable
->sPriv
;
361 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
362 struct dri_screen
*screen
= dri_screen(sPriv
);
363 struct pipe_resource templ
;
364 boolean alloc_depthstencil
= FALSE
;
366 const __DRIimageLoaderExtension
*image
= sPriv
->image
.loader
;
367 /* Image specific variables */
368 struct __DRIimageList images
;
369 /* Dri2 specific variables */
370 __DRIbuffer
*buffers
;
371 struct winsys_handle whandle
;
372 unsigned num_buffers
= statts_count
;
374 /* First get the buffers from the loader */
376 if (!dri_image_drawable_get_buffers(drawable
, &images
,
377 statts
, statts_count
))
381 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
382 if (!buffers
|| (drawable
->old_num
== num_buffers
&&
383 drawable
->old_w
== dri_drawable
->w
&&
384 drawable
->old_h
== dri_drawable
->h
&&
385 memcmp(drawable
->old
, buffers
,
386 sizeof(__DRIbuffer
) * num_buffers
) == 0))
390 /* Second clean useless resources*/
392 /* See if we need a depth-stencil buffer. */
393 for (i
= 0; i
< statts_count
; i
++) {
394 if (statts
[i
] == ST_ATTACHMENT_DEPTH_STENCIL
) {
395 alloc_depthstencil
= TRUE
;
400 /* Delete the resources we won't need. */
401 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
402 /* Don't delete the depth-stencil buffer, we can reuse it. */
403 if (i
== ST_ATTACHMENT_DEPTH_STENCIL
&& alloc_depthstencil
)
406 /* Flush the texture before unreferencing, so that other clients can
407 * see what the driver has rendered.
409 if (i
!= ST_ATTACHMENT_DEPTH_STENCIL
&& drawable
->textures
[i
]) {
410 struct pipe_context
*pipe
= ctx
->st
->pipe
;
411 pipe
->flush_resource(pipe
, drawable
->textures
[i
]);
414 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
417 if (drawable
->stvis
.samples
> 1) {
418 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
421 /* Don't delete MSAA resources for the attachments which are enabled,
422 * we can reuse them. */
423 for (j
= 0; j
< statts_count
; j
++) {
424 if (i
== statts
[j
]) {
431 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
436 /* Third use the buffers retrieved to fill the drawable info */
438 memset(&templ
, 0, sizeof(templ
));
439 templ
.target
= screen
->target
;
440 templ
.last_level
= 0;
442 templ
.array_size
= 1;
445 if (images
.image_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
446 struct pipe_resource
**buf
=
447 &drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
448 struct pipe_resource
*texture
= images
.front
->texture
;
450 dri_drawable
->w
= texture
->width0
;
451 dri_drawable
->h
= texture
->height0
;
453 pipe_resource_reference(buf
, texture
);
456 if (images
.image_mask
& __DRI_IMAGE_BUFFER_BACK
) {
457 struct pipe_resource
**buf
=
458 &drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
];
459 struct pipe_resource
*texture
= images
.back
->texture
;
461 dri_drawable
->w
= texture
->width0
;
462 dri_drawable
->h
= texture
->height0
;
464 pipe_resource_reference(buf
, texture
);
467 /* Note: if there is both a back and a front buffer,
468 * then they have the same size.
470 templ
.width0
= dri_drawable
->w
;
471 templ
.height0
= dri_drawable
->h
;
474 memset(&whandle
, 0, sizeof(whandle
));
476 /* Process DRI-provided buffers and get pipe_resources. */
477 for (i
= 0; i
< num_buffers
; i
++) {
478 __DRIbuffer
*buf
= &buffers
[i
];
479 enum st_attachment_type statt
;
480 enum pipe_format format
;
482 switch (buf
->attachment
) {
483 case __DRI_BUFFER_FRONT_LEFT
:
484 if (!screen
->auto_fake_front
) {
485 continue; /* invalid attachment */
488 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
489 statt
= ST_ATTACHMENT_FRONT_LEFT
;
491 case __DRI_BUFFER_BACK_LEFT
:
492 statt
= ST_ATTACHMENT_BACK_LEFT
;
495 continue; /* invalid attachment */
498 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
499 if (format
== PIPE_FORMAT_NONE
)
502 /* dri2_drawable_get_buffers has already filled dri_drawable->w
503 * and dri_drawable->h */
504 templ
.width0
= dri_drawable
->w
;
505 templ
.height0
= dri_drawable
->h
;
506 templ
.format
= format
;
508 whandle
.handle
= buf
->name
;
509 whandle
.stride
= buf
->pitch
;
510 if (screen
->can_share_buffer
)
511 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
513 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
514 drawable
->textures
[statt
] =
515 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
517 assert(drawable
->textures
[statt
]);
521 /* Allocate private MSAA colorbuffers. */
522 if (drawable
->stvis
.samples
> 1) {
523 for (i
= 0; i
< statts_count
; i
++) {
524 enum st_attachment_type statt
= statts
[i
];
526 if (statt
== ST_ATTACHMENT_DEPTH_STENCIL
)
529 if (drawable
->textures
[statt
]) {
530 templ
.format
= drawable
->textures
[statt
]->format
;
531 templ
.bind
= drawable
->textures
[statt
]->bind
;
532 templ
.nr_samples
= drawable
->stvis
.samples
;
534 /* Try to reuse the resource.
535 * (the other resource parameters should be constant)
537 if (!drawable
->msaa_textures
[statt
] ||
538 drawable
->msaa_textures
[statt
]->width0
!= templ
.width0
||
539 drawable
->msaa_textures
[statt
]->height0
!= templ
.height0
) {
540 /* Allocate a new one. */
541 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
543 drawable
->msaa_textures
[statt
] =
544 screen
->base
.screen
->resource_create(screen
->base
.screen
,
546 assert(drawable
->msaa_textures
[statt
]);
548 /* If there are any MSAA resources, we should initialize them
549 * such that they contain the same data as the single-sample
550 * resources we just got from the X server.
552 * The reason for this is that the state tracker (and
553 * therefore the app) can access the MSAA resources only.
554 * The single-sample resources are not exposed
555 * to the state tracker.
558 dri_pipe_blit(ctx
->st
->pipe
,
559 drawable
->msaa_textures
[statt
],
560 drawable
->textures
[statt
]);
564 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
569 /* Allocate a private depth-stencil buffer. */
570 if (alloc_depthstencil
) {
571 enum st_attachment_type statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
572 struct pipe_resource
**zsbuf
;
573 enum pipe_format format
;
576 dri_drawable_get_format(drawable
, statt
, &format
, &bind
);
579 templ
.format
= format
;
582 if (drawable
->stvis
.samples
> 1) {
583 templ
.nr_samples
= drawable
->stvis
.samples
;
584 zsbuf
= &drawable
->msaa_textures
[statt
];
587 templ
.nr_samples
= 0;
588 zsbuf
= &drawable
->textures
[statt
];
591 /* Try to reuse the resource.
592 * (the other resource parameters should be constant)
595 (*zsbuf
)->width0
!= templ
.width0
||
596 (*zsbuf
)->height0
!= templ
.height0
) {
597 /* Allocate a new one. */
598 pipe_resource_reference(zsbuf
, NULL
);
599 *zsbuf
= screen
->base
.screen
->resource_create(screen
->base
.screen
,
605 pipe_resource_reference(&drawable
->msaa_textures
[statt
], NULL
);
606 pipe_resource_reference(&drawable
->textures
[statt
], NULL
);
610 /* For DRI2, we may get the same buffers again from the server.
611 * To prevent useless imports of gem names, drawable->old* is used
612 * to bypass the import if we get the same buffers. This doesn't apply
613 * to DRI3/Wayland, users of image.loader, since the buffer is managed
614 * by the client (no import), and the back buffer is going to change
618 drawable
->old_num
= num_buffers
;
619 drawable
->old_w
= dri_drawable
->w
;
620 drawable
->old_h
= dri_drawable
->h
;
621 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * num_buffers
);
626 dri2_flush_frontbuffer(struct dri_context
*ctx
,
627 struct dri_drawable
*drawable
,
628 enum st_attachment_type statt
)
630 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
631 const __DRIimageLoaderExtension
*image
= drawable
->sPriv
->image
.loader
;
632 const __DRIdri2LoaderExtension
*loader
= drawable
->sPriv
->dri2
.loader
;
633 struct pipe_context
*pipe
= ctx
->st
->pipe
;
635 if (statt
!= ST_ATTACHMENT_FRONT_LEFT
)
638 if (drawable
->stvis
.samples
> 1) {
639 /* Resolve the front buffer. */
640 dri_pipe_blit(ctx
->st
->pipe
,
641 drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
],
642 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
]);
645 if (drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]) {
646 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
]);
649 pipe
->flush(pipe
, NULL
, 0);
652 image
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
654 else if (loader
->flushFrontBuffer
) {
655 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
660 dri2_update_tex_buffer(struct dri_drawable
*drawable
,
661 struct dri_context
*ctx
,
662 struct pipe_resource
*res
)
668 dri2_lookup_egl_image(struct dri_screen
*screen
, void *handle
)
670 const __DRIimageLookupExtension
*loader
= screen
->sPriv
->dri2
.image
;
673 if (!loader
->lookupEGLImage
)
676 img
= loader
->lookupEGLImage(screen
->sPriv
,
677 handle
, screen
->sPriv
->loaderPrivate
);
683 dri2_create_image_from_winsys(__DRIscreen
*_screen
,
684 int width
, int height
, int format
,
685 struct winsys_handle
*whandle
, int pitch
,
688 struct dri_screen
*screen
= dri_screen(_screen
);
690 struct pipe_resource templ
;
694 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
697 case __DRI_IMAGE_FORMAT_RGB565
:
698 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
700 case __DRI_IMAGE_FORMAT_XRGB8888
:
701 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
703 case __DRI_IMAGE_FORMAT_ARGB8888
:
704 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
706 case __DRI_IMAGE_FORMAT_ABGR8888
:
707 pf
= PIPE_FORMAT_R8G8B8A8_UNORM
;
710 pf
= PIPE_FORMAT_NONE
;
713 if (pf
== PIPE_FORMAT_NONE
)
716 img
= CALLOC_STRUCT(__DRIimageRec
);
720 memset(&templ
, 0, sizeof(templ
));
721 templ
.bind
= tex_usage
;
723 templ
.target
= screen
->target
;
724 templ
.last_level
= 0;
725 templ
.width0
= width
;
726 templ
.height0
= height
;
728 templ
.array_size
= 1;
730 whandle
->stride
= pitch
* util_format_get_blocksize(pf
);
732 img
->texture
= screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
741 img
->dri_format
= format
;
742 img
->loader_private
= loaderPrivate
;
748 dri2_create_image_from_name(__DRIscreen
*_screen
,
749 int width
, int height
, int format
,
750 int name
, int pitch
, void *loaderPrivate
)
752 struct winsys_handle whandle
;
754 memset(&whandle
, 0, sizeof(whandle
));
755 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
756 whandle
.handle
= name
;
758 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
759 &whandle
, pitch
, loaderPrivate
);
763 dri2_create_image_from_fd(__DRIscreen
*_screen
,
764 int width
, int height
, int format
,
765 int fd
, int pitch
, void *loaderPrivate
)
767 struct winsys_handle whandle
;
772 memset(&whandle
, 0, sizeof(whandle
));
773 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
774 whandle
.handle
= (unsigned)fd
;
776 return dri2_create_image_from_winsys(_screen
, width
, height
, format
,
777 &whandle
, pitch
, loaderPrivate
);
781 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
782 int renderbuffer
, void *loaderPrivate
)
784 struct dri_context
*ctx
= dri_context(context
);
786 if (!ctx
->st
->get_resource_for_egl_image
)
794 dri2_create_image(__DRIscreen
*_screen
,
795 int width
, int height
, int format
,
796 unsigned int use
, void *loaderPrivate
)
798 struct dri_screen
*screen
= dri_screen(_screen
);
800 struct pipe_resource templ
;
804 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
805 if (use
& __DRI_IMAGE_USE_SCANOUT
)
806 tex_usage
|= PIPE_BIND_SCANOUT
;
807 if (use
& __DRI_IMAGE_USE_SHARE
)
808 tex_usage
|= PIPE_BIND_SHARED
;
809 if (use
& __DRI_IMAGE_USE_LINEAR
)
810 tex_usage
|= PIPE_BIND_LINEAR
;
811 if (use
& __DRI_IMAGE_USE_CURSOR
) {
812 if (width
!= 64 || height
!= 64)
814 tex_usage
|= PIPE_BIND_CURSOR
;
818 case __DRI_IMAGE_FORMAT_RGB565
:
819 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
821 case __DRI_IMAGE_FORMAT_XRGB8888
:
822 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
824 case __DRI_IMAGE_FORMAT_ARGB8888
:
825 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
827 case __DRI_IMAGE_FORMAT_ABGR8888
:
828 pf
= PIPE_FORMAT_R8G8B8A8_UNORM
;
831 pf
= PIPE_FORMAT_NONE
;
834 if (pf
== PIPE_FORMAT_NONE
)
837 img
= CALLOC_STRUCT(__DRIimageRec
);
841 memset(&templ
, 0, sizeof(templ
));
842 templ
.bind
= tex_usage
;
844 templ
.target
= PIPE_TEXTURE_2D
;
845 templ
.last_level
= 0;
846 templ
.width0
= width
;
847 templ
.height0
= height
;
849 templ
.array_size
= 1;
851 img
->texture
= screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
859 img
->dri_format
= format
;
860 img
->dri_components
= 0;
862 img
->loader_private
= loaderPrivate
;
867 dri2_query_image(__DRIimage
*image
, int attrib
, int *value
)
869 struct winsys_handle whandle
;
870 memset(&whandle
, 0, sizeof(whandle
));
873 case __DRI_IMAGE_ATTRIB_STRIDE
:
874 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
875 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
876 image
->texture
, &whandle
);
877 *value
= whandle
.stride
;
879 case __DRI_IMAGE_ATTRIB_HANDLE
:
880 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
881 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
882 image
->texture
, &whandle
);
883 *value
= whandle
.handle
;
885 case __DRI_IMAGE_ATTRIB_NAME
:
886 whandle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
887 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
888 image
->texture
, &whandle
);
889 *value
= whandle
.handle
;
891 case __DRI_IMAGE_ATTRIB_FD
:
892 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
893 image
->texture
->screen
->resource_get_handle(image
->texture
->screen
,
894 image
->texture
, &whandle
);
895 *value
= whandle
.handle
;
897 case __DRI_IMAGE_ATTRIB_FORMAT
:
898 *value
= image
->dri_format
;
900 case __DRI_IMAGE_ATTRIB_WIDTH
:
901 *value
= image
->texture
->width0
;
903 case __DRI_IMAGE_ATTRIB_HEIGHT
:
904 *value
= image
->texture
->height0
;
906 case __DRI_IMAGE_ATTRIB_COMPONENTS
:
907 if (image
->dri_components
== 0)
909 *value
= image
->dri_components
;
917 dri2_dup_image(__DRIimage
*image
, void *loaderPrivate
)
921 img
= CALLOC_STRUCT(__DRIimageRec
);
926 pipe_resource_reference(&img
->texture
, image
->texture
);
927 img
->level
= image
->level
;
928 img
->layer
= image
->layer
;
929 img
->dri_format
= image
->dri_format
;
930 /* This should be 0 for sub images, but dup is also used for base images. */
931 img
->dri_components
= image
->dri_components
;
932 img
->loader_private
= loaderPrivate
;
938 dri2_validate_usage(__DRIimage
*image
, unsigned int use
)
941 * Gallium drivers are bad at adding usages to the resources
942 * once opened again in another process, which is the main use
943 * case for this, so we have to lie.
952 dri2_from_names(__DRIscreen
*screen
, int width
, int height
, int format
,
953 int *names
, int num_names
, int *strides
, int *offsets
,
957 int stride
, dri_components
;
964 format
= convert_fourcc(format
, &dri_components
);
968 /* Strides are in bytes not pixels. */
969 stride
= strides
[0] /4;
971 img
= dri2_create_image_from_name(screen
, width
, height
, format
,
972 names
[0], stride
, loaderPrivate
);
976 img
->dri_components
= dri_components
;
981 dri2_from_planar(__DRIimage
*image
, int plane
, void *loaderPrivate
)
988 if (image
->dri_components
== 0)
991 img
= dri2_dup_image(image
, loaderPrivate
);
995 /* set this to 0 for sub images. */
996 img
->dri_components
= 0;
1001 dri2_create_from_texture(__DRIcontext
*context
, int target
, unsigned texture
,
1002 int depth
, int level
, unsigned *error
,
1003 void *loaderPrivate
)
1006 struct gl_context
*ctx
= ((struct st_context
*)dri_context(context
)->st
)->ctx
;
1007 struct gl_texture_object
*obj
;
1008 struct pipe_resource
*tex
;
1011 obj
= _mesa_lookup_texture(ctx
, texture
);
1012 if (!obj
|| obj
->Target
!= target
) {
1013 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1017 tex
= st_get_texobj_resource(obj
);
1019 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1023 if (target
== GL_TEXTURE_CUBE_MAP
)
1026 _mesa_test_texobj_completeness(ctx
, obj
);
1027 if (!obj
->_BaseComplete
|| (level
> 0 && !obj
->_MipmapComplete
)) {
1028 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1032 if (level
< obj
->BaseLevel
|| level
> obj
->_MaxLevel
) {
1033 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1037 if (target
== GL_TEXTURE_3D
&& obj
->Image
[face
][level
]->Depth
< depth
) {
1038 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1042 img
= CALLOC_STRUCT(__DRIimageRec
);
1044 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1050 img
->dri_format
= driGLFormatToImageFormat(obj
->Image
[face
][level
]->TexFormat
);
1052 img
->loader_private
= loaderPrivate
;
1054 if (img
->dri_format
== __DRI_IMAGE_FORMAT_NONE
) {
1055 *error
= __DRI_IMAGE_ERROR_BAD_PARAMETER
;
1060 pipe_resource_reference(&img
->texture
, tex
);
1062 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1067 dri2_from_fds(__DRIscreen
*screen
, int width
, int height
, int fourcc
,
1068 int *fds
, int num_fds
, int *strides
, int *offsets
,
1069 void *loaderPrivate
)
1072 int format
, stride
, dri_components
;
1076 if (offsets
[0] != 0)
1079 format
= convert_fourcc(fourcc
, &dri_components
);
1083 /* Strides are in bytes not pixels. */
1084 stride
= strides
[0] /4;
1086 img
= dri2_create_image_from_fd(screen
, width
, height
, format
,
1087 fds
[0], stride
, loaderPrivate
);
1091 img
->dri_components
= dri_components
;
1096 dri2_from_dma_bufs(__DRIscreen
*screen
,
1097 int width
, int height
, int fourcc
,
1098 int *fds
, int num_fds
,
1099 int *strides
, int *offsets
,
1100 enum __DRIYUVColorSpace yuv_color_space
,
1101 enum __DRISampleRange sample_range
,
1102 enum __DRIChromaSiting horizontal_siting
,
1103 enum __DRIChromaSiting vertical_siting
,
1105 void *loaderPrivate
)
1108 int format
, stride
, dri_components
;
1110 if (num_fds
!= 1 || offsets
[0] != 0) {
1111 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1115 format
= convert_fourcc(fourcc
, &dri_components
);
1117 *error
= __DRI_IMAGE_ERROR_BAD_MATCH
;
1121 /* Strides are in bytes not pixels. */
1122 stride
= strides
[0] /4;
1124 img
= dri2_create_image_from_fd(screen
, width
, height
, format
,
1125 fds
[0], stride
, loaderPrivate
);
1127 *error
= __DRI_IMAGE_ERROR_BAD_ALLOC
;
1131 img
->yuv_color_space
= yuv_color_space
;
1132 img
->sample_range
= sample_range
;
1133 img
->horizontal_siting
= horizontal_siting
;
1134 img
->vertical_siting
= vertical_siting
;
1135 img
->dri_components
= dri_components
;
1137 *error
= __DRI_IMAGE_ERROR_SUCCESS
;
1142 dri2_blit_image(__DRIcontext
*context
, __DRIimage
*dst
, __DRIimage
*src
,
1143 int dstx0
, int dsty0
, int dstwidth
, int dstheight
,
1144 int srcx0
, int srcy0
, int srcwidth
, int srcheight
,
1147 struct dri_context
*ctx
= dri_context(context
);
1148 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1149 struct pipe_screen
*screen
;
1150 struct pipe_fence_handle
*fence
;
1151 struct pipe_blit_info blit
;
1156 memset(&blit
, 0, sizeof(blit
));
1157 blit
.dst
.resource
= dst
->texture
;
1158 blit
.dst
.box
.x
= dstx0
;
1159 blit
.dst
.box
.y
= dsty0
;
1160 blit
.dst
.box
.width
= dstwidth
;
1161 blit
.dst
.box
.height
= dstheight
;
1162 blit
.dst
.box
.depth
= 1;
1163 blit
.dst
.format
= dst
->texture
->format
;
1164 blit
.src
.resource
= src
->texture
;
1165 blit
.src
.box
.x
= srcx0
;
1166 blit
.src
.box
.y
= srcy0
;
1167 blit
.src
.box
.width
= srcwidth
;
1168 blit
.src
.box
.height
= srcheight
;
1169 blit
.src
.box
.depth
= 1;
1170 blit
.src
.format
= src
->texture
->format
;
1171 blit
.mask
= PIPE_MASK_RGBA
;
1172 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1174 pipe
->blit(pipe
, &blit
);
1176 if (flush_flag
== __BLIT_FLAG_FLUSH
) {
1177 pipe
->flush_resource(pipe
, dst
->texture
);
1178 ctx
->st
->flush(ctx
->st
, 0, NULL
);
1179 } else if (flush_flag
== __BLIT_FLAG_FINISH
) {
1180 screen
= dri_screen(ctx
->sPriv
)->base
.screen
;
1181 pipe
->flush_resource(pipe
, dst
->texture
);
1182 ctx
->st
->flush(ctx
->st
, 0, &fence
);
1183 (void) screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
1184 screen
->fence_reference(screen
, &fence
, NULL
);
1189 dri2_destroy_image(__DRIimage
*img
)
1191 pipe_resource_reference(&img
->texture
, NULL
);
1196 dri2_get_capabilities(__DRIscreen
*_screen
)
1198 struct dri_screen
*screen
= dri_screen(_screen
);
1200 return (screen
->can_share_buffer
? __DRI_IMAGE_CAP_GLOBAL_NAMES
: 0);
1203 /* The extension is modified during runtime if DRI_PRIME is detected */
1204 static __DRIimageExtension dri2ImageExtension
= {
1205 .base
= { __DRI_IMAGE
, 10 },
1207 .createImageFromName
= dri2_create_image_from_name
,
1208 .createImageFromRenderbuffer
= dri2_create_image_from_renderbuffer
,
1209 .destroyImage
= dri2_destroy_image
,
1210 .createImage
= dri2_create_image
,
1211 .queryImage
= dri2_query_image
,
1212 .dupImage
= dri2_dup_image
,
1213 .validateUsage
= dri2_validate_usage
,
1214 .createImageFromNames
= dri2_from_names
,
1215 .fromPlanar
= dri2_from_planar
,
1216 .createImageFromTexture
= dri2_create_from_texture
,
1217 .createImageFromFds
= NULL
,
1218 .createImageFromDmaBufs
= NULL
,
1219 .blitImage
= dri2_blit_image
,
1220 .getCapabilities
= dri2_get_capabilities
,
1224 * Backend function init_screen.
1227 static const __DRIextension
*dri_screen_extensions
[] = {
1228 &driTexBufferExtension
.base
,
1229 &dri2FlushExtension
.base
,
1230 &dri2ImageExtension
.base
,
1231 &dri2ConfigQueryExtension
.base
,
1232 &dri2ThrottleExtension
.base
,
1237 * This is the driver specific part of the createNewScreen entry point.
1239 * Returns the struct gl_config supported by this driver.
1241 static const __DRIconfig
**
1242 dri2_init_screen(__DRIscreen
* sPriv
)
1244 const __DRIconfig
**configs
;
1245 struct dri_screen
*screen
;
1246 struct pipe_screen
*pscreen
= NULL
;
1247 const struct drm_conf_ret
*throttle_ret
= NULL
;
1248 const struct drm_conf_ret
*dmabuf_ret
= NULL
;
1250 screen
= CALLOC_STRUCT(dri_screen
);
1254 screen
->sPriv
= sPriv
;
1255 screen
->fd
= sPriv
->fd
;
1257 sPriv
->driverPrivate
= (void *)screen
;
1259 #if GALLIUM_STATIC_TARGETS
1260 pscreen
= dd_create_screen(screen
->fd
);
1262 throttle_ret
= dd_configuration(DRM_CONF_THROTTLE
);
1263 dmabuf_ret
= dd_configuration(DRM_CONF_SHARE_FD
);
1265 if (pipe_loader_drm_probe_fd(&screen
->dev
, screen
->fd
, true)) {
1266 pscreen
= pipe_loader_create_screen(screen
->dev
, PIPE_SEARCH_DIR
);
1268 throttle_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_THROTTLE
);
1269 dmabuf_ret
= pipe_loader_configuration(screen
->dev
, DRM_CONF_SHARE_FD
);
1271 #endif // GALLIUM_STATIC_TARGETS
1273 if (throttle_ret
&& throttle_ret
->val
.val_int
!= -1) {
1274 screen
->throttling_enabled
= TRUE
;
1275 screen
->default_throttle_frames
= throttle_ret
->val
.val_int
;
1278 if (dmabuf_ret
&& dmabuf_ret
->val
.val_bool
) {
1281 if (drmGetCap(sPriv
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
1282 (cap
& DRM_PRIME_CAP_IMPORT
)) {
1283 dri2ImageExtension
.createImageFromFds
= dri2_from_fds
;
1284 dri2ImageExtension
.createImageFromDmaBufs
= dri2_from_dma_bufs
;
1288 sPriv
->extensions
= dri_screen_extensions
;
1290 /* dri_init_screen_helper checks pscreen for us */
1292 #if GALLIUM_STATIC_TARGETS
1293 configs
= dri_init_screen_helper(screen
, pscreen
, dd_driver_name());
1295 configs
= dri_init_screen_helper(screen
, pscreen
, screen
->dev
->driver_name
);
1296 #endif // GALLIUM_STATIC_TARGETS
1300 screen
->can_share_buffer
= true;
1301 screen
->auto_fake_front
= dri_with_format(sPriv
);
1302 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1303 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1307 dri_destroy_screen_helper(screen
);
1308 #if !GALLIUM_STATIC_TARGETS
1310 pipe_loader_release(&screen
->dev
, 1);
1311 #endif // !GALLIUM_STATIC_TARGETS
1317 * This is the driver specific part of the createNewScreen entry point.
1319 * Returns the struct gl_config supported by this driver.
1321 static const __DRIconfig
**
1322 dri_kms_init_screen(__DRIscreen
* sPriv
)
1324 #if GALLIUM_STATIC_TARGETS
1325 const __DRIconfig
**configs
;
1326 struct dri_screen
*screen
;
1327 struct pipe_screen
*pscreen
= NULL
;
1329 screen
= CALLOC_STRUCT(dri_screen
);
1333 screen
->sPriv
= sPriv
;
1334 screen
->fd
= sPriv
->fd
;
1336 sPriv
->driverPrivate
= (void *)screen
;
1338 pscreen
= kms_swrast_create_screen(screen
->fd
);
1339 sPriv
->extensions
= dri_screen_extensions
;
1341 /* dri_init_screen_helper checks pscreen for us */
1342 configs
= dri_init_screen_helper(screen
, pscreen
, "swrast");
1346 screen
->can_share_buffer
= false;
1347 screen
->auto_fake_front
= dri_with_format(sPriv
);
1348 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
1349 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
1353 dri_destroy_screen_helper(screen
);
1355 #endif // GALLIUM_STATIC_TARGETS
1360 dri2_create_buffer(__DRIscreen
* sPriv
,
1361 __DRIdrawable
* dPriv
,
1362 const struct gl_config
* visual
, boolean isPixmap
)
1364 struct dri_drawable
*drawable
= NULL
;
1366 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
1369 drawable
= dPriv
->driverPrivate
;
1371 drawable
->allocate_textures
= dri2_allocate_textures
;
1372 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
1373 drawable
->update_tex_buffer
= dri2_update_tex_buffer
;
1379 * DRI driver virtual function table.
1381 * DRI versions differ in their implementation of init_screen and swap_buffers.
1383 const struct __DriverAPIRec galliumdrm_driver_api
= {
1384 .InitScreen
= dri2_init_screen
,
1385 .DestroyScreen
= dri_destroy_screen
,
1386 .CreateContext
= dri_create_context
,
1387 .DestroyContext
= dri_destroy_context
,
1388 .CreateBuffer
= dri2_create_buffer
,
1389 .DestroyBuffer
= dri_destroy_buffer
,
1390 .MakeCurrent
= dri_make_current
,
1391 .UnbindContext
= dri_unbind_context
,
1393 .AllocateBuffer
= dri2_allocate_buffer
,
1394 .ReleaseBuffer
= dri2_release_buffer
,
1398 * DRI driver virtual function table.
1400 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1401 * hook. The latter is used to explicitly initialise the kms_swrast driver
1402 * rather than selecting the approapriate driver as suggested by the loader.
1404 const struct __DriverAPIRec dri_kms_driver_api
= {
1405 .InitScreen
= dri_kms_init_screen
,
1406 .DestroyScreen
= dri_destroy_screen
,
1407 .CreateContext
= dri_create_context
,
1408 .DestroyContext
= dri_destroy_context
,
1409 .CreateBuffer
= dri2_create_buffer
,
1410 .DestroyBuffer
= dri_destroy_buffer
,
1411 .MakeCurrent
= dri_make_current
,
1412 .UnbindContext
= dri_unbind_context
,
1414 .AllocateBuffer
= dri2_allocate_buffer
,
1415 .ReleaseBuffer
= dri2_release_buffer
,
1418 /* This is the table of extensions that the loader will dlsym() for. */
1419 const __DRIextension
*galliumdrm_driver_extensions
[] = {
1420 &driCoreExtension
.base
,
1421 &driImageDriverExtension
.base
,
1422 &driDRI2Extension
.base
,
1423 &gallium_config_options
.base
,
1427 /* vim: set sw=3 ts=8 sts=3 expandtab: */