radeon/uvd: recalculate dbp buffer size
[mesa.git] / src / gallium / state_trackers / va / subpicture.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 #include "util/u_sampler.h"
32
33 #include "va_private.h"
34
35 static VAImageFormat subpic_formats[] = {
36 {
37 .fourcc = VA_FOURCC_BGRA,
38 .byte_order = VA_LSB_FIRST,
39 .bits_per_pixel = 32,
40 .depth = 32,
41 .red_mask = 0x00ff0000ul,
42 .green_mask = 0x0000ff00ul,
43 .blue_mask = 0x000000fful,
44 .alpha_mask = 0xff000000ul,
45 },
46 };
47
48 VAStatus
49 vlVaQuerySubpictureFormats(VADriverContextP ctx, VAImageFormat *format_list,
50 unsigned int *flags, unsigned int *num_formats)
51 {
52 if (!ctx)
53 return VA_STATUS_ERROR_INVALID_CONTEXT;
54
55 if (!(format_list && flags && num_formats))
56 return VA_STATUS_ERROR_UNKNOWN;
57
58 *num_formats = sizeof(subpic_formats)/sizeof(VAImageFormat);
59 memcpy(format_list, subpic_formats, sizeof(subpic_formats));
60
61 return VA_STATUS_SUCCESS;
62 }
63
64 VAStatus
65 vlVaCreateSubpicture(VADriverContextP ctx, VAImageID image,
66 VASubpictureID *subpicture)
67 {
68 vlVaSubpicture *sub;
69 VAImage *img;
70
71 if (!ctx)
72 return VA_STATUS_ERROR_INVALID_CONTEXT;
73
74 img = handle_table_get(VL_VA_DRIVER(ctx)->htab, image);
75 if (!img)
76 return VA_STATUS_ERROR_INVALID_IMAGE;
77
78 sub = CALLOC(1, sizeof(*sub));
79 if (!sub)
80 return VA_STATUS_ERROR_ALLOCATION_FAILED;
81
82 sub->image = img;
83 *subpicture = handle_table_add(VL_VA_DRIVER(ctx)->htab, sub);
84
85 return VA_STATUS_SUCCESS;
86 }
87
88 VAStatus
89 vlVaDestroySubpicture(VADriverContextP ctx, VASubpictureID subpicture)
90 {
91 vlVaSubpicture *sub;
92
93 if (!ctx)
94 return VA_STATUS_ERROR_INVALID_CONTEXT;
95
96 sub = handle_table_get(VL_VA_DRIVER(ctx)->htab, subpicture);
97 if (!sub)
98 return VA_STATUS_ERROR_INVALID_SUBPICTURE;
99
100 FREE(sub);
101 handle_table_remove(VL_VA_DRIVER(ctx)->htab, subpicture);
102
103 return VA_STATUS_SUCCESS;
104 }
105
106 VAStatus
107 vlVaSubpictureImage(VADriverContextP ctx, VASubpictureID subpicture, VAImageID image)
108 {
109 vlVaSubpicture *sub;
110 VAImage *img;
111
112 if (!ctx)
113 return VA_STATUS_ERROR_INVALID_CONTEXT;
114
115 img = handle_table_get(VL_VA_DRIVER(ctx)->htab, image);
116 if (!img)
117 return VA_STATUS_ERROR_INVALID_IMAGE;
118
119 sub = handle_table_get(VL_VA_DRIVER(ctx)->htab, subpicture);
120 if (!sub)
121 return VA_STATUS_ERROR_INVALID_SUBPICTURE;
122
123 sub->image = img;
124
125 return VA_STATUS_SUCCESS;
126 }
127
128 VAStatus
129 vlVaSetSubpictureChromakey(VADriverContextP ctx, VASubpictureID subpicture,
130 unsigned int chromakey_min, unsigned int chromakey_max, unsigned int chromakey_mask)
131 {
132 if (!ctx)
133 return VA_STATUS_ERROR_INVALID_CONTEXT;
134
135 return VA_STATUS_ERROR_UNIMPLEMENTED;
136 }
137
138 VAStatus
139 vlVaSetSubpictureGlobalAlpha(VADriverContextP ctx, VASubpictureID subpicture, float global_alpha)
140 {
141 if (!ctx)
142 return VA_STATUS_ERROR_INVALID_CONTEXT;
143
144 return VA_STATUS_ERROR_UNIMPLEMENTED;
145 }
146
147 VAStatus
148 vlVaAssociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,
149 VASurfaceID *target_surfaces, int num_surfaces,
150 short src_x, short src_y, unsigned short src_width,
151 unsigned short src_height, short dest_x, short dest_y,
152 unsigned short dest_width, unsigned short dest_height,
153 unsigned int flags)
154 {
155 vlVaSubpicture *sub;
156 struct pipe_resource tex_temp, *tex;
157 struct pipe_sampler_view sampler_templ;
158 vlVaDriver *drv;
159 vlVaSurface *surf;
160 int i;
161 struct u_rect src_rect = {src_x, src_x + src_width, src_y, src_y + src_height};
162 struct u_rect dst_rect = {dest_x, dest_x + dest_width, dest_y, dest_y + dest_height};
163
164 if (!ctx)
165 return VA_STATUS_ERROR_INVALID_CONTEXT;
166 drv = VL_VA_DRIVER(ctx);
167
168 sub = handle_table_get(drv->htab, subpicture);
169 if (!sub)
170 return VA_STATUS_ERROR_INVALID_SUBPICTURE;
171
172 for (i = 0; i < num_surfaces; i++) {
173 surf = handle_table_get(drv->htab, target_surfaces[i]);
174 if (!surf)
175 return VA_STATUS_ERROR_INVALID_SURFACE;
176 }
177
178 sub->src_rect = src_rect;
179 sub->dst_rect = dst_rect;
180
181 memset(&tex_temp, 0, sizeof(tex_temp));
182 tex_temp.target = PIPE_TEXTURE_2D;
183 tex_temp.format = PIPE_FORMAT_B8G8R8A8_UNORM;
184 tex_temp.last_level = 0;
185 tex_temp.width0 = src_width;
186 tex_temp.height0 = src_height;
187 tex_temp.depth0 = 1;
188 tex_temp.array_size = 1;
189 tex_temp.usage = PIPE_USAGE_DYNAMIC;
190 tex_temp.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
191 tex_temp.flags = 0;
192 if (!drv->pipe->screen->is_format_supported(
193 drv->pipe->screen, tex_temp.format, tex_temp.target,
194 tex_temp.nr_samples, tex_temp.bind))
195 return VA_STATUS_ERROR_ALLOCATION_FAILED;
196
197 tex = drv->pipe->screen->resource_create(drv->pipe->screen, &tex_temp);
198
199 memset(&sampler_templ, 0, sizeof(sampler_templ));
200 u_sampler_view_default_template(&sampler_templ, tex, tex->format);
201 sub->sampler = drv->pipe->create_sampler_view(drv->pipe, tex, &sampler_templ);
202 pipe_resource_reference(&tex, NULL);
203 if (!sub->sampler)
204 return VA_STATUS_ERROR_ALLOCATION_FAILED;
205
206 for (i = 0; i < num_surfaces; i++) {
207 surf = handle_table_get(drv->htab, target_surfaces[i]);
208 util_dynarray_append(&surf->subpics, vlVaSubpicture *, sub);
209 }
210
211 return VA_STATUS_SUCCESS;
212 }
213
214 VAStatus
215 vlVaDeassociateSubpicture(VADriverContextP ctx, VASubpictureID subpicture,
216 VASurfaceID *target_surfaces, int num_surfaces)
217 {
218 int i;
219 int j;
220 vlVaSurface *surf;
221 vlVaSubpicture *sub, **array;
222 vlVaDriver *drv;
223
224 if (!ctx)
225 return VA_STATUS_ERROR_INVALID_CONTEXT;
226 drv = VL_VA_DRIVER(ctx);
227
228 sub = handle_table_get(drv->htab, subpicture);
229 if (!sub)
230 return VA_STATUS_ERROR_INVALID_SUBPICTURE;
231
232 for (i = 0; i < num_surfaces; i++) {
233 surf = handle_table_get(drv->htab, target_surfaces[i]);
234 if (!surf)
235 return VA_STATUS_ERROR_INVALID_SURFACE;
236
237 array = surf->subpics.data;
238 if (!array)
239 continue;
240
241 for (j = 0; j < surf->subpics.size/sizeof(vlVaSubpicture *); j++) {
242 if (array[j] == sub)
243 array[j] = NULL;
244 }
245
246 while (surf->subpics.size && util_dynarray_top(&surf->subpics, vlVaSubpicture *) == NULL)
247 (void)util_dynarray_pop(&surf->subpics, vlVaSubpicture *);
248 }
249
250 return VA_STATUS_SUCCESS;
251 }