58008b3c58477773314fc72d43055c4eae5c9b26
[platform/upstream/mesa.git] / src / gallium / frontends / 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/format/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40
41 #include "state_tracker/st_context.h"
42
43 static uint32_t drifb_ID = 0;
44
45 static bool
46 dri_st_framebuffer_validate(struct st_context *st,
47                             struct pipe_frontend_drawable *pdrawable,
48                             const enum st_attachment_type *statts,
49                             unsigned count,
50                             struct pipe_resource **out,
51                             struct pipe_resource **resolve)
52 {
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;
57    bool new_stamp;
58    int i;
59    unsigned int lastStamp;
60    struct pipe_resource **textures =
61       drawable->stvis.samples > 1 ? drawable->msaa_textures
62                                   : drawable->textures;
63
64    statt_mask = 0x0;
65    for (i = 0; i < count; i++)
66       statt_mask |= (1 << statts[i]);
67
68    /* record newly allocated textures */
69    new_mask = (statt_mask & ~drawable->texture_mask);
70
71    do {
72       lastStamp = drawable->lastStamp;
73       new_stamp = (drawable->texture_stamp != lastStamp);
74
75       if (new_stamp || new_mask) {
76          if (new_stamp && drawable->update_drawable_info)
77             drawable->update_drawable_info(drawable);
78
79          drawable->allocate_textures(ctx, drawable, statts, count);
80
81          /* add existing textures */
82          for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
83             if (textures[i])
84                statt_mask |= (1 << i);
85          }
86
87          drawable->texture_stamp = lastStamp;
88          drawable->texture_mask = statt_mask;
89       }
90    } while (lastStamp != drawable->lastStamp);
91
92    /* Flush the pending set_damage_region request. */
93    struct pipe_screen *pscreen = screen->base.screen;
94
95    if (new_mask & (1 << ST_ATTACHMENT_BACK_LEFT) &&
96        pscreen->set_damage_region) {
97       struct pipe_resource *resource = textures[ST_ATTACHMENT_BACK_LEFT];
98
99       pscreen->set_damage_region(pscreen, resource,
100                                  drawable->num_damage_rects,
101                                  drawable->damage_rects);
102    }
103
104    if (!out)
105       return true;
106
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]);
115    }
116
117    return true;
118 }
119
120 static bool
121 dri_st_framebuffer_flush_front(struct st_context *st,
122                                struct pipe_frontend_drawable *pdrawable,
123                                enum st_attachment_type statt)
124 {
125    struct dri_context *ctx = (struct dri_context *)st->frontend_context;
126    struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
127
128    /* XXX remove this and just set the correct one on the framebuffer */
129    return drawable->flush_frontbuffer(ctx, drawable, statt);
130 }
131
132 /**
133  * The gallium frontend framebuffer interface flush_swapbuffers callback
134  */
135 static bool
136 dri_st_framebuffer_flush_swapbuffers(struct st_context *st,
137                                      struct pipe_frontend_drawable *pdrawable)
138 {
139    struct dri_context *ctx = (struct dri_context *)st->frontend_context;
140    struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
141
142    if (drawable->flush_swapbuffers)
143       drawable->flush_swapbuffers(ctx, drawable);
144
145    return true;
146 }
147
148 /**
149  * This is called when we need to set up GL rendering to a new X window.
150  */
151 struct dri_drawable *
152 dri_create_drawable(struct dri_screen *screen, const struct gl_config *visual,
153                     bool isPixmap, void *loaderPrivate)
154 {
155    struct dri_drawable *drawable = NULL;
156
157    if (isPixmap)
158       goto fail;                       /* not implemented */
159
160    drawable = CALLOC_STRUCT(dri_drawable);
161    if (drawable == NULL)
162       goto fail;
163
164    drawable->loaderPrivate = loaderPrivate;
165    drawable->refcount = 1;
166    drawable->lastStamp = 0;
167    drawable->w = 0;
168    drawable->h = 0;
169
170    dri_fill_st_visual(&drawable->stvis, screen, visual);
171
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;
177
178    drawable->screen = screen;
179
180    p_atomic_set(&drawable->base.stamp, 1);
181    drawable->base.ID = p_atomic_inc_return(&drifb_ID);
182    drawable->base.fscreen = &screen->base;
183
184    return drawable;
185 fail:
186    FREE(drawable);
187    return NULL;
188 }
189
190 static void
191 dri_destroy_drawable(struct dri_drawable *drawable)
192 {
193    struct dri_screen *screen = drawable->screen;
194    int i;
195
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);
200
201    screen->base.screen->fence_reference(screen->base.screen,
202          &drawable->throttle_fence, NULL);
203
204    /* Notify the st manager that this drawable is no longer valid */
205    st_api_destroy_drawable(&drawable->base);
206
207    FREE(drawable->damage_rects);
208    FREE(drawable);
209 }
210
211 void
212 dri_put_drawable(struct dri_drawable *drawable)
213 {
214    if (drawable) {
215       int refcount = --drawable->refcount;
216       assert(refcount >= 0);
217
218       if (!refcount)
219          dri_destroy_drawable(drawable);
220    }
221 }
222
223 /**
224  * Validate the texture at an attachment.  Allocate the texture if it does not
225  * exist.  Used by the TFP extension.
226  */
227 static void
228 dri_drawable_validate_att(struct dri_context *ctx,
229                           struct dri_drawable *drawable,
230                           enum st_attachment_type statt)
231 {
232    enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
233    unsigned i, count = 0;
234
235    /* check if buffer already exists */
236    if (drawable->texture_mask & (1 << statt))
237       return;
238
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)) {
242          statts[count++] = i;
243       }
244    }
245    statts[count++] = statt;
246
247    drawable->texture_stamp = drawable->lastStamp - 1;
248
249    drawable->base.validate(ctx->st, &drawable->base, statts, count, NULL, NULL);
250 }
251
252 /**
253  * These are used for GLX_EXT_texture_from_pixmap
254  */
255 static void
256 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
257                     GLint format, __DRIdrawable *dPriv)
258 {
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;
263
264    _mesa_glthread_finish(st->ctx);
265
266    dri_drawable_validate_att(ctx, drawable, ST_ATTACHMENT_FRONT_LEFT);
267
268    /* Use the pipe resource associated with the X drawable */
269    pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
270
271    if (pt) {
272       enum pipe_format internal_format = pt->format;
273
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;
279             break;
280          case PIPE_FORMAT_B10G10R10A2_UNORM:
281             internal_format = PIPE_FORMAT_B10G10R10X2_UNORM;
282             break;
283          case PIPE_FORMAT_R10G10B10A2_UNORM:
284             internal_format = PIPE_FORMAT_R10G10B10X2_UNORM;
285             break;
286          case PIPE_FORMAT_BGRA8888_UNORM:
287             internal_format = PIPE_FORMAT_BGRX8888_UNORM;
288             break;
289          case PIPE_FORMAT_ARGB8888_UNORM:
290             internal_format = PIPE_FORMAT_XRGB8888_UNORM;
291             break;
292          default:
293             break;
294          }
295       }
296
297       drawable->update_tex_buffer(drawable, ctx, pt);
298
299       st_context_teximage(ctx->st, target, 0, internal_format, pt, false);
300    }
301 }
302
303 static void
304 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
305                    __DRIdrawable *dPriv)
306 {
307    dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
308 }
309
310 const __DRItexBufferExtension driTexBufferExtension = {
311    .base = { __DRI_TEX_BUFFER, 2 },
312
313    .setTexBuffer       = dri_set_tex_buffer,
314    .setTexBuffer2      = dri_set_tex_buffer2,
315    .releaseTexBuffer   = NULL,
316 };
317
318 /**
319  * Get the format and binding of an attachment.
320  */
321 void
322 dri_drawable_get_format(struct dri_drawable *drawable,
323                         enum st_attachment_type statt,
324                         enum pipe_format *format,
325                         unsigned *bind)
326 {
327    switch (statt) {
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.
336        */
337       *format = util_format_linear(drawable->stvis.color_format);
338       *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
339       break;
340    case ST_ATTACHMENT_DEPTH_STENCIL:
341       *format = drawable->stvis.depth_stencil_format;
342       *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
343       break;
344    default:
345       *format = PIPE_FORMAT_NONE;
346       *bind = 0;
347       break;
348    }
349 }
350
351 void
352 dri_pipe_blit(struct pipe_context *pipe,
353               struct pipe_resource *dst,
354               struct pipe_resource *src)
355 {
356    struct pipe_blit_info blit;
357
358    if (!dst || !src)
359       return;
360
361    /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
362     *  Fragment Operations):
363     *
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).
376     *
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.
380     */
381
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;
395
396    pipe->blit(pipe, &blit);
397 }
398
399 static void
400 dri_postprocessing(struct dri_context *ctx,
401                    struct dri_drawable *drawable,
402                    enum st_attachment_type att)
403 {
404    struct pipe_resource *src = drawable->textures[att];
405    struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
406
407    if (ctx->pp && src)
408       pp_run(ctx->pp, src, src, zsbuf);
409 }
410
411 struct notify_before_flush_cb_args {
412    struct dri_context *ctx;
413    struct dri_drawable *drawable;
414    unsigned flags;
415    enum __DRI2throttleReason reason;
416    bool swap_msaa_buffers;
417 };
418
419 static void
420 notify_before_flush_cb(void* _args)
421 {
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;
425
426    /* Wait for glthread to finish because we can't use pipe_context from
427     * multiple threads.
428     */
429    _mesa_glthread_finish(st->ctx);
430
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]);
439
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;
445       }
446
447       /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
448    }
449
450    dri_postprocessing(args->ctx, args->drawable, ST_ATTACHMENT_BACK_LEFT);
451
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]);
458    }
459
460    if (args->ctx->hud) {
461       hud_run(args->ctx->hud, args->ctx->st->cso_context,
462               args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
463    }
464
465    pipe->flush_resource(pipe, args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
466 }
467
468 /**
469  * DRI2 flush extension, the flush_with_flags function.
470  *
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
475  */
476 void
477 dri_flush(__DRIcontext *cPriv,
478           __DRIdrawable *dPriv,
479           unsigned flags,
480           enum __DRI2throttleReason reason)
481 {
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 };
487
488    if (!ctx) {
489       assert(0);
490       return;
491    }
492
493    st = ctx->st;
494    _mesa_glthread_finish(st->ctx);
495
496    if (drawable) {
497       /* prevent recursion */
498       if (drawable->flushing)
499          return;
500
501       drawable->flushing = true;
502    }
503    else {
504       flags &= ~__DRI2_FLUSH_DRAWABLE;
505    }
506
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.
514        */
515       args.ctx = ctx;
516       args.drawable = drawable;
517       args.flags = flags;
518       args.reason = reason;
519    }
520
521    flush_flags = 0;
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;
527
528    /* Flush the context and throttle if needed. */
529    if (ctx->screen->throttle &&
530        drawable &&
531        (reason == __DRI2_THROTTLE_SWAPBUFFER ||
532         reason == __DRI2_THROTTLE_FLUSHFRONT)) {
533
534       struct pipe_screen *screen = drawable->screen->base.screen;
535       struct pipe_fence_handle *new_fence = NULL;
536
537       st_context_flush(st, flush_flags, &new_fence, args.ctx ? notify_before_flush_cb : NULL, &args);
538
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);
543       }
544       drawable->throttle_fence = new_fence;
545    }
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);
548    }
549
550    if (drawable) {
551       drawable->flushing = false;
552    }
553
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.
557     */
558    if (args.swap_msaa_buffers) {
559       struct pipe_resource *tmp =
560          drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
561
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;
565
566       /* Now that we have swapped the buffers, this tells the gallium
567        * frontend to revalidate the framebuffer.
568        */
569       p_atomic_inc(&drawable->base.stamp);
570    }
571
572    st_context_invalidate_state(st, ST_INVALIDATE_FB_STATE);
573 }
574
575 /**
576  * DRI2 flush extension.
577  */
578 void
579 dri_flush_drawable(__DRIdrawable *dPriv)
580 {
581    struct dri_context *ctx = dri_get_current();
582
583    if (ctx)
584       dri_flush(opaque_dri_context(ctx), dPriv, __DRI2_FLUSH_DRAWABLE, -1);
585 }
586
587 /**
588  * dri_throttle - A DRI2ThrottleExtension throttling function.
589  */
590 static void
591 dri_throttle(__DRIcontext *cPriv, __DRIdrawable *dPriv,
592              enum __DRI2throttleReason reason)
593 {
594    dri_flush(cPriv, dPriv, 0, reason);
595 }
596
597
598 const __DRI2throttleExtension dri2ThrottleExtension = {
599     .base = { __DRI2_THROTTLE, 1 },
600
601     .throttle          = dri_throttle,
602 };
603
604
605 /* vim: set sw=3 ts=8 sts=3 expandtab: */