Home | History | Annotate | Download | only in GL
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright 2016 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 /* Mesa OpenGL inter-driver interoperability interface designed for but not
     26  * limited to OpenCL.
     27  *
     28  * This is a driver-agnostic, backward-compatible interface. The structures
     29  * are only allowed to grow. They can never shrink and their members can
     30  * never be removed, renamed, or redefined.
     31  *
     32  * The interface doesn't return a lot of static texture parameters like
     33  * width, height, etc. It mainly returns mutable buffer and texture view
     34  * parameters that can't be part of the texture allocation (because they are
     35  * mutable). If drivers want to return more data or want to return static
     36  * allocation parameters, they can do it in one of these two ways:
     37  * - attaching the data to the DMABUF handle in a driver-specific way
     38  * - passing the data via "out_driver_data" in the "in" structure.
     39  *
     40  * Mesa is expected to do a lot of error checking on behalf of OpenCL, such
     41  * as checking the target, miplevel, and texture completeness.
     42  *
     43  * OpenCL, on the other hand, needs to check if the display+context combo
     44  * is compatible with the OpenCL driver by querying the device information.
     45  * It also needs to check if the texture internal format and channel ordering
     46  * (returned in a driver-specific way) is supported by OpenCL, among other
     47  * things.
     48  */
     49 
     50 #ifndef MESA_GLINTEROP_H
     51 #define MESA_GLINTEROP_H
     52 
     53 #include <stddef.h>
     54 #include <stdint.h>
     55 
     56 #ifdef __cplusplus
     57 extern "C" {
     58 #endif
     59 
     60 /* Forward declarations to avoid inclusion of GL/glx.h */
     61 struct _XDisplay;
     62 struct __GLXcontextRec;
     63 
     64 /* Forward declarations to avoid inclusion of EGL/egl.h */
     65 typedef void *EGLDisplay;
     66 typedef void *EGLContext;
     67 
     68 /** Returned error codes. */
     69 enum {
     70    MESA_GLINTEROP_SUCCESS = 0,
     71    MESA_GLINTEROP_OUT_OF_RESOURCES,
     72    MESA_GLINTEROP_OUT_OF_HOST_MEMORY,
     73    MESA_GLINTEROP_INVALID_OPERATION,
     74    MESA_GLINTEROP_INVALID_VERSION,
     75    MESA_GLINTEROP_INVALID_DISPLAY,
     76    MESA_GLINTEROP_INVALID_CONTEXT,
     77    MESA_GLINTEROP_INVALID_TARGET,
     78    MESA_GLINTEROP_INVALID_OBJECT,
     79    MESA_GLINTEROP_INVALID_MIP_LEVEL,
     80    MESA_GLINTEROP_UNSUPPORTED
     81 };
     82 
     83 /** Access flags. */
     84 enum {
     85    MESA_GLINTEROP_ACCESS_READ_WRITE = 0,
     86    MESA_GLINTEROP_ACCESS_READ_ONLY,
     87    MESA_GLINTEROP_ACCESS_WRITE_ONLY
     88 };
     89 
     90 #define MESA_GLINTEROP_DEVICE_INFO_VERSION 1
     91 
     92 /**
     93  * Device information returned by Mesa.
     94  */
     95 struct mesa_glinterop_device_info {
     96    /* The caller should set this to the version of the struct they support */
     97    /* The callee will overwrite it if it supports a lower version.
     98     *
     99     * The caller should check the value and access up-to the version supported
    100     * by the callee.
    101     */
    102    /* NOTE: Do not use the MESA_GLINTEROP_DEVICE_INFO_VERSION macro */
    103    uint32_t version;
    104 
    105    /* PCI location */
    106    uint32_t pci_segment_group;
    107    uint32_t pci_bus;
    108    uint32_t pci_device;
    109    uint32_t pci_function;
    110 
    111    /* Device identification */
    112    uint32_t vendor_id;
    113    uint32_t device_id;
    114 
    115    /* Structure version 1 ends here. */
    116 };
    117 
    118 #define MESA_GLINTEROP_EXPORT_IN_VERSION 1
    119 
    120 /**
    121  * Input parameters to Mesa interop export functions.
    122  */
    123 struct mesa_glinterop_export_in {
    124    /* The caller should set this to the version of the struct they support */
    125    /* The callee will overwrite it if it supports a lower version.
    126     *
    127     * The caller should check the value and access up-to the version supported
    128     * by the callee.
    129     */
    130    /* NOTE: Do not use the MESA_GLINTEROP_EXPORT_IN_VERSION macro */
    131    uint32_t version;
    132 
    133    /* One of the following:
    134     * - GL_TEXTURE_BUFFER
    135     * - GL_TEXTURE_1D
    136     * - GL_TEXTURE_2D
    137     * - GL_TEXTURE_3D
    138     * - GL_TEXTURE_RECTANGLE
    139     * - GL_TEXTURE_1D_ARRAY
    140     * - GL_TEXTURE_2D_ARRAY
    141     * - GL_TEXTURE_CUBE_MAP_ARRAY
    142     * - GL_TEXTURE_CUBE_MAP
    143     * - GL_TEXTURE_CUBE_MAP_POSITIVE_X
    144     * - GL_TEXTURE_CUBE_MAP_NEGATIVE_X
    145     * - GL_TEXTURE_CUBE_MAP_POSITIVE_Y
    146     * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
    147     * - GL_TEXTURE_CUBE_MAP_POSITIVE_Z
    148     * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
    149     * - GL_TEXTURE_2D_MULTISAMPLE
    150     * - GL_TEXTURE_2D_MULTISAMPLE_ARRAY
    151     * - GL_TEXTURE_EXTERNAL_OES
    152     * - GL_RENDERBUFFER
    153     * - GL_ARRAY_BUFFER
    154     */
    155    unsigned target;
    156 
    157    /* If target is GL_ARRAY_BUFFER, it's a buffer object.
    158     * If target is GL_RENDERBUFFER, it's a renderbuffer object.
    159     * If target is GL_TEXTURE_*, it's a texture object.
    160     */
    161    unsigned obj;
    162 
    163    /* Mipmap level. Ignored for non-texture objects. */
    164    unsigned miplevel;
    165 
    166    /* One of MESA_GLINTEROP_ACCESS_* flags. This describes how the exported
    167     * object is going to be used.
    168     */
    169    uint32_t access;
    170 
    171    /* Size of memory pointed to by out_driver_data. */
    172    uint32_t out_driver_data_size;
    173 
    174    /* If the caller wants to query driver-specific data about the OpenGL
    175     * object, this should point to the memory where that data will be stored.
    176     * This is expected to be a temporary staging memory. The pointer is not
    177     * allowed to be saved for later use by Mesa.
    178     */
    179    void *out_driver_data;
    180    /* Structure version 1 ends here. */
    181 };
    182 
    183 #define MESA_GLINTEROP_EXPORT_OUT_VERSION 1
    184 
    185 /**
    186  * Outputs of Mesa interop export functions.
    187  */
    188 struct mesa_glinterop_export_out {
    189    /* The caller should set this to the version of the struct they support */
    190    /* The callee will overwrite it if it supports a lower version.
    191     *
    192     * The caller should check the value and access up-to the version supported
    193     * by the callee.
    194     */
    195    /* NOTE: Do not use the MESA_GLINTEROP_EXPORT_OUT_VERSION macro */
    196    uint32_t version;
    197 
    198    /* The DMABUF handle. It must be closed by the caller using the POSIX
    199     * close() function when it's not needed anymore. Mesa is not responsible
    200     * for closing the handle.
    201     *
    202     * Not closing the handle by the caller will lead to a resource leak,
    203     * will prevent releasing the GPU buffer, and may prevent creating new
    204     * DMABUF handles within the process.
    205     */
    206    int dmabuf_fd;
    207 
    208    /* The mutable OpenGL internal format specified by glTextureView or
    209     * glTexBuffer. If the object is not one of those, the original internal
    210     * format specified by glTexStorage, glTexImage, or glRenderbufferStorage
    211     * will be returned.
    212     */
    213    unsigned internal_format;
    214 
    215    /* Buffer offset and size for GL_ARRAY_BUFFER and GL_TEXTURE_BUFFER.
    216     * This allows interop with suballocations (a buffer allocated within
    217     * a larger buffer).
    218     *
    219     * Parameters specified by glTexBufferRange for GL_TEXTURE_BUFFER are
    220     * applied to these and can shrink the range further.
    221     */
    222    ptrdiff_t buf_offset;
    223    ptrdiff_t buf_size;
    224 
    225    /* Parameters specified by glTextureView. If the object is not a texture
    226     * view, default parameters covering the whole texture will be returned.
    227     */
    228    unsigned view_minlevel;
    229    unsigned view_numlevels;
    230    unsigned view_minlayer;
    231    unsigned view_numlayers;
    232 
    233    /* The number of bytes written to out_driver_data. */
    234    uint32_t out_driver_data_written;
    235    /* Structure version 1 ends here. */
    236 };
    237 
    238 
    239 /**
    240  * Query device information.
    241  *
    242  * \param dpy        GLX display
    243  * \param context    GLX context
    244  * \param out        where to return the information
    245  *
    246  * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
    247  */
    248 int
    249 MesaGLInteropGLXQueryDeviceInfo(struct _XDisplay *dpy, struct __GLXcontextRec *context,
    250                                 struct mesa_glinterop_device_info *out);
    251 
    252 
    253 /**
    254  * Same as MesaGLInteropGLXQueryDeviceInfo except that it accepts EGLDisplay
    255  * and EGLContext.
    256  */
    257 int
    258 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
    259                                 struct mesa_glinterop_device_info *out);
    260 
    261 
    262 /**
    263  * Create and return a DMABUF handle corresponding to the given OpenGL
    264  * object, and return other parameters about the OpenGL object.
    265  *
    266  * \param dpy        GLX display
    267  * \param context    GLX context
    268  * \param in         input parameters
    269  * \param out        return values
    270  *
    271  * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
    272  */
    273 int
    274 MesaGLInteropGLXExportObject(struct _XDisplay *dpy, struct __GLXcontextRec *context,
    275                              struct mesa_glinterop_export_in *in,
    276                              struct mesa_glinterop_export_out *out);
    277 
    278 
    279 /**
    280  * Same as MesaGLInteropGLXExportObject except that it accepts
    281  * EGLDisplay and EGLContext.
    282  */
    283 int
    284 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
    285                              struct mesa_glinterop_export_in *in,
    286                              struct mesa_glinterop_export_out *out);
    287 
    288 
    289 typedef int (PFNMESAGLINTEROPGLXQUERYDEVICEINFOPROC)(struct _XDisplay *dpy, struct __GLXcontextRec *context,
    290                                                      struct mesa_glinterop_device_info *out);
    291 typedef int (PFNMESAGLINTEROPEGLQUERYDEVICEINFOPROC)(EGLDisplay dpy, EGLContext context,
    292                                                      struct mesa_glinterop_device_info *out);
    293 typedef int (PFNMESAGLINTEROPGLXEXPORTOBJECTPROC)(struct _XDisplay *dpy, struct __GLXcontextRec *context,
    294                                                   struct mesa_glinterop_export_in *in,
    295                                                   struct mesa_glinterop_export_out *out);
    296 typedef int (PFNMESAGLINTEROPEGLEXPORTOBJECTPROC)(EGLDisplay dpy, EGLContext context,
    297                                                   struct mesa_glinterop_export_in *in,
    298                                                   struct mesa_glinterop_export_out *out);
    299 
    300 #ifdef __cplusplus
    301 }
    302 #endif
    303 
    304 #endif /* MESA_GLINTEROP_H */
    305