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/format/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
41 #include "state_tracker/st_context.h"
43 static uint32_t drifb_ID = 0;
46 dri_st_framebuffer_validate(struct st_context *st,
47 struct pipe_frontend_drawable *pdrawable,
48 const enum st_attachment_type *statts,
50 struct pipe_resource **out,
51 struct pipe_resource **resolve)
53 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
54 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
55 struct dri_screen *screen = drawable->screen;
56 unsigned statt_mask, new_mask;
59 unsigned int lastStamp;
60 struct pipe_resource **textures =
61 drawable->stvis.samples > 1 ? drawable->msaa_textures
65 for (i = 0; i < count; i++)
66 statt_mask |= (1 << statts[i]);
68 /* record newly allocated textures */
69 new_mask = (statt_mask & ~drawable->texture_mask);
72 lastStamp = drawable->lastStamp;
73 new_stamp = (drawable->texture_stamp != lastStamp);
75 if (new_stamp || new_mask) {
76 if (new_stamp && drawable->update_drawable_info)
77 drawable->update_drawable_info(drawable);
79 drawable->allocate_textures(ctx, drawable, statts, count);
81 /* add existing textures */
82 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
84 statt_mask |= (1 << i);
87 drawable->texture_stamp = lastStamp;
88 drawable->texture_mask = statt_mask;
90 } while (lastStamp != drawable->lastStamp);
92 /* Flush the pending set_damage_region request. */
93 struct pipe_screen *pscreen = screen->base.screen;
95 if (new_mask & (1 << ST_ATTACHMENT_BACK_LEFT) &&
96 pscreen->set_damage_region) {
97 struct pipe_resource *resource = textures[ST_ATTACHMENT_BACK_LEFT];
99 pscreen->set_damage_region(pscreen, resource,
100 drawable->num_damage_rects,
101 drawable->damage_rects);
107 /* Set the window-system buffers for the gallium frontend. */
108 for (i = 0; i < count; i++)
109 pipe_resource_reference(&out[i], textures[statts[i]]);
110 if (resolve && drawable->stvis.samples > 1) {
111 if (statt_mask & BITFIELD_BIT(ST_ATTACHMENT_FRONT_LEFT))
112 pipe_resource_reference(resolve, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
113 else if (statt_mask & BITFIELD_BIT(ST_ATTACHMENT_BACK_LEFT))
114 pipe_resource_reference(resolve, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
121 dri_st_framebuffer_flush_front(struct st_context *st,
122 struct pipe_frontend_drawable *pdrawable,
123 enum st_attachment_type statt)
125 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
126 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
128 /* XXX remove this and just set the correct one on the framebuffer */
129 return drawable->flush_frontbuffer(ctx, drawable, statt);
133 * The gallium frontend framebuffer interface flush_swapbuffers callback
136 dri_st_framebuffer_flush_swapbuffers(struct st_context *st,
137 struct pipe_frontend_drawable *pdrawable)
139 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
140 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
142 if (drawable->flush_swapbuffers)
143 drawable->flush_swapbuffers(ctx, drawable);
149 * This is called when we need to set up GL rendering to a new X window.
151 struct dri_drawable *
152 dri_create_drawable(struct dri_screen *screen, const struct gl_config *visual,
153 bool isPixmap, void *loaderPrivate)
155 struct dri_drawable *drawable = NULL;
158 goto fail; /* not implemented */
160 drawable = CALLOC_STRUCT(dri_drawable);
161 if (drawable == NULL)
164 drawable->loaderPrivate = loaderPrivate;
165 drawable->refcount = 1;
166 drawable->lastStamp = 0;
170 dri_fill_st_visual(&drawable->stvis, screen, visual);
172 /* setup the pipe_frontend_drawable */
173 drawable->base.visual = &drawable->stvis;
174 drawable->base.flush_front = dri_st_framebuffer_flush_front;
175 drawable->base.validate = dri_st_framebuffer_validate;
176 drawable->base.flush_swapbuffers = dri_st_framebuffer_flush_swapbuffers;
178 drawable->screen = screen;
180 p_atomic_set(&drawable->base.stamp, 1);
181 drawable->base.ID = p_atomic_inc_return(&drifb_ID);
182 drawable->base.fscreen = &screen->base;
191 dri_destroy_drawable(struct dri_drawable *drawable)
193 struct dri_screen *screen = drawable->screen;
196 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
197 pipe_resource_reference(&drawable->textures[i], NULL);
198 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
199 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
201 screen->base.screen->fence_reference(screen->base.screen,
202 &drawable->throttle_fence, NULL);
204 /* Notify the st manager that this drawable is no longer valid */
205 st_api_destroy_drawable(&drawable->base);
207 FREE(drawable->damage_rects);
212 dri_put_drawable(struct dri_drawable *drawable)
215 int refcount = --drawable->refcount;
216 assert(refcount >= 0);
219 dri_destroy_drawable(drawable);
224 * Validate the texture at an attachment. Allocate the texture if it does not
225 * exist. Used by the TFP extension.
228 dri_drawable_validate_att(struct dri_context *ctx,
229 struct dri_drawable *drawable,
230 enum st_attachment_type statt)
232 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
233 unsigned i, count = 0;
235 /* check if buffer already exists */
236 if (drawable->texture_mask & (1 << statt))
239 /* make sure DRI2 does not destroy existing buffers */
240 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
241 if (drawable->texture_mask & (1 << i)) {
245 statts[count++] = statt;
247 drawable->texture_stamp = drawable->lastStamp - 1;
249 drawable->base.validate(ctx->st, &drawable->base, statts, count, NULL, NULL);
253 * These are used for GLX_EXT_texture_from_pixmap
256 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
257 GLint format, __DRIdrawable *dPriv)
259 struct dri_context *ctx = dri_context(pDRICtx);
260 struct st_context *st = ctx->st;
261 struct dri_drawable *drawable = dri_drawable(dPriv);
262 struct pipe_resource *pt;
264 _mesa_glthread_finish(st->ctx);
266 dri_drawable_validate_att(ctx, drawable, ST_ATTACHMENT_FRONT_LEFT);
268 /* Use the pipe resource associated with the X drawable */
269 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
272 enum pipe_format internal_format = pt->format;
274 if (format == __DRI_TEXTURE_FORMAT_RGB) {
275 /* only need to cover the formats recognized by dri_fill_st_visual */
276 switch (internal_format) {
277 case PIPE_FORMAT_R16G16B16A16_FLOAT:
278 internal_format = PIPE_FORMAT_R16G16B16X16_FLOAT;
280 case PIPE_FORMAT_B10G10R10A2_UNORM:
281 internal_format = PIPE_FORMAT_B10G10R10X2_UNORM;
283 case PIPE_FORMAT_R10G10B10A2_UNORM:
284 internal_format = PIPE_FORMAT_R10G10B10X2_UNORM;
286 case PIPE_FORMAT_BGRA8888_UNORM:
287 internal_format = PIPE_FORMAT_BGRX8888_UNORM;
289 case PIPE_FORMAT_ARGB8888_UNORM:
290 internal_format = PIPE_FORMAT_XRGB8888_UNORM;
297 drawable->update_tex_buffer(drawable, ctx, pt);
299 st_context_teximage(ctx->st, target, 0, internal_format, pt, false);
304 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
305 __DRIdrawable *dPriv)
307 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
310 const __DRItexBufferExtension driTexBufferExtension = {
311 .base = { __DRI_TEX_BUFFER, 2 },
313 .setTexBuffer = dri_set_tex_buffer,
314 .setTexBuffer2 = dri_set_tex_buffer2,
315 .releaseTexBuffer = NULL,
319 * Get the format and binding of an attachment.
322 dri_drawable_get_format(struct dri_drawable *drawable,
323 enum st_attachment_type statt,
324 enum pipe_format *format,
328 case ST_ATTACHMENT_FRONT_LEFT:
329 case ST_ATTACHMENT_BACK_LEFT:
330 case ST_ATTACHMENT_FRONT_RIGHT:
331 case ST_ATTACHMENT_BACK_RIGHT:
332 /* Other pieces of the driver stack get confused and behave incorrectly
333 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
334 * though other means and handles it correctly, so we don't really need
335 * to use an sRGB format here.
337 *format = util_format_linear(drawable->stvis.color_format);
338 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
340 case ST_ATTACHMENT_DEPTH_STENCIL:
341 *format = drawable->stvis.depth_stencil_format;
342 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
345 *format = PIPE_FORMAT_NONE;
352 dri_pipe_blit(struct pipe_context *pipe,
353 struct pipe_resource *dst,
354 struct pipe_resource *src)
356 struct pipe_blit_info blit;
361 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
362 * Fragment Operations):
364 * If a framebuffer object is not bound, after all operations have
365 * been completed on the multisample buffer, the sample values for
366 * each color in the multisample buffer are combined to produce a
367 * single color value, and that value is written into the
368 * corresponding color buffers selected by DrawBuffer or
369 * DrawBuffers. An implementation may defer the writing of the color
370 * buffers until a later time, but the state of the framebuffer must
371 * behave as if the color buffers were updated as each fragment was
372 * processed. The method of combination is not specified. If the
373 * framebuffer contains sRGB values, then it is recommended that the
374 * an average of sample values is computed in a linearized space, as
375 * for blending (see section 4.1.7).
377 * In other words, to do a resolve operation in a linear space, we have
378 * to set sRGB formats if the original resources were sRGB, so don't use
379 * util_format_linear.
382 memset(&blit, 0, sizeof(blit));
383 blit.dst.resource = dst;
384 blit.dst.box.width = dst->width0;
385 blit.dst.box.height = dst->height0;
386 blit.dst.box.depth = 1;
387 blit.dst.format = dst->format;
388 blit.src.resource = src;
389 blit.src.box.width = src->width0;
390 blit.src.box.height = src->height0;
391 blit.src.box.depth = 1;
392 blit.src.format = src->format;
393 blit.mask = PIPE_MASK_RGBA;
394 blit.filter = PIPE_TEX_FILTER_NEAREST;
396 pipe->blit(pipe, &blit);
400 dri_postprocessing(struct dri_context *ctx,
401 struct dri_drawable *drawable,
402 enum st_attachment_type att)
404 struct pipe_resource *src = drawable->textures[att];
405 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
408 pp_run(ctx->pp, src, src, zsbuf);
411 struct notify_before_flush_cb_args {
412 struct dri_context *ctx;
413 struct dri_drawable *drawable;
415 enum __DRI2throttleReason reason;
416 bool swap_msaa_buffers;
420 notify_before_flush_cb(void* _args)
422 struct notify_before_flush_cb_args *args = (struct notify_before_flush_cb_args *) _args;
423 struct st_context *st = args->ctx->st;
424 struct pipe_context *pipe = st->pipe;
426 /* Wait for glthread to finish because we can't use pipe_context from
429 _mesa_glthread_finish(st->ctx);
431 if (args->drawable->stvis.samples > 1 &&
432 (args->reason == __DRI2_THROTTLE_SWAPBUFFER ||
433 args->reason == __DRI2_NOTHROTTLE_SWAPBUFFER ||
434 args->reason == __DRI2_THROTTLE_COPYSUBBUFFER)) {
435 /* Resolve the MSAA back buffer. */
436 dri_pipe_blit(st->pipe,
437 args->drawable->textures[ST_ATTACHMENT_BACK_LEFT],
438 args->drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
440 if ((args->reason == __DRI2_THROTTLE_SWAPBUFFER ||
441 args->reason == __DRI2_NOTHROTTLE_SWAPBUFFER) &&
442 args->drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] &&
443 args->drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]) {
444 args->swap_msaa_buffers = true;
447 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
450 dri_postprocessing(args->ctx, args->drawable, ST_ATTACHMENT_BACK_LEFT);
452 if (pipe->invalidate_resource &&
453 (args->flags & __DRI2_FLUSH_INVALIDATE_ANCILLARY)) {
454 if (args->drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
455 pipe->invalidate_resource(pipe, args->drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
456 if (args->drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL])
457 pipe->invalidate_resource(pipe, args->drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL]);
460 if (args->ctx->hud) {
461 hud_run(args->ctx->hud, args->ctx->st->cso_context,
462 args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
465 pipe->flush_resource(pipe, args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
469 * DRI2 flush extension, the flush_with_flags function.
471 * \param context the context
472 * \param drawable the drawable to flush
473 * \param flags a combination of _DRI2_FLUSH_xxx flags
474 * \param throttle_reason the reason for throttling, 0 = no throttling
477 dri_flush(__DRIcontext *cPriv,
478 __DRIdrawable *dPriv,
480 enum __DRI2throttleReason reason)
482 struct dri_context *ctx = dri_context(cPriv);
483 struct dri_drawable *drawable = dri_drawable(dPriv);
484 struct st_context *st;
485 unsigned flush_flags;
486 struct notify_before_flush_cb_args args = { 0 };
494 _mesa_glthread_finish(st->ctx);
497 /* prevent recursion */
498 if (drawable->flushing)
501 drawable->flushing = true;
504 flags &= ~__DRI2_FLUSH_DRAWABLE;
507 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
508 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
509 /* We can't do operations on the back buffer here, because there
510 * may be some pending operations that will get flushed by the
511 * call to st->flush (eg: FLUSH_VERTICES).
512 * Instead we register a callback to be notified when all operations
513 * have been submitted but before the call to st_flush.
516 args.drawable = drawable;
518 args.reason = reason;
522 if (flags & __DRI2_FLUSH_CONTEXT)
523 flush_flags |= ST_FLUSH_FRONT;
524 if (reason == __DRI2_THROTTLE_SWAPBUFFER ||
525 reason == __DRI2_NOTHROTTLE_SWAPBUFFER)
526 flush_flags |= ST_FLUSH_END_OF_FRAME;
528 /* Flush the context and throttle if needed. */
529 if (ctx->screen->throttle &&
531 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
532 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
534 struct pipe_screen *screen = drawable->screen->base.screen;
535 struct pipe_fence_handle *new_fence = NULL;
537 st_context_flush(st, flush_flags, &new_fence, args.ctx ? notify_before_flush_cb : NULL, &args);
539 /* throttle on the previous fence */
540 if (drawable->throttle_fence) {
541 screen->fence_finish(screen, NULL, drawable->throttle_fence, OS_TIMEOUT_INFINITE);
542 screen->fence_reference(screen, &drawable->throttle_fence, NULL);
544 drawable->throttle_fence = new_fence;
546 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
547 st_context_flush(st, flush_flags, NULL, args.ctx ? notify_before_flush_cb : NULL, &args);
551 drawable->flushing = false;
554 /* Swap the MSAA front and back buffers, so that reading
555 * from the front buffer after SwapBuffers returns what was
556 * in the back buffer.
558 if (args.swap_msaa_buffers) {
559 struct pipe_resource *tmp =
560 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
562 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] =
563 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
564 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT] = tmp;
566 /* Now that we have swapped the buffers, this tells the gallium
567 * frontend to revalidate the framebuffer.
569 p_atomic_inc(&drawable->base.stamp);
572 st_context_invalidate_state(st, ST_INVALIDATE_FB_STATE);
576 * DRI2 flush extension.
579 dri_flush_drawable(__DRIdrawable *dPriv)
581 struct dri_context *ctx = dri_get_current();
584 dri_flush(opaque_dri_context(ctx), dPriv, __DRI2_FLUSH_DRAWABLE, -1);
588 * dri_throttle - A DRI2ThrottleExtension throttling function.
591 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
592 enum __DRI2throttleReason reason)
594 dri_flush(cPriv, dPriv, 0, reason);
598 const __DRI2throttleExtension dri2ThrottleExtension = {
599 .base = { __DRI2_THROTTLE, 1 },
601 .throttle = dri_throttle,
605 /* vim: set sw=3 ts=8 sts=3 expandtab: */