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