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