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 VMWARE 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 "util/u_surface.h"
37 #include "util/u_video.h"
38 #include "vl/vl_defines.h"
40 #include "state_tracker/drm_driver.h"
42 #include "vdpau_private.h"
44 enum getbits_conversion
{
46 CONVERSION_NV12_TO_YV12
,
47 CONVERSION_YV12_TO_NV12
,
48 CONVERSION_SWAP_YUYV_UYVY
,
52 * Create a VdpVideoSurface.
55 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
56 uint32_t width
, uint32_t height
,
57 VdpVideoSurface
*surface
)
59 struct pipe_context
*pipe
;
63 if (!(width
&& height
)) {
64 ret
= VDP_STATUS_INVALID_SIZE
;
68 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
70 ret
= VDP_STATUS_RESOURCES
;
74 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
76 ret
= VDP_STATUS_INVALID_HANDLE
;
80 DeviceReference(&p_surf
->device
, dev
);
83 pipe_mutex_lock(dev
->mutex
);
84 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
85 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
88 PIPE_VIDEO_PROFILE_UNKNOWN
,
89 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
90 PIPE_VIDEO_CAP_PREFERED_FORMAT
92 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
93 p_surf
->templat
.width
= width
;
94 p_surf
->templat
.height
= height
;
95 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
98 PIPE_VIDEO_PROFILE_UNKNOWN
,
99 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
100 PIPE_VIDEO_CAP_PREFERS_INTERLACED
102 if (p_surf
->templat
.buffer_format
!= PIPE_FORMAT_NONE
)
103 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
105 /* do not mandate early allocation of a video buffer */
106 vlVdpVideoSurfaceClear(p_surf
);
107 pipe_mutex_unlock(dev
->mutex
);
109 *surface
= vlAddDataHTAB(p_surf
);
111 ret
= VDP_STATUS_ERROR
;
115 return VDP_STATUS_OK
;
118 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
121 DeviceReference(&p_surf
->device
, NULL
);
130 * Destroy a VdpVideoSurface.
133 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
135 vlVdpSurface
*p_surf
;
137 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
139 return VDP_STATUS_INVALID_HANDLE
;
141 pipe_mutex_lock(p_surf
->device
->mutex
);
142 if (p_surf
->video_buffer
)
143 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
144 pipe_mutex_unlock(p_surf
->device
->mutex
);
146 vlRemoveDataHTAB(surface
);
147 DeviceReference(&p_surf
->device
, NULL
);
150 return VDP_STATUS_OK
;
154 * Retrieve the parameters used to create a VdpVideoSurface.
157 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
158 VdpChromaType
*chroma_type
,
159 uint32_t *width
, uint32_t *height
)
161 if (!(width
&& height
&& chroma_type
))
162 return VDP_STATUS_INVALID_POINTER
;
164 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
166 return VDP_STATUS_INVALID_HANDLE
;
168 if (p_surf
->video_buffer
) {
169 *width
= p_surf
->video_buffer
->width
;
170 *height
= p_surf
->video_buffer
->height
;
171 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
173 *width
= p_surf
->templat
.width
;
174 *height
= p_surf
->templat
.height
;
175 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
178 return VDP_STATUS_OK
;
182 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
183 unsigned *width
, unsigned *height
)
185 *width
= p_surf
->templat
.width
;
186 *height
= p_surf
->templat
.height
;
188 vl_video_buffer_adjust_size(width
, height
, component
,
189 p_surf
->templat
.chroma_format
,
190 p_surf
->templat
.interlaced
);
194 * Copy image data from a VdpVideoSurface to application memory in a specified
198 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
199 VdpYCbCrFormat destination_ycbcr_format
,
200 void *const *destination_data
,
201 uint32_t const *destination_pitches
)
203 vlVdpSurface
*vlsurface
;
204 struct pipe_context
*pipe
;
205 enum pipe_format format
, buffer_format
;
206 struct pipe_sampler_view
**sampler_views
;
207 enum getbits_conversion conversion
= CONVERSION_NONE
;
210 vlsurface
= vlGetDataHTAB(surface
);
212 return VDP_STATUS_INVALID_HANDLE
;
214 pipe
= vlsurface
->device
->context
;
216 return VDP_STATUS_INVALID_HANDLE
;
218 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
219 if (format
== PIPE_FORMAT_NONE
)
220 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
222 if (vlsurface
->video_buffer
== NULL
)
223 return VDP_STATUS_INVALID_VALUE
;
225 buffer_format
= vlsurface
->video_buffer
->buffer_format
;
226 if (format
!= buffer_format
) {
227 if (format
== PIPE_FORMAT_YV12
&& buffer_format
== PIPE_FORMAT_NV12
)
228 conversion
= CONVERSION_NV12_TO_YV12
;
229 else if (format
== PIPE_FORMAT_NV12
&& buffer_format
== PIPE_FORMAT_YV12
)
230 conversion
= CONVERSION_YV12_TO_NV12
;
231 else if ((format
== PIPE_FORMAT_YUYV
&& buffer_format
== PIPE_FORMAT_UYVY
) ||
232 (format
== PIPE_FORMAT_UYVY
&& buffer_format
== PIPE_FORMAT_YUYV
))
233 conversion
= CONVERSION_SWAP_YUYV_UYVY
;
235 return VDP_STATUS_NO_IMPLEMENTATION
;
238 pipe_mutex_lock(vlsurface
->device
->mutex
);
239 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
240 if (!sampler_views
) {
241 pipe_mutex_unlock(vlsurface
->device
->mutex
);
242 return VDP_STATUS_RESOURCES
;
245 for (i
= 0; i
< 3; ++i
) {
246 unsigned width
, height
;
247 struct pipe_sampler_view
*sv
= sampler_views
[i
];
250 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
252 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
253 struct pipe_box box
= {
257 struct pipe_transfer
*transfer
;
260 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
261 PIPE_TRANSFER_READ
, &box
, &transfer
);
263 pipe_mutex_unlock(vlsurface
->device
->mutex
);
264 return VDP_STATUS_RESOURCES
;
267 if (conversion
== CONVERSION_NV12_TO_YV12
&& i
== 1) {
268 u_copy_nv12_to_yv12(destination_data
, destination_pitches
,
269 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
270 map
, box
.width
, box
.height
);
271 } else if (conversion
== CONVERSION_YV12_TO_NV12
&& i
> 0) {
272 u_copy_yv12_to_nv12(destination_data
, destination_pitches
,
273 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
274 map
, box
.width
, box
.height
);
275 } else if (conversion
== CONVERSION_SWAP_YUYV_UYVY
) {
276 u_copy_swap422_packed(destination_data
, destination_pitches
,
277 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
278 map
, box
.width
, box
.height
);
280 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
281 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
282 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
285 pipe_transfer_unmap(pipe
, transfer
);
288 pipe_mutex_unlock(vlsurface
->device
->mutex
);
290 return VDP_STATUS_OK
;
294 * Copy image data from application memory in a specific YCbCr format to
298 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
299 VdpYCbCrFormat source_ycbcr_format
,
300 void const *const *source_data
,
301 uint32_t const *source_pitches
)
303 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
304 struct pipe_context
*pipe
;
305 struct pipe_sampler_view
**sampler_views
;
308 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
310 return VDP_STATUS_INVALID_HANDLE
;
312 pipe
= p_surf
->device
->context
;
314 return VDP_STATUS_INVALID_HANDLE
;
316 pipe_mutex_lock(p_surf
->device
->mutex
);
317 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
319 /* destroy the old one */
320 if (p_surf
->video_buffer
)
321 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
323 /* adjust the template parameters */
324 p_surf
->templat
.buffer_format
= pformat
;
326 /* and try to create the video buffer with the new format */
327 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
329 /* stil no luck? ok forget it we don't support it */
330 if (!p_surf
->video_buffer
) {
331 pipe_mutex_unlock(p_surf
->device
->mutex
);
332 return VDP_STATUS_NO_IMPLEMENTATION
;
334 vlVdpVideoSurfaceClear(p_surf
);
337 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
338 if (!sampler_views
) {
339 pipe_mutex_unlock(p_surf
->device
->mutex
);
340 return VDP_STATUS_RESOURCES
;
343 for (i
= 0; i
< 3; ++i
) {
344 unsigned width
, height
;
345 struct pipe_sampler_view
*sv
= sampler_views
[i
];
346 if (!sv
|| !source_pitches
[i
]) continue;
348 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
350 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
351 struct pipe_box dst_box
= {
356 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
357 PIPE_TRANSFER_WRITE
, &dst_box
,
358 source_data
[i
] + source_pitches
[i
] * j
,
359 source_pitches
[i
] * sv
->texture
->array_size
,
363 pipe_mutex_unlock(p_surf
->device
->mutex
);
365 return VDP_STATUS_OK
;
369 * Helper function to initially clear the VideoSurface after (re-)creation
372 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
374 struct pipe_context
*pipe
= vlsurf
->device
->context
;
375 struct pipe_surface
**surfaces
;
378 if (!vlsurf
->video_buffer
)
381 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
382 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
383 union pipe_color_union c
= {};
388 if (i
> !!vlsurf
->templat
.interlaced
)
389 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
391 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
392 surfaces
[i
]->width
, surfaces
[i
]->height
);
394 pipe
->flush(pipe
, NULL
, 0);
398 * Interop to mesa state tracker
400 struct pipe_video_buffer
*vlVdpVideoSurfaceGallium(VdpVideoSurface surface
)
402 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
406 pipe_mutex_lock(p_surf
->device
->mutex
);
407 if (p_surf
->video_buffer
== NULL
) {
408 struct pipe_context
*pipe
= p_surf
->device
->context
;
410 /* try to create a video buffer if we don't already have one */
411 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
413 pipe_mutex_unlock(p_surf
->device
->mutex
);
415 return p_surf
->video_buffer
;
418 VdpStatus
vlVdpVideoSurfaceDMABuf(VdpVideoSurface surface
,
419 VdpVideoSurfacePlane plane
,
420 struct VdpSurfaceDMABufDesc
*result
)
422 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
424 struct pipe_screen
*pscreen
;
425 struct winsys_handle whandle
;
427 struct pipe_surface
*surf
;
430 return VDP_STATUS_INVALID_HANDLE
;
433 return VDP_STATUS_INVALID_VALUE
;
436 return VDP_STATUS_INVALID_POINTER
;
438 memset(result
, 0, sizeof(*result
));
441 pipe_mutex_lock(p_surf
->device
->mutex
);
442 if (p_surf
->video_buffer
== NULL
) {
443 struct pipe_context
*pipe
= p_surf
->device
->context
;
445 /* try to create a video buffer if we don't already have one */
446 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
449 /* Check if surface match interop requirements */
450 if (p_surf
->video_buffer
== NULL
|| !p_surf
->video_buffer
->interlaced
||
451 p_surf
->video_buffer
->buffer_format
!= PIPE_FORMAT_NV12
) {
452 pipe_mutex_unlock(p_surf
->device
->mutex
);
453 return VDP_STATUS_NO_IMPLEMENTATION
;
456 surf
= p_surf
->video_buffer
->get_surfaces(p_surf
->video_buffer
)[plane
];
457 pipe_mutex_unlock(p_surf
->device
->mutex
);
460 return VDP_STATUS_RESOURCES
;
462 memset(&whandle
, 0, sizeof(struct winsys_handle
));
463 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
464 whandle
.layer
= surf
->u
.tex
.first_layer
;
466 pscreen
= surf
->texture
->screen
;
467 if (!pscreen
->resource_get_handle(pscreen
, surf
->texture
, &whandle
,
468 PIPE_HANDLE_USAGE_READ_WRITE
))
469 return VDP_STATUS_NO_IMPLEMENTATION
;
471 result
->handle
= whandle
.handle
;
472 result
->width
= surf
->width
;
473 result
->height
= surf
->height
;
474 result
->offset
= whandle
.offset
;
475 result
->stride
= whandle
.stride
;
477 if (surf
->format
== PIPE_FORMAT_R8_UNORM
)
478 result
->format
= VDP_RGBA_FORMAT_R8
;
480 result
->format
= VDP_RGBA_FORMAT_R8G8
;
482 return VDP_STATUS_OK
;