intel: Don't segfault on TFP from a bad drawable.
[mesa.git] / src / mesa / drivers / dri / intel / intel_context.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
29 #include "glheader.h"
30 #include "context.h"
31 #include "matrix.h"
32 #include "simple_list.h"
33 #include "extensions.h"
34 #include "framebuffer.h"
35 #include "imports.h"
36 #include "points.h"
37
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
40 #include "tnl/tnl.h"
41
42 #include "tnl/t_pipeline.h"
43 #include "tnl/t_vertex.h"
44
45 #include "drivers/common/driverfuncs.h"
46
47 #include "intel_screen.h"
48
49 #include "i830_dri.h"
50
51 #include "intel_chipset.h"
52 #include "intel_buffers.h"
53 #include "intel_tex.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_blit.h"
56 #include "intel_pixel.h"
57 #include "intel_regions.h"
58 #include "intel_buffer_objects.h"
59 #include "intel_fbo.h"
60 #include "intel_decode.h"
61 #include "intel_bufmgr.h"
62
63 #include "drirenderbuffer.h"
64 #include "vblank.h"
65 #include "utils.h"
66 #include "xmlpool.h" /* for symbolic values of enum-type options */
67 #ifndef INTEL_DEBUG
68 int INTEL_DEBUG = (0);
69 #endif
70
71 #define need_GL_NV_point_sprite
72 #define need_GL_ARB_multisample
73 #define need_GL_ARB_point_parameters
74 #define need_GL_ARB_texture_compression
75 #define need_GL_ARB_vertex_buffer_object
76 #define need_GL_ARB_vertex_program
77 #define need_GL_ARB_window_pos
78 #define need_GL_ARB_occlusion_query
79 #define need_GL_EXT_blend_color
80 #define need_GL_EXT_blend_equation_separate
81 #define need_GL_EXT_blend_func_separate
82 #define need_GL_EXT_blend_minmax
83 #define need_GL_EXT_cull_vertex
84 #define need_GL_EXT_fog_coord
85 #define need_GL_EXT_framebuffer_object
86 #define need_GL_EXT_multi_draw_arrays
87 #define need_GL_EXT_secondary_color
88 #define need_GL_NV_vertex_program
89 #define need_GL_ATI_separate_stencil
90 #define need_GL_EXT_point_parameters
91 #define need_GL_VERSION_2_0
92 #define need_GL_VERSION_2_1
93 #define need_GL_ARB_shader_objects
94 #define need_GL_ARB_vertex_shader
95
96 #include "extension_helper.h"
97
98 #define DRIVER_DATE "20080716"
99 #define DRIVER_DATE_GEM "GEM " DRIVER_DATE
100
101 static const GLubyte *
102 intelGetString(GLcontext * ctx, GLenum name)
103 {
104 const struct intel_context *const intel = intel_context(ctx);
105 const char *chipset;
106 static char buffer[128];
107
108 switch (name) {
109 case GL_VENDOR:
110 return (GLubyte *) "Tungsten Graphics, Inc";
111 break;
112
113 case GL_RENDERER:
114 switch (intel->intelScreen->deviceID) {
115 case PCI_CHIP_845_G:
116 chipset = "Intel(R) 845G";
117 break;
118 case PCI_CHIP_I830_M:
119 chipset = "Intel(R) 830M";
120 break;
121 case PCI_CHIP_I855_GM:
122 chipset = "Intel(R) 852GM/855GM";
123 break;
124 case PCI_CHIP_I865_G:
125 chipset = "Intel(R) 865G";
126 break;
127 case PCI_CHIP_I915_G:
128 chipset = "Intel(R) 915G";
129 break;
130 case PCI_CHIP_E7221_G:
131 chipset = "Intel (R) E7221G (i915)";
132 break;
133 case PCI_CHIP_I915_GM:
134 chipset = "Intel(R) 915GM";
135 break;
136 case PCI_CHIP_I945_G:
137 chipset = "Intel(R) 945G";
138 break;
139 case PCI_CHIP_I945_GM:
140 chipset = "Intel(R) 945GM";
141 break;
142 case PCI_CHIP_I945_GME:
143 chipset = "Intel(R) 945GME";
144 break;
145 case PCI_CHIP_G33_G:
146 chipset = "Intel(R) G33";
147 break;
148 case PCI_CHIP_Q35_G:
149 chipset = "Intel(R) Q35";
150 break;
151 case PCI_CHIP_Q33_G:
152 chipset = "Intel(R) Q33";
153 break;
154 case PCI_CHIP_I965_Q:
155 chipset = "Intel(R) 965Q";
156 break;
157 case PCI_CHIP_I965_G:
158 case PCI_CHIP_I965_G_1:
159 chipset = "Intel(R) 965G";
160 break;
161 case PCI_CHIP_I946_GZ:
162 chipset = "Intel(R) 946GZ";
163 break;
164 case PCI_CHIP_I965_GM:
165 chipset = "Intel(R) 965GM";
166 break;
167 case PCI_CHIP_I965_GME:
168 chipset = "Intel(R) 965GME/GLE";
169 break;
170 case PCI_CHIP_GM45_GM:
171 chipset = "Mobile IntelĀ® GM45 Express Chipset";
172 break;
173 case PCI_CHIP_IGD_E_G:
174 chipset = "Intel(R) Integrated Graphics Device";
175 break;
176 case PCI_CHIP_G45_G:
177 chipset = "Intel(R) G45/G43";
178 break;
179 case PCI_CHIP_Q45_G:
180 chipset = "Intel(R) Q45/Q43";
181 break;
182 case PCI_CHIP_G41_G:
183 chipset = "Intel(R) G41";
184 break;
185 default:
186 chipset = "Unknown Intel Chipset";
187 break;
188 }
189
190 (void) driGetRendererString(buffer, chipset,
191 (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
192 0);
193 return (GLubyte *) buffer;
194
195 default:
196 return NULL;
197 }
198 }
199
200 void
201 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
202 {
203 struct intel_framebuffer *intel_fb = drawable->driverPrivate;
204 struct intel_renderbuffer *rb;
205 struct intel_region *region, *depth_region;
206 struct intel_context *intel = context->driverPrivate;
207 __DRIbuffer *buffers;
208 __DRIscreen *screen;
209 int i, count;
210 unsigned int attachments[10];
211 uint32_t name;
212 const char *region_name;
213
214 if (INTEL_DEBUG & DEBUG_DRI)
215 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
216
217 screen = intel->intelScreen->driScrnPriv;
218
219 i = 0;
220 if (intel_fb->color_rb[0])
221 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
222 if (intel_fb->color_rb[1])
223 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
224 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
225 attachments[i++] = __DRI_BUFFER_DEPTH;
226 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
227 attachments[i++] = __DRI_BUFFER_STENCIL;
228
229 buffers = (*screen->dri2.loader->getBuffers)(drawable,
230 &drawable->w,
231 &drawable->h,
232 attachments, i,
233 &count,
234 drawable->loaderPrivate);
235
236 if (buffers == NULL)
237 return;
238
239 drawable->x = 0;
240 drawable->y = 0;
241 drawable->backX = 0;
242 drawable->backY = 0;
243 drawable->numClipRects = 1;
244 drawable->pClipRects[0].x1 = 0;
245 drawable->pClipRects[0].y1 = 0;
246 drawable->pClipRects[0].x2 = drawable->w;
247 drawable->pClipRects[0].y2 = drawable->h;
248 drawable->numBackClipRects = 1;
249 drawable->pBackClipRects[0].x1 = 0;
250 drawable->pBackClipRects[0].y1 = 0;
251 drawable->pBackClipRects[0].x2 = drawable->w;
252 drawable->pBackClipRects[0].y2 = drawable->h;
253
254 depth_region = NULL;
255 for (i = 0; i < count; i++) {
256 switch (buffers[i].attachment) {
257 case __DRI_BUFFER_FRONT_LEFT:
258 rb = intel_fb->color_rb[0];
259 region_name = "dri2 front buffer";
260 break;
261
262 case __DRI_BUFFER_BACK_LEFT:
263 rb = intel_fb->color_rb[1];
264 region_name = "dri2 back buffer";
265 break;
266
267 case __DRI_BUFFER_DEPTH:
268 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
269 region_name = "dri2 depth buffer";
270 break;
271
272 case __DRI_BUFFER_STENCIL:
273 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
274 region_name = "dri2 stencil buffer";
275 break;
276
277 case __DRI_BUFFER_ACCUM:
278 default:
279 fprintf(stderr,
280 "unhandled buffer attach event, attacment type %d\n",
281 buffers[i].attachment);
282 return;
283 }
284
285 if (rb->region) {
286 dri_bo_flink(rb->region->buffer, &name);
287 if (name == buffers[i].name)
288 continue;
289 }
290
291 if (INTEL_DEBUG & DEBUG_DRI)
292 fprintf(stderr,
293 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
294 buffers[i].name, buffers[i].attachment,
295 buffers[i].cpp, buffers[i].pitch);
296
297 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
298 if (INTEL_DEBUG & DEBUG_DRI)
299 fprintf(stderr, "(reusing depth buffer as stencil)\n");
300 intel_region_reference(&region, depth_region);
301 }
302 else
303 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
304 buffers[i].pitch / buffers[i].cpp,
305 drawable->h,
306 buffers[i].name,
307 region_name);
308
309 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
310 depth_region = region;
311
312 intel_renderbuffer_set_region(rb, region);
313 intel_region_release(&region);
314 }
315
316 driUpdateFramebufferSize(&intel->ctx, drawable);
317 }
318
319 static void
320 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
321 {
322 struct intel_context *intel = intel_context(ctx);
323 __DRIcontext *driContext = intel->driContext;
324
325 if (!driContext->driScreenPriv->dri2.enabled)
326 return;
327
328 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
329 if (driContext->driDrawablePriv != driContext->driReadablePriv)
330 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
331
332 ctx->Driver.Viewport = NULL;
333 intel->driDrawable = driContext->driDrawablePriv;
334 intelWindowMoved(intel);
335 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
336 ctx->Driver.Viewport = intel_viewport;
337 }
338
339 /**
340 * Extension strings exported by the intel driver.
341 *
342 * Extensions supported by all chips supported by i830_dri, i915_dri, or
343 * i965_dri.
344 */
345 static const struct dri_extension card_extensions[] = {
346 {"GL_ARB_multisample", GL_ARB_multisample_functions},
347 {"GL_ARB_multitexture", NULL},
348 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
349 {"GL_NV_point_sprite", GL_NV_point_sprite_functions},
350 {"GL_ARB_texture_border_clamp", NULL},
351 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions},
352 {"GL_ARB_texture_cube_map", NULL},
353 {"GL_ARB_texture_env_add", NULL},
354 {"GL_ARB_texture_env_combine", NULL},
355 {"GL_ARB_texture_env_crossbar", NULL},
356 {"GL_ARB_texture_env_dot3", NULL},
357 {"GL_ARB_texture_mirrored_repeat", NULL},
358 {"GL_ARB_texture_non_power_of_two", NULL },
359 {"GL_ARB_texture_rectangle", NULL},
360 {"GL_NV_texture_rectangle", NULL},
361 {"GL_EXT_texture_rectangle", NULL},
362 {"GL_ARB_point_parameters", NULL},
363 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
364 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
365 {"GL_ARB_window_pos", GL_ARB_window_pos_functions},
366 {"GL_EXT_blend_color", GL_EXT_blend_color_functions},
367 {"GL_EXT_blend_equation_separate",
368 GL_EXT_blend_equation_separate_functions},
369 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
370 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
371 {"GL_EXT_blend_logic_op", NULL},
372 {"GL_EXT_blend_subtract", NULL},
373 {"GL_EXT_cull_vertex", GL_EXT_cull_vertex_functions},
374 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions},
375 {"GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions},
376 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions},
377 #if 1 /* XXX FBO temporary? */
378 {"GL_EXT_packed_depth_stencil", NULL},
379 #endif
380 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
381 {"GL_EXT_stencil_wrap", NULL},
382 {"GL_EXT_texture_edge_clamp", NULL},
383 {"GL_EXT_texture_env_combine", NULL},
384 {"GL_EXT_texture_env_dot3", NULL},
385 {"GL_EXT_texture_filter_anisotropic", NULL},
386 {"GL_EXT_texture_lod_bias", NULL},
387 {"GL_3DFX_texture_compression_FXT1", NULL},
388 {"GL_APPLE_client_storage", NULL},
389 {"GL_MESA_pack_invert", NULL},
390 {"GL_MESA_ycbcr_texture", NULL},
391 {"GL_NV_blend_square", NULL},
392 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
393 {"GL_NV_vertex_program1_1", NULL},
394 { "GL_SGIS_generate_mipmap", NULL },
395 {NULL, NULL}
396 };
397
398 static const struct dri_extension brw_extensions[] = {
399 { "GL_ARB_shading_language_100", GL_VERSION_2_0_functions},
400 { "GL_ARB_shading_language_120", GL_VERSION_2_1_functions},
401 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions},
402 { "GL_ARB_vertex_shader", GL_ARB_vertex_shader_functions},
403 { "GL_ARB_point_sprite", NULL},
404 { "GL_ARB_fragment_shader", NULL },
405 { "GL_ARB_draw_buffers", NULL },
406 { "GL_ARB_depth_texture", NULL },
407 { "GL_ARB_fragment_program", NULL },
408 { "GL_ARB_shadow", NULL },
409 { "GL_EXT_shadow_funcs", NULL },
410 { "GL_ARB_fragment_program_shadow", NULL },
411 /* ARB extn won't work if not enabled */
412 { "GL_SGIX_depth_texture", NULL },
413 { "GL_EXT_texture_sRGB", NULL},
414 { NULL, NULL }
415 };
416
417 #ifdef I915_MMIO_READ
418 static const struct dri_extension arb_oc_extensions[] = {
419 {"GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions},
420 {NULL, NULL}
421 };
422 #endif
423
424 static const struct dri_extension ttm_extensions[] = {
425 {"GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions},
426 {"GL_ARB_pixel_buffer_object", NULL},
427 {NULL, NULL}
428 };
429
430 /**
431 * Initializes potential list of extensions if ctx == NULL, or actually enables
432 * extensions for a context.
433 */
434 void intelInitExtensions(GLcontext *ctx, GLboolean enable_imaging)
435 {
436 struct intel_context *intel = ctx?intel_context(ctx):NULL;
437
438 /* Disable imaging extension until convolution is working in teximage paths.
439 */
440 enable_imaging = GL_FALSE;
441
442 driInitExtensions(ctx, card_extensions, enable_imaging);
443
444 if (intel == NULL || intel->ttm)
445 driInitExtensions(ctx, ttm_extensions, GL_FALSE);
446
447 #ifdef I915_MMIO_READ
448 if (intel == NULL ||
449 (IS_965(intel->intelScreen->deviceID) &&
450 intel->intelScreen->drmMinor >= 8))
451 driInitExtensions(ctx, arb_oc_extensions, GL_FALSE);
452 #endif
453
454 if (intel == NULL || IS_965(intel->intelScreen->deviceID))
455 driInitExtensions(ctx, brw_extensions, GL_FALSE);
456 }
457
458 static const struct dri_debug_control debug_control[] = {
459 { "tex", DEBUG_TEXTURE},
460 { "state", DEBUG_STATE},
461 { "ioctl", DEBUG_IOCTL},
462 { "blit", DEBUG_BLIT},
463 { "mip", DEBUG_MIPTREE},
464 { "fall", DEBUG_FALLBACKS},
465 { "verb", DEBUG_VERBOSE},
466 { "bat", DEBUG_BATCH},
467 { "pix", DEBUG_PIXEL},
468 { "buf", DEBUG_BUFMGR},
469 { "reg", DEBUG_REGION},
470 { "fbo", DEBUG_FBO},
471 { "lock", DEBUG_LOCK},
472 { "sync", DEBUG_SYNC},
473 { "prim", DEBUG_PRIMS },
474 { "vert", DEBUG_VERTS },
475 { "dri", DEBUG_DRI },
476 { "dma", DEBUG_DMA },
477 { "san", DEBUG_SANITY },
478 { "sleep", DEBUG_SLEEP },
479 { "stats", DEBUG_STATS },
480 { "tile", DEBUG_TILE },
481 { "sing", DEBUG_SINGLE_THREAD },
482 { "thre", DEBUG_SINGLE_THREAD },
483 { "wm", DEBUG_WM },
484 { "urb", DEBUG_URB },
485 { "vs", DEBUG_VS },
486 { NULL, 0 }
487 };
488
489
490 static void
491 intelInvalidateState(GLcontext * ctx, GLuint new_state)
492 {
493 struct intel_context *intel = intel_context(ctx);
494
495 _swrast_InvalidateState(ctx, new_state);
496 _swsetup_InvalidateState(ctx, new_state);
497 _vbo_InvalidateState(ctx, new_state);
498 _tnl_InvalidateState(ctx, new_state);
499 _tnl_invalidate_vertex_state(ctx, new_state);
500
501 intel->NewGLState |= new_state;
502
503 if (intel->vtbl.invalidate_state)
504 intel->vtbl.invalidate_state( intel, new_state );
505 }
506
507
508 void
509 intelFlush(GLcontext * ctx)
510 {
511 struct intel_context *intel = intel_context(ctx);
512
513 if (intel->Fallback)
514 _swrast_flush(ctx);
515
516 if (!IS_965(intel->intelScreen->deviceID))
517 INTEL_FIREVERTICES(intel);
518
519 /* Emit a flush so that any frontbuffer rendering that might have occurred
520 * lands onscreen in a timely manner, even if the X Server doesn't trigger
521 * a flush for us.
522 */
523 intel_batchbuffer_emit_mi_flush(intel->batch);
524
525 if (intel->batch->map != intel->batch->ptr)
526 intel_batchbuffer_flush(intel->batch);
527 }
528
529 void
530 intelFinish(GLcontext * ctx)
531 {
532 struct gl_framebuffer *fb = ctx->DrawBuffer;
533 int i;
534
535 intelFlush(ctx);
536
537 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
538 struct intel_renderbuffer *irb;
539
540 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
541
542 if (irb->region)
543 dri_bo_wait_rendering(irb->region->buffer);
544 }
545 if (fb->_DepthBuffer) {
546 /* XXX: Wait on buffer idle */
547 }
548 }
549
550 #ifdef I915_MMIO_READ
551 static void
552 intelBeginQuery(GLcontext *ctx, GLenum target, struct gl_query_object *q)
553 {
554 struct intel_context *intel = intel_context( ctx );
555 struct drm_i915_mmio io = {
556 .read_write = I915_MMIO_READ,
557 .reg = MMIO_REGS_PS_DEPTH_COUNT,
558 .data = &q->Result
559 };
560 intel->stats_wm++;
561 intelFinish(&intel->ctx);
562 drmCommandWrite(intel->driFd, DRM_I915_MMIO, &io, sizeof(io));
563 }
564
565 static void
566 intelEndQuery(GLcontext *ctx, GLenum target, struct gl_query_object *q)
567 {
568 struct intel_context *intel = intel_context( ctx );
569 GLuint64EXT tmp;
570 struct drm_i915_mmio io = {
571 .read_write = I915_MMIO_READ,
572 .reg = MMIO_REGS_PS_DEPTH_COUNT,
573 .data = &tmp
574 };
575 intelFinish(&intel->ctx);
576 drmCommandWrite(intel->driFd, DRM_I915_MMIO, &io, sizeof(io));
577 q->Result = tmp - q->Result;
578 q->Ready = GL_TRUE;
579 intel->stats_wm--;
580 }
581 #endif
582
583 void
584 intelInitDriverFunctions(struct dd_function_table *functions)
585 {
586 _mesa_init_driver_functions(functions);
587
588 functions->Flush = intelFlush;
589 functions->Finish = intelFinish;
590 functions->GetString = intelGetString;
591 functions->UpdateState = intelInvalidateState;
592 functions->Viewport = intel_viewport;
593
594 functions->CopyColorTable = _swrast_CopyColorTable;
595 functions->CopyColorSubTable = _swrast_CopyColorSubTable;
596 functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
597 functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
598
599 #ifdef I915_MMIO_READ
600 functions->BeginQuery = intelBeginQuery;
601 functions->EndQuery = intelEndQuery;
602 #endif
603
604 intelInitTextureFuncs(functions);
605 intelInitStateFuncs(functions);
606 intelInitBufferFuncs(functions);
607 intelInitPixelFuncs(functions);
608 }
609
610
611 GLboolean
612 intelInitContext(struct intel_context *intel,
613 const __GLcontextModes * mesaVis,
614 __DRIcontextPrivate * driContextPriv,
615 void *sharedContextPrivate,
616 struct dd_function_table *functions)
617 {
618 GLcontext *ctx = &intel->ctx;
619 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
620 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
621 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
622 int fthrottle_mode;
623
624 if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
625 functions, (void *) intel)) {
626 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
627 return GL_FALSE;
628 }
629
630 driContextPriv->driverPrivate = intel;
631 intel->intelScreen = intelScreen;
632 intel->driScreen = sPriv;
633 intel->sarea = intelScreen->sarea;
634 intel->driContext = driContextPriv;
635
636 /* Dri stuff */
637 intel->hHWContext = driContextPriv->hHWContext;
638 intel->driFd = sPriv->fd;
639 intel->driHwLock = sPriv->lock;
640
641 intel->width = intelScreen->width;
642 intel->height = intelScreen->height;
643
644 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
645 intel->driScreen->myNum,
646 IS_965(intelScreen->deviceID) ? "i965" : "i915");
647 if (intelScreen->deviceID == PCI_CHIP_I865_G)
648 intel->maxBatchSize = 4096;
649 else
650 intel->maxBatchSize = BATCH_SZ;
651
652 intel->bufmgr = intelScreen->bufmgr;
653 intel->ttm = intelScreen->ttm;
654 if (intel->ttm) {
655 int bo_reuse_mode;
656
657 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
658 switch (bo_reuse_mode) {
659 case DRI_CONF_BO_REUSE_DISABLED:
660 break;
661 case DRI_CONF_BO_REUSE_ALL:
662 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
663 break;
664 }
665 }
666
667 ctx->Const.MaxTextureMaxAnisotropy = 2.0;
668
669 /* This doesn't yet catch all non-conformant rendering, but it's a
670 * start.
671 */
672 if (getenv("INTEL_STRICT_CONFORMANCE")) {
673 intel->strict_conformance = 1;
674 }
675
676 if (intel->strict_conformance) {
677 ctx->Const.MinLineWidth = 1.0;
678 ctx->Const.MinLineWidthAA = 1.0;
679 ctx->Const.MaxLineWidth = 1.0;
680 ctx->Const.MaxLineWidthAA = 1.0;
681 ctx->Const.LineWidthGranularity = 1.0;
682 }
683 else {
684 ctx->Const.MinLineWidth = 1.0;
685 ctx->Const.MinLineWidthAA = 1.0;
686 ctx->Const.MaxLineWidth = 5.0;
687 ctx->Const.MaxLineWidthAA = 5.0;
688 ctx->Const.LineWidthGranularity = 0.5;
689 }
690
691 ctx->Const.MinPointSize = 1.0;
692 ctx->Const.MinPointSizeAA = 1.0;
693 ctx->Const.MaxPointSize = 255.0;
694 ctx->Const.MaxPointSizeAA = 3.0;
695 ctx->Const.PointSizeGranularity = 1.0;
696
697 /* reinitialize the context point state.
698 * It depend on constants in __GLcontextRec::Const
699 */
700 _mesa_init_point(ctx);
701
702 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
703
704 /* Initialize the software rasterizer and helper modules. */
705 _swrast_CreateContext(ctx);
706 _vbo_CreateContext(ctx);
707 _tnl_CreateContext(ctx);
708 _swsetup_CreateContext(ctx);
709
710 /* Configure swrast to match hardware characteristics: */
711 _swrast_allow_pixel_fog(ctx, GL_FALSE);
712 _swrast_allow_vertex_fog(ctx, GL_TRUE);
713
714 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
715 intel->hw_stipple = 1;
716
717 /* XXX FBO: this doesn't seem to be used anywhere */
718 switch (mesaVis->depthBits) {
719 case 0: /* what to do in this case? */
720 case 16:
721 intel->polygon_offset_scale = 1.0;
722 break;
723 case 24:
724 intel->polygon_offset_scale = 2.0; /* req'd to pass glean */
725 break;
726 default:
727 assert(0);
728 break;
729 }
730
731 if (IS_965(intelScreen->deviceID))
732 intel->polygon_offset_scale /= 0xffff;
733
734 intel->RenderIndex = ~0;
735
736 fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
737 intel->irqsEmitted = 0;
738
739 intel->do_irqs = (intel->intelScreen->irq_active &&
740 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
741
742 intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
743
744 _math_matrix_ctr(&intel->ViewportMatrix);
745
746 if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
747 _mesa_printf("IRQs not active. Exiting\n");
748 exit(1);
749 }
750
751 intelInitExtensions(ctx, GL_FALSE);
752
753 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
754 if (INTEL_DEBUG & DEBUG_BUFMGR)
755 dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
756
757 if (!sPriv->dri2.enabled)
758 intel_recreate_static_regions(intel);
759
760 intel->batch = intel_batchbuffer_alloc(intel);
761
762 intel_bufferobj_init(intel);
763 intel_fbo_init(intel);
764
765 if (intel->ctx.Mesa_DXTn) {
766 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
767 _mesa_enable_extension(ctx, "GL_S3_s3tc");
768 }
769 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
770 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
771 }
772
773 intel->prim.primitive = ~0;
774
775 /* Force all software fallbacks */
776 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
777 fprintf(stderr, "disabling 3D rasterization\n");
778 intel->no_rast = 1;
779 }
780
781 /* Disable all hardware rendering (skip emitting batches and fences/waits
782 * to the kernel)
783 */
784 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
785
786 return GL_TRUE;
787 }
788
789 void
790 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
791 {
792 struct intel_context *intel =
793 (struct intel_context *) driContextPriv->driverPrivate;
794
795 assert(intel); /* should never be null */
796 if (intel) {
797 GLboolean release_texture_heaps;
798
799 INTEL_FIREVERTICES(intel);
800
801 intel->vtbl.destroy(intel);
802
803 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
804 _swsetup_DestroyContext(&intel->ctx);
805 _tnl_DestroyContext(&intel->ctx);
806 _vbo_DestroyContext(&intel->ctx);
807
808 _swrast_DestroyContext(&intel->ctx);
809 intel->Fallback = 0; /* don't call _swrast_Flush later */
810
811 intel_batchbuffer_free(intel->batch);
812 free(intel->prim.vb);
813
814 if (release_texture_heaps) {
815 /* This share group is about to go away, free our private
816 * texture object data.
817 */
818 if (INTEL_DEBUG & DEBUG_TEXTURE)
819 fprintf(stderr, "do something to free texture heaps\n");
820 }
821
822 /* free the Mesa context */
823 _mesa_free_context_data(&intel->ctx);
824
825 dri_bufmgr_destroy(intel->bufmgr);
826 }
827 }
828
829 GLboolean
830 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
831 {
832 return GL_TRUE;
833 }
834
835 GLboolean
836 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
837 __DRIdrawablePrivate * driDrawPriv,
838 __DRIdrawablePrivate * driReadPriv)
839 {
840 __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
841
842 if (driContextPriv) {
843 struct intel_context *intel =
844 (struct intel_context *) driContextPriv->driverPrivate;
845 struct intel_framebuffer *intel_fb =
846 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
847 GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
848
849 if (driContextPriv->driScreenPriv->dri2.enabled) {
850 intel_update_renderbuffers(driContextPriv, driDrawPriv);
851 if (driDrawPriv != driReadPriv)
852 intel_update_renderbuffers(driContextPriv, driReadPriv);
853 } else {
854 /* XXX FBO temporary fix-ups! */
855 /* if the renderbuffers don't have regions, init them from the context */
856 struct intel_renderbuffer *irbDepth
857 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
858 struct intel_renderbuffer *irbStencil
859 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
860
861 if (intel_fb->color_rb[0]) {
862 intel_renderbuffer_set_region(intel_fb->color_rb[0],
863 intel->front_region);
864 }
865 if (intel_fb->color_rb[1]) {
866 intel_renderbuffer_set_region(intel_fb->color_rb[1],
867 intel->back_region);
868 }
869 #if 0
870 if (intel_fb->color_rb[2]) {
871 intel_renderbuffer_set_region(intel_fb->color_rb[2],
872 intel->third_region);
873 }
874 #endif
875 if (irbDepth) {
876 intel_renderbuffer_set_region(irbDepth, intel->depth_region);
877 }
878 if (irbStencil) {
879 intel_renderbuffer_set_region(irbStencil, intel->depth_region);
880 }
881 }
882
883 /* set GLframebuffer size to match window, if needed */
884 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
885
886 if (driReadPriv != driDrawPriv) {
887 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
888 }
889
890 _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
891
892 /* The drawbuffer won't always be updated by _mesa_make_current:
893 */
894 if (intel->ctx.DrawBuffer == &intel_fb->Base) {
895
896 if (intel->driReadDrawable != driReadPriv)
897 intel->driReadDrawable = driReadPriv;
898
899 if (intel->driDrawable != driDrawPriv) {
900 if (driDrawPriv->swap_interval == (unsigned)-1) {
901 int i;
902
903 driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
904 ? driGetDefaultVBlankFlags(&intel->optionCache)
905 : VBLANK_FLAG_NO_IRQ;
906
907 (*psp->systemTime->getUST) (&intel_fb->swap_ust);
908 driDrawableInitVBlank(driDrawPriv);
909 intel_fb->vbl_waited = driDrawPriv->vblSeq;
910
911 for (i = 0; i < (intel->intelScreen->third.handle ? 3 : 2); i++) {
912 if (intel_fb->color_rb[i])
913 intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
914 }
915 }
916 intel->driDrawable = driDrawPriv;
917 intelWindowMoved(intel);
918 }
919
920 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
921 }
922 }
923 else {
924 _mesa_make_current(NULL, NULL, NULL);
925 }
926
927 return GL_TRUE;
928 }
929
930 static void
931 intelContendedLock(struct intel_context *intel, GLuint flags)
932 {
933 __DRIdrawablePrivate *dPriv = intel->driDrawable;
934 __DRIscreenPrivate *sPriv = intel->driScreen;
935 volatile struct drm_i915_sarea *sarea = intel->sarea;
936 int me = intel->hHWContext;
937
938 drmGetLock(intel->driFd, intel->hHWContext, flags);
939 intel->locked = 1;
940
941 if (INTEL_DEBUG & DEBUG_LOCK)
942 _mesa_printf("%s - got contended lock\n", __progname);
943
944 /* If the window moved, may need to set a new cliprect now.
945 *
946 * NOTE: This releases and regains the hw lock, so all state
947 * checking must be done *after* this call:
948 */
949 if (dPriv)
950 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
951
952 if (sarea && sarea->ctxOwner != me) {
953 if (INTEL_DEBUG & DEBUG_BUFMGR) {
954 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
955 sarea->ctxOwner, me);
956 }
957 sarea->ctxOwner = me;
958 }
959
960 /* If the last consumer of the texture memory wasn't us, notify the fake
961 * bufmgr and record the new owner. We should have the memory shared
962 * between contexts of a single fake bufmgr, but this will at least make
963 * things correct for now.
964 */
965 if (!intel->ttm && sarea->texAge != intel->hHWContext) {
966 sarea->texAge = intel->hHWContext;
967 intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
968 if (INTEL_DEBUG & DEBUG_BATCH)
969 intel_decode_context_reset();
970 if (INTEL_DEBUG & DEBUG_BUFMGR)
971 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
972 sarea->ctxOwner, intel->hHWContext);
973 }
974
975 if (sarea->width != intel->width || sarea->height != intel->height) {
976 int numClipRects = intel->numClipRects;
977
978 /*
979 * FIXME: Really only need to do this when drawing to a
980 * common back- or front buffer.
981 */
982
983 /*
984 * This will essentially drop the outstanding batchbuffer on
985 * the floor.
986 */
987 intel->numClipRects = 0;
988
989 if (intel->Fallback)
990 _swrast_flush(&intel->ctx);
991
992 if (!IS_965(intel->intelScreen->deviceID))
993 INTEL_FIREVERTICES(intel);
994
995 if (intel->batch->map != intel->batch->ptr)
996 intel_batchbuffer_flush(intel->batch);
997
998 intel->numClipRects = numClipRects;
999
1000 /* force window update */
1001 intel->lastStamp = 0;
1002
1003 intel->width = sarea->width;
1004 intel->height = sarea->height;
1005 }
1006
1007 /* Drawable changed?
1008 */
1009 if (dPriv && intel->lastStamp != dPriv->lastStamp) {
1010 intelWindowMoved(intel);
1011 intel->lastStamp = dPriv->lastStamp;
1012 }
1013 }
1014
1015
1016 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
1017
1018 /* Lock the hardware and validate our state.
1019 */
1020 void LOCK_HARDWARE( struct intel_context *intel )
1021 {
1022 __DRIdrawable *dPriv = intel->driDrawable;
1023 __DRIscreen *sPriv = intel->driScreen;
1024 char __ret = 0;
1025 struct intel_framebuffer *intel_fb = NULL;
1026 struct intel_renderbuffer *intel_rb = NULL;
1027
1028 _glthread_LOCK_MUTEX(lockMutex);
1029 assert(!intel->locked);
1030 intel->locked = 1;
1031
1032 if (intel->driDrawable) {
1033 intel_fb = intel->driDrawable->driverPrivate;
1034
1035 if (intel_fb)
1036 intel_rb =
1037 intel_get_renderbuffer(&intel_fb->Base,
1038 intel_fb->Base._ColorDrawBufferIndexes[0]);
1039 }
1040
1041 if (intel_rb && dPriv->vblFlags &&
1042 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
1043 (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
1044 drmVBlank vbl;
1045
1046 vbl.request.type = DRM_VBLANK_ABSOLUTE;
1047
1048 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
1049 vbl.request.type |= DRM_VBLANK_SECONDARY;
1050 }
1051
1052 vbl.request.sequence = intel_rb->vbl_pending;
1053 drmWaitVBlank(intel->driFd, &vbl);
1054 intel_fb->vbl_waited = vbl.reply.sequence;
1055 }
1056
1057 if (!sPriv->dri2.enabled) {
1058 DRM_CAS(intel->driHwLock, intel->hHWContext,
1059 (DRM_LOCK_HELD|intel->hHWContext), __ret);
1060
1061 if (__ret)
1062 intelContendedLock( intel, 0 );
1063 }
1064
1065
1066 if (INTEL_DEBUG & DEBUG_LOCK)
1067 _mesa_printf("%s - locked\n", __progname);
1068 }
1069
1070
1071 /* Unlock the hardware using the global current context
1072 */
1073 void UNLOCK_HARDWARE( struct intel_context *intel )
1074 {
1075 __DRIscreen *sPriv = intel->driScreen;
1076
1077 intel->vtbl.note_unlock( intel );
1078 intel->locked = 0;
1079
1080 if (!sPriv->dri2.enabled)
1081 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1082
1083 _glthread_UNLOCK_MUTEX(lockMutex);
1084
1085 if (INTEL_DEBUG & DEBUG_LOCK)
1086 _mesa_printf("%s - unlocked\n", __progname);
1087
1088 /**
1089 * Nothing should be left in batch outside of LOCK/UNLOCK which references
1090 * cliprects.
1091 */
1092 if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1093 intel_batchbuffer_flush(intel->batch);
1094 }
1095