xorg_renderer.c revision eafb7f234d11a290b00dcaf5492b9bdad1cf5148
18fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "xorg_exa.h"
28fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "xorg_renderer.h"
38fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
48fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "xorg_exa_tgsi.h"
58fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
68fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "cso_cache/cso_context.h"
78fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "util/u_draw_quad.h"
85970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org#include "util/u_math.h"
98fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "util/u_memory.h"
108fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "util/u_sampler.h"
115970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org
128fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "util/u_inlines.h"
138fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "util/u_box.h"
148fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
158fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include <math.h>
168fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
178fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
188fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#define floatIsZero(x) (floatsEqual((x) + 1, 1))
198fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
208fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#define NUM_COMPONENTS 4
218fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
228fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgstatic INLINE boolean is_affine(float *matrix)
238fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org{
248fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
258fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      && floatsEqual(matrix[8], 1);
268fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
273e629e16548e45750fcad5e7a28bbf6dfc8571efLeon Scroggins IIIstatic INLINE void map_point(float *mat, float x, float y,
288fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org                             float *out_x, float *out_y)
298fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org{
303e629e16548e45750fcad5e7a28bbf6dfc8571efLeon Scroggins III   if (!mat) {
318fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      *out_x = x;
328fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      *out_y = y;
338fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      return;
348fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   }
358fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
368fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   *out_x = mat[0]*x + mat[3]*y + mat[6];
378fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   *out_y = mat[1]*x + mat[4]*y + mat[7];
38b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.com   if (!is_affine(mat)) {
398fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      float w = 1/(mat[2]*x + mat[5]*y + mat[8]);
40b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.com      *out_x *= w;
418fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      *out_y *= w;
42b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.com   }
438fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
448fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
458fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgstatic INLINE struct pipe_resource *
468fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgrenderer_buffer_create(struct xorg_renderer *r)
478fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org{
488fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   struct pipe_resource *buf =
498fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org      pipe_user_buffer_create(r->pipe->screen,
508fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org                              r->buffer,
51b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.com                              sizeof(float)*
528fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org                              r->buffer_size,
53b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.com/* XXX was: PIPE_BUFFER_USAGE_PIXEL/PIPE_BUFFER_USAGE_GPU_WRITE even though this is a vertex buffer??? */
548fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org			      PIPE_BIND_VERTEX_BUFFER);
558fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   r->buffer_size = 0;
568fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
578fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   return buf;
588fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
598fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
608fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgstatic INLINE void
61b2c82c99f891e4e846e4959c811661bf68fa43d6skia.committer@gmail.comrenderer_draw(struct xorg_renderer *r)
628fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org{
638fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org   struct pipe_context *pipe = r->pipe;
64   struct pipe_resource *buf = 0;
65   int num_verts = r->buffer_size/(r->attrs_per_vertex * NUM_COMPONENTS);
66
67   if (!r->buffer_size)
68      return;
69
70   buf = renderer_buffer_create(r);
71
72
73   if (buf) {
74      cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
75
76      util_draw_vertex_buffer(pipe, r->cso, buf, 0,
77                              PIPE_PRIM_QUADS,
78                              num_verts,  /* verts */
79                              r->attrs_per_vertex); /* attribs/vert */
80
81      pipe_resource_reference(&buf, NULL);
82   }
83}
84
85static INLINE void
86renderer_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
95static void
96renderer_init_state(struct xorg_renderer *r)
97{
98   struct pipe_depth_stencil_alpha_state dsa;
99   struct pipe_rasterizer_state raster;
100   unsigned i;
101
102   /* set common initial clip state */
103   memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
104   cso_set_depth_stencil_alpha(r->cso, &dsa);
105
106
107   /* XXX: move to renderer_init_state? */
108   memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
109   raster.gl_rasterization_rules = 1;
110   cso_set_rasterizer(r->cso, &raster);
111
112   /* vertex elements state */
113   memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
114   for (i = 0; i < 3; i++) {
115      r->velems[i].src_offset = i * 4 * sizeof(float);
116      r->velems[i].instance_divisor = 0;
117      r->velems[i].vertex_buffer_index = 0;
118      r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
119   }
120}
121
122
123static INLINE void
124add_vertex_color(struct xorg_renderer *r,
125                 float x, float y,
126                 float color[4])
127{
128   float *vertex = r->buffer + r->buffer_size;
129
130   vertex[0] = x;
131   vertex[1] = y;
132   vertex[2] = 0.f; /*z*/
133   vertex[3] = 1.f; /*w*/
134
135   vertex[4] = color[0]; /*r*/
136   vertex[5] = color[1]; /*g*/
137   vertex[6] = color[2]; /*b*/
138   vertex[7] = color[3]; /*a*/
139
140   r->buffer_size += 8;
141}
142
143static INLINE void
144add_vertex_1tex(struct xorg_renderer *r,
145                float x, float y, float s, float t)
146{
147   float *vertex = r->buffer + r->buffer_size;
148
149   vertex[0] = x;
150   vertex[1] = y;
151   vertex[2] = 0.f; /*z*/
152   vertex[3] = 1.f; /*w*/
153
154   vertex[4] = s;   /*s*/
155   vertex[5] = t;   /*t*/
156   vertex[6] = 0.f; /*r*/
157   vertex[7] = 1.f; /*q*/
158
159   r->buffer_size += 8;
160}
161
162static void
163add_vertex_data1(struct xorg_renderer *r,
164                 float srcX, float srcY,  float dstX, float dstY,
165                 float width, float height,
166                 struct pipe_resource *src, float *src_matrix)
167{
168   float s0, t0, s1, t1, s2, t2, s3, t3;
169   float pt0[2], pt1[2], pt2[2], pt3[2];
170
171   pt0[0] = srcX;
172   pt0[1] = srcY;
173   pt1[0] = (srcX + width);
174   pt1[1] = srcY;
175   pt2[0] = (srcX + width);
176   pt2[1] = (srcY + height);
177   pt3[0] = srcX;
178   pt3[1] = (srcY + height);
179
180   if (src_matrix) {
181      map_point(src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
182      map_point(src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
183      map_point(src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
184      map_point(src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
185   }
186
187   s0 =  pt0[0] / src->width0;
188   s1 =  pt1[0] / src->width0;
189   s2 =  pt2[0] / src->width0;
190   s3 =  pt3[0] / src->width0;
191   t0 =  pt0[1] / src->height0;
192   t1 =  pt1[1] / src->height0;
193   t2 =  pt2[1] / src->height0;
194   t3 =  pt3[1] / src->height0;
195
196   /* 1st vertex */
197   add_vertex_1tex(r, dstX, dstY, s0, t0);
198   /* 2nd vertex */
199   add_vertex_1tex(r, dstX + width, dstY, s1, t1);
200   /* 3rd vertex */
201   add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
202   /* 4th vertex */
203   add_vertex_1tex(r, dstX, dstY + height, s3, t3);
204}
205
206
207static INLINE void
208add_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
232static void
233add_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_resource *src,
237                 struct pipe_resource *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->width0;
266   src_t0 = spt0[1] / src->height0;
267   src_s1 = spt1[0] / src->width0;
268   src_t1 = spt1[1] / src->height0;
269
270   mask_s0 = mpt0[0] / mask->width0;
271   mask_t0 = mpt0[1] / mask->height0;
272   mask_s1 = mpt1[0] / mask->width0;
273   mask_t1 = mpt1[1] / mask->height0;
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
289static struct pipe_resource *
290setup_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_resource **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]->width0;
304   t0 = spt0[1] / tex[0]->height0;
305   s1 = spt1[0] / tex[0]->width0;
306   t1 = spt1[1] / tex[0]->height0;
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/* Set up framebuffer, viewport and vertex shader constant buffer
326 * state for a particular destinaton surface.  In all our rendering,
327 * these concepts are linked.
328 */
329void renderer_bind_destination(struct xorg_renderer *r,
330                               struct pipe_surface *surface,
331                               int width,
332                               int height )
333{
334
335   struct pipe_framebuffer_state fb;
336   struct pipe_viewport_state viewport;
337
338   /* Framebuffer uses actual surface width/height
339    */
340   memset(&fb, 0, sizeof fb);
341   fb.width  = surface->width;
342   fb.height = surface->height;
343   fb.nr_cbufs = 1;
344   fb.cbufs[0] = surface;
345   fb.zsbuf = 0;
346
347   /* Viewport just touches the bit we're interested in:
348    */
349   viewport.scale[0] =  width / 2.f;
350   viewport.scale[1] =  height / 2.f;
351   viewport.scale[2] =  1.0;
352   viewport.scale[3] =  1.0;
353   viewport.translate[0] = width / 2.f;
354   viewport.translate[1] = height / 2.f;
355   viewport.translate[2] = 0.0;
356   viewport.translate[3] = 0.0;
357
358   /* Constant buffer set up to match viewport dimensions:
359    */
360   if (r->fb_width != width ||
361       r->fb_height != height)
362   {
363      float vs_consts[8] = {
364         2.f/width, 2.f/height, 1, 1,
365         -1, -1, 0, 0
366      };
367
368      r->fb_width = width;
369      r->fb_height = height;
370
371      renderer_set_constants(r, PIPE_SHADER_VERTEX,
372                             vs_consts, sizeof vs_consts);
373   }
374
375   cso_set_framebuffer(r->cso, &fb);
376   cso_set_viewport(r->cso, &viewport);
377}
378
379
380struct xorg_renderer * renderer_create(struct pipe_context *pipe)
381{
382   struct xorg_renderer *renderer = CALLOC_STRUCT(xorg_renderer);
383
384   renderer->pipe = pipe;
385   renderer->cso = cso_create_context(pipe);
386   renderer->shaders = xorg_shaders_create(renderer);
387
388   renderer_init_state(renderer);
389
390   return renderer;
391}
392
393void renderer_destroy(struct xorg_renderer *r)
394{
395   struct pipe_resource **vsbuf = &r->vs_const_buffer;
396   struct pipe_resource **fsbuf = &r->fs_const_buffer;
397
398   if (*vsbuf)
399      pipe_resource_reference(vsbuf, NULL);
400
401   if (*fsbuf)
402      pipe_resource_reference(fsbuf, NULL);
403
404   if (r->shaders) {
405      xorg_shaders_destroy(r->shaders);
406      r->shaders = NULL;
407   }
408
409   if (r->cso) {
410      cso_release_all(r->cso);
411      cso_destroy_context(r->cso);
412      r->cso = NULL;
413   }
414}
415
416
417
418
419
420void renderer_set_constants(struct xorg_renderer *r,
421                            int shader_type,
422                            const float *params,
423                            int param_bytes)
424{
425   struct pipe_resource **cbuf =
426      (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
427      &r->fs_const_buffer;
428
429   pipe_resource_reference(cbuf, NULL);
430   *cbuf = pipe_buffer_create(r->pipe->screen,
431                              PIPE_BIND_CONSTANT_BUFFER,
432                              PIPE_USAGE_STATIC,
433                              param_bytes);
434
435   if (*cbuf) {
436      pipe_buffer_write(r->pipe, *cbuf,
437                        0, param_bytes, params);
438   }
439   r->pipe->set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
440}
441
442
443void renderer_copy_prepare(struct xorg_renderer *r,
444                           struct pipe_surface *dst_surface,
445                           struct pipe_resource *src_texture)
446{
447   struct pipe_context *pipe = r->pipe;
448   struct pipe_screen *screen = pipe->screen;
449   struct xorg_shader shader;
450
451   assert(screen->is_format_supported(screen, dst_surface->format,
452                                      PIPE_TEXTURE_2D, 0,
453                                      PIPE_BIND_RENDER_TARGET,
454                                      0));
455   (void) screen;
456
457
458   /* set misc state we care about */
459   {
460      struct pipe_blend_state blend;
461      memset(&blend, 0, sizeof(blend));
462      blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
463      blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
464      blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
465      blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
466      blend.rt[0].colormask = PIPE_MASK_RGBA;
467      cso_set_blend(r->cso, &blend);
468   }
469
470   /* sampler */
471   {
472      struct pipe_sampler_state sampler;
473      memset(&sampler, 0, sizeof(sampler));
474      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
475      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
476      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
477      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
478      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
479      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
480      sampler.normalized_coords = 1;
481      cso_single_sampler(r->cso, 0, &sampler);
482      cso_single_sampler_done(r->cso);
483   }
484
485   renderer_bind_destination(r, dst_surface,
486                             dst_surface->width,
487                             dst_surface->height);
488
489   /* texture/sampler view */
490   {
491      struct pipe_sampler_view templ;
492      struct pipe_sampler_view *src_view;
493      u_sampler_view_default_template(&templ,
494                                      src_texture,
495                                      src_texture->format);
496      src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
497      cso_set_fragment_sampler_views(r->cso, 1, &src_view);
498      pipe_sampler_view_reference(&src_view, NULL);
499   }
500
501   /* shaders */
502   shader = xorg_shaders_get(r->shaders,
503                             VS_COMPOSITE,
504                             FS_COMPOSITE);
505   cso_set_vertex_shader_handle(r->cso, shader.vs);
506   cso_set_fragment_shader_handle(r->cso, shader.fs);
507
508   r->buffer_size = 0;
509   r->attrs_per_vertex = 2;
510}
511
512struct pipe_resource *
513renderer_clone_texture(struct xorg_renderer *r,
514                       struct pipe_resource *src)
515{
516   enum pipe_format format;
517   struct pipe_context *pipe = r->pipe;
518   struct pipe_screen *screen = pipe->screen;
519   struct pipe_resource *pt;
520   struct pipe_resource templ;
521
522   if (pipe->is_resource_referenced(pipe, src, 0, 0) &
523       PIPE_REFERENCED_FOR_WRITE)
524      pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
525
526   /* the coming in texture should already have that invariance */
527   debug_assert(screen->is_format_supported(screen, src->format,
528                                            PIPE_TEXTURE_2D, 0,
529                                            PIPE_BIND_SAMPLER_VIEW, 0));
530
531   format = src->format;
532
533   memset(&templ, 0, sizeof(templ));
534   templ.target = PIPE_TEXTURE_2D;
535   templ.format = format;
536   templ.last_level = 0;
537   templ.width0 = src->width0;
538   templ.height0 = src->height0;
539   templ.depth0 = 1;
540   templ.array_size = 1;
541   templ.bind = PIPE_BIND_SAMPLER_VIEW;
542
543   pt = screen->resource_create(screen, &templ);
544
545   debug_assert(!pt || pipe_is_referenced(&pt->reference));
546
547   if (!pt)
548      return NULL;
549
550   {
551      /* copy source framebuffer surface into texture */
552      struct pipe_box src_box;
553      u_box_origin_2d(src->width0, src->height0, &src_box);
554
555      pipe->resource_copy_region(pipe,
556                                 pt, /* dest */
557                                 0, /* dest_level */
558                                 0, 0, 0, /* destx/y/z */
559                                 src,
560                                 0, &src_box);
561   }
562
563   return pt;
564}
565
566
567void renderer_copy_pixmap(struct xorg_renderer *r,
568                          int dx, int dy,
569                          int sx, int sy,
570                          int width, int height,
571                          float src_width,
572                          float src_height)
573{
574   float s0, t0, s1, t1;
575   float x0, y0, x1, y1;
576
577
578   /* XXX: could put the texcoord scaling calculation into the vertex
579    * shader.
580    */
581   s0 = sx            / src_width;
582   s1 = (sx + width)  / src_width;
583   t0 = sy            / src_height;
584   t1 = (sy + height) / src_height;
585
586   x0 = dx;
587   x1 = dx + width;
588   y0 = dy;
589   y1 = dy + height;
590
591   /* draw quad */
592   renderer_draw_conditional(r, 4*8);
593   add_vertex_1tex(r, x0, y0, s0, t0);
594   add_vertex_1tex(r, x1, y0, s1, t0);
595   add_vertex_1tex(r, x1, y1, s1, t1);
596   add_vertex_1tex(r, x0, y1, s0, t1);
597}
598
599
600
601
602void renderer_draw_yuv(struct xorg_renderer *r,
603                       float src_x, float src_y, float src_w, float src_h,
604                       int dst_x, int dst_y, int dst_w, int dst_h,
605                       struct pipe_resource **textures)
606{
607   struct pipe_context *pipe = r->pipe;
608   struct pipe_resource *buf = 0;
609
610   buf = setup_vertex_data_yuv(r,
611                               src_x, src_y, src_w, src_h,
612                               dst_x, dst_y, dst_w, dst_h,
613                               textures);
614
615   if (buf) {
616      const int num_attribs = 2; /*pos + tex coord*/
617
618      cso_set_vertex_elements(r->cso, num_attribs, r->velems);
619
620      util_draw_vertex_buffer(pipe, r->cso, buf, 0,
621                              PIPE_PRIM_QUADS,
622                              4,  /* verts */
623                              num_attribs); /* attribs/vert */
624
625      pipe_resource_reference(&buf, NULL);
626   }
627}
628
629void renderer_begin_solid(struct xorg_renderer *r)
630{
631   r->buffer_size = 0;
632   r->attrs_per_vertex = 2;
633}
634
635void renderer_solid(struct xorg_renderer *r,
636                    int x0, int y0,
637                    int x1, int y1,
638                    float *color)
639{
640   /*
641   debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
642   x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
643
644   renderer_draw_conditional(r, 4 * 8);
645
646   /* 1st vertex */
647   add_vertex_color(r, x0, y0, color);
648   /* 2nd vertex */
649   add_vertex_color(r, x1, y0, color);
650   /* 3rd vertex */
651   add_vertex_color(r, x1, y1, color);
652   /* 4th vertex */
653   add_vertex_color(r, x0, y1, color);
654}
655
656void renderer_draw_flush(struct xorg_renderer *r)
657{
658   renderer_draw_conditional(r, 0);
659}
660
661void renderer_begin_textures(struct xorg_renderer *r,
662                             int num_textures)
663{
664   r->attrs_per_vertex = 1 + num_textures;
665   r->buffer_size = 0;
666}
667
668void renderer_texture(struct xorg_renderer *r,
669                      int *pos,
670                      int width, int height,
671                      struct pipe_sampler_view **sampler_view,
672                      int num_textures,
673                      float *src_matrix,
674                      float *mask_matrix)
675{
676
677#if 0
678   if (src_matrix) {
679      debug_printf("src_matrix = \n");
680      debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
681      debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
682      debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
683   }
684   if (mask_matrix) {
685      debug_printf("mask_matrix = \n");
686      debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
687      debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
688      debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
689   }
690#endif
691
692   switch(r->attrs_per_vertex) {
693   case 2:
694      renderer_draw_conditional(r, 4 * 8);
695      add_vertex_data1(r,
696                       pos[0], pos[1], /* src */
697                       pos[4], pos[5], /* dst */
698                       width, height,
699                       sampler_view[0]->texture, src_matrix);
700      break;
701   case 3:
702      renderer_draw_conditional(r, 4 * 12);
703      add_vertex_data2(r,
704                       pos[0], pos[1], /* src */
705                       pos[2], pos[3], /* mask */
706                       pos[4], pos[5], /* dst */
707                       width, height,
708                       sampler_view[0]->texture, sampler_view[1]->texture,
709                       src_matrix, mask_matrix);
710      break;
711   default:
712      debug_assert(!"Unsupported number of textures");
713      break;
714   }
715}
716