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 **************************************************************************/
31 #include "pipe/p_state.h"
33 #include "util/u_memory.h"
34 #include "util/u_debug.h"
35 #include "util/u_rect.h"
36 #include "vl/vl_defines.h"
38 #include "vdpau_private.h"
41 * Create a VdpVideoSurface.
44 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
45 uint32_t width
, uint32_t height
,
46 VdpVideoSurface
*surface
)
48 struct pipe_context
*pipe
;
52 if (!(width
&& height
)) {
53 ret
= VDP_STATUS_INVALID_SIZE
;
57 if (!vlCreateHTAB()) {
58 ret
= VDP_STATUS_RESOURCES
;
62 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
64 ret
= VDP_STATUS_RESOURCES
;
68 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
70 ret
= VDP_STATUS_INVALID_HANDLE
;
77 pipe_mutex_lock(dev
->mutex
);
78 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
79 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
82 PIPE_VIDEO_PROFILE_UNKNOWN
,
83 PIPE_VIDEO_CAP_PREFERED_FORMAT
85 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
86 p_surf
->templat
.width
= width
;
87 p_surf
->templat
.height
= height
;
88 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
91 PIPE_VIDEO_PROFILE_UNKNOWN
,
92 PIPE_VIDEO_CAP_PREFERS_INTERLACED
94 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
95 vlVdpVideoSurfaceClear(p_surf
);
96 pipe_mutex_unlock(dev
->mutex
);
98 *surface
= vlAddDataHTAB(p_surf
);
100 ret
= VDP_STATUS_ERROR
;
104 return VDP_STATUS_OK
;
107 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
119 * Destroy a VdpVideoSurface.
122 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
124 vlVdpSurface
*p_surf
;
126 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
128 return VDP_STATUS_INVALID_HANDLE
;
130 pipe_mutex_lock(p_surf
->device
->mutex
);
131 if (p_surf
->video_buffer
)
132 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
133 pipe_mutex_unlock(p_surf
->device
->mutex
);
136 return VDP_STATUS_OK
;
140 * Retrieve the parameters used to create a VdpVideoSurface.
143 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
144 VdpChromaType
*chroma_type
,
145 uint32_t *width
, uint32_t *height
)
147 if (!(width
&& height
&& chroma_type
))
148 return VDP_STATUS_INVALID_POINTER
;
150 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
152 return VDP_STATUS_INVALID_HANDLE
;
154 if (p_surf
->video_buffer
) {
155 *width
= p_surf
->video_buffer
->width
;
156 *height
= p_surf
->video_buffer
->height
;
157 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
159 *width
= p_surf
->templat
.width
;
160 *height
= p_surf
->templat
.height
;
161 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
164 return VDP_STATUS_OK
;
168 * Copy image data from a VdpVideoSurface to application memory in a specified
172 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
173 VdpYCbCrFormat destination_ycbcr_format
,
174 void *const *destination_data
,
175 uint32_t const *destination_pitches
)
177 vlVdpSurface
*vlsurface
;
178 struct pipe_context
*pipe
;
179 enum pipe_format format
;
180 struct pipe_sampler_view
**sampler_views
;
183 vlsurface
= vlGetDataHTAB(surface
);
185 return VDP_STATUS_INVALID_HANDLE
;
187 pipe
= vlsurface
->device
->context
;
189 return VDP_STATUS_INVALID_HANDLE
;
191 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
192 if (format
== PIPE_FORMAT_NONE
)
193 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
195 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
196 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
198 pipe_mutex_lock(vlsurface
->device
->mutex
);
199 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
200 if (!sampler_views
) {
201 pipe_mutex_unlock(vlsurface
->device
->mutex
);
202 return VDP_STATUS_RESOURCES
;
205 for (i
= 0; i
< 3; ++i
) {
206 struct pipe_sampler_view
*sv
= sampler_views
[i
];
209 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
210 struct pipe_box box
= {
212 sv
->texture
->width0
, sv
->texture
->height0
, 1
214 struct pipe_transfer
*transfer
;
217 transfer
= pipe
->get_transfer(pipe
, sv
->texture
, 0, PIPE_TRANSFER_READ
, &box
);
218 if (transfer
== NULL
) {
219 pipe_mutex_unlock(vlsurface
->device
->mutex
);
220 return VDP_STATUS_RESOURCES
;
223 map
= pipe_transfer_map(pipe
, transfer
);
225 pipe_transfer_destroy(pipe
, transfer
);
226 pipe_mutex_unlock(vlsurface
->device
->mutex
);
227 return VDP_STATUS_RESOURCES
;
230 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
231 destination_pitches
[i
] * sv
->texture
->depth0
, 0, 0,
232 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
234 pipe_transfer_unmap(pipe
, transfer
);
235 pipe_transfer_destroy(pipe
, transfer
);
238 pipe_mutex_unlock(vlsurface
->device
->mutex
);
240 return VDP_STATUS_OK
;
244 * Copy image data from application memory in a specific YCbCr format to
248 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
249 VdpYCbCrFormat source_ycbcr_format
,
250 void const *const *source_data
,
251 uint32_t const *source_pitches
)
253 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
254 struct pipe_context
*pipe
;
255 struct pipe_sampler_view
**sampler_views
;
259 return VDP_STATUS_RESOURCES
;
261 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
263 return VDP_STATUS_INVALID_HANDLE
;
265 pipe
= p_surf
->device
->context
;
267 return VDP_STATUS_INVALID_HANDLE
;
269 pipe_mutex_lock(p_surf
->device
->mutex
);
270 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
272 /* destroy the old one */
273 if (p_surf
->video_buffer
)
274 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
276 /* adjust the template parameters */
277 p_surf
->templat
.buffer_format
= pformat
;
279 /* and try to create the video buffer with the new format */
280 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
282 /* stil no luck? ok forget it we don't support it */
283 if (!p_surf
->video_buffer
) {
284 pipe_mutex_unlock(p_surf
->device
->mutex
);
285 return VDP_STATUS_NO_IMPLEMENTATION
;
287 vlVdpVideoSurfaceClear(p_surf
);
290 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
291 if (!sampler_views
) {
292 pipe_mutex_unlock(p_surf
->device
->mutex
);
293 return VDP_STATUS_RESOURCES
;
296 for (i
= 0; i
< 3; ++i
) {
297 struct pipe_sampler_view
*sv
= sampler_views
[i
];
298 if (!sv
|| !source_pitches
[i
]) continue;
300 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
301 struct pipe_box dst_box
= {
303 sv
->texture
->width0
, sv
->texture
->height0
, 1
306 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
307 PIPE_TRANSFER_WRITE
, &dst_box
,
308 source_data
[i
] + source_pitches
[i
] * j
,
309 source_pitches
[i
] * sv
->texture
->depth0
,
313 pipe_mutex_unlock(p_surf
->device
->mutex
);
315 return VDP_STATUS_OK
;
319 * Helper function to initially clear the VideoSurface after (re-)creation
322 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
324 struct pipe_context
*pipe
= vlsurf
->device
->context
;
325 struct pipe_surface
**surfaces
;
328 if (!vlsurf
->video_buffer
)
331 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
332 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
333 union pipe_color_union black
= {};
338 pipe
->clear_render_target(pipe
, surfaces
[i
], &black
, 0, 0,
339 surfaces
[i
]->width
, surfaces
[i
]->height
);
341 pipe
->flush(pipe
, NULL
);