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 "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"
45 #include "util/u_memory.h"
47 static struct pipe_surface
*
48 dri_surface_from_handle(struct drm_api
*api
,
49 struct pipe_screen
*screen
,
51 enum pipe_format format
,
52 unsigned width
, unsigned height
, unsigned pitch
)
54 struct pipe_surface
*surface
= NULL
;
55 struct pipe_texture
*texture
= NULL
;
56 struct pipe_texture templat
;
57 struct pipe_buffer
*buf
= NULL
;
59 buf
= api
->buffer_from_handle(api
, screen
, "dri2 buffer", handle
);
61 debug_printf("%s: Failed to get buffer from handle\n", __func__
);
65 memset(&templat
, 0, sizeof(templat
));
66 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
67 templat
.target
= PIPE_TEXTURE_2D
;
68 templat
.last_level
= 0;
70 templat
.format
= format
;
71 templat
.width
[0] = width
;
72 templat
.height
[0] = height
;
73 pf_get_block(templat
.format
, &templat
.block
);
75 texture
= screen
->texture_blanket(screen
, &templat
, &pitch
, buf
);
77 /* we don't need the buffer from this point on */
78 pipe_buffer_reference(&buf
, NULL
);
81 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__
);
85 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
86 PIPE_BUFFER_USAGE_GPU_READ
|
87 PIPE_BUFFER_USAGE_GPU_WRITE
);
89 /* we don't need the texture from this point on */
90 pipe_texture_reference(&texture
, NULL
);
95 * Pixmaps have will have the same name of fake front and front.
98 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
100 boolean found
= FALSE
;
101 boolean is_pixmap
= FALSE
;
105 for (i
= 0; i
< count
; i
++) {
106 switch (buffers
[i
].attachment
) {
107 case __DRI_BUFFER_FRONT_LEFT
:
108 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
110 is_pixmap
= buffers
[i
].name
== name
;
112 name
= buffers
[i
].name
;
124 * This will be called a drawable is known to have been resized.
127 dri_get_buffers(__DRIdrawablePrivate
* dPriv
)
130 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
131 struct pipe_surface
*surface
= NULL
;
132 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
133 __DRIbuffer
*buffers
= NULL
;
134 __DRIscreen
*dri_screen
= drawable
->sPriv
;
135 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
136 struct drm_api
*api
= ((struct dri_screen
*)(dri_screen
->private))->api
;
137 boolean have_depth
= FALSE
;
140 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
143 drawable
->attachments
,
145 num_attachments
, &count
,
149 if (buffers
== NULL
) {
153 /* set one cliprect to cover the whole dri_drawable */
156 dri_drawable
->backX
= 0;
157 dri_drawable
->backY
= 0;
158 dri_drawable
->numClipRects
= 1;
159 dri_drawable
->pClipRects
[0].x1
= 0;
160 dri_drawable
->pClipRects
[0].y1
= 0;
161 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
162 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
163 dri_drawable
->numBackClipRects
= 1;
164 dri_drawable
->pBackClipRects
[0].x1
= 0;
165 dri_drawable
->pBackClipRects
[0].y1
= 0;
166 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
167 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
169 if (drawable
->old_num
== count
&&
170 drawable
->old_w
== dri_drawable
->w
&&
171 drawable
->old_h
== dri_drawable
->h
&&
172 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
175 drawable
->old_num
= count
;
176 drawable
->old_w
= dri_drawable
->w
;
177 drawable
->old_h
= dri_drawable
->h
;
178 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
181 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
183 for (i
= 0; i
< count
; i
++) {
184 enum pipe_format format
= 0;
187 switch (buffers
[i
].attachment
) {
188 case __DRI_BUFFER_FRONT_LEFT
:
190 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
191 index
= ST_SURFACE_FRONT_LEFT
;
192 format
= drawable
->color_format
;
194 case __DRI_BUFFER_BACK_LEFT
:
195 index
= ST_SURFACE_BACK_LEFT
;
196 format
= drawable
->color_format
;
198 case __DRI_BUFFER_DEPTH
:
199 index
= ST_SURFACE_DEPTH
;
200 format
= drawable
->depth_format
;
202 case __DRI_BUFFER_STENCIL
:
203 index
= ST_SURFACE_DEPTH
;
204 format
= drawable
->stencil_format
;
206 case __DRI_BUFFER_ACCUM
:
211 if (index
== ST_SURFACE_DEPTH
) {
218 surface
= dri_surface_from_handle(api
,
223 dri_drawable
->h
, buffers
[i
].pitch
);
225 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
226 pipe_surface_reference(&surface
, NULL
);
228 /* this needed, or else the state tracker fails to pick the new buffers */
229 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
233 * These are used for GLX_EXT_texture_from_pixmap
235 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
236 GLint format
, __DRIdrawable
*dPriv
)
238 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
239 struct pipe_surface
*ps
;
241 dri_get_buffers(drawable
->dPriv
);
242 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
244 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
246 format
== GLX_TEXTURE_FORMAT_RGBA_EXT
?
247 PIPE_FORMAT_R8G8B8A8_UNORM
: PIPE_FORMAT_R8G8B8X8_UNORM
);
250 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
251 __DRIdrawable
*dPriv
)
253 dri2_set_tex_buffer2(pDRICtx
, target
, GLX_TEXTURE_FORMAT_RGBA_EXT
, dPriv
);
257 dri_flush_frontbuffer(struct pipe_screen
*screen
,
258 struct pipe_surface
*surf
, void *context_private
)
260 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
261 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
262 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
263 __DRIscreen
*dri_screen
= ctx
->sPriv
;
265 /* XXX Does this function get called with DRI1? */
267 if (ctx
->dPriv
== NULL
) {
268 debug_printf("%s: no drawable bound to context\n", __func__
);
273 /* TODO if rendering to pixmaps is slow enable this code. */
274 if (drawable
->is_pixmap
)
278 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
279 dri_drawable
->loaderPrivate
);
283 * This is called when we need to set up GL rendering to a new X window.
286 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
287 __DRIdrawablePrivate
* dPriv
,
288 const __GLcontextModes
* visual
, boolean isPixmap
)
290 struct dri_screen
*screen
= sPriv
->private;
291 struct dri_drawable
*drawable
= NULL
;
295 goto fail
; /* not implemented */
297 drawable
= CALLOC_STRUCT(dri_drawable
);
298 if (drawable
== NULL
)
301 if (visual
->redBits
== 8) {
302 if (visual
->alphaBits
== 8)
303 drawable
->color_format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
305 drawable
->color_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
307 drawable
->color_format
= PIPE_FORMAT_R5G6B5_UNORM
;
310 switch(visual
->depthBits
) {
313 drawable
->depth_format
= PIPE_FORMAT_NONE
;
316 drawable
->depth_format
= PIPE_FORMAT_Z16_UNORM
;
319 if (visual
->stencilBits
== 0) {
320 drawable
->depth_format
= (screen
->d_depth_bits_last
) ?
321 PIPE_FORMAT_X8Z24_UNORM
:
322 PIPE_FORMAT_Z24X8_UNORM
;
324 drawable
->depth_format
= (screen
->sd_depth_bits_last
) ?
325 PIPE_FORMAT_S8Z24_UNORM
:
326 PIPE_FORMAT_Z24S8_UNORM
;
330 drawable
->depth_format
= PIPE_FORMAT_Z32_UNORM
;
334 switch(visual
->stencilBits
) {
337 drawable
->stencil_format
= PIPE_FORMAT_NONE
;
340 drawable
->stencil_format
= (screen
->sd_depth_bits_last
) ?
341 PIPE_FORMAT_S8Z24_UNORM
:
342 PIPE_FORMAT_Z24S8_UNORM
;
346 drawable
->stfb
= st_create_framebuffer(visual
,
347 drawable
->color_format
,
348 drawable
->depth_format
,
349 drawable
->stencil_format
,
351 dPriv
->h
, (void *)drawable
);
352 if (drawable
->stfb
== NULL
)
355 drawable
->sPriv
= sPriv
;
356 drawable
->dPriv
= dPriv
;
357 dPriv
->driverPrivate
= (void *)drawable
;
359 /* setup dri2 buffers information */
360 /* TODO incase of double buffer visual, delay fake creation */
362 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
363 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
365 if (visual
->doubleBufferMode
)
366 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
367 if (visual
->depthBits
)
368 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
369 if (visual
->stencilBits
)
370 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
371 drawable
->num_attachments
= i
;
373 drawable
->desired_fences
= 2;
381 static struct pipe_fence_handle
*
382 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
384 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
385 struct pipe_fence_handle
*fence
= NULL
;
387 if (draw
->cur_fences
>= draw
->desired_fences
) {
388 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
389 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
391 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
397 dri_swap_fences_push_back(struct dri_drawable
*draw
,
398 struct pipe_fence_handle
*fence
)
400 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
405 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
407 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
409 draw
->head
&= DRI_SWAP_FENCES_MASK
;
414 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
416 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
417 struct pipe_fence_handle
*fence
;
418 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
420 st_unreference_framebuffer(drawable
->stfb
);
421 drawable
->desired_fences
= 0;
422 while (drawable
->cur_fences
) {
423 fence
= dri_swap_fences_pop_front(drawable
);
424 screen
->fence_reference(screen
, &fence
, NULL
);
431 dri1_update_drawables_locked(struct dri_context
*ctx
,
432 __DRIdrawablePrivate
* driDrawPriv
,
433 __DRIdrawablePrivate
* driReadPriv
)
435 if (ctx
->stLostLock
) {
436 ctx
->stLostLock
= FALSE
;
437 if (driDrawPriv
== driReadPriv
)
438 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
440 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
446 * This ensures all contexts which bind to a drawable pick up the
447 * drawable change and signal new buffer state.
448 * Calling st_resize_framebuffer for each context may seem like overkill,
449 * but no new buffers will actually be allocated if the dimensions don't
454 dri1_propagate_drawable_change(struct dri_context
*ctx
)
456 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
457 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
458 boolean flushed
= FALSE
;
460 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
462 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
464 ctx
->d_stamp
= dPriv
->lastStamp
;
465 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
469 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
472 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
473 ctx
->r_stamp
= rPriv
->lastStamp
;
474 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
476 } else if (rPriv
&& dPriv
== rPriv
) {
478 ctx
->r_stamp
= ctx
->d_stamp
;
484 dri1_update_drawables(struct dri_context
*ctx
,
485 struct dri_drawable
*draw
, struct dri_drawable
*read
)
488 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
491 dri1_propagate_drawable_change(ctx
);
494 static INLINE boolean
495 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
498 const struct drm_clip_rect
*src
,
499 const struct drm_clip_rect
*bbox
)
504 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
505 (int)bbox
->x1
+ dst_x
;
506 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
507 (int)bbox
->x2
+ dst_x
;
508 if (xy1
>= xy2
|| xy1
< 0)
514 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
515 (int)bbox
->y1
+ dst_y
;
516 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
517 (int)bbox
->y2
+ dst_y
;
518 if (xy1
>= xy2
|| xy1
< 0)
527 dri1_swap_copy(struct dri_context
*ctx
,
528 struct pipe_surface
*dst
,
529 struct pipe_surface
*src
,
530 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
532 struct pipe_context
*pipe
= ctx
->pipe
;
533 struct drm_clip_rect clip
;
534 struct drm_clip_rect
*cur
;
537 cur
= dPriv
->pClipRects
;
539 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
540 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
541 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
543 (int)clip
.x1
- dPriv
->x
,
544 (int)clip
.y1
- dPriv
->y
,
545 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
550 dri1_copy_to_front(struct dri_context
*ctx
,
551 struct pipe_surface
*surf
,
552 __DRIdrawablePrivate
* dPriv
,
553 const struct drm_clip_rect
*sub_box
,
554 struct pipe_fence_handle
**fence
)
556 struct pipe_context
*pipe
= ctx
->pipe
;
557 boolean save_lost_lock
;
560 struct drm_clip_rect bbox
;
561 boolean visible
= TRUE
;
566 save_lost_lock
= ctx
->stLostLock
;
567 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
568 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
576 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
578 if (visible
&& __dri1_api_hooks
->present_locked
) {
580 __dri1_api_hooks
->present_locked(pipe
,
584 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
586 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
588 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
591 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
593 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
596 ctx
->stLostLock
= save_lost_lock
;
599 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
603 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
606 dri1_propagate_drawable_change(ctx
);
610 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
611 struct pipe_surface
*surf
, void *context_private
)
613 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
614 struct pipe_fence_handle
*dummy_fence
;
616 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
617 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
620 * FIXME: Do we need swap throttling here?
625 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
627 struct dri_context
*ctx
;
628 struct pipe_surface
*back_surf
;
629 struct dri_drawable
*draw
= dri_drawable(dPriv
);
630 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
631 struct pipe_fence_handle
*fence
;
632 struct st_context
*st
= st_get_current();
634 assert(__dri1_api_hooks
!= NULL
);
637 return; /* For now */
639 ctx
= (struct dri_context
*)st
->pipe
->priv
;
641 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
643 st_notify_swapbuffers(draw
->stfb
);
644 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
645 fence
= dri_swap_fences_pop_front(draw
);
647 (void)screen
->fence_finish(screen
, fence
, 0);
648 screen
->fence_reference(screen
, &fence
, NULL
);
650 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
651 dri_swap_fences_push_back(draw
, fence
);
652 screen
->fence_reference(screen
, &fence
, NULL
);
657 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
659 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
660 struct drm_clip_rect sub_bbox
;
661 struct dri_context
*ctx
;
662 struct pipe_surface
*back_surf
;
663 struct dri_drawable
*draw
= dri_drawable(dPriv
);
664 struct pipe_fence_handle
*dummy_fence
;
665 struct st_context
*st
= st_get_current();
667 assert(__dri1_api_hooks
!= NULL
);
672 ctx
= (struct dri_context
*)st
->pipe
->priv
;
679 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
681 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
682 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
683 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
687 /* vim: set sw=3 ts=8 sts=3 expandtab: */