c32d54a2f955c6df6edd7380a0ac6d09371f12db
[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_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_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
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->ddx_version.minor >= 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 = dPriv->vblFlags;
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 = dPriv->vblFlags | VBLANK_FLAG_SECONDARY;
315 } else {
316 flags = dPriv->vblFlags & ~VBLANK_FLAG_SECONDARY;
317 }
318
319 /* Check to see if we changed pipes */
320 if (flags != dPriv->vblFlags && dPriv->vblFlags &&
321 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ)) {
322 int64_t count;
323 drmVBlank vbl;
324 int i;
325
326 /*
327 * Deal with page flipping
328 */
329 vbl.request.type = DRM_VBLANK_ABSOLUTE;
330
331 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
332 vbl.request.type |= DRM_VBLANK_SECONDARY;
333 }
334
335 for (i = 0; i < intel_fb->pf_num_pages; i++) {
336 if (!intel_fb->color_rb[i] ||
337 (intel_fb->vbl_waited - intel_fb->color_rb[i]->vbl_pending) <=
338 (1<<23))
339 continue;
340
341 vbl.request.sequence = intel_fb->color_rb[i]->vbl_pending;
342 drmWaitVBlank(intel->driFd, &vbl);
343 }
344
345 /*
346 * Update msc_base from old pipe
347 */
348 driDrawableGetMSC32(dPriv->driScreenPriv, dPriv, &count);
349 dPriv->msc_base = count;
350 /*
351 * Then get new vblank_base and vblSeq values
352 */
353 dPriv->vblFlags = flags;
354 driGetCurrentVBlank(dPriv);
355 dPriv->vblank_base = dPriv->vblSeq;
356
357 intel_fb->vbl_waited = dPriv->vblSeq;
358
359 for (i = 0; i < intel_fb->pf_num_pages; i++) {
360 if (intel_fb->color_rb[i])
361 intel_fb->color_rb[i]->vbl_pending = intel_fb->vbl_waited;
362 }
363 }
364 } else {
365 dPriv->vblFlags &= ~VBLANK_FLAG_SECONDARY;
366 }
367
368 /* Update Mesa's notion of window size */
369 driUpdateFramebufferSize(ctx, dPriv);
370 intel_fb->Base.Initialized = GL_TRUE; /* XXX remove someday */
371
372 /* Update hardware scissor */
373 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
374 ctx->Scissor.Width, ctx->Scissor.Height);
375
376 /* Re-calculate viewport related state */
377 ctx->Driver.DepthRange( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
378 }
379
380
381
382 /* A true meta version of this would be very simple and additionally
383 * machine independent. Maybe we'll get there one day.
384 */
385 static void
386 intelClearWithTris(struct intel_context *intel, GLbitfield mask)
387 {
388 GLcontext *ctx = &intel->ctx;
389 struct gl_framebuffer *fb = ctx->DrawBuffer;
390 GLuint buf;
391
392 if (INTEL_DEBUG & DEBUG_BLIT)
393 _mesa_printf("%s 0x%x\n", __FUNCTION__, mask);
394
395 intel->vtbl.install_meta_state(intel);
396
397 /* Back and stencil cliprects are the same. Try and do both
398 * buffers at once:
399 */
400 if (mask & (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH)) {
401 struct intel_region *backRegion =
402 intel_get_rb_region(fb, BUFFER_BACK_LEFT);
403 struct intel_region *depthRegion =
404 intel_get_rb_region(fb, BUFFER_DEPTH);
405 const GLuint clearColor = (backRegion && backRegion->cpp == 4)
406 ? intel->ClearColor8888 : intel->ClearColor565;
407
408 intel->vtbl.meta_draw_region(intel, backRegion, depthRegion);
409
410 if (mask & BUFFER_BIT_BACK_LEFT)
411 intel->vtbl.meta_color_mask(intel, GL_TRUE);
412 else
413 intel->vtbl.meta_color_mask(intel, GL_FALSE);
414
415 if (mask & BUFFER_BIT_STENCIL)
416 intel->vtbl.meta_stencil_replace(intel,
417 intel->ctx.Stencil.WriteMask[0],
418 intel->ctx.Stencil.Clear);
419 else
420 intel->vtbl.meta_no_stencil_write(intel);
421
422 if (mask & BUFFER_BIT_DEPTH)
423 intel->vtbl.meta_depth_replace(intel);
424 else
425 intel->vtbl.meta_no_depth_write(intel);
426
427 intel_meta_draw_quad(intel,
428 fb->_Xmin,
429 fb->_Xmax,
430 fb->_Ymin,
431 fb->_Ymax,
432 intel->ctx.Depth.Clear, clearColor,
433 0, 0, 0, 0); /* texcoords */
434
435 mask &= ~(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,
458 fb->_Xmin,
459 fb->_Xmax,
460 fb->_Ymin,
461 fb->_Ymax,
462 0, color,
463 0, 0, 0, 0); /* texcoords */
464
465 mask &= ~bufBit;
466 }
467 }
468
469 intel->vtbl.leave_meta_state(intel);
470 intel_batchbuffer_flush(intel->batch);
471 }
472
473 /**
474 * Called by ctx->Driver.Clear.
475 */
476 static void
477 intelClear(GLcontext *ctx, GLbitfield mask)
478 {
479 struct intel_context *intel = intel_context(ctx);
480 const GLuint colorMask = *((GLuint *) & ctx->Color.ColorMask);
481 GLbitfield tri_mask = 0;
482 GLbitfield blit_mask = 0;
483 GLbitfield swrast_mask = 0;
484 struct gl_framebuffer *fb = ctx->DrawBuffer;
485 GLuint i;
486
487 if (0)
488 fprintf(stderr, "%s\n", __FUNCTION__);
489
490 /* HW color buffers (front, back, aux, generic FBO, etc) */
491 if (colorMask == ~0) {
492 /* clear all R,G,B,A */
493 /* XXX FBO: need to check if colorbuffers are software RBOs! */
494 blit_mask |= (mask & BUFFER_BITS_COLOR);
495 }
496 else {
497 /* glColorMask in effect */
498 tri_mask |= (mask & BUFFER_BITS_COLOR);
499 }
500
501 /* HW stencil */
502 if (mask & BUFFER_BIT_STENCIL) {
503 const struct intel_region *stencilRegion
504 = intel_get_rb_region(fb, BUFFER_STENCIL);
505 if (stencilRegion) {
506 /* have hw stencil */
507 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
508 /* not clearing all stencil bits, so use triangle clearing */
509 tri_mask |= BUFFER_BIT_STENCIL;
510 }
511 else {
512 /* clearing all stencil bits, use blitting */
513 blit_mask |= BUFFER_BIT_STENCIL;
514 }
515 }
516 }
517
518 /* HW depth */
519 if (mask & BUFFER_BIT_DEPTH) {
520 /* clear depth with whatever method is used for stencil (see above) */
521 if (tri_mask & BUFFER_BIT_STENCIL)
522 tri_mask |= BUFFER_BIT_DEPTH;
523 else
524 blit_mask |= BUFFER_BIT_DEPTH;
525 }
526
527 /* SW fallback clearing */
528 swrast_mask = mask & ~tri_mask & ~blit_mask;
529
530 for (i = 0; i < BUFFER_COUNT; i++) {
531 GLuint bufBit = 1 << i;
532 if ((blit_mask | tri_mask) & bufBit) {
533 if (!fb->Attachment[i].Renderbuffer->ClassID) {
534 blit_mask &= ~bufBit;
535 tri_mask &= ~bufBit;
536 swrast_mask |= bufBit;
537 }
538 }
539 }
540
541
542 intelFlush(ctx); /* XXX intelClearWithBlit also does this */
543
544 if (blit_mask)
545 intelClearWithBlit(ctx, blit_mask);
546
547 if (tri_mask)
548 intelClearWithTris(intel, tri_mask);
549
550 if (swrast_mask)
551 _swrast_Clear(ctx, swrast_mask);
552 }
553
554
555 /* Emit wait for pending flips */
556 void
557 intel_wait_flips(struct intel_context *intel, GLuint batch_flags)
558 {
559 struct intel_framebuffer *intel_fb =
560 (struct intel_framebuffer *) intel->ctx.DrawBuffer;
561 struct intel_renderbuffer *intel_rb =
562 intel_get_renderbuffer(&intel_fb->Base,
563 intel_fb->Base._ColorDrawBufferMask[0] ==
564 BUFFER_BIT_FRONT_LEFT ? BUFFER_FRONT_LEFT :
565 BUFFER_BACK_LEFT);
566
567 if (intel_fb->Base.Name == 0 && intel_rb &&
568 intel_rb->pf_pending == intel_fb->pf_seq) {
569 GLint pf_planes = intel_fb->pf_planes;
570 BATCH_LOCALS;
571
572 /* Wait for pending flips to take effect */
573 BEGIN_BATCH(2, batch_flags);
574 OUT_BATCH(pf_planes & 0x1 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP)
575 : 0);
576 OUT_BATCH(pf_planes & 0x2 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_B_FLIP)
577 : 0);
578 ADVANCE_BATCH();
579
580 intel_rb->pf_pending--;
581 }
582 }
583
584
585 /* Flip the front & back buffers
586 */
587 static GLboolean
588 intelPageFlip(const __DRIdrawablePrivate * dPriv)
589 {
590 struct intel_context *intel;
591 int ret;
592 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
593
594 if (INTEL_DEBUG & DEBUG_IOCTL)
595 fprintf(stderr, "%s\n", __FUNCTION__);
596
597 assert(dPriv);
598 assert(dPriv->driContextPriv);
599 assert(dPriv->driContextPriv->driverPrivate);
600
601 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
602
603 if (intel->intelScreen->drmMinor < 9)
604 return GL_FALSE;
605
606 intelFlush(&intel->ctx);
607
608 ret = 0;
609
610 LOCK_HARDWARE(intel);
611
612 if (dPriv->numClipRects && intel_fb->pf_active) {
613 drm_i915_flip_t flip;
614
615 flip.pipes = intel_fb->pf_planes;
616
617 ret = drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
618 }
619
620 UNLOCK_HARDWARE(intel);
621
622 if (ret || !intel_fb->pf_active)
623 return GL_FALSE;
624
625 if (!dPriv->numClipRects) {
626 usleep(10000); /* throttle invisible client 10ms */
627 }
628
629 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
630 (intel_fb->pf_planes & 0x2)) & 0x3;
631
632 if (dPriv->numClipRects != 0) {
633 intel_get_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT)->pf_pending =
634 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->pf_pending =
635 ++intel_fb->pf_seq;
636 }
637
638 intel_flip_renderbuffers(intel_fb);
639 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
640
641 return GL_TRUE;
642 }
643
644 #if 0
645 void
646 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
647 {
648 if (dPriv->driverPrivate) {
649 const struct gl_framebuffer *fb
650 = (struct gl_framebuffer *) dPriv->driverPrivate;
651 if (fb->Visual.doubleBufferMode) {
652 GET_CURRENT_CONTEXT(ctx);
653 if (ctx && ctx->DrawBuffer == fb) {
654 _mesa_notifySwapBuffers(ctx); /* flush pending rendering */
655 }
656 if (intel->doPageFlip) {
657 intelPageFlip(dPriv);
658 }
659 else {
660 intelCopyBuffer(dPriv);
661 }
662 }
663 }
664 else {
665 _mesa_problem(NULL,
666 "dPriv has no gl_framebuffer pointer in intelSwapBuffers");
667 }
668 }
669 #else
670 /* Trunk version:
671 */
672
673 static GLboolean
674 intelScheduleSwap(__DRIdrawablePrivate * dPriv, GLboolean *missed_target)
675 {
676 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
677 unsigned int interval;
678 struct intel_context *intel =
679 intelScreenContext(dPriv->driScreenPriv->private);
680 const intelScreenPrivate *intelScreen = intel->intelScreen;
681 unsigned int target;
682 drm_i915_vblank_swap_t swap;
683 GLboolean ret;
684
685 if (!dPriv->vblFlags ||
686 (dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) ||
687 intelScreen->drmMinor < (intel_fb->pf_active ? 9 : 6))
688 return GL_FALSE;
689
690 interval = driGetVBlankInterval(dPriv);
691
692 swap.seqtype = DRM_VBLANK_ABSOLUTE;
693
694 if (dPriv->vblFlags & VBLANK_FLAG_SYNC) {
695 swap.seqtype |= DRM_VBLANK_NEXTONMISS;
696 } else if (interval == 0) {
697 return GL_FALSE;
698 }
699
700 swap.drawable = dPriv->hHWDrawable;
701 target = swap.sequence = dPriv->vblSeq + interval;
702
703 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
704 swap.seqtype |= DRM_VBLANK_SECONDARY;
705 }
706
707 LOCK_HARDWARE(intel);
708
709 intel_batchbuffer_flush(intel->batch);
710
711 if ( intel_fb->pf_active ) {
712 swap.seqtype |= DRM_VBLANK_FLIP;
713
714 intel_fb->pf_current_page = (((intel->sarea->pf_current_page >>
715 (intel_fb->pf_planes & 0x2)) & 0x3) + 1) %
716 intel_fb->pf_num_pages;
717 }
718
719 if (!drmCommandWriteRead(intel->driFd, DRM_I915_VBLANK_SWAP, &swap,
720 sizeof(swap))) {
721 dPriv->vblSeq = swap.sequence;
722 swap.sequence -= target;
723 *missed_target = swap.sequence > 0 && swap.sequence <= (1 << 23);
724
725 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->vbl_pending =
726 intel_get_renderbuffer(&intel_fb->Base,
727 BUFFER_FRONT_LEFT)->vbl_pending =
728 dPriv->vblSeq;
729
730 if (swap.seqtype & DRM_VBLANK_FLIP) {
731 intel_flip_renderbuffers(intel_fb);
732 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
733 }
734
735 ret = GL_TRUE;
736 } else {
737 if (swap.seqtype & DRM_VBLANK_FLIP) {
738 intel_fb->pf_current_page = ((intel->sarea->pf_current_page >>
739 (intel_fb->pf_planes & 0x2)) & 0x3) %
740 intel_fb->pf_num_pages;
741 }
742
743 ret = GL_FALSE;
744 }
745
746 UNLOCK_HARDWARE(intel);
747
748 return ret;
749 }
750
751 void
752 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
753 {
754 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
755 GET_CURRENT_CONTEXT(ctx);
756 struct intel_context *intel;
757
758 if (ctx == NULL)
759 return;
760
761 intel = intel_context(ctx);
762
763 if (ctx->Visual.doubleBufferMode) {
764 GLboolean missed_target;
765 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
766 int64_t ust;
767
768 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
769
770 if (!intelScheduleSwap(dPriv, &missed_target)) {
771 driWaitForVBlank(dPriv, &missed_target);
772
773 if (!intelPageFlip(dPriv)) {
774 intelCopyBuffer(dPriv, NULL);
775 }
776 }
777
778 intel_fb->swap_count++;
779 (*dri_interface->getUST) (&ust);
780 if (missed_target) {
781 intel_fb->swap_missed_count++;
782 intel_fb->swap_missed_ust = ust - intel_fb->swap_ust;
783 }
784
785 intel_fb->swap_ust = ust;
786 }
787 }
788 else {
789 /* XXX this shouldn't be an error but we can't handle it for now */
790 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
791 }
792 }
793 #endif
794
795 void
796 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
797 {
798 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
799 struct intel_context *intel =
800 (struct intel_context *) dPriv->driContextPriv->driverPrivate;
801 GLcontext *ctx = &intel->ctx;
802
803 if (ctx->Visual.doubleBufferMode) {
804 drm_clip_rect_t rect;
805 rect.x1 = x + dPriv->x;
806 rect.y1 = (dPriv->h - y - h) + dPriv->y;
807 rect.x2 = rect.x1 + w;
808 rect.y2 = rect.y1 + h;
809 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
810 intelCopyBuffer(dPriv, &rect);
811 }
812 }
813 else {
814 /* XXX this shouldn't be an error but we can't handle it for now */
815 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
816 }
817 }
818
819
820 /**
821 * Update the hardware state for drawing into a window or framebuffer object.
822 *
823 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
824 * places within the driver.
825 *
826 * Basically, this needs to be called any time the current framebuffer
827 * changes, the renderbuffers change, or we need to draw into different
828 * color buffers.
829 */
830 void
831 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
832 {
833 struct intel_context *intel = intel_context(ctx);
834 struct intel_region *colorRegion, *depthRegion = NULL;
835 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
836 int front = 0; /* drawing to front color buffer? */
837
838 if (!fb) {
839 /* this can happen during the initial context initialization */
840 return;
841 }
842
843 /* Do this here, note core Mesa, since this function is called from
844 * many places within the driver.
845 */
846 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
847 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
848 _mesa_update_framebuffer(ctx);
849 /* this updates the DrawBuffer's Width/Height if it's a FBO */
850 _mesa_update_draw_buffer_bounds(ctx);
851 }
852
853 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
854 /* this may occur when we're called by glBindFrameBuffer() during
855 * the process of someone setting up renderbuffers, etc.
856 */
857 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
858 return;
859 }
860
861 if (fb->Name)
862 intel_validate_paired_depth_stencil(ctx, fb);
863
864 /*
865 * How many color buffers are we drawing into?
866 */
867 if (fb->_NumColorDrawBuffers[0] != 1) {
868 /* writing to 0 or 2 or 4 color buffers */
869 /*_mesa_debug(ctx, "Software rendering\n");*/
870 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
871 colorRegion = NULL;
872 }
873 else {
874 /* draw to exactly one color buffer */
875 /*_mesa_debug(ctx, "Hardware rendering\n");*/
876 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
877 if (fb->_ColorDrawBufferMask[0] == BUFFER_BIT_FRONT_LEFT) {
878 front = 1;
879 }
880
881 /*
882 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
883 * And set up cliprects.
884 */
885 if (fb->Name == 0) {
886 /* drawing to window system buffer */
887 if (front) {
888 intelSetFrontClipRects(intel);
889 colorRegion = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
890 }
891 else {
892 intelSetBackClipRects(intel);
893 colorRegion = intel_get_rb_region(fb, BUFFER_BACK_LEFT);
894 }
895 }
896 else {
897 /* drawing to user-created FBO */
898 struct intel_renderbuffer *irb;
899 intelSetRenderbufferClipRects(intel);
900 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0][0]);
901 colorRegion = (irb && irb->region) ? irb->region : NULL;
902 }
903 }
904
905 /* Update culling direction which changes depending on the
906 * orientation of the buffer:
907 */
908 if (ctx->Driver.FrontFace)
909 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
910 else
911 ctx->NewState |= _NEW_POLYGON;
912
913 if (!colorRegion) {
914 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
915 }
916 else {
917 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
918 }
919
920 /***
921 *** Get depth buffer region and check if we need a software fallback.
922 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
923 ***/
924 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
925 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
926 if (irbDepth && irbDepth->region) {
927 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
928 depthRegion = irbDepth->region;
929 }
930 else {
931 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
932 depthRegion = NULL;
933 }
934 }
935 else {
936 /* not using depth buffer */
937 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
938 depthRegion = NULL;
939 }
940
941 /***
942 *** Stencil buffer
943 *** This can only be hardware accelerated if we're using a
944 *** combined DEPTH_STENCIL buffer (for now anyway).
945 ***/
946 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
947 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
948 if (irbStencil && irbStencil->region) {
949 ASSERT(irbStencil->Base._ActualFormat == GL_DEPTH24_STENCIL8_EXT);
950 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
951 /* need to re-compute stencil hw state */
952 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
953 if (!depthRegion)
954 depthRegion = irbStencil->region;
955 }
956 else {
957 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
958 }
959 }
960 else {
961 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
962 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
963 /* need to re-compute stencil hw state */
964 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
965 }
966
967 /*
968 * Update depth test state
969 */
970 if (ctx->Depth.Test && fb->Visual.depthBits > 0) {
971 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_TRUE);
972 }
973 else {
974 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_FALSE);
975 }
976
977 /**
978 ** Release old regions, reference new regions
979 **/
980 #if 0 /* XXX FBO: this seems to be redundant with i915_state_draw_region() */
981 if (intel->draw_region != colorRegion) {
982 intel_region_release(&intel->draw_region);
983 intel_region_reference(&intel->draw_region, colorRegion);
984 }
985 if (intel->intelScreen->depth_region != depthRegion) {
986 intel_region_release(&intel->intelScreen->depth_region);
987 intel_region_reference(&intel->intelScreen->depth_region, depthRegion);
988 }
989 #endif
990
991 intel->vtbl.set_draw_region(intel, colorRegion, depthRegion);
992
993 /* update viewport since it depends on window size */
994 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
995 ctx->Viewport.Width, ctx->Viewport.Height);
996
997 /* Update hardware scissor */
998 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
999 ctx->Scissor.Width, ctx->Scissor.Height);
1000 }
1001
1002
1003 static void
1004 intelDrawBuffer(GLcontext * ctx, GLenum mode)
1005 {
1006 intel_draw_buffer(ctx, ctx->DrawBuffer);
1007 }
1008
1009
1010 static void
1011 intelReadBuffer(GLcontext * ctx, GLenum mode)
1012 {
1013 if (ctx->ReadBuffer == ctx->DrawBuffer) {
1014 /* This will update FBO completeness status.
1015 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1016 * refers to a missing renderbuffer. Calling glReadBuffer can set
1017 * that straight and can make the drawing buffer complete.
1018 */
1019 intel_draw_buffer(ctx, ctx->DrawBuffer);
1020 }
1021 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1022 * reference ctx->ReadBuffer and do appropriate state checks.
1023 */
1024 }
1025
1026
1027 void
1028 intelInitBufferFuncs(struct dd_function_table *functions)
1029 {
1030 functions->Clear = intelClear;
1031 functions->DrawBuffer = intelDrawBuffer;
1032 functions->ReadBuffer = intelReadBuffer;
1033 }