101e04c3fSmrg/*
201e04c3fSmrg * Mesa 3-D graphics library
301e04c3fSmrg *
401e04c3fSmrg * Copyright 2016 Advanced Micro Devices, Inc.
501e04c3fSmrg *
601e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
701e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
801e04c3fSmrg * to deal in the Software without restriction, including without limitation
901e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1001e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
1101e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1201e04c3fSmrg *
1301e04c3fSmrg * The above copyright notice and this permission notice shall be included
1401e04c3fSmrg * in all copies or substantial portions of the Software.
1501e04c3fSmrg *
1601e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1701e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1801e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1901e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2001e04c3fSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2101e04c3fSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2201e04c3fSmrg * OTHER DEALINGS IN THE SOFTWARE.
2301e04c3fSmrg */
2401e04c3fSmrg
2501e04c3fSmrg/* Mesa OpenGL inter-driver interoperability interface designed for but not
2601e04c3fSmrg * limited to OpenCL.
2701e04c3fSmrg *
2801e04c3fSmrg * This is a driver-agnostic, backward-compatible interface. The structures
2901e04c3fSmrg * are only allowed to grow. They can never shrink and their members can
3001e04c3fSmrg * never be removed, renamed, or redefined.
3101e04c3fSmrg *
3201e04c3fSmrg * The interface doesn't return a lot of static texture parameters like
3301e04c3fSmrg * width, height, etc. It mainly returns mutable buffer and texture view
3401e04c3fSmrg * parameters that can't be part of the texture allocation (because they are
3501e04c3fSmrg * mutable). If drivers want to return more data or want to return static
3601e04c3fSmrg * allocation parameters, they can do it in one of these two ways:
3701e04c3fSmrg * - attaching the data to the DMABUF handle in a driver-specific way
3801e04c3fSmrg * - passing the data via "out_driver_data" in the "in" structure.
3901e04c3fSmrg *
4001e04c3fSmrg * Mesa is expected to do a lot of error checking on behalf of OpenCL, such
4101e04c3fSmrg * as checking the target, miplevel, and texture completeness.
4201e04c3fSmrg *
4301e04c3fSmrg * OpenCL, on the other hand, needs to check if the display+context combo
4401e04c3fSmrg * is compatible with the OpenCL driver by querying the device information.
4501e04c3fSmrg * It also needs to check if the texture internal format and channel ordering
4601e04c3fSmrg * (returned in a driver-specific way) is supported by OpenCL, among other
4701e04c3fSmrg * things.
4801e04c3fSmrg */
4901e04c3fSmrg
5001e04c3fSmrg#ifndef MESA_GLINTEROP_H
5101e04c3fSmrg#define MESA_GLINTEROP_H
5201e04c3fSmrg
5301e04c3fSmrg#include <stddef.h>
5401e04c3fSmrg#include <stdint.h>
5501e04c3fSmrg
5601e04c3fSmrg#ifdef __cplusplus
5701e04c3fSmrgextern "C" {
5801e04c3fSmrg#endif
5901e04c3fSmrg
6001e04c3fSmrg/* Forward declarations to avoid inclusion of GL/glx.h */
6101e04c3fSmrg#ifndef GLX_H
6201e04c3fSmrgstruct _XDisplay;
6301e04c3fSmrgstruct __GLXcontextRec;
6401e04c3fSmrg#endif
6501e04c3fSmrg
6601e04c3fSmrg/* Forward declarations to avoid inclusion of EGL/egl.h */
6701e04c3fSmrg#ifndef __egl_h_
6801e04c3fSmrgtypedef void *EGLDisplay;
6901e04c3fSmrgtypedef void *EGLContext;
7001e04c3fSmrg#endif
7101e04c3fSmrg
7201e04c3fSmrg/** Returned error codes. */
7301e04c3fSmrgenum {
7401e04c3fSmrg   MESA_GLINTEROP_SUCCESS = 0,
7501e04c3fSmrg   MESA_GLINTEROP_OUT_OF_RESOURCES,
7601e04c3fSmrg   MESA_GLINTEROP_OUT_OF_HOST_MEMORY,
7701e04c3fSmrg   MESA_GLINTEROP_INVALID_OPERATION,
7801e04c3fSmrg   MESA_GLINTEROP_INVALID_VERSION,
7901e04c3fSmrg   MESA_GLINTEROP_INVALID_DISPLAY,
8001e04c3fSmrg   MESA_GLINTEROP_INVALID_CONTEXT,
8101e04c3fSmrg   MESA_GLINTEROP_INVALID_TARGET,
8201e04c3fSmrg   MESA_GLINTEROP_INVALID_OBJECT,
8301e04c3fSmrg   MESA_GLINTEROP_INVALID_MIP_LEVEL,
8401e04c3fSmrg   MESA_GLINTEROP_UNSUPPORTED
8501e04c3fSmrg};
8601e04c3fSmrg
8701e04c3fSmrg/** Access flags. */
8801e04c3fSmrgenum {
8901e04c3fSmrg   MESA_GLINTEROP_ACCESS_READ_WRITE = 0,
9001e04c3fSmrg   MESA_GLINTEROP_ACCESS_READ_ONLY,
9101e04c3fSmrg   MESA_GLINTEROP_ACCESS_WRITE_ONLY
9201e04c3fSmrg};
9301e04c3fSmrg
9401e04c3fSmrg#define MESA_GLINTEROP_DEVICE_INFO_VERSION 1
9501e04c3fSmrg
9601e04c3fSmrg/**
9701e04c3fSmrg * Device information returned by Mesa.
9801e04c3fSmrg */
9901e04c3fSmrgstruct mesa_glinterop_device_info {
10001e04c3fSmrg   /* The caller should set this to the version of the struct they support */
10101e04c3fSmrg   /* The callee will overwrite it if it supports a lower version.
10201e04c3fSmrg    *
10301e04c3fSmrg    * The caller should check the value and access up-to the version supported
10401e04c3fSmrg    * by the callee.
10501e04c3fSmrg    */
10601e04c3fSmrg   /* NOTE: Do not use the MESA_GLINTEROP_DEVICE_INFO_VERSION macro */
10701e04c3fSmrg   uint32_t version;
10801e04c3fSmrg
10901e04c3fSmrg   /* PCI location */
11001e04c3fSmrg   uint32_t pci_segment_group;
11101e04c3fSmrg   uint32_t pci_bus;
11201e04c3fSmrg   uint32_t pci_device;
11301e04c3fSmrg   uint32_t pci_function;
11401e04c3fSmrg
11501e04c3fSmrg   /* Device identification */
11601e04c3fSmrg   uint32_t vendor_id;
11701e04c3fSmrg   uint32_t device_id;
11801e04c3fSmrg
11901e04c3fSmrg   /* Structure version 1 ends here. */
12001e04c3fSmrg};
12101e04c3fSmrg
12201e04c3fSmrg#define MESA_GLINTEROP_EXPORT_IN_VERSION 1
12301e04c3fSmrg
12401e04c3fSmrg/**
12501e04c3fSmrg * Input parameters to Mesa interop export functions.
12601e04c3fSmrg */
12701e04c3fSmrgstruct mesa_glinterop_export_in {
12801e04c3fSmrg   /* The caller should set this to the version of the struct they support */
12901e04c3fSmrg   /* The callee will overwrite it if it supports a lower version.
13001e04c3fSmrg    *
13101e04c3fSmrg    * The caller should check the value and access up-to the version supported
13201e04c3fSmrg    * by the callee.
13301e04c3fSmrg    */
13401e04c3fSmrg   /* NOTE: Do not use the MESA_GLINTEROP_EXPORT_IN_VERSION macro */
13501e04c3fSmrg   uint32_t version;
13601e04c3fSmrg
13701e04c3fSmrg   /* One of the following:
13801e04c3fSmrg    * - GL_TEXTURE_BUFFER
13901e04c3fSmrg    * - GL_TEXTURE_1D
14001e04c3fSmrg    * - GL_TEXTURE_2D
14101e04c3fSmrg    * - GL_TEXTURE_3D
14201e04c3fSmrg    * - GL_TEXTURE_RECTANGLE
14301e04c3fSmrg    * - GL_TEXTURE_1D_ARRAY
14401e04c3fSmrg    * - GL_TEXTURE_2D_ARRAY
14501e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_ARRAY
14601e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP
14701e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_POSITIVE_X
14801e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_NEGATIVE_X
14901e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_POSITIVE_Y
15001e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
15101e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_POSITIVE_Z
15201e04c3fSmrg    * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
15301e04c3fSmrg    * - GL_TEXTURE_2D_MULTISAMPLE
15401e04c3fSmrg    * - GL_TEXTURE_2D_MULTISAMPLE_ARRAY
15501e04c3fSmrg    * - GL_TEXTURE_EXTERNAL_OES
15601e04c3fSmrg    * - GL_RENDERBUFFER
15701e04c3fSmrg    * - GL_ARRAY_BUFFER
15801e04c3fSmrg    */
15901e04c3fSmrg   unsigned target;
16001e04c3fSmrg
16101e04c3fSmrg   /* If target is GL_ARRAY_BUFFER, it's a buffer object.
16201e04c3fSmrg    * If target is GL_RENDERBUFFER, it's a renderbuffer object.
16301e04c3fSmrg    * If target is GL_TEXTURE_*, it's a texture object.
16401e04c3fSmrg    */
16501e04c3fSmrg   unsigned obj;
16601e04c3fSmrg
16701e04c3fSmrg   /* Mipmap level. Ignored for non-texture objects. */
16801e04c3fSmrg   unsigned miplevel;
16901e04c3fSmrg
17001e04c3fSmrg   /* One of MESA_GLINTEROP_ACCESS_* flags. This describes how the exported
17101e04c3fSmrg    * object is going to be used.
17201e04c3fSmrg    */
17301e04c3fSmrg   uint32_t access;
17401e04c3fSmrg
17501e04c3fSmrg   /* Size of memory pointed to by out_driver_data. */
17601e04c3fSmrg   uint32_t out_driver_data_size;
17701e04c3fSmrg
17801e04c3fSmrg   /* If the caller wants to query driver-specific data about the OpenGL
17901e04c3fSmrg    * object, this should point to the memory where that data will be stored.
18001e04c3fSmrg    * This is expected to be a temporary staging memory. The pointer is not
18101e04c3fSmrg    * allowed to be saved for later use by Mesa.
18201e04c3fSmrg    */
18301e04c3fSmrg   void *out_driver_data;
18401e04c3fSmrg   /* Structure version 1 ends here. */
18501e04c3fSmrg};
18601e04c3fSmrg
18701e04c3fSmrg#define MESA_GLINTEROP_EXPORT_OUT_VERSION 1
18801e04c3fSmrg
18901e04c3fSmrg/**
19001e04c3fSmrg * Outputs of Mesa interop export functions.
19101e04c3fSmrg */
19201e04c3fSmrgstruct mesa_glinterop_export_out {
19301e04c3fSmrg   /* The caller should set this to the version of the struct they support */
19401e04c3fSmrg   /* The callee will overwrite it if it supports a lower version.
19501e04c3fSmrg    *
19601e04c3fSmrg    * The caller should check the value and access up-to the version supported
19701e04c3fSmrg    * by the callee.
19801e04c3fSmrg    */
19901e04c3fSmrg   /* NOTE: Do not use the MESA_GLINTEROP_EXPORT_OUT_VERSION macro */
20001e04c3fSmrg   uint32_t version;
20101e04c3fSmrg
20201e04c3fSmrg   /* The DMABUF handle. It must be closed by the caller using the POSIX
20301e04c3fSmrg    * close() function when it's not needed anymore. Mesa is not responsible
20401e04c3fSmrg    * for closing the handle.
20501e04c3fSmrg    *
20601e04c3fSmrg    * Not closing the handle by the caller will lead to a resource leak,
20701e04c3fSmrg    * will prevent releasing the GPU buffer, and may prevent creating new
20801e04c3fSmrg    * DMABUF handles within the process.
20901e04c3fSmrg    */
21001e04c3fSmrg   int dmabuf_fd;
21101e04c3fSmrg
21201e04c3fSmrg   /* The mutable OpenGL internal format specified by glTextureView or
21301e04c3fSmrg    * glTexBuffer. If the object is not one of those, the original internal
21401e04c3fSmrg    * format specified by glTexStorage, glTexImage, or glRenderbufferStorage
21501e04c3fSmrg    * will be returned.
21601e04c3fSmrg    */
21701e04c3fSmrg   unsigned internal_format;
21801e04c3fSmrg
21901e04c3fSmrg   /* Buffer offset and size for GL_ARRAY_BUFFER and GL_TEXTURE_BUFFER.
22001e04c3fSmrg    * This allows interop with suballocations (a buffer allocated within
22101e04c3fSmrg    * a larger buffer).
22201e04c3fSmrg    *
22301e04c3fSmrg    * Parameters specified by glTexBufferRange for GL_TEXTURE_BUFFER are
22401e04c3fSmrg    * applied to these and can shrink the range further.
22501e04c3fSmrg    */
22601e04c3fSmrg   ptrdiff_t buf_offset;
22701e04c3fSmrg   ptrdiff_t buf_size;
22801e04c3fSmrg
22901e04c3fSmrg   /* Parameters specified by glTextureView. If the object is not a texture
23001e04c3fSmrg    * view, default parameters covering the whole texture will be returned.
23101e04c3fSmrg    */
23201e04c3fSmrg   unsigned view_minlevel;
23301e04c3fSmrg   unsigned view_numlevels;
23401e04c3fSmrg   unsigned view_minlayer;
23501e04c3fSmrg   unsigned view_numlayers;
23601e04c3fSmrg
23701e04c3fSmrg   /* The number of bytes written to out_driver_data. */
23801e04c3fSmrg   uint32_t out_driver_data_written;
23901e04c3fSmrg   /* Structure version 1 ends here. */
24001e04c3fSmrg};
24101e04c3fSmrg
24201e04c3fSmrg
24301e04c3fSmrg/**
24401e04c3fSmrg * Query device information.
24501e04c3fSmrg *
24601e04c3fSmrg * \param dpy        GLX display
24701e04c3fSmrg * \param context    GLX context
24801e04c3fSmrg * \param out        where to return the information
24901e04c3fSmrg *
25001e04c3fSmrg * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
25101e04c3fSmrg */
25201e04c3fSmrgint
25301e04c3fSmrgMesaGLInteropGLXQueryDeviceInfo(struct _XDisplay *dpy, struct __GLXcontextRec *context,
25401e04c3fSmrg                                struct mesa_glinterop_device_info *out);
25501e04c3fSmrg
25601e04c3fSmrg
25701e04c3fSmrg/**
25801e04c3fSmrg * Same as MesaGLInteropGLXQueryDeviceInfo except that it accepts EGLDisplay
25901e04c3fSmrg * and EGLContext.
26001e04c3fSmrg */
26101e04c3fSmrgint
26201e04c3fSmrgMesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
26301e04c3fSmrg                                struct mesa_glinterop_device_info *out);
26401e04c3fSmrg
26501e04c3fSmrg
26601e04c3fSmrg/**
26701e04c3fSmrg * Create and return a DMABUF handle corresponding to the given OpenGL
26801e04c3fSmrg * object, and return other parameters about the OpenGL object.
26901e04c3fSmrg *
27001e04c3fSmrg * \param dpy        GLX display
27101e04c3fSmrg * \param context    GLX context
27201e04c3fSmrg * \param in         input parameters
27301e04c3fSmrg * \param out        return values
27401e04c3fSmrg *
27501e04c3fSmrg * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
27601e04c3fSmrg */
27701e04c3fSmrgint
27801e04c3fSmrgMesaGLInteropGLXExportObject(struct _XDisplay *dpy, struct __GLXcontextRec *context,
27901e04c3fSmrg                             struct mesa_glinterop_export_in *in,
28001e04c3fSmrg                             struct mesa_glinterop_export_out *out);
28101e04c3fSmrg
28201e04c3fSmrg
28301e04c3fSmrg/**
28401e04c3fSmrg * Same as MesaGLInteropGLXExportObject except that it accepts
28501e04c3fSmrg * EGLDisplay and EGLContext.
28601e04c3fSmrg */
28701e04c3fSmrgint
28801e04c3fSmrgMesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
28901e04c3fSmrg                             struct mesa_glinterop_export_in *in,
29001e04c3fSmrg                             struct mesa_glinterop_export_out *out);
29101e04c3fSmrg
29201e04c3fSmrg
29301e04c3fSmrgtypedef int (PFNMESAGLINTEROPGLXQUERYDEVICEINFOPROC)(struct _XDisplay *dpy, struct __GLXcontextRec *context,
29401e04c3fSmrg                                                     struct mesa_glinterop_device_info *out);
29501e04c3fSmrgtypedef int (PFNMESAGLINTEROPEGLQUERYDEVICEINFOPROC)(EGLDisplay dpy, EGLContext context,
29601e04c3fSmrg                                                     struct mesa_glinterop_device_info *out);
29701e04c3fSmrgtypedef int (PFNMESAGLINTEROPGLXEXPORTOBJECTPROC)(struct _XDisplay *dpy, struct __GLXcontextRec *context,
29801e04c3fSmrg                                                  struct mesa_glinterop_export_in *in,
29901e04c3fSmrg                                                  struct mesa_glinterop_export_out *out);
30001e04c3fSmrgtypedef int (PFNMESAGLINTEROPEGLEXPORTOBJECTPROC)(EGLDisplay dpy, EGLContext context,
30101e04c3fSmrg                                                  struct mesa_glinterop_export_in *in,
30201e04c3fSmrg                                                  struct mesa_glinterop_export_out *out);
30301e04c3fSmrg
30401e04c3fSmrg#ifdef __cplusplus
30501e04c3fSmrg}
30601e04c3fSmrg#endif
30701e04c3fSmrg
30801e04c3fSmrg#endif /* MESA_GLINTEROP_H */
309