1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
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 VMWARE 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 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
42 swap_fences_unref(struct dri_drawable
*draw
);
45 dri_st_framebuffer_validate(struct st_context_iface
*stctx
,
46 struct st_framebuffer_iface
*stfbi
,
47 const enum st_attachment_type
*statts
,
49 struct pipe_resource
**out
)
51 struct dri_context
*ctx
= (struct dri_context
*)stctx
->st_manager_private
;
52 struct dri_drawable
*drawable
=
53 (struct dri_drawable
*) stfbi
->st_manager_private
;
54 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
55 unsigned statt_mask
, new_mask
;
58 unsigned int lastStamp
;
59 struct pipe_resource
**textures
=
60 drawable
->stvis
.samples
> 1 ? drawable
->msaa_textures
64 for (i
= 0; i
< count
; i
++)
65 statt_mask
|= (1 << statts
[i
]);
67 /* record newly allocated textures */
68 new_mask
= (statt_mask
& ~drawable
->texture_mask
);
71 * dPriv->dri2.stamp is the server stamp. dPriv->lastStamp is the
72 * client stamp. It has the value of the server stamp when last
76 lastStamp
= drawable
->dPriv
->lastStamp
;
77 new_stamp
= (drawable
->texture_stamp
!= lastStamp
);
79 if (new_stamp
|| new_mask
|| screen
->broken_invalidate
) {
80 if (new_stamp
&& drawable
->update_drawable_info
)
81 drawable
->update_drawable_info(drawable
);
83 drawable
->allocate_textures(ctx
, drawable
, statts
, count
);
85 /* add existing textures */
86 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
88 statt_mask
|= (1 << i
);
91 drawable
->texture_stamp
= lastStamp
;
92 drawable
->texture_mask
= statt_mask
;
94 } while (lastStamp
!= drawable
->dPriv
->lastStamp
);
99 /* Set the window-system buffers for the state tracker. */
100 for (i
= 0; i
< count
; i
++) {
102 pipe_resource_reference(&out
[i
], textures
[statts
[i
]]);
109 dri_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
110 struct st_framebuffer_iface
*stfbi
,
111 enum st_attachment_type statt
)
113 struct dri_context
*ctx
= (struct dri_context
*)stctx
->st_manager_private
;
114 struct dri_drawable
*drawable
=
115 (struct dri_drawable
*) stfbi
->st_manager_private
;
117 /* XXX remove this and just set the correct one on the framebuffer */
118 drawable
->flush_frontbuffer(ctx
, drawable
, statt
);
124 * This is called when we need to set up GL rendering to a new X window.
127 dri_create_buffer(__DRIscreen
* sPriv
,
128 __DRIdrawable
* dPriv
,
129 const struct gl_config
* visual
, boolean isPixmap
)
131 struct dri_screen
*screen
= sPriv
->driverPrivate
;
132 struct dri_drawable
*drawable
= NULL
;
135 goto fail
; /* not implemented */
137 drawable
= CALLOC_STRUCT(dri_drawable
);
138 if (drawable
== NULL
)
141 dri_fill_st_visual(&drawable
->stvis
, screen
, visual
);
143 /* setup the st_framebuffer_iface */
144 drawable
->base
.visual
= &drawable
->stvis
;
145 drawable
->base
.flush_front
= dri_st_framebuffer_flush_front
;
146 drawable
->base
.validate
= dri_st_framebuffer_validate
;
147 drawable
->base
.st_manager_private
= (void *) drawable
;
149 drawable
->screen
= screen
;
150 drawable
->sPriv
= sPriv
;
151 drawable
->dPriv
= dPriv
;
152 drawable
->desired_fences
= screen
->default_throttle_frames
;
153 if (drawable
->desired_fences
> DRI_SWAP_FENCES_MAX
)
154 drawable
->desired_fences
= DRI_SWAP_FENCES_MAX
;
156 dPriv
->driverPrivate
= (void *)drawable
;
157 p_atomic_set(&drawable
->base
.stamp
, 1);
166 dri_destroy_buffer(__DRIdrawable
* dPriv
)
168 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
171 pipe_surface_reference(&drawable
->drisw_surface
, NULL
);
173 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
174 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
175 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
176 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
178 swap_fences_unref(drawable
);
184 * Validate the texture at an attachment. Allocate the texture if it does not
185 * exist. Used by the TFP extension.
188 dri_drawable_validate_att(struct dri_context
*ctx
,
189 struct dri_drawable
*drawable
,
190 enum st_attachment_type statt
)
192 enum st_attachment_type statts
[ST_ATTACHMENT_COUNT
];
193 unsigned i
, count
= 0;
195 /* check if buffer already exists */
196 if (drawable
->texture_mask
& (1 << statt
))
199 /* make sure DRI2 does not destroy existing buffers */
200 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
201 if (drawable
->texture_mask
& (1 << i
)) {
205 statts
[count
++] = statt
;
207 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
- 1;
209 drawable
->base
.validate(ctx
->st
, &drawable
->base
, statts
, count
, NULL
);
213 * These are used for GLX_EXT_texture_from_pixmap
216 dri_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
217 GLint format
, __DRIdrawable
*dPriv
)
219 struct dri_context
*ctx
= dri_context(pDRICtx
);
220 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
221 struct pipe_resource
*pt
;
223 dri_drawable_validate_att(ctx
, drawable
, ST_ATTACHMENT_FRONT_LEFT
);
225 /* Use the pipe resource associated with the X drawable */
226 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
229 enum pipe_format internal_format
= pt
->format
;
231 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
232 /* only need to cover the formats recognized by dri_fill_st_visual */
233 switch (internal_format
) {
234 case PIPE_FORMAT_BGRA8888_UNORM
:
235 internal_format
= PIPE_FORMAT_BGRX8888_UNORM
;
237 case PIPE_FORMAT_ARGB8888_UNORM
:
238 internal_format
= PIPE_FORMAT_XRGB8888_UNORM
;
245 drawable
->update_tex_buffer(drawable
, ctx
, pt
);
247 ctx
->st
->teximage(ctx
->st
,
248 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
249 0, internal_format
, pt
, FALSE
);
254 dri_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
255 __DRIdrawable
*dPriv
)
257 dri_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
260 const __DRItexBufferExtension driTexBufferExtension
= {
261 .base
= { __DRI_TEX_BUFFER
, 2 },
263 .setTexBuffer
= dri_set_tex_buffer
,
264 .setTexBuffer2
= dri_set_tex_buffer2
,
265 .releaseTexBuffer
= NULL
,
269 * Get the format and binding of an attachment.
272 dri_drawable_get_format(struct dri_drawable
*drawable
,
273 enum st_attachment_type statt
,
274 enum pipe_format
*format
,
278 case ST_ATTACHMENT_FRONT_LEFT
:
279 case ST_ATTACHMENT_BACK_LEFT
:
280 case ST_ATTACHMENT_FRONT_RIGHT
:
281 case ST_ATTACHMENT_BACK_RIGHT
:
282 /* Other pieces of the driver stack get confused and behave incorrectly
283 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
284 * though other means and handles it correctly, so we don't really need
285 * to use an sRGB format here.
287 *format
= util_format_linear(drawable
->stvis
.color_format
);
288 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
290 case ST_ATTACHMENT_DEPTH_STENCIL
:
291 *format
= drawable
->stvis
.depth_stencil_format
;
292 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
295 *format
= PIPE_FORMAT_NONE
;
303 * swap_fences_pop_front - pull a fence from the throttle queue
305 * If the throttle queue is filled to the desired number of fences,
306 * pull fences off the queue until the number is less than the desired
307 * number of fences, and return the last fence pulled.
309 static struct pipe_fence_handle
*
310 swap_fences_pop_front(struct dri_drawable
*draw
)
312 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
313 struct pipe_fence_handle
*fence
= NULL
;
315 if (draw
->desired_fences
== 0)
318 if (draw
->cur_fences
>= draw
->desired_fences
) {
319 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
320 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
321 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
329 * swap_fences_push_back - push a fence onto the throttle queue
331 * push a fence onto the throttle queue and pull fences of the queue
332 * so that the desired number of fences are on the queue.
335 swap_fences_push_back(struct dri_drawable
*draw
,
336 struct pipe_fence_handle
*fence
)
338 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
340 if (!fence
|| draw
->desired_fences
== 0)
343 while(draw
->cur_fences
== draw
->desired_fences
)
344 swap_fences_pop_front(draw
);
347 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
349 draw
->head
&= DRI_SWAP_FENCES_MASK
;
354 * swap_fences_unref - empty the throttle queue
356 * pulls fences of the throttle queue until it is empty.
359 swap_fences_unref(struct dri_drawable
*draw
)
361 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
363 while(draw
->cur_fences
) {
364 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
365 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
371 dri_pipe_blit(struct pipe_context
*pipe
,
372 struct pipe_resource
*dst
,
373 struct pipe_resource
*src
)
375 struct pipe_blit_info blit
;
380 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
381 * Fragment Operations):
383 * If a framebuffer object is not bound, after all operations have
384 * been completed on the multisample buffer, the sample values for
385 * each color in the multisample buffer are combined to produce a
386 * single color value, and that value is written into the
387 * corresponding color buffers selected by DrawBuffer or
388 * DrawBuffers. An implementation may defer the writing of the color
389 * buffers until a later time, but the state of the framebuffer must
390 * behave as if the color buffers were updated as each fragment was
391 * processed. The method of combination is not specified. If the
392 * framebuffer contains sRGB values, then it is recommended that the
393 * an average of sample values is computed in a linearized space, as
394 * for blending (see section 4.1.7).
396 * In other words, to do a resolve operation in a linear space, we have
397 * to set sRGB formats if the original resources were sRGB, so don't use
398 * util_format_linear.
401 memset(&blit
, 0, sizeof(blit
));
402 blit
.dst
.resource
= dst
;
403 blit
.dst
.box
.width
= dst
->width0
;
404 blit
.dst
.box
.height
= dst
->height0
;
405 blit
.dst
.box
.depth
= 1;
406 blit
.dst
.format
= dst
->format
;
407 blit
.src
.resource
= src
;
408 blit
.src
.box
.width
= src
->width0
;
409 blit
.src
.box
.height
= src
->height0
;
410 blit
.src
.box
.depth
= 1;
411 blit
.src
.format
= src
->format
;
412 blit
.mask
= PIPE_MASK_RGBA
;
413 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
415 pipe
->blit(pipe
, &blit
);
419 dri_postprocessing(struct dri_context
*ctx
,
420 struct dri_drawable
*drawable
,
421 enum st_attachment_type att
)
423 struct pipe_resource
*src
= drawable
->textures
[att
];
424 struct pipe_resource
*zsbuf
= drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
];
427 pp_run(ctx
->pp
, src
, src
, zsbuf
);
431 * DRI2 flush extension, the flush_with_flags function.
433 * \param context the context
434 * \param drawable the drawable to flush
435 * \param flags a combination of _DRI2_FLUSH_xxx flags
436 * \param throttle_reason the reason for throttling, 0 = no throttling
439 dri_flush(__DRIcontext
*cPriv
,
440 __DRIdrawable
*dPriv
,
442 enum __DRI2throttleReason reason
)
444 struct dri_context
*ctx
= dri_context(cPriv
);
445 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
446 struct st_context_iface
*st
;
447 unsigned flush_flags
;
448 boolean swap_msaa_buffers
= FALSE
;
458 /* prevent recursion */
459 if (drawable
->flushing
)
462 drawable
->flushing
= TRUE
;
465 flags
&= ~__DRI2_FLUSH_DRAWABLE
;
468 /* Flush the drawable. */
469 if ((flags
& __DRI2_FLUSH_DRAWABLE
) &&
470 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]) {
471 struct pipe_context
*pipe
= st
->pipe
;
473 if (drawable
->stvis
.samples
> 1 &&
474 reason
== __DRI2_THROTTLE_SWAPBUFFER
) {
475 /* Resolve the MSAA back buffer. */
476 dri_pipe_blit(st
->pipe
,
477 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
],
478 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]);
480 if (drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] &&
481 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]) {
482 swap_msaa_buffers
= TRUE
;
485 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
488 dri_postprocessing(ctx
, drawable
, ST_ATTACHMENT_BACK_LEFT
);
491 hud_draw(ctx
->hud
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
494 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
496 if (pipe
->invalidate_resource
&&
497 (flags
& __DRI2_FLUSH_INVALIDATE_ANCILLARY
)) {
498 if (drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
499 pipe
->invalidate_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
500 if (drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
501 pipe
->invalidate_resource(pipe
, drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
506 if (flags
& __DRI2_FLUSH_CONTEXT
)
507 flush_flags
|= ST_FLUSH_FRONT
;
508 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
)
509 flush_flags
|= ST_FLUSH_END_OF_FRAME
;
511 /* Flush the context and throttle if needed. */
512 if (dri_screen(ctx
->sPriv
)->throttling_enabled
&&
514 (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
515 reason
== __DRI2_THROTTLE_FLUSHFRONT
)) {
518 * This pulls a fence off the throttling queue and waits for it if the
519 * number of fences on the throttling queue has reached the desired
522 * Then flushes to insert a fence at the current rendering position, and
523 * pushes that fence on the queue. This requires that the st_context_iface
524 * flush method returns a fence even if there are no commands to flush.
526 struct pipe_screen
*screen
= drawable
->screen
->base
.screen
;
527 struct pipe_fence_handle
*fence
;
529 fence
= swap_fences_pop_front(drawable
);
531 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
532 screen
->fence_reference(screen
, &fence
, NULL
);
535 st
->flush(st
, flush_flags
, &fence
);
538 swap_fences_push_back(drawable
, fence
);
539 screen
->fence_reference(screen
, &fence
, NULL
);
542 else if (flags
& (__DRI2_FLUSH_DRAWABLE
| __DRI2_FLUSH_CONTEXT
)) {
543 st
->flush(st
, flush_flags
, NULL
);
547 drawable
->flushing
= FALSE
;
550 /* Swap the MSAA front and back buffers, so that reading
551 * from the front buffer after SwapBuffers returns what was
552 * in the back buffer.
554 if (swap_msaa_buffers
) {
555 struct pipe_resource
*tmp
=
556 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
];
558 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] =
559 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
];
560 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
] = tmp
;
562 /* Now that we have swapped the buffers, this tells the state
563 * tracker to revalidate the framebuffer.
565 p_atomic_inc(&drawable
->base
.stamp
);
570 * dri_throttle - A DRI2ThrottleExtension throttling function.
573 dri_throttle(__DRIcontext
*cPriv
, __DRIdrawable
*dPriv
,
574 enum __DRI2throttleReason reason
)
576 dri_flush(cPriv
, dPriv
, 0, reason
);
580 const __DRI2throttleExtension dri2ThrottleExtension
= {
581 .base
= { __DRI2_THROTTLE
, 1 },
583 .throttle
= dri_throttle
,
587 /* vim: set sw=3 ts=8 sts=3 expandtab: */