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_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 "main/context.h"
39 #include "main/framebuffer.h"
40 #include "swrast/swrast.h"
42 #include "drirenderbuffer.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]);
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
);
127 intel_get_cliprects(struct intel_context
*intel
,
128 struct drm_clip_rect
**cliprects
,
129 unsigned int *num_cliprects
,
130 int *x_off
, int *y_off
)
132 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
133 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
135 if (intel
->constant_cliprect
) {
136 /* FBO or DRI2 rendering, which can just use the fb's size. */
137 intel
->fboRect
.x1
= 0;
138 intel
->fboRect
.y1
= 0;
139 intel
->fboRect
.x2
= intel
->ctx
.DrawBuffer
->Width
;
140 intel
->fboRect
.y2
= intel
->ctx
.DrawBuffer
->Height
;
142 *cliprects
= &intel
->fboRect
;
146 } else if (intel
->front_cliprects
||
147 intel_fb
->pf_active
|| dPriv
->numBackClipRects
== 0) {
148 /* use the front clip rects */
149 *cliprects
= dPriv
->pClipRects
;
150 *num_cliprects
= dPriv
->numClipRects
;
155 /* use the back clip rects */
156 *num_cliprects
= dPriv
->numBackClipRects
;
157 *cliprects
= dPriv
->pBackClipRects
;
158 *x_off
= dPriv
->backX
;
159 *y_off
= dPriv
->backY
;
164 intelUpdatePageFlipping(struct intel_context
*intel
,
165 GLint areaA
, GLint areaB
)
167 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
168 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
172 /* Update page flipping info */
181 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
182 (intel_fb
->pf_planes
& 0x2)) & 0x3;
184 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
186 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
188 if (INTEL_DEBUG
& DEBUG_LOCK
)
189 if (pf_active
!= intel_fb
->pf_active
)
190 _mesa_printf("%s - Page flipping %sactive\n", __progname
,
191 pf_active
? "" : "in");
194 /* Sync pages between planes if flipping on both at the same time */
195 if (pf_planes
== 0x3 && pf_planes
!= intel_fb
->pf_planes
&&
196 (intel
->sarea
->pf_current_page
& 0x3) !=
197 (((intel
->sarea
->pf_current_page
) >> 2) & 0x3)) {
198 drm_i915_flip_t flip
;
200 if (intel_fb
->pf_current_page
==
201 (intel
->sarea
->pf_current_page
& 0x3)) {
202 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
203 * lockups for unknown reasons.
205 intel
->sarea
->pf_current_page
=
206 intel
->sarea
->pf_current_page
& 0x3;
207 intel
->sarea
->pf_current_page
|=
208 ((intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
209 intel_fb
->pf_num_pages
) << 2;
213 intel
->sarea
->pf_current_page
=
214 intel
->sarea
->pf_current_page
& (0x3 << 2);
215 intel
->sarea
->pf_current_page
|=
216 (intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
217 intel_fb
->pf_num_pages
;
222 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
225 intel_fb
->pf_planes
= pf_planes
;
228 intel_fb
->pf_active
= pf_active
;
229 intel_flip_renderbuffers(intel_fb
);
230 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
234 * This will be called whenever the currently bound window is moved/resized.
235 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
238 intelWindowMoved(struct intel_context
*intel
)
240 GLcontext
*ctx
= &intel
->ctx
;
241 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
242 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
244 if (!intel
->intelScreen
->driScrnPriv
->dri2
.enabled
&&
245 intel
->intelScreen
->driScrnPriv
->ddx_version
.minor
>= 7) {
246 volatile struct drm_i915_sarea
*sarea
= intel
->sarea
;
247 drm_clip_rect_t drw_rect
= { .x1
= dPriv
->x
, .x2
= dPriv
->x
+ dPriv
->w
,
248 .y1
= dPriv
->y
, .y2
= dPriv
->y
+ dPriv
->h
};
249 drm_clip_rect_t planeA_rect
= { .x1
= sarea
->planeA_x
, .y1
= sarea
->planeA_y
,
250 .x2
= sarea
->planeA_x
+ sarea
->planeA_w
,
251 .y2
= sarea
->planeA_y
+ sarea
->planeA_h
};
252 drm_clip_rect_t planeB_rect
= { .x1
= sarea
->planeB_x
, .y1
= sarea
->planeB_y
,
253 .x2
= sarea
->planeB_x
+ sarea
->planeB_w
,
254 .y2
= sarea
->planeB_y
+ sarea
->planeB_h
};
255 GLint areaA
= driIntersectArea( drw_rect
, planeA_rect
);
256 GLint areaB
= driIntersectArea( drw_rect
, planeB_rect
);
257 GLuint flags
= dPriv
->vblFlags
;
259 intelUpdatePageFlipping(intel
, areaA
, areaB
);
261 /* Update vblank info
263 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
264 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
266 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
269 /* Check to see if we changed pipes */
270 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
271 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
277 * Deal with page flipping
279 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
281 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
282 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
285 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
286 if (!intel_fb
->color_rb
[i
] ||
287 (intel_fb
->vbl_waited
- intel_fb
->color_rb
[i
]->vbl_pending
) <=
291 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
292 drmWaitVBlank(intel
->driFd
, &vbl
);
296 * Update msc_base from old pipe
298 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
299 dPriv
->msc_base
= count
;
301 * Then get new vblank_base and vblSeq values
303 dPriv
->vblFlags
= flags
;
304 driGetCurrentVBlank(dPriv
);
305 dPriv
->vblank_base
= dPriv
->vblSeq
;
307 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
309 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
310 if (intel_fb
->color_rb
[i
])
311 intel_fb
->color_rb
[i
]->vbl_pending
= intel_fb
->vbl_waited
;
315 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
318 /* Update Mesa's notion of window size */
319 driUpdateFramebufferSize(ctx
, dPriv
);
320 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
322 /* Update hardware scissor */
323 if (ctx
->Driver
.Scissor
!= NULL
) {
324 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
325 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
328 /* Re-calculate viewport related state */
329 if (ctx
->Driver
.DepthRange
!= NULL
)
330 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
335 /* A true meta version of this would be very simple and additionally
336 * machine independent. Maybe we'll get there one day.
339 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
341 GLcontext
*ctx
= &intel
->ctx
;
342 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
345 intel
->vtbl
.install_meta_state(intel
);
347 /* Back and stencil cliprects are the same. Try and do both
350 if (mask
& (BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
)) {
351 struct intel_region
*backRegion
=
352 intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
353 struct intel_region
*depthRegion
=
354 intel_get_rb_region(fb
, BUFFER_DEPTH
);
356 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
358 if (mask
& BUFFER_BIT_BACK_LEFT
)
359 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
361 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
363 if (mask
& BUFFER_BIT_STENCIL
)
364 intel
->vtbl
.meta_stencil_replace(intel
,
365 intel
->ctx
.Stencil
.WriteMask
[0],
366 intel
->ctx
.Stencil
.Clear
);
368 intel
->vtbl
.meta_no_stencil_write(intel
);
370 if (mask
& BUFFER_BIT_DEPTH
)
371 intel
->vtbl
.meta_depth_replace(intel
);
373 intel
->vtbl
.meta_no_depth_write(intel
);
375 intel
->vtbl
.meta_draw_quad(intel
,
380 intel
->ctx
.Depth
.Clear
,
381 intel
->ClearColor8888
,
382 0, 0, 0, 0); /* texcoords */
384 mask
&= ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
387 /* clear the remaining (color) renderbuffers */
388 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
389 const GLuint bufBit
= 1 << buf
;
391 struct intel_renderbuffer
*irbColor
=
392 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
396 intel
->vtbl
.meta_no_depth_write(intel
);
397 intel
->vtbl
.meta_no_stencil_write(intel
);
398 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
399 intel
->vtbl
.meta_draw_region(intel
, irbColor
->region
, NULL
);
401 intel
->vtbl
.meta_draw_quad(intel
,
406 0, intel
->ClearColor8888
,
407 0, 0, 0, 0); /* texcoords */
413 intel
->vtbl
.leave_meta_state(intel
);
416 static const char *buffer_names
[] = {
417 [BUFFER_FRONT_LEFT
] = "front",
418 [BUFFER_BACK_LEFT
] = "back",
419 [BUFFER_FRONT_RIGHT
] = "front right",
420 [BUFFER_BACK_RIGHT
] = "back right",
421 [BUFFER_AUX0
] = "aux0",
422 [BUFFER_AUX1
] = "aux1",
423 [BUFFER_AUX2
] = "aux2",
424 [BUFFER_AUX3
] = "aux3",
425 [BUFFER_DEPTH
] = "depth",
426 [BUFFER_STENCIL
] = "stencil",
427 [BUFFER_ACCUM
] = "accum",
428 [BUFFER_COLOR0
] = "color0",
429 [BUFFER_COLOR1
] = "color1",
430 [BUFFER_COLOR2
] = "color2",
431 [BUFFER_COLOR3
] = "color3",
432 [BUFFER_COLOR4
] = "color4",
433 [BUFFER_COLOR5
] = "color5",
434 [BUFFER_COLOR6
] = "color6",
435 [BUFFER_COLOR7
] = "color7",
439 * Called by ctx->Driver.Clear.
442 intelClear(GLcontext
*ctx
, GLbitfield mask
)
444 struct intel_context
*intel
= intel_context(ctx
);
445 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
446 GLbitfield tri_mask
= 0;
447 GLbitfield blit_mask
= 0;
448 GLbitfield swrast_mask
= 0;
449 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
453 fprintf(stderr
, "%s\n", __FUNCTION__
);
455 /* HW color buffers (front, back, aux, generic FBO, etc) */
456 if (colorMask
== ~0) {
457 /* clear all R,G,B,A */
458 /* XXX FBO: need to check if colorbuffers are software RBOs! */
459 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
462 /* glColorMask in effect */
463 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
467 if (mask
& BUFFER_BIT_STENCIL
) {
468 const struct intel_region
*stencilRegion
469 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
471 /* have hw stencil */
472 if (IS_965(intel
->intelScreen
->deviceID
) ||
473 (ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
474 /* We have to use the 3D engine if we're clearing a partial mask
475 * of the stencil buffer, or if we're on a 965 which has a tiled
476 * depth/stencil buffer in a layout we can't blit to.
478 tri_mask
|= BUFFER_BIT_STENCIL
;
481 /* clearing all stencil bits, use blitting */
482 blit_mask
|= BUFFER_BIT_STENCIL
;
488 if (mask
& BUFFER_BIT_DEPTH
) {
489 /* clear depth with whatever method is used for stencil (see above) */
490 if (IS_965(intel
->intelScreen
->deviceID
) ||
491 tri_mask
& BUFFER_BIT_STENCIL
)
492 tri_mask
|= BUFFER_BIT_DEPTH
;
494 blit_mask
|= BUFFER_BIT_DEPTH
;
497 /* SW fallback clearing */
498 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
500 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
501 GLuint bufBit
= 1 << i
;
502 if ((blit_mask
| tri_mask
) & bufBit
) {
503 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
504 blit_mask
&= ~bufBit
;
506 swrast_mask
|= bufBit
;
512 if (INTEL_DEBUG
& DEBUG_BLIT
) {
514 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
515 if (blit_mask
& (1 << i
))
516 DBG(" %s", buffer_names
[i
]);
520 intelClearWithBlit(ctx
, blit_mask
);
524 if (INTEL_DEBUG
& DEBUG_BLIT
) {
526 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
527 if (tri_mask
& (1 << i
))
528 DBG(" %s", buffer_names
[i
]);
532 intelClearWithTris(intel
, tri_mask
);
536 if (INTEL_DEBUG
& DEBUG_BLIT
) {
537 DBG("swrast clear:");
538 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
539 if (swrast_mask
& (1 << i
))
540 DBG(" %s", buffer_names
[i
]);
544 _swrast_Clear(ctx
, swrast_mask
);
549 /* Emit wait for pending flips */
551 intel_wait_flips(struct intel_context
*intel
)
553 struct intel_framebuffer
*intel_fb
=
554 (struct intel_framebuffer
*) intel
->ctx
.DrawBuffer
;
555 struct intel_renderbuffer
*intel_rb
=
556 intel_get_renderbuffer(&intel_fb
->Base
,
557 intel_fb
->Base
._ColorDrawBufferIndexes
[0] ==
558 BUFFER_FRONT_LEFT
? BUFFER_FRONT_LEFT
:
561 if (intel
->intelScreen
->driScrnPriv
->dri2
.enabled
)
564 if (intel_fb
->Base
.Name
== 0 && intel_rb
&&
565 intel_rb
->pf_pending
== intel_fb
->pf_seq
) {
566 GLint pf_planes
= intel_fb
->pf_planes
;
569 /* Wait for pending flips to take effect */
570 BEGIN_BATCH(2, NO_LOOP_CLIPRECTS
);
571 OUT_BATCH(pf_planes
& 0x1 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
)
573 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
577 intel_rb
->pf_pending
--;
582 /* Flip the front & back buffers
585 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
587 struct intel_context
*intel
;
589 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
591 if (INTEL_DEBUG
& DEBUG_IOCTL
)
592 fprintf(stderr
, "%s\n", __FUNCTION__
);
595 assert(dPriv
->driContextPriv
);
596 assert(dPriv
->driContextPriv
->driverPrivate
);
598 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
600 if (intel
->intelScreen
->drmMinor
< 9)
603 intelFlush(&intel
->ctx
);
607 LOCK_HARDWARE(intel
);
609 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
610 drm_i915_flip_t flip
;
612 flip
.pipes
= intel_fb
->pf_planes
;
614 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
617 UNLOCK_HARDWARE(intel
);
619 if (ret
|| !intel_fb
->pf_active
)
622 if (!dPriv
->numClipRects
) {
623 usleep(10000); /* throttle invisible client 10ms */
626 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
627 (intel_fb
->pf_planes
& 0x2)) & 0x3;
629 if (dPriv
->numClipRects
!= 0) {
630 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_FRONT_LEFT
)->pf_pending
=
631 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->pf_pending
=
635 intel_flip_renderbuffers(intel_fb
);
636 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
642 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
644 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
645 unsigned int interval
;
646 struct intel_context
*intel
=
647 intelScreenContext(dPriv
->driScreenPriv
->private);
648 const intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
650 drm_i915_vblank_swap_t swap
;
653 if (!dPriv
->vblFlags
||
654 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
655 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
658 interval
= driGetVBlankInterval(dPriv
);
660 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
662 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
663 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
664 } else if (interval
== 0)
667 swap
.drawable
= dPriv
->hHWDrawable
;
668 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
670 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
671 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
674 LOCK_HARDWARE(intel
);
676 intel_batchbuffer_flush(intel
->batch
);
678 if ( intel_fb
->pf_active
) {
679 swap
.seqtype
|= DRM_VBLANK_FLIP
;
681 intel_fb
->pf_current_page
= (((intel
->sarea
->pf_current_page
>>
682 (intel_fb
->pf_planes
& 0x2)) & 0x3) + 1) %
683 intel_fb
->pf_num_pages
;
686 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
688 dPriv
->vblSeq
= swap
.sequence
;
689 swap
.sequence
-= target
;
690 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
692 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->vbl_pending
=
693 intel_get_renderbuffer(&intel_fb
->Base
,
694 BUFFER_FRONT_LEFT
)->vbl_pending
=
697 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
698 intel_flip_renderbuffers(intel_fb
);
699 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
704 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
705 intel_fb
->pf_current_page
= ((intel
->sarea
->pf_current_page
>>
706 (intel_fb
->pf_planes
& 0x2)) & 0x3) %
707 intel_fb
->pf_num_pages
;
713 UNLOCK_HARDWARE(intel
);
719 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
721 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
723 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
724 GET_CURRENT_CONTEXT(ctx
);
725 struct intel_context
*intel
;
730 intel
= intel_context(ctx
);
732 if (ctx
->Visual
.doubleBufferMode
) {
733 GLboolean missed_target
;
734 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
737 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
739 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
740 driWaitForVBlank(dPriv
, &missed_target
);
743 * Update each buffer's vbl_pending so we don't get too out of
746 intel_get_renderbuffer(&intel_fb
->Base
,
747 BUFFER_BACK_LEFT
)->vbl_pending
=
748 intel_get_renderbuffer(&intel_fb
->Base
,
749 BUFFER_FRONT_LEFT
)->vbl_pending
=
751 if (!intelPageFlip(dPriv
)) {
752 intelCopyBuffer(dPriv
, NULL
);
756 intel_fb
->swap_count
++;
757 (*psp
->systemTime
->getUST
) (&ust
);
759 intel_fb
->swap_missed_count
++;
760 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
763 intel_fb
->swap_ust
= ust
;
765 drmCommandNone(intel
->driFd
, DRM_I915_GEM_THROTTLE
);
769 /* XXX this shouldn't be an error but we can't handle it for now */
770 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
775 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
777 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
778 struct intel_context
*intel
=
779 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
780 GLcontext
*ctx
= &intel
->ctx
;
782 if (ctx
->Visual
.doubleBufferMode
) {
783 drm_clip_rect_t rect
;
784 rect
.x1
= x
+ dPriv
->x
;
785 rect
.y1
= (dPriv
->h
- y
- h
) + dPriv
->y
;
786 rect
.x2
= rect
.x1
+ w
;
787 rect
.y2
= rect
.y1
+ h
;
788 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
789 intelCopyBuffer(dPriv
, &rect
);
793 /* XXX this shouldn't be an error but we can't handle it for now */
794 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
800 * Update the hardware state for drawing into a window or framebuffer object.
802 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
803 * places within the driver.
805 * Basically, this needs to be called any time the current framebuffer
806 * changes, the renderbuffers change, or we need to draw into different
810 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
812 struct intel_context
*intel
= intel_context(ctx
);
813 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
814 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
817 /* this can happen during the initial context initialization */
821 /* Do this here, note core Mesa, since this function is called from
822 * many places within the driver.
824 if (ctx
->NewState
& (_NEW_BUFFERS
| _NEW_COLOR
| _NEW_PIXEL
)) {
825 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
826 _mesa_update_framebuffer(ctx
);
827 /* this updates the DrawBuffer's Width/Height if it's a FBO */
828 _mesa_update_draw_buffer_bounds(ctx
);
831 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
832 /* this may occur when we're called by glBindFrameBuffer() during
833 * the process of someone setting up renderbuffers, etc.
835 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
840 intel_validate_paired_depth_stencil(ctx
, fb
);
843 * How many color buffers are we drawing into?
845 if (fb
->_NumColorDrawBuffers
== 0) {
847 colorRegions
[0] = NULL
;
848 intel
->constant_cliprect
= GL_TRUE
;
849 } else if (fb
->_NumColorDrawBuffers
> 1) {
851 struct intel_renderbuffer
*irb
;
853 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
854 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
855 colorRegions
[i
] = irb
? irb
->region
: NULL
;
857 intel
->constant_cliprect
= GL_TRUE
;
860 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
861 * into, and set up cliprects if it's .
864 intel
->constant_cliprect
= intel
->driScreen
->dri2
.enabled
;
865 /* drawing to window system buffer */
866 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
867 if (!intel
->constant_cliprect
&& !intel
->front_cliprects
)
868 intel_batchbuffer_flush(intel
->batch
);
869 intel
->front_cliprects
= GL_TRUE
;
870 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
873 if (!intel
->constant_cliprect
&& intel
->front_cliprects
)
874 intel_batchbuffer_flush(intel
->batch
);
875 intel
->front_cliprects
= GL_FALSE
;
876 colorRegions
[0]= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
880 /* drawing to user-created FBO */
881 struct intel_renderbuffer
*irb
;
882 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
883 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
884 intel
->constant_cliprect
= GL_TRUE
;
888 /* Update culling direction which changes depending on the
889 * orientation of the buffer:
891 if (ctx
->Driver
.FrontFace
)
892 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
894 ctx
->NewState
|= _NEW_POLYGON
;
896 if (!colorRegions
[0]) {
897 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
900 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
904 *** Get depth buffer region and check if we need a software fallback.
905 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
907 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
908 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
909 if (irbDepth
&& irbDepth
->region
) {
910 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
911 depthRegion
= irbDepth
->region
;
914 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
919 /* not using depth buffer */
920 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
926 *** This can only be hardware accelerated if we're using a
927 *** combined DEPTH_STENCIL buffer (for now anyway).
929 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
930 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
931 if (irbStencil
&& irbStencil
->region
) {
932 ASSERT(irbStencil
->Base
._ActualFormat
== GL_DEPTH24_STENCIL8_EXT
);
933 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
934 /* need to re-compute stencil hw state */
935 if (ctx
->Driver
.Enable
!= NULL
)
936 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
938 ctx
->NewState
|= _NEW_STENCIL
;
940 depthRegion
= irbStencil
->region
;
943 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
947 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
948 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
949 /* need to re-compute stencil hw state */
950 if (ctx
->Driver
.Enable
!= NULL
)
951 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
953 ctx
->NewState
|= _NEW_STENCIL
;
957 * Update depth test state
959 if (ctx
->Driver
.Enable
) {
960 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
961 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
963 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
966 ctx
->NewState
|= _NEW_DEPTH
;
969 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
970 fb
->_NumColorDrawBuffers
);
972 /* update viewport since it depends on window size */
973 if (ctx
->Driver
.Viewport
) {
974 ctx
->Driver
.Viewport(ctx
, ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
975 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
);
977 ctx
->NewState
|= _NEW_VIEWPORT
;
980 /* Set state we know depends on drawable parameters:
982 if (ctx
->Driver
.Scissor
)
983 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
984 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
985 intel
->NewGLState
|= _NEW_SCISSOR
;
987 if (ctx
->Driver
.DepthRange
)
988 ctx
->Driver
.DepthRange(ctx
,
995 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
997 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1002 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
1004 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
1005 /* This will update FBO completeness status.
1006 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1007 * refers to a missing renderbuffer. Calling glReadBuffer can set
1008 * that straight and can make the drawing buffer complete.
1010 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1012 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1013 * reference ctx->ReadBuffer and do appropriate state checks.
1019 intelInitBufferFuncs(struct dd_function_table
*functions
)
1021 functions
->Clear
= intelClear
;
1022 functions
->DrawBuffer
= intelDrawBuffer
;
1023 functions
->ReadBuffer
= intelReadBuffer
;