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_format.h"
48 #include "util/u_memory.h"
49 #include "util/u_rect.h"
51 static struct pipe_surface
*
52 dri_surface_from_handle(struct drm_api
*api
,
53 struct pipe_screen
*screen
,
55 enum pipe_format format
,
56 unsigned width
, unsigned height
, unsigned pitch
)
58 struct pipe_surface
*surface
= NULL
;
59 struct pipe_texture
*texture
= NULL
;
60 struct pipe_texture templat
;
62 memset(&templat
, 0, sizeof(templat
));
63 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
64 templat
.target
= PIPE_TEXTURE_2D
;
65 templat
.last_level
= 0;
67 templat
.format
= format
;
68 templat
.width0
= width
;
69 templat
.height0
= height
;
71 texture
= api
->texture_from_shared_handle(api
, screen
, &templat
,
72 "dri2 buffer", pitch
, handle
);
75 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__
);
79 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
80 PIPE_BUFFER_USAGE_GPU_READ
|
81 PIPE_BUFFER_USAGE_GPU_WRITE
);
83 /* we don't need the texture from this point on */
84 pipe_texture_reference(&texture
, NULL
);
89 * Pixmaps have will have the same name of fake front and front.
92 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
94 boolean found
= FALSE
;
95 boolean is_pixmap
= FALSE
;
99 for (i
= 0; i
< count
; i
++) {
100 switch (buffers
[i
].attachment
) {
101 case __DRI_BUFFER_FRONT_LEFT
:
102 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
104 is_pixmap
= buffers
[i
].name
== name
;
106 name
= buffers
[i
].name
;
118 * This will be called a drawable is known to have been resized.
121 dri_get_buffers(__DRIdrawable
* dPriv
)
124 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
125 struct pipe_surface
*surface
= NULL
;
126 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
127 __DRIbuffer
*buffers
= NULL
;
128 __DRIscreen
*dri_screen
= drawable
->sPriv
;
129 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
130 struct drm_api
*api
= ((struct dri_screen
*)(dri_screen
->private))->api
;
131 boolean have_depth
= FALSE
;
134 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
137 drawable
->attachments
,
139 num_attachments
, &count
,
143 if (buffers
== NULL
) {
147 /* set one cliprect to cover the whole dri_drawable */
150 dri_drawable
->backX
= 0;
151 dri_drawable
->backY
= 0;
152 dri_drawable
->numClipRects
= 1;
153 dri_drawable
->pClipRects
[0].x1
= 0;
154 dri_drawable
->pClipRects
[0].y1
= 0;
155 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
156 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
157 dri_drawable
->numBackClipRects
= 1;
158 dri_drawable
->pBackClipRects
[0].x1
= 0;
159 dri_drawable
->pBackClipRects
[0].y1
= 0;
160 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
161 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
163 if (drawable
->old_num
== count
&&
164 drawable
->old_w
== dri_drawable
->w
&&
165 drawable
->old_h
== dri_drawable
->h
&&
166 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
169 drawable
->old_num
= count
;
170 drawable
->old_w
= dri_drawable
->w
;
171 drawable
->old_h
= dri_drawable
->h
;
172 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
175 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
177 for (i
= 0; i
< count
; i
++) {
178 enum pipe_format format
= 0;
181 switch (buffers
[i
].attachment
) {
182 case __DRI_BUFFER_FRONT_LEFT
:
183 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
184 index
= ST_SURFACE_FRONT_LEFT
;
185 format
= drawable
->color_format
;
187 case __DRI_BUFFER_BACK_LEFT
:
188 index
= ST_SURFACE_BACK_LEFT
;
189 format
= drawable
->color_format
;
191 case __DRI_BUFFER_DEPTH
:
192 case __DRI_BUFFER_DEPTH_STENCIL
:
193 case __DRI_BUFFER_STENCIL
:
194 index
= ST_SURFACE_DEPTH
;
195 format
= drawable
->depth_stencil_format
;
197 case __DRI_BUFFER_ACCUM
:
202 if (index
== ST_SURFACE_DEPTH
) {
209 surface
= dri_surface_from_handle(api
,
214 dri_drawable
->h
, buffers
[i
].pitch
);
216 switch (buffers
[i
].attachment
) {
217 case __DRI_BUFFER_FRONT_LEFT
:
218 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
219 case __DRI_BUFFER_BACK_LEFT
:
220 drawable
->color_format
= surface
->format
;
222 case __DRI_BUFFER_DEPTH
:
223 case __DRI_BUFFER_DEPTH_STENCIL
:
224 case __DRI_BUFFER_STENCIL
:
225 drawable
->depth_stencil_format
= surface
->format
;
227 case __DRI_BUFFER_ACCUM
:
232 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
233 pipe_surface_reference(&surface
, NULL
);
235 /* this needed, or else the state tracker fails to pick the new buffers */
236 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
240 * These are used for GLX_EXT_texture_from_pixmap
242 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
243 GLint format
, __DRIdrawable
*dPriv
)
245 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
246 struct pipe_surface
*ps
;
248 if (!drawable
->stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
249 struct gl_renderbuffer
*rb
=
250 st_new_renderbuffer_fb(drawable
->color_format
, 0 /*XXX*/, FALSE
);
251 _mesa_add_renderbuffer(&drawable
->stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
254 dri_get_buffers(drawable
->dPriv
);
255 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
260 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
261 ST_TEXTURE_RECT
, 0, drawable
->color_format
);
264 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
265 __DRIdrawable
*dPriv
)
267 dri2_set_tex_buffer2(pDRICtx
, target
, GLX_TEXTURE_FORMAT_RGBA_EXT
, dPriv
);
271 dri_update_buffer(struct pipe_screen
*screen
, void *context_private
)
273 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
275 dri_get_buffers(ctx
->dPriv
);
279 dri_flush_frontbuffer(struct pipe_screen
*screen
,
280 struct pipe_surface
*surf
, void *context_private
)
282 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
283 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
284 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
285 __DRIscreen
*dri_screen
= ctx
->sPriv
;
287 /* XXX Does this function get called with DRI1? */
289 if (ctx
->dPriv
== NULL
) {
290 debug_printf("%s: no drawable bound to context\n", __func__
);
295 /* TODO if rendering to pixmaps is slow enable this code. */
296 if (drawable
->is_pixmap
)
302 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
303 dri_drawable
->loaderPrivate
);
307 * This is called when we need to set up GL rendering to a new X window.
310 dri_create_buffer(__DRIscreen
* sPriv
,
311 __DRIdrawable
* dPriv
,
312 const __GLcontextModes
* visual
, boolean isPixmap
)
314 struct dri_screen
*screen
= sPriv
->private;
315 struct dri_drawable
*drawable
= NULL
;
319 goto fail
; /* not implemented */
321 drawable
= CALLOC_STRUCT(dri_drawable
);
322 if (drawable
== NULL
)
325 if (visual
->redBits
== 8) {
326 if (visual
->alphaBits
== 8)
327 drawable
->color_format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
329 drawable
->color_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
331 drawable
->color_format
= PIPE_FORMAT_R5G6B5_UNORM
;
334 switch(visual
->depthBits
) {
337 drawable
->depth_stencil_format
= PIPE_FORMAT_NONE
;
340 drawable
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
343 if (visual
->stencilBits
== 0) {
344 drawable
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
345 PIPE_FORMAT_X8Z24_UNORM
:
346 PIPE_FORMAT_Z24X8_UNORM
;
348 drawable
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
349 PIPE_FORMAT_S8Z24_UNORM
:
350 PIPE_FORMAT_Z24S8_UNORM
;
354 drawable
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
358 drawable
->stfb
= st_create_framebuffer(visual
,
359 drawable
->color_format
,
360 drawable
->depth_stencil_format
,
361 drawable
->depth_stencil_format
,
363 dPriv
->h
, (void *)drawable
);
364 if (drawable
->stfb
== NULL
)
367 drawable
->sPriv
= sPriv
;
368 drawable
->dPriv
= dPriv
;
369 dPriv
->driverPrivate
= (void *)drawable
;
371 /* setup dri2 buffers information */
372 /* TODO incase of double buffer visual, delay fake creation */
374 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
376 if (visual
->doubleBufferMode
)
377 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
378 if (visual
->depthBits
&& visual
->stencilBits
)
379 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
380 else if (visual
->depthBits
)
381 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
382 else if (visual
->stencilBits
)
383 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
384 drawable
->num_attachments
= i
;
386 drawable
->desired_fences
= 2;
394 static struct pipe_fence_handle
*
395 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
397 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
398 struct pipe_fence_handle
*fence
= NULL
;
400 if (draw
->cur_fences
>= draw
->desired_fences
) {
401 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
402 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
404 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
410 dri_swap_fences_push_back(struct dri_drawable
*draw
,
411 struct pipe_fence_handle
*fence
)
413 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
418 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
420 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
422 draw
->head
&= DRI_SWAP_FENCES_MASK
;
427 dri_destroy_buffer(__DRIdrawable
* dPriv
)
429 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
430 struct pipe_fence_handle
*fence
;
431 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
433 st_unreference_framebuffer(drawable
->stfb
);
434 drawable
->desired_fences
= 0;
435 while (drawable
->cur_fences
) {
436 fence
= dri_swap_fences_pop_front(drawable
);
437 screen
->fence_reference(screen
, &fence
, NULL
);
444 dri1_update_drawables_locked(struct dri_context
*ctx
,
445 __DRIdrawable
* driDrawPriv
,
446 __DRIdrawable
* driReadPriv
)
448 if (ctx
->stLostLock
) {
449 ctx
->stLostLock
= FALSE
;
450 if (driDrawPriv
== driReadPriv
)
451 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
453 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
459 * This ensures all contexts which bind to a drawable pick up the
460 * drawable change and signal new buffer state.
461 * Calling st_resize_framebuffer for each context may seem like overkill,
462 * but no new buffers will actually be allocated if the dimensions don't
467 dri1_propagate_drawable_change(struct dri_context
*ctx
)
469 __DRIdrawable
*dPriv
= ctx
->dPriv
;
470 __DRIdrawable
*rPriv
= ctx
->rPriv
;
471 boolean flushed
= FALSE
;
473 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
475 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
477 ctx
->d_stamp
= dPriv
->lastStamp
;
478 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
482 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
485 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
486 ctx
->r_stamp
= rPriv
->lastStamp
;
487 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
489 } else if (rPriv
&& dPriv
== rPriv
) {
491 ctx
->r_stamp
= ctx
->d_stamp
;
497 dri1_update_drawables(struct dri_context
*ctx
,
498 struct dri_drawable
*draw
, struct dri_drawable
*read
)
501 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
504 dri1_propagate_drawable_change(ctx
);
507 static INLINE boolean
508 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
511 const struct drm_clip_rect
*src
,
512 const struct drm_clip_rect
*bbox
)
517 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
518 (int)bbox
->x1
+ dst_x
;
519 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
520 (int)bbox
->x2
+ dst_x
;
521 if (xy1
>= xy2
|| xy1
< 0)
527 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
528 (int)bbox
->y1
+ dst_y
;
529 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
530 (int)bbox
->y2
+ dst_y
;
531 if (xy1
>= xy2
|| xy1
< 0)
540 dri1_swap_copy(struct dri_context
*ctx
,
541 struct pipe_surface
*dst
,
542 struct pipe_surface
*src
,
543 __DRIdrawable
* dPriv
, const struct drm_clip_rect
*bbox
)
545 struct pipe_context
*pipe
= ctx
->pipe
;
546 struct drm_clip_rect clip
;
547 struct drm_clip_rect
*cur
;
550 cur
= dPriv
->pClipRects
;
552 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
553 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
)) {
554 if (pipe
->surface_copy
) {
555 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
557 (int)clip
.x1
- dPriv
->x
,
558 (int)clip
.y1
- dPriv
->y
,
559 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
561 util_surface_copy(pipe
, FALSE
, dst
, clip
.x1
, clip
.y1
,
563 (int)clip
.x1
- dPriv
->x
,
564 (int)clip
.y1
- dPriv
->y
,
565 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
572 dri1_copy_to_front(struct dri_context
*ctx
,
573 struct pipe_surface
*surf
,
574 __DRIdrawable
* dPriv
,
575 const struct drm_clip_rect
*sub_box
,
576 struct pipe_fence_handle
**fence
)
578 struct pipe_context
*pipe
= ctx
->pipe
;
579 boolean save_lost_lock
;
582 struct drm_clip_rect bbox
;
583 boolean visible
= TRUE
;
588 save_lost_lock
= ctx
->stLostLock
;
589 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
590 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
598 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
600 if (visible
&& __dri1_api_hooks
->present_locked
) {
602 __dri1_api_hooks
->present_locked(pipe
,
606 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
608 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
610 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
613 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
615 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
618 ctx
->stLostLock
= save_lost_lock
;
621 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
625 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
628 dri1_propagate_drawable_change(ctx
);
632 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
633 struct pipe_surface
*surf
, void *context_private
)
635 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
636 struct pipe_fence_handle
*dummy_fence
;
638 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
639 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
642 * FIXME: Do we need swap throttling here?
647 dri_swap_buffers(__DRIdrawable
* dPriv
)
649 struct dri_context
*ctx
;
650 struct pipe_surface
*back_surf
;
651 struct dri_drawable
*draw
= dri_drawable(dPriv
);
652 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
653 struct pipe_fence_handle
*fence
;
654 struct st_context
*st
= st_get_current();
656 assert(__dri1_api_hooks
!= NULL
);
659 return; /* For now */
661 ctx
= (struct dri_context
*)st
->pipe
->priv
;
663 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
665 st_notify_swapbuffers(draw
->stfb
);
666 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
667 fence
= dri_swap_fences_pop_front(draw
);
669 (void)screen
->fence_finish(screen
, fence
, 0);
670 screen
->fence_reference(screen
, &fence
, NULL
);
672 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
673 dri_swap_fences_push_back(draw
, fence
);
674 screen
->fence_reference(screen
, &fence
, NULL
);
679 dri_copy_sub_buffer(__DRIdrawable
* dPriv
, int x
, int y
, int w
, int h
)
681 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
682 struct drm_clip_rect sub_bbox
;
683 struct dri_context
*ctx
;
684 struct pipe_surface
*back_surf
;
685 struct dri_drawable
*draw
= dri_drawable(dPriv
);
686 struct pipe_fence_handle
*dummy_fence
;
687 struct st_context
*st
= st_get_current();
689 assert(__dri1_api_hooks
!= NULL
);
694 ctx
= (struct dri_context
*)st
->pipe
->priv
;
701 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
703 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
704 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
705 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
709 /* vim: set sw=3 ts=8 sts=3 expandtab: */