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