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"
41 static uint32_t drifb_ID
= 0;
44 swap_fences_unref(struct dri_drawable
*draw
);
47 dri_st_framebuffer_validate(struct st_context_iface
*stctx
,
48 struct st_framebuffer_iface
*stfbi
,
49 const enum st_attachment_type
*statts
,
51 struct pipe_resource
**out
)
53 struct dri_context
*ctx
= (struct dri_context
*)stctx
->st_manager_private
;
54 struct dri_drawable
*drawable
=
55 (struct dri_drawable
*) stfbi
->st_manager_private
;
56 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
57 unsigned statt_mask
, new_mask
;
60 unsigned int lastStamp
;
61 struct pipe_resource
**textures
=
62 drawable
->stvis
.samples
> 1 ? drawable
->msaa_textures
66 for (i
= 0; i
< count
; i
++)
67 statt_mask
|= (1 << statts
[i
]);
69 /* record newly allocated textures */
70 new_mask
= (statt_mask
& ~drawable
->texture_mask
);
73 * dPriv->dri2.stamp is the server stamp. dPriv->lastStamp is the
74 * client stamp. It has the value of the server stamp when last
78 lastStamp
= drawable
->dPriv
->lastStamp
;
79 new_stamp
= (drawable
->texture_stamp
!= lastStamp
);
81 if (new_stamp
|| new_mask
|| screen
->broken_invalidate
) {
82 if (new_stamp
&& drawable
->update_drawable_info
)
83 drawable
->update_drawable_info(drawable
);
85 drawable
->allocate_textures(ctx
, drawable
, statts
, count
);
87 /* add existing textures */
88 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
90 statt_mask
|= (1 << i
);
93 drawable
->texture_stamp
= lastStamp
;
94 drawable
->texture_mask
= statt_mask
;
96 } while (lastStamp
!= drawable
->dPriv
->lastStamp
);
101 /* Set the window-system buffers for the state tracker. */
102 for (i
= 0; i
< count
; i
++) {
104 pipe_resource_reference(&out
[i
], textures
[statts
[i
]]);
111 dri_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
112 struct st_framebuffer_iface
*stfbi
,
113 enum st_attachment_type statt
)
115 struct dri_context
*ctx
= (struct dri_context
*)stctx
->st_manager_private
;
116 struct dri_drawable
*drawable
=
117 (struct dri_drawable
*) stfbi
->st_manager_private
;
119 /* XXX remove this and just set the correct one on the framebuffer */
120 drawable
->flush_frontbuffer(ctx
, drawable
, statt
);
126 * This is called when we need to set up GL rendering to a new X window.
129 dri_create_buffer(__DRIscreen
* sPriv
,
130 __DRIdrawable
* dPriv
,
131 const struct gl_config
* visual
, boolean isPixmap
)
133 struct dri_screen
*screen
= sPriv
->driverPrivate
;
134 struct dri_drawable
*drawable
= NULL
;
137 goto fail
; /* not implemented */
139 drawable
= CALLOC_STRUCT(dri_drawable
);
140 if (drawable
== NULL
)
143 dri_fill_st_visual(&drawable
->stvis
, screen
, visual
);
145 /* setup the st_framebuffer_iface */
146 drawable
->base
.visual
= &drawable
->stvis
;
147 drawable
->base
.flush_front
= dri_st_framebuffer_flush_front
;
148 drawable
->base
.validate
= dri_st_framebuffer_validate
;
149 drawable
->base
.st_manager_private
= (void *) drawable
;
151 drawable
->screen
= screen
;
152 drawable
->sPriv
= sPriv
;
153 drawable
->dPriv
= dPriv
;
154 drawable
->desired_fences
= screen
->default_throttle_frames
;
155 if (drawable
->desired_fences
> DRI_SWAP_FENCES_MAX
)
156 drawable
->desired_fences
= DRI_SWAP_FENCES_MAX
;
158 dPriv
->driverPrivate
= (void *)drawable
;
159 p_atomic_set(&drawable
->base
.stamp
, 1);
160 drawable
->base
.ID
= p_atomic_inc_return(&drifb_ID
);
169 dri_destroy_buffer(__DRIdrawable
* dPriv
)
171 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
172 struct dri_screen
*screen
= drawable
->screen
;
173 struct st_api
*stapi
= screen
->st_api
;
176 pipe_surface_reference(&drawable
->drisw_surface
, NULL
);
178 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
179 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
180 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
181 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
183 swap_fences_unref(drawable
);
185 /* Notify the st manager that this drawable is no longer valid */
186 stapi
->destroy_drawable(stapi
, &drawable
->base
);
192 * Validate the texture at an attachment. Allocate the texture if it does not
193 * exist. Used by the TFP extension.
196 dri_drawable_validate_att(struct dri_context
*ctx
,
197 struct dri_drawable
*drawable
,
198 enum st_attachment_type statt
)
200 enum st_attachment_type statts
[ST_ATTACHMENT_COUNT
];
201 unsigned i
, count
= 0;
203 /* check if buffer already exists */
204 if (drawable
->texture_mask
& (1 << statt
))
207 /* make sure DRI2 does not destroy existing buffers */
208 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
209 if (drawable
->texture_mask
& (1 << i
)) {
213 statts
[count
++] = statt
;
215 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
- 1;
217 drawable
->base
.validate(ctx
->st
, &drawable
->base
, statts
, count
, NULL
);
221 * These are used for GLX_EXT_texture_from_pixmap
224 dri_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
225 GLint format
, __DRIdrawable
*dPriv
)
227 struct dri_context
*ctx
= dri_context(pDRICtx
);
228 struct st_context_iface
*st
= ctx
->st
;
229 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
230 struct pipe_resource
*pt
;
232 if (st
->thread_finish
)
233 st
->thread_finish(st
);
235 dri_drawable_validate_att(ctx
, drawable
, ST_ATTACHMENT_FRONT_LEFT
);
237 /* Use the pipe resource associated with the X drawable */
238 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
241 enum pipe_format internal_format
= pt
->format
;
243 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
244 /* only need to cover the formats recognized by dri_fill_st_visual */
245 switch (internal_format
) {
246 case PIPE_FORMAT_BGRA8888_UNORM
:
247 internal_format
= PIPE_FORMAT_BGRX8888_UNORM
;
249 case PIPE_FORMAT_ARGB8888_UNORM
:
250 internal_format
= PIPE_FORMAT_XRGB8888_UNORM
;
257 drawable
->update_tex_buffer(drawable
, ctx
, pt
);
259 ctx
->st
->teximage(ctx
->st
,
260 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
261 0, internal_format
, pt
, FALSE
);
266 dri_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
267 __DRIdrawable
*dPriv
)
269 dri_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
272 const __DRItexBufferExtension driTexBufferExtension
= {
273 .base
= { __DRI_TEX_BUFFER
, 2 },
275 .setTexBuffer
= dri_set_tex_buffer
,
276 .setTexBuffer2
= dri_set_tex_buffer2
,
277 .releaseTexBuffer
= NULL
,
281 * Get the format and binding of an attachment.
284 dri_drawable_get_format(struct dri_drawable
*drawable
,
285 enum st_attachment_type statt
,
286 enum pipe_format
*format
,
290 case ST_ATTACHMENT_FRONT_LEFT
:
291 case ST_ATTACHMENT_BACK_LEFT
:
292 case ST_ATTACHMENT_FRONT_RIGHT
:
293 case ST_ATTACHMENT_BACK_RIGHT
:
294 /* Other pieces of the driver stack get confused and behave incorrectly
295 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
296 * though other means and handles it correctly, so we don't really need
297 * to use an sRGB format here.
299 *format
= util_format_linear(drawable
->stvis
.color_format
);
300 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
302 case ST_ATTACHMENT_DEPTH_STENCIL
:
303 *format
= drawable
->stvis
.depth_stencil_format
;
304 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
307 *format
= PIPE_FORMAT_NONE
;
315 * swap_fences_pop_front - pull a fence from the throttle queue
317 * If the throttle queue is filled to the desired number of fences,
318 * pull fences off the queue until the number is less than the desired
319 * number of fences, and return the last fence pulled.
321 static struct pipe_fence_handle
*
322 swap_fences_pop_front(struct dri_drawable
*draw
)
324 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
325 struct pipe_fence_handle
*fence
= NULL
;
327 if (draw
->desired_fences
== 0)
330 if (draw
->cur_fences
>= draw
->desired_fences
) {
331 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
332 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
333 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
341 * swap_fences_push_back - push a fence onto the throttle queue
343 * push a fence onto the throttle queue and pull fences of the queue
344 * so that the desired number of fences are on the queue.
347 swap_fences_push_back(struct dri_drawable
*draw
,
348 struct pipe_fence_handle
*fence
)
350 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
352 if (!fence
|| draw
->desired_fences
== 0)
355 while(draw
->cur_fences
== draw
->desired_fences
)
356 swap_fences_pop_front(draw
);
359 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
361 draw
->head
&= DRI_SWAP_FENCES_MASK
;
366 * swap_fences_unref - empty the throttle queue
368 * pulls fences of the throttle queue until it is empty.
371 swap_fences_unref(struct dri_drawable
*draw
)
373 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
375 while(draw
->cur_fences
) {
376 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
377 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
383 dri_pipe_blit(struct pipe_context
*pipe
,
384 struct pipe_resource
*dst
,
385 struct pipe_resource
*src
)
387 struct pipe_blit_info blit
;
392 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
393 * Fragment Operations):
395 * If a framebuffer object is not bound, after all operations have
396 * been completed on the multisample buffer, the sample values for
397 * each color in the multisample buffer are combined to produce a
398 * single color value, and that value is written into the
399 * corresponding color buffers selected by DrawBuffer or
400 * DrawBuffers. An implementation may defer the writing of the color
401 * buffers until a later time, but the state of the framebuffer must
402 * behave as if the color buffers were updated as each fragment was
403 * processed. The method of combination is not specified. If the
404 * framebuffer contains sRGB values, then it is recommended that the
405 * an average of sample values is computed in a linearized space, as
406 * for blending (see section 4.1.7).
408 * In other words, to do a resolve operation in a linear space, we have
409 * to set sRGB formats if the original resources were sRGB, so don't use
410 * util_format_linear.
413 memset(&blit
, 0, sizeof(blit
));
414 blit
.dst
.resource
= dst
;
415 blit
.dst
.box
.width
= dst
->width0
;
416 blit
.dst
.box
.height
= dst
->height0
;
417 blit
.dst
.box
.depth
= 1;
418 blit
.dst
.format
= dst
->format
;
419 blit
.src
.resource
= src
;
420 blit
.src
.box
.width
= src
->width0
;
421 blit
.src
.box
.height
= src
->height0
;
422 blit
.src
.box
.depth
= 1;
423 blit
.src
.format
= src
->format
;
424 blit
.mask
= PIPE_MASK_RGBA
;
425 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
427 pipe
->blit(pipe
, &blit
);
431 dri_postprocessing(struct dri_context
*ctx
,
432 struct dri_drawable
*drawable
,
433 enum st_attachment_type att
)
435 struct pipe_resource
*src
= drawable
->textures
[att
];
436 struct pipe_resource
*zsbuf
= drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
];
439 pp_run(ctx
->pp
, src
, src
, zsbuf
);
443 * DRI2 flush extension, the flush_with_flags function.
445 * \param context the context
446 * \param drawable the drawable to flush
447 * \param flags a combination of _DRI2_FLUSH_xxx flags
448 * \param throttle_reason the reason for throttling, 0 = no throttling
451 dri_flush(__DRIcontext
*cPriv
,
452 __DRIdrawable
*dPriv
,
454 enum __DRI2throttleReason reason
)
456 struct dri_context
*ctx
= dri_context(cPriv
);
457 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
458 struct st_context_iface
*st
;
459 unsigned flush_flags
;
460 boolean swap_msaa_buffers
= FALSE
;
468 if (st
->thread_finish
)
469 st
->thread_finish(st
);
472 /* prevent recursion */
473 if (drawable
->flushing
)
476 drawable
->flushing
= TRUE
;
479 flags
&= ~__DRI2_FLUSH_DRAWABLE
;
482 /* Flush the drawable. */
483 if ((flags
& __DRI2_FLUSH_DRAWABLE
) &&
484 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]) {
485 struct pipe_context
*pipe
= st
->pipe
;
487 if (drawable
->stvis
.samples
> 1 &&
488 reason
== __DRI2_THROTTLE_SWAPBUFFER
) {
489 /* Resolve the MSAA back buffer. */
490 dri_pipe_blit(st
->pipe
,
491 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
],
492 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]);
494 if (drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] &&
495 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]) {
496 swap_msaa_buffers
= TRUE
;
499 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
502 dri_postprocessing(ctx
, drawable
, ST_ATTACHMENT_BACK_LEFT
);
505 hud_draw(ctx
->hud
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
508 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
510 if (pipe
->invalidate_resource
&&
511 (flags
& __DRI2_FLUSH_INVALIDATE_ANCILLARY
)) {
512 if (drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
513 pipe
->invalidate_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
514 if (drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
515 pipe
->invalidate_resource(pipe
, drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
520 if (flags
& __DRI2_FLUSH_CONTEXT
)
521 flush_flags
|= ST_FLUSH_FRONT
;
522 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
)
523 flush_flags
|= ST_FLUSH_END_OF_FRAME
;
525 /* Flush the context and throttle if needed. */
526 if (dri_screen(ctx
->sPriv
)->throttling_enabled
&&
528 (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
529 reason
== __DRI2_THROTTLE_FLUSHFRONT
)) {
532 * This pulls a fence off the throttling queue and waits for it if the
533 * number of fences on the throttling queue has reached the desired
536 * Then flushes to insert a fence at the current rendering position, and
537 * pushes that fence on the queue. This requires that the st_context_iface
538 * flush method returns a fence even if there are no commands to flush.
540 struct pipe_screen
*screen
= drawable
->screen
->base
.screen
;
541 struct pipe_fence_handle
*fence
;
543 fence
= swap_fences_pop_front(drawable
);
545 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
546 screen
->fence_reference(screen
, &fence
, NULL
);
549 st
->flush(st
, flush_flags
, &fence
);
552 swap_fences_push_back(drawable
, fence
);
553 screen
->fence_reference(screen
, &fence
, NULL
);
556 else if (flags
& (__DRI2_FLUSH_DRAWABLE
| __DRI2_FLUSH_CONTEXT
)) {
557 st
->flush(st
, flush_flags
, NULL
);
561 drawable
->flushing
= FALSE
;
564 /* Swap the MSAA front and back buffers, so that reading
565 * from the front buffer after SwapBuffers returns what was
566 * in the back buffer.
568 if (swap_msaa_buffers
) {
569 struct pipe_resource
*tmp
=
570 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
];
572 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] =
573 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
];
574 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
] = tmp
;
576 /* Now that we have swapped the buffers, this tells the state
577 * tracker to revalidate the framebuffer.
579 p_atomic_inc(&drawable
->base
.stamp
);
584 * dri_throttle - A DRI2ThrottleExtension throttling function.
587 dri_throttle(__DRIcontext
*cPriv
, __DRIdrawable
*dPriv
,
588 enum __DRI2throttleReason reason
)
590 dri_flush(cPriv
, dPriv
, 0, reason
);
594 const __DRI2throttleExtension dri2ThrottleExtension
= {
595 .base
= { __DRI2_THROTTLE
, 1 },
597 .throttle
= dri_throttle
,
601 /* vim: set sw=3 ts=8 sts=3 expandtab: */