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