gallium: remove the geom_flags param from is_format_supported
[mesa.git] / src / gallium / state_trackers / egl / drm / native_drm.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.8
4 *
5 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include "util/u_memory.h"
31 #include "egllog.h"
32
33 #include "native_drm.h"
34
35 /* see get_drm_screen_name */
36 #include <radeon_drm.h>
37 #include "radeon/drm/radeon_drm_public.h"
38
39 static boolean
40 drm_display_is_format_supported(struct native_display *ndpy,
41 enum pipe_format fmt, boolean is_color)
42 {
43 return ndpy->screen->is_format_supported(ndpy->screen,
44 fmt, PIPE_TEXTURE_2D, 0,
45 (is_color) ? PIPE_BIND_RENDER_TARGET :
46 PIPE_BIND_DEPTH_STENCIL);
47 }
48
49 static const struct native_config **
50 drm_display_get_configs(struct native_display *ndpy, int *num_configs)
51 {
52 struct drm_display *drmdpy = drm_display(ndpy);
53 const struct native_config **configs;
54
55 /* first time */
56 if (!drmdpy->config) {
57 struct native_config *nconf;
58 enum pipe_format format;
59
60 drmdpy->config = CALLOC(1, sizeof(*drmdpy->config));
61 if (!drmdpy->config)
62 return NULL;
63
64 nconf = &drmdpy->config->base;
65
66 nconf->buffer_mask =
67 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
68 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
69
70 format = PIPE_FORMAT_B8G8R8A8_UNORM;
71 if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE)) {
72 format = PIPE_FORMAT_A8R8G8B8_UNORM;
73 if (!drm_display_is_format_supported(&drmdpy->base, format, TRUE))
74 format = PIPE_FORMAT_NONE;
75 }
76 if (format == PIPE_FORMAT_NONE) {
77 FREE(drmdpy->config);
78 drmdpy->config = NULL;
79 return NULL;
80 }
81
82 nconf->color_format = format;
83
84 /* support KMS */
85 if (drmdpy->resources)
86 nconf->scanout_bit = TRUE;
87 }
88
89 configs = MALLOC(sizeof(*configs));
90 if (configs) {
91 configs[0] = &drmdpy->config->base;
92 if (num_configs)
93 *num_configs = 1;
94 }
95
96 return configs;
97 }
98
99 static int
100 drm_display_get_param(struct native_display *ndpy,
101 enum native_param_type param)
102 {
103 int val;
104
105 switch (param) {
106 case NATIVE_PARAM_USE_NATIVE_BUFFER:
107 case NATIVE_PARAM_PRESERVE_BUFFER:
108 case NATIVE_PARAM_MAX_SWAP_INTERVAL:
109 default:
110 val = 0;
111 break;
112 }
113
114 return val;
115 }
116
117 static void
118 drm_display_destroy(struct native_display *ndpy)
119 {
120 struct drm_display *drmdpy = drm_display(ndpy);
121
122 if (drmdpy->config)
123 FREE(drmdpy->config);
124
125 drm_display_fini_modeset(&drmdpy->base);
126
127 ndpy_uninit(ndpy);
128
129 if (drmdpy->fd >= 0)
130 close(drmdpy->fd);
131
132 FREE(drmdpy);
133 }
134
135 static const char *
136 get_drm_screen_name(int fd, drmVersionPtr version)
137 {
138 const char *name = version->name;
139
140 if (name && !strcmp(name, "radeon")) {
141 int chip_id;
142 struct drm_radeon_info info;
143
144 memset(&info, 0, sizeof(info));
145 info.request = RADEON_INFO_DEVICE_ID;
146 info.value = pointer_to_intptr(&chip_id);
147 if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
148 return NULL;
149
150 name = is_r3xx(chip_id) ? "r300" : "r600";
151 }
152
153 return name;
154 }
155
156 /**
157 * Initialize KMS and pipe screen.
158 */
159 static boolean
160 drm_display_init_screen(struct native_display *ndpy)
161 {
162 struct drm_display *drmdpy = drm_display(ndpy);
163 drmVersionPtr version;
164 const char *name;
165
166 version = drmGetVersion(drmdpy->fd);
167 if (!version) {
168 _eglLog(_EGL_WARNING, "invalid fd %d", drmdpy->fd);
169 return FALSE;
170 }
171
172 name = get_drm_screen_name(drmdpy->fd, version);
173 if (name) {
174 drmdpy->base.screen =
175 drmdpy->event_handler->new_drm_screen(&drmdpy->base, name, drmdpy->fd);
176 }
177 drmFreeVersion(version);
178
179 if (!drmdpy->base.screen) {
180 _eglLog(_EGL_DEBUG, "failed to create DRM screen");
181 return FALSE;
182 }
183
184 return TRUE;
185 }
186
187 static struct pipe_resource *
188 drm_display_import_buffer(struct native_display *ndpy,
189 const struct pipe_resource *templ,
190 void *buf)
191 {
192 return ndpy->screen->resource_from_handle(ndpy->screen,
193 templ, (struct winsys_handle *) buf);
194 }
195
196 static boolean
197 drm_display_export_buffer(struct native_display *ndpy,
198 struct pipe_resource *res,
199 void *buf)
200 {
201 return ndpy->screen->resource_get_handle(ndpy->screen,
202 res, (struct winsys_handle *) buf);
203 }
204
205 static struct native_display_buffer drm_display_buffer = {
206 drm_display_import_buffer,
207 drm_display_export_buffer
208 };
209
210 static struct native_display *
211 drm_create_display(int fd, struct native_event_handler *event_handler,
212 void *user_data)
213 {
214 struct drm_display *drmdpy;
215
216 drmdpy = CALLOC_STRUCT(drm_display);
217 if (!drmdpy)
218 return NULL;
219
220 drmdpy->fd = fd;
221 drmdpy->event_handler = event_handler;
222 drmdpy->base.user_data = user_data;
223
224 if (!drm_display_init_screen(&drmdpy->base)) {
225 drm_display_destroy(&drmdpy->base);
226 return NULL;
227 }
228
229 drmdpy->base.destroy = drm_display_destroy;
230 drmdpy->base.get_param = drm_display_get_param;
231 drmdpy->base.get_configs = drm_display_get_configs;
232
233 drmdpy->base.buffer = &drm_display_buffer;
234 drm_display_init_modeset(&drmdpy->base);
235
236 return &drmdpy->base;
237 }
238
239 static struct native_event_handler *drm_event_handler;
240
241 static void
242 native_set_event_handler(struct native_event_handler *event_handler)
243 {
244 drm_event_handler = event_handler;
245 }
246
247 static struct native_display *
248 native_create_display(void *dpy, boolean use_sw, void *user_data)
249 {
250 int fd;
251
252 if (dpy) {
253 fd = dup((int) pointer_to_intptr(dpy));
254 }
255 else {
256 fd = open("/dev/dri/card0", O_RDWR);
257 }
258 if (fd < 0)
259 return NULL;
260
261 return drm_create_display(fd, drm_event_handler, user_data);
262 }
263
264 static const struct native_platform drm_platform = {
265 "DRM", /* name */
266 native_set_event_handler,
267 native_create_display
268 };
269
270 const struct native_platform *
271 native_get_drm_platform(void)
272 {
273 return &drm_platform;
274 }