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"
37 #include "vdpau_private.h"
40 * Create a VdpVideoSurface.
43 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
44 uint32_t width
, uint32_t height
,
45 VdpVideoSurface
*surface
)
47 struct pipe_context
*pipe
;
51 if (!(width
&& height
)) {
52 ret
= VDP_STATUS_INVALID_SIZE
;
56 if (!vlCreateHTAB()) {
57 ret
= VDP_STATUS_RESOURCES
;
61 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
63 ret
= VDP_STATUS_RESOURCES
;
67 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
69 ret
= VDP_STATUS_INVALID_HANDLE
;
76 pipe_mutex_lock(dev
->mutex
);
77 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
78 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
81 PIPE_VIDEO_PROFILE_UNKNOWN
,
82 PIPE_VIDEO_CAP_PREFERED_FORMAT
84 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
85 p_surf
->templat
.width
= width
;
86 p_surf
->templat
.height
= height
;
87 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
90 PIPE_VIDEO_PROFILE_UNKNOWN
,
91 PIPE_VIDEO_CAP_PREFERS_INTERLACED
93 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
94 pipe_mutex_unlock(dev
->mutex
);
96 *surface
= vlAddDataHTAB(p_surf
);
98 ret
= VDP_STATUS_ERROR
;
102 return VDP_STATUS_OK
;
105 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
117 * Destroy a VdpVideoSurface.
120 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
122 vlVdpSurface
*p_surf
;
124 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
126 return VDP_STATUS_INVALID_HANDLE
;
128 pipe_mutex_lock(p_surf
->device
->mutex
);
129 if (p_surf
->video_buffer
)
130 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
131 pipe_mutex_unlock(p_surf
->device
->mutex
);
134 return VDP_STATUS_OK
;
138 * Retrieve the parameters used to create a VdpVideoSurface.
141 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
142 VdpChromaType
*chroma_type
,
143 uint32_t *width
, uint32_t *height
)
145 if (!(width
&& height
&& chroma_type
))
146 return VDP_STATUS_INVALID_POINTER
;
148 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
150 return VDP_STATUS_INVALID_HANDLE
;
152 if (p_surf
->video_buffer
) {
153 *width
= p_surf
->video_buffer
->width
;
154 *height
= p_surf
->video_buffer
->height
;
155 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
157 *width
= p_surf
->templat
.width
;
158 *height
= p_surf
->templat
.height
;
159 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
162 return VDP_STATUS_OK
;
166 * Copy image data from a VdpVideoSurface to application memory in a specified
170 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
171 VdpYCbCrFormat destination_ycbcr_format
,
172 void *const *destination_data
,
173 uint32_t const *destination_pitches
)
175 vlVdpSurface
*vlsurface
;
176 struct pipe_context
*pipe
;
177 enum pipe_format format
;
178 struct pipe_sampler_view
**sampler_views
;
181 vlsurface
= vlGetDataHTAB(surface
);
183 return VDP_STATUS_INVALID_HANDLE
;
185 pipe
= vlsurface
->device
->context
;
187 return VDP_STATUS_INVALID_HANDLE
;
189 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
190 if (format
== PIPE_FORMAT_NONE
)
191 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
193 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
194 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
196 pipe_mutex_lock(vlsurface
->device
->mutex
);
197 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
198 if (!sampler_views
) {
199 pipe_mutex_unlock(vlsurface
->device
->mutex
);
200 return VDP_STATUS_RESOURCES
;
203 for (i
= 0; i
< 3; ++i
) {
204 struct pipe_sampler_view
*sv
= sampler_views
[i
];
207 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
208 struct pipe_box box
= {
210 sv
->texture
->width0
, sv
->texture
->height0
, 1
212 struct pipe_transfer
*transfer
;
215 transfer
= pipe
->get_transfer(pipe
, sv
->texture
, 0, PIPE_TRANSFER_READ
, &box
);
216 if (transfer
== NULL
) {
217 pipe_mutex_unlock(vlsurface
->device
->mutex
);
218 return VDP_STATUS_RESOURCES
;
221 map
= pipe_transfer_map(pipe
, transfer
);
223 pipe_transfer_destroy(pipe
, transfer
);
224 pipe_mutex_unlock(vlsurface
->device
->mutex
);
225 return VDP_STATUS_RESOURCES
;
228 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
229 destination_pitches
[i
] * sv
->texture
->depth0
, 0, 0,
230 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
232 pipe_transfer_unmap(pipe
, transfer
);
233 pipe_transfer_destroy(pipe
, transfer
);
236 pipe_mutex_unlock(vlsurface
->device
->mutex
);
238 return VDP_STATUS_OK
;
242 * Copy image data from application memory in a specific YCbCr format to
246 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
247 VdpYCbCrFormat source_ycbcr_format
,
248 void const *const *source_data
,
249 uint32_t const *source_pitches
)
251 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
252 struct pipe_context
*pipe
;
253 struct pipe_sampler_view
**sampler_views
;
257 return VDP_STATUS_RESOURCES
;
259 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
261 return VDP_STATUS_INVALID_HANDLE
;
263 pipe
= p_surf
->device
->context
;
265 return VDP_STATUS_INVALID_HANDLE
;
267 pipe_mutex_lock(p_surf
->device
->mutex
);
268 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
270 /* destroy the old one */
271 if (p_surf
->video_buffer
)
272 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
274 /* adjust the template parameters */
275 p_surf
->templat
.buffer_format
= pformat
;
277 /* and try to create the video buffer with the new format */
278 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
280 /* stil no luck? ok forget it we don't support it */
281 if (!p_surf
->video_buffer
) {
282 pipe_mutex_unlock(p_surf
->device
->mutex
);
283 return VDP_STATUS_NO_IMPLEMENTATION
;
287 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
288 if (!sampler_views
) {
289 pipe_mutex_unlock(p_surf
->device
->mutex
);
290 return VDP_STATUS_RESOURCES
;
293 for (i
= 0; i
< 3; ++i
) {
294 struct pipe_sampler_view
*sv
= sampler_views
[i
];
295 if (!sv
|| !source_pitches
[i
]) continue;
297 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
298 struct pipe_box dst_box
= {
300 sv
->texture
->width0
, sv
->texture
->height0
, 1
303 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
304 PIPE_TRANSFER_WRITE
, &dst_box
,
305 source_data
[i
] + source_pitches
[i
] * j
,
306 source_pitches
[i
] * sv
->texture
->depth0
,
310 pipe_mutex_unlock(p_surf
->device
->mutex
);
312 return VDP_STATUS_OK
;