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 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 **************************************************************************/
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
;
65 DeviceReference(&pq
->device
, dev
);
66 pq
->drawable
= pqt
->drawable
;
68 mtx_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
;
86 DeviceReference(&pq
->device
, NULL
);
92 * Destroy a VdpPresentationQueue.
95 vlVdpPresentationQueueDestroy(VdpPresentationQueue presentation_queue
)
97 vlVdpPresentationQueue
*pq
;
99 pq
= vlGetDataHTAB(presentation_queue
);
101 return VDP_STATUS_INVALID_HANDLE
;
103 mtx_lock(&pq
->device
->mutex
);
104 vl_compositor_cleanup_state(&pq
->cstate
);
105 pipe_mutex_unlock(pq
->device
->mutex
);
107 vlRemoveDataHTAB(presentation_queue
);
108 DeviceReference(&pq
->device
, NULL
);
111 return VDP_STATUS_OK
;
115 * Configure the background color setting.
118 vlVdpPresentationQueueSetBackgroundColor(VdpPresentationQueue presentation_queue
,
119 VdpColor
*const background_color
)
121 vlVdpPresentationQueue
*pq
;
122 union pipe_color_union color
;
124 if (!background_color
)
125 return VDP_STATUS_INVALID_POINTER
;
127 pq
= vlGetDataHTAB(presentation_queue
);
129 return VDP_STATUS_INVALID_HANDLE
;
131 color
.f
[0] = background_color
->red
;
132 color
.f
[1] = background_color
->green
;
133 color
.f
[2] = background_color
->blue
;
134 color
.f
[3] = background_color
->alpha
;
136 mtx_lock(&pq
->device
->mutex
);
137 vl_compositor_set_clear_color(&pq
->cstate
, &color
);
138 pipe_mutex_unlock(pq
->device
->mutex
);
140 return VDP_STATUS_OK
;
144 * Retrieve the current background color setting.
147 vlVdpPresentationQueueGetBackgroundColor(VdpPresentationQueue presentation_queue
,
148 VdpColor
*const background_color
)
150 vlVdpPresentationQueue
*pq
;
151 union pipe_color_union color
;
153 if (!background_color
)
154 return VDP_STATUS_INVALID_POINTER
;
156 pq
= vlGetDataHTAB(presentation_queue
);
158 return VDP_STATUS_INVALID_HANDLE
;
160 mtx_lock(&pq
->device
->mutex
);
161 vl_compositor_get_clear_color(&pq
->cstate
, &color
);
162 pipe_mutex_unlock(pq
->device
->mutex
);
164 background_color
->red
= color
.f
[0];
165 background_color
->green
= color
.f
[1];
166 background_color
->blue
= color
.f
[2];
167 background_color
->alpha
= color
.f
[3];
169 return VDP_STATUS_OK
;
173 * Retrieve the presentation queue's "current" time.
176 vlVdpPresentationQueueGetTime(VdpPresentationQueue presentation_queue
,
177 VdpTime
*current_time
)
179 vlVdpPresentationQueue
*pq
;
182 return VDP_STATUS_INVALID_POINTER
;
184 pq
= vlGetDataHTAB(presentation_queue
);
186 return VDP_STATUS_INVALID_HANDLE
;
188 mtx_lock(&pq
->device
->mutex
);
189 *current_time
= pq
->device
->vscreen
->get_timestamp(pq
->device
->vscreen
,
190 (void *)pq
->drawable
);
191 pipe_mutex_unlock(pq
->device
->mutex
);
193 return VDP_STATUS_OK
;
197 * Enter a surface into the presentation queue.
200 vlVdpPresentationQueueDisplay(VdpPresentationQueue presentation_queue
,
201 VdpOutputSurface surface
,
203 uint32_t clip_height
,
204 VdpTime earliest_presentation_time
)
206 static int dump_window
= -1;
208 vlVdpPresentationQueue
*pq
;
209 vlVdpOutputSurface
*surf
;
211 struct pipe_context
*pipe
;
212 struct pipe_resource
*tex
;
213 struct pipe_surface surf_templ
, *surf_draw
;
214 struct u_rect src_rect
, dst_clip
, *dirty_area
;
216 struct vl_compositor
*compositor
;
217 struct vl_compositor_state
*cstate
;
218 struct vl_screen
*vscreen
;
220 pq
= vlGetDataHTAB(presentation_queue
);
222 return VDP_STATUS_INVALID_HANDLE
;
224 surf
= vlGetDataHTAB(surface
);
226 return VDP_STATUS_INVALID_HANDLE
;
228 pipe
= pq
->device
->context
;
229 compositor
= &pq
->device
->compositor
;
230 cstate
= &pq
->cstate
;
231 vscreen
= pq
->device
->vscreen
;
233 mtx_lock(&pq
->device
->mutex
);
234 if (vscreen
->set_back_texture_from_output
&& surf
->send_to_X
)
235 vscreen
->set_back_texture_from_output(vscreen
, surf
->surface
->texture
, clip_width
, clip_height
);
236 tex
= vscreen
->texture_from_drawable(vscreen
, (void *)pq
->drawable
);
238 pipe_mutex_unlock(pq
->device
->mutex
);
239 return VDP_STATUS_INVALID_HANDLE
;
242 if (!vscreen
->set_back_texture_from_output
|| !surf
->send_to_X
) {
243 dirty_area
= vscreen
->get_dirty_area(vscreen
);
245 memset(&surf_templ
, 0, sizeof(surf_templ
));
246 surf_templ
.format
= tex
->format
;
247 surf_draw
= pipe
->create_surface(pipe
, tex
, &surf_templ
);
251 dst_clip
.x1
= clip_width
? clip_width
: surf_draw
->width
;
252 dst_clip
.y1
= clip_height
? clip_height
: surf_draw
->height
;
256 src_rect
.x1
= surf_draw
->width
;
257 src_rect
.y1
= surf_draw
->height
;
259 vl_compositor_clear_layers(cstate
);
260 vl_compositor_set_rgba_layer(cstate
, compositor
, 0, surf
->sampler_view
, &src_rect
, NULL
, NULL
);
261 vl_compositor_set_dst_clip(cstate
, &dst_clip
);
262 vl_compositor_render(cstate
, compositor
, surf_draw
, dirty_area
, true);
265 vscreen
->set_next_timestamp(vscreen
, earliest_presentation_time
);
267 // flush before calling flush_frontbuffer so that rendering is flushed
268 // to back buffer so the texture can be copied in flush_frontbuffer
269 pipe
->screen
->fence_reference(pipe
->screen
, &surf
->fence
, NULL
);
270 pipe
->flush(pipe
, &surf
->fence
, 0);
271 pipe
->screen
->flush_frontbuffer(pipe
->screen
, tex
, 0, 0,
272 vscreen
->get_private(vscreen
), NULL
);
274 pq
->last_surf
= surf
;
276 if (dump_window
== -1) {
277 dump_window
= debug_get_num_option("VDPAU_DUMP", 0);
281 static unsigned int framenum
= 0;
285 sprintf(cmd
, "xwd -id %d -silent -out vdpau_frame_%08d.xwd", (int)pq
->drawable
, framenum
);
286 if (system(cmd
) != 0)
287 VDPAU_MSG(VDPAU_ERR
, "[VDPAU] Dumping surface %d failed.\n", surface
);
292 if (!vscreen
->set_back_texture_from_output
|| !surf
->send_to_X
) {
293 pipe_resource_reference(&tex
, NULL
);
294 pipe_surface_reference(&surf_draw
, NULL
);
296 pipe_mutex_unlock(pq
->device
->mutex
);
298 return VDP_STATUS_OK
;
302 * Wait for a surface to finish being displayed.
305 vlVdpPresentationQueueBlockUntilSurfaceIdle(VdpPresentationQueue presentation_queue
,
306 VdpOutputSurface surface
,
307 VdpTime
*first_presentation_time
)
309 vlVdpPresentationQueue
*pq
;
310 vlVdpOutputSurface
*surf
;
311 struct pipe_screen
*screen
;
313 if (!first_presentation_time
)
314 return VDP_STATUS_INVALID_POINTER
;
316 pq
= vlGetDataHTAB(presentation_queue
);
318 return VDP_STATUS_INVALID_HANDLE
;
320 surf
= vlGetDataHTAB(surface
);
322 return VDP_STATUS_INVALID_HANDLE
;
324 mtx_lock(&pq
->device
->mutex
);
326 screen
= pq
->device
->vscreen
->pscreen
;
327 screen
->fence_finish(screen
, NULL
, surf
->fence
, PIPE_TIMEOUT_INFINITE
);
328 screen
->fence_reference(screen
, &surf
->fence
, NULL
);
330 pipe_mutex_unlock(pq
->device
->mutex
);
332 return vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
336 * Poll the current queue status of a surface.
339 vlVdpPresentationQueueQuerySurfaceStatus(VdpPresentationQueue presentation_queue
,
340 VdpOutputSurface surface
,
341 VdpPresentationQueueStatus
*status
,
342 VdpTime
*first_presentation_time
)
344 vlVdpPresentationQueue
*pq
;
345 vlVdpOutputSurface
*surf
;
346 struct pipe_screen
*screen
;
348 if (!(status
&& first_presentation_time
))
349 return VDP_STATUS_INVALID_POINTER
;
351 pq
= vlGetDataHTAB(presentation_queue
);
353 return VDP_STATUS_INVALID_HANDLE
;
355 surf
= vlGetDataHTAB(surface
);
357 return VDP_STATUS_INVALID_HANDLE
;
359 *first_presentation_time
= 0;
362 if (pq
->last_surf
== surf
)
363 *status
= VDP_PRESENTATION_QUEUE_STATUS_VISIBLE
;
365 *status
= VDP_PRESENTATION_QUEUE_STATUS_IDLE
;
367 mtx_lock(&pq
->device
->mutex
);
368 screen
= pq
->device
->vscreen
->pscreen
;
369 if (screen
->fence_finish(screen
, NULL
, surf
->fence
, 0)) {
370 screen
->fence_reference(screen
, &surf
->fence
, NULL
);
371 *status
= VDP_PRESENTATION_QUEUE_STATUS_VISIBLE
;
372 pipe_mutex_unlock(pq
->device
->mutex
);
374 // We actually need to query the timestamp of the last VSYNC event from the hardware
375 vlVdpPresentationQueueGetTime(presentation_queue
, first_presentation_time
);
376 *first_presentation_time
+= 1;
378 *status
= VDP_PRESENTATION_QUEUE_STATUS_QUEUED
;
379 pipe_mutex_unlock(pq
->device
->mutex
);
383 return VDP_STATUS_OK
;