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 <benjaminfranzke@googlemail.com>
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> /* dri_interface needs GL types */
38#include "GL/internal/dri_interface.h"
39#include "kopper_interface.h"
40
41struct gbm_dri_surface;
42struct gbm_dri_bo;
43
44struct gbm_dri_visual {
45   uint32_t gbm_format;
46   int dri_image_format;
47   struct {
48      int red;
49      int green;
50      int blue;
51      int alpha;
52   } rgba_shifts;
53   struct {
54      unsigned int red;
55      unsigned int green;
56      unsigned int blue;
57      unsigned int alpha;
58   } rgba_sizes;
59   bool is_float;
60};
61
62struct gbm_dri_device {
63   struct gbm_device base;
64
65   void *driver;
66   char *driver_name; /* Name of the DRI module, without the _dri suffix */
67   bool software; /* A software driver was loaded */
68
69   __DRIscreen *screen;
70   __DRIcontext *context;
71   mtx_t mutex;
72
73   const __DRIcoreExtension   *core;
74   const __DRIdri2Extension   *dri2;
75   const __DRI2fenceExtension *fence;
76   const __DRIimageExtension  *image;
77   const __DRIswrastExtension *swrast;
78   const __DRIkopperExtension *kopper;
79   const __DRI2flushExtension *flush;
80
81   const __DRIconfig   **driver_configs;
82   const __DRIextension **loader_extensions;
83   const __DRIextension **driver_extensions;
84
85   __DRIimage *(*lookup_image)(__DRIscreen *screen, void *image, void *data);
86   GLboolean (*validate_image)(void *image, void *data);
87   __DRIimage *(*lookup_image_validated)(void *image, void *data);
88   void *lookup_user_data;
89
90   __DRIbuffer *(*get_buffers)(__DRIdrawable * driDrawable,
91                               int *width, int *height,
92                               unsigned int *attachments, int count,
93                               int *out_count, void *data);
94   void (*flush_front_buffer)(__DRIdrawable * driDrawable, void *data);
95   __DRIbuffer *(*get_buffers_with_format)(__DRIdrawable * driDrawable,
96			     int *width, int *height,
97			     unsigned int *attachments, int count,
98			     int *out_count, void *data);
99   int (*image_get_buffers)(__DRIdrawable *driDrawable,
100                            unsigned int format,
101                            uint32_t *stamp,
102                            void *loaderPrivate,
103                            uint32_t buffer_mask,
104                            struct __DRIimageList *buffers);
105   void (*swrast_put_image2)(__DRIdrawable *driDrawable,
106                             int            op,
107                             int            x,
108                             int            y,
109                             int            width,
110                             int            height,
111                             int            stride,
112                             char          *data,
113                             void          *loaderPrivate);
114   void (*swrast_get_image)(__DRIdrawable *driDrawable,
115                            int            x,
116                            int            y,
117                            int            width,
118                            int            height,
119                            char          *data,
120                            void          *loaderPrivate);
121
122   struct wl_drm *wl_drm;
123
124   const struct gbm_dri_visual *visual_table;
125   int num_visuals;
126};
127
128struct gbm_dri_bo {
129   struct gbm_bo base;
130
131   __DRIimage *image;
132
133   /* Used for cursors and the swrast front BO */
134   uint32_t handle, size;
135   void *map;
136};
137
138struct gbm_dri_surface {
139   struct gbm_surface base;
140
141   void *dri_private;
142};
143
144static inline struct gbm_dri_device *
145gbm_dri_device(struct gbm_device *gbm)
146{
147   return (struct gbm_dri_device *) gbm;
148}
149
150static inline struct gbm_dri_bo *
151gbm_dri_bo(struct gbm_bo *bo)
152{
153   return (struct gbm_dri_bo *) bo;
154}
155
156static inline struct gbm_dri_surface *
157gbm_dri_surface(struct gbm_surface *surface)
158{
159   return (struct gbm_dri_surface *) surface;
160}
161
162static inline void *
163gbm_dri_bo_map_dumb(struct gbm_dri_bo *bo)
164{
165   struct drm_mode_map_dumb map_arg;
166   int ret;
167
168   if (bo->image != NULL)
169      return NULL;
170
171   if (bo->map != NULL)
172      return bo->map;
173
174   memset(&map_arg, 0, sizeof(map_arg));
175   map_arg.handle = bo->handle;
176
177   ret = drmIoctl(bo->base.gbm->v0.fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
178   if (ret)
179      return NULL;
180
181   bo->map = mmap(NULL, bo->size, PROT_WRITE,
182                  MAP_SHARED, bo->base.gbm->v0.fd, map_arg.offset);
183   if (bo->map == MAP_FAILED) {
184      bo->map = NULL;
185      return NULL;
186   }
187
188   return bo->map;
189}
190
191static inline void
192gbm_dri_bo_unmap_dumb(struct gbm_dri_bo *bo)
193{
194   munmap(bo->map, bo->size);
195   bo->map = NULL;
196}
197
198#endif
199