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
->base
);
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
->base
);
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(draw
->sPriv
);
261 struct dri_screen
*screen
= dri_screen(draw
->sPriv
);
262 struct pipe_screen
*pipe_screen
= screen
->base
.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_drawable
*draw
= dri_drawable(dPriv
);
284 struct dri_context
*ctx
= dri_get_current(draw
->sPriv
);
285 struct dri_screen
*screen
= dri_screen(draw
->sPriv
);
286 struct pipe_screen
*pipe_screen
= screen
->base
.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(dPriv
->driScreenPriv
);
313 struct dri_screen
*screen
= dri_screen(dPriv
->driScreenPriv
);
314 struct pipe_screen
*pipe_screen
= screen
->base
.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
,
347 const enum st_attachment_type
*statts
,
350 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
351 struct pipe_resource templ
;
352 unsigned width
, height
;
356 width
= drawable
->dPriv
->w
;
357 height
= drawable
->dPriv
->h
;
359 resized
= (drawable
->old_w
!= width
||
360 drawable
->old_h
!= height
);
362 /* remove outdated textures */
364 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
365 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
368 memset(&templ
, 0, sizeof(templ
));
369 templ
.target
= PIPE_TEXTURE_2D
;
370 templ
.width0
= width
;
371 templ
.height0
= height
;
373 templ
.last_level
= 0;
375 for (i
= 0; i
< count
; i
++) {
376 enum pipe_format format
;
379 /* the texture already exists */
380 if (drawable
->textures
[statts
[i
]])
383 dri_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
385 if (format
== PIPE_FORMAT_NONE
)
388 templ
.format
= format
;
391 drawable
->textures
[statts
[i
]] =
392 screen
->base
.screen
->resource_create(screen
->base
.screen
, &templ
);
395 drawable
->old_w
= width
;
396 drawable
->old_h
= height
;
400 * Backend function for init_screen.
403 static const __DRIextension
*dri1_screen_extensions
[] = {
404 &driReadDrawableExtension
,
405 &driCopySubBufferExtension
.base
,
406 &driSwapControlExtension
.base
,
407 &driFrameTrackingExtension
.base
,
408 &driMediaStreamCounterExtension
.base
,
413 st_dri_lock(struct pipe_context
*pipe
)
415 dri1_lock((struct dri_context
*)pipe
->priv
);
419 st_dri_unlock(struct pipe_context
*pipe
)
421 dri1_unlock((struct dri_context
*)pipe
->priv
);
425 st_dri_is_locked(struct pipe_context
*pipe
)
427 return ((struct dri_context
*)pipe
->priv
)->isLocked
;
431 st_dri_lost_lock(struct pipe_context
*pipe
)
433 return ((struct dri_context
*)pipe
->priv
)->wsLostLock
;
437 st_dri_clear_lost_lock(struct pipe_context
*pipe
)
439 ((struct dri_context
*)pipe
->priv
)->wsLostLock
= FALSE
;
442 static struct dri1_api_lock_funcs dri1_lf
= {
444 .unlock
= st_dri_unlock
,
445 .is_locked
= st_dri_is_locked
,
446 .is_lock_lost
= st_dri_lost_lock
,
447 .clear_lost_lock
= st_dri_clear_lost_lock
451 dri1_copy_version(struct dri1_api_version
*dst
,
452 const struct __DRIversionRec
*src
)
454 dst
->major
= src
->major
;
455 dst
->minor
= src
->minor
;
456 dst
->patch_level
= src
->patch
;
459 struct dri1_api
*__dri1_api_hooks
= NULL
;
462 dri1_init_screen(__DRIscreen
* sPriv
)
464 const __DRIconfig
**configs
;
465 struct pipe_screen
*pscreen
;
466 struct dri_screen
*screen
;
467 struct dri1_create_screen_arg arg
;
469 screen
= CALLOC_STRUCT(dri_screen
);
473 screen
->api
= drm_api_create();
474 screen
->sPriv
= sPriv
;
475 screen
->fd
= sPriv
->fd
;
476 screen
->drmLock
= (drmLock
*) & sPriv
->pSAREA
->lock
;
477 screen
->allocate_textures
= dri1_allocate_textures
;
478 screen
->flush_frontbuffer
= dri1_flush_frontbuffer
;
480 sPriv
->private = (void *)screen
;
481 sPriv
->extensions
= dri1_screen_extensions
;
483 arg
.base
.mode
= DRM_CREATE_DRI1
;
485 arg
.ddx_info
= sPriv
->pDevPriv
;
486 arg
.ddx_info_size
= sPriv
->devPrivSize
;
487 arg
.sarea
= sPriv
->pSAREA
;
488 dri1_copy_version(&arg
.ddx_version
, &sPriv
->ddx_version
);
489 dri1_copy_version(&arg
.dri_version
, &sPriv
->dri_version
);
490 dri1_copy_version(&arg
.drm_version
, &sPriv
->drm_version
);
494 * FIXME: If the driver supports format conversion swapbuffer blits, we might
495 * want to support other color bit depths than the server is currently
499 pscreen
= screen
->api
->create_screen(screen
->api
, screen
->fd
, &arg
.base
);
500 /* dri_init_screen_helper checks pscreen for us */
502 configs
= dri_init_screen_helper(screen
, pscreen
, sPriv
->fbBPP
);
507 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__
);
511 __dri1_api_hooks
= arg
.api
;
517 dri_destroy_screen_helper(screen
);