Merge branch 'ext-provoking-vertex'
[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 = drawable->color_format;
161 break;
162 case __DRI_BUFFER_FAKE_FRONT_LEFT:
163 index = ST_SURFACE_FRONT_LEFT;
164 format = drawable->color_format;
165 break;
166 case __DRI_BUFFER_BACK_LEFT:
167 index = ST_SURFACE_BACK_LEFT;
168 format = drawable->color_format;
169 break;
170 case __DRI_BUFFER_DEPTH:
171 index = ST_SURFACE_DEPTH;
172 format = drawable->depth_format;
173 break;
174 case __DRI_BUFFER_STENCIL:
175 index = ST_SURFACE_DEPTH;
176 format = drawable->stencil_format;
177 break;
178 case __DRI_BUFFER_ACCUM:
179 default:
180 assert(0);
181 }
182
183 if (index == ST_SURFACE_DEPTH) {
184 if (have_depth)
185 continue;
186 else
187 have_depth = TRUE;
188 }
189
190 surface = dri_surface_from_handle(screen,
191 buffers[i].name,
192 format,
193 dri_drawable->w,
194 dri_drawable->h, buffers[i].pitch);
195
196 st_set_framebuffer_surface(drawable->stfb, index, surface);
197 pipe_surface_reference(&surface, NULL);
198 }
199 /* this needed, or else the state tracker fails to pick the new buffers */
200 st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
201 }
202
203 void
204 dri_flush_frontbuffer(struct pipe_screen *screen,
205 struct pipe_surface *surf, void *context_private)
206 {
207 struct dri_context *ctx = (struct dri_context *)context_private;
208
209 dri_copy_to_front(ctx->dPriv, surf, 0, 0, surf->width, surf->height);
210 }
211
212 /**
213 * This is called when we need to set up GL rendering to a new X window.
214 */
215 boolean
216 dri_create_buffer(__DRIscreenPrivate * sPriv,
217 __DRIdrawablePrivate * dPriv,
218 const __GLcontextModes * visual, boolean isPixmap)
219 {
220 struct dri_screen *screen = sPriv->private;
221 struct dri_drawable *drawable = NULL;
222 int i;
223
224 if (isPixmap)
225 goto fail; /* not implemented */
226
227 drawable = CALLOC_STRUCT(dri_drawable);
228 if (drawable == NULL)
229 goto fail;
230
231 drawable->color_format = (visual->redBits == 8) ?
232 PIPE_FORMAT_A8R8G8B8_UNORM : PIPE_FORMAT_R5G6B5_UNORM;
233
234 debug_printf("Red bits is %d\n", visual->redBits);
235
236 switch(visual->depthBits) {
237 default:
238 case 0:
239 debug_printf("Depth buffer 0.\n");
240 drawable->depth_format = PIPE_FORMAT_NONE;
241 break;
242 case 16:
243 debug_printf("Depth buffer 16.\n");
244 drawable->depth_format = PIPE_FORMAT_Z16_UNORM;
245 break;
246 case 24:
247 if (visual->stencilBits == 0) {
248 debug_printf("Depth buffer 24. Stencil 0.\n");
249 drawable->depth_format = (screen->d_depth_bits_last) ?
250 PIPE_FORMAT_X8Z24_UNORM:
251 PIPE_FORMAT_Z24X8_UNORM;
252 } else {
253 debug_printf("Combined depth stencil 24 / 8.\n");
254 drawable->depth_format = (screen->sd_depth_bits_last) ?
255 PIPE_FORMAT_S8Z24_UNORM:
256 PIPE_FORMAT_Z24S8_UNORM;
257 }
258 break;
259 }
260
261 switch(visual->stencilBits) {
262 default:
263 case 0:
264 drawable->stencil_format = PIPE_FORMAT_NONE;
265 break;
266 case 8:
267 drawable->stencil_format = (screen->sd_depth_bits_last) ?
268 PIPE_FORMAT_S8Z24_UNORM:
269 PIPE_FORMAT_Z24S8_UNORM;
270 break;
271 }
272
273 drawable->stfb = st_create_framebuffer(visual,
274 drawable->color_format,
275 drawable->depth_format,
276 drawable->stencil_format,
277 dPriv->w,
278 dPriv->h, (void *)drawable);
279 if (drawable->stfb == NULL)
280 goto fail;
281
282 drawable->sPriv = sPriv;
283 drawable->dPriv = dPriv;
284 dPriv->driverPrivate = (void *)drawable;
285
286 /* setup dri2 buffers information */
287 i = 0;
288 drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
289 #if 0
290 /* TODO incase of double buffer visual, delay fake creation */
291 drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
292 #endif
293 if (visual->doubleBufferMode)
294 drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
295 if (visual->depthBits)
296 drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
297 if (visual->stencilBits)
298 drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
299 drawable->num_attachments = i;
300
301 drawable->desired_fences = 2;
302
303 return GL_TRUE;
304 fail:
305 FREE(drawable);
306 return GL_FALSE;
307 }
308
309 static struct pipe_fence_handle *
310 dri_swap_fences_pop_front(struct dri_drawable *draw)
311 {
312 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
313 struct pipe_fence_handle *fence = NULL;
314
315 if (draw->cur_fences >= draw->desired_fences) {
316 screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
317 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
318 --draw->cur_fences;
319 draw->tail &= DRI_SWAP_FENCES_MASK;
320 }
321 return fence;
322 }
323
324 static void
325 dri_swap_fences_push_back(struct dri_drawable *draw,
326 struct pipe_fence_handle *fence)
327 {
328 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
329
330 if (!fence)
331 return;
332
333 if (draw->cur_fences < DRI_SWAP_FENCES_MAX) {
334 draw->cur_fences++;
335 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
336 fence);
337 draw->head &= DRI_SWAP_FENCES_MASK;
338 }
339 }
340
341 void
342 dri_destroy_buffer(__DRIdrawablePrivate * dPriv)
343 {
344 struct dri_drawable *drawable = dri_drawable(dPriv);
345 struct pipe_fence_handle *fence;
346 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
347
348 st_unreference_framebuffer(drawable->stfb);
349 drawable->desired_fences = 0;
350 while (drawable->cur_fences) {
351 fence = dri_swap_fences_pop_front(drawable);
352 screen->fence_reference(screen, &fence, NULL);
353 }
354
355 FREE(drawable);
356 }
357
358 static void
359 dri1_update_drawables_locked(struct dri_context *ctx,
360 __DRIdrawablePrivate * driDrawPriv,
361 __DRIdrawablePrivate * driReadPriv)
362 {
363 if (ctx->stLostLock) {
364 ctx->stLostLock = FALSE;
365 if (driDrawPriv == driReadPriv)
366 DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
367 else
368 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv,
369 driReadPriv);
370 }
371 }
372
373 /**
374 * This ensures all contexts which bind to a drawable pick up the
375 * drawable change and signal new buffer state.
376 * Calling st_resize_framebuffer for each context may seem like overkill,
377 * but no new buffers will actually be allocated if the dimensions don't
378 * change.
379 */
380
381 static void
382 dri1_propagate_drawable_change(struct dri_context *ctx)
383 {
384 __DRIdrawablePrivate *dPriv = ctx->dPriv;
385 __DRIdrawablePrivate *rPriv = ctx->rPriv;
386 boolean flushed = FALSE;
387
388 if (dPriv && ctx->d_stamp != dPriv->lastStamp) {
389
390 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
391 flushed = TRUE;
392 ctx->d_stamp = dPriv->lastStamp;
393 st_resize_framebuffer(dri_drawable(dPriv)->stfb, dPriv->w, dPriv->h);
394
395 }
396
397 if (rPriv && dPriv != rPriv && ctx->r_stamp != rPriv->lastStamp) {
398
399 if (!flushed)
400 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
401 ctx->r_stamp = rPriv->lastStamp;
402 st_resize_framebuffer(dri_drawable(rPriv)->stfb, rPriv->w, rPriv->h);
403
404 } else if (rPriv && dPriv == rPriv) {
405
406 ctx->r_stamp = ctx->d_stamp;
407
408 }
409 }
410
411 void
412 dri1_update_drawables(struct dri_context *ctx,
413 struct dri_drawable *draw, struct dri_drawable *read)
414 {
415 dri_lock(ctx);
416 dri1_update_drawables_locked(ctx, draw->dPriv, read->dPriv);
417 dri_unlock(ctx);
418
419 dri1_propagate_drawable_change(ctx);
420 }
421
422 static INLINE boolean
423 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
424 int dst_x,
425 int dst_y,
426 const struct drm_clip_rect *src,
427 const struct drm_clip_rect *bbox)
428 {
429 int xy1;
430 int xy2;
431
432 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
433 (int)bbox->x1 + dst_x;
434 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
435 (int)bbox->x2 + dst_x;
436 if (xy1 >= xy2 || xy1 < 0)
437 return FALSE;
438
439 dst->x1 = xy1;
440 dst->x2 = xy2;
441
442 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
443 (int)bbox->y1 + dst_y;
444 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
445 (int)bbox->y2 + dst_y;
446 if (xy1 >= xy2 || xy1 < 0)
447 return FALSE;
448
449 dst->y1 = xy1;
450 dst->y2 = xy2;
451 return TRUE;
452 }
453
454 static void
455 dri1_swap_copy(struct dri_context *ctx,
456 struct pipe_surface *dst,
457 struct pipe_surface *src,
458 __DRIdrawablePrivate * dPriv, const struct drm_clip_rect *bbox)
459 {
460 struct pipe_context *pipe = ctx->pipe;
461 struct drm_clip_rect clip;
462 struct drm_clip_rect *cur;
463 int i;
464
465 cur = dPriv->pClipRects;
466
467 for (i = 0; i < dPriv->numClipRects; ++i) {
468 if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox))
469 pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
470 src,
471 (int)clip.x1 - dPriv->x,
472 (int)clip.y1 - dPriv->y,
473 clip.x2 - clip.x1, clip.y2 - clip.y1);
474 }
475 }
476
477 static void
478 dri1_copy_to_front(struct dri_context *ctx,
479 struct pipe_surface *surf,
480 __DRIdrawablePrivate * dPriv,
481 const struct drm_clip_rect *sub_box,
482 struct pipe_fence_handle **fence)
483 {
484 struct pipe_context *pipe = ctx->pipe;
485 boolean save_lost_lock;
486 uint cur_w;
487 uint cur_h;
488 struct drm_clip_rect bbox;
489 boolean visible = TRUE;
490
491 *fence = NULL;
492
493 dri_lock(ctx);
494 save_lost_lock = ctx->stLostLock;
495 dri1_update_drawables_locked(ctx, dPriv, dPriv);
496 st_get_framebuffer_dimensions(dri_drawable(dPriv)->stfb, &cur_w, &cur_h);
497
498 bbox.x1 = 0;
499 bbox.x2 = cur_w;
500 bbox.y1 = 0;
501 bbox.y2 = cur_h;
502
503 if (sub_box)
504 visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
505
506 if (visible && __dri1_api_hooks->present_locked) {
507
508 __dri1_api_hooks->present_locked(pipe,
509 surf,
510 dPriv->pClipRects,
511 dPriv->numClipRects,
512 dPriv->x, dPriv->y, &bbox, fence);
513
514 } else if (visible && __dri1_api_hooks->front_srf_locked) {
515
516 struct pipe_surface *front = __dri1_api_hooks->front_srf_locked(pipe);
517
518 if (front)
519 dri1_swap_copy(ctx, front, surf, dPriv, &bbox);
520
521 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
522 }
523
524 ctx->stLostLock = save_lost_lock;
525
526 /**
527 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
528 */
529
530 if (!sub_box)
531 dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
532
533 dri_unlock(ctx);
534 dri1_propagate_drawable_change(ctx);
535 }
536
537 void
538 dri1_flush_frontbuffer(struct pipe_screen *screen,
539 struct pipe_surface *surf, void *context_private)
540 {
541 struct dri_context *ctx = (struct dri_context *)context_private;
542 struct pipe_fence_handle *dummy_fence;
543
544 dri1_copy_to_front(ctx, surf, ctx->dPriv, NULL, &dummy_fence);
545 screen->fence_reference(screen, &dummy_fence, NULL);
546
547 /**
548 * FIXME: Do we need swap throttling here?
549 */
550 }
551
552 void
553 dri_swap_buffers(__DRIdrawablePrivate * dPriv)
554 {
555 struct dri_context *ctx;
556 struct pipe_surface *back_surf;
557 struct dri_drawable *draw = dri_drawable(dPriv);
558 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
559 struct pipe_fence_handle *fence;
560 struct st_context *st = st_get_current();
561
562 assert(__dri1_api_hooks != NULL);
563
564 if (!st)
565 return; /* For now */
566
567 ctx = (struct dri_context *)st->pipe->priv;
568
569 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
570 if (back_surf) {
571 st_notify_swapbuffers(draw->stfb);
572 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
573 fence = dri_swap_fences_pop_front(draw);
574 if (fence) {
575 (void)screen->fence_finish(screen, fence, 0);
576 screen->fence_reference(screen, &fence, NULL);
577 }
578 dri1_copy_to_front(ctx, back_surf, dPriv, NULL, &fence);
579 dri_swap_fences_push_back(draw, fence);
580 screen->fence_reference(screen, &fence, NULL);
581 }
582 }
583
584 void
585 dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
586 {
587 struct pipe_screen *screen = dri_screen(dPriv->driScreenPriv)->pipe_screen;
588 struct drm_clip_rect sub_bbox;
589 struct dri_context *ctx;
590 struct pipe_surface *back_surf;
591 struct dri_drawable *draw = dri_drawable(dPriv);
592 struct pipe_fence_handle *dummy_fence;
593 struct st_context *st = st_get_current();
594
595 assert(__dri1_api_hooks != NULL);
596
597 if (!st)
598 return;
599
600 ctx = (struct dri_context *)st->pipe->priv;
601
602 sub_bbox.x1 = x;
603 sub_bbox.x2 = x + w;
604 sub_bbox.y1 = y;
605 sub_bbox.y2 = y + h;
606
607 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
608 if (back_surf) {
609 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
610 dri1_copy_to_front(ctx, back_surf, dPriv, &sub_bbox, &dummy_fence);
611 screen->fence_reference(screen, &dummy_fence, NULL);
612 }
613 }
614
615 /* vim: set sw=3 ts=8 sts=3 expandtab: */