1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
4 * Copyright 2011 Christian König.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include <vdpau/vdpau.h>
31 #include "util/u_debug.h"
32 #include "util/u_memory.h"
33 #include "util/u_sampler.h"
34 #include "util/u_format.h"
36 #include "vdpau_private.h"
39 vlVdpOutputSurfaceCreate(VdpDevice device
,
40 VdpRGBAFormat rgba_format
,
41 uint32_t width
, uint32_t height
,
42 VdpOutputSurface
*surface
)
44 struct pipe_context
*pipe
;
45 struct pipe_resource res_tmpl
, *res
;
46 struct pipe_sampler_view sv_templ
;
47 struct pipe_surface surf_templ
;
49 vlVdpOutputSurface
*vlsurface
= NULL
;
51 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Creating output surface\n");
52 if (!(width
&& height
))
53 return VDP_STATUS_INVALID_SIZE
;
55 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
57 return VDP_STATUS_INVALID_HANDLE
;
59 pipe
= dev
->context
->pipe
;
61 return VDP_STATUS_INVALID_HANDLE
;
63 vlsurface
= CALLOC(1, sizeof(vlVdpOutputSurface
));
65 return VDP_STATUS_RESOURCES
;
67 vlsurface
->device
= dev
;
69 memset(&res_tmpl
, 0, sizeof(res_tmpl
));
71 res_tmpl
.target
= PIPE_TEXTURE_2D
;
72 res_tmpl
.format
= FormatRGBAToPipe(rgba_format
);
73 res_tmpl
.width0
= width
;
74 res_tmpl
.height0
= height
;
76 res_tmpl
.array_size
= 1;
77 res_tmpl
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
78 res_tmpl
.usage
= PIPE_USAGE_STATIC
;
80 res
= pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
83 return VDP_STATUS_ERROR
;
86 memset(&sv_templ
, 0, sizeof(sv_templ
));
87 u_sampler_view_default_template(&sv_templ
, res
, res
->format
);
88 vlsurface
->sampler_view
= pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
89 if (!vlsurface
->sampler_view
) {
90 pipe_resource_reference(&res
, NULL
);
92 return VDP_STATUS_ERROR
;
95 memset(&surf_templ
, 0, sizeof(surf_templ
));
96 surf_templ
.format
= res
->format
;
97 surf_templ
.usage
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
98 vlsurface
->surface
= pipe
->create_surface(pipe
, res
, &surf_templ
);
99 if (!vlsurface
->surface
) {
100 pipe_resource_reference(&res
, NULL
);
102 return VDP_STATUS_ERROR
;
105 *surface
= vlAddDataHTAB(vlsurface
);
107 pipe_resource_reference(&res
, NULL
);
109 return VDP_STATUS_ERROR
;
112 pipe_resource_reference(&res
, NULL
);
114 return VDP_STATUS_OK
;
118 vlVdpOutputSurfaceDestroy(VdpOutputSurface surface
)
120 vlVdpOutputSurface
*vlsurface
;
122 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Destroying output surface\n");
124 vlsurface
= vlGetDataHTAB(surface
);
126 return VDP_STATUS_INVALID_HANDLE
;
128 pipe_surface_reference(&vlsurface
->surface
, NULL
);
129 pipe_sampler_view_reference(&vlsurface
->sampler_view
, NULL
);
131 vlRemoveDataHTAB(surface
);
134 return VDP_STATUS_OK
;
138 vlVdpOutputSurfaceGetParameters(VdpOutputSurface surface
,
139 VdpRGBAFormat
*rgba_format
,
140 uint32_t *width
, uint32_t *height
)
142 vlVdpOutputSurface
*vlsurface
;
144 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] getting surface parameters\n");
146 vlsurface
= vlGetDataHTAB(surface
);
148 return VDP_STATUS_INVALID_HANDLE
;
150 *rgba_format
= PipeToFormatRGBA(vlsurface
->sampler_view
->texture
->format
);
151 *width
= vlsurface
->sampler_view
->texture
->width0
;
152 *height
= vlsurface
->sampler_view
->texture
->height0
;
154 return VDP_STATUS_OK
;
158 vlVdpOutputSurfaceGetBitsNative(VdpOutputSurface surface
,
159 VdpRect
const *source_rect
,
160 void *const *destination_data
,
161 uint32_t const *destination_pitches
)
163 return VDP_STATUS_NO_IMPLEMENTATION
;
167 vlVdpOutputSurfacePutBitsNative(VdpOutputSurface surface
,
168 void const *const *source_data
,
169 uint32_t const *source_pitches
,
170 VdpRect
const *destination_rect
)
172 return VDP_STATUS_NO_IMPLEMENTATION
;
176 vlVdpOutputSurfacePutBitsIndexed(VdpOutputSurface surface
,
177 VdpIndexedFormat source_indexed_format
,
178 void const *const *source_data
,
179 uint32_t const *source_pitch
,
180 VdpRect
const *destination_rect
,
181 VdpColorTableFormat color_table_format
,
182 void const *color_table
)
184 vlVdpOutputSurface
*vlsurface
;
185 struct pipe_context
*context
;
186 struct vl_compositor
*compositor
;
188 enum pipe_format index_format
;
189 enum pipe_format colortbl_format
;
191 struct pipe_resource
*res
, res_tmpl
;
192 struct pipe_sampler_view sv_tmpl
;
193 struct pipe_sampler_view
*sv_idx
= NULL
, *sv_tbl
= NULL
;
196 struct pipe_video_rect dst_rect
;
198 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] uploading indexed output surface\n");
200 vlsurface
= vlGetDataHTAB(surface
);
202 return VDP_STATUS_INVALID_HANDLE
;
204 context
= vlsurface
->device
->context
->pipe
;
205 compositor
= &vlsurface
->device
->compositor
;
207 index_format
= FormatIndexedToPipe(source_indexed_format
);
208 if (index_format
== PIPE_FORMAT_NONE
)
209 return VDP_STATUS_INVALID_INDEXED_FORMAT
;
211 if (!source_data
|| !source_pitch
)
212 return VDP_STATUS_INVALID_POINTER
;
214 colortbl_format
= FormatColorTableToPipe(color_table_format
);
215 if (colortbl_format
== PIPE_FORMAT_NONE
)
216 return VDP_STATUS_INVALID_COLOR_TABLE_FORMAT
;
219 return VDP_STATUS_INVALID_POINTER
;
221 memset(&res_tmpl
, 0, sizeof(res_tmpl
));
222 res_tmpl
.target
= PIPE_TEXTURE_2D
;
223 res_tmpl
.format
= index_format
;
225 if (destination_rect
) {
226 res_tmpl
.width0
= abs(destination_rect
->x0
-destination_rect
->x1
);
227 res_tmpl
.height0
= abs(destination_rect
->y0
-destination_rect
->y1
);
229 res_tmpl
.width0
= vlsurface
->surface
->texture
->width0
;
230 res_tmpl
.height0
= vlsurface
->surface
->texture
->height0
;
233 res_tmpl
.array_size
= 1;
234 res_tmpl
.usage
= PIPE_USAGE_STAGING
;
235 res_tmpl
.bind
= PIPE_BIND_SAMPLER_VIEW
;
237 res
= context
->screen
->resource_create(context
->screen
, &res_tmpl
);
241 box
.x
= box
.y
= box
.z
= 0;
242 box
.width
= res
->width0
;
243 box
.height
= res
->height0
;
244 box
.depth
= res
->depth0
;
246 context
->transfer_inline_write(context
, res
, 0, PIPE_TRANSFER_WRITE
, &box
,
247 source_data
[0], source_pitch
[0],
248 source_pitch
[0] * res
->height0
);
250 memset(&sv_tmpl
, 0, sizeof(sv_tmpl
));
251 u_sampler_view_default_template(&sv_tmpl
, res
, res
->format
);
253 sv_idx
= context
->create_sampler_view(context
, res
, &sv_tmpl
);
254 pipe_resource_reference(&res
, NULL
);
259 memset(&res_tmpl
, 0, sizeof(res_tmpl
));
260 res_tmpl
.target
= PIPE_TEXTURE_1D
;
261 res_tmpl
.format
= colortbl_format
;
262 res_tmpl
.width0
= 1 << util_format_get_component_bits(
263 index_format
, UTIL_FORMAT_COLORSPACE_RGB
, 0);
264 res_tmpl
.height0
= 1;
266 res_tmpl
.array_size
= 1;
267 res_tmpl
.usage
= PIPE_USAGE_STAGING
;
268 res_tmpl
.bind
= PIPE_BIND_SAMPLER_VIEW
;
270 res
= context
->screen
->resource_create(context
->screen
, &res_tmpl
);
274 box
.x
= box
.y
= box
.z
= 0;
275 box
.width
= res
->width0
;
276 box
.height
= res
->height0
;
277 box
.depth
= res
->depth0
;
279 context
->transfer_inline_write(context
, res
, 0, PIPE_TRANSFER_WRITE
, &box
, color_table
,
280 util_format_get_stride(colortbl_format
, res
->width0
), 0);
282 memset(&sv_tmpl
, 0, sizeof(sv_tmpl
));
283 u_sampler_view_default_template(&sv_tmpl
, res
, res
->format
);
285 sv_tbl
= context
->create_sampler_view(context
, res
, &sv_tmpl
);
286 pipe_resource_reference(&res
, NULL
);
291 vl_compositor_clear_layers(compositor
);
292 vl_compositor_set_palette_layer(compositor
, 0, sv_idx
, sv_tbl
, NULL
, NULL
, false);
293 vl_compositor_render(compositor
, vlsurface
->surface
,
294 RectToPipe(destination_rect
, &dst_rect
), NULL
, false);
296 pipe_sampler_view_reference(&sv_idx
, NULL
);
297 pipe_sampler_view_reference(&sv_tbl
, NULL
);
299 return VDP_STATUS_OK
;
302 pipe_sampler_view_reference(&sv_idx
, NULL
);
303 pipe_sampler_view_reference(&sv_tbl
, NULL
);
304 return VDP_STATUS_RESOURCES
;
308 vlVdpOutputSurfacePutBitsYCbCr(VdpOutputSurface surface
,
309 VdpYCbCrFormat source_ycbcr_format
,
310 void const *const *source_data
,
311 uint32_t const *source_pitches
,
312 VdpRect
const *destination_rect
,
313 VdpCSCMatrix
const *csc_matrix
)
315 return VDP_STATUS_NO_IMPLEMENTATION
;
319 BlendFactorToPipe(VdpOutputSurfaceRenderBlendFactor factor
)
322 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ZERO
:
323 return PIPE_BLENDFACTOR_ZERO
;
324 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
:
325 return PIPE_BLENDFACTOR_ONE
;
326 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_COLOR
:
327 return PIPE_BLENDFACTOR_SRC_COLOR
;
328 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
:
329 return PIPE_BLENDFACTOR_INV_SRC_COLOR
;
330 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
:
331 return PIPE_BLENDFACTOR_SRC_ALPHA
;
332 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
:
333 return PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
334 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_DST_ALPHA
:
335 return PIPE_BLENDFACTOR_DST_ALPHA
;
336 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
:
337 return PIPE_BLENDFACTOR_INV_DST_ALPHA
;
338 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_DST_COLOR
:
339 return PIPE_BLENDFACTOR_DST_COLOR
;
340 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_DST_COLOR
:
341 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
342 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA_SATURATE
:
343 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
;
344 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_CONSTANT_COLOR
:
345 return PIPE_BLENDFACTOR_CONST_COLOR
;
346 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
:
347 return PIPE_BLENDFACTOR_INV_CONST_COLOR
;
348 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_CONSTANT_ALPHA
:
349 return PIPE_BLENDFACTOR_CONST_ALPHA
;
350 case VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
:
351 return PIPE_BLENDFACTOR_INV_CONST_ALPHA
;
354 return PIPE_BLENDFACTOR_ONE
;
359 BlendEquationToPipe(VdpOutputSurfaceRenderBlendEquation equation
)
362 case VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_SUBTRACT
:
363 return PIPE_BLEND_SUBTRACT
;
364 case VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_REVERSE_SUBTRACT
:
365 return PIPE_BLEND_REVERSE_SUBTRACT
;
366 case VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
:
367 return PIPE_BLEND_ADD
;
368 case VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_MIN
:
369 return PIPE_BLEND_MIN
;
370 case VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_MAX
:
371 return PIPE_BLEND_MAX
;
374 return PIPE_BLEND_ADD
;
379 BlenderToPipe(struct pipe_context
*context
,
380 VdpOutputSurfaceRenderBlendState
const *blend_state
)
382 struct pipe_blend_state blend
;
384 memset(&blend
, 0, sizeof blend
);
385 blend
.independent_blend_enable
= 0;
388 blend
.rt
[0].blend_enable
= 1;
389 blend
.rt
[0].rgb_src_factor
= BlendFactorToPipe(blend_state
->blend_factor_source_color
);
390 blend
.rt
[0].rgb_dst_factor
= BlendFactorToPipe(blend_state
->blend_factor_destination_color
);
391 blend
.rt
[0].alpha_src_factor
= BlendFactorToPipe(blend_state
->blend_factor_source_alpha
);
392 blend
.rt
[0].alpha_dst_factor
= BlendFactorToPipe(blend_state
->blend_factor_destination_alpha
);
393 blend
.rt
[0].rgb_func
= BlendEquationToPipe(blend_state
->blend_equation_color
);
394 blend
.rt
[0].alpha_func
= BlendEquationToPipe(blend_state
->blend_equation_alpha
);
396 blend
.rt
[0].blend_enable
= 0;
399 blend
.logicop_enable
= 0;
400 blend
.logicop_func
= PIPE_LOGICOP_CLEAR
;
401 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
404 return context
->create_blend_state(context
, &blend
);
408 vlVdpOutputSurfaceRenderOutputSurface(VdpOutputSurface destination_surface
,
409 VdpRect
const *destination_rect
,
410 VdpOutputSurface source_surface
,
411 VdpRect
const *source_rect
,
412 VdpColor
const *colors
,
413 VdpOutputSurfaceRenderBlendState
const *blend_state
,
416 vlVdpOutputSurface
*dst_vlsurface
;
417 vlVdpOutputSurface
*src_vlsurface
;
419 struct pipe_context
*context
;
420 struct vl_compositor
*compositor
;
422 struct pipe_video_rect src_rect
;
423 struct pipe_video_rect dst_rect
;
427 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] composing output surfaces\n");
429 dst_vlsurface
= vlGetDataHTAB(destination_surface
);
431 return VDP_STATUS_INVALID_HANDLE
;
433 src_vlsurface
= vlGetDataHTAB(source_surface
);
435 return VDP_STATUS_INVALID_HANDLE
;
437 if (dst_vlsurface
->device
!= src_vlsurface
->device
)
438 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
440 context
= dst_vlsurface
->device
->context
->pipe
;
441 compositor
= &dst_vlsurface
->device
->compositor
;
443 blend
= BlenderToPipe(context
, blend_state
);
445 vl_compositor_clear_layers(compositor
);
446 vl_compositor_set_layer_blend(compositor
, 0, blend
, false);
447 vl_compositor_set_rgba_layer(compositor
, 0, src_vlsurface
->sampler_view
,
448 RectToPipe(source_rect
, &src_rect
), NULL
);
449 vl_compositor_render(compositor
, dst_vlsurface
->surface
,
450 RectToPipe(destination_rect
, &dst_rect
), NULL
, false);
452 context
->delete_blend_state(context
, blend
);
454 return VDP_STATUS_OK
;
458 vlVdpOutputSurfaceRenderBitmapSurface(VdpOutputSurface destination_surface
,
459 VdpRect
const *destination_rect
,
460 VdpBitmapSurface source_surface
,
461 VdpRect
const *source_rect
,
462 VdpColor
const *colors
,
463 VdpOutputSurfaceRenderBlendState
const *blend_state
,
466 return VDP_STATUS_NO_IMPLEMENTATION
;