Merge branch 'gallium-vertex-linear' into gallium-0.1
[mesa.git] / src / mesa / drivers / dri / sis / sis_screen.c
1 /**************************************************************************
2
3 Copyright 2003 Eric Anholt
4 All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 on the rights to use, copy, modify, merge, publish, distribute, sub
10 license, and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice (including the next
14 paragraph) shall be included in all copies or substantial portions of the
15 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 NON-INFRINGEMENT. IN NO EVENT SHALL
20 ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 **************************************************************************/
25
26 /*
27 * Authors:
28 * Eric Anholt <anholt@FreeBSD.org>
29 */
30
31 #include "dri_util.h"
32
33 #include "context.h"
34 #include "utils.h"
35 #include "imports.h"
36 #include "framebuffer.h"
37 #include "renderbuffer.h"
38
39 #include "sis_context.h"
40 #include "sis_dri.h"
41 #include "sis_lock.h"
42 #include "sis_span.h"
43
44 #include "xmlpool.h"
45
46 #include "GL/internal/dri_interface.h"
47
48 #define SIS_AGP_DISABLE(def) \
49 DRI_CONF_OPT_BEGIN(agp_disable,bool,def) \
50 DRI_CONF_DESC(en,"Disable AGP vertex dispatch") \
51 DRI_CONF_OPT_END
52
53 PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN
55 DRI_CONF_SECTION_QUALITY
56 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
57 DRI_CONF_SECTION_END
58 DRI_CONF_SECTION_DEBUG
59 SIS_AGP_DISABLE(true)
60 DRI_CONF_NO_RAST(false)
61 DRI_CONF_SECTION_END
62 DRI_CONF_END;
63 static const GLuint __driNConfigOptions = 3;
64
65 extern const struct dri_extension card_extensions[];
66
67 static __GLcontextModes *
68 sisFillInModes(int bpp)
69 {
70 __GLcontextModes *modes;
71 __GLcontextModes *m;
72 unsigned num_modes;
73 unsigned depth_buffer_factor;
74 unsigned back_buffer_factor;
75 GLenum fb_format;
76 GLenum fb_type;
77 static const GLenum back_buffer_modes[] = {
78 GLX_NONE, GLX_SWAP_UNDEFINED_OML
79 };
80 u_int8_t depth_bits_array[4];
81 u_int8_t stencil_bits_array[4];
82
83 depth_bits_array[0] = 0;
84 stencil_bits_array[0] = 0;
85 depth_bits_array[1] = 16;
86 stencil_bits_array[1] = 0;
87 depth_bits_array[2] = 24;
88 stencil_bits_array[2] = 8;
89 depth_bits_array[3] = 32;
90 stencil_bits_array[3] = 0;
91
92 depth_buffer_factor = 4;
93 back_buffer_factor = 2;
94
95 /* Last 4 is for GLX_TRUE_COLOR & GLX_DIRECT_COLOR, with/without accum */
96 num_modes = depth_buffer_factor * back_buffer_factor * 4;
97
98 if (bpp == 16) {
99 fb_format = GL_RGB;
100 fb_type = GL_UNSIGNED_SHORT_5_6_5;
101 } else {
102 fb_format = GL_BGRA;
103 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
104 }
105
106 modes = (*dri_interface->createContextModes)(num_modes, sizeof(__GLcontextModes));
107 m = modes;
108 if (!driFillInModes(&m, fb_format, fb_type, depth_bits_array,
109 stencil_bits_array, depth_buffer_factor,
110 back_buffer_modes, back_buffer_factor,
111 GLX_TRUE_COLOR)) {
112 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__, __LINE__);
113 return NULL;
114 }
115
116 if (!driFillInModes(&m, fb_format, fb_type, depth_bits_array,
117 stencil_bits_array, depth_buffer_factor,
118 back_buffer_modes, back_buffer_factor,
119 GLX_DIRECT_COLOR)) {
120 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__, __LINE__);
121 return NULL;
122 }
123
124 return modes;
125 }
126
127
128 /* Create the device specific screen private data struct.
129 */
130 static sisScreenPtr
131 sisCreateScreen( __DRIscreenPrivate *sPriv )
132 {
133 sisScreenPtr sisScreen;
134 SISDRIPtr sisDRIPriv = (SISDRIPtr)sPriv->pDevPriv;
135
136 if (sPriv->devPrivSize != sizeof(SISDRIRec)) {
137 fprintf(stderr,"\nERROR! sizeof(SISDRIRec) does not match passed size from device driver\n");
138 return GL_FALSE;
139 }
140
141 /* Allocate the private area */
142 sisScreen = (sisScreenPtr)CALLOC( sizeof(*sisScreen) );
143 if ( sisScreen == NULL )
144 return NULL;
145
146 sisScreen->screenX = sisDRIPriv->width;
147 sisScreen->screenY = sisDRIPriv->height;
148 sisScreen->cpp = sisDRIPriv->bytesPerPixel;
149 sisScreen->deviceID = sisDRIPriv->deviceID;
150 sisScreen->AGPCmdBufOffset = sisDRIPriv->AGPCmdBufOffset;
151 sisScreen->AGPCmdBufSize = sisDRIPriv->AGPCmdBufSize;
152 sisScreen->sarea_priv_offset = sizeof(drm_sarea_t);
153
154 sisScreen->mmio.handle = sisDRIPriv->regs.handle;
155 sisScreen->mmio.size = sisDRIPriv->regs.size;
156 if ( drmMap( sPriv->fd, sisScreen->mmio.handle, sisScreen->mmio.size,
157 &sisScreen->mmio.map ) )
158 {
159 FREE( sisScreen );
160 return NULL;
161 }
162
163 if (sisDRIPriv->agp.size) {
164 sisScreen->agp.handle = sisDRIPriv->agp.handle;
165 sisScreen->agpBaseOffset = drmAgpBase(sPriv->fd);
166 sisScreen->agp.size = sisDRIPriv->agp.size;
167 if ( drmMap( sPriv->fd, sisScreen->agp.handle, sisScreen->agp.size,
168 &sisScreen->agp.map ) )
169 {
170 sisScreen->agp.size = 0;
171 }
172 }
173
174 sisScreen->driScreen = sPriv;
175
176 /* parse information in __driConfigOptions */
177 driParseOptionInfo(&sisScreen->optionCache,
178 __driConfigOptions, __driNConfigOptions);
179
180 return sisScreen;
181 }
182
183 /* Destroy the device specific screen private data struct.
184 */
185 static void
186 sisDestroyScreen( __DRIscreenPrivate *sPriv )
187 {
188 sisScreenPtr sisScreen = (sisScreenPtr)sPriv->private;
189
190 if ( sisScreen == NULL )
191 return;
192
193 if (sisScreen->agp.size != 0)
194 drmUnmap( sisScreen->agp.map, sisScreen->agp.size );
195 drmUnmap( sisScreen->mmio.map, sisScreen->mmio.size );
196
197 FREE( sisScreen );
198 sPriv->private = NULL;
199 }
200
201
202 /* Create and initialize the Mesa and driver specific pixmap buffer
203 * data.
204 */
205 static GLboolean
206 sisCreateBuffer( __DRIscreenPrivate *driScrnPriv,
207 __DRIdrawablePrivate *driDrawPriv,
208 const __GLcontextModes *mesaVis,
209 GLboolean isPixmap )
210 {
211 /*sisScreenPtr screen = (sisScreenPtr) driScrnPriv->private;*/
212 struct gl_framebuffer *fb;
213
214 if (isPixmap)
215 return GL_FALSE; /* not implemented */
216
217 fb = _mesa_create_framebuffer(mesaVis);
218
219 _mesa_add_soft_renderbuffers(fb,
220 GL_FALSE, /* color */
221 GL_FALSE, /* depth */
222 mesaVis->stencilBits > 0,
223 mesaVis->accumRedBits > 0,
224 GL_FALSE, /* alpha */
225 GL_FALSE /* aux */);
226 driDrawPriv->driverPrivate = (void *) fb;
227
228 return (driDrawPriv->driverPrivate != NULL);
229 }
230
231
232 static void
233 sisDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
234 {
235 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
236 }
237
238 static void sisCopyBuffer( __DRIdrawablePrivate *dPriv )
239 {
240 sisContextPtr smesa = (sisContextPtr)dPriv->driContextPriv->driverPrivate;
241 int i;
242
243 while ((*smesa->FrameCountPtr) - MMIO_READ(0x8a2c) > SIS_MAX_FRAME_LENGTH)
244 ;
245
246 LOCK_HARDWARE();
247
248 for (i = 0; i < dPriv->numClipRects; i++) {
249 drm_clip_rect_t *box = &dPriv->pClipRects[i];
250
251 mWait3DCmdQueue(10);
252 MMIO(REG_SRC_ADDR, smesa->back.offset);
253 MMIO(REG_SRC_PITCH, smesa->back.pitch | ((smesa->bytesPerPixel == 4) ?
254 BLIT_DEPTH_32 : BLIT_DEPTH_16));
255 MMIO(REG_SRC_X_Y, ((box->x1 - dPriv->x) << 16) | (box->y1 - dPriv->y));
256 MMIO(REG_DST_X_Y, ((box->x1 - dPriv->x) << 16) | (box->y1 - dPriv->y));
257 MMIO(REG_DST_ADDR, smesa->front.offset);
258 MMIO(REG_DST_PITCH_HEIGHT, (smesa->virtualY << 16) | smesa->front.pitch);
259 MMIO(REG_WIDTH_HEIGHT, ((box->y2 - box->y1) << 16) | (box->x2 - box->x1));
260 MMIO(REG_BLIT_CMD, CMD_DIR_X_INC | CMD_DIR_Y_INC | CMD_ROP_SRC);
261 MMIO(REG_CommandQueue, -1);
262 }
263
264 *(GLint *)(smesa->IOBase+0x8a2c) = *smesa->FrameCountPtr;
265 (*smesa->FrameCountPtr)++;
266
267 UNLOCK_HARDWARE ();
268 }
269
270
271 /* Copy the back color buffer to the front color buffer */
272 static void
273 sisSwapBuffers(__DRIdrawablePrivate *dPriv)
274 {
275 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
276 sisContextPtr smesa = (sisContextPtr) dPriv->driContextPriv->driverPrivate;
277 GLcontext *ctx = smesa->glCtx;
278
279 if (ctx->Visual.doubleBufferMode) {
280 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
281 sisCopyBuffer( dPriv );
282 }
283 } else {
284 /* XXX this shouldn't be an error but we can't handle it for now */
285 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
286 }
287 }
288
289
290 /* Initialize the driver specific screen private data.
291 */
292 static GLboolean
293 sisInitDriver( __DRIscreenPrivate *sPriv )
294 {
295 sPriv->private = (void *) sisCreateScreen( sPriv );
296
297 if ( !sPriv->private ) {
298 sisDestroyScreen( sPriv );
299 return GL_FALSE;
300 }
301
302 return GL_TRUE;
303 }
304
305 static struct __DriverAPIRec sisAPI = {
306 .InitDriver = sisInitDriver,
307 .DestroyScreen = sisDestroyScreen,
308 .CreateContext = sisCreateContext,
309 .DestroyContext = sisDestroyContext,
310 .CreateBuffer = sisCreateBuffer,
311 .DestroyBuffer = sisDestroyBuffer,
312 .SwapBuffers = sisSwapBuffers,
313 .MakeCurrent = sisMakeCurrent,
314 .UnbindContext = sisUnbindContext,
315 .GetSwapInfo = NULL,
316 .GetMSC = NULL,
317 .WaitForMSC = NULL,
318 .WaitForSBC = NULL,
319 .SwapBuffersMSC = NULL
320
321 };
322
323
324 /**
325 * This is the bootstrap function for the driver. libGL supplies all of the
326 * requisite information about the system, and the driver initializes itself.
327 * This routine also fills in the linked list pointed to by \c driver_modes
328 * with the \c __GLcontextModes that the driver can support for windows or
329 * pbuffers.
330 *
331 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
332 * failure.
333 */
334 PUBLIC
335 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn,
336 __DRIscreen *psc,
337 const __GLcontextModes *modes,
338 const __DRIversion *ddx_version,
339 const __DRIversion *dri_version,
340 const __DRIversion *drm_version,
341 const __DRIframebuffer *frame_buffer,
342 drmAddress pSAREA, int fd,
343 int internal_api_version,
344 const __DRIinterfaceMethods * interface,
345 __GLcontextModes **driver_modes )
346
347 {
348 __DRIscreenPrivate *psp;
349 static const __DRIversion ddx_expected = {0, 8, 0};
350 static const __DRIversion dri_expected = {4, 0, 0};
351 static const __DRIversion drm_expected = {1, 0, 0};
352 static const char *driver_name = "SiS";
353 dri_interface = interface;
354
355 if (!driCheckDriDdxDrmVersions2(driver_name, dri_version, &dri_expected,
356 ddx_version, &ddx_expected,
357 drm_version, &drm_expected)) {
358 return NULL;
359 }
360
361 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
362 ddx_version, dri_version, drm_version,
363 frame_buffer, pSAREA, fd,
364 internal_api_version, &sisAPI);
365 if (psp != NULL) {
366 SISDRIPtr dri_priv = (SISDRIPtr)psp->pDevPriv;
367 *driver_modes = sisFillInModes(dri_priv->bytesPerPixel * 8);
368
369 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
370 * enable the extensions. It just makes sure that all the dispatch offsets for all
371 * the extensions that *might* be enables are known. This is needed because the
372 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
373 * enable the extensions until we have a context pointer.
374 *
375 * Hello chicken. Hello egg. How are you two today?
376 */
377 driInitExtensions( NULL, card_extensions, GL_FALSE );
378 }
379
380 return (void *)psp;
381 }