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
);
161 drawable
->base
.state_manager
= &screen
->base
;
170 dri_destroy_buffer(__DRIdrawable
* dPriv
)
172 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
173 struct dri_screen
*screen
= drawable
->screen
;
174 struct st_api
*stapi
= screen
->st_api
;
177 pipe_surface_reference(&drawable
->drisw_surface
, NULL
);
179 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
180 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
181 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
182 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
184 swap_fences_unref(drawable
);
186 /* Notify the st manager that this drawable is no longer valid */
187 stapi
->destroy_drawable(stapi
, &drawable
->base
);
193 * Validate the texture at an attachment. Allocate the texture if it does not
194 * exist. Used by the TFP extension.
197 dri_drawable_validate_att(struct dri_context
*ctx
,
198 struct dri_drawable
*drawable
,
199 enum st_attachment_type statt
)
201 enum st_attachment_type statts
[ST_ATTACHMENT_COUNT
];
202 unsigned i
, count
= 0;
204 /* check if buffer already exists */
205 if (drawable
->texture_mask
& (1 << statt
))
208 /* make sure DRI2 does not destroy existing buffers */
209 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
210 if (drawable
->texture_mask
& (1 << i
)) {
214 statts
[count
++] = statt
;
216 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
- 1;
218 drawable
->base
.validate(ctx
->st
, &drawable
->base
, statts
, count
, NULL
);
222 * These are used for GLX_EXT_texture_from_pixmap
225 dri_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
226 GLint format
, __DRIdrawable
*dPriv
)
228 struct dri_context
*ctx
= dri_context(pDRICtx
);
229 struct st_context_iface
*st
= ctx
->st
;
230 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
231 struct pipe_resource
*pt
;
233 if (st
->thread_finish
)
234 st
->thread_finish(st
);
236 dri_drawable_validate_att(ctx
, drawable
, ST_ATTACHMENT_FRONT_LEFT
);
238 /* Use the pipe resource associated with the X drawable */
239 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
242 enum pipe_format internal_format
= pt
->format
;
244 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
245 /* only need to cover the formats recognized by dri_fill_st_visual */
246 switch (internal_format
) {
247 case PIPE_FORMAT_BGRA8888_UNORM
:
248 internal_format
= PIPE_FORMAT_BGRX8888_UNORM
;
250 case PIPE_FORMAT_ARGB8888_UNORM
:
251 internal_format
= PIPE_FORMAT_XRGB8888_UNORM
;
258 drawable
->update_tex_buffer(drawable
, ctx
, pt
);
260 ctx
->st
->teximage(ctx
->st
,
261 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
262 0, internal_format
, pt
, FALSE
);
267 dri_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
268 __DRIdrawable
*dPriv
)
270 dri_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
273 const __DRItexBufferExtension driTexBufferExtension
= {
274 .base
= { __DRI_TEX_BUFFER
, 2 },
276 .setTexBuffer
= dri_set_tex_buffer
,
277 .setTexBuffer2
= dri_set_tex_buffer2
,
278 .releaseTexBuffer
= NULL
,
282 * Get the format and binding of an attachment.
285 dri_drawable_get_format(struct dri_drawable
*drawable
,
286 enum st_attachment_type statt
,
287 enum pipe_format
*format
,
291 case ST_ATTACHMENT_FRONT_LEFT
:
292 case ST_ATTACHMENT_BACK_LEFT
:
293 case ST_ATTACHMENT_FRONT_RIGHT
:
294 case ST_ATTACHMENT_BACK_RIGHT
:
295 /* Other pieces of the driver stack get confused and behave incorrectly
296 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
297 * though other means and handles it correctly, so we don't really need
298 * to use an sRGB format here.
300 *format
= util_format_linear(drawable
->stvis
.color_format
);
301 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
303 case ST_ATTACHMENT_DEPTH_STENCIL
:
304 *format
= drawable
->stvis
.depth_stencil_format
;
305 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
308 *format
= PIPE_FORMAT_NONE
;
316 * swap_fences_pop_front - pull a fence from the throttle queue
318 * If the throttle queue is filled to the desired number of fences,
319 * pull fences off the queue until the number is less than the desired
320 * number of fences, and return the last fence pulled.
322 static struct pipe_fence_handle
*
323 swap_fences_pop_front(struct dri_drawable
*draw
)
325 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
326 struct pipe_fence_handle
*fence
= NULL
;
328 if (draw
->desired_fences
== 0)
331 if (draw
->cur_fences
>= draw
->desired_fences
) {
332 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
333 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
334 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
342 * swap_fences_push_back - push a fence onto the throttle queue
344 * push a fence onto the throttle queue and pull fences of the queue
345 * so that the desired number of fences are on the queue.
348 swap_fences_push_back(struct dri_drawable
*draw
,
349 struct pipe_fence_handle
*fence
)
351 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
353 if (!fence
|| draw
->desired_fences
== 0)
356 while(draw
->cur_fences
== draw
->desired_fences
)
357 swap_fences_pop_front(draw
);
360 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
362 draw
->head
&= DRI_SWAP_FENCES_MASK
;
367 * swap_fences_unref - empty the throttle queue
369 * pulls fences of the throttle queue until it is empty.
372 swap_fences_unref(struct dri_drawable
*draw
)
374 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
376 while(draw
->cur_fences
) {
377 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
378 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
384 dri_pipe_blit(struct pipe_context
*pipe
,
385 struct pipe_resource
*dst
,
386 struct pipe_resource
*src
)
388 struct pipe_blit_info blit
;
393 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
394 * Fragment Operations):
396 * If a framebuffer object is not bound, after all operations have
397 * been completed on the multisample buffer, the sample values for
398 * each color in the multisample buffer are combined to produce a
399 * single color value, and that value is written into the
400 * corresponding color buffers selected by DrawBuffer or
401 * DrawBuffers. An implementation may defer the writing of the color
402 * buffers until a later time, but the state of the framebuffer must
403 * behave as if the color buffers were updated as each fragment was
404 * processed. The method of combination is not specified. If the
405 * framebuffer contains sRGB values, then it is recommended that the
406 * an average of sample values is computed in a linearized space, as
407 * for blending (see section 4.1.7).
409 * In other words, to do a resolve operation in a linear space, we have
410 * to set sRGB formats if the original resources were sRGB, so don't use
411 * util_format_linear.
414 memset(&blit
, 0, sizeof(blit
));
415 blit
.dst
.resource
= dst
;
416 blit
.dst
.box
.width
= dst
->width0
;
417 blit
.dst
.box
.height
= dst
->height0
;
418 blit
.dst
.box
.depth
= 1;
419 blit
.dst
.format
= dst
->format
;
420 blit
.src
.resource
= src
;
421 blit
.src
.box
.width
= src
->width0
;
422 blit
.src
.box
.height
= src
->height0
;
423 blit
.src
.box
.depth
= 1;
424 blit
.src
.format
= src
->format
;
425 blit
.mask
= PIPE_MASK_RGBA
;
426 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
428 pipe
->blit(pipe
, &blit
);
432 dri_postprocessing(struct dri_context
*ctx
,
433 struct dri_drawable
*drawable
,
434 enum st_attachment_type att
)
436 struct pipe_resource
*src
= drawable
->textures
[att
];
437 struct pipe_resource
*zsbuf
= drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
];
440 pp_run(ctx
->pp
, src
, src
, zsbuf
);
444 * DRI2 flush extension, the flush_with_flags function.
446 * \param context the context
447 * \param drawable the drawable to flush
448 * \param flags a combination of _DRI2_FLUSH_xxx flags
449 * \param throttle_reason the reason for throttling, 0 = no throttling
452 dri_flush(__DRIcontext
*cPriv
,
453 __DRIdrawable
*dPriv
,
455 enum __DRI2throttleReason reason
)
457 struct dri_context
*ctx
= dri_context(cPriv
);
458 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
459 struct st_context_iface
*st
;
460 unsigned flush_flags
;
461 boolean swap_msaa_buffers
= FALSE
;
469 if (st
->thread_finish
)
470 st
->thread_finish(st
);
473 /* prevent recursion */
474 if (drawable
->flushing
)
477 drawable
->flushing
= TRUE
;
480 flags
&= ~__DRI2_FLUSH_DRAWABLE
;
483 /* Flush the drawable. */
484 if ((flags
& __DRI2_FLUSH_DRAWABLE
) &&
485 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]) {
486 struct pipe_context
*pipe
= st
->pipe
;
488 if (drawable
->stvis
.samples
> 1 &&
489 reason
== __DRI2_THROTTLE_SWAPBUFFER
) {
490 /* Resolve the MSAA back buffer. */
491 dri_pipe_blit(st
->pipe
,
492 drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
],
493 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]);
495 if (drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] &&
496 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
]) {
497 swap_msaa_buffers
= TRUE
;
500 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
503 dri_postprocessing(ctx
, drawable
, ST_ATTACHMENT_BACK_LEFT
);
506 hud_draw(ctx
->hud
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
509 pipe
->flush_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_BACK_LEFT
]);
511 if (pipe
->invalidate_resource
&&
512 (flags
& __DRI2_FLUSH_INVALIDATE_ANCILLARY
)) {
513 if (drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
514 pipe
->invalidate_resource(pipe
, drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
515 if (drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
])
516 pipe
->invalidate_resource(pipe
, drawable
->msaa_textures
[ST_ATTACHMENT_DEPTH_STENCIL
]);
521 if (flags
& __DRI2_FLUSH_CONTEXT
)
522 flush_flags
|= ST_FLUSH_FRONT
;
523 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
)
524 flush_flags
|= ST_FLUSH_END_OF_FRAME
;
526 /* Flush the context and throttle if needed. */
527 if (dri_screen(ctx
->sPriv
)->throttling_enabled
&&
529 (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
530 reason
== __DRI2_THROTTLE_FLUSHFRONT
)) {
533 * This pulls a fence off the throttling queue and waits for it if the
534 * number of fences on the throttling queue has reached the desired
537 * Then flushes to insert a fence at the current rendering position, and
538 * pushes that fence on the queue. This requires that the st_context_iface
539 * flush method returns a fence even if there are no commands to flush.
541 struct pipe_screen
*screen
= drawable
->screen
->base
.screen
;
542 struct pipe_fence_handle
*fence
;
544 fence
= swap_fences_pop_front(drawable
);
546 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
547 screen
->fence_reference(screen
, &fence
, NULL
);
550 st
->flush(st
, flush_flags
, &fence
);
553 swap_fences_push_back(drawable
, fence
);
554 screen
->fence_reference(screen
, &fence
, NULL
);
557 else if (flags
& (__DRI2_FLUSH_DRAWABLE
| __DRI2_FLUSH_CONTEXT
)) {
558 st
->flush(st
, flush_flags
, NULL
);
562 drawable
->flushing
= FALSE
;
565 /* Swap the MSAA front and back buffers, so that reading
566 * from the front buffer after SwapBuffers returns what was
567 * in the back buffer.
569 if (swap_msaa_buffers
) {
570 struct pipe_resource
*tmp
=
571 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
];
573 drawable
->msaa_textures
[ST_ATTACHMENT_FRONT_LEFT
] =
574 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
];
575 drawable
->msaa_textures
[ST_ATTACHMENT_BACK_LEFT
] = tmp
;
577 /* Now that we have swapped the buffers, this tells the state
578 * tracker to revalidate the framebuffer.
580 p_atomic_inc(&drawable
->base
.stamp
);
585 * dri_throttle - A DRI2ThrottleExtension throttling function.
588 dri_throttle(__DRIcontext
*cPriv
, __DRIdrawable
*dPriv
,
589 enum __DRI2throttleReason reason
)
591 dri_flush(cPriv
, dPriv
, 0, reason
);
595 const __DRI2throttleExtension dri2ThrottleExtension
= {
596 .base
= { __DRI2_THROTTLE
, 1 },
598 .throttle
= dri_throttle
,
602 /* vim: set sw=3 ts=8 sts=3 expandtab: */