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 for (i
= 0; i
< count
; i
++) {
142 enum pipe_format format
= 0;
145 switch (buffers
[i
].attachment
) {
146 case __DRI_BUFFER_FRONT_LEFT
:
147 index
= ST_SURFACE_FRONT_LEFT
;
148 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
150 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
151 index
= ST_SURFACE_FRONT_LEFT
;
152 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
154 case __DRI_BUFFER_BACK_LEFT
:
155 index
= ST_SURFACE_BACK_LEFT
;
156 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
158 case __DRI_BUFFER_DEPTH
:
159 index
= ST_SURFACE_DEPTH
;
160 format
= PIPE_FORMAT_Z24S8_UNORM
;
162 case __DRI_BUFFER_STENCIL
:
163 index
= ST_SURFACE_DEPTH
;
164 format
= PIPE_FORMAT_Z24S8_UNORM
;
166 case __DRI_BUFFER_ACCUM
:
170 assert(buffers
[i
].cpp
== 4);
172 if (index
== ST_SURFACE_DEPTH
) {
179 surface
= dri_surface_from_handle(screen
,
183 dri_drawable
->h
, buffers
[i
].pitch
);
185 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
186 pipe_surface_reference(&surface
, NULL
);
188 /* this needed, or else the state tracker fails to pick the new buffers */
189 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
193 dri_flush_frontbuffer(struct pipe_screen
*screen
,
194 struct pipe_surface
*surf
, void *context_private
)
196 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
198 dri_copy_to_front(ctx
->dPriv
, surf
, 0, 0, surf
->width
, surf
->height
);
202 * This is called when we need to set up GL rendering to a new X window.
205 dri_create_buffer(__DRIscreenPrivate
* sPriv
,
206 __DRIdrawablePrivate
* dPriv
,
207 const __GLcontextModes
* visual
, boolean isPixmap
)
209 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
210 struct dri_screen
*screen
= sPriv
->private;
211 struct dri_drawable
*drawable
= NULL
;
212 struct pipe_screen
*pscreen
= screen
->pipe_screen
;
216 goto fail
; /* not implemented */
218 drawable
= CALLOC_STRUCT(dri_drawable
);
219 if (drawable
== NULL
)
222 /* XXX: todo: use the pipe_screen queries to figure out which
223 * render targets are supportable.
225 assert(visual
->redBits
== 8);
226 assert(visual
->depthBits
== 24 || visual
->depthBits
== 0);
227 assert(visual
->stencilBits
== 8 || visual
->stencilBits
== 0);
229 colorFormat
= PIPE_FORMAT_A8R8G8B8_UNORM
;
231 if (visual
->depthBits
) {
232 if (pscreen
->is_format_supported(pscreen
, PIPE_FORMAT_Z24S8_UNORM
,
234 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
235 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
236 depthFormat
= PIPE_FORMAT_Z24S8_UNORM
;
238 depthFormat
= PIPE_FORMAT_S8Z24_UNORM
;
240 depthFormat
= PIPE_FORMAT_NONE
;
242 if (visual
->stencilBits
) {
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 stencilFormat
= PIPE_FORMAT_Z24S8_UNORM
;
249 stencilFormat
= PIPE_FORMAT_S8Z24_UNORM
;
251 stencilFormat
= PIPE_FORMAT_NONE
;
253 drawable
->stfb
= st_create_framebuffer(visual
,
258 dPriv
->h
, (void *)drawable
);
259 if (drawable
->stfb
== NULL
)
262 drawable
->sPriv
= sPriv
;
263 drawable
->dPriv
= dPriv
;
264 dPriv
->driverPrivate
= (void *)drawable
;
266 /* setup dri2 buffers information */
268 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
270 /* TODO incase of double buffer visual, delay fake creation */
271 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
273 if (visual
->doubleBufferMode
)
274 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
275 if (visual
->depthBits
)
276 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
277 if (visual
->stencilBits
)
278 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
279 drawable
->num_attachments
= i
;
281 drawable
->desired_fences
= 2;
289 static struct pipe_fence_handle
*
290 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
292 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
293 struct pipe_fence_handle
*fence
= NULL
;
295 if (draw
->cur_fences
>= draw
->desired_fences
) {
296 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
297 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
299 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
305 dri_swap_fences_push_back(struct dri_drawable
*draw
,
306 struct pipe_fence_handle
*fence
)
308 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
313 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
315 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
317 draw
->head
&= DRI_SWAP_FENCES_MASK
;
322 dri_destroy_buffer(__DRIdrawablePrivate
* dPriv
)
324 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
325 struct pipe_fence_handle
*fence
;
326 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
328 st_unreference_framebuffer(drawable
->stfb
);
329 drawable
->desired_fences
= 0;
330 while (drawable
->cur_fences
) {
331 fence
= dri_swap_fences_pop_front(drawable
);
332 screen
->fence_reference(screen
, &fence
, NULL
);
339 dri1_update_drawables_locked(struct dri_context
*ctx
,
340 __DRIdrawablePrivate
* driDrawPriv
,
341 __DRIdrawablePrivate
* driReadPriv
)
343 if (ctx
->stLostLock
) {
344 ctx
->stLostLock
= FALSE
;
345 if (driDrawPriv
== driReadPriv
)
346 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
348 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
354 * This ensures all contexts which bind to a drawable pick up the
355 * drawable change and signal new buffer state.
356 * Calling st_resize_framebuffer for each context may seem like overkill,
357 * but no new buffers will actually be allocated if the dimensions don't
362 dri1_propagate_drawable_change(struct dri_context
*ctx
)
364 __DRIdrawablePrivate
*dPriv
= ctx
->dPriv
;
365 __DRIdrawablePrivate
*rPriv
= ctx
->rPriv
;
366 boolean flushed
= FALSE
;
368 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
370 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
372 ctx
->d_stamp
= dPriv
->lastStamp
;
373 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
377 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
380 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
381 ctx
->r_stamp
= rPriv
->lastStamp
;
382 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
384 } else if (rPriv
&& dPriv
== rPriv
) {
386 ctx
->r_stamp
= ctx
->d_stamp
;
392 dri1_update_drawables(struct dri_context
*ctx
,
393 struct dri_drawable
*draw
, struct dri_drawable
*read
)
396 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
399 dri1_propagate_drawable_change(ctx
);
402 static INLINE boolean
403 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
406 const struct drm_clip_rect
*src
,
407 const struct drm_clip_rect
*bbox
)
412 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
413 (int)bbox
->x1
+ dst_x
;
414 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
415 (int)bbox
->x2
+ dst_x
;
416 if (xy1
>= xy2
|| xy1
< 0)
422 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
423 (int)bbox
->y1
+ dst_y
;
424 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
425 (int)bbox
->y2
+ dst_y
;
426 if (xy1
>= xy2
|| xy1
< 0)
435 dri1_swap_copy(struct dri_context
*ctx
,
436 struct pipe_surface
*dst
,
437 struct pipe_surface
*src
,
438 __DRIdrawablePrivate
* dPriv
, const struct drm_clip_rect
*bbox
)
440 struct pipe_context
*pipe
= ctx
->pipe
;
441 struct drm_clip_rect clip
;
442 struct drm_clip_rect
*cur
;
445 cur
= dPriv
->pClipRects
;
447 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
448 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
))
449 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
451 (int)clip
.x1
- dPriv
->x
,
452 (int)clip
.y1
- dPriv
->y
,
453 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
458 dri1_copy_to_front(struct dri_context
*ctx
,
459 struct pipe_surface
*surf
,
460 __DRIdrawablePrivate
* dPriv
,
461 const struct drm_clip_rect
*sub_box
,
462 struct pipe_fence_handle
**fence
)
464 struct pipe_context
*pipe
= ctx
->pipe
;
465 boolean save_lost_lock
;
468 struct drm_clip_rect bbox
;
469 boolean visible
= TRUE
;
474 save_lost_lock
= ctx
->stLostLock
;
475 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
476 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
484 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
486 if (visible
&& __dri1_api_hooks
->present_locked
) {
488 __dri1_api_hooks
->present_locked(pipe
,
492 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
494 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
496 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
499 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
501 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
504 ctx
->stLostLock
= save_lost_lock
;
507 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
511 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
514 dri1_propagate_drawable_change(ctx
);
518 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
519 struct pipe_surface
*surf
, void *context_private
)
521 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
522 struct pipe_fence_handle
*dummy_fence
;
524 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
525 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
528 * FIXME: Do we need swap throttling here?
533 dri_swap_buffers(__DRIdrawablePrivate
* dPriv
)
535 struct dri_context
*ctx
;
536 struct pipe_surface
*back_surf
;
537 struct dri_drawable
*draw
= dri_drawable(dPriv
);
538 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
539 struct pipe_fence_handle
*fence
;
540 struct st_context
*st
= st_get_current();
542 assert(__dri1_api_hooks
!= NULL
);
545 return; /* For now */
547 ctx
= (struct dri_context
*)st
->pipe
->priv
;
549 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
551 st_notify_swapbuffers(draw
->stfb
);
552 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
553 fence
= dri_swap_fences_pop_front(draw
);
555 (void)screen
->fence_finish(screen
, fence
, 0);
556 screen
->fence_reference(screen
, &fence
, NULL
);
558 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
559 dri_swap_fences_push_back(draw
, fence
);
560 screen
->fence_reference(screen
, &fence
, NULL
);
565 dri_copy_sub_buffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
567 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
568 struct drm_clip_rect sub_bbox
;
569 struct dri_context
*ctx
;
570 struct pipe_surface
*back_surf
;
571 struct dri_drawable
*draw
= dri_drawable(dPriv
);
572 struct pipe_fence_handle
*dummy_fence
;
573 struct st_context
*st
= st_get_current();
575 assert(__dri1_api_hooks
!= NULL
);
580 ctx
= (struct dri_context
*)st
->pipe
->priv
;
587 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
589 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
590 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
591 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
595 /* vim: set sw=3 ts=8 sts=3 expandtab: */