st/xorg: implement basic src/mask transformations
[mesa.git] / src / gallium / state_trackers / xorg / xorg_renderer.c
1 #include "xorg_exa.h"
2 #include "xorg_renderer.h"
3
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 #include "util/u_memory.h"
10 #include "util/u_rect.h"
11
12 #include "pipe/p_inlines.h"
13
14 #include <math.h>
15
16 enum AxisOrientation {
17 Y0_BOTTOM,
18 Y0_TOP
19 };
20
21 #define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
22 #define floatIsZero(x) (floatsEqual((x) + 1, 1))
23
24 static INLINE boolean is_affine(float *matrix)
25 {
26 return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
27 && floatsEqual(matrix[8], 1);
28 }
29 static INLINE void map_point(float *mat, float x, float y,
30 float *out_x, float *out_y)
31 {
32 if (!mat) {
33 *out_x = x;
34 *out_y = y;
35 return;
36 }
37
38 *out_x = mat[0]*x + mat[3]*y + mat[6];
39 *out_y = mat[1]*x + mat[4]*y + mat[7];
40 if (!is_affine(mat)) {
41 float w = 1/(mat[2]*x + mat[5]*y + mat[8]);
42 *out_x *= w;
43 *out_y *= w;
44 }
45 }
46
47 static void
48 renderer_init_state(struct xorg_renderer *r)
49 {
50 struct pipe_depth_stencil_alpha_state dsa;
51
52 /* set common initial clip state */
53 memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
54 cso_set_depth_stencil_alpha(r->cso, &dsa);
55 }
56
57
58 static INLINE void
59 setup_vertex0(float vertex[2][4], float x, float y,
60 float color[4])
61 {
62 vertex[0][0] = x;
63 vertex[0][1] = y;
64 vertex[0][2] = 0.f; /*z*/
65 vertex[0][3] = 1.f; /*w*/
66
67 vertex[1][0] = color[0]; /*r*/
68 vertex[1][1] = color[1]; /*g*/
69 vertex[1][2] = color[2]; /*b*/
70 vertex[1][3] = color[3]; /*a*/
71 }
72
73 static INLINE void
74 setup_vertex1(float vertex[2][4], float x, float y, float s, float t)
75 {
76 vertex[0][0] = x;
77 vertex[0][1] = y;
78 vertex[0][2] = 0.f; /*z*/
79 vertex[0][3] = 1.f; /*w*/
80
81 vertex[1][0] = s; /*s*/
82 vertex[1][1] = t; /*t*/
83 vertex[1][2] = 0.f; /*r*/
84 vertex[1][3] = 1.f; /*q*/
85 }
86
87 static struct pipe_buffer *
88 setup_vertex_data1(struct xorg_renderer *r,
89 int srcX, int srcY, int dstX, int dstY,
90 int width, int height,
91 struct pipe_texture *src, float *src_matrix)
92 {
93 float s0, t0, s1, t1, stmp, ttmp;
94
95 s0 = srcX / src->width[0];
96 s1 = srcX + width / src->width[0];
97 t0 = srcY / src->height[0];
98 t1 = srcY + height / src->height[0];
99
100 if (src_matrix) {
101 /* 1st vertex */
102 map_point(src_matrix, s0, t0, &stmp, &ttmp);
103 setup_vertex1(r->vertices2[0], dstX, dstY, stmp, ttmp);
104 /* 2nd vertex */
105 map_point(src_matrix, s1, t0, &stmp, &ttmp);
106 setup_vertex1(r->vertices2[1], dstX + width, dstY, stmp, ttmp);
107 /* 3rd vertex */
108 map_point(src_matrix, s1, t1, &stmp, &ttmp);
109 setup_vertex1(r->vertices2[2], dstX + width, dstY + height, stmp, ttmp);
110 /* 4th vertex */
111 map_point(src_matrix, s0, t1, &stmp, &ttmp);
112 setup_vertex1(r->vertices2[3], dstX, dstY + height, stmp, ttmp);
113 } else {
114 /* 1st vertex */
115 setup_vertex1(r->vertices2[0], dstX, dstY, s0, t0);
116 /* 2nd vertex */
117 setup_vertex1(r->vertices2[1], dstX + width, dstY, s1, t0);
118 /* 3rd vertex */
119 setup_vertex1(r->vertices2[2], dstX + width, dstY + height, s1, t1);
120 /* 4th vertex */
121 setup_vertex1(r->vertices2[3], dstX, dstY + height, s0, t1);
122 }
123
124 return pipe_user_buffer_create(r->pipe->screen,
125 r->vertices2,
126 sizeof(r->vertices2));
127 }
128
129 static struct pipe_buffer *
130 setup_vertex_data_tex(struct xorg_renderer *r,
131 float x0, float y0, float x1, float y1,
132 float s0, float t0, float s1, float t1,
133 float z)
134 {
135 /* 1st vertex */
136 setup_vertex1(r->vertices2[0], x0, y0, s0, t0);
137 /* 2nd vertex */
138 setup_vertex1(r->vertices2[1], x1, y0, s1, t0);
139 /* 3rd vertex */
140 setup_vertex1(r->vertices2[2], x1, y1, s1, t1);
141 /* 4th vertex */
142 setup_vertex1(r->vertices2[3], x0, y1, s0, t1);
143
144 return pipe_user_buffer_create(r->pipe->screen,
145 r->vertices2,
146 sizeof(r->vertices2));
147 }
148
149 static INLINE void
150 setup_vertex2(float vertex[3][4], float x, float y,
151 float s0, float t0, float s1, float t1)
152 {
153 vertex[0][0] = x;
154 vertex[0][1] = y;
155 vertex[0][2] = 0.f; /*z*/
156 vertex[0][3] = 1.f; /*w*/
157
158 vertex[1][0] = s0; /*s*/
159 vertex[1][1] = t0; /*t*/
160 vertex[1][2] = 0.f; /*r*/
161 vertex[1][3] = 1.f; /*q*/
162
163 vertex[2][0] = s1; /*s*/
164 vertex[2][1] = t1; /*t*/
165 vertex[2][2] = 0.f; /*r*/
166 vertex[2][3] = 1.f; /*q*/
167 }
168
169 static struct pipe_buffer *
170 setup_vertex_data2(struct xorg_renderer *r,
171 int srcX, int srcY, int maskX, int maskY,
172 int dstX, int dstY, int width, int height,
173 struct pipe_texture *src,
174 struct pipe_texture *mask,
175 float *src_matrix, float *mask_matrix)
176 {
177 float st0[4], st1[4];
178 float pt0[2], pt1[2];
179
180 st0[0] = srcX / src->width[0];
181 st0[1] = srcY / src->height[0];
182 st0[2] = srcX + width / src->width[0];
183 st0[3] = srcY + height / src->height[0];
184
185 st1[0] = maskX / mask->width[0];
186 st1[1] = maskY / mask->height[0];
187 st1[2] = maskX + width / mask->width[0];
188 st1[3] = maskY + height / mask->height[0];
189
190 if (src_matrix || mask_matrix) {
191 /* 1st vertex */
192 map_point(src_matrix, st0[0], st0[1],
193 pt0 + 0, pt0 + 1);
194 map_point(mask_matrix, st1[0], st1[1],
195 pt1 + 0, pt1 + 1);
196 setup_vertex2(r->vertices3[0], dstX, dstY,
197 pt0[0], pt0[1], pt1[0], pt1[1]);
198 /* 2nd vertex */
199 map_point(src_matrix, st0[2], st0[1],
200 pt0 + 0, pt0 + 1);
201 map_point(mask_matrix, st1[2], st1[1],
202 pt1 + 0, pt1 + 1);
203 setup_vertex2(r->vertices3[1], dstX + width, dstY,
204 pt0[0], pt0[1], pt1[0], pt1[1]);
205 /* 3rd vertex */
206 map_point(src_matrix, st0[2], st0[3],
207 pt0 + 0, pt0 + 1);
208 map_point(mask_matrix, st1[2], st1[3],
209 pt1 + 0, pt1 + 1);
210 setup_vertex2(r->vertices3[2], dstX + width, dstY + height,
211 pt0[0], pt0[1], pt1[0], pt1[1]);
212 /* 4th vertex */
213 map_point(src_matrix, st0[0], st0[3],
214 pt0 + 0, pt0 + 1);
215 map_point(mask_matrix, st1[0], st1[3],
216 pt1 + 0, pt1 + 1);
217 setup_vertex2(r->vertices3[3], dstX, dstY + height,
218 pt0[0], pt0[1], pt1[0], pt1[1]);
219 } else {
220 /* 1st vertex */
221 setup_vertex2(r->vertices3[0], dstX, dstY,
222 st0[0], st0[1], st1[0], st1[1]);
223 /* 2nd vertex */
224 setup_vertex2(r->vertices3[1], dstX + width, dstY,
225 st0[2], st0[1], st1[2], st1[1]);
226 /* 3rd vertex */
227 setup_vertex2(r->vertices3[2], dstX + width, dstY + height,
228 st0[2], st0[3], st1[2], st1[3]);
229 /* 4th vertex */
230 setup_vertex2(r->vertices3[3], dstX, dstY + height,
231 st0[0], st0[3], st1[0], st1[3]);
232 }
233
234 return pipe_user_buffer_create(r->pipe->screen,
235 r->vertices3,
236 sizeof(r->vertices3));
237 }
238
239
240
241 static void
242 set_viewport(struct xorg_renderer *r, int width, int height,
243 enum AxisOrientation orientation)
244 {
245 struct pipe_viewport_state viewport;
246 float y_scale = (orientation == Y0_BOTTOM) ? -2.f : 2.f;
247
248 viewport.scale[0] = width / 2.f;
249 viewport.scale[1] = height / y_scale;
250 viewport.scale[2] = 1.0;
251 viewport.scale[3] = 1.0;
252 viewport.translate[0] = width / 2.f;
253 viewport.translate[1] = height / 2.f;
254 viewport.translate[2] = 0.0;
255 viewport.translate[3] = 0.0;
256
257 cso_set_viewport(r->cso, &viewport);
258 }
259
260
261
262 struct xorg_renderer * renderer_create(struct pipe_context *pipe)
263 {
264 struct xorg_renderer *renderer = CALLOC_STRUCT(xorg_renderer);
265
266 renderer->pipe = pipe;
267 renderer->cso = cso_create_context(pipe);
268 renderer->shaders = xorg_shaders_create(renderer);
269
270 renderer_init_state(renderer);
271
272 return renderer;
273 }
274
275 void renderer_destroy(struct xorg_renderer *r)
276 {
277 struct pipe_constant_buffer *vsbuf = &r->vs_const_buffer;
278 struct pipe_constant_buffer *fsbuf = &r->fs_const_buffer;
279
280 if (vsbuf && vsbuf->buffer)
281 pipe_buffer_reference(&vsbuf->buffer, NULL);
282
283 if (fsbuf && fsbuf->buffer)
284 pipe_buffer_reference(&fsbuf->buffer, NULL);
285
286 if (r->cso) {
287 cso_release_all(r->cso);
288 cso_destroy_context(r->cso);
289 }
290
291 if (r->shaders) {
292 xorg_shaders_destroy(r->shaders);
293 }
294 }
295
296 void renderer_bind_framebuffer(struct xorg_renderer *r,
297 struct exa_pixmap_priv *priv)
298 {
299 unsigned i;
300 struct pipe_framebuffer_state state;
301 struct pipe_surface *surface = xorg_gpu_surface(r->pipe->screen, priv);
302 memset(&state, 0, sizeof(struct pipe_framebuffer_state));
303
304 state.width = priv->tex->width[0];
305 state.height = priv->tex->height[0];
306
307 state.nr_cbufs = 1;
308 state.cbufs[0] = surface;
309 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
310 state.cbufs[i] = 0;
311
312 /* currently we don't use depth/stencil */
313 state.zsbuf = 0;
314
315 cso_set_framebuffer(r->cso, &state);
316
317 /* we do fire and forget for the framebuffer, this is the forget part */
318 pipe_surface_reference(&surface, NULL);
319 }
320
321 void renderer_bind_viewport(struct xorg_renderer *r,
322 struct exa_pixmap_priv *dst)
323 {
324 int width = dst->tex->width[0];
325 int height = dst->tex->height[0];
326
327 /*debug_printf("Bind viewport (%d, %d)\n", width, height);*/
328
329 set_viewport(r, width, height, Y0_TOP);
330 }
331
332 void renderer_bind_rasterizer(struct xorg_renderer *r)
333 {
334 struct pipe_rasterizer_state raster;
335
336 /* XXX: move to renderer_init_state? */
337 memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
338 raster.gl_rasterization_rules = 1;
339 cso_set_rasterizer(r->cso, &raster);
340 }
341
342 void renderer_set_constants(struct xorg_renderer *r,
343 int shader_type,
344 const float *params,
345 int param_bytes)
346 {
347 struct pipe_constant_buffer *cbuf =
348 (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
349 &r->fs_const_buffer;
350
351 pipe_buffer_reference(&cbuf->buffer, NULL);
352 cbuf->buffer = pipe_buffer_create(r->pipe->screen, 16,
353 PIPE_BUFFER_USAGE_CONSTANT,
354 param_bytes);
355
356 if (cbuf->buffer) {
357 pipe_buffer_write(r->pipe->screen, cbuf->buffer,
358 0, param_bytes, params);
359 }
360 r->pipe->set_constant_buffer(r->pipe, shader_type, 0, cbuf);
361 }
362
363 static void
364 setup_vs_constant_buffer(struct xorg_renderer *r,
365 int width, int height)
366 {
367 const int param_bytes = 8 * sizeof(float);
368 float vs_consts[8] = {
369 2.f/width, 2.f/height, 1, 1,
370 -1, -1, 0, 0
371 };
372 renderer_set_constants(r, PIPE_SHADER_VERTEX,
373 vs_consts, param_bytes);
374 }
375
376 static void
377 setup_fs_constant_buffer(struct xorg_renderer *r)
378 {
379 const int param_bytes = 4 * sizeof(float);
380 const float fs_consts[8] = {
381 0, 0, 0, 1,
382 };
383 renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
384 fs_consts, param_bytes);
385 }
386
387 static INLINE void shift_rectx(float coords[4],
388 const float *bounds,
389 const float shift)
390 {
391 coords[0] += shift;
392 coords[2] -= shift;
393 if (bounds) {
394 coords[2] = MIN2(coords[2], bounds[2]);
395 /* bound x/y + width/height */
396 if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
397 coords[2] = (bounds[0] + bounds[2]) - coords[0];
398 }
399 }
400 }
401
402 static INLINE void shift_recty(float coords[4],
403 const float *bounds,
404 const float shift)
405 {
406 coords[1] += shift;
407 coords[3] -= shift;
408 if (bounds) {
409 coords[3] = MIN2(coords[3], bounds[3]);
410 if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
411 coords[3] = (bounds[1] + bounds[3]) - coords[1];
412 }
413 }
414 }
415
416 static INLINE void bound_rect(float coords[4],
417 const float bounds[4],
418 float shift[4])
419 {
420 /* if outside the bounds */
421 if (coords[0] > (bounds[0] + bounds[2]) ||
422 coords[1] > (bounds[1] + bounds[3]) ||
423 (coords[0] + coords[2]) < bounds[0] ||
424 (coords[1] + coords[3]) < bounds[1]) {
425 coords[0] = 0.f;
426 coords[1] = 0.f;
427 coords[2] = 0.f;
428 coords[3] = 0.f;
429 shift[0] = 0.f;
430 shift[1] = 0.f;
431 return;
432 }
433
434 /* bound x */
435 if (coords[0] < bounds[0]) {
436 shift[0] = bounds[0] - coords[0];
437 coords[2] -= shift[0];
438 coords[0] = bounds[0];
439 } else
440 shift[0] = 0.f;
441
442 /* bound y */
443 if (coords[1] < bounds[1]) {
444 shift[1] = bounds[1] - coords[1];
445 coords[3] -= shift[1];
446 coords[1] = bounds[1];
447 } else
448 shift[1] = 0.f;
449
450 shift[2] = bounds[2] - coords[2];
451 shift[3] = bounds[3] - coords[3];
452 /* bound width/height */
453 coords[2] = MIN2(coords[2], bounds[2]);
454 coords[3] = MIN2(coords[3], bounds[3]);
455
456 /* bound x/y + width/height */
457 if ((coords[0] + coords[2]) > (bounds[0] + bounds[2])) {
458 coords[2] = (bounds[0] + bounds[2]) - coords[0];
459 }
460 if ((coords[1] + coords[3]) > (bounds[1] + bounds[3])) {
461 coords[3] = (bounds[1] + bounds[3]) - coords[1];
462 }
463
464 /* if outside the bounds */
465 if ((coords[0] + coords[2]) < bounds[0] ||
466 (coords[1] + coords[3]) < bounds[1]) {
467 coords[0] = 0.f;
468 coords[1] = 0.f;
469 coords[2] = 0.f;
470 coords[3] = 0.f;
471 return;
472 }
473 }
474
475 static INLINE void sync_size(float *src_loc, float *dst_loc)
476 {
477 src_loc[2] = MIN2(src_loc[2], dst_loc[2]);
478 src_loc[3] = MIN2(src_loc[3], dst_loc[3]);
479 dst_loc[2] = src_loc[2];
480 dst_loc[3] = src_loc[3];
481 }
482
483 static void renderer_copy_texture(struct xorg_renderer *r,
484 struct pipe_texture *src,
485 float sx1, float sy1,
486 float sx2, float sy2,
487 struct pipe_texture *dst,
488 float dx1, float dy1,
489 float dx2, float dy2)
490 {
491 struct pipe_context *pipe = r->pipe;
492 struct pipe_screen *screen = pipe->screen;
493 struct pipe_buffer *buf;
494 struct pipe_surface *dst_surf = screen->get_tex_surface(
495 screen, dst, 0, 0, 0,
496 PIPE_BUFFER_USAGE_GPU_WRITE);
497 struct pipe_framebuffer_state fb;
498 float s0, t0, s1, t1;
499 struct xorg_shader shader;
500
501 assert(src->width[0] != 0);
502 assert(src->height[0] != 0);
503 assert(dst->width[0] != 0);
504 assert(dst->height[0] != 0);
505
506 #if 1
507 s0 = sx1 / src->width[0];
508 s1 = sx2 / src->width[0];
509 t0 = sy1 / src->height[0];
510 t1 = sy2 / src->height[0];
511 #else
512 s0 = 0;
513 s1 = 1;
514 t0 = 0;
515 t1 = 1;
516 #endif
517
518 #if 0
519 debug_printf("copy texture src=[%f, %f, %f, %f], dst=[%f, %f, %f, %f], tex=[%f, %f, %f, %f]\n",
520 sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2,
521 s0, t0, s1, t1);
522 #endif
523
524 assert(screen->is_format_supported(screen, dst_surf->format,
525 PIPE_TEXTURE_2D,
526 PIPE_TEXTURE_USAGE_RENDER_TARGET,
527 0));
528
529 /* save state (restored below) */
530 cso_save_blend(r->cso);
531 cso_save_samplers(r->cso);
532 cso_save_sampler_textures(r->cso);
533 cso_save_framebuffer(r->cso);
534 cso_save_fragment_shader(r->cso);
535 cso_save_vertex_shader(r->cso);
536
537 cso_save_viewport(r->cso);
538
539
540 /* set misc state we care about */
541 {
542 struct pipe_blend_state blend;
543 memset(&blend, 0, sizeof(blend));
544 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
545 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
546 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
547 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
548 blend.colormask = PIPE_MASK_RGBA;
549 cso_set_blend(r->cso, &blend);
550 }
551
552 /* sampler */
553 {
554 struct pipe_sampler_state sampler;
555 memset(&sampler, 0, sizeof(sampler));
556 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
557 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
558 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
559 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
560 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
561 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
562 sampler.normalized_coords = 1;
563 cso_single_sampler(r->cso, 0, &sampler);
564 cso_single_sampler_done(r->cso);
565 }
566
567 set_viewport(r, dst_surf->width, dst_surf->height, Y0_TOP);
568
569 /* texture */
570 cso_set_sampler_textures(r->cso, 1, &src);
571
572 renderer_bind_rasterizer(r);
573
574 /* shaders */
575 shader = xorg_shaders_get(r->shaders,
576 VS_COMPOSITE,
577 FS_COMPOSITE);
578 cso_set_vertex_shader_handle(r->cso, shader.vs);
579 cso_set_fragment_shader_handle(r->cso, shader.fs);
580
581 /* drawing dest */
582 memset(&fb, 0, sizeof(fb));
583 fb.width = dst_surf->width;
584 fb.height = dst_surf->height;
585 fb.nr_cbufs = 1;
586 fb.cbufs[0] = dst_surf;
587 {
588 int i;
589 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
590 fb.cbufs[i] = 0;
591 }
592 cso_set_framebuffer(r->cso, &fb);
593 setup_vs_constant_buffer(r, fb.width, fb.height);
594 setup_fs_constant_buffer(r);
595
596 /* draw quad */
597 buf = setup_vertex_data_tex(r,
598 dx1, dy1,
599 dx2, dy2,
600 s0, t0, s1, t1,
601 0.0f);
602
603 if (buf) {
604 util_draw_vertex_buffer(r->pipe, buf, 0,
605 PIPE_PRIM_TRIANGLE_FAN,
606 4, /* verts */
607 2); /* attribs/vert */
608
609 pipe_buffer_reference(&buf, NULL);
610 }
611
612 /* restore state we changed */
613 cso_restore_blend(r->cso);
614 cso_restore_samplers(r->cso);
615 cso_restore_sampler_textures(r->cso);
616 cso_restore_framebuffer(r->cso);
617 cso_restore_vertex_shader(r->cso);
618 cso_restore_fragment_shader(r->cso);
619 cso_restore_viewport(r->cso);
620
621 pipe_surface_reference(&dst_surf, NULL);
622 }
623
624 static struct pipe_texture *
625 create_sampler_texture(struct xorg_renderer *r,
626 struct pipe_texture *src)
627 {
628 enum pipe_format format;
629 struct pipe_context *pipe = r->pipe;
630 struct pipe_screen *screen = pipe->screen;
631 struct pipe_texture *pt;
632 struct pipe_texture templ;
633
634 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
635
636 /* the coming in texture should already have that invariance */
637 debug_assert(screen->is_format_supported(screen, src->format,
638 PIPE_TEXTURE_2D,
639 PIPE_TEXTURE_USAGE_SAMPLER, 0));
640
641 format = src->format;
642
643 memset(&templ, 0, sizeof(templ));
644 templ.target = PIPE_TEXTURE_2D;
645 templ.format = format;
646 templ.last_level = 0;
647 templ.width[0] = src->width[0];
648 templ.height[0] = src->height[0];
649 templ.depth[0] = 1;
650 pf_get_block(format, &templ.block);
651 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
652
653 pt = screen->texture_create(screen, &templ);
654
655 debug_assert(!pt || pipe_is_referenced(&pt->reference));
656
657 if (!pt)
658 return NULL;
659
660 {
661 /* copy source framebuffer surface into texture */
662 struct pipe_surface *ps_read = screen->get_tex_surface(
663 screen, src, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
664 struct pipe_surface *ps_tex = screen->get_tex_surface(
665 screen, pt, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE );
666 if (pipe->surface_copy) {
667 pipe->surface_copy(pipe,
668 ps_tex, /* dest */
669 0, 0, /* destx/y */
670 ps_read,
671 0, 0, src->width[0], src->height[0]);
672 } else {
673 util_surface_copy(pipe, FALSE,
674 ps_tex, /* dest */
675 0, 0, /* destx/y */
676 ps_read,
677 0, 0, src->width[0], src->height[0]);
678 }
679 pipe_surface_reference(&ps_read, NULL);
680 pipe_surface_reference(&ps_tex, NULL);
681 }
682
683 return pt;
684 }
685
686
687 void renderer_copy_pixmap(struct xorg_renderer *r,
688 struct exa_pixmap_priv *dst_priv, int dx, int dy,
689 struct exa_pixmap_priv *src_priv, int sx, int sy,
690 int width, int height)
691 {
692 float dst_loc[4], src_loc[4];
693 float dst_bounds[4], src_bounds[4];
694 float src_shift[4], dst_shift[4], shift[4];
695 struct pipe_texture *dst = dst_priv->tex;
696 struct pipe_texture *src = src_priv->tex;
697
698 if (r->pipe->is_texture_referenced(r->pipe, src, 0, 0) &
699 PIPE_REFERENCED_FOR_WRITE)
700 r->pipe->flush(r->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
701
702 dst_loc[0] = dx;
703 dst_loc[1] = dy;
704 dst_loc[2] = width;
705 dst_loc[3] = height;
706 dst_bounds[0] = 0.f;
707 dst_bounds[1] = 0.f;
708 dst_bounds[2] = dst->width[0];
709 dst_bounds[3] = dst->height[0];
710
711 src_loc[0] = sx;
712 src_loc[1] = sy;
713 src_loc[2] = width;
714 src_loc[3] = height;
715 src_bounds[0] = 0.f;
716 src_bounds[1] = 0.f;
717 src_bounds[2] = src->width[0];
718 src_bounds[3] = src->height[0];
719
720 bound_rect(src_loc, src_bounds, src_shift);
721 bound_rect(dst_loc, dst_bounds, dst_shift);
722 shift[0] = src_shift[0] - dst_shift[0];
723 shift[1] = src_shift[1] - dst_shift[1];
724
725 if (shift[0] < 0)
726 shift_rectx(src_loc, src_bounds, -shift[0]);
727 else
728 shift_rectx(dst_loc, dst_bounds, shift[0]);
729
730 if (shift[1] < 0)
731 shift_recty(src_loc, src_bounds, -shift[1]);
732 else
733 shift_recty(dst_loc, dst_bounds, shift[1]);
734
735 sync_size(src_loc, dst_loc);
736
737 if (src_loc[2] >= 0 && src_loc[3] >= 0 &&
738 dst_loc[2] >= 0 && dst_loc[3] >= 0) {
739 struct pipe_texture *temp_src = src;
740
741 if (src == dst)
742 temp_src = create_sampler_texture(r, src);
743
744 renderer_copy_texture(r,
745 temp_src,
746 src_loc[0],
747 src_loc[1],
748 src_loc[0] + src_loc[2],
749 src_loc[1] + src_loc[3],
750 dst,
751 dst_loc[0],
752 dst_loc[1],
753 dst_loc[0] + dst_loc[2],
754 dst_loc[1] + dst_loc[3]);
755
756 if (src == dst)
757 pipe_texture_reference(&temp_src, NULL);
758 }
759 }
760
761 void renderer_draw_solid_rect(struct xorg_renderer *r,
762 int x0, int y0,
763 int x1, int y1,
764 float *color)
765 {
766 struct pipe_context *pipe = r->pipe;
767 struct pipe_buffer *buf = 0;
768
769 /*
770 debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
771 x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
772 /* 1st vertex */
773 setup_vertex0(r->vertices2[0], x0, y0, color);
774 /* 2nd vertex */
775 setup_vertex0(r->vertices2[1], x1, y0, color);
776 /* 3rd vertex */
777 setup_vertex0(r->vertices2[2], x1, y1, color);
778 /* 4th vertex */
779 setup_vertex0(r->vertices2[3], x0, y1, color);
780
781 buf = pipe_user_buffer_create(pipe->screen,
782 r->vertices2,
783 sizeof(r->vertices2));
784
785
786 if (buf) {
787 util_draw_vertex_buffer(pipe, buf, 0,
788 PIPE_PRIM_TRIANGLE_FAN,
789 4, /* verts */
790 2); /* attribs/vert */
791
792 pipe_buffer_reference(&buf, NULL);
793 }
794 }
795
796 void renderer_draw_textures(struct xorg_renderer *r,
797 int *pos,
798 int width, int height,
799 struct pipe_texture **textures,
800 int num_textures,
801 float *src_matrix, float *mask_matrix)
802 {
803 struct pipe_context *pipe = r->pipe;
804 struct pipe_buffer *buf = 0;
805
806 switch(num_textures) {
807 case 1:
808 buf = setup_vertex_data1(r,
809 pos[0], pos[1], /* src */
810 pos[4], pos[5], /* dst */
811 width, height,
812 textures[0], src_matrix);
813 break;
814 case 2:
815 buf = setup_vertex_data2(r,
816 pos[0], pos[1], /* src */
817 pos[2], pos[3], /* mask */
818 pos[4], pos[5], /* dst */
819 width, height,
820 textures[0], textures[1],
821 src_matrix, mask_matrix);
822 break;
823 default:
824 debug_assert(!"Unsupported number of textures");
825 break;
826 }
827
828 if (buf) {
829 int num_attribs = 1; /*pos*/
830 num_attribs += num_textures;
831
832 util_draw_vertex_buffer(pipe, buf, 0,
833 PIPE_PRIM_TRIANGLE_FAN,
834 4, /* verts */
835 num_attribs); /* attribs/vert */
836
837 pipe_buffer_reference(&buf, NULL);
838 }
839 }