Merge branch 'origin' into glsl-compiler-1
[mesa.git] / src / mesa / drivers / dri / i915tex / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, 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 TUNGSTEN GRAPHICS 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 #include "glheader.h"
29 #include "context.h"
30 #include "framebuffer.h"
31 #include "matrix.h"
32 #include "renderbuffer.h"
33 #include "simple_list.h"
34 #include "utils.h"
35 #include "vblank.h"
36 #include "xmlpool.h"
37
38
39 #include "intel_screen.h"
40
41 #include "intel_buffers.h"
42 #include "intel_tex.h"
43 #include "intel_span.h"
44 #include "intel_tris.h"
45 #include "intel_ioctl.h"
46 #include "intel_fbo.h"
47
48 #include "i830_dri.h"
49 #include "dri_bufpool.h"
50 #include "intel_regions.h"
51 #include "intel_batchbuffer.h"
52
53 PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
57 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
58 DRI_CONF_FORCE_S3TC_ENABLE(false)
59 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
60 DRI_CONF_SECTION_END DRI_CONF_END;
61 const GLuint __driNConfigOptions = 4;
62
63 #ifdef USE_NEW_INTERFACE
64 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
65 #endif /*USE_NEW_INTERFACE */
66
67 extern const struct dri_extension card_extensions[];
68
69 /**
70 * Map all the memory regions described by the screen.
71 * \return GL_TRUE if success, GL_FALSE if error.
72 */
73 GLboolean
74 intelMapScreenRegions(__DRIscreenPrivate * sPriv)
75 {
76 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
77
78 if (intelScreen->front.handle) {
79 if (drmMap(sPriv->fd,
80 intelScreen->front.handle,
81 intelScreen->front.size,
82 (drmAddress *) & intelScreen->front.map) != 0) {
83 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
84 return GL_FALSE;
85 }
86 }
87 else {
88 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
89 }
90
91 if (0)
92 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
93 if (drmMap(sPriv->fd,
94 intelScreen->back.handle,
95 intelScreen->back.size,
96 (drmAddress *) & intelScreen->back.map) != 0) {
97 intelUnmapScreenRegions(intelScreen);
98 return GL_FALSE;
99 }
100
101 if (0)
102 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
103 if (drmMap(sPriv->fd,
104 intelScreen->depth.handle,
105 intelScreen->depth.size,
106 (drmAddress *) & intelScreen->depth.map) != 0) {
107 intelUnmapScreenRegions(intelScreen);
108 return GL_FALSE;
109 }
110
111 #if 0
112 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
113 if (drmMap(sPriv->fd,
114 intelScreen->tex.handle,
115 intelScreen->tex.size,
116 (drmAddress *) & intelScreen->tex.map) != 0) {
117 intelUnmapScreenRegions(intelScreen);
118 return GL_FALSE;
119 }
120 #endif
121 if (0)
122 printf("Mappings: front: %p back: %p depth: %p tex: %p\n",
123 intelScreen->front.map,
124 intelScreen->back.map,
125 intelScreen->depth.map, intelScreen->tex.map);
126 return GL_TRUE;
127 }
128
129
130 static struct intel_region *
131 intel_recreate_static(intelScreenPrivate *intelScreen,
132 struct intel_region *region,
133 GLuint mem_type,
134 GLuint offset,
135 void *virtual,
136 GLuint cpp, GLuint pitch, GLuint height)
137 {
138 if (region) {
139 intel_region_update_static(intelScreen, region, mem_type, offset,
140 virtual, cpp, pitch, height);
141 } else {
142 region = intel_region_create_static(intelScreen, mem_type, offset,
143 virtual, cpp, pitch, height);
144 }
145 return region;
146 }
147
148
149 /* Create intel_region structs to describe the static front,back,depth
150 * buffers created by the xserver.
151 *
152 * Although FBO's mean we now no longer use these as render targets in
153 * all circumstances, they won't go away until the back and depth
154 * buffers become private, and the front and rotated buffers will
155 * remain even then.
156 *
157 * Note that these don't allocate video memory, just describe
158 * allocations alread made by the X server.
159 */
160 static void
161 intel_recreate_static_regions(intelScreenPrivate *intelScreen)
162 {
163 intelScreen->front_region =
164 intel_recreate_static(intelScreen,
165 intelScreen->front_region,
166 DRM_BO_FLAG_MEM_TT,
167 intelScreen->front.offset,
168 intelScreen->front.map,
169 intelScreen->cpp,
170 intelScreen->front.pitch / intelScreen->cpp,
171 intelScreen->height);
172
173 intelScreen->rotated_region =
174 intel_recreate_static(intelScreen,
175 intelScreen->rotated_region,
176 DRM_BO_FLAG_MEM_TT,
177 intelScreen->rotated.offset,
178 intelScreen->rotated.map,
179 intelScreen->cpp,
180 intelScreen->rotated.pitch /
181 intelScreen->cpp, intelScreen->height);
182
183
184 intelScreen->back_region =
185 intel_recreate_static(intelScreen,
186 intelScreen->back_region,
187 DRM_BO_FLAG_MEM_TT,
188 intelScreen->back.offset,
189 intelScreen->back.map,
190 intelScreen->cpp,
191 intelScreen->back.pitch / intelScreen->cpp,
192 intelScreen->height);
193
194 /* Still assuming front.cpp == depth.cpp
195 */
196 intelScreen->depth_region =
197 intel_recreate_static(intelScreen,
198 intelScreen->depth_region,
199 DRM_BO_FLAG_MEM_TT,
200 intelScreen->depth.offset,
201 intelScreen->depth.map,
202 intelScreen->cpp,
203 intelScreen->depth.pitch / intelScreen->cpp,
204 intelScreen->height);
205 }
206
207 /**
208 * Use the information in the sarea to update the screen parameters
209 * related to screen rotation. Needs to be called locked.
210 */
211 void
212 intelUpdateScreenRotation(__DRIscreenPrivate * sPriv, drmI830Sarea * sarea)
213 {
214 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
215
216 intelUnmapScreenRegions(intelScreen);
217 intelUpdateScreenFromSAREA(intelScreen, sarea);
218 if (!intelMapScreenRegions(sPriv)) {
219 fprintf(stderr, "ERROR Remapping screen regions!!!\n");
220 }
221 intel_recreate_static_regions(intelScreen);
222 }
223
224
225 void
226 intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
227 {
228 #define REALLY_UNMAP 1
229 if (intelScreen->front.map) {
230 #if REALLY_UNMAP
231 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
232 printf("drmUnmap front failed!\n");
233 #endif
234 intelScreen->front.map = NULL;
235 }
236 if (intelScreen->back.map) {
237 #if REALLY_UNMAP
238 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
239 printf("drmUnmap back failed!\n");
240 #endif
241 intelScreen->back.map = NULL;
242 }
243 if (intelScreen->depth.map) {
244 #if REALLY_UNMAP
245 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
246 intelScreen->depth.map = NULL;
247 #endif
248 }
249 if (intelScreen->tex.map) {
250 #if REALLY_UNMAP
251 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
252 intelScreen->tex.map = NULL;
253 #endif
254 }
255 }
256
257
258 static void
259 intelPrintDRIInfo(intelScreenPrivate * intelScreen,
260 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
261 {
262 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
263 intelScreen->front.size, intelScreen->front.offset,
264 intelScreen->front.pitch);
265 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
266 intelScreen->back.size, intelScreen->back.offset,
267 intelScreen->back.pitch);
268 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
269 intelScreen->depth.size, intelScreen->depth.offset,
270 intelScreen->depth.pitch);
271 fprintf(stderr, "*** Rotated size: 0x%x offset: 0x%x pitch: %d\n",
272 intelScreen->rotated.size, intelScreen->rotated.offset,
273 intelScreen->rotated.pitch);
274 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
275 intelScreen->tex.size, intelScreen->tex.offset);
276 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
277 }
278
279
280 static void
281 intelPrintSAREA(const drmI830Sarea * sarea)
282 {
283 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
284 sarea->height);
285 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
286 fprintf(stderr,
287 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
288 sarea->front_offset, sarea->front_size,
289 (unsigned) sarea->front_handle);
290 fprintf(stderr,
291 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
292 sarea->back_offset, sarea->back_size,
293 (unsigned) sarea->back_handle);
294 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
295 sarea->depth_offset, sarea->depth_size,
296 (unsigned) sarea->depth_handle);
297 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
298 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
299 fprintf(stderr, "SAREA: rotation: %d\n", sarea->rotation);
300 fprintf(stderr,
301 "SAREA: rotated offset: 0x%08x size: 0x%x\n",
302 sarea->rotated_offset, sarea->rotated_size);
303 fprintf(stderr, "SAREA: rotated pitch: %d\n", sarea->rotated_pitch);
304 }
305
306
307 /**
308 * A number of the screen parameters are obtained/computed from
309 * information in the SAREA. This function updates those parameters.
310 */
311 void
312 intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
313 drmI830Sarea * sarea)
314 {
315 intelScreen->width = sarea->width;
316 intelScreen->height = sarea->height;
317
318 intelScreen->front.offset = sarea->front_offset;
319 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
320 intelScreen->front.handle = sarea->front_handle;
321 intelScreen->front.size = sarea->front_size;
322
323 intelScreen->back.offset = sarea->back_offset;
324 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
325 intelScreen->back.handle = sarea->back_handle;
326 intelScreen->back.size = sarea->back_size;
327
328 intelScreen->depth.offset = sarea->depth_offset;
329 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
330 intelScreen->depth.handle = sarea->depth_handle;
331 intelScreen->depth.size = sarea->depth_size;
332
333 intelScreen->tex.offset = sarea->tex_offset;
334 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
335 intelScreen->tex.handle = sarea->tex_handle;
336 intelScreen->tex.size = sarea->tex_size;
337
338 intelScreen->rotated.offset = sarea->rotated_offset;
339 intelScreen->rotated.pitch = sarea->rotated_pitch * intelScreen->cpp;
340 intelScreen->rotated.size = sarea->rotated_size;
341 intelScreen->current_rotation = sarea->rotation;
342 matrix23Rotate(&intelScreen->rotMatrix,
343 sarea->width, sarea->height, sarea->rotation);
344 intelScreen->rotatedWidth = sarea->virtualX;
345 intelScreen->rotatedHeight = sarea->virtualY;
346
347 if (0)
348 intelPrintSAREA(sarea);
349 }
350
351
352 static GLboolean
353 intelInitDriver(__DRIscreenPrivate * sPriv)
354 {
355 intelScreenPrivate *intelScreen;
356 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
357 drmI830Sarea *sarea;
358 unsigned batchPoolSize = 1024*1024;
359
360 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
361 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->
362 getProcAddress("glxEnableExtension"));
363 void *const psc = sPriv->psc->screenConfigs;
364
365 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
366 fprintf(stderr,
367 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
368 return GL_FALSE;
369 }
370
371 /* Allocate the private area */
372 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
373 if (!intelScreen) {
374 fprintf(stderr, "\nERROR! Allocating private area failed\n");
375 return GL_FALSE;
376 }
377 /* parse information in __driConfigOptions */
378 driParseOptionInfo(&intelScreen->optionCache,
379 __driConfigOptions, __driNConfigOptions);
380
381 intelScreen->driScrnPriv = sPriv;
382 sPriv->private = (void *) intelScreen;
383 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
384 sarea = (drmI830Sarea *)
385 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
386
387 intelScreen->maxBatchSize = BATCH_SZ;
388 intelScreen->deviceID = gDRIPriv->deviceID;
389 if (intelScreen->deviceID == PCI_CHIP_I865_G)
390 intelScreen->maxBatchSize = 4096;
391 batchPoolSize /= intelScreen->maxBatchSize;
392
393 intelScreen->mem = gDRIPriv->mem;
394 intelScreen->cpp = gDRIPriv->cpp;
395
396 switch (gDRIPriv->bitsPerPixel) {
397 case 16:
398 intelScreen->fbFormat = DV_PF_565;
399 break;
400 case 32:
401 intelScreen->fbFormat = DV_PF_8888;
402 break;
403 default:
404 exit(1);
405 break;
406 }
407
408 intelUpdateScreenFromSAREA(intelScreen, sarea);
409
410 if (!intelMapScreenRegions(sPriv)) {
411 fprintf(stderr, "\nERROR! mapping regions\n");
412 _mesa_free(intelScreen);
413 sPriv->private = NULL;
414 return GL_FALSE;
415 }
416
417 #if 0
418
419 /*
420 * FIXME: Remove this code and its references.
421 */
422
423 intelScreen->tex.offset = gDRIPriv->textureOffset;
424 intelScreen->logTextureGranularity = gDRIPriv->logTextureGranularity;
425 intelScreen->tex.handle = gDRIPriv->textures;
426 intelScreen->tex.size = gDRIPriv->textureSize;
427
428 #else
429 intelScreen->tex.offset = 0;
430 intelScreen->logTextureGranularity = 0;
431 intelScreen->tex.handle = 0;
432 intelScreen->tex.size = 0;
433 #endif
434
435 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
436
437 if (0)
438 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
439
440 intelScreen->drmMinor = sPriv->drmMinor;
441
442 /* Determine if IRQs are active? */
443 {
444 int ret;
445 drmI830GetParam gp;
446
447 gp.param = I830_PARAM_IRQ_ACTIVE;
448 gp.value = &intelScreen->irq_active;
449
450 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
451 &gp, sizeof(gp));
452 if (ret) {
453 fprintf(stderr, "drmI830GetParam: %d\n", ret);
454 return GL_FALSE;
455 }
456 }
457
458 /* Determine if batchbuffers are allowed */
459 {
460 int ret;
461 drmI830GetParam gp;
462
463 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
464 gp.value = &intelScreen->allow_batchbuffer;
465
466 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
467 &gp, sizeof(gp));
468 if (ret) {
469 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
470 return GL_FALSE;
471 }
472 }
473
474 if (glx_enable_extension != NULL) {
475 (*glx_enable_extension) (psc, "GLX_SGI_swap_control");
476 (*glx_enable_extension) (psc, "GLX_SGI_video_sync");
477 (*glx_enable_extension) (psc, "GLX_MESA_swap_control");
478 (*glx_enable_extension) (psc, "GLX_MESA_swap_frame_usage");
479 (*glx_enable_extension) (psc, "GLX_SGI_make_current_read");
480 }
481
482 intelScreen->regionPool = driDRMPoolInit(sPriv->fd);
483
484 if (!intelScreen->regionPool)
485 return GL_FALSE;
486
487 intelScreen->staticPool = driDRMStaticPoolInit(sPriv->fd);
488
489 if (!intelScreen->staticPool)
490 return GL_FALSE;
491
492 intelScreen->texPool = intelScreen->regionPool;
493
494 intelScreen->batchPool = driBatchPoolInit(sPriv->fd,
495 DRM_BO_FLAG_EXE |
496 DRM_BO_FLAG_MEM_TT |
497 DRM_BO_FLAG_MEM_LOCAL,
498 intelScreen->maxBatchSize,
499 batchPoolSize, 5);
500 if (!intelScreen->batchPool) {
501 fprintf(stderr, "Failed to initialize batch pool - possible incorrect agpgart installed\n");
502 return GL_FALSE;
503 }
504
505 intel_recreate_static_regions(intelScreen);
506
507 return GL_TRUE;
508 }
509
510
511 static void
512 intelDestroyScreen(__DRIscreenPrivate * sPriv)
513 {
514 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
515
516 intelUnmapScreenRegions(intelScreen);
517
518 driPoolTakeDown(intelScreen->regionPool);
519 driPoolTakeDown(intelScreen->staticPool);
520 driPoolTakeDown(intelScreen->batchPool);
521 FREE(intelScreen);
522 sPriv->private = NULL;
523 }
524
525
526 /**
527 * This is called when we need to set up GL rendering to a new X window.
528 */
529 static GLboolean
530 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
531 __DRIdrawablePrivate * driDrawPriv,
532 const __GLcontextModes * mesaVis, GLboolean isPixmap)
533 {
534 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
535
536 if (isPixmap) {
537 return GL_FALSE; /* not implemented */
538 }
539 else {
540 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
541 mesaVis->depthBits != 24);
542 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
543
544 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
545
546 /* setup the hardware-based renderbuffers */
547 {
548 struct intel_renderbuffer *frontRb
549 = intel_create_renderbuffer(rgbFormat,
550 screen->width, screen->height,
551 screen->front.offset,
552 screen->front.pitch,
553 screen->cpp,
554 screen->front.map);
555 intel_set_span_functions(&frontRb->Base);
556 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
557 }
558
559 if (mesaVis->doubleBufferMode) {
560 struct intel_renderbuffer *backRb
561 = intel_create_renderbuffer(rgbFormat,
562 screen->width, screen->height,
563 screen->back.offset,
564 screen->back.pitch,
565 screen->cpp,
566 screen->back.map);
567 intel_set_span_functions(&backRb->Base);
568 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
569 }
570
571 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
572 /* combined depth/stencil buffer */
573 struct intel_renderbuffer *depthStencilRb
574 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
575 screen->width, screen->height,
576 screen->depth.offset,
577 screen->depth.pitch,
578 screen->cpp, /* 4! */
579 screen->depth.map);
580 intel_set_span_functions(&depthStencilRb->Base);
581 /* note: bind RB to two attachment points */
582 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
583 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
584 }
585 else if (mesaVis->depthBits == 16) {
586 /* just 16-bit depth buffer, no hw stencil */
587 struct intel_renderbuffer *depthRb
588 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
589 screen->width, screen->height,
590 screen->depth.offset,
591 screen->depth.pitch,
592 screen->cpp, /* 2! */
593 screen->depth.map);
594 intel_set_span_functions(&depthRb->Base);
595 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
596 }
597
598 /* now add any/all software-based renderbuffers we may need */
599 _mesa_add_soft_renderbuffers(fb, GL_FALSE, /* never sw color */
600 GL_FALSE, /* never sw depth */
601 swStencil, mesaVis->accumRedBits > 0, GL_FALSE, /* never sw alpha */
602 GL_FALSE /* never sw aux */ );
603 driDrawPriv->driverPrivate = (void *) fb;
604
605 return (driDrawPriv->driverPrivate != NULL);
606 }
607 }
608
609 static void
610 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
611 {
612 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
613 }
614
615
616 /**
617 * Get information about previous buffer swaps.
618 */
619 static int
620 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
621 {
622 struct intel_context *intel;
623
624 if ((dPriv == NULL) || (dPriv->driContextPriv == NULL)
625 || (dPriv->driContextPriv->driverPrivate == NULL)
626 || (sInfo == NULL)) {
627 return -1;
628 }
629
630 intel = dPriv->driContextPriv->driverPrivate;
631 sInfo->swap_count = intel->swap_count;
632 sInfo->swap_ust = intel->swap_ust;
633 sInfo->swap_missed_count = intel->swap_missed_count;
634
635 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
636 ? driCalculateSwapUsage(dPriv, 0, intel->swap_missed_ust)
637 : 0.0;
638
639 return 0;
640 }
641
642
643 /* There are probably better ways to do this, such as an
644 * init-designated function to register chipids and createcontext
645 * functions.
646 */
647 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
648 __DRIcontextPrivate * driContextPriv,
649 void *sharedContextPrivate);
650
651 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
652 __DRIcontextPrivate * driContextPriv,
653 void *sharedContextPrivate);
654
655
656
657
658 static GLboolean
659 intelCreateContext(const __GLcontextModes * mesaVis,
660 __DRIcontextPrivate * driContextPriv,
661 void *sharedContextPrivate)
662 {
663 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
664 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
665
666 switch (intelScreen->deviceID) {
667 /* Don't deal with i830 until texture work complete:
668 */
669 case PCI_CHIP_845_G:
670 case PCI_CHIP_I830_M:
671 case PCI_CHIP_I855_GM:
672 case PCI_CHIP_I865_G:
673 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
674
675 case PCI_CHIP_I915_G:
676 case PCI_CHIP_I915_GM:
677 case PCI_CHIP_I945_G:
678 case PCI_CHIP_I945_GM:
679 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
680
681 default:
682 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
683 return GL_FALSE;
684 }
685 }
686
687
688 static const struct __DriverAPIRec intelAPI = {
689 .InitDriver = intelInitDriver,
690 .DestroyScreen = intelDestroyScreen,
691 .CreateContext = intelCreateContext,
692 .DestroyContext = intelDestroyContext,
693 .CreateBuffer = intelCreateBuffer,
694 .DestroyBuffer = intelDestroyBuffer,
695 .SwapBuffers = intelSwapBuffers,
696 .MakeCurrent = intelMakeCurrent,
697 .UnbindContext = intelUnbindContext,
698 .GetSwapInfo = intelGetSwapInfo,
699 .GetMSC = driGetMSC32,
700 .WaitForMSC = driWaitForMSC32,
701 .WaitForSBC = NULL,
702 .SwapBuffersMSC = NULL,
703 .CopySubBuffer = intelCopySubBuffer
704 };
705
706
707 static __GLcontextModes *
708 intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
709 unsigned stencil_bits, GLboolean have_back_buffer)
710 {
711 __GLcontextModes *modes;
712 __GLcontextModes *m;
713 unsigned num_modes;
714 unsigned depth_buffer_factor;
715 unsigned back_buffer_factor;
716 GLenum fb_format;
717 GLenum fb_type;
718
719 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
720 * support pageflipping at all.
721 */
722 static const GLenum back_buffer_modes[] = {
723 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
724 };
725
726 u_int8_t depth_bits_array[3];
727 u_int8_t stencil_bits_array[3];
728
729
730 depth_bits_array[0] = 0;
731 depth_bits_array[1] = depth_bits;
732 depth_bits_array[2] = depth_bits;
733
734 /* Just like with the accumulation buffer, always provide some modes
735 * with a stencil buffer. It will be a sw fallback, but some apps won't
736 * care about that.
737 */
738 stencil_bits_array[0] = 0;
739 stencil_bits_array[1] = 0;
740 if (depth_bits == 24)
741 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
742
743 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
744
745 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
746 back_buffer_factor = (have_back_buffer) ? 3 : 1;
747
748 num_modes = depth_buffer_factor * back_buffer_factor * 4;
749
750 if (pixel_bits == 16) {
751 fb_format = GL_RGB;
752 fb_type = GL_UNSIGNED_SHORT_5_6_5;
753 }
754 else {
755 fb_format = GL_BGRA;
756 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
757 }
758
759 modes =
760 (*dri_interface->createContextModes) (num_modes,
761 sizeof(__GLcontextModes));
762 m = modes;
763 if (!driFillInModes(&m, fb_format, fb_type,
764 depth_bits_array, stencil_bits_array,
765 depth_buffer_factor, back_buffer_modes,
766 back_buffer_factor, GLX_TRUE_COLOR)) {
767 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
768 __LINE__);
769 return NULL;
770 }
771 if (!driFillInModes(&m, fb_format, fb_type,
772 depth_bits_array, stencil_bits_array,
773 depth_buffer_factor, back_buffer_modes,
774 back_buffer_factor, GLX_DIRECT_COLOR)) {
775 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
776 __LINE__);
777 return NULL;
778 }
779
780 /* Mark the visual as slow if there are "fake" stencil bits.
781 */
782 for (m = modes; m != NULL; m = m->next) {
783 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
784 m->visualRating = GLX_SLOW_CONFIG;
785 }
786 }
787
788 return modes;
789 }
790
791
792 /**
793 * This is the bootstrap function for the driver. libGL supplies all of the
794 * requisite information about the system, and the driver initializes itself.
795 * This routine also fills in the linked list pointed to by \c driver_modes
796 * with the \c __GLcontextModes that the driver can support for windows or
797 * pbuffers.
798 *
799 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
800 * failure.
801 */
802 PUBLIC void *
803 __driCreateNewScreen_20050727(__DRInativeDisplay * dpy, int scrn,
804 __DRIscreen * psc,
805 const __GLcontextModes * modes,
806 const __DRIversion * ddx_version,
807 const __DRIversion * dri_version,
808 const __DRIversion * drm_version,
809 const __DRIframebuffer * frame_buffer,
810 drmAddress pSAREA, int fd,
811 int internal_api_version,
812 const __DRIinterfaceMethods * interface,
813 __GLcontextModes ** driver_modes)
814 {
815 __DRIscreenPrivate *psp;
816 static const __DRIversion ddx_expected = { 1, 5, 0 };
817 static const __DRIversion dri_expected = { 4, 0, 0 };
818 static const __DRIversion drm_expected = { 1, 7, 0 };
819
820 dri_interface = interface;
821
822 if (!driCheckDriDdxDrmVersions2("i915",
823 dri_version, &dri_expected,
824 ddx_version, &ddx_expected,
825 drm_version, &drm_expected)) {
826 return NULL;
827 }
828
829 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
830 ddx_version, dri_version, drm_version,
831 frame_buffer, pSAREA, fd,
832 internal_api_version, &intelAPI);
833 if (psp != NULL) {
834 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
835 *driver_modes = intelFillInModes(dri_priv->cpp * 8,
836 (dri_priv->cpp == 2) ? 16 : 24,
837 (dri_priv->cpp == 2) ? 0 : 8, 1);
838
839 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
840 * enable the extensions. It just makes sure that all the dispatch offsets for all
841 * the extensions that *might* be enables are known. This is needed because the
842 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
843 * enable the extensions until we have a context pointer.
844 *
845 * Hello chicken. Hello egg. How are you two today?
846 */
847 driInitExtensions(NULL, card_extensions, GL_FALSE);
848 }
849
850 return (void *) psp;
851 }
852
853 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
854 {
855 /*
856 * This should probably change to have the screen allocate a dummy
857 * context at screen creation. For now just use the current context.
858 */
859
860 GET_CURRENT_CONTEXT(ctx);
861 if (ctx == NULL) {
862 _mesa_problem(NULL, "No current context in intelScreenContext\n");
863 return NULL;
864 }
865 return intel_context(ctx);
866 }
867