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 /* XXX DRI1 is untested after the switch to st_api.h */
34 #include "util/u_memory.h"
35 #include "util/u_rect.h"
36 #include "util/u_inlines.h"
37 #include "pipe/p_context.h"
38 #include "state_tracker/dri1_api.h"
40 #include "dri_screen.h"
41 #include "dri_context.h"
42 #include "dri_drawable.h"
43 #include "dri1_helper.h"
47 dri1_lock(struct dri_context
*ctx
)
49 drm_context_t hw_context
= ctx
->cPriv
->hHWContext
;
52 DRM_CAS(ctx
->lock
, hw_context
, DRM_LOCK_HELD
| hw_context
, ret
);
54 drmGetLock(ctx
->sPriv
->fd
, hw_context
, 0);
55 ctx
->stLostLock
= TRUE
;
56 ctx
->wsLostLock
= TRUE
;
62 dri1_unlock(struct dri_context
*ctx
)
64 ctx
->isLocked
= FALSE
;
65 DRM_UNLOCK(ctx
->sPriv
->fd
, ctx
->lock
, ctx
->cPriv
->hHWContext
);
69 dri1_update_drawables_locked(struct dri_context
*ctx
,
70 __DRIdrawable
* driDrawPriv
,
71 __DRIdrawable
* driReadPriv
)
73 if (ctx
->stLostLock
) {
74 ctx
->stLostLock
= FALSE
;
75 if (driDrawPriv
== driReadPriv
)
76 DRI_VALIDATE_DRAWABLE_INFO(ctx
->sPriv
, driDrawPriv
);
78 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx
->sPriv
, driDrawPriv
,
84 * This ensures all contexts which bind to a drawable pick up the
85 * drawable change and signal new buffer state.
88 dri1_propagate_drawable_change(struct dri_context
*ctx
)
90 __DRIdrawable
*dPriv
= ctx
->dPriv
;
91 __DRIdrawable
*rPriv
= ctx
->rPriv
;
92 struct dri_drawable
*draw
;
93 struct dri_drawable
*read
;
94 boolean flushed
= FALSE
;
97 draw
= dri_drawable(dPriv
);
101 read
= dri_drawable(rPriv
);
104 if (dPriv
&& draw
->texture_stamp
!= dPriv
->lastStamp
) {
105 ctx
->st
->flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
107 ctx
->st
->notify_invalid_framebuffer(ctx
->st
, draw
->stfb
);
110 if (rPriv
&& dPriv
!= rPriv
&& read
->texture_stamp
!= rPriv
->lastStamp
) {
112 ctx
->st
->flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
113 ctx
->st
->notify_invalid_framebuffer(ctx
->st
, read
->stfb
);
117 static INLINE boolean
118 dri1_intersect_src_bbox(struct drm_clip_rect
*dst
,
121 const struct drm_clip_rect
*src
,
122 const struct drm_clip_rect
*bbox
)
127 xy1
= ((int)src
->x1
> (int)bbox
->x1
+ dst_x
) ? src
->x1
:
128 (int)bbox
->x1
+ dst_x
;
129 xy2
= ((int)src
->x2
< (int)bbox
->x2
+ dst_x
) ? src
->x2
:
130 (int)bbox
->x2
+ dst_x
;
131 if (xy1
>= xy2
|| xy1
< 0)
137 xy1
= ((int)src
->y1
> (int)bbox
->y1
+ dst_y
) ? src
->y1
:
138 (int)bbox
->y1
+ dst_y
;
139 xy2
= ((int)src
->y2
< (int)bbox
->y2
+ dst_y
) ? src
->y2
:
140 (int)bbox
->y2
+ dst_y
;
141 if (xy1
>= xy2
|| xy1
< 0)
150 dri1_swap_copy(struct pipe_context
*pipe
,
151 struct pipe_surface
*dst
,
152 struct pipe_surface
*src
,
153 __DRIdrawable
* dPriv
, const struct drm_clip_rect
*bbox
)
155 struct drm_clip_rect clip
;
156 struct drm_clip_rect
*cur
;
159 cur
= dPriv
->pClipRects
;
161 for (i
= 0; i
< dPriv
->numClipRects
; ++i
) {
162 if (dri1_intersect_src_bbox(&clip
, dPriv
->x
, dPriv
->y
, cur
++, bbox
)) {
163 if (pipe
->surface_copy
) {
164 pipe
->surface_copy(pipe
, dst
, clip
.x1
, clip
.y1
,
166 (int)clip
.x1
- dPriv
->x
,
167 (int)clip
.y1
- dPriv
->y
,
168 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
170 util_surface_copy(pipe
, FALSE
, dst
, clip
.x1
, clip
.y1
,
172 (int)clip
.x1
- dPriv
->x
,
173 (int)clip
.y1
- dPriv
->y
,
174 clip
.x2
- clip
.x1
, clip
.y2
- clip
.y1
);
181 dri1_present_texture_locked(__DRIdrawable
* dPriv
,
182 struct pipe_resource
*ptex
,
183 const struct drm_clip_rect
*sub_box
,
184 struct pipe_fence_handle
**fence
)
186 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
187 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
188 struct pipe_context
*pipe
;
189 struct pipe_surface
*psurf
;
190 struct drm_clip_rect bbox
;
191 boolean visible
= TRUE
;
196 bbox
.x2
= ptex
->width0
;
198 bbox
.y2
= ptex
->height0
;
201 visible
= dri1_intersect_src_bbox(&bbox
, 0, 0, &bbox
, sub_box
);
205 pipe
= dri1_get_pipe_context(screen
);
206 psurf
= dri1_get_pipe_surface(drawable
, ptex
);
210 if (__dri1_api_hooks
->present_locked
) {
211 __dri1_api_hooks
->present_locked(pipe
, psurf
,
212 dPriv
->pClipRects
, dPriv
->numClipRects
,
213 dPriv
->x
, dPriv
->y
, &bbox
, fence
);
214 } else if (__dri1_api_hooks
->front_srf_locked
) {
215 struct pipe_surface
*front
= __dri1_api_hooks
->front_srf_locked(pipe
);
218 dri1_swap_copy(pipe
, front
, psurf
, dPriv
, &bbox
);
220 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, fence
);
225 dri1_copy_to_front(struct dri_context
*ctx
,
226 struct pipe_resource
*ptex
,
227 __DRIdrawable
* dPriv
,
228 const struct drm_clip_rect
*sub_box
,
229 struct pipe_fence_handle
**fence
)
231 boolean save_lost_lock
;
234 save_lost_lock
= ctx
->stLostLock
;
235 dri1_update_drawables_locked(ctx
, dPriv
, dPriv
);
237 dri1_present_texture_locked(dPriv
, ptex
, sub_box
, fence
);
239 ctx
->stLostLock
= save_lost_lock
;
242 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
246 dri1_update_drawables_locked(ctx
, ctx
->dPriv
, ctx
->rPriv
);
249 dri1_propagate_drawable_change(ctx
);
253 * Backend functions for st_framebuffer interface and swap_buffers.
257 dri1_flush_frontbuffer(struct dri_drawable
*draw
,
258 enum st_attachment_type statt
)
260 struct dri_context
*ctx
= dri_get_current();
261 struct dri_screen
*screen
= dri_screen(draw
->sPriv
);
262 struct pipe_screen
*pipe_screen
= screen
->pipe_screen
;
263 struct pipe_fence_handle
*dummy_fence
;
264 struct pipe_resource
*ptex
;
267 return; /* For now */
269 ptex
= draw
->textures
[statt
];
271 dri1_copy_to_front(ctx
, ptex
, ctx
->dPriv
, NULL
, &dummy_fence
);
272 pipe_screen
->fence_reference(pipe_screen
, &dummy_fence
, NULL
);
276 * FIXME: Do we need swap throttling here?
281 dri1_swap_buffers(__DRIdrawable
* dPriv
)
283 struct dri_context
*ctx
= dri_get_current();
284 struct dri_drawable
*draw
= dri_drawable(dPriv
);
285 struct dri_screen
*screen
= dri_screen(draw
->sPriv
);
286 struct pipe_screen
*pipe_screen
= screen
->pipe_screen
;
287 struct pipe_fence_handle
*fence
;
288 struct pipe_resource
*ptex
;
290 assert(__dri1_api_hooks
!= NULL
);
293 return; /* For now */
295 ptex
= draw
->textures
[ST_ATTACHMENT_BACK_LEFT
];
297 ctx
->st
->flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
298 fence
= dri1_swap_fences_pop_front(draw
);
300 (void)pipe_screen
->fence_finish(pipe_screen
, fence
, 0);
301 pipe_screen
->fence_reference(pipe_screen
, &fence
, NULL
);
303 dri1_copy_to_front(ctx
, ptex
, dPriv
, NULL
, &fence
);
304 dri1_swap_fences_push_back(draw
, fence
);
305 pipe_screen
->fence_reference(pipe_screen
, &fence
, NULL
);
310 dri1_copy_sub_buffer(__DRIdrawable
* dPriv
, int x
, int y
, int w
, int h
)
312 struct dri_context
*ctx
= dri_get_current();
313 struct dri_screen
*screen
= dri_screen(dPriv
->driScreenPriv
);
314 struct pipe_screen
*pipe_screen
= screen
->pipe_screen
;
315 struct drm_clip_rect sub_bbox
;
316 struct dri_drawable
*draw
= dri_drawable(dPriv
);
317 struct pipe_fence_handle
*dummy_fence
;
318 struct pipe_resource
*ptex
;
320 assert(__dri1_api_hooks
!= NULL
);
330 ptex
= draw
->textures
[ST_ATTACHMENT_BACK_LEFT
];
332 ctx
->st
->flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
333 dri1_copy_to_front(ctx
, ptex
, dPriv
, &sub_bbox
, &dummy_fence
);
334 pipe_screen
->fence_reference(pipe_screen
, &dummy_fence
, NULL
);
339 * Allocate framebuffer attachments.
341 * During fixed-size operation, the function keeps allocating new attachments
342 * as they are requested. Unused attachments are not removed, not until the
343 * framebuffer is resized or destroyed.
346 dri1_allocate_textures(struct dri_drawable
*drawable
,
349 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
350 struct pipe_resource templ
;
351 unsigned width
, height
;
355 width
= drawable
->dPriv
->w
;
356 height
= drawable
->dPriv
->h
;
358 resized
= (drawable
->old_w
!= width
||
359 drawable
->old_h
!= height
);
361 /* remove outdated textures */
363 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
364 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
367 memset(&templ
, 0, sizeof(templ
));
368 templ
.target
= PIPE_TEXTURE_2D
;
369 templ
.width0
= width
;
370 templ
.height0
= height
;
372 templ
.last_level
= 0;
374 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
375 enum pipe_format format
;
378 /* the texture already exists or not requested */
379 if (drawable
->textures
[i
] || !(mask
& (1 << i
))) {
384 case ST_ATTACHMENT_FRONT_LEFT
:
385 case ST_ATTACHMENT_BACK_LEFT
:
386 case ST_ATTACHMENT_FRONT_RIGHT
:
387 case ST_ATTACHMENT_BACK_RIGHT
:
388 format
= drawable
->stvis
.color_format
;
389 tex_usage
= PIPE_BIND_DISPLAY_TARGET
|
390 PIPE_BIND_RENDER_TARGET
;
392 case ST_ATTACHMENT_DEPTH_STENCIL
:
393 format
= drawable
->stvis
.depth_stencil_format
;
394 tex_usage
= PIPE_BIND_DEPTH_STENCIL
;
397 format
= PIPE_FORMAT_NONE
;
401 if (format
!= PIPE_FORMAT_NONE
) {
402 templ
.format
= format
;
403 templ
.bind
= tex_usage
;
405 drawable
->textures
[i
] =
406 screen
->pipe_screen
->resource_create(screen
->pipe_screen
, &templ
);
410 drawable
->old_w
= width
;
411 drawable
->old_h
= height
;
415 * Backend function for init_screen.
418 static const __DRIextension
*dri1_screen_extensions
[] = {
419 &driReadDrawableExtension
,
420 &driCopySubBufferExtension
.base
,
421 &driSwapControlExtension
.base
,
422 &driFrameTrackingExtension
.base
,
423 &driMediaStreamCounterExtension
.base
,
428 st_dri_lock(struct pipe_context
*pipe
)
430 dri1_lock((struct dri_context
*)pipe
->priv
);
434 st_dri_unlock(struct pipe_context
*pipe
)
436 dri1_unlock((struct dri_context
*)pipe
->priv
);
440 st_dri_is_locked(struct pipe_context
*pipe
)
442 return ((struct dri_context
*)pipe
->priv
)->isLocked
;
446 st_dri_lost_lock(struct pipe_context
*pipe
)
448 return ((struct dri_context
*)pipe
->priv
)->wsLostLock
;
452 st_dri_clear_lost_lock(struct pipe_context
*pipe
)
454 ((struct dri_context
*)pipe
->priv
)->wsLostLock
= FALSE
;
457 static struct dri1_api_lock_funcs dri1_lf
= {
459 .unlock
= st_dri_unlock
,
460 .is_locked
= st_dri_is_locked
,
461 .is_lock_lost
= st_dri_lost_lock
,
462 .clear_lost_lock
= st_dri_clear_lost_lock
466 dri1_copy_version(struct dri1_api_version
*dst
,
467 const struct __DRIversionRec
*src
)
469 dst
->major
= src
->major
;
470 dst
->minor
= src
->minor
;
471 dst
->patch_level
= src
->patch
;
474 struct dri1_api
*__dri1_api_hooks
= NULL
;
477 dri1_init_screen(__DRIscreen
* sPriv
)
479 const __DRIconfig
**configs
;
480 struct pipe_screen
*pscreen
;
481 struct dri_screen
*screen
;
482 struct dri1_create_screen_arg arg
;
484 screen
= CALLOC_STRUCT(dri_screen
);
488 screen
->api
= drm_api_create();
489 screen
->sPriv
= sPriv
;
490 screen
->fd
= sPriv
->fd
;
491 screen
->drmLock
= (drmLock
*) & sPriv
->pSAREA
->lock
;
493 sPriv
->private = (void *)screen
;
494 sPriv
->extensions
= dri1_screen_extensions
;
496 arg
.base
.mode
= DRM_CREATE_DRI1
;
498 arg
.ddx_info
= sPriv
->pDevPriv
;
499 arg
.ddx_info_size
= sPriv
->devPrivSize
;
500 arg
.sarea
= sPriv
->pSAREA
;
501 dri1_copy_version(&arg
.ddx_version
, &sPriv
->ddx_version
);
502 dri1_copy_version(&arg
.dri_version
, &sPriv
->dri_version
);
503 dri1_copy_version(&arg
.drm_version
, &sPriv
->drm_version
);
507 * FIXME: If the driver supports format conversion swapbuffer blits, we might
508 * want to support other color bit depths than the server is currently
512 pscreen
= screen
->api
->create_screen(screen
->api
, screen
->fd
, &arg
.base
);
513 /* dri_init_screen_helper checks pscreen for us */
515 configs
= dri_init_screen_helper(screen
, pscreen
, sPriv
->fbBPP
);
520 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__
);
524 __dri1_api_hooks
= arg
.api
;
530 dri_destroy_screen_helper(screen
);