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 "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "util/u_format.h"
36 #include "util/u_debug.h"
37 #include "state_tracker/drm_driver.h"
38 #include "state_tracker/st_texture.h"
39 #include "state_tracker/st_context.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "main/texobj.h"
43 #include "dri_screen.h"
44 #include "dri_context.h"
45 #include "dri_drawable.h"
46 #include "dri_query_renderer.h"
47 #include "dri2_buffer.h"
49 static int convert_fourcc(int format, int *dri_components_p)
53 case __DRI_IMAGE_FOURCC_RGB565:
54 format = __DRI_IMAGE_FORMAT_RGB565;
55 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
57 case __DRI_IMAGE_FOURCC_ARGB8888:
58 format = __DRI_IMAGE_FORMAT_ARGB8888;
59 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
61 case __DRI_IMAGE_FOURCC_XRGB8888:
62 format = __DRI_IMAGE_FORMAT_XRGB8888;
63 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
65 case __DRI_IMAGE_FOURCC_ABGR8888:
66 format = __DRI_IMAGE_FORMAT_ABGR8888;
67 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
69 case __DRI_IMAGE_FOURCC_XBGR8888:
70 format = __DRI_IMAGE_FORMAT_XBGR8888;
71 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
76 *dri_components_p = dri_components;
80 static int convert_to_fourcc(int format)
83 case __DRI_IMAGE_FORMAT_RGB565:
84 format = __DRI_IMAGE_FOURCC_RGB565;
86 case __DRI_IMAGE_FORMAT_ARGB8888:
87 format = __DRI_IMAGE_FOURCC_ARGB8888;
89 case __DRI_IMAGE_FORMAT_XRGB8888:
90 format = __DRI_IMAGE_FOURCC_XRGB8888;
92 case __DRI_IMAGE_FORMAT_ABGR8888:
93 format = __DRI_IMAGE_FOURCC_ABGR8888;
95 case __DRI_IMAGE_FORMAT_XBGR8888:
96 format = __DRI_IMAGE_FOURCC_XBGR8888;
105 * DRI2 flush extension.
108 dri2_flush_drawable(__DRIdrawable *dPriv)
110 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
114 dri2_invalidate_drawable(__DRIdrawable *dPriv)
116 struct dri_drawable *drawable = dri_drawable(dPriv);
118 dri2InvalidateDrawable(dPriv);
119 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
121 p_atomic_inc(&drawable->base.stamp);
124 static const __DRI2flushExtension dri2FlushExtension = {
125 .base = { __DRI2_FLUSH, 4 },
127 .flush = dri2_flush_drawable,
128 .invalidate = dri2_invalidate_drawable,
129 .flush_with_flags = dri_flush,
133 * Retrieve __DRIbuffer from the DRI loader.
136 dri2_drawable_get_buffers(struct dri_drawable *drawable,
137 const enum st_attachment_type *atts,
140 __DRIdrawable *dri_drawable = drawable->dPriv;
141 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
143 __DRIbuffer *buffers;
145 unsigned attachments[10];
146 unsigned num_attachments, i;
149 with_format = dri_with_format(drawable->sPriv);
153 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
155 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
157 for (i = 0; i < *count; i++) {
158 enum pipe_format format;
162 dri_drawable_get_format(drawable, atts[i], &format, &bind);
163 if (format == PIPE_FORMAT_NONE)
167 case ST_ATTACHMENT_FRONT_LEFT:
171 att = __DRI_BUFFER_FRONT_LEFT;
173 case ST_ATTACHMENT_BACK_LEFT:
174 att = __DRI_BUFFER_BACK_LEFT;
176 case ST_ATTACHMENT_FRONT_RIGHT:
177 att = __DRI_BUFFER_FRONT_RIGHT;
179 case ST_ATTACHMENT_BACK_RIGHT:
180 att = __DRI_BUFFER_BACK_RIGHT;
187 * In this switch statement we must support all formats that
188 * may occur as the stvis->color_format.
191 case PIPE_FORMAT_BGRA8888_UNORM:
194 case PIPE_FORMAT_BGRX8888_UNORM:
197 case PIPE_FORMAT_B5G6R5_UNORM:
201 depth = util_format_get_blocksizebits(format);
202 assert(!"Unexpected format in dri2_drawable_get_buffers()");
205 attachments[num_attachments++] = att;
207 attachments[num_attachments++] = depth;
212 num_attachments /= 2;
213 buffers = loader->getBuffersWithFormat(dri_drawable,
214 &dri_drawable->w, &dri_drawable->h,
215 attachments, num_attachments,
216 &num_buffers, dri_drawable->loaderPrivate);
219 buffers = loader->getBuffers(dri_drawable,
220 &dri_drawable->w, &dri_drawable->h,
221 attachments, num_attachments,
222 &num_buffers, dri_drawable->loaderPrivate);
226 *count = num_buffers;
232 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
233 struct __DRIimageList *images,
234 const enum st_attachment_type *statts,
235 unsigned statts_count)
237 __DRIdrawable *dPriv = drawable->dPriv;
238 __DRIscreen *sPriv = drawable->sPriv;
239 unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
241 uint32_t buffer_mask = 0;
244 for (i = 0; i < statts_count; i++) {
245 dri_drawable_get_format(drawable, statts[i], &pf, &bind);
246 if (pf == PIPE_FORMAT_NONE)
250 case ST_ATTACHMENT_FRONT_LEFT:
251 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
253 case ST_ATTACHMENT_BACK_LEFT:
254 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
261 case PIPE_FORMAT_B5G6R5_UNORM:
262 image_format = __DRI_IMAGE_FORMAT_RGB565;
264 case PIPE_FORMAT_BGRX8888_UNORM:
265 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
267 case PIPE_FORMAT_BGRA8888_UNORM:
268 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
270 case PIPE_FORMAT_RGBA8888_UNORM:
271 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
274 image_format = __DRI_IMAGE_FORMAT_NONE;
279 return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
280 (uint32_t *) &drawable->base.stamp,
281 dPriv->loaderPrivate, buffer_mask,
286 dri2_allocate_buffer(__DRIscreen *sPriv,
287 unsigned attachment, unsigned format,
288 int width, int height)
290 struct dri_screen *screen = dri_screen(sPriv);
291 struct dri2_buffer *buffer;
292 struct pipe_resource templ;
295 struct winsys_handle whandle;
297 switch (attachment) {
298 case __DRI_BUFFER_FRONT_LEFT:
299 case __DRI_BUFFER_FAKE_FRONT_LEFT:
300 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
302 case __DRI_BUFFER_BACK_LEFT:
303 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
305 case __DRI_BUFFER_DEPTH:
306 case __DRI_BUFFER_DEPTH_STENCIL:
307 case __DRI_BUFFER_STENCIL:
308 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
312 /* because we get the handle and stride */
313 bind |= PIPE_BIND_SHARED;
317 pf = PIPE_FORMAT_BGRA8888_UNORM;
320 pf = PIPE_FORMAT_BGRX8888_UNORM;
323 pf = PIPE_FORMAT_Z16_UNORM;
329 buffer = CALLOC_STRUCT(dri2_buffer);
333 memset(&templ, 0, sizeof(templ));
336 templ.target = PIPE_TEXTURE_2D;
337 templ.last_level = 0;
338 templ.width0 = width;
339 templ.height0 = height;
341 templ.array_size = 1;
344 screen->base.screen->resource_create(screen->base.screen, &templ);
345 if (!buffer->resource) {
350 memset(&whandle, 0, sizeof(whandle));
351 if (screen->can_share_buffer)
352 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
354 whandle.type = DRM_API_HANDLE_TYPE_KMS;
356 screen->base.screen->resource_get_handle(screen->base.screen,
357 buffer->resource, &whandle,
358 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
360 buffer->base.attachment = attachment;
361 buffer->base.name = whandle.handle;
362 buffer->base.cpp = util_format_get_blocksize(pf);
363 buffer->base.pitch = whandle.stride;
365 return &buffer->base;
369 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
371 struct dri2_buffer *buffer = dri2_buffer(bPriv);
373 pipe_resource_reference(&buffer->resource, NULL);
378 * Backend functions for st_framebuffer interface.
382 dri2_allocate_textures(struct dri_context *ctx,
383 struct dri_drawable *drawable,
384 const enum st_attachment_type *statts,
385 unsigned statts_count)
387 __DRIscreen *sPriv = drawable->sPriv;
388 __DRIdrawable *dri_drawable = drawable->dPriv;
389 struct dri_screen *screen = dri_screen(sPriv);
390 struct pipe_resource templ;
391 boolean alloc_depthstencil = FALSE;
393 const __DRIimageLoaderExtension *image = sPriv->image.loader;
394 /* Image specific variables */
395 struct __DRIimageList images;
396 /* Dri2 specific variables */
397 __DRIbuffer *buffers = NULL;
398 struct winsys_handle whandle;
399 unsigned num_buffers = statts_count;
401 /* First get the buffers from the loader */
403 if (!dri_image_drawable_get_buffers(drawable, &images,
404 statts, statts_count))
408 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
409 if (!buffers || (drawable->old_num == num_buffers &&
410 drawable->old_w == dri_drawable->w &&
411 drawable->old_h == dri_drawable->h &&
412 memcmp(drawable->old, buffers,
413 sizeof(__DRIbuffer) * num_buffers) == 0))
417 /* Second clean useless resources*/
419 /* See if we need a depth-stencil buffer. */
420 for (i = 0; i < statts_count; i++) {
421 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
422 alloc_depthstencil = TRUE;
427 /* Delete the resources we won't need. */
428 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
429 /* Don't delete the depth-stencil buffer, we can reuse it. */
430 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
433 /* Flush the texture before unreferencing, so that other clients can
434 * see what the driver has rendered.
436 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
437 struct pipe_context *pipe = ctx->st->pipe;
438 pipe->flush_resource(pipe, drawable->textures[i]);
441 pipe_resource_reference(&drawable->textures[i], NULL);
444 if (drawable->stvis.samples > 1) {
445 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
448 /* Don't delete MSAA resources for the attachments which are enabled,
449 * we can reuse them. */
450 for (j = 0; j < statts_count; j++) {
451 if (i == statts[j]) {
458 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
463 /* Third use the buffers retrieved to fill the drawable info */
465 memset(&templ, 0, sizeof(templ));
466 templ.target = screen->target;
467 templ.last_level = 0;
469 templ.array_size = 1;
472 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
473 struct pipe_resource **buf =
474 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
475 struct pipe_resource *texture = images.front->texture;
477 dri_drawable->w = texture->width0;
478 dri_drawable->h = texture->height0;
480 pipe_resource_reference(buf, texture);
483 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
484 struct pipe_resource **buf =
485 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
486 struct pipe_resource *texture = images.back->texture;
488 dri_drawable->w = texture->width0;
489 dri_drawable->h = texture->height0;
491 pipe_resource_reference(buf, texture);
494 /* Note: if there is both a back and a front buffer,
495 * then they have the same size.
497 templ.width0 = dri_drawable->w;
498 templ.height0 = dri_drawable->h;
501 memset(&whandle, 0, sizeof(whandle));
503 /* Process DRI-provided buffers and get pipe_resources. */
504 for (i = 0; i < num_buffers; i++) {
505 __DRIbuffer *buf = &buffers[i];
506 enum st_attachment_type statt;
507 enum pipe_format format;
509 switch (buf->attachment) {
510 case __DRI_BUFFER_FRONT_LEFT:
511 if (!screen->auto_fake_front) {
512 continue; /* invalid attachment */
515 case __DRI_BUFFER_FAKE_FRONT_LEFT:
516 statt = ST_ATTACHMENT_FRONT_LEFT;
518 case __DRI_BUFFER_BACK_LEFT:
519 statt = ST_ATTACHMENT_BACK_LEFT;
522 continue; /* invalid attachment */
525 dri_drawable_get_format(drawable, statt, &format, &bind);
526 if (format == PIPE_FORMAT_NONE)
529 /* dri2_drawable_get_buffers has already filled dri_drawable->w
530 * and dri_drawable->h */
531 templ.width0 = dri_drawable->w;
532 templ.height0 = dri_drawable->h;
533 templ.format = format;
535 whandle.handle = buf->name;
536 whandle.stride = buf->pitch;
538 if (screen->can_share_buffer)
539 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
541 whandle.type = DRM_API_HANDLE_TYPE_KMS;
542 drawable->textures[statt] =
543 screen->base.screen->resource_from_handle(screen->base.screen,
545 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
546 assert(drawable->textures[statt]);
550 /* Allocate private MSAA colorbuffers. */
551 if (drawable->stvis.samples > 1) {
552 for (i = 0; i < statts_count; i++) {
553 enum st_attachment_type statt = statts[i];
555 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
558 if (drawable->textures[statt]) {
559 templ.format = drawable->textures[statt]->format;
560 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
561 templ.nr_samples = drawable->stvis.samples;
563 /* Try to reuse the resource.
564 * (the other resource parameters should be constant)
566 if (!drawable->msaa_textures[statt] ||
567 drawable->msaa_textures[statt]->width0 != templ.width0 ||
568 drawable->msaa_textures[statt]->height0 != templ.height0) {
569 /* Allocate a new one. */
570 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
572 drawable->msaa_textures[statt] =
573 screen->base.screen->resource_create(screen->base.screen,
575 assert(drawable->msaa_textures[statt]);
577 /* If there are any MSAA resources, we should initialize them
578 * such that they contain the same data as the single-sample
579 * resources we just got from the X server.
581 * The reason for this is that the state tracker (and
582 * therefore the app) can access the MSAA resources only.
583 * The single-sample resources are not exposed
584 * to the state tracker.
587 dri_pipe_blit(ctx->st->pipe,
588 drawable->msaa_textures[statt],
589 drawable->textures[statt]);
593 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
598 /* Allocate a private depth-stencil buffer. */
599 if (alloc_depthstencil) {
600 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
601 struct pipe_resource **zsbuf;
602 enum pipe_format format;
605 dri_drawable_get_format(drawable, statt, &format, &bind);
608 templ.format = format;
611 if (drawable->stvis.samples > 1) {
612 templ.nr_samples = drawable->stvis.samples;
613 zsbuf = &drawable->msaa_textures[statt];
616 templ.nr_samples = 0;
617 zsbuf = &drawable->textures[statt];
620 /* Try to reuse the resource.
621 * (the other resource parameters should be constant)
624 (*zsbuf)->width0 != templ.width0 ||
625 (*zsbuf)->height0 != templ.height0) {
626 /* Allocate a new one. */
627 pipe_resource_reference(zsbuf, NULL);
628 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
634 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
635 pipe_resource_reference(&drawable->textures[statt], NULL);
639 /* For DRI2, we may get the same buffers again from the server.
640 * To prevent useless imports of gem names, drawable->old* is used
641 * to bypass the import if we get the same buffers. This doesn't apply
642 * to DRI3/Wayland, users of image.loader, since the buffer is managed
643 * by the client (no import), and the back buffer is going to change
647 drawable->old_num = num_buffers;
648 drawable->old_w = dri_drawable->w;
649 drawable->old_h = dri_drawable->h;
650 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
655 dri2_flush_frontbuffer(struct dri_context *ctx,
656 struct dri_drawable *drawable,
657 enum st_attachment_type statt)
659 __DRIdrawable *dri_drawable = drawable->dPriv;
660 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
661 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
662 struct pipe_context *pipe = ctx->st->pipe;
664 if (statt != ST_ATTACHMENT_FRONT_LEFT)
667 if (drawable->stvis.samples > 1) {
668 /* Resolve the front buffer. */
669 dri_pipe_blit(ctx->st->pipe,
670 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
671 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
674 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
675 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
678 pipe->flush(pipe, NULL, 0);
681 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
683 else if (loader->flushFrontBuffer) {
684 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
689 dri2_update_tex_buffer(struct dri_drawable *drawable,
690 struct dri_context *ctx,
691 struct pipe_resource *res)
697 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
699 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
702 if (!loader->lookupEGLImage)
705 img = loader->lookupEGLImage(screen->sPriv,
706 handle, screen->sPriv->loaderPrivate);
712 dri2_create_image_from_winsys(__DRIscreen *_screen,
713 int width, int height, int format,
714 struct winsys_handle *whandle, int pitch,
717 struct dri_screen *screen = dri_screen(_screen);
719 struct pipe_resource templ;
723 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
726 case __DRI_IMAGE_FORMAT_RGB565:
727 pf = PIPE_FORMAT_B5G6R5_UNORM;
729 case __DRI_IMAGE_FORMAT_XRGB8888:
730 pf = PIPE_FORMAT_BGRX8888_UNORM;
732 case __DRI_IMAGE_FORMAT_ARGB8888:
733 pf = PIPE_FORMAT_BGRA8888_UNORM;
735 case __DRI_IMAGE_FORMAT_ABGR8888:
736 pf = PIPE_FORMAT_RGBA8888_UNORM;
739 pf = PIPE_FORMAT_NONE;
742 if (pf == PIPE_FORMAT_NONE)
745 img = CALLOC_STRUCT(__DRIimageRec);
749 memset(&templ, 0, sizeof(templ));
750 templ.bind = tex_usage;
752 templ.target = screen->target;
753 templ.last_level = 0;
754 templ.width0 = width;
755 templ.height0 = height;
757 templ.array_size = 1;
759 whandle->stride = pitch * util_format_get_blocksize(pf);
762 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
763 &templ, whandle, PIPE_HANDLE_USAGE_READ_WRITE);
771 img->dri_format = format;
773 img->loader_private = loaderPrivate;
779 dri2_create_image_from_name(__DRIscreen *_screen,
780 int width, int height, int format,
781 int name, int pitch, void *loaderPrivate)
783 struct winsys_handle whandle;
785 memset(&whandle, 0, sizeof(whandle));
786 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
787 whandle.handle = name;
789 return dri2_create_image_from_winsys(_screen, width, height, format,
790 &whandle, pitch, loaderPrivate);
794 dri2_create_image_from_fd(__DRIscreen *_screen,
795 int width, int height, int format,
796 int fd, int pitch, void *loaderPrivate)
798 struct winsys_handle whandle;
803 memset(&whandle, 0, sizeof(whandle));
804 whandle.type = DRM_API_HANDLE_TYPE_FD;
805 whandle.handle = (unsigned)fd;
807 return dri2_create_image_from_winsys(_screen, width, height, format,
808 &whandle, pitch, loaderPrivate);
812 dri2_create_image_from_renderbuffer(__DRIcontext *context,
813 int renderbuffer, void *loaderPrivate)
815 struct dri_context *ctx = dri_context(context);
817 if (!ctx->st->get_resource_for_egl_image)
825 dri2_create_image(__DRIscreen *_screen,
826 int width, int height, int format,
827 unsigned int use, void *loaderPrivate)
829 struct dri_screen *screen = dri_screen(_screen);
831 struct pipe_resource templ;
835 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
836 if (use & __DRI_IMAGE_USE_SCANOUT)
837 tex_usage |= PIPE_BIND_SCANOUT;
838 if (use & __DRI_IMAGE_USE_SHARE)
839 tex_usage |= PIPE_BIND_SHARED;
840 if (use & __DRI_IMAGE_USE_LINEAR)
841 tex_usage |= PIPE_BIND_LINEAR;
842 if (use & __DRI_IMAGE_USE_CURSOR) {
843 if (width != 64 || height != 64)
845 tex_usage |= PIPE_BIND_CURSOR;
849 case __DRI_IMAGE_FORMAT_RGB565:
850 pf = PIPE_FORMAT_B5G6R5_UNORM;
852 case __DRI_IMAGE_FORMAT_XRGB8888:
853 pf = PIPE_FORMAT_BGRX8888_UNORM;
855 case __DRI_IMAGE_FORMAT_ARGB8888:
856 pf = PIPE_FORMAT_BGRA8888_UNORM;
858 case __DRI_IMAGE_FORMAT_ABGR8888:
859 pf = PIPE_FORMAT_RGBA8888_UNORM;
862 pf = PIPE_FORMAT_NONE;
865 if (pf == PIPE_FORMAT_NONE)
868 img = CALLOC_STRUCT(__DRIimageRec);
872 memset(&templ, 0, sizeof(templ));
873 templ.bind = tex_usage;
875 templ.target = PIPE_TEXTURE_2D;
876 templ.last_level = 0;
877 templ.width0 = width;
878 templ.height0 = height;
880 templ.array_size = 1;
882 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
890 img->dri_format = format;
891 img->dri_components = 0;
894 img->loader_private = loaderPrivate;
899 dri2_query_image(__DRIimage *image, int attrib, int *value)
901 struct winsys_handle whandle;
904 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
905 usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
907 usage = PIPE_HANDLE_USAGE_READ_WRITE;
909 memset(&whandle, 0, sizeof(whandle));
912 case __DRI_IMAGE_ATTRIB_STRIDE:
913 whandle.type = DRM_API_HANDLE_TYPE_KMS;
914 image->texture->screen->resource_get_handle(image->texture->screen,
915 image->texture, &whandle, usage);
916 *value = whandle.stride;
918 case __DRI_IMAGE_ATTRIB_HANDLE:
919 whandle.type = DRM_API_HANDLE_TYPE_KMS;
920 image->texture->screen->resource_get_handle(image->texture->screen,
921 image->texture, &whandle, usage);
922 *value = whandle.handle;
924 case __DRI_IMAGE_ATTRIB_NAME:
925 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
926 image->texture->screen->resource_get_handle(image->texture->screen,
927 image->texture, &whandle, usage);
928 *value = whandle.handle;
930 case __DRI_IMAGE_ATTRIB_FD:
931 whandle.type= DRM_API_HANDLE_TYPE_FD;
932 image->texture->screen->resource_get_handle(image->texture->screen,
933 image->texture, &whandle, usage);
934 *value = whandle.handle;
936 case __DRI_IMAGE_ATTRIB_FORMAT:
937 *value = image->dri_format;
939 case __DRI_IMAGE_ATTRIB_WIDTH:
940 *value = image->texture->width0;
942 case __DRI_IMAGE_ATTRIB_HEIGHT:
943 *value = image->texture->height0;
945 case __DRI_IMAGE_ATTRIB_COMPONENTS:
946 if (image->dri_components == 0)
948 *value = image->dri_components;
950 case __DRI_IMAGE_ATTRIB_FOURCC:
951 *value = convert_to_fourcc(image->dri_format);
953 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
962 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
966 img = CALLOC_STRUCT(__DRIimageRec);
971 pipe_resource_reference(&img->texture, image->texture);
972 img->level = image->level;
973 img->layer = image->layer;
974 img->dri_format = image->dri_format;
975 /* This should be 0 for sub images, but dup is also used for base images. */
976 img->dri_components = image->dri_components;
977 img->loader_private = loaderPrivate;
983 dri2_validate_usage(__DRIimage *image, unsigned int use)
986 * Gallium drivers are bad at adding usages to the resources
987 * once opened again in another process, which is the main use
988 * case for this, so we have to lie.
997 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
998 int *names, int num_names, int *strides, int *offsets,
1002 int stride, dri_components;
1006 if (offsets[0] != 0)
1009 format = convert_fourcc(format, &dri_components);
1013 /* Strides are in bytes not pixels. */
1014 stride = strides[0] /4;
1016 img = dri2_create_image_from_name(screen, width, height, format,
1017 names[0], stride, loaderPrivate);
1021 img->dri_components = dri_components;
1026 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1033 if (image->dri_components == 0)
1036 img = dri2_dup_image(image, loaderPrivate);
1040 /* set this to 0 for sub images. */
1041 img->dri_components = 0;
1046 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1047 int depth, int level, unsigned *error,
1048 void *loaderPrivate)
1051 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1052 struct gl_texture_object *obj;
1053 struct pipe_resource *tex;
1056 obj = _mesa_lookup_texture(ctx, texture);
1057 if (!obj || obj->Target != target) {
1058 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1062 tex = st_get_texobj_resource(obj);
1064 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1068 if (target == GL_TEXTURE_CUBE_MAP)
1071 _mesa_test_texobj_completeness(ctx, obj);
1072 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1073 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1077 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1078 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1082 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1083 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1087 img = CALLOC_STRUCT(__DRIimageRec);
1089 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1095 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1097 img->loader_private = loaderPrivate;
1099 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1100 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1105 pipe_resource_reference(&img->texture, tex);
1107 *error = __DRI_IMAGE_ERROR_SUCCESS;
1112 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1113 int *fds, int num_fds, int *strides, int *offsets,
1114 void *loaderPrivate)
1117 int format, stride, dri_components;
1121 if (offsets[0] != 0)
1124 format = convert_fourcc(fourcc, &dri_components);
1128 /* Strides are in bytes not pixels. */
1129 stride = strides[0] /4;
1131 img = dri2_create_image_from_fd(screen, width, height, format,
1132 fds[0], stride, loaderPrivate);
1136 img->dri_components = dri_components;
1141 dri2_from_dma_bufs(__DRIscreen *screen,
1142 int width, int height, int fourcc,
1143 int *fds, int num_fds,
1144 int *strides, int *offsets,
1145 enum __DRIYUVColorSpace yuv_color_space,
1146 enum __DRISampleRange sample_range,
1147 enum __DRIChromaSiting horizontal_siting,
1148 enum __DRIChromaSiting vertical_siting,
1150 void *loaderPrivate)
1153 int format, stride, dri_components;
1155 if (num_fds != 1 || offsets[0] != 0) {
1156 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1160 format = convert_fourcc(fourcc, &dri_components);
1162 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1166 /* Strides are in bytes not pixels. */
1167 stride = strides[0] /4;
1169 img = dri2_create_image_from_fd(screen, width, height, format,
1170 fds[0], stride, loaderPrivate);
1172 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1176 img->yuv_color_space = yuv_color_space;
1177 img->sample_range = sample_range;
1178 img->horizontal_siting = horizontal_siting;
1179 img->vertical_siting = vertical_siting;
1180 img->dri_components = dri_components;
1182 *error = __DRI_IMAGE_ERROR_SUCCESS;
1187 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1188 int dstx0, int dsty0, int dstwidth, int dstheight,
1189 int srcx0, int srcy0, int srcwidth, int srcheight,
1192 struct dri_context *ctx = dri_context(context);
1193 struct pipe_context *pipe = ctx->st->pipe;
1194 struct pipe_screen *screen;
1195 struct pipe_fence_handle *fence;
1196 struct pipe_blit_info blit;
1201 memset(&blit, 0, sizeof(blit));
1202 blit.dst.resource = dst->texture;
1203 blit.dst.box.x = dstx0;
1204 blit.dst.box.y = dsty0;
1205 blit.dst.box.width = dstwidth;
1206 blit.dst.box.height = dstheight;
1207 blit.dst.box.depth = 1;
1208 blit.dst.format = dst->texture->format;
1209 blit.src.resource = src->texture;
1210 blit.src.box.x = srcx0;
1211 blit.src.box.y = srcy0;
1212 blit.src.box.width = srcwidth;
1213 blit.src.box.height = srcheight;
1214 blit.src.box.depth = 1;
1215 blit.src.format = src->texture->format;
1216 blit.mask = PIPE_MASK_RGBA;
1217 blit.filter = PIPE_TEX_FILTER_NEAREST;
1219 pipe->blit(pipe, &blit);
1221 if (flush_flag == __BLIT_FLAG_FLUSH) {
1222 pipe->flush_resource(pipe, dst->texture);
1223 ctx->st->flush(ctx->st, 0, NULL);
1224 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1225 screen = dri_screen(ctx->sPriv)->base.screen;
1226 pipe->flush_resource(pipe, dst->texture);
1227 ctx->st->flush(ctx->st, 0, &fence);
1228 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1229 screen->fence_reference(screen, &fence, NULL);
1234 dri2_destroy_image(__DRIimage *img)
1236 pipe_resource_reference(&img->texture, NULL);
1241 dri2_get_capabilities(__DRIscreen *_screen)
1243 struct dri_screen *screen = dri_screen(_screen);
1245 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1248 /* The extension is modified during runtime if DRI_PRIME is detected */
1249 static __DRIimageExtension dri2ImageExtension = {
1250 .base = { __DRI_IMAGE, 11 },
1252 .createImageFromName = dri2_create_image_from_name,
1253 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1254 .destroyImage = dri2_destroy_image,
1255 .createImage = dri2_create_image,
1256 .queryImage = dri2_query_image,
1257 .dupImage = dri2_dup_image,
1258 .validateUsage = dri2_validate_usage,
1259 .createImageFromNames = dri2_from_names,
1260 .fromPlanar = dri2_from_planar,
1261 .createImageFromTexture = dri2_create_from_texture,
1262 .createImageFromFds = NULL,
1263 .createImageFromDmaBufs = NULL,
1264 .blitImage = dri2_blit_image,
1265 .getCapabilities = dri2_get_capabilities,
1270 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1272 return screen->opencl_dri_event_add_ref &&
1273 screen->opencl_dri_event_release &&
1274 screen->opencl_dri_event_wait &&
1275 screen->opencl_dri_event_get_fence;
1279 dri2_load_opencl_interop(struct dri_screen *screen)
1281 #if defined(RTLD_DEFAULT)
1284 pipe_mutex_lock(screen->opencl_func_mutex);
1286 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1287 pipe_mutex_unlock(screen->opencl_func_mutex);
1291 screen->opencl_dri_event_add_ref =
1292 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1293 screen->opencl_dri_event_release =
1294 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1295 screen->opencl_dri_event_wait =
1296 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1297 screen->opencl_dri_event_get_fence =
1298 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1300 success = dri2_is_opencl_interop_loaded_locked(screen);
1301 pipe_mutex_unlock(screen->opencl_func_mutex);
1309 struct dri_screen *driscreen;
1310 struct pipe_fence_handle *pipe_fence;
1315 dri2_create_fence(__DRIcontext *_ctx)
1317 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1318 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1323 ctx->flush(ctx, &fence->pipe_fence, 0);
1325 if (!fence->pipe_fence) {
1330 fence->driscreen = dri_screen(_ctx->driScreenPriv);
1335 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1337 struct dri_screen *driscreen = dri_screen(_screen);
1338 struct dri2_fence *fence;
1340 if (!dri2_load_opencl_interop(driscreen))
1343 fence = CALLOC_STRUCT(dri2_fence);
1347 fence->cl_event = (void*)cl_event;
1349 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1354 fence->driscreen = driscreen;
1359 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1361 struct dri_screen *driscreen = dri_screen(_screen);
1362 struct pipe_screen *screen = driscreen->base.screen;
1363 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1365 if (fence->pipe_fence)
1366 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1367 else if (fence->cl_event)
1368 driscreen->opencl_dri_event_release(fence->cl_event);
1376 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1379 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1380 struct dri_screen *driscreen = fence->driscreen;
1381 struct pipe_screen *screen = driscreen->base.screen;
1383 /* No need to flush. The context was flushed when the fence was created. */
1385 if (fence->pipe_fence)
1386 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1387 else if (fence->cl_event) {
1388 struct pipe_fence_handle *pipe_fence =
1389 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1392 return screen->fence_finish(screen, pipe_fence, timeout);
1394 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1403 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1405 /* AFAIK, no driver currently supports parallel context execution. */
1408 static __DRI2fenceExtension dri2FenceExtension = {
1409 .base = { __DRI2_FENCE, 1 },
1411 .create_fence = dri2_create_fence,
1412 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1413 .destroy_fence = dri2_destroy_fence,
1414 .client_wait_sync = dri2_client_wait_sync,
1415 .server_wait_sync = dri2_server_wait_sync
1418 static const __DRIrobustnessExtension dri2Robustness = {
1419 .base = { __DRI2_ROBUSTNESS, 1 }
1423 * Backend function init_screen.
1426 static const __DRIextension *dri_screen_extensions[] = {
1427 &driTexBufferExtension.base,
1428 &dri2FlushExtension.base,
1429 &dri2ImageExtension.base,
1430 &dri2RendererQueryExtension.base,
1431 &dri2ConfigQueryExtension.base,
1432 &dri2ThrottleExtension.base,
1433 &dri2FenceExtension.base,
1437 static const __DRIextension *dri_robust_screen_extensions[] = {
1438 &driTexBufferExtension.base,
1439 &dri2FlushExtension.base,
1440 &dri2ImageExtension.base,
1441 &dri2RendererQueryExtension.base,
1442 &dri2ConfigQueryExtension.base,
1443 &dri2ThrottleExtension.base,
1444 &dri2FenceExtension.base,
1445 &dri2Robustness.base,
1450 * This is the driver specific part of the createNewScreen entry point.
1452 * Returns the struct gl_config supported by this driver.
1454 static const __DRIconfig **
1455 dri2_init_screen(__DRIscreen * sPriv)
1457 const __DRIconfig **configs;
1458 struct dri_screen *screen;
1459 struct pipe_screen *pscreen = NULL;
1460 const struct drm_conf_ret *throttle_ret;
1461 const struct drm_conf_ret *dmabuf_ret;
1464 screen = CALLOC_STRUCT(dri_screen);
1468 screen->sPriv = sPriv;
1469 screen->fd = sPriv->fd;
1470 pipe_mutex_init(screen->opencl_func_mutex);
1472 sPriv->driverPrivate = (void *)screen;
1474 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1477 if (pipe_loader_drm_probe_fd(&screen->dev, fd))
1478 pscreen = pipe_loader_create_screen(screen->dev);
1483 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1484 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1486 if (throttle_ret && throttle_ret->val.val_int != -1) {
1487 screen->throttling_enabled = TRUE;
1488 screen->default_throttle_frames = throttle_ret->val.val_int;
1491 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1494 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1495 (cap & DRM_PRIME_CAP_IMPORT)) {
1496 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1497 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1501 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1502 sPriv->extensions = dri_robust_screen_extensions;
1503 screen->has_reset_status_query = true;
1506 sPriv->extensions = dri_screen_extensions;
1508 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1512 screen->can_share_buffer = true;
1513 screen->auto_fake_front = dri_with_format(sPriv);
1514 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1515 screen->lookup_egl_image = dri2_lookup_egl_image;
1519 dri_destroy_screen_helper(screen);
1521 pipe_loader_release(&screen->dev, 1);
1529 * This is the driver specific part of the createNewScreen entry point.
1531 * Returns the struct gl_config supported by this driver.
1533 static const __DRIconfig **
1534 dri_kms_init_screen(__DRIscreen * sPriv)
1536 #if defined(GALLIUM_SOFTPIPE)
1537 const __DRIconfig **configs;
1538 struct dri_screen *screen;
1539 struct pipe_screen *pscreen = NULL;
1543 screen = CALLOC_STRUCT(dri_screen);
1547 screen->sPriv = sPriv;
1548 screen->fd = sPriv->fd;
1550 sPriv->driverPrivate = (void *)screen;
1552 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1555 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
1556 pscreen = pipe_loader_create_screen(screen->dev);
1561 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1562 (cap & DRM_PRIME_CAP_IMPORT)) {
1563 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1564 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1567 sPriv->extensions = dri_screen_extensions;
1569 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1573 screen->can_share_buffer = false;
1574 screen->auto_fake_front = dri_with_format(sPriv);
1575 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1576 screen->lookup_egl_image = dri2_lookup_egl_image;
1580 dri_destroy_screen_helper(screen);
1582 pipe_loader_release(&screen->dev, 1);
1586 #endif // GALLIUM_SOFTPIPE
1591 dri2_create_buffer(__DRIscreen * sPriv,
1592 __DRIdrawable * dPriv,
1593 const struct gl_config * visual, boolean isPixmap)
1595 struct dri_drawable *drawable = NULL;
1597 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1600 drawable = dPriv->driverPrivate;
1602 drawable->allocate_textures = dri2_allocate_textures;
1603 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1604 drawable->update_tex_buffer = dri2_update_tex_buffer;
1610 * DRI driver virtual function table.
1612 * DRI versions differ in their implementation of init_screen and swap_buffers.
1614 const struct __DriverAPIRec galliumdrm_driver_api = {
1615 .InitScreen = dri2_init_screen,
1616 .DestroyScreen = dri_destroy_screen,
1617 .CreateContext = dri_create_context,
1618 .DestroyContext = dri_destroy_context,
1619 .CreateBuffer = dri2_create_buffer,
1620 .DestroyBuffer = dri_destroy_buffer,
1621 .MakeCurrent = dri_make_current,
1622 .UnbindContext = dri_unbind_context,
1624 .AllocateBuffer = dri2_allocate_buffer,
1625 .ReleaseBuffer = dri2_release_buffer,
1629 * DRI driver virtual function table.
1631 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1632 * hook. The latter is used to explicitly initialise the kms_swrast driver
1633 * rather than selecting the approapriate driver as suggested by the loader.
1635 const struct __DriverAPIRec dri_kms_driver_api = {
1636 .InitScreen = dri_kms_init_screen,
1637 .DestroyScreen = dri_destroy_screen,
1638 .CreateContext = dri_create_context,
1639 .DestroyContext = dri_destroy_context,
1640 .CreateBuffer = dri2_create_buffer,
1641 .DestroyBuffer = dri_destroy_buffer,
1642 .MakeCurrent = dri_make_current,
1643 .UnbindContext = dri_unbind_context,
1645 .AllocateBuffer = dri2_allocate_buffer,
1646 .ReleaseBuffer = dri2_release_buffer,
1649 /* This is the table of extensions that the loader will dlsym() for. */
1650 const __DRIextension *galliumdrm_driver_extensions[] = {
1651 &driCoreExtension.base,
1652 &driImageDriverExtension.base,
1653 &driDRI2Extension.base,
1654 &gallium_config_options.base,
1658 /* vim: set sw=3 ts=8 sts=3 expandtab: */