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