gallium: Update the dri2 state tracker to support dri1.
[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
48 static void
49 dri_copy_to_front(__DRIdrawablePrivate *dPriv,
50 struct pipe_surface *from,
51 int x, int y, unsigned w, unsigned h)
52 {
53 /* TODO send a message to the Xserver to copy to the real front buffer */
54 }
55
56
57 static struct pipe_surface *
58 dri_surface_from_handle(struct pipe_screen *screen,
59 unsigned handle,
60 enum pipe_format format,
61 unsigned width,
62 unsigned height,
63 unsigned pitch)
64 {
65 struct pipe_surface *surface = NULL;
66 struct pipe_texture *texture = NULL;
67 struct pipe_texture templat;
68 struct pipe_buffer *buf = NULL;
69
70 buf = drm_api_hooks.buffer_from_handle(screen, "dri2 buffer", handle);
71 if (!buf)
72 return NULL;
73
74 memset(&templat, 0, sizeof(templat));
75 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
76 templat.target = PIPE_TEXTURE_2D;
77 templat.last_level = 0;
78 templat.depth[0] = 1;
79 templat.format = format;
80 templat.width[0] = width;
81 templat.height[0] = height;
82 pf_get_block(templat.format, &templat.block);
83
84 texture = screen->texture_blanket(screen,
85 &templat,
86 &pitch,
87 buf);
88
89 /* we don't need the buffer from this point on */
90 pipe_buffer_reference(&buf, NULL);
91
92 if (!texture)
93 return NULL;
94
95 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
96 PIPE_BUFFER_USAGE_GPU_READ |
97 PIPE_BUFFER_USAGE_GPU_WRITE);
98
99 /* we don't need the texture from this point on */
100 pipe_texture_reference(&texture, NULL);
101 return surface;
102 }
103
104
105 /**
106 * This will be called a drawable is known to have been resized.
107 */
108 void
109 dri_get_buffers(__DRIdrawablePrivate *dPriv)
110 {
111 struct dri_drawable *drawable = dri_drawable(dPriv);
112 struct pipe_surface *surface = NULL;
113 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
114 __DRIbuffer *buffers = NULL;
115 __DRIscreen *dri_screen = drawable->sPriv;
116 __DRIdrawable *dri_drawable = drawable->dPriv;
117 boolean have_depth = FALSE;
118 int i, count;
119
120 buffers = (*dri_screen->dri2.loader->getBuffers)(dri_drawable,
121 &dri_drawable->w,
122 &dri_drawable->h,
123 drawable->attachments,
124 drawable->num_attachments,
125 &count,
126 dri_drawable->loaderPrivate);
127
128 if (buffers == NULL) {
129 return;
130 }
131
132 /* set one cliprect to cover the whole dri_drawable */
133 dri_drawable->x = 0;
134 dri_drawable->y = 0;
135 dri_drawable->backX = 0;
136 dri_drawable->backY = 0;
137 dri_drawable->numClipRects = 1;
138 dri_drawable->pClipRects[0].x1 = 0;
139 dri_drawable->pClipRects[0].y1 = 0;
140 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
141 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
142 dri_drawable->numBackClipRects = 1;
143 dri_drawable->pBackClipRects[0].x1 = 0;
144 dri_drawable->pBackClipRects[0].y1 = 0;
145 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
146 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
147
148 for (i = 0; i < count; i++) {
149 enum pipe_format format = 0;
150 int index = 0;
151
152 switch (buffers[i].attachment) {
153 case __DRI_BUFFER_FRONT_LEFT:
154 index = ST_SURFACE_FRONT_LEFT;
155 format = PIPE_FORMAT_A8R8G8B8_UNORM;
156 break;
157 case __DRI_BUFFER_FAKE_FRONT_LEFT:
158 index = ST_SURFACE_FRONT_LEFT;
159 format = PIPE_FORMAT_A8R8G8B8_UNORM;
160 break;
161 case __DRI_BUFFER_BACK_LEFT:
162 index = ST_SURFACE_BACK_LEFT;
163 format = PIPE_FORMAT_A8R8G8B8_UNORM;
164 break;
165 case __DRI_BUFFER_DEPTH:
166 index = ST_SURFACE_DEPTH;
167 format = PIPE_FORMAT_Z24S8_UNORM;
168 break;
169 case __DRI_BUFFER_STENCIL:
170 index = ST_SURFACE_DEPTH;
171 format = PIPE_FORMAT_Z24S8_UNORM;
172 break;
173 case __DRI_BUFFER_ACCUM:
174 default:
175 assert(0);
176 }
177 assert(buffers[i].cpp == 4);
178
179 if (index == ST_SURFACE_DEPTH) {
180 if (have_depth)
181 continue;
182 else
183 have_depth = TRUE;
184 }
185
186 surface = dri_surface_from_handle(screen,
187 buffers[i].name,
188 format,
189 dri_drawable->w,
190 dri_drawable->h,
191 buffers[i].pitch);
192
193 st_set_framebuffer_surface(drawable->stfb, index, surface);
194 pipe_surface_reference(&surface, NULL);
195 }
196 /* this needed, or else the state tracker fails to pick the new buffers */
197 st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
198 }
199
200
201 void
202 dri_flush_frontbuffer(struct pipe_screen *screen,
203 struct pipe_surface *surf,
204 void *context_private)
205 {
206 struct dri_context *ctx = (struct dri_context *)context_private;
207 dri_copy_to_front(ctx->dPriv, surf, 0, 0, surf->width, surf->height);
208 }
209
210
211 /**
212 * This is called when we need to set up GL rendering to a new X window.
213 */
214 boolean
215 dri_create_buffer(__DRIscreenPrivate *sPriv,
216 __DRIdrawablePrivate *dPriv,
217 const __GLcontextModes *visual,
218 boolean isPixmap)
219 {
220 enum pipe_format colorFormat, depthFormat, stencilFormat;
221 struct dri_screen *screen = sPriv->private;
222 struct dri_drawable *drawable = NULL;
223 struct pipe_screen *pscreen = screen->pipe_screen;
224 int i;
225
226 if (isPixmap)
227 goto fail; /* not implemented */
228
229 drawable = CALLOC_STRUCT(dri_drawable);
230 if (drawable == NULL)
231 goto fail;
232
233 /* XXX: todo: use the pipe_screen queries to figure out which
234 * render targets are supportable.
235 */
236 assert(visual->redBits == 8);
237 assert(visual->depthBits == 24 || visual->depthBits == 0);
238 assert(visual->stencilBits == 8 || visual->stencilBits == 0);
239
240 colorFormat = PIPE_FORMAT_A8R8G8B8_UNORM;
241
242 if (visual->depthBits) {
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 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_RENDER_TARGET |
257 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0))
258 stencilFormat = PIPE_FORMAT_Z24S8_UNORM;
259 else
260 stencilFormat = PIPE_FORMAT_S8Z24_UNORM;
261 } else
262 stencilFormat = PIPE_FORMAT_NONE;
263
264 drawable->stfb = st_create_framebuffer(visual,
265 colorFormat,
266 depthFormat,
267 stencilFormat,
268 dPriv->w,
269 dPriv->h,
270 (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++], fence);
328 draw->head &= DRI_SWAP_FENCES_MASK;
329 }
330 }
331
332 void
333 dri_destroy_buffer(__DRIdrawablePrivate *dPriv)
334 {
335 struct dri_drawable *drawable = dri_drawable(dPriv);
336 struct pipe_fence_handle *fence;
337 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
338
339 st_unreference_framebuffer(drawable->stfb);
340 drawable->desired_fences = 0;
341 while(drawable->cur_fences) {
342 fence = dri_swap_fences_pop_front(drawable);
343 screen->fence_reference(screen, &fence, NULL);
344 }
345
346 FREE(drawable);
347 }
348
349 static void
350 dri1_update_drawables_locked(struct dri_context *ctx,
351 __DRIdrawablePrivate *driDrawPriv,
352 __DRIdrawablePrivate *driReadPriv)
353 {
354 if (ctx->stLostLock) {
355 ctx->stLostLock = FALSE;
356 if (driDrawPriv == driReadPriv)
357 DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
358 else
359 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv, driReadPriv);
360 }
361 }
362
363 /**
364 * This ensures all contexts which binds to a drawable picks up the
365 * drawable change and signals 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 doesn'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,
404 struct dri_drawable *read)
405 {
406 dri_lock(ctx);
407 dri1_update_drawables_locked(ctx, draw->dPriv, read->dPriv);
408 dri_unlock(ctx);
409
410 dri1_propagate_drawable_change(ctx);
411 }
412
413 static INLINE boolean
414 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
415 int dst_x,
416 int dst_y,
417 const struct drm_clip_rect *src,
418 const struct drm_clip_rect *bbox)
419 {
420 int xy1;
421 int xy2;
422
423 xy1 = ((int) src->x1 > (int) bbox->x1 + dst_x) ? src->x1 :
424 (int) bbox->x1 + dst_x;
425 xy2 = ((int) src->x2 < (int) bbox->x2 + dst_x) ? src->x2 :
426 (int) bbox->x2 + dst_x;
427 if (xy1 >= xy2 || xy1 < 0)
428 return FALSE;
429
430 dst->x1 = xy1;
431 dst->x2 = xy2;
432
433 xy1 = ((int) src->y1 > (int) bbox->y1 + dst_x) ? src->y1 :
434 (int) bbox->y1 + dst_x;
435 xy2 = ((int) src->y2 < (int) bbox->y2 + dst_x) ? src->y2 :
436 (int) bbox->y2 + dst_x;
437 if (xy1 >= xy2 || xy1 < 0)
438 return FALSE;
439
440 dst->y1 = xy1;
441 dst->y2 = xy2;
442 return TRUE;
443 }
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,
451 const struct drm_clip_rect *bbox)
452 {
453 struct pipe_context *pipe = ctx->pipe;
454 struct drm_clip_rect clip;
455 struct drm_clip_rect *cur;
456 int i;
457
458 cur = dPriv->pClipRects;
459
460 for (i=0; i<dPriv->numClipRects; ++i) {
461 if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox))
462 pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
463 src,
464 (int) clip.x1 - dPriv->x,
465 (int) clip.y1 - dPriv->y,
466 clip.x2 - clip.x1,
467 clip.y2 - clip.y1);
468 }
469 }
470
471 static void
472 dri1_copy_to_front(struct dri_context *ctx,
473 struct pipe_surface *surf,
474 __DRIdrawablePrivate *dPriv,
475 const struct drm_clip_rect *sub_box,
476 struct pipe_fence_handle **fence)
477 {
478 struct pipe_context *pipe = ctx->pipe;
479 boolean save_lost_lock;
480 uint cur_w;
481 uint cur_h;
482 struct drm_clip_rect bbox;
483 boolean visible = TRUE;
484
485 *fence = NULL;
486
487 dri_lock(ctx);
488 save_lost_lock = ctx->stLostLock;
489 dri1_update_drawables_locked(ctx, dPriv, dPriv);
490 st_get_framebuffer_dimensions(dri_drawable(dPriv)->stfb, &cur_w, &cur_h);
491
492 bbox.x1 = 0;
493 bbox.x2 = cur_w;
494 bbox.y1 = 0;
495 bbox.y2 = cur_h;
496
497 if (sub_box)
498 visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
499
500 if (visible && __dri1_api_hooks->present_locked) {
501
502 __dri1_api_hooks->present_locked(pipe,
503 surf,
504 dPriv->pClipRects,
505 dPriv->numClipRects,
506 dPriv->x,
507 dPriv->y,
508 &bbox,
509 fence);
510
511 } else if (visible && __dri1_api_hooks->front_srf_locked) {
512
513 struct pipe_surface *front =
514 __dri1_api_hooks->front_srf_locked(pipe);
515
516 if (front)
517 dri1_swap_copy(ctx, front, surf, dPriv, &bbox);
518
519 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
520 }
521
522 ctx->stLostLock = save_lost_lock;
523
524 /**
525 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
526 */
527
528 if (!sub_box)
529 dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
530
531 dri_unlock(ctx);
532 dri1_propagate_drawable_change(ctx);
533 }
534
535 void
536 dri1_flush_frontbuffer(struct pipe_screen *screen,
537 struct pipe_surface *surf,
538 void *context_private)
539 {
540 struct dri_context *ctx = (struct dri_context *)context_private;
541 struct pipe_fence_handle *dummy_fence;
542
543 dri1_copy_to_front(ctx, surf, ctx->dPriv, NULL, &dummy_fence);
544
545 /**
546 * FIXME: Do we need swap throttling here?
547 */
548 }
549
550 void
551 dri_swap_buffers(__DRIdrawablePrivate * dPriv)
552 {
553 struct dri_context *ctx;
554 struct pipe_surface *back_surf;
555 struct dri_drawable *draw = dri_drawable(dPriv);
556 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
557 struct pipe_fence_handle *fence;
558 GET_CURRENT_CONTEXT(glCtx);
559
560 assert(__dri1_api_hooks != NULL);
561
562 if (!glCtx)
563 return; /* For now */
564
565 ctx = (struct dri_context *) glCtx->st->pipe->priv;
566
567 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
568 if (back_surf) {
569 st_notify_swapbuffers(draw->stfb);
570 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
571 fence = dri_swap_fences_pop_front(draw);
572 if (fence) {
573 (void) screen->fence_finish(screen, fence, 0);
574 screen->fence_reference(screen, &fence, NULL);
575 }
576 dri1_copy_to_front(ctx, back_surf, dPriv, NULL, &fence);
577 dri_swap_fences_push_back(draw, fence);
578 screen->fence_reference(screen, &fence, NULL);
579 }
580 }
581
582 void
583 dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
584 {
585 struct drm_clip_rect sub_bbox;
586 struct dri_context *ctx;
587 struct pipe_surface *back_surf;
588 struct dri_drawable *draw = dri_drawable(dPriv);
589 struct pipe_fence_handle *dummy_fence;
590 GET_CURRENT_CONTEXT(glCtx);
591
592 assert(__dri1_api_hooks != NULL);
593
594 if (!glCtx)
595 return;
596
597 ctx = (struct dri_context *) glCtx->st->pipe->priv;
598
599 sub_bbox.x1 = x;
600 sub_bbox.x2 = x + w;
601 sub_bbox.y1 = y;
602 sub_bbox.y2 = y + h;
603
604 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
605 if (back_surf) {
606 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
607 dri1_copy_to_front(ctx, back_surf, dPriv, &sub_bbox, &dummy_fence);
608 }
609 }
610
611 /* vim: set sw=3 ts=8 sts=3 expandtab: */