change back driver change from previous commit
[mesa.git] / src / mesa / drivers / dri / i915tex / 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_buffers.h"
52 #include "intel_tex.h"
53 #include "intel_span.h"
54 #include "intel_tris.h"
55 #include "intel_ioctl.h"
56 #include "intel_batchbuffer.h"
57 #include "intel_blit.h"
58 #include "intel_pixel.h"
59 #include "intel_regions.h"
60 #include "intel_buffer_objects.h"
61 #include "intel_fbo.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_ARB_multisample
72 #define need_GL_ARB_point_parameters
73 #define need_GL_ARB_texture_compression
74 #define need_GL_ARB_vertex_buffer_object
75 #define need_GL_ARB_vertex_program
76 #define need_GL_ARB_window_pos
77 #define need_GL_EXT_blend_color
78 #define need_GL_EXT_blend_equation_separate
79 #define need_GL_EXT_blend_func_separate
80 #define need_GL_EXT_blend_minmax
81 #define need_GL_EXT_cull_vertex
82 #define need_GL_EXT_fog_coord
83 #define need_GL_EXT_framebuffer_object
84 #define need_GL_EXT_multi_draw_arrays
85 #define need_GL_EXT_secondary_color
86 #define need_GL_NV_vertex_program
87 #include "extension_helper.h"
88
89
90 #define DRIVER_DATE "20061102"
91
92 _glthread_Mutex lockMutex;
93 static GLboolean lockMutexInit = GL_FALSE;
94
95
96 static const GLubyte *
97 intelGetString(GLcontext * ctx, GLenum name)
98 {
99 const char *chipset;
100 static char buffer[128];
101
102 switch (name) {
103 case GL_VENDOR:
104 return (GLubyte *) "Tungsten Graphics, Inc";
105 break;
106
107 case GL_RENDERER:
108 switch (intel_context(ctx)->intelScreen->deviceID) {
109 case PCI_CHIP_845_G:
110 chipset = "Intel(R) 845G";
111 break;
112 case PCI_CHIP_I830_M:
113 chipset = "Intel(R) 830M";
114 break;
115 case PCI_CHIP_I855_GM:
116 chipset = "Intel(R) 852GM/855GM";
117 break;
118 case PCI_CHIP_I865_G:
119 chipset = "Intel(R) 865G";
120 break;
121 case PCI_CHIP_I915_G:
122 chipset = "Intel(R) 915G";
123 break;
124 case PCI_CHIP_I915_GM:
125 chipset = "Intel(R) 915GM";
126 break;
127 case PCI_CHIP_I945_G:
128 chipset = "Intel(R) 945G";
129 break;
130 case PCI_CHIP_I945_GM:
131 chipset = "Intel(R) 945GM";
132 break;
133 case PCI_CHIP_I945_GME:
134 chipset = "Intel(R) 945GME";
135 break;
136 case PCI_CHIP_G33_G:
137 chipset = "Intel(R) G33";
138 break;
139 case PCI_CHIP_Q35_G:
140 chipset = "Intel(R) Q35";
141 break;
142 case PCI_CHIP_Q33_G:
143 chipset = "Intel(R) Q33";
144 break;
145 default:
146 chipset = "Unknown Intel Chipset";
147 break;
148 }
149
150 (void) driGetRendererString(buffer, chipset, DRIVER_DATE, 0);
151 return (GLubyte *) buffer;
152
153 default:
154 return NULL;
155 }
156 }
157
158
159 /**
160 * Extension strings exported by the intel driver.
161 *
162 * \note
163 * It appears that ARB_texture_env_crossbar has "disappeared" compared to the
164 * old i830-specific driver.
165 */
166 const struct dri_extension card_extensions[] = {
167 {"GL_ARB_multisample", GL_ARB_multisample_functions},
168 {"GL_ARB_multitexture", NULL},
169 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
170 {"GL_ARB_texture_border_clamp", NULL},
171 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions},
172 {"GL_ARB_texture_cube_map", NULL},
173 {"GL_ARB_texture_env_add", NULL},
174 {"GL_ARB_texture_env_combine", NULL},
175 {"GL_ARB_texture_env_dot3", NULL},
176 {"GL_ARB_texture_mirrored_repeat", NULL},
177 {"GL_ARB_texture_rectangle", NULL},
178 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
179 {"GL_ARB_pixel_buffer_object", NULL},
180 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
181 {"GL_ARB_window_pos", GL_ARB_window_pos_functions},
182 {"GL_EXT_blend_color", GL_EXT_blend_color_functions},
183 {"GL_EXT_blend_equation_separate",
184 GL_EXT_blend_equation_separate_functions},
185 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
186 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
187 {"GL_EXT_blend_subtract", NULL},
188 {"GL_EXT_cull_vertex", GL_EXT_cull_vertex_functions},
189 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions},
190 {"GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions},
191 {"GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions},
192 #if 1 /* XXX FBO temporary? */
193 {"GL_EXT_packed_depth_stencil", NULL},
194 #endif
195 {"GL_EXT_pixel_buffer_object", NULL},
196 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
197 {"GL_EXT_stencil_wrap", NULL},
198 {"GL_EXT_texture_edge_clamp", NULL},
199 {"GL_EXT_texture_env_combine", NULL},
200 {"GL_EXT_texture_env_dot3", NULL},
201 {"GL_EXT_texture_filter_anisotropic", NULL},
202 {"GL_EXT_texture_lod_bias", NULL},
203 {"GL_3DFX_texture_compression_FXT1", NULL},
204 {"GL_APPLE_client_storage", NULL},
205 {"GL_MESA_pack_invert", NULL},
206 {"GL_MESA_ycbcr_texture", NULL},
207 {"GL_NV_blend_square", NULL},
208 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
209 {"GL_NV_vertex_program1_1", NULL},
210 /* { "GL_SGIS_generate_mipmap", NULL }, */
211 {NULL, NULL}
212 };
213
214 extern const struct tnl_pipeline_stage _intel_render_stage;
215
216 static const struct tnl_pipeline_stage *intel_pipeline[] = {
217 &_tnl_vertex_transform_stage,
218 &_tnl_vertex_cull_stage,
219 &_tnl_normal_transform_stage,
220 &_tnl_lighting_stage,
221 &_tnl_fog_coordinate_stage,
222 &_tnl_texgen_stage,
223 &_tnl_texture_transform_stage,
224 &_tnl_point_attenuation_stage,
225 &_tnl_vertex_program_stage,
226 #if 1
227 &_intel_render_stage, /* ADD: unclipped rastersetup-to-dma */
228 #endif
229 &_tnl_render_stage,
230 0,
231 };
232
233
234 static const struct dri_debug_control debug_control[] = {
235 {"tex", DEBUG_TEXTURE},
236 {"state", DEBUG_STATE},
237 {"ioctl", DEBUG_IOCTL},
238 {"blit", DEBUG_BLIT},
239 {"mip", DEBUG_MIPTREE},
240 {"fall", DEBUG_FALLBACKS},
241 {"verb", DEBUG_VERBOSE},
242 {"bat", DEBUG_BATCH},
243 {"pix", DEBUG_PIXEL},
244 {"buf", DEBUG_BUFMGR},
245 {"reg", DEBUG_REGION},
246 {"fbo", DEBUG_FBO},
247 {"lock", DEBUG_LOCK},
248 {NULL, 0}
249 };
250
251
252 static void
253 intelInvalidateState(GLcontext * ctx, GLuint new_state)
254 {
255 _swrast_InvalidateState(ctx, new_state);
256 _swsetup_InvalidateState(ctx, new_state);
257 _vbo_InvalidateState(ctx, new_state);
258 _tnl_InvalidateState(ctx, new_state);
259 _tnl_invalidate_vertex_state(ctx, new_state);
260 intel_context(ctx)->NewGLState |= new_state;
261 }
262
263
264 void
265 intelFlush(GLcontext * ctx)
266 {
267 struct intel_context *intel = intel_context(ctx);
268
269 if (intel->Fallback)
270 _swrast_flush(ctx);
271
272 INTEL_FIREVERTICES(intel);
273
274 if (intel->batch->map != intel->batch->ptr)
275 intel_batchbuffer_flush(intel->batch);
276
277 /* XXX: Need to do an MI_FLUSH here.
278 */
279 }
280
281
282 /**
283 * Check if we need to rotate/warp the front color buffer to the
284 * rotated screen. We generally need to do this when we get a glFlush
285 * or glFinish after drawing to the front color buffer.
286 * If no rotation, just copy the private fake front buffer to the real one.
287 */
288 static void
289 intelCheckFrontUpdate(GLcontext * ctx)
290 {
291 struct intel_context *intel = intel_context(ctx);
292 /* rely on _ColorDrawBufferMask being kept up to date by mesa
293 even for window-fbos. */
294 /* not sure. Might need that for all masks including
295 BUFFER_BIT_FRONT_LEFT maybe? */
296 if (intel->ctx.DrawBuffer->_ColorDrawBufferMask[0] ==
297 BUFFER_BIT_FRONT_LEFT) {
298 intelScreenPrivate *screen = intel->intelScreen;
299 __DRIdrawablePrivate *dPriv = intel->driDrawable;
300 if (screen->current_rotation != 0) {
301 intelRotateWindow(intel, dPriv, BUFFER_BIT_FRONT_LEFT);
302 }
303 else {
304 intelCopyBuffer(dPriv, NULL);
305 }
306 }
307 }
308
309
310 /**
311 * Called via glFlush.
312 */
313 static void
314 intelglFlush(GLcontext * ctx)
315 {
316 intelFlush(ctx);
317 intelCheckFrontUpdate(ctx);
318 }
319
320 void
321 intelFinish(GLcontext * ctx)
322 {
323 struct intel_context *intel = intel_context(ctx);
324 intelFlush(ctx);
325 if (intel->batch->last_fence) {
326 driFenceFinish(intel->batch->last_fence,
327 0, GL_FALSE);
328 driFenceUnReference(intel->batch->last_fence);
329 intel->batch->last_fence = NULL;
330 }
331 intelCheckFrontUpdate(ctx);
332 }
333
334
335 void
336 intelInitDriverFunctions(struct dd_function_table *functions)
337 {
338 _mesa_init_driver_functions(functions);
339
340 functions->Flush = intelglFlush;
341 functions->Finish = intelFinish;
342 functions->GetString = intelGetString;
343 functions->UpdateState = intelInvalidateState;
344 functions->CopyColorTable = _swrast_CopyColorTable;
345 functions->CopyColorSubTable = _swrast_CopyColorSubTable;
346 functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
347 functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
348
349 intelInitTextureFuncs(functions);
350 intelInitPixelFuncs(functions);
351 intelInitStateFuncs(functions);
352 intelInitBufferFuncs(functions);
353 }
354
355
356 GLboolean
357 intelInitContext(struct intel_context *intel,
358 const __GLcontextModes * mesaVis,
359 __DRIcontextPrivate * driContextPriv,
360 void *sharedContextPrivate,
361 struct dd_function_table *functions)
362 {
363 GLcontext *ctx = &intel->ctx;
364 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
365 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
366 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
367 drmI830Sarea *saPriv = (drmI830Sarea *)
368 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
369 int fthrottle_mode;
370 GLboolean havePools;
371
372 DRM_LIGHT_LOCK(sPriv->fd, &sPriv->pSAREA->lock, driContextPriv->hHWContext);
373 havePools = intelCreatePools(intelScreen);
374 DRM_UNLOCK(sPriv->fd, &sPriv->pSAREA->lock, driContextPriv->hHWContext);
375
376 if (!havePools)
377 return GL_FALSE;
378
379 if (!_mesa_initialize_context(&intel->ctx,
380 mesaVis, shareCtx,
381 functions, (void *) intel))
382 return GL_FALSE;
383
384 driContextPriv->driverPrivate = intel;
385 intel->intelScreen = intelScreen;
386 intel->driScreen = sPriv;
387 intel->sarea = saPriv;
388
389 intel->width = intelScreen->width;
390 intel->height = intelScreen->height;
391 intel->current_rotation = intelScreen->current_rotation;
392
393 if (!lockMutexInit) {
394 lockMutexInit = GL_TRUE;
395 _glthread_INIT_MUTEX(lockMutex);
396 }
397
398 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
399 intel->driScreen->myNum, "i915");
400
401 ctx->Const.MaxTextureMaxAnisotropy = 2.0;
402
403 /* This doesn't yet catch all non-conformant rendering, but it's a
404 * start.
405 */
406 if (getenv("INTEL_STRICT_CONFORMANCE")) {
407 intel->strict_conformance = 1;
408 }
409
410 ctx->Const.MinLineWidth = 1.0;
411 ctx->Const.MinLineWidthAA = 1.0;
412 ctx->Const.MaxLineWidth = 3.0;
413 ctx->Const.MaxLineWidthAA = 3.0;
414 ctx->Const.LineWidthGranularity = 1.0;
415
416 ctx->Const.MinPointSize = 1.0;
417 ctx->Const.MinPointSizeAA = 1.0;
418 ctx->Const.MaxPointSize = 255.0;
419 ctx->Const.MaxPointSizeAA = 3.0;
420 ctx->Const.PointSizeGranularity = 1.0;
421
422 /* reinitialize the context point state.
423 * It depend on constants in __GLcontextRec::Const
424 */
425 _mesa_init_point(ctx);
426
427 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
428
429 /* Initialize the software rasterizer and helper modules. */
430 _swrast_CreateContext(ctx);
431 _vbo_CreateContext(ctx);
432 _tnl_CreateContext(ctx);
433 _swsetup_CreateContext(ctx);
434
435 /* Install the customized pipeline: */
436 _tnl_destroy_pipeline(ctx);
437 _tnl_install_pipeline(ctx, intel_pipeline);
438
439 /* Configure swrast to match hardware characteristics: */
440 _swrast_allow_pixel_fog(ctx, GL_FALSE);
441 _swrast_allow_vertex_fog(ctx, GL_TRUE);
442
443 /* Dri stuff */
444 intel->hHWContext = driContextPriv->hHWContext;
445 intel->driFd = sPriv->fd;
446 intel->driHwLock = (drmLock *) & sPriv->pSAREA->lock;
447
448 intel->hw_stipple = 1;
449
450 /* XXX FBO: this doesn't seem to be used anywhere */
451 switch (mesaVis->depthBits) {
452 case 0: /* what to do in this case? */
453 case 16:
454 intel->polygon_offset_scale = 1.0 / 0xffff;
455 break;
456 case 24:
457 intel->polygon_offset_scale = 2.0 / 0xffffff; /* req'd to pass glean */
458 break;
459 default:
460 assert(0);
461 break;
462 }
463
464 /* Initialize swrast, tnl driver tables: */
465 intelInitSpanFuncs(ctx);
466 intelInitTriFuncs(ctx);
467
468
469 intel->RenderIndex = ~0;
470
471 fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
472 intel->iw.irq_seq = -1;
473 intel->irqsEmitted = 0;
474
475 intel->do_irqs = (intel->intelScreen->irq_active &&
476 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
477
478 intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
479
480 _math_matrix_ctr(&intel->ViewportMatrix);
481
482 /* Disable imaging extension until convolution is working in
483 * teximage paths:
484 */
485 driInitExtensions(ctx, card_extensions,
486 /* GL_TRUE, */
487 GL_FALSE);
488
489
490 intel->batch = intel_batchbuffer_alloc(intel);
491 intel->last_swap_fence = NULL;
492 intel->first_swap_fence = NULL;
493
494 intel_bufferobj_init(intel);
495 intel_fbo_init(intel);
496
497 if (intel->ctx.Mesa_DXTn) {
498 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
499 _mesa_enable_extension(ctx, "GL_S3_s3tc");
500 }
501 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
502 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
503 }
504
505 intel->prim.primitive = ~0;
506
507
508 #if DO_DEBUG
509 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
510 #endif
511
512 if (getenv("INTEL_NO_RAST")) {
513 fprintf(stderr, "disabling 3D rasterization\n");
514 FALLBACK(intel, INTEL_FALLBACK_USER, 1);
515 }
516
517 return GL_TRUE;
518 }
519
520 void
521 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
522 {
523 struct intel_context *intel =
524 (struct intel_context *) driContextPriv->driverPrivate;
525
526 assert(intel); /* should never be null */
527 if (intel) {
528 GLboolean release_texture_heaps;
529
530 INTEL_FIREVERTICES(intel);
531
532 intel->vtbl.destroy(intel);
533
534 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
535 _swsetup_DestroyContext(&intel->ctx);
536 _tnl_DestroyContext(&intel->ctx);
537 _vbo_DestroyContext(&intel->ctx);
538
539 _swrast_DestroyContext(&intel->ctx);
540 intel->Fallback = 0; /* don't call _swrast_Flush later */
541
542 intel_batchbuffer_free(intel->batch);
543
544 if (intel->last_swap_fence) {
545 driFenceFinish(intel->last_swap_fence, DRM_FENCE_TYPE_EXE, GL_TRUE);
546 driFenceUnReference(intel->last_swap_fence);
547 intel->last_swap_fence = NULL;
548 }
549 if (intel->first_swap_fence) {
550 driFenceFinish(intel->first_swap_fence, DRM_FENCE_TYPE_EXE, GL_TRUE);
551 driFenceUnReference(intel->first_swap_fence);
552 intel->first_swap_fence = NULL;
553 }
554
555
556 if (release_texture_heaps) {
557 /* This share group is about to go away, free our private
558 * texture object data.
559 */
560 if (INTEL_DEBUG & DEBUG_TEXTURE)
561 fprintf(stderr, "do something to free texture heaps\n");
562 }
563
564 /* free the Mesa context */
565 _mesa_free_context_data(&intel->ctx);
566 }
567 }
568
569 GLboolean
570 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
571 {
572 return GL_TRUE;
573 }
574
575 GLboolean
576 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
577 __DRIdrawablePrivate * driDrawPriv,
578 __DRIdrawablePrivate * driReadPriv)
579 {
580
581 #if 0
582 if (driDrawPriv) {
583 fprintf(stderr, "x %d, y %d, width %d, height %d\n",
584 driDrawPriv->x, driDrawPriv->y, driDrawPriv->w, driDrawPriv->h);
585 }
586 #endif
587
588 if (driContextPriv) {
589 struct intel_context *intel =
590 (struct intel_context *) driContextPriv->driverPrivate;
591 struct intel_framebuffer *intel_fb =
592 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
593 GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
594
595 /* this is a hack so we have a valid context when the region allocation
596 is done. Need a per-screen context? */
597 intel->intelScreen->dummyctxptr = intel;
598
599 /* XXX FBO temporary fix-ups! */
600 /* if the renderbuffers don't have regions, init them from the context */
601 {
602 struct intel_renderbuffer *irbDepth
603 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
604 struct intel_renderbuffer *irbStencil
605 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
606
607 if (intel_fb->color_rb[0] && !intel_fb->color_rb[0]->region) {
608 intel_region_reference(&intel_fb->color_rb[0]->region,
609 intel->intelScreen->front_region);
610 }
611 if (intel_fb->color_rb[1] && !intel_fb->color_rb[1]->region) {
612 intel_region_reference(&intel_fb->color_rb[1]->region,
613 intel->intelScreen->back_region);
614 }
615 if (intel_fb->color_rb[2] && !intel_fb->color_rb[2]->region) {
616 intel_region_reference(&intel_fb->color_rb[2]->region,
617 intel->intelScreen->third_region);
618 }
619 if (irbDepth && !irbDepth->region) {
620 intel_region_reference(&irbDepth->region, intel->intelScreen->depth_region);
621 }
622 if (irbStencil && !irbStencil->region) {
623 intel_region_reference(&irbStencil->region, intel->intelScreen->depth_region);
624 }
625 }
626
627 /* update GLframebuffer size to match window if needed */
628 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
629
630 if (driReadPriv != driDrawPriv) {
631 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
632 }
633
634 _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
635
636 /* The drawbuffer won't always be updated by _mesa_make_current:
637 */
638 if (intel->ctx.DrawBuffer == &intel_fb->Base) {
639
640 if (intel->driDrawable != driDrawPriv) {
641 intel_fb->vblank_flags = (intel->intelScreen->irq_active != 0)
642 ? driGetDefaultVBlankFlags(&intel->optionCache)
643 : VBLANK_FLAG_NO_IRQ;
644 (*dri_interface->getUST) (&intel_fb->swap_ust);
645 driDrawableInitVBlank(driDrawPriv, intel_fb->vblank_flags,
646 &intel_fb->vbl_seq);
647 intel->driDrawable = driDrawPriv;
648 intelWindowMoved(intel);
649 intel->lastStamp = driDrawPriv->lastStamp;
650 }
651 else if (intel->lastStamp != driDrawPriv->lastStamp) {
652 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
653 }
654 }
655 }
656 else {
657 _mesa_make_current(NULL, NULL, NULL);
658 }
659
660 return GL_TRUE;
661 }
662
663 static void
664 intelContendedLock(struct intel_context *intel, GLuint flags)
665 {
666 __DRIdrawablePrivate *dPriv = intel->driDrawable;
667 __DRIscreenPrivate *sPriv = intel->driScreen;
668 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
669 drmI830Sarea *sarea = intel->sarea;
670
671 drmGetLock(intel->driFd, intel->hHWContext, flags);
672
673 if (INTEL_DEBUG & DEBUG_LOCK)
674 _mesa_printf("%s - got contended lock\n", __progname);
675
676 /* If the window moved, may need to set a new cliprect now.
677 *
678 * NOTE: This releases and regains the hw lock, so all state
679 * checking must be done *after* this call:
680 */
681 if (dPriv)
682 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
683
684 if (sarea->width != intelScreen->width ||
685 sarea->height != intelScreen->height ||
686 sarea->rotation != intelScreen->current_rotation) {
687
688 intelUpdateScreenRotation(sPriv, sarea);
689 }
690
691 #if 0
692 if (sarea->width != intel->width ||
693 sarea->height != intel->height ||
694 sarea->rotation != intel->current_rotation) {
695
696 void *batchMap = intel->batch->map;
697
698 /*
699 * FIXME: Really only need to do this when drawing to a
700 * common back- or front buffer.
701 */
702
703 /*
704 * This will drop the outstanding batchbuffer on the floor
705 */
706
707 if (batchMap != NULL) {
708 driBOUnmap(intel->batch->buffer);
709 intel->batch->map = NULL;
710 }
711
712 intel_batchbuffer_reset(intel->batch);
713
714 if (batchMap == NULL) {
715 driBOUnmap(intel->batch->buffer);
716 intel->batch->map = NULL;
717 }
718
719 /* lose all primitives */
720 intel->prim.primitive = ~0;
721 intel->prim.start_ptr = 0;
722 intel->prim.flush = 0;
723
724 /* re-emit all state */
725 intel->vtbl.lost_hardware(intel);
726
727 /* force window update */
728 intel->lastStamp = 0;
729
730 intel->width = sarea->width;
731 intel->height = sarea->height;
732 intel->current_rotation = sarea->rotation;
733 }
734 #endif
735 }
736
737
738
739 /* Lock the hardware and validate our state.
740 */
741 void LOCK_HARDWARE( struct intel_context *intel )
742 {
743 char __ret=0;
744 struct intel_framebuffer *intel_fb = NULL;
745 struct intel_renderbuffer *intel_rb = NULL;
746 _glthread_LOCK_MUTEX(lockMutex);
747 assert(!intel->locked);
748
749 if (intel->driDrawable) {
750 intel_fb = intel->driDrawable->driverPrivate;
751
752 if (intel_fb)
753 intel_rb =
754 intel_get_renderbuffer(&intel_fb->Base,
755 intel_fb->Base._ColorDrawBufferMask[0] ==
756 BUFFER_BIT_FRONT_LEFT ? BUFFER_FRONT_LEFT :
757 BUFFER_BACK_LEFT);
758 }
759
760 if (intel_rb && (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
761 drmVBlank vbl;
762
763 vbl.request.type = DRM_VBLANK_ABSOLUTE;
764
765 if ( intel_fb->vblank_flags & VBLANK_FLAG_SECONDARY ) {
766 vbl.request.type |= DRM_VBLANK_SECONDARY;
767 }
768
769 vbl.request.sequence = intel_rb->vbl_pending;
770 drmWaitVBlank(intel->driFd, &vbl);
771 intel_fb->vbl_waited = vbl.reply.sequence;
772 }
773
774 DRM_CAS(intel->driHwLock, intel->hHWContext,
775 (DRM_LOCK_HELD|intel->hHWContext), __ret);
776
777 if (__ret)
778 intelContendedLock( intel, 0 );
779
780 if (INTEL_DEBUG & DEBUG_LOCK)
781 _mesa_printf("%s - locked\n", __progname);
782
783 intel->locked = 1;
784 }
785
786
787 /* Unlock the hardware using the global current context
788 */
789 void UNLOCK_HARDWARE( struct intel_context *intel )
790 {
791 intel->locked = 0;
792
793 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
794
795 _glthread_UNLOCK_MUTEX(lockMutex);
796
797 if (INTEL_DEBUG & DEBUG_LOCK)
798 _mesa_printf("%s - unlocked\n", __progname);
799 }
800