Merge remote branch 'nouveau/gallium-0.1' into nouveau-gallium-0.2
[mesa.git] / src / mesa / drivers / dri / i810 / i810screen.c
1 /**************************************************************************
2
3 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
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
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sub license, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial portions
16 of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 *
32 */
33
34
35 #include "glheader.h"
36 #include "imports.h"
37 #include "context.h"
38 #include "framebuffer.h"
39 #include "fbobject.h"
40 #include "matrix.h"
41 #include "renderbuffer.h"
42 #include "simple_list.h"
43 #include "utils.h"
44
45 #include "i810screen.h"
46 #include "i810_dri.h"
47
48 #include "i810state.h"
49 #include "i810tex.h"
50 #include "i810span.h"
51 #include "i810tris.h"
52 #include "i810ioctl.h"
53
54 #include "GL/internal/dri_interface.h"
55
56 extern const struct dri_extension card_extensions[];
57
58 static const __DRIconfig **
59 i810FillInModes( __DRIscreenPrivate *psp,
60 unsigned pixel_bits, unsigned depth_bits,
61 unsigned stencil_bits, GLboolean have_back_buffer )
62 {
63 __DRIconfig **configs;
64 __GLcontextModes * m;
65 unsigned depth_buffer_factor;
66 unsigned back_buffer_factor;
67 unsigned i;
68
69 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
70 * enough to add support. Basically, if a context is created with an
71 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
72 * will never be used.
73 */
74 static const GLenum back_buffer_modes[] = {
75 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
76 };
77
78 u_int8_t depth_bits_array[2];
79 u_int8_t stencil_bits_array[2];
80
81 depth_bits_array[0] = depth_bits;
82 depth_bits_array[1] = depth_bits;
83
84 /* Just like with the accumulation buffer, always provide some modes
85 * with a stencil buffer. It will be a sw fallback, but some apps won't
86 * care about that.
87 */
88 stencil_bits_array[0] = 0;
89 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
90
91 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
92 back_buffer_factor = (have_back_buffer) ? 2 : 1;
93
94 configs = driCreateConfigs(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
95 depth_bits_array, stencil_bits_array,
96 depth_buffer_factor,
97 back_buffer_modes, back_buffer_factor);
98 if (configs == NULL) {
99 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
100 __func__, __LINE__ );
101 return NULL;
102 }
103
104 /* Mark the visual as slow if there are "fake" stencil bits.
105 */
106 for (i = 0; configs[i]; i++) {
107 m = &configs[i]->modes;
108 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
109 m->visualRating = GLX_SLOW_CONFIG;
110 }
111 }
112
113 return (const __DRIconfig **) configs;
114 }
115
116
117 /* static int i810_malloc_proxy_buf(drmBufMapPtr buffers) */
118 /* { */
119 /* char *buffer; */
120 /* drmBufPtr buf; */
121 /* int i; */
122
123 /* buffer = CALLOC(I810_DMA_BUF_SZ); */
124 /* if(buffer == NULL) return -1; */
125 /* for(i = 0; i < I810_DMA_BUF_NR; i++) { */
126 /* buf = &(buffers->list[i]); */
127 /* buf->address = (drmAddress)buffer; */
128 /* } */
129 /* return 0; */
130 /* } */
131
132 static drmBufMapPtr i810_create_empty_buffers(void)
133 {
134 drmBufMapPtr retval;
135
136 retval = (drmBufMapPtr)ALIGN_MALLOC(sizeof(drmBufMap), 32);
137 if(retval == NULL) return NULL;
138 memset(retval, 0, sizeof(drmBufMap));
139 retval->list = (drmBufPtr)ALIGN_MALLOC(sizeof(drmBuf) * I810_DMA_BUF_NR, 32);
140 if(retval->list == NULL) {
141 ALIGN_FREE(retval);
142 return NULL;
143 }
144 memset(retval->list, 0, sizeof(drmBuf) * I810_DMA_BUF_NR);
145 return retval;
146 }
147
148
149 static const __DRIconfig **
150 i810InitScreen(__DRIscreen *sPriv)
151 {
152 static const __DRIversion ddx_expected = { 1, 0, 0 };
153 static const __DRIversion dri_expected = { 4, 0, 0 };
154 static const __DRIversion drm_expected = { 1, 2, 0 };
155 i810ScreenPrivate *i810Screen;
156 I810DRIPtr gDRIPriv = (I810DRIPtr)sPriv->pDevPriv;
157
158 if ( ! driCheckDriDdxDrmVersions2( "i810",
159 &sPriv->dri_version, & dri_expected,
160 &sPriv->ddx_version, & ddx_expected,
161 &sPriv->drm_version, & drm_expected ) ) {
162 return NULL;
163 }
164
165 driInitExtensions( NULL, card_extensions, GL_TRUE );
166
167 if (sPriv->devPrivSize != sizeof(I810DRIRec)) {
168 fprintf(stderr,"\nERROR! sizeof(I810DRIRec) does not match passed size from device driver\n");
169 return GL_FALSE;
170 }
171
172 /* Allocate the private area */
173 i810Screen = (i810ScreenPrivate *)CALLOC(sizeof(i810ScreenPrivate));
174 if (!i810Screen) {
175 __driUtilMessage("i810InitDriver: alloc i810ScreenPrivate struct failed");
176 return GL_FALSE;
177 }
178
179 i810Screen->driScrnPriv = sPriv;
180 sPriv->private = (void *)i810Screen;
181
182 i810Screen->deviceID=gDRIPriv->deviceID;
183 i810Screen->width=gDRIPriv->width;
184 i810Screen->height=gDRIPriv->height;
185 i810Screen->mem=gDRIPriv->mem;
186 i810Screen->cpp=gDRIPriv->cpp;
187 i810Screen->fbStride=gDRIPriv->fbStride;
188 i810Screen->fbOffset=gDRIPriv->fbOffset;
189
190 if (gDRIPriv->bitsPerPixel == 15)
191 i810Screen->fbFormat = DV_PF_555;
192 else
193 i810Screen->fbFormat = DV_PF_565;
194
195 i810Screen->backOffset=gDRIPriv->backOffset;
196 i810Screen->depthOffset=gDRIPriv->depthOffset;
197 i810Screen->backPitch = gDRIPriv->auxPitch;
198 i810Screen->backPitchBits = gDRIPriv->auxPitchBits;
199 i810Screen->textureOffset=gDRIPriv->textureOffset;
200 i810Screen->textureSize=gDRIPriv->textureSize;
201 i810Screen->logTextureGranularity = gDRIPriv->logTextureGranularity;
202
203 i810Screen->bufs = i810_create_empty_buffers();
204 if (i810Screen->bufs == NULL) {
205 __driUtilMessage("i810InitDriver: i810_create_empty_buffers() failed");
206 FREE(i810Screen);
207 return GL_FALSE;
208 }
209
210 i810Screen->back.handle = gDRIPriv->backbuffer;
211 i810Screen->back.size = gDRIPriv->backbufferSize;
212
213 if (drmMap(sPriv->fd,
214 i810Screen->back.handle,
215 i810Screen->back.size,
216 (drmAddress *)&i810Screen->back.map) != 0) {
217 FREE(i810Screen);
218 sPriv->private = NULL;
219 __driUtilMessage("i810InitDriver: drmMap failed");
220 return GL_FALSE;
221 }
222
223 i810Screen->depth.handle = gDRIPriv->depthbuffer;
224 i810Screen->depth.size = gDRIPriv->depthbufferSize;
225
226 if (drmMap(sPriv->fd,
227 i810Screen->depth.handle,
228 i810Screen->depth.size,
229 (drmAddress *)&i810Screen->depth.map) != 0) {
230 drmUnmap(i810Screen->back.map, i810Screen->back.size);
231 FREE(i810Screen);
232 sPriv->private = NULL;
233 __driUtilMessage("i810InitDriver: drmMap (2) failed");
234 return GL_FALSE;
235 }
236
237 i810Screen->tex.handle = gDRIPriv->textures;
238 i810Screen->tex.size = gDRIPriv->textureSize;
239
240 if (drmMap(sPriv->fd,
241 i810Screen->tex.handle,
242 i810Screen->tex.size,
243 (drmAddress *)&i810Screen->tex.map) != 0) {
244 drmUnmap(i810Screen->back.map, i810Screen->back.size);
245 drmUnmap(i810Screen->depth.map, i810Screen->depth.size);
246 FREE(i810Screen);
247 sPriv->private = NULL;
248 __driUtilMessage("i810InitDriver: drmMap (3) failed");
249 return GL_FALSE;
250 }
251
252 i810Screen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
253
254 return i810FillInModes(sPriv, 16, 16, 0, 1);
255 }
256
257 static void
258 i810DestroyScreen(__DRIscreenPrivate *sPriv)
259 {
260 i810ScreenPrivate *i810Screen = (i810ScreenPrivate *)sPriv->private;
261
262 /* Need to unmap all the bufs and maps here:
263 */
264 drmUnmap(i810Screen->back.map, i810Screen->back.size);
265 drmUnmap(i810Screen->depth.map, i810Screen->depth.size);
266 drmUnmap(i810Screen->tex.map, i810Screen->tex.size);
267
268 FREE(i810Screen);
269 sPriv->private = NULL;
270 }
271
272
273 /**
274 * Create a buffer which corresponds to the window.
275 */
276 static GLboolean
277 i810CreateBuffer( __DRIscreenPrivate *driScrnPriv,
278 __DRIdrawablePrivate *driDrawPriv,
279 const __GLcontextModes *mesaVis,
280 GLboolean isPixmap )
281 {
282 i810ScreenPrivate *screen = (i810ScreenPrivate *) driScrnPriv->private;
283
284 if (isPixmap) {
285 return GL_FALSE; /* not implemented */
286 }
287 else {
288 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
289
290 {
291 driRenderbuffer *frontRb
292 = driNewRenderbuffer(GL_RGBA,
293 driScrnPriv->pFB,
294 screen->cpp,
295 /*screen->frontOffset*/0, screen->backPitch,
296 driDrawPriv);
297 i810SetSpanFunctions(frontRb, mesaVis);
298 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
299 }
300
301 if (mesaVis->doubleBufferMode) {
302 driRenderbuffer *backRb
303 = driNewRenderbuffer(GL_RGBA,
304 screen->back.map,
305 screen->cpp,
306 screen->backOffset, screen->backPitch,
307 driDrawPriv);
308 i810SetSpanFunctions(backRb, mesaVis);
309 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
310 }
311
312 if (mesaVis->depthBits == 16) {
313 driRenderbuffer *depthRb
314 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
315 screen->depth.map,
316 screen->cpp,
317 screen->depthOffset, screen->backPitch,
318 driDrawPriv);
319 i810SetSpanFunctions(depthRb, mesaVis);
320 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
321 }
322
323 _mesa_add_soft_renderbuffers(fb,
324 GL_FALSE, /* color */
325 GL_FALSE, /* depth */
326 mesaVis->stencilBits > 0,
327 mesaVis->accumRedBits > 0,
328 GL_FALSE, /* alpha */
329 GL_FALSE /* aux */);
330 driDrawPriv->driverPrivate = (void *) fb;
331
332 return (driDrawPriv->driverPrivate != NULL);
333 }
334 }
335
336
337 static void
338 i810DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
339 {
340 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
341 }
342
343 const struct __DriverAPIRec driDriverAPI = {
344 .InitScreen = i810InitScreen,
345 .DestroyScreen = i810DestroyScreen,
346 .CreateContext = i810CreateContext,
347 .DestroyContext = i810DestroyContext,
348 .CreateBuffer = i810CreateBuffer,
349 .DestroyBuffer = i810DestroyBuffer,
350 .SwapBuffers = i810SwapBuffers,
351 .MakeCurrent = i810MakeCurrent,
352 .UnbindContext = i810UnbindContext,
353 .GetSwapInfo = NULL,
354 .GetDrawableMSC = NULL,
355 .WaitForMSC = NULL,
356 .WaitForSBC = NULL,
357 .SwapBuffersMSC = NULL
358 };