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_B8G8R8A8_UNORM:
194 case PIPE_FORMAT_B8G8R8X8_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_B8G8R8X8_UNORM:
265 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
267 case PIPE_FORMAT_B8G8R8A8_UNORM:
268 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
270 case PIPE_FORMAT_R8G8B8A8_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_B8G8R8A8_UNORM;
320 pf = PIPE_FORMAT_B8G8R8X8_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);
359 buffer->base.attachment = attachment;
360 buffer->base.name = whandle.handle;
361 buffer->base.cpp = util_format_get_blocksize(pf);
362 buffer->base.pitch = whandle.stride;
364 return &buffer->base;
368 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
370 struct dri2_buffer *buffer = dri2_buffer(bPriv);
372 pipe_resource_reference(&buffer->resource, NULL);
377 * Backend functions for st_framebuffer interface.
381 dri2_allocate_textures(struct dri_context *ctx,
382 struct dri_drawable *drawable,
383 const enum st_attachment_type *statts,
384 unsigned statts_count)
386 __DRIscreen *sPriv = drawable->sPriv;
387 __DRIdrawable *dri_drawable = drawable->dPriv;
388 struct dri_screen *screen = dri_screen(sPriv);
389 struct pipe_resource templ;
390 boolean alloc_depthstencil = FALSE;
392 const __DRIimageLoaderExtension *image = sPriv->image.loader;
393 /* Image specific variables */
394 struct __DRIimageList images;
395 /* Dri2 specific variables */
396 __DRIbuffer *buffers = NULL;
397 struct winsys_handle whandle;
398 unsigned num_buffers = statts_count;
400 /* First get the buffers from the loader */
402 if (!dri_image_drawable_get_buffers(drawable, &images,
403 statts, statts_count))
407 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
408 if (!buffers || (drawable->old_num == num_buffers &&
409 drawable->old_w == dri_drawable->w &&
410 drawable->old_h == dri_drawable->h &&
411 memcmp(drawable->old, buffers,
412 sizeof(__DRIbuffer) * num_buffers) == 0))
416 /* Second clean useless resources*/
418 /* See if we need a depth-stencil buffer. */
419 for (i = 0; i < statts_count; i++) {
420 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
421 alloc_depthstencil = TRUE;
426 /* Delete the resources we won't need. */
427 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
428 /* Don't delete the depth-stencil buffer, we can reuse it. */
429 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
432 /* Flush the texture before unreferencing, so that other clients can
433 * see what the driver has rendered.
435 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
436 struct pipe_context *pipe = ctx->st->pipe;
437 pipe->flush_resource(pipe, drawable->textures[i]);
440 pipe_resource_reference(&drawable->textures[i], NULL);
443 if (drawable->stvis.samples > 1) {
444 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
447 /* Don't delete MSAA resources for the attachments which are enabled,
448 * we can reuse them. */
449 for (j = 0; j < statts_count; j++) {
450 if (i == statts[j]) {
457 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
462 /* Third use the buffers retrieved to fill the drawable info */
464 memset(&templ, 0, sizeof(templ));
465 templ.target = screen->target;
466 templ.last_level = 0;
468 templ.array_size = 1;
471 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
472 struct pipe_resource **buf =
473 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
474 struct pipe_resource *texture = images.front->texture;
476 dri_drawable->w = texture->width0;
477 dri_drawable->h = texture->height0;
479 pipe_resource_reference(buf, texture);
482 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
483 struct pipe_resource **buf =
484 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
485 struct pipe_resource *texture = images.back->texture;
487 dri_drawable->w = texture->width0;
488 dri_drawable->h = texture->height0;
490 pipe_resource_reference(buf, texture);
493 /* Note: if there is both a back and a front buffer,
494 * then they have the same size.
496 templ.width0 = dri_drawable->w;
497 templ.height0 = dri_drawable->h;
500 memset(&whandle, 0, sizeof(whandle));
502 /* Process DRI-provided buffers and get pipe_resources. */
503 for (i = 0; i < num_buffers; i++) {
504 __DRIbuffer *buf = &buffers[i];
505 enum st_attachment_type statt;
506 enum pipe_format format;
508 switch (buf->attachment) {
509 case __DRI_BUFFER_FRONT_LEFT:
510 if (!screen->auto_fake_front) {
511 continue; /* invalid attachment */
514 case __DRI_BUFFER_FAKE_FRONT_LEFT:
515 statt = ST_ATTACHMENT_FRONT_LEFT;
517 case __DRI_BUFFER_BACK_LEFT:
518 statt = ST_ATTACHMENT_BACK_LEFT;
521 continue; /* invalid attachment */
524 dri_drawable_get_format(drawable, statt, &format, &bind);
525 if (format == PIPE_FORMAT_NONE)
528 /* dri2_drawable_get_buffers has already filled dri_drawable->w
529 * and dri_drawable->h */
530 templ.width0 = dri_drawable->w;
531 templ.height0 = dri_drawable->h;
532 templ.format = format;
534 whandle.handle = buf->name;
535 whandle.stride = buf->pitch;
536 if (screen->can_share_buffer)
537 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
539 whandle.type = DRM_API_HANDLE_TYPE_KMS;
540 drawable->textures[statt] =
541 screen->base.screen->resource_from_handle(screen->base.screen,
543 assert(drawable->textures[statt]);
547 /* Allocate private MSAA colorbuffers. */
548 if (drawable->stvis.samples > 1) {
549 for (i = 0; i < statts_count; i++) {
550 enum st_attachment_type statt = statts[i];
552 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
555 if (drawable->textures[statt]) {
556 templ.format = drawable->textures[statt]->format;
557 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
558 templ.nr_samples = drawable->stvis.samples;
560 /* Try to reuse the resource.
561 * (the other resource parameters should be constant)
563 if (!drawable->msaa_textures[statt] ||
564 drawable->msaa_textures[statt]->width0 != templ.width0 ||
565 drawable->msaa_textures[statt]->height0 != templ.height0) {
566 /* Allocate a new one. */
567 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
569 drawable->msaa_textures[statt] =
570 screen->base.screen->resource_create(screen->base.screen,
572 assert(drawable->msaa_textures[statt]);
574 /* If there are any MSAA resources, we should initialize them
575 * such that they contain the same data as the single-sample
576 * resources we just got from the X server.
578 * The reason for this is that the state tracker (and
579 * therefore the app) can access the MSAA resources only.
580 * The single-sample resources are not exposed
581 * to the state tracker.
584 dri_pipe_blit(ctx->st->pipe,
585 drawable->msaa_textures[statt],
586 drawable->textures[statt]);
590 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
595 /* Allocate a private depth-stencil buffer. */
596 if (alloc_depthstencil) {
597 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
598 struct pipe_resource **zsbuf;
599 enum pipe_format format;
602 dri_drawable_get_format(drawable, statt, &format, &bind);
605 templ.format = format;
608 if (drawable->stvis.samples > 1) {
609 templ.nr_samples = drawable->stvis.samples;
610 zsbuf = &drawable->msaa_textures[statt];
613 templ.nr_samples = 0;
614 zsbuf = &drawable->textures[statt];
617 /* Try to reuse the resource.
618 * (the other resource parameters should be constant)
621 (*zsbuf)->width0 != templ.width0 ||
622 (*zsbuf)->height0 != templ.height0) {
623 /* Allocate a new one. */
624 pipe_resource_reference(zsbuf, NULL);
625 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
631 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
632 pipe_resource_reference(&drawable->textures[statt], NULL);
636 /* For DRI2, we may get the same buffers again from the server.
637 * To prevent useless imports of gem names, drawable->old* is used
638 * to bypass the import if we get the same buffers. This doesn't apply
639 * to DRI3/Wayland, users of image.loader, since the buffer is managed
640 * by the client (no import), and the back buffer is going to change
644 drawable->old_num = num_buffers;
645 drawable->old_w = dri_drawable->w;
646 drawable->old_h = dri_drawable->h;
647 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
652 dri2_flush_frontbuffer(struct dri_context *ctx,
653 struct dri_drawable *drawable,
654 enum st_attachment_type statt)
656 __DRIdrawable *dri_drawable = drawable->dPriv;
657 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
658 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
659 struct pipe_context *pipe = ctx->st->pipe;
661 if (statt != ST_ATTACHMENT_FRONT_LEFT)
664 if (drawable->stvis.samples > 1) {
665 /* Resolve the front buffer. */
666 dri_pipe_blit(ctx->st->pipe,
667 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
668 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
671 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
672 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
675 pipe->flush(pipe, NULL, 0);
678 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
680 else if (loader->flushFrontBuffer) {
681 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
686 dri2_update_tex_buffer(struct dri_drawable *drawable,
687 struct dri_context *ctx,
688 struct pipe_resource *res)
694 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
696 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
699 if (!loader->lookupEGLImage)
702 img = loader->lookupEGLImage(screen->sPriv,
703 handle, screen->sPriv->loaderPrivate);
709 dri2_create_image_from_winsys(__DRIscreen *_screen,
710 int width, int height, int format,
711 struct winsys_handle *whandle, int pitch,
714 struct dri_screen *screen = dri_screen(_screen);
716 struct pipe_resource templ;
720 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
723 case __DRI_IMAGE_FORMAT_RGB565:
724 pf = PIPE_FORMAT_B5G6R5_UNORM;
726 case __DRI_IMAGE_FORMAT_XRGB8888:
727 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
729 case __DRI_IMAGE_FORMAT_ARGB8888:
730 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
732 case __DRI_IMAGE_FORMAT_ABGR8888:
733 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
736 pf = PIPE_FORMAT_NONE;
739 if (pf == PIPE_FORMAT_NONE)
742 img = CALLOC_STRUCT(__DRIimageRec);
746 memset(&templ, 0, sizeof(templ));
747 templ.bind = tex_usage;
749 templ.target = screen->target;
750 templ.last_level = 0;
751 templ.width0 = width;
752 templ.height0 = height;
754 templ.array_size = 1;
756 whandle->stride = pitch * util_format_get_blocksize(pf);
758 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
767 img->dri_format = format;
768 img->loader_private = loaderPrivate;
774 dri2_create_image_from_name(__DRIscreen *_screen,
775 int width, int height, int format,
776 int name, int pitch, void *loaderPrivate)
778 struct winsys_handle whandle;
780 memset(&whandle, 0, sizeof(whandle));
781 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
782 whandle.handle = name;
784 return dri2_create_image_from_winsys(_screen, width, height, format,
785 &whandle, pitch, loaderPrivate);
789 dri2_create_image_from_fd(__DRIscreen *_screen,
790 int width, int height, int format,
791 int fd, int pitch, void *loaderPrivate)
793 struct winsys_handle whandle;
798 memset(&whandle, 0, sizeof(whandle));
799 whandle.type = DRM_API_HANDLE_TYPE_FD;
800 whandle.handle = (unsigned)fd;
802 return dri2_create_image_from_winsys(_screen, width, height, format,
803 &whandle, pitch, loaderPrivate);
807 dri2_create_image_from_renderbuffer(__DRIcontext *context,
808 int renderbuffer, void *loaderPrivate)
810 struct dri_context *ctx = dri_context(context);
812 if (!ctx->st->get_resource_for_egl_image)
820 dri2_create_image(__DRIscreen *_screen,
821 int width, int height, int format,
822 unsigned int use, void *loaderPrivate)
824 struct dri_screen *screen = dri_screen(_screen);
826 struct pipe_resource templ;
830 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
831 if (use & __DRI_IMAGE_USE_SCANOUT)
832 tex_usage |= PIPE_BIND_SCANOUT;
833 if (use & __DRI_IMAGE_USE_SHARE)
834 tex_usage |= PIPE_BIND_SHARED;
835 if (use & __DRI_IMAGE_USE_LINEAR)
836 tex_usage |= PIPE_BIND_LINEAR;
837 if (use & __DRI_IMAGE_USE_CURSOR) {
838 if (width != 64 || height != 64)
840 tex_usage |= PIPE_BIND_CURSOR;
844 case __DRI_IMAGE_FORMAT_RGB565:
845 pf = PIPE_FORMAT_B5G6R5_UNORM;
847 case __DRI_IMAGE_FORMAT_XRGB8888:
848 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
850 case __DRI_IMAGE_FORMAT_ARGB8888:
851 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
853 case __DRI_IMAGE_FORMAT_ABGR8888:
854 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
857 pf = PIPE_FORMAT_NONE;
860 if (pf == PIPE_FORMAT_NONE)
863 img = CALLOC_STRUCT(__DRIimageRec);
867 memset(&templ, 0, sizeof(templ));
868 templ.bind = tex_usage;
870 templ.target = PIPE_TEXTURE_2D;
871 templ.last_level = 0;
872 templ.width0 = width;
873 templ.height0 = height;
875 templ.array_size = 1;
877 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
885 img->dri_format = format;
886 img->dri_components = 0;
888 img->loader_private = loaderPrivate;
893 dri2_query_image(__DRIimage *image, int attrib, int *value)
895 struct winsys_handle whandle;
896 memset(&whandle, 0, sizeof(whandle));
899 case __DRI_IMAGE_ATTRIB_STRIDE:
900 whandle.type = DRM_API_HANDLE_TYPE_KMS;
901 image->texture->screen->resource_get_handle(image->texture->screen,
902 image->texture, &whandle);
903 *value = whandle.stride;
905 case __DRI_IMAGE_ATTRIB_HANDLE:
906 whandle.type = DRM_API_HANDLE_TYPE_KMS;
907 image->texture->screen->resource_get_handle(image->texture->screen,
908 image->texture, &whandle);
909 *value = whandle.handle;
911 case __DRI_IMAGE_ATTRIB_NAME:
912 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
913 image->texture->screen->resource_get_handle(image->texture->screen,
914 image->texture, &whandle);
915 *value = whandle.handle;
917 case __DRI_IMAGE_ATTRIB_FD:
918 whandle.type= DRM_API_HANDLE_TYPE_FD;
919 image->texture->screen->resource_get_handle(image->texture->screen,
920 image->texture, &whandle);
921 *value = whandle.handle;
923 case __DRI_IMAGE_ATTRIB_FORMAT:
924 *value = image->dri_format;
926 case __DRI_IMAGE_ATTRIB_WIDTH:
927 *value = image->texture->width0;
929 case __DRI_IMAGE_ATTRIB_HEIGHT:
930 *value = image->texture->height0;
932 case __DRI_IMAGE_ATTRIB_COMPONENTS:
933 if (image->dri_components == 0)
935 *value = image->dri_components;
937 case __DRI_IMAGE_ATTRIB_FOURCC:
938 *value = convert_to_fourcc(image->dri_format);
940 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
949 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
953 img = CALLOC_STRUCT(__DRIimageRec);
958 pipe_resource_reference(&img->texture, image->texture);
959 img->level = image->level;
960 img->layer = image->layer;
961 img->dri_format = image->dri_format;
962 /* This should be 0 for sub images, but dup is also used for base images. */
963 img->dri_components = image->dri_components;
964 img->loader_private = loaderPrivate;
970 dri2_validate_usage(__DRIimage *image, unsigned int use)
973 * Gallium drivers are bad at adding usages to the resources
974 * once opened again in another process, which is the main use
975 * case for this, so we have to lie.
984 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
985 int *names, int num_names, int *strides, int *offsets,
989 int stride, dri_components;
996 format = convert_fourcc(format, &dri_components);
1000 /* Strides are in bytes not pixels. */
1001 stride = strides[0] /4;
1003 img = dri2_create_image_from_name(screen, width, height, format,
1004 names[0], stride, loaderPrivate);
1008 img->dri_components = dri_components;
1013 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1020 if (image->dri_components == 0)
1023 img = dri2_dup_image(image, loaderPrivate);
1027 /* set this to 0 for sub images. */
1028 img->dri_components = 0;
1033 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1034 int depth, int level, unsigned *error,
1035 void *loaderPrivate)
1038 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1039 struct gl_texture_object *obj;
1040 struct pipe_resource *tex;
1043 obj = _mesa_lookup_texture(ctx, texture);
1044 if (!obj || obj->Target != target) {
1045 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1049 tex = st_get_texobj_resource(obj);
1051 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1055 if (target == GL_TEXTURE_CUBE_MAP)
1058 _mesa_test_texobj_completeness(ctx, obj);
1059 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1060 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1064 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1065 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1069 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1070 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1074 img = CALLOC_STRUCT(__DRIimageRec);
1076 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1082 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1084 img->loader_private = loaderPrivate;
1086 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1087 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1092 pipe_resource_reference(&img->texture, tex);
1094 *error = __DRI_IMAGE_ERROR_SUCCESS;
1099 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1100 int *fds, int num_fds, int *strides, int *offsets,
1101 void *loaderPrivate)
1104 int format, stride, dri_components;
1108 if (offsets[0] != 0)
1111 format = convert_fourcc(fourcc, &dri_components);
1115 /* Strides are in bytes not pixels. */
1116 stride = strides[0] /4;
1118 img = dri2_create_image_from_fd(screen, width, height, format,
1119 fds[0], stride, loaderPrivate);
1123 img->dri_components = dri_components;
1128 dri2_from_dma_bufs(__DRIscreen *screen,
1129 int width, int height, int fourcc,
1130 int *fds, int num_fds,
1131 int *strides, int *offsets,
1132 enum __DRIYUVColorSpace yuv_color_space,
1133 enum __DRISampleRange sample_range,
1134 enum __DRIChromaSiting horizontal_siting,
1135 enum __DRIChromaSiting vertical_siting,
1137 void *loaderPrivate)
1140 int format, stride, dri_components;
1142 if (num_fds != 1 || offsets[0] != 0) {
1143 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1147 format = convert_fourcc(fourcc, &dri_components);
1149 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1153 /* Strides are in bytes not pixels. */
1154 stride = strides[0] /4;
1156 img = dri2_create_image_from_fd(screen, width, height, format,
1157 fds[0], stride, loaderPrivate);
1159 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1163 img->yuv_color_space = yuv_color_space;
1164 img->sample_range = sample_range;
1165 img->horizontal_siting = horizontal_siting;
1166 img->vertical_siting = vertical_siting;
1167 img->dri_components = dri_components;
1169 *error = __DRI_IMAGE_ERROR_SUCCESS;
1174 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1175 int dstx0, int dsty0, int dstwidth, int dstheight,
1176 int srcx0, int srcy0, int srcwidth, int srcheight,
1179 struct dri_context *ctx = dri_context(context);
1180 struct pipe_context *pipe = ctx->st->pipe;
1181 struct pipe_screen *screen;
1182 struct pipe_fence_handle *fence;
1183 struct pipe_blit_info blit;
1188 memset(&blit, 0, sizeof(blit));
1189 blit.dst.resource = dst->texture;
1190 blit.dst.box.x = dstx0;
1191 blit.dst.box.y = dsty0;
1192 blit.dst.box.width = dstwidth;
1193 blit.dst.box.height = dstheight;
1194 blit.dst.box.depth = 1;
1195 blit.dst.format = dst->texture->format;
1196 blit.src.resource = src->texture;
1197 blit.src.box.x = srcx0;
1198 blit.src.box.y = srcy0;
1199 blit.src.box.width = srcwidth;
1200 blit.src.box.height = srcheight;
1201 blit.src.box.depth = 1;
1202 blit.src.format = src->texture->format;
1203 blit.mask = PIPE_MASK_RGBA;
1204 blit.filter = PIPE_TEX_FILTER_NEAREST;
1206 pipe->blit(pipe, &blit);
1208 if (flush_flag == __BLIT_FLAG_FLUSH) {
1209 pipe->flush_resource(pipe, dst->texture);
1210 ctx->st->flush(ctx->st, 0, NULL);
1211 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1212 screen = dri_screen(ctx->sPriv)->base.screen;
1213 pipe->flush_resource(pipe, dst->texture);
1214 ctx->st->flush(ctx->st, 0, &fence);
1215 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1216 screen->fence_reference(screen, &fence, NULL);
1221 dri2_destroy_image(__DRIimage *img)
1223 pipe_resource_reference(&img->texture, NULL);
1228 dri2_get_capabilities(__DRIscreen *_screen)
1230 struct dri_screen *screen = dri_screen(_screen);
1232 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1235 /* The extension is modified during runtime if DRI_PRIME is detected */
1236 static __DRIimageExtension dri2ImageExtension = {
1237 .base = { __DRI_IMAGE, 11 },
1239 .createImageFromName = dri2_create_image_from_name,
1240 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1241 .destroyImage = dri2_destroy_image,
1242 .createImage = dri2_create_image,
1243 .queryImage = dri2_query_image,
1244 .dupImage = dri2_dup_image,
1245 .validateUsage = dri2_validate_usage,
1246 .createImageFromNames = dri2_from_names,
1247 .fromPlanar = dri2_from_planar,
1248 .createImageFromTexture = dri2_create_from_texture,
1249 .createImageFromFds = NULL,
1250 .createImageFromDmaBufs = NULL,
1251 .blitImage = dri2_blit_image,
1252 .getCapabilities = dri2_get_capabilities,
1257 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1259 return screen->opencl_dri_event_add_ref &&
1260 screen->opencl_dri_event_release &&
1261 screen->opencl_dri_event_wait &&
1262 screen->opencl_dri_event_get_fence;
1266 dri2_load_opencl_interop(struct dri_screen *screen)
1268 #if defined(RTLD_DEFAULT)
1271 pipe_mutex_lock(screen->opencl_func_mutex);
1273 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1274 pipe_mutex_unlock(screen->opencl_func_mutex);
1278 screen->opencl_dri_event_add_ref =
1279 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1280 screen->opencl_dri_event_release =
1281 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1282 screen->opencl_dri_event_wait =
1283 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1284 screen->opencl_dri_event_get_fence =
1285 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1287 success = dri2_is_opencl_interop_loaded_locked(screen);
1288 pipe_mutex_unlock(screen->opencl_func_mutex);
1296 struct pipe_fence_handle *pipe_fence;
1301 dri2_create_fence(__DRIcontext *_ctx)
1303 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1304 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1309 ctx->flush(ctx, &fence->pipe_fence, 0);
1311 if (!fence->pipe_fence) {
1320 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1322 struct dri_screen *driscreen = dri_screen(_screen);
1323 struct dri2_fence *fence;
1325 if (!dri2_load_opencl_interop(driscreen))
1328 fence = CALLOC_STRUCT(dri2_fence);
1332 fence->cl_event = (void*)cl_event;
1334 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1343 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1345 struct dri_screen *driscreen = dri_screen(_screen);
1346 struct pipe_screen *screen = driscreen->base.screen;
1347 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1349 if (fence->pipe_fence)
1350 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1351 else if (fence->cl_event)
1352 driscreen->opencl_dri_event_release(fence->cl_event);
1360 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1363 struct dri_screen *driscreen = dri_screen(_ctx->driScreenPriv);
1364 struct pipe_screen *screen = driscreen->base.screen;
1365 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1367 /* No need to flush. The context was flushed when the fence was created. */
1369 if (fence->pipe_fence)
1370 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1371 else if (fence->cl_event) {
1372 struct pipe_fence_handle *pipe_fence =
1373 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1376 return screen->fence_finish(screen, pipe_fence, timeout);
1378 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1387 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1389 /* AFAIK, no driver currently supports parallel context execution. */
1392 static __DRI2fenceExtension dri2FenceExtension = {
1393 .base = { __DRI2_FENCE, 1 },
1395 .create_fence = dri2_create_fence,
1396 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1397 .destroy_fence = dri2_destroy_fence,
1398 .client_wait_sync = dri2_client_wait_sync,
1399 .server_wait_sync = dri2_server_wait_sync
1402 static const __DRIrobustnessExtension dri2Robustness = {
1403 .base = { __DRI2_ROBUSTNESS, 1 }
1407 * Backend function init_screen.
1410 static const __DRIextension *dri_screen_extensions[] = {
1411 &driTexBufferExtension.base,
1412 &dri2FlushExtension.base,
1413 &dri2ImageExtension.base,
1414 &dri2RendererQueryExtension.base,
1415 &dri2ConfigQueryExtension.base,
1416 &dri2ThrottleExtension.base,
1417 &dri2FenceExtension.base,
1421 static const __DRIextension *dri_robust_screen_extensions[] = {
1422 &driTexBufferExtension.base,
1423 &dri2FlushExtension.base,
1424 &dri2ImageExtension.base,
1425 &dri2RendererQueryExtension.base,
1426 &dri2ConfigQueryExtension.base,
1427 &dri2ThrottleExtension.base,
1428 &dri2FenceExtension.base,
1429 &dri2Robustness.base,
1434 * This is the driver specific part of the createNewScreen entry point.
1436 * Returns the struct gl_config supported by this driver.
1438 static const __DRIconfig **
1439 dri2_init_screen(__DRIscreen * sPriv)
1441 const __DRIconfig **configs;
1442 struct dri_screen *screen;
1443 struct pipe_screen *pscreen = NULL;
1444 const struct drm_conf_ret *throttle_ret = NULL;
1445 const struct drm_conf_ret *dmabuf_ret = NULL;
1447 screen = CALLOC_STRUCT(dri_screen);
1451 screen->sPriv = sPriv;
1452 screen->fd = sPriv->fd;
1453 pipe_mutex_init(screen->opencl_func_mutex);
1455 sPriv->driverPrivate = (void *)screen;
1457 #if GALLIUM_STATIC_TARGETS
1458 pscreen = dd_create_screen(screen->fd);
1460 throttle_ret = dd_configuration(DRM_CONF_THROTTLE);
1461 dmabuf_ret = dd_configuration(DRM_CONF_SHARE_FD);
1463 if (pipe_loader_drm_probe_fd(&screen->dev, screen->fd)) {
1464 pscreen = pipe_loader_create_screen(screen->dev, PIPE_SEARCH_DIR);
1466 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1467 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1469 #endif // GALLIUM_STATIC_TARGETS
1471 if (throttle_ret && throttle_ret->val.val_int != -1) {
1472 screen->throttling_enabled = TRUE;
1473 screen->default_throttle_frames = throttle_ret->val.val_int;
1476 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1479 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1480 (cap & DRM_PRIME_CAP_IMPORT)) {
1481 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1482 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1486 if (pscreen && pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1487 sPriv->extensions = dri_robust_screen_extensions;
1488 screen->has_reset_status_query = true;
1491 sPriv->extensions = dri_screen_extensions;
1493 /* dri_init_screen_helper checks pscreen for us */
1495 #if GALLIUM_STATIC_TARGETS
1496 configs = dri_init_screen_helper(screen, pscreen, dd_driver_name());
1498 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1499 #endif // GALLIUM_STATIC_TARGETS
1503 screen->can_share_buffer = true;
1504 screen->auto_fake_front = dri_with_format(sPriv);
1505 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1506 screen->lookup_egl_image = dri2_lookup_egl_image;
1510 dri_destroy_screen_helper(screen);
1511 #if !GALLIUM_STATIC_TARGETS
1513 pipe_loader_release(&screen->dev, 1);
1514 #endif // !GALLIUM_STATIC_TARGETS
1520 * This is the driver specific part of the createNewScreen entry point.
1522 * Returns the struct gl_config supported by this driver.
1524 static const __DRIconfig **
1525 dri_kms_init_screen(__DRIscreen * sPriv)
1527 #if GALLIUM_STATIC_TARGETS
1528 #if defined(GALLIUM_SOFTPIPE)
1529 const __DRIconfig **configs;
1530 struct dri_screen *screen;
1531 struct pipe_screen *pscreen = NULL;
1534 screen = CALLOC_STRUCT(dri_screen);
1538 screen->sPriv = sPriv;
1539 screen->fd = sPriv->fd;
1541 sPriv->driverPrivate = (void *)screen;
1543 pscreen = kms_swrast_create_screen(screen->fd);
1545 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1546 (cap & DRM_PRIME_CAP_IMPORT)) {
1547 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1548 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1551 sPriv->extensions = dri_screen_extensions;
1553 /* dri_init_screen_helper checks pscreen for us */
1554 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1558 screen->can_share_buffer = false;
1559 screen->auto_fake_front = dri_with_format(sPriv);
1560 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1561 screen->lookup_egl_image = dri2_lookup_egl_image;
1565 dri_destroy_screen_helper(screen);
1567 #endif // GALLIUM_SOFTPIPE
1568 #endif // GALLIUM_STATIC_TARGETS
1573 dri2_create_buffer(__DRIscreen * sPriv,
1574 __DRIdrawable * dPriv,
1575 const struct gl_config * visual, boolean isPixmap)
1577 struct dri_drawable *drawable = NULL;
1579 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1582 drawable = dPriv->driverPrivate;
1584 drawable->allocate_textures = dri2_allocate_textures;
1585 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1586 drawable->update_tex_buffer = dri2_update_tex_buffer;
1592 * DRI driver virtual function table.
1594 * DRI versions differ in their implementation of init_screen and swap_buffers.
1596 const struct __DriverAPIRec galliumdrm_driver_api = {
1597 .InitScreen = dri2_init_screen,
1598 .DestroyScreen = dri_destroy_screen,
1599 .CreateContext = dri_create_context,
1600 .DestroyContext = dri_destroy_context,
1601 .CreateBuffer = dri2_create_buffer,
1602 .DestroyBuffer = dri_destroy_buffer,
1603 .MakeCurrent = dri_make_current,
1604 .UnbindContext = dri_unbind_context,
1606 .AllocateBuffer = dri2_allocate_buffer,
1607 .ReleaseBuffer = dri2_release_buffer,
1611 * DRI driver virtual function table.
1613 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1614 * hook. The latter is used to explicitly initialise the kms_swrast driver
1615 * rather than selecting the approapriate driver as suggested by the loader.
1617 const struct __DriverAPIRec dri_kms_driver_api = {
1618 .InitScreen = dri_kms_init_screen,
1619 .DestroyScreen = dri_destroy_screen,
1620 .CreateContext = dri_create_context,
1621 .DestroyContext = dri_destroy_context,
1622 .CreateBuffer = dri2_create_buffer,
1623 .DestroyBuffer = dri_destroy_buffer,
1624 .MakeCurrent = dri_make_current,
1625 .UnbindContext = dri_unbind_context,
1627 .AllocateBuffer = dri2_allocate_buffer,
1628 .ReleaseBuffer = dri2_release_buffer,
1631 /* This is the table of extensions that the loader will dlsym() for. */
1632 const __DRIextension *galliumdrm_driver_extensions[] = {
1633 &driCoreExtension.base,
1634 &driImageDriverExtension.base,
1635 &driDRI2Extension.base,
1636 &gallium_config_options.base,
1640 /* vim: set sw=3 ts=8 sts=3 expandtab: */