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
);
63 memset(&templat
, 0, sizeof(templat
));
64 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
65 templat
.target
= PIPE_TEXTURE_2D
;
66 templat
.last_level
= 0;
68 templat
.format
= format
;
69 templat
.width
[0] = width
;
70 templat
.height
[0] = height
;
71 pf_get_block(templat
.format
, &templat
.block
);
73 texture
= screen
->texture_blanket(screen
, &templat
, &pitch
, buf
);
75 /* we don't need the buffer from this point on */
76 pipe_buffer_reference(&buf
, NULL
);
81 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
82 PIPE_BUFFER_USAGE_GPU_READ
|
83 PIPE_BUFFER_USAGE_GPU_WRITE
);
85 /* we don't need the texture from this point on */
86 pipe_texture_reference(&texture
, NULL
);
91 * Pixmaps have will have the same name of fake front and front.
94 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
96 boolean found
= FALSE
;
97 boolean is_pixmap
= FALSE
;
101 for (i
= 0; i
< count
; i
++) {
102 switch (buffers
[i
].attachment
) {
103 case __DRI_BUFFER_FRONT_LEFT
:
104 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
106 is_pixmap
= buffers
[i
].name
== name
;
108 name
= buffers
[i
].name
;
120 * This will be called a drawable is known to have been resized.
123 dri_get_buffers(__DRIdrawablePrivate
* dPriv
)
126 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
127 struct pipe_surface
*surface
= NULL
;
128 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
129 __DRIbuffer
*buffers
= NULL
;
130 __DRIscreen
*dri_screen
= drawable
->sPriv
;
131 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
132 struct drm_api
*api
= ((struct dri_screen
*)(dri_screen
->private))->api
;
133 boolean have_depth
= FALSE
;
136 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
139 drawable
->attachments
,
141 num_attachments
, &count
,
145 if (buffers
== NULL
) {
149 /* set one cliprect to cover the whole dri_drawable */
152 dri_drawable
->backX
= 0;
153 dri_drawable
->backY
= 0;
154 dri_drawable
->numClipRects
= 1;
155 dri_drawable
->pClipRects
[0].x1
= 0;
156 dri_drawable
->pClipRects
[0].y1
= 0;
157 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
158 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
159 dri_drawable
->numBackClipRects
= 1;
160 dri_drawable
->pBackClipRects
[0].x1
= 0;
161 dri_drawable
->pBackClipRects
[0].y1
= 0;
162 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
163 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
165 if (drawable
->old_num
== count
&&
166 drawable
->old_w
== dri_drawable
->w
&&
167 drawable
->old_h
== dri_drawable
->h
&&
168 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
171 drawable
->old_num
= count
;
172 drawable
->old_w
= dri_drawable
->w
;
173 drawable
->old_h
= dri_drawable
->h
;
174 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
177 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
179 for (i
= 0; i
< count
; i
++) {
180 enum pipe_format format
= 0;
183 switch (buffers
[i
].attachment
) {
184 case __DRI_BUFFER_FRONT_LEFT
:
186 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
187 index
= ST_SURFACE_FRONT_LEFT
;
188 format
= drawable
->color_format
;
190 case __DRI_BUFFER_BACK_LEFT
:
191 index
= ST_SURFACE_BACK_LEFT
;
192 format
= drawable
->color_format
;
194 case __DRI_BUFFER_DEPTH
:
195 index
= ST_SURFACE_DEPTH
;
196 format
= drawable
->depth_format
;
198 case __DRI_BUFFER_STENCIL
:
199 index
= ST_SURFACE_DEPTH
;
200 format
= drawable
->stencil_format
;
202 case __DRI_BUFFER_ACCUM
:
207 if (index
== ST_SURFACE_DEPTH
) {
214 surface
= dri_surface_from_handle(api
,
219 dri_drawable
->h
, buffers
[i
].pitch
);
221 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
222 pipe_surface_reference(&surface
, NULL
);
224 /* this needed, or else the state tracker fails to pick the new buffers */
225 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
229 * These are used for GLX_EXT_texture_from_pixmap
231 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
232 GLint format
, __DRIdrawable
*dPriv
)
234 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
235 struct pipe_surface
*ps
;
237 dri_get_buffers(drawable
->dPriv
);
238 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
240 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
242 format
== GLX_TEXTURE_FORMAT_RGBA_EXT
?
243 PIPE_FORMAT_R8G8B8A8_UNORM
: PIPE_FORMAT_R8G8B8X8_UNORM
);
246 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
247 __DRIdrawable
*dPriv
)
249 dri2_set_tex_buffer2(pDRICtx
, target
, GLX_TEXTURE_FORMAT_RGBA_EXT
, dPriv
);
253 dri_flush_frontbuffer(struct pipe_screen
*screen
,
254 struct pipe_surface
*surf
, void *context_private
)
256 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
257 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
258 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
259 __DRIscreen
*dri_screen
= ctx
->sPriv
;
261 /* XXX Does this function get called with DRI1? */
263 if (ctx
->dPriv
== NULL
) {
264 debug_printf("%s: no drawable bound to context\n", __func__
);
269 /* TODO if rendering to pixmaps is slow enable this code. */
270 if (drawable
->is_pixmap
)
274 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
275 dri_drawable
->loaderPrivate
);
279 * This is called when we need to set up GL rendering to a new X window.
282 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
283 __DRIdrawablePrivate
* dPriv
,
284 const __GLcontextModes
* visual
, boolean isPixmap
)
286 struct dri_screen
*screen
= sPriv
->private;
287 struct dri_drawable
*drawable
= NULL
;
291 goto fail
; /* not implemented */
293 drawable
= CALLOC_STRUCT(dri_drawable
);
294 if (drawable
== NULL
)
297 drawable
->color_format
= (visual
->redBits
== 8) ?
298 PIPE_FORMAT_A8R8G8B8_UNORM
: PIPE_FORMAT_R5G6B5_UNORM
;
300 switch(visual
->depthBits
) {
303 drawable
->depth_format
= PIPE_FORMAT_NONE
;
306 drawable
->depth_format
= PIPE_FORMAT_Z16_UNORM
;
309 if (visual
->stencilBits
== 0) {
310 drawable
->depth_format
= (screen
->d_depth_bits_last
) ?
311 PIPE_FORMAT_X8Z24_UNORM
:
312 PIPE_FORMAT_Z24X8_UNORM
;
314 drawable
->depth_format
= (screen
->sd_depth_bits_last
) ?
315 PIPE_FORMAT_S8Z24_UNORM
:
316 PIPE_FORMAT_Z24S8_UNORM
;
321 switch(visual
->stencilBits
) {
324 drawable
->stencil_format
= PIPE_FORMAT_NONE
;
327 drawable
->stencil_format
= (screen
->sd_depth_bits_last
) ?
328 PIPE_FORMAT_S8Z24_UNORM
:
329 PIPE_FORMAT_Z24S8_UNORM
;
333 drawable
->stfb
= st_create_framebuffer(visual
,
334 drawable
->color_format
,
335 drawable
->depth_format
,
336 drawable
->stencil_format
,
338 dPriv
->h
, (void *)drawable
);
339 if (drawable
->stfb
== NULL
)
342 drawable
->sPriv
= sPriv
;
343 drawable
->dPriv
= dPriv
;
344 dPriv
->driverPrivate
= (void *)drawable
;
346 /* setup dri2 buffers information */
347 /* TODO incase of double buffer visual, delay fake creation */
349 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
350 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
352 if (visual
->doubleBufferMode
)
353 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
354 if (visual
->depthBits
)
355 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
356 if (visual
->stencilBits
)
357 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
358 drawable
->num_attachments
= i
;
360 drawable
->desired_fences
= 2;
368 static struct pipe_fence_handle
*
369 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
371 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
372 struct pipe_fence_handle
*fence
= NULL
;
374 if (draw
->cur_fences
>= draw
->desired_fences
) {
375 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
376 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
378 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
384 dri_swap_fences_push_back(struct dri_drawable
*draw
,
385 struct pipe_fence_handle
*fence
)
387 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
392 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
394 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
396 draw
->head
&= DRI_SWAP_FENCES_MASK
;
401 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
403 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
404 struct pipe_fence_handle
*fence
;
405 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
407 st_unreference_framebuffer(drawable
->stfb
);
408 drawable
->desired_fences
= 0;
409 while (drawable
->cur_fences
) {
410 fence
= dri_swap_fences_pop_front(drawable
);
411 screen
->fence_reference(screen
, &fence
, NULL
);
418 dri1_update_drawables_locked(struct dri_context
*ctx
,
419 __DRIdrawablePrivate
* driDrawPriv
,
420 __DRIdrawablePrivate
* driReadPriv
)
422 if (ctx
->stLostLock
) {
423 ctx
->stLostLock
= FALSE
;
424 if (driDrawPriv
== driReadPriv
)
425 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
427 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
433 * This ensures all contexts which bind to a drawable pick up the
434 * drawable change and signal new buffer state.
435 * Calling st_resize_framebuffer for each context may seem like overkill,
436 * but no new buffers will actually be allocated if the dimensions don't
441 dri1_propagate_drawable_change(struct dri_context
*ctx
)
443 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
444 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
445 boolean flushed
= FALSE
;
447 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
449 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
451 ctx
->d_stamp
= dPriv
->lastStamp
;
452 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
456 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
459 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
460 ctx
->r_stamp
= rPriv
->lastStamp
;
461 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
463 } else if (rPriv
&& dPriv
== rPriv
) {
465 ctx
->r_stamp
= ctx
->d_stamp
;
471 dri1_update_drawables(struct dri_context
*ctx
,
472 struct dri_drawable
*draw
, struct dri_drawable
*read
)
475 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
478 dri1_propagate_drawable_change(ctx
);
481 static INLINE boolean
482 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
485 const struct drm_clip_rect
*src
,
486 const struct drm_clip_rect
*bbox
)
491 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
492 (int)bbox
->x1
+ dst_x
;
493 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
494 (int)bbox
->x2
+ dst_x
;
495 if (xy1
>= xy2
|| xy1
< 0)
501 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
502 (int)bbox
->y1
+ dst_y
;
503 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
504 (int)bbox
->y2
+ dst_y
;
505 if (xy1
>= xy2
|| xy1
< 0)
514 dri1_swap_copy(struct dri_context
*ctx
,
515 struct pipe_surface
*dst
,
516 struct pipe_surface
*src
,
517 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
519 struct pipe_context
*pipe
= ctx
->pipe
;
520 struct drm_clip_rect clip
;
521 struct drm_clip_rect
*cur
;
524 cur
= dPriv
->pClipRects
;
526 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
527 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
528 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
530 (int)clip
.x1
- dPriv
->x
,
531 (int)clip
.y1
- dPriv
->y
,
532 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
537 dri1_copy_to_front(struct dri_context
*ctx
,
538 struct pipe_surface
*surf
,
539 __DRIdrawablePrivate
* dPriv
,
540 const struct drm_clip_rect
*sub_box
,
541 struct pipe_fence_handle
**fence
)
543 struct pipe_context
*pipe
= ctx
->pipe
;
544 boolean save_lost_lock
;
547 struct drm_clip_rect bbox
;
548 boolean visible
= TRUE
;
553 save_lost_lock
= ctx
->stLostLock
;
554 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
555 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
563 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
565 if (visible
&& __dri1_api_hooks
->present_locked
) {
567 __dri1_api_hooks
->present_locked(pipe
,
571 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
573 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
575 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
578 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
580 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
583 ctx
->stLostLock
= save_lost_lock
;
586 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
590 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
593 dri1_propagate_drawable_change(ctx
);
597 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
598 struct pipe_surface
*surf
, void *context_private
)
600 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
601 struct pipe_fence_handle
*dummy_fence
;
603 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
604 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
607 * FIXME: Do we need swap throttling here?
612 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
614 struct dri_context
*ctx
;
615 struct pipe_surface
*back_surf
;
616 struct dri_drawable
*draw
= dri_drawable(dPriv
);
617 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
618 struct pipe_fence_handle
*fence
;
619 struct st_context
*st
= st_get_current();
621 assert(__dri1_api_hooks
!= NULL
);
624 return; /* For now */
626 ctx
= (struct dri_context
*)st
->pipe
->priv
;
628 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
630 st_notify_swapbuffers(draw
->stfb
);
631 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
632 fence
= dri_swap_fences_pop_front(draw
);
634 (void)screen
->fence_finish(screen
, fence
, 0);
635 screen
->fence_reference(screen
, &fence
, NULL
);
637 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
638 dri_swap_fences_push_back(draw
, fence
);
639 screen
->fence_reference(screen
, &fence
, NULL
);
644 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
646 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
647 struct drm_clip_rect sub_bbox
;
648 struct dri_context
*ctx
;
649 struct pipe_surface
*back_surf
;
650 struct dri_drawable
*draw
= dri_drawable(dPriv
);
651 struct pipe_fence_handle
*dummy_fence
;
652 struct st_context
*st
= st_get_current();
654 assert(__dri1_api_hooks
!= NULL
);
659 ctx
= (struct dri_context
*)st
->pipe
->priv
;
666 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
668 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
669 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
670 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
674 /* vim: set sw=3 ts=8 sts=3 expandtab: */