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"
48 dri_copy_to_front(__DRIdrawablePrivate
* dPriv
,
49 struct pipe_surface
*from
,
50 int x
, int y
, unsigned w
, unsigned h
)
52 /* TODO send a message to the Xserver to copy to the real front buffer */
55 static struct pipe_surface
*
56 dri_surface_from_handle(struct pipe_screen
*screen
,
58 enum pipe_format format
,
59 unsigned width
, unsigned height
, unsigned pitch
)
61 struct pipe_surface
*surface
= NULL
;
62 struct pipe_texture
*texture
= NULL
;
63 struct pipe_texture templat
;
64 struct pipe_buffer
*buf
= NULL
;
66 buf
= drm_api_hooks
.buffer_from_handle(screen
, "dri2 buffer", handle
);
70 memset(&templat
, 0, sizeof(templat
));
71 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
72 templat
.target
= PIPE_TEXTURE_2D
;
73 templat
.last_level
= 0;
75 templat
.format
= format
;
76 templat
.width
[0] = width
;
77 templat
.height
[0] = height
;
78 pf_get_block(templat
.format
, &templat
.block
);
80 texture
= screen
->texture_blanket(screen
, &templat
, &pitch
, buf
);
82 /* we don't need the buffer from this point on */
83 pipe_buffer_reference(&buf
, NULL
);
88 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
89 PIPE_BUFFER_USAGE_GPU_READ
|
90 PIPE_BUFFER_USAGE_GPU_WRITE
);
92 /* we don't need the texture from this point on */
93 pipe_texture_reference(&texture
, NULL
);
98 * This will be called a drawable is known to have been resized.
101 dri_get_buffers(__DRIdrawablePrivate
* dPriv
)
103 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
104 struct pipe_surface
*surface
= NULL
;
105 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
106 __DRIbuffer
*buffers
= NULL
;
107 __DRIscreen
*dri_screen
= drawable
->sPriv
;
108 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
109 boolean have_depth
= FALSE
;
112 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
115 drawable
->attachments
,
117 num_attachments
, &count
,
121 if (buffers
== NULL
) {
125 /* set one cliprect to cover the whole dri_drawable */
128 dri_drawable
->backX
= 0;
129 dri_drawable
->backY
= 0;
130 dri_drawable
->numClipRects
= 1;
131 dri_drawable
->pClipRects
[0].x1
= 0;
132 dri_drawable
->pClipRects
[0].y1
= 0;
133 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
134 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
135 dri_drawable
->numBackClipRects
= 1;
136 dri_drawable
->pBackClipRects
[0].x1
= 0;
137 dri_drawable
->pBackClipRects
[0].y1
= 0;
138 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
139 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
141 if (drawable
->old_num
== count
&&
142 drawable
->old_w
== dri_drawable
->w
&&
143 drawable
->old_h
== dri_drawable
->h
&&
144 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
147 drawable
->old_num
= count
;
148 drawable
->old_w
= dri_drawable
->w
;
149 drawable
->old_h
= dri_drawable
->h
;
150 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
153 for (i
= 0; i
< count
; i
++) {
154 enum pipe_format format
= 0;
157 switch (buffers
[i
].attachment
) {
158 case __DRI_BUFFER_FRONT_LEFT
:
159 index
= ST_SURFACE_FRONT_LEFT
;
160 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
162 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
163 index
= ST_SURFACE_FRONT_LEFT
;
164 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
166 case __DRI_BUFFER_BACK_LEFT
:
167 index
= ST_SURFACE_BACK_LEFT
;
168 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
170 case __DRI_BUFFER_DEPTH
:
171 index
= ST_SURFACE_DEPTH
;
172 format
= PIPE_FORMAT_Z24S8_UNORM
;
174 case __DRI_BUFFER_STENCIL
:
175 index
= ST_SURFACE_DEPTH
;
176 format
= PIPE_FORMAT_Z24S8_UNORM
;
178 case __DRI_BUFFER_ACCUM
:
182 assert(buffers
[i
].cpp
== 4);
184 if (index
== ST_SURFACE_DEPTH
) {
191 surface
= dri_surface_from_handle(screen
,
195 dri_drawable
->h
, buffers
[i
].pitch
);
197 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
198 pipe_surface_reference(&surface
, NULL
);
200 /* this needed, or else the state tracker fails to pick the new buffers */
201 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
205 dri_flush_frontbuffer(struct pipe_screen
*screen
,
206 struct pipe_surface
*surf
, void *context_private
)
208 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
210 dri_copy_to_front(ctx
->dPriv
, surf
, 0, 0, surf
->width
, surf
->height
);
214 * This is called when we need to set up GL rendering to a new X window.
217 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
218 __DRIdrawablePrivate
* dPriv
,
219 const __GLcontextModes
* visual
, boolean isPixmap
)
221 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
222 struct dri_screen
*screen
= sPriv
->private;
223 struct dri_drawable
*drawable
= NULL
;
224 struct pipe_screen
*pscreen
= screen
->pipe_screen
;
228 goto fail
; /* not implemented */
230 drawable
= CALLOC_STRUCT(dri_drawable
);
231 if (drawable
== NULL
)
234 /* XXX: todo: use the pipe_screen queries to figure out which
235 * render targets are supportable.
237 assert(visual
->redBits
== 8);
238 assert(visual
->depthBits
== 24 || visual
->depthBits
== 0);
239 assert(visual
->stencilBits
== 8 || visual
->stencilBits
== 0);
241 colorFormat
= PIPE_FORMAT_A8R8G8B8_UNORM
;
243 if (visual
->depthBits
) {
244 if (pscreen
->is_format_supported(pscreen
, PIPE_FORMAT_Z24S8_UNORM
,
246 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
247 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
248 depthFormat
= PIPE_FORMAT_Z24S8_UNORM
;
250 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
252 depthFormat
= PIPE_FORMAT_NONE
;
254 if (visual
->stencilBits
) {
255 if (pscreen
->is_format_supported(pscreen
, PIPE_FORMAT_Z24S8_UNORM
,
257 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
258 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
259 stencilFormat
= PIPE_FORMAT_Z24S8_UNORM
;
261 stencilFormat
= PIPE_FORMAT_S8Z24_UNORM
;
263 stencilFormat
= PIPE_FORMAT_NONE
;
265 drawable
->stfb
= st_create_framebuffer(visual
,
270 dPriv
->h
, (void *)drawable
);
271 if (drawable
->stfb
== NULL
)
274 drawable
->sPriv
= sPriv
;
275 drawable
->dPriv
= dPriv
;
276 dPriv
->driverPrivate
= (void *)drawable
;
278 /* setup dri2 buffers information */
280 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
282 /* TODO incase of double buffer visual, delay fake creation */
283 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
285 if (visual
->doubleBufferMode
)
286 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
287 if (visual
->depthBits
)
288 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
289 if (visual
->stencilBits
)
290 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
291 drawable
->num_attachments
= i
;
293 drawable
->desired_fences
= 2;
301 static struct pipe_fence_handle
*
302 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
304 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
305 struct pipe_fence_handle
*fence
= NULL
;
307 if (draw
->cur_fences
>= draw
->desired_fences
) {
308 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
309 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
311 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
317 dri_swap_fences_push_back(struct dri_drawable
*draw
,
318 struct pipe_fence_handle
*fence
)
320 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
325 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
327 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
329 draw
->head
&= DRI_SWAP_FENCES_MASK
;
334 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
336 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
337 struct pipe_fence_handle
*fence
;
338 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
340 st_unreference_framebuffer(drawable
->stfb
);
341 drawable
->desired_fences
= 0;
342 while (drawable
->cur_fences
) {
343 fence
= dri_swap_fences_pop_front(drawable
);
344 screen
->fence_reference(screen
, &fence
, NULL
);
351 dri1_update_drawables_locked(struct dri_context
*ctx
,
352 __DRIdrawablePrivate
* driDrawPriv
,
353 __DRIdrawablePrivate
* driReadPriv
)
355 if (ctx
->stLostLock
) {
356 ctx
->stLostLock
= FALSE
;
357 if (driDrawPriv
== driReadPriv
)
358 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
360 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
366 * This ensures all contexts which bind to a drawable pick up the
367 * drawable change and signal new buffer state.
368 * Calling st_resize_framebuffer for each context may seem like overkill,
369 * but no new buffers will actually be allocated if the dimensions don't
374 dri1_propagate_drawable_change(struct dri_context
*ctx
)
376 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
377 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
378 boolean flushed
= FALSE
;
380 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
382 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
384 ctx
->d_stamp
= dPriv
->lastStamp
;
385 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
389 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
392 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
393 ctx
->r_stamp
= rPriv
->lastStamp
;
394 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
396 } else if (rPriv
&& dPriv
== rPriv
) {
398 ctx
->r_stamp
= ctx
->d_stamp
;
404 dri1_update_drawables(struct dri_context
*ctx
,
405 struct dri_drawable
*draw
, struct dri_drawable
*read
)
408 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
411 dri1_propagate_drawable_change(ctx
);
414 static INLINE boolean
415 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
418 const struct drm_clip_rect
*src
,
419 const struct drm_clip_rect
*bbox
)
424 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
425 (int)bbox
->x1
+ dst_x
;
426 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
427 (int)bbox
->x2
+ dst_x
;
428 if (xy1
>= xy2
|| xy1
< 0)
434 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
435 (int)bbox
->y1
+ dst_y
;
436 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
437 (int)bbox
->y2
+ dst_y
;
438 if (xy1
>= xy2
|| xy1
< 0)
447 dri1_swap_copy(struct dri_context
*ctx
,
448 struct pipe_surface
*dst
,
449 struct pipe_surface
*src
,
450 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
452 struct pipe_context
*pipe
= ctx
->pipe
;
453 struct drm_clip_rect clip
;
454 struct drm_clip_rect
*cur
;
457 cur
= dPriv
->pClipRects
;
459 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
460 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
461 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
463 (int)clip
.x1
- dPriv
->x
,
464 (int)clip
.y1
- dPriv
->y
,
465 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
470 dri1_copy_to_front(struct dri_context
*ctx
,
471 struct pipe_surface
*surf
,
472 __DRIdrawablePrivate
* dPriv
,
473 const struct drm_clip_rect
*sub_box
,
474 struct pipe_fence_handle
**fence
)
476 struct pipe_context
*pipe
= ctx
->pipe
;
477 boolean save_lost_lock
;
480 struct drm_clip_rect bbox
;
481 boolean visible
= TRUE
;
486 save_lost_lock
= ctx
->stLostLock
;
487 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
488 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
496 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
498 if (visible
&& __dri1_api_hooks
->present_locked
) {
500 __dri1_api_hooks
->present_locked(pipe
,
504 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
506 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
508 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
511 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
513 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
516 ctx
->stLostLock
= save_lost_lock
;
519 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
523 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
526 dri1_propagate_drawable_change(ctx
);
530 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
531 struct pipe_surface
*surf
, void *context_private
)
533 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
534 struct pipe_fence_handle
*dummy_fence
;
536 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
537 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
540 * FIXME: Do we need swap throttling here?
545 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
547 struct dri_context
*ctx
;
548 struct pipe_surface
*back_surf
;
549 struct dri_drawable
*draw
= dri_drawable(dPriv
);
550 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
551 struct pipe_fence_handle
*fence
;
552 struct st_context
*st
= st_get_current();
554 assert(__dri1_api_hooks
!= NULL
);
557 return; /* For now */
559 ctx
= (struct dri_context
*)st
->pipe
->priv
;
561 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
563 st_notify_swapbuffers(draw
->stfb
);
564 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
565 fence
= dri_swap_fences_pop_front(draw
);
567 (void)screen
->fence_finish(screen
, fence
, 0);
568 screen
->fence_reference(screen
, &fence
, NULL
);
570 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
571 dri_swap_fences_push_back(draw
, fence
);
572 screen
->fence_reference(screen
, &fence
, NULL
);
577 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
579 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
580 struct drm_clip_rect sub_bbox
;
581 struct dri_context
*ctx
;
582 struct pipe_surface
*back_surf
;
583 struct dri_drawable
*draw
= dri_drawable(dPriv
);
584 struct pipe_fence_handle
*dummy_fence
;
585 struct st_context
*st
= st_get_current();
587 assert(__dri1_api_hooks
!= NULL
);
592 ctx
= (struct dri_context
*)st
->pipe
->priv
;
599 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
601 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
602 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
603 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
607 /* vim: set sw=3 ts=8 sts=3 expandtab: */