Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / intel / intel_buffers.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_screen.h"
29 #include "intel_context.h"
30 #include "intel_blit.h"
31 #include "intel_buffers.h"
32 #include "intel_chipset.h"
33 #include "intel_depthstencil.h"
34 #include "intel_fbo.h"
35 #include "intel_regions.h"
36 #include "intel_batchbuffer.h"
37 #include "intel_reg.h"
38 #include "main/context.h"
39 #include "main/framebuffer.h"
40 #include "swrast/swrast.h"
41 #include "utils.h"
42 #include "drirenderbuffer.h"
43 #include "vblank.h"
44 #include "i915_drm.h"
45
46 /* This block can be removed when libdrm >= 2.3.1 is required */
47
48 #ifndef DRM_IOCTL_I915_FLIP
49
50 #define DRM_VBLANK_FLIP 0x8000000
51
52 typedef struct drm_i915_flip {
53 int pipes;
54 } drm_i915_flip_t;
55
56 #undef DRM_IOCTL_I915_FLIP
57 #define DRM_IOCTL_I915_FLIP DRM_IOW(DRM_COMMAND_BASE + DRM_I915_FLIP, \
58 drm_i915_flip_t)
59
60 #endif
61
62 #define FILE_DEBUG_FLAG DEBUG_BLIT
63
64 /**
65 * XXX move this into a new dri/common/cliprects.c file.
66 */
67 GLboolean
68 intel_intersect_cliprects(drm_clip_rect_t * dst,
69 const drm_clip_rect_t * a,
70 const drm_clip_rect_t * b)
71 {
72 GLint bx = b->x1;
73 GLint by = b->y1;
74 GLint bw = b->x2 - bx;
75 GLint bh = b->y2 - by;
76
77 if (bx < a->x1)
78 bw -= a->x1 - bx, bx = a->x1;
79 if (by < a->y1)
80 bh -= a->y1 - by, by = a->y1;
81 if (bx + bw > a->x2)
82 bw = a->x2 - bx;
83 if (by + bh > a->y2)
84 bh = a->y2 - by;
85 if (bw <= 0)
86 return GL_FALSE;
87 if (bh <= 0)
88 return GL_FALSE;
89
90 dst->x1 = bx;
91 dst->y1 = by;
92 dst->x2 = bx + bw;
93 dst->y2 = by + bh;
94
95 return GL_TRUE;
96 }
97
98 /**
99 * Return pointer to current color drawing region, or NULL.
100 */
101 struct intel_region *
102 intel_drawbuf_region(struct intel_context *intel)
103 {
104 struct intel_renderbuffer *irbColor =
105 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0]);
106 if (irbColor)
107 return irbColor->region;
108 else
109 return NULL;
110 }
111
112 /**
113 * Return pointer to current color reading region, or NULL.
114 */
115 struct intel_region *
116 intel_readbuf_region(struct intel_context *intel)
117 {
118 struct intel_renderbuffer *irb
119 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
120 if (irb)
121 return irb->region;
122 else
123 return NULL;
124 }
125
126 void
127 intel_get_cliprects(struct intel_context *intel,
128 struct drm_clip_rect **cliprects,
129 unsigned int *num_cliprects,
130 int *x_off, int *y_off)
131 {
132 __DRIdrawablePrivate *dPriv = intel->driDrawable;
133 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
134
135 if (intel->constant_cliprect) {
136 /* FBO or DRI2 rendering, which can just use the fb's size. */
137 intel->fboRect.x1 = 0;
138 intel->fboRect.y1 = 0;
139 intel->fboRect.x2 = intel->ctx.DrawBuffer->Width;
140 intel->fboRect.y2 = intel->ctx.DrawBuffer->Height;
141
142 *cliprects = &intel->fboRect;
143 *num_cliprects = 1;
144 *x_off = 0;
145 *y_off = 0;
146 } else if (intel->front_cliprects ||
147 intel_fb->pf_active || dPriv->numBackClipRects == 0) {
148 /* use the front clip rects */
149 *cliprects = dPriv->pClipRects;
150 *num_cliprects = dPriv->numClipRects;
151 *x_off = dPriv->x;
152 *y_off = dPriv->y;
153 }
154 else {
155 /* use the back clip rects */
156 *num_cliprects = dPriv->numBackClipRects;
157 *cliprects = dPriv->pBackClipRects;
158 *x_off = dPriv->backX;
159 *y_off = dPriv->backY;
160 }
161 }
162
163 static void
164 intelUpdatePageFlipping(struct intel_context *intel,
165 GLint areaA, GLint areaB)
166 {
167 __DRIdrawablePrivate *dPriv = intel->driDrawable;
168 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
169 GLboolean pf_active;
170 GLint pf_planes;
171
172 /* Update page flipping info */
173 pf_planes = 0;
174
175 if (areaA > 0)
176 pf_planes |= 1;
177
178 if (areaB > 0)
179 pf_planes |= 2;
180
181 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
182 (intel_fb->pf_planes & 0x2)) & 0x3;
183
184 intel_fb->pf_num_pages = 2;
185
186 pf_active = pf_planes && (pf_planes & intel->sarea->pf_active) == pf_planes;
187
188 if (INTEL_DEBUG & DEBUG_LOCK)
189 if (pf_active != intel_fb->pf_active)
190 _mesa_printf("%s - Page flipping %sactive\n", __progname,
191 pf_active ? "" : "in");
192
193 if (pf_active) {
194 /* Sync pages between planes if flipping on both at the same time */
195 if (pf_planes == 0x3 && pf_planes != intel_fb->pf_planes &&
196 (intel->sarea->pf_current_page & 0x3) !=
197 (((intel->sarea->pf_current_page) >> 2) & 0x3)) {
198 drm_i915_flip_t flip;
199
200 if (intel_fb->pf_current_page ==
201 (intel->sarea->pf_current_page & 0x3)) {
202 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
203 * lockups for unknown reasons.
204 */
205 intel->sarea->pf_current_page =
206 intel->sarea->pf_current_page & 0x3;
207 intel->sarea->pf_current_page |=
208 ((intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
209 intel_fb->pf_num_pages) << 2;
210
211 flip.pipes = 0x2;
212 } else {
213 intel->sarea->pf_current_page =
214 intel->sarea->pf_current_page & (0x3 << 2);
215 intel->sarea->pf_current_page |=
216 (intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
217 intel_fb->pf_num_pages;
218
219 flip.pipes = 0x1;
220 }
221
222 drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
223 }
224
225 intel_fb->pf_planes = pf_planes;
226 }
227
228 intel_fb->pf_active = pf_active;
229 intel_flip_renderbuffers(intel_fb);
230 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
231 }
232
233 /**
234 * This will be called whenever the currently bound window is moved/resized.
235 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
236 */
237 void
238 intelWindowMoved(struct intel_context *intel)
239 {
240 GLcontext *ctx = &intel->ctx;
241 __DRIdrawablePrivate *dPriv = intel->driDrawable;
242 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
243
244 if (!intel->intelScreen->driScrnPriv->dri2.enabled &&
245 intel->intelScreen->driScrnPriv->ddx_version.minor >= 7) {
246 volatile struct drm_i915_sarea *sarea = intel->sarea;
247 drm_clip_rect_t drw_rect = { .x1 = dPriv->x, .x2 = dPriv->x + dPriv->w,
248 .y1 = dPriv->y, .y2 = dPriv->y + dPriv->h };
249 drm_clip_rect_t planeA_rect = { .x1 = sarea->planeA_x, .y1 = sarea->planeA_y,
250 .x2 = sarea->planeA_x + sarea->planeA_w,
251 .y2 = sarea->planeA_y + sarea->planeA_h };
252 drm_clip_rect_t planeB_rect = { .x1 = sarea->planeB_x, .y1 = sarea->planeB_y,
253 .x2 = sarea->planeB_x + sarea->planeB_w,
254 .y2 = sarea->planeB_y + sarea->planeB_h };
255 GLint areaA = driIntersectArea( drw_rect, planeA_rect );
256 GLint areaB = driIntersectArea( drw_rect, planeB_rect );
257 GLuint flags = dPriv->vblFlags;
258
259 intelUpdatePageFlipping(intel, areaA, areaB);
260
261 /* Update vblank info
262 */
263 if (areaB > areaA || (areaA == areaB && areaB > 0)) {
264 flags = dPriv->vblFlags | VBLANK_FLAG_SECONDARY;
265 } else {
266 flags = dPriv->vblFlags & ~VBLANK_FLAG_SECONDARY;
267 }
268
269 /* Check to see if we changed pipes */
270 if (flags != dPriv->vblFlags && dPriv->vblFlags &&
271 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ)) {
272 int64_t count;
273 drmVBlank vbl;
274 int i;
275
276 /*
277 * Deal with page flipping
278 */
279 vbl.request.type = DRM_VBLANK_ABSOLUTE;
280
281 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
282 vbl.request.type |= DRM_VBLANK_SECONDARY;
283 }
284
285 for (i = 0; i < intel_fb->pf_num_pages; i++) {
286 if (!intel_fb->color_rb[i] ||
287 (intel_fb->vbl_waited - intel_fb->color_rb[i]->vbl_pending) <=
288 (1<<23))
289 continue;
290
291 vbl.request.sequence = intel_fb->color_rb[i]->vbl_pending;
292 drmWaitVBlank(intel->driFd, &vbl);
293 }
294
295 /*
296 * Update msc_base from old pipe
297 */
298 driDrawableGetMSC32(dPriv->driScreenPriv, dPriv, &count);
299 dPriv->msc_base = count;
300 /*
301 * Then get new vblank_base and vblSeq values
302 */
303 dPriv->vblFlags = flags;
304 driGetCurrentVBlank(dPriv);
305 dPriv->vblank_base = dPriv->vblSeq;
306
307 intel_fb->vbl_waited = dPriv->vblSeq;
308
309 for (i = 0; i < intel_fb->pf_num_pages; i++) {
310 if (intel_fb->color_rb[i])
311 intel_fb->color_rb[i]->vbl_pending = intel_fb->vbl_waited;
312 }
313 }
314 } else {
315 dPriv->vblFlags &= ~VBLANK_FLAG_SECONDARY;
316 }
317
318 /* Update Mesa's notion of window size */
319 driUpdateFramebufferSize(ctx, dPriv);
320 intel_fb->Base.Initialized = GL_TRUE; /* XXX remove someday */
321
322 /* Update hardware scissor */
323 if (ctx->Driver.Scissor != NULL) {
324 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
325 ctx->Scissor.Width, ctx->Scissor.Height);
326 }
327
328 /* Re-calculate viewport related state */
329 if (ctx->Driver.DepthRange != NULL)
330 ctx->Driver.DepthRange( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
331 }
332
333
334
335 /* A true meta version of this would be very simple and additionally
336 * machine independent. Maybe we'll get there one day.
337 */
338 static void
339 intelClearWithTris(struct intel_context *intel, GLbitfield mask)
340 {
341 GLcontext *ctx = &intel->ctx;
342 struct gl_framebuffer *fb = ctx->DrawBuffer;
343 GLuint buf;
344
345 intel->vtbl.install_meta_state(intel);
346
347 /* Back and stencil cliprects are the same. Try and do both
348 * buffers at once:
349 */
350 if (mask & (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH)) {
351 struct intel_region *backRegion =
352 intel_get_rb_region(fb, BUFFER_BACK_LEFT);
353 struct intel_region *depthRegion =
354 intel_get_rb_region(fb, BUFFER_DEPTH);
355
356 intel->vtbl.meta_draw_region(intel, backRegion, depthRegion);
357
358 if (mask & BUFFER_BIT_BACK_LEFT)
359 intel->vtbl.meta_color_mask(intel, GL_TRUE);
360 else
361 intel->vtbl.meta_color_mask(intel, GL_FALSE);
362
363 if (mask & BUFFER_BIT_STENCIL)
364 intel->vtbl.meta_stencil_replace(intel,
365 intel->ctx.Stencil.WriteMask[0],
366 intel->ctx.Stencil.Clear);
367 else
368 intel->vtbl.meta_no_stencil_write(intel);
369
370 if (mask & BUFFER_BIT_DEPTH)
371 intel->vtbl.meta_depth_replace(intel);
372 else
373 intel->vtbl.meta_no_depth_write(intel);
374
375 intel->vtbl.meta_draw_quad(intel,
376 fb->_Xmin,
377 fb->_Xmax,
378 fb->_Ymin,
379 fb->_Ymax,
380 intel->ctx.Depth.Clear,
381 intel->ClearColor8888,
382 0, 0, 0, 0); /* texcoords */
383
384 mask &= ~(BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH);
385 }
386
387 /* clear the remaining (color) renderbuffers */
388 for (buf = 0; buf < BUFFER_COUNT && mask; buf++) {
389 const GLuint bufBit = 1 << buf;
390 if (mask & bufBit) {
391 struct intel_renderbuffer *irbColor =
392 intel_renderbuffer(fb->Attachment[buf].Renderbuffer);
393
394 ASSERT(irbColor);
395
396 intel->vtbl.meta_no_depth_write(intel);
397 intel->vtbl.meta_no_stencil_write(intel);
398 intel->vtbl.meta_color_mask(intel, GL_TRUE);
399 intel->vtbl.meta_draw_region(intel, irbColor->region, NULL);
400
401 intel->vtbl.meta_draw_quad(intel,
402 fb->_Xmin,
403 fb->_Xmax,
404 fb->_Ymin,
405 fb->_Ymax,
406 0, intel->ClearColor8888,
407 0, 0, 0, 0); /* texcoords */
408
409 mask &= ~bufBit;
410 }
411 }
412
413 intel->vtbl.leave_meta_state(intel);
414 }
415
416 static const char *buffer_names[] = {
417 [BUFFER_FRONT_LEFT] = "front",
418 [BUFFER_BACK_LEFT] = "back",
419 [BUFFER_FRONT_RIGHT] = "front right",
420 [BUFFER_BACK_RIGHT] = "back right",
421 [BUFFER_AUX0] = "aux0",
422 [BUFFER_AUX1] = "aux1",
423 [BUFFER_AUX2] = "aux2",
424 [BUFFER_AUX3] = "aux3",
425 [BUFFER_DEPTH] = "depth",
426 [BUFFER_STENCIL] = "stencil",
427 [BUFFER_ACCUM] = "accum",
428 [BUFFER_COLOR0] = "color0",
429 [BUFFER_COLOR1] = "color1",
430 [BUFFER_COLOR2] = "color2",
431 [BUFFER_COLOR3] = "color3",
432 [BUFFER_COLOR4] = "color4",
433 [BUFFER_COLOR5] = "color5",
434 [BUFFER_COLOR6] = "color6",
435 [BUFFER_COLOR7] = "color7",
436 };
437
438 /**
439 * Called by ctx->Driver.Clear.
440 */
441 static void
442 intelClear(GLcontext *ctx, GLbitfield mask)
443 {
444 struct intel_context *intel = intel_context(ctx);
445 const GLuint colorMask = *((GLuint *) & ctx->Color.ColorMask);
446 GLbitfield tri_mask = 0;
447 GLbitfield blit_mask = 0;
448 GLbitfield swrast_mask = 0;
449 struct gl_framebuffer *fb = ctx->DrawBuffer;
450 GLuint i;
451
452 if (0)
453 fprintf(stderr, "%s\n", __FUNCTION__);
454
455 /* HW color buffers (front, back, aux, generic FBO, etc) */
456 if (colorMask == ~0) {
457 /* clear all R,G,B,A */
458 /* XXX FBO: need to check if colorbuffers are software RBOs! */
459 blit_mask |= (mask & BUFFER_BITS_COLOR);
460 }
461 else {
462 /* glColorMask in effect */
463 tri_mask |= (mask & BUFFER_BITS_COLOR);
464 }
465
466 /* HW stencil */
467 if (mask & BUFFER_BIT_STENCIL) {
468 const struct intel_region *stencilRegion
469 = intel_get_rb_region(fb, BUFFER_STENCIL);
470 if (stencilRegion) {
471 /* have hw stencil */
472 if (IS_965(intel->intelScreen->deviceID) ||
473 (ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
474 /* We have to use the 3D engine if we're clearing a partial mask
475 * of the stencil buffer, or if we're on a 965 which has a tiled
476 * depth/stencil buffer in a layout we can't blit to.
477 */
478 tri_mask |= BUFFER_BIT_STENCIL;
479 }
480 else {
481 /* clearing all stencil bits, use blitting */
482 blit_mask |= BUFFER_BIT_STENCIL;
483 }
484 }
485 }
486
487 /* HW depth */
488 if (mask & BUFFER_BIT_DEPTH) {
489 /* clear depth with whatever method is used for stencil (see above) */
490 if (IS_965(intel->intelScreen->deviceID) ||
491 tri_mask & BUFFER_BIT_STENCIL)
492 tri_mask |= BUFFER_BIT_DEPTH;
493 else
494 blit_mask |= BUFFER_BIT_DEPTH;
495 }
496
497 /* SW fallback clearing */
498 swrast_mask = mask & ~tri_mask & ~blit_mask;
499
500 for (i = 0; i < BUFFER_COUNT; i++) {
501 GLuint bufBit = 1 << i;
502 if ((blit_mask | tri_mask) & bufBit) {
503 if (!fb->Attachment[i].Renderbuffer->ClassID) {
504 blit_mask &= ~bufBit;
505 tri_mask &= ~bufBit;
506 swrast_mask |= bufBit;
507 }
508 }
509 }
510
511 if (blit_mask) {
512 if (INTEL_DEBUG & DEBUG_BLIT) {
513 DBG("blit clear:");
514 for (i = 0; i < BUFFER_COUNT; i++) {
515 if (blit_mask & (1 << i))
516 DBG(" %s", buffer_names[i]);
517 }
518 DBG("\n");
519 }
520 intelClearWithBlit(ctx, blit_mask);
521 }
522
523 if (tri_mask) {
524 if (INTEL_DEBUG & DEBUG_BLIT) {
525 DBG("tri clear:");
526 for (i = 0; i < BUFFER_COUNT; i++) {
527 if (tri_mask & (1 << i))
528 DBG(" %s", buffer_names[i]);
529 }
530 DBG("\n");
531 }
532 intelClearWithTris(intel, tri_mask);
533 }
534
535 if (swrast_mask) {
536 if (INTEL_DEBUG & DEBUG_BLIT) {
537 DBG("swrast clear:");
538 for (i = 0; i < BUFFER_COUNT; i++) {
539 if (swrast_mask & (1 << i))
540 DBG(" %s", buffer_names[i]);
541 }
542 DBG("\n");
543 }
544 _swrast_Clear(ctx, swrast_mask);
545 }
546 }
547
548
549 /* Emit wait for pending flips */
550 void
551 intel_wait_flips(struct intel_context *intel)
552 {
553 struct intel_framebuffer *intel_fb =
554 (struct intel_framebuffer *) intel->ctx.DrawBuffer;
555 struct intel_renderbuffer *intel_rb =
556 intel_get_renderbuffer(&intel_fb->Base,
557 intel_fb->Base._ColorDrawBufferIndexes[0] ==
558 BUFFER_FRONT_LEFT ? BUFFER_FRONT_LEFT :
559 BUFFER_BACK_LEFT);
560
561 if (intel->intelScreen->driScrnPriv->dri2.enabled)
562 return;
563
564 if (intel_fb->Base.Name == 0 && intel_rb &&
565 intel_rb->pf_pending == intel_fb->pf_seq) {
566 GLint pf_planes = intel_fb->pf_planes;
567 BATCH_LOCALS;
568
569 /* Wait for pending flips to take effect */
570 BEGIN_BATCH(2, NO_LOOP_CLIPRECTS);
571 OUT_BATCH(pf_planes & 0x1 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP)
572 : 0);
573 OUT_BATCH(pf_planes & 0x2 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_B_FLIP)
574 : 0);
575 ADVANCE_BATCH();
576
577 intel_rb->pf_pending--;
578 }
579 }
580
581
582 /* Flip the front & back buffers
583 */
584 static GLboolean
585 intelPageFlip(const __DRIdrawablePrivate * dPriv)
586 {
587 struct intel_context *intel;
588 int ret;
589 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
590
591 if (INTEL_DEBUG & DEBUG_IOCTL)
592 fprintf(stderr, "%s\n", __FUNCTION__);
593
594 assert(dPriv);
595 assert(dPriv->driContextPriv);
596 assert(dPriv->driContextPriv->driverPrivate);
597
598 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
599
600 if (intel->intelScreen->drmMinor < 9)
601 return GL_FALSE;
602
603 intelFlush(&intel->ctx);
604
605 ret = 0;
606
607 LOCK_HARDWARE(intel);
608
609 if (dPriv->numClipRects && intel_fb->pf_active) {
610 drm_i915_flip_t flip;
611
612 flip.pipes = intel_fb->pf_planes;
613
614 ret = drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
615 }
616
617 UNLOCK_HARDWARE(intel);
618
619 if (ret || !intel_fb->pf_active)
620 return GL_FALSE;
621
622 if (!dPriv->numClipRects) {
623 usleep(10000); /* throttle invisible client 10ms */
624 }
625
626 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
627 (intel_fb->pf_planes & 0x2)) & 0x3;
628
629 if (dPriv->numClipRects != 0) {
630 intel_get_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT)->pf_pending =
631 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->pf_pending =
632 ++intel_fb->pf_seq;
633 }
634
635 intel_flip_renderbuffers(intel_fb);
636 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
637
638 return GL_TRUE;
639 }
640
641 static GLboolean
642 intelScheduleSwap(__DRIdrawablePrivate * dPriv, GLboolean *missed_target)
643 {
644 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
645 unsigned int interval;
646 struct intel_context *intel =
647 intelScreenContext(dPriv->driScreenPriv->private);
648 const intelScreenPrivate *intelScreen = intel->intelScreen;
649 unsigned int target;
650 drm_i915_vblank_swap_t swap;
651 GLboolean ret;
652
653 if (!dPriv->vblFlags ||
654 (dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) ||
655 intelScreen->drmMinor < (intel_fb->pf_active ? 9 : 6))
656 return GL_FALSE;
657
658 interval = driGetVBlankInterval(dPriv);
659
660 swap.seqtype = DRM_VBLANK_ABSOLUTE;
661
662 if (dPriv->vblFlags & VBLANK_FLAG_SYNC) {
663 swap.seqtype |= DRM_VBLANK_NEXTONMISS;
664 } else if (interval == 0)
665 return GL_FALSE;
666
667 swap.drawable = dPriv->hHWDrawable;
668 target = swap.sequence = dPriv->vblSeq + interval;
669
670 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
671 swap.seqtype |= DRM_VBLANK_SECONDARY;
672 }
673
674 LOCK_HARDWARE(intel);
675
676 intel_batchbuffer_flush(intel->batch);
677
678 if ( intel_fb->pf_active ) {
679 swap.seqtype |= DRM_VBLANK_FLIP;
680
681 intel_fb->pf_current_page = (((intel->sarea->pf_current_page >>
682 (intel_fb->pf_planes & 0x2)) & 0x3) + 1) %
683 intel_fb->pf_num_pages;
684 }
685
686 if (!drmCommandWriteRead(intel->driFd, DRM_I915_VBLANK_SWAP, &swap,
687 sizeof(swap))) {
688 dPriv->vblSeq = swap.sequence;
689 swap.sequence -= target;
690 *missed_target = swap.sequence > 0 && swap.sequence <= (1 << 23);
691
692 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->vbl_pending =
693 intel_get_renderbuffer(&intel_fb->Base,
694 BUFFER_FRONT_LEFT)->vbl_pending =
695 dPriv->vblSeq;
696
697 if (swap.seqtype & DRM_VBLANK_FLIP) {
698 intel_flip_renderbuffers(intel_fb);
699 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
700 }
701
702 ret = GL_TRUE;
703 } else {
704 if (swap.seqtype & DRM_VBLANK_FLIP) {
705 intel_fb->pf_current_page = ((intel->sarea->pf_current_page >>
706 (intel_fb->pf_planes & 0x2)) & 0x3) %
707 intel_fb->pf_num_pages;
708 }
709
710 ret = GL_FALSE;
711 }
712
713 UNLOCK_HARDWARE(intel);
714
715 return ret;
716 }
717
718 void
719 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
720 {
721 __DRIscreenPrivate *psp = dPriv->driScreenPriv;
722
723 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
724 GET_CURRENT_CONTEXT(ctx);
725 struct intel_context *intel;
726
727 if (ctx == NULL)
728 return;
729
730 intel = intel_context(ctx);
731
732 if (ctx->Visual.doubleBufferMode) {
733 GLboolean missed_target;
734 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
735 int64_t ust;
736
737 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
738
739 if (!intelScheduleSwap(dPriv, &missed_target)) {
740 driWaitForVBlank(dPriv, &missed_target);
741
742 /*
743 * Update each buffer's vbl_pending so we don't get too out of
744 * sync
745 */
746 intel_get_renderbuffer(&intel_fb->Base,
747 BUFFER_BACK_LEFT)->vbl_pending =
748 intel_get_renderbuffer(&intel_fb->Base,
749 BUFFER_FRONT_LEFT)->vbl_pending =
750 dPriv->vblSeq;
751 if (!intelPageFlip(dPriv)) {
752 intelCopyBuffer(dPriv, NULL);
753 }
754 }
755
756 intel_fb->swap_count++;
757 (*psp->systemTime->getUST) (&ust);
758 if (missed_target) {
759 intel_fb->swap_missed_count++;
760 intel_fb->swap_missed_ust = ust - intel_fb->swap_ust;
761 }
762
763 intel_fb->swap_ust = ust;
764 }
765 drmCommandNone(intel->driFd, DRM_I915_GEM_THROTTLE);
766
767 }
768 else {
769 /* XXX this shouldn't be an error but we can't handle it for now */
770 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
771 }
772 }
773
774 void
775 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
776 {
777 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
778 struct intel_context *intel =
779 (struct intel_context *) dPriv->driContextPriv->driverPrivate;
780 GLcontext *ctx = &intel->ctx;
781
782 if (ctx->Visual.doubleBufferMode) {
783 drm_clip_rect_t rect;
784 rect.x1 = x + dPriv->x;
785 rect.y1 = (dPriv->h - y - h) + dPriv->y;
786 rect.x2 = rect.x1 + w;
787 rect.y2 = rect.y1 + h;
788 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
789 intelCopyBuffer(dPriv, &rect);
790 }
791 }
792 else {
793 /* XXX this shouldn't be an error but we can't handle it for now */
794 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
795 }
796 }
797
798
799 /**
800 * Update the hardware state for drawing into a window or framebuffer object.
801 *
802 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
803 * places within the driver.
804 *
805 * Basically, this needs to be called any time the current framebuffer
806 * changes, the renderbuffers change, or we need to draw into different
807 * color buffers.
808 */
809 void
810 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
811 {
812 struct intel_context *intel = intel_context(ctx);
813 struct intel_region *colorRegions[MAX_DRAW_BUFFERS], *depthRegion = NULL;
814 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
815
816 if (!fb) {
817 /* this can happen during the initial context initialization */
818 return;
819 }
820
821 /* Do this here, note core Mesa, since this function is called from
822 * many places within the driver.
823 */
824 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
825 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
826 _mesa_update_framebuffer(ctx);
827 /* this updates the DrawBuffer's Width/Height if it's a FBO */
828 _mesa_update_draw_buffer_bounds(ctx);
829 }
830
831 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
832 /* this may occur when we're called by glBindFrameBuffer() during
833 * the process of someone setting up renderbuffers, etc.
834 */
835 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
836 return;
837 }
838
839 if (fb->Name)
840 intel_validate_paired_depth_stencil(ctx, fb);
841
842 /*
843 * How many color buffers are we drawing into?
844 */
845 if (fb->_NumColorDrawBuffers == 0) {
846 /* writing to 0 */
847 colorRegions[0] = NULL;
848 intel->constant_cliprect = GL_TRUE;
849 } else if (fb->_NumColorDrawBuffers > 1) {
850 int i;
851 struct intel_renderbuffer *irb;
852
853 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
854 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
855 colorRegions[i] = irb ? irb->region : NULL;
856 }
857 intel->constant_cliprect = GL_TRUE;
858 }
859 else {
860 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
861 * into, and set up cliprects if it's .
862 */
863 if (fb->Name == 0) {
864 intel->constant_cliprect = intel->driScreen->dri2.enabled;
865 /* drawing to window system buffer */
866 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
867 if (!intel->constant_cliprect && !intel->front_cliprects)
868 intel_batchbuffer_flush(intel->batch);
869 intel->front_cliprects = GL_TRUE;
870 colorRegions[0] = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
871 }
872 else {
873 if (!intel->constant_cliprect && intel->front_cliprects)
874 intel_batchbuffer_flush(intel->batch);
875 intel->front_cliprects = GL_FALSE;
876 colorRegions[0]= intel_get_rb_region(fb, BUFFER_BACK_LEFT);
877 }
878 }
879 else {
880 /* drawing to user-created FBO */
881 struct intel_renderbuffer *irb;
882 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
883 colorRegions[0] = (irb && irb->region) ? irb->region : NULL;
884 intel->constant_cliprect = GL_TRUE;
885 }
886 }
887
888 /* Update culling direction which changes depending on the
889 * orientation of the buffer:
890 */
891 if (ctx->Driver.FrontFace)
892 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
893 else
894 ctx->NewState |= _NEW_POLYGON;
895
896 if (!colorRegions[0]) {
897 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
898 }
899 else {
900 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
901 }
902
903 /***
904 *** Get depth buffer region and check if we need a software fallback.
905 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
906 ***/
907 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
908 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
909 if (irbDepth && irbDepth->region) {
910 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
911 depthRegion = irbDepth->region;
912 }
913 else {
914 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
915 depthRegion = NULL;
916 }
917 }
918 else {
919 /* not using depth buffer */
920 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
921 depthRegion = NULL;
922 }
923
924 /***
925 *** Stencil buffer
926 *** This can only be hardware accelerated if we're using a
927 *** combined DEPTH_STENCIL buffer (for now anyway).
928 ***/
929 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
930 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
931 if (irbStencil && irbStencil->region) {
932 ASSERT(irbStencil->Base._ActualFormat == GL_DEPTH24_STENCIL8_EXT);
933 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
934 /* need to re-compute stencil hw state */
935 if (ctx->Driver.Enable != NULL)
936 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
937 else
938 ctx->NewState |= _NEW_STENCIL;
939 if (!depthRegion)
940 depthRegion = irbStencil->region;
941 }
942 else {
943 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
944 }
945 }
946 else {
947 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
948 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
949 /* need to re-compute stencil hw state */
950 if (ctx->Driver.Enable != NULL)
951 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
952 else
953 ctx->NewState |= _NEW_STENCIL;
954 }
955
956 /*
957 * Update depth test state
958 */
959 if (ctx->Driver.Enable) {
960 if (ctx->Depth.Test && fb->Visual.depthBits > 0) {
961 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_TRUE);
962 } else {
963 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_FALSE);
964 }
965 } else {
966 ctx->NewState |= _NEW_DEPTH;
967 }
968
969 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
970 fb->_NumColorDrawBuffers);
971
972 /* update viewport since it depends on window size */
973 if (ctx->Driver.Viewport) {
974 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
975 ctx->Viewport.Width, ctx->Viewport.Height);
976 } else {
977 ctx->NewState |= _NEW_VIEWPORT;
978 }
979
980 /* Set state we know depends on drawable parameters:
981 */
982 if (ctx->Driver.Scissor)
983 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
984 ctx->Scissor.Width, ctx->Scissor.Height);
985 intel->NewGLState |= _NEW_SCISSOR;
986
987 if (ctx->Driver.DepthRange)
988 ctx->Driver.DepthRange(ctx,
989 ctx->Viewport.Near,
990 ctx->Viewport.Far);
991 }
992
993
994 static void
995 intelDrawBuffer(GLcontext * ctx, GLenum mode)
996 {
997 intel_draw_buffer(ctx, ctx->DrawBuffer);
998 }
999
1000
1001 static void
1002 intelReadBuffer(GLcontext * ctx, GLenum mode)
1003 {
1004 if (ctx->ReadBuffer == ctx->DrawBuffer) {
1005 /* This will update FBO completeness status.
1006 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1007 * refers to a missing renderbuffer. Calling glReadBuffer can set
1008 * that straight and can make the drawing buffer complete.
1009 */
1010 intel_draw_buffer(ctx, ctx->DrawBuffer);
1011 }
1012 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1013 * reference ctx->ReadBuffer and do appropriate state checks.
1014 */
1015 }
1016
1017
1018 void
1019 intelInitBufferFuncs(struct dd_function_table *functions)
1020 {
1021 functions->Clear = intelClear;
1022 functions->DrawBuffer = intelDrawBuffer;
1023 functions->ReadBuffer = intelReadBuffer;
1024 }