xorg_xv.c revision 7356d1c140f2989df9e8645cf4b85962f27d0fca
1#include "xorg_tracker.h"
2
3#include <xf86xv.h>
4#include <X11/extensions/Xv.h>
5#include <fourcc.h>
6
7#include "xorg_exa.h"
8#include "xorg_renderer.h"
9#include "xorg_exa_tgsi.h"
10
11#include "cso_cache/cso_context.h"
12#include "util/u_sampler.h"
13
14#include "pipe/p_screen.h"
15
16/*XXX get these from pipe's texture limits */
17#define IMAGE_MAX_WIDTH		2048
18#define IMAGE_MAX_HEIGHT	2048
19
20#define RES_720P_X 1280
21#define RES_720P_Y 720
22
23
24/* The ITU-R BT.601 conversion matrix for SDTV. */
25/* original, matrix, but we transpose it to
26 * make the shader easier
27static const float bt_601[] = {
28    1.0, 0.0, 1.4075,   ,
29    1.0, -0.3455, -0.7169, 0,
30    1.0, 1.7790, 0., 0,
31};*/
32static const float bt_601[] = {
33    1.0, 1.0, 1.0,        0.5,
34    0.0, -0.3455, 1.7790, 0,
35    1.4075, -0.7169, 0.,  0,
36};
37
38/* The ITU-R BT.709 conversion matrix for HDTV. */
39/* original, but we transpose to make the conversion
40 * in the shader easier
41static const float bt_709[] = {
42    1.0, 0.0, 1.581, 0,
43    1.0, -0.1881, -0.47, 0,
44    1.0, 1.8629, 0., 0,
45};*/
46static const float bt_709[] = {
47    1.0,   1.0,     1.0,     0.5,
48    0.0,  -0.1881,  1.8629,  0,
49    1.581,-0.47   , 0.0,     0,
50};
51
52#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
53
54static Atom xvBrightness, xvContrast;
55
56#define NUM_TEXTURED_ATTRIBUTES 2
57static XF86AttributeRec TexturedAttributes[NUM_TEXTURED_ATTRIBUTES] = {
58   {XvSettable | XvGettable, -128, 127, "XV_BRIGHTNESS"},
59   {XvSettable | XvGettable, 0, 255, "XV_CONTRAST"}
60};
61
62#define NUM_FORMATS 3
63static XF86VideoFormatRec Formats[NUM_FORMATS] = {
64   {15, TrueColor}, {16, TrueColor}, {24, TrueColor}
65};
66
67static XF86VideoEncodingRec DummyEncoding[1] = {
68   {
69      0,
70      "XV_IMAGE",
71      IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
72      {1, 1}
73   }
74};
75
76#define NUM_IMAGES 3
77static XF86ImageRec Images[NUM_IMAGES] = {
78   XVIMAGE_UYVY,
79   XVIMAGE_YUY2,
80   XVIMAGE_YV12,
81};
82
83struct xorg_xv_port_priv {
84   struct xorg_renderer *r;
85
86   RegionRec clip;
87
88   int brightness;
89   int contrast;
90
91   int current_set;
92   /* juggle two sets of seperate Y, U and V
93    * textures */
94   struct pipe_resource *yuv[2][3];
95   struct pipe_sampler_view *yuv_views[2][3];
96};
97
98
99static void
100stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
101{
102   struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
103
104   REGION_EMPTY(pScrn->pScreen, &priv->clip);
105}
106
107static int
108set_port_attribute(ScrnInfoPtr pScrn,
109                   Atom attribute, INT32 value, pointer data)
110{
111   struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
112
113   if (attribute == xvBrightness) {
114      if ((value < -128) || (value > 127))
115         return BadValue;
116      priv->brightness = value;
117   } else if (attribute == xvContrast) {
118      if ((value < 0) || (value > 255))
119         return BadValue;
120      priv->contrast = value;
121   } else
122      return BadMatch;
123
124   return Success;
125}
126
127static int
128get_port_attribute(ScrnInfoPtr pScrn,
129                   Atom attribute, INT32 * value, pointer data)
130{
131   struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
132
133   if (attribute == xvBrightness)
134      *value = priv->brightness;
135   else if (attribute == xvContrast)
136      *value = priv->contrast;
137   else
138      return BadMatch;
139
140   return Success;
141}
142
143static void
144query_best_size(ScrnInfoPtr pScrn,
145                Bool motion,
146                short vid_w, short vid_h,
147                short drw_w, short drw_h,
148                unsigned int *p_w, unsigned int *p_h, pointer data)
149{
150   if (vid_w > (drw_w << 1))
151      drw_w = vid_w >> 1;
152   if (vid_h > (drw_h << 1))
153      drw_h = vid_h >> 1;
154
155   *p_w = drw_w;
156   *p_h = drw_h;
157}
158
159static INLINE struct pipe_resource *
160create_component_texture(struct pipe_context *pipe,
161                         int width, int height)
162{
163   struct pipe_screen *screen = pipe->screen;
164   struct pipe_resource *tex = 0;
165   struct pipe_resource templ;
166
167   memset(&templ, 0, sizeof(templ));
168   templ.target = PIPE_TEXTURE_2D;
169   templ.format = PIPE_FORMAT_L8_UNORM;
170   templ.last_level = 0;
171   templ.width0 = width;
172   templ.height0 = height;
173   templ.depth0 = 1;
174   templ.bind = PIPE_BIND_SAMPLER_VIEW;
175
176   tex = screen->resource_create(screen, &templ);
177
178   return tex;
179}
180
181static int
182check_yuv_textures(struct xorg_xv_port_priv *priv,  int width, int height)
183{
184   struct pipe_resource **dst = priv->yuv[priv->current_set];
185   struct pipe_sampler_view **dst_view = priv->yuv_views[priv->current_set];
186   struct pipe_sampler_view view_templ;
187   struct pipe_context *pipe = priv->r->pipe;
188
189   if (!dst[0] ||
190       dst[0]->width0 != width ||
191       dst[0]->height0 != height) {
192      pipe_resource_reference(&dst[0], NULL);
193      pipe_sampler_view_reference(&dst_view[0], NULL);
194   }
195   if (!dst[1] ||
196       dst[1]->width0 != width ||
197       dst[1]->height0 != height) {
198      pipe_resource_reference(&dst[1], NULL);
199      pipe_sampler_view_reference(&dst_view[1], NULL);
200   }
201   if (!dst[2] ||
202       dst[2]->width0 != width ||
203       dst[2]->height0 != height) {
204      pipe_resource_reference(&dst[2], NULL);
205      pipe_sampler_view_reference(&dst_view[2], NULL);
206   }
207
208   if (!dst[0]) {
209      dst[0] = create_component_texture(priv->r->pipe, width, height);
210      if (dst[0]) {
211         u_sampler_view_default_template(&view_templ,
212                                         dst[0],
213                                         dst[0]->format);
214         dst_view[0] = pipe->create_sampler_view(pipe, dst[0], &view_templ);
215      }
216   }
217
218   if (!dst[1]) {
219      dst[1] = create_component_texture(priv->r->pipe, width, height);
220      if (dst[1]) {
221         u_sampler_view_default_template(&view_templ,
222                                         dst[1],
223                                         dst[1]->format);
224         dst_view[1] = pipe->create_sampler_view(pipe, dst[1], &view_templ);
225      }
226   }
227
228   if (!dst[2]) {
229      dst[2] = create_component_texture(priv->r->pipe, width, height);
230      if (dst[2]) {
231         u_sampler_view_default_template(&view_templ,
232                                      dst[2],
233                                      dst[2]->format);
234         dst_view[2] = pipe->create_sampler_view(pipe, dst[2], &view_templ);
235      }
236   }
237
238   if (!dst[0] || !dst[1] || !dst[2] || !dst_view[0] || !dst_view[1] || !dst_view[2] )
239      return BadAlloc;
240
241   return Success;
242}
243
244static int
245query_image_attributes(ScrnInfoPtr pScrn,
246                       int id,
247                       unsigned short *w, unsigned short *h,
248                       int *pitches, int *offsets)
249{
250   int size, tmp;
251
252   if (*w > IMAGE_MAX_WIDTH)
253      *w = IMAGE_MAX_WIDTH;
254   if (*h > IMAGE_MAX_HEIGHT)
255      *h = IMAGE_MAX_HEIGHT;
256
257   *w = (*w + 1) & ~1;
258   if (offsets)
259      offsets[0] = 0;
260
261   switch (id) {
262   case FOURCC_YV12:
263      *h = (*h + 1) & ~1;
264      size = (*w + 3) & ~3;
265      if (pitches) {
266         pitches[0] = size;
267      }
268      size *= *h;
269      if (offsets) {
270         offsets[1] = size;
271      }
272      tmp = ((*w >> 1) + 3) & ~3;
273      if (pitches) {
274         pitches[1] = pitches[2] = tmp;
275      }
276      tmp *= (*h >> 1);
277      size += tmp;
278      if (offsets) {
279         offsets[2] = size;
280      }
281      size += tmp;
282      break;
283   case FOURCC_UYVY:
284   case FOURCC_YUY2:
285   default:
286      size = *w << 1;
287      if (pitches)
288	 pitches[0] = size;
289      size *= *h;
290      break;
291   }
292
293   return size;
294}
295
296static void
297copy_packed_data(ScrnInfoPtr pScrn,
298                 struct xorg_xv_port_priv *port,
299                 int id,
300                 unsigned char *buf,
301                 int left,
302                 int top,
303                 unsigned short w, unsigned short h)
304{
305   int i, j;
306   struct pipe_resource **dst = port->yuv[port->current_set];
307   struct pipe_transfer *ytrans, *utrans, *vtrans;
308   struct pipe_context *pipe = port->r->pipe;
309   char *ymap, *vmap, *umap;
310   unsigned char y1, y2, u, v;
311   int yidx, uidx, vidx;
312   int y_array_size = w * h;
313
314   ytrans = pipe_get_transfer(pipe, dst[0],
315                                   0, 0, 0,
316                                   PIPE_TRANSFER_WRITE,
317                                   left, top, w, h);
318   utrans = pipe_get_transfer(pipe, dst[1],
319                                   0, 0, 0,
320                                   PIPE_TRANSFER_WRITE,
321                                   left, top, w, h);
322   vtrans = pipe_get_transfer(pipe, dst[2],
323                                   0, 0, 0,
324                                   PIPE_TRANSFER_WRITE,
325                                   left, top, w, h);
326
327   ymap = (char*)pipe->transfer_map(pipe, ytrans);
328   umap = (char*)pipe->transfer_map(pipe, utrans);
329   vmap = (char*)pipe->transfer_map(pipe, vtrans);
330
331   yidx = uidx = vidx = 0;
332
333   switch (id) {
334   case FOURCC_YV12: {
335      int pitches[3], offsets[3];
336      unsigned char *y, *u, *v;
337      query_image_attributes(pScrn, FOURCC_YV12,
338                             &w, &h, pitches, offsets);
339
340      y = buf + offsets[0];
341      v = buf + offsets[1];
342      u = buf + offsets[2];
343      for (i = 0; i < h; ++i) {
344         for (j = 0; j < w; ++j) {
345            int yoffset = (w*i+j);
346            int ii = (i|1), jj = (j|1);
347            int vuoffset = (w/2)*(ii/2) + (jj/2);
348            ymap[yidx++] = y[yoffset];
349            umap[uidx++] = u[vuoffset];
350            vmap[vidx++] = v[vuoffset];
351         }
352      }
353   }
354      break;
355   case FOURCC_UYVY:
356      for (i = 0; i < y_array_size; i +=2 ) {
357         /* extracting two pixels */
358         u  = buf[0];
359         y1 = buf[1];
360         v  = buf[2];
361         y2 = buf[3];
362         buf += 4;
363
364         ymap[yidx++] = y1;
365         ymap[yidx++] = y2;
366         umap[uidx++] = u;
367         umap[uidx++] = u;
368         vmap[vidx++] = v;
369         vmap[vidx++] = v;
370      }
371      break;
372   case FOURCC_YUY2:
373      for (i = 0; i < y_array_size; i +=2 ) {
374         /* extracting two pixels */
375         y1 = buf[0];
376         u  = buf[1];
377         y2 = buf[2];
378         v  = buf[3];
379
380         buf += 4;
381
382         ymap[yidx++] = y1;
383         ymap[yidx++] = y2;
384         umap[uidx++] = u;
385         umap[uidx++] = u;
386         vmap[vidx++] = v;
387         vmap[vidx++] = v;
388      }
389      break;
390   default:
391      debug_assert(!"Unsupported yuv format!");
392      break;
393   }
394
395   pipe->transfer_unmap(pipe, ytrans);
396   pipe->transfer_unmap(pipe, utrans);
397   pipe->transfer_unmap(pipe, vtrans);
398   pipe->transfer_destroy(pipe, ytrans);
399   pipe->transfer_destroy(pipe, utrans);
400   pipe->transfer_destroy(pipe, vtrans);
401}
402
403
404static void
405setup_fs_video_constants(struct xorg_renderer *r, boolean hdtv)
406{
407   const int param_bytes = 12 * sizeof(float);
408   const float *video_constants = (hdtv) ? bt_709 : bt_601;
409
410   renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
411                          video_constants, param_bytes);
412}
413
414static void
415draw_yuv(struct xorg_xv_port_priv *port,
416         float src_x, float src_y, float src_w, float src_h,
417         int dst_x, int dst_y, int dst_w, int dst_h)
418{
419   struct pipe_resource **textures = port->yuv[port->current_set];
420
421   /*debug_printf("  draw_yuv([%d, %d, %d ,%d], [%d, %d, %d, %d])\n",
422                src_x, src_y, src_w, src_h,
423                dst_x, dst_y, dst_w, dst_h);*/
424   renderer_draw_yuv(port->r,
425                     src_x, src_y, src_w, src_h,
426                     dst_x, dst_y, dst_w, dst_h,
427                     textures);
428}
429
430static void
431bind_blend_state(struct xorg_xv_port_priv *port)
432{
433   struct pipe_blend_state blend;
434
435   memset(&blend, 0, sizeof(struct pipe_blend_state));
436   blend.rt[0].blend_enable = 0;
437   blend.rt[0].colormask = PIPE_MASK_RGBA;
438
439   /* porter&duff src */
440   blend.rt[0].rgb_src_factor   = PIPE_BLENDFACTOR_ONE;
441   blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
442   blend.rt[0].rgb_dst_factor   = PIPE_BLENDFACTOR_ZERO;
443   blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
444
445   cso_set_blend(port->r->cso, &blend);
446}
447
448
449static void
450bind_shaders(struct xorg_xv_port_priv *port)
451{
452   unsigned vs_traits = 0, fs_traits = 0;
453   struct xorg_shader shader;
454
455   vs_traits |= VS_YUV;
456   fs_traits |= FS_YUV;
457
458   shader = xorg_shaders_get(port->r->shaders, vs_traits, fs_traits);
459   cso_set_vertex_shader_handle(port->r->cso, shader.vs);
460   cso_set_fragment_shader_handle(port->r->cso, shader.fs);
461}
462
463static INLINE void
464conditional_flush(struct pipe_context *pipe, struct pipe_resource **tex,
465                  int num)
466{
467   int i;
468   for (i = 0; i < num; ++i) {
469      if (tex[i] && pipe->is_resource_referenced(pipe, tex[i], 0, 0) &
470          PIPE_REFERENCED_FOR_WRITE) {
471         pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
472         return;
473      }
474   }
475}
476
477static void
478bind_samplers(struct xorg_xv_port_priv *port)
479{
480   struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
481   struct pipe_sampler_state sampler;
482   struct pipe_resource **dst = port->yuv[port->current_set];
483   struct pipe_sampler_view **dst_views = port->yuv_views[port->current_set];
484
485   memset(&sampler, 0, sizeof(struct pipe_sampler_state));
486
487   conditional_flush(port->r->pipe, dst, 3);
488
489   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
490   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
491   sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
492   sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
493   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
494   sampler.normalized_coords = 1;
495
496   samplers[0] = &sampler;
497   samplers[1] = &sampler;
498   samplers[2] = &sampler;
499
500
501   cso_set_samplers(port->r->cso, 3,
502                    (const struct pipe_sampler_state **)samplers);
503   cso_set_fragment_sampler_views(port->r->cso, 3, dst_views);
504}
505
506static int
507display_video(ScrnInfoPtr pScrn, struct xorg_xv_port_priv *pPriv, int id,
508              RegionPtr dstRegion,
509              int src_x, int src_y, int src_w, int src_h,
510              int dstX, int dstY, int dst_w, int dst_h,
511              PixmapPtr pPixmap)
512{
513   modesettingPtr ms = modesettingPTR(pScrn);
514   BoxPtr pbox;
515   int nbox;
516   int dxo, dyo;
517   Bool hdtv;
518   int x, y, w, h;
519   struct exa_pixmap_priv *dst;
520   struct pipe_surface *dst_surf = NULL;
521
522   exaMoveInPixmap(pPixmap);
523   dst = exaGetPixmapDriverPrivate(pPixmap);
524
525   /*debug_printf("display_video([%d, %d, %d, %d], [%d, %d, %d, %d])\n",
526     src_x, src_y, src_w, src_h, dstX, dstY, dst_w, dst_h);*/
527
528   if (dst && !dst->tex) {
529	xorg_exa_set_shared_usage(pPixmap);
530	pScrn->pScreen->ModifyPixmapHeader(pPixmap, 0, 0, 0, 0, 0, NULL);
531   }
532
533   if (!dst || !dst->tex)
534      XORG_FALLBACK("Xv destination %s", !dst ? "!dst" : "!dst->tex");
535
536   dst_surf = xorg_gpu_surface(pPriv->r->pipe->screen, dst);
537   hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
538
539   REGION_TRANSLATE(pScrn->pScreen, dstRegion, -pPixmap->screen_x,
540                    -pPixmap->screen_y);
541
542   dxo = dstRegion->extents.x1;
543   dyo = dstRegion->extents.y1;
544
545   pbox = REGION_RECTS(dstRegion);
546   nbox = REGION_NUM_RECTS(dstRegion);
547
548   renderer_bind_destination(pPriv->r, dst_surf,
549                             dst_surf->width, dst_surf->height);
550
551   bind_blend_state(pPriv);
552   bind_shaders(pPriv);
553   bind_samplers(pPriv);
554   setup_fs_video_constants(pPriv->r, hdtv);
555
556   DamageDamageRegion(&pPixmap->drawable, dstRegion);
557
558   while (nbox--) {
559      int box_x1 = pbox->x1;
560      int box_y1 = pbox->y1;
561      int box_x2 = pbox->x2;
562      int box_y2 = pbox->y2;
563      float diff_x = (float)src_w / (float)dst_w;
564      float diff_y = (float)src_h / (float)dst_h;
565      float offset_x = box_x1 - dstX + pPixmap->screen_x;
566      float offset_y = box_y1 - dstY + pPixmap->screen_y;
567      float offset_w;
568      float offset_h;
569
570      x = box_x1;
571      y = box_y1;
572      w = box_x2 - box_x1;
573      h = box_y2 - box_y1;
574
575      offset_w = dst_w - w;
576      offset_h = dst_h - h;
577
578      draw_yuv(pPriv,
579               (float) src_x + offset_x*diff_x, (float) src_y + offset_y*diff_y,
580               (float) src_w - offset_w*diff_x, (float) src_h - offset_h*diff_y,
581               x, y, w, h);
582
583      pbox++;
584   }
585   DamageRegionProcessPending(&pPixmap->drawable);
586
587   pipe_surface_reference(&dst_surf, NULL);
588
589   return TRUE;
590}
591
592static int
593put_image(ScrnInfoPtr pScrn,
594          short src_x, short src_y,
595          short drw_x, short drw_y,
596          short src_w, short src_h,
597          short drw_w, short drw_h,
598          int id, unsigned char *buf,
599          short width, short height,
600          Bool sync, RegionPtr clipBoxes, pointer data,
601          DrawablePtr pDraw)
602{
603   struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
604   ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
605   PixmapPtr pPixmap;
606   INT32 x1, x2, y1, y2;
607   BoxRec dstBox;
608   int ret;
609
610   /* Clip */
611   x1 = src_x;
612   x2 = src_x + src_w;
613   y1 = src_y;
614   y2 = src_y + src_h;
615
616   dstBox.x1 = drw_x;
617   dstBox.x2 = drw_x + drw_w;
618   dstBox.y1 = drw_y;
619   dstBox.y2 = drw_y + drw_h;
620
621   if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
622			      width, height))
623      return Success;
624
625   ret = check_yuv_textures(pPriv, width, height);
626
627   if (ret)
628      return ret;
629
630   copy_packed_data(pScrn, pPriv, id, buf,
631                    src_x, src_y, width, height);
632
633   if (pDraw->type == DRAWABLE_WINDOW) {
634      pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
635   } else {
636      pPixmap = (PixmapPtr)pDraw;
637   }
638
639   display_video(pScrn, pPriv, id, clipBoxes,
640                 src_x, src_y, src_w, src_h,
641                 drw_x, drw_y,
642                 drw_w, drw_h, pPixmap);
643
644   pPriv->current_set = (pPriv->current_set + 1) & 1;
645   return Success;
646}
647
648static struct xorg_xv_port_priv *
649port_priv_create(struct xorg_renderer *r)
650{
651   struct xorg_xv_port_priv *priv = NULL;
652
653   priv = calloc(1, sizeof(struct xorg_xv_port_priv));
654
655   if (!priv)
656      return NULL;
657
658   priv->r = r;
659
660   REGION_NULL(pScreen, &priv->clip);
661
662   debug_assert(priv && priv->r);
663
664   return priv;
665}
666
667static XF86VideoAdaptorPtr
668xorg_setup_textured_adapter(ScreenPtr pScreen)
669{
670   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
671   modesettingPtr ms = modesettingPTR(pScrn);
672   XF86VideoAdaptorPtr adapt;
673   XF86AttributePtr attrs;
674   DevUnion *dev_unions;
675   int nports = 16, i;
676   int nattributes;
677
678   nattributes = NUM_TEXTURED_ATTRIBUTES;
679
680   debug_assert(ms->exa);
681   debug_assert(ms->exa->renderer);
682
683   adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
684   dev_unions = calloc(nports, sizeof(DevUnion));
685   attrs = calloc(nattributes, sizeof(XF86AttributeRec));
686   if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
687      free(adapt);
688      free(dev_unions);
689      free(attrs);
690      return NULL;
691   }
692
693   adapt->type = XvWindowMask | XvInputMask | XvImageMask;
694   adapt->flags = 0;
695   adapt->name = "Gallium3D Textured Video";
696   adapt->nEncodings = 1;
697   adapt->pEncodings = DummyEncoding;
698   adapt->nFormats = NUM_FORMATS;
699   adapt->pFormats = Formats;
700   adapt->nPorts = 0;
701   adapt->pPortPrivates = dev_unions;
702   adapt->nAttributes = nattributes;
703   adapt->pAttributes = attrs;
704   memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
705   adapt->nImages = NUM_IMAGES;
706   adapt->pImages = Images;
707   adapt->PutVideo = NULL;
708   adapt->PutStill = NULL;
709   adapt->GetVideo = NULL;
710   adapt->GetStill = NULL;
711   adapt->StopVideo = stop_video;
712   adapt->SetPortAttribute = set_port_attribute;
713   adapt->GetPortAttribute = get_port_attribute;
714   adapt->QueryBestSize = query_best_size;
715   adapt->PutImage = put_image;
716   adapt->QueryImageAttributes = query_image_attributes;
717
718   for (i = 0; i < nports; i++) {
719      struct xorg_xv_port_priv *priv =
720         port_priv_create(ms->exa->renderer);
721
722      adapt->pPortPrivates[i].ptr = (pointer) (priv);
723      adapt->nPorts++;
724   }
725
726   return adapt;
727}
728
729void
730xorg_xv_init(ScreenPtr pScreen)
731{
732   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
733   /*modesettingPtr ms = modesettingPTR(pScrn);*/
734   XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
735   XF86VideoAdaptorPtr textured_adapter;
736   int num_adaptors;
737
738   num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
739   new_adaptors = malloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr *));
740   if (new_adaptors == NULL)
741      return;
742
743   memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
744   adaptors = new_adaptors;
745
746   /* Add the adaptors supported by our hardware.  First, set up the atoms
747    * that will be used by both output adaptors.
748    */
749   xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
750   xvContrast = MAKE_ATOM("XV_CONTRAST");
751
752   textured_adapter = xorg_setup_textured_adapter(pScreen);
753
754   debug_assert(textured_adapter);
755
756   if (textured_adapter) {
757      adaptors[num_adaptors++] = textured_adapter;
758   }
759
760   if (num_adaptors) {
761      xf86XVScreenInit(pScreen, adaptors, num_adaptors);
762   } else {
763      xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
764                 "Disabling Xv because no adaptors could be initialized.\n");
765   }
766
767   free(adaptors);
768}
769