1848b8605Smrg/*
2848b8605Smrg * Copyright © 2011 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16848b8605Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18848b8605Smrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19848b8605Smrg * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20848b8605Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21848b8605Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22848b8605Smrg * DEALINGS IN THE SOFTWARE.
23848b8605Smrg *
24848b8605Smrg * Authors:
25848b8605Smrg *    Benjamin Franzke <benjaminfranzke@googlemail.com>
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg#ifndef _GBM_DRI_INTERNAL_H_
29848b8605Smrg#define _GBM_DRI_INTERNAL_H_
30848b8605Smrg
31b8e80941Smrg#include <xf86drm.h>
32b8e80941Smrg#include <string.h>
33848b8605Smrg#include <sys/mman.h>
34848b8605Smrg#include "gbmint.h"
35b8e80941Smrg#include "c11/threads.h"
36848b8605Smrg
37848b8605Smrg#include <GL/gl.h> /* dri_interface needs GL types */
38848b8605Smrg#include "GL/internal/dri_interface.h"
39848b8605Smrg
40848b8605Smrgstruct gbm_dri_surface;
41848b8605Smrgstruct gbm_dri_bo;
42848b8605Smrg
43b8e80941Smrgstruct gbm_dri_visual {
44b8e80941Smrg   uint32_t gbm_format;
45b8e80941Smrg   int dri_image_format;
46b8e80941Smrg   struct {
47b8e80941Smrg      uint32_t red;
48b8e80941Smrg      uint32_t green;
49b8e80941Smrg      uint32_t blue;
50b8e80941Smrg      uint32_t alpha;
51b8e80941Smrg   } rgba_masks;
52b8e80941Smrg};
53b8e80941Smrg
54848b8605Smrgstruct gbm_dri_device {
55b8e80941Smrg   struct gbm_device base;
56848b8605Smrg
57848b8605Smrg   void *driver;
58b8e80941Smrg   char *driver_name; /* Name of the DRI module, without the _dri suffix */
59848b8605Smrg
60848b8605Smrg   __DRIscreen *screen;
61b8e80941Smrg   __DRIcontext *context;
62b8e80941Smrg   mtx_t mutex;
63848b8605Smrg
64848b8605Smrg   const __DRIcoreExtension   *core;
65848b8605Smrg   const __DRIdri2Extension   *dri2;
66b8e80941Smrg   const __DRI2fenceExtension *fence;
67848b8605Smrg   const __DRIimageExtension  *image;
68848b8605Smrg   const __DRIswrastExtension *swrast;
69848b8605Smrg   const __DRI2flushExtension *flush;
70848b8605Smrg
71848b8605Smrg   const __DRIconfig   **driver_configs;
72b8e80941Smrg   const __DRIextension **loader_extensions;
73848b8605Smrg   const __DRIextension **driver_extensions;
74848b8605Smrg
75848b8605Smrg   __DRIimage *(*lookup_image)(__DRIscreen *screen, void *image, void *data);
76848b8605Smrg   void *lookup_user_data;
77848b8605Smrg
78848b8605Smrg   __DRIbuffer *(*get_buffers)(__DRIdrawable * driDrawable,
79848b8605Smrg                               int *width, int *height,
80848b8605Smrg                               unsigned int *attachments, int count,
81848b8605Smrg                               int *out_count, void *data);
82848b8605Smrg   void (*flush_front_buffer)(__DRIdrawable * driDrawable, void *data);
83848b8605Smrg   __DRIbuffer *(*get_buffers_with_format)(__DRIdrawable * driDrawable,
84848b8605Smrg			     int *width, int *height,
85848b8605Smrg			     unsigned int *attachments, int count,
86848b8605Smrg			     int *out_count, void *data);
87848b8605Smrg   int (*image_get_buffers)(__DRIdrawable *driDrawable,
88848b8605Smrg                            unsigned int format,
89848b8605Smrg                            uint32_t *stamp,
90848b8605Smrg                            void *loaderPrivate,
91848b8605Smrg                            uint32_t buffer_mask,
92848b8605Smrg                            struct __DRIimageList *buffers);
93848b8605Smrg   void (*swrast_put_image2)(__DRIdrawable *driDrawable,
94848b8605Smrg                             int            op,
95848b8605Smrg                             int            x,
96848b8605Smrg                             int            y,
97848b8605Smrg                             int            width,
98848b8605Smrg                             int            height,
99848b8605Smrg                             int            stride,
100848b8605Smrg                             char          *data,
101848b8605Smrg                             void          *loaderPrivate);
102848b8605Smrg   void (*swrast_get_image)(__DRIdrawable *driDrawable,
103848b8605Smrg                            int            x,
104848b8605Smrg                            int            y,
105848b8605Smrg                            int            width,
106848b8605Smrg                            int            height,
107848b8605Smrg                            char          *data,
108848b8605Smrg                            void          *loaderPrivate);
109848b8605Smrg
110848b8605Smrg   struct wl_drm *wl_drm;
111b8e80941Smrg
112b8e80941Smrg   const struct gbm_dri_visual *visual_table;
113b8e80941Smrg   int num_visuals;
114848b8605Smrg};
115848b8605Smrg
116848b8605Smrgstruct gbm_dri_bo {
117b8e80941Smrg   struct gbm_bo base;
118848b8605Smrg
119848b8605Smrg   __DRIimage *image;
120848b8605Smrg
121848b8605Smrg   /* Used for cursors and the swrast front BO */
122848b8605Smrg   uint32_t handle, size;
123848b8605Smrg   void *map;
124848b8605Smrg};
125848b8605Smrg
126848b8605Smrgstruct gbm_dri_surface {
127848b8605Smrg   struct gbm_surface base;
128848b8605Smrg
129848b8605Smrg   void *dri_private;
130848b8605Smrg};
131848b8605Smrg
132848b8605Smrgstatic inline struct gbm_dri_device *
133848b8605Smrggbm_dri_device(struct gbm_device *gbm)
134848b8605Smrg{
135848b8605Smrg   return (struct gbm_dri_device *) gbm;
136848b8605Smrg}
137848b8605Smrg
138848b8605Smrgstatic inline struct gbm_dri_bo *
139848b8605Smrggbm_dri_bo(struct gbm_bo *bo)
140848b8605Smrg{
141848b8605Smrg   return (struct gbm_dri_bo *) bo;
142848b8605Smrg}
143848b8605Smrg
144848b8605Smrgstatic inline struct gbm_dri_surface *
145848b8605Smrggbm_dri_surface(struct gbm_surface *surface)
146848b8605Smrg{
147848b8605Smrg   return (struct gbm_dri_surface *) surface;
148848b8605Smrg}
149848b8605Smrg
150848b8605Smrgstatic inline void *
151b8e80941Smrggbm_dri_bo_map_dumb(struct gbm_dri_bo *bo)
152848b8605Smrg{
153848b8605Smrg   struct drm_mode_map_dumb map_arg;
154848b8605Smrg   int ret;
155848b8605Smrg
156848b8605Smrg   if (bo->image != NULL)
157848b8605Smrg      return NULL;
158848b8605Smrg
159848b8605Smrg   if (bo->map != NULL)
160848b8605Smrg      return bo->map;
161848b8605Smrg
162848b8605Smrg   memset(&map_arg, 0, sizeof(map_arg));
163848b8605Smrg   map_arg.handle = bo->handle;
164848b8605Smrg
165b8e80941Smrg   ret = drmIoctl(bo->base.gbm->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
166848b8605Smrg   if (ret)
167848b8605Smrg      return NULL;
168848b8605Smrg
169848b8605Smrg   bo->map = mmap(0, bo->size, PROT_WRITE,
170b8e80941Smrg                  MAP_SHARED, bo->base.gbm->fd, map_arg.offset);
171848b8605Smrg   if (bo->map == MAP_FAILED) {
172848b8605Smrg      bo->map = NULL;
173848b8605Smrg      return NULL;
174848b8605Smrg   }
175848b8605Smrg
176848b8605Smrg   return bo->map;
177848b8605Smrg}
178848b8605Smrg
179848b8605Smrgstatic inline void
180b8e80941Smrggbm_dri_bo_unmap_dumb(struct gbm_dri_bo *bo)
181848b8605Smrg{
182848b8605Smrg   munmap(bo->map, bo->size);
183848b8605Smrg   bo->map = NULL;
184848b8605Smrg}
185848b8605Smrg
186848b8605Smrg#endif
187