xref: /aosp_15_r20/external/mesa3d/src/gbm/backends/dri/gbm_driint.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Benjamin Franzke <[email protected]>
26  */
27 
28 #ifndef _GBM_DRI_INTERNAL_H_
29 #define _GBM_DRI_INTERNAL_H_
30 
31 #include <xf86drm.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include "gbmint.h"
35 #include "c11/threads.h"
36 
37 #include <GL/gl.h> /* mesa_interface needs GL types */
38 #include "mesa_interface.h"
39 #include "kopper_interface.h"
40 
41 struct gbm_dri_surface;
42 struct gbm_dri_bo;
43 
44 struct gbm_dri_visual {
45    uint32_t gbm_format;
46    int dri_image_format;
47 };
48 
49 struct gbm_dri_device {
50    struct gbm_device base;
51 
52    char *driver_name; /* Name of the DRI module, without the _dri suffix */
53    bool software; /* A software driver was loaded */
54    bool swrast; /* this is swrast */
55    bool has_dmabuf_import;
56    bool has_dmabuf_export;
57    bool has_compression_modifiers;
58 
59    __DRIscreen *screen;
60    __DRIcontext *context;
61    mtx_t mutex;
62 
63    const __DRIconfig   **driver_configs;
64    const __DRIextension **loader_extensions;
65 
66    GLboolean (*validate_image)(void *image, void *data);
67    __DRIimage *(*lookup_image_validated)(void *image, void *data);
68    void *lookup_user_data;
69 
70    void (*flush_front_buffer)(__DRIdrawable * driDrawable, void *data);
71    int (*image_get_buffers)(__DRIdrawable *driDrawable,
72                             unsigned int format,
73                             uint32_t *stamp,
74                             void *loaderPrivate,
75                             uint32_t buffer_mask,
76                             struct __DRIimageList *buffers);
77    void (*swrast_put_image2)(__DRIdrawable *driDrawable,
78                              int            op,
79                              int            x,
80                              int            y,
81                              int            width,
82                              int            height,
83                              int            stride,
84                              char          *data,
85                              void          *loaderPrivate);
86    void (*swrast_get_image)(__DRIdrawable *driDrawable,
87                             int            x,
88                             int            y,
89                             int            width,
90                             int            height,
91                             char          *data,
92                             void          *loaderPrivate);
93 
94    struct wl_drm *wl_drm;
95 
96    const struct gbm_dri_visual *visual_table;
97    int num_visuals;
98 };
99 
100 struct gbm_dri_bo {
101    struct gbm_bo base;
102 
103    __DRIimage *image;
104 
105    /* Used for cursors and the swrast front BO */
106    uint32_t handle, size;
107    void *map;
108 };
109 
110 struct gbm_dri_surface {
111    struct gbm_surface base;
112 
113    void *dri_private;
114 };
115 
116 static inline struct gbm_dri_device *
gbm_dri_device(struct gbm_device * gbm)117 gbm_dri_device(struct gbm_device *gbm)
118 {
119    return (struct gbm_dri_device *) gbm;
120 }
121 
122 static inline struct gbm_dri_bo *
gbm_dri_bo(struct gbm_bo * bo)123 gbm_dri_bo(struct gbm_bo *bo)
124 {
125    return (struct gbm_dri_bo *) bo;
126 }
127 
128 static inline struct gbm_dri_surface *
gbm_dri_surface(struct gbm_surface * surface)129 gbm_dri_surface(struct gbm_surface *surface)
130 {
131    return (struct gbm_dri_surface *) surface;
132 }
133 
134 static inline void *
gbm_dri_bo_map_dumb(struct gbm_dri_bo * bo)135 gbm_dri_bo_map_dumb(struct gbm_dri_bo *bo)
136 {
137    struct drm_mode_map_dumb map_arg;
138    int ret;
139 
140    if (bo->image != NULL)
141       return NULL;
142 
143    if (bo->map != NULL)
144       return bo->map;
145 
146    memset(&map_arg, 0, sizeof(map_arg));
147    map_arg.handle = bo->handle;
148 
149    ret = drmIoctl(bo->base.gbm->v0.fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
150    if (ret)
151       return NULL;
152 
153    bo->map = mmap(NULL, bo->size, PROT_WRITE,
154                   MAP_SHARED, bo->base.gbm->v0.fd, map_arg.offset);
155    if (bo->map == MAP_FAILED) {
156       bo->map = NULL;
157       return NULL;
158    }
159 
160    return bo->map;
161 }
162 
163 static inline void
gbm_dri_bo_unmap_dumb(struct gbm_dri_bo * bo)164 gbm_dri_bo_unmap_dumb(struct gbm_dri_bo *bo)
165 {
166    munmap(bo->map, bo->size);
167    bo->map = NULL;
168 }
169 
170 #endif
171