OSDN Git Service

st/dri: Fix some warnings
[android-x86/external-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_context.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_inlines.h"
39 #include "main/mtypes.h"
40 #include "main/renderbuffer.h"
41 #include "state_tracker/drm_api.h"
42 #include "state_tracker/dri1_api.h"
43 #include "state_tracker/st_public.h"
44 #include "state_tracker/st_context.h"
45 #include "state_tracker/st_cb_fbo.h"
46
47 #include "util/u_memory.h"
48
49 static struct pipe_surface *
50 dri_surface_from_handle(struct drm_api *api,
51                         struct pipe_screen *screen,
52                         unsigned handle,
53                         enum pipe_format format,
54                         unsigned width, unsigned height, unsigned pitch)
55 {
56    struct pipe_surface *surface = NULL;
57    struct pipe_texture *texture = NULL;
58    struct pipe_texture templat;
59    struct pipe_buffer *buf = NULL;
60
61    buf = api->buffer_from_handle(api, screen, "dri2 buffer", handle);
62    if (!buf) {
63       debug_printf("%s: Failed to get buffer from handle\n", __func__);
64       return NULL;
65    }
66
67    memset(&templat, 0, sizeof(templat));
68    templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
69    templat.target = PIPE_TEXTURE_2D;
70    templat.last_level = 0;
71    templat.depth[0] = 1;
72    templat.format = format;
73    templat.width[0] = width;
74    templat.height[0] = height;
75    pf_get_block(templat.format, &templat.block);
76
77    texture = screen->texture_blanket(screen, &templat, &pitch, buf);
78
79    /* we don't need the buffer from this point on */
80    pipe_buffer_reference(&buf, NULL);
81
82    if (!texture) {
83       debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__);
84       return NULL;
85    }
86
87    surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
88                                      PIPE_BUFFER_USAGE_GPU_READ |
89                                      PIPE_BUFFER_USAGE_GPU_WRITE);
90
91    /* we don't need the texture from this point on */
92    pipe_texture_reference(&texture, NULL);
93    return surface;
94 }
95
96 /**
97  * Pixmaps have will have the same name of fake front and front.
98  */
99 static boolean
100 dri2_check_if_pixmap(__DRIbuffer *buffers, int count)
101 {
102    boolean found = FALSE;
103    boolean is_pixmap = FALSE;
104    unsigned name;
105    int i;
106
107    for (i = 0; i < count; i++) {
108       switch (buffers[i].attachment) {
109       case __DRI_BUFFER_FRONT_LEFT:
110       case __DRI_BUFFER_FAKE_FRONT_LEFT:
111          if (found) {
112             is_pixmap = buffers[i].name == name;
113          } else {
114             name = buffers[i].name;
115             found = TRUE;
116          }
117       default:
118          continue;
119       }
120    }
121
122    return is_pixmap;
123 }
124
125 /**
126  * This will be called a drawable is known to have been resized.
127  */
128 void
129 dri_get_buffers(__DRIdrawablePrivate * dPriv)
130 {
131
132    struct dri_drawable *drawable = dri_drawable(dPriv);
133    struct pipe_surface *surface = NULL;
134    struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
135    __DRIbuffer *buffers = NULL;
136    __DRIscreen *dri_screen = drawable->sPriv;
137    __DRIdrawable *dri_drawable = drawable->dPriv;
138    struct drm_api *api = ((struct dri_screen*)(dri_screen->private))->api;
139    boolean have_depth = FALSE;
140    int i, count;
141
142    buffers = (*dri_screen->dri2.loader->getBuffers) (dri_drawable,
143                                                      &dri_drawable->w,
144                                                      &dri_drawable->h,
145                                                      drawable->attachments,
146                                                      drawable->
147                                                      num_attachments, &count,
148                                                      dri_drawable->
149                                                      loaderPrivate);
150
151    if (buffers == NULL) {
152       return;
153    }
154
155    /* set one cliprect to cover the whole dri_drawable */
156    dri_drawable->x = 0;
157    dri_drawable->y = 0;
158    dri_drawable->backX = 0;
159    dri_drawable->backY = 0;
160    dri_drawable->numClipRects = 1;
161    dri_drawable->pClipRects[0].x1 = 0;
162    dri_drawable->pClipRects[0].y1 = 0;
163    dri_drawable->pClipRects[0].x2 = dri_drawable->w;
164    dri_drawable->pClipRects[0].y2 = dri_drawable->h;
165    dri_drawable->numBackClipRects = 1;
166    dri_drawable->pBackClipRects[0].x1 = 0;
167    dri_drawable->pBackClipRects[0].y1 = 0;
168    dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
169    dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
170
171    if (drawable->old_num == count &&
172        drawable->old_w == dri_drawable->w &&
173        drawable->old_h == dri_drawable->h &&
174        memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0) {
175        return;
176    } else {
177       drawable->old_num = count;
178       drawable->old_w = dri_drawable->w;
179       drawable->old_h = dri_drawable->h;
180       memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
181    }
182
183    drawable->is_pixmap = dri2_check_if_pixmap(buffers, count);
184
185    for (i = 0; i < count; i++) {
186       enum pipe_format format = 0;
187       int index = 0;
188
189       switch (buffers[i].attachment) {
190       case __DRI_BUFFER_FRONT_LEFT:
191          continue;
192       case __DRI_BUFFER_FAKE_FRONT_LEFT:
193          index = ST_SURFACE_FRONT_LEFT;
194          format = drawable->color_format;
195          break;
196       case __DRI_BUFFER_BACK_LEFT:
197          index = ST_SURFACE_BACK_LEFT;
198          format = drawable->color_format;
199          break;
200       case __DRI_BUFFER_DEPTH:
201          index = ST_SURFACE_DEPTH;
202          format = drawable->depth_format;
203          break;
204       case __DRI_BUFFER_STENCIL:
205          index = ST_SURFACE_DEPTH;
206          format = drawable->stencil_format;
207          break;
208       case __DRI_BUFFER_ACCUM:
209       default:
210          assert(0);
211       }
212
213       if (index == ST_SURFACE_DEPTH) {
214          if (have_depth)
215             continue;
216          else
217             have_depth = TRUE;
218       }
219
220       surface = dri_surface_from_handle(api,
221                                         screen,
222                                         buffers[i].name,
223                                         format,
224                                         dri_drawable->w,
225                                         dri_drawable->h, buffers[i].pitch);
226
227       st_set_framebuffer_surface(drawable->stfb, index, surface);
228       pipe_surface_reference(&surface, NULL);
229    }
230    /* this needed, or else the state tracker fails to pick the new buffers */
231    st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
232 }
233
234 /**
235  * These are used for GLX_EXT_texture_from_pixmap
236  */
237 void dri2_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
238                           GLint format, __DRIdrawable *dPriv)
239 {
240    struct dri_drawable *drawable = dri_drawable(dPriv);
241    struct pipe_surface *ps;
242
243    if (!drawable->stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer) {
244       struct gl_renderbuffer *rb =
245          st_new_renderbuffer_fb(drawable->color_format, 0 /*XXX*/, FALSE);
246       _mesa_add_renderbuffer(&drawable->stfb->Base, BUFFER_FRONT_LEFT, rb);
247    }
248
249    dri_get_buffers(drawable->dPriv);
250    st_get_framebuffer_surface(drawable->stfb, ST_SURFACE_FRONT_LEFT, &ps);
251
252    st_bind_texture_surface(ps, target == GL_TEXTURE_2D ? ST_TEXTURE_2D :
253                            ST_TEXTURE_RECT, 0,
254                            format == GLX_TEXTURE_FORMAT_RGBA_EXT ?
255                            PIPE_FORMAT_R8G8B8A8_UNORM : PIPE_FORMAT_R8G8B8X8_UNORM);
256 }
257
258 void dri2_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
259                          __DRIdrawable *dPriv)
260 {
261    dri2_set_tex_buffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
262 }
263
264 void
265 dri_flush_frontbuffer(struct pipe_screen *screen,
266                       struct pipe_surface *surf, void *context_private)
267 {
268    struct dri_context *ctx = (struct dri_context *)context_private;
269    struct dri_drawable *drawable = dri_drawable(ctx->dPriv);
270    __DRIdrawable *dri_drawable = ctx->dPriv;
271    __DRIscreen *dri_screen = ctx->sPriv;
272
273    /* XXX Does this function get called with DRI1? */
274
275    if (ctx->dPriv == NULL) {
276       debug_printf("%s: no drawable bound to context\n", __func__);
277       return;
278    }
279
280 #if 0
281    /* TODO if rendering to pixmaps is slow enable this code. */
282    if (drawable->is_pixmap)
283       return;
284 #else
285    (void)drawable;
286 #endif
287
288    (*dri_screen->dri2.loader->flushFrontBuffer)(dri_drawable,
289                                                 dri_drawable->loaderPrivate);
290 }
291
292 /**
293  * This is called when we need to set up GL rendering to a new X window.
294  */
295 boolean
296 dri_create_buffer(__DRIscreenPrivate * sPriv,
297                   __DRIdrawablePrivate * dPriv,
298                   const __GLcontextModes * visual, boolean isPixmap)
299 {
300    struct dri_screen *screen = sPriv->private;
301    struct dri_drawable *drawable = NULL;
302    int i;
303
304    if (isPixmap)
305       goto fail;                       /* not implemented */
306
307    drawable = CALLOC_STRUCT(dri_drawable);
308    if (drawable == NULL)
309       goto fail;
310
311    if (visual->redBits == 8) {
312       if (visual->alphaBits == 8)
313          drawable->color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
314       else
315          drawable->color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
316    } else {
317       drawable->color_format = PIPE_FORMAT_R5G6B5_UNORM;
318    }
319
320    switch(visual->depthBits) {
321    default:
322    case 0:
323       drawable->depth_format = PIPE_FORMAT_NONE;
324       break;
325    case 16:
326       drawable->depth_format = PIPE_FORMAT_Z16_UNORM;
327       break;
328    case 24:
329       if (visual->stencilBits == 0) {
330          drawable->depth_format = (screen->d_depth_bits_last) ?
331             PIPE_FORMAT_X8Z24_UNORM:
332             PIPE_FORMAT_Z24X8_UNORM;
333       } else {
334          drawable->depth_format = (screen->sd_depth_bits_last) ?
335             PIPE_FORMAT_S8Z24_UNORM:
336             PIPE_FORMAT_Z24S8_UNORM;
337       }
338       break;
339    case 32:
340       drawable->depth_format = PIPE_FORMAT_Z32_UNORM;
341       break;
342    }
343
344    switch(visual->stencilBits) {
345    default:
346    case 0:
347       drawable->stencil_format = PIPE_FORMAT_NONE;
348       break;
349    case 8:
350       drawable->stencil_format = (screen->sd_depth_bits_last) ?
351          PIPE_FORMAT_S8Z24_UNORM:
352          PIPE_FORMAT_Z24S8_UNORM;
353       break;
354    }
355
356    drawable->stfb = st_create_framebuffer(visual,
357                                           drawable->color_format,
358                                           drawable->depth_format,
359                                           drawable->stencil_format,
360                                           dPriv->w,
361                                           dPriv->h, (void *)drawable);
362    if (drawable->stfb == NULL)
363       goto fail;
364
365    drawable->sPriv = sPriv;
366    drawable->dPriv = dPriv;
367    dPriv->driverPrivate = (void *)drawable;
368
369    /* setup dri2 buffers information */
370    /* TODO incase of double buffer visual, delay fake creation */
371    i = 0;
372    drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
373
374    if (visual->doubleBufferMode)
375       drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
376    else
377       drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
378    if (visual->depthBits)
379       drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
380    if (visual->stencilBits)
381       drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
382    drawable->num_attachments = i;
383
384    drawable->desired_fences = 2;
385
386    return GL_TRUE;
387  fail:
388    FREE(drawable);
389    return GL_FALSE;
390 }
391
392 static struct pipe_fence_handle *
393 dri_swap_fences_pop_front(struct dri_drawable *draw)
394 {
395    struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
396    struct pipe_fence_handle *fence = NULL;
397
398    if (draw->cur_fences >= draw->desired_fences) {
399       screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
400       screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
401       --draw->cur_fences;
402       draw->tail &= DRI_SWAP_FENCES_MASK;
403    }
404    return fence;
405 }
406
407 static void
408 dri_swap_fences_push_back(struct dri_drawable *draw,
409                           struct pipe_fence_handle *fence)
410 {
411    struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
412
413    if (!fence)
414       return;
415
416    if (draw->cur_fences < DRI_SWAP_FENCES_MAX) {
417       draw->cur_fences++;
418       screen->fence_reference(screen, &draw->swap_fences[draw->head++],
419                               fence);
420       draw->head &= DRI_SWAP_FENCES_MASK;
421    }
422 }
423
424 void
425 dri_destroy_buffer(__DRIdrawablePrivate * dPriv)
426 {
427    struct dri_drawable *drawable = dri_drawable(dPriv);
428    struct pipe_fence_handle *fence;
429    struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
430
431    st_unreference_framebuffer(drawable->stfb);
432    drawable->desired_fences = 0;
433    while (drawable->cur_fences) {
434       fence = dri_swap_fences_pop_front(drawable);
435       screen->fence_reference(screen, &fence, NULL);
436    }
437
438    FREE(drawable);
439 }
440
441 static void
442 dri1_update_drawables_locked(struct dri_context *ctx,
443                              __DRIdrawablePrivate * driDrawPriv,
444                              __DRIdrawablePrivate * driReadPriv)
445 {
446    if (ctx->stLostLock) {
447       ctx->stLostLock = FALSE;
448       if (driDrawPriv == driReadPriv)
449          DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
450       else
451          DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv,
452                                          driReadPriv);
453    }
454 }
455
456 /**
457  * This ensures all contexts which bind to a drawable pick up the
458  * drawable change and signal new buffer state.
459  * Calling st_resize_framebuffer for each context may seem like overkill,
460  * but no new buffers will actually be allocated if the dimensions don't
461  * change.
462  */
463
464 static void
465 dri1_propagate_drawable_change(struct dri_context *ctx)
466 {
467    __DRIdrawablePrivate *dPriv = ctx->dPriv;
468    __DRIdrawablePrivate *rPriv = ctx->rPriv;
469    boolean flushed = FALSE;
470
471    if (dPriv && ctx->d_stamp != dPriv->lastStamp) {
472
473       st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
474       flushed = TRUE;
475       ctx->d_stamp = dPriv->lastStamp;
476       st_resize_framebuffer(dri_drawable(dPriv)->stfb, dPriv->w, dPriv->h);
477
478    }
479
480    if (rPriv && dPriv != rPriv && ctx->r_stamp != rPriv->lastStamp) {
481
482       if (!flushed)
483          st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
484       ctx->r_stamp = rPriv->lastStamp;
485       st_resize_framebuffer(dri_drawable(rPriv)->stfb, rPriv->w, rPriv->h);
486
487    } else if (rPriv && dPriv == rPriv) {
488
489       ctx->r_stamp = ctx->d_stamp;
490
491    }
492 }
493
494 void
495 dri1_update_drawables(struct dri_context *ctx,
496                       struct dri_drawable *draw, struct dri_drawable *read)
497 {
498    dri_lock(ctx);
499    dri1_update_drawables_locked(ctx, draw->dPriv, read->dPriv);
500    dri_unlock(ctx);
501
502    dri1_propagate_drawable_change(ctx);
503 }
504
505 static INLINE boolean
506 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
507                         int dst_x,
508                         int dst_y,
509                         const struct drm_clip_rect *src,
510                         const struct drm_clip_rect *bbox)
511 {
512    int xy1;
513    int xy2;
514
515    xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
516       (int)bbox->x1 + dst_x;
517    xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
518       (int)bbox->x2 + dst_x;
519    if (xy1 >= xy2 || xy1 < 0)
520       return FALSE;
521
522    dst->x1 = xy1;
523    dst->x2 = xy2;
524
525    xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
526       (int)bbox->y1 + dst_y;
527    xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
528       (int)bbox->y2 + dst_y;
529    if (xy1 >= xy2 || xy1 < 0)
530       return FALSE;
531
532    dst->y1 = xy1;
533    dst->y2 = xy2;
534    return TRUE;
535 }
536
537 static void
538 dri1_swap_copy(struct dri_context *ctx,
539                struct pipe_surface *dst,
540                struct pipe_surface *src,
541                __DRIdrawablePrivate * dPriv, const struct drm_clip_rect *bbox)
542 {
543    struct pipe_context *pipe = ctx->pipe;
544    struct drm_clip_rect clip;
545    struct drm_clip_rect *cur;
546    int i;
547
548    cur = dPriv->pClipRects;
549
550    for (i = 0; i < dPriv->numClipRects; ++i) {
551       if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox))
552          pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
553                             src,
554                             (int)clip.x1 - dPriv->x,
555                             (int)clip.y1 - dPriv->y,
556                             clip.x2 - clip.x1, clip.y2 - clip.y1);
557    }
558 }
559
560 static void
561 dri1_copy_to_front(struct dri_context *ctx,
562                    struct pipe_surface *surf,
563                    __DRIdrawablePrivate * dPriv,
564                    const struct drm_clip_rect *sub_box,
565                    struct pipe_fence_handle **fence)
566 {
567    struct pipe_context *pipe = ctx->pipe;
568    boolean save_lost_lock;
569    uint cur_w;
570    uint cur_h;
571    struct drm_clip_rect bbox;
572    boolean visible = TRUE;
573
574    *fence = NULL;
575
576    dri_lock(ctx);
577    save_lost_lock = ctx->stLostLock;
578    dri1_update_drawables_locked(ctx, dPriv, dPriv);
579    st_get_framebuffer_dimensions(dri_drawable(dPriv)->stfb, &cur_w, &cur_h);
580
581    bbox.x1 = 0;
582    bbox.x2 = cur_w;
583    bbox.y1 = 0;
584    bbox.y2 = cur_h;
585
586    if (sub_box)
587       visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
588
589    if (visible && __dri1_api_hooks->present_locked) {
590
591       __dri1_api_hooks->present_locked(pipe,
592                                        surf,
593                                        dPriv->pClipRects,
594                                        dPriv->numClipRects,
595                                        dPriv->x, dPriv->y, &bbox, fence);
596
597    } else if (visible && __dri1_api_hooks->front_srf_locked) {
598
599       struct pipe_surface *front = __dri1_api_hooks->front_srf_locked(pipe);
600
601       if (front)
602          dri1_swap_copy(ctx, front, surf, dPriv, &bbox);
603
604       st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
605    }
606
607    ctx->stLostLock = save_lost_lock;
608
609    /**
610     * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
611     */
612
613    if (!sub_box)
614       dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
615
616    dri_unlock(ctx);
617    dri1_propagate_drawable_change(ctx);
618 }
619
620 void
621 dri1_flush_frontbuffer(struct pipe_screen *screen,
622                        struct pipe_surface *surf, void *context_private)
623 {
624    struct dri_context *ctx = (struct dri_context *)context_private;
625    struct pipe_fence_handle *dummy_fence;
626
627    dri1_copy_to_front(ctx, surf, ctx->dPriv, NULL, &dummy_fence);
628    screen->fence_reference(screen, &dummy_fence, NULL);
629
630    /**
631     * FIXME: Do we need swap throttling here?
632     */
633 }
634
635 void
636 dri_swap_buffers(__DRIdrawablePrivate * dPriv)
637 {
638    struct dri_context *ctx;
639    struct pipe_surface *back_surf;
640    struct dri_drawable *draw = dri_drawable(dPriv);
641    struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
642    struct pipe_fence_handle *fence;
643    struct st_context *st = st_get_current();
644
645    assert(__dri1_api_hooks != NULL);
646
647    if (!st)
648       return;                          /* For now */
649
650    ctx = (struct dri_context *)st->pipe->priv;
651
652    st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
653    if (back_surf) {
654       st_notify_swapbuffers(draw->stfb);
655       st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
656       fence = dri_swap_fences_pop_front(draw);
657       if (fence) {
658          (void)screen->fence_finish(screen, fence, 0);
659          screen->fence_reference(screen, &fence, NULL);
660       }
661       dri1_copy_to_front(ctx, back_surf, dPriv, NULL, &fence);
662       dri_swap_fences_push_back(draw, fence);
663       screen->fence_reference(screen, &fence, NULL);
664    }
665 }
666
667 void
668 dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
669 {
670    struct pipe_screen *screen = dri_screen(dPriv->driScreenPriv)->pipe_screen;
671    struct drm_clip_rect sub_bbox;
672    struct dri_context *ctx;
673    struct pipe_surface *back_surf;
674    struct dri_drawable *draw = dri_drawable(dPriv);
675    struct pipe_fence_handle *dummy_fence;
676    struct st_context *st = st_get_current();
677
678    assert(__dri1_api_hooks != NULL);
679
680    if (!st)
681       return;
682
683    ctx = (struct dri_context *)st->pipe->priv;
684
685    sub_bbox.x1 = x;
686    sub_bbox.x2 = x + w;
687    sub_bbox.y1 = y;
688    sub_bbox.y2 = y + h;
689
690    st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
691    if (back_surf) {
692       st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
693       dri1_copy_to_front(ctx, back_surf, dPriv, &sub_bbox, &dummy_fence);
694       screen->fence_reference(screen, &dummy_fence, NULL);
695    }
696 }
697
698 /* vim: set sw=3 ts=8 sts=3 expandtab: */