[g3dvl] add some more PIPE_VIDEO_CAPs
[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 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
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 *
54 vl_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
68 boolean
69 vl_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
91 unsigned
92 vl_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
101 static void
102 vl_video_buffer_destroy(struct pipe_video_buffer *buffer)
103 {
104 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
105 unsigned i;
106
107 assert(buf);
108
109 for (i = 0; i < VL_MAX_PLANES; ++i) {
110 pipe_surface_reference(&buf->surfaces[i], NULL);
111 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
112 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
113 pipe_resource_reference(&buf->resources[i], NULL);
114 }
115
116 FREE(buffer);
117 }
118
119 static struct pipe_sampler_view **
120 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer)
121 {
122 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
123 struct pipe_sampler_view sv_templ;
124 struct pipe_context *pipe;
125 unsigned i;
126
127 assert(buf);
128
129 pipe = buf->base.context;
130
131 for (i = 0; i < buf->num_planes; ++i ) {
132 if (!buf->sampler_view_planes[i]) {
133 memset(&sv_templ, 0, sizeof(sv_templ));
134 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
135
136 if (util_format_get_nr_components(buf->resources[i]->format) == 1)
137 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_RED;
138
139 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
140 if (!buf->sampler_view_planes[i])
141 goto error;
142 }
143 }
144
145 return buf->sampler_view_planes;
146
147 error:
148 for (i = 0; i < buf->num_planes; ++i )
149 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL);
150
151 return NULL;
152 }
153
154 static struct pipe_sampler_view **
155 vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer)
156 {
157 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
158 struct pipe_sampler_view sv_templ;
159 struct pipe_context *pipe;
160 unsigned i, j, component;
161
162 assert(buf);
163
164 pipe = buf->base.context;
165
166 for (component = 0, i = 0; i < buf->num_planes; ++i ) {
167 unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format);
168
169 for (j = 0; j < nr_components; ++j, ++component) {
170 assert(component < VL_MAX_PLANES);
171
172 if (!buf->sampler_view_components[component]) {
173 memset(&sv_templ, 0, sizeof(sv_templ));
174 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format);
175 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;
176 sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
177 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ);
178 if (!buf->sampler_view_components[component])
179 goto error;
180 }
181 }
182 }
183
184 return buf->sampler_view_components;
185
186 error:
187 for (i = 0; i < VL_MAX_PLANES; ++i )
188 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL);
189
190 return NULL;
191 }
192
193 static struct pipe_surface **
194 vl_video_buffer_surfaces(struct pipe_video_buffer *buffer)
195 {
196 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer;
197 struct pipe_surface surf_templ;
198 struct pipe_context *pipe;
199 unsigned i;
200
201 assert(buf);
202
203 pipe = buf->base.context;
204
205 for (i = 0; i < buf->num_planes; ++i ) {
206 if (!buf->surfaces[i]) {
207 memset(&surf_templ, 0, sizeof(surf_templ));
208 surf_templ.format = buf->resources[i]->format;
209 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
210 buf->surfaces[i] = pipe->create_surface(pipe, buf->resources[i], &surf_templ);
211 if (!buf->surfaces[i])
212 goto error;
213 }
214 }
215
216 return buf->surfaces;
217
218 error:
219 for (i = 0; i < buf->num_planes; ++i )
220 pipe_surface_reference(&buf->surfaces[i], NULL);
221
222 return NULL;
223 }
224
225 struct pipe_video_buffer *
226 vl_video_buffer_create(struct pipe_context *pipe,
227 enum pipe_format buffer_format,
228 enum pipe_video_chroma_format chroma_format,
229 unsigned width, unsigned height)
230 {
231 const enum pipe_format *resource_formats;
232 struct pipe_video_buffer *result;
233 unsigned buffer_width, buffer_height;
234 bool pot_buffers;
235
236 assert(pipe);
237 assert(width > 0 && height > 0);
238
239 pot_buffers = !pipe->screen->get_video_param
240 (
241 pipe->screen,
242 PIPE_VIDEO_PROFILE_UNKNOWN,
243 PIPE_VIDEO_CAP_NPOT_TEXTURES
244 );
245
246 resource_formats = vl_video_buffer_formats(pipe->screen, buffer_format);
247 if (!resource_formats)
248 return NULL;
249
250 buffer_width = pot_buffers ? util_next_power_of_two(width) : align(width, MACROBLOCK_WIDTH);
251 buffer_height = pot_buffers ? util_next_power_of_two(height) : align(height, MACROBLOCK_HEIGHT);
252
253 result = vl_video_buffer_create_ex
254 (
255 pipe, buffer_width, buffer_height, 1,
256 chroma_format, resource_formats, PIPE_USAGE_STATIC
257 );
258 if (result)
259 result->buffer_format = buffer_format;
260
261 return result;
262 }
263
264 struct pipe_video_buffer *
265 vl_video_buffer_create_ex(struct pipe_context *pipe,
266 unsigned width, unsigned height, unsigned depth,
267 enum pipe_video_chroma_format chroma_format,
268 const enum pipe_format resource_formats[VL_MAX_PLANES],
269 unsigned usage)
270 {
271 struct vl_video_buffer *buffer;
272 struct pipe_resource templ;
273 unsigned i;
274
275 assert(pipe);
276
277 buffer = CALLOC_STRUCT(vl_video_buffer);
278
279 buffer->base.context = pipe;
280 buffer->base.destroy = vl_video_buffer_destroy;
281 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes;
282 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components;
283 buffer->base.get_surfaces = vl_video_buffer_surfaces;
284 buffer->base.chroma_format = chroma_format;
285 buffer->base.width = width;
286 buffer->base.height = height;
287 buffer->num_planes = 1;
288
289 memset(&templ, 0, sizeof(templ));
290 templ.target = depth > 1 ? PIPE_TEXTURE_3D : PIPE_TEXTURE_2D;
291 templ.format = resource_formats[0];
292 templ.width0 = width;
293 templ.height0 = height;
294 templ.depth0 = depth;
295 templ.array_size = 1;
296 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
297 templ.usage = usage;
298
299 buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
300 if (!buffer->resources[0])
301 goto error;
302
303 if (resource_formats[1] == PIPE_FORMAT_NONE) {
304 assert(chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444);
305 assert(resource_formats[2] == PIPE_FORMAT_NONE);
306 return &buffer->base;
307 } else
308 buffer->num_planes = 2;
309
310 templ.format = resource_formats[1];
311 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
312 templ.width0 /= 2;
313 templ.height0 /= 2;
314 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
315 templ.height0 /= 2;
316 }
317
318 buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
319 if (!buffer->resources[1])
320 goto error;
321
322 if (resource_formats[2] == PIPE_FORMAT_NONE)
323 return &buffer->base;
324 else
325 buffer->num_planes = 3;
326
327 templ.format = resource_formats[2];
328 buffer->resources[2] = pipe->screen->resource_create(pipe->screen, &templ);
329 if (!buffer->resources[2])
330 goto error;
331
332 return &buffer->base;
333
334 error:
335 for (i = 0; i < VL_MAX_PLANES; ++i)
336 pipe_resource_reference(&buffer->resources[i], NULL);
337 FREE(buffer);
338
339 return NULL;
340 }