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_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_DEPTH_STENCIL
, 0))
257 stencilFormat
= PIPE_FORMAT_Z24S8_UNORM
;
259 stencilFormat
= PIPE_FORMAT_S8Z24_UNORM
;
261 stencilFormat
= PIPE_FORMAT_NONE
;
263 drawable
->stfb
= st_create_framebuffer(visual
,
268 dPriv
->h
, (void *)drawable
);
269 if (drawable
->stfb
== NULL
)
272 drawable
->sPriv
= sPriv
;
273 drawable
->dPriv
= dPriv
;
274 dPriv
->driverPrivate
= (void *)drawable
;
276 /* setup dri2 buffers information */
278 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
280 /* TODO incase of double buffer visual, delay fake creation */
281 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
283 if (visual
->doubleBufferMode
)
284 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
285 if (visual
->depthBits
)
286 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
287 if (visual
->stencilBits
)
288 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
289 drawable
->num_attachments
= i
;
291 drawable
->desired_fences
= 2;
299 static struct pipe_fence_handle
*
300 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
302 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
303 struct pipe_fence_handle
*fence
= NULL
;
305 if (draw
->cur_fences
>= draw
->desired_fences
) {
306 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
307 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
309 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
315 dri_swap_fences_push_back(struct dri_drawable
*draw
,
316 struct pipe_fence_handle
*fence
)
318 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
323 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
325 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
327 draw
->head
&= DRI_SWAP_FENCES_MASK
;
332 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
334 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
335 struct pipe_fence_handle
*fence
;
336 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
338 st_unreference_framebuffer(drawable
->stfb
);
339 drawable
->desired_fences
= 0;
340 while (drawable
->cur_fences
) {
341 fence
= dri_swap_fences_pop_front(drawable
);
342 screen
->fence_reference(screen
, &fence
, NULL
);
349 dri1_update_drawables_locked(struct dri_context
*ctx
,
350 __DRIdrawablePrivate
* driDrawPriv
,
351 __DRIdrawablePrivate
* driReadPriv
)
353 if (ctx
->stLostLock
) {
354 ctx
->stLostLock
= FALSE
;
355 if (driDrawPriv
== driReadPriv
)
356 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
358 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
364 * This ensures all contexts which bind to a drawable pick up the
365 * drawable change and signal 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 don'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
, struct dri_drawable
*read
)
406 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
409 dri1_propagate_drawable_change(ctx
);
412 static INLINE boolean
413 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
416 const struct drm_clip_rect
*src
,
417 const struct drm_clip_rect
*bbox
)
422 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
423 (int)bbox
->x1
+ dst_x
;
424 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
425 (int)bbox
->x2
+ dst_x
;
426 if (xy1
>= xy2
|| xy1
< 0)
432 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
433 (int)bbox
->y1
+ dst_y
;
434 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
435 (int)bbox
->y2
+ dst_y
;
436 if (xy1
>= xy2
|| xy1
< 0)
445 dri1_swap_copy(struct dri_context
*ctx
,
446 struct pipe_surface
*dst
,
447 struct pipe_surface
*src
,
448 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
450 struct pipe_context
*pipe
= ctx
->pipe
;
451 struct drm_clip_rect clip
;
452 struct drm_clip_rect
*cur
;
455 cur
= dPriv
->pClipRects
;
457 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
458 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
459 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
461 (int)clip
.x1
- dPriv
->x
,
462 (int)clip
.y1
- dPriv
->y
,
463 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
468 dri1_copy_to_front(struct dri_context
*ctx
,
469 struct pipe_surface
*surf
,
470 __DRIdrawablePrivate
* dPriv
,
471 const struct drm_clip_rect
*sub_box
,
472 struct pipe_fence_handle
**fence
)
474 struct pipe_context
*pipe
= ctx
->pipe
;
475 boolean save_lost_lock
;
478 struct drm_clip_rect bbox
;
479 boolean visible
= TRUE
;
484 save_lost_lock
= ctx
->stLostLock
;
485 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
486 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
494 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
496 if (visible
&& __dri1_api_hooks
->present_locked
) {
498 __dri1_api_hooks
->present_locked(pipe
,
502 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
504 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
506 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
509 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
511 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
514 ctx
->stLostLock
= save_lost_lock
;
517 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
521 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
524 dri1_propagate_drawable_change(ctx
);
528 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
529 struct pipe_surface
*surf
, void *context_private
)
531 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
532 struct pipe_fence_handle
*dummy_fence
;
534 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
535 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
538 * FIXME: Do we need swap throttling here?
543 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
545 struct dri_context
*ctx
;
546 struct pipe_surface
*back_surf
;
547 struct dri_drawable
*draw
= dri_drawable(dPriv
);
548 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
549 struct pipe_fence_handle
*fence
;
550 struct st_context
*st
= st_get_current();
552 assert(__dri1_api_hooks
!= NULL
);
555 return; /* For now */
557 ctx
= (struct dri_context
*)st
->pipe
->priv
;
559 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
561 st_notify_swapbuffers(draw
->stfb
);
562 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
563 fence
= dri_swap_fences_pop_front(draw
);
565 (void)screen
->fence_finish(screen
, fence
, 0);
566 screen
->fence_reference(screen
, &fence
, NULL
);
568 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
569 dri_swap_fences_push_back(draw
, fence
);
570 screen
->fence_reference(screen
, &fence
, NULL
);
575 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
577 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
578 struct drm_clip_rect sub_bbox
;
579 struct dri_context
*ctx
;
580 struct pipe_surface
*back_surf
;
581 struct dri_drawable
*draw
= dri_drawable(dPriv
);
582 struct pipe_fence_handle
*dummy_fence
;
583 struct st_context
*st
= st_get_current();
585 assert(__dri1_api_hooks
!= NULL
);
590 ctx
= (struct dri_context
*)st
->pipe
->priv
;
597 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
599 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
600 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
601 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
605 /* vim: set sw=3 ts=8 sts=3 expandtab: */