1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
40 #include "drirenderbuffer.h"
41 #include "framebuffer.h"
42 #include "swrast/swrast.h"
46 /* This block can be removed when libdrm >= 2.3.1 is required */
48 #ifndef DRM_IOCTL_I915_FLIP
50 #define DRM_VBLANK_FLIP 0x8000000
52 typedef struct drm_i915_flip
{
56 #undef DRM_IOCTL_I915_FLIP
57 #define DRM_IOCTL_I915_FLIP DRM_IOW(DRM_COMMAND_BASE + DRM_I915_FLIP, \
62 #define FILE_DEBUG_FLAG DEBUG_BLIT
65 * XXX move this into a new dri/common/cliprects.c file.
68 intel_intersect_cliprects(drm_clip_rect_t
* dst
,
69 const drm_clip_rect_t
* a
,
70 const drm_clip_rect_t
* b
)
74 GLint bw
= b
->x2
- bx
;
75 GLint bh
= b
->y2
- by
;
78 bw
-= a
->x1
- bx
, bx
= a
->x1
;
80 bh
-= a
->y1
- by
, by
= a
->y1
;
99 * Return pointer to current color drawing region, or NULL.
101 struct intel_region
*
102 intel_drawbuf_region(struct intel_context
*intel
)
104 struct intel_renderbuffer
*irbColor
=
105 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0][0]);
107 return irbColor
->region
;
113 * Return pointer to current color reading region, or NULL.
115 struct intel_region
*
116 intel_readbuf_region(struct intel_context
*intel
)
118 struct intel_renderbuffer
*irb
119 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
129 * Update the following fields for rendering to a user-created FBO:
130 * intel->numClipRects
136 intelSetRenderbufferClipRects(struct intel_context
*intel
)
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
;
152 * As above, but for rendering to front buffer of a window.
153 * \sa intelSetRenderbufferClipRects
156 intelSetFrontClipRects(struct intel_context
*intel
)
158 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
163 intel
->numClipRects
= dPriv
->numClipRects
;
164 intel
->pClipRects
= dPriv
->pClipRects
;
165 intel
->drawX
= dPriv
->x
;
166 intel
->drawY
= dPriv
->y
;
171 * As above, but for rendering to back buffer of a window.
174 intelSetBackClipRects(struct intel_context
*intel
)
176 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
177 struct intel_framebuffer
*intel_fb
;
182 intel_fb
= dPriv
->driverPrivate
;
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
;
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
;
202 * This will be called whenever the currently bound window is moved/resized.
203 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
206 intelWindowMoved(struct intel_context
*intel
)
208 GLcontext
*ctx
= &intel
->ctx
;
209 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
210 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
212 if (!intel
->ctx
.DrawBuffer
) {
213 /* when would this happen? -BP */
214 intelSetFrontClipRects(intel
);
216 else if (intel
->ctx
.DrawBuffer
->Name
!= 0) {
217 /* drawing to user-created FBO - do nothing */
218 /* Cliprects would be set from intelDrawBuffer() */
221 /* drawing to a window */
222 switch (intel_fb
->Base
._ColorDrawBufferMask
[0]) {
223 case BUFFER_BIT_FRONT_LEFT
:
224 intelSetFrontClipRects(intel
);
226 case BUFFER_BIT_BACK_LEFT
:
227 intelSetBackClipRects(intel
);
230 /* glDrawBuffer(GL_NONE or GL_FRONT_AND_BACK): software fallback */
231 intelSetFrontClipRects(intel
);
235 if (intel
->intelScreen
->driScrnPriv
->ddx_version
.minor
>= 7) {
236 drmI830Sarea
*sarea
= intel
->sarea
;
237 drm_clip_rect_t drw_rect
= { .x1
= dPriv
->x
, .x2
= dPriv
->x
+ dPriv
->w
,
238 .y1
= dPriv
->y
, .y2
= dPriv
->y
+ dPriv
->h
};
239 drm_clip_rect_t planeA_rect
= { .x1
= sarea
->planeA_x
, .y1
= sarea
->planeA_y
,
240 .x2
= sarea
->planeA_x
+ sarea
->planeA_w
,
241 .y2
= sarea
->planeA_y
+ sarea
->planeA_h
};
242 drm_clip_rect_t planeB_rect
= { .x1
= sarea
->planeB_x
, .y1
= sarea
->planeB_y
,
243 .x2
= sarea
->planeB_x
+ sarea
->planeB_w
,
244 .y2
= sarea
->planeB_y
+ sarea
->planeB_h
};
245 GLint areaA
= driIntersectArea( drw_rect
, planeA_rect
);
246 GLint areaB
= driIntersectArea( drw_rect
, planeB_rect
);
247 GLuint flags
= dPriv
->vblFlags
;
251 /* Update page flipping info
261 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
262 (intel_fb
->pf_planes
& 0x2)) & 0x3;
264 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
266 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
268 if (INTEL_DEBUG
& DEBUG_LOCK
)
269 if (pf_active
!= intel_fb
->pf_active
)
270 _mesa_printf("%s - Page flipping %sactive\n", __progname
,
271 pf_active
? "" : "in");
274 /* Sync pages between planes if flipping on both at the same time */
275 if (pf_planes
== 0x3 && pf_planes
!= intel_fb
->pf_planes
&&
276 (intel
->sarea
->pf_current_page
& 0x3) !=
277 (((intel
->sarea
->pf_current_page
) >> 2) & 0x3)) {
278 drm_i915_flip_t flip
;
280 if (intel_fb
->pf_current_page
==
281 (intel
->sarea
->pf_current_page
& 0x3)) {
282 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
283 * lockups for unknown reasons.
285 intel
->sarea
->pf_current_page
=
286 intel
->sarea
->pf_current_page
& 0x3;
287 intel
->sarea
->pf_current_page
|=
288 ((intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
289 intel_fb
->pf_num_pages
) << 2;
293 intel
->sarea
->pf_current_page
=
294 intel
->sarea
->pf_current_page
& (0x3 << 2);
295 intel
->sarea
->pf_current_page
|=
296 (intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
297 intel_fb
->pf_num_pages
;
302 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
305 intel_fb
->pf_planes
= pf_planes
;
308 intel_fb
->pf_active
= pf_active
;
309 intel_flip_renderbuffers(intel_fb
);
310 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
312 /* Update vblank info
314 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
315 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
317 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
320 /* Check to see if we changed pipes */
321 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
322 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
328 * Deal with page flipping
330 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
332 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
333 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
336 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
337 if (!intel_fb
->color_rb
[i
] ||
338 (intel_fb
->vbl_waited
- intel_fb
->color_rb
[i
]->vbl_pending
) <=
342 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
343 drmWaitVBlank(intel
->driFd
, &vbl
);
347 * Update msc_base from old pipe
349 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
350 dPriv
->msc_base
= count
;
352 * Then get new vblank_base and vblSeq values
354 dPriv
->vblFlags
= flags
;
355 driGetCurrentVBlank(dPriv
);
356 dPriv
->vblank_base
= dPriv
->vblSeq
;
358 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
360 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
361 if (intel_fb
->color_rb
[i
])
362 intel_fb
->color_rb
[i
]->vbl_pending
= intel_fb
->vbl_waited
;
366 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
369 /* Update Mesa's notion of window size */
370 driUpdateFramebufferSize(ctx
, dPriv
);
371 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
373 /* Update hardware scissor */
374 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
375 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
377 /* Re-calculate viewport related state */
378 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
383 /* A true meta version of this would be very simple and additionally
384 * machine independent. Maybe we'll get there one day.
387 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
389 GLcontext
*ctx
= &intel
->ctx
;
390 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
393 intel
->vtbl
.install_meta_state(intel
);
395 /* Back and stencil cliprects are the same. Try and do both
398 if (mask
& (BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
)) {
399 struct intel_region
*backRegion
=
400 intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
401 struct intel_region
*depthRegion
=
402 intel_get_rb_region(fb
, BUFFER_DEPTH
);
404 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
406 if (mask
& BUFFER_BIT_BACK_LEFT
)
407 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
409 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
411 if (mask
& BUFFER_BIT_STENCIL
)
412 intel
->vtbl
.meta_stencil_replace(intel
,
413 intel
->ctx
.Stencil
.WriteMask
[0],
414 intel
->ctx
.Stencil
.Clear
);
416 intel
->vtbl
.meta_no_stencil_write(intel
);
418 if (mask
& BUFFER_BIT_DEPTH
)
419 intel
->vtbl
.meta_depth_replace(intel
);
421 intel
->vtbl
.meta_no_depth_write(intel
);
423 intel_meta_draw_quad(intel
,
428 intel
->ctx
.Depth
.Clear
, intel
->ClearColor8888
,
429 0, 0, 0, 0); /* texcoords */
431 mask
&= ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
434 /* clear the remaining (color) renderbuffers */
435 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
436 const GLuint bufBit
= 1 << buf
;
438 struct intel_renderbuffer
*irbColor
=
439 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
443 intel
->vtbl
.meta_no_depth_write(intel
);
444 intel
->vtbl
.meta_no_stencil_write(intel
);
445 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
446 intel
->vtbl
.meta_draw_region(intel
, irbColor
->region
, NULL
);
448 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
449 * drawing origin may not be correctly emitted.
451 intel_meta_draw_quad(intel
,
456 0, intel
->ClearColor8888
,
457 0, 0, 0, 0); /* texcoords */
463 intel
->vtbl
.leave_meta_state(intel
);
464 intel_batchbuffer_flush(intel
->batch
);
467 static const char *buffer_names
[] = {
468 [BUFFER_FRONT_LEFT
] = "front",
469 [BUFFER_BACK_LEFT
] = "back",
470 [BUFFER_FRONT_RIGHT
] = "front right",
471 [BUFFER_BACK_RIGHT
] = "back right",
472 [BUFFER_AUX0
] = "aux0",
473 [BUFFER_AUX1
] = "aux1",
474 [BUFFER_AUX2
] = "aux2",
475 [BUFFER_AUX3
] = "aux3",
476 [BUFFER_DEPTH
] = "depth",
477 [BUFFER_STENCIL
] = "stencil",
478 [BUFFER_ACCUM
] = "accum",
479 [BUFFER_COLOR0
] = "color0",
480 [BUFFER_COLOR1
] = "color1",
481 [BUFFER_COLOR2
] = "color2",
482 [BUFFER_COLOR3
] = "color3",
483 [BUFFER_COLOR4
] = "color4",
484 [BUFFER_COLOR5
] = "color5",
485 [BUFFER_COLOR6
] = "color6",
486 [BUFFER_COLOR7
] = "color7",
490 * Called by ctx->Driver.Clear.
493 intelClear(GLcontext
*ctx
, GLbitfield mask
)
495 struct intel_context
*intel
= intel_context(ctx
);
496 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
497 GLbitfield tri_mask
= 0;
498 GLbitfield blit_mask
= 0;
499 GLbitfield swrast_mask
= 0;
500 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
504 fprintf(stderr
, "%s\n", __FUNCTION__
);
506 /* HW color buffers (front, back, aux, generic FBO, etc) */
507 if (colorMask
== ~0) {
508 /* clear all R,G,B,A */
509 /* XXX FBO: need to check if colorbuffers are software RBOs! */
510 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
513 /* glColorMask in effect */
514 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
518 if (mask
& BUFFER_BIT_STENCIL
) {
519 const struct intel_region
*stencilRegion
520 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
522 /* have hw stencil */
523 if ((ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
524 /* not clearing all stencil bits, so use triangle clearing */
525 tri_mask
|= BUFFER_BIT_STENCIL
;
528 /* clearing all stencil bits, use blitting */
529 blit_mask
|= BUFFER_BIT_STENCIL
;
535 if (mask
& BUFFER_BIT_DEPTH
) {
536 /* clear depth with whatever method is used for stencil (see above) */
537 if (tri_mask
& BUFFER_BIT_STENCIL
)
538 tri_mask
|= BUFFER_BIT_DEPTH
;
540 blit_mask
|= BUFFER_BIT_DEPTH
;
543 /* SW fallback clearing */
544 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
546 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
547 GLuint bufBit
= 1 << i
;
548 if ((blit_mask
| tri_mask
) & bufBit
) {
549 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
550 blit_mask
&= ~bufBit
;
552 swrast_mask
|= bufBit
;
557 intelFlush(ctx
); /* XXX intelClearWithBlit also does this */
560 if (INTEL_DEBUG
& DEBUG_BLIT
) {
562 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
563 if (blit_mask
& (1 << i
))
564 DBG(" %s", buffer_names
[i
]);
568 intelClearWithBlit(ctx
, blit_mask
);
572 if (INTEL_DEBUG
& DEBUG_BLIT
) {
574 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
575 if (tri_mask
& (1 << i
))
576 DBG(" %s", buffer_names
[i
]);
580 intelClearWithTris(intel
, tri_mask
);
584 if (INTEL_DEBUG
& DEBUG_BLIT
) {
585 DBG("swrast clear:");
586 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
587 if (swrast_mask
& (1 << i
))
588 DBG(" %s", buffer_names
[i
]);
592 _swrast_Clear(ctx
, swrast_mask
);
597 /* Emit wait for pending flips */
599 intel_wait_flips(struct intel_context
*intel
, GLuint batch_flags
)
601 struct intel_framebuffer
*intel_fb
=
602 (struct intel_framebuffer
*) intel
->ctx
.DrawBuffer
;
603 struct intel_renderbuffer
*intel_rb
=
604 intel_get_renderbuffer(&intel_fb
->Base
,
605 intel_fb
->Base
._ColorDrawBufferMask
[0] ==
606 BUFFER_BIT_FRONT_LEFT
? BUFFER_FRONT_LEFT
:
609 if (intel_fb
->Base
.Name
== 0 && intel_rb
&&
610 intel_rb
->pf_pending
== intel_fb
->pf_seq
) {
611 GLint pf_planes
= intel_fb
->pf_planes
;
614 /* Wait for pending flips to take effect */
615 BEGIN_BATCH(2, batch_flags
);
616 OUT_BATCH(pf_planes
& 0x1 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
)
618 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
622 intel_rb
->pf_pending
--;
627 /* Flip the front & back buffers
630 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
632 struct intel_context
*intel
;
634 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
636 if (INTEL_DEBUG
& DEBUG_IOCTL
)
637 fprintf(stderr
, "%s\n", __FUNCTION__
);
640 assert(dPriv
->driContextPriv
);
641 assert(dPriv
->driContextPriv
->driverPrivate
);
643 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
645 if (intel
->intelScreen
->drmMinor
< 9)
648 intelFlush(&intel
->ctx
);
652 LOCK_HARDWARE(intel
);
654 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
655 drm_i915_flip_t flip
;
657 flip
.pipes
= intel_fb
->pf_planes
;
659 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
662 UNLOCK_HARDWARE(intel
);
664 if (ret
|| !intel_fb
->pf_active
)
667 if (!dPriv
->numClipRects
) {
668 usleep(10000); /* throttle invisible client 10ms */
671 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
672 (intel_fb
->pf_planes
& 0x2)) & 0x3;
674 if (dPriv
->numClipRects
!= 0) {
675 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_FRONT_LEFT
)->pf_pending
=
676 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->pf_pending
=
680 intel_flip_renderbuffers(intel_fb
);
681 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
688 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
690 if (dPriv
->driverPrivate
) {
691 const struct gl_framebuffer
*fb
692 = (struct gl_framebuffer
*) dPriv
->driverPrivate
;
693 if (fb
->Visual
.doubleBufferMode
) {
694 GET_CURRENT_CONTEXT(ctx
);
695 if (ctx
&& ctx
->DrawBuffer
== fb
) {
696 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering */
698 if (intel
->doPageFlip
) {
699 intelPageFlip(dPriv
);
702 intelCopyBuffer(dPriv
);
708 "dPriv has no gl_framebuffer pointer in intelSwapBuffers");
716 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
718 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
719 unsigned int interval
;
720 struct intel_context
*intel
=
721 intelScreenContext(dPriv
->driScreenPriv
->private);
722 const intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
724 drm_i915_vblank_swap_t swap
;
727 if (!dPriv
->vblFlags
||
728 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
729 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
732 interval
= driGetVBlankInterval(dPriv
);
734 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
736 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
737 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
738 } else if (interval
== 0) {
742 swap
.drawable
= dPriv
->hHWDrawable
;
743 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
745 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
746 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
749 LOCK_HARDWARE(intel
);
751 intel_batchbuffer_flush(intel
->batch
);
753 if ( intel_fb
->pf_active
) {
754 swap
.seqtype
|= DRM_VBLANK_FLIP
;
756 intel_fb
->pf_current_page
= (((intel
->sarea
->pf_current_page
>>
757 (intel_fb
->pf_planes
& 0x2)) & 0x3) + 1) %
758 intel_fb
->pf_num_pages
;
761 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
763 dPriv
->vblSeq
= swap
.sequence
;
764 swap
.sequence
-= target
;
765 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
767 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->vbl_pending
=
768 intel_get_renderbuffer(&intel_fb
->Base
,
769 BUFFER_FRONT_LEFT
)->vbl_pending
=
772 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
773 intel_flip_renderbuffers(intel_fb
);
774 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
779 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
780 intel_fb
->pf_current_page
= ((intel
->sarea
->pf_current_page
>>
781 (intel_fb
->pf_planes
& 0x2)) & 0x3) %
782 intel_fb
->pf_num_pages
;
788 UNLOCK_HARDWARE(intel
);
794 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
796 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
797 GET_CURRENT_CONTEXT(ctx
);
798 struct intel_context
*intel
;
803 intel
= intel_context(ctx
);
805 if (ctx
->Visual
.doubleBufferMode
) {
806 GLboolean missed_target
;
807 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
810 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
812 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
813 driWaitForVBlank(dPriv
, &missed_target
);
815 if (!intelPageFlip(dPriv
)) {
816 intelCopyBuffer(dPriv
, NULL
);
820 intel_fb
->swap_count
++;
821 (*dri_interface
->getUST
) (&ust
);
823 intel_fb
->swap_missed_count
++;
824 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
827 intel_fb
->swap_ust
= ust
;
831 /* XXX this shouldn't be an error but we can't handle it for now */
832 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
838 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
840 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
841 struct intel_context
*intel
=
842 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
843 GLcontext
*ctx
= &intel
->ctx
;
845 if (ctx
->Visual
.doubleBufferMode
) {
846 drm_clip_rect_t rect
;
847 rect
.x1
= x
+ dPriv
->x
;
848 rect
.y1
= (dPriv
->h
- y
- h
) + dPriv
->y
;
849 rect
.x2
= rect
.x1
+ w
;
850 rect
.y2
= rect
.y1
+ h
;
851 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
852 intelCopyBuffer(dPriv
, &rect
);
856 /* XXX this shouldn't be an error but we can't handle it for now */
857 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
863 * Update the hardware state for drawing into a window or framebuffer object.
865 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
866 * places within the driver.
868 * Basically, this needs to be called any time the current framebuffer
869 * changes, the renderbuffers change, or we need to draw into different
873 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
875 struct intel_context
*intel
= intel_context(ctx
);
876 struct intel_region
*colorRegion
, *depthRegion
= NULL
;
877 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
878 int front
= 0; /* drawing to front color buffer? */
881 /* this can happen during the initial context initialization */
885 /* Do this here, note core Mesa, since this function is called from
886 * many places within the driver.
888 if (ctx
->NewState
& (_NEW_BUFFERS
| _NEW_COLOR
| _NEW_PIXEL
)) {
889 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
890 _mesa_update_framebuffer(ctx
);
891 /* this updates the DrawBuffer's Width/Height if it's a FBO */
892 _mesa_update_draw_buffer_bounds(ctx
);
895 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
896 /* this may occur when we're called by glBindFrameBuffer() during
897 * the process of someone setting up renderbuffers, etc.
899 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
904 intel_validate_paired_depth_stencil(ctx
, fb
);
907 * How many color buffers are we drawing into?
909 if (fb
->_NumColorDrawBuffers
[0] != 1) {
910 /* writing to 0 or 2 or 4 color buffers */
911 /*_mesa_debug(ctx, "Software rendering\n");*/
912 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
916 intelSetRenderbufferClipRects(intel
);
919 /* draw to exactly one color buffer */
920 /*_mesa_debug(ctx, "Hardware rendering\n");*/
921 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
922 if (fb
->_ColorDrawBufferMask
[0] == BUFFER_BIT_FRONT_LEFT
) {
927 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
928 * And set up cliprects.
931 /* drawing to window system buffer */
933 intelSetFrontClipRects(intel
);
934 colorRegion
= intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
937 intelSetBackClipRects(intel
);
938 colorRegion
= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
942 /* drawing to user-created FBO */
943 struct intel_renderbuffer
*irb
;
944 intelSetRenderbufferClipRects(intel
);
945 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0][0]);
946 colorRegion
= (irb
&& irb
->region
) ? irb
->region
: NULL
;
950 /* Update culling direction which changes depending on the
951 * orientation of the buffer:
953 if (ctx
->Driver
.FrontFace
)
954 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
956 ctx
->NewState
|= _NEW_POLYGON
;
959 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
962 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
966 *** Get depth buffer region and check if we need a software fallback.
967 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
969 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
970 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
971 if (irbDepth
&& irbDepth
->region
) {
972 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
973 depthRegion
= irbDepth
->region
;
976 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
981 /* not using depth buffer */
982 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
988 *** This can only be hardware accelerated if we're using a
989 *** combined DEPTH_STENCIL buffer (for now anyway).
991 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
992 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
993 if (irbStencil
&& irbStencil
->region
) {
994 ASSERT(irbStencil
->Base
._ActualFormat
== GL_DEPTH24_STENCIL8_EXT
);
995 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
996 /* need to re-compute stencil hw state */
997 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
999 depthRegion
= irbStencil
->region
;
1002 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
1006 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
1007 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
1008 /* need to re-compute stencil hw state */
1009 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
1013 * Update depth test state
1015 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
1016 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1019 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1022 intel
->vtbl
.set_draw_region(intel
, colorRegion
, depthRegion
);
1024 /* update viewport since it depends on window size */
1025 ctx
->Driver
.Viewport(ctx
, ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
1026 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
);
1028 /* Update hardware scissor */
1029 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
1030 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
1035 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
1037 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1042 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
1044 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
1045 /* This will update FBO completeness status.
1046 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1047 * refers to a missing renderbuffer. Calling glReadBuffer can set
1048 * that straight and can make the drawing buffer complete.
1050 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1052 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1053 * reference ctx->ReadBuffer and do appropriate state checks.
1059 intelInitBufferFuncs(struct dd_function_table
*functions
)
1061 functions
->Clear
= intelClear
;
1062 functions
->DrawBuffer
= intelDrawBuffer
;
1063 functions
->ReadBuffer
= intelReadBuffer
;