vl_video_buffer.c revision e027759336bf49e3f568bd73b9e5f26d56ef6f83
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 TUNGSTEN GRAPHICS 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
41const 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
47const enum pipe_format const_resource_formats_NV12[3] = {
48   PIPE_FORMAT_R8_UNORM,
49   PIPE_FORMAT_R8G8_UNORM,
50   PIPE_FORMAT_NONE
51};
52
53const enum pipe_format *
54vl_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format)
55{
56   switch(format) {
57   case PIPE_FORMAT_YV12:
58      return const_resource_formats_YV12;
59
60   case PIPE_FORMAT_NV12:
61      return const_resource_formats_NV12;
62
63   default:
64      return NULL;
65   }
66}
67
68boolean
69vl_video_buffer_is_format_supported(struct pipe_screen *screen,
70                                    enum pipe_format format,
71                                    enum pipe_video_profile profile)
72{
73   const enum pipe_format *resource_formats;
74   unsigned i;
75
76   resource_formats = vl_video_buffer_formats(screen, format);
77   if (!resource_formats)
78      return false;
79
80   for (i = 0; i < VL_MAX_PLANES; ++i) {
81      if (!resource_formats[i])
82         continue;
83
84      if (!screen->is_format_supported(screen, resource_formats[i], PIPE_TEXTURE_2D, 0, PIPE_USAGE_STATIC))
85         return false;
86   }
87
88   return true;
89}
90
91unsigned
92vl_video_buffer_max_size(struct pipe_screen *screen)
93{
94   uint32_t max_2d_texture_level;
95
96   max_2d_texture_level = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
97
98   return 1 << (max_2d_texture_level-1);
99}
100
101void
102vl_video_buffer_set_associated_data(struct pipe_video_buffer *vbuf,
103                                    struct pipe_video_decoder *vdec,
104                                    void *associated_data,
105                                    void (*destroy_associated_data)(void *))
106{
107   vbuf->decoder = vdec;
108
109   if (vbuf->associated_data == associated_data)
110      return;
111
112   if (vbuf->associated_data)
113      vbuf->destroy_associated_data(vbuf->associated_data);
114
115   vbuf->associated_data = associated_data;
116   vbuf->destroy_associated_data = destroy_associated_data;
117}
118
119void *
120vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf,
121                                    struct pipe_video_decoder *vdec)
122{
123   if (vbuf->decoder == vdec)
124      return vbuf->associated_data;
125   else
126      return NULL;
127}
128
129static void
130vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
131{
132   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
133   unsigned i;
134
135   assert(buf);
136
137   for (i = 0; i < VL_MAX_PLANES; ++i) {
138      pipe_surface_reference(&buf->surfaces[i], NULL);
139      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
140      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
141      pipe_resource_reference(&buf->resources[i], NULL);
142   }
143   vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL);
144
145   FREE(buffer);
146}
147
148static struct pipe_sampler_view **
149vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
150{
151   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
152   struct pipe_sampler_view sv_templ;
153   struct pipe_context *pipe;
154   unsigned i;
155
156   assert(buf);
157
158   pipe = buf->base.context;
159
160   for (i = 0; i < buf->num_planes; ++i ) {
161      if (!buf->sampler_view_planes[i]) {
162         memset(&sv_templ, 0, sizeof(sv_templ));
163         u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
164
165         if (util_format_get_nr_components(buf->resources[i]->format) == 1)
166            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_RED;
167
168         buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
169         if (!buf->sampler_view_planes[i])
170            goto error;
171      }
172   }
173
174   return buf->sampler_view_planes;
175
176error:
177   for (i = 0; i < buf->num_planes; ++i )
178      pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
179
180   return NULL;
181}
182
183static struct pipe_sampler_view **
184vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
185{
186   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
187   struct pipe_sampler_view sv_templ;
188   struct pipe_context *pipe;
189   unsigned i, j, component;
190
191   assert(buf);
192
193   pipe = buf->base.context;
194
195   for (component = 0, i = 0; i < buf->num_planes; ++i ) {
196      unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format);
197
198      for (j = 0; j < nr_components; ++j, ++component) {
199         assert(component < VL_MAX_PLANES);
200
201         if (!buf->sampler_view_components[component]) {
202            memset(&sv_templ, 0, sizeof(sv_templ));
203            u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
204            sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;
205            sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
206            buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
207            if (!buf->sampler_view_components[component])
208               goto error;
209         }
210      }
211   }
212
213   return buf->sampler_view_components;
214
215error:
216   for (i = 0; i < VL_MAX_PLANES; ++i )
217      pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
218
219   return NULL;
220}
221
222static struct pipe_surface **
223vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
224{
225   struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
226   struct pipe_surface surf_templ;
227   struct pipe_context *pipe;
228   unsigned i;
229
230   assert(buf);
231
232   pipe = buf->base.context;
233
234   for (i = 0; i < buf->num_planes; ++i ) {
235      if (!buf->surfaces[i]) {
236         memset(&surf_templ, 0, sizeof(surf_templ));
237         surf_templ.format = buf->resources[i]->format;
238         surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
239         buf->surfaces[i] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
240         if (!buf->surfaces[i])
241            goto error;
242      }
243   }
244
245   return buf->surfaces;
246
247error:
248   for (i = 0; i < buf->num_planes; ++i )
249      pipe_surface_reference(&buf->surfaces[i], NULL);
250
251   return NULL;
252}
253
254struct pipe_video_buffer *
255vl_video_buffer_create(struct pipe_context *pipe,
256                       const struct pipe_video_buffer *tmpl)
257{
258   const enum pipe_format *resource_formats;
259   struct pipe_video_buffer templat;
260   bool pot_buffers;
261
262   assert(pipe);
263   assert(tmpl->width > 0 && tmpl->height > 0);
264
265   pot_buffers = !pipe->screen->get_video_param
266   (
267      pipe->screen,
268      PIPE_VIDEO_PROFILE_UNKNOWN,
269      PIPE_VIDEO_CAP_NPOT_TEXTURES
270   );
271
272   resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format);
273   if (!resource_formats)
274      return NULL;
275
276   templat = *tmpl;
277   templat.width = pot_buffers ? util_next_power_of_two(tmpl->width)
278                 : align(tmpl->width, MACROBLOCK_WIDTH);
279   templat.height = pot_buffers ? util_next_power_of_two(tmpl->height)
280                  : align(tmpl->height, MACROBLOCK_HEIGHT);
281
282   return vl_video_buffer_create_ex
283   (
284      pipe, &templat, resource_formats,
285      1, PIPE_USAGE_STATIC
286   );
287}
288
289struct pipe_video_buffer *
290vl_video_buffer_create_ex(struct pipe_context *pipe,
291                          const struct pipe_video_buffer *tmpl,
292                          const enum pipe_format resource_formats[VL_MAX_PLANES],
293                          unsigned depth, unsigned usage)
294{
295   struct vl_video_buffer *buffer;
296   struct pipe_resource templ;
297   unsigned i;
298
299   assert(pipe);
300
301   buffer = CALLOC_STRUCT(vl_video_buffer);
302
303   buffer->base = *tmpl;
304   buffer->base.context = pipe;
305   buffer->base.destroy = vl_video_buffer_destroy;
306   buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
307   buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
308   buffer->base.get_surfaces = vl_video_buffer_surfaces;
309   buffer->num_planes = 1;
310
311   memset(&templ, 0, sizeof(templ));
312   templ.target = depth > 1 ? PIPE_TEXTURE_3D : PIPE_TEXTURE_2D;
313   templ.format = resource_formats[0];
314   templ.width0 = tmpl->width;
315   templ.height0 = tmpl->height;
316   templ.depth0 = depth;
317   templ.array_size = 1;
318   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
319   templ.usage = usage;
320
321   buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
322   if (!buffer->resources[0])
323      goto error;
324
325   if (resource_formats[1] == PIPE_FORMAT_NONE) {
326      assert(tmpl->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444);
327      assert(resource_formats[2] == PIPE_FORMAT_NONE);
328      return &buffer->base;
329   } else
330      buffer->num_planes = 2;
331
332   templ.format = resource_formats[1];
333   if (tmpl->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
334      templ.width0 /= 2;
335      templ.height0 /= 2;
336   } else if (tmpl->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
337      templ.height0 /= 2;
338   }
339
340   buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
341   if (!buffer->resources[1])
342      goto error;
343
344   if (resource_formats[2] == PIPE_FORMAT_NONE)
345      return &buffer->base;
346   else
347      buffer->num_planes = 3;
348
349   templ.format = resource_formats[2];
350   buffer->resources[2] = pipe->screen->resource_create(pipe->screen, &templ);
351   if (!buffer->resources[2])
352      goto error;
353
354   return &buffer->base;
355
356error:
357   for (i = 0; i < VL_MAX_PLANES; ++i)
358      pipe_resource_reference(&buffer->resources[i], NULL);
359   FREE(buffer);
360
361   return NULL;
362}
363