1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
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 TUNGSTEN GRAPHICS 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 **************************************************************************/
29 #include <vdpau/vdpau.h>
31 #include "util/u_debug.h"
32 #include "util/u_memory.h"
34 #include "vdpau_private.h"
37 * Create a VdpPresentationQueue.
40 vlVdpPresentationQueueCreate(VdpDevice device
,
41 VdpPresentationQueueTarget presentation_queue_target
,
42 VdpPresentationQueue
*presentation_queue
)
44 vlVdpPresentationQueue
*pq
= NULL
;
47 if (!presentation_queue
)
48 return VDP_STATUS_INVALID_POINTER
;
50 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
52 return VDP_STATUS_INVALID_HANDLE
;
54 vlVdpPresentationQueueTarget
*pqt
= vlGetDataHTAB(presentation_queue_target
);
56 return VDP_STATUS_INVALID_HANDLE
;
58 if (dev
!= pqt
->device
)
59 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
61 pq
= CALLOC(1, sizeof(vlVdpPresentationQueue
));
63 return VDP_STATUS_RESOURCES
;
66 pq
->drawable
= pqt
->drawable
;
68 pipe_mutex_lock(dev
->mutex
);
69 if (!vl_compositor_init_state(&pq
->cstate
, dev
->context
)) {
70 pipe_mutex_unlock(dev
->mutex
);
71 ret
= VDP_STATUS_ERROR
;
74 pipe_mutex_unlock(dev
->mutex
);
76 *presentation_queue
= vlAddDataHTAB(pq
);
77 if (*presentation_queue
== 0) {
78 ret
= VDP_STATUS_ERROR
;
91 * Destroy a VdpPresentationQueue.
94 vlVdpPresentationQueueDestroy(VdpPresentationQueue presentation_queue
)
96 vlVdpPresentationQueue
*pq
;
98 pq
= vlGetDataHTAB(presentation_queue
);
100 return VDP_STATUS_INVALID_HANDLE
;
102 pipe_mutex_lock(pq
->device
->mutex
);
103 vl_compositor_cleanup_state(&pq
->cstate
);
104 pipe_mutex_unlock(pq
->device
->mutex
);
106 vlRemoveDataHTAB(presentation_queue
);
109 return VDP_STATUS_OK
;
113 * Configure the background color setting.
116 vlVdpPresentationQueueSetBackgroundColor(VdpPresentationQueue presentation_queue
,
117 VdpColor
*const background_color
)
119 vlVdpPresentationQueue
*pq
;
120 union pipe_color_union color
;
122 if (!background_color
)
123 return VDP_STATUS_INVALID_POINTER
;
125 pq
= vlGetDataHTAB(presentation_queue
);
127 return VDP_STATUS_INVALID_HANDLE
;
129 color
.f
[0] = background_color
->red
;
130 color
.f
[1] = background_color
->green
;
131 color
.f
[2] = background_color
->blue
;
132 color
.f
[3] = background_color
->alpha
;
134 pipe_mutex_lock(pq
->device
->mutex
);
135 vl_compositor_set_clear_color(&pq
->cstate
, &color
);
136 pipe_mutex_unlock(pq
->device
->mutex
);
138 return VDP_STATUS_OK
;
142 * Retrieve the current background color setting.
145 vlVdpPresentationQueueGetBackgroundColor(VdpPresentationQueue presentation_queue
,
146 VdpColor
*const background_color
)
148 vlVdpPresentationQueue
*pq
;
149 union pipe_color_union color
;
151 if (!background_color
)
152 return VDP_STATUS_INVALID_POINTER
;
154 pq
= vlGetDataHTAB(presentation_queue
);
156 return VDP_STATUS_INVALID_HANDLE
;
158 pipe_mutex_lock(pq
->device
->mutex
);
159 vl_compositor_get_clear_color(&pq
->cstate
, &color
);
160 pipe_mutex_unlock(pq
->device
->mutex
);
162 background_color
->red
= color
.f
[0];
163 background_color
->green
= color
.f
[1];
164 background_color
->blue
= color
.f
[2];
165 background_color
->alpha
= color
.f
[3];
167 return VDP_STATUS_OK
;
171 * Retrieve the presentation queue's "current" time.
174 vlVdpPresentationQueueGetTime(VdpPresentationQueue presentation_queue
,
175 VdpTime
*current_time
)
177 vlVdpPresentationQueue
*pq
;
180 return VDP_STATUS_INVALID_POINTER
;
182 pq
= vlGetDataHTAB(presentation_queue
);
184 return VDP_STATUS_INVALID_HANDLE
;
186 pipe_mutex_lock(pq
->device
->mutex
);
187 *current_time
= vl_screen_get_timestamp(pq
->device
->vscreen
, pq
->drawable
);
188 pipe_mutex_unlock(pq
->device
->mutex
);
190 return VDP_STATUS_OK
;
194 * Enter a surface into the presentation queue.
197 vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue
,
198 VdpOutputSurface surface
,
200 uint32_t clip_height
,
201 VdpTime earliest_presentation_time
)
203 static int dump_window
= -1;
205 vlVdpPresentationQueue
*pq
;
206 vlVdpOutputSurface
*surf
;
208 struct pipe_context
*pipe
;
209 struct pipe_resource
*tex
;
210 struct pipe_surface surf_templ
, *surf_draw
;
211 struct u_rect src_rect
, dst_clip
, *dirty_area
;
213 struct vl_compositor
*compositor
;
214 struct vl_compositor_state
*cstate
;
216 pq
= vlGetDataHTAB(presentation_queue
);
218 return VDP_STATUS_INVALID_HANDLE
;
220 surf
= vlGetDataHTAB(surface
);
222 return VDP_STATUS_INVALID_HANDLE
;
224 pipe
= pq
->device
->context
;
225 compositor
= &pq
->device
->compositor
;
226 cstate
= &pq
->cstate
;
228 pipe_mutex_lock(pq
->device
->mutex
);
229 tex
= vl_screen_texture_from_drawable(pq
->device
->vscreen
, pq
->drawable
);
231 pipe_mutex_unlock(pq
->device
->mutex
);
232 return VDP_STATUS_INVALID_HANDLE
;
235 dirty_area
= vl_screen_get_dirty_area(pq
->device
->vscreen
);
237 memset(&surf_templ
, 0, sizeof(surf_templ
));
238 surf_templ
.format
= tex
->format
;
239 surf_draw
= pipe
->create_surface(pipe
, tex
, &surf_templ
);
241 surf
->timestamp
= (vlVdpTime
)earliest_presentation_time
;
245 dst_clip
.x1
= clip_width
? clip_width
: surf_draw
->width
;
246 dst_clip
.y1
= clip_height
? clip_height
: surf_draw
->height
;
248 if (pq
->device
->delayed_rendering
.surface
== surface
&&
249 dst_clip
.x1
== surf_draw
->width
&& dst_clip
.y1
== surf_draw
->height
) {
251 // TODO: we correctly support the clipping here, but not the pq background color in the clipped area....
252 cstate
= pq
->device
->delayed_rendering
.cstate
;
253 vl_compositor_set_dst_clip(cstate
, &dst_clip
);
254 vlVdpResolveDelayedRendering(pq
->device
, surf_draw
, dirty_area
);
257 vlVdpResolveDelayedRendering(pq
->device
, NULL
, NULL
);
261 src_rect
.x1
= surf_draw
->width
;
262 src_rect
.y1
= surf_draw
->height
;
264 vl_compositor_clear_layers(cstate
);
265 vl_compositor_set_rgba_layer(cstate
, compositor
, 0, surf
->sampler_view
, &src_rect
, NULL
, NULL
);
266 vl_compositor_set_dst_clip(cstate
, &dst_clip
);
267 vl_compositor_render(cstate
, compositor
, surf_draw
, dirty_area
);
270 vl_screen_set_next_timestamp(pq
->device
->vscreen
, earliest_presentation_time
);
271 pipe
->screen
->flush_frontbuffer
273 pipe
->screen
, tex
, 0, 0,
274 vl_screen_get_private(pq
->device
->vscreen
)
277 pipe
->screen
->fence_reference(pipe
->screen
, &surf
->fence
, NULL
);
278 pipe
->flush(pipe
, &surf
->fence
);
280 if (dump_window
== -1) {
281 dump_window
= debug_get_num_option("VDPAU_DUMP", 0);
285 static unsigned int framenum
= 0;
289 sprintf(cmd
, "xwd -id %d -silent -out vdpau_frame_%08d.xwd", (int)pq
->drawable
, framenum
);
290 if (system(cmd
) != 0)
291 VDPAU_MSG(VDPAU_ERR
, "[VDPAU] Dumping surface %d failed.\n", surface
);
296 pipe_resource_reference(&tex
, NULL
);
297 pipe_surface_reference(&surf_draw
, NULL
);
298 pipe_mutex_unlock(pq
->device
->mutex
);
300 return VDP_STATUS_OK
;
304 * Wait for a surface to finish being displayed.
307 vlVdpPresentationQueueBlockUntilSurfaceIdle(VdpPresentationQueue presentation_queue
,
308 VdpOutputSurface surface
,
309 VdpTime
*first_presentation_time
)
311 vlVdpPresentationQueue
*pq
;
312 vlVdpOutputSurface
*surf
;
313 struct pipe_screen
*screen
;
315 if (!first_presentation_time
)
316 return VDP_STATUS_INVALID_POINTER
;
318 pq
= vlGetDataHTAB(presentation_queue
);
320 return VDP_STATUS_INVALID_HANDLE
;
322 surf
= vlGetDataHTAB(surface
);
324 return VDP_STATUS_INVALID_HANDLE
;
326 pipe_mutex_lock(pq
->device
->mutex
);
328 screen
= pq
->device
->vscreen
->pscreen
;
329 screen
->fence_finish(screen
, surf
->fence
, 0);
331 pipe_mutex_unlock(pq
->device
->mutex
);
333 return vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
337 * Poll the current queue status of a surface.
340 vlVdpPresentationQueueQuerySurfaceStatus(VdpPresentationQueue presentation_queue
,
341 VdpOutputSurface surface
,
342 VdpPresentationQueueStatus
*status
,
343 VdpTime
*first_presentation_time
)
345 vlVdpPresentationQueue
*pq
;
346 vlVdpOutputSurface
*surf
;
347 struct pipe_screen
*screen
;
349 if (!(status
&& first_presentation_time
))
350 return VDP_STATUS_INVALID_POINTER
;
352 pq
= vlGetDataHTAB(presentation_queue
);
354 return VDP_STATUS_INVALID_HANDLE
;
356 surf
= vlGetDataHTAB(surface
);
358 return VDP_STATUS_INVALID_HANDLE
;
360 *first_presentation_time
= 0;
363 *status
= VDP_PRESENTATION_QUEUE_STATUS_IDLE
;
365 pipe_mutex_lock(pq
->device
->mutex
);
366 screen
= pq
->device
->vscreen
->pscreen
;
367 if (screen
->fence_signalled(screen
, surf
->fence
)) {
368 screen
->fence_reference(screen
, &surf
->fence
, NULL
);
369 *status
= VDP_PRESENTATION_QUEUE_STATUS_VISIBLE
;
370 pipe_mutex_unlock(pq
->device
->mutex
);
372 // We actually need to query the timestamp of the last VSYNC event from the hardware
373 vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
374 *first_presentation_time
+= 1;
376 *status
= VDP_PRESENTATION_QUEUE_STATUS_QUEUED
;
377 pipe_mutex_unlock(pq
->device
->mutex
);
381 return VDP_STATUS_OK
;