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