st/nine: Refactor how user constbufs sizes are calculated
[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 buf->data = REALLOC(buf->data, buf->size * buf->num_elements,
77 buf->size * num_elements);
78 buf->num_elements = num_elements;
79
80 if (!buf->data)
81 return VA_STATUS_ERROR_ALLOCATION_FAILED;
82
83 return VA_STATUS_SUCCESS;
84 }
85
86 VAStatus
87 vlVaMapBuffer(VADriverContextP ctx, VABufferID buf_id, void **pbuff)
88 {
89 vlVaBuffer *buf;
90
91 if (!ctx)
92 return VA_STATUS_ERROR_INVALID_CONTEXT;
93
94 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
95 if (!buf)
96 return VA_STATUS_ERROR_INVALID_BUFFER;
97
98 *pbuff = buf->data;
99
100 return VA_STATUS_SUCCESS;
101 }
102
103 VAStatus
104 vlVaUnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
105 {
106 vlVaBuffer *buf;
107
108 if (!ctx)
109 return VA_STATUS_ERROR_INVALID_CONTEXT;
110
111 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
112 if (!buf)
113 return VA_STATUS_ERROR_INVALID_BUFFER;
114
115 /* Nothing to do here */
116
117 return VA_STATUS_SUCCESS;
118 }
119
120 VAStatus
121 vlVaDestroyBuffer(VADriverContextP ctx, VABufferID buf_id)
122 {
123 vlVaBuffer *buf;
124
125 if (!ctx)
126 return VA_STATUS_ERROR_INVALID_CONTEXT;
127
128 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
129 if (!buf)
130 return VA_STATUS_ERROR_INVALID_BUFFER;
131
132 FREE(buf->data);
133 FREE(buf);
134 handle_table_remove(VL_VA_DRIVER(ctx)->htab, buf_id);
135
136 return VA_STATUS_SUCCESS;
137 }
138
139 VAStatus
140 vlVaBufferInfo(VADriverContextP ctx, VABufferID buf_id, VABufferType *type,
141 unsigned int *size, unsigned int *num_elements)
142 {
143 vlVaBuffer *buf;
144
145 if (!ctx)
146 return VA_STATUS_ERROR_INVALID_CONTEXT;
147
148 buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, buf_id);
149 if (!buf)
150 return VA_STATUS_ERROR_INVALID_BUFFER;
151
152 *type = buf->type;
153 *size = buf->size;
154 *num_elements = buf->num_elements;
155
156 return VA_STATUS_SUCCESS;
157 }