st/dri: resolve the back buffer only in SwapBuffers
[mesa.git] / src / gallium / state_trackers / dri / common / dri_drawable.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
35
36 #include "pipe/p_screen.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40
41 static void
42 swap_fences_unref(struct dri_drawable *draw);
43
44 static boolean
45 dri_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
46 const enum st_attachment_type *statts,
47 unsigned count,
48 struct pipe_resource **out)
49 {
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;
54 boolean new_stamp;
55 int i;
56 unsigned int lastStamp;
57 struct pipe_resource **textures =
58 drawable->stvis.samples > 1 ? drawable->msaa_textures
59 : drawable->textures;
60
61 statt_mask = 0x0;
62 for (i = 0; i < count; i++)
63 statt_mask |= (1 << statts[i]);
64
65 /* record newly allocated textures */
66 new_mask = (statt_mask & ~drawable->texture_mask);
67
68 /*
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
71 * checked.
72 */
73 do {
74 lastStamp = drawable->dPriv->lastStamp;
75 new_stamp = (drawable->texture_stamp != lastStamp);
76
77 if (new_stamp || new_mask || screen->broken_invalidate) {
78 if (new_stamp && drawable->update_drawable_info)
79 drawable->update_drawable_info(drawable);
80
81 drawable->allocate_textures(drawable, statts, count);
82
83 /* add existing textures */
84 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
85 if (textures[i])
86 statt_mask |= (1 << i);
87 }
88
89 drawable->texture_stamp = lastStamp;
90 drawable->texture_mask = statt_mask;
91 }
92 } while (lastStamp != drawable->dPriv->lastStamp);
93
94 if (!out)
95 return TRUE;
96
97 /* Set the window-system buffers for the state tracker. */
98 for (i = 0; i < count; i++) {
99 out[i] = NULL;
100 pipe_resource_reference(&out[i], textures[statts[i]]);
101 }
102
103 return TRUE;
104 }
105
106 static boolean
107 dri_st_framebuffer_flush_front(struct st_context_iface *stctx,
108 struct st_framebuffer_iface *stfbi,
109 enum st_attachment_type statt)
110 {
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;
114
115 /* XXX remove this and just set the correct one on the framebuffer */
116 drawable->flush_frontbuffer(ctx, drawable, statt);
117
118 return TRUE;
119 }
120
121 /**
122 * This is called when we need to set up GL rendering to a new X window.
123 */
124 boolean
125 dri_create_buffer(__DRIscreen * sPriv,
126 __DRIdrawable * dPriv,
127 const struct gl_config * visual, boolean isPixmap)
128 {
129 struct dri_screen *screen = sPriv->driverPrivate;
130 struct dri_drawable *drawable = NULL;
131
132 if (isPixmap)
133 goto fail; /* not implemented */
134
135 drawable = CALLOC_STRUCT(dri_drawable);
136 if (drawable == NULL)
137 goto fail;
138
139 dri_fill_st_visual(&drawable->stvis, screen, visual);
140
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;
146
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;
153
154 dPriv->driverPrivate = (void *)drawable;
155 p_atomic_set(&drawable->base.stamp, 1);
156
157 return GL_TRUE;
158 fail:
159 FREE(drawable);
160 return GL_FALSE;
161 }
162
163 void
164 dri_destroy_buffer(__DRIdrawable * dPriv)
165 {
166 struct dri_drawable *drawable = dri_drawable(dPriv);
167 int i;
168
169 pipe_surface_reference(&drawable->drisw_surface, NULL);
170
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);
175
176 swap_fences_unref(drawable);
177
178 FREE(drawable);
179 }
180
181 /**
182 * Validate the texture at an attachment. Allocate the texture if it does not
183 * exist. Used by the TFP extension.
184 */
185 static void
186 dri_drawable_validate_att(struct dri_drawable *drawable,
187 enum st_attachment_type statt)
188 {
189 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
190 unsigned i, count = 0;
191
192 /* check if buffer already exists */
193 if (drawable->texture_mask & (1 << statt))
194 return;
195
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)) {
199 statts[count++] = i;
200 }
201 }
202 statts[count++] = statt;
203
204 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
205
206 drawable->base.validate(&drawable->base, statts, count, NULL);
207 }
208
209 /**
210 * These are used for GLX_EXT_texture_from_pixmap
211 */
212 static void
213 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
214 GLint format, __DRIdrawable *dPriv)
215 {
216 struct dri_context *ctx = dri_context(pDRICtx);
217 struct dri_drawable *drawable = dri_drawable(dPriv);
218 struct pipe_resource *pt;
219
220 dri_drawable_validate_att(drawable, ST_ATTACHMENT_FRONT_LEFT);
221
222 /* Use the pipe resource associated with the X drawable */
223 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
224
225 if (pt) {
226 enum pipe_format internal_format = pt->format;
227
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;
233 break;
234 case PIPE_FORMAT_A8R8G8B8_UNORM:
235 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM;
236 break;
237 default:
238 break;
239 }
240 }
241
242 drawable->update_tex_buffer(drawable, ctx, pt);
243
244 ctx->st->teximage(ctx->st,
245 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
246 0, internal_format, pt, FALSE);
247 }
248 }
249
250 static void
251 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
252 __DRIdrawable *dPriv)
253 {
254 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
255 }
256
257 const __DRItexBufferExtension driTexBufferExtension = {
258 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
259 dri_set_tex_buffer,
260 dri_set_tex_buffer2,
261 NULL,
262 };
263
264 /**
265 * Get the format and binding of an attachment.
266 */
267 void
268 dri_drawable_get_format(struct dri_drawable *drawable,
269 enum st_attachment_type statt,
270 enum pipe_format *format,
271 unsigned *bind)
272 {
273 switch (statt) {
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;
280 break;
281 case ST_ATTACHMENT_DEPTH_STENCIL:
282 *format = drawable->stvis.depth_stencil_format;
283 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
284 break;
285 default:
286 *format = PIPE_FORMAT_NONE;
287 *bind = 0;
288 break;
289 }
290 }
291
292
293 /**
294 * swap_fences_pop_front - pull a fence from the throttle queue
295 *
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.
299 */
300 static struct pipe_fence_handle *
301 swap_fences_pop_front(struct dri_drawable *draw)
302 {
303 struct pipe_screen *screen = draw->screen->base.screen;
304 struct pipe_fence_handle *fence = NULL;
305
306 if (draw->desired_fences == 0)
307 return NULL;
308
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;
313 --draw->cur_fences;
314 }
315 return fence;
316 }
317
318
319 /**
320 * swap_fences_push_back - push a fence onto the throttle queue
321 *
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.
324 */
325 static void
326 swap_fences_push_back(struct dri_drawable *draw,
327 struct pipe_fence_handle *fence)
328 {
329 struct pipe_screen *screen = draw->screen->base.screen;
330
331 if (!fence || draw->desired_fences == 0)
332 return;
333
334 while(draw->cur_fences == draw->desired_fences)
335 swap_fences_pop_front(draw);
336
337 draw->cur_fences++;
338 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
339 fence);
340 draw->head &= DRI_SWAP_FENCES_MASK;
341 }
342
343
344 /**
345 * swap_fences_unref - empty the throttle queue
346 *
347 * pulls fences of the throttle queue until it is empty.
348 */
349 static void
350 swap_fences_unref(struct dri_drawable *draw)
351 {
352 struct pipe_screen *screen = draw->screen->base.screen;
353
354 while(draw->cur_fences) {
355 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
356 draw->tail &= DRI_SWAP_FENCES_MASK;
357 --draw->cur_fences;
358 }
359 }
360
361 void
362 dri_pipe_blit(struct pipe_context *pipe,
363 struct pipe_resource *dst,
364 struct pipe_resource *src)
365 {
366 struct pipe_blit_info blit;
367
368 if (!dst || !src)
369 return;
370
371 memset(&blit, 0, sizeof(blit));
372 blit.dst.resource = dst;
373 blit.dst.box.width = dst->width0;
374 blit.dst.box.height = dst->height0;
375 blit.dst.box.depth = 1;
376 blit.dst.format = util_format_linear(dst->format);
377 blit.src.resource = src;
378 blit.src.box.width = src->width0;
379 blit.src.box.height = src->height0;
380 blit.src.box.depth = 1;
381 blit.src.format = util_format_linear(src->format);
382 blit.mask = PIPE_MASK_RGBA;
383 blit.filter = PIPE_TEX_FILTER_NEAREST;
384
385 pipe->blit(pipe, &blit);
386 }
387
388 static void
389 dri_postprocessing(struct dri_context *ctx,
390 struct dri_drawable *drawable,
391 enum st_attachment_type att)
392 {
393 struct pipe_resource *src = drawable->textures[att];
394 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
395
396 if (ctx->pp && src && zsbuf)
397 pp_run(ctx->pp, src, src, zsbuf);
398 }
399
400 /**
401 * DRI2 flush extension, the flush_with_flags function.
402 *
403 * \param context the context
404 * \param drawable the drawable to flush
405 * \param flags a combination of _DRI2_FLUSH_xxx flags
406 * \param throttle_reason the reason for throttling, 0 = no throttling
407 */
408 void
409 dri_flush(__DRIcontext *cPriv,
410 __DRIdrawable *dPriv,
411 unsigned flags,
412 enum __DRI2throttleReason reason)
413 {
414 struct dri_context *ctx = dri_context(cPriv);
415 struct dri_drawable *drawable = dri_drawable(dPriv);
416 unsigned flush_flags;
417 boolean swap_msaa_buffers = FALSE;
418
419 if (!ctx) {
420 assert(0);
421 return;
422 }
423
424 if (drawable) {
425 /* prevent recursion */
426 if (drawable->flushing)
427 return;
428
429 drawable->flushing = TRUE;
430 }
431 else {
432 flags &= ~__DRI2_FLUSH_DRAWABLE;
433 }
434
435 /* Flush the drawable. */
436 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
437 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
438 if (drawable->stvis.samples > 1 &&
439 reason == __DRI2_THROTTLE_SWAPBUFFER) {
440 /* Resolve the MSAA back buffer. */
441 dri_pipe_blit(ctx->st->pipe,
442 drawable->textures[ST_ATTACHMENT_BACK_LEFT],
443 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
444
445 if (drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] &&
446 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]) {
447 swap_msaa_buffers = TRUE;
448 }
449
450 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
451 }
452
453 dri_postprocessing(ctx, drawable, ST_ATTACHMENT_BACK_LEFT);
454
455 if (ctx->hud) {
456 hud_draw(ctx->hud, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
457 }
458 }
459
460 flush_flags = 0;
461 if (flags & __DRI2_FLUSH_CONTEXT)
462 flush_flags |= ST_FLUSH_FRONT;
463 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
464 flush_flags |= ST_FLUSH_END_OF_FRAME;
465
466 /* Flush the context and throttle if needed. */
467 if (dri_screen(ctx->sPriv)->throttling_enabled &&
468 drawable &&
469 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
470 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
471 /* Throttle.
472 *
473 * This pulls a fence off the throttling queue and waits for it if the
474 * number of fences on the throttling queue has reached the desired
475 * number.
476 *
477 * Then flushes to insert a fence at the current rendering position, and
478 * pushes that fence on the queue. This requires that the st_context_iface
479 * flush method returns a fence even if there are no commands to flush.
480 */
481 struct pipe_screen *screen = drawable->screen->base.screen;
482 struct pipe_fence_handle *fence;
483
484 fence = swap_fences_pop_front(drawable);
485 if (fence) {
486 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
487 screen->fence_reference(screen, &fence, NULL);
488 }
489
490 ctx->st->flush(ctx->st, flush_flags, &fence);
491
492 if (fence) {
493 swap_fences_push_back(drawable, fence);
494 screen->fence_reference(screen, &fence, NULL);
495 }
496 }
497 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
498 ctx->st->flush(ctx->st, flush_flags, NULL);
499 }
500
501 if (drawable) {
502 drawable->flushing = FALSE;
503 }
504
505 /* Swap the MSAA front and back buffers, so that reading
506 * from the front buffer after SwapBuffers returns what was
507 * in the back buffer.
508 */
509 if (swap_msaa_buffers) {
510 struct pipe_resource *tmp =
511 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
512
513 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] =
514 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
515 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT] = tmp;
516
517 /* Now that we have swapped the buffers, this tells the state
518 * tracker to revalidate the framebuffer.
519 */
520 p_atomic_inc(&drawable->base.stamp);
521 }
522 }
523
524 /**
525 * dri_throttle - A DRI2ThrottleExtension throttling function.
526 */
527 static void
528 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
529 enum __DRI2throttleReason reason)
530 {
531 dri_flush(cPriv, dPriv, 0, reason);
532 }
533
534
535 const __DRI2throttleExtension dri2ThrottleExtension = {
536 .base = { __DRI2_THROTTLE, __DRI2_THROTTLE_VERSION },
537 .throttle = dri_throttle,
538 };
539
540
541 /* vim: set sw=3 ts=8 sts=3 expandtab: */