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