vl: Subpicture/compositing fixes.
[mesa.git] / src / gallium / drivers / softpipe / sp_video_context.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 "sp_video_context.h"
29 #include <pipe/p_inlines.h>
30 #include <util/u_memory.h>
31 #include <util/u_rect.h>
32 #include <util/u_video.h>
33 #include "sp_winsys.h"
34 #include "sp_texture.h"
35
36 static void
37 sp_mpeg12_destroy(struct pipe_video_context *vpipe)
38 {
39 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
40
41 assert(vpipe);
42
43 /* Asserted in softpipe_delete_fs_state() for some reason */
44 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
45 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
46
47 ctx->pipe->delete_blend_state(ctx->pipe, ctx->blend);
48 ctx->pipe->delete_rasterizer_state(ctx->pipe, ctx->rast);
49 ctx->pipe->delete_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
50
51 pipe_video_surface_reference(&ctx->decode_target, NULL);
52 vl_compositor_cleanup(&ctx->compositor);
53 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
54 ctx->pipe->destroy(ctx->pipe);
55
56 FREE(ctx);
57 }
58
59 static void
60 sp_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
61 struct pipe_video_surface *past,
62 struct pipe_video_surface *future,
63 unsigned num_macroblocks,
64 struct pipe_macroblock *macroblocks,
65 struct pipe_fence_handle **fence)
66 {
67 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
68 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
69
70 assert(vpipe);
71 assert(num_macroblocks);
72 assert(macroblocks);
73 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
74 assert(ctx->decode_target);
75
76 vl_mpeg12_mc_renderer_render_macroblocks(&ctx->mc_renderer,
77 softpipe_video_surface(ctx->decode_target)->tex,
78 past ? softpipe_video_surface(past)->tex : NULL,
79 future ? softpipe_video_surface(future)->tex : NULL,
80 num_macroblocks, mpeg12_macroblocks, fence);
81 }
82
83 static void
84 sp_mpeg12_surface_fill(struct pipe_video_context *vpipe,
85 struct pipe_surface *dst,
86 unsigned dstx, unsigned dsty,
87 unsigned width, unsigned height,
88 unsigned value)
89 {
90 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
91
92 assert(vpipe);
93 assert(dst);
94
95 if (ctx->pipe->surface_fill)
96 ctx->pipe->surface_fill(ctx->pipe, dst, dstx, dsty, width, height, value);
97 else
98 util_surface_fill(ctx->pipe, dst, dstx, dsty, width, height, value);
99 }
100
101 static void
102 sp_mpeg12_surface_copy(struct pipe_video_context *vpipe,
103 struct pipe_surface *dst,
104 unsigned dstx, unsigned dsty,
105 struct pipe_surface *src,
106 unsigned srcx, unsigned srcy,
107 unsigned width, unsigned height)
108 {
109 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
110
111 assert(vpipe);
112 assert(dst);
113
114 if (ctx->pipe->surface_copy)
115 ctx->pipe->surface_copy(ctx->pipe, dst, dstx, dsty, src, srcx, srcy, width, height);
116 else
117 util_surface_copy(ctx->pipe, FALSE, dst, dstx, dsty, src, srcx, srcy, width, height);
118 }
119
120 static void
121 sp_mpeg12_render_picture(struct pipe_video_context *vpipe,
122 struct pipe_video_surface *src_surface,
123 enum pipe_mpeg12_picture_type picture_type,
124 /*unsigned num_past_surfaces,
125 struct pipe_video_surface *past_surfaces,
126 unsigned num_future_surfaces,
127 struct pipe_video_surface *future_surfaces,*/
128 struct pipe_video_rect *src_area,
129 struct pipe_surface *dst_surface,
130 struct pipe_video_rect *dst_area,
131 struct pipe_fence_handle **fence)
132 {
133 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
134
135 assert(vpipe);
136 assert(src_surface);
137 assert(src_area);
138 assert(dst_surface);
139 assert(dst_area);
140
141 vl_compositor_render(&ctx->compositor, softpipe_video_surface(src_surface)->tex,
142 picture_type, src_area, dst_surface->texture, dst_area, fence);
143 }
144
145 static void
146 sp_mpeg12_set_picture_background(struct pipe_video_context *vpipe,
147 struct pipe_texture *bg,
148 struct pipe_video_rect *bg_src_rect)
149 {
150 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
151
152 assert(vpipe);
153 assert(bg);
154 assert(bg_src_rect);
155
156 vl_compositor_set_background(&ctx->compositor, bg, bg_src_rect);
157 }
158
159 static void
160 sp_mpeg12_set_picture_layers(struct pipe_video_context *vpipe,
161 struct pipe_texture *layers[],
162 struct pipe_video_rect *src_rects[],
163 struct pipe_video_rect *dst_rects[],
164 unsigned num_layers)
165 {
166 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
167
168 assert(vpipe);
169 assert((layers && src_rects && dst_rects) ||
170 (!layers && !src_rects && !dst_rects));
171
172 vl_compositor_set_layers(&ctx->compositor, layers, src_rects, dst_rects, num_layers);
173 }
174
175 static void
176 sp_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
177 struct pipe_video_surface *dt)
178 {
179 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
180
181 assert(vpipe);
182 assert(dt);
183
184 pipe_video_surface_reference(&ctx->decode_target, dt);
185 }
186
187 static void
188 sp_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
189 {
190 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
191
192 assert(vpipe);
193
194 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
195 }
196
197 static bool
198 init_pipe_state(struct sp_mpeg12_context *ctx)
199 {
200 struct pipe_rasterizer_state rast;
201 struct pipe_blend_state blend;
202 struct pipe_depth_stencil_alpha_state dsa;
203 unsigned i;
204
205 assert(ctx);
206
207 rast.flatshade = 1;
208 rast.flatshade_first = 0;
209 rast.light_twoside = 0;
210 rast.front_winding = PIPE_WINDING_CCW;
211 rast.cull_mode = PIPE_WINDING_CW;
212 rast.fill_cw = PIPE_POLYGON_MODE_FILL;
213 rast.fill_ccw = PIPE_POLYGON_MODE_FILL;
214 rast.offset_cw = 0;
215 rast.offset_ccw = 0;
216 rast.scissor = 0;
217 rast.poly_smooth = 0;
218 rast.poly_stipple_enable = 0;
219 rast.point_sprite = 0;
220 rast.point_size_per_vertex = 0;
221 rast.multisample = 0;
222 rast.line_smooth = 0;
223 rast.line_stipple_enable = 0;
224 rast.line_stipple_factor = 0;
225 rast.line_stipple_pattern = 0;
226 rast.line_last_pixel = 0;
227 rast.bypass_vs_clip_and_viewport = 0;
228 rast.line_width = 1;
229 rast.point_smooth = 0;
230 rast.point_size = 1;
231 rast.offset_units = 1;
232 rast.offset_scale = 1;
233 rast.gl_rasterization_rules = 1;
234 /*rast.sprite_coord_mode[i] = ;*/
235 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
236 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
237
238 blend.blend_enable = 0;
239 blend.rgb_func = PIPE_BLEND_ADD;
240 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
241 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
242 blend.alpha_func = PIPE_BLEND_ADD;
243 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
244 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
245 blend.logicop_enable = 0;
246 blend.logicop_func = PIPE_LOGICOP_CLEAR;
247 /* Needed to allow color writes to FB, even if blending disabled */
248 blend.colormask = PIPE_MASK_RGBA;
249 blend.dither = 0;
250 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
251 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
252
253 dsa.depth.enabled = 0;
254 dsa.depth.writemask = 0;
255 dsa.depth.func = PIPE_FUNC_ALWAYS;
256 for (i = 0; i < 2; ++i) {
257 dsa.stencil[i].enabled = 0;
258 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
259 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
260 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
261 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
262 dsa.stencil[i].ref_value = 0;
263 dsa.stencil[i].valuemask = 0;
264 dsa.stencil[i].writemask = 0;
265 }
266 dsa.alpha.enabled = 0;
267 dsa.alpha.func = PIPE_FUNC_ALWAYS;
268 dsa.alpha.ref_value = 0;
269 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
270 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
271
272 return true;
273 }
274
275 static struct pipe_video_context *
276 sp_mpeg12_create(struct pipe_context *pipe, enum pipe_video_profile profile,
277 enum pipe_video_chroma_format chroma_format,
278 unsigned width, unsigned height,
279 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
280 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
281 bool pot_buffers)
282 {
283 struct sp_mpeg12_context *ctx;
284
285 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
286
287 ctx = CALLOC_STRUCT(sp_mpeg12_context);
288
289 if (!ctx)
290 return NULL;
291
292 ctx->base.profile = profile;
293 ctx->base.chroma_format = chroma_format;
294 ctx->base.width = width;
295 ctx->base.height = height;
296
297 ctx->base.screen = pipe->screen;
298 ctx->base.destroy = sp_mpeg12_destroy;
299 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
300 ctx->base.render_picture = sp_mpeg12_render_picture;
301 ctx->base.surface_fill = sp_mpeg12_surface_fill;
302 ctx->base.surface_copy = sp_mpeg12_surface_copy;
303 ctx->base.set_picture_background = sp_mpeg12_set_picture_background;
304 ctx->base.set_picture_layers = sp_mpeg12_set_picture_layers;
305 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
306 ctx->base.set_csc_matrix = sp_mpeg12_set_csc_matrix;
307
308 ctx->pipe = pipe;
309
310 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
311 width, height, chroma_format,
312 bufmode, eb_handling, pot_buffers)) {
313 ctx->pipe->destroy(ctx->pipe);
314 FREE(ctx);
315 return NULL;
316 }
317
318 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
319 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
320 ctx->pipe->destroy(ctx->pipe);
321 FREE(ctx);
322 return NULL;
323 }
324
325 if (!init_pipe_state(ctx)) {
326 vl_compositor_cleanup(&ctx->compositor);
327 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
328 ctx->pipe->destroy(ctx->pipe);
329 FREE(ctx);
330 return NULL;
331 }
332
333 return &ctx->base;
334 }
335
336 struct pipe_video_context *
337 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
338 enum pipe_video_chroma_format chroma_format,
339 unsigned width, unsigned height)
340 {
341 struct pipe_context *pipe;
342
343 assert(screen);
344 assert(width && height);
345
346 pipe = softpipe_create(screen);
347 if (!pipe)
348 return NULL;
349
350 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
351 /* TODO: Use XFER_NONE when implemented */
352 return sp_video_create_ex(pipe, profile,
353 chroma_format,
354 width, height,
355 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
356 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
357 true);
358 }
359
360 struct pipe_video_context *
361 sp_video_create_ex(struct pipe_context *pipe, enum pipe_video_profile profile,
362 enum pipe_video_chroma_format chroma_format,
363 unsigned width, unsigned height,
364 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
365 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
366 bool pot_buffers)
367 {
368 assert(pipe);
369 assert(width && height);
370
371 switch (u_reduce_video_profile(profile)) {
372 case PIPE_VIDEO_CODEC_MPEG12:
373 return sp_mpeg12_create(pipe, profile,
374 chroma_format,
375 width, height,
376 bufmode, eb_handling,
377 pot_buffers);
378 default:
379 return NULL;
380 }
381 }