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