i915tex: The intended triple buffering fix.
[mesa.git] / src / mesa / drivers / dri / i915tex / 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_depthstencil.h"
33 #include "intel_fbo.h"
34 #include "intel_tris.h"
35 #include "intel_regions.h"
36 #include "intel_batchbuffer.h"
37 #include "intel_reg.h"
38 #include "context.h"
39 #include "utils.h"
40 #include "drirenderbuffer.h"
41 #include "framebuffer.h"
42 #include "swrast/swrast.h"
43 #include "vblank.h"
44
45
46 /* This block can be removed when libdrm >= 2.3.1 is required */
47
48 #ifndef DRM_VBLANK_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
63 /**
64 * XXX move this into a new dri/common/cliprects.c file.
65 */
66 GLboolean
67 intel_intersect_cliprects(drm_clip_rect_t * dst,
68 const drm_clip_rect_t * a,
69 const drm_clip_rect_t * b)
70 {
71 GLint bx = b->x1;
72 GLint by = b->y1;
73 GLint bw = b->x2 - bx;
74 GLint bh = b->y2 - by;
75
76 if (bx < a->x1)
77 bw -= a->x1 - bx, bx = a->x1;
78 if (by < a->y1)
79 bh -= a->y1 - by, by = a->y1;
80 if (bx + bw > a->x2)
81 bw = a->x2 - bx;
82 if (by + bh > a->y2)
83 bh = a->y2 - by;
84 if (bw <= 0)
85 return GL_FALSE;
86 if (bh <= 0)
87 return GL_FALSE;
88
89 dst->x1 = bx;
90 dst->y1 = by;
91 dst->x2 = bx + bw;
92 dst->y2 = by + bh;
93
94 return GL_TRUE;
95 }
96
97 /**
98 * Return pointer to current color drawing region, or NULL.
99 */
100 struct intel_region *
101 intel_drawbuf_region(struct intel_context *intel)
102 {
103 struct intel_renderbuffer *irbColor =
104 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0][0]);
105 if (irbColor)
106 return irbColor->region;
107 else
108 return NULL;
109 }
110
111 /**
112 * Return pointer to current color reading region, or NULL.
113 */
114 struct intel_region *
115 intel_readbuf_region(struct intel_context *intel)
116 {
117 struct intel_renderbuffer *irb
118 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
119 if (irb)
120 return irb->region;
121 else
122 return NULL;
123 }
124
125
126
127 /**
128 * Update the following fields for rendering to a user-created FBO:
129 * intel->numClipRects
130 * intel->pClipRects
131 * intel->drawX
132 * intel->drawY
133 */
134 static void
135 intelSetRenderbufferClipRects(struct intel_context *intel)
136 {
137 assert(intel->ctx.DrawBuffer->Width > 0);
138 assert(intel->ctx.DrawBuffer->Height > 0);
139 intel->fboRect.x1 = 0;
140 intel->fboRect.y1 = 0;
141 intel->fboRect.x2 = intel->ctx.DrawBuffer->Width;
142 intel->fboRect.y2 = intel->ctx.DrawBuffer->Height;
143 intel->numClipRects = 1;
144 intel->pClipRects = &intel->fboRect;
145 intel->drawX = 0;
146 intel->drawY = 0;
147 }
148
149
150 /**
151 * As above, but for rendering to front buffer of a window.
152 * \sa intelSetRenderbufferClipRects
153 */
154 static void
155 intelSetFrontClipRects(struct intel_context *intel)
156 {
157 __DRIdrawablePrivate *dPriv = intel->driDrawable;
158
159 if (!dPriv)
160 return;
161
162 intel->numClipRects = dPriv->numClipRects;
163 intel->pClipRects = dPriv->pClipRects;
164 intel->drawX = dPriv->x;
165 intel->drawY = dPriv->y;
166 }
167
168
169 /**
170 * As above, but for rendering to back buffer of a window.
171 */
172 static void
173 intelSetBackClipRects(struct intel_context *intel)
174 {
175 __DRIdrawablePrivate *dPriv = intel->driDrawable;
176 struct intel_framebuffer *intel_fb;
177
178 if (!dPriv)
179 return;
180
181 intel_fb = dPriv->driverPrivate;
182
183 if (intel_fb->pf_active || dPriv->numBackClipRects == 0) {
184 /* use the front clip rects */
185 intel->numClipRects = dPriv->numClipRects;
186 intel->pClipRects = dPriv->pClipRects;
187 intel->drawX = dPriv->x;
188 intel->drawY = dPriv->y;
189 }
190 else {
191 /* use the back clip rects */
192 intel->numClipRects = dPriv->numBackClipRects;
193 intel->pClipRects = dPriv->pBackClipRects;
194 intel->drawX = dPriv->backX;
195 intel->drawY = dPriv->backY;
196 }
197 }
198
199
200 /**
201 * This will be called whenever the currently bound window is moved/resized.
202 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
203 */
204 void
205 intelWindowMoved(struct intel_context *intel)
206 {
207 GLcontext *ctx = &intel->ctx;
208 __DRIdrawablePrivate *dPriv = intel->driDrawable;
209 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
210
211 if (!intel->ctx.DrawBuffer) {
212 /* when would this happen? -BP */
213 intelSetFrontClipRects(intel);
214 }
215 else if (intel->ctx.DrawBuffer->Name != 0) {
216 /* drawing to user-created FBO - do nothing */
217 /* Cliprects would be set from intelDrawBuffer() */
218 }
219 else {
220 /* drawing to a window */
221 switch (intel_fb->Base._ColorDrawBufferMask[0]) {
222 case BUFFER_BIT_FRONT_LEFT:
223 intelSetFrontClipRects(intel);
224 break;
225 case BUFFER_BIT_BACK_LEFT:
226 intelSetBackClipRects(intel);
227 break;
228 default:
229 /* glDrawBuffer(GL_NONE or GL_FRONT_AND_BACK): software fallback */
230 intelSetFrontClipRects(intel);
231 }
232 }
233
234 if (intel->intelScreen->driScrnPriv->ddxMinor >= 7) {
235 drmI830Sarea *sarea = intel->sarea;
236 drm_clip_rect_t drw_rect = { .x1 = dPriv->x, .x2 = dPriv->x + dPriv->w,
237 .y1 = dPriv->y, .y2 = dPriv->y + dPriv->h };
238 drm_clip_rect_t pipeA_rect = { .x1 = sarea->pipeA_x, .y1 = sarea->pipeA_y,
239 .x2 = sarea->pipeA_x + sarea->pipeA_w,
240 .y2 = sarea->pipeA_y + sarea->pipeA_h };
241 drm_clip_rect_t pipeB_rect = { .x1 = sarea->pipeB_x, .y1 = sarea->pipeB_y,
242 .x2 = sarea->pipeB_x + sarea->pipeB_w,
243 .y2 = sarea->pipeB_y + sarea->pipeB_h };
244 GLint areaA = driIntersectArea( drw_rect, pipeA_rect );
245 GLint areaB = driIntersectArea( drw_rect, pipeB_rect );
246 GLuint flags = intel_fb->vblank_flags;
247 GLboolean pf_active;
248 GLint pf_pipes;
249
250 /* Update page flipping info
251 */
252 pf_pipes = 0;
253
254 if (areaA > 0)
255 pf_pipes |= 1;
256
257 if (areaB > 0)
258 pf_pipes |= 2;
259
260 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
261 (intel_fb->pf_pipes & 0x2)) & 0x3;
262
263 intel_fb->pf_num_pages = intel->intelScreen->third.handle ? 3 : 2;
264
265 pf_active = pf_pipes && (pf_pipes & intel->sarea->pf_active) == pf_pipes;
266
267 if (INTEL_DEBUG & DEBUG_LOCK)
268 if (pf_active != intel_fb->pf_active)
269 _mesa_printf("%s - Page flipping %sactive\n", __progname,
270 pf_active ? "" : "in");
271
272 if (pf_active) {
273 /* Sync pages between pipes if we're flipping on both at the same time */
274 if (pf_pipes == 0x3 && pf_pipes != intel_fb->pf_pipes &&
275 (intel->sarea->pf_current_page & 0x3) !=
276 (((intel->sarea->pf_current_page) >> 2) & 0x3)) {
277 drm_i915_flip_t flip;
278
279 if (intel_fb->pf_current_page ==
280 (intel->sarea->pf_current_page & 0x3)) {
281 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
282 * lockups for unknown reasons.
283 */
284 intel->sarea->pf_current_page =
285 intel->sarea->pf_current_page & 0x3;
286 intel->sarea->pf_current_page |=
287 ((intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
288 intel_fb->pf_num_pages) << 2;
289
290 flip.pipes = 0x2;
291 } else {
292 intel->sarea->pf_current_page =
293 intel->sarea->pf_current_page & (0x3 << 2);
294 intel->sarea->pf_current_page |=
295 (intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
296 intel_fb->pf_num_pages;
297
298 flip.pipes = 0x1;
299 }
300
301 drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
302 }
303
304 intel_fb->pf_pipes = pf_pipes;
305 }
306
307 intel_fb->pf_active = pf_active;
308 intel_flip_renderbuffers(intel_fb);
309 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
310
311 /* Update vblank info
312 */
313 if (areaB > areaA || (areaA == areaB && areaB > 0)) {
314 flags = intel_fb->vblank_flags | VBLANK_FLAG_SECONDARY;
315 } else {
316 flags = intel_fb->vblank_flags & ~VBLANK_FLAG_SECONDARY;
317 }
318
319 if (flags != intel_fb->vblank_flags) {
320 drmVBlank vbl;
321 int i;
322
323 vbl.request.type = DRM_VBLANK_ABSOLUTE;
324
325 if ( intel_fb->vblank_flags & VBLANK_FLAG_SECONDARY ) {
326 vbl.request.type |= DRM_VBLANK_SECONDARY;
327 }
328
329 for (i = 0; i < intel_fb->pf_num_pages; i++) {
330 if (!intel_fb->color_rb[i])
331 continue;
332
333 vbl.request.sequence = intel_fb->color_rb[i]->vbl_pending;
334 drmWaitVBlank(intel->driFd, &vbl);
335 }
336
337 intel_fb->vblank_flags = flags;
338 driGetCurrentVBlank(dPriv, intel_fb->vblank_flags, &intel_fb->vbl_seq);
339 intel_fb->vbl_waited = intel_fb->vbl_seq;
340
341 for (i = 0; i < intel_fb->pf_num_pages; i++) {
342 if (intel_fb->color_rb[i])
343 intel_fb->color_rb[i]->vbl_pending = intel_fb->vbl_waited;
344 }
345 }
346 } else {
347 intel_fb->vblank_flags &= ~VBLANK_FLAG_SECONDARY;
348 }
349
350 /* Update Mesa's notion of window size */
351 driUpdateFramebufferSize(ctx, dPriv);
352 intel_fb->Base.Initialized = GL_TRUE; /* XXX remove someday */
353
354 /* Update hardware scissor */
355 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
356 ctx->Scissor.Width, ctx->Scissor.Height);
357
358 /* Re-calculate viewport related state */
359 ctx->Driver.DepthRange( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
360 }
361
362
363
364 /* A true meta version of this would be very simple and additionally
365 * machine independent. Maybe we'll get there one day.
366 */
367 static void
368 intelClearWithTris(struct intel_context *intel, GLbitfield mask)
369 {
370 GLcontext *ctx = &intel->ctx;
371 struct gl_framebuffer *fb = ctx->DrawBuffer;
372 drm_clip_rect_t clear;
373
374 if (INTEL_DEBUG & DEBUG_BLIT)
375 _mesa_printf("%s 0x%x\n", __FUNCTION__, mask);
376
377 LOCK_HARDWARE(intel);
378
379 /* XXX FBO: was: intel->driDrawable->numClipRects */
380 if (intel->numClipRects) {
381 GLint cx, cy, cw, ch;
382 GLuint buf;
383
384 intel->vtbl.install_meta_state(intel);
385
386 /* Get clear bounds after locking */
387 cx = fb->_Xmin;
388 cy = fb->_Ymin;
389 ch = fb->_Ymax - cx;
390 cw = fb->_Xmax - cy;
391
392 /* note: regardless of 'all', cx, cy, cw, ch are now correct */
393 clear.x1 = cx;
394 clear.y1 = cy;
395 clear.x2 = cx + cw;
396 clear.y2 = cy + ch;
397
398 /* Back and stencil cliprects are the same. Try and do both
399 * buffers at once:
400 */
401 if (mask &
402 (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH)) {
403 struct intel_region *backRegion =
404 intel_get_rb_region(fb, BUFFER_BACK_LEFT);
405 struct intel_region *depthRegion =
406 intel_get_rb_region(fb, BUFFER_DEPTH);
407 const GLuint clearColor = (backRegion && backRegion->cpp == 4)
408 ? intel->ClearColor8888 : intel->ClearColor565;
409
410 intel->vtbl.meta_draw_region(intel, backRegion, depthRegion);
411
412 if (mask & BUFFER_BIT_BACK_LEFT)
413 intel->vtbl.meta_color_mask(intel, GL_TRUE);
414 else
415 intel->vtbl.meta_color_mask(intel, GL_FALSE);
416
417 if (mask & BUFFER_BIT_STENCIL)
418 intel->vtbl.meta_stencil_replace(intel,
419 intel->ctx.Stencil.WriteMask[0],
420 intel->ctx.Stencil.Clear);
421 else
422 intel->vtbl.meta_no_stencil_write(intel);
423
424 if (mask & BUFFER_BIT_DEPTH)
425 intel->vtbl.meta_depth_replace(intel);
426 else
427 intel->vtbl.meta_no_depth_write(intel);
428
429 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
430 * drawing origin may not be correctly emitted.
431 */
432 intel_meta_draw_quad(intel, clear.x1, clear.x2, clear.y1, clear.y2, intel->ctx.Depth.Clear, clearColor, 0, 0, 0, 0); /* texcoords */
433
434 mask &=
435 ~(BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH);
436 }
437
438 /* clear the remaining (color) renderbuffers */
439 for (buf = 0; buf < BUFFER_COUNT && mask; buf++) {
440 const GLuint bufBit = 1 << buf;
441 if (mask & bufBit) {
442 struct intel_renderbuffer *irbColor =
443 intel_renderbuffer(fb->Attachment[buf].Renderbuffer);
444 GLuint color = (irbColor->region->cpp == 4)
445 ? intel->ClearColor8888 : intel->ClearColor565;
446
447 ASSERT(irbColor);
448
449 intel->vtbl.meta_no_depth_write(intel);
450 intel->vtbl.meta_no_stencil_write(intel);
451 intel->vtbl.meta_color_mask(intel, GL_TRUE);
452 intel->vtbl.meta_draw_region(intel, irbColor->region, NULL);
453
454 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
455 * drawing origin may not be correctly emitted.
456 */
457 intel_meta_draw_quad(intel, clear.x1, clear.x2, clear.y1, clear.y2, 0, /* depth clear val */
458 color, 0, 0, 0, 0); /* texcoords */
459
460 mask &= ~bufBit;
461 }
462 }
463
464 intel->vtbl.leave_meta_state(intel);
465 intel_batchbuffer_flush(intel->batch);
466 }
467 UNLOCK_HARDWARE(intel);
468 }
469
470
471
472
473 /**
474 * Copy the window contents named by dPriv to the rotated (or reflected)
475 * color buffer.
476 * srcBuf is BUFFER_BIT_FRONT_LEFT or BUFFER_BIT_BACK_LEFT to indicate the source.
477 */
478 void
479 intelRotateWindow(struct intel_context *intel,
480 __DRIdrawablePrivate * dPriv, GLuint srcBuf)
481 {
482 intelScreenPrivate *screen = intel->intelScreen;
483 drm_clip_rect_t fullRect;
484 struct intel_framebuffer *intel_fb;
485 struct intel_region *src;
486 const drm_clip_rect_t *clipRects;
487 int numClipRects;
488 int i;
489 GLenum format, type;
490
491 int xOrig, yOrig;
492 int origNumClipRects;
493 drm_clip_rect_t *origRects;
494
495 /*
496 * set up hardware state
497 */
498 intelFlush(&intel->ctx);
499
500 LOCK_HARDWARE(intel);
501
502 if (!intel->numClipRects) {
503 UNLOCK_HARDWARE(intel);
504 return;
505 }
506
507 intel->vtbl.install_meta_state(intel);
508
509 intel->vtbl.meta_no_depth_write(intel);
510 intel->vtbl.meta_no_stencil_write(intel);
511 intel->vtbl.meta_color_mask(intel, GL_FALSE);
512
513
514 /* save current drawing origin and cliprects (restored at end) */
515 xOrig = intel->drawX;
516 yOrig = intel->drawY;
517 origNumClipRects = intel->numClipRects;
518 origRects = intel->pClipRects;
519
520 /*
521 * set drawing origin, cliprects for full-screen access to rotated screen
522 */
523 fullRect.x1 = 0;
524 fullRect.y1 = 0;
525 fullRect.x2 = screen->rotatedWidth;
526 fullRect.y2 = screen->rotatedHeight;
527 intel->drawX = 0;
528 intel->drawY = 0;
529 intel->numClipRects = 1;
530 intel->pClipRects = &fullRect;
531
532 intel->vtbl.meta_draw_region(intel, screen->rotated_region, NULL); /* ? */
533
534 intel_fb = dPriv->driverPrivate;
535
536 if ((srcBuf == BUFFER_BIT_BACK_LEFT && !intel_fb->pf_active)) {
537 src = intel_get_rb_region(&intel_fb->Base, BUFFER_BACK_LEFT);
538 clipRects = dPriv->pBackClipRects;
539 numClipRects = dPriv->numBackClipRects;
540 }
541 else {
542 src = intel_get_rb_region(&intel_fb->Base, BUFFER_FRONT_LEFT);
543 clipRects = dPriv->pClipRects;
544 numClipRects = dPriv->numClipRects;
545 }
546
547 if (src->cpp == 4) {
548 format = GL_BGRA;
549 type = GL_UNSIGNED_BYTE;
550 }
551 else {
552 format = GL_BGR;
553 type = GL_UNSIGNED_SHORT_5_6_5_REV;
554 }
555
556 /* set the whole screen up as a texture to avoid alignment issues */
557 intel->vtbl.meta_tex_rect_source(intel,
558 src->buffer,
559 screen->width,
560 screen->height, src->pitch, format, type);
561
562 intel->vtbl.meta_texture_blend_replace(intel);
563
564 /*
565 * loop over the source window's cliprects
566 */
567 for (i = 0; i < numClipRects; i++) {
568 int srcX0 = clipRects[i].x1;
569 int srcY0 = clipRects[i].y1;
570 int srcX1 = clipRects[i].x2;
571 int srcY1 = clipRects[i].y2;
572 GLfloat verts[4][2], tex[4][2];
573 int j;
574
575 /* build vertices for four corners of clip rect */
576 verts[0][0] = srcX0;
577 verts[0][1] = srcY0;
578 verts[1][0] = srcX1;
579 verts[1][1] = srcY0;
580 verts[2][0] = srcX1;
581 verts[2][1] = srcY1;
582 verts[3][0] = srcX0;
583 verts[3][1] = srcY1;
584
585 /* .. and texcoords */
586 tex[0][0] = srcX0;
587 tex[0][1] = srcY0;
588 tex[1][0] = srcX1;
589 tex[1][1] = srcY0;
590 tex[2][0] = srcX1;
591 tex[2][1] = srcY1;
592 tex[3][0] = srcX0;
593 tex[3][1] = srcY1;
594
595 /* transform coords to rotated screen coords */
596
597 for (j = 0; j < 4; j++) {
598 matrix23TransformCoordf(&screen->rotMatrix,
599 &verts[j][0], &verts[j][1]);
600 }
601
602 /* draw polygon to map source image to dest region */
603 intel_meta_draw_poly(intel, 4, verts, 0, 0, tex);
604
605 } /* cliprect loop */
606
607 intel->vtbl.leave_meta_state(intel);
608 intel_batchbuffer_flush(intel->batch);
609
610 /* restore original drawing origin and cliprects */
611 intel->drawX = xOrig;
612 intel->drawY = yOrig;
613 intel->numClipRects = origNumClipRects;
614 intel->pClipRects = origRects;
615
616 UNLOCK_HARDWARE(intel);
617 }
618
619
620 /**
621 * Called by ctx->Driver.Clear.
622 */
623 static void
624 intelClear(GLcontext *ctx, GLbitfield mask)
625 {
626 struct intel_context *intel = intel_context(ctx);
627 const GLuint colorMask = *((GLuint *) & ctx->Color.ColorMask);
628 GLbitfield tri_mask = 0;
629 GLbitfield blit_mask = 0;
630 GLbitfield swrast_mask = 0;
631 struct gl_framebuffer *fb = ctx->DrawBuffer;
632 GLuint i;
633
634 if (0)
635 fprintf(stderr, "%s\n", __FUNCTION__);
636
637 /* HW color buffers (front, back, aux, generic FBO, etc) */
638 if (colorMask == ~0) {
639 /* clear all R,G,B,A */
640 /* XXX FBO: need to check if colorbuffers are software RBOs! */
641 blit_mask |= (mask & BUFFER_BITS_COLOR);
642 }
643 else {
644 /* glColorMask in effect */
645 tri_mask |= (mask & BUFFER_BITS_COLOR);
646 }
647
648 /* HW stencil */
649 if (mask & BUFFER_BIT_STENCIL) {
650 const struct intel_region *stencilRegion
651 = intel_get_rb_region(fb, BUFFER_STENCIL);
652 if (stencilRegion) {
653 /* have hw stencil */
654 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
655 /* not clearing all stencil bits, so use triangle clearing */
656 tri_mask |= BUFFER_BIT_STENCIL;
657 }
658 else {
659 /* clearing all stencil bits, use blitting */
660 blit_mask |= BUFFER_BIT_STENCIL;
661 }
662 }
663 }
664
665 /* HW depth */
666 if (mask & BUFFER_BIT_DEPTH) {
667 /* clear depth with whatever method is used for stencil (see above) */
668 if (tri_mask & BUFFER_BIT_STENCIL)
669 tri_mask |= BUFFER_BIT_DEPTH;
670 else
671 blit_mask |= BUFFER_BIT_DEPTH;
672 }
673
674 /* SW fallback clearing */
675 swrast_mask = mask & ~tri_mask & ~blit_mask;
676
677 for (i = 0; i < BUFFER_COUNT; i++) {
678 GLuint bufBit = 1 << i;
679 if ((blit_mask | tri_mask) & bufBit) {
680 if (!fb->Attachment[i].Renderbuffer->ClassID) {
681 blit_mask &= ~bufBit;
682 tri_mask &= ~bufBit;
683 swrast_mask |= bufBit;
684 }
685 }
686 }
687
688
689 intelFlush(ctx); /* XXX intelClearWithBlit also does this */
690
691 if (blit_mask)
692 intelClearWithBlit(ctx, blit_mask);
693
694 if (tri_mask)
695 intelClearWithTris(intel, tri_mask);
696
697 if (swrast_mask)
698 _swrast_Clear(ctx, swrast_mask);
699 }
700
701
702 /* Emit wait for pending flips */
703 void
704 intel_wait_flips(struct intel_context *intel, GLuint batch_flags)
705 {
706 struct intel_framebuffer *intel_fb =
707 (struct intel_framebuffer *) intel->ctx.DrawBuffer;
708 struct intel_renderbuffer *intel_rb =
709 intel_get_renderbuffer(&intel_fb->Base,
710 intel_fb->Base._ColorDrawBufferMask[0] ==
711 BUFFER_BIT_FRONT_LEFT ? BUFFER_FRONT_LEFT :
712 BUFFER_BACK_LEFT);
713
714 if (intel_fb->Base.Name == 0 && intel_rb->pf_pending == intel_fb->pf_seq) {
715 GLint pf_pipes = intel_fb->pf_pipes;
716 BATCH_LOCALS;
717
718 /* Wait for pending flips to take effect */
719 BEGIN_BATCH(2, batch_flags);
720 OUT_BATCH(pf_pipes & 0x1 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP)
721 : 0);
722 OUT_BATCH(pf_pipes & 0x2 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_B_FLIP)
723 : 0);
724 ADVANCE_BATCH();
725
726 intel_rb->pf_pending--;
727 }
728 }
729
730
731 /* Flip the front & back buffers
732 */
733 static GLboolean
734 intelPageFlip(const __DRIdrawablePrivate * dPriv)
735 {
736 struct intel_context *intel;
737 int ret;
738 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
739
740 if (INTEL_DEBUG & DEBUG_IOCTL)
741 fprintf(stderr, "%s\n", __FUNCTION__);
742
743 assert(dPriv);
744 assert(dPriv->driContextPriv);
745 assert(dPriv->driContextPriv->driverPrivate);
746
747 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
748
749 if (intel->intelScreen->drmMinor < 9)
750 return GL_FALSE;
751
752 intelFlush(&intel->ctx);
753
754 ret = 0;
755
756 LOCK_HARDWARE(intel);
757
758 if (dPriv->numClipRects && intel_fb->pf_active) {
759 drm_i915_flip_t flip;
760
761 flip.pipes = intel_fb->pf_pipes;
762
763 ret = drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
764 }
765
766 UNLOCK_HARDWARE(intel);
767
768 if (ret || !intel_fb->pf_active)
769 return GL_FALSE;
770
771 if (!dPriv->numClipRects) {
772 usleep(10000); /* throttle invisible client 10ms */
773 }
774
775 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
776 (intel_fb->pf_pipes & 0x2)) & 0x3;
777
778 if (dPriv->numClipRects != 0) {
779 intel_get_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT)->pf_pending =
780 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->pf_pending =
781 ++intel_fb->pf_seq;
782 }
783
784 intel_flip_renderbuffers(intel_fb);
785 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
786
787 return GL_TRUE;
788 }
789
790 #if 0
791 void
792 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
793 {
794 if (dPriv->driverPrivate) {
795 const struct gl_framebuffer *fb
796 = (struct gl_framebuffer *) dPriv->driverPrivate;
797 if (fb->Visual.doubleBufferMode) {
798 GET_CURRENT_CONTEXT(ctx);
799 if (ctx && ctx->DrawBuffer == fb) {
800 _mesa_notifySwapBuffers(ctx); /* flush pending rendering */
801 }
802 if (intel->doPageFlip) {
803 intelPageFlip(dPriv);
804 }
805 else {
806 intelCopyBuffer(dPriv);
807 }
808 }
809 }
810 else {
811 _mesa_problem(NULL,
812 "dPriv has no gl_framebuffer pointer in intelSwapBuffers");
813 }
814 }
815 #else
816 /* Trunk version:
817 */
818
819 static GLboolean
820 intelScheduleSwap(const __DRIdrawablePrivate * dPriv, GLboolean *missed_target)
821 {
822 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
823 unsigned int interval = driGetVBlankInterval(dPriv, intel_fb->vblank_flags);
824 struct intel_context *intel =
825 intelScreenContext(dPriv->driScreenPriv->private);
826 const intelScreenPrivate *intelScreen = intel->intelScreen;
827 unsigned int target;
828 drm_i915_vblank_swap_t swap;
829 GLboolean ret;
830
831 if ((intel_fb->vblank_flags & VBLANK_FLAG_NO_IRQ) ||
832 intelScreen->current_rotation != 0 ||
833 intelScreen->drmMinor < (intel_fb->pf_active ? 9 : 6))
834 return GL_FALSE;
835
836 swap.seqtype = DRM_VBLANK_ABSOLUTE;
837
838 if (intel_fb->vblank_flags & VBLANK_FLAG_SYNC) {
839 swap.seqtype |= DRM_VBLANK_NEXTONMISS;
840 } else if (interval == 0) {
841 return GL_FALSE;
842 }
843
844 swap.drawable = dPriv->hHWDrawable;
845 target = swap.sequence = intel_fb->vbl_seq + interval;
846
847 if ( intel_fb->vblank_flags & VBLANK_FLAG_SECONDARY ) {
848 swap.seqtype |= DRM_VBLANK_SECONDARY;
849 }
850
851 LOCK_HARDWARE(intel);
852
853 intel_batchbuffer_flush(intel->batch);
854
855 if ( intel_fb->pf_active ) {
856 swap.seqtype |= DRM_VBLANK_FLIP;
857
858 intel_fb->pf_current_page = (((intel->sarea->pf_current_page >>
859 (intel_fb->pf_pipes & 0x2)) & 0x3) + 1) %
860 intel_fb->pf_num_pages;
861 }
862
863 if (!drmCommandWriteRead(intel->driFd, DRM_I915_VBLANK_SWAP, &swap,
864 sizeof(swap))) {
865 intel_fb->vbl_seq = swap.sequence;
866 swap.sequence -= target;
867 *missed_target = swap.sequence > 0 && swap.sequence <= (1 << 23);
868
869 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->vbl_pending =
870 intel_get_renderbuffer(&intel_fb->Base,
871 BUFFER_FRONT_LEFT)->vbl_pending =
872 intel_fb->vbl_seq;
873
874 if (swap.seqtype & DRM_VBLANK_FLIP) {
875 intel_flip_renderbuffers(intel_fb);
876 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
877 }
878
879 ret = GL_TRUE;
880 } else {
881 if (swap.seqtype & DRM_VBLANK_FLIP) {
882 intel_fb->pf_current_page = ((intel->sarea->pf_current_page >>
883 (intel_fb->pf_pipes & 0x2)) & 0x3) %
884 intel_fb->pf_num_pages;
885 }
886
887 ret = GL_FALSE;
888 }
889
890 UNLOCK_HARDWARE(intel);
891
892 return ret;
893 }
894
895 void
896 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
897 {
898 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
899 GET_CURRENT_CONTEXT(ctx);
900 struct intel_context *intel;
901
902 if (ctx == NULL)
903 return;
904
905 intel = intel_context(ctx);
906
907 if (ctx->Visual.doubleBufferMode) {
908 intelScreenPrivate *screen = intel->intelScreen;
909 GLboolean missed_target;
910 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
911 int64_t ust;
912
913 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
914
915 if (screen->current_rotation != 0 ||
916 !intelScheduleSwap(dPriv, &missed_target)) {
917 driWaitForVBlank(dPriv, &intel_fb->vbl_seq, intel_fb->vblank_flags,
918 &missed_target);
919
920 if (screen->current_rotation != 0 || !intelPageFlip(dPriv)) {
921 intelCopyBuffer(dPriv, NULL);
922 }
923
924 if (screen->current_rotation != 0) {
925 intelRotateWindow(intel, dPriv, BUFFER_BIT_FRONT_LEFT);
926 }
927 }
928
929 intel_fb->swap_count++;
930 (*dri_interface->getUST) (&ust);
931 if (missed_target) {
932 intel_fb->swap_missed_count++;
933 intel_fb->swap_missed_ust = ust - intel_fb->swap_ust;
934 }
935
936 intel_fb->swap_ust = ust;
937 }
938 }
939 else {
940 /* XXX this shouldn't be an error but we can't handle it for now */
941 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
942 }
943 }
944 #endif
945
946 void
947 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
948 {
949 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
950 struct intel_context *intel =
951 (struct intel_context *) dPriv->driContextPriv->driverPrivate;
952 GLcontext *ctx = &intel->ctx;
953
954 if (ctx->Visual.doubleBufferMode) {
955 drm_clip_rect_t rect;
956 rect.x1 = x + dPriv->x;
957 rect.y1 = (dPriv->h - y - h) + dPriv->y;
958 rect.x2 = rect.x1 + w;
959 rect.y2 = rect.y1 + h;
960 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
961 intelCopyBuffer(dPriv, &rect);
962 }
963 }
964 else {
965 /* XXX this shouldn't be an error but we can't handle it for now */
966 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
967 }
968 }
969
970
971 /**
972 * Update the hardware state for drawing into a window or framebuffer object.
973 *
974 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
975 * places within the driver.
976 *
977 * Basically, this needs to be called any time the current framebuffer
978 * changes, the renderbuffers change, or we need to draw into different
979 * color buffers.
980 */
981 void
982 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
983 {
984 struct intel_context *intel = intel_context(ctx);
985 struct intel_region *colorRegion, *depthRegion = NULL;
986 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
987 int front = 0; /* drawing to front color buffer? */
988
989 if (!fb) {
990 /* this can happen during the initial context initialization */
991 return;
992 }
993
994 /* Do this here, note core Mesa, since this function is called from
995 * many places within the driver.
996 */
997 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
998 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
999 _mesa_update_framebuffer(ctx);
1000 /* this updates the DrawBuffer's Width/Height if it's a FBO */
1001 _mesa_update_draw_buffer_bounds(ctx);
1002 }
1003
1004 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1005 /* this may occur when we're called by glBindFrameBuffer() during
1006 * the process of someone setting up renderbuffers, etc.
1007 */
1008 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
1009 return;
1010 }
1011
1012 if (fb->Name)
1013 intel_validate_paired_depth_stencil(ctx, fb);
1014
1015 /*
1016 * How many color buffers are we drawing into?
1017 */
1018 if (fb->_NumColorDrawBuffers[0] != 1
1019 #if 0
1020 /* XXX FBO temporary - always use software rendering */
1021 || 1
1022 #endif
1023 ) {
1024 /* writing to 0 or 2 or 4 color buffers */
1025 /*_mesa_debug(ctx, "Software rendering\n");*/
1026 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
1027 front = 1; /* might not have back color buffer */
1028 }
1029 else {
1030 /* draw to exactly one color buffer */
1031 /*_mesa_debug(ctx, "Hardware rendering\n");*/
1032 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
1033 if (fb->_ColorDrawBufferMask[0] == BUFFER_BIT_FRONT_LEFT) {
1034 front = 1;
1035 }
1036 }
1037
1038 /*
1039 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
1040 * And set up cliprects.
1041 */
1042 if (fb->Name == 0) {
1043 /* drawing to window system buffer */
1044 if (front) {
1045 intelSetFrontClipRects(intel);
1046 colorRegion = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
1047 }
1048 else {
1049 intelSetBackClipRects(intel);
1050 colorRegion = intel_get_rb_region(fb, BUFFER_BACK_LEFT);
1051 }
1052 }
1053 else {
1054 /* drawing to user-created FBO */
1055 struct intel_renderbuffer *irb;
1056 intelSetRenderbufferClipRects(intel);
1057 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0][0]);
1058 colorRegion = (irb && irb->region) ? irb->region : NULL;
1059 }
1060
1061 /* Update culling direction which changes depending on the
1062 * orientation of the buffer:
1063 */
1064 if (ctx->Driver.FrontFace)
1065 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
1066 else
1067 ctx->NewState |= _NEW_POLYGON;
1068
1069 if (!colorRegion) {
1070 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
1071 }
1072 else {
1073 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
1074 }
1075
1076 /***
1077 *** Get depth buffer region and check if we need a software fallback.
1078 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
1079 ***/
1080 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
1081 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
1082 if (irbDepth && irbDepth->region) {
1083 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
1084 depthRegion = irbDepth->region;
1085 }
1086 else {
1087 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
1088 depthRegion = NULL;
1089 }
1090 }
1091 else {
1092 /* not using depth buffer */
1093 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
1094 depthRegion = NULL;
1095 }
1096
1097 /***
1098 *** Stencil buffer
1099 *** This can only be hardware accelerated if we're using a
1100 *** combined DEPTH_STENCIL buffer (for now anyway).
1101 ***/
1102 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
1103 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
1104 if (irbStencil && irbStencil->region) {
1105 ASSERT(irbStencil->Base._ActualFormat == GL_DEPTH24_STENCIL8_EXT);
1106 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
1107 /* need to re-compute stencil hw state */
1108 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1109 if (!depthRegion)
1110 depthRegion = irbStencil->region;
1111 }
1112 else {
1113 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
1114 }
1115 }
1116 else {
1117 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
1118 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
1119 /* need to re-compute stencil hw state */
1120 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1121 }
1122
1123
1124 /**
1125 ** Release old regions, reference new regions
1126 **/
1127 #if 0 /* XXX FBO: this seems to be redundant with i915_state_draw_region() */
1128 if (intel->draw_region != colorRegion) {
1129 intel_region_release(&intel->draw_region);
1130 intel_region_reference(&intel->draw_region, colorRegion);
1131 }
1132 if (intel->intelScreen->depth_region != depthRegion) {
1133 intel_region_release(&intel->intelScreen->depth_region);
1134 intel_region_reference(&intel->intelScreen->depth_region, depthRegion);
1135 }
1136 #endif
1137
1138 intel->vtbl.set_draw_region(intel, colorRegion, depthRegion);
1139
1140 /* update viewport since it depends on window size */
1141 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
1142 ctx->Viewport.Width, ctx->Viewport.Height);
1143
1144 /* Update hardware scissor */
1145 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
1146 ctx->Scissor.Width, ctx->Scissor.Height);
1147 }
1148
1149
1150 static void
1151 intelDrawBuffer(GLcontext * ctx, GLenum mode)
1152 {
1153 intel_draw_buffer(ctx, ctx->DrawBuffer);
1154 }
1155
1156
1157 static void
1158 intelReadBuffer(GLcontext * ctx, GLenum mode)
1159 {
1160 if (ctx->ReadBuffer == ctx->DrawBuffer) {
1161 /* This will update FBO completeness status.
1162 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1163 * refers to a missing renderbuffer. Calling glReadBuffer can set
1164 * that straight and can make the drawing buffer complete.
1165 */
1166 intel_draw_buffer(ctx, ctx->DrawBuffer);
1167 }
1168 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1169 * reference ctx->ReadBuffer and do appropriate state checks.
1170 */
1171 }
1172
1173
1174 void
1175 intelInitBufferFuncs(struct dd_function_table *functions)
1176 {
1177 functions->Clear = intelClear;
1178 functions->DrawBuffer = intelDrawBuffer;
1179 functions->ReadBuffer = intelReadBuffer;
1180 }