Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / softpipe / sp_video_context.c
1 #include "sp_video_context.h"
2 #include <pipe/p_inlines.h>
3 #include <util/u_memory.h>
4 #include "softpipe/sp_winsys.h"
5 #include "softpipe/sp_texture.h"
6
7 static void
8 sp_mpeg12_destroy(struct pipe_video_context *vpipe)
9 {
10 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
11
12 assert(vpipe);
13
14 /* Asserted in softpipe_delete_fs_state() for some reason */
15 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
16 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
17
18 ctx->pipe->delete_blend_state(ctx->pipe, ctx->blend);
19 ctx->pipe->delete_rasterizer_state(ctx->pipe, ctx->rast);
20 ctx->pipe->delete_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
21
22 pipe_video_surface_reference(&ctx->decode_target, NULL);
23 vl_compositor_cleanup(&ctx->compositor);
24 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
25 ctx->pipe->destroy(ctx->pipe);
26
27 FREE(ctx);
28 }
29
30 static void
31 sp_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
32 struct pipe_video_surface *past,
33 struct pipe_video_surface *future,
34 unsigned num_macroblocks,
35 struct pipe_macroblock *macroblocks,
36 struct pipe_fence_handle **fence)
37 {
38 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
39 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
40
41 assert(vpipe);
42 assert(num_macroblocks);
43 assert(macroblocks);
44 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
45 assert(ctx->decode_target);
46
47 vl_mpeg12_mc_renderer_render_macroblocks(&ctx->mc_renderer,
48 softpipe_video_surface(ctx->decode_target)->tex,
49 past ? softpipe_video_surface(past)->tex : NULL,
50 future ? softpipe_video_surface(future)->tex : NULL,
51 num_macroblocks, mpeg12_macroblocks, fence);
52 }
53
54 static void
55 sp_mpeg12_clear_surface(struct pipe_video_context *vpipe,
56 unsigned x, unsigned y,
57 unsigned width, unsigned height,
58 unsigned value,
59 struct pipe_surface *surface)
60 {
61 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
62
63 assert(vpipe);
64 assert(surface);
65
66 ctx->pipe->surface_fill(ctx->pipe, surface, x, y, width, height, value);
67 }
68
69 static void
70 sp_mpeg12_render_picture(struct pipe_video_context *vpipe,
71 /*struct pipe_surface *backround,
72 struct pipe_video_rect *backround_area,*/
73 struct pipe_video_surface *src_surface,
74 enum pipe_mpeg12_picture_type picture_type,
75 /*unsigned num_past_surfaces,
76 struct pipe_video_surface *past_surfaces,
77 unsigned num_future_surfaces,
78 struct pipe_video_surface *future_surfaces,*/
79 struct pipe_video_rect *src_area,
80 struct pipe_surface *dst_surface,
81 struct pipe_video_rect *dst_area,
82 /*unsigned num_layers,
83 struct pipe_surface *layers,
84 struct pipe_video_rect *layer_src_areas,
85 struct pipe_video_rect *layer_dst_areas*/
86 struct pipe_fence_handle **fence)
87 {
88 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
89
90 assert(vpipe);
91 assert(src_surface);
92 assert(src_area);
93 assert(dst_surface);
94 assert(dst_area);
95
96 vl_compositor_render(&ctx->compositor, softpipe_video_surface(src_surface)->tex,
97 picture_type, src_area, dst_surface->texture, dst_area, fence);
98 }
99
100 static void
101 sp_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
102 struct pipe_video_surface *dt)
103 {
104 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
105
106 assert(vpipe);
107 assert(dt);
108
109 pipe_video_surface_reference(&ctx->decode_target, dt);
110 }
111
112 static bool
113 init_pipe_state(struct sp_mpeg12_context *ctx)
114 {
115 struct pipe_rasterizer_state rast;
116 struct pipe_blend_state blend;
117 struct pipe_depth_stencil_alpha_state dsa;
118 unsigned i;
119
120 assert(ctx);
121
122 rast.flatshade = 1;
123 rast.flatshade_first = 0;
124 rast.light_twoside = 0;
125 rast.front_winding = PIPE_WINDING_CCW;
126 rast.cull_mode = PIPE_WINDING_CW;
127 rast.fill_cw = PIPE_POLYGON_MODE_FILL;
128 rast.fill_ccw = PIPE_POLYGON_MODE_FILL;
129 rast.offset_cw = 0;
130 rast.offset_ccw = 0;
131 rast.scissor = 0;
132 rast.poly_smooth = 0;
133 rast.poly_stipple_enable = 0;
134 rast.point_sprite = 0;
135 rast.point_size_per_vertex = 0;
136 rast.multisample = 0;
137 rast.line_smooth = 0;
138 rast.line_stipple_enable = 0;
139 rast.line_stipple_factor = 0;
140 rast.line_stipple_pattern = 0;
141 rast.line_last_pixel = 0;
142 rast.bypass_vs_clip_and_viewport = 0;
143 rast.line_width = 1;
144 rast.point_smooth = 0;
145 rast.point_size = 1;
146 rast.offset_units = 1;
147 rast.offset_scale = 1;
148 /*rast.sprite_coord_mode[i] = ;*/
149 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
150 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
151
152 blend.blend_enable = 0;
153 blend.rgb_func = PIPE_BLEND_ADD;
154 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
155 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
156 blend.alpha_func = PIPE_BLEND_ADD;
157 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
158 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
159 blend.logicop_enable = 0;
160 blend.logicop_func = PIPE_LOGICOP_CLEAR;
161 /* Needed to allow color writes to FB, even if blending disabled */
162 blend.colormask = PIPE_MASK_RGBA;
163 blend.dither = 0;
164 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
165 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
166
167 dsa.depth.enabled = 0;
168 dsa.depth.writemask = 0;
169 dsa.depth.func = PIPE_FUNC_ALWAYS;
170 dsa.depth.occlusion_count = 0;
171 for (i = 0; i < 2; ++i) {
172 dsa.stencil[i].enabled = 0;
173 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
174 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
175 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
176 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
177 dsa.stencil[i].ref_value = 0;
178 dsa.stencil[i].valuemask = 0;
179 dsa.stencil[i].writemask = 0;
180 }
181 dsa.alpha.enabled = 0;
182 dsa.alpha.func = PIPE_FUNC_ALWAYS;
183 dsa.alpha.ref_value = 0;
184 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
185 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
186
187 return true;
188 }
189
190 static struct pipe_video_context *
191 sp_mpeg12_create(struct pipe_screen *screen, enum pipe_video_profile profile,
192 enum pipe_video_chroma_format chroma_format,
193 unsigned width, unsigned height)
194 {
195 struct sp_mpeg12_context *ctx;
196
197 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
198
199 ctx = CALLOC_STRUCT(sp_mpeg12_context);
200
201 if (!ctx)
202 return NULL;
203
204 ctx->base.profile = profile;
205 ctx->base.chroma_format = chroma_format;
206 ctx->base.width = width;
207 ctx->base.height = height;
208
209 ctx->base.screen = screen;
210 ctx->base.destroy = sp_mpeg12_destroy;
211 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
212 ctx->base.clear_surface = sp_mpeg12_clear_surface;
213 ctx->base.render_picture = sp_mpeg12_render_picture;
214 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
215
216 ctx->pipe = softpipe_create(screen);
217 if (!ctx->pipe) {
218 FREE(ctx);
219 return NULL;
220 }
221
222 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture */
223 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
224 width, height, chroma_format,
225 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
226 /* TODO: Use XFER_NONE when implemented */
227 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
228 true)) {
229 ctx->pipe->destroy(ctx->pipe);
230 FREE(ctx);
231 return NULL;
232 }
233
234 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
235 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
236 ctx->pipe->destroy(ctx->pipe);
237 FREE(ctx);
238 return NULL;
239 }
240
241 if (!init_pipe_state(ctx)) {
242 vl_compositor_cleanup(&ctx->compositor);
243 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
244 ctx->pipe->destroy(ctx->pipe);
245 FREE(ctx);
246 return NULL;
247 }
248
249 return &ctx->base;
250 }
251
252 struct pipe_video_context *
253 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
254 enum pipe_video_chroma_format chroma_format,
255 unsigned width, unsigned height)
256 {
257 assert(screen);
258 assert(width && height);
259
260 switch (u_reduce_video_profile(profile)) {
261 case PIPE_VIDEO_CODEC_MPEG12:
262 return sp_mpeg12_create(screen, profile,
263 chroma_format,
264 width, height);
265 default:
266 return NULL;
267 }
268 }