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;
104 static enum pipe_format dri2_format_to_pipe_format (int format)
109 case __DRI_IMAGE_FORMAT_RGB565:
110 pf = PIPE_FORMAT_B5G6R5_UNORM;
112 case __DRI_IMAGE_FORMAT_XRGB8888:
113 pf = PIPE_FORMAT_BGRX8888_UNORM;
115 case __DRI_IMAGE_FORMAT_ARGB8888:
116 pf = PIPE_FORMAT_BGRA8888_UNORM;
118 case __DRI_IMAGE_FORMAT_ABGR8888:
119 pf = PIPE_FORMAT_RGBA8888_UNORM;
122 pf = PIPE_FORMAT_NONE;
130 * DRI2 flush extension.
133 dri2_flush_drawable(__DRIdrawable *dPriv)
135 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
139 dri2_invalidate_drawable(__DRIdrawable *dPriv)
141 struct dri_drawable *drawable = dri_drawable(dPriv);
143 dri2InvalidateDrawable(dPriv);
144 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
146 p_atomic_inc(&drawable->base.stamp);
149 static const __DRI2flushExtension dri2FlushExtension = {
150 .base = { __DRI2_FLUSH, 4 },
152 .flush = dri2_flush_drawable,
153 .invalidate = dri2_invalidate_drawable,
154 .flush_with_flags = dri_flush,
158 * Retrieve __DRIbuffer from the DRI loader.
161 dri2_drawable_get_buffers(struct dri_drawable *drawable,
162 const enum st_attachment_type *atts,
165 __DRIdrawable *dri_drawable = drawable->dPriv;
166 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
168 __DRIbuffer *buffers;
170 unsigned attachments[10];
171 unsigned num_attachments, i;
174 with_format = dri_with_format(drawable->sPriv);
178 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
180 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
182 for (i = 0; i < *count; i++) {
183 enum pipe_format format;
187 dri_drawable_get_format(drawable, atts[i], &format, &bind);
188 if (format == PIPE_FORMAT_NONE)
192 case ST_ATTACHMENT_FRONT_LEFT:
196 att = __DRI_BUFFER_FRONT_LEFT;
198 case ST_ATTACHMENT_BACK_LEFT:
199 att = __DRI_BUFFER_BACK_LEFT;
201 case ST_ATTACHMENT_FRONT_RIGHT:
202 att = __DRI_BUFFER_FRONT_RIGHT;
204 case ST_ATTACHMENT_BACK_RIGHT:
205 att = __DRI_BUFFER_BACK_RIGHT;
212 * In this switch statement we must support all formats that
213 * may occur as the stvis->color_format.
216 case PIPE_FORMAT_BGRA8888_UNORM:
219 case PIPE_FORMAT_BGRX8888_UNORM:
222 case PIPE_FORMAT_B5G6R5_UNORM:
226 depth = util_format_get_blocksizebits(format);
227 assert(!"Unexpected format in dri2_drawable_get_buffers()");
230 attachments[num_attachments++] = att;
232 attachments[num_attachments++] = depth;
237 num_attachments /= 2;
238 buffers = loader->getBuffersWithFormat(dri_drawable,
239 &dri_drawable->w, &dri_drawable->h,
240 attachments, num_attachments,
241 &num_buffers, dri_drawable->loaderPrivate);
244 buffers = loader->getBuffers(dri_drawable,
245 &dri_drawable->w, &dri_drawable->h,
246 attachments, num_attachments,
247 &num_buffers, dri_drawable->loaderPrivate);
251 *count = num_buffers;
257 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
258 struct __DRIimageList *images,
259 const enum st_attachment_type *statts,
260 unsigned statts_count)
262 __DRIdrawable *dPriv = drawable->dPriv;
263 __DRIscreen *sPriv = drawable->sPriv;
264 unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
266 uint32_t buffer_mask = 0;
269 for (i = 0; i < statts_count; i++) {
270 dri_drawable_get_format(drawable, statts[i], &pf, &bind);
271 if (pf == PIPE_FORMAT_NONE)
275 case ST_ATTACHMENT_FRONT_LEFT:
276 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
278 case ST_ATTACHMENT_BACK_LEFT:
279 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
286 case PIPE_FORMAT_B5G6R5_UNORM:
287 image_format = __DRI_IMAGE_FORMAT_RGB565;
289 case PIPE_FORMAT_BGRX8888_UNORM:
290 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
292 case PIPE_FORMAT_BGRA8888_UNORM:
293 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
295 case PIPE_FORMAT_RGBA8888_UNORM:
296 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
299 image_format = __DRI_IMAGE_FORMAT_NONE;
304 return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
305 (uint32_t *) &drawable->base.stamp,
306 dPriv->loaderPrivate, buffer_mask,
311 dri2_allocate_buffer(__DRIscreen *sPriv,
312 unsigned attachment, unsigned format,
313 int width, int height)
315 struct dri_screen *screen = dri_screen(sPriv);
316 struct dri2_buffer *buffer;
317 struct pipe_resource templ;
320 struct winsys_handle whandle;
322 switch (attachment) {
323 case __DRI_BUFFER_FRONT_LEFT:
324 case __DRI_BUFFER_FAKE_FRONT_LEFT:
325 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
327 case __DRI_BUFFER_BACK_LEFT:
328 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
330 case __DRI_BUFFER_DEPTH:
331 case __DRI_BUFFER_DEPTH_STENCIL:
332 case __DRI_BUFFER_STENCIL:
333 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
337 /* because we get the handle and stride */
338 bind |= PIPE_BIND_SHARED;
342 pf = PIPE_FORMAT_BGRA8888_UNORM;
345 pf = PIPE_FORMAT_BGRX8888_UNORM;
348 pf = PIPE_FORMAT_Z16_UNORM;
354 buffer = CALLOC_STRUCT(dri2_buffer);
358 memset(&templ, 0, sizeof(templ));
361 templ.target = PIPE_TEXTURE_2D;
362 templ.last_level = 0;
363 templ.width0 = width;
364 templ.height0 = height;
366 templ.array_size = 1;
369 screen->base.screen->resource_create(screen->base.screen, &templ);
370 if (!buffer->resource) {
375 memset(&whandle, 0, sizeof(whandle));
376 if (screen->can_share_buffer)
377 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
379 whandle.type = DRM_API_HANDLE_TYPE_KMS;
381 screen->base.screen->resource_get_handle(screen->base.screen,
382 buffer->resource, &whandle,
383 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
385 buffer->base.attachment = attachment;
386 buffer->base.name = whandle.handle;
387 buffer->base.cpp = util_format_get_blocksize(pf);
388 buffer->base.pitch = whandle.stride;
390 return &buffer->base;
394 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
396 struct dri2_buffer *buffer = dri2_buffer(bPriv);
398 pipe_resource_reference(&buffer->resource, NULL);
403 * Backend functions for st_framebuffer interface.
407 dri2_allocate_textures(struct dri_context *ctx,
408 struct dri_drawable *drawable,
409 const enum st_attachment_type *statts,
410 unsigned statts_count)
412 __DRIscreen *sPriv = drawable->sPriv;
413 __DRIdrawable *dri_drawable = drawable->dPriv;
414 struct dri_screen *screen = dri_screen(sPriv);
415 struct pipe_resource templ;
416 boolean alloc_depthstencil = FALSE;
418 const __DRIimageLoaderExtension *image = sPriv->image.loader;
419 /* Image specific variables */
420 struct __DRIimageList images;
421 /* Dri2 specific variables */
422 __DRIbuffer *buffers = NULL;
423 struct winsys_handle whandle;
424 unsigned num_buffers = statts_count;
426 /* First get the buffers from the loader */
428 if (!dri_image_drawable_get_buffers(drawable, &images,
429 statts, statts_count))
433 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
434 if (!buffers || (drawable->old_num == num_buffers &&
435 drawable->old_w == dri_drawable->w &&
436 drawable->old_h == dri_drawable->h &&
437 memcmp(drawable->old, buffers,
438 sizeof(__DRIbuffer) * num_buffers) == 0))
442 /* Second clean useless resources*/
444 /* See if we need a depth-stencil buffer. */
445 for (i = 0; i < statts_count; i++) {
446 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
447 alloc_depthstencil = TRUE;
452 /* Delete the resources we won't need. */
453 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
454 /* Don't delete the depth-stencil buffer, we can reuse it. */
455 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
458 /* Flush the texture before unreferencing, so that other clients can
459 * see what the driver has rendered.
461 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
462 struct pipe_context *pipe = ctx->st->pipe;
463 pipe->flush_resource(pipe, drawable->textures[i]);
466 pipe_resource_reference(&drawable->textures[i], NULL);
469 if (drawable->stvis.samples > 1) {
470 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
473 /* Don't delete MSAA resources for the attachments which are enabled,
474 * we can reuse them. */
475 for (j = 0; j < statts_count; j++) {
476 if (i == statts[j]) {
483 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
488 /* Third use the buffers retrieved to fill the drawable info */
490 memset(&templ, 0, sizeof(templ));
491 templ.target = screen->target;
492 templ.last_level = 0;
494 templ.array_size = 1;
497 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
498 struct pipe_resource **buf =
499 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
500 struct pipe_resource *texture = images.front->texture;
502 dri_drawable->w = texture->width0;
503 dri_drawable->h = texture->height0;
505 pipe_resource_reference(buf, texture);
508 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
509 struct pipe_resource **buf =
510 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
511 struct pipe_resource *texture = images.back->texture;
513 dri_drawable->w = texture->width0;
514 dri_drawable->h = texture->height0;
516 pipe_resource_reference(buf, texture);
519 /* Note: if there is both a back and a front buffer,
520 * then they have the same size.
522 templ.width0 = dri_drawable->w;
523 templ.height0 = dri_drawable->h;
526 memset(&whandle, 0, sizeof(whandle));
528 /* Process DRI-provided buffers and get pipe_resources. */
529 for (i = 0; i < num_buffers; i++) {
530 __DRIbuffer *buf = &buffers[i];
531 enum st_attachment_type statt;
532 enum pipe_format format;
534 switch (buf->attachment) {
535 case __DRI_BUFFER_FRONT_LEFT:
536 if (!screen->auto_fake_front) {
537 continue; /* invalid attachment */
540 case __DRI_BUFFER_FAKE_FRONT_LEFT:
541 statt = ST_ATTACHMENT_FRONT_LEFT;
543 case __DRI_BUFFER_BACK_LEFT:
544 statt = ST_ATTACHMENT_BACK_LEFT;
547 continue; /* invalid attachment */
550 dri_drawable_get_format(drawable, statt, &format, &bind);
551 if (format == PIPE_FORMAT_NONE)
554 /* dri2_drawable_get_buffers has already filled dri_drawable->w
555 * and dri_drawable->h */
556 templ.width0 = dri_drawable->w;
557 templ.height0 = dri_drawable->h;
558 templ.format = format;
560 whandle.handle = buf->name;
561 whandle.stride = buf->pitch;
563 if (screen->can_share_buffer)
564 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
566 whandle.type = DRM_API_HANDLE_TYPE_KMS;
567 drawable->textures[statt] =
568 screen->base.screen->resource_from_handle(screen->base.screen,
570 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
571 assert(drawable->textures[statt]);
575 /* Allocate private MSAA colorbuffers. */
576 if (drawable->stvis.samples > 1) {
577 for (i = 0; i < statts_count; i++) {
578 enum st_attachment_type statt = statts[i];
580 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
583 if (drawable->textures[statt]) {
584 templ.format = drawable->textures[statt]->format;
585 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
586 templ.nr_samples = drawable->stvis.samples;
588 /* Try to reuse the resource.
589 * (the other resource parameters should be constant)
591 if (!drawable->msaa_textures[statt] ||
592 drawable->msaa_textures[statt]->width0 != templ.width0 ||
593 drawable->msaa_textures[statt]->height0 != templ.height0) {
594 /* Allocate a new one. */
595 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
597 drawable->msaa_textures[statt] =
598 screen->base.screen->resource_create(screen->base.screen,
600 assert(drawable->msaa_textures[statt]);
602 /* If there are any MSAA resources, we should initialize them
603 * such that they contain the same data as the single-sample
604 * resources we just got from the X server.
606 * The reason for this is that the state tracker (and
607 * therefore the app) can access the MSAA resources only.
608 * The single-sample resources are not exposed
609 * to the state tracker.
612 dri_pipe_blit(ctx->st->pipe,
613 drawable->msaa_textures[statt],
614 drawable->textures[statt]);
618 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
623 /* Allocate a private depth-stencil buffer. */
624 if (alloc_depthstencil) {
625 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
626 struct pipe_resource **zsbuf;
627 enum pipe_format format;
630 dri_drawable_get_format(drawable, statt, &format, &bind);
633 templ.format = format;
636 if (drawable->stvis.samples > 1) {
637 templ.nr_samples = drawable->stvis.samples;
638 zsbuf = &drawable->msaa_textures[statt];
641 templ.nr_samples = 0;
642 zsbuf = &drawable->textures[statt];
645 /* Try to reuse the resource.
646 * (the other resource parameters should be constant)
649 (*zsbuf)->width0 != templ.width0 ||
650 (*zsbuf)->height0 != templ.height0) {
651 /* Allocate a new one. */
652 pipe_resource_reference(zsbuf, NULL);
653 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
659 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
660 pipe_resource_reference(&drawable->textures[statt], NULL);
664 /* For DRI2, we may get the same buffers again from the server.
665 * To prevent useless imports of gem names, drawable->old* is used
666 * to bypass the import if we get the same buffers. This doesn't apply
667 * to DRI3/Wayland, users of image.loader, since the buffer is managed
668 * by the client (no import), and the back buffer is going to change
672 drawable->old_num = num_buffers;
673 drawable->old_w = dri_drawable->w;
674 drawable->old_h = dri_drawable->h;
675 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
680 dri2_flush_frontbuffer(struct dri_context *ctx,
681 struct dri_drawable *drawable,
682 enum st_attachment_type statt)
684 __DRIdrawable *dri_drawable = drawable->dPriv;
685 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
686 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
687 struct pipe_context *pipe = ctx->st->pipe;
689 if (statt != ST_ATTACHMENT_FRONT_LEFT)
692 if (drawable->stvis.samples > 1) {
693 /* Resolve the front buffer. */
694 dri_pipe_blit(ctx->st->pipe,
695 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
696 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
699 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
700 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
703 pipe->flush(pipe, NULL, 0);
706 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
708 else if (loader->flushFrontBuffer) {
709 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
714 dri2_update_tex_buffer(struct dri_drawable *drawable,
715 struct dri_context *ctx,
716 struct pipe_resource *res)
722 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
724 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
727 if (!loader->lookupEGLImage)
730 img = loader->lookupEGLImage(screen->sPriv,
731 handle, screen->sPriv->loaderPrivate);
737 dri2_create_image_from_winsys(__DRIscreen *_screen,
738 int width, int height, int format,
739 struct winsys_handle *whandle,
742 struct dri_screen *screen = dri_screen(_screen);
744 struct pipe_resource templ;
748 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
750 pf = dri2_format_to_pipe_format (format);
751 if (pf == PIPE_FORMAT_NONE)
754 img = CALLOC_STRUCT(__DRIimageRec);
758 memset(&templ, 0, sizeof(templ));
759 templ.bind = tex_usage;
761 templ.target = screen->target;
762 templ.last_level = 0;
763 templ.width0 = width;
764 templ.height0 = height;
766 templ.array_size = 1;
770 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
771 &templ, whandle, PIPE_HANDLE_USAGE_READ_WRITE);
779 img->dri_format = format;
781 img->loader_private = loaderPrivate;
787 dri2_create_image_from_name(__DRIscreen *_screen,
788 int width, int height, int format,
789 int name, int pitch, void *loaderPrivate)
791 struct winsys_handle whandle;
794 memset(&whandle, 0, sizeof(whandle));
795 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
796 whandle.handle = name;
798 pf = dri2_format_to_pipe_format (format);
799 if (pf == PIPE_FORMAT_NONE)
802 whandle.stride = pitch * util_format_get_blocksize(pf);
804 return dri2_create_image_from_winsys(_screen, width, height, format,
805 &whandle, loaderPrivate);
809 dri2_create_image_from_fd(__DRIscreen *_screen,
810 int width, int height, int format,
811 int fd, int stride, void *loaderPrivate)
813 struct winsys_handle whandle;
818 memset(&whandle, 0, sizeof(whandle));
819 whandle.type = DRM_API_HANDLE_TYPE_FD;
820 whandle.handle = (unsigned)fd;
821 whandle.stride = stride;
823 return dri2_create_image_from_winsys(_screen, width, height, format,
824 &whandle, loaderPrivate);
828 dri2_create_image_from_renderbuffer(__DRIcontext *context,
829 int renderbuffer, void *loaderPrivate)
831 struct dri_context *ctx = dri_context(context);
833 if (!ctx->st->get_resource_for_egl_image)
841 dri2_create_image(__DRIscreen *_screen,
842 int width, int height, int format,
843 unsigned int use, void *loaderPrivate)
845 struct dri_screen *screen = dri_screen(_screen);
847 struct pipe_resource templ;
851 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
852 if (use & __DRI_IMAGE_USE_SCANOUT)
853 tex_usage |= PIPE_BIND_SCANOUT;
854 if (use & __DRI_IMAGE_USE_SHARE)
855 tex_usage |= PIPE_BIND_SHARED;
856 if (use & __DRI_IMAGE_USE_LINEAR)
857 tex_usage |= PIPE_BIND_LINEAR;
858 if (use & __DRI_IMAGE_USE_CURSOR) {
859 if (width != 64 || height != 64)
861 tex_usage |= PIPE_BIND_CURSOR;
864 pf = dri2_format_to_pipe_format (format);
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,
1003 struct winsys_handle whandle;
1007 if (offsets[0] != 0)
1010 format = convert_fourcc(format, &dri_components);
1014 memset(&whandle, 0, sizeof(whandle));
1015 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1016 whandle.handle = names[0];
1017 whandle.stride = strides[0];
1019 img = dri2_create_image_from_winsys(screen, width, height, format,
1020 &whandle, loaderPrivate);
1024 img->dri_components = dri_components;
1029 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1036 if (image->dri_components == 0)
1039 img = dri2_dup_image(image, loaderPrivate);
1043 /* set this to 0 for sub images. */
1044 img->dri_components = 0;
1049 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1050 int depth, int level, unsigned *error,
1051 void *loaderPrivate)
1054 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1055 struct gl_texture_object *obj;
1056 struct pipe_resource *tex;
1059 obj = _mesa_lookup_texture(ctx, texture);
1060 if (!obj || obj->Target != target) {
1061 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1065 tex = st_get_texobj_resource(obj);
1067 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1071 if (target == GL_TEXTURE_CUBE_MAP)
1074 _mesa_test_texobj_completeness(ctx, obj);
1075 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1076 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1080 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1081 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1085 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1086 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1090 img = CALLOC_STRUCT(__DRIimageRec);
1092 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1098 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1100 img->loader_private = loaderPrivate;
1102 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1103 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1108 pipe_resource_reference(&img->texture, tex);
1110 *error = __DRI_IMAGE_ERROR_SUCCESS;
1115 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1116 int *fds, int num_fds, int *strides, int *offsets,
1117 void *loaderPrivate)
1120 int format, dri_components;
1124 if (offsets[0] != 0)
1127 format = convert_fourcc(fourcc, &dri_components);
1131 img = dri2_create_image_from_fd(screen, width, height, format,
1132 fds[0], strides[0], 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, 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 img = dri2_create_image_from_fd(screen, width, height, format,
1167 fds[0], strides[0], loaderPrivate);
1169 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1173 img->yuv_color_space = yuv_color_space;
1174 img->sample_range = sample_range;
1175 img->horizontal_siting = horizontal_siting;
1176 img->vertical_siting = vertical_siting;
1177 img->dri_components = dri_components;
1179 *error = __DRI_IMAGE_ERROR_SUCCESS;
1184 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1185 int dstx0, int dsty0, int dstwidth, int dstheight,
1186 int srcx0, int srcy0, int srcwidth, int srcheight,
1189 struct dri_context *ctx = dri_context(context);
1190 struct pipe_context *pipe = ctx->st->pipe;
1191 struct pipe_screen *screen;
1192 struct pipe_fence_handle *fence;
1193 struct pipe_blit_info blit;
1198 memset(&blit, 0, sizeof(blit));
1199 blit.dst.resource = dst->texture;
1200 blit.dst.box.x = dstx0;
1201 blit.dst.box.y = dsty0;
1202 blit.dst.box.width = dstwidth;
1203 blit.dst.box.height = dstheight;
1204 blit.dst.box.depth = 1;
1205 blit.dst.format = dst->texture->format;
1206 blit.src.resource = src->texture;
1207 blit.src.box.x = srcx0;
1208 blit.src.box.y = srcy0;
1209 blit.src.box.width = srcwidth;
1210 blit.src.box.height = srcheight;
1211 blit.src.box.depth = 1;
1212 blit.src.format = src->texture->format;
1213 blit.mask = PIPE_MASK_RGBA;
1214 blit.filter = PIPE_TEX_FILTER_NEAREST;
1216 pipe->blit(pipe, &blit);
1218 if (flush_flag == __BLIT_FLAG_FLUSH) {
1219 pipe->flush_resource(pipe, dst->texture);
1220 ctx->st->flush(ctx->st, 0, NULL);
1221 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1222 screen = dri_screen(ctx->sPriv)->base.screen;
1223 pipe->flush_resource(pipe, dst->texture);
1224 ctx->st->flush(ctx->st, 0, &fence);
1225 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1226 screen->fence_reference(screen, &fence, NULL);
1231 dri2_destroy_image(__DRIimage *img)
1233 pipe_resource_reference(&img->texture, NULL);
1238 dri2_get_capabilities(__DRIscreen *_screen)
1240 struct dri_screen *screen = dri_screen(_screen);
1242 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1245 /* The extension is modified during runtime if DRI_PRIME is detected */
1246 static __DRIimageExtension dri2ImageExtension = {
1247 .base = { __DRI_IMAGE, 11 },
1249 .createImageFromName = dri2_create_image_from_name,
1250 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1251 .destroyImage = dri2_destroy_image,
1252 .createImage = dri2_create_image,
1253 .queryImage = dri2_query_image,
1254 .dupImage = dri2_dup_image,
1255 .validateUsage = dri2_validate_usage,
1256 .createImageFromNames = dri2_from_names,
1257 .fromPlanar = dri2_from_planar,
1258 .createImageFromTexture = dri2_create_from_texture,
1259 .createImageFromFds = NULL,
1260 .createImageFromDmaBufs = NULL,
1261 .blitImage = dri2_blit_image,
1262 .getCapabilities = dri2_get_capabilities,
1267 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1269 return screen->opencl_dri_event_add_ref &&
1270 screen->opencl_dri_event_release &&
1271 screen->opencl_dri_event_wait &&
1272 screen->opencl_dri_event_get_fence;
1276 dri2_load_opencl_interop(struct dri_screen *screen)
1278 #if defined(RTLD_DEFAULT)
1281 pipe_mutex_lock(screen->opencl_func_mutex);
1283 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1284 pipe_mutex_unlock(screen->opencl_func_mutex);
1288 screen->opencl_dri_event_add_ref =
1289 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1290 screen->opencl_dri_event_release =
1291 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1292 screen->opencl_dri_event_wait =
1293 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1294 screen->opencl_dri_event_get_fence =
1295 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1297 success = dri2_is_opencl_interop_loaded_locked(screen);
1298 pipe_mutex_unlock(screen->opencl_func_mutex);
1306 struct dri_screen *driscreen;
1307 struct pipe_fence_handle *pipe_fence;
1312 dri2_create_fence(__DRIcontext *_ctx)
1314 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1315 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1320 ctx->flush(ctx, &fence->pipe_fence, 0);
1322 if (!fence->pipe_fence) {
1327 fence->driscreen = dri_screen(_ctx->driScreenPriv);
1332 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1334 struct dri_screen *driscreen = dri_screen(_screen);
1335 struct dri2_fence *fence;
1337 if (!dri2_load_opencl_interop(driscreen))
1340 fence = CALLOC_STRUCT(dri2_fence);
1344 fence->cl_event = (void*)cl_event;
1346 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1351 fence->driscreen = driscreen;
1356 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1358 struct dri_screen *driscreen = dri_screen(_screen);
1359 struct pipe_screen *screen = driscreen->base.screen;
1360 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1362 if (fence->pipe_fence)
1363 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1364 else if (fence->cl_event)
1365 driscreen->opencl_dri_event_release(fence->cl_event);
1373 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1376 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1377 struct dri_screen *driscreen = fence->driscreen;
1378 struct pipe_screen *screen = driscreen->base.screen;
1380 /* No need to flush. The context was flushed when the fence was created. */
1382 if (fence->pipe_fence)
1383 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1384 else if (fence->cl_event) {
1385 struct pipe_fence_handle *pipe_fence =
1386 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1389 return screen->fence_finish(screen, pipe_fence, timeout);
1391 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1400 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1402 /* AFAIK, no driver currently supports parallel context execution. */
1405 static __DRI2fenceExtension dri2FenceExtension = {
1406 .base = { __DRI2_FENCE, 1 },
1408 .create_fence = dri2_create_fence,
1409 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1410 .destroy_fence = dri2_destroy_fence,
1411 .client_wait_sync = dri2_client_wait_sync,
1412 .server_wait_sync = dri2_server_wait_sync
1415 static const __DRIrobustnessExtension dri2Robustness = {
1416 .base = { __DRI2_ROBUSTNESS, 1 }
1420 * Backend function init_screen.
1423 static const __DRIextension *dri_screen_extensions[] = {
1424 &driTexBufferExtension.base,
1425 &dri2FlushExtension.base,
1426 &dri2ImageExtension.base,
1427 &dri2RendererQueryExtension.base,
1428 &dri2ConfigQueryExtension.base,
1429 &dri2ThrottleExtension.base,
1430 &dri2FenceExtension.base,
1434 static const __DRIextension *dri_robust_screen_extensions[] = {
1435 &driTexBufferExtension.base,
1436 &dri2FlushExtension.base,
1437 &dri2ImageExtension.base,
1438 &dri2RendererQueryExtension.base,
1439 &dri2ConfigQueryExtension.base,
1440 &dri2ThrottleExtension.base,
1441 &dri2FenceExtension.base,
1442 &dri2Robustness.base,
1447 * This is the driver specific part of the createNewScreen entry point.
1449 * Returns the struct gl_config supported by this driver.
1451 static const __DRIconfig **
1452 dri2_init_screen(__DRIscreen * sPriv)
1454 const __DRIconfig **configs;
1455 struct dri_screen *screen;
1456 struct pipe_screen *pscreen = NULL;
1457 const struct drm_conf_ret *throttle_ret;
1458 const struct drm_conf_ret *dmabuf_ret;
1461 screen = CALLOC_STRUCT(dri_screen);
1465 screen->sPriv = sPriv;
1466 screen->fd = sPriv->fd;
1467 pipe_mutex_init(screen->opencl_func_mutex);
1469 sPriv->driverPrivate = (void *)screen;
1471 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1474 if (pipe_loader_drm_probe_fd(&screen->dev, fd))
1475 pscreen = pipe_loader_create_screen(screen->dev);
1480 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1481 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1483 if (throttle_ret && throttle_ret->val.val_int != -1) {
1484 screen->throttling_enabled = TRUE;
1485 screen->default_throttle_frames = throttle_ret->val.val_int;
1488 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1491 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1492 (cap & DRM_PRIME_CAP_IMPORT)) {
1493 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1494 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1498 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1499 sPriv->extensions = dri_robust_screen_extensions;
1500 screen->has_reset_status_query = true;
1503 sPriv->extensions = dri_screen_extensions;
1505 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1509 screen->can_share_buffer = true;
1510 screen->auto_fake_front = dri_with_format(sPriv);
1511 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1512 screen->lookup_egl_image = dri2_lookup_egl_image;
1516 dri_destroy_screen_helper(screen);
1518 pipe_loader_release(&screen->dev, 1);
1526 * This is the driver specific part of the createNewScreen entry point.
1528 * Returns the struct gl_config supported by this driver.
1530 static const __DRIconfig **
1531 dri_kms_init_screen(__DRIscreen * sPriv)
1533 #if defined(GALLIUM_SOFTPIPE)
1534 const __DRIconfig **configs;
1535 struct dri_screen *screen;
1536 struct pipe_screen *pscreen = NULL;
1540 screen = CALLOC_STRUCT(dri_screen);
1544 screen->sPriv = sPriv;
1545 screen->fd = sPriv->fd;
1547 sPriv->driverPrivate = (void *)screen;
1549 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1552 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
1553 pscreen = pipe_loader_create_screen(screen->dev);
1558 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1559 (cap & DRM_PRIME_CAP_IMPORT)) {
1560 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1561 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1564 sPriv->extensions = dri_screen_extensions;
1566 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1570 screen->can_share_buffer = false;
1571 screen->auto_fake_front = dri_with_format(sPriv);
1572 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1573 screen->lookup_egl_image = dri2_lookup_egl_image;
1577 dri_destroy_screen_helper(screen);
1579 pipe_loader_release(&screen->dev, 1);
1583 #endif // GALLIUM_SOFTPIPE
1588 dri2_create_buffer(__DRIscreen * sPriv,
1589 __DRIdrawable * dPriv,
1590 const struct gl_config * visual, boolean isPixmap)
1592 struct dri_drawable *drawable = NULL;
1594 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1597 drawable = dPriv->driverPrivate;
1599 drawable->allocate_textures = dri2_allocate_textures;
1600 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1601 drawable->update_tex_buffer = dri2_update_tex_buffer;
1607 * DRI driver virtual function table.
1609 * DRI versions differ in their implementation of init_screen and swap_buffers.
1611 const struct __DriverAPIRec galliumdrm_driver_api = {
1612 .InitScreen = dri2_init_screen,
1613 .DestroyScreen = dri_destroy_screen,
1614 .CreateContext = dri_create_context,
1615 .DestroyContext = dri_destroy_context,
1616 .CreateBuffer = dri2_create_buffer,
1617 .DestroyBuffer = dri_destroy_buffer,
1618 .MakeCurrent = dri_make_current,
1619 .UnbindContext = dri_unbind_context,
1621 .AllocateBuffer = dri2_allocate_buffer,
1622 .ReleaseBuffer = dri2_release_buffer,
1626 * DRI driver virtual function table.
1628 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1629 * hook. The latter is used to explicitly initialise the kms_swrast driver
1630 * rather than selecting the approapriate driver as suggested by the loader.
1632 const struct __DriverAPIRec dri_kms_driver_api = {
1633 .InitScreen = dri_kms_init_screen,
1634 .DestroyScreen = dri_destroy_screen,
1635 .CreateContext = dri_create_context,
1636 .DestroyContext = dri_destroy_context,
1637 .CreateBuffer = dri2_create_buffer,
1638 .DestroyBuffer = dri_destroy_buffer,
1639 .MakeCurrent = dri_make_current,
1640 .UnbindContext = dri_unbind_context,
1642 .AllocateBuffer = dri2_allocate_buffer,
1643 .ReleaseBuffer = dri2_release_buffer,
1646 /* This is the table of extensions that the loader will dlsym() for. */
1647 const __DRIextension *galliumdrm_driver_extensions[] = {
1648 &driCoreExtension.base,
1649 &driImageDriverExtension.base,
1650 &driDRI2Extension.base,
1651 &gallium_config_options.base,
1655 /* vim: set sw=3 ts=8 sts=3 expandtab: */