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