OSDN Git Service

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