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