mesa: move _mesa_meta_init/free() calls to core Mesa
[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 "main/glheader.h"
30 #include "main/context.h"
31 #include "main/arrayobj.h"
32 #include "main/extensions.h"
33 #include "main/framebuffer.h"
34 #include "main/imports.h"
35 #include "main/points.h"
36
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "drivers/common/driverfuncs.h"
41
42 #include "i830_dri.h"
43
44 #include "intel_chipset.h"
45 #include "intel_buffers.h"
46 #include "intel_tex.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_clear.h"
49 #include "intel_extensions.h"
50 #include "intel_pixel.h"
51 #include "intel_regions.h"
52 #include "intel_buffer_objects.h"
53 #include "intel_fbo.h"
54 #include "intel_decode.h"
55 #include "intel_bufmgr.h"
56 #include "intel_screen.h"
57 #include "intel_swapbuffers.h"
58
59 #include "drirenderbuffer.h"
60 #include "vblank.h"
61 #include "utils.h"
62 #include "xmlpool.h" /* for symbolic values of enum-type options */
63
64
65 #ifndef INTEL_DEBUG
66 int INTEL_DEBUG = (0);
67 #endif
68
69
70 #define DRIVER_DATE "20090712 2009Q2 RC3"
71 #define DRIVER_DATE_GEM "GEM " DRIVER_DATE
72
73
74 static void intel_flush(GLcontext *ctx, GLboolean needs_mi_flush);
75
76 static const GLubyte *
77 intelGetString(GLcontext * ctx, GLenum name)
78 {
79 const struct intel_context *const intel = intel_context(ctx);
80 const char *chipset;
81 static char buffer[128];
82
83 switch (name) {
84 case GL_VENDOR:
85 return (GLubyte *) "Tungsten Graphics, Inc";
86 break;
87
88 case GL_RENDERER:
89 switch (intel->intelScreen->deviceID) {
90 case PCI_CHIP_845_G:
91 chipset = "Intel(R) 845G";
92 break;
93 case PCI_CHIP_I830_M:
94 chipset = "Intel(R) 830M";
95 break;
96 case PCI_CHIP_I855_GM:
97 chipset = "Intel(R) 852GM/855GM";
98 break;
99 case PCI_CHIP_I865_G:
100 chipset = "Intel(R) 865G";
101 break;
102 case PCI_CHIP_I915_G:
103 chipset = "Intel(R) 915G";
104 break;
105 case PCI_CHIP_E7221_G:
106 chipset = "Intel (R) E7221G (i915)";
107 break;
108 case PCI_CHIP_I915_GM:
109 chipset = "Intel(R) 915GM";
110 break;
111 case PCI_CHIP_I945_G:
112 chipset = "Intel(R) 945G";
113 break;
114 case PCI_CHIP_I945_GM:
115 chipset = "Intel(R) 945GM";
116 break;
117 case PCI_CHIP_I945_GME:
118 chipset = "Intel(R) 945GME";
119 break;
120 case PCI_CHIP_G33_G:
121 chipset = "Intel(R) G33";
122 break;
123 case PCI_CHIP_Q35_G:
124 chipset = "Intel(R) Q35";
125 break;
126 case PCI_CHIP_Q33_G:
127 chipset = "Intel(R) Q33";
128 break;
129 case PCI_CHIP_IGD_GM:
130 case PCI_CHIP_IGD_G:
131 chipset = "Intel(R) IGD";
132 break;
133 case PCI_CHIP_I965_Q:
134 chipset = "Intel(R) 965Q";
135 break;
136 case PCI_CHIP_I965_G:
137 case PCI_CHIP_I965_G_1:
138 chipset = "Intel(R) 965G";
139 break;
140 case PCI_CHIP_I946_GZ:
141 chipset = "Intel(R) 946GZ";
142 break;
143 case PCI_CHIP_I965_GM:
144 chipset = "Intel(R) 965GM";
145 break;
146 case PCI_CHIP_I965_GME:
147 chipset = "Intel(R) 965GME/GLE";
148 break;
149 case PCI_CHIP_GM45_GM:
150 chipset = "Mobile IntelĀ® GM45 Express Chipset";
151 break;
152 case PCI_CHIP_IGD_E_G:
153 chipset = "Intel(R) Integrated Graphics Device";
154 break;
155 case PCI_CHIP_G45_G:
156 chipset = "Intel(R) G45/G43";
157 break;
158 case PCI_CHIP_Q45_G:
159 chipset = "Intel(R) Q45/Q43";
160 break;
161 case PCI_CHIP_G41_G:
162 chipset = "Intel(R) G41";
163 break;
164 case PCI_CHIP_B43_G:
165 chipset = "Intel(R) B43";
166 break;
167 case PCI_CHIP_ILD_G:
168 chipset = "Intel(R) IGDNG_D";
169 break;
170 case PCI_CHIP_ILM_G:
171 chipset = "Intel(R) IGDNG_M";
172 break;
173 default:
174 chipset = "Unknown Intel Chipset";
175 break;
176 }
177
178 (void) driGetRendererString(buffer, chipset,
179 (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
180 0);
181 return (GLubyte *) buffer;
182
183 default:
184 return NULL;
185 }
186 }
187
188 static unsigned
189 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
190 {
191 switch (rb->Base._ActualFormat) {
192 case GL_RGB5:
193 case GL_DEPTH_COMPONENT16:
194 return 16;
195 case GL_RGB8:
196 case GL_RGBA8:
197 case GL_DEPTH_COMPONENT24:
198 case GL_DEPTH24_STENCIL8_EXT:
199 case GL_STENCIL_INDEX8_EXT:
200 return 32;
201 default:
202 return 0;
203 }
204 }
205
206 void
207 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
208 {
209 struct intel_framebuffer *intel_fb = drawable->driverPrivate;
210 struct intel_renderbuffer *rb;
211 struct intel_region *region, *depth_region;
212 struct intel_context *intel = context->driverPrivate;
213 __DRIbuffer *buffers = NULL;
214 __DRIscreen *screen;
215 int i, count;
216 unsigned int attachments[10];
217 uint32_t name;
218 const char *region_name;
219
220 if (INTEL_DEBUG & DEBUG_DRI)
221 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
222
223 screen = intel->intelScreen->driScrnPriv;
224
225 if (screen->dri2.loader
226 && (screen->dri2.loader->base.version > 2)
227 && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
228 struct intel_renderbuffer *depth_rb;
229 struct intel_renderbuffer *stencil_rb;
230
231 i = 0;
232 if ((intel->is_front_buffer_rendering ||
233 intel->is_front_buffer_reading ||
234 !intel_fb->color_rb[1])
235 && intel_fb->color_rb[0]) {
236 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
237 attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[0]);
238 }
239
240 if (intel_fb->color_rb[1]) {
241 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
242 attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[1]);
243 }
244
245 depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
246 stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
247
248 if ((depth_rb != NULL) && (stencil_rb != NULL)) {
249 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
250 attachments[i++] = intel_bits_per_pixel(depth_rb);
251 } else if (depth_rb != NULL) {
252 attachments[i++] = __DRI_BUFFER_DEPTH;
253 attachments[i++] = intel_bits_per_pixel(depth_rb);
254 } else if (stencil_rb != NULL) {
255 attachments[i++] = __DRI_BUFFER_STENCIL;
256 attachments[i++] = intel_bits_per_pixel(stencil_rb);
257 }
258
259 buffers =
260 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
261 &drawable->w,
262 &drawable->h,
263 attachments, i / 2,
264 &count,
265 drawable->loaderPrivate);
266 } else if (screen->dri2.loader) {
267 i = 0;
268 if (intel_fb->color_rb[0])
269 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
270 if (intel_fb->color_rb[1])
271 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
272 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
273 attachments[i++] = __DRI_BUFFER_DEPTH;
274 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
275 attachments[i++] = __DRI_BUFFER_STENCIL;
276
277 buffers = (*screen->dri2.loader->getBuffers)(drawable,
278 &drawable->w,
279 &drawable->h,
280 attachments, i,
281 &count,
282 drawable->loaderPrivate);
283 }
284
285 if (buffers == NULL)
286 return;
287
288 drawable->x = 0;
289 drawable->y = 0;
290 drawable->backX = 0;
291 drawable->backY = 0;
292 drawable->numClipRects = 1;
293 drawable->pClipRects[0].x1 = 0;
294 drawable->pClipRects[0].y1 = 0;
295 drawable->pClipRects[0].x2 = drawable->w;
296 drawable->pClipRects[0].y2 = drawable->h;
297 drawable->numBackClipRects = 1;
298 drawable->pBackClipRects[0].x1 = 0;
299 drawable->pBackClipRects[0].y1 = 0;
300 drawable->pBackClipRects[0].x2 = drawable->w;
301 drawable->pBackClipRects[0].y2 = drawable->h;
302
303 depth_region = NULL;
304 for (i = 0; i < count; i++) {
305 switch (buffers[i].attachment) {
306 case __DRI_BUFFER_FRONT_LEFT:
307 rb = intel_fb->color_rb[0];
308 region_name = "dri2 front buffer";
309 break;
310
311 case __DRI_BUFFER_FAKE_FRONT_LEFT:
312 rb = intel_fb->color_rb[0];
313 region_name = "dri2 fake front buffer";
314 break;
315
316 case __DRI_BUFFER_BACK_LEFT:
317 rb = intel_fb->color_rb[1];
318 region_name = "dri2 back buffer";
319 break;
320
321 case __DRI_BUFFER_DEPTH:
322 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
323 region_name = "dri2 depth buffer";
324 break;
325
326 case __DRI_BUFFER_DEPTH_STENCIL:
327 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
328 region_name = "dri2 depth / stencil buffer";
329 break;
330
331 case __DRI_BUFFER_STENCIL:
332 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
333 region_name = "dri2 stencil buffer";
334 break;
335
336 case __DRI_BUFFER_ACCUM:
337 default:
338 fprintf(stderr,
339 "unhandled buffer attach event, attacment type %d\n",
340 buffers[i].attachment);
341 return;
342 }
343
344 if (rb == NULL)
345 continue;
346
347 if (rb->region) {
348 dri_bo_flink(rb->region->buffer, &name);
349 if (name == buffers[i].name)
350 continue;
351 }
352
353 if (INTEL_DEBUG & DEBUG_DRI)
354 fprintf(stderr,
355 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
356 buffers[i].name, buffers[i].attachment,
357 buffers[i].cpp, buffers[i].pitch);
358
359 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
360 if (INTEL_DEBUG & DEBUG_DRI)
361 fprintf(stderr, "(reusing depth buffer as stencil)\n");
362 intel_region_reference(&region, depth_region);
363 }
364 else
365 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
366 drawable->w,
367 drawable->h,
368 buffers[i].pitch / buffers[i].cpp,
369 buffers[i].name,
370 region_name);
371
372 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
373 depth_region = region;
374
375 intel_renderbuffer_set_region(rb, region);
376 intel_region_release(&region);
377
378 if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
379 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
380 if (rb != NULL) {
381 struct intel_region *stencil_region = NULL;
382
383 if (rb->region) {
384 dri_bo_flink(rb->region->buffer, &name);
385 if (name == buffers[i].name)
386 continue;
387 }
388
389 intel_region_reference(&stencil_region, region);
390 intel_renderbuffer_set_region(rb, stencil_region);
391 intel_region_release(&stencil_region);
392 }
393 }
394 }
395
396 driUpdateFramebufferSize(&intel->ctx, drawable);
397 }
398
399 void
400 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
401 {
402 struct intel_context *intel = intel_context(ctx);
403 __DRIcontext *driContext = intel->driContext;
404 void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
405 GLsizei w, GLsizei h);
406
407 if (!driContext->driScreenPriv->dri2.enabled)
408 return;
409
410 if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
411 /* If we're rendering to the fake front buffer, make sure all the pending
412 * drawing has landed on the real front buffer. Otherwise when we
413 * eventually get to DRI2GetBuffersWithFormat the stale real front
414 * buffer contents will get copied to the new fake front buffer.
415 */
416 if (intel->is_front_buffer_rendering) {
417 intel_flush(ctx, GL_FALSE);
418 }
419
420 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
421 if (driContext->driDrawablePriv != driContext->driReadablePriv)
422 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
423 }
424
425 old_viewport = ctx->Driver.Viewport;
426 ctx->Driver.Viewport = NULL;
427 intel->driDrawable = driContext->driDrawablePriv;
428 intelWindowMoved(intel);
429 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
430 ctx->Driver.Viewport = old_viewport;
431 }
432
433
434 static const struct dri_debug_control debug_control[] = {
435 { "tex", DEBUG_TEXTURE},
436 { "state", DEBUG_STATE},
437 { "ioctl", DEBUG_IOCTL},
438 { "blit", DEBUG_BLIT},
439 { "mip", DEBUG_MIPTREE},
440 { "fall", DEBUG_FALLBACKS},
441 { "verb", DEBUG_VERBOSE},
442 { "bat", DEBUG_BATCH},
443 { "pix", DEBUG_PIXEL},
444 { "buf", DEBUG_BUFMGR},
445 { "reg", DEBUG_REGION},
446 { "fbo", DEBUG_FBO},
447 { "lock", DEBUG_LOCK},
448 { "sync", DEBUG_SYNC},
449 { "prim", DEBUG_PRIMS },
450 { "vert", DEBUG_VERTS },
451 { "dri", DEBUG_DRI },
452 { "dma", DEBUG_DMA },
453 { "san", DEBUG_SANITY },
454 { "sleep", DEBUG_SLEEP },
455 { "stats", DEBUG_STATS },
456 { "tile", DEBUG_TILE },
457 { "sing", DEBUG_SINGLE_THREAD },
458 { "thre", DEBUG_SINGLE_THREAD },
459 { "wm", DEBUG_WM },
460 { "urb", DEBUG_URB },
461 { "vs", DEBUG_VS },
462 { NULL, 0 }
463 };
464
465
466 static void
467 intelInvalidateState(GLcontext * ctx, GLuint new_state)
468 {
469 struct intel_context *intel = intel_context(ctx);
470
471 _swrast_InvalidateState(ctx, new_state);
472 _swsetup_InvalidateState(ctx, new_state);
473 _vbo_InvalidateState(ctx, new_state);
474 _tnl_InvalidateState(ctx, new_state);
475 _tnl_invalidate_vertex_state(ctx, new_state);
476
477 intel->NewGLState |= new_state;
478
479 if (intel->vtbl.invalidate_state)
480 intel->vtbl.invalidate_state( intel, new_state );
481 }
482
483 static void
484 intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
485 {
486 struct intel_context *intel = intel_context(ctx);
487
488 if (intel->Fallback)
489 _swrast_flush(ctx);
490
491 if (!IS_965(intel->intelScreen->deviceID))
492 INTEL_FIREVERTICES(intel);
493
494 /* Emit a flush so that any frontbuffer rendering that might have occurred
495 * lands onscreen in a timely manner, even if the X Server doesn't trigger
496 * a flush for us.
497 */
498 if (needs_mi_flush)
499 intel_batchbuffer_emit_mi_flush(intel->batch);
500
501 if (intel->batch->map != intel->batch->ptr)
502 intel_batchbuffer_flush(intel->batch);
503
504 if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
505 __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
506
507 if (screen->dri2.loader &&
508 (screen->dri2.loader->base.version >= 2)
509 && (screen->dri2.loader->flushFrontBuffer != NULL)) {
510 (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
511 intel->driDrawable->loaderPrivate);
512
513 /* Only clear the dirty bit if front-buffer rendering is no longer
514 * enabled. This is done so that the dirty bit can only be set in
515 * glDrawBuffer. Otherwise the dirty bit would have to be set at
516 * each of N places that do rendering. This has worse performances,
517 * but it is much easier to get correct.
518 */
519 if (!intel->is_front_buffer_rendering) {
520 intel->front_buffer_dirty = GL_FALSE;
521 }
522 }
523 }
524 }
525
526 void
527 intelFlush(GLcontext * ctx)
528 {
529 intel_flush(ctx, GL_FALSE);
530 }
531
532 static void
533 intel_glFlush(GLcontext *ctx)
534 {
535 struct intel_context *intel = intel_context(ctx);
536
537 intel_flush(ctx, GL_TRUE);
538
539 /* We're using glFlush as an indicator that a frame is done, which is
540 * what DRI2 does before calling SwapBuffers (and means we should catch
541 * people doing front-buffer rendering, as well)..
542 *
543 * Wait for the swapbuffers before the one we just emitted, so we don't
544 * get too many swaps outstanding for apps that are GPU-heavy but not
545 * CPU-heavy.
546 *
547 * Unfortunately, we don't have a handle to the batch containing the swap,
548 * and getting our hands on that doesn't seem worth it, so we just us the
549 * first batch we emitted after the last swap.
550 */
551 if (intel->first_post_swapbuffers_batch != NULL) {
552 drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
553 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
554 intel->first_post_swapbuffers_batch = NULL;
555 }
556 }
557
558 void
559 intelFinish(GLcontext * ctx)
560 {
561 struct gl_framebuffer *fb = ctx->DrawBuffer;
562 int i;
563
564 intelFlush(ctx);
565
566 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
567 struct intel_renderbuffer *irb;
568
569 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
570
571 if (irb && irb->region)
572 dri_bo_wait_rendering(irb->region->buffer);
573 }
574 if (fb->_DepthBuffer) {
575 /* XXX: Wait on buffer idle */
576 }
577 }
578
579 void
580 intelInitDriverFunctions(struct dd_function_table *functions)
581 {
582 _mesa_init_driver_functions(functions);
583
584 functions->Flush = intel_glFlush;
585 functions->Finish = intelFinish;
586 functions->GetString = intelGetString;
587 functions->UpdateState = intelInvalidateState;
588
589 intelInitTextureFuncs(functions);
590 intelInitTextureImageFuncs(functions);
591 intelInitTextureSubImageFuncs(functions);
592 intelInitTextureCopyImageFuncs(functions);
593 intelInitStateFuncs(functions);
594 intelInitClearFuncs(functions);
595 intelInitBufferFuncs(functions);
596 intelInitPixelFuncs(functions);
597 intelInitBufferObjectFuncs(functions);
598 intel_init_syncobj_functions(functions);
599 }
600
601
602 GLboolean
603 intelInitContext(struct intel_context *intel,
604 const __GLcontextModes * mesaVis,
605 __DRIcontextPrivate * driContextPriv,
606 void *sharedContextPrivate,
607 struct dd_function_table *functions)
608 {
609 GLcontext *ctx = &intel->ctx;
610 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
611 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
612 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
613 int fthrottle_mode;
614
615 if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
616 functions, (void *) intel)) {
617 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
618 return GL_FALSE;
619 }
620
621 driContextPriv->driverPrivate = intel;
622 intel->intelScreen = intelScreen;
623 intel->driScreen = sPriv;
624 intel->sarea = intelScreen->sarea;
625 intel->driContext = driContextPriv;
626
627 /* Dri stuff */
628 intel->hHWContext = driContextPriv->hHWContext;
629 intel->driFd = sPriv->fd;
630 intel->driHwLock = sPriv->lock;
631
632 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
633 intel->driScreen->myNum,
634 IS_965(intelScreen->deviceID) ? "i965" : "i915");
635 if (intelScreen->deviceID == PCI_CHIP_I865_G)
636 intel->maxBatchSize = 4096;
637 else
638 intel->maxBatchSize = BATCH_SZ;
639
640 intel->bufmgr = intelScreen->bufmgr;
641 intel->ttm = intelScreen->ttm;
642 if (intel->ttm) {
643 int bo_reuse_mode;
644
645 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
646 switch (bo_reuse_mode) {
647 case DRI_CONF_BO_REUSE_DISABLED:
648 break;
649 case DRI_CONF_BO_REUSE_ALL:
650 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
651 break;
652 }
653 }
654
655 /* This doesn't yet catch all non-conformant rendering, but it's a
656 * start.
657 */
658 if (getenv("INTEL_STRICT_CONFORMANCE")) {
659 unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
660 if (value > 0) {
661 intel->conformance_mode = value;
662 }
663 else {
664 intel->conformance_mode = 1;
665 }
666 }
667
668 if (intel->conformance_mode > 0) {
669 ctx->Const.MinLineWidth = 1.0;
670 ctx->Const.MinLineWidthAA = 1.0;
671 ctx->Const.MaxLineWidth = 1.0;
672 ctx->Const.MaxLineWidthAA = 1.0;
673 ctx->Const.LineWidthGranularity = 1.0;
674 }
675 else {
676 ctx->Const.MinLineWidth = 1.0;
677 ctx->Const.MinLineWidthAA = 1.0;
678 ctx->Const.MaxLineWidth = 5.0;
679 ctx->Const.MaxLineWidthAA = 5.0;
680 ctx->Const.LineWidthGranularity = 0.5;
681 }
682
683 ctx->Const.MinPointSize = 1.0;
684 ctx->Const.MinPointSizeAA = 1.0;
685 ctx->Const.MaxPointSize = 255.0;
686 ctx->Const.MaxPointSizeAA = 3.0;
687 ctx->Const.PointSizeGranularity = 1.0;
688
689 /* reinitialize the context point state.
690 * It depend on constants in __GLcontextRec::Const
691 */
692 _mesa_init_point(ctx);
693
694 meta_init_metaops(ctx, &intel->meta);
695 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
696 if (IS_965(intelScreen->deviceID)) {
697 if (MAX_WIDTH > 8192)
698 ctx->Const.MaxRenderbufferSize = 8192;
699 } else {
700 if (MAX_WIDTH > 2048)
701 ctx->Const.MaxRenderbufferSize = 2048;
702 }
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 if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
745 _mesa_printf("IRQs not active. Exiting\n");
746 exit(1);
747 }
748
749 intelInitExtensions(ctx, GL_FALSE);
750
751 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
752 if (INTEL_DEBUG & DEBUG_BUFMGR)
753 dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
754
755 if (!sPriv->dri2.enabled)
756 intel_recreate_static_regions(intel);
757
758 intel->batch = intel_batchbuffer_alloc(intel);
759
760 intel_fbo_init(intel);
761
762 if (intel->ctx.Mesa_DXTn) {
763 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
764 _mesa_enable_extension(ctx, "GL_S3_s3tc");
765 }
766 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
767 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
768 }
769 intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
770 "texture_tiling");
771 if (intel->use_texture_tiling &&
772 !intel->intelScreen->kernel_exec_fencing) {
773 fprintf(stderr, "No kernel support for execution fencing, "
774 "disabling texture tiling\n");
775 intel->use_texture_tiling = GL_FALSE;
776 }
777 intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
778
779 intel->prim.primitive = ~0;
780
781 /* Force all software fallbacks */
782 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
783 fprintf(stderr, "disabling 3D rasterization\n");
784 intel->no_rast = 1;
785 }
786
787 if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
788 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
789 intel->always_flush_batch = 1;
790 }
791
792 if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
793 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
794 intel->always_flush_cache = 1;
795 }
796
797 /* Disable all hardware rendering (skip emitting batches and fences/waits
798 * to the kernel)
799 */
800 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
801
802 return GL_TRUE;
803 }
804
805 void
806 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
807 {
808 struct intel_context *intel =
809 (struct intel_context *) driContextPriv->driverPrivate;
810
811 assert(intel); /* should never be null */
812 if (intel) {
813 GLboolean release_texture_heaps;
814
815 INTEL_FIREVERTICES(intel);
816
817 meta_destroy_metaops(&intel->meta);
818
819 intel->vtbl.destroy(intel);
820
821 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
822 _swsetup_DestroyContext(&intel->ctx);
823 _tnl_DestroyContext(&intel->ctx);
824 _vbo_DestroyContext(&intel->ctx);
825
826 _swrast_DestroyContext(&intel->ctx);
827 intel->Fallback = 0; /* don't call _swrast_Flush later */
828
829 intel_batchbuffer_free(intel->batch);
830 intel->batch = NULL;
831
832 free(intel->prim.vb);
833 intel->prim.vb = NULL;
834 dri_bo_unreference(intel->prim.vb_bo);
835 intel->prim.vb_bo = NULL;
836 dri_bo_unreference(intel->first_post_swapbuffers_batch);
837 intel->first_post_swapbuffers_batch = NULL;
838
839 if (release_texture_heaps) {
840 /* Nothing is currently done here to free texture heaps;
841 * but we're not using the texture heap utilities, so I
842 * rather think we shouldn't. I've taken a look, and can't
843 * find any private texture data hanging around anywhere, but
844 * I'm not yet certain there isn't any at all...
845 */
846 /* if (INTEL_DEBUG & DEBUG_TEXTURE)
847 fprintf(stderr, "do something to free texture heaps\n");
848 */
849 }
850
851 /* XXX In intelMakeCurrent() below, the context's static regions are
852 * referenced inside the frame buffer; it's listed as a hack,
853 * with a comment of "XXX FBO temporary fix-ups!", but
854 * as long as it's there, we should release the regions here.
855 * The do/while loop around the block is used to allow the
856 * "continue" statements inside the block to exit the block,
857 * to avoid many layers of "if" constructs.
858 */
859 do {
860 __DRIdrawablePrivate * driDrawPriv = intel->driDrawable;
861 struct intel_framebuffer *intel_fb;
862 struct intel_renderbuffer *irbDepth, *irbStencil;
863 if (!driDrawPriv) {
864 /* We're already detached from the drawable; exit this block. */
865 continue;
866 }
867 intel_fb = (struct intel_framebuffer *) driDrawPriv->driverPrivate;
868 if (!intel_fb) {
869 /* The frame buffer is already gone; exit this block. */
870 continue;
871 }
872 irbDepth = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
873 irbStencil = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
874
875 /* If the regions of the frame buffer still match the regions
876 * of the context, release them. If they've changed somehow,
877 * leave them alone.
878 */
879 if (intel_fb->color_rb[0] && intel_fb->color_rb[0]->region == intel->front_region) {
880 intel_renderbuffer_set_region(intel_fb->color_rb[0], NULL);
881 }
882 if (intel_fb->color_rb[1] && intel_fb->color_rb[1]->region == intel->back_region) {
883 intel_renderbuffer_set_region(intel_fb->color_rb[1], NULL);
884 }
885
886 if (irbDepth && irbDepth->region == intel->depth_region) {
887 intel_renderbuffer_set_region(irbDepth, NULL);
888 }
889 /* Usually, the stencil buffer is the same as the depth buffer;
890 * but they're handled separately in MakeCurrent, so we'll
891 * handle them separately here.
892 */
893 if (irbStencil && irbStencil->region == intel->depth_region) {
894 intel_renderbuffer_set_region(irbStencil, NULL);
895 }
896 } while (0);
897
898 intel_region_release(&intel->front_region);
899 intel_region_release(&intel->back_region);
900 intel_region_release(&intel->depth_region);
901
902 driDestroyOptionCache(&intel->optionCache);
903
904 /* free the Mesa context */
905 _mesa_free_context_data(&intel->ctx);
906
907 FREE(intel);
908 driContextPriv->driverPrivate = NULL;
909 }
910 }
911
912 GLboolean
913 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
914 {
915 struct intel_context *intel =
916 (struct intel_context *) driContextPriv->driverPrivate;
917
918 /* Deassociate the context with the drawables.
919 */
920 intel->driDrawable = NULL;
921 intel->driReadDrawable = NULL;
922
923 return GL_TRUE;
924 }
925
926 GLboolean
927 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
928 __DRIdrawablePrivate * driDrawPriv,
929 __DRIdrawablePrivate * driReadPriv)
930 {
931 __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
932
933 if (driContextPriv) {
934 struct intel_context *intel =
935 (struct intel_context *) driContextPriv->driverPrivate;
936 struct intel_framebuffer *intel_fb =
937 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
938 GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
939
940 if (driContextPriv->driScreenPriv->dri2.enabled) {
941 intel_update_renderbuffers(driContextPriv, driDrawPriv);
942 if (driDrawPriv != driReadPriv)
943 intel_update_renderbuffers(driContextPriv, driReadPriv);
944 } else {
945 /* XXX FBO temporary fix-ups! These are released in
946 * intelDextroyContext(), above. Changes here should be
947 * reflected there.
948 */
949 /* if the renderbuffers don't have regions, init them from the context */
950 struct intel_renderbuffer *irbDepth
951 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
952 struct intel_renderbuffer *irbStencil
953 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
954
955 if (intel_fb->color_rb[0]) {
956 intel_renderbuffer_set_region(intel_fb->color_rb[0],
957 intel->front_region);
958 }
959 if (intel_fb->color_rb[1]) {
960 intel_renderbuffer_set_region(intel_fb->color_rb[1],
961 intel->back_region);
962 }
963
964 if (irbDepth) {
965 intel_renderbuffer_set_region(irbDepth, intel->depth_region);
966 }
967 if (irbStencil) {
968 intel_renderbuffer_set_region(irbStencil, intel->depth_region);
969 }
970 }
971
972 /* set GLframebuffer size to match window, if needed */
973 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
974
975 if (driReadPriv != driDrawPriv) {
976 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
977 }
978
979 _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
980
981 /* The drawbuffer won't always be updated by _mesa_make_current:
982 */
983 if (intel->ctx.DrawBuffer == &intel_fb->Base) {
984
985 if (intel->driReadDrawable != driReadPriv)
986 intel->driReadDrawable = driReadPriv;
987
988 if (intel->driDrawable != driDrawPriv) {
989 if (driDrawPriv->swap_interval == (unsigned)-1) {
990 int i;
991
992 driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
993 ? driGetDefaultVBlankFlags(&intel->optionCache)
994 : VBLANK_FLAG_NO_IRQ;
995
996 /* Prevent error printf if one crtc is disabled, this will
997 * be properly calculated in intelWindowMoved() next.
998 */
999 driDrawPriv->vblFlags = intelFixupVblank(intel, driDrawPriv);
1000
1001 (*psp->systemTime->getUST) (&intel_fb->swap_ust);
1002 driDrawableInitVBlank(driDrawPriv);
1003 intel_fb->vbl_waited = driDrawPriv->vblSeq;
1004
1005 for (i = 0; i < 2; i++) {
1006 if (intel_fb->color_rb[i])
1007 intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
1008 }
1009 }
1010 intel->driDrawable = driDrawPriv;
1011 intelWindowMoved(intel);
1012 }
1013
1014 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
1015 }
1016 }
1017 else {
1018 _mesa_make_current(NULL, NULL, NULL);
1019 }
1020
1021 return GL_TRUE;
1022 }
1023
1024 static void
1025 intelContendedLock(struct intel_context *intel, GLuint flags)
1026 {
1027 __DRIdrawablePrivate *dPriv = intel->driDrawable;
1028 __DRIscreenPrivate *sPriv = intel->driScreen;
1029 volatile drm_i915_sarea_t *sarea = intel->sarea;
1030 int me = intel->hHWContext;
1031
1032 drmGetLock(intel->driFd, intel->hHWContext, flags);
1033
1034 if (INTEL_DEBUG & DEBUG_LOCK)
1035 _mesa_printf("%s - got contended lock\n", __progname);
1036
1037 /* If the window moved, may need to set a new cliprect now.
1038 *
1039 * NOTE: This releases and regains the hw lock, so all state
1040 * checking must be done *after* this call:
1041 */
1042 if (dPriv)
1043 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
1044
1045 if (sarea && sarea->ctxOwner != me) {
1046 if (INTEL_DEBUG & DEBUG_BUFMGR) {
1047 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
1048 sarea->ctxOwner, me);
1049 }
1050 sarea->ctxOwner = me;
1051 }
1052
1053 /* If the last consumer of the texture memory wasn't us, notify the fake
1054 * bufmgr and record the new owner. We should have the memory shared
1055 * between contexts of a single fake bufmgr, but this will at least make
1056 * things correct for now.
1057 */
1058 if (!intel->ttm && sarea->texAge != intel->hHWContext) {
1059 sarea->texAge = intel->hHWContext;
1060 intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
1061 if (INTEL_DEBUG & DEBUG_BATCH)
1062 intel_decode_context_reset();
1063 if (INTEL_DEBUG & DEBUG_BUFMGR)
1064 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
1065 sarea->ctxOwner, intel->hHWContext);
1066 }
1067
1068 /* Drawable changed?
1069 */
1070 if (dPriv && intel->lastStamp != dPriv->lastStamp) {
1071 intelWindowMoved(intel);
1072 intel->lastStamp = dPriv->lastStamp;
1073 }
1074 }
1075
1076
1077 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
1078
1079 /* Lock the hardware and validate our state.
1080 */
1081 void LOCK_HARDWARE( struct intel_context *intel )
1082 {
1083 __DRIdrawable *dPriv = intel->driDrawable;
1084 __DRIscreen *sPriv = intel->driScreen;
1085 char __ret = 0;
1086 struct intel_framebuffer *intel_fb = NULL;
1087 struct intel_renderbuffer *intel_rb = NULL;
1088
1089 intel->locked++;
1090 if (intel->locked >= 2)
1091 return;
1092
1093 if (!sPriv->dri2.enabled)
1094 _glthread_LOCK_MUTEX(lockMutex);
1095
1096 if (intel->driDrawable) {
1097 intel_fb = intel->driDrawable->driverPrivate;
1098
1099 if (intel_fb)
1100 intel_rb =
1101 intel_get_renderbuffer(&intel_fb->Base,
1102 intel_fb->Base._ColorDrawBufferIndexes[0]);
1103 }
1104
1105 if (intel_rb && dPriv->vblFlags &&
1106 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
1107 (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
1108 drmVBlank vbl;
1109
1110 vbl.request.type = DRM_VBLANK_ABSOLUTE;
1111
1112 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
1113 vbl.request.type |= DRM_VBLANK_SECONDARY;
1114 }
1115
1116 vbl.request.sequence = intel_rb->vbl_pending;
1117 drmWaitVBlank(intel->driFd, &vbl);
1118 intel_fb->vbl_waited = vbl.reply.sequence;
1119 }
1120
1121 if (!sPriv->dri2.enabled) {
1122 DRM_CAS(intel->driHwLock, intel->hHWContext,
1123 (DRM_LOCK_HELD|intel->hHWContext), __ret);
1124
1125 if (__ret)
1126 intelContendedLock( intel, 0 );
1127 }
1128
1129
1130 if (INTEL_DEBUG & DEBUG_LOCK)
1131 _mesa_printf("%s - locked\n", __progname);
1132 }
1133
1134
1135 /* Unlock the hardware using the global current context
1136 */
1137 void UNLOCK_HARDWARE( struct intel_context *intel )
1138 {
1139 __DRIscreen *sPriv = intel->driScreen;
1140
1141 intel->locked--;
1142 if (intel->locked > 0)
1143 return;
1144
1145 assert(intel->locked == 0);
1146
1147 if (!sPriv->dri2.enabled) {
1148 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1149 _glthread_UNLOCK_MUTEX(lockMutex);
1150 }
1151
1152 if (INTEL_DEBUG & DEBUG_LOCK)
1153 _mesa_printf("%s - unlocked\n", __progname);
1154
1155 /**
1156 * Nothing should be left in batch outside of LOCK/UNLOCK which references
1157 * cliprects.
1158 */
1159 if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1160 intel_batchbuffer_flush(intel->batch);
1161 }
1162