st/mesa: create framebuffer iface hash table per st manager
[mesa.git] / src / gallium / state_trackers / dri / 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 uint32_t drifb_ID = 0;
42
43 static void
44 swap_fences_unref(struct dri_drawable *draw);
45
46 static boolean
47 dri_st_framebuffer_validate(struct st_context_iface *stctx,
48 struct st_framebuffer_iface *stfbi,
49 const enum st_attachment_type *statts,
50 unsigned count,
51 struct pipe_resource **out)
52 {
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;
58 boolean new_stamp;
59 int i;
60 unsigned int lastStamp;
61 struct pipe_resource **textures =
62 drawable->stvis.samples > 1 ? drawable->msaa_textures
63 : drawable->textures;
64
65 statt_mask = 0x0;
66 for (i = 0; i < count; i++)
67 statt_mask |= (1 << statts[i]);
68
69 /* record newly allocated textures */
70 new_mask = (statt_mask & ~drawable->texture_mask);
71
72 /*
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
75 * checked.
76 */
77 do {
78 lastStamp = drawable->dPriv->lastStamp;
79 new_stamp = (drawable->texture_stamp != lastStamp);
80
81 if (new_stamp || new_mask || screen->broken_invalidate) {
82 if (new_stamp && drawable->update_drawable_info)
83 drawable->update_drawable_info(drawable);
84
85 drawable->allocate_textures(ctx, drawable, statts, count);
86
87 /* add existing textures */
88 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
89 if (textures[i])
90 statt_mask |= (1 << i);
91 }
92
93 drawable->texture_stamp = lastStamp;
94 drawable->texture_mask = statt_mask;
95 }
96 } while (lastStamp != drawable->dPriv->lastStamp);
97
98 if (!out)
99 return TRUE;
100
101 /* Set the window-system buffers for the state tracker. */
102 for (i = 0; i < count; i++) {
103 out[i] = NULL;
104 pipe_resource_reference(&out[i], textures[statts[i]]);
105 }
106
107 return TRUE;
108 }
109
110 static boolean
111 dri_st_framebuffer_flush_front(struct st_context_iface *stctx,
112 struct st_framebuffer_iface *stfbi,
113 enum st_attachment_type statt)
114 {
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;
118
119 /* XXX remove this and just set the correct one on the framebuffer */
120 drawable->flush_frontbuffer(ctx, drawable, statt);
121
122 return TRUE;
123 }
124
125 /**
126 * This is called when we need to set up GL rendering to a new X window.
127 */
128 boolean
129 dri_create_buffer(__DRIscreen * sPriv,
130 __DRIdrawable * dPriv,
131 const struct gl_config * visual, boolean isPixmap)
132 {
133 struct dri_screen *screen = sPriv->driverPrivate;
134 struct dri_drawable *drawable = NULL;
135
136 if (isPixmap)
137 goto fail; /* not implemented */
138
139 drawable = CALLOC_STRUCT(dri_drawable);
140 if (drawable == NULL)
141 goto fail;
142
143 dri_fill_st_visual(&drawable->stvis, screen, visual);
144
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;
150
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;
157
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;
162
163 return GL_TRUE;
164 fail:
165 FREE(drawable);
166 return GL_FALSE;
167 }
168
169 void
170 dri_destroy_buffer(__DRIdrawable * dPriv)
171 {
172 struct dri_drawable *drawable = dri_drawable(dPriv);
173 struct dri_screen *screen = drawable->screen;
174 struct st_api *stapi = screen->st_api;
175 int i;
176
177 pipe_surface_reference(&drawable->drisw_surface, NULL);
178
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);
183
184 swap_fences_unref(drawable);
185
186 /* Notify the st manager that this drawable is no longer valid */
187 stapi->destroy_drawable(stapi, &drawable->base);
188
189 FREE(drawable);
190 }
191
192 /**
193 * Validate the texture at an attachment. Allocate the texture if it does not
194 * exist. Used by the TFP extension.
195 */
196 static void
197 dri_drawable_validate_att(struct dri_context *ctx,
198 struct dri_drawable *drawable,
199 enum st_attachment_type statt)
200 {
201 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
202 unsigned i, count = 0;
203
204 /* check if buffer already exists */
205 if (drawable->texture_mask & (1 << statt))
206 return;
207
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)) {
211 statts[count++] = i;
212 }
213 }
214 statts[count++] = statt;
215
216 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
217
218 drawable->base.validate(ctx->st, &drawable->base, statts, count, NULL);
219 }
220
221 /**
222 * These are used for GLX_EXT_texture_from_pixmap
223 */
224 static void
225 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
226 GLint format, __DRIdrawable *dPriv)
227 {
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;
232
233 if (st->thread_finish)
234 st->thread_finish(st);
235
236 dri_drawable_validate_att(ctx, drawable, ST_ATTACHMENT_FRONT_LEFT);
237
238 /* Use the pipe resource associated with the X drawable */
239 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
240
241 if (pt) {
242 enum pipe_format internal_format = pt->format;
243
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;
249 break;
250 case PIPE_FORMAT_ARGB8888_UNORM:
251 internal_format = PIPE_FORMAT_XRGB8888_UNORM;
252 break;
253 default:
254 break;
255 }
256 }
257
258 drawable->update_tex_buffer(drawable, ctx, pt);
259
260 ctx->st->teximage(ctx->st,
261 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
262 0, internal_format, pt, FALSE);
263 }
264 }
265
266 static void
267 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
268 __DRIdrawable *dPriv)
269 {
270 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
271 }
272
273 const __DRItexBufferExtension driTexBufferExtension = {
274 .base = { __DRI_TEX_BUFFER, 2 },
275
276 .setTexBuffer = dri_set_tex_buffer,
277 .setTexBuffer2 = dri_set_tex_buffer2,
278 .releaseTexBuffer = NULL,
279 };
280
281 /**
282 * Get the format and binding of an attachment.
283 */
284 void
285 dri_drawable_get_format(struct dri_drawable *drawable,
286 enum st_attachment_type statt,
287 enum pipe_format *format,
288 unsigned *bind)
289 {
290 switch (statt) {
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.
299 */
300 *format = util_format_linear(drawable->stvis.color_format);
301 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
302 break;
303 case ST_ATTACHMENT_DEPTH_STENCIL:
304 *format = drawable->stvis.depth_stencil_format;
305 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
306 break;
307 default:
308 *format = PIPE_FORMAT_NONE;
309 *bind = 0;
310 break;
311 }
312 }
313
314
315 /**
316 * swap_fences_pop_front - pull a fence from the throttle queue
317 *
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.
321 */
322 static struct pipe_fence_handle *
323 swap_fences_pop_front(struct dri_drawable *draw)
324 {
325 struct pipe_screen *screen = draw->screen->base.screen;
326 struct pipe_fence_handle *fence = NULL;
327
328 if (draw->desired_fences == 0)
329 return NULL;
330
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;
335 --draw->cur_fences;
336 }
337 return fence;
338 }
339
340
341 /**
342 * swap_fences_push_back - push a fence onto the throttle queue
343 *
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.
346 */
347 static void
348 swap_fences_push_back(struct dri_drawable *draw,
349 struct pipe_fence_handle *fence)
350 {
351 struct pipe_screen *screen = draw->screen->base.screen;
352
353 if (!fence || draw->desired_fences == 0)
354 return;
355
356 while(draw->cur_fences == draw->desired_fences)
357 swap_fences_pop_front(draw);
358
359 draw->cur_fences++;
360 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
361 fence);
362 draw->head &= DRI_SWAP_FENCES_MASK;
363 }
364
365
366 /**
367 * swap_fences_unref - empty the throttle queue
368 *
369 * pulls fences of the throttle queue until it is empty.
370 */
371 static void
372 swap_fences_unref(struct dri_drawable *draw)
373 {
374 struct pipe_screen *screen = draw->screen->base.screen;
375
376 while(draw->cur_fences) {
377 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
378 draw->tail &= DRI_SWAP_FENCES_MASK;
379 --draw->cur_fences;
380 }
381 }
382
383 void
384 dri_pipe_blit(struct pipe_context *pipe,
385 struct pipe_resource *dst,
386 struct pipe_resource *src)
387 {
388 struct pipe_blit_info blit;
389
390 if (!dst || !src)
391 return;
392
393 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
394 * Fragment Operations):
395 *
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).
408 *
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.
412 */
413
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;
427
428 pipe->blit(pipe, &blit);
429 }
430
431 static void
432 dri_postprocessing(struct dri_context *ctx,
433 struct dri_drawable *drawable,
434 enum st_attachment_type att)
435 {
436 struct pipe_resource *src = drawable->textures[att];
437 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
438
439 if (ctx->pp && src)
440 pp_run(ctx->pp, src, src, zsbuf);
441 }
442
443 /**
444 * DRI2 flush extension, the flush_with_flags function.
445 *
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
450 */
451 void
452 dri_flush(__DRIcontext *cPriv,
453 __DRIdrawable *dPriv,
454 unsigned flags,
455 enum __DRI2throttleReason reason)
456 {
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;
462
463 if (!ctx) {
464 assert(0);
465 return;
466 }
467
468 st = ctx->st;
469 if (st->thread_finish)
470 st->thread_finish(st);
471
472 if (drawable) {
473 /* prevent recursion */
474 if (drawable->flushing)
475 return;
476
477 drawable->flushing = TRUE;
478 }
479 else {
480 flags &= ~__DRI2_FLUSH_DRAWABLE;
481 }
482
483 /* Flush the drawable. */
484 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
485 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
486 struct pipe_context *pipe = st->pipe;
487
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]);
494
495 if (drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] &&
496 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]) {
497 swap_msaa_buffers = TRUE;
498 }
499
500 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
501 }
502
503 dri_postprocessing(ctx, drawable, ST_ATTACHMENT_BACK_LEFT);
504
505 if (ctx->hud) {
506 hud_draw(ctx->hud, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
507 }
508
509 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
510
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]);
517 }
518 }
519
520 flush_flags = 0;
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;
525
526 /* Flush the context and throttle if needed. */
527 if (dri_screen(ctx->sPriv)->throttling_enabled &&
528 drawable &&
529 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
530 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
531 /* Throttle.
532 *
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
535 * number.
536 *
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.
540 */
541 struct pipe_screen *screen = drawable->screen->base.screen;
542 struct pipe_fence_handle *fence;
543
544 fence = swap_fences_pop_front(drawable);
545 if (fence) {
546 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
547 screen->fence_reference(screen, &fence, NULL);
548 }
549
550 st->flush(st, flush_flags, &fence);
551
552 if (fence) {
553 swap_fences_push_back(drawable, fence);
554 screen->fence_reference(screen, &fence, NULL);
555 }
556 }
557 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
558 st->flush(st, flush_flags, NULL);
559 }
560
561 if (drawable) {
562 drawable->flushing = FALSE;
563 }
564
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.
568 */
569 if (swap_msaa_buffers) {
570 struct pipe_resource *tmp =
571 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
572
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;
576
577 /* Now that we have swapped the buffers, this tells the state
578 * tracker to revalidate the framebuffer.
579 */
580 p_atomic_inc(&drawable->base.stamp);
581 }
582 }
583
584 /**
585 * dri_throttle - A DRI2ThrottleExtension throttling function.
586 */
587 static void
588 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
589 enum __DRI2throttleReason reason)
590 {
591 dri_flush(cPriv, dPriv, 0, reason);
592 }
593
594
595 const __DRI2throttleExtension dri2ThrottleExtension = {
596 .base = { __DRI2_THROTTLE, 1 },
597
598 .throttle = dri_throttle,
599 };
600
601
602 /* vim: set sw=3 ts=8 sts=3 expandtab: */