1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
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 THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 **************************************************************************/
29 #include "pipe/p_screen.h"
30 #include "state_tracker/drm_driver.h"
31 #include "util/u_memory.h"
32 #include "util/u_handle_table.h"
33 #include "util/u_transfer.h"
34 #include "vl/vl_winsys.h"
36 #include "va_private.h"
39 vlVaCreateBuffer(VADriverContextP ctx
, VAContextID context
, VABufferType type
,
40 unsigned int size
, unsigned int num_elements
, void *data
,
46 return VA_STATUS_ERROR_INVALID_CONTEXT
;
48 buf
= CALLOC(1, sizeof(vlVaBuffer
));
50 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
54 buf
->num_elements
= num_elements
;
55 buf
->data
= MALLOC(size
* num_elements
);
59 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
63 memcpy(buf
->data
, data
, size
* num_elements
);
65 *buf_id
= handle_table_add(VL_VA_DRIVER(ctx
)->htab
, buf
);
67 return VA_STATUS_SUCCESS
;
71 vlVaBufferSetNumElements(VADriverContextP ctx
, VABufferID buf_id
,
72 unsigned int num_elements
)
77 return VA_STATUS_ERROR_INVALID_CONTEXT
;
79 buf
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
81 return VA_STATUS_ERROR_INVALID_BUFFER
;
83 if (buf
->derived_surface
.resource
)
84 return VA_STATUS_ERROR_INVALID_BUFFER
;
86 buf
->data
= REALLOC(buf
->data
, buf
->size
* buf
->num_elements
,
87 buf
->size
* num_elements
);
88 buf
->num_elements
= num_elements
;
91 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
93 return VA_STATUS_SUCCESS
;
97 vlVaMapBuffer(VADriverContextP ctx
, VABufferID buf_id
, void **pbuff
)
103 return VA_STATUS_ERROR_INVALID_CONTEXT
;
105 drv
= VL_VA_DRIVER(ctx
);
107 return VA_STATUS_ERROR_INVALID_CONTEXT
;
110 return VA_STATUS_ERROR_INVALID_PARAMETER
;
112 buf
= handle_table_get(drv
->htab
, buf_id
);
114 return VA_STATUS_ERROR_INVALID_BUFFER
;
116 if (buf
->export_refcount
> 0)
117 return VA_STATUS_ERROR_INVALID_BUFFER
;
119 if (buf
->derived_surface
.resource
) {
120 *pbuff
= pipe_buffer_map(drv
->pipe
, buf
->derived_surface
.resource
,
122 &buf
->derived_surface
.transfer
);
124 if (!buf
->derived_surface
.transfer
|| !*pbuff
)
125 return VA_STATUS_ERROR_INVALID_BUFFER
;
131 return VA_STATUS_SUCCESS
;
135 vlVaUnmapBuffer(VADriverContextP ctx
, VABufferID buf_id
)
141 return VA_STATUS_ERROR_INVALID_CONTEXT
;
143 drv
= VL_VA_DRIVER(ctx
);
145 return VA_STATUS_ERROR_INVALID_CONTEXT
;
147 buf
= handle_table_get(drv
->htab
, buf_id
);
149 return VA_STATUS_ERROR_INVALID_BUFFER
;
151 if (buf
->export_refcount
> 0)
152 return VA_STATUS_ERROR_INVALID_BUFFER
;
154 if (buf
->derived_surface
.resource
) {
155 if (!buf
->derived_surface
.transfer
)
156 return VA_STATUS_ERROR_INVALID_BUFFER
;
158 pipe_buffer_unmap(drv
->pipe
, buf
->derived_surface
.transfer
);
159 buf
->derived_surface
.transfer
= NULL
;
162 return VA_STATUS_SUCCESS
;
166 vlVaDestroyBuffer(VADriverContextP ctx
, VABufferID buf_id
)
171 return VA_STATUS_ERROR_INVALID_CONTEXT
;
173 buf
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
175 return VA_STATUS_ERROR_INVALID_BUFFER
;
177 if (buf
->derived_surface
.resource
) {
178 if (buf
->export_refcount
> 0)
179 return VA_STATUS_ERROR_INVALID_BUFFER
;
181 pipe_resource_reference(&buf
->derived_surface
.resource
, NULL
);
186 handle_table_remove(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
188 return VA_STATUS_SUCCESS
;
192 vlVaBufferInfo(VADriverContextP ctx
, VABufferID buf_id
, VABufferType
*type
,
193 unsigned int *size
, unsigned int *num_elements
)
198 return VA_STATUS_ERROR_INVALID_CONTEXT
;
200 buf
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
202 return VA_STATUS_ERROR_INVALID_BUFFER
;
206 *num_elements
= buf
->num_elements
;
208 return VA_STATUS_SUCCESS
;
212 vlVaAcquireBufferHandle(VADriverContextP ctx
, VABufferID buf_id
,
213 VABufferInfo
*out_buf_info
)
219 struct pipe_screen
*screen
;
221 /* List of supported memory types, in preferred order. */
222 static const uint32_t mem_types
[] = {
223 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME
,
228 return VA_STATUS_ERROR_INVALID_CONTEXT
;
230 buf
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
233 return VA_STATUS_ERROR_INVALID_BUFFER
;
235 /* Only VA surface|image like buffers are supported for now .*/
236 if (buf
->type
!= VAImageBufferType
)
237 return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE
;
240 return VA_STATUS_ERROR_INVALID_PARAMETER
;
242 if (!out_buf_info
->mem_type
)
243 mem_type
= mem_types
[0];
246 for (i
= 0; mem_types
[i
] != 0; i
++) {
247 if (out_buf_info
->mem_type
& mem_types
[i
]) {
248 mem_type
= out_buf_info
->mem_type
;
253 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE
;
256 if (!buf
->derived_surface
.resource
)
257 return VA_STATUS_ERROR_INVALID_BUFFER
;
259 drv
= VL_VA_DRIVER(ctx
);
260 screen
= VL_VA_PSCREEN(ctx
);
262 if (buf
->export_refcount
> 0) {
263 if (buf
->export_state
.mem_type
!= mem_type
)
264 return VA_STATUS_ERROR_INVALID_PARAMETER
;
266 VABufferInfo
* const buf_info
= &buf
->export_state
;
269 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME
: {
270 struct winsys_handle whandle
;
272 drv
->pipe
->flush(drv
->pipe
, NULL
, 0);
274 memset(&whandle
, 0, sizeof(whandle
));
275 whandle
.type
= DRM_API_HANDLE_TYPE_FD
;
277 if (!screen
->resource_get_handle(screen
, buf
->derived_surface
.resource
, &whandle
))
278 return VA_STATUS_ERROR_INVALID_BUFFER
;
280 buf_info
->handle
= (intptr_t)whandle
.handle
;
284 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE
;
287 buf_info
->type
= buf
->type
;
288 buf_info
->mem_type
= mem_type
;
289 buf_info
->mem_size
= buf
->num_elements
* buf
->size
;
292 buf
->export_refcount
++;
294 *out_buf_info
= buf
->export_state
;
296 return VA_STATUS_SUCCESS
;
300 vlVaReleaseBufferHandle(VADriverContextP ctx
, VABufferID buf_id
)
305 return VA_STATUS_ERROR_INVALID_CONTEXT
;
307 buf
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, buf_id
);
310 return VA_STATUS_ERROR_INVALID_BUFFER
;
312 if (buf
->export_refcount
== 0)
313 return VA_STATUS_ERROR_INVALID_BUFFER
;
315 if (--buf
->export_refcount
== 0) {
316 VABufferInfo
* const buf_info
= &buf
->export_state
;
318 switch (buf_info
->mem_type
) {
319 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME
:
320 close((intptr_t)buf_info
->handle
);
323 return VA_STATUS_ERROR_INVALID_BUFFER
;
326 buf_info
->mem_type
= 0;
329 return VA_STATUS_SUCCESS
;