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