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