OSDN Git Service

vl/buffers: fixes vl_video_buffer_formats for RGBX
[android-x86/external-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/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 enum pipe_format const_resource_formats_YV12[3] = {
42    PIPE_FORMAT_R8_UNORM,
43    PIPE_FORMAT_R8_UNORM,
44    PIPE_FORMAT_R8_UNORM
45 };
46
47 const enum pipe_format const_resource_formats_NV12[3] = {
48    PIPE_FORMAT_R8_UNORM,
49    PIPE_FORMAT_R8G8_UNORM,
50    PIPE_FORMAT_NONE
51 };
52
53 const enum pipe_format const_resource_formats_YUVA[3] = {
54    PIPE_FORMAT_R8G8B8A8_UNORM,
55    PIPE_FORMAT_NONE,
56    PIPE_FORMAT_NONE
57 };
58
59 const enum pipe_format const_resource_formats_VUYA[3] = {
60    PIPE_FORMAT_B8G8R8A8_UNORM,
61    PIPE_FORMAT_NONE,
62    PIPE_FORMAT_NONE
63 };
64
65 const enum pipe_format const_resource_formats_YUVX[3] = {
66    PIPE_FORMAT_R8G8B8X8_UNORM,
67    PIPE_FORMAT_NONE,
68    PIPE_FORMAT_NONE
69 };
70
71 const enum pipe_format const_resource_formats_VUYX[3] = {
72    PIPE_FORMAT_B8G8R8X8_UNORM,
73    PIPE_FORMAT_NONE,
74    PIPE_FORMAT_NONE
75 };
76
77 const enum pipe_format const_resource_formats_YUYV[3] = {
78    PIPE_FORMAT_R8G8_R8B8_UNORM,
79    PIPE_FORMAT_NONE,
80    PIPE_FORMAT_NONE
81 };
82
83 const enum pipe_format const_resource_formats_UYVY[3] = {
84    PIPE_FORMAT_G8R8_B8R8_UNORM,
85    PIPE_FORMAT_NONE,
86    PIPE_FORMAT_NONE
87 };
88
89 const unsigned const_resource_plane_order_YUV[3] = {
90    0,
91    1,
92    2
93 };
94
95 const unsigned const_resource_plane_order_YVU[3] = {
96    0,
97    2,
98    1
99 };
100
101 const enum pipe_format *
102 vl_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format)
103 {
104    switch(format) {
105    case PIPE_FORMAT_YV12:
106       return const_resource_formats_YV12;
107
108    case PIPE_FORMAT_NV12:
109       return const_resource_formats_NV12;
110
111    case PIPE_FORMAT_R8G8B8A8_UNORM:
112       return const_resource_formats_YUVA;
113
114    case PIPE_FORMAT_B8G8R8A8_UNORM:
115       return const_resource_formats_VUYA;
116
117    case PIPE_FORMAT_R8G8B8X8_UNORM:
118       return const_resource_formats_YUVX;
119
120    case PIPE_FORMAT_B8G8R8X8_UNORM:
121       return const_resource_formats_VUYX;
122
123    case PIPE_FORMAT_YUYV:
124       return const_resource_formats_YUYV;
125
126    case PIPE_FORMAT_UYVY:
127       return const_resource_formats_UYVY;
128
129    default:
130       return NULL;
131    }
132 }
133
134 const unsigned *
135 vl_video_buffer_plane_order(enum pipe_format format)
136 {
137    switch(format) {
138    case PIPE_FORMAT_YV12:
139       return const_resource_plane_order_YVU;
140
141    case PIPE_FORMAT_NV12:
142    case PIPE_FORMAT_R8G8B8A8_UNORM:
143    case PIPE_FORMAT_B8G8R8A8_UNORM:
144    case PIPE_FORMAT_YUYV:
145    case PIPE_FORMAT_UYVY:
146       return const_resource_plane_order_YUV;
147
148    default:
149       return NULL;
150    }
151 }
152
153 static enum pipe_format
154 vl_video_buffer_surface_format(enum pipe_format format)
155 {
156    const struct util_format_description *desc = util_format_description(format);
157
158    /* a subsampled formats can't work as surface use RGBA instead */
159    if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
160       return PIPE_FORMAT_R8G8B8A8_UNORM;
161
162    return format;
163 }
164
165 boolean
166 vl_video_buffer_is_format_supported(struct pipe_screen *screen,
167                                     enum pipe_format format,
168                                     enum pipe_video_profile profile,
169                                     enum pipe_video_entrypoint entrypoint)
170 {
171    const enum pipe_format *resource_formats;
172    unsigned i;
173
174    resource_formats = vl_video_buffer_formats(screen, format);
175    if (!resource_formats)
176       return false;
177
178    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
179       enum pipe_format format = resource_formats[i];
180
181       if (format == PIPE_FORMAT_NONE)
182          continue;
183
184       /* we at least need to sample from it */
185       if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW))
186          return false;
187
188       format = vl_video_buffer_surface_format(format);
189       if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET))
190          return false;
191    }
192
193    return true;
194 }
195
196 unsigned
197 vl_video_buffer_max_size(struct pipe_screen *screen)
198 {
199    uint32_t max_2d_texture_level;
200
201    max_2d_texture_level = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
202
203    return 1 << (max_2d_texture_level-1);
204 }
205
206 void
207 vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
208                                     struct pipe_video_codec *vcodec,
209                                     void *associated_data,
210                                     void (*destroy_associated_data)(void *))
211 {
212    vbuf->codec = vcodec;
213
214    if (vbuf->associated_data == associated_data)
215       return;
216
217    if (vbuf->associated_data)
218       vbuf->destroy_associated_data(vbuf->associated_data);
219
220    vbuf->associated_data = associated_data;
221    vbuf->destroy_associated_data = destroy_associated_data;
222 }
223
224 void *
225 vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
226                                     struct pipe_video_codec *vcodec)
227 {
228    if (vbuf->codec == vcodec)
229       return vbuf->associated_data;
230    else
231       return NULL;
232 }
233
234 void
235 vl_video_buffer_template(struct pipe_resource *templ,
236                          const struct pipe_video_buffer *tmpl,
237                          enum pipe_format resource_format,
238                          unsigned depth, unsigned array_size,
239                          unsigned usage, unsigned plane)
240 {
241    memset(templ, 0, sizeof(*templ));
242    if (depth > 1)
243       templ->target = PIPE_TEXTURE_3D;
244    else if (array_size > 1)
245       templ->target = PIPE_TEXTURE_2D_ARRAY;
246    else
247       templ->target = PIPE_TEXTURE_2D;
248    templ->format = resource_format;
249    templ->width0 = tmpl->width;
250    templ->height0 = tmpl->height;
251    templ->depth0 = depth;
252    templ->array_size = array_size;
253    templ->bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
254    templ->usage = usage;
255
256    if (plane > 0) {
257       if (tmpl->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
258          templ->width0 /= 2;
259          templ->height0 /= 2;
260       } else if (tmpl->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
261          templ->width0 /= 2;
262       }
263    }
264 }
265
266 static void
267 vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
268 {
269    struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
270    unsigned i;
271
272    assert(buf);
273
274    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
275       pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
276       pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
277       pipe_resource_reference(&buf->resources[i], NULL);
278    }
279
280    for (i = 0; i < VL_MAX_SURFACES; ++i)
281       pipe_surface_reference(&buf->surfaces[i], NULL);
282
283    vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
284
285    FREE(buffer);
286 }
287
288 static struct pipe_sampler_view **
289 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
290 {
291    struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
292    struct pipe_sampler_view sv_templ;
293    struct pipe_context *pipe;
294    unsigned i;
295
296    assert(buf);
297
298    pipe = buf->base.context;
299
300    for (i = 0; i < buf->num_planes; ++i ) {
301       if (!buf->sampler_view_planes[i]) {
302          memset(&sv_templ, 0, sizeof(sv_templ));
303          u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
304
305          if (util_format_get_nr_components(buf->resources[i]->format) == 1)
306             sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_RED;
307
308          buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
309          if (!buf->sampler_view_planes[i])
310             goto error;
311       }
312    }
313
314    return buf->sampler_view_planes;
315
316 error:
317    for (i = 0; i < buf->num_planes; ++i )
318       pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
319
320    return NULL;
321 }
322
323 static struct pipe_sampler_view **
324 vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
325 {
326    struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
327    struct pipe_sampler_view sv_templ;
328    struct pipe_context *pipe;
329    const enum pipe_format *sampler_format;
330    const unsigned *plane_order;
331    unsigned i, j, component;
332
333    assert(buf);
334
335    pipe = buf->base.context;
336
337    sampler_format = vl_video_buffer_formats(pipe->screen, buf->base.buffer_format);
338    plane_order = vl_video_buffer_plane_order(buf->base.buffer_format);
339
340    for (component = 0, i = 0; i < buf->num_planes; ++i ) {
341       struct pipe_resource *res = buf->resources[plane_order[i]];
342       const struct util_format_description *desc = util_format_description(res->format);
343       unsigned nr_components = util_format_get_nr_components(res->format);
344       if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
345          nr_components = 3;
346
347       for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) {
348          if (buf->sampler_view_components[component])
349             continue;
350
351          memset(&sv_templ, 0, sizeof(sv_templ));
352          u_sampler_view_default_template(&sv_templ, res, sampler_format[plane_order[i]]);
353          sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;
354          sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
355          buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
356          if (!buf->sampler_view_components[component])
357             goto error;
358       }
359    }
360    assert(component == VL_NUM_COMPONENTS);
361
362    return buf->sampler_view_components;
363
364 error:
365    for (i = 0; i < VL_NUM_COMPONENTS; ++i )
366       pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
367
368    return NULL;
369 }
370
371 static struct pipe_surface **
372 vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
373 {
374    struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
375    struct pipe_surface surf_templ;
376    struct pipe_context *pipe;
377    unsigned i, j, array_size, surf;
378
379    assert(buf);
380
381    pipe = buf->base.context;
382
383    array_size = buffer->interlaced ? 2 : 1;
384    for (i = 0, surf = 0; i < VL_NUM_COMPONENTS; ++i) {
385       for (j = 0; j < array_size; ++j, ++surf) {
386          assert(surf < VL_MAX_SURFACES);
387
388          if (!buf->resources[i]) {
389             pipe_surface_reference(&buf->surfaces[surf], NULL);
390             continue;
391          }
392
393          if (!buf->surfaces[surf]) {
394             memset(&surf_templ, 0, sizeof(surf_templ));
395             surf_templ.format = vl_video_buffer_surface_format(buf->resources[i]->format);
396             surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = j;
397             buf->surfaces[surf] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
398             if (!buf->surfaces[surf])
399                goto error;
400          }
401       }
402    }
403
404    return buf->surfaces;
405
406 error:
407    for (i = 0; i < VL_MAX_SURFACES; ++i )
408       pipe_surface_reference(&buf->surfaces[i], NULL);
409
410    return NULL;
411 }
412
413 struct pipe_video_buffer *
414 vl_video_buffer_create(struct pipe_context *pipe,
415                        const struct pipe_video_buffer *tmpl)
416 {
417    const enum pipe_format *resource_formats;
418    struct pipe_video_buffer templat, *result;
419    bool pot_buffers;
420
421    assert(pipe);
422    assert(tmpl->width > 0 && tmpl->height > 0);
423
424    pot_buffers = !pipe->screen->get_video_param
425    (
426       pipe->screen,
427       PIPE_VIDEO_PROFILE_UNKNOWN,
428       PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
429       PIPE_VIDEO_CAP_NPOT_TEXTURES
430    );
431
432    resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format);
433    if (!resource_formats)
434       return NULL;
435
436    templat = *tmpl;
437    templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
438                  : align(tmpl->width, VL_MACROBLOCK_WIDTH);
439    templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
440                   : align(tmpl->height, VL_MACROBLOCK_HEIGHT);
441
442    if (tmpl->interlaced)
443       templat.height /= 2;
444
445    result = vl_video_buffer_create_ex
446    (
447       pipe, &templat, resource_formats,
448       1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT
449    );
450
451
452    if (result && tmpl->interlaced)
453       result->height *= 2;
454
455    return result;
456 }
457
458 struct pipe_video_buffer *
459 vl_video_buffer_create_ex(struct pipe_context *pipe,
460                           const struct pipe_video_buffer *tmpl,
461                           const enum pipe_format resource_formats[VL_NUM_COMPONENTS],
462                           unsigned depth, unsigned array_size, unsigned usage)
463 {
464    struct pipe_resource res_tmpl;
465    struct pipe_resource *resources[VL_NUM_COMPONENTS];
466    unsigned i;
467
468    assert(pipe);
469
470    memset(resources, 0, sizeof resources);
471
472    vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[0], depth, array_size, usage, 0);
473    resources[0] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
474    if (!resources[0])
475       goto error;
476
477    if (resource_formats[1] == PIPE_FORMAT_NONE) {
478       assert(resource_formats[2] == PIPE_FORMAT_NONE);
479       return vl_video_buffer_create_ex2(pipe, tmpl, resources);
480    }
481
482    vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[1], depth, array_size, usage, 1);
483    resources[1] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
484    if (!resources[1])
485       goto error;
486
487    if (resource_formats[2] == PIPE_FORMAT_NONE)
488       return vl_video_buffer_create_ex2(pipe, tmpl, resources);
489
490    vl_video_buffer_template(&res_tmpl, tmpl, resource_formats[2], depth, array_size, usage, 2);
491    resources[2] = pipe->screen->resource_create(pipe->screen, &res_tmpl);
492    if (!resources[2])
493       goto error;
494
495    return vl_video_buffer_create_ex2(pipe, tmpl, resources);
496
497 error:
498    for (i = 0; i < VL_NUM_COMPONENTS; ++i)
499       pipe_resource_reference(&resources[i], NULL);
500
501    return NULL;
502 }
503
504 struct pipe_video_buffer *
505 vl_video_buffer_create_ex2(struct pipe_context *pipe,
506                            const struct pipe_video_buffer *tmpl,
507                            struct pipe_resource *resources[VL_NUM_COMPONENTS])
508 {
509    struct vl_video_buffer *buffer;
510    unsigned i;
511
512    buffer = CALLOC_STRUCT(vl_video_buffer);
513    if (!buffer)
514       return NULL;
515
516    buffer->base = *tmpl;
517    buffer->base.context = pipe;
518    buffer->base.destroy = vl_video_buffer_destroy;
519    buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
520    buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
521    buffer->base.get_surfaces = vl_video_buffer_surfaces;
522    buffer->num_planes = 0;
523
524    for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
525       buffer->resources[i] = resources[i];
526       if (resources[i])
527          buffer->num_planes++;
528    }
529
530    return &buffer->base;
531 }