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 "util/u_memory.h"
33 #include "util/u_rect.h"
34 #include "pipe/p_context.h"
35 #include "state_tracker/st_context.h"
36 #include "state_tracker/st_public.h"
37 #include "state_tracker/dri1_api.h"
39 #include "dri_screen.h"
40 #include "dri_context.h"
41 #include "dri_drawable.h"
45 dri1_lock(struct dri_context
*ctx
)
47 drm_context_t hw_context
= ctx
->cPriv
->hHWContext
;
50 DRM_CAS(ctx
->lock
, hw_context
, DRM_LOCK_HELD
| hw_context
, ret
);
52 drmGetLock(ctx
->sPriv
->fd
, hw_context
, 0);
53 ctx
->stLostLock
= TRUE
;
54 ctx
->wsLostLock
= TRUE
;
60 dri1_unlock(struct dri_context
*ctx
)
62 ctx
->isLocked
= FALSE
;
63 DRM_UNLOCK(ctx
->sPriv
->fd
, ctx
->lock
, ctx
->cPriv
->hHWContext
);
66 static struct pipe_fence_handle
*
67 dri_swap_fences_pop_front(struct dri_drawable
*draw
)
69 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
70 struct pipe_fence_handle
*fence
= NULL
;
72 if (draw
->cur_fences
>= draw
->desired_fences
) {
73 screen
->fence_reference(screen
, &fence
, draw
->swap_fences
[draw
->tail
]);
74 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->tail
++], NULL
);
76 draw
->tail
&= DRI_SWAP_FENCES_MASK
;
82 dri_swap_fences_push_back(struct dri_drawable
*draw
,
83 struct pipe_fence_handle
*fence
)
85 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
90 if (draw
->cur_fences
< DRI_SWAP_FENCES_MAX
) {
92 screen
->fence_reference(screen
, &draw
->swap_fences
[draw
->head
++],
94 draw
->head
&= DRI_SWAP_FENCES_MASK
;
99 dri1_swap_fences_clear(struct dri_drawable
*drawable
)
101 struct pipe_screen
*screen
= dri_screen(drawable
->sPriv
)->pipe_screen
;
102 struct pipe_fence_handle
*fence
;
104 while (drawable
->cur_fences
) {
105 fence
= dri_swap_fences_pop_front(drawable
);
106 screen
->fence_reference(screen
, &fence
, NULL
);
111 dri1_update_drawables_locked(struct dri_context
*ctx
,
112 __DRIdrawable
* driDrawPriv
,
113 __DRIdrawable
* driReadPriv
)
115 if (ctx
->stLostLock
) {
116 ctx
->stLostLock
= FALSE
;
117 if (driDrawPriv
== driReadPriv
)
118 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
120 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
126 * This ensures all contexts which bind to a drawable pick up the
127 * drawable change and signal new buffer state.
128 * Calling st_resize_framebuffer for each context may seem like overkill,
129 * but no new buffers will actually be allocated if the dimensions don't
134 dri1_propagate_drawable_change(struct dri_context
*ctx
)
136 __DRIdrawable
*dPriv
= ctx
->dPriv
;
137 __DRIdrawable
*rPriv
= ctx
->rPriv
;
138 boolean flushed
= FALSE
;
140 if (dPriv
&& ctx
->d_stamp
!= dPriv
->lastStamp
) {
142 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
144 ctx
->d_stamp
= dPriv
->lastStamp
;
145 st_resize_framebuffer(dri_drawable(dPriv
)->stfb
, dPriv
->w
, dPriv
->h
);
149 if (rPriv
&& dPriv
!= rPriv
&& ctx
->r_stamp
!= rPriv
->lastStamp
) {
152 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
153 ctx
->r_stamp
= rPriv
->lastStamp
;
154 st_resize_framebuffer(dri_drawable(rPriv
)->stfb
, rPriv
->w
, rPriv
->h
);
156 } else if (rPriv
&& dPriv
== rPriv
) {
158 ctx
->r_stamp
= ctx
->d_stamp
;
164 dri1_update_drawables(struct dri_context
*ctx
,
165 struct dri_drawable
*draw
, struct dri_drawable
*read
)
168 dri1_update_drawables_locked(ctx
, draw
->dPriv
, read
->dPriv
);
171 dri1_propagate_drawable_change(ctx
);
174 static INLINE boolean
175 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
178 const struct drm_clip_rect
*src
,
179 const struct drm_clip_rect
*bbox
)
184 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
185 (int)bbox
->x1
+ dst_x
;
186 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
187 (int)bbox
->x2
+ dst_x
;
188 if (xy1
>= xy2
|| xy1
< 0)
194 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
195 (int)bbox
->y1
+ dst_y
;
196 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
197 (int)bbox
->y2
+ dst_y
;
198 if (xy1
>= xy2
|| xy1
< 0)
207 dri1_swap_copy(struct dri_context
*ctx
,
208 struct pipe_surface
*dst
,
209 struct pipe_surface
*src
,
210 __DRIdrawable
* dPriv
, const struct drm_clip_rect
*bbox
)
212 struct pipe_context
*pipe
= ctx
->pipe
;
213 struct drm_clip_rect clip
;
214 struct drm_clip_rect
*cur
;
217 cur
= dPriv
->pClipRects
;
219 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
220 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
)) {
221 if (pipe
->surface_copy
) {
222 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
224 (int)clip
.x1
- dPriv
->x
,
225 (int)clip
.y1
- dPriv
->y
,
226 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
228 util_surface_copy(pipe
, FALSE
, dst
, clip
.x1
, clip
.y1
,
230 (int)clip
.x1
- dPriv
->x
,
231 (int)clip
.y1
- dPriv
->y
,
232 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
239 dri1_copy_to_front(struct dri_context
*ctx
,
240 struct pipe_surface
*surf
,
241 __DRIdrawable
* dPriv
,
242 const struct drm_clip_rect
*sub_box
,
243 struct pipe_fence_handle
**fence
)
245 struct pipe_context
*pipe
= ctx
->pipe
;
246 boolean save_lost_lock
;
249 struct drm_clip_rect bbox
;
250 boolean visible
= TRUE
;
255 save_lost_lock
= ctx
->stLostLock
;
256 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
257 st_get_framebuffer_dimensions(dri_drawable(dPriv
)->stfb
, &cur_w
, &cur_h
);
265 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
267 if (visible
&& __dri1_api_hooks
->present_locked
) {
269 __dri1_api_hooks
->present_locked(pipe
,
273 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
275 } else if (visible
&& __dri1_api_hooks
->front_srf_locked
) {
277 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
280 dri1_swap_copy(ctx
, front
, surf
, dPriv
, &bbox
);
282 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, fence
);
285 ctx
->stLostLock
= save_lost_lock
;
288 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
292 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
295 dri1_propagate_drawable_change(ctx
);
299 dri1_flush_frontbuffer(struct pipe_screen
*screen
,
300 struct pipe_surface
*surf
, void *context_private
)
302 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
303 struct pipe_fence_handle
*dummy_fence
;
305 dri1_copy_to_front(ctx
, surf
, ctx
->dPriv
, NULL
, &dummy_fence
);
306 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
309 * FIXME: Do we need swap throttling here?
314 dri1_swap_buffers(__DRIdrawable
* dPriv
)
316 struct dri_context
*ctx
;
317 struct pipe_surface
*back_surf
;
318 struct dri_drawable
*draw
= dri_drawable(dPriv
);
319 struct pipe_screen
*screen
= dri_screen(draw
->sPriv
)->pipe_screen
;
320 struct pipe_fence_handle
*fence
;
321 struct st_context
*st
= st_get_current();
323 assert(__dri1_api_hooks
!= NULL
);
326 return; /* For now */
328 ctx
= (struct dri_context
*)st
->pipe
->priv
;
330 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
332 st_notify_swapbuffers(draw
->stfb
);
333 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
334 fence
= dri_swap_fences_pop_front(draw
);
336 (void)screen
->fence_finish(screen
, fence
, 0);
337 screen
->fence_reference(screen
, &fence
, NULL
);
339 dri1_copy_to_front(ctx
, back_surf
, dPriv
, NULL
, &fence
);
340 dri_swap_fences_push_back(draw
, fence
);
341 screen
->fence_reference(screen
, &fence
, NULL
);
346 dri1_copy_sub_buffer(__DRIdrawable
* dPriv
, int x
, int y
, int w
, int h
)
348 struct pipe_screen
*screen
= dri_screen(dPriv
->driScreenPriv
)->pipe_screen
;
349 struct drm_clip_rect sub_bbox
;
350 struct dri_context
*ctx
;
351 struct pipe_surface
*back_surf
;
352 struct dri_drawable
*draw
= dri_drawable(dPriv
);
353 struct pipe_fence_handle
*dummy_fence
;
354 struct st_context
*st
= st_get_current();
356 assert(__dri1_api_hooks
!= NULL
);
361 ctx
= (struct dri_context
*)st
->pipe
->priv
;
368 st_get_framebuffer_surface(draw
->stfb
, ST_SURFACE_BACK_LEFT
, &back_surf
);
370 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
371 dri1_copy_to_front(ctx
, back_surf
, dPriv
, &sub_bbox
, &dummy_fence
);
372 screen
->fence_reference(screen
, &dummy_fence
, NULL
);
377 st_dri_lock(struct pipe_context
*pipe
)
379 dri1_lock((struct dri_context
*)pipe
->priv
);
383 st_dri_unlock(struct pipe_context
*pipe
)
385 dri1_unlock((struct dri_context
*)pipe
->priv
);
389 st_dri_is_locked(struct pipe_context
*pipe
)
391 return ((struct dri_context
*)pipe
->priv
)->isLocked
;
395 st_dri_lost_lock(struct pipe_context
*pipe
)
397 return ((struct dri_context
*)pipe
->priv
)->wsLostLock
;
401 st_dri_clear_lost_lock(struct pipe_context
*pipe
)
403 ((struct dri_context
*)pipe
->priv
)->wsLostLock
= FALSE
;
406 static struct dri1_api_lock_funcs dri1_lf
= {
408 .unlock
= st_dri_unlock
,
409 .is_locked
= st_dri_is_locked
,
410 .is_lock_lost
= st_dri_lost_lock
,
411 .clear_lost_lock
= st_dri_clear_lost_lock
414 static const __DRIextension
*dri1_screen_extensions
[] = {
415 &driReadDrawableExtension
,
416 &driCopySubBufferExtension
.base
,
417 &driSwapControlExtension
.base
,
418 &driFrameTrackingExtension
.base
,
419 &driMediaStreamCounterExtension
.base
,
423 struct dri1_api
*__dri1_api_hooks
= NULL
;
426 dri1_copy_version(struct dri1_api_version
*dst
,
427 const struct __DRIversionRec
*src
)
429 dst
->major
= src
->major
;
430 dst
->minor
= src
->minor
;
431 dst
->patch_level
= src
->patch
;
435 dri1_init_screen(__DRIscreen
* sPriv
)
437 struct dri_screen
*screen
;
438 const __DRIconfig
**configs
;
439 struct dri1_create_screen_arg arg
;
441 screen
= CALLOC_STRUCT(dri_screen
);
445 screen
->api
= drm_api_create();
446 screen
->sPriv
= sPriv
;
447 screen
->fd
= sPriv
->fd
;
448 screen
->drmLock
= (drmLock
*) & sPriv
->pSAREA
->lock
;
450 sPriv
->private = (void *)screen
;
451 sPriv
->extensions
= dri1_screen_extensions
;
453 arg
.base
.mode
= DRM_CREATE_DRI1
;
455 arg
.ddx_info
= sPriv
->pDevPriv
;
456 arg
.ddx_info_size
= sPriv
->devPrivSize
;
457 arg
.sarea
= sPriv
->pSAREA
;
458 dri1_copy_version(&arg
.ddx_version
, &sPriv
->ddx_version
);
459 dri1_copy_version(&arg
.dri_version
, &sPriv
->dri_version
);
460 dri1_copy_version(&arg
.drm_version
, &sPriv
->drm_version
);
463 screen
->pipe_screen
= screen
->api
->create_screen(screen
->api
, screen
->fd
, &arg
.base
);
465 if (!screen
->pipe_screen
|| !arg
.api
) {
466 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__
);
470 __dri1_api_hooks
= arg
.api
;
472 screen
->pipe_screen
->flush_frontbuffer
= dri1_flush_frontbuffer
;
473 driParseOptionInfo(&screen
->optionCache
,
474 __driConfigOptions
, __driNConfigOptions
);
477 * FIXME: If the driver supports format conversion swapbuffer blits, we might
478 * want to support other color bit depths than the server is currently
482 configs
= dri_fill_in_modes(screen
, sPriv
->fbBPP
);
488 screen
->pipe_screen
->destroy(screen
->pipe_screen
);