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"
49 dri_copy_to_front(__DRIdrawablePrivate
*dPriv
,
50 struct pipe_surface
*from
,
51 int x
, int y
, unsigned w
, unsigned h
)
53 /* TODO send a message to the Xserver to copy to the real front buffer */
57 static struct pipe_surface
*
58 dri_surface_from_handle(struct pipe_screen
*screen
,
60 enum pipe_format format
,
65 struct pipe_surface
*surface
= NULL
;
66 struct pipe_texture
*texture
= NULL
;
67 struct pipe_texture templat
;
68 struct pipe_buffer
*buf
= NULL
;
70 buf
= drm_api_hooks
.buffer_from_handle(screen
, "dri2 buffer", handle
);
74 memset(&templat
, 0, sizeof(templat
));
75 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
76 templat
.target
= PIPE_TEXTURE_2D
;
77 templat
.last_level
= 0;
79 templat
.format
= format
;
80 templat
.width
[0] = width
;
81 templat
.height
[0] = height
;
82 pf_get_block(templat
.format
, &templat
.block
);
84 texture
= screen
->texture_blanket(screen
,
89 /* we don't need the buffer from this point on */
90 pipe_buffer_reference(&buf
, NULL
);
95 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
96 PIPE_BUFFER_USAGE_GPU_READ
|
97 PIPE_BUFFER_USAGE_GPU_WRITE
);
99 /* we don't need the texture from this point on */
100 pipe_texture_reference(&texture
, NULL
);
106 * This will be called a drawable is known to have been resized.
109 dri_get_buffers(__DRIdrawablePrivate
*dPriv
)
111 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
112 struct pipe_surface
*surface
= NULL
;
113 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
114 __DRIbuffer
*buffers
= NULL
;
115 __DRIscreen
*dri_screen
= drawable
->sPriv
;
116 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
117 boolean have_depth
= FALSE
;
120 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
)(dri_drawable
,
123 drawable
->attachments
,
124 drawable
->num_attachments
,
126 dri_drawable
->loaderPrivate
);
128 if (buffers
== NULL
) {
132 /* set one cliprect to cover the whole dri_drawable */
135 dri_drawable
->backX
= 0;
136 dri_drawable
->backY
= 0;
137 dri_drawable
->numClipRects
= 1;
138 dri_drawable
->pClipRects
[0].x1
= 0;
139 dri_drawable
->pClipRects
[0].y1
= 0;
140 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
141 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
142 dri_drawable
->numBackClipRects
= 1;
143 dri_drawable
->pBackClipRects
[0].x1
= 0;
144 dri_drawable
->pBackClipRects
[0].y1
= 0;
145 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
146 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
148 for (i
= 0; i
< count
; i
++) {
149 enum pipe_format format
= 0;
152 switch (buffers
[i
].attachment
) {
153 case __DRI_BUFFER_FRONT_LEFT
:
154 index
= ST_SURFACE_FRONT_LEFT
;
155 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
157 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
158 index
= ST_SURFACE_FRONT_LEFT
;
159 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
161 case __DRI_BUFFER_BACK_LEFT
:
162 index
= ST_SURFACE_BACK_LEFT
;
163 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
165 case __DRI_BUFFER_DEPTH
:
166 index
= ST_SURFACE_DEPTH
;
167 format
= PIPE_FORMAT_Z24S8_UNORM
;
169 case __DRI_BUFFER_STENCIL
:
170 index
= ST_SURFACE_DEPTH
;
171 format
= PIPE_FORMAT_Z24S8_UNORM
;
173 case __DRI_BUFFER_ACCUM
:
177 assert(buffers
[i
].cpp
== 4);
179 if (index
== ST_SURFACE_DEPTH
) {
186 surface
= dri_surface_from_handle(screen
,
193 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
194 pipe_surface_reference(&surface
, NULL
);
196 /* this needed, or else the state tracker fails to pick the new buffers */
197 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
202 dri_flush_frontbuffer(struct pipe_screen
*screen
,
203 struct pipe_surface
*surf
,
204 void *context_private
)
206 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
207 dri_copy_to_front(ctx
->dPriv
, surf
, 0, 0, surf
->width
, surf
->height
);
212 * This is called when we need to set up GL rendering to a new X window.
215 dri_create_buffer(__DRIscreenPrivate
*sPriv
,
216 __DRIdrawablePrivate
*dPriv
,
217 const __GLcontextModes
*visual
,
220 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
221 struct dri_screen
*screen
= sPriv
->private;
222 struct dri_drawable
*drawable
= NULL
;
223 struct pipe_screen
*pscreen
= screen
->pipe_screen
;
227 goto fail
; /* not implemented */
229 drawable
= CALLOC_STRUCT(dri_drawable
);
230 if (drawable
== NULL
)
233 /* XXX: todo: use the pipe_screen queries to figure out which
234 * render targets are supportable.
236 assert(visual
->redBits
== 8);
237 assert(visual
->depthBits
== 24 || visual
->depthBits
== 0);
238 assert(visual
->stencilBits
== 8 || visual
->stencilBits
== 0);
240 colorFormat
= PIPE_FORMAT_A8R8G8B8_UNORM
;
242 if (visual
->depthBits
) {
243 if (pscreen
->is_format_supported(pscreen
, PIPE_FORMAT_Z24S8_UNORM
,
245 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
246 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
247 depthFormat
= PIPE_FORMAT_Z24S8_UNORM
;
249 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
251 depthFormat
= PIPE_FORMAT_NONE
;
253 if (visual
->stencilBits
) {
254 if (pscreen
->is_format_supported(pscreen
, PIPE_FORMAT_Z24S8_UNORM
,
256 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
257 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
258 stencilFormat
= PIPE_FORMAT_Z24S8_UNORM
;
260 stencilFormat
= PIPE_FORMAT_S8Z24_UNORM
;
262 stencilFormat
= PIPE_FORMAT_NONE
;
264 drawable
->stfb
= st_create_framebuffer(visual
,
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
++], fence
);
328 draw
->head
&= DRI_SWAP_FENCES_MASK
;
333 dri_destroy_buffer(__DRIdrawablePrivate
*dPriv
)
335 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
336 struct pipe_fence_handle
*fence
;
337 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
339 st_unreference_framebuffer(drawable
->stfb
);
340 drawable
->desired_fences
= 0;
341 while(drawable
->cur_fences
) {
342 fence
= dri_swap_fences_pop_front(drawable
);
343 screen
->fence_reference(screen
, &fence
, NULL
);
350 dri1_update_drawables_locked(struct dri_context
*ctx
,
351 __DRIdrawablePrivate
*driDrawPriv
,
352 __DRIdrawablePrivate
*driReadPriv
)
354 if (ctx
->stLostLock
) {
355 ctx
->stLostLock
= FALSE
;
356 if (driDrawPriv
== driReadPriv
)
357 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
359 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
, driReadPriv
);
364 * This ensures all contexts which binds to a drawable picks up the
365 * drawable change and signals new buffer state.
366 * Calling st_resize_framebuffer for each context may seem like overkill,
367 * but no new buffers will actually be allocated if the dimensions doesn't
372 dri1_propagate_drawable_change(struct dri_context
*ctx
)
374 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
375 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
376 boolean flushed
= FALSE
;
378 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
380 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
382 ctx
->d_stamp
= dPriv
->lastStamp
;
383 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
387 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
390 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
391 ctx
->r_stamp
= rPriv
->lastStamp
;
392 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
394 } else if (rPriv
&& dPriv
== rPriv
) {
396 ctx
->r_stamp
= ctx
->d_stamp
;
402 dri1_update_drawables(struct dri_context
*ctx
,
403 struct dri_drawable
*draw
,
404 struct dri_drawable
*read
)
407 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
410 dri1_propagate_drawable_change(ctx
);
413 static INLINE boolean
414 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
417 const struct drm_clip_rect
*src
,
418 const struct drm_clip_rect
*bbox
)
423 xy1
= ((int) src
->x1
> (int) bbox
->x1
+ dst_x
) ? src
->x1
:
424 (int) bbox
->x1
+ dst_x
;
425 xy2
= ((int) src
->x2
< (int) bbox
->x2
+ dst_x
) ? src
->x2
:
426 (int) bbox
->x2
+ dst_x
;
427 if (xy1
>= xy2
|| xy1
< 0)
433 xy1
= ((int) src
->y1
> (int) bbox
->y1
+ dst_x
) ? src
->y1
:
434 (int) bbox
->y1
+ dst_x
;
435 xy2
= ((int) src
->y2
< (int) bbox
->y2
+ dst_x
) ? src
->y2
:
436 (int) bbox
->y2
+ dst_x
;
437 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
,
451 const struct drm_clip_rect
*bbox
)
453 struct pipe_context
*pipe
= ctx
->pipe
;
454 struct drm_clip_rect clip
;
455 struct drm_clip_rect
*cur
;
458 cur
= dPriv
->pClipRects
;
460 for (i
=0; i
<dPriv
->numClipRects
; ++i
) {
461 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
462 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
464 (int) clip
.x1
- dPriv
->x
,
465 (int) clip
.y1
- dPriv
->y
,
472 dri1_copy_to_front(struct dri_context
*ctx
,
473 struct pipe_surface
*surf
,
474 __DRIdrawablePrivate
*dPriv
,
475 const struct drm_clip_rect
*sub_box
,
476 struct pipe_fence_handle
**fence
)
478 struct pipe_context
*pipe
= ctx
->pipe
;
479 boolean save_lost_lock
;
482 struct drm_clip_rect bbox
;
483 boolean visible
= TRUE
;
488 save_lost_lock
= ctx
->stLostLock
;
489 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
490 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
498 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
500 if (visible
&& __dri1_api_hooks
->present_locked
) {
502 __dri1_api_hooks
->present_locked(pipe
,
511 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
513 struct pipe_surface
*front
=
514 __dri1_api_hooks
->front_srf_locked(pipe
);
517 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
519 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
522 ctx
->stLostLock
= save_lost_lock
;
525 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
529 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
532 dri1_propagate_drawable_change(ctx
);
536 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
537 struct pipe_surface
*surf
,
538 void *context_private
)
540 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
541 struct pipe_fence_handle
*dummy_fence
;
543 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
546 * FIXME: Do we need swap throttling here?
551 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
553 struct dri_context
*ctx
;
554 struct pipe_surface
*back_surf
;
555 struct dri_drawable
*draw
= dri_drawable(dPriv
);
556 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
557 struct pipe_fence_handle
*fence
;
558 GET_CURRENT_CONTEXT(glCtx
);
560 assert(__dri1_api_hooks
!= NULL
);
563 return; /* For now */
565 ctx
= (struct dri_context
*) glCtx
->st
->pipe
->priv
;
567 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
569 st_notify_swapbuffers(draw
->stfb
);
570 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
571 fence
= dri_swap_fences_pop_front(draw
);
573 (void) screen
->fence_finish(screen
, fence
, 0);
574 screen
->fence_reference(screen
, &fence
, NULL
);
576 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
577 dri_swap_fences_push_back(draw
, fence
);
578 screen
->fence_reference(screen
, &fence
, NULL
);
583 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
585 struct drm_clip_rect sub_bbox
;
586 struct dri_context
*ctx
;
587 struct pipe_surface
*back_surf
;
588 struct dri_drawable
*draw
= dri_drawable(dPriv
);
589 struct pipe_fence_handle
*dummy_fence
;
590 GET_CURRENT_CONTEXT(glCtx
);
592 assert(__dri1_api_hooks
!= NULL
);
597 ctx
= (struct dri_context
*) glCtx
->st
->pipe
->priv
;
604 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
606 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
607 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
611 /* vim: set sw=3 ts=8 sts=3 expandtab: */