Merge master and fix conflicts
[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_DEPTH_STENCIL, 0))
247 depthFormat = PIPE_FORMAT_Z24S8_UNORM;
248 else
249 depthFormat = PIPE_FORMAT_S8Z24_UNORM;
250 } else
251 depthFormat = PIPE_FORMAT_NONE;
252
253 if (visual->stencilBits) {
254 if (pscreen->is_format_supported(pscreen, PIPE_FORMAT_Z24S8_UNORM,
255 PIPE_TEXTURE_2D,
256 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0))
257 stencilFormat = PIPE_FORMAT_Z24S8_UNORM;
258 else
259 stencilFormat = PIPE_FORMAT_S8Z24_UNORM;
260 } else
261 stencilFormat = PIPE_FORMAT_NONE;
262
263 drawable->stfb = st_create_framebuffer(visual,
264 colorFormat,
265 depthFormat,
266 stencilFormat,
267 dPriv->w,
268 dPriv->h, (void *)drawable);
269 if (drawable->stfb == NULL)
270 goto fail;
271
272 drawable->sPriv = sPriv;
273 drawable->dPriv = dPriv;
274 dPriv->driverPrivate = (void *)drawable;
275
276 /* setup dri2 buffers information */
277 i = 0;
278 drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
279 #if 0
280 /* TODO incase of double buffer visual, delay fake creation */
281 drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
282 #endif
283 if (visual->doubleBufferMode)
284 drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
285 if (visual->depthBits)
286 drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
287 if (visual->stencilBits)
288 drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
289 drawable->num_attachments = i;
290
291 drawable->desired_fences = 2;
292
293 return GL_TRUE;
294 fail:
295 FREE(drawable);
296 return GL_FALSE;
297 }
298
299 static struct pipe_fence_handle *
300 dri_swap_fences_pop_front(struct dri_drawable *draw)
301 {
302 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
303 struct pipe_fence_handle *fence = NULL;
304
305 if (draw->cur_fences >= draw->desired_fences) {
306 screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
307 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
308 --draw->cur_fences;
309 draw->tail &= DRI_SWAP_FENCES_MASK;
310 }
311 return fence;
312 }
313
314 static void
315 dri_swap_fences_push_back(struct dri_drawable *draw,
316 struct pipe_fence_handle *fence)
317 {
318 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
319
320 if (!fence)
321 return;
322
323 if (draw->cur_fences < DRI_SWAP_FENCES_MAX) {
324 draw->cur_fences++;
325 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
326 fence);
327 draw->head &= DRI_SWAP_FENCES_MASK;
328 }
329 }
330
331 void
332 dri_destroy_buffer(__DRIdrawablePrivate * dPriv)
333 {
334 struct dri_drawable *drawable = dri_drawable(dPriv);
335 struct pipe_fence_handle *fence;
336 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
337
338 st_unreference_framebuffer(drawable->stfb);
339 drawable->desired_fences = 0;
340 while (drawable->cur_fences) {
341 fence = dri_swap_fences_pop_front(drawable);
342 screen->fence_reference(screen, &fence, NULL);
343 }
344
345 FREE(drawable);
346 }
347
348 static void
349 dri1_update_drawables_locked(struct dri_context *ctx,
350 __DRIdrawablePrivate * driDrawPriv,
351 __DRIdrawablePrivate * driReadPriv)
352 {
353 if (ctx->stLostLock) {
354 ctx->stLostLock = FALSE;
355 if (driDrawPriv == driReadPriv)
356 DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
357 else
358 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv,
359 driReadPriv);
360 }
361 }
362
363 /**
364 * This ensures all contexts which bind to a drawable pick up the
365 * drawable change and signal new buffer state.
366 * Calling st_resize_framebuffer for each context may seem like overkill,
367 * but no new buffers will actually be allocated if the dimensions don't
368 * change.
369 */
370
371 static void
372 dri1_propagate_drawable_change(struct dri_context *ctx)
373 {
374 __DRIdrawablePrivate *dPriv = ctx->dPriv;
375 __DRIdrawablePrivate *rPriv = ctx->rPriv;
376 boolean flushed = FALSE;
377
378 if (dPriv && ctx->d_stamp != dPriv->lastStamp) {
379
380 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
381 flushed = TRUE;
382 ctx->d_stamp = dPriv->lastStamp;
383 st_resize_framebuffer(dri_drawable(dPriv)->stfb, dPriv->w, dPriv->h);
384
385 }
386
387 if (rPriv && dPriv != rPriv && ctx->r_stamp != rPriv->lastStamp) {
388
389 if (!flushed)
390 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
391 ctx->r_stamp = rPriv->lastStamp;
392 st_resize_framebuffer(dri_drawable(rPriv)->stfb, rPriv->w, rPriv->h);
393
394 } else if (rPriv && dPriv == rPriv) {
395
396 ctx->r_stamp = ctx->d_stamp;
397
398 }
399 }
400
401 void
402 dri1_update_drawables(struct dri_context *ctx,
403 struct dri_drawable *draw, struct dri_drawable *read)
404 {
405 dri_lock(ctx);
406 dri1_update_drawables_locked(ctx, draw->dPriv, read->dPriv);
407 dri_unlock(ctx);
408
409 dri1_propagate_drawable_change(ctx);
410 }
411
412 static INLINE boolean
413 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
414 int dst_x,
415 int dst_y,
416 const struct drm_clip_rect *src,
417 const struct drm_clip_rect *bbox)
418 {
419 int xy1;
420 int xy2;
421
422 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
423 (int)bbox->x1 + dst_x;
424 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
425 (int)bbox->x2 + dst_x;
426 if (xy1 >= xy2 || xy1 < 0)
427 return FALSE;
428
429 dst->x1 = xy1;
430 dst->x2 = xy2;
431
432 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
433 (int)bbox->y1 + dst_y;
434 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
435 (int)bbox->y2 + dst_y;
436 if (xy1 >= xy2 || xy1 < 0)
437 return FALSE;
438
439 dst->y1 = xy1;
440 dst->y2 = xy2;
441 return TRUE;
442 }
443
444 static void
445 dri1_swap_copy(struct dri_context *ctx,
446 struct pipe_surface *dst,
447 struct pipe_surface *src,
448 __DRIdrawablePrivate * dPriv, const struct drm_clip_rect *bbox)
449 {
450 struct pipe_context *pipe = ctx->pipe;
451 struct drm_clip_rect clip;
452 struct drm_clip_rect *cur;
453 int i;
454
455 cur = dPriv->pClipRects;
456
457 for (i = 0; i < dPriv->numClipRects; ++i) {
458 if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox))
459 pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
460 src,
461 (int)clip.x1 - dPriv->x,
462 (int)clip.y1 - dPriv->y,
463 clip.x2 - clip.x1, clip.y2 - clip.y1);
464 }
465 }
466
467 static void
468 dri1_copy_to_front(struct dri_context *ctx,
469 struct pipe_surface *surf,
470 __DRIdrawablePrivate * dPriv,
471 const struct drm_clip_rect *sub_box,
472 struct pipe_fence_handle **fence)
473 {
474 struct pipe_context *pipe = ctx->pipe;
475 boolean save_lost_lock;
476 uint cur_w;
477 uint cur_h;
478 struct drm_clip_rect bbox;
479 boolean visible = TRUE;
480
481 *fence = NULL;
482
483 dri_lock(ctx);
484 save_lost_lock = ctx->stLostLock;
485 dri1_update_drawables_locked(ctx, dPriv, dPriv);
486 st_get_framebuffer_dimensions(dri_drawable(dPriv)->stfb, &cur_w, &cur_h);
487
488 bbox.x1 = 0;
489 bbox.x2 = cur_w;
490 bbox.y1 = 0;
491 bbox.y2 = cur_h;
492
493 if (sub_box)
494 visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
495
496 if (visible && __dri1_api_hooks->present_locked) {
497
498 __dri1_api_hooks->present_locked(pipe,
499 surf,
500 dPriv->pClipRects,
501 dPriv->numClipRects,
502 dPriv->x, dPriv->y, &bbox, fence);
503
504 } else if (visible && __dri1_api_hooks->front_srf_locked) {
505
506 struct pipe_surface *front = __dri1_api_hooks->front_srf_locked(pipe);
507
508 if (front)
509 dri1_swap_copy(ctx, front, surf, dPriv, &bbox);
510
511 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
512 }
513
514 ctx->stLostLock = save_lost_lock;
515
516 /**
517 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
518 */
519
520 if (!sub_box)
521 dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
522
523 dri_unlock(ctx);
524 dri1_propagate_drawable_change(ctx);
525 }
526
527 void
528 dri1_flush_frontbuffer(struct pipe_screen *screen,
529 struct pipe_surface *surf, void *context_private)
530 {
531 struct dri_context *ctx = (struct dri_context *)context_private;
532 struct pipe_fence_handle *dummy_fence;
533
534 dri1_copy_to_front(ctx, surf, ctx->dPriv, NULL, &dummy_fence);
535 screen->fence_reference(screen, &dummy_fence, NULL);
536
537 /**
538 * FIXME: Do we need swap throttling here?
539 */
540 }
541
542 void
543 dri_swap_buffers(__DRIdrawablePrivate * dPriv)
544 {
545 struct dri_context *ctx;
546 struct pipe_surface *back_surf;
547 struct dri_drawable *draw = dri_drawable(dPriv);
548 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
549 struct pipe_fence_handle *fence;
550 struct st_context *st = st_get_current();
551
552 assert(__dri1_api_hooks != NULL);
553
554 if (!st)
555 return; /* For now */
556
557 ctx = (struct dri_context *)st->pipe->priv;
558
559 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
560 if (back_surf) {
561 st_notify_swapbuffers(draw->stfb);
562 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
563 fence = dri_swap_fences_pop_front(draw);
564 if (fence) {
565 (void)screen->fence_finish(screen, fence, 0);
566 screen->fence_reference(screen, &fence, NULL);
567 }
568 dri1_copy_to_front(ctx, back_surf, dPriv, NULL, &fence);
569 dri_swap_fences_push_back(draw, fence);
570 screen->fence_reference(screen, &fence, NULL);
571 }
572 }
573
574 void
575 dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
576 {
577 struct pipe_screen *screen = dri_screen(dPriv->driScreenPriv)->pipe_screen;
578 struct drm_clip_rect sub_bbox;
579 struct dri_context *ctx;
580 struct pipe_surface *back_surf;
581 struct dri_drawable *draw = dri_drawable(dPriv);
582 struct pipe_fence_handle *dummy_fence;
583 struct st_context *st = st_get_current();
584
585 assert(__dri1_api_hooks != NULL);
586
587 if (!st)
588 return;
589
590 ctx = (struct dri_context *)st->pipe->priv;
591
592 sub_bbox.x1 = x;
593 sub_bbox.x2 = x + w;
594 sub_bbox.y1 = y;
595 sub_bbox.y2 = y + h;
596
597 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
598 if (back_surf) {
599 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
600 dri1_copy_to_front(ctx, back_surf, dPriv, &sub_bbox, &dummy_fence);
601 screen->fence_reference(screen, &dummy_fence, NULL);
602 }
603 }
604
605 /* vim: set sw=3 ts=8 sts=3 expandtab: */