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