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 mtx_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 assert(pipe_format_to_chroma_format(p_surf
->templat
.buffer_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 mtx_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 mtx_lock(&p_surf
->device
->mutex
);
142 if (p_surf
->video_buffer
)
143 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
144 mtx_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(pipe_format_to_chroma_format(p_surf
->video_buffer
->buffer_format
));
173 *width
= p_surf
->templat
.width
;
174 *height
= p_surf
->templat
.height
;
175 *chroma_type
= PipeToChroma(pipe_format_to_chroma_format(p_surf
->templat
.buffer_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 pipe_format_to_chroma_format(p_surf
->templat
.buffer_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 if (!destination_data
|| !destination_pitches
)
219 return VDP_STATUS_INVALID_POINTER
;
221 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
222 if (format
== PIPE_FORMAT_NONE
)
223 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
225 if (vlsurface
->video_buffer
== NULL
)
226 return VDP_STATUS_INVALID_VALUE
;
228 buffer_format
= vlsurface
->video_buffer
->buffer_format
;
229 if (format
!= buffer_format
) {
230 if (format
== PIPE_FORMAT_YV12
&& buffer_format
== PIPE_FORMAT_NV12
)
231 conversion
= CONVERSION_NV12_TO_YV12
;
232 else if (format
== PIPE_FORMAT_NV12
&& buffer_format
== PIPE_FORMAT_YV12
)
233 conversion
= CONVERSION_YV12_TO_NV12
;
234 else if ((format
== PIPE_FORMAT_YUYV
&& buffer_format
== PIPE_FORMAT_UYVY
) ||
235 (format
== PIPE_FORMAT_UYVY
&& buffer_format
== PIPE_FORMAT_YUYV
))
236 conversion
= CONVERSION_SWAP_YUYV_UYVY
;
238 return VDP_STATUS_NO_IMPLEMENTATION
;
241 mtx_lock(&vlsurface
->device
->mutex
);
242 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
243 if (!sampler_views
) {
244 mtx_unlock(&vlsurface
->device
->mutex
);
245 return VDP_STATUS_RESOURCES
;
248 for (i
= 0; i
< 3; ++i
) {
249 unsigned width
, height
;
250 struct pipe_sampler_view
*sv
= sampler_views
[i
];
253 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
255 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
256 struct pipe_box box
= {
260 struct pipe_transfer
*transfer
;
263 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
264 PIPE_TRANSFER_READ
, &box
, &transfer
);
266 mtx_unlock(&vlsurface
->device
->mutex
);
267 return VDP_STATUS_RESOURCES
;
270 if (conversion
== CONVERSION_NV12_TO_YV12
&& i
== 1) {
271 u_copy_nv12_to_yv12(destination_data
, destination_pitches
,
272 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
273 map
, box
.width
, box
.height
);
274 } else if (conversion
== CONVERSION_YV12_TO_NV12
&& i
> 0) {
275 u_copy_yv12_to_nv12(destination_data
, destination_pitches
,
276 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
277 map
, box
.width
, box
.height
);
278 } else if (conversion
== CONVERSION_SWAP_YUYV_UYVY
) {
279 u_copy_swap422_packed(destination_data
, destination_pitches
,
280 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
281 map
, box
.width
, box
.height
);
283 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
284 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
285 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
288 pipe_transfer_unmap(pipe
, transfer
);
291 mtx_unlock(&vlsurface
->device
->mutex
);
293 return VDP_STATUS_OK
;
297 * Copy image data from application memory in a specific YCbCr format to
301 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
302 VdpYCbCrFormat source_ycbcr_format
,
303 void const *const *source_data
,
304 uint32_t const *source_pitches
)
306 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
307 enum getbits_conversion conversion
= CONVERSION_NONE
;
308 struct pipe_context
*pipe
;
309 struct pipe_sampler_view
**sampler_views
;
311 unsigned usage
= PIPE_TRANSFER_WRITE
;
313 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
315 return VDP_STATUS_INVALID_HANDLE
;
317 pipe
= p_surf
->device
->context
;
319 return VDP_STATUS_INVALID_HANDLE
;
321 if (!source_data
|| !source_pitches
)
322 return VDP_STATUS_INVALID_POINTER
;
324 mtx_lock(&p_surf
->device
->mutex
);
326 if (p_surf
->video_buffer
== NULL
||
327 ((pformat
!= p_surf
->video_buffer
->buffer_format
))) {
328 enum pipe_format nformat
= pformat
;
329 struct pipe_screen
*screen
= pipe
->screen
;
331 /* Determine the most suitable format for the new surface */
332 if (!screen
->is_video_format_supported(screen
, nformat
,
333 PIPE_VIDEO_PROFILE_UNKNOWN
,
334 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
)) {
335 nformat
= screen
->get_video_param(screen
,
336 PIPE_VIDEO_PROFILE_UNKNOWN
,
337 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
338 PIPE_VIDEO_CAP_PREFERED_FORMAT
);
339 if (nformat
== PIPE_FORMAT_NONE
) {
340 mtx_unlock(&p_surf
->device
->mutex
);
341 return VDP_STATUS_NO_IMPLEMENTATION
;
345 if (p_surf
->video_buffer
== NULL
||
346 nformat
!= p_surf
->video_buffer
->buffer_format
) {
347 /* destroy the old one */
348 if (p_surf
->video_buffer
)
349 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
351 /* adjust the template parameters */
352 p_surf
->templat
.buffer_format
= nformat
;
353 if (nformat
== PIPE_FORMAT_YUYV
|| nformat
== PIPE_FORMAT_UYVY
)
354 p_surf
->templat
.interlaced
= false;
356 /* and try to create the video buffer with the new format */
357 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
359 /* stil no luck? ok forget it we don't support it */
360 if (!p_surf
->video_buffer
) {
361 mtx_unlock(&p_surf
->device
->mutex
);
362 return VDP_STATUS_NO_IMPLEMENTATION
;
364 vlVdpVideoSurfaceClear(p_surf
);
368 if (pformat
!= p_surf
->video_buffer
->buffer_format
) {
369 if (pformat
== PIPE_FORMAT_YV12
&&
370 p_surf
->video_buffer
->buffer_format
== PIPE_FORMAT_NV12
)
371 conversion
= CONVERSION_YV12_TO_NV12
;
373 mtx_unlock(&p_surf
->device
->mutex
);
374 return VDP_STATUS_NO_IMPLEMENTATION
;
378 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
379 if (!sampler_views
) {
380 mtx_unlock(&p_surf
->device
->mutex
);
381 return VDP_STATUS_RESOURCES
;
384 for (i
= 0; i
< 3; ++i
) {
385 unsigned width
, height
;
386 struct pipe_sampler_view
*sv
= sampler_views
[i
];
387 struct pipe_resource
*tex
;
388 if (!sv
|| !source_pitches
[i
]) continue;
391 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
393 for (j
= 0; j
< tex
->array_size
; ++j
) {
394 struct pipe_box dst_box
= {
399 if (conversion
== CONVERSION_YV12_TO_NV12
&& i
== 1) {
400 struct pipe_transfer
*transfer
;
403 map
= pipe
->transfer_map(pipe
, tex
, 0, usage
,
404 &dst_box
, &transfer
);
406 mtx_unlock(&p_surf
->device
->mutex
);
407 return VDP_STATUS_RESOURCES
;
410 u_copy_nv12_from_yv12(source_data
, source_pitches
,
411 i
, j
, transfer
->stride
, tex
->array_size
,
412 map
, dst_box
.width
, dst_box
.height
);
414 pipe_transfer_unmap(pipe
, transfer
);
416 pipe
->texture_subdata(pipe
, tex
, 0,
417 PIPE_TRANSFER_WRITE
, &dst_box
,
418 source_data
[i
] + source_pitches
[i
] * j
,
419 source_pitches
[i
] * tex
->array_size
,
423 * This surface has already been synced
426 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
429 mtx_unlock(&p_surf
->device
->mutex
);
431 return VDP_STATUS_OK
;
435 * Helper function to initially clear the VideoSurface after (re-)creation
438 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
440 struct pipe_context
*pipe
= vlsurf
->device
->context
;
441 struct pipe_surface
**surfaces
;
444 if (!vlsurf
->video_buffer
)
447 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
448 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
449 union pipe_color_union c
= {};
454 if (i
> !!vlsurf
->templat
.interlaced
)
455 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
457 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
458 surfaces
[i
]->width
, surfaces
[i
]->height
, false);
460 pipe
->flush(pipe
, NULL
, 0);
464 * Interop to mesa state tracker
466 struct pipe_video_buffer
*vlVdpVideoSurfaceGallium(VdpVideoSurface surface
)
468 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
472 mtx_lock(&p_surf
->device
->mutex
);
473 if (p_surf
->video_buffer
== NULL
) {
474 struct pipe_context
*pipe
= p_surf
->device
->context
;
476 /* try to create a video buffer if we don't already have one */
477 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
479 mtx_unlock(&p_surf
->device
->mutex
);
481 return p_surf
->video_buffer
;
484 VdpStatus
vlVdpVideoSurfaceDMABuf(VdpVideoSurface surface
,
485 VdpVideoSurfacePlane plane
,
486 struct VdpSurfaceDMABufDesc
*result
)
488 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
490 struct pipe_screen
*pscreen
;
491 struct winsys_handle whandle
;
493 struct pipe_surface
*surf
;
496 return VDP_STATUS_INVALID_HANDLE
;
499 return VDP_STATUS_INVALID_VALUE
;
502 return VDP_STATUS_INVALID_POINTER
;
504 memset(result
, 0, sizeof(*result
));
507 mtx_lock(&p_surf
->device
->mutex
);
508 if (p_surf
->video_buffer
== NULL
) {
509 struct pipe_context
*pipe
= p_surf
->device
->context
;
511 /* try to create a video buffer if we don't already have one */
512 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
515 /* Check if surface match interop requirements */
516 if (p_surf
->video_buffer
== NULL
|| !p_surf
->video_buffer
->interlaced
||
517 p_surf
->video_buffer
->buffer_format
!= PIPE_FORMAT_NV12
) {
518 mtx_unlock(&p_surf
->device
->mutex
);
519 return VDP_STATUS_NO_IMPLEMENTATION
;
522 surf
= p_surf
->video_buffer
->get_surfaces(p_surf
->video_buffer
)[plane
];
524 mtx_unlock(&p_surf
->device
->mutex
);
525 return VDP_STATUS_RESOURCES
;
528 memset(&whandle
, 0, sizeof(struct winsys_handle
));
529 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
530 whandle
.layer
= surf
->u
.tex
.first_layer
;
532 pscreen
= surf
->texture
->screen
;
533 if (!pscreen
->resource_get_handle(pscreen
, p_surf
->device
->context
,
534 surf
->texture
, &whandle
,
535 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
)) {
536 mtx_unlock(&p_surf
->device
->mutex
);
537 return VDP_STATUS_NO_IMPLEMENTATION
;
540 mtx_unlock(&p_surf
->device
->mutex
);
542 result
->handle
= whandle
.handle
;
543 result
->width
= surf
->width
;
544 result
->height
= surf
->height
;
545 result
->offset
= whandle
.offset
;
546 result
->stride
= whandle
.stride
;
548 if (surf
->format
== PIPE_FORMAT_R8_UNORM
)
549 result
->format
= VDP_RGBA_FORMAT_R8
;
551 result
->format
= VDP_RGBA_FORMAT_R8G8
;
553 return VDP_STATUS_OK
;