2 * Mesa 3-D graphics library
4 * Copyright 2009, VMware, Inc.
6 * Copyright (C) 2010 LunarG Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
28 * <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
33 #include "GL/mesa_glinterop.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_debug.h"
38 #include "state_tracker/drm_driver.h"
39 #include "state_tracker/st_cb_bufferobjects.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_texture.h"
43 #include "state_tracker/st_context.h"
44 #include "pipe-loader/pipe_loader.h"
45 #include "main/bufferobj.h"
46 #include "main/texobj.h"
48 #include "dri_screen.h"
49 #include "dri_context.h"
50 #include "dri_drawable.h"
51 #include "dri_query_renderer.h"
52 #include "dri2_buffer.h"
54 static int convert_fourcc(int format, int *dri_components_p)
58 case __DRI_IMAGE_FOURCC_RGB565:
59 format = __DRI_IMAGE_FORMAT_RGB565;
60 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
62 case __DRI_IMAGE_FOURCC_ARGB8888:
63 format = __DRI_IMAGE_FORMAT_ARGB8888;
64 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
66 case __DRI_IMAGE_FOURCC_XRGB8888:
67 format = __DRI_IMAGE_FORMAT_XRGB8888;
68 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
70 case __DRI_IMAGE_FOURCC_ABGR8888:
71 format = __DRI_IMAGE_FORMAT_ABGR8888;
72 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
74 case __DRI_IMAGE_FOURCC_XBGR8888:
75 format = __DRI_IMAGE_FORMAT_XBGR8888;
76 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
78 case __DRI_IMAGE_FOURCC_R8:
79 format = __DRI_IMAGE_FORMAT_R8;
80 dri_components = __DRI_IMAGE_COMPONENTS_R;
82 case __DRI_IMAGE_FOURCC_GR88:
83 format = __DRI_IMAGE_FORMAT_GR88;
84 dri_components = __DRI_IMAGE_COMPONENTS_RG;
89 *dri_components_p = dri_components;
93 static int convert_to_fourcc(int format)
96 case __DRI_IMAGE_FORMAT_RGB565:
97 format = __DRI_IMAGE_FOURCC_RGB565;
99 case __DRI_IMAGE_FORMAT_ARGB8888:
100 format = __DRI_IMAGE_FOURCC_ARGB8888;
102 case __DRI_IMAGE_FORMAT_XRGB8888:
103 format = __DRI_IMAGE_FOURCC_XRGB8888;
105 case __DRI_IMAGE_FORMAT_ABGR8888:
106 format = __DRI_IMAGE_FOURCC_ABGR8888;
108 case __DRI_IMAGE_FORMAT_XBGR8888:
109 format = __DRI_IMAGE_FOURCC_XBGR8888;
111 case __DRI_IMAGE_FORMAT_R8:
112 format = __DRI_IMAGE_FOURCC_R8;
114 case __DRI_IMAGE_FORMAT_GR88:
115 format = __DRI_IMAGE_FOURCC_GR88;
123 static enum pipe_format dri2_format_to_pipe_format (int format)
128 case __DRI_IMAGE_FORMAT_RGB565:
129 pf = PIPE_FORMAT_B5G6R5_UNORM;
131 case __DRI_IMAGE_FORMAT_XRGB8888:
132 pf = PIPE_FORMAT_BGRX8888_UNORM;
134 case __DRI_IMAGE_FORMAT_ARGB8888:
135 pf = PIPE_FORMAT_BGRA8888_UNORM;
137 case __DRI_IMAGE_FORMAT_ABGR8888:
138 pf = PIPE_FORMAT_RGBA8888_UNORM;
140 case __DRI_IMAGE_FORMAT_R8:
141 pf = PIPE_FORMAT_R8_UNORM;
143 case __DRI_IMAGE_FORMAT_GR88:
144 pf = PIPE_FORMAT_RG88_UNORM;
147 pf = PIPE_FORMAT_NONE;
155 * DRI2 flush extension.
158 dri2_flush_drawable(__DRIdrawable *dPriv)
160 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
164 dri2_invalidate_drawable(__DRIdrawable *dPriv)
166 struct dri_drawable *drawable = dri_drawable(dPriv);
168 dri2InvalidateDrawable(dPriv);
169 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
171 p_atomic_inc(&drawable->base.stamp);
174 static const __DRI2flushExtension dri2FlushExtension = {
175 .base = { __DRI2_FLUSH, 4 },
177 .flush = dri2_flush_drawable,
178 .invalidate = dri2_invalidate_drawable,
179 .flush_with_flags = dri_flush,
183 * Retrieve __DRIbuffer from the DRI loader.
186 dri2_drawable_get_buffers(struct dri_drawable *drawable,
187 const enum st_attachment_type *atts,
190 __DRIdrawable *dri_drawable = drawable->dPriv;
191 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
193 __DRIbuffer *buffers;
195 unsigned attachments[10];
196 unsigned num_attachments, i;
199 with_format = dri_with_format(drawable->sPriv);
203 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
205 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
207 for (i = 0; i < *count; i++) {
208 enum pipe_format format;
212 dri_drawable_get_format(drawable, atts[i], &format, &bind);
213 if (format == PIPE_FORMAT_NONE)
217 case ST_ATTACHMENT_FRONT_LEFT:
221 att = __DRI_BUFFER_FRONT_LEFT;
223 case ST_ATTACHMENT_BACK_LEFT:
224 att = __DRI_BUFFER_BACK_LEFT;
226 case ST_ATTACHMENT_FRONT_RIGHT:
227 att = __DRI_BUFFER_FRONT_RIGHT;
229 case ST_ATTACHMENT_BACK_RIGHT:
230 att = __DRI_BUFFER_BACK_RIGHT;
237 * In this switch statement we must support all formats that
238 * may occur as the stvis->color_format.
241 case PIPE_FORMAT_BGRA8888_UNORM:
244 case PIPE_FORMAT_BGRX8888_UNORM:
247 case PIPE_FORMAT_B5G6R5_UNORM:
251 depth = util_format_get_blocksizebits(format);
252 assert(!"Unexpected format in dri2_drawable_get_buffers()");
255 attachments[num_attachments++] = att;
257 attachments[num_attachments++] = depth;
262 num_attachments /= 2;
263 buffers = loader->getBuffersWithFormat(dri_drawable,
264 &dri_drawable->w, &dri_drawable->h,
265 attachments, num_attachments,
266 &num_buffers, dri_drawable->loaderPrivate);
269 buffers = loader->getBuffers(dri_drawable,
270 &dri_drawable->w, &dri_drawable->h,
271 attachments, num_attachments,
272 &num_buffers, dri_drawable->loaderPrivate);
276 *count = num_buffers;
282 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
283 struct __DRIimageList *images,
284 const enum st_attachment_type *statts,
285 unsigned statts_count)
287 __DRIdrawable *dPriv = drawable->dPriv;
288 __DRIscreen *sPriv = drawable->sPriv;
289 unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
291 uint32_t buffer_mask = 0;
294 for (i = 0; i < statts_count; i++) {
295 dri_drawable_get_format(drawable, statts[i], &pf, &bind);
296 if (pf == PIPE_FORMAT_NONE)
300 case ST_ATTACHMENT_FRONT_LEFT:
301 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
303 case ST_ATTACHMENT_BACK_LEFT:
304 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
311 case PIPE_FORMAT_B5G6R5_UNORM:
312 image_format = __DRI_IMAGE_FORMAT_RGB565;
314 case PIPE_FORMAT_BGRX8888_UNORM:
315 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
317 case PIPE_FORMAT_BGRA8888_UNORM:
318 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
320 case PIPE_FORMAT_RGBA8888_UNORM:
321 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
324 image_format = __DRI_IMAGE_FORMAT_NONE;
329 return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
330 (uint32_t *) &drawable->base.stamp,
331 dPriv->loaderPrivate, buffer_mask,
336 dri2_allocate_buffer(__DRIscreen *sPriv,
337 unsigned attachment, unsigned format,
338 int width, int height)
340 struct dri_screen *screen = dri_screen(sPriv);
341 struct dri2_buffer *buffer;
342 struct pipe_resource templ;
345 struct winsys_handle whandle;
347 switch (attachment) {
348 case __DRI_BUFFER_FRONT_LEFT:
349 case __DRI_BUFFER_FAKE_FRONT_LEFT:
350 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
352 case __DRI_BUFFER_BACK_LEFT:
353 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
355 case __DRI_BUFFER_DEPTH:
356 case __DRI_BUFFER_DEPTH_STENCIL:
357 case __DRI_BUFFER_STENCIL:
358 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
362 /* because we get the handle and stride */
363 bind |= PIPE_BIND_SHARED;
367 pf = PIPE_FORMAT_BGRA8888_UNORM;
370 pf = PIPE_FORMAT_BGRX8888_UNORM;
373 pf = PIPE_FORMAT_Z16_UNORM;
379 buffer = CALLOC_STRUCT(dri2_buffer);
383 memset(&templ, 0, sizeof(templ));
386 templ.target = PIPE_TEXTURE_2D;
387 templ.last_level = 0;
388 templ.width0 = width;
389 templ.height0 = height;
391 templ.array_size = 1;
394 screen->base.screen->resource_create(screen->base.screen, &templ);
395 if (!buffer->resource) {
400 memset(&whandle, 0, sizeof(whandle));
401 if (screen->can_share_buffer)
402 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
404 whandle.type = DRM_API_HANDLE_TYPE_KMS;
406 screen->base.screen->resource_get_handle(screen->base.screen,
407 buffer->resource, &whandle,
408 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
410 buffer->base.attachment = attachment;
411 buffer->base.name = whandle.handle;
412 buffer->base.cpp = util_format_get_blocksize(pf);
413 buffer->base.pitch = whandle.stride;
415 return &buffer->base;
419 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
421 struct dri2_buffer *buffer = dri2_buffer(bPriv);
423 pipe_resource_reference(&buffer->resource, NULL);
428 * Backend functions for st_framebuffer interface.
432 dri2_allocate_textures(struct dri_context *ctx,
433 struct dri_drawable *drawable,
434 const enum st_attachment_type *statts,
435 unsigned statts_count)
437 __DRIscreen *sPriv = drawable->sPriv;
438 __DRIdrawable *dri_drawable = drawable->dPriv;
439 struct dri_screen *screen = dri_screen(sPriv);
440 struct pipe_resource templ;
441 boolean alloc_depthstencil = FALSE;
443 const __DRIimageLoaderExtension *image = sPriv->image.loader;
444 /* Image specific variables */
445 struct __DRIimageList images;
446 /* Dri2 specific variables */
447 __DRIbuffer *buffers = NULL;
448 struct winsys_handle whandle;
449 unsigned num_buffers = statts_count;
451 /* First get the buffers from the loader */
453 if (!dri_image_drawable_get_buffers(drawable, &images,
454 statts, statts_count))
458 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
459 if (!buffers || (drawable->old_num == num_buffers &&
460 drawable->old_w == dri_drawable->w &&
461 drawable->old_h == dri_drawable->h &&
462 memcmp(drawable->old, buffers,
463 sizeof(__DRIbuffer) * num_buffers) == 0))
467 /* Second clean useless resources*/
469 /* See if we need a depth-stencil buffer. */
470 for (i = 0; i < statts_count; i++) {
471 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
472 alloc_depthstencil = TRUE;
477 /* Delete the resources we won't need. */
478 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
479 /* Don't delete the depth-stencil buffer, we can reuse it. */
480 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
483 /* Flush the texture before unreferencing, so that other clients can
484 * see what the driver has rendered.
486 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
487 struct pipe_context *pipe = ctx->st->pipe;
488 pipe->flush_resource(pipe, drawable->textures[i]);
491 pipe_resource_reference(&drawable->textures[i], NULL);
494 if (drawable->stvis.samples > 1) {
495 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
498 /* Don't delete MSAA resources for the attachments which are enabled,
499 * we can reuse them. */
500 for (j = 0; j < statts_count; j++) {
501 if (i == statts[j]) {
508 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
513 /* Third use the buffers retrieved to fill the drawable info */
515 memset(&templ, 0, sizeof(templ));
516 templ.target = screen->target;
517 templ.last_level = 0;
519 templ.array_size = 1;
522 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
523 struct pipe_resource **buf =
524 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
525 struct pipe_resource *texture = images.front->texture;
527 dri_drawable->w = texture->width0;
528 dri_drawable->h = texture->height0;
530 pipe_resource_reference(buf, texture);
533 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
534 struct pipe_resource **buf =
535 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
536 struct pipe_resource *texture = images.back->texture;
538 dri_drawable->w = texture->width0;
539 dri_drawable->h = texture->height0;
541 pipe_resource_reference(buf, texture);
544 /* Note: if there is both a back and a front buffer,
545 * then they have the same size.
547 templ.width0 = dri_drawable->w;
548 templ.height0 = dri_drawable->h;
551 memset(&whandle, 0, sizeof(whandle));
553 /* Process DRI-provided buffers and get pipe_resources. */
554 for (i = 0; i < num_buffers; i++) {
555 __DRIbuffer *buf = &buffers[i];
556 enum st_attachment_type statt;
557 enum pipe_format format;
559 switch (buf->attachment) {
560 case __DRI_BUFFER_FRONT_LEFT:
561 if (!screen->auto_fake_front) {
562 continue; /* invalid attachment */
565 case __DRI_BUFFER_FAKE_FRONT_LEFT:
566 statt = ST_ATTACHMENT_FRONT_LEFT;
568 case __DRI_BUFFER_BACK_LEFT:
569 statt = ST_ATTACHMENT_BACK_LEFT;
572 continue; /* invalid attachment */
575 dri_drawable_get_format(drawable, statt, &format, &bind);
576 if (format == PIPE_FORMAT_NONE)
579 /* dri2_drawable_get_buffers has already filled dri_drawable->w
580 * and dri_drawable->h */
581 templ.width0 = dri_drawable->w;
582 templ.height0 = dri_drawable->h;
583 templ.format = format;
585 whandle.handle = buf->name;
586 whandle.stride = buf->pitch;
588 if (screen->can_share_buffer)
589 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
591 whandle.type = DRM_API_HANDLE_TYPE_KMS;
592 drawable->textures[statt] =
593 screen->base.screen->resource_from_handle(screen->base.screen,
595 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
596 assert(drawable->textures[statt]);
600 /* Allocate private MSAA colorbuffers. */
601 if (drawable->stvis.samples > 1) {
602 for (i = 0; i < statts_count; i++) {
603 enum st_attachment_type statt = statts[i];
605 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
608 if (drawable->textures[statt]) {
609 templ.format = drawable->textures[statt]->format;
610 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
611 templ.nr_samples = drawable->stvis.samples;
613 /* Try to reuse the resource.
614 * (the other resource parameters should be constant)
616 if (!drawable->msaa_textures[statt] ||
617 drawable->msaa_textures[statt]->width0 != templ.width0 ||
618 drawable->msaa_textures[statt]->height0 != templ.height0) {
619 /* Allocate a new one. */
620 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
622 drawable->msaa_textures[statt] =
623 screen->base.screen->resource_create(screen->base.screen,
625 assert(drawable->msaa_textures[statt]);
627 /* If there are any MSAA resources, we should initialize them
628 * such that they contain the same data as the single-sample
629 * resources we just got from the X server.
631 * The reason for this is that the state tracker (and
632 * therefore the app) can access the MSAA resources only.
633 * The single-sample resources are not exposed
634 * to the state tracker.
637 dri_pipe_blit(ctx->st->pipe,
638 drawable->msaa_textures[statt],
639 drawable->textures[statt]);
643 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
648 /* Allocate a private depth-stencil buffer. */
649 if (alloc_depthstencil) {
650 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
651 struct pipe_resource **zsbuf;
652 enum pipe_format format;
655 dri_drawable_get_format(drawable, statt, &format, &bind);
658 templ.format = format;
661 if (drawable->stvis.samples > 1) {
662 templ.nr_samples = drawable->stvis.samples;
663 zsbuf = &drawable->msaa_textures[statt];
666 templ.nr_samples = 0;
667 zsbuf = &drawable->textures[statt];
670 /* Try to reuse the resource.
671 * (the other resource parameters should be constant)
674 (*zsbuf)->width0 != templ.width0 ||
675 (*zsbuf)->height0 != templ.height0) {
676 /* Allocate a new one. */
677 pipe_resource_reference(zsbuf, NULL);
678 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
684 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
685 pipe_resource_reference(&drawable->textures[statt], NULL);
689 /* For DRI2, we may get the same buffers again from the server.
690 * To prevent useless imports of gem names, drawable->old* is used
691 * to bypass the import if we get the same buffers. This doesn't apply
692 * to DRI3/Wayland, users of image.loader, since the buffer is managed
693 * by the client (no import), and the back buffer is going to change
697 drawable->old_num = num_buffers;
698 drawable->old_w = dri_drawable->w;
699 drawable->old_h = dri_drawable->h;
700 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
705 dri2_flush_frontbuffer(struct dri_context *ctx,
706 struct dri_drawable *drawable,
707 enum st_attachment_type statt)
709 __DRIdrawable *dri_drawable = drawable->dPriv;
710 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
711 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
712 struct pipe_context *pipe = ctx->st->pipe;
714 if (statt != ST_ATTACHMENT_FRONT_LEFT)
717 if (drawable->stvis.samples > 1) {
718 /* Resolve the front buffer. */
719 dri_pipe_blit(ctx->st->pipe,
720 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
721 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
724 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
725 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
728 pipe->flush(pipe, NULL, 0);
731 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
733 else if (loader->flushFrontBuffer) {
734 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
739 dri2_update_tex_buffer(struct dri_drawable *drawable,
740 struct dri_context *ctx,
741 struct pipe_resource *res)
747 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
749 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
752 if (!loader->lookupEGLImage)
755 img = loader->lookupEGLImage(screen->sPriv,
756 handle, screen->sPriv->loaderPrivate);
762 dri2_create_image_from_winsys(__DRIscreen *_screen,
763 int width, int height, int format,
764 struct winsys_handle *whandle,
767 struct dri_screen *screen = dri_screen(_screen);
769 struct pipe_resource templ;
773 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
775 pf = dri2_format_to_pipe_format (format);
776 if (pf == PIPE_FORMAT_NONE)
779 img = CALLOC_STRUCT(__DRIimageRec);
783 memset(&templ, 0, sizeof(templ));
784 templ.bind = tex_usage;
786 templ.target = screen->target;
787 templ.last_level = 0;
788 templ.width0 = width;
789 templ.height0 = height;
791 templ.array_size = 1;
795 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
796 &templ, whandle, PIPE_HANDLE_USAGE_READ_WRITE);
804 img->dri_format = format;
806 img->loader_private = loaderPrivate;
812 dri2_create_image_from_name(__DRIscreen *_screen,
813 int width, int height, int format,
814 int name, int pitch, void *loaderPrivate)
816 struct winsys_handle whandle;
819 memset(&whandle, 0, sizeof(whandle));
820 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
821 whandle.handle = name;
823 pf = dri2_format_to_pipe_format (format);
824 if (pf == PIPE_FORMAT_NONE)
827 whandle.stride = pitch * util_format_get_blocksize(pf);
829 return dri2_create_image_from_winsys(_screen, width, height, format,
830 &whandle, loaderPrivate);
834 dri2_create_image_from_fd(__DRIscreen *_screen,
835 int width, int height, int fourcc,
836 int *fds, int num_fds, int *strides,
837 int *offsets, unsigned *error,
838 int *dri_components, void *loaderPrivate)
840 struct winsys_handle whandle;
842 __DRIimage *img = NULL;
843 unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
845 if (num_fds != 1 || offsets[0] != 0) {
846 err = __DRI_IMAGE_ERROR_BAD_MATCH;
850 format = convert_fourcc(fourcc, dri_components);
852 err = __DRI_IMAGE_ERROR_BAD_MATCH;
857 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
861 memset(&whandle, 0, sizeof(whandle));
862 whandle.type = DRM_API_HANDLE_TYPE_FD;
863 whandle.handle = (unsigned)fds[0];
864 whandle.stride = (unsigned)strides[0];
865 whandle.offset = (unsigned)offsets[0];
867 img = dri2_create_image_from_winsys(_screen, width, height, format,
868 &whandle, loaderPrivate);
870 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
880 dri2_create_image_from_renderbuffer(__DRIcontext *context,
881 int renderbuffer, void *loaderPrivate)
883 struct dri_context *ctx = dri_context(context);
885 if (!ctx->st->get_resource_for_egl_image)
893 dri2_create_image(__DRIscreen *_screen,
894 int width, int height, int format,
895 unsigned int use, void *loaderPrivate)
897 struct dri_screen *screen = dri_screen(_screen);
899 struct pipe_resource templ;
903 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
904 if (use & __DRI_IMAGE_USE_SCANOUT)
905 tex_usage |= PIPE_BIND_SCANOUT;
906 if (use & __DRI_IMAGE_USE_SHARE)
907 tex_usage |= PIPE_BIND_SHARED;
908 if (use & __DRI_IMAGE_USE_LINEAR)
909 tex_usage |= PIPE_BIND_LINEAR;
910 if (use & __DRI_IMAGE_USE_CURSOR) {
911 if (width != 64 || height != 64)
913 tex_usage |= PIPE_BIND_CURSOR;
916 pf = dri2_format_to_pipe_format (format);
917 if (pf == PIPE_FORMAT_NONE)
920 img = CALLOC_STRUCT(__DRIimageRec);
924 memset(&templ, 0, sizeof(templ));
925 templ.bind = tex_usage;
927 templ.target = PIPE_TEXTURE_2D;
928 templ.last_level = 0;
929 templ.width0 = width;
930 templ.height0 = height;
932 templ.array_size = 1;
934 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
942 img->dri_format = format;
943 img->dri_components = 0;
946 img->loader_private = loaderPrivate;
951 dri2_query_image(__DRIimage *image, int attrib, int *value)
953 struct winsys_handle whandle;
956 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
957 usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
959 usage = PIPE_HANDLE_USAGE_READ_WRITE;
961 memset(&whandle, 0, sizeof(whandle));
964 case __DRI_IMAGE_ATTRIB_STRIDE:
965 whandle.type = DRM_API_HANDLE_TYPE_KMS;
966 image->texture->screen->resource_get_handle(image->texture->screen,
967 image->texture, &whandle, usage);
968 *value = whandle.stride;
970 case __DRI_IMAGE_ATTRIB_HANDLE:
971 whandle.type = DRM_API_HANDLE_TYPE_KMS;
972 image->texture->screen->resource_get_handle(image->texture->screen,
973 image->texture, &whandle, usage);
974 *value = whandle.handle;
976 case __DRI_IMAGE_ATTRIB_NAME:
977 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
978 image->texture->screen->resource_get_handle(image->texture->screen,
979 image->texture, &whandle, usage);
980 *value = whandle.handle;
982 case __DRI_IMAGE_ATTRIB_FD:
983 whandle.type= DRM_API_HANDLE_TYPE_FD;
984 image->texture->screen->resource_get_handle(image->texture->screen,
985 image->texture, &whandle, usage);
986 *value = whandle.handle;
988 case __DRI_IMAGE_ATTRIB_FORMAT:
989 *value = image->dri_format;
991 case __DRI_IMAGE_ATTRIB_WIDTH:
992 *value = image->texture->width0;
994 case __DRI_IMAGE_ATTRIB_HEIGHT:
995 *value = image->texture->height0;
997 case __DRI_IMAGE_ATTRIB_COMPONENTS:
998 if (image->dri_components == 0)
1000 *value = image->dri_components;
1002 case __DRI_IMAGE_ATTRIB_FOURCC:
1003 *value = convert_to_fourcc(image->dri_format);
1005 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1014 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1018 img = CALLOC_STRUCT(__DRIimageRec);
1022 img->texture = NULL;
1023 pipe_resource_reference(&img->texture, image->texture);
1024 img->level = image->level;
1025 img->layer = image->layer;
1026 img->dri_format = image->dri_format;
1027 /* This should be 0 for sub images, but dup is also used for base images. */
1028 img->dri_components = image->dri_components;
1029 img->loader_private = loaderPrivate;
1035 dri2_validate_usage(__DRIimage *image, unsigned int use)
1038 * Gallium drivers are bad at adding usages to the resources
1039 * once opened again in another process, which is the main use
1040 * case for this, so we have to lie.
1049 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1050 int *names, int num_names, int *strides, int *offsets,
1051 void *loaderPrivate)
1055 struct winsys_handle whandle;
1059 if (offsets[0] != 0)
1062 format = convert_fourcc(format, &dri_components);
1066 memset(&whandle, 0, sizeof(whandle));
1067 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
1068 whandle.handle = names[0];
1069 whandle.stride = strides[0];
1071 img = dri2_create_image_from_winsys(screen, width, height, format,
1072 &whandle, loaderPrivate);
1076 img->dri_components = dri_components;
1081 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1088 if (image->dri_components == 0)
1091 img = dri2_dup_image(image, loaderPrivate);
1095 /* set this to 0 for sub images. */
1096 img->dri_components = 0;
1101 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1102 int depth, int level, unsigned *error,
1103 void *loaderPrivate)
1106 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1107 struct gl_texture_object *obj;
1108 struct pipe_resource *tex;
1111 obj = _mesa_lookup_texture(ctx, texture);
1112 if (!obj || obj->Target != target) {
1113 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1117 tex = st_get_texobj_resource(obj);
1119 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1123 if (target == GL_TEXTURE_CUBE_MAP)
1126 _mesa_test_texobj_completeness(ctx, obj);
1127 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1128 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1132 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1133 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1137 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1138 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1142 img = CALLOC_STRUCT(__DRIimageRec);
1144 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1150 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1152 img->loader_private = loaderPrivate;
1154 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1155 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1160 pipe_resource_reference(&img->texture, tex);
1162 *error = __DRI_IMAGE_ERROR_SUCCESS;
1167 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1168 int *fds, int num_fds, int *strides, int *offsets,
1169 void *loaderPrivate)
1174 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1175 fds, num_fds, strides, offsets, NULL,
1176 &dri_components, loaderPrivate);
1180 img->dri_components = dri_components;
1185 dri2_from_dma_bufs(__DRIscreen *screen,
1186 int width, int height, int fourcc,
1187 int *fds, int num_fds,
1188 int *strides, int *offsets,
1189 enum __DRIYUVColorSpace yuv_color_space,
1190 enum __DRISampleRange sample_range,
1191 enum __DRIChromaSiting horizontal_siting,
1192 enum __DRIChromaSiting vertical_siting,
1194 void *loaderPrivate)
1199 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1200 fds, num_fds, strides, offsets, error,
1201 &dri_components, loaderPrivate);
1205 img->yuv_color_space = yuv_color_space;
1206 img->sample_range = sample_range;
1207 img->horizontal_siting = horizontal_siting;
1208 img->vertical_siting = vertical_siting;
1209 img->dri_components = dri_components;
1211 *error = __DRI_IMAGE_ERROR_SUCCESS;
1216 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1217 int dstx0, int dsty0, int dstwidth, int dstheight,
1218 int srcx0, int srcy0, int srcwidth, int srcheight,
1221 struct dri_context *ctx = dri_context(context);
1222 struct pipe_context *pipe = ctx->st->pipe;
1223 struct pipe_screen *screen;
1224 struct pipe_fence_handle *fence;
1225 struct pipe_blit_info blit;
1230 memset(&blit, 0, sizeof(blit));
1231 blit.dst.resource = dst->texture;
1232 blit.dst.box.x = dstx0;
1233 blit.dst.box.y = dsty0;
1234 blit.dst.box.width = dstwidth;
1235 blit.dst.box.height = dstheight;
1236 blit.dst.box.depth = 1;
1237 blit.dst.format = dst->texture->format;
1238 blit.src.resource = src->texture;
1239 blit.src.box.x = srcx0;
1240 blit.src.box.y = srcy0;
1241 blit.src.box.width = srcwidth;
1242 blit.src.box.height = srcheight;
1243 blit.src.box.depth = 1;
1244 blit.src.format = src->texture->format;
1245 blit.mask = PIPE_MASK_RGBA;
1246 blit.filter = PIPE_TEX_FILTER_NEAREST;
1248 pipe->blit(pipe, &blit);
1250 if (flush_flag == __BLIT_FLAG_FLUSH) {
1251 pipe->flush_resource(pipe, dst->texture);
1252 ctx->st->flush(ctx->st, 0, NULL);
1253 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1254 screen = dri_screen(ctx->sPriv)->base.screen;
1255 pipe->flush_resource(pipe, dst->texture);
1256 ctx->st->flush(ctx->st, 0, &fence);
1257 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1258 screen->fence_reference(screen, &fence, NULL);
1263 dri2_map_image(__DRIcontext *context, __DRIimage *image,
1264 int x0, int y0, int width, int height,
1265 unsigned int flags, int *stride, void **data)
1267 struct dri_context *ctx = dri_context(context);
1268 struct pipe_context *pipe = ctx->st->pipe;
1269 enum pipe_transfer_usage pipe_access = 0;
1270 struct pipe_transfer *trans;
1273 if (!image || !data || *data)
1276 if (flags & __DRI_IMAGE_TRANSFER_READ)
1277 pipe_access |= PIPE_TRANSFER_READ;
1278 if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1279 pipe_access |= PIPE_TRANSFER_WRITE;
1281 map = pipe_transfer_map(pipe, image->texture,
1282 0, 0, pipe_access, x0, y0, width, height,
1286 *stride = trans->stride;
1293 dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1295 struct dri_context *ctx = dri_context(context);
1296 struct pipe_context *pipe = ctx->st->pipe;
1298 pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
1302 dri2_destroy_image(__DRIimage *img)
1304 pipe_resource_reference(&img->texture, NULL);
1309 dri2_get_capabilities(__DRIscreen *_screen)
1311 struct dri_screen *screen = dri_screen(_screen);
1313 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1316 /* The extension is modified during runtime if DRI_PRIME is detected */
1317 static __DRIimageExtension dri2ImageExtension = {
1318 .base = { __DRI_IMAGE, 12 },
1320 .createImageFromName = dri2_create_image_from_name,
1321 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1322 .destroyImage = dri2_destroy_image,
1323 .createImage = dri2_create_image,
1324 .queryImage = dri2_query_image,
1325 .dupImage = dri2_dup_image,
1326 .validateUsage = dri2_validate_usage,
1327 .createImageFromNames = dri2_from_names,
1328 .fromPlanar = dri2_from_planar,
1329 .createImageFromTexture = dri2_create_from_texture,
1330 .createImageFromFds = NULL,
1331 .createImageFromDmaBufs = NULL,
1332 .blitImage = dri2_blit_image,
1333 .getCapabilities = dri2_get_capabilities,
1334 .mapImage = dri2_map_image,
1335 .unmapImage = dri2_unmap_image,
1340 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1342 return screen->opencl_dri_event_add_ref &&
1343 screen->opencl_dri_event_release &&
1344 screen->opencl_dri_event_wait &&
1345 screen->opencl_dri_event_get_fence;
1349 dri2_load_opencl_interop(struct dri_screen *screen)
1351 #if defined(RTLD_DEFAULT)
1354 pipe_mutex_lock(screen->opencl_func_mutex);
1356 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1357 pipe_mutex_unlock(screen->opencl_func_mutex);
1361 screen->opencl_dri_event_add_ref =
1362 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1363 screen->opencl_dri_event_release =
1364 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1365 screen->opencl_dri_event_wait =
1366 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1367 screen->opencl_dri_event_get_fence =
1368 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1370 success = dri2_is_opencl_interop_loaded_locked(screen);
1371 pipe_mutex_unlock(screen->opencl_func_mutex);
1379 struct dri_screen *driscreen;
1380 struct pipe_fence_handle *pipe_fence;
1385 dri2_create_fence(__DRIcontext *_ctx)
1387 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1388 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1393 ctx->flush(ctx, &fence->pipe_fence, 0);
1395 if (!fence->pipe_fence) {
1400 fence->driscreen = dri_screen(_ctx->driScreenPriv);
1405 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1407 struct dri_screen *driscreen = dri_screen(_screen);
1408 struct dri2_fence *fence;
1410 if (!dri2_load_opencl_interop(driscreen))
1413 fence = CALLOC_STRUCT(dri2_fence);
1417 fence->cl_event = (void*)cl_event;
1419 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1424 fence->driscreen = driscreen;
1429 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1431 struct dri_screen *driscreen = dri_screen(_screen);
1432 struct pipe_screen *screen = driscreen->base.screen;
1433 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1435 if (fence->pipe_fence)
1436 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1437 else if (fence->cl_event)
1438 driscreen->opencl_dri_event_release(fence->cl_event);
1446 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1449 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1450 struct dri_screen *driscreen = fence->driscreen;
1451 struct pipe_screen *screen = driscreen->base.screen;
1453 /* No need to flush. The context was flushed when the fence was created. */
1455 if (fence->pipe_fence)
1456 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1457 else if (fence->cl_event) {
1458 struct pipe_fence_handle *pipe_fence =
1459 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1462 return screen->fence_finish(screen, pipe_fence, timeout);
1464 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1473 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1475 /* AFAIK, no driver currently supports parallel context execution. */
1478 static __DRI2fenceExtension dri2FenceExtension = {
1479 .base = { __DRI2_FENCE, 1 },
1481 .create_fence = dri2_create_fence,
1482 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1483 .destroy_fence = dri2_destroy_fence,
1484 .client_wait_sync = dri2_client_wait_sync,
1485 .server_wait_sync = dri2_server_wait_sync
1488 static const __DRIrobustnessExtension dri2Robustness = {
1489 .base = { __DRI2_ROBUSTNESS, 1 }
1493 dri2_interop_query_device_info(__DRIcontext *_ctx,
1494 mesa_glinterop_device_info *out)
1496 struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1498 /* There is no version 0, thus we do not support it */
1499 if (out->version == 0)
1500 return MESA_GLINTEROP_INVALID_VERSION;
1502 out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1503 out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1504 out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1505 out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1507 out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1508 out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1510 /* Instruct the caller that we support up-to version one of the interface */
1513 return MESA_GLINTEROP_SUCCESS;
1517 dri2_interop_export_object(__DRIcontext *_ctx,
1518 mesa_glinterop_export_in *in,
1519 mesa_glinterop_export_out *out)
1521 struct st_context_iface *st = dri_context(_ctx)->st;
1522 struct pipe_screen *screen = st->pipe->screen;
1523 struct gl_context *ctx = ((struct st_context *)st)->ctx;
1524 struct pipe_resource *res = NULL;
1525 struct winsys_handle whandle;
1526 unsigned target, usage;
1529 /* There is no version 0, thus we do not support it */
1530 if (in->version == 0 || out->version == 0)
1531 return MESA_GLINTEROP_INVALID_VERSION;
1533 /* Validate the target. */
1534 switch (in->target) {
1535 case GL_TEXTURE_BUFFER:
1539 case GL_TEXTURE_RECTANGLE:
1540 case GL_TEXTURE_1D_ARRAY:
1541 case GL_TEXTURE_2D_ARRAY:
1542 case GL_TEXTURE_CUBE_MAP_ARRAY:
1543 case GL_TEXTURE_CUBE_MAP:
1544 case GL_TEXTURE_2D_MULTISAMPLE:
1545 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1546 case GL_TEXTURE_EXTERNAL_OES:
1547 case GL_RENDERBUFFER:
1548 case GL_ARRAY_BUFFER:
1549 target = in->target;
1551 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1552 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1553 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1554 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1555 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1556 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1557 target = GL_TEXTURE_CUBE_MAP;
1560 return MESA_GLINTEROP_INVALID_TARGET;
1563 /* Validate the simple case of miplevel. */
1564 if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1566 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1568 /* Validate the OpenGL object and get pipe_resource. */
1569 mtx_lock(&ctx->Shared->Mutex);
1571 if (target == GL_ARRAY_BUFFER) {
1574 * The error checking is based on the documentation of
1575 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1577 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1579 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1580 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1581 * a GL buffer object but does not have an existing data store or
1582 * the size of the buffer is 0."
1584 if (!buf || buf->Size == 0) {
1585 mtx_unlock(&ctx->Shared->Mutex);
1586 return MESA_GLINTEROP_INVALID_OBJECT;
1589 res = st_buffer_object(buf)->buffer;
1591 /* this shouldn't happen */
1592 mtx_unlock(&ctx->Shared->Mutex);
1593 return MESA_GLINTEROP_INVALID_OBJECT;
1596 out->buf_offset = 0;
1597 out->buf_size = buf->Size;
1599 buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1600 } else if (target == GL_RENDERBUFFER) {
1603 * The error checking is based on the documentation of
1604 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1606 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1608 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1609 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1610 * object or if the width or height of renderbuffer is zero."
1612 if (!rb || rb->Width == 0 || rb->Height == 0) {
1613 mtx_unlock(&ctx->Shared->Mutex);
1614 return MESA_GLINTEROP_INVALID_OBJECT;
1617 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1618 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1619 * renderbuffer object."
1621 if (rb->NumSamples > 1) {
1622 mtx_unlock(&ctx->Shared->Mutex);
1623 return MESA_GLINTEROP_INVALID_OPERATION;
1626 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1627 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1628 * required by the OpenCL implementation on the device."
1630 res = st_renderbuffer(rb)->texture;
1632 mtx_unlock(&ctx->Shared->Mutex);
1633 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1636 out->internal_format = rb->InternalFormat;
1637 out->view_minlevel = 0;
1638 out->view_numlevels = 1;
1639 out->view_minlayer = 0;
1640 out->view_numlayers = 1;
1644 * The error checking is based on the documentation of
1645 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1647 struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1650 _mesa_test_texobj_completeness(ctx, obj);
1652 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1653 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1654 * type matches texture_target, if the specified miplevel of texture
1655 * is not defined, or if the width or height of the specified
1656 * miplevel is zero or if the GL texture object is incomplete."
1659 obj->Target != target ||
1660 !obj->_BaseComplete ||
1661 (in->miplevel > 0 && !obj->_MipmapComplete)) {
1662 mtx_unlock(&ctx->Shared->Mutex);
1663 return MESA_GLINTEROP_INVALID_OBJECT;
1666 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1667 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1668 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1669 * implementations); or greater than the value of q (for both OpenGL
1670 * and OpenGL ES). levelbase and q are defined for the texture in
1671 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1672 * specification and section 3.7.10 of the OpenGL ES 2.0."
1674 if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
1675 mtx_unlock(&ctx->Shared->Mutex);
1676 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1679 if (!st_finalize_texture(ctx, st->pipe, obj)) {
1680 mtx_unlock(&ctx->Shared->Mutex);
1681 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1684 res = st_get_texobj_resource(obj);
1686 /* Incomplete texture buffer object? This shouldn't really occur. */
1687 mtx_unlock(&ctx->Shared->Mutex);
1688 return MESA_GLINTEROP_INVALID_OBJECT;
1691 if (target == GL_TEXTURE_BUFFER) {
1692 out->internal_format = obj->BufferObjectFormat;
1693 out->buf_offset = obj->BufferOffset;
1694 out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1697 obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1699 out->internal_format = obj->Image[0][0]->InternalFormat;
1700 out->view_minlevel = obj->MinLevel;
1701 out->view_numlevels = obj->NumLevels;
1702 out->view_minlayer = obj->MinLayer;
1703 out->view_numlayers = obj->NumLayers;
1707 /* Get the handle. */
1708 switch (in->access) {
1709 case MESA_GLINTEROP_ACCESS_READ_WRITE:
1710 usage = PIPE_HANDLE_USAGE_READ_WRITE;
1712 case MESA_GLINTEROP_ACCESS_READ_ONLY:
1713 usage = PIPE_HANDLE_USAGE_READ;
1715 case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
1716 usage = PIPE_HANDLE_USAGE_WRITE;
1722 memset(&whandle, 0, sizeof(whandle));
1723 whandle.type = DRM_API_HANDLE_TYPE_FD;
1725 success = screen->resource_get_handle(screen, res, &whandle, usage);
1726 mtx_unlock(&ctx->Shared->Mutex);
1729 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
1731 out->dmabuf_fd = whandle.handle;
1732 out->out_driver_data_written = 0;
1734 if (res->target == PIPE_BUFFER)
1735 out->buf_offset += whandle.offset;
1737 /* Instruct the caller that we support up-to version one of the interface */
1741 return MESA_GLINTEROP_SUCCESS;
1744 static const __DRI2interopExtension dri2InteropExtension = {
1745 .base = { __DRI2_INTEROP, 1 },
1746 .query_device_info = dri2_interop_query_device_info,
1747 .export_object = dri2_interop_export_object
1751 * Backend function init_screen.
1754 static const __DRIextension *dri_screen_extensions[] = {
1755 &driTexBufferExtension.base,
1756 &dri2FlushExtension.base,
1757 &dri2ImageExtension.base,
1758 &dri2RendererQueryExtension.base,
1759 &dri2ConfigQueryExtension.base,
1760 &dri2ThrottleExtension.base,
1761 &dri2FenceExtension.base,
1762 &dri2InteropExtension.base,
1766 static const __DRIextension *dri_robust_screen_extensions[] = {
1767 &driTexBufferExtension.base,
1768 &dri2FlushExtension.base,
1769 &dri2ImageExtension.base,
1770 &dri2RendererQueryExtension.base,
1771 &dri2ConfigQueryExtension.base,
1772 &dri2ThrottleExtension.base,
1773 &dri2FenceExtension.base,
1774 &dri2InteropExtension.base,
1775 &dri2Robustness.base,
1780 * This is the driver specific part of the createNewScreen entry point.
1782 * Returns the struct gl_config supported by this driver.
1784 static const __DRIconfig **
1785 dri2_init_screen(__DRIscreen * sPriv)
1787 const __DRIconfig **configs;
1788 struct dri_screen *screen;
1789 struct pipe_screen *pscreen = NULL;
1790 const struct drm_conf_ret *throttle_ret;
1791 const struct drm_conf_ret *dmabuf_ret;
1794 screen = CALLOC_STRUCT(dri_screen);
1798 screen->sPriv = sPriv;
1799 screen->fd = sPriv->fd;
1800 pipe_mutex_init(screen->opencl_func_mutex);
1802 sPriv->driverPrivate = (void *)screen;
1804 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1807 if (pipe_loader_drm_probe_fd(&screen->dev, fd))
1808 pscreen = pipe_loader_create_screen(screen->dev);
1813 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1814 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1816 if (throttle_ret && throttle_ret->val.val_int != -1) {
1817 screen->throttling_enabled = TRUE;
1818 screen->default_throttle_frames = throttle_ret->val.val_int;
1821 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1824 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1825 (cap & DRM_PRIME_CAP_IMPORT)) {
1826 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1827 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1831 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1832 sPriv->extensions = dri_robust_screen_extensions;
1833 screen->has_reset_status_query = true;
1836 sPriv->extensions = dri_screen_extensions;
1838 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1840 goto destroy_screen;
1842 screen->can_share_buffer = true;
1843 screen->auto_fake_front = dri_with_format(sPriv);
1844 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1845 screen->lookup_egl_image = dri2_lookup_egl_image;
1850 dri_destroy_screen_helper(screen);
1854 pipe_loader_release(&screen->dev, 1);
1864 * This is the driver specific part of the createNewScreen entry point.
1866 * Returns the struct gl_config supported by this driver.
1868 static const __DRIconfig **
1869 dri_kms_init_screen(__DRIscreen * sPriv)
1871 #if defined(GALLIUM_SOFTPIPE)
1872 const __DRIconfig **configs;
1873 struct dri_screen *screen;
1874 struct pipe_screen *pscreen = NULL;
1878 screen = CALLOC_STRUCT(dri_screen);
1882 screen->sPriv = sPriv;
1883 screen->fd = sPriv->fd;
1885 sPriv->driverPrivate = (void *)screen;
1887 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1890 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
1891 pscreen = pipe_loader_create_screen(screen->dev);
1896 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1897 (cap & DRM_PRIME_CAP_IMPORT)) {
1898 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1899 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1902 sPriv->extensions = dri_screen_extensions;
1904 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1906 goto destroy_screen;
1908 screen->can_share_buffer = false;
1909 screen->auto_fake_front = dri_with_format(sPriv);
1910 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1911 screen->lookup_egl_image = dri2_lookup_egl_image;
1916 dri_destroy_screen_helper(screen);
1920 pipe_loader_release(&screen->dev, 1);
1926 #endif // GALLIUM_SOFTPIPE
1931 dri2_create_buffer(__DRIscreen * sPriv,
1932 __DRIdrawable * dPriv,
1933 const struct gl_config * visual, boolean isPixmap)
1935 struct dri_drawable *drawable = NULL;
1937 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1940 drawable = dPriv->driverPrivate;
1942 drawable->allocate_textures = dri2_allocate_textures;
1943 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1944 drawable->update_tex_buffer = dri2_update_tex_buffer;
1950 * DRI driver virtual function table.
1952 * DRI versions differ in their implementation of init_screen and swap_buffers.
1954 const struct __DriverAPIRec galliumdrm_driver_api = {
1955 .InitScreen = dri2_init_screen,
1956 .DestroyScreen = dri_destroy_screen,
1957 .CreateContext = dri_create_context,
1958 .DestroyContext = dri_destroy_context,
1959 .CreateBuffer = dri2_create_buffer,
1960 .DestroyBuffer = dri_destroy_buffer,
1961 .MakeCurrent = dri_make_current,
1962 .UnbindContext = dri_unbind_context,
1964 .AllocateBuffer = dri2_allocate_buffer,
1965 .ReleaseBuffer = dri2_release_buffer,
1969 * DRI driver virtual function table.
1971 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1972 * hook. The latter is used to explicitly initialise the kms_swrast driver
1973 * rather than selecting the approapriate driver as suggested by the loader.
1975 const struct __DriverAPIRec dri_kms_driver_api = {
1976 .InitScreen = dri_kms_init_screen,
1977 .DestroyScreen = dri_destroy_screen,
1978 .CreateContext = dri_create_context,
1979 .DestroyContext = dri_destroy_context,
1980 .CreateBuffer = dri2_create_buffer,
1981 .DestroyBuffer = dri_destroy_buffer,
1982 .MakeCurrent = dri_make_current,
1983 .UnbindContext = dri_unbind_context,
1985 .AllocateBuffer = dri2_allocate_buffer,
1986 .ReleaseBuffer = dri2_release_buffer,
1989 /* This is the table of extensions that the loader will dlsym() for. */
1990 const __DRIextension *galliumdrm_driver_extensions[] = {
1991 &driCoreExtension.base,
1992 &driImageDriverExtension.base,
1993 &driDRI2Extension.base,
1994 &gallium_config_options.base,
1998 /* vim: set sw=3 ts=8 sts=3 expandtab: */