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"
48 #include "util/u_rect.h"
50 static struct pipe_surface
*
51 dri_surface_from_handle(struct drm_api
*api
,
52 struct pipe_screen
*screen
,
54 enum pipe_format format
,
55 unsigned width
, unsigned height
, unsigned pitch
)
57 struct pipe_surface
*surface
= NULL
;
58 struct pipe_texture
*texture
= NULL
;
59 struct pipe_texture templat
;
61 memset(&templat
, 0, sizeof(templat
));
62 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
63 templat
.target
= PIPE_TEXTURE_2D
;
64 templat
.last_level
= 0;
66 templat
.format
= format
;
67 templat
.width
[0] = width
;
68 templat
.height
[0] = height
;
69 pf_get_block(templat
.format
, &templat
.block
);
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(__DRIdrawablePrivate
* dPriv
)
124 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
125 struct pipe_surface
*surface
= NULL
;
126 struct dri_screen
*st_screen
= dri_screen(drawable
->sPriv
);
127 struct pipe_screen
*screen
= st_screen
->pipe_screen
;
128 __DRIbuffer
*buffers
= NULL
;
129 __DRIscreen
*dri_screen
= drawable
->sPriv
;
130 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
131 struct drm_api
*api
= st_screen
->api
;
132 boolean have_depth
= FALSE
;
135 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
138 drawable
->attachments
,
140 num_attachments
, &count
,
144 if (buffers
== NULL
) {
148 /* set one cliprect to cover the whole dri_drawable */
151 dri_drawable
->backX
= 0;
152 dri_drawable
->backY
= 0;
153 dri_drawable
->numClipRects
= 1;
154 dri_drawable
->pClipRects
[0].x1
= 0;
155 dri_drawable
->pClipRects
[0].y1
= 0;
156 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
157 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
158 dri_drawable
->numBackClipRects
= 1;
159 dri_drawable
->pBackClipRects
[0].x1
= 0;
160 dri_drawable
->pBackClipRects
[0].y1
= 0;
161 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
162 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
164 if (drawable
->old_num
== count
&&
165 drawable
->old_w
== dri_drawable
->w
&&
166 drawable
->old_h
== dri_drawable
->h
&&
167 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
170 drawable
->old_num
= count
;
171 drawable
->old_w
= dri_drawable
->w
;
172 drawable
->old_h
= dri_drawable
->h
;
173 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
176 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
178 for (i
= 0; i
< count
; i
++) {
179 enum pipe_format format
= 0;
182 switch (buffers
[i
].attachment
) {
183 case __DRI_BUFFER_FRONT_LEFT
:
184 if (!st_screen
->auto_fake_front
)
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 case __DRI_BUFFER_DEPTH_STENCIL
:
196 case __DRI_BUFFER_STENCIL
:
197 index
= ST_SURFACE_DEPTH
;
198 format
= drawable
->depth_stencil_format
;
200 case __DRI_BUFFER_ACCUM
:
205 if (index
== ST_SURFACE_DEPTH
) {
212 surface
= dri_surface_from_handle(api
,
217 dri_drawable
->h
, buffers
[i
].pitch
);
219 switch (buffers
[i
].attachment
) {
220 case __DRI_BUFFER_FRONT_LEFT
:
221 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
222 case __DRI_BUFFER_BACK_LEFT
:
223 drawable
->color_format
= surface
->format
;
225 case __DRI_BUFFER_DEPTH
:
226 case __DRI_BUFFER_DEPTH_STENCIL
:
227 case __DRI_BUFFER_STENCIL
:
228 drawable
->depth_stencil_format
= surface
->format
;
230 case __DRI_BUFFER_ACCUM
:
235 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
236 pipe_surface_reference(&surface
, NULL
);
238 /* this needed, or else the state tracker fails to pick the new buffers */
239 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
243 * These are used for GLX_EXT_texture_from_pixmap
245 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
246 GLint format
, __DRIdrawable
*dPriv
)
248 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
249 struct pipe_surface
*ps
;
251 if (!drawable
->stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
252 struct gl_renderbuffer
*rb
=
253 st_new_renderbuffer_fb(drawable
->color_format
, 0 /*XXX*/, FALSE
);
254 _mesa_add_renderbuffer(&drawable
->stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
257 dri_get_buffers(drawable
->dPriv
);
258 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
263 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
264 ST_TEXTURE_RECT
, 0, drawable
->color_format
);
267 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
268 __DRIdrawable
*dPriv
)
270 dri2_set_tex_buffer2(pDRICtx
, target
, GLX_TEXTURE_FORMAT_RGBA_EXT
, dPriv
);
274 dri_flush_frontbuffer(struct pipe_screen
*screen
,
275 struct pipe_surface
*surf
, void *context_private
)
277 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
278 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
279 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
280 __DRIscreen
*dri_screen
= ctx
->sPriv
;
282 /* XXX Does this function get called with DRI1? */
284 if (ctx
->dPriv
== NULL
) {
285 debug_printf("%s: no drawable bound to context\n", __func__
);
290 /* TODO if rendering to pixmaps is slow enable this code. */
291 if (drawable
->is_pixmap
)
297 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
298 dri_drawable
->loaderPrivate
);
302 * This is called when we need to set up GL rendering to a new X window.
305 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
306 __DRIdrawablePrivate
* dPriv
,
307 const __GLcontextModes
* visual
, boolean isPixmap
)
309 struct dri_screen
*screen
= sPriv
->private;
310 struct dri_drawable
*drawable
= NULL
;
314 goto fail
; /* not implemented */
316 drawable
= CALLOC_STRUCT(dri_drawable
);
317 if (drawable
== NULL
)
320 if (visual
->redBits
== 8) {
321 if (visual
->alphaBits
== 8)
322 drawable
->color_format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
324 drawable
->color_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
326 drawable
->color_format
= PIPE_FORMAT_R5G6B5_UNORM
;
329 switch(visual
->depthBits
) {
332 drawable
->depth_stencil_format
= PIPE_FORMAT_NONE
;
335 drawable
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
338 if (visual
->stencilBits
== 0) {
339 drawable
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
340 PIPE_FORMAT_X8Z24_UNORM
:
341 PIPE_FORMAT_Z24X8_UNORM
;
343 drawable
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
344 PIPE_FORMAT_S8Z24_UNORM
:
345 PIPE_FORMAT_Z24S8_UNORM
;
349 drawable
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
353 drawable
->stfb
= st_create_framebuffer(visual
,
354 drawable
->color_format
,
355 drawable
->depth_stencil_format
,
356 drawable
->depth_stencil_format
,
358 dPriv
->h
, (void *)drawable
);
359 if (drawable
->stfb
== NULL
)
362 drawable
->sPriv
= sPriv
;
363 drawable
->dPriv
= dPriv
;
364 dPriv
->driverPrivate
= (void *)drawable
;
366 /* setup dri2 buffers information */
367 /* TODO incase of double buffer visual, delay fake creation */
369 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
370 if (!screen
->auto_fake_front
)
371 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
372 if (visual
->doubleBufferMode
)
373 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
374 if (visual
->depthBits
&& visual
->stencilBits
)
375 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
376 else if (visual
->depthBits
)
377 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
378 else if (visual
->stencilBits
)
379 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
380 drawable
->num_attachments
= i
;
382 drawable
->desired_fences
= 2;
390 static struct pipe_fence_handle
*
391 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
393 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
394 struct pipe_fence_handle
*fence
= NULL
;
396 if (draw
->cur_fences
>= draw
->desired_fences
) {
397 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
398 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
400 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
406 dri_swap_fences_push_back(struct dri_drawable
*draw
,
407 struct pipe_fence_handle
*fence
)
409 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
414 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
416 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
418 draw
->head
&= DRI_SWAP_FENCES_MASK
;
423 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
425 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
426 struct pipe_fence_handle
*fence
;
427 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
429 st_unreference_framebuffer(drawable
->stfb
);
430 drawable
->desired_fences
= 0;
431 while (drawable
->cur_fences
) {
432 fence
= dri_swap_fences_pop_front(drawable
);
433 screen
->fence_reference(screen
, &fence
, NULL
);
440 dri1_update_drawables_locked(struct dri_context
*ctx
,
441 __DRIdrawablePrivate
* driDrawPriv
,
442 __DRIdrawablePrivate
* driReadPriv
)
444 if (ctx
->stLostLock
) {
445 ctx
->stLostLock
= FALSE
;
446 if (driDrawPriv
== driReadPriv
)
447 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
449 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
455 * This ensures all contexts which bind to a drawable pick up the
456 * drawable change and signal new buffer state.
457 * Calling st_resize_framebuffer for each context may seem like overkill,
458 * but no new buffers will actually be allocated if the dimensions don't
463 dri1_propagate_drawable_change(struct dri_context
*ctx
)
465 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
466 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
467 boolean flushed
= FALSE
;
469 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
471 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
473 ctx
->d_stamp
= dPriv
->lastStamp
;
474 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
478 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
481 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
482 ctx
->r_stamp
= rPriv
->lastStamp
;
483 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
485 } else if (rPriv
&& dPriv
== rPriv
) {
487 ctx
->r_stamp
= ctx
->d_stamp
;
493 dri1_update_drawables(struct dri_context
*ctx
,
494 struct dri_drawable
*draw
, struct dri_drawable
*read
)
497 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
500 dri1_propagate_drawable_change(ctx
);
503 static INLINE boolean
504 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
507 const struct drm_clip_rect
*src
,
508 const struct drm_clip_rect
*bbox
)
513 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
514 (int)bbox
->x1
+ dst_x
;
515 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
516 (int)bbox
->x2
+ dst_x
;
517 if (xy1
>= xy2
|| xy1
< 0)
523 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
524 (int)bbox
->y1
+ dst_y
;
525 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
526 (int)bbox
->y2
+ dst_y
;
527 if (xy1
>= xy2
|| xy1
< 0)
536 dri1_swap_copy(struct dri_context
*ctx
,
537 struct pipe_surface
*dst
,
538 struct pipe_surface
*src
,
539 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
541 struct pipe_context
*pipe
= ctx
->pipe
;
542 struct drm_clip_rect clip
;
543 struct drm_clip_rect
*cur
;
546 cur
= dPriv
->pClipRects
;
548 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
549 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
)) {
550 if (pipe
->surface_copy
) {
551 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
553 (int)clip
.x1
- dPriv
->x
,
554 (int)clip
.y1
- dPriv
->y
,
555 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
557 util_surface_copy(pipe
, FALSE
, dst
, clip
.x1
, clip
.y1
,
559 (int)clip
.x1
- dPriv
->x
,
560 (int)clip
.y1
- dPriv
->y
,
561 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
568 dri1_copy_to_front(struct dri_context
*ctx
,
569 struct pipe_surface
*surf
,
570 __DRIdrawablePrivate
* dPriv
,
571 const struct drm_clip_rect
*sub_box
,
572 struct pipe_fence_handle
**fence
)
574 struct pipe_context
*pipe
= ctx
->pipe
;
575 boolean save_lost_lock
;
578 struct drm_clip_rect bbox
;
579 boolean visible
= TRUE
;
584 save_lost_lock
= ctx
->stLostLock
;
585 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
586 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
594 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
596 if (visible
&& __dri1_api_hooks
->present_locked
) {
598 __dri1_api_hooks
->present_locked(pipe
,
602 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
604 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
606 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
609 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
611 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
614 ctx
->stLostLock
= save_lost_lock
;
617 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
621 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
624 dri1_propagate_drawable_change(ctx
);
628 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
629 struct pipe_surface
*surf
, void *context_private
)
631 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
632 struct pipe_fence_handle
*dummy_fence
;
634 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
635 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
638 * FIXME: Do we need swap throttling here?
643 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
645 struct dri_context
*ctx
;
646 struct pipe_surface
*back_surf
;
647 struct dri_drawable
*draw
= dri_drawable(dPriv
);
648 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
649 struct pipe_fence_handle
*fence
;
650 struct st_context
*st
= st_get_current();
652 assert(__dri1_api_hooks
!= NULL
);
655 return; /* For now */
657 ctx
= (struct dri_context
*)st
->pipe
->priv
;
659 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
661 st_notify_swapbuffers(draw
->stfb
);
662 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
663 fence
= dri_swap_fences_pop_front(draw
);
665 (void)screen
->fence_finish(screen
, fence
, 0);
666 screen
->fence_reference(screen
, &fence
, NULL
);
668 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
669 dri_swap_fences_push_back(draw
, fence
);
670 screen
->fence_reference(screen
, &fence
, NULL
);
675 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
677 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
678 struct drm_clip_rect sub_bbox
;
679 struct dri_context
*ctx
;
680 struct pipe_surface
*back_surf
;
681 struct dri_drawable
*draw
= dri_drawable(dPriv
);
682 struct pipe_fence_handle
*dummy_fence
;
683 struct st_context
*st
= st_get_current();
685 assert(__dri1_api_hooks
!= NULL
);
690 ctx
= (struct dri_context
*)st
->pipe
->priv
;
697 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
699 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
700 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
701 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
705 /* vim: set sw=3 ts=8 sts=3 expandtab: */