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