4b2a4acaf98130cf9d59285647c74d8123149f00
[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:
129 * intel->numClipRects
130 * intel->pClipRects
131 */
132 static void
133 intelSetRenderbufferClipRects(struct intel_context *intel)
134 {
135 /* zero-sized buffers might be legal? */
136 assert(intel->ctx.DrawBuffer->Width > 0);
137 assert(intel->ctx.DrawBuffer->Height > 0);
138 intel->fboRect.x1 = 0;
139 intel->fboRect.y1 = 0;
140 intel->fboRect.x2 = intel->ctx.DrawBuffer->Width;
141 intel->fboRect.y2 = intel->ctx.DrawBuffer->Height;
142 intel->numClipRects = 1;
143 intel->pClipRects = &intel->fboRect;
144 }
145
146
147 /**
148 * This will be called whenever the currently bound window is moved/resized.
149 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
150 */
151 void
152 intelWindowMoved(struct intel_context *intel)
153 {
154 GLcontext *ctx = &intel->ctx;
155 __DRIdrawablePrivate *dPriv = intel->driDrawable;
156 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
157
158 if (!intel->ctx.DrawBuffer) {
159 /* when would this happen? -BP */
160 assert(0);
161 intel->numClipRects = 0;
162 }
163
164 /* Update Mesa's notion of window size */
165 driUpdateFramebufferSize(ctx, dPriv);
166 intel_fb->Base.Initialized = GL_TRUE; /* XXX remove someday */
167
168 {
169 drmI830Sarea *sarea = intel->sarea;
170 drm_clip_rect_t drw_rect = { .x1 = dPriv->x, .x2 = dPriv->x + dPriv->w,
171 .y1 = dPriv->y, .y2 = dPriv->y + dPriv->h };
172 drm_clip_rect_t pipeA_rect = { .x1 = sarea->pipeA_x, .y1 = sarea->pipeA_y,
173 .x2 = sarea->pipeA_x + sarea->pipeA_w,
174 .y2 = sarea->pipeA_y + sarea->pipeA_h };
175 drm_clip_rect_t pipeB_rect = { .x1 = sarea->pipeB_x, .y1 = sarea->pipeB_y,
176 .x2 = sarea->pipeB_x + sarea->pipeB_w,
177 .y2 = sarea->pipeB_y + sarea->pipeB_h };
178 GLint areaA = driIntersectArea( drw_rect, pipeA_rect );
179 GLint areaB = driIntersectArea( drw_rect, pipeB_rect );
180 GLuint flags = intel_fb->vblank_flags;
181 GLboolean pf_active;
182 GLint pf_pipes;
183
184 /* Update page flipping info
185 */
186 pf_pipes = 0;
187
188 if (areaA > 0)
189 pf_pipes |= 1;
190
191 if (areaB > 0)
192 pf_pipes |= 2;
193
194 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
195 (intel_fb->pf_pipes & 0x2)) & 0x3;
196
197 intel_fb->pf_num_pages = 2 /*intel->intelScreen->third.handle ? 3 : 2*/;
198
199 pf_active = pf_pipes && (pf_pipes & intel->sarea->pf_active) == pf_pipes;
200
201 if (INTEL_DEBUG & DEBUG_LOCK)
202 if (pf_active != intel_fb->pf_active)
203 _mesa_printf("%s - Page flipping %sactive\n", __progname,
204 pf_active ? "" : "in");
205
206 if (pf_active) {
207 /* Sync pages between pipes if we're flipping on both at the same time */
208 if (pf_pipes == 0x3 && pf_pipes != intel_fb->pf_pipes &&
209 (intel->sarea->pf_current_page & 0x3) !=
210 (((intel->sarea->pf_current_page) >> 2) & 0x3)) {
211 drm_i915_flip_t flip;
212
213 if (intel_fb->pf_current_page ==
214 (intel->sarea->pf_current_page & 0x3)) {
215 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
216 * lockups for unknown reasons.
217 */
218 intel->sarea->pf_current_page =
219 intel->sarea->pf_current_page & 0x3;
220 intel->sarea->pf_current_page |=
221 ((intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
222 intel_fb->pf_num_pages) << 2;
223
224 flip.pipes = 0x2;
225 } else {
226 intel->sarea->pf_current_page =
227 intel->sarea->pf_current_page & (0x3 << 2);
228 intel->sarea->pf_current_page |=
229 (intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
230 intel_fb->pf_num_pages;
231
232 flip.pipes = 0x1;
233 }
234
235 drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
236 }
237
238 intel_fb->pf_pipes = pf_pipes;
239 }
240
241 intel_fb->pf_active = pf_active;
242 intel_flip_renderbuffers(intel_fb);
243 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
244
245 /* Update vblank info
246 */
247 if (areaB > areaA || (areaA == areaB && areaB > 0)) {
248 flags = intel_fb->vblank_flags | VBLANK_FLAG_SECONDARY;
249 } else {
250 flags = intel_fb->vblank_flags & ~VBLANK_FLAG_SECONDARY;
251 }
252
253 if (flags != intel_fb->vblank_flags) {
254 drmVBlank vbl;
255 int i;
256
257 vbl.request.type = DRM_VBLANK_ABSOLUTE;
258
259 if ( intel_fb->vblank_flags & VBLANK_FLAG_SECONDARY ) {
260 vbl.request.type |= DRM_VBLANK_SECONDARY;
261 }
262
263 for (i = 0; i < intel_fb->pf_num_pages; i++) {
264 if (!intel_fb->color_rb[i])
265 continue;
266
267 vbl.request.sequence = intel_fb->color_rb[i]->vbl_pending;
268 drmWaitVBlank(intel->driFd, &vbl);
269 }
270
271 intel_fb->vblank_flags = flags;
272 driGetCurrentVBlank(dPriv, intel_fb->vblank_flags, &intel_fb->vbl_seq);
273 intel_fb->vbl_waited = intel_fb->vbl_seq;
274
275 for (i = 0; i < intel_fb->pf_num_pages; i++) {
276 if (intel_fb->color_rb[i])
277 intel_fb->color_rb[i]->vbl_pending = intel_fb->vbl_waited;
278 }
279 }
280 }
281
282 /* Update hardware scissor */
283 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
284 ctx->Scissor.Width, ctx->Scissor.Height);
285
286 /* Re-calculate viewport related state */
287 ctx->Driver.DepthRange( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
288 }
289
290
291
292 /* A true meta version of this would be very simple and additionally
293 * machine independent. Maybe we'll get there one day.
294 */
295 static void
296 intelClearWithTris(struct intel_context *intel, GLbitfield mask)
297 {
298 GLcontext *ctx = &intel->ctx;
299 struct gl_framebuffer *fb = ctx->DrawBuffer;
300 drm_clip_rect_t clear;
301
302 if (INTEL_DEBUG & DEBUG_BLIT)
303 _mesa_printf("%s 0x%x\n", __FUNCTION__, mask);
304
305 LOCK_HARDWARE(intel);
306
307 if (intel->numClipRects) {
308 GLint cx, cy, cw, ch;
309 GLuint buf;
310
311 intel->vtbl.install_meta_state(intel);
312
313 /* Get clear bounds after locking */
314 cx = fb->_Xmin;
315 cy = fb->_Ymin;
316 ch = fb->_Ymax - cx;
317 cw = fb->_Xmax - cy;
318
319 /* note: regardless of 'all', cx, cy, cw, ch are now correct */
320 clear.x1 = cx;
321 clear.y1 = cy;
322 clear.x2 = cx + cw;
323 clear.y2 = cy + ch;
324
325 /* Back and stencil cliprects are the same. Try and do both
326 * buffers at once:
327 */
328 if (mask &
329 (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH)) {
330 struct intel_region *backRegion =
331 intel_get_rb_region(fb, BUFFER_BACK_LEFT);
332 struct intel_region *depthRegion =
333 intel_get_rb_region(fb, BUFFER_DEPTH);
334 const GLuint clearColor = (backRegion && backRegion->cpp == 4)
335 ? intel->ClearColor8888 : intel->ClearColor565;
336
337 intel->vtbl.meta_draw_region(intel, backRegion, depthRegion);
338
339 if (mask & BUFFER_BIT_BACK_LEFT)
340 intel->vtbl.meta_color_mask(intel, GL_TRUE);
341 else
342 intel->vtbl.meta_color_mask(intel, GL_FALSE);
343
344 if (mask & BUFFER_BIT_STENCIL)
345 intel->vtbl.meta_stencil_replace(intel,
346 intel->ctx.Stencil.WriteMask[0],
347 intel->ctx.Stencil.Clear);
348 else
349 intel->vtbl.meta_no_stencil_write(intel);
350
351 if (mask & BUFFER_BIT_DEPTH)
352 intel->vtbl.meta_depth_replace(intel);
353 else
354 intel->vtbl.meta_no_depth_write(intel);
355
356 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
357 * drawing origin may not be correctly emitted.
358 */
359 intel_meta_draw_quad(intel, clear.x1, clear.x2, clear.y1, clear.y2,
360 intel->ctx.Depth.Clear, clearColor, 0, 0, 0, 0); /* texcoords */
361
362 mask &=
363 ~(BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH);
364 }
365
366 /* clear the remaining (color) renderbuffers */
367 for (buf = 0; buf < BUFFER_COUNT && mask; buf++) {
368 const GLuint bufBit = 1 << buf;
369 if (mask & bufBit) {
370 struct intel_renderbuffer *irbColor =
371 intel_renderbuffer(fb->Attachment[buf].Renderbuffer);
372 GLuint color = (irbColor->region->cpp == 4)
373 ? intel->ClearColor8888 : intel->ClearColor565;
374
375 ASSERT(irbColor);
376
377 intel->vtbl.meta_no_depth_write(intel);
378 intel->vtbl.meta_no_stencil_write(intel);
379 intel->vtbl.meta_color_mask(intel, GL_TRUE);
380 intel->vtbl.meta_draw_region(intel, irbColor->region, NULL);
381
382 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
383 * drawing origin may not be correctly emitted.
384 */
385 intel_meta_draw_quad(intel, clear.x1, clear.x2, clear.y1, clear.y2, 0, /* depth clear val */
386 color, 0, 0, 0, 0); /* texcoords */
387
388 mask &= ~bufBit;
389 }
390 }
391
392 intel->vtbl.leave_meta_state(intel);
393 intel_batchbuffer_flush(intel->batch);
394 }
395 UNLOCK_HARDWARE(intel);
396 }
397
398
399 /**
400 * Called by ctx->Driver.Clear.
401 */
402 static void
403 intelClear(GLcontext *ctx, GLbitfield mask)
404 {
405 struct intel_context *intel = intel_context(ctx);
406 const GLuint colorMask = *((GLuint *) & ctx->Color.ColorMask);
407 GLbitfield tri_mask = 0;
408 GLbitfield blit_mask = 0;
409 GLbitfield swrast_mask = 0;
410 struct gl_framebuffer *fb = ctx->DrawBuffer;
411 GLuint i;
412
413 if (0)
414 fprintf(stderr, "%s\n", __FUNCTION__);
415
416 /* HW color buffers (front, back, aux, generic FBO, etc) */
417 if (colorMask == ~0) {
418 /* clear all R,G,B,A */
419 /* XXX FBO: need to check if colorbuffers are software RBOs! */
420 blit_mask |= (mask & BUFFER_BITS_COLOR);
421 }
422 else {
423 /* glColorMask in effect */
424 tri_mask |= (mask & BUFFER_BITS_COLOR);
425 }
426
427 /* HW stencil */
428 if (mask & BUFFER_BIT_STENCIL) {
429 const struct intel_region *stencilRegion
430 = intel_get_rb_region(fb, BUFFER_STENCIL);
431 if (stencilRegion) {
432 /* have hw stencil */
433 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
434 /* not clearing all stencil bits, so use triangle clearing */
435 tri_mask |= BUFFER_BIT_STENCIL;
436 }
437 else {
438 /* clearing all stencil bits, use blitting */
439 blit_mask |= BUFFER_BIT_STENCIL;
440 }
441 }
442 }
443
444 /* HW depth */
445 if (mask & BUFFER_BIT_DEPTH) {
446 /* clear depth with whatever method is used for stencil (see above) */
447 if (tri_mask & BUFFER_BIT_STENCIL)
448 tri_mask |= BUFFER_BIT_DEPTH;
449 else
450 blit_mask |= BUFFER_BIT_DEPTH;
451 }
452
453 /* SW fallback clearing */
454 swrast_mask = mask & ~tri_mask & ~blit_mask;
455
456 for (i = 0; i < BUFFER_COUNT; i++) {
457 GLuint bufBit = 1 << i;
458 if ((blit_mask | tri_mask) & bufBit) {
459 if (!fb->Attachment[i].Renderbuffer->ClassID) {
460 blit_mask &= ~bufBit;
461 tri_mask &= ~bufBit;
462 swrast_mask |= bufBit;
463 }
464 }
465 }
466
467
468 intelFlush(ctx); /* XXX intelClearWithBlit also does this */
469
470 if (blit_mask)
471 intelClearWithBlit(ctx, blit_mask);
472
473 if (tri_mask)
474 intelClearWithTris(intel, tri_mask);
475
476 if (swrast_mask)
477 _swrast_Clear(ctx, swrast_mask);
478 }
479
480
481 /* Emit wait for pending flips */
482 void
483 intel_wait_flips(struct intel_context *intel, GLuint batch_flags)
484 {
485 struct intel_framebuffer *intel_fb =
486 (struct intel_framebuffer *) intel->ctx.DrawBuffer;
487 struct intel_renderbuffer *intel_rb =
488 intel_get_renderbuffer(&intel_fb->Base,
489 intel_fb->Base._ColorDrawBufferMask[0] ==
490 BUFFER_BIT_FRONT_LEFT ? BUFFER_FRONT_LEFT :
491 BUFFER_BACK_LEFT);
492
493 if (intel_fb->Base.Name == 0 && intel_rb->pf_pending == intel_fb->pf_seq) {
494 GLint pf_pipes = intel_fb->pf_pipes;
495 BATCH_LOCALS;
496
497 /* Wait for pending flips to take effect */
498 BEGIN_BATCH(2, batch_flags);
499 OUT_BATCH(pf_pipes & 0x1 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP)
500 : 0);
501 OUT_BATCH(pf_pipes & 0x2 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_B_FLIP)
502 : 0);
503 ADVANCE_BATCH();
504
505 intel_rb->pf_pending--;
506 }
507 }
508
509 #if 0
510 /* Flip the front & back buffers
511 */
512 static GLboolean
513 intelPageFlip(const __DRIdrawablePrivate * dPriv)
514 {
515 struct intel_context *intel;
516 int ret;
517 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
518
519 if (INTEL_DEBUG & DEBUG_IOCTL)
520 fprintf(stderr, "%s\n", __FUNCTION__);
521
522 assert(dPriv);
523 assert(dPriv->driContextPriv);
524 assert(dPriv->driContextPriv->driverPrivate);
525
526 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
527
528 if (intel->intelScreen->drmMinor < 9)
529 return GL_FALSE;
530
531 intelFlush(&intel->ctx);
532
533 ret = 0;
534
535 LOCK_HARDWARE(intel);
536
537 if (dPriv->numClipRects && intel_fb->pf_active) {
538 drm_i915_flip_t flip;
539
540 flip.pipes = intel_fb->pf_pipes;
541
542 ret = drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
543 }
544
545 UNLOCK_HARDWARE(intel);
546
547 if (ret || !intel_fb->pf_active)
548 return GL_FALSE;
549
550 if (!dPriv->numClipRects) {
551 usleep(10000); /* throttle invisible client 10ms */
552 }
553
554 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
555 (intel_fb->pf_pipes & 0x2)) & 0x3;
556
557 if (dPriv->numClipRects != 0) {
558 intel_get_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT)->pf_pending =
559 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->pf_pending =
560 ++intel_fb->pf_seq;
561 }
562
563 intel_flip_renderbuffers(intel_fb);
564 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
565
566 if (INTEL_DEBUG & DEBUG_IOCTL)
567 fprintf(stderr, "%s: success\n", __FUNCTION__);
568
569 return GL_TRUE;
570 }
571 #endif
572
573
574 static GLboolean
575 intelScheduleSwap(const __DRIdrawablePrivate * dPriv, GLboolean *missed_target)
576 {
577 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
578 unsigned int interval = driGetVBlankInterval(dPriv, intel_fb->vblank_flags);
579 struct intel_context *intel =
580 intelScreenContext(dPriv->driScreenPriv->private);
581 const intelScreenPrivate *intelScreen = intel->intelScreen;
582 unsigned int target;
583 drm_i915_vblank_swap_t swap;
584 GLboolean ret;
585
586 if ((intel_fb->vblank_flags & VBLANK_FLAG_NO_IRQ) ||
587 intelScreen->drmMinor < (intel_fb->pf_active ? 9 : 6))
588 return GL_FALSE;
589
590 swap.seqtype = DRM_VBLANK_ABSOLUTE;
591
592 if (intel_fb->vblank_flags & VBLANK_FLAG_SYNC) {
593 swap.seqtype |= DRM_VBLANK_NEXTONMISS;
594 } else if (interval == 0) {
595 return GL_FALSE;
596 }
597
598 swap.drawable = dPriv->hHWDrawable;
599 target = swap.sequence = intel_fb->vbl_seq + interval;
600
601 if ( intel_fb->vblank_flags & VBLANK_FLAG_SECONDARY ) {
602 swap.seqtype |= DRM_VBLANK_SECONDARY;
603 }
604
605 LOCK_HARDWARE(intel);
606
607 intel_batchbuffer_flush(intel->batch);
608
609 if ( intel_fb->pf_active ) {
610 swap.seqtype |= DRM_VBLANK_FLIP;
611
612 intel_fb->pf_current_page = (((intel->sarea->pf_current_page >>
613 (intel_fb->pf_pipes & 0x2)) & 0x3) + 1) %
614 intel_fb->pf_num_pages;
615 }
616
617 if (!drmCommandWriteRead(intel->driFd, DRM_I915_VBLANK_SWAP, &swap,
618 sizeof(swap))) {
619 intel_fb->vbl_seq = swap.sequence;
620 swap.sequence -= target;
621 *missed_target = swap.sequence > 0 && swap.sequence <= (1 << 23);
622
623 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->vbl_pending =
624 intel_get_renderbuffer(&intel_fb->Base,
625 BUFFER_FRONT_LEFT)->vbl_pending =
626 intel_fb->vbl_seq;
627
628 if (swap.seqtype & DRM_VBLANK_FLIP) {
629 intel_flip_renderbuffers(intel_fb);
630 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
631 }
632
633 ret = GL_TRUE;
634 } else {
635 if (swap.seqtype & DRM_VBLANK_FLIP) {
636 intel_fb->pf_current_page = ((intel->sarea->pf_current_page >>
637 (intel_fb->pf_pipes & 0x2)) & 0x3) %
638 intel_fb->pf_num_pages;
639 }
640
641 ret = GL_FALSE;
642 }
643
644 UNLOCK_HARDWARE(intel);
645
646 return ret;
647 }
648
649 void
650 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
651 {
652 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
653 GET_CURRENT_CONTEXT(ctx);
654 struct intel_context *intel;
655
656 if (ctx == NULL)
657 return;
658
659 intel = intel_context(ctx);
660
661 if (ctx->Visual.doubleBufferMode) {
662 GLboolean missed_target;
663 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
664 int64_t ust;
665
666 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
667
668 if (!intelScheduleSwap(dPriv, &missed_target)) {
669 driWaitForVBlank(dPriv, &intel_fb->vbl_seq, intel_fb->vblank_flags,
670 &missed_target);
671
672 intelCopyBuffer(dPriv, NULL);
673 }
674
675 intel_fb->swap_count++;
676 (*dri_interface->getUST) (&ust);
677 if (missed_target) {
678 intel_fb->swap_missed_count++;
679 intel_fb->swap_missed_ust = ust - intel_fb->swap_ust;
680 }
681
682 intel_fb->swap_ust = ust;
683 }
684 }
685 else {
686 /* XXX this shouldn't be an error but we can't handle it for now */
687 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
688 }
689 }
690
691 void
692 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
693 {
694 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
695 struct intel_context *intel =
696 (struct intel_context *) dPriv->driContextPriv->driverPrivate;
697 GLcontext *ctx = &intel->ctx;
698
699 if (ctx->Visual.doubleBufferMode) {
700 drm_clip_rect_t rect;
701 /* fixup cliprect (driDrawable may have changed?) later */
702 rect.x1 = x;
703 rect.y1 = y;
704 rect.x2 = w;
705 rect.y2 = h;
706 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
707 intelCopyBuffer(dPriv, &rect);
708 }
709 }
710 else {
711 /* XXX this shouldn't be an error but we can't handle it for now */
712 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
713 }
714 }
715
716
717 /**
718 * Update the hardware state for drawing into a window or framebuffer object.
719 *
720 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
721 * places within the driver.
722 *
723 * Basically, this needs to be called any time the current framebuffer
724 * changes, the renderbuffers change, or we need to draw into different
725 * color buffers.
726 */
727 void
728 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
729 {
730 struct intel_context *intel = intel_context(ctx);
731 struct intel_region *colorRegion, *depthRegion = NULL;
732 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
733
734 if (!fb) {
735 /* this can happen during the initial context initialization */
736 return;
737 }
738
739 /* Do this here, not core Mesa, since this function is called from
740 * many places within the driver.
741 */
742 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
743 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
744 _mesa_update_framebuffer(ctx);
745 /* this updates the DrawBuffer's Width/Height if it's a FBO */
746 _mesa_update_draw_buffer_bounds(ctx);
747 }
748
749 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
750 /* this may occur when we're called by glBindFrameBuffer() during
751 * the process of someone setting up renderbuffers, etc.
752 */
753 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
754 return;
755 }
756
757 if (fb->Name)
758 intel_validate_paired_depth_stencil(ctx, fb);
759
760 /*
761 * How many color buffers are we drawing into?
762 */
763 if (fb->_NumColorDrawBuffers[0] != 1) {
764 /* writing to 0 or 2 or 4 color buffers */
765 /*_mesa_debug(ctx, "Software rendering\n");*/
766 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
767 }
768 else {
769 /* draw to exactly one color buffer */
770 /*_mesa_debug(ctx, "Hardware rendering\n");*/
771 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
772 }
773
774 /*
775 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
776 * And set up cliprects.
777 */
778 {
779 struct intel_renderbuffer *irb;
780 intelSetRenderbufferClipRects(intel);
781 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0][0]);
782 colorRegion = (irb && irb->region) ? irb->region : NULL;
783 }
784
785 /* Update culling direction which changes depending on the
786 * orientation of the buffer:
787 */
788 if (ctx->Driver.FrontFace)
789 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
790 else
791 ctx->NewState |= _NEW_POLYGON;
792
793 if (!colorRegion) {
794 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
795 }
796 else {
797 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
798 }
799
800 /***
801 *** Get depth buffer region and check if we need a software fallback.
802 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
803 ***/
804 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
805 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
806 if (irbDepth && irbDepth->region) {
807 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
808 depthRegion = irbDepth->region;
809 }
810 else {
811 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
812 depthRegion = NULL;
813 }
814 }
815 else {
816 /* not using depth buffer */
817 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
818 depthRegion = NULL;
819 }
820
821 /***
822 *** Stencil buffer
823 *** This can only be hardware accelerated if we're using a
824 *** combined DEPTH_STENCIL buffer (for now anyway).
825 ***/
826 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
827 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
828 if (irbStencil && irbStencil->region) {
829 ASSERT(irbStencil->Base._ActualFormat == GL_DEPTH24_STENCIL8_EXT);
830 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
831 /* need to re-compute stencil hw state */
832 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
833 if (!depthRegion)
834 depthRegion = irbStencil->region;
835 }
836 else {
837 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
838 }
839 }
840 else {
841 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
842 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
843 /* need to re-compute stencil hw state */
844 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
845 }
846
847
848 /**
849 ** Release old regions, reference new regions
850 **/
851
852 intel->vtbl.set_draw_region(intel, colorRegion, depthRegion);
853
854 /* update viewport since it depends on window size */
855 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
856 ctx->Viewport.Width, ctx->Viewport.Height);
857
858 /* Update hardware scissor */
859 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
860 ctx->Scissor.Width, ctx->Scissor.Height);
861 }
862
863
864 static void
865 intelDrawBuffer(GLcontext * ctx, GLenum mode)
866 {
867 intel_draw_buffer(ctx, ctx->DrawBuffer);
868 }
869
870
871 static void
872 intelReadBuffer(GLcontext * ctx, GLenum mode)
873 {
874 if (ctx->ReadBuffer == ctx->DrawBuffer) {
875 /* This will update FBO completeness status.
876 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
877 * refers to a missing renderbuffer. Calling glReadBuffer can set
878 * that straight and can make the drawing buffer complete.
879 */
880 intel_draw_buffer(ctx, ctx->DrawBuffer);
881 }
882 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
883 * reference ctx->ReadBuffer and do appropriate state checks.
884 */
885 }
886
887
888 void
889 intelInitBufferFuncs(struct dd_function_table *functions)
890 {
891 functions->Clear = intelClear;
892 functions->DrawBuffer = intelDrawBuffer;
893 functions->ReadBuffer = intelReadBuffer;
894 }