xorg_renderer.c revision 899d20cfaa003913b38ae9e095ca87b8725a19c1
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
16enum 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
26static INLINE boolean is_affine(float *matrix)
27{
28   return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
29      && floatsEqual(matrix[8], 1);
30}
31static 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
49static INLINE struct pipe_buffer *
50renderer_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
62static INLINE void
63renderer_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
86static INLINE void
87renderer_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
96static void
97renderer_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
107static INLINE void
108add_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
127static INLINE void
128add_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
146static void
147add_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
190static struct pipe_buffer *
191setup_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
208static INLINE void
209add_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
233static void
234add_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
290static struct pipe_buffer *
291setup_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 */
330void 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
375struct 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
388void 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
415void 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
425void 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
446static void
447setup_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
457static 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
472static 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
486static 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
545static 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
553static 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
599   /* set misc state we care about */
600   {
601      struct pipe_blend_state blend;
602      memset(&blend, 0, sizeof(blend));
603      blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
604      blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
605      blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
606      blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
607      blend.colormask = PIPE_MASK_RGBA;
608      cso_set_blend(r->cso, &blend);
609   }
610
611   /* sampler */
612   {
613      struct pipe_sampler_state sampler;
614      memset(&sampler, 0, sizeof(sampler));
615      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
616      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
617      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
618      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
619      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
620      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
621      sampler.normalized_coords = 1;
622      cso_single_sampler(r->cso, 0, &sampler);
623      cso_single_sampler_done(r->cso);
624   }
625
626   renderer_bind_destination(r, dst_surf);
627
628   /* texture */
629   cso_set_sampler_textures(r->cso, 1, &src);
630
631   renderer_bind_rasterizer(r);
632
633   /* shaders */
634   shader = xorg_shaders_get(r->shaders,
635                             VS_COMPOSITE,
636                             FS_COMPOSITE);
637   cso_set_vertex_shader_handle(r->cso, shader.vs);
638   cso_set_fragment_shader_handle(r->cso, shader.fs);
639
640   setup_fs_constant_buffer(r);
641
642   /* draw quad */
643   buf = setup_vertex_data_tex(r,
644                               dx1, dy1,
645                               dx2, dy2,
646                               s0, t0, s1, t1,
647                               0.0f);
648
649   if (buf) {
650      util_draw_vertex_buffer(r->pipe, buf, 0,
651                              PIPE_PRIM_QUADS,
652                              4,  /* verts */
653                              2); /* attribs/vert */
654
655      pipe_buffer_reference(&buf, NULL);
656   }
657
658   pipe_surface_reference(&dst_surf, NULL);
659}
660
661static struct pipe_texture *
662create_sampler_texture(struct xorg_renderer *r,
663                       struct pipe_texture *src)
664{
665   enum pipe_format format;
666   struct pipe_context *pipe = r->pipe;
667   struct pipe_screen *screen = pipe->screen;
668   struct pipe_texture *pt;
669   struct pipe_texture templ;
670
671   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
672
673   /* the coming in texture should already have that invariance */
674   debug_assert(screen->is_format_supported(screen, src->format,
675                                            PIPE_TEXTURE_2D,
676                                            PIPE_TEXTURE_USAGE_SAMPLER, 0));
677
678   format = src->format;
679
680   memset(&templ, 0, sizeof(templ));
681   templ.target = PIPE_TEXTURE_2D;
682   templ.format = format;
683   templ.last_level = 0;
684   templ.width[0] = src->width[0];
685   templ.height[0] = src->height[0];
686   templ.depth[0] = 1;
687   pf_get_block(format, &templ.block);
688   templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
689
690   pt = screen->texture_create(screen, &templ);
691
692   debug_assert(!pt || pipe_is_referenced(&pt->reference));
693
694   if (!pt)
695      return NULL;
696
697   {
698      /* copy source framebuffer surface into texture */
699      struct pipe_surface *ps_read = screen->get_tex_surface(
700         screen, src, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
701      struct pipe_surface *ps_tex = screen->get_tex_surface(
702         screen, pt, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE );
703      if (pipe->surface_copy) {
704         pipe->surface_copy(pipe,
705                ps_tex, /* dest */
706                0, 0, /* destx/y */
707                ps_read,
708                0, 0, src->width[0], src->height[0]);
709      } else {
710          util_surface_copy(pipe, FALSE,
711                ps_tex, /* dest */
712                0, 0, /* destx/y */
713                ps_read,
714                0, 0, src->width[0], src->height[0]);
715      }
716      pipe_surface_reference(&ps_read, NULL);
717      pipe_surface_reference(&ps_tex, NULL);
718   }
719
720   return pt;
721}
722
723
724void renderer_copy_pixmap(struct xorg_renderer *r,
725                          struct exa_pixmap_priv *dst_priv, int dx, int dy,
726                          struct exa_pixmap_priv *src_priv, int sx, int sy,
727                          int width, int height)
728{
729   float dst_loc[4], src_loc[4];
730   float dst_bounds[4], src_bounds[4];
731   float src_shift[4], dst_shift[4], shift[4];
732   struct pipe_texture *dst = dst_priv->tex;
733   struct pipe_texture *src = src_priv->tex;
734
735   if (r->pipe->is_texture_referenced(r->pipe, src, 0, 0) &
736       PIPE_REFERENCED_FOR_WRITE)
737      r->pipe->flush(r->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
738
739   dst_loc[0] = dx;
740   dst_loc[1] = dy;
741   dst_loc[2] = width;
742   dst_loc[3] = height;
743   dst_bounds[0] = 0.f;
744   dst_bounds[1] = 0.f;
745   dst_bounds[2] = dst->width[0];
746   dst_bounds[3] = dst->height[0];
747
748   src_loc[0] = sx;
749   src_loc[1] = sy;
750   src_loc[2] = width;
751   src_loc[3] = height;
752   src_bounds[0] = 0.f;
753   src_bounds[1] = 0.f;
754   src_bounds[2] = src->width[0];
755   src_bounds[3] = src->height[0];
756
757   bound_rect(src_loc, src_bounds, src_shift);
758   bound_rect(dst_loc, dst_bounds, dst_shift);
759   shift[0] = src_shift[0] - dst_shift[0];
760   shift[1] = src_shift[1] - dst_shift[1];
761
762   if (shift[0] < 0)
763      shift_rectx(src_loc, src_bounds, -shift[0]);
764   else
765      shift_rectx(dst_loc, dst_bounds, shift[0]);
766
767   if (shift[1] < 0)
768      shift_recty(src_loc, src_bounds, -shift[1]);
769   else
770      shift_recty(dst_loc, dst_bounds, shift[1]);
771
772   sync_size(src_loc, dst_loc);
773
774   if (src_loc[2] >= 0 && src_loc[3] >= 0 &&
775       dst_loc[2] >= 0 && dst_loc[3] >= 0) {
776      struct pipe_texture *temp_src = src;
777
778      if (src == dst)
779         temp_src = create_sampler_texture(r, src);
780
781      renderer_copy_texture(r,
782                            temp_src,
783                            src_loc[0],
784                            src_loc[1],
785                            src_loc[0] + src_loc[2],
786                            src_loc[1] + src_loc[3],
787                            dst,
788                            dst_loc[0],
789                            dst_loc[1],
790                            dst_loc[0] + dst_loc[2],
791                            dst_loc[1] + dst_loc[3]);
792
793      if (src == dst)
794         pipe_texture_reference(&temp_src, NULL);
795   }
796}
797
798void renderer_draw_yuv(struct xorg_renderer *r,
799                       int src_x, int src_y, int src_w, int src_h,
800                       int dst_x, int dst_y, int dst_w, int dst_h,
801                       struct pipe_texture **textures)
802{
803   struct pipe_context *pipe = r->pipe;
804   struct pipe_buffer *buf = 0;
805
806   buf = setup_vertex_data_yuv(r,
807                               src_x, src_y, src_w, src_h,
808                               dst_x, dst_y, dst_w, dst_h,
809                               textures);
810
811   if (buf) {
812      const int num_attribs = 2; /*pos + tex coord*/
813
814      util_draw_vertex_buffer(pipe, buf, 0,
815                              PIPE_PRIM_QUADS,
816                              4,  /* verts */
817                              num_attribs); /* attribs/vert */
818
819      pipe_buffer_reference(&buf, NULL);
820   }
821}
822
823void renderer_begin_solid(struct xorg_renderer *r)
824{
825   r->buffer_size = 0;
826   r->attrs_per_vertex = 2;
827}
828
829void renderer_solid(struct xorg_renderer *r,
830                    int x0, int y0,
831                    int x1, int y1,
832                    float *color)
833{
834   /*
835   debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
836   x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
837
838   renderer_draw_conditional(r, 4 * 8);
839
840   /* 1st vertex */
841   add_vertex_color(r, x0, y0, color);
842   /* 2nd vertex */
843   add_vertex_color(r, x1, y0, color);
844   /* 3rd vertex */
845   add_vertex_color(r, x1, y1, color);
846   /* 4th vertex */
847   add_vertex_color(r, x0, y1, color);
848}
849
850void renderer_draw_flush(struct xorg_renderer *r)
851{
852   renderer_draw_conditional(r, 0);
853}
854
855void renderer_begin_textures(struct xorg_renderer *r,
856                             struct pipe_texture **textures,
857                             int num_textures)
858{
859   r->attrs_per_vertex = 1 + num_textures;
860   r->buffer_size = 0;
861}
862
863void renderer_texture(struct xorg_renderer *r,
864                      int *pos,
865                      int width, int height,
866                      struct pipe_texture **textures,
867                      int num_textures,
868                      float *src_matrix,
869                      float *mask_matrix)
870{
871
872#if 0
873   if (src_matrix) {
874      debug_printf("src_matrix = \n");
875      debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
876      debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
877      debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
878   }
879   if (mask_matrix) {
880      debug_printf("mask_matrix = \n");
881      debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
882      debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
883      debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
884   }
885#endif
886
887   switch(r->attrs_per_vertex) {
888   case 2:
889      renderer_draw_conditional(r, 4 * 8);
890      add_vertex_data1(r,
891                       pos[0], pos[1], /* src */
892                       pos[4], pos[5], /* dst */
893                       width, height,
894                       textures[0], src_matrix);
895      break;
896   case 3:
897      renderer_draw_conditional(r, 4 * 12);
898      add_vertex_data2(r,
899                       pos[0], pos[1], /* src */
900                       pos[2], pos[3], /* mask */
901                       pos[4], pos[5], /* dst */
902                       width, height,
903                       textures[0], textures[1],
904                       src_matrix, mask_matrix);
905      break;
906   default:
907      debug_assert(!"Unsupported number of textures");
908      break;
909   }
910}
911