st/va: add more errors checks in vlVaBufferSetNumElements and vlVaMapBuffer
[mesa.git] / src / gallium / state_trackers / va / buffer.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 #include "util/u_memory.h"
30 #include "util/u_handle_table.h"
31
32 #include "va_private.h"
33
34 VAStatus
35 vlVaCreateBuffer(VADriverContextP ctx, VAContextID context, VABufferType type,
36 unsigned int size, unsigned int num_elements, void *data,
37 VABufferID *buf_id)
38 {
39 vlVaBuffer *buf;
40
41 if (!ctx)
42 return VA_STATUS_ERROR_INVALID_CONTEXT;
43
44 buf = CALLOC(1, sizeof(vlVaBuffer));
45 if (!buf)
46 return VA_STATUS_ERROR_ALLOCATION_FAILED;
47
48 buf->type = type;
49 buf->size = size;
50 buf->num_elements = num_elements;
51 buf->data = MALLOC(size * num_elements);
52
53 if (!buf->data) {
54 FREE(buf);
55 return VA_STATUS_ERROR_ALLOCATION_FAILED;
56 }
57
58 if (data)
59 memcpy(buf->data, data, size * num_elements);
60
61 *buf_id = handle_table_add(VL_VA_DRIVER(ctx)->htab, buf);
62
63 return VA_STATUS_SUCCESS;
64 }
65
66 VAStatus
67 vlVaBufferSetNumElements(VADriverContextP ctx, VABufferID buf_id,
68 unsigned int num_elements)
69 {
70 vlVaBuffer *buf;
71
72 if (!ctx)
73 return VA_STATUS_ERROR_INVALID_CONTEXT;
74
75 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
76 if (!buf)
77 return VA_STATUS_ERROR_INVALID_BUFFER;
78
79 buf->data = REALLOC(buf->data, buf->size * buf->num_elements,
80 buf->size * num_elements);
81 buf->num_elements = num_elements;
82
83 if (!buf->data)
84 return VA_STATUS_ERROR_ALLOCATION_FAILED;
85
86 return VA_STATUS_SUCCESS;
87 }
88
89 VAStatus
90 vlVaMapBuffer(VADriverContextP ctx, VABufferID buf_id, void **pbuff)
91 {
92 vlVaBuffer *buf;
93
94 if (!ctx)
95 return VA_STATUS_ERROR_INVALID_CONTEXT;
96
97 if (!pbuff)
98 return VA_STATUS_ERROR_INVALID_PARAMETER;
99
100 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
101 if (!buf)
102 return VA_STATUS_ERROR_INVALID_BUFFER;
103
104 *pbuff = buf->data;
105
106 return VA_STATUS_SUCCESS;
107 }
108
109 VAStatus
110 vlVaUnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
111 {
112 vlVaBuffer *buf;
113
114 if (!ctx)
115 return VA_STATUS_ERROR_INVALID_CONTEXT;
116
117 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
118 if (!buf)
119 return VA_STATUS_ERROR_INVALID_BUFFER;
120
121 /* Nothing to do here */
122
123 return VA_STATUS_SUCCESS;
124 }
125
126 VAStatus
127 vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buf_id)
128 {
129 vlVaBuffer *buf;
130
131 if (!ctx)
132 return VA_STATUS_ERROR_INVALID_CONTEXT;
133
134 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
135 if (!buf)
136 return VA_STATUS_ERROR_INVALID_BUFFER;
137
138 FREE(buf->data);
139 FREE(buf);
140 handle_table_remove(VL_VA_DRIVER(ctx)->htab, buf_id);
141
142 return VA_STATUS_SUCCESS;
143 }
144
145 VAStatus
146 vlVaBufferInfo(VADriverContextP ctx, VABufferID buf_id, VABufferType *type,
147 unsigned int *size, unsigned int *num_elements)
148 {
149 vlVaBuffer *buf;
150
151 if (!ctx)
152 return VA_STATUS_ERROR_INVALID_CONTEXT;
153
154 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
155 if (!buf)
156 return VA_STATUS_ERROR_INVALID_BUFFER;
157
158 *type = buf->type;
159 *size = buf->size;
160 *num_elements = buf->num_elements;
161
162 return VA_STATUS_SUCCESS;
163 }