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