1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_inlines.h"
39 #include "main/mtypes.h"
40 #include "main/renderbuffer.h"
41 #include "state_tracker/drm_api.h"
42 #include "state_tracker/dri1_api.h"
43 #include "state_tracker/st_public.h"
44 #include "state_tracker/st_context.h"
45 #include "state_tracker/st_cb_fbo.h"
47 #include "util/u_memory.h"
49 static struct pipe_surface *
50 dri_surface_from_handle(struct drm_api *api,
51 struct pipe_screen *screen,
53 enum pipe_format format,
54 unsigned width, unsigned height, unsigned pitch)
56 struct pipe_surface *surface = NULL;
57 struct pipe_texture *texture = NULL;
58 struct pipe_texture templat;
59 struct pipe_buffer *buf = NULL;
61 buf = api->buffer_from_handle(api, screen, "dri2 buffer", handle);
63 debug_printf("%s: Failed to get buffer from handle\n", __func__);
67 memset(&templat, 0, sizeof(templat));
68 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
69 templat.target = PIPE_TEXTURE_2D;
70 templat.last_level = 0;
72 templat.format = format;
73 templat.width[0] = width;
74 templat.height[0] = height;
75 pf_get_block(templat.format, &templat.block);
77 texture = screen->texture_blanket(screen, &templat, &pitch, buf);
79 /* we don't need the buffer from this point on */
80 pipe_buffer_reference(&buf, NULL);
83 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__);
87 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
88 PIPE_BUFFER_USAGE_GPU_READ |
89 PIPE_BUFFER_USAGE_GPU_WRITE);
91 /* we don't need the texture from this point on */
92 pipe_texture_reference(&texture, NULL);
97 * Pixmaps have will have the same name of fake front and front.
100 dri2_check_if_pixmap(__DRIbuffer *buffers, int count)
102 boolean found = FALSE;
103 boolean is_pixmap = FALSE;
107 for (i = 0; i < count; i++) {
108 switch (buffers[i].attachment) {
109 case __DRI_BUFFER_FRONT_LEFT:
110 case __DRI_BUFFER_FAKE_FRONT_LEFT:
112 is_pixmap = buffers[i].name == name;
114 name = buffers[i].name;
126 * This will be called a drawable is known to have been resized.
129 dri_get_buffers(__DRIdrawablePrivate * dPriv)
132 struct dri_drawable *drawable = dri_drawable(dPriv);
133 struct pipe_surface *surface = NULL;
134 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
135 __DRIbuffer *buffers = NULL;
136 __DRIscreen *dri_screen = drawable->sPriv;
137 __DRIdrawable *dri_drawable = drawable->dPriv;
138 struct drm_api *api = ((struct dri_screen*)(dri_screen->private))->api;
139 boolean have_depth = FALSE;
142 buffers = (*dri_screen->dri2.loader->getBuffers) (dri_drawable,
145 drawable->attachments,
147 num_attachments, &count,
151 if (buffers == NULL) {
155 /* set one cliprect to cover the whole dri_drawable */
158 dri_drawable->backX = 0;
159 dri_drawable->backY = 0;
160 dri_drawable->numClipRects = 1;
161 dri_drawable->pClipRects[0].x1 = 0;
162 dri_drawable->pClipRects[0].y1 = 0;
163 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
164 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
165 dri_drawable->numBackClipRects = 1;
166 dri_drawable->pBackClipRects[0].x1 = 0;
167 dri_drawable->pBackClipRects[0].y1 = 0;
168 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
169 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
171 if (drawable->old_num == count &&
172 drawable->old_w == dri_drawable->w &&
173 drawable->old_h == dri_drawable->h &&
174 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0) {
177 drawable->old_num = count;
178 drawable->old_w = dri_drawable->w;
179 drawable->old_h = dri_drawable->h;
180 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
183 drawable->is_pixmap = dri2_check_if_pixmap(buffers, count);
185 for (i = 0; i < count; i++) {
186 enum pipe_format format = 0;
189 switch (buffers[i].attachment) {
190 case __DRI_BUFFER_FRONT_LEFT:
192 case __DRI_BUFFER_FAKE_FRONT_LEFT:
193 index = ST_SURFACE_FRONT_LEFT;
194 format = drawable->color_format;
196 case __DRI_BUFFER_BACK_LEFT:
197 index = ST_SURFACE_BACK_LEFT;
198 format = drawable->color_format;
200 case __DRI_BUFFER_DEPTH:
201 index = ST_SURFACE_DEPTH;
202 format = drawable->depth_format;
204 case __DRI_BUFFER_STENCIL:
205 index = ST_SURFACE_DEPTH;
206 format = drawable->stencil_format;
208 case __DRI_BUFFER_ACCUM:
213 if (index == ST_SURFACE_DEPTH) {
220 surface = dri_surface_from_handle(api,
225 dri_drawable->h, buffers[i].pitch);
227 st_set_framebuffer_surface(drawable->stfb, index, surface);
228 pipe_surface_reference(&surface, NULL);
230 /* this needed, or else the state tracker fails to pick the new buffers */
231 st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
235 * These are used for GLX_EXT_texture_from_pixmap
237 void dri2_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
238 GLint format, __DRIdrawable *dPriv)
240 struct dri_drawable *drawable = dri_drawable(dPriv);
241 struct pipe_surface *ps;
243 if (!drawable->stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer) {
244 struct gl_renderbuffer *rb =
245 st_new_renderbuffer_fb(drawable->color_format, 0 /*XXX*/, FALSE);
246 _mesa_add_renderbuffer(&drawable->stfb->Base, BUFFER_FRONT_LEFT, rb);
249 dri_get_buffers(drawable->dPriv);
250 st_get_framebuffer_surface(drawable->stfb, ST_SURFACE_FRONT_LEFT, &ps);
252 st_bind_texture_surface(ps, target == GL_TEXTURE_2D ? ST_TEXTURE_2D :
254 format == GLX_TEXTURE_FORMAT_RGBA_EXT ?
255 PIPE_FORMAT_R8G8B8A8_UNORM : PIPE_FORMAT_R8G8B8X8_UNORM);
258 void dri2_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
259 __DRIdrawable *dPriv)
261 dri2_set_tex_buffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
265 dri_flush_frontbuffer(struct pipe_screen *screen,
266 struct pipe_surface *surf, void *context_private)
268 struct dri_context *ctx = (struct dri_context *)context_private;
269 struct dri_drawable *drawable = dri_drawable(ctx->dPriv);
270 __DRIdrawable *dri_drawable = ctx->dPriv;
271 __DRIscreen *dri_screen = ctx->sPriv;
273 /* XXX Does this function get called with DRI1? */
275 if (ctx->dPriv == NULL) {
276 debug_printf("%s: no drawable bound to context\n", __func__);
281 /* TODO if rendering to pixmaps is slow enable this code. */
282 if (drawable->is_pixmap)
288 (*dri_screen->dri2.loader->flushFrontBuffer)(dri_drawable,
289 dri_drawable->loaderPrivate);
293 * This is called when we need to set up GL rendering to a new X window.
296 dri_create_buffer(__DRIscreenPrivate * sPriv,
297 __DRIdrawablePrivate * dPriv,
298 const __GLcontextModes * visual, boolean isPixmap)
300 struct dri_screen *screen = sPriv->private;
301 struct dri_drawable *drawable = NULL;
305 goto fail; /* not implemented */
307 drawable = CALLOC_STRUCT(dri_drawable);
308 if (drawable == NULL)
311 if (visual->redBits == 8) {
312 if (visual->alphaBits == 8)
313 drawable->color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
315 drawable->color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
317 drawable->color_format = PIPE_FORMAT_R5G6B5_UNORM;
320 switch(visual->depthBits) {
323 drawable->depth_format = PIPE_FORMAT_NONE;
326 drawable->depth_format = PIPE_FORMAT_Z16_UNORM;
329 if (visual->stencilBits == 0) {
330 drawable->depth_format = (screen->d_depth_bits_last) ?
331 PIPE_FORMAT_X8Z24_UNORM:
332 PIPE_FORMAT_Z24X8_UNORM;
334 drawable->depth_format = (screen->sd_depth_bits_last) ?
335 PIPE_FORMAT_S8Z24_UNORM:
336 PIPE_FORMAT_Z24S8_UNORM;
340 drawable->depth_format = PIPE_FORMAT_Z32_UNORM;
344 switch(visual->stencilBits) {
347 drawable->stencil_format = PIPE_FORMAT_NONE;
350 drawable->stencil_format = (screen->sd_depth_bits_last) ?
351 PIPE_FORMAT_S8Z24_UNORM:
352 PIPE_FORMAT_Z24S8_UNORM;
356 drawable->stfb = st_create_framebuffer(visual,
357 drawable->color_format,
358 drawable->depth_format,
359 drawable->stencil_format,
361 dPriv->h, (void *)drawable);
362 if (drawable->stfb == NULL)
365 drawable->sPriv = sPriv;
366 drawable->dPriv = dPriv;
367 dPriv->driverPrivate = (void *)drawable;
369 /* setup dri2 buffers information */
370 /* TODO incase of double buffer visual, delay fake creation */
372 drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
374 if (visual->doubleBufferMode)
375 drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
377 drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
378 if (visual->depthBits)
379 drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
380 if (visual->stencilBits)
381 drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
382 drawable->num_attachments = i;
384 drawable->desired_fences = 2;
392 static struct pipe_fence_handle *
393 dri_swap_fences_pop_front(struct dri_drawable *draw)
395 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
396 struct pipe_fence_handle *fence = NULL;
398 if (draw->cur_fences >= draw->desired_fences) {
399 screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
400 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
402 draw->tail &= DRI_SWAP_FENCES_MASK;
408 dri_swap_fences_push_back(struct dri_drawable *draw,
409 struct pipe_fence_handle *fence)
411 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
416 if (draw->cur_fences < DRI_SWAP_FENCES_MAX) {
418 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
420 draw->head &= DRI_SWAP_FENCES_MASK;
425 dri_destroy_buffer(__DRIdrawablePrivate * dPriv)
427 struct dri_drawable *drawable = dri_drawable(dPriv);
428 struct pipe_fence_handle *fence;
429 struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
431 st_unreference_framebuffer(drawable->stfb);
432 drawable->desired_fences = 0;
433 while (drawable->cur_fences) {
434 fence = dri_swap_fences_pop_front(drawable);
435 screen->fence_reference(screen, &fence, NULL);
442 dri1_update_drawables_locked(struct dri_context *ctx,
443 __DRIdrawablePrivate * driDrawPriv,
444 __DRIdrawablePrivate * driReadPriv)
446 if (ctx->stLostLock) {
447 ctx->stLostLock = FALSE;
448 if (driDrawPriv == driReadPriv)
449 DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
451 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv,
457 * This ensures all contexts which bind to a drawable pick up the
458 * drawable change and signal new buffer state.
459 * Calling st_resize_framebuffer for each context may seem like overkill,
460 * but no new buffers will actually be allocated if the dimensions don't
465 dri1_propagate_drawable_change(struct dri_context *ctx)
467 __DRIdrawablePrivate *dPriv = ctx->dPriv;
468 __DRIdrawablePrivate *rPriv = ctx->rPriv;
469 boolean flushed = FALSE;
471 if (dPriv && ctx->d_stamp != dPriv->lastStamp) {
473 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
475 ctx->d_stamp = dPriv->lastStamp;
476 st_resize_framebuffer(dri_drawable(dPriv)->stfb, dPriv->w, dPriv->h);
480 if (rPriv && dPriv != rPriv && ctx->r_stamp != rPriv->lastStamp) {
483 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
484 ctx->r_stamp = rPriv->lastStamp;
485 st_resize_framebuffer(dri_drawable(rPriv)->stfb, rPriv->w, rPriv->h);
487 } else if (rPriv && dPriv == rPriv) {
489 ctx->r_stamp = ctx->d_stamp;
495 dri1_update_drawables(struct dri_context *ctx,
496 struct dri_drawable *draw, struct dri_drawable *read)
499 dri1_update_drawables_locked(ctx, draw->dPriv, read->dPriv);
502 dri1_propagate_drawable_change(ctx);
505 static INLINE boolean
506 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
509 const struct drm_clip_rect *src,
510 const struct drm_clip_rect *bbox)
515 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
516 (int)bbox->x1 + dst_x;
517 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
518 (int)bbox->x2 + dst_x;
519 if (xy1 >= xy2 || xy1 < 0)
525 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
526 (int)bbox->y1 + dst_y;
527 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
528 (int)bbox->y2 + dst_y;
529 if (xy1 >= xy2 || xy1 < 0)
538 dri1_swap_copy(struct dri_context *ctx,
539 struct pipe_surface *dst,
540 struct pipe_surface *src,
541 __DRIdrawablePrivate * dPriv, const struct drm_clip_rect *bbox)
543 struct pipe_context *pipe = ctx->pipe;
544 struct drm_clip_rect clip;
545 struct drm_clip_rect *cur;
548 cur = dPriv->pClipRects;
550 for (i = 0; i < dPriv->numClipRects; ++i) {
551 if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox))
552 pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
554 (int)clip.x1 - dPriv->x,
555 (int)clip.y1 - dPriv->y,
556 clip.x2 - clip.x1, clip.y2 - clip.y1);
561 dri1_copy_to_front(struct dri_context *ctx,
562 struct pipe_surface *surf,
563 __DRIdrawablePrivate * dPriv,
564 const struct drm_clip_rect *sub_box,
565 struct pipe_fence_handle **fence)
567 struct pipe_context *pipe = ctx->pipe;
568 boolean save_lost_lock;
571 struct drm_clip_rect bbox;
572 boolean visible = TRUE;
577 save_lost_lock = ctx->stLostLock;
578 dri1_update_drawables_locked(ctx, dPriv, dPriv);
579 st_get_framebuffer_dimensions(dri_drawable(dPriv)->stfb, &cur_w, &cur_h);
587 visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
589 if (visible && __dri1_api_hooks->present_locked) {
591 __dri1_api_hooks->present_locked(pipe,
595 dPriv->x, dPriv->y, &bbox, fence);
597 } else if (visible && __dri1_api_hooks->front_srf_locked) {
599 struct pipe_surface *front = __dri1_api_hooks->front_srf_locked(pipe);
602 dri1_swap_copy(ctx, front, surf, dPriv, &bbox);
604 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
607 ctx->stLostLock = save_lost_lock;
610 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
614 dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
617 dri1_propagate_drawable_change(ctx);
621 dri1_flush_frontbuffer(struct pipe_screen *screen,
622 struct pipe_surface *surf, void *context_private)
624 struct dri_context *ctx = (struct dri_context *)context_private;
625 struct pipe_fence_handle *dummy_fence;
627 dri1_copy_to_front(ctx, surf, ctx->dPriv, NULL, &dummy_fence);
628 screen->fence_reference(screen, &dummy_fence, NULL);
631 * FIXME: Do we need swap throttling here?
636 dri_swap_buffers(__DRIdrawablePrivate * dPriv)
638 struct dri_context *ctx;
639 struct pipe_surface *back_surf;
640 struct dri_drawable *draw = dri_drawable(dPriv);
641 struct pipe_screen *screen = dri_screen(draw->sPriv)->pipe_screen;
642 struct pipe_fence_handle *fence;
643 struct st_context *st = st_get_current();
645 assert(__dri1_api_hooks != NULL);
648 return; /* For now */
650 ctx = (struct dri_context *)st->pipe->priv;
652 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
654 st_notify_swapbuffers(draw->stfb);
655 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
656 fence = dri_swap_fences_pop_front(draw);
658 (void)screen->fence_finish(screen, fence, 0);
659 screen->fence_reference(screen, &fence, NULL);
661 dri1_copy_to_front(ctx, back_surf, dPriv, NULL, &fence);
662 dri_swap_fences_push_back(draw, fence);
663 screen->fence_reference(screen, &fence, NULL);
668 dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
670 struct pipe_screen *screen = dri_screen(dPriv->driScreenPriv)->pipe_screen;
671 struct drm_clip_rect sub_bbox;
672 struct dri_context *ctx;
673 struct pipe_surface *back_surf;
674 struct dri_drawable *draw = dri_drawable(dPriv);
675 struct pipe_fence_handle *dummy_fence;
676 struct st_context *st = st_get_current();
678 assert(__dri1_api_hooks != NULL);
683 ctx = (struct dri_context *)st->pipe->priv;
690 st_get_framebuffer_surface(draw->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
692 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
693 dri1_copy_to_front(ctx, back_surf, dPriv, &sub_bbox, &dummy_fence);
694 screen->fence_reference(screen, &dummy_fence, NULL);
698 /* vim: set sw=3 ts=8 sts=3 expandtab: */