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
{
42 enum xa_composite_op op
: 8;
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
},
83 * The alpha value stored in a luminance texture is read by the
87 xa_convert_blend_for_luminance(unsigned factor
)
90 case PIPE_BLENDFACTOR_DST_ALPHA
:
91 return PIPE_BLENDFACTOR_DST_COLOR
;
92 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
93 return PIPE_BLENDFACTOR_INV_DST_COLOR
;
102 blend_for_op(struct xa_composite_blend
*blend
,
103 enum xa_composite_op op
,
104 struct xa_picture
*src_pic
,
105 struct xa_picture
*mask_pic
,
106 struct xa_picture
*dst_pic
)
108 const int num_blends
=
109 sizeof(xa_blends
)/sizeof(struct xa_composite_blend
);
111 boolean supported
= FALSE
;
114 * Temporarily disable component alpha since it appears buggy.
116 if (src_pic
->component_alpha
||
117 (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 blend
->rgb_src
= xa_convert_blend_for_luminance(blend
->rgb_src
);
137 blend
->rgb_dst
= xa_convert_blend_for_luminance(blend
->rgb_dst
);
141 * If there's no dst alpha channel, adjust the blend op so that we'll treat
145 if (xa_format_a(dst_pic
->pict_format
) == 0 && blend
->alpha_dst
) {
146 if (blend
->rgb_src
== PIPE_BLENDFACTOR_DST_ALPHA
)
147 blend
->rgb_src
= PIPE_BLENDFACTOR_ONE
;
148 else if (blend
->rgb_src
== PIPE_BLENDFACTOR_INV_DST_ALPHA
)
149 blend
->rgb_src
= PIPE_BLENDFACTOR_ZERO
;
153 * If the source alpha is being used, then we should only be in a case where
154 * the source blend factor is 0, and the source blend value is the mask
155 * channels multiplied by the source picture's alpha.
157 if (mask_pic
&& mask_pic
->component_alpha
&&
158 xa_format_rgb(mask_pic
->pict_format
) &&
160 if (blend
->rgb_dst
== PIPE_BLENDFACTOR_SRC_ALPHA
) {
161 blend
->rgb_dst
= PIPE_BLENDFACTOR_SRC_COLOR
;
162 } else if (blend
->rgb_dst
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
) {
163 blend
->rgb_dst
= PIPE_BLENDFACTOR_INV_SRC_COLOR
;
172 xa_repeat_to_gallium(int mode
)
175 case xa_wrap_clamp_to_border
:
176 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
178 return PIPE_TEX_WRAP_REPEAT
;
179 case xa_wrap_mirror_repeat
:
180 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
181 case xa_wrap_clamp_to_edge
:
182 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
186 return PIPE_TEX_WRAP_REPEAT
;
189 static INLINE boolean
190 xa_filter_to_gallium(int xrender_filter
, int *out_filter
)
193 switch (xrender_filter
) {
194 case xa_filter_nearest
:
195 *out_filter
= PIPE_TEX_FILTER_NEAREST
;
197 case xa_filter_linear
:
198 *out_filter
= PIPE_TEX_FILTER_LINEAR
;
201 *out_filter
= PIPE_TEX_FILTER_NEAREST
;
208 xa_is_filter_accelerated(struct xa_picture
*pic
)
211 if (pic
&& !xa_filter_to_gallium(pic
->filter
, &filter
))
217 xa_composite_check_accelerated(const struct xa_composite
*comp
)
219 struct xa_composite_blend blend
;
220 struct xa_picture
*src_pic
= comp
->src
;
222 if (!xa_is_filter_accelerated(src_pic
) ||
223 !xa_is_filter_accelerated(comp
->mask
)) {
224 return -XA_ERR_INVAL
;
228 if (src_pic
->src_pict
) {
229 if (src_pic
->src_pict
->type
!= xa_src_pict_solid_fill
)
230 return -XA_ERR_INVAL
;
233 if (blend_for_op(&blend
, comp
->op
, comp
->src
, comp
->mask
, comp
->dst
)) {
234 struct xa_picture
*mask
= comp
->mask
;
235 if (mask
&& mask
->component_alpha
&&
236 xa_format_rgb(mask
->pict_format
)) {
237 if (blend
.alpha_src
&& blend
.rgb_src
!= PIPE_BLENDFACTOR_ZERO
) {
238 return -XA_ERR_INVAL
;
244 return -XA_ERR_INVAL
;
248 bind_composite_blend_state(struct xa_context
*ctx
,
249 const struct xa_composite
*comp
)
251 struct xa_composite_blend blend_opt
;
252 struct pipe_blend_state blend
;
254 if (!blend_for_op(&blend_opt
, comp
->op
, comp
->src
, comp
->mask
, comp
->dst
))
255 return -XA_ERR_INVAL
;
257 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
258 blend
.rt
[0].blend_enable
= 1;
259 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
261 blend
.rt
[0].rgb_src_factor
= blend_opt
.rgb_src
;
262 blend
.rt
[0].alpha_src_factor
= blend_opt
.rgb_src
;
263 blend
.rt
[0].rgb_dst_factor
= blend_opt
.rgb_dst
;
264 blend
.rt
[0].alpha_dst_factor
= blend_opt
.rgb_dst
;
266 cso_set_blend(ctx
->cso
, &blend
);
271 picture_format_fixups(struct xa_picture
*src_pic
,
274 boolean set_alpha
= FALSE
;
275 boolean swizzle
= FALSE
;
277 struct xa_surface
*src
= src_pic
->srf
;
278 enum xa_formats src_hw_format
, src_pic_format
;
279 enum xa_surface_type src_hw_type
, src_pic_type
;
284 src_hw_format
= xa_surface_format(src
);
285 src_pic_format
= src_pic
->pict_format
;
287 set_alpha
= (xa_format_type_is_color(src_pic_format
) &&
288 xa_format_a(src_pic_format
) == 0);
291 ret
|= mask
? FS_MASK_SET_ALPHA
: FS_SRC_SET_ALPHA
;
293 if (src_hw_format
== src_pic_format
) {
294 if (src
->tex
->format
== PIPE_FORMAT_L8_UNORM
)
295 return ((mask
) ? FS_MASK_LUMINANCE
: FS_SRC_LUMINANCE
);
300 src_hw_type
= xa_format_type(src_hw_format
);
301 src_pic_type
= xa_format_type(src_pic_format
);
303 swizzle
= ((src_hw_type
== xa_type_argb
&&
304 src_pic_type
== xa_type_abgr
) ||
305 ((src_hw_type
== xa_type_abgr
&&
306 src_pic_type
== xa_type_argb
)));
308 if (!swizzle
&& (src_hw_type
!= src_pic_type
))
312 ret
|= mask
? FS_MASK_SWIZZLE_RGB
: FS_SRC_SWIZZLE_RGB
;
318 bind_shaders(struct xa_context
*ctx
, const struct xa_composite
*comp
)
320 unsigned vs_traits
= 0, fs_traits
= 0;
321 struct xa_shader shader
;
322 struct xa_picture
*src_pic
= comp
->src
;
323 struct xa_picture
*mask_pic
= comp
->mask
;
325 ctx
->has_solid_color
= FALSE
;
328 if (src_pic
->wrap
== xa_wrap_clamp_to_border
&& src_pic
->has_transform
)
329 fs_traits
|= FS_SRC_REPEAT_NONE
;
331 if (src_pic
->src_pict
) {
332 if (src_pic
->src_pict
->type
== xa_src_pict_solid_fill
) {
333 fs_traits
|= FS_SOLID_FILL
;
334 vs_traits
|= VS_SOLID_FILL
;
335 xa_pixel_to_float4(src_pic
->src_pict
->solid_fill
.color
,
337 ctx
->has_solid_color
= TRUE
;
340 fs_traits
|= FS_COMPOSITE
;
341 vs_traits
|= VS_COMPOSITE
;
344 fs_traits
|= picture_format_fixups(src_pic
, 0);
348 vs_traits
|= VS_MASK
;
349 fs_traits
|= FS_MASK
;
350 if (mask_pic
->wrap
== xa_wrap_clamp_to_border
&&
351 mask_pic
->has_transform
)
352 fs_traits
|= FS_MASK_REPEAT_NONE
;
354 if (mask_pic
->component_alpha
) {
355 struct xa_composite_blend blend
;
356 if (!blend_for_op(&blend
, comp
->op
, src_pic
, mask_pic
, NULL
))
357 return -XA_ERR_INVAL
;
359 if (blend
.alpha_src
) {
360 fs_traits
|= FS_CA_SRCALPHA
;
362 fs_traits
|= FS_CA_FULL
;
365 fs_traits
|= picture_format_fixups(mask_pic
, 1);
368 if (ctx
->srf
->format
== PIPE_FORMAT_L8_UNORM
)
369 fs_traits
|= FS_DST_LUMINANCE
;
371 shader
= xa_shaders_get(ctx
->shaders
, vs_traits
, fs_traits
);
372 cso_set_vertex_shader_handle(ctx
->cso
, shader
.vs
);
373 cso_set_fragment_shader_handle(ctx
->cso
, shader
.fs
);
378 bind_samplers(struct xa_context
*ctx
,
379 const struct xa_composite
*comp
)
381 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
382 struct pipe_sampler_state src_sampler
, mask_sampler
;
383 struct pipe_sampler_view view_templ
;
384 struct pipe_sampler_view
*src_view
;
385 struct pipe_context
*pipe
= ctx
->pipe
;
386 struct xa_picture
*src_pic
= comp
->src
;
387 struct xa_picture
*mask_pic
= comp
->mask
;
389 ctx
->num_bound_samplers
= 0;
391 memset(&src_sampler
, 0, sizeof(struct pipe_sampler_state
));
392 memset(&mask_sampler
, 0, sizeof(struct pipe_sampler_state
));
395 if (ctx
->has_solid_color
) {
397 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[0], NULL
);
399 unsigned src_wrap
= xa_repeat_to_gallium(src_pic
->wrap
);
402 (void) xa_filter_to_gallium(src_pic
->filter
, &filter
);
404 src_sampler
.wrap_s
= src_wrap
;
405 src_sampler
.wrap_t
= src_wrap
;
406 src_sampler
.min_img_filter
= filter
;
407 src_sampler
.mag_img_filter
= filter
;
408 src_sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
409 src_sampler
.normalized_coords
= 1;
410 samplers
[0] = &src_sampler
;
411 ctx
->num_bound_samplers
= 1;
412 u_sampler_view_default_template(&view_templ
,
414 src_pic
->srf
->tex
->format
);
415 src_view
= pipe
->create_sampler_view(pipe
, src_pic
->srf
->tex
,
417 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[0], NULL
);
418 ctx
->bound_sampler_views
[0] = src_view
;
423 unsigned mask_wrap
= xa_repeat_to_gallium(mask_pic
->wrap
);
426 (void) xa_filter_to_gallium(mask_pic
->filter
, &filter
);
428 mask_sampler
.wrap_s
= mask_wrap
;
429 mask_sampler
.wrap_t
= mask_wrap
;
430 mask_sampler
.min_img_filter
= filter
;
431 mask_sampler
.mag_img_filter
= filter
;
432 src_sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
433 mask_sampler
.normalized_coords
= 1;
434 samplers
[1] = &mask_sampler
;
435 ctx
->num_bound_samplers
= 2;
436 u_sampler_view_default_template(&view_templ
,
438 mask_pic
->srf
->tex
->format
);
439 src_view
= pipe
->create_sampler_view(pipe
, mask_pic
->srf
->tex
,
441 pipe_sampler_view_reference(&ctx
->bound_sampler_views
[1], NULL
);
442 ctx
->bound_sampler_views
[1] = src_view
;
445 cso_set_samplers(ctx
->cso
, ctx
->num_bound_samplers
,
446 (const struct pipe_sampler_state
**)samplers
);
447 cso_set_fragment_sampler_views(ctx
->cso
, ctx
->num_bound_samplers
,
448 ctx
->bound_sampler_views
);
452 xa_composite_prepare(struct xa_context
*ctx
,
453 const struct xa_composite
*comp
)
455 struct xa_surface
*dst_srf
= comp
->dst
->srf
;
458 ret
= xa_ctx_srf_create(ctx
, dst_srf
);
459 if (ret
!= XA_ERR_NONE
)
463 renderer_bind_destination(ctx
, ctx
->srf
, ctx
->srf
->width
,
466 ret
= bind_composite_blend_state(ctx
, comp
);
467 if (ret
!= XA_ERR_NONE
)
469 ret
= bind_shaders(ctx
, comp
);
470 if (ret
!= XA_ERR_NONE
)
472 bind_samplers(ctx
, comp
);
474 if (ctx
->num_bound_samplers
== 0 ) { /* solid fill */
475 renderer_begin_solid(ctx
);
477 renderer_begin_textures(ctx
);
481 xa_ctx_srf_destroy(ctx
);
485 void xa_composite_rect(struct xa_context
*ctx
,
486 int srcX
, int srcY
, int maskX
, int maskY
,
487 int dstX
, int dstY
, int width
, int height
)
489 if (ctx
->num_bound_samplers
== 0 ) { /* solid fill */
490 renderer_solid(ctx
, dstX
, dstY
, dstX
+ width
, dstY
+ height
,
493 const struct xa_composite
*comp
= ctx
->comp
;
494 int pos
[6] = {srcX
, srcY
, maskX
, maskY
, dstX
, dstY
};
495 const float *src_matrix
= NULL
;
496 const float *mask_matrix
= NULL
;
498 if (comp
->src
->has_transform
)
499 src_matrix
= comp
->src
->transform
;
500 if (comp
->mask
&& comp
->mask
->has_transform
)
501 mask_matrix
= comp
->mask
->transform
;
503 renderer_texture(ctx
, pos
, width
, height
,
504 src_matrix
, mask_matrix
);
509 xa_composite_done(struct xa_context
*ctx
)
511 renderer_draw_flush(ctx
);
512 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
);
515 ctx
->has_solid_color
= FALSE
;
516 xa_ctx_sampler_views_destroy(ctx
);
519 static const struct xa_composite_allocation a
= {
520 .xa_composite_size
= sizeof(struct xa_composite
),
521 .xa_picture_size
= sizeof(struct xa_picture
),
522 .xa_source_pict_size
= sizeof(union xa_source_pict
),
525 const struct xa_composite_allocation
*
526 xa_composite_allocation(void)