4ec4b675b31c04c2c0c8c1e42b8da52a708d27d9
[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 "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_fbo.h"
30 #include "nouveau_texture.h"
31 #include "nv04_driver.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
34
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/renderbuffer.h"
38 #include "swrast/s_renderbuffer.h"
39
40 static const __DRIextension *nouveau_screen_extensions[];
41
42 static void
43 nouveau_destroy_screen(__DRIscreen *dri_screen);
44
45 static const __DRIconfig **
46 nouveau_get_configs(void)
47 {
48 __DRIconfig **configs = NULL;
49 int i;
50
51 const uint8_t depth_bits[] = { 0, 16, 24, 24 };
52 const uint8_t stencil_bits[] = { 0, 0, 0, 8 };
53 const uint8_t msaa_samples[] = { 0 };
54
55 static const mesa_format formats[3] = {
56 MESA_FORMAT_RGB565,
57 MESA_FORMAT_B8G8R8A8_UNORM,
58 MESA_FORMAT_B8G8R8X8_UNORM,
59 };
60
61 const GLenum back_buffer_modes[] = {
62 GLX_NONE, GLX_SWAP_UNDEFINED_OML
63 };
64
65 for (i = 0; i < Elements(formats); i++) {
66 __DRIconfig **config;
67
68 config = driCreateConfigs(formats[i],
69 depth_bits, stencil_bits,
70 Elements(depth_bits),
71 back_buffer_modes,
72 Elements(back_buffer_modes),
73 msaa_samples,
74 Elements(msaa_samples),
75 GL_TRUE);
76 assert(config);
77
78 configs = driConcatConfigs(configs, config);
79 }
80
81 return (const __DRIconfig **)configs;
82 }
83
84 static const __DRIconfig **
85 nouveau_init_screen2(__DRIscreen *dri_screen)
86 {
87 const __DRIconfig **configs;
88 struct nouveau_screen *screen;
89 int ret;
90
91 /* Allocate the screen. */
92 screen = CALLOC_STRUCT(nouveau_screen);
93 if (!screen)
94 return NULL;
95
96 /* Open the DRM device. */
97 ret = nouveau_device_wrap(dri_screen->fd, 0, &screen->device);
98 if (ret) {
99 nouveau_error("Error opening the DRM device.\n");
100 goto fail;
101 }
102
103 /* Choose the card specific function pointers. */
104 switch (screen->device->chipset & 0xf0) {
105 case 0x00:
106 screen->driver = &nv04_driver;
107 break;
108 case 0x10:
109 screen->driver = &nv10_driver;
110 break;
111 case 0x20:
112 screen->driver = &nv20_driver;
113 break;
114 default:
115 assert(0);
116 }
117
118 /* Compat version validation will occur at context init after
119 * _mesa_compute_version().
120 */
121 dri_screen->max_gl_compat_version = 15;
122
123 /* NV10 and NV20 can support OpenGL ES 1.0 only. Older chips
124 * cannot do even that.
125 */
126 if ((screen->device->chipset & 0xf0) != 0x00)
127 dri_screen->max_gl_es1_version = 10;
128
129 dri_screen->driverPrivate = screen;
130 dri_screen->extensions = nouveau_screen_extensions;
131 screen->dri_screen = dri_screen;
132
133 configs = nouveau_get_configs();
134 if (!configs)
135 goto fail;
136
137 return configs;
138 fail:
139 nouveau_destroy_screen(dri_screen);
140 return NULL;
141
142 }
143
144 static void
145 nouveau_destroy_screen(__DRIscreen *dri_screen)
146 {
147 struct nouveau_screen *screen = dri_screen->driverPrivate;
148
149 if (!screen)
150 return;
151
152 nouveau_device_del(&screen->device);
153
154 free(screen);
155 dri_screen->driverPrivate = NULL;
156 }
157
158 static GLboolean
159 nouveau_create_buffer(__DRIscreen *dri_screen,
160 __DRIdrawable *drawable,
161 const struct gl_config *visual,
162 GLboolean is_pixmap)
163 {
164 struct gl_renderbuffer *rb;
165 struct gl_framebuffer *fb;
166 GLenum color_format;
167
168 if (is_pixmap)
169 return GL_FALSE; /* not implemented */
170
171 if (visual->redBits == 5)
172 color_format = GL_RGB5;
173 else if (visual->alphaBits == 0)
174 color_format = GL_RGB8;
175 else
176 color_format = GL_RGBA8;
177
178 fb = nouveau_framebuffer_dri_new(visual);
179 if (!fb)
180 return GL_FALSE;
181
182 /* Front buffer. */
183 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
184 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, rb);
185
186 /* Back buffer */
187 if (visual->doubleBufferMode) {
188 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
189 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, rb);
190 }
191
192 /* Depth/stencil buffer. */
193 if (visual->depthBits == 24 && visual->stencilBits == 8) {
194 rb = nouveau_renderbuffer_dri_new(GL_DEPTH24_STENCIL8_EXT, drawable);
195 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
196 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
197
198 } else if (visual->depthBits == 24) {
199 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT24, drawable);
200 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
201
202 } else if (visual->depthBits == 16) {
203 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT16, drawable);
204 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
205 }
206
207 /* Software renderbuffers. */
208 _swrast_add_soft_renderbuffers(fb, GL_FALSE, GL_FALSE, GL_FALSE,
209 visual->accumRedBits > 0,
210 GL_FALSE, GL_FALSE);
211
212 drawable->driverPrivate = fb;
213
214 return GL_TRUE;
215 }
216
217 static void
218 nouveau_destroy_buffer(__DRIdrawable *drawable)
219 {
220 _mesa_reference_framebuffer(
221 (struct gl_framebuffer **)&drawable->driverPrivate, NULL);
222 }
223
224 static void
225 nouveau_drawable_flush(__DRIdrawable *draw)
226 {
227 }
228
229 static const struct __DRI2flushExtensionRec nouveau_flush_extension = {
230 { __DRI2_FLUSH, 3 },
231 nouveau_drawable_flush,
232 dri2InvalidateDrawable,
233 };
234
235 static const struct __DRItexBufferExtensionRec nouveau_texbuffer_extension = {
236 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
237 NULL,
238 nouveau_set_texbuffer,
239 };
240
241 static const __DRIextension *nouveau_screen_extensions[] = {
242 &nouveau_flush_extension.base,
243 &nouveau_texbuffer_extension.base,
244 &dri2ConfigQueryExtension.base,
245 NULL
246 };
247
248 const struct __DriverAPIRec nouveau_driver_api = {
249 .InitScreen = nouveau_init_screen2,
250 .DestroyScreen = nouveau_destroy_screen,
251 .CreateBuffer = nouveau_create_buffer,
252 .DestroyBuffer = nouveau_destroy_buffer,
253 .CreateContext = nouveau_context_create,
254 .DestroyContext = nouveau_context_destroy,
255 .MakeCurrent = nouveau_context_make_current,
256 .UnbindContext = nouveau_context_unbind,
257 };
258
259 static const struct __DRIDriverVtableExtensionRec nouveau_vtable = {
260 .base = { __DRI_DRIVER_VTABLE, 1 },
261 .vtable = &nouveau_driver_api,
262 };
263
264 /* This is the table of extensions that the loader will dlsym() for. */
265 static const __DRIextension *nouveau_driver_extensions[] = {
266 &driCoreExtension.base,
267 &driDRI2Extension.base,
268 &nouveau_vtable.base,
269 NULL
270 };
271
272 PUBLIC const __DRIextension **__driDriverGetExtensions_nouveau_vieux(void)
273 {
274 globalDriverAPI = &nouveau_driver_api;
275
276 return nouveau_driver_extensions;
277 }