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