st/xorg: solid fills with masks are supported
[mesa.git] / src / gallium / state_trackers / xorg / xorg_composite.c
1 #include "xorg_composite.h"
2
3 #include "xorg_renderer.h"
4 #include "xorg_exa_tgsi.h"
5
6 #include "cso_cache/cso_context.h"
7 #include "util/u_draw_quad.h"
8 #include "util/u_math.h"
9
10 #include "pipe/p_inlines.h"
11
12 struct xorg_composite_blend {
13 int op:8;
14
15 unsigned rgb_src_factor:5; /**< PIPE_BLENDFACTOR_x */
16 unsigned alpha_src_factor:5; /**< PIPE_BLENDFACTOR_x */
17
18 unsigned rgb_dst_factor:5; /**< PIPE_BLENDFACTOR_x */
19 unsigned alpha_dst_factor:5; /**< PIPE_BLENDFACTOR_x */
20 };
21
22 #define BLEND_OP_OVER 3
23 static const struct xorg_composite_blend xorg_blends[] = {
24 { PictOpClear,
25 PIPE_BLENDFACTOR_CONST_COLOR, PIPE_BLENDFACTOR_CONST_ALPHA,
26 PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_ZERO },
27
28 { PictOpSrc,
29 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ONE,
30 PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_ZERO },
31
32 { PictOpDst,
33 PIPE_BLENDFACTOR_ZERO, PIPE_BLENDFACTOR_ZERO,
34 PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ONE },
35
36 { PictOpOver,
37 PIPE_BLENDFACTOR_SRC_ALPHA, PIPE_BLENDFACTOR_ONE,
38 PIPE_BLENDFACTOR_INV_SRC_ALPHA, PIPE_BLENDFACTOR_INV_SRC_ALPHA },
39
40 { PictOpOverReverse,
41 PIPE_BLENDFACTOR_SRC_ALPHA, PIPE_BLENDFACTOR_ONE,
42 PIPE_BLENDFACTOR_INV_SRC_ALPHA, PIPE_BLENDFACTOR_INV_SRC_ALPHA },
43 };
44
45
46 static INLINE void
47 pixel_to_float4(Pixel pixel, float *color)
48 {
49 CARD32 r, g, b, a;
50
51 a = (pixel >> 24) & 0xff;
52 r = (pixel >> 16) & 0xff;
53 g = (pixel >> 8) & 0xff;
54 b = (pixel >> 0) & 0xff;
55 color[0] = ((float)r) / 255.;
56 color[1] = ((float)g) / 255.;
57 color[2] = ((float)b) / 255.;
58 color[3] = ((float)a) / 255.;
59 }
60
61 struct acceleration_info {
62 int op : 16;
63 int with_mask : 1;
64 int component_alpha : 1;
65 };
66 static const struct acceleration_info accelerated_ops[] = {
67 {PictOpClear, 1, 0},
68 {PictOpSrc, 1, 0},
69 {PictOpDst, 1, 0},
70 {PictOpOver, 1, 0},
71 {PictOpOverReverse, 1, 0},
72 {PictOpIn, 1, 0},
73 {PictOpInReverse, 1, 0},
74 {PictOpOut, 1, 0},
75 {PictOpOutReverse, 1, 0},
76 {PictOpAtop, 1, 0},
77 {PictOpAtopReverse, 1, 0},
78 {PictOpXor, 1, 0},
79 {PictOpAdd, 1, 0},
80 {PictOpSaturate, 1, 0},
81 };
82
83 static struct xorg_composite_blend
84 blend_for_op(int op)
85 {
86 const int num_blends =
87 sizeof(xorg_blends)/sizeof(struct xorg_composite_blend);
88 int i;
89
90 for (i = 0; i < num_blends; ++i) {
91 if (xorg_blends[i].op == op)
92 return xorg_blends[i];
93 }
94 return xorg_blends[BLEND_OP_OVER];
95 }
96
97 static INLINE int
98 render_repeat_to_gallium(int mode)
99 {
100 switch(mode) {
101 case RepeatNone:
102 return PIPE_TEX_WRAP_CLAMP;
103 case RepeatNormal:
104 return PIPE_TEX_WRAP_REPEAT;
105 case RepeatReflect:
106 return PIPE_TEX_WRAP_MIRROR_REPEAT;
107 case RepeatPad:
108 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
109 default:
110 debug_printf("Unsupported repeat mode\n");
111 }
112 return PIPE_TEX_WRAP_REPEAT;
113 }
114
115 boolean xorg_composite_accelerated(int op,
116 PicturePtr pSrcPicture,
117 PicturePtr pMaskPicture,
118 PicturePtr pDstPicture)
119 {
120 ScreenPtr pScreen = pDstPicture->pDrawable->pScreen;
121 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
122 modesettingPtr ms = modesettingPTR(pScrn);
123 unsigned i;
124 unsigned accel_ops_count =
125 sizeof(accelerated_ops)/sizeof(struct acceleration_info);
126
127 if (pSrcPicture->pSourcePict) {
128 if (pSrcPicture->pSourcePict->type != SourcePictTypeSolidFill)
129 XORG_FALLBACK("gradients not enabled (haven't been well tested)");
130 }
131
132 for (i = 0; i < accel_ops_count; ++i) {
133 if (op == accelerated_ops[i].op) {
134 /* Check for unsupported component alpha */
135 if ((pSrcPicture->componentAlpha &&
136 !accelerated_ops[i].component_alpha) ||
137 (pMaskPicture &&
138 (!accelerated_ops[i].with_mask ||
139 (pMaskPicture->componentAlpha &&
140 !accelerated_ops[i].component_alpha))))
141 XORG_FALLBACK("component alpha unsupported");
142 return TRUE;
143 }
144 }
145 XORG_FALLBACK("unsupported operation");
146 }
147
148 static void
149 bind_blend_state(struct exa_context *exa, int op,
150 PicturePtr pSrcPicture, PicturePtr pMaskPicture)
151 {
152 struct xorg_composite_blend blend_opt;
153 struct pipe_blend_state blend;
154
155 blend_opt = blend_for_op(op);
156
157 memset(&blend, 0, sizeof(struct pipe_blend_state));
158 blend.blend_enable = 1;
159 blend.colormask |= PIPE_MASK_R;
160 blend.colormask |= PIPE_MASK_G;
161 blend.colormask |= PIPE_MASK_B;
162 blend.colormask |= PIPE_MASK_A;
163
164 blend.rgb_src_factor = blend_opt.rgb_src_factor;
165 blend.alpha_src_factor = blend_opt.alpha_src_factor;
166 blend.rgb_dst_factor = blend_opt.rgb_dst_factor;
167 blend.alpha_dst_factor = blend_opt.alpha_dst_factor;
168
169 cso_set_blend(exa->renderer->cso, &blend);
170 }
171
172
173 static void
174 bind_shaders(struct exa_context *exa, int op,
175 PicturePtr pSrcPicture, PicturePtr pMaskPicture)
176 {
177 unsigned vs_traits = 0, fs_traits = 0;
178 struct xorg_shader shader;
179
180 exa->has_solid_color = FALSE;
181
182 if (pSrcPicture) {
183 if (pSrcPicture->pSourcePict) {
184 if (pSrcPicture->pSourcePict->type == SourcePictTypeSolidFill) {
185 fs_traits |= FS_SOLID_FILL;
186 vs_traits |= VS_SOLID_FILL;
187 debug_assert(pSrcPicture->format == PICT_a8r8g8b8);
188 pixel_to_float4(pSrcPicture->pSourcePict->solidFill.color,
189 exa->solid_color);
190 exa->has_solid_color = TRUE;
191 } else {
192 debug_assert("!gradients not supported");
193 }
194 } else {
195 fs_traits |= FS_COMPOSITE;
196 vs_traits |= VS_COMPOSITE;
197 }
198 }
199
200 if (pMaskPicture) {
201 vs_traits |= VS_MASK;
202 fs_traits |= FS_MASK;
203 }
204
205 shader = xorg_shaders_get(exa->renderer->shaders, vs_traits, fs_traits);
206 cso_set_vertex_shader_handle(exa->renderer->cso, shader.vs);
207 cso_set_fragment_shader_handle(exa->renderer->cso, shader.fs);
208 }
209
210
211 static void
212 bind_samplers(struct exa_context *exa, int op,
213 PicturePtr pSrcPicture, PicturePtr pMaskPicture,
214 PicturePtr pDstPicture,
215 struct exa_pixmap_priv *pSrc,
216 struct exa_pixmap_priv *pMask,
217 struct exa_pixmap_priv *pDst)
218 {
219 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
220 struct pipe_sampler_state src_sampler, mask_sampler;
221
222 exa->num_bound_samplers = 0;
223
224 memset(&src_sampler, 0, sizeof(struct pipe_sampler_state));
225 memset(&mask_sampler, 0, sizeof(struct pipe_sampler_state));
226
227 if ((pSrc && exa->pipe->is_texture_referenced(exa->pipe, pSrc->tex, 0, 0) &
228 PIPE_REFERENCED_FOR_WRITE) ||
229 (pMask && exa->pipe->is_texture_referenced(exa->pipe, pMask->tex, 0, 0) &
230 PIPE_REFERENCED_FOR_WRITE))
231 exa->pipe->flush(exa->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
232
233 if (pSrcPicture && pSrc) {
234 unsigned src_wrap = render_repeat_to_gallium(
235 pSrcPicture->repeatType);
236 src_sampler.wrap_s = src_wrap;
237 src_sampler.wrap_t = src_wrap;
238 src_sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
239 src_sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
240 src_sampler.normalized_coords = 1;
241 samplers[0] = &src_sampler;
242 exa->bound_textures[0] = pSrc->tex;
243 ++exa->num_bound_samplers;
244 }
245
246 if (pMaskPicture && pMask) {
247 unsigned mask_wrap = render_repeat_to_gallium(
248 pMaskPicture->repeatType);
249 mask_sampler.wrap_s = mask_wrap;
250 mask_sampler.wrap_t = mask_wrap;
251 mask_sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
252 mask_sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
253 mask_sampler.normalized_coords = 1;
254 samplers[1] = &mask_sampler;
255 exa->bound_textures[1] = pMask->tex;
256 ++exa->num_bound_samplers;
257 }
258
259 cso_set_samplers(exa->renderer->cso, exa->num_bound_samplers,
260 (const struct pipe_sampler_state **)samplers);
261 cso_set_sampler_textures(exa->renderer->cso, exa->num_bound_samplers,
262 exa->bound_textures);
263 }
264
265 static void
266 setup_vs_constant_buffer(struct exa_context *exa,
267 int width, int height)
268 {
269 const int param_bytes = 8 * sizeof(float);
270 float vs_consts[8] = {
271 2.f/width, 2.f/height, 1, 1,
272 -1, -1, 0, 0
273 };
274 renderer_set_constants(exa->renderer, PIPE_SHADER_VERTEX,
275 vs_consts, param_bytes);
276 }
277
278
279 static void
280 setup_fs_constant_buffer(struct exa_context *exa)
281 {
282 const int param_bytes = 4 * sizeof(float);
283 const float fs_consts[8] = {
284 0, 0, 0, 1,
285 };
286 renderer_set_constants(exa->renderer, PIPE_SHADER_FRAGMENT,
287 fs_consts, param_bytes);
288 }
289
290 static void
291 setup_constant_buffers(struct exa_context *exa, struct exa_pixmap_priv *pDst)
292 {
293 int width = pDst->tex->width[0];
294 int height = pDst->tex->height[0];
295
296 setup_vs_constant_buffer(exa, width, height);
297 setup_fs_constant_buffer(exa);
298 }
299
300 boolean xorg_composite_bind_state(struct exa_context *exa,
301 int op,
302 PicturePtr pSrcPicture,
303 PicturePtr pMaskPicture,
304 PicturePtr pDstPicture,
305 struct exa_pixmap_priv *pSrc,
306 struct exa_pixmap_priv *pMask,
307 struct exa_pixmap_priv *pDst)
308 {
309 renderer_bind_framebuffer(exa->renderer, pDst);
310 renderer_bind_viewport(exa->renderer, pDst);
311 bind_blend_state(exa, op, pSrcPicture, pMaskPicture);
312 renderer_bind_rasterizer(exa->renderer);
313 bind_shaders(exa, op, pSrcPicture, pMaskPicture);
314 bind_samplers(exa, op, pSrcPicture, pMaskPicture,
315 pDstPicture, pSrc, pMask, pDst);
316 setup_constant_buffers(exa, pDst);
317
318 return TRUE;
319 }
320
321 void xorg_composite(struct exa_context *exa,
322 struct exa_pixmap_priv *dst,
323 int srcX, int srcY, int maskX, int maskY,
324 int dstX, int dstY, int width, int height)
325 {
326 if (exa->num_bound_samplers == 0 ) { /* solid fill */
327 renderer_draw_solid_rect(exa->renderer,
328 dstX, dstY, dstX + width, dstY + height,
329 exa->solid_color);
330 } else {
331 int pos[6] = {srcX, srcY, maskX, maskY, dstX, dstY};
332 renderer_draw_textures(exa->renderer,
333 pos, width, height,
334 exa->bound_textures,
335 exa->num_bound_samplers);
336 }
337 }
338
339 boolean xorg_solid_bind_state(struct exa_context *exa,
340 struct exa_pixmap_priv *pixmap,
341 Pixel fg)
342 {
343 unsigned vs_traits, fs_traits;
344 struct xorg_shader shader;
345
346 pixel_to_float4(fg, exa->solid_color);
347 exa->has_solid_color = TRUE;
348
349 exa->solid_color[3] = 1.f;
350
351 #if 0
352 debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
353 (fg >> 24) & 0xff, (fg >> 16) & 0xff,
354 (fg >> 8) & 0xff, (fg >> 0) & 0xff,
355 exa->solid_color[0], exa->solid_color[1],
356 exa->solid_color[2], exa->solid_color[3]);
357 #endif
358
359 vs_traits = VS_SOLID_FILL;
360 fs_traits = FS_SOLID_FILL;
361
362 renderer_bind_framebuffer(exa->renderer, pixmap);
363 renderer_bind_viewport(exa->renderer, pixmap);
364 renderer_bind_rasterizer(exa->renderer);
365 bind_blend_state(exa, PictOpSrc, NULL, NULL);
366 setup_constant_buffers(exa, pixmap);
367
368 shader = xorg_shaders_get(exa->renderer->shaders, vs_traits, fs_traits);
369 cso_set_vertex_shader_handle(exa->renderer->cso, shader.vs);
370 cso_set_fragment_shader_handle(exa->renderer->cso, shader.fs);
371
372 return TRUE;
373 }
374
375 void xorg_solid(struct exa_context *exa,
376 struct exa_pixmap_priv *pixmap,
377 int x0, int y0, int x1, int y1)
378 {
379 renderer_draw_solid_rect(exa->renderer,
380 x0, y0, x1, y1, exa->solid_color);
381 }
382