st/xa: Use PIPE_FORMAT_R8_UNORM when available
[mesa.git] / src / gallium / state_trackers / xa / xa_composite.c
1 /**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 *********************************************************
25 * Authors:
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
28 */
29
30 #include "xa_composite.h"
31 #include "xa_context.h"
32 #include "xa_priv.h"
33 #include "cso_cache/cso_context.h"
34 #include "util/u_sampler.h"
35 #include "util/u_inlines.h"
36
37
38 /*XXX also in Xrender.h but the including it here breaks compilition */
39 #define XFixedToDouble(f) (((double) (f)) / 65536.)
40
41 struct xa_composite_blend {
42 unsigned op : 8;
43
44 unsigned alpha_dst : 4;
45 unsigned alpha_src : 4;
46
47 unsigned rgb_src : 8; /**< PIPE_BLENDFACTOR_x */
48 unsigned rgb_dst : 8; /**< PIPE_BLENDFACTOR_x */
49 };
50
51 #define XA_BLEND_OP_OVER 3
52 static const struct xa_composite_blend xa_blends[] = {
53 { xa_op_clear,
54 0, 0, PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_ZERO},
55 { xa_op_src,
56 0, 0, PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO},
57 { xa_op_dst,
58 0, 0, PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_ONE},
59 { xa_op_over,
60 0, 1, PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_INV_SRC_ALPHA},
61 { xa_op_over_reverse,
62 1, 0, PIPE_BLENDFACTOR_INV_DST_ALPHA, PIPE_BLENDFACTOR_ONE},
63 { xa_op_in,
64 1, 0, PIPE_BLENDFACTOR_DST_ALPHA, PIPE_BLENDFACTOR_ZERO},
65 { xa_op_in_reverse,
66 0, 1, PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_SRC_ALPHA},
67 { xa_op_out,
68 1, 0, PIPE_BLENDFACTOR_INV_DST_ALPHA, PIPE_BLENDFACTOR_ZERO},
69 { xa_op_out_reverse,
70 0, 1, PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_INV_SRC_ALPHA},
71 { xa_op_atop,
72 1, 1, PIPE_BLENDFACTOR_DST_ALPHA, PIPE_BLENDFACTOR_INV_SRC_ALPHA},
73 { xa_op_atop_reverse,
74 1, 1, PIPE_BLENDFACTOR_INV_DST_ALPHA, PIPE_BLENDFACTOR_SRC_ALPHA},
75 { xa_op_xor,
76 1, 1, PIPE_BLENDFACTOR_INV_DST_ALPHA, PIPE_BLENDFACTOR_INV_SRC_ALPHA},
77 { xa_op_add,
78 0, 0, PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ONE},
79 };
80
81 static boolean
82 blend_for_op(struct xa_composite_blend *blend,
83 enum xa_composite_op op,
84 struct xa_picture *src_pic,
85 struct xa_picture *mask_pic,
86 struct xa_picture *dst_pic)
87 {
88 const int num_blends =
89 sizeof(xa_blends)/sizeof(struct xa_composite_blend);
90 int i;
91 boolean supported = FALSE;
92
93 /*
94 * Temporarily disable component alpha since it appears buggy.
95 */
96 if (mask_pic && mask_pic->component_alpha)
97 return FALSE;
98
99 /*
100 * our default in case something goes wrong
101 */
102 *blend = xa_blends[XA_BLEND_OP_OVER];
103
104 for (i = 0; i < num_blends; ++i) {
105 if (xa_blends[i].op == op) {
106 *blend = xa_blends[i];
107 supported = TRUE;
108 }
109 }
110
111 if (!dst_pic->srf)
112 return supported;
113
114 /*
115 * None of the hardware formats we might use for dst A8 are
116 * suitable for dst_alpha blending, since they present the
117 * alpha channel either in all color channels (L8_UNORM) or
118 * in the red channel only (R8_UNORM)
119 */
120 if ((dst_pic->srf->tex->format == PIPE_FORMAT_L8_UNORM ||
121 dst_pic->srf->tex->format == PIPE_FORMAT_R8_UNORM) &&
122 blend->alpha_dst)
123 return FALSE;
124
125 /*
126 * If there's no dst alpha channel, adjust the blend op so that we'll treat
127 * it as always 1.
128 */
129
130 if (xa_format_a(dst_pic->pict_format) == 0 && blend->alpha_dst) {
131 if (blend->rgb_src == PIPE_BLENDFACTOR_DST_ALPHA)
132 blend->rgb_src = PIPE_BLENDFACTOR_ONE;
133 else if (blend->rgb_src == PIPE_BLENDFACTOR_INV_DST_ALPHA)
134 blend->rgb_src = PIPE_BLENDFACTOR_ZERO;
135 }
136
137 /*
138 * If the source alpha is being used, then we should only be in a case where
139 * the source blend factor is 0, and the source blend value is the mask
140 * channels multiplied by the source picture's alpha.
141 */
142 if (mask_pic && mask_pic->component_alpha &&
143 xa_format_rgb(mask_pic->pict_format) &&
144 blend->alpha_src) {
145 if (blend->rgb_dst == PIPE_BLENDFACTOR_SRC_ALPHA) {
146 blend->rgb_dst = PIPE_BLENDFACTOR_SRC_COLOR;
147 } else if (blend->rgb_dst == PIPE_BLENDFACTOR_INV_SRC_ALPHA) {
148 blend->rgb_dst = PIPE_BLENDFACTOR_INV_SRC_COLOR;
149 }
150 }
151
152 return supported;
153 }
154
155
156 static inline int
157 xa_repeat_to_gallium(int mode)
158 {
159 switch(mode) {
160 case xa_wrap_clamp_to_border:
161 return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
162 case xa_wrap_repeat:
163 return PIPE_TEX_WRAP_REPEAT;
164 case xa_wrap_mirror_repeat:
165 return PIPE_TEX_WRAP_MIRROR_REPEAT;
166 case xa_wrap_clamp_to_edge:
167 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
168 default:
169 break;
170 }
171 return PIPE_TEX_WRAP_REPEAT;
172 }
173
174 static inline boolean
175 xa_filter_to_gallium(int xrender_filter, int *out_filter)
176 {
177
178 switch (xrender_filter) {
179 case xa_filter_nearest:
180 *out_filter = PIPE_TEX_FILTER_NEAREST;
181 break;
182 case xa_filter_linear:
183 *out_filter = PIPE_TEX_FILTER_LINEAR;
184 break;
185 default:
186 *out_filter = PIPE_TEX_FILTER_NEAREST;
187 return FALSE;
188 }
189 return TRUE;
190 }
191
192 static int
193 xa_is_filter_accelerated(struct xa_picture *pic)
194 {
195 int filter;
196 if (pic && !xa_filter_to_gallium(pic->filter, &filter))
197 return 0;
198 return 1;
199 }
200
201 XA_EXPORT int
202 xa_composite_check_accelerated(const struct xa_composite *comp)
203 {
204 struct xa_composite_blend blend;
205 struct xa_picture *src_pic = comp->src;
206
207 if (!xa_is_filter_accelerated(src_pic) ||
208 !xa_is_filter_accelerated(comp->mask)) {
209 return -XA_ERR_INVAL;
210 }
211
212
213 if (src_pic->src_pict) {
214 if (src_pic->src_pict->type != xa_src_pict_solid_fill)
215 return -XA_ERR_INVAL;
216
217 /*
218 * Currently we don't support solid fill with a mask.
219 * We can easily do that, but that would require shader,
220 * sampler view setup and vertex setup modification.
221 */
222 if (comp->mask)
223 return -XA_ERR_INVAL;
224 }
225
226 if (blend_for_op(&blend, comp->op, comp->src, comp->mask, comp->dst)) {
227 struct xa_picture *mask = comp->mask;
228 if (mask && mask->component_alpha &&
229 xa_format_rgb(mask->pict_format)) {
230 if (blend.alpha_src && blend.rgb_src != PIPE_BLENDFACTOR_ZERO) {
231 return -XA_ERR_INVAL;
232 }
233 }
234
235 return XA_ERR_NONE;
236 }
237 return -XA_ERR_INVAL;
238 }
239
240 static int
241 bind_composite_blend_state(struct xa_context *ctx,
242 const struct xa_composite *comp)
243 {
244 struct xa_composite_blend blend_opt;
245 struct pipe_blend_state blend;
246
247 if (!blend_for_op(&blend_opt, comp->op, comp->src, comp->mask, comp->dst))
248 return -XA_ERR_INVAL;
249
250 memset(&blend, 0, sizeof(struct pipe_blend_state));
251 blend.rt[0].blend_enable = 1;
252 blend.rt[0].colormask = PIPE_MASK_RGBA;
253
254 blend.rt[0].rgb_src_factor = blend_opt.rgb_src;
255 blend.rt[0].alpha_src_factor = blend_opt.rgb_src;
256 blend.rt[0].rgb_dst_factor = blend_opt.rgb_dst;
257 blend.rt[0].alpha_dst_factor = blend_opt.rgb_dst;
258
259 cso_set_blend(ctx->cso, &blend);
260 return XA_ERR_NONE;
261 }
262
263 static unsigned int
264 picture_format_fixups(struct xa_picture *src_pic,
265 int mask)
266 {
267 boolean set_alpha = FALSE;
268 boolean swizzle = FALSE;
269 unsigned ret = 0;
270 struct xa_surface *src = src_pic->srf;
271 enum xa_formats src_hw_format, src_pic_format;
272 enum xa_surface_type src_hw_type, src_pic_type;
273
274 if (!src)
275 return 0;
276
277 src_hw_format = xa_surface_format(src);
278 src_pic_format = src_pic->pict_format;
279
280 set_alpha = (xa_format_type_is_color(src_pic_format) &&
281 xa_format_a(src_pic_format) == 0);
282
283 if (set_alpha)
284 ret |= mask ? FS_MASK_SET_ALPHA : FS_SRC_SET_ALPHA;
285
286 if (src_hw_format == src_pic_format) {
287 if (src->tex->format == PIPE_FORMAT_L8_UNORM ||
288 src->tex->format == PIPE_FORMAT_R8_UNORM)
289 return ((mask) ? FS_MASK_LUMINANCE : FS_SRC_LUMINANCE);
290
291 return ret;
292 }
293
294 src_hw_type = xa_format_type(src_hw_format);
295 src_pic_type = xa_format_type(src_pic_format);
296
297 swizzle = ((src_hw_type == xa_type_argb &&
298 src_pic_type == xa_type_abgr) ||
299 ((src_hw_type == xa_type_abgr &&
300 src_pic_type == xa_type_argb)));
301
302 if (!swizzle && (src_hw_type != src_pic_type))
303 return ret;
304
305 if (swizzle)
306 ret |= mask ? FS_MASK_SWIZZLE_RGB : FS_SRC_SWIZZLE_RGB;
307
308 return ret;
309 }
310
311 static int
312 bind_shaders(struct xa_context *ctx, const struct xa_composite *comp)
313 {
314 unsigned vs_traits = 0, fs_traits = 0;
315 struct xa_shader shader;
316 struct xa_picture *src_pic = comp->src;
317 struct xa_picture *mask_pic = comp->mask;
318
319 ctx->has_solid_color = FALSE;
320
321 if (src_pic) {
322 if (src_pic->wrap == xa_wrap_clamp_to_border && src_pic->has_transform)
323 fs_traits |= FS_SRC_REPEAT_NONE;
324
325 if (src_pic->src_pict) {
326 if (src_pic->src_pict->type == xa_src_pict_solid_fill) {
327 fs_traits |= FS_SOLID_FILL | FS_FILL;
328 vs_traits |= VS_SOLID_FILL;
329 xa_pixel_to_float4(src_pic->src_pict->solid_fill.color,
330 ctx->solid_color);
331 ctx->has_solid_color = TRUE;
332 }
333 } else {
334 fs_traits |= FS_COMPOSITE;
335 vs_traits |= VS_COMPOSITE;
336 }
337
338 fs_traits |= picture_format_fixups(src_pic, 0);
339 }
340
341 if (mask_pic) {
342 vs_traits |= VS_MASK;
343 fs_traits |= FS_MASK;
344 if (mask_pic->wrap == xa_wrap_clamp_to_border &&
345 mask_pic->has_transform)
346 fs_traits |= FS_MASK_REPEAT_NONE;
347
348 if (mask_pic->component_alpha) {
349 struct xa_composite_blend blend;
350 if (!blend_for_op(&blend, comp->op, src_pic, mask_pic, NULL))
351 return -XA_ERR_INVAL;
352
353 if (blend.alpha_src) {
354 fs_traits |= FS_CA_SRCALPHA;
355 } else
356 fs_traits |= FS_CA_FULL;
357 }
358
359 fs_traits |= picture_format_fixups(mask_pic, 1);
360 }
361
362 if (ctx->srf->format == PIPE_FORMAT_L8_UNORM ||
363 ctx->srf->format == PIPE_FORMAT_R8_UNORM)
364 fs_traits |= FS_DST_LUMINANCE;
365
366 shader = xa_shaders_get(ctx->shaders, vs_traits, fs_traits);
367 cso_set_vertex_shader_handle(ctx->cso, shader.vs);
368 cso_set_fragment_shader_handle(ctx->cso, shader.fs);
369 return XA_ERR_NONE;
370 }
371
372 static void
373 bind_samplers(struct xa_context *ctx,
374 const struct xa_composite *comp)
375 {
376 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
377 struct pipe_sampler_state src_sampler, mask_sampler;
378 struct pipe_sampler_view view_templ;
379 struct pipe_sampler_view *src_view;
380 struct pipe_context *pipe = ctx->pipe;
381 struct xa_picture *src_pic = comp->src;
382 struct xa_picture *mask_pic = comp->mask;
383
384 ctx->num_bound_samplers = 0;
385
386 memset(&src_sampler, 0, sizeof(struct pipe_sampler_state));
387 memset(&mask_sampler, 0, sizeof(struct pipe_sampler_state));
388
389 if (src_pic) {
390 if (ctx->has_solid_color) {
391 samplers[0] = NULL;
392 pipe_sampler_view_reference(&ctx->bound_sampler_views[0], NULL);
393 } else {
394 unsigned src_wrap = xa_repeat_to_gallium(src_pic->wrap);
395 int filter;
396
397 (void) xa_filter_to_gallium(src_pic->filter, &filter);
398
399 src_sampler.wrap_s = src_wrap;
400 src_sampler.wrap_t = src_wrap;
401 src_sampler.min_img_filter = filter;
402 src_sampler.mag_img_filter = filter;
403 src_sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
404 src_sampler.normalized_coords = 1;
405 samplers[0] = &src_sampler;
406 ctx->num_bound_samplers = 1;
407 u_sampler_view_default_template(&view_templ,
408 src_pic->srf->tex,
409 src_pic->srf->tex->format);
410 src_view = pipe->create_sampler_view(pipe, src_pic->srf->tex,
411 &view_templ);
412 pipe_sampler_view_reference(&ctx->bound_sampler_views[0], NULL);
413 ctx->bound_sampler_views[0] = src_view;
414 }
415 }
416
417 if (mask_pic) {
418 unsigned mask_wrap = xa_repeat_to_gallium(mask_pic->wrap);
419 int filter;
420
421 (void) xa_filter_to_gallium(mask_pic->filter, &filter);
422
423 mask_sampler.wrap_s = mask_wrap;
424 mask_sampler.wrap_t = mask_wrap;
425 mask_sampler.min_img_filter = filter;
426 mask_sampler.mag_img_filter = filter;
427 src_sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
428 mask_sampler.normalized_coords = 1;
429 samplers[1] = &mask_sampler;
430 ctx->num_bound_samplers = 2;
431 u_sampler_view_default_template(&view_templ,
432 mask_pic->srf->tex,
433 mask_pic->srf->tex->format);
434 src_view = pipe->create_sampler_view(pipe, mask_pic->srf->tex,
435 &view_templ);
436 pipe_sampler_view_reference(&ctx->bound_sampler_views[1], NULL);
437 ctx->bound_sampler_views[1] = src_view;
438
439
440 /*
441 * If src is a solid color, we have no src view, so set up a
442 * dummy one that will not be used anyway.
443 */
444 if (ctx->bound_sampler_views[0] == NULL)
445 pipe_sampler_view_reference(&ctx->bound_sampler_views[0],
446 src_view);
447
448 }
449
450 cso_set_samplers(ctx->cso, PIPE_SHADER_FRAGMENT, ctx->num_bound_samplers,
451 (const struct pipe_sampler_state **)samplers);
452 cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, ctx->num_bound_samplers,
453 ctx->bound_sampler_views);
454 }
455
456 XA_EXPORT int
457 xa_composite_prepare(struct xa_context *ctx,
458 const struct xa_composite *comp)
459 {
460 struct xa_surface *dst_srf = comp->dst->srf;
461 int ret;
462
463 if (comp->mask && !comp->mask->srf)
464 return -XA_ERR_INVAL;
465
466 ret = xa_ctx_srf_create(ctx, dst_srf);
467 if (ret != XA_ERR_NONE)
468 return ret;
469
470 ctx->dst = dst_srf;
471 renderer_bind_destination(ctx, ctx->srf);
472
473 ret = bind_composite_blend_state(ctx, comp);
474 if (ret != XA_ERR_NONE)
475 return ret;
476 ret = bind_shaders(ctx, comp);
477 if (ret != XA_ERR_NONE)
478 return ret;
479 bind_samplers(ctx, comp);
480
481 if (ctx->num_bound_samplers == 0 ) { /* solid fill */
482 renderer_begin_solid(ctx);
483 } else {
484 renderer_begin_textures(ctx);
485 ctx->comp = comp;
486 }
487
488 xa_ctx_srf_destroy(ctx);
489 return XA_ERR_NONE;
490 }
491
492 XA_EXPORT void
493 xa_composite_rect(struct xa_context *ctx,
494 int srcX, int srcY, int maskX, int maskY,
495 int dstX, int dstY, int width, int height)
496 {
497 if (ctx->num_bound_samplers == 0 ) { /* solid fill */
498 renderer_solid(ctx, dstX, dstY, dstX + width, dstY + height,
499 ctx->solid_color);
500 } else {
501 const struct xa_composite *comp = ctx->comp;
502 int pos[6] = {srcX, srcY, maskX, maskY, dstX, dstY};
503 const float *src_matrix = NULL;
504 const float *mask_matrix = NULL;
505
506 xa_scissor_update(ctx, dstX, dstY, dstX + width, dstY + height);
507
508 if (comp->src->has_transform)
509 src_matrix = comp->src->transform;
510 if (comp->mask && comp->mask->has_transform)
511 mask_matrix = comp->mask->transform;
512
513 renderer_texture(ctx, pos, width, height,
514 src_matrix, mask_matrix);
515 }
516 }
517
518 XA_EXPORT void
519 xa_composite_done(struct xa_context *ctx)
520 {
521 renderer_draw_flush(ctx);
522
523 ctx->comp = NULL;
524 ctx->has_solid_color = FALSE;
525 xa_ctx_sampler_views_destroy(ctx);
526 }
527
528 static const struct xa_composite_allocation a = {
529 .xa_composite_size = sizeof(struct xa_composite),
530 .xa_picture_size = sizeof(struct xa_picture),
531 .xa_source_pict_size = sizeof(union xa_source_pict),
532 };
533
534 XA_EXPORT const struct xa_composite_allocation *
535 xa_composite_allocation(void)
536 {
537 return &a;
538 }