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 **************************************************************************/
30 #include <sys/timeb.h>
32 #include <vdpau/vdpau.h>
34 #include "util/u_debug.h"
35 #include "util/u_memory.h"
37 #include "vdpau_private.h"
40 * Create a VdpPresentationQueue.
43 vlVdpPresentationQueueCreate(VdpDevice device
,
44 VdpPresentationQueueTarget presentation_queue_target
,
45 VdpPresentationQueue
*presentation_queue
)
47 vlVdpPresentationQueue
*pq
= NULL
;
50 if (!presentation_queue
)
51 return VDP_STATUS_INVALID_POINTER
;
53 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
55 return VDP_STATUS_INVALID_HANDLE
;
57 vlVdpPresentationQueueTarget
*pqt
= vlGetDataHTAB(presentation_queue_target
);
59 return VDP_STATUS_INVALID_HANDLE
;
61 if (dev
!= pqt
->device
)
62 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
64 pq
= CALLOC(1, sizeof(vlVdpPresentationQueue
));
66 return VDP_STATUS_RESOURCES
;
69 pq
->drawable
= pqt
->drawable
;
71 if (!vl_compositor_init_state(&pq
->cstate
, dev
->context
)) {
72 ret
= VDP_STATUS_ERROR
;
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 vl_compositor_cleanup_state(&pq
->cstate
);
104 vlRemoveDataHTAB(presentation_queue
);
107 return VDP_STATUS_OK
;
111 * Configure the background color setting.
114 vlVdpPresentationQueueSetBackgroundColor(VdpPresentationQueue presentation_queue
,
115 VdpColor
*const background_color
)
117 vlVdpPresentationQueue
*pq
;
118 union pipe_color_union color
;
120 if (!background_color
)
121 return VDP_STATUS_INVALID_POINTER
;
123 pq
= vlGetDataHTAB(presentation_queue
);
125 return VDP_STATUS_INVALID_HANDLE
;
127 color
.f
[0] = background_color
->red
;
128 color
.f
[1] = background_color
->green
;
129 color
.f
[2] = background_color
->blue
;
130 color
.f
[3] = background_color
->alpha
;
132 vl_compositor_set_clear_color(&pq
->cstate
, &color
);
134 return VDP_STATUS_OK
;
138 * Retrieve the current background color setting.
141 vlVdpPresentationQueueGetBackgroundColor(VdpPresentationQueue presentation_queue
,
142 VdpColor
*const background_color
)
144 vlVdpPresentationQueue
*pq
;
145 union pipe_color_union color
;
147 if (!background_color
)
148 return VDP_STATUS_INVALID_POINTER
;
150 pq
= vlGetDataHTAB(presentation_queue
);
152 return VDP_STATUS_INVALID_HANDLE
;
154 vl_compositor_get_clear_color(&pq
->cstate
, &color
);
156 background_color
->red
= color
.f
[0];
157 background_color
->green
= color
.f
[1];
158 background_color
->blue
= color
.f
[2];
159 background_color
->alpha
= color
.f
[3];
161 return VDP_STATUS_OK
;
165 * Retrieve the presentation queue's "current" time.
168 vlVdpPresentationQueueGetTime(VdpPresentationQueue presentation_queue
,
169 VdpTime
*current_time
)
171 vlVdpPresentationQueue
*pq
;
175 return VDP_STATUS_INVALID_POINTER
;
177 pq
= vlGetDataHTAB(presentation_queue
);
179 return VDP_STATUS_INVALID_HANDLE
;
181 clock_gettime(CLOCK_REALTIME
, &ts
);
182 *current_time
= (uint64_t)ts
.tv_sec
* 1000000000LL + (uint64_t)ts
.tv_nsec
;
184 return VDP_STATUS_OK
;
188 * Enter a surface into the presentation queue.
191 vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue
,
192 VdpOutputSurface surface
,
194 uint32_t clip_height
,
195 VdpTime earliest_presentation_time
)
197 static int dump_window
= -1;
199 vlVdpPresentationQueue
*pq
;
200 vlVdpOutputSurface
*surf
;
202 struct pipe_context
*pipe
;
203 struct pipe_resource
*tex
;
204 struct pipe_surface surf_templ
, *surf_draw
;
205 struct u_rect src_rect
, dst_clip
, *dirty_area
;
207 struct vl_compositor
*compositor
;
208 struct vl_compositor_state
*cstate
;
210 pq
= vlGetDataHTAB(presentation_queue
);
212 return VDP_STATUS_INVALID_HANDLE
;
215 pipe
= pq
->device
->context
;
216 compositor
= &pq
->device
->compositor
;
217 cstate
= &pq
->cstate
;
219 tex
= vl_screen_texture_from_drawable(pq
->device
->vscreen
, pq
->drawable
);
221 return VDP_STATUS_INVALID_HANDLE
;
223 dirty_area
= vl_screen_get_dirty_area(pq
->device
->vscreen
);
225 memset(&surf_templ
, 0, sizeof(surf_templ
));
226 surf_templ
.format
= tex
->format
;
227 surf_templ
.usage
= PIPE_BIND_RENDER_TARGET
;
228 surf_draw
= pipe
->create_surface(pipe
, tex
, &surf_templ
);
230 surf
= vlGetDataHTAB(surface
);
232 return VDP_STATUS_INVALID_HANDLE
;
234 surf
->timestamp
= (vlVdpTime
)earliest_presentation_time
;
238 dst_clip
.x1
= clip_width
? clip_width
: surf_draw
->width
;
239 dst_clip
.y1
= clip_height
? clip_height
: surf_draw
->height
;
241 if (pq
->device
->delayed_rendering
.surface
== surface
&&
242 dst_clip
.x1
== surf_draw
->width
&& dst_clip
.y1
== surf_draw
->height
) {
244 // TODO: we correctly support the clipping here, but not the pq background color in the clipped area....
245 cstate
= pq
->device
->delayed_rendering
.cstate
;
246 vl_compositor_set_dst_clip(cstate
, &dst_clip
);
247 vlVdpResolveDelayedRendering(pq
->device
, surf_draw
, dirty_area
);
250 vlVdpResolveDelayedRendering(pq
->device
, NULL
, NULL
);
254 src_rect
.x1
= surf_draw
->width
;
255 src_rect
.y1
= surf_draw
->height
;
257 vl_compositor_clear_layers(cstate
);
258 vl_compositor_set_rgba_layer(cstate
, compositor
, 0, surf
->sampler_view
, &src_rect
, NULL
, NULL
);
259 vl_compositor_set_dst_clip(cstate
, &dst_clip
);
260 vl_compositor_render(cstate
, compositor
, surf_draw
, dirty_area
);
263 pipe
->screen
->flush_frontbuffer
265 pipe
->screen
, tex
, 0, 0,
266 vl_screen_get_private(pq
->device
->vscreen
)
269 pipe
->screen
->fence_reference(pipe
->screen
, &surf
->fence
, NULL
);
270 pipe
->flush(pipe
, &surf
->fence
);
272 if (dump_window
== -1) {
273 dump_window
= debug_get_num_option("VDPAU_DUMP", 0);
277 static unsigned int framenum
= 0;
280 sprintf(cmd
, "xwd -id %d -out vdpau_frame_%08d.xwd", (int)pq
->drawable
, ++framenum
);
281 if (system(cmd
) != 0)
282 VDPAU_MSG(VDPAU_ERR
, "[VDPAU] Dumping surface %d failed.\n", surface
);
285 pipe_resource_reference(&tex
, NULL
);
286 pipe_surface_reference(&surf_draw
, NULL
);
288 return VDP_STATUS_OK
;
292 * Wait for a surface to finish being displayed.
295 vlVdpPresentationQueueBlockUntilSurfaceIdle(VdpPresentationQueue presentation_queue
,
296 VdpOutputSurface surface
,
297 VdpTime
*first_presentation_time
)
299 vlVdpPresentationQueue
*pq
;
300 vlVdpOutputSurface
*surf
;
301 struct pipe_screen
*screen
;
303 if (!first_presentation_time
)
304 return VDP_STATUS_INVALID_POINTER
;
306 pq
= vlGetDataHTAB(presentation_queue
);
308 return VDP_STATUS_INVALID_HANDLE
;
310 surf
= vlGetDataHTAB(surface
);
312 return VDP_STATUS_INVALID_HANDLE
;
315 screen
= pq
->device
->vscreen
->pscreen
;
316 screen
->fence_finish(screen
, surf
->fence
, 0);
319 // We actually need to query the timestamp of the last VSYNC event from the hardware
320 vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
322 return VDP_STATUS_OK
;
326 * Poll the current queue status of a surface.
329 vlVdpPresentationQueueQuerySurfaceStatus(VdpPresentationQueue presentation_queue
,
330 VdpOutputSurface surface
,
331 VdpPresentationQueueStatus
*status
,
332 VdpTime
*first_presentation_time
)
334 vlVdpPresentationQueue
*pq
;
335 vlVdpOutputSurface
*surf
;
336 struct pipe_screen
*screen
;
338 if (!(status
&& first_presentation_time
))
339 return VDP_STATUS_INVALID_POINTER
;
341 pq
= vlGetDataHTAB(presentation_queue
);
343 return VDP_STATUS_INVALID_HANDLE
;
345 surf
= vlGetDataHTAB(surface
);
347 return VDP_STATUS_INVALID_HANDLE
;
349 *first_presentation_time
= 0;
352 *status
= VDP_PRESENTATION_QUEUE_STATUS_IDLE
;
354 screen
= pq
->device
->vscreen
->pscreen
;
355 if (screen
->fence_signalled(screen
, surf
->fence
)) {
356 screen
->fence_reference(screen
, &surf
->fence
, NULL
);
357 *status
= VDP_PRESENTATION_QUEUE_STATUS_VISIBLE
;
359 // We actually need to query the timestamp of the last VSYNC event from the hardware
360 vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
361 *first_presentation_time
+= 1;
363 *status
= VDP_PRESENTATION_QUEUE_STATUS_QUEUED
;
367 return VDP_STATUS_OK
;