nouveau_screen.c revision 532828b1d3fdf5e9eed8d5ec72ada154b31f92cc
1/**************************************************************************
2
3Copyright 2006 Stephane Marchesin
4All Rights Reserved.
5
6Permission is hereby granted, free of charge, to any person obtaining a
7copy of this software and associated documentation files (the "Software"),
8to deal in the Software without restriction, including without limitation
9on the rights to use, copy, modify, merge, publish, distribute, sub
10license, and/or sell copies of the Software, and to permit persons to whom
11the Software is furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice (including the next
14paragraph) shall be included in all copies or substantial portions of the
15Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
21DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25**************************************************************************/
26
27#include "glheader.h"
28#include "imports.h"
29#include "mtypes.h"
30#include "framebuffer.h"
31#include "renderbuffer.h"
32
33#include "nouveau_context.h"
34#include "nouveau_screen.h"
35#include "nouveau_object.h"
36#include "nouveau_span.h"
37#include "nouveau_msg.h"
38
39#include "utils.h"
40#include "context.h"
41#include "vblank.h"
42#include "drirenderbuffer.h"
43
44#include "GL/internal/dri_interface.h"
45
46#include "xmlpool.h"
47
48PUBLIC const char __driConfigOptions[] =
49DRI_CONF_BEGIN
50    DRI_CONF_SECTION_DEBUG
51        DRI_CONF_NO_RAST(false)
52    DRI_CONF_SECTION_END
53DRI_CONF_END;
54static const GLuint __driNConfigOptions = 1;
55
56extern const struct dri_extension common_extensions[];
57extern const struct dri_extension nv10_extensions[];
58extern const struct dri_extension nv20_extensions[];
59extern const struct dri_extension nv30_extensions[];
60extern const struct dri_extension nv40_extensions[];
61extern const struct dri_extension nv50_extensions[];
62
63static nouveauScreenPtr nouveauCreateScreen(__DRIscreenPrivate *sPriv)
64{
65	nouveauScreenPtr screen;
66	NOUVEAUDRIPtr dri_priv=(NOUVEAUDRIPtr)sPriv->pDevPriv;
67
68	/* allocate screen */
69	screen = (nouveauScreenPtr) CALLOC( sizeof(*screen) );
70	if ( !screen ) {
71		__driUtilMessage("%s: Could not allocate memory for screen structure",__FUNCTION__);
72		return NULL;
73	}
74
75	screen->card=nouveau_card_lookup(dri_priv->device_id);
76	if (!screen->card) {
77		__driUtilMessage("%s: Unknown card type 0x%04x:0x%04x\n",
78			__func__, dri_priv->device_id >> 16, dri_priv->device_id & 0xFFFF);
79		FREE(screen);
80		return NULL;
81	}
82
83	/* parse information in __driConfigOptions */
84	driParseOptionInfo (&screen->optionCache,__driConfigOptions, __driNConfigOptions);
85
86	screen->fbFormat    = dri_priv->bpp / 8;
87	screen->frontOffset = dri_priv->front_offset;
88	screen->frontPitch  = dri_priv->front_pitch;
89	screen->backOffset  = dri_priv->back_offset;
90	screen->backPitch   = dri_priv->back_pitch;
91	screen->depthOffset = dri_priv->depth_offset;
92	screen->depthPitch  = dri_priv->depth_pitch;
93
94	screen->driScreen = sPriv;
95	return screen;
96}
97
98static void
99nouveauDestroyScreen(__DRIscreenPrivate *sPriv)
100{
101	nouveauScreenPtr screen = (nouveauScreenPtr)sPriv->private;
102
103	if (!screen) return;
104
105	/* free all option information */
106	driDestroyOptionInfo (&screen->optionCache);
107
108	FREE(screen);
109	sPriv->private = NULL;
110}
111
112static GLboolean nouveauInitDriver(__DRIscreenPrivate *sPriv)
113{
114	sPriv->private = (void *) nouveauCreateScreen( sPriv );
115	if ( !sPriv->private ) {
116		nouveauDestroyScreen( sPriv );
117		return GL_FALSE;
118	}
119
120	return GL_TRUE;
121}
122
123/**
124 * Create the Mesa framebuffer and renderbuffers for a given window/drawable.
125 *
126 * \todo This function (and its interface) will need to be updated to support
127 * pbuffers.
128 */
129static GLboolean
130nouveauCreateBuffer(__DRIscreenPrivate *driScrnPriv,
131                    __DRIdrawablePrivate *driDrawPriv,
132                    const __GLcontextModes *mesaVis,
133                    GLboolean isPixmap)
134{
135	nouveauScreenPtr screen = (nouveauScreenPtr) driScrnPriv->private;
136	nouveau_renderbuffer_t *nrb;
137	struct gl_framebuffer *fb;
138	const GLboolean swAccum = mesaVis->accumRedBits > 0;
139	const GLboolean swStencil = (mesaVis->stencilBits > 0 &&
140				     mesaVis->depthBits != 24);
141	GLenum color_format = screen->fbFormat == 4 ? GL_RGBA8 : GL_RGB5;
142
143	if (isPixmap)
144		return GL_FALSE; /* not implemented */
145
146	fb = _mesa_create_framebuffer(mesaVis);
147	if (!fb)
148		return GL_FALSE;
149
150	/* Front buffer */
151	nrb = nouveau_renderbuffer_new(color_format);
152	_mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &nrb->mesa);
153
154	if (mesaVis->doubleBufferMode) {
155		nrb = nouveau_renderbuffer_new(color_format);
156		_mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &nrb->mesa);
157	}
158
159	if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
160		nrb = nouveau_renderbuffer_new(GL_DEPTH24_STENCIL8_EXT);
161		_mesa_add_renderbuffer(fb, BUFFER_DEPTH, &nrb->mesa);
162		_mesa_add_renderbuffer(fb, BUFFER_STENCIL, &nrb->mesa);
163	} else
164	if (mesaVis->depthBits == 24) {
165		nrb = nouveau_renderbuffer_new(GL_DEPTH_COMPONENT24);
166		_mesa_add_renderbuffer(fb, BUFFER_DEPTH, &nrb->mesa);
167	} else
168	if (mesaVis->depthBits == 16) {
169		nrb = nouveau_renderbuffer_new(GL_DEPTH_COMPONENT16);
170		_mesa_add_renderbuffer(fb, BUFFER_DEPTH, &nrb->mesa);
171	}
172
173	_mesa_add_soft_renderbuffers(fb,
174				     GL_FALSE, /* color */
175				     GL_FALSE, /* depth */
176				     swStencil,
177				     swAccum,
178				     GL_FALSE, /* alpha */
179				     GL_FALSE  /* aux */);
180
181	driDrawPriv->driverPrivate = (void *) fb;
182	return (driDrawPriv->driverPrivate != NULL);
183}
184
185
186static void
187nouveauDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
188{
189	_mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
190}
191
192static int
193nouveauGetSwapInfo(__DRIdrawablePrivate *dpriv, __DRIswapInfo *sInfo)
194{
195	return -1;
196}
197
198static const struct __DriverAPIRec nouveauAPI = {
199	.DestroyScreen   = nouveauDestroyScreen,
200	.CreateContext   = nouveauCreateContext,
201	.DestroyContext  = nouveauDestroyContext,
202	.CreateBuffer    = nouveauCreateBuffer,
203	.DestroyBuffer   = nouveauDestroyBuffer,
204	.SwapBuffers     = nouveauSwapBuffers,
205	.MakeCurrent     = nouveauMakeCurrent,
206	.UnbindContext   = nouveauUnbindContext,
207	.GetSwapInfo     = nouveauGetSwapInfo,
208	.GetMSC          = driGetMSC32,
209	.GetDrawableMSC  = driDrawableGetMSC32,
210	.WaitForMSC      = driWaitForMSC32,
211	.WaitForSBC      = NULL,
212	.SwapBuffersMSC  = NULL,
213	.CopySubBuffer   = nouveauCopySubBuffer
214};
215
216
217static __GLcontextModes *
218nouveauFillInModes( unsigned pixel_bits, unsigned depth_bits,
219		 unsigned stencil_bits, GLboolean have_back_buffer )
220{
221	__GLcontextModes * modes;
222	__GLcontextModes * m;
223	unsigned num_modes;
224	unsigned depth_buffer_factor;
225	unsigned back_buffer_factor;
226	int i;
227
228	static const struct {
229		GLenum format;
230		GLenum type;
231	} fb_format_array[] = {
232		{ GL_RGB , GL_UNSIGNED_SHORT_5_6_5     },
233		{ GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV },
234		{ GL_BGR , GL_UNSIGNED_INT_8_8_8_8_REV },
235	};
236
237	/* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
238	 * support pageflipping at all.
239	 */
240	static const GLenum back_buffer_modes[] = {
241		GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
242	};
243
244	u_int8_t depth_bits_array[4]   = { 0, 16, 24, 24 };
245	u_int8_t stencil_bits_array[4] = { 0,  0,  0,  8 };
246
247	depth_buffer_factor = 4;
248	back_buffer_factor  = (have_back_buffer) ? 3 : 1;
249
250	num_modes = ((pixel_bits==16) ? 1 : 2) *
251		depth_buffer_factor * back_buffer_factor * 4;
252	modes = (*dri_interface->createContextModes)(num_modes,
253						     sizeof(__GLcontextModes));
254	m = modes;
255
256	for (i=((pixel_bits==16)?0:1);i<((pixel_bits==16)?1:3);i++) {
257		if (!driFillInModes(&m, fb_format_array[i].format,
258					fb_format_array[i].type,
259					depth_bits_array,
260					stencil_bits_array,
261					depth_buffer_factor,
262					back_buffer_modes,
263					back_buffer_factor,
264					GLX_TRUE_COLOR)) {
265		fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
266				__func__, __LINE__ );
267		return NULL;
268		}
269
270		if (!driFillInModes(&m, fb_format_array[i].format,
271					fb_format_array[i].type,
272					depth_bits_array,
273					stencil_bits_array,
274					depth_buffer_factor,
275					back_buffer_modes,
276					back_buffer_factor,
277					GLX_DIRECT_COLOR)) {
278		fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
279				__func__, __LINE__ );
280		return NULL;
281		}
282	}
283
284	return modes;
285}
286
287
288/**
289 * This is the driver specific part of the createNewScreen entry point.
290 *
291 * \todo maybe fold this into intelInitDriver
292 *
293 * \return the __GLcontextModes supported by this driver
294 */
295__GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
296{
297	static const __DRIversion ddx_expected = { 0, 0, NOUVEAU_DRM_HEADER_PATCHLEVEL };
298	static const __DRIversion dri_expected = { 4, 0, 0 };
299	static const __DRIversion drm_expected = { 0, 0, NOUVEAU_DRM_HEADER_PATCHLEVEL };
300	NOUVEAUDRIPtr dri_priv = (NOUVEAUDRIPtr)psp->pDevPriv;
301
302	WARN_ONCE("\nThis driver is not currently maintained\n\n"
303		  "Current work on 3D is in the gallium-0.1 branch of:\n"
304		  "  git://anongit.freedesktop.org/git/nouveau/mesa\n");
305
306#if NOUVEAU_DRM_HEADER_PATCHLEVEL != 10
307#error nouveau_drm.h version doesn't match expected version
308#endif
309
310	if (!driCheckDriDdxDrmVersions2("nouveau",
311					&psp->dri_version, & dri_expected,
312					&psp->ddx_version, & ddx_expected,
313					&psp->drm_version, & drm_expected))
314		return NULL;
315
316	// temporary lock step versioning
317	if (drm_expected.patch != psp->drm_version.patch) {
318		__driUtilMessage("%s: wrong DRM version, expected %d, got %d\n",
319				 __func__,
320				 drm_expected.patch, psp->drm_version.patch);
321		return NULL;
322	}
323
324	psp->DriverAPI = nouveauAPI;
325
326	/* Calling driInitExtensions here, with a NULL context
327	 * pointer, does not actually enable the extensions.  It just
328	 * makes sure that all the dispatch offsets for all the
329	 * extensions that *might* be enables are known.  This is
330	 * needed because the dispatch offsets need to be known when
331	 * _mesa_context_create is called, but we can't enable the
332	 * extensions until we have a context pointer.
333	 *
334	 * Hello chicken.  Hello egg.  How are you two today?
335	 */
336	driInitExtensions( NULL, common_extensions, GL_FALSE );
337	driInitExtensions( NULL,   nv10_extensions, GL_FALSE );
338	driInitExtensions( NULL,   nv10_extensions, GL_FALSE );
339	driInitExtensions( NULL,   nv30_extensions, GL_FALSE );
340	driInitExtensions( NULL,   nv40_extensions, GL_FALSE );
341	driInitExtensions( NULL,   nv50_extensions, GL_FALSE );
342
343	if (!nouveauInitDriver(psp))
344		return NULL;
345
346	return nouveauFillInModes(dri_priv->bpp,
347				  (dri_priv->bpp == 16) ? 16 : 24,
348				  (dri_priv->bpp == 16) ? 0  : 8,
349				  1);
350}
351
352