1 /**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 *********************************************************
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 #include "xa_composite.h"
31 #include "xa_context.h"
33 #include "cso_cache/cso_context.h"
34 #include "util/u_sampler.h"
35 #include "util/u_inlines.h"
38 /*XXX also in Xrender.h but the including it here breaks compilition */
39 #define XFixedToDouble(f) (((double) (f)) / 65536.)
41 struct xa_composite_blend
{
44 unsigned alpha_dst
: 4;
45 unsigned alpha_src
: 4;
47 unsigned rgb_src
: 8; /**< PIPE_BLENDFACTOR_x */
48 unsigned rgb_dst
: 8; /**< PIPE_BLENDFACTOR_x */
51 #define XA_BLEND_OP_OVER 3
52 static const struct xa_composite_blend xa_blends
[] = {
54 0, 0, PIPE_BLENDFACTOR_ZERO
, PIPE_BLENDFACTOR_ZERO
},
56 0, 0, PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ZERO
},
58 0, 0, PIPE_BLENDFACTOR_ZERO
, PIPE_BLENDFACTOR_ONE
},
60 0, 1, PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_INV_SRC_ALPHA
},
62 1, 0, PIPE_BLENDFACTOR_INV_DST_ALPHA
, PIPE_BLENDFACTOR_ONE
},
64 1, 0, PIPE_BLENDFACTOR_DST_ALPHA
, PIPE_BLENDFACTOR_ZERO
},
66 0, 1, PIPE_BLENDFACTOR_ZERO
, PIPE_BLENDFACTOR_SRC_ALPHA
},
68 1, 0, PIPE_BLENDFACTOR_INV_DST_ALPHA
, PIPE_BLENDFACTOR_ZERO
},
70 0, 1, PIPE_BLENDFACTOR_ZERO
, PIPE_BLENDFACTOR_INV_SRC_ALPHA
},
72 1, 1, PIPE_BLENDFACTOR_DST_ALPHA
, PIPE_BLENDFACTOR_INV_SRC_ALPHA
},
74 1, 1, PIPE_BLENDFACTOR_INV_DST_ALPHA
, PIPE_BLENDFACTOR_SRC_ALPHA
},
76 1, 1, PIPE_BLENDFACTOR_INV_DST_ALPHA
, PIPE_BLENDFACTOR_INV_SRC_ALPHA
},
78 0, 0, PIPE_BLENDFACTOR_ONE
, PIPE_BLENDFACTOR_ONE
},
82 * The alpha value stored in a L8 texture is read by the
83 * hardware as color, and R8 is read as red. The source alpha value
84 * at the end of the fragment shader is stored in all color channels,
85 * so the correct approach is to blend using DST_COLOR instead of
86 * DST_ALPHA and then output any color channel (L8) or the red channel (R8).
89 xa_convert_blend_for_luminance(unsigned factor
)
92 case PIPE_BLENDFACTOR_DST_ALPHA
:
93 return PIPE_BLENDFACTOR_DST_COLOR
;
94 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
95 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
103 blend_for_op(struct xa_composite_blend
*blend
,
104 enum xa_composite_op op
,
105 struct xa_picture
*src_pic
,
106 struct xa_picture
*mask_pic
,
107 struct xa_picture
*dst_pic
)
109 const int num_blends
=
110 sizeof(xa_blends
)/sizeof(struct xa_composite_blend
);
112 boolean supported
= FALSE
;
115 * Temporarily disable component alpha since it appears buggy.
117 if (mask_pic
&& mask_pic
->component_alpha
)
121 * our default in case something goes wrong
123 *blend
= xa_blends
[XA_BLEND_OP_OVER
];
125 for (i
= 0; i
< num_blends
; ++i
) {
126 if (xa_blends
[i
].op
== op
) {
127 *blend
= xa_blends
[i
];
135 if ((dst_pic
->srf
->tex
->format
== PIPE_FORMAT_L8_UNORM
||
136 dst_pic
->srf
->tex
->format
== PIPE_FORMAT_R8_UNORM
)) {
137 blend
->rgb_src
= xa_convert_blend_for_luminance(blend
->rgb_src
);
138 blend
->rgb_dst
= xa_convert_blend_for_luminance(blend
->rgb_dst
);
142 * If there's no dst alpha channel, adjust the blend op so that we'll treat
146 if (xa_format_a(dst_pic
->pict_format
) == 0 && blend
->alpha_dst
) {
147 if (blend
->rgb_src
== PIPE_BLENDFACTOR_DST_ALPHA
)
148 blend
->rgb_src
= PIPE_BLENDFACTOR_ONE
;
149 else if (blend
->rgb_src
== PIPE_BLENDFACTOR_INV_DST_ALPHA
)
150 blend
->rgb_src
= PIPE_BLENDFACTOR_ZERO
;
154 * If the source alpha is being used, then we should only be in a case where
155 * the source blend factor is 0, and the source blend value is the mask
156 * channels multiplied by the source picture's alpha.
158 if (mask_pic
&& mask_pic
->component_alpha
&&
159 xa_format_rgb(mask_pic
->pict_format
) &&
161 if (blend
->rgb_dst
== PIPE_BLENDFACTOR_SRC_ALPHA
) {
162 blend
->rgb_dst
= PIPE_BLENDFACTOR_SRC_COLOR
;
163 } else if (blend
->rgb_dst
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
) {
164 blend
->rgb_dst
= PIPE_BLENDFACTOR_INV_SRC_COLOR
;
173 xa_repeat_to_gallium(int mode
)
176 case xa_wrap_clamp_to_border
:
177 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
179 return PIPE_TEX_WRAP_REPEAT
;
180 case xa_wrap_mirror_repeat
:
181 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
182 case xa_wrap_clamp_to_edge
:
183 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
187 return PIPE_TEX_WRAP_REPEAT
;
190 static inline boolean
191 xa_filter_to_gallium(int xrender_filter
, int *out_filter
)
194 switch (xrender_filter
) {
195 case xa_filter_nearest
:
196 *out_filter
= PIPE_TEX_FILTER_NEAREST
;
198 case xa_filter_linear
:
199 *out_filter
= PIPE_TEX_FILTER_LINEAR
;
202 *out_filter
= PIPE_TEX_FILTER_NEAREST
;
209 xa_is_filter_accelerated(struct xa_picture
*pic
)
212 if (pic
&& !xa_filter_to_gallium(pic
->filter
, &filter
))
218 xa_composite_check_accelerated(const struct xa_composite
*comp
)
220 struct xa_composite_blend blend
;
221 struct xa_picture
*src_pic
= comp
->src
;
223 if (!xa_is_filter_accelerated(src_pic
) ||
224 !xa_is_filter_accelerated(comp
->mask
)) {
225 return -XA_ERR_INVAL
;
229 if (src_pic
->src_pict
) {
230 if (src_pic
->src_pict
->type
!= xa_src_pict_solid_fill
)
231 return -XA_ERR_INVAL
;
234 * Currently we don't support solid fill with a mask.
235 * We can easily do that, but that would require shader,
236 * sampler view setup and vertex setup modification.
239 return -XA_ERR_INVAL
;
242 if (blend_for_op(&blend
, comp
->op
, comp
->src
, comp
->mask
, comp
->dst
)) {
243 struct xa_picture
*mask
= comp
->mask
;
244 if (mask
&& mask
->component_alpha
&&
245 xa_format_rgb(mask
->pict_format
)) {
246 if (blend
.alpha_src
&& blend
.rgb_src
!= PIPE_BLENDFACTOR_ZERO
) {
247 return -XA_ERR_INVAL
;
253 return -XA_ERR_INVAL
;
257 bind_composite_blend_state(struct xa_context
*ctx
,
258 const struct xa_composite
*comp
)
260 struct xa_composite_blend blend_opt
;
261 struct pipe_blend_state blend
;
263 if (!blend_for_op(&blend_opt
, comp
->op
, comp
->src
, comp
->mask
, comp
->dst
))
264 return -XA_ERR_INVAL
;
266 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
267 blend
.rt
[0].blend_enable
= 1;
268 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
270 blend
.rt
[0].rgb_src_factor
= blend_opt
.rgb_src
;
271 blend
.rt
[0].alpha_src_factor
= blend_opt
.rgb_src
;
272 blend
.rt
[0].rgb_dst_factor
= blend_opt
.rgb_dst
;
273 blend
.rt
[0].alpha_dst_factor
= blend_opt
.rgb_dst
;
275 cso_set_blend(ctx
->cso
, &blend
);
280 picture_format_fixups(struct xa_picture
*src_pic
,
283 boolean set_alpha
= FALSE
;
284 boolean swizzle
= FALSE
;
286 struct xa_surface
*src
= src_pic
->srf
;
287 enum xa_formats src_hw_format
, src_pic_format
;
288 enum xa_surface_type src_hw_type
, src_pic_type
;
293 src_hw_format
= xa_surface_format(src
);
294 src_pic_format
= src_pic
->pict_format
;
296 set_alpha
= (xa_format_type_is_color(src_pic_format
) &&
297 xa_format_a(src_pic_format
) == 0);
300 ret
|= mask
? FS_MASK_SET_ALPHA
: FS_SRC_SET_ALPHA
;
302 if (src_hw_format
== src_pic_format
) {
303 if (src
->tex
->format
== PIPE_FORMAT_L8_UNORM
||
304 src
->tex
->format
== PIPE_FORMAT_R8_UNORM
)
305 return ((mask
) ? FS_MASK_LUMINANCE
: FS_SRC_LUMINANCE
);
310 src_hw_type
= xa_format_type(src_hw_format
);
311 src_pic_type
= xa_format_type(src_pic_format
);
313 swizzle
= ((src_hw_type
== xa_type_argb
&&
314 src_pic_type
== xa_type_abgr
) ||
315 ((src_hw_type
== xa_type_abgr
&&
316 src_pic_type
== xa_type_argb
)));
318 if (!swizzle
&& (src_hw_type
!= src_pic_type
))
322 ret
|= mask
? FS_MASK_SWIZZLE_RGB
: FS_SRC_SWIZZLE_RGB
;
328 bind_shaders(struct xa_context
*ctx
, const struct xa_composite
*comp
)
330 unsigned vs_traits
= 0, fs_traits
= 0;
331 struct xa_shader shader
;
332 struct xa_picture
*src_pic
= comp
->src
;
333 struct xa_picture
*mask_pic
= comp
->mask
;
335 ctx
->has_solid_color
= FALSE
;
338 if (src_pic
->wrap
== xa_wrap_clamp_to_border
&& src_pic
->has_transform
)
339 fs_traits
|= FS_SRC_REPEAT_NONE
;
341 if (src_pic
->src_pict
) {
342 if (src_pic
->src_pict
->type
== xa_src_pict_solid_fill
) {
343 fs_traits
|= FS_SOLID_FILL
| FS_FILL
;
344 vs_traits
|= VS_SOLID_FILL
;
345 xa_pixel_to_float4(src_pic
->src_pict
->solid_fill
.color
,
347 ctx
->has_solid_color
= TRUE
;
350 fs_traits
|= FS_COMPOSITE
;
351 vs_traits
|= VS_COMPOSITE
;
354 fs_traits
|= picture_format_fixups(src_pic
, 0);
358 vs_traits
|= VS_MASK
;
359 fs_traits
|= FS_MASK
;
360 if (mask_pic
->wrap
== xa_wrap_clamp_to_border
&&
361 mask_pic
->has_transform
)
362 fs_traits
|= FS_MASK_REPEAT_NONE
;
364 if (mask_pic
->component_alpha
) {
365 struct xa_composite_blend blend
;
366 if (!blend_for_op(&blend
, comp
->op
, src_pic
, mask_pic
, NULL
))
367 return -XA_ERR_INVAL
;
369 if (blend
.alpha_src
) {
370 fs_traits
|= FS_CA_SRCALPHA
;
372 fs_traits
|= FS_CA_FULL
;
375 fs_traits
|= picture_format_fixups(mask_pic
, 1);
378 if (ctx
->srf
->format
== PIPE_FORMAT_L8_UNORM
||
379 ctx
->srf
->format
== PIPE_FORMAT_R8_UNORM
)
380 fs_traits
|= FS_DST_LUMINANCE
;
382 shader
= xa_shaders_get(ctx
->shaders
, vs_traits
, fs_traits
);
383 cso_set_vertex_shader_handle(ctx
->cso
, shader
.vs
);
384 cso_set_fragment_shader_handle(ctx
->cso
, shader
.fs
);
389 bind_samplers(struct xa_context
*ctx
,
390 const struct xa_composite
*comp
)
392 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
393 struct pipe_sampler_state src_sampler
, mask_sampler
;
394 struct pipe_sampler_view view_templ
;
395 struct pipe_sampler_view
*src_view
;
396 struct pipe_context
*pipe
= ctx
->pipe
;
397 struct xa_picture
*src_pic
= comp
->src
;
398 struct xa_picture
*mask_pic
= comp
->mask
;
400 ctx
->num_bound_samplers
= 0;
402 memset(&src_sampler
, 0, sizeof(struct pipe_sampler_state
));
403 memset(&mask_sampler
, 0, sizeof(struct pipe_sampler_state
));
406 if (ctx
->has_solid_color
) {
408 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[0], NULL
);
410 unsigned src_wrap
= xa_repeat_to_gallium(src_pic
->wrap
);
413 (void) xa_filter_to_gallium(src_pic
->filter
, &filter
);
415 src_sampler
.wrap_s
= src_wrap
;
416 src_sampler
.wrap_t
= src_wrap
;
417 src_sampler
.min_img_filter
= filter
;
418 src_sampler
.mag_img_filter
= filter
;
419 src_sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
420 src_sampler
.normalized_coords
= 1;
421 samplers
[0] = &src_sampler
;
422 ctx
->num_bound_samplers
= 1;
423 u_sampler_view_default_template(&view_templ
,
425 src_pic
->srf
->tex
->format
);
426 src_view
= pipe
->create_sampler_view(pipe
, src_pic
->srf
->tex
,
428 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[0], NULL
);
429 ctx
->bound_sampler_views
[0] = src_view
;
434 unsigned mask_wrap
= xa_repeat_to_gallium(mask_pic
->wrap
);
437 (void) xa_filter_to_gallium(mask_pic
->filter
, &filter
);
439 mask_sampler
.wrap_s
= mask_wrap
;
440 mask_sampler
.wrap_t
= mask_wrap
;
441 mask_sampler
.min_img_filter
= filter
;
442 mask_sampler
.mag_img_filter
= filter
;
443 src_sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
444 mask_sampler
.normalized_coords
= 1;
445 samplers
[1] = &mask_sampler
;
446 ctx
->num_bound_samplers
= 2;
447 u_sampler_view_default_template(&view_templ
,
449 mask_pic
->srf
->tex
->format
);
450 src_view
= pipe
->create_sampler_view(pipe
, mask_pic
->srf
->tex
,
452 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[1], NULL
);
453 ctx
->bound_sampler_views
[1] = src_view
;
457 * If src is a solid color, we have no src view, so set up a
458 * dummy one that will not be used anyway.
460 if (ctx
->bound_sampler_views
[0] == NULL
)
461 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[0],
466 cso_set_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
, ctx
->num_bound_samplers
,
467 (const struct pipe_sampler_state
**)samplers
);
468 cso_set_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
, ctx
->num_bound_samplers
,
469 ctx
->bound_sampler_views
);
473 xa_composite_prepare(struct xa_context
*ctx
,
474 const struct xa_composite
*comp
)
476 struct xa_surface
*dst_srf
= comp
->dst
->srf
;
479 if (comp
->mask
&& !comp
->mask
->srf
)
480 return -XA_ERR_INVAL
;
482 ret
= xa_ctx_srf_create(ctx
, dst_srf
);
483 if (ret
!= XA_ERR_NONE
)
487 renderer_bind_destination(ctx
, ctx
->srf
);
489 ret
= bind_composite_blend_state(ctx
, comp
);
490 if (ret
!= XA_ERR_NONE
)
492 ret
= bind_shaders(ctx
, comp
);
493 if (ret
!= XA_ERR_NONE
)
495 bind_samplers(ctx
, comp
);
497 if (ctx
->num_bound_samplers
== 0 ) { /* solid fill */
498 renderer_begin_solid(ctx
);
500 renderer_begin_textures(ctx
);
504 xa_ctx_srf_destroy(ctx
);
509 xa_composite_rect(struct xa_context
*ctx
,
510 int srcX
, int srcY
, int maskX
, int maskY
,
511 int dstX
, int dstY
, int width
, int height
)
513 if (ctx
->num_bound_samplers
== 0 ) { /* solid fill */
514 renderer_solid(ctx
, dstX
, dstY
, dstX
+ width
, dstY
+ height
,
517 const struct xa_composite
*comp
= ctx
->comp
;
518 int pos
[6] = {srcX
, srcY
, maskX
, maskY
, dstX
, dstY
};
519 const float *src_matrix
= NULL
;
520 const float *mask_matrix
= NULL
;
522 xa_scissor_update(ctx
, dstX
, dstY
, dstX
+ width
, dstY
+ height
);
524 if (comp
->src
->has_transform
)
525 src_matrix
= comp
->src
->transform
;
526 if (comp
->mask
&& comp
->mask
->has_transform
)
527 mask_matrix
= comp
->mask
->transform
;
529 renderer_texture(ctx
, pos
, width
, height
,
530 src_matrix
, mask_matrix
);
535 xa_composite_done(struct xa_context
*ctx
)
537 renderer_draw_flush(ctx
);
540 ctx
->has_solid_color
= FALSE
;
541 xa_ctx_sampler_views_destroy(ctx
);
544 static const struct xa_composite_allocation a
= {
545 .xa_composite_size
= sizeof(struct xa_composite
),
546 .xa_picture_size
= sizeof(struct xa_picture
),
547 .xa_source_pict_size
= sizeof(union xa_source_pict
),
550 XA_EXPORT
const struct xa_composite_allocation
*
551 xa_composite_allocation(void)