g3dvl: Color space conv interface & vl impl.
[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 void sp_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
113 {
114 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
115
116 assert(vpipe);
117
118 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
119 }
120
121 static bool
122 init_pipe_state(struct sp_mpeg12_context *ctx)
123 {
124 struct pipe_rasterizer_state rast;
125 struct pipe_blend_state blend;
126 struct pipe_depth_stencil_alpha_state dsa;
127 unsigned i;
128
129 assert(ctx);
130
131 rast.flatshade = 1;
132 rast.flatshade_first = 0;
133 rast.light_twoside = 0;
134 rast.front_winding = PIPE_WINDING_CCW;
135 rast.cull_mode = PIPE_WINDING_CW;
136 rast.fill_cw = PIPE_POLYGON_MODE_FILL;
137 rast.fill_ccw = PIPE_POLYGON_MODE_FILL;
138 rast.offset_cw = 0;
139 rast.offset_ccw = 0;
140 rast.scissor = 0;
141 rast.poly_smooth = 0;
142 rast.poly_stipple_enable = 0;
143 rast.point_sprite = 0;
144 rast.point_size_per_vertex = 0;
145 rast.multisample = 0;
146 rast.line_smooth = 0;
147 rast.line_stipple_enable = 0;
148 rast.line_stipple_factor = 0;
149 rast.line_stipple_pattern = 0;
150 rast.line_last_pixel = 0;
151 rast.bypass_vs_clip_and_viewport = 0;
152 rast.line_width = 1;
153 rast.point_smooth = 0;
154 rast.point_size = 1;
155 rast.offset_units = 1;
156 rast.offset_scale = 1;
157 /*rast.sprite_coord_mode[i] = ;*/
158 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
159 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
160
161 blend.blend_enable = 0;
162 blend.rgb_func = PIPE_BLEND_ADD;
163 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
164 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
165 blend.alpha_func = PIPE_BLEND_ADD;
166 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
167 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
168 blend.logicop_enable = 0;
169 blend.logicop_func = PIPE_LOGICOP_CLEAR;
170 /* Needed to allow color writes to FB, even if blending disabled */
171 blend.colormask = PIPE_MASK_RGBA;
172 blend.dither = 0;
173 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
174 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
175
176 dsa.depth.enabled = 0;
177 dsa.depth.writemask = 0;
178 dsa.depth.func = PIPE_FUNC_ALWAYS;
179 for (i = 0; i < 2; ++i) {
180 dsa.stencil[i].enabled = 0;
181 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
182 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
183 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
184 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
185 dsa.stencil[i].ref_value = 0;
186 dsa.stencil[i].valuemask = 0;
187 dsa.stencil[i].writemask = 0;
188 }
189 dsa.alpha.enabled = 0;
190 dsa.alpha.func = PIPE_FUNC_ALWAYS;
191 dsa.alpha.ref_value = 0;
192 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
193 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
194
195 return true;
196 }
197
198 static struct pipe_video_context *
199 sp_mpeg12_create(struct pipe_screen *screen, enum pipe_video_profile profile,
200 enum pipe_video_chroma_format chroma_format,
201 unsigned width, unsigned height)
202 {
203 struct sp_mpeg12_context *ctx;
204
205 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
206
207 ctx = CALLOC_STRUCT(sp_mpeg12_context);
208
209 if (!ctx)
210 return NULL;
211
212 ctx->base.profile = profile;
213 ctx->base.chroma_format = chroma_format;
214 ctx->base.width = width;
215 ctx->base.height = height;
216
217 ctx->base.screen = screen;
218 ctx->base.destroy = sp_mpeg12_destroy;
219 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
220 ctx->base.clear_surface = sp_mpeg12_clear_surface;
221 ctx->base.render_picture = sp_mpeg12_render_picture;
222 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
223 ctx->base.set_csc_matrix = sp_mpeg12_set_csc_matrix;
224
225 ctx->pipe = softpipe_create(screen);
226 if (!ctx->pipe) {
227 FREE(ctx);
228 return NULL;
229 }
230
231 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture */
232 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
233 width, height, chroma_format,
234 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
235 /* TODO: Use XFER_NONE when implemented */
236 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
237 true)) {
238 ctx->pipe->destroy(ctx->pipe);
239 FREE(ctx);
240 return NULL;
241 }
242
243 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
244 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
245 ctx->pipe->destroy(ctx->pipe);
246 FREE(ctx);
247 return NULL;
248 }
249
250 if (!init_pipe_state(ctx)) {
251 vl_compositor_cleanup(&ctx->compositor);
252 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
253 ctx->pipe->destroy(ctx->pipe);
254 FREE(ctx);
255 return NULL;
256 }
257
258 return &ctx->base;
259 }
260
261 struct pipe_video_context *
262 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
263 enum pipe_video_chroma_format chroma_format,
264 unsigned width, unsigned height)
265 {
266 assert(screen);
267 assert(width && height);
268
269 switch (u_reduce_video_profile(profile)) {
270 case PIPE_VIDEO_CODEC_MPEG12:
271 return sp_mpeg12_create(screen, profile,
272 chroma_format,
273 width, height);
274 default:
275 return NULL;
276 }
277 }