radeon/r200/r300: cleanup some of the renderbuffer code
[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 "main/glheader.h"
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/framebuffer.h"
39 #include "main/fbobject.h"
40 #include "main/matrix.h"
41 #include "main/renderbuffer.h"
42 #include "main/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 uint8_t depth_bits_array[2];
79 uint8_t stencil_bits_array[2];
80 uint8_t msaa_samples_array[1];
81
82 depth_bits_array[0] = depth_bits;
83 depth_bits_array[1] = depth_bits;
84
85 /* Just like with the accumulation buffer, always provide some modes
86 * with a stencil buffer. It will be a sw fallback, but some apps won't
87 * care about that.
88 */
89 stencil_bits_array[0] = 0;
90 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
91
92 msaa_samples_array[0] = 0;
93
94 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
95 back_buffer_factor = (have_back_buffer) ? 2 : 1;
96
97 configs = driCreateConfigs(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
98 depth_bits_array, stencil_bits_array,
99 depth_buffer_factor,
100 back_buffer_modes, back_buffer_factor,
101 msaa_samples_array, 1);
102 if (configs == NULL) {
103 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
104 __func__, __LINE__ );
105 return NULL;
106 }
107
108 /* Mark the visual as slow if there are "fake" stencil bits.
109 */
110 for (i = 0; configs[i]; i++) {
111 m = &configs[i]->modes;
112 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
113 m->visualRating = GLX_SLOW_CONFIG;
114 }
115 }
116
117 return (const __DRIconfig **) configs;
118 }
119
120
121 /* static int i810_malloc_proxy_buf(drmBufMapPtr buffers) */
122 /* { */
123 /* char *buffer; */
124 /* drmBufPtr buf; */
125 /* int i; */
126
127 /* buffer = CALLOC(I810_DMA_BUF_SZ); */
128 /* if(buffer == NULL) return -1; */
129 /* for(i = 0; i < I810_DMA_BUF_NR; i++) { */
130 /* buf = &(buffers->list[i]); */
131 /* buf->address = (drmAddress)buffer; */
132 /* } */
133 /* return 0; */
134 /* } */
135
136 static drmBufMapPtr i810_create_empty_buffers(void)
137 {
138 drmBufMapPtr retval;
139
140 retval = (drmBufMapPtr)ALIGN_MALLOC(sizeof(drmBufMap), 32);
141 if(retval == NULL) return NULL;
142 memset(retval, 0, sizeof(drmBufMap));
143 retval->list = (drmBufPtr)ALIGN_MALLOC(sizeof(drmBuf) * I810_DMA_BUF_NR, 32);
144 if(retval->list == NULL) {
145 ALIGN_FREE(retval);
146 return NULL;
147 }
148 memset(retval->list, 0, sizeof(drmBuf) * I810_DMA_BUF_NR);
149 return retval;
150 }
151
152
153 static const __DRIconfig **
154 i810InitScreen(__DRIscreen *sPriv)
155 {
156 static const __DRIversion ddx_expected = { 1, 0, 0 };
157 static const __DRIversion dri_expected = { 4, 0, 0 };
158 static const __DRIversion drm_expected = { 1, 2, 0 };
159 i810ScreenPrivate *i810Screen;
160 I810DRIPtr gDRIPriv = (I810DRIPtr)sPriv->pDevPriv;
161
162 if ( ! driCheckDriDdxDrmVersions2( "i810",
163 &sPriv->dri_version, & dri_expected,
164 &sPriv->ddx_version, & ddx_expected,
165 &sPriv->drm_version, & drm_expected ) ) {
166 return NULL;
167 }
168
169 driInitExtensions( NULL, card_extensions, GL_TRUE );
170
171 if (sPriv->devPrivSize != sizeof(I810DRIRec)) {
172 fprintf(stderr,"\nERROR! sizeof(I810DRIRec) does not match passed size from device driver\n");
173 return GL_FALSE;
174 }
175
176 /* Allocate the private area */
177 i810Screen = (i810ScreenPrivate *)CALLOC(sizeof(i810ScreenPrivate));
178 if (!i810Screen) {
179 __driUtilMessage("i810InitDriver: alloc i810ScreenPrivate struct failed");
180 return GL_FALSE;
181 }
182
183 i810Screen->driScrnPriv = sPriv;
184 sPriv->private = (void *)i810Screen;
185
186 i810Screen->deviceID=gDRIPriv->deviceID;
187 i810Screen->width=gDRIPriv->width;
188 i810Screen->height=gDRIPriv->height;
189 i810Screen->mem=gDRIPriv->mem;
190 i810Screen->cpp=gDRIPriv->cpp;
191 i810Screen->fbStride=gDRIPriv->fbStride;
192 i810Screen->fbOffset=gDRIPriv->fbOffset;
193
194 if (gDRIPriv->bitsPerPixel == 15)
195 i810Screen->fbFormat = DV_PF_555;
196 else
197 i810Screen->fbFormat = DV_PF_565;
198
199 i810Screen->backOffset=gDRIPriv->backOffset;
200 i810Screen->depthOffset=gDRIPriv->depthOffset;
201 i810Screen->backPitch = gDRIPriv->auxPitch;
202 i810Screen->backPitchBits = gDRIPriv->auxPitchBits;
203 i810Screen->textureOffset=gDRIPriv->textureOffset;
204 i810Screen->textureSize=gDRIPriv->textureSize;
205 i810Screen->logTextureGranularity = gDRIPriv->logTextureGranularity;
206
207 i810Screen->bufs = i810_create_empty_buffers();
208 if (i810Screen->bufs == NULL) {
209 __driUtilMessage("i810InitDriver: i810_create_empty_buffers() failed");
210 FREE(i810Screen);
211 return GL_FALSE;
212 }
213
214 i810Screen->back.handle = gDRIPriv->backbuffer;
215 i810Screen->back.size = gDRIPriv->backbufferSize;
216
217 if (drmMap(sPriv->fd,
218 i810Screen->back.handle,
219 i810Screen->back.size,
220 (drmAddress *)&i810Screen->back.map) != 0) {
221 FREE(i810Screen);
222 sPriv->private = NULL;
223 __driUtilMessage("i810InitDriver: drmMap failed");
224 return GL_FALSE;
225 }
226
227 i810Screen->depth.handle = gDRIPriv->depthbuffer;
228 i810Screen->depth.size = gDRIPriv->depthbufferSize;
229
230 if (drmMap(sPriv->fd,
231 i810Screen->depth.handle,
232 i810Screen->depth.size,
233 (drmAddress *)&i810Screen->depth.map) != 0) {
234 drmUnmap(i810Screen->back.map, i810Screen->back.size);
235 FREE(i810Screen);
236 sPriv->private = NULL;
237 __driUtilMessage("i810InitDriver: drmMap (2) failed");
238 return GL_FALSE;
239 }
240
241 i810Screen->tex.handle = gDRIPriv->textures;
242 i810Screen->tex.size = gDRIPriv->textureSize;
243
244 if (drmMap(sPriv->fd,
245 i810Screen->tex.handle,
246 i810Screen->tex.size,
247 (drmAddress *)&i810Screen->tex.map) != 0) {
248 drmUnmap(i810Screen->back.map, i810Screen->back.size);
249 drmUnmap(i810Screen->depth.map, i810Screen->depth.size);
250 FREE(i810Screen);
251 sPriv->private = NULL;
252 __driUtilMessage("i810InitDriver: drmMap (3) failed");
253 return GL_FALSE;
254 }
255
256 i810Screen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
257
258 return i810FillInModes(sPriv, 16, 16, 0, 1);
259 }
260
261 static void
262 i810DestroyScreen(__DRIscreenPrivate *sPriv)
263 {
264 i810ScreenPrivate *i810Screen = (i810ScreenPrivate *)sPriv->private;
265
266 /* Need to unmap all the bufs and maps here:
267 */
268 drmUnmap(i810Screen->back.map, i810Screen->back.size);
269 drmUnmap(i810Screen->depth.map, i810Screen->depth.size);
270 drmUnmap(i810Screen->tex.map, i810Screen->tex.size);
271
272 FREE(i810Screen);
273 sPriv->private = NULL;
274 }
275
276
277 /**
278 * Create a buffer which corresponds to the window.
279 */
280 static GLboolean
281 i810CreateBuffer( __DRIscreenPrivate *driScrnPriv,
282 __DRIdrawablePrivate *driDrawPriv,
283 const __GLcontextModes *mesaVis,
284 GLboolean isPixmap )
285 {
286 i810ScreenPrivate *screen = (i810ScreenPrivate *) driScrnPriv->private;
287
288 if (isPixmap) {
289 return GL_FALSE; /* not implemented */
290 }
291 else {
292 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
293
294 {
295 driRenderbuffer *frontRb
296 = driNewRenderbuffer(GL_RGBA,
297 driScrnPriv->pFB,
298 screen->cpp,
299 /*screen->frontOffset*/0, screen->backPitch,
300 driDrawPriv);
301 i810SetSpanFunctions(frontRb, mesaVis);
302 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
303 }
304
305 if (mesaVis->doubleBufferMode) {
306 driRenderbuffer *backRb
307 = driNewRenderbuffer(GL_RGBA,
308 screen->back.map,
309 screen->cpp,
310 screen->backOffset, screen->backPitch,
311 driDrawPriv);
312 i810SetSpanFunctions(backRb, mesaVis);
313 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
314 }
315
316 if (mesaVis->depthBits == 16) {
317 driRenderbuffer *depthRb
318 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
319 screen->depth.map,
320 screen->cpp,
321 screen->depthOffset, screen->backPitch,
322 driDrawPriv);
323 i810SetSpanFunctions(depthRb, mesaVis);
324 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
325 }
326
327 _mesa_add_soft_renderbuffers(fb,
328 GL_FALSE, /* color */
329 GL_FALSE, /* depth */
330 mesaVis->stencilBits > 0,
331 mesaVis->accumRedBits > 0,
332 GL_FALSE, /* alpha */
333 GL_FALSE /* aux */);
334 driDrawPriv->driverPrivate = (void *) fb;
335
336 return (driDrawPriv->driverPrivate != NULL);
337 }
338 }
339
340
341 static void
342 i810DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
343 {
344 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
345 }
346
347 const struct __DriverAPIRec driDriverAPI = {
348 .InitScreen = i810InitScreen,
349 .DestroyScreen = i810DestroyScreen,
350 .CreateContext = i810CreateContext,
351 .DestroyContext = i810DestroyContext,
352 .CreateBuffer = i810CreateBuffer,
353 .DestroyBuffer = i810DestroyBuffer,
354 .SwapBuffers = i810SwapBuffers,
355 .MakeCurrent = i810MakeCurrent,
356 .UnbindContext = i810UnbindContext,
357 .GetSwapInfo = NULL,
358 .GetDrawableMSC = NULL,
359 .WaitForMSC = NULL,
360 .WaitForSBC = NULL,
361 .SwapBuffersMSC = NULL
362 };