Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / gallium / auxiliary / vl / vl_video_buffer.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Christian König.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33
34 #include "util/format/u_format.h"
35 #include "util/u_inlines.h"
36 #include "util/u_sampler.h"
37 #include "util/u_memory.h"
38
39 #include "vl_video_buffer.h"
40
41 const unsigned const_resource_plane_order_YUV[3] = {
42 0,
43 1,
44 2
45 };
46
47 const unsigned const_resource_plane_order_YVU[3] = {
48 0,
49 2,
50 1
51 };
52
53 void
54 vl_get_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format,
55 enum pipe_format out_format[VL_NUM_COMPONENTS])
56 {
57 unsigned num_planes = util_format_get_num_planes(format);
58 unsigned i;
59
60 for (i = 0; i < num_planes; i++)
61 out_format[i] = util_format_get_plane_format(format, i);
62 for (; i < VL_NUM_COMPONENTS; i++)
63 out_format[i] = PIPE_FORMAT_NONE;
64
65 if (format == PIPE_FORMAT_YUYV)
66 out_format[0] = PIPE_FORMAT_R8G8_R8B8_UNORM;
67 else if (format == PIPE_FORMAT_UYVY)
68 out_format[0] = PIPE_FORMAT_G8R8_B8R8_UNORM;
69 }
70
71 const unsigned *
72 vl_video_buffer_plane_order(enum pipe_format format)
73 {
74 switch(format) {
75 case PIPE_FORMAT_YV12:
76 return const_resource_plane_order_YVU;
77
78 case PIPE_FORMAT_NV12:
79 case PIPE_FORMAT_R8G8B8A8_UNORM:
80 case PIPE_FORMAT_B8G8R8A8_UNORM:
81 case PIPE_FORMAT_YUYV:
82 case PIPE_FORMAT_UYVY:
83 case PIPE_FORMAT_P010:
84 case PIPE_FORMAT_P016:
85 return const_resource_plane_order_YUV;
86
87 default:
88 return NULL;
89 }
90 }
91
92 static enum pipe_format
93 vl_video_buffer_surface_format(enum pipe_format format)
94 {
95 const struct util_format_description *desc = util_format_description(format);
96
97 /* a subsampled formats can't work as surface use RGBA instead */
98 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
99 return PIPE_FORMAT_R8G8B8A8_UNORM;
100
101 return format;
102 }
103
104 bool
105 vl_video_buffer_is_format_supported(struct pipe_screen *screen,
106 enum pipe_format format,
107 enum pipe_video_profile profile,
108 enum pipe_video_entrypoint entrypoint)
109 {
110 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
111 unsigned i;
112
113 vl_get_video_buffer_formats(screen, format, resource_formats);
114
115 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
116 enum pipe_format format = resource_formats[i];
117
118 if (format == PIPE_FORMAT_NONE)
119 continue;
120
121 /* we at least need to sample from it */
122 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW))
123 return false;
124
125 format = vl_video_buffer_surface_format(format);
126 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET))
127 return false;
128 }
129
130 return true;
131 }
132
133 unsigned
134 vl_video_buffer_max_size(struct pipe_screen *screen)
135 {
136 return screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
137 }
138
139 void
140 vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
141 struct pipe_video_codec *vcodec,
142 void *associated_data,
143 void (*destroy_associated_data)(void *))
144 {
145 vbuf->codec = vcodec;
146
147 if (vbuf->associated_data == associated_data)
148 return;
149
150 if (vbuf->associated_data)
151 vbuf->destroy_associated_data(vbuf->associated_data);
152
153 vbuf->associated_data = associated_data;
154 vbuf->destroy_associated_data = destroy_associated_data;
155 }
156
157 void *
158 vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
159 struct pipe_video_codec *vcodec)
160 {
161 if (vbuf->codec == vcodec)
162 return vbuf->associated_data;
163 else
164 return NULL;
165 }
166
167 void
168 vl_video_buffer_template(struct pipe_resource *templ,
169 const struct pipe_video_buffer *tmpl,
170 enum pipe_format resource_format,
171 unsigned depth, unsigned array_size,
172 unsigned usage, unsigned plane)
173 {
174 unsigned height = tmpl->height;
175
176 memset(templ, 0, sizeof(*templ));
177 if (depth > 1)
178 templ->target = PIPE_TEXTURE_3D;
179 else if (array_size > 1)
180 templ->target = PIPE_TEXTURE_2D_ARRAY;
181 else
182 templ->target = PIPE_TEXTURE_2D;
183 templ->format = resource_format;
184 templ->width0 = tmpl->width;
185 templ->depth0 = depth;
186 templ->array_size = array_size;
187 templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
188 templ->usage = usage;
189
190 vl_video_buffer_adjust_size(&templ->width0, &height, plane,
191 pipe_format_to_chroma_format(tmpl->buffer_format), false);
192 templ->height0 = height;
193 }
194
195 static void
196 vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
197 {
198 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
199 unsigned i;
200
201 assert(buf);
202
203 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
204 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
205 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
206 pipe_resource_reference(&buf->resources[i], NULL);
207 }
208
209 for (i = 0; i < VL_MAX_SURFACES; ++i)
210 pipe_surface_reference(&buf->surfaces[i], NULL);
211
212 vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
213
214 FREE(buffer);
215 }
216
217 static struct pipe_sampler_view **
218 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
219 {
220 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
221 struct pipe_sampler_view sv_templ;
222 struct pipe_context *pipe;
223 unsigned i;
224
225 assert(buf);
226
227 pipe = buf->base.context;
228
229 for (i = 0; i < buf->num_planes; ++i ) {
230 if (!buf->sampler_view_planes[i]) {
231 memset(&sv_templ, 0, sizeof(sv_templ));
232 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
233
234 if (util_format_get_nr_components(buf->resources[i]->format) == 1)
235 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_X;
236
237 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
238 if (!buf->sampler_view_planes[i])
239 goto error;
240 }
241 }
242
243 return buf->sampler_view_planes;
244
245 error:
246 for (i = 0; i < buf->num_planes; ++i )
247 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
248
249 return NULL;
250 }
251
252 static struct pipe_sampler_view **
253 vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
254 {
255 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
256 struct pipe_sampler_view sv_templ;
257 struct pipe_context *pipe;
258 enum pipe_format sampler_format[VL_NUM_COMPONENTS];
259 const unsigned *plane_order;
260 unsigned i, j, component;
261
262 assert(buf);
263
264 pipe = buf->base.context;
265
266 vl_get_video_buffer_formats(pipe->screen, buf->base.buffer_format, sampler_format);
267 plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);
268
269 for (component = 0, i = 0; i < buf->num_planes; ++i ) {
270 struct pipe_resource *res = buf->resources[plane_order[i]];
271 const struct util_format_description *desc = util_format_description(res->format);
272 unsigned nr_components = util_format_get_nr_components(res->format);
273 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
274 nr_components = 3;
275
276 for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
277 if (buf->sampler_view_components[component])
278 continue;
279
280 memset(&sv_templ, 0, sizeof(sv_templ));
281 u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
282 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_X + j;
283 sv_templ.swizzle_a = PIPE_SWIZZLE_1;
284 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
285 if (!buf->sampler_view_components[component])
286 goto error;
287 }
288 }
289 assert(component == VL_NUM_COMPONENTS);
290
291 return buf->sampler_view_components;
292
293 error:
294 for (i = 0; i < VL_NUM_COMPONENTS; ++i )
295 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
296
297 return NULL;
298 }
299
300 static struct pipe_surface **
301 vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
302 {
303 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
304 struct pipe_surface surf_templ;
305 struct pipe_context *pipe;
306 unsigned i, j, array_size, surf;
307
308 assert(buf);
309
310 pipe = buf->base.context;
311
312 array_size = buffer->interlaced ? 2 : 1;
313 for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
314 for (j = 0; j < array_size; ++j, ++surf) {
315 assert(surf < VL_MAX_SURFACES);
316
317 if (!buf->resources[i]) {
318 pipe_surface_reference(&buf->surfaces[surf], NULL);
319 continue;
320 }
321
322 if (!buf->surfaces[surf]) {
323 memset(&surf_templ, 0, sizeof(surf_templ));
324 surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
325 surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
326 buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
327 if (!buf->surfaces[surf])
328 goto error;
329 }
330 }
331 }
332
333 return buf->surfaces;
334
335 error:
336 for (i = 0; i < VL_MAX_SURFACES; ++i )
337 pipe_surface_reference(&buf->surfaces[i], NULL);
338
339 return NULL;
340 }
341
342 struct pipe_video_buffer *
343 vl_video_buffer_create(struct pipe_context *pipe,
344 const struct pipe_video_buffer *tmpl)
345 {
346 enum pipe_format resource_formats[VL_NUM_COMPONENTS];
347 struct pipe_video_buffer templat, *result;
348 bool pot_buffers;
349
350 assert(pipe);
351 assert(tmpl->width > 0 && tmpl->height > 0);
352
353 pot_buffers = !pipe->screen->get_video_param
354 (
355 pipe->screen,
356 PIPE_VIDEO_PROFILE_UNKNOWN,
357 PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
358 PIPE_VIDEO_CAP_NPOT_TEXTURES
359 );
360
361 vl_get_video_buffer_formats(pipe->screen, tmpl->buffer_format, resource_formats);
362
363 templat = *tmpl;
364 templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
365 : align(tmpl->width, VL_MACROBLOCK_WIDTH);
366 templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
367 : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
368
369 if (tmpl->interlaced)
370 templat.height /= 2;
371
372 result = vl_video_buffer_create_ex
373 (
374 pipe, &templat, resource_formats,
375 1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT
376 );
377
378
379 if (result && tmpl->interlaced)
380 result->height *= 2;
381
382 return result;
383 }
384
385 struct pipe_video_buffer *
386 vl_video_buffer_create_ex(struct pipe_context *pipe,
387 const struct pipe_video_buffer *tmpl,
388 const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
389 unsigned depth, unsigned array_size, unsigned usage)
390 {
391 struct pipe_resource res_tmpl;
392 struct pipe_resource *resources[VL_NUM_COMPONENTS];
393 unsigned i;
394
395 assert(pipe);
396
397 memset(resources, 0, sizeof resources);
398
399 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size, usage, 0);
400 resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
401 if (!resources[0])
402 goto error;
403
404 if (resource_formats[1] == PIPE_FORMAT_NONE) {
405 assert(resource_formats[2] == PIPE_FORMAT_NONE);
406 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
407 }
408
409 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size, usage, 1);
410 resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
411 if (!resources[1])
412 goto error;
413
414 if (resource_formats[2] == PIPE_FORMAT_NONE)
415 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
416
417 vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size, usage, 2);
418 resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
419 if (!resources[2])
420 goto error;
421
422 return vl_video_buffer_create_ex2(pipe, tmpl, resources);
423
424 error:
425 for (i = 0; i < VL_NUM_COMPONENTS; ++i)
426 pipe_resource_reference(&resources[i], NULL);
427
428 return NULL;
429 }
430
431 struct pipe_video_buffer *
432 vl_video_buffer_create_ex2(struct pipe_context *pipe,
433 const struct pipe_video_buffer *tmpl,
434 struct pipe_resource *resources[VL_NUM_COMPONENTS])
435 {
436 struct vl_video_buffer *buffer;
437 unsigned i;
438
439 buffer = CALLOC_STRUCT(vl_video_buffer);
440 if (!buffer)
441 return NULL;
442
443 buffer->base = *tmpl;
444 buffer->base.context = pipe;
445 buffer->base.destroy = vl_video_buffer_destroy;
446 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
447 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
448 buffer->base.get_surfaces = vl_video_buffer_surfaces;
449 buffer->num_planes = 0;
450
451 for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
452 buffer->resources[i] = resources[i];
453 if (resources[i])
454 buffer->num_planes++;
455 }
456
457 return &buffer->base;
458 }
459
460 /* Create pipe_video_buffer by using resource_create with planar formats. */
461 struct pipe_video_buffer *
462 vl_video_buffer_create_as_resource(struct pipe_context *pipe,
463 const struct pipe_video_buffer *tmpl)
464 {
465 struct pipe_resource templ, *resources[VL_NUM_COMPONENTS] = {0};
466 unsigned array_size = tmpl->interlaced ? 2 : 1;
467
468 memset(&templ, 0, sizeof(templ));
469 templ.target = array_size > 1 ? PIPE_TEXTURE_2D_ARRAY : PIPE_TEXTURE_2D;
470 templ.width0 = align(tmpl->width, VL_MACROBLOCK_WIDTH);
471 templ.height0 = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
472 templ.depth0 = 1;
473 templ.array_size = array_size;
474 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET | tmpl->bind;
475 templ.usage = PIPE_USAGE_DEFAULT;
476
477 if (tmpl->buffer_format == PIPE_FORMAT_YUYV)
478 templ.format = PIPE_FORMAT_R8G8_R8B8_UNORM;
479 else if (tmpl->buffer_format == PIPE_FORMAT_UYVY)
480 templ.format = PIPE_FORMAT_G8R8_B8R8_UNORM;
481 else
482 templ.format = tmpl->buffer_format;
483
484 resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
485 if (!resources[0])
486 return NULL;
487
488 if (resources[0]->next) {
489 pipe_resource_reference(&resources[1], resources[0]->next);
490 if (resources[1]->next)
491 pipe_resource_reference(&resources[2], resources[1]->next);
492 }
493
494 struct pipe_video_buffer vidtemplate = *tmpl;
495 vidtemplate.width = templ.width0;
496 vidtemplate.height = templ.height0 * array_size;
497 return vl_video_buffer_create_ex2(pipe, &vidtemplate, resources);
498 }