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 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 pipe_mutex_lock(vlsurface
->device
->mutex
);
242 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
243 if (!sampler_views
) {
244 pipe_mutex_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 pipe_mutex_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 pipe_mutex_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 struct pipe_context
*pipe
;
308 struct pipe_sampler_view
**sampler_views
;
311 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
313 return VDP_STATUS_INVALID_HANDLE
;
315 pipe
= p_surf
->device
->context
;
317 return VDP_STATUS_INVALID_HANDLE
;
319 if (!source_data
|| !source_pitches
)
320 return VDP_STATUS_INVALID_POINTER
;
322 pipe_mutex_lock(p_surf
->device
->mutex
);
323 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
325 /* destroy the old one */
326 if (p_surf
->video_buffer
)
327 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
329 /* adjust the template parameters */
330 p_surf
->templat
.buffer_format
= pformat
;
332 /* and try to create the video buffer with the new format */
333 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
335 /* stil no luck? ok forget it we don't support it */
336 if (!p_surf
->video_buffer
) {
337 pipe_mutex_unlock(p_surf
->device
->mutex
);
338 return VDP_STATUS_NO_IMPLEMENTATION
;
340 vlVdpVideoSurfaceClear(p_surf
);
343 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
344 if (!sampler_views
) {
345 pipe_mutex_unlock(p_surf
->device
->mutex
);
346 return VDP_STATUS_RESOURCES
;
349 for (i
= 0; i
< 3; ++i
) {
350 unsigned width
, height
;
351 struct pipe_sampler_view
*sv
= sampler_views
[i
];
352 if (!sv
|| !source_pitches
[i
]) continue;
354 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
356 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
357 struct pipe_box dst_box
= {
362 pipe
->texture_subdata(pipe
, sv
->texture
, 0,
363 PIPE_TRANSFER_WRITE
, &dst_box
,
364 source_data
[i
] + source_pitches
[i
] * j
,
365 source_pitches
[i
] * sv
->texture
->array_size
,
369 pipe_mutex_unlock(p_surf
->device
->mutex
);
371 return VDP_STATUS_OK
;
375 * Helper function to initially clear the VideoSurface after (re-)creation
378 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
380 struct pipe_context
*pipe
= vlsurf
->device
->context
;
381 struct pipe_surface
**surfaces
;
384 if (!vlsurf
->video_buffer
)
387 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
388 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
389 union pipe_color_union c
= {};
394 if (i
> !!vlsurf
->templat
.interlaced
)
395 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
397 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
398 surfaces
[i
]->width
, surfaces
[i
]->height
, false);
400 pipe
->flush(pipe
, NULL
, 0);
404 * Interop to mesa state tracker
406 struct pipe_video_buffer
*vlVdpVideoSurfaceGallium(VdpVideoSurface surface
)
408 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
412 pipe_mutex_lock(p_surf
->device
->mutex
);
413 if (p_surf
->video_buffer
== NULL
) {
414 struct pipe_context
*pipe
= p_surf
->device
->context
;
416 /* try to create a video buffer if we don't already have one */
417 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
419 pipe_mutex_unlock(p_surf
->device
->mutex
);
421 return p_surf
->video_buffer
;
424 VdpStatus
vlVdpVideoSurfaceDMABuf(VdpVideoSurface surface
,
425 VdpVideoSurfacePlane plane
,
426 struct VdpSurfaceDMABufDesc
*result
)
428 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
430 struct pipe_screen
*pscreen
;
431 struct winsys_handle whandle
;
433 struct pipe_surface
*surf
;
436 return VDP_STATUS_INVALID_HANDLE
;
439 return VDP_STATUS_INVALID_VALUE
;
442 return VDP_STATUS_INVALID_POINTER
;
444 memset(result
, 0, sizeof(*result
));
447 pipe_mutex_lock(p_surf
->device
->mutex
);
448 if (p_surf
->video_buffer
== NULL
) {
449 struct pipe_context
*pipe
= p_surf
->device
->context
;
451 /* try to create a video buffer if we don't already have one */
452 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
455 /* Check if surface match interop requirements */
456 if (p_surf
->video_buffer
== NULL
|| !p_surf
->video_buffer
->interlaced
||
457 p_surf
->video_buffer
->buffer_format
!= PIPE_FORMAT_NV12
) {
458 pipe_mutex_unlock(p_surf
->device
->mutex
);
459 return VDP_STATUS_NO_IMPLEMENTATION
;
462 surf
= p_surf
->video_buffer
->get_surfaces(p_surf
->video_buffer
)[plane
];
463 pipe_mutex_unlock(p_surf
->device
->mutex
);
466 return VDP_STATUS_RESOURCES
;
468 memset(&whandle
, 0, sizeof(struct winsys_handle
));
469 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
470 whandle
.layer
= surf
->u
.tex
.first_layer
;
472 pscreen
= surf
->texture
->screen
;
473 if (!pscreen
->resource_get_handle(pscreen
, p_surf
->device
->context
,
474 surf
->texture
, &whandle
,
475 PIPE_HANDLE_USAGE_READ_WRITE
))
476 return VDP_STATUS_NO_IMPLEMENTATION
;
478 result
->handle
= whandle
.handle
;
479 result
->width
= surf
->width
;
480 result
->height
= surf
->height
;
481 result
->offset
= whandle
.offset
;
482 result
->stride
= whandle
.stride
;
484 if (surf
->format
== PIPE_FORMAT_R8_UNORM
)
485 result
->format
= VDP_RGBA_FORMAT_R8
;
487 result
->format
= VDP_RGBA_FORMAT_R8G8
;
489 return VDP_STATUS_OK
;