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_framebuffer_iface
*stfbi
,
46 const enum st_attachment_type
*statts
,
48 struct pipe_resource
**out
)
50 struct dri_drawable
*drawable
=
51 (struct dri_drawable
*) stfbi
->st_manager_private
;
52 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
53 unsigned statt_mask
, new_mask
;
56 unsigned int lastStamp
;
57 struct pipe_resource
**textures
=
58 drawable
->stvis
.samples
> 1 ? drawable
->msaa_textures
62 for (i
= 0; i
< count
; i
++)
63 statt_mask
|= (1 << statts
[i
]);
65 /* record newly allocated textures */
66 new_mask
= (statt_mask
& ~drawable
->texture_mask
);
69 * dPriv->dri2.stamp is the server stamp. dPriv->lastStamp is the
70 * client stamp. It has the value of the server stamp when last
74 lastStamp
= drawable
->dPriv
->lastStamp
;
75 new_stamp
= (drawable
->texture_stamp
!= lastStamp
);
77 if (new_stamp
|| new_mask
|| screen
->broken_invalidate
) {
78 if (new_stamp
&& drawable
->update_drawable_info
)
79 drawable
->update_drawable_info(drawable
);
81 drawable
->allocate_textures(drawable
, statts
, count
);
83 /* add existing textures */
84 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
86 statt_mask
|= (1 << i
);
89 drawable
->texture_stamp
= lastStamp
;
90 drawable
->texture_mask
= statt_mask
;
92 } while (lastStamp
!= drawable
->dPriv
->lastStamp
);
97 /* Set the window-system buffers for the state tracker. */
98 for (i
= 0; i
< count
; i
++) {
100 pipe_resource_reference(&out
[i
], textures
[statts
[i
]]);
107 dri_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
108 struct st_framebuffer_iface
*stfbi
,
109 enum st_attachment_type statt
)
111 struct dri_context
*ctx
= (struct dri_context
*)stctx
->st_manager_private
;
112 struct dri_drawable
*drawable
=
113 (struct dri_drawable
*) stfbi
->st_manager_private
;
115 /* XXX remove this and just set the correct one on the framebuffer */
116 drawable
->flush_frontbuffer(ctx
, drawable
, statt
);
122 * This is called when we need to set up GL rendering to a new X window.
125 dri_create_buffer(__DRIscreen
* sPriv
,
126 __DRIdrawable
* dPriv
,
127 const struct gl_config
* visual
, boolean isPixmap
)
129 struct dri_screen
*screen
= sPriv
->driverPrivate
;
130 struct dri_drawable
*drawable
= NULL
;
133 goto fail
; /* not implemented */
135 drawable
= CALLOC_STRUCT(dri_drawable
);
136 if (drawable
== NULL
)
139 dri_fill_st_visual(&drawable
->stvis
, screen
, visual
);
141 /* setup the st_framebuffer_iface */
142 drawable
->base
.visual
= &drawable
->stvis
;
143 drawable
->base
.flush_front
= dri_st_framebuffer_flush_front
;
144 drawable
->base
.validate
= dri_st_framebuffer_validate
;
145 drawable
->base
.st_manager_private
= (void *) drawable
;
147 drawable
->screen
= screen
;
148 drawable
->sPriv
= sPriv
;
149 drawable
->dPriv
= dPriv
;
150 drawable
->desired_fences
= screen
->default_throttle_frames
;
151 if (drawable
->desired_fences
> DRI_SWAP_FENCES_MAX
)
152 drawable
->desired_fences
= DRI_SWAP_FENCES_MAX
;
154 dPriv
->driverPrivate
= (void *)drawable
;
155 p_atomic_set(&drawable
->base
.stamp
, 1);
164 dri_destroy_buffer(__DRIdrawable
* dPriv
)
166 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
169 pipe_surface_reference(&drawable
->drisw_surface
, NULL
);
171 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
172 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
173 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
174 pipe_resource_reference(&drawable
->msaa_textures
[i
], NULL
);
176 swap_fences_unref(drawable
);
182 * Validate the texture at an attachment. Allocate the texture if it does not
183 * exist. Used by the TFP extension.
186 dri_drawable_validate_att(struct dri_drawable
*drawable
,
187 enum st_attachment_type statt
)
189 enum st_attachment_type statts
[ST_ATTACHMENT_COUNT
];
190 unsigned i
, count
= 0;
192 /* check if buffer already exists */
193 if (drawable
->texture_mask
& (1 << statt
))
196 /* make sure DRI2 does not destroy existing buffers */
197 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
198 if (drawable
->texture_mask
& (1 << i
)) {
202 statts
[count
++] = statt
;
204 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
- 1;
206 drawable
->base
.validate(&drawable
->base
, statts
, count
, NULL
);
210 * These are used for GLX_EXT_texture_from_pixmap
213 dri_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
214 GLint format
, __DRIdrawable
*dPriv
)
216 struct dri_context
*ctx
= dri_context(pDRICtx
);
217 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
218 struct pipe_resource
*pt
;
220 dri_drawable_validate_att(drawable
, ST_ATTACHMENT_FRONT_LEFT
);
222 /* Use the pipe resource associated with the X drawable */
223 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
226 enum pipe_format internal_format
= pt
->format
;
228 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
229 /* only need to cover the formats recognized by dri_fill_st_visual */
230 switch (internal_format
) {
231 case PIPE_FORMAT_B8G8R8A8_UNORM
:
232 internal_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
234 case PIPE_FORMAT_A8R8G8B8_UNORM
:
235 internal_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
242 drawable
->update_tex_buffer(drawable
, ctx
, pt
);
244 ctx
->st
->teximage(ctx
->st
,
245 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
246 0, internal_format
, pt
, FALSE
);
251 dri_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
252 __DRIdrawable
*dPriv
)
254 dri_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
257 const __DRItexBufferExtension driTexBufferExtension
= {
258 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
265 * Get the format and binding of an attachment.
268 dri_drawable_get_format(struct dri_drawable
*drawable
,
269 enum st_attachment_type statt
,
270 enum pipe_format
*format
,
274 case ST_ATTACHMENT_FRONT_LEFT
:
275 case ST_ATTACHMENT_BACK_LEFT
:
276 case ST_ATTACHMENT_FRONT_RIGHT
:
277 case ST_ATTACHMENT_BACK_RIGHT
:
278 *format
= drawable
->stvis
.color_format
;
279 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
281 case ST_ATTACHMENT_DEPTH_STENCIL
:
282 *format
= drawable
->stvis
.depth_stencil_format
;
283 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
286 *format
= PIPE_FORMAT_NONE
;
294 * swap_fences_pop_front - pull a fence from the throttle queue
296 * If the throttle queue is filled to the desired number of fences,
297 * pull fences off the queue until the number is less than the desired
298 * number of fences, and return the last fence pulled.
300 static struct pipe_fence_handle
*
301 swap_fences_pop_front(struct dri_drawable
*draw
)
303 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
304 struct pipe_fence_handle
*fence
= NULL
;
306 if (draw
->desired_fences
== 0)
309 if (draw
->cur_fences
>= draw
->desired_fences
) {
310 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
311 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
312 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
320 * swap_fences_push_back - push a fence onto the throttle queue
322 * push a fence onto the throttle queue and pull fences of the queue
323 * so that the desired number of fences are on the queue.
326 swap_fences_push_back(struct dri_drawable
*draw
,
327 struct pipe_fence_handle
*fence
)
329 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
331 if (!fence
|| draw
->desired_fences
== 0)
334 while(draw
->cur_fences
== draw
->desired_fences
)
335 swap_fences_pop_front(draw
);
338 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
340 draw
->head
&= DRI_SWAP_FENCES_MASK
;
345 * swap_fences_unref - empty the throttle queue
347 * pulls fences of the throttle queue until it is empty.
350 swap_fences_unref(struct dri_drawable
*draw
)
352 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
354 while(draw
->cur_fences
) {
355 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
356 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
362 dri_msaa_resolve(struct dri_context
*ctx
,
363 struct dri_drawable
*drawable
,
364 enum st_attachment_type att
)
366 struct pipe_context
*pipe
= ctx
->st
->pipe
;
367 struct pipe_resource
*dst
= drawable
->textures
[att
];
368 struct pipe_resource
*src
= drawable
->msaa_textures
[att
];
369 struct pipe_blit_info blit
;
374 memset(&blit
, 0, sizeof(blit
));
375 blit
.dst
.resource
= dst
;
376 blit
.dst
.box
.width
= dst
->width0
;
377 blit
.dst
.box
.height
= dst
->width0
;
378 blit
.dst
.box
.depth
= 1;
379 blit
.dst
.format
= util_format_linear(dst
->format
);
380 blit
.src
.resource
= src
;
381 blit
.src
.box
.width
= src
->width0
;
382 blit
.src
.box
.height
= src
->width0
;
383 blit
.src
.box
.depth
= 1;
384 blit
.src
.format
= util_format_linear(src
->format
);
385 blit
.mask
= PIPE_MASK_RGBA
;
386 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
388 pipe
->blit(pipe
, &blit
);
392 dri_postprocessing(struct dri_context
*ctx
,
393 struct dri_drawable
*drawable
,
394 enum st_attachment_type att
)
396 struct pipe_resource
*src
= drawable
->textures
[att
];
397 struct pipe_resource
*zsbuf
= drawable
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
];
399 if (ctx
->pp
&& src
&& zsbuf
)
400 pp_run(ctx
->pp
, src
, src
, zsbuf
);
404 * DRI2 flush extension, the flush_with_flags function.
406 * \param context the context
407 * \param drawable the drawable to flush
408 * \param flags a combination of _DRI2_FLUSH_xxx flags
409 * \param throttle_reason the reason for throttling, 0 = no throttling
412 dri_flush(__DRIcontext
*cPriv
,
413 __DRIdrawable
*dPriv
,
415 enum __DRI2throttleReason reason
)
417 struct dri_context
*ctx
= dri_context(cPriv
);
418 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
419 unsigned flush_flags
;
427 flags
&= ~__DRI2_FLUSH_DRAWABLE
;
430 /* Flush the drawable. */
431 if (flags
& __DRI2_FLUSH_DRAWABLE
) {
432 /* Resolve MSAA buffers. */
433 if (drawable
->stvis
.samples
> 1) {
434 dri_msaa_resolve(ctx
, drawable
, ST_ATTACHMENT_BACK_LEFT
);
435 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
438 dri_postprocessing(ctx
, drawable
, ST_ATTACHMENT_BACK_LEFT
);
442 if (flags
& __DRI2_FLUSH_CONTEXT
)
443 flush_flags
|= ST_FLUSH_FRONT
;
444 if (reason
== __DRI2_THROTTLE_SWAPBUFFER
)
445 flush_flags
|= ST_FLUSH_END_OF_FRAME
;
447 /* Flush the context and throttle if needed. */
448 if (dri_screen(ctx
->sPriv
)->throttling_enabled
&&
449 (reason
== __DRI2_THROTTLE_SWAPBUFFER
||
450 reason
== __DRI2_THROTTLE_FLUSHFRONT
)) {
453 * This pulls a fence off the throttling queue and waits for it if the
454 * number of fences on the throttling queue has reached the desired
457 * Then flushes to insert a fence at the current rendering position, and
458 * pushes that fence on the queue. This requires that the st_context_iface
459 * flush method returns a fence even if there are no commands to flush.
461 struct dri_drawable
*draw
= dri_drawable(dPriv
);
462 struct pipe_screen
*screen
= draw
->screen
->base
.screen
;
463 struct pipe_fence_handle
*fence
;
465 fence
= swap_fences_pop_front(draw
);
467 (void) screen
->fence_finish(screen
, fence
, PIPE_TIMEOUT_INFINITE
);
468 screen
->fence_reference(screen
, &fence
, NULL
);
471 ctx
->st
->flush(ctx
->st
, flush_flags
, &fence
);
473 swap_fences_push_back(draw
, fence
);
474 screen
->fence_reference(screen
, &fence
, NULL
);
477 else if (flags
& (__DRI2_FLUSH_DRAWABLE
| __DRI2_FLUSH_CONTEXT
)) {
478 ctx
->st
->flush(ctx
->st
, flush_flags
, NULL
);
483 * dri_throttle - A DRI2ThrottleExtension throttling function.
486 dri_throttle(__DRIcontext
*cPriv
, __DRIdrawable
*dPriv
,
487 enum __DRI2throttleReason reason
)
489 dri_flush(cPriv
, dPriv
, 0, reason
);
493 const __DRI2throttleExtension dri2ThrottleExtension
= {
494 .base
= { __DRI2_THROTTLE
, __DRI2_THROTTLE_VERSION
},
495 .throttle
= dri_throttle
,
499 /* vim: set sw=3 ts=8 sts=3 expandtab: */