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