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
;
60 memset(&templat
, 0, sizeof(templat
));
61 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
62 templat
.target
= PIPE_TEXTURE_2D
;
63 templat
.last_level
= 0;
65 templat
.format
= format
;
66 templat
.width
[0] = width
;
67 templat
.height
[0] = height
;
68 pf_get_block(templat
.format
, &templat
.block
);
70 texture
= api
->texture_from_shared_handle(api
, screen
, &templat
,
71 "dri2 buffer", pitch
, handle
);
74 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__
);
78 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
79 PIPE_BUFFER_USAGE_GPU_READ
|
80 PIPE_BUFFER_USAGE_GPU_WRITE
);
82 /* we don't need the texture from this point on */
83 pipe_texture_reference(&texture
, NULL
);
88 * Pixmaps have will have the same name of fake front and front.
91 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
93 boolean found
= FALSE
;
94 boolean is_pixmap
= FALSE
;
98 for (i
= 0; i
< count
; i
++) {
99 switch (buffers
[i
].attachment
) {
100 case __DRI_BUFFER_FRONT_LEFT
:
101 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
103 is_pixmap
= buffers
[i
].name
== name
;
105 name
= buffers
[i
].name
;
117 * This will be called a drawable is known to have been resized.
120 dri_get_buffers(__DRIdrawablePrivate
* dPriv
)
123 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
124 struct pipe_surface
*surface
= NULL
;
125 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
126 __DRIbuffer
*buffers
= NULL
;
127 __DRIscreen
*dri_screen
= drawable
->sPriv
;
128 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
129 struct drm_api
*api
= ((struct dri_screen
*)(dri_screen
->private))->api
;
130 boolean have_depth
= FALSE
;
133 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
136 drawable
->attachments
,
138 num_attachments
, &count
,
142 if (buffers
== NULL
) {
146 /* set one cliprect to cover the whole dri_drawable */
149 dri_drawable
->backX
= 0;
150 dri_drawable
->backY
= 0;
151 dri_drawable
->numClipRects
= 1;
152 dri_drawable
->pClipRects
[0].x1
= 0;
153 dri_drawable
->pClipRects
[0].y1
= 0;
154 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
155 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
156 dri_drawable
->numBackClipRects
= 1;
157 dri_drawable
->pBackClipRects
[0].x1
= 0;
158 dri_drawable
->pBackClipRects
[0].y1
= 0;
159 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
160 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
162 if (drawable
->old_num
== count
&&
163 drawable
->old_w
== dri_drawable
->w
&&
164 drawable
->old_h
== dri_drawable
->h
&&
165 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
168 drawable
->old_num
= count
;
169 drawable
->old_w
= dri_drawable
->w
;
170 drawable
->old_h
= dri_drawable
->h
;
171 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
174 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
176 for (i
= 0; i
< count
; i
++) {
177 enum pipe_format format
= 0;
180 switch (buffers
[i
].attachment
) {
181 case __DRI_BUFFER_FRONT_LEFT
:
182 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
183 index
= ST_SURFACE_FRONT_LEFT
;
184 format
= drawable
->color_format
;
186 case __DRI_BUFFER_BACK_LEFT
:
187 index
= ST_SURFACE_BACK_LEFT
;
188 format
= drawable
->color_format
;
190 case __DRI_BUFFER_DEPTH
:
191 case __DRI_BUFFER_DEPTH_STENCIL
:
192 case __DRI_BUFFER_STENCIL
:
193 index
= ST_SURFACE_DEPTH
;
194 format
= drawable
->depth_stencil_format
;
196 case __DRI_BUFFER_ACCUM
:
201 if (index
== ST_SURFACE_DEPTH
) {
208 surface
= dri_surface_from_handle(api
,
213 dri_drawable
->h
, buffers
[i
].pitch
);
215 switch (buffers
[i
].attachment
) {
216 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
217 case __DRI_BUFFER_BACK_LEFT
:
218 drawable
->color_format
= surface
->format
;
220 case __DRI_BUFFER_DEPTH
:
221 case __DRI_BUFFER_DEPTH_STENCIL
:
222 case __DRI_BUFFER_STENCIL
:
223 drawable
->depth_stencil_format
= surface
->format
;
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
:
253 ST_TEXTURE_RECT
, 0, drawable
->color_format
);
256 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
257 __DRIdrawable
*dPriv
)
259 dri2_set_tex_buffer2(pDRICtx
, target
, GLX_TEXTURE_FORMAT_RGBA_EXT
, dPriv
);
263 dri_flush_frontbuffer(struct pipe_screen
*screen
,
264 struct pipe_surface
*surf
, void *context_private
)
266 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
267 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
268 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
269 __DRIscreen
*dri_screen
= ctx
->sPriv
;
271 /* XXX Does this function get called with DRI1? */
273 if (ctx
->dPriv
== NULL
) {
274 debug_printf("%s: no drawable bound to context\n", __func__
);
279 /* TODO if rendering to pixmaps is slow enable this code. */
280 if (drawable
->is_pixmap
)
286 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
287 dri_drawable
->loaderPrivate
);
291 * This is called when we need to set up GL rendering to a new X window.
294 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
295 __DRIdrawablePrivate
* dPriv
,
296 const __GLcontextModes
* visual
, boolean isPixmap
)
298 struct dri_screen
*screen
= sPriv
->private;
299 struct dri_drawable
*drawable
= NULL
;
303 goto fail
; /* not implemented */
305 drawable
= CALLOC_STRUCT(dri_drawable
);
306 if (drawable
== NULL
)
309 if (visual
->redBits
== 8) {
310 if (visual
->alphaBits
== 8)
311 drawable
->color_format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
313 drawable
->color_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
315 drawable
->color_format
= PIPE_FORMAT_R5G6B5_UNORM
;
318 switch(visual
->depthBits
) {
321 drawable
->depth_stencil_format
= PIPE_FORMAT_NONE
;
324 drawable
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
327 if (visual
->stencilBits
== 0) {
328 drawable
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
329 PIPE_FORMAT_X8Z24_UNORM
:
330 PIPE_FORMAT_Z24X8_UNORM
;
332 drawable
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
333 PIPE_FORMAT_S8Z24_UNORM
:
334 PIPE_FORMAT_Z24S8_UNORM
;
338 drawable
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
342 drawable
->stfb
= st_create_framebuffer(visual
,
343 drawable
->color_format
,
344 drawable
->depth_stencil_format
,
345 drawable
->depth_stencil_format
,
347 dPriv
->h
, (void *)drawable
);
348 if (drawable
->stfb
== NULL
)
351 drawable
->sPriv
= sPriv
;
352 drawable
->dPriv
= dPriv
;
353 dPriv
->driverPrivate
= (void *)drawable
;
355 /* setup dri2 buffers information */
356 /* TODO incase of double buffer visual, delay fake creation */
358 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
360 if (visual
->doubleBufferMode
)
361 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
362 if (visual
->depthBits
&& visual
->stencilBits
)
363 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
364 else if (visual
->depthBits
)
365 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
366 else if (visual
->stencilBits
)
367 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
368 drawable
->num_attachments
= i
;
370 drawable
->desired_fences
= 2;
378 static struct pipe_fence_handle
*
379 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
381 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
382 struct pipe_fence_handle
*fence
= NULL
;
384 if (draw
->cur_fences
>= draw
->desired_fences
) {
385 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
386 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
388 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
394 dri_swap_fences_push_back(struct dri_drawable
*draw
,
395 struct pipe_fence_handle
*fence
)
397 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
402 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
404 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
406 draw
->head
&= DRI_SWAP_FENCES_MASK
;
411 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
413 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
414 struct pipe_fence_handle
*fence
;
415 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
417 st_unreference_framebuffer(drawable
->stfb
);
418 drawable
->desired_fences
= 0;
419 while (drawable
->cur_fences
) {
420 fence
= dri_swap_fences_pop_front(drawable
);
421 screen
->fence_reference(screen
, &fence
, NULL
);
428 dri1_update_drawables_locked(struct dri_context
*ctx
,
429 __DRIdrawablePrivate
* driDrawPriv
,
430 __DRIdrawablePrivate
* driReadPriv
)
432 if (ctx
->stLostLock
) {
433 ctx
->stLostLock
= FALSE
;
434 if (driDrawPriv
== driReadPriv
)
435 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
437 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
443 * This ensures all contexts which bind to a drawable pick up the
444 * drawable change and signal new buffer state.
445 * Calling st_resize_framebuffer for each context may seem like overkill,
446 * but no new buffers will actually be allocated if the dimensions don't
451 dri1_propagate_drawable_change(struct dri_context
*ctx
)
453 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
454 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
455 boolean flushed
= FALSE
;
457 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
459 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
461 ctx
->d_stamp
= dPriv
->lastStamp
;
462 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
466 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
469 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
470 ctx
->r_stamp
= rPriv
->lastStamp
;
471 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
473 } else if (rPriv
&& dPriv
== rPriv
) {
475 ctx
->r_stamp
= ctx
->d_stamp
;
481 dri1_update_drawables(struct dri_context
*ctx
,
482 struct dri_drawable
*draw
, struct dri_drawable
*read
)
485 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
488 dri1_propagate_drawable_change(ctx
);
491 static INLINE boolean
492 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
495 const struct drm_clip_rect
*src
,
496 const struct drm_clip_rect
*bbox
)
501 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
502 (int)bbox
->x1
+ dst_x
;
503 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
504 (int)bbox
->x2
+ dst_x
;
505 if (xy1
>= xy2
|| xy1
< 0)
511 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
512 (int)bbox
->y1
+ dst_y
;
513 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
514 (int)bbox
->y2
+ dst_y
;
515 if (xy1
>= xy2
|| xy1
< 0)
524 dri1_swap_copy(struct dri_context
*ctx
,
525 struct pipe_surface
*dst
,
526 struct pipe_surface
*src
,
527 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
529 struct pipe_context
*pipe
= ctx
->pipe
;
530 struct drm_clip_rect clip
;
531 struct drm_clip_rect
*cur
;
534 cur
= dPriv
->pClipRects
;
536 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
537 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
538 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
540 (int)clip
.x1
- dPriv
->x
,
541 (int)clip
.y1
- dPriv
->y
,
542 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
547 dri1_copy_to_front(struct dri_context
*ctx
,
548 struct pipe_surface
*surf
,
549 __DRIdrawablePrivate
* dPriv
,
550 const struct drm_clip_rect
*sub_box
,
551 struct pipe_fence_handle
**fence
)
553 struct pipe_context
*pipe
= ctx
->pipe
;
554 boolean save_lost_lock
;
557 struct drm_clip_rect bbox
;
558 boolean visible
= TRUE
;
563 save_lost_lock
= ctx
->stLostLock
;
564 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
565 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
573 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
575 if (visible
&& __dri1_api_hooks
->present_locked
) {
577 __dri1_api_hooks
->present_locked(pipe
,
581 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
583 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
585 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
588 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
590 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
593 ctx
->stLostLock
= save_lost_lock
;
596 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
600 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
603 dri1_propagate_drawable_change(ctx
);
607 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
608 struct pipe_surface
*surf
, void *context_private
)
610 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
611 struct pipe_fence_handle
*dummy_fence
;
613 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
614 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
617 * FIXME: Do we need swap throttling here?
622 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
624 struct dri_context
*ctx
;
625 struct pipe_surface
*back_surf
;
626 struct dri_drawable
*draw
= dri_drawable(dPriv
);
627 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
628 struct pipe_fence_handle
*fence
;
629 struct st_context
*st
= st_get_current();
631 assert(__dri1_api_hooks
!= NULL
);
634 return; /* For now */
636 ctx
= (struct dri_context
*)st
->pipe
->priv
;
638 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
640 st_notify_swapbuffers(draw
->stfb
);
641 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
642 fence
= dri_swap_fences_pop_front(draw
);
644 (void)screen
->fence_finish(screen
, fence
, 0);
645 screen
->fence_reference(screen
, &fence
, NULL
);
647 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
648 dri_swap_fences_push_back(draw
, fence
);
649 screen
->fence_reference(screen
, &fence
, NULL
);
654 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
656 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
657 struct drm_clip_rect sub_bbox
;
658 struct dri_context
*ctx
;
659 struct pipe_surface
*back_surf
;
660 struct dri_drawable
*draw
= dri_drawable(dPriv
);
661 struct pipe_fence_handle
*dummy_fence
;
662 struct st_context
*st
= st_get_current();
664 assert(__dri1_api_hooks
!= NULL
);
669 ctx
= (struct dri_context
*)st
->pipe
->priv
;
676 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
678 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
679 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
680 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
684 /* vim: set sw=3 ts=8 sts=3 expandtab: */