Merge commit 'origin/master' into gallium-0.2
[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 const __DRIconfig **
68 sisFillInModes(__DRIscreenPrivate *psp, int bpp)
69 {
70 __DRIconfig **configs;
71 unsigned depth_buffer_factor;
72 unsigned back_buffer_factor;
73 GLenum fb_format;
74 GLenum fb_type;
75 static const GLenum back_buffer_modes[] = {
76 GLX_NONE, GLX_SWAP_UNDEFINED_OML
77 };
78 uint8_t depth_bits_array[4];
79 uint8_t stencil_bits_array[4];
80
81 depth_bits_array[0] = 0;
82 stencil_bits_array[0] = 0;
83 depth_bits_array[1] = 16;
84 stencil_bits_array[1] = 0;
85 depth_bits_array[2] = 24;
86 stencil_bits_array[2] = 8;
87 depth_bits_array[3] = 32;
88 stencil_bits_array[3] = 0;
89
90 depth_buffer_factor = 4;
91 back_buffer_factor = 2;
92
93 if (bpp == 16) {
94 fb_format = GL_RGB;
95 fb_type = GL_UNSIGNED_SHORT_5_6_5;
96 } else {
97 fb_format = GL_BGRA;
98 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
99 }
100
101 configs = driCreateConfigs(fb_format, fb_type, depth_bits_array,
102 stencil_bits_array, depth_buffer_factor,
103 back_buffer_modes, back_buffer_factor);
104 if (configs == NULL) {
105 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__, __LINE__);
106 return NULL;
107 }
108
109 return (const __DRIconfig **) configs;
110 }
111
112
113 /* Create the device specific screen private data struct.
114 */
115 static sisScreenPtr
116 sisCreateScreen( __DRIscreenPrivate *sPriv )
117 {
118 sisScreenPtr sisScreen;
119 SISDRIPtr sisDRIPriv = (SISDRIPtr)sPriv->pDevPriv;
120
121 if (sPriv->devPrivSize != sizeof(SISDRIRec)) {
122 fprintf(stderr,"\nERROR! sizeof(SISDRIRec) does not match passed size from device driver\n");
123 return GL_FALSE;
124 }
125
126 /* Allocate the private area */
127 sisScreen = (sisScreenPtr)CALLOC( sizeof(*sisScreen) );
128 if ( sisScreen == NULL )
129 return NULL;
130
131 sisScreen->screenX = sisDRIPriv->width;
132 sisScreen->screenY = sisDRIPriv->height;
133 sisScreen->cpp = sisDRIPriv->bytesPerPixel;
134 sisScreen->deviceID = sisDRIPriv->deviceID;
135 sisScreen->AGPCmdBufOffset = sisDRIPriv->AGPCmdBufOffset;
136 sisScreen->AGPCmdBufSize = sisDRIPriv->AGPCmdBufSize;
137 sisScreen->sarea_priv_offset = sizeof(drm_sarea_t);
138
139 sisScreen->mmio.handle = sisDRIPriv->regs.handle;
140 sisScreen->mmio.size = sisDRIPriv->regs.size;
141 if ( drmMap( sPriv->fd, sisScreen->mmio.handle, sisScreen->mmio.size,
142 &sisScreen->mmio.map ) )
143 {
144 FREE( sisScreen );
145 return NULL;
146 }
147
148 if (sisDRIPriv->agp.size) {
149 sisScreen->agp.handle = sisDRIPriv->agp.handle;
150 sisScreen->agpBaseOffset = drmAgpBase(sPriv->fd);
151 sisScreen->agp.size = sisDRIPriv->agp.size;
152 if ( drmMap( sPriv->fd, sisScreen->agp.handle, sisScreen->agp.size,
153 &sisScreen->agp.map ) )
154 {
155 sisScreen->agp.size = 0;
156 }
157 }
158
159 sisScreen->driScreen = sPriv;
160
161 /* parse information in __driConfigOptions */
162 driParseOptionInfo(&sisScreen->optionCache,
163 __driConfigOptions, __driNConfigOptions);
164
165 return sisScreen;
166 }
167
168 /* Destroy the device specific screen private data struct.
169 */
170 static void
171 sisDestroyScreen( __DRIscreenPrivate *sPriv )
172 {
173 sisScreenPtr sisScreen = (sisScreenPtr)sPriv->private;
174
175 if ( sisScreen == NULL )
176 return;
177
178 if (sisScreen->agp.size != 0)
179 drmUnmap( sisScreen->agp.map, sisScreen->agp.size );
180 drmUnmap( sisScreen->mmio.map, sisScreen->mmio.size );
181
182 FREE( sisScreen );
183 sPriv->private = NULL;
184 }
185
186
187 /* Create and initialize the Mesa and driver specific pixmap buffer
188 * data.
189 */
190 static GLboolean
191 sisCreateBuffer( __DRIscreenPrivate *driScrnPriv,
192 __DRIdrawablePrivate *driDrawPriv,
193 const __GLcontextModes *mesaVis,
194 GLboolean isPixmap )
195 {
196 /*sisScreenPtr screen = (sisScreenPtr) driScrnPriv->private;*/
197 struct gl_framebuffer *fb;
198
199 if (isPixmap)
200 return GL_FALSE; /* not implemented */
201
202 fb = _mesa_create_framebuffer(mesaVis);
203
204 _mesa_add_soft_renderbuffers(fb,
205 GL_FALSE, /* color */
206 GL_FALSE, /* depth */
207 mesaVis->stencilBits > 0,
208 mesaVis->accumRedBits > 0,
209 GL_FALSE, /* alpha */
210 GL_FALSE /* aux */);
211 driDrawPriv->driverPrivate = (void *) fb;
212
213 return (driDrawPriv->driverPrivate != NULL);
214 }
215
216
217 static void
218 sisDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
219 {
220 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
221 }
222
223 static void sisCopyBuffer( __DRIdrawablePrivate *dPriv )
224 {
225 sisContextPtr smesa = (sisContextPtr)dPriv->driContextPriv->driverPrivate;
226 int i;
227
228 while ((*smesa->FrameCountPtr) - MMIO_READ(0x8a2c) > SIS_MAX_FRAME_LENGTH)
229 ;
230
231 LOCK_HARDWARE();
232
233 for (i = 0; i < dPriv->numClipRects; i++) {
234 drm_clip_rect_t *box = &dPriv->pClipRects[i];
235
236 mWait3DCmdQueue(10);
237 MMIO(REG_SRC_ADDR, smesa->back.offset);
238 MMIO(REG_SRC_PITCH, smesa->back.pitch | ((smesa->bytesPerPixel == 4) ?
239 BLIT_DEPTH_32 : BLIT_DEPTH_16));
240 MMIO(REG_SRC_X_Y, ((box->x1 - dPriv->x) << 16) | (box->y1 - dPriv->y));
241 MMIO(REG_DST_X_Y, ((box->x1 - dPriv->x) << 16) | (box->y1 - dPriv->y));
242 MMIO(REG_DST_ADDR, smesa->front.offset);
243 MMIO(REG_DST_PITCH_HEIGHT, (smesa->virtualY << 16) | smesa->front.pitch);
244 MMIO(REG_WIDTH_HEIGHT, ((box->y2 - box->y1) << 16) | (box->x2 - box->x1));
245 MMIO(REG_BLIT_CMD, CMD_DIR_X_INC | CMD_DIR_Y_INC | CMD_ROP_SRC);
246 MMIO(REG_CommandQueue, -1);
247 }
248
249 *(GLint *)(smesa->IOBase+0x8a2c) = *smesa->FrameCountPtr;
250 (*smesa->FrameCountPtr)++;
251
252 UNLOCK_HARDWARE ();
253 }
254
255
256 /* Copy the back color buffer to the front color buffer */
257 static void
258 sisSwapBuffers(__DRIdrawablePrivate *dPriv)
259 {
260 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
261 sisContextPtr smesa = (sisContextPtr) dPriv->driContextPriv->driverPrivate;
262 GLcontext *ctx = smesa->glCtx;
263
264 if (ctx->Visual.doubleBufferMode) {
265 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
266 sisCopyBuffer( dPriv );
267 }
268 } else {
269 /* XXX this shouldn't be an error but we can't handle it for now */
270 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
271 }
272 }
273
274
275 /**
276 * This is the driver specific part of the createNewScreen entry point.
277 *
278 * \todo maybe fold this into intelInitDriver
279 *
280 * \return the __GLcontextModes supported by this driver
281 */
282 static const __DRIconfig **
283 sisInitScreen(__DRIscreenPrivate *psp)
284 {
285 static const __DRIversion ddx_expected = {0, 8, 0};
286 static const __DRIversion dri_expected = {4, 0, 0};
287 static const __DRIversion drm_expected = {1, 0, 0};
288 static const char *driver_name = "SiS";
289 SISDRIPtr dri_priv = (SISDRIPtr)psp->pDevPriv;
290
291 if (!driCheckDriDdxDrmVersions2(driver_name,
292 &psp->dri_version, &dri_expected,
293 &psp->ddx_version, &ddx_expected,
294 &psp->drm_version, &drm_expected))
295 return NULL;
296
297 /* Calling driInitExtensions here, with a NULL context pointer,
298 * does not actually enable the extensions. It just makes sure
299 * that all the dispatch offsets for all the extensions that
300 * *might* be enables are known. This is needed because the
301 * dispatch offsets need to be known when _mesa_context_create is
302 * called, but we can't enable the extensions until we have a
303 * context pointer.
304 *
305 * Hello chicken. Hello egg. How are you two today?
306 */
307 driInitExtensions( NULL, card_extensions, GL_FALSE );
308
309 psp->private = sisCreateScreen(psp);
310
311 if (!psp->private) {
312 sisDestroyScreen(psp);
313 return NULL;
314 }
315
316 return sisFillInModes(psp, dri_priv->bytesPerPixel * 8);
317 }
318
319 const struct __DriverAPIRec driDriverAPI = {
320 .InitScreen = sisInitScreen,
321 .DestroyScreen = sisDestroyScreen,
322 .CreateContext = sisCreateContext,
323 .DestroyContext = sisDestroyContext,
324 .CreateBuffer = sisCreateBuffer,
325 .DestroyBuffer = sisDestroyBuffer,
326 .SwapBuffers = sisSwapBuffers,
327 .MakeCurrent = sisMakeCurrent,
328 .UnbindContext = sisUnbindContext,
329 .GetSwapInfo = NULL,
330 .GetDrawableMSC = NULL,
331 .WaitForMSC = NULL,
332 .WaitForSBC = NULL,
333 .SwapBuffersMSC = NULL
334
335 };