f784ee65ea9fcaf46caca43670ab629f7007fff7
[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_msaa_resolve(struct dri_context *ctx,
363 struct dri_drawable *drawable,
364 enum st_attachment_type att)
365 {
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;
370
371 if (!dst || !src)
372 return;
373
374 memset(&blit, 0, sizeof(blit));
375 blit.dst.resource = dst;
376 blit.dst.box.width = dst->width0;
377 blit.dst.box.height = dst->height0;
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->height0;
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;
387
388 pipe->blit(pipe, &blit);
389 }
390
391 static void
392 dri_postprocessing(struct dri_context *ctx,
393 struct dri_drawable *drawable,
394 enum st_attachment_type att)
395 {
396 struct pipe_resource *src = drawable->textures[att];
397 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
398
399 if (ctx->pp && src && zsbuf)
400 pp_run(ctx->pp, src, src, zsbuf);
401 }
402
403 /**
404 * DRI2 flush extension, the flush_with_flags function.
405 *
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
410 */
411 void
412 dri_flush(__DRIcontext *cPriv,
413 __DRIdrawable *dPriv,
414 unsigned flags,
415 enum __DRI2throttleReason reason)
416 {
417 struct dri_context *ctx = dri_context(cPriv);
418 struct dri_drawable *drawable = dri_drawable(dPriv);
419 unsigned flush_flags;
420
421 if (!ctx) {
422 assert(0);
423 return;
424 }
425
426 if (drawable) {
427 /* prevent recursion */
428 if (drawable->flushing)
429 return;
430
431 drawable->flushing = TRUE;
432 }
433 else {
434 flags &= ~__DRI2_FLUSH_DRAWABLE;
435 }
436
437 /* Flush the drawable. */
438 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
439 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
440 /* Resolve MSAA buffers. */
441 if (drawable->stvis.samples > 1) {
442 dri_msaa_resolve(ctx, drawable, ST_ATTACHMENT_BACK_LEFT);
443 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
444 }
445
446 dri_postprocessing(ctx, drawable, ST_ATTACHMENT_BACK_LEFT);
447
448 if (ctx->hud) {
449 hud_draw(ctx->hud, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
450 }
451 }
452
453 flush_flags = 0;
454 if (flags & __DRI2_FLUSH_CONTEXT)
455 flush_flags |= ST_FLUSH_FRONT;
456 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
457 flush_flags |= ST_FLUSH_END_OF_FRAME;
458
459 /* Flush the context and throttle if needed. */
460 if (dri_screen(ctx->sPriv)->throttling_enabled &&
461 drawable &&
462 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
463 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
464 /* Throttle.
465 *
466 * This pulls a fence off the throttling queue and waits for it if the
467 * number of fences on the throttling queue has reached the desired
468 * number.
469 *
470 * Then flushes to insert a fence at the current rendering position, and
471 * pushes that fence on the queue. This requires that the st_context_iface
472 * flush method returns a fence even if there are no commands to flush.
473 */
474 struct pipe_screen *screen = drawable->screen->base.screen;
475 struct pipe_fence_handle *fence;
476
477 fence = swap_fences_pop_front(drawable);
478 if (fence) {
479 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
480 screen->fence_reference(screen, &fence, NULL);
481 }
482
483 ctx->st->flush(ctx->st, flush_flags, &fence);
484
485 if (fence) {
486 swap_fences_push_back(drawable, fence);
487 screen->fence_reference(screen, &fence, NULL);
488 }
489 }
490 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
491 ctx->st->flush(ctx->st, flush_flags, NULL);
492 }
493
494 if (drawable) {
495 drawable->flushing = FALSE;
496 }
497 }
498
499 /**
500 * dri_throttle - A DRI2ThrottleExtension throttling function.
501 */
502 static void
503 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
504 enum __DRI2throttleReason reason)
505 {
506 dri_flush(cPriv, dPriv, 0, reason);
507 }
508
509
510 const __DRI2throttleExtension dri2ThrottleExtension = {
511 .base = { __DRI2_THROTTLE, __DRI2_THROTTLE_VERSION },
512 .throttle = dri_throttle,
513 };
514
515
516 /* vim: set sw=3 ts=8 sts=3 expandtab: */