150dd8e9209a96a06cf8a161e8f3c0b32aec75e1
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_screen.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include <xf86drm.h>
28 #include <nouveau_drm.h>
29 #include "nouveau_driver.h"
30 #include "nouveau_context.h"
31 #include "nouveau_fbo.h"
32 #include "nouveau_texture.h"
33 #include "nv04_driver.h"
34 #include "nv10_driver.h"
35 #include "nv20_driver.h"
36
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/s_renderbuffer.h"
41
42 static const __DRIextension *nouveau_screen_extensions[];
43
44 static void
45 nouveau_destroy_screen(__DRIscreen *dri_screen);
46
47 static const __DRIconfig **
48 nouveau_get_configs(void)
49 {
50 __DRIconfig **configs = NULL;
51 int i;
52
53 const uint8_t depth_bits[] = { 0, 16, 24, 24 };
54 const uint8_t stencil_bits[] = { 0, 0, 0, 8 };
55 const uint8_t msaa_samples[] = { 0 };
56
57 static const mesa_format formats[3] = {
58 MESA_FORMAT_B5G6R5_UNORM,
59 MESA_FORMAT_B8G8R8A8_UNORM,
60 MESA_FORMAT_B8G8R8X8_UNORM,
61 };
62
63 const GLenum back_buffer_modes[] = {
64 GLX_NONE, GLX_SWAP_UNDEFINED_OML
65 };
66
67 for (i = 0; i < Elements(formats); i++) {
68 __DRIconfig **config;
69
70 config = driCreateConfigs(formats[i],
71 depth_bits, stencil_bits,
72 Elements(depth_bits),
73 back_buffer_modes,
74 Elements(back_buffer_modes),
75 msaa_samples,
76 Elements(msaa_samples),
77 GL_TRUE);
78 assert(config);
79
80 configs = driConcatConfigs(configs, config);
81 }
82
83 return (const __DRIconfig **)configs;
84 }
85
86 static const __DRIconfig **
87 nouveau_init_screen2(__DRIscreen *dri_screen)
88 {
89 const __DRIconfig **configs;
90 struct nouveau_screen *screen;
91 int ret;
92
93 /* Allocate the screen. */
94 screen = CALLOC_STRUCT(nouveau_screen);
95 if (!screen)
96 return NULL;
97
98 dri_screen->driverPrivate = screen;
99
100 /* Open the DRM device. */
101 ret = nouveau_device_wrap(dri_screen->fd, 0, &screen->device);
102 if (ret) {
103 nouveau_error("Error opening the DRM device.\n");
104 goto fail;
105 }
106
107 /* Choose the card specific function pointers. */
108 switch (screen->device->chipset & 0xf0) {
109 case 0x00:
110 screen->driver = &nv04_driver;
111 dri_screen->max_gl_compat_version = 12;
112 break;
113 case 0x10:
114 screen->driver = &nv10_driver;
115 dri_screen->max_gl_compat_version = 12;
116 dri_screen->max_gl_es1_version = 10;
117 break;
118 case 0x20:
119 screen->driver = &nv20_driver;
120 dri_screen->max_gl_compat_version = 13;
121 dri_screen->max_gl_es1_version = 10;
122 break;
123 default:
124 nouveau_error("Unknown chipset: %02X\n",
125 screen->device->chipset);
126 goto fail;
127 }
128
129 dri_screen->extensions = nouveau_screen_extensions;
130 screen->dri_screen = dri_screen;
131
132 configs = nouveau_get_configs();
133 if (!configs)
134 goto fail;
135
136 return configs;
137 fail:
138 nouveau_destroy_screen(dri_screen);
139 return NULL;
140
141 }
142
143 static int
144 nouveau_query_renderer_integer(__DRIscreen *psp, int param,
145 unsigned int *value)
146 {
147 const struct nouveau_screen *const screen =
148 (struct nouveau_screen *) psp->driverPrivate;
149
150 switch (param) {
151 case __DRI2_RENDERER_VENDOR_ID:
152 value[0] = 0x10de;
153 return 0;
154 case __DRI2_RENDERER_DEVICE_ID: {
155 uint64_t device_id;
156
157 if (nouveau_getparam(screen->device,
158 NOUVEAU_GETPARAM_PCI_DEVICE,
159 &device_id)) {
160 nouveau_error("Error retrieving the device PCIID.\n");
161 device_id = -1;
162 }
163 value[0] = (unsigned int) device_id;
164 return 0;
165 }
166 case __DRI2_RENDERER_ACCELERATED:
167 value[0] = 1;
168 return 0;
169 case __DRI2_RENDERER_VIDEO_MEMORY:
170 /* XXX: return vram_size or vram_limit ? */
171 value[0] = screen->device->vram_size >> 20;
172 return 0;
173 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
174 value[0] = 0;
175 return 0;
176 default:
177 return driQueryRendererIntegerCommon(psp, param, value);
178 }
179 }
180
181 static int
182 nouveau_query_renderer_string(__DRIscreen *psp, int param, const char **value)
183 {
184 const struct nouveau_screen *const screen =
185 (struct nouveau_screen *) psp->driverPrivate;
186
187 switch (param) {
188 case __DRI2_RENDERER_VENDOR_ID:
189 value[0] = nouveau_vendor_string;
190 return 0;
191 case __DRI2_RENDERER_DEVICE_ID:
192 value[0] = nouveau_get_renderer_string(screen->device->chipset);
193 return 0;
194 default:
195 return -1;
196 }
197 }
198
199 static const __DRI2rendererQueryExtension nouveau_renderer_query_extension = {
200 .base = { __DRI2_RENDERER_QUERY, 1 },
201
202 .queryInteger = nouveau_query_renderer_integer,
203 .queryString = nouveau_query_renderer_string
204 };
205
206 static void
207 nouveau_destroy_screen(__DRIscreen *dri_screen)
208 {
209 struct nouveau_screen *screen = dri_screen->driverPrivate;
210
211 if (!screen)
212 return;
213
214 nouveau_device_del(&screen->device);
215
216 free(screen);
217 dri_screen->driverPrivate = NULL;
218 }
219
220 static GLboolean
221 nouveau_create_buffer(__DRIscreen *dri_screen,
222 __DRIdrawable *drawable,
223 const struct gl_config *visual,
224 GLboolean is_pixmap)
225 {
226 struct gl_renderbuffer *rb;
227 struct gl_framebuffer *fb;
228 GLenum color_format;
229
230 if (is_pixmap)
231 return GL_FALSE; /* not implemented */
232
233 if (visual->redBits == 5)
234 color_format = GL_RGB5;
235 else if (visual->alphaBits == 0)
236 color_format = GL_RGB8;
237 else
238 color_format = GL_RGBA8;
239
240 fb = nouveau_framebuffer_dri_new(visual);
241 if (!fb)
242 return GL_FALSE;
243
244 /* Front buffer. */
245 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
246 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, rb);
247
248 /* Back buffer */
249 if (visual->doubleBufferMode) {
250 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
251 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, rb);
252 }
253
254 /* Depth/stencil buffer. */
255 if (visual->depthBits == 24 && visual->stencilBits == 8) {
256 rb = nouveau_renderbuffer_dri_new(GL_DEPTH24_STENCIL8_EXT, drawable);
257 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
258 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
259
260 } else if (visual->depthBits == 24) {
261 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT24, drawable);
262 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
263
264 } else if (visual->depthBits == 16) {
265 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT16, drawable);
266 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
267 }
268
269 /* Software renderbuffers. */
270 _swrast_add_soft_renderbuffers(fb, GL_FALSE, GL_FALSE, GL_FALSE,
271 visual->accumRedBits > 0,
272 GL_FALSE, GL_FALSE);
273
274 drawable->driverPrivate = fb;
275
276 return GL_TRUE;
277 }
278
279 static void
280 nouveau_destroy_buffer(__DRIdrawable *drawable)
281 {
282 _mesa_reference_framebuffer(
283 (struct gl_framebuffer **)&drawable->driverPrivate, NULL);
284 }
285
286 static void
287 nouveau_drawable_flush(__DRIdrawable *draw)
288 {
289 }
290
291 static const struct __DRI2flushExtensionRec nouveau_flush_extension = {
292 .base = { __DRI2_FLUSH, 3 },
293
294 .flush = nouveau_drawable_flush,
295 .invalidate = dri2InvalidateDrawable,
296 };
297
298 static const struct __DRItexBufferExtensionRec nouveau_texbuffer_extension = {
299 .base = { __DRI_TEX_BUFFER, 3 },
300
301 .setTexBuffer = NULL,
302 .setTexBuffer2 = nouveau_set_texbuffer,
303 .releaseTexBuffer = NULL,
304 };
305
306 static const __DRIextension *nouveau_screen_extensions[] = {
307 &nouveau_flush_extension.base,
308 &nouveau_texbuffer_extension.base,
309 &nouveau_renderer_query_extension.base,
310 &dri2ConfigQueryExtension.base,
311 NULL
312 };
313
314 const struct __DriverAPIRec nouveau_driver_api = {
315 .InitScreen = nouveau_init_screen2,
316 .DestroyScreen = nouveau_destroy_screen,
317 .CreateBuffer = nouveau_create_buffer,
318 .DestroyBuffer = nouveau_destroy_buffer,
319 .CreateContext = nouveau_context_create,
320 .DestroyContext = nouveau_context_destroy,
321 .MakeCurrent = nouveau_context_make_current,
322 .UnbindContext = nouveau_context_unbind,
323 };
324
325 static const struct __DRIDriverVtableExtensionRec nouveau_vtable = {
326 .base = { __DRI_DRIVER_VTABLE, 1 },
327 .vtable = &nouveau_driver_api,
328 };
329
330 /* This is the table of extensions that the loader will dlsym() for. */
331 static const __DRIextension *nouveau_driver_extensions[] = {
332 &driCoreExtension.base,
333 &driDRI2Extension.base,
334 &nouveau_vtable.base,
335 NULL
336 };
337
338 PUBLIC const __DRIextension **__driDriverGetExtensions_nouveau_vieux(void)
339 {
340 globalDriverAPI = &nouveau_driver_api;
341
342 return nouveau_driver_extensions;
343 }