st/mesa: add destroy_drawable interface
[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
162 return GL_TRUE;
163 fail:
164 FREE(drawable);
165 return GL_FALSE;
166 }
167
168 void
169 dri_destroy_buffer(__DRIdrawable * dPriv)
170 {
171 struct dri_drawable *drawable = dri_drawable(dPriv);
172 struct dri_screen *screen = drawable->screen;
173 struct st_api *stapi = screen->st_api;
174 int i;
175
176 pipe_surface_reference(&drawable->drisw_surface, NULL);
177
178 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
179 pipe_resource_reference(&drawable->textures[i], NULL);
180 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
181 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
182
183 swap_fences_unref(drawable);
184
185 /* Notify the st manager that this drawable is no longer valid */
186 stapi->destroy_drawable(stapi, &drawable->base);
187
188 FREE(drawable);
189 }
190
191 /**
192 * Validate the texture at an attachment. Allocate the texture if it does not
193 * exist. Used by the TFP extension.
194 */
195 static void
196 dri_drawable_validate_att(struct dri_context *ctx,
197 struct dri_drawable *drawable,
198 enum st_attachment_type statt)
199 {
200 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
201 unsigned i, count = 0;
202
203 /* check if buffer already exists */
204 if (drawable->texture_mask & (1 << statt))
205 return;
206
207 /* make sure DRI2 does not destroy existing buffers */
208 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
209 if (drawable->texture_mask & (1 << i)) {
210 statts[count++] = i;
211 }
212 }
213 statts[count++] = statt;
214
215 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
216
217 drawable->base.validate(ctx->st, &drawable->base, statts, count, NULL);
218 }
219
220 /**
221 * These are used for GLX_EXT_texture_from_pixmap
222 */
223 static void
224 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
225 GLint format, __DRIdrawable *dPriv)
226 {
227 struct dri_context *ctx = dri_context(pDRICtx);
228 struct st_context_iface *st = ctx->st;
229 struct dri_drawable *drawable = dri_drawable(dPriv);
230 struct pipe_resource *pt;
231
232 if (st->thread_finish)
233 st->thread_finish(st);
234
235 dri_drawable_validate_att(ctx, drawable, ST_ATTACHMENT_FRONT_LEFT);
236
237 /* Use the pipe resource associated with the X drawable */
238 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
239
240 if (pt) {
241 enum pipe_format internal_format = pt->format;
242
243 if (format == __DRI_TEXTURE_FORMAT_RGB) {
244 /* only need to cover the formats recognized by dri_fill_st_visual */
245 switch (internal_format) {
246 case PIPE_FORMAT_BGRA8888_UNORM:
247 internal_format = PIPE_FORMAT_BGRX8888_UNORM;
248 break;
249 case PIPE_FORMAT_ARGB8888_UNORM:
250 internal_format = PIPE_FORMAT_XRGB8888_UNORM;
251 break;
252 default:
253 break;
254 }
255 }
256
257 drawable->update_tex_buffer(drawable, ctx, pt);
258
259 ctx->st->teximage(ctx->st,
260 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
261 0, internal_format, pt, FALSE);
262 }
263 }
264
265 static void
266 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
267 __DRIdrawable *dPriv)
268 {
269 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
270 }
271
272 const __DRItexBufferExtension driTexBufferExtension = {
273 .base = { __DRI_TEX_BUFFER, 2 },
274
275 .setTexBuffer = dri_set_tex_buffer,
276 .setTexBuffer2 = dri_set_tex_buffer2,
277 .releaseTexBuffer = NULL,
278 };
279
280 /**
281 * Get the format and binding of an attachment.
282 */
283 void
284 dri_drawable_get_format(struct dri_drawable *drawable,
285 enum st_attachment_type statt,
286 enum pipe_format *format,
287 unsigned *bind)
288 {
289 switch (statt) {
290 case ST_ATTACHMENT_FRONT_LEFT:
291 case ST_ATTACHMENT_BACK_LEFT:
292 case ST_ATTACHMENT_FRONT_RIGHT:
293 case ST_ATTACHMENT_BACK_RIGHT:
294 /* Other pieces of the driver stack get confused and behave incorrectly
295 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
296 * though other means and handles it correctly, so we don't really need
297 * to use an sRGB format here.
298 */
299 *format = util_format_linear(drawable->stvis.color_format);
300 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
301 break;
302 case ST_ATTACHMENT_DEPTH_STENCIL:
303 *format = drawable->stvis.depth_stencil_format;
304 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
305 break;
306 default:
307 *format = PIPE_FORMAT_NONE;
308 *bind = 0;
309 break;
310 }
311 }
312
313
314 /**
315 * swap_fences_pop_front - pull a fence from the throttle queue
316 *
317 * If the throttle queue is filled to the desired number of fences,
318 * pull fences off the queue until the number is less than the desired
319 * number of fences, and return the last fence pulled.
320 */
321 static struct pipe_fence_handle *
322 swap_fences_pop_front(struct dri_drawable *draw)
323 {
324 struct pipe_screen *screen = draw->screen->base.screen;
325 struct pipe_fence_handle *fence = NULL;
326
327 if (draw->desired_fences == 0)
328 return NULL;
329
330 if (draw->cur_fences >= draw->desired_fences) {
331 screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
332 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
333 draw->tail &= DRI_SWAP_FENCES_MASK;
334 --draw->cur_fences;
335 }
336 return fence;
337 }
338
339
340 /**
341 * swap_fences_push_back - push a fence onto the throttle queue
342 *
343 * push a fence onto the throttle queue and pull fences of the queue
344 * so that the desired number of fences are on the queue.
345 */
346 static void
347 swap_fences_push_back(struct dri_drawable *draw,
348 struct pipe_fence_handle *fence)
349 {
350 struct pipe_screen *screen = draw->screen->base.screen;
351
352 if (!fence || draw->desired_fences == 0)
353 return;
354
355 while(draw->cur_fences == draw->desired_fences)
356 swap_fences_pop_front(draw);
357
358 draw->cur_fences++;
359 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
360 fence);
361 draw->head &= DRI_SWAP_FENCES_MASK;
362 }
363
364
365 /**
366 * swap_fences_unref - empty the throttle queue
367 *
368 * pulls fences of the throttle queue until it is empty.
369 */
370 static void
371 swap_fences_unref(struct dri_drawable *draw)
372 {
373 struct pipe_screen *screen = draw->screen->base.screen;
374
375 while(draw->cur_fences) {
376 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
377 draw->tail &= DRI_SWAP_FENCES_MASK;
378 --draw->cur_fences;
379 }
380 }
381
382 void
383 dri_pipe_blit(struct pipe_context *pipe,
384 struct pipe_resource *dst,
385 struct pipe_resource *src)
386 {
387 struct pipe_blit_info blit;
388
389 if (!dst || !src)
390 return;
391
392 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
393 * Fragment Operations):
394 *
395 * If a framebuffer object is not bound, after all operations have
396 * been completed on the multisample buffer, the sample values for
397 * each color in the multisample buffer are combined to produce a
398 * single color value, and that value is written into the
399 * corresponding color buffers selected by DrawBuffer or
400 * DrawBuffers. An implementation may defer the writing of the color
401 * buffers until a later time, but the state of the framebuffer must
402 * behave as if the color buffers were updated as each fragment was
403 * processed. The method of combination is not specified. If the
404 * framebuffer contains sRGB values, then it is recommended that the
405 * an average of sample values is computed in a linearized space, as
406 * for blending (see section 4.1.7).
407 *
408 * In other words, to do a resolve operation in a linear space, we have
409 * to set sRGB formats if the original resources were sRGB, so don't use
410 * util_format_linear.
411 */
412
413 memset(&blit, 0, sizeof(blit));
414 blit.dst.resource = dst;
415 blit.dst.box.width = dst->width0;
416 blit.dst.box.height = dst->height0;
417 blit.dst.box.depth = 1;
418 blit.dst.format = dst->format;
419 blit.src.resource = src;
420 blit.src.box.width = src->width0;
421 blit.src.box.height = src->height0;
422 blit.src.box.depth = 1;
423 blit.src.format = src->format;
424 blit.mask = PIPE_MASK_RGBA;
425 blit.filter = PIPE_TEX_FILTER_NEAREST;
426
427 pipe->blit(pipe, &blit);
428 }
429
430 static void
431 dri_postprocessing(struct dri_context *ctx,
432 struct dri_drawable *drawable,
433 enum st_attachment_type att)
434 {
435 struct pipe_resource *src = drawable->textures[att];
436 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
437
438 if (ctx->pp && src)
439 pp_run(ctx->pp, src, src, zsbuf);
440 }
441
442 /**
443 * DRI2 flush extension, the flush_with_flags function.
444 *
445 * \param context the context
446 * \param drawable the drawable to flush
447 * \param flags a combination of _DRI2_FLUSH_xxx flags
448 * \param throttle_reason the reason for throttling, 0 = no throttling
449 */
450 void
451 dri_flush(__DRIcontext *cPriv,
452 __DRIdrawable *dPriv,
453 unsigned flags,
454 enum __DRI2throttleReason reason)
455 {
456 struct dri_context *ctx = dri_context(cPriv);
457 struct dri_drawable *drawable = dri_drawable(dPriv);
458 struct st_context_iface *st;
459 unsigned flush_flags;
460 boolean swap_msaa_buffers = FALSE;
461
462 if (!ctx) {
463 assert(0);
464 return;
465 }
466
467 st = ctx->st;
468 if (st->thread_finish)
469 st->thread_finish(st);
470
471 if (drawable) {
472 /* prevent recursion */
473 if (drawable->flushing)
474 return;
475
476 drawable->flushing = TRUE;
477 }
478 else {
479 flags &= ~__DRI2_FLUSH_DRAWABLE;
480 }
481
482 /* Flush the drawable. */
483 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
484 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
485 struct pipe_context *pipe = st->pipe;
486
487 if (drawable->stvis.samples > 1 &&
488 reason == __DRI2_THROTTLE_SWAPBUFFER) {
489 /* Resolve the MSAA back buffer. */
490 dri_pipe_blit(st->pipe,
491 drawable->textures[ST_ATTACHMENT_BACK_LEFT],
492 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
493
494 if (drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] &&
495 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]) {
496 swap_msaa_buffers = TRUE;
497 }
498
499 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
500 }
501
502 dri_postprocessing(ctx, drawable, ST_ATTACHMENT_BACK_LEFT);
503
504 if (ctx->hud) {
505 hud_draw(ctx->hud, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
506 }
507
508 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
509
510 if (pipe->invalidate_resource &&
511 (flags & __DRI2_FLUSH_INVALIDATE_ANCILLARY)) {
512 if (drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
513 pipe->invalidate_resource(pipe, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
514 if (drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL])
515 pipe->invalidate_resource(pipe, drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL]);
516 }
517 }
518
519 flush_flags = 0;
520 if (flags & __DRI2_FLUSH_CONTEXT)
521 flush_flags |= ST_FLUSH_FRONT;
522 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
523 flush_flags |= ST_FLUSH_END_OF_FRAME;
524
525 /* Flush the context and throttle if needed. */
526 if (dri_screen(ctx->sPriv)->throttling_enabled &&
527 drawable &&
528 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
529 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
530 /* Throttle.
531 *
532 * This pulls a fence off the throttling queue and waits for it if the
533 * number of fences on the throttling queue has reached the desired
534 * number.
535 *
536 * Then flushes to insert a fence at the current rendering position, and
537 * pushes that fence on the queue. This requires that the st_context_iface
538 * flush method returns a fence even if there are no commands to flush.
539 */
540 struct pipe_screen *screen = drawable->screen->base.screen;
541 struct pipe_fence_handle *fence;
542
543 fence = swap_fences_pop_front(drawable);
544 if (fence) {
545 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
546 screen->fence_reference(screen, &fence, NULL);
547 }
548
549 st->flush(st, flush_flags, &fence);
550
551 if (fence) {
552 swap_fences_push_back(drawable, fence);
553 screen->fence_reference(screen, &fence, NULL);
554 }
555 }
556 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
557 st->flush(st, flush_flags, NULL);
558 }
559
560 if (drawable) {
561 drawable->flushing = FALSE;
562 }
563
564 /* Swap the MSAA front and back buffers, so that reading
565 * from the front buffer after SwapBuffers returns what was
566 * in the back buffer.
567 */
568 if (swap_msaa_buffers) {
569 struct pipe_resource *tmp =
570 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
571
572 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] =
573 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
574 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT] = tmp;
575
576 /* Now that we have swapped the buffers, this tells the state
577 * tracker to revalidate the framebuffer.
578 */
579 p_atomic_inc(&drawable->base.stamp);
580 }
581 }
582
583 /**
584 * dri_throttle - A DRI2ThrottleExtension throttling function.
585 */
586 static void
587 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
588 enum __DRI2throttleReason reason)
589 {
590 dri_flush(cPriv, dPriv, 0, reason);
591 }
592
593
594 const __DRI2throttleExtension dri2ThrottleExtension = {
595 .base = { __DRI2_THROTTLE, 1 },
596
597 .throttle = dri_throttle,
598 };
599
600
601 /* vim: set sw=3 ts=8 sts=3 expandtab: */