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